Print any start-up exceptions in Session's constructor, so custom main's don't need to worry about them

This commit is contained in:
Phil Nash 2017-11-02 17:57:52 +00:00
parent 31cc62e6b7
commit fe05062f9e
2 changed files with 28 additions and 14 deletions

View File

@ -116,8 +116,26 @@ namespace Catch {
Session::Session() { Session::Session() {
static bool alreadyInstantiated = false; static bool alreadyInstantiated = false;
if( alreadyInstantiated ) if( alreadyInstantiated ) {
CATCH_INTERNAL_ERROR( "Only one instance of Catch::Session can ever be used" ); try { CATCH_INTERNAL_ERROR( "Only one instance of Catch::Session can ever be used" ); }
catch(...) { getMutableRegistryHub().registerStartupException(); }
}
const auto& exceptions = getRegistryHub().getStartupExceptionRegistry().getExceptions();
if ( !exceptions.empty() ) {
m_startupExceptions = true;
Colour colourGuard( Colour::Red );
Catch::cerr() << "Errors occured during startup!" << '\n';
// iterate over all exceptions and notify user
for ( const auto& ex_ptr : exceptions ) {
try {
std::rethrow_exception(ex_ptr);
} catch ( std::exception const& ex ) {
Catch::cerr() << Column( ex.what() ).indent(2) << '\n';
}
}
}
alreadyInstantiated = true; alreadyInstantiated = true;
m_cli = makeCommandLineParser( m_configData ); m_cli = makeCommandLineParser( m_configData );
} }
@ -140,6 +158,9 @@ namespace Catch {
} }
int Session::applyCommandLine( int argc, char* argv[] ) { int Session::applyCommandLine( int argc, char* argv[] ) {
if( m_startupExceptions )
return 1;
auto result = m_cli.parse( clara::Args( argc, argv ) ); auto result = m_cli.parse( clara::Args( argc, argv ) );
if( !result ) { if( !result ) {
Catch::cerr() Catch::cerr()
@ -165,19 +186,8 @@ namespace Catch {
} }
int Session::run( int argc, char* argv[] ) { int Session::run( int argc, char* argv[] ) {
const auto& exceptions = getRegistryHub().getStartupExceptionRegistry().getExceptions(); if( m_startupExceptions )
if ( !exceptions.empty() ) {
Catch::cerr() << "Errors occured during startup!" << '\n';
// iterate over all exceptions and notify user
for ( const auto& ex_ptr : exceptions ) {
try {
std::rethrow_exception(ex_ptr);
} catch ( std::exception const& ex ) {
Catch::cerr() << ex.what() << '\n';
}
}
return 1; return 1;
}
int returnCode = applyCommandLine( argc, argv ); int returnCode = applyCommandLine( argc, argv );
if( returnCode == 0 ) if( returnCode == 0 )
returnCode = run(); returnCode = run();
@ -236,6 +246,9 @@ namespace Catch {
} }
int Session::runInternal() { int Session::runInternal() {
if( m_startupExceptions )
return 1;
if( m_configData.showHelp || m_configData.libIdentify ) if( m_configData.showHelp || m_configData.libIdentify )
return 0; return 0;

View File

@ -45,6 +45,7 @@ namespace Catch {
clara::Parser m_cli; clara::Parser m_cli;
ConfigData m_configData; ConfigData m_configData;
std::shared_ptr<Config> m_config; std::shared_ptr<Config> m_config;
bool m_startupExceptions = false;
}; };
} // end namespace Catch } // end namespace Catch