Progmasoft Git

catch3

Progmasoft Catch3: an independently maintained C++17 test framework with a parallel runner, property checks, snapshots and structured results.

Clonegit clone https://git.progmasoft.com/catch3

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

Commit
fe05062f9e3b00251b86fb76b3dab9732e1e7787
Author
Phil Nash ·
Parents
31cc62e6b7c7

Files · Download .tar.gz

Changes

 include/internal/catch_session.cpp | 41 +++++++++++++++++++++++++-------------
 include/internal/catch_session.h   |  1 +
 2 files changed, 28 insertions(+), 14 deletions(-)

diff --git a/include/internal/catch_session.cpp b/include/internal/catch_session.cpp
index d799b9ea..5a6a8434 100644
--- a/include/internal/catch_session.cpp
+++ b/include/internal/catch_session.cpp
@@ -116,8 +116,26 @@ namespace Catch {
 
     Session::Session() {
         static bool alreadyInstantiated = false;
-        if( alreadyInstantiated )
-            CATCH_INTERNAL_ERROR( "Only one instance of Catch::Session can ever be used" );
+        if( alreadyInstantiated ) {
+            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;
         m_cli = makeCommandLineParser( m_configData );
     }
@@ -140,6 +158,9 @@ namespace Catch {
     }
 
     int Session::applyCommandLine( int argc, char* argv[] ) {
+        if( m_startupExceptions )
+            return 1;
+
         auto result = m_cli.parse( clara::Args( argc, argv ) );
         if( !result ) {
             Catch::cerr()
@@ -165,19 +186,8 @@ namespace Catch {
     }
 
     int Session::run( int argc, char* argv[] ) {
-        const auto& exceptions = getRegistryHub().getStartupExceptionRegistry().getExceptions();
-        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';
-                }
-            }
+        if( m_startupExceptions )
             return 1;
-        }
         int returnCode = applyCommandLine( argc, argv );
         if( returnCode == 0 )
             returnCode = run();
@@ -236,6 +246,9 @@ namespace Catch {
     }
 
     int Session::runInternal() {
+        if( m_startupExceptions )
+            return 1;
+
         if( m_configData.showHelp || m_configData.libIdentify )
             return 0;
 
diff --git a/include/internal/catch_session.h b/include/internal/catch_session.h
index dca8a051..b814aa6a 100644
--- a/include/internal/catch_session.h
+++ b/include/internal/catch_session.h
@@ -45,6 +45,7 @@ namespace Catch {
         clara::Parser m_cli;
         ConfigData m_configData;
         std::shared_ptr<Config> m_config;
+        bool m_startupExceptions = false;
     };
 
 } // end namespace Catch