diff options
| author | Martin Hořeňovský | 2022-01-01 16:19:31 +0100 |
|---|---|---|
| committer | GitHub | 2022-01-01 16:19:31 +0100 |
| commit | f2f585b4788b61ed202e78b73b105f099d9cce7b (patch) | |
| tree | 174f72ba09e22e564a571f96c21a175a68d85103 /src/catch2/internal | |
| parent | 6b55f5d780c5d8e935720f9d7d8ed28a3ba8617f (diff) | |
| parent | b5547f2ef6be18f70d2e9638d8f1eb0d8d42d2ba (diff) | |
| download | catch3-f2f585b4788b61ed202e78b73b105f099d9cce7b.tar.gz | |
Merge pull request #2183 from mjerabek/dev-multiple-reporters
Add support for multiple reporters
Diffstat (limited to 'src/catch2/internal')
| -rw-r--r-- | src/catch2/internal/catch_commandline.cpp | 76 | ||||
| -rw-r--r-- | src/catch2/internal/catch_console_colour.cpp | 2 | ||||
| -rw-r--r-- | src/catch2/internal/catch_reporter_registry.cpp | 4 | ||||
| -rw-r--r-- | src/catch2/internal/catch_reporter_registry.hpp | 2 | ||||
| -rw-r--r-- | src/catch2/internal/catch_stream.cpp | 3 |
5 files changed, 73 insertions, 14 deletions
diff --git a/src/catch2/internal/catch_commandline.cpp b/src/catch2/internal/catch_commandline.cpp index de27e899..ca0e1c20 100644 --- a/src/catch2/internal/catch_commandline.cpp +++ b/src/catch2/internal/catch_commandline.cpp @@ -14,6 +14,7 @@ #include <catch2/interfaces/catch_interfaces_reporter_registry.hpp> #include <catch2/interfaces/catch_interfaces_reporter.hpp> +#include <algorithm> #include <fstream> #include <string> @@ -133,15 +134,72 @@ namespace Catch { return ParserResult::runtimeError( "Unrecognised verbosity, '" + verbosity + '\'' ); return ParserResult::ok( ParseResultType::Matched ); }; - auto const setReporter = [&]( std::string const& reporter ) { + auto const setReporter = [&]( std::string const& reporterSpec ) { + if ( reporterSpec.empty() ) { + return ParserResult::runtimeError( "Received empty reporter spec." ); + } + IReporterRegistry::FactoryMap const& factories = getRegistryHub().getReporterRegistry().getFactories(); - auto result = factories.find( reporter ); + // clear the default reporter + if (!config._nonDefaultReporterSpecifications) { + config.reporterSpecifications.clear(); + config._nonDefaultReporterSpecifications = true; + } + + + // Exactly one of the reporters may be specified without an output + // file, in which case it defaults to the output specified by "-o" + // (or standard output). + static constexpr auto separator = "::"; + static constexpr size_t separatorSize = 2; + auto fileNameSeparatorPos = reporterSpec.find( separator ); + const bool containsFileName = fileNameSeparatorPos != reporterSpec.npos; + if ( containsFileName ) { + auto nextSeparatorPos = reporterSpec.find( + separator, fileNameSeparatorPos + separatorSize ); + if ( nextSeparatorPos != reporterSpec.npos ) { + return ParserResult::runtimeError( + "Too many separators in reporter spec '" + reporterSpec + '\'' ); + } + } + + std::string reporterName; + Optional<std::string> outputFileName; + reporterName = reporterSpec.substr( 0, fileNameSeparatorPos ); + if ( reporterName.empty() ) { + return ParserResult::runtimeError( "Reporter name cannot be empty." ); + } + + if ( containsFileName ) { + outputFileName = reporterSpec.substr( + fileNameSeparatorPos + separatorSize, reporterSpec.size() ); + } + + auto result = factories.find( reporterName ); + + if( result == factories.end() ) + return ParserResult::runtimeError( "Unrecognized reporter, '" + reporterName + "'. Check available with --list-reporters" ); + if( containsFileName && outputFileName->empty() ) + return ParserResult::runtimeError( "Reporter '" + reporterName + "' has empty filename specified as its output. Supply a filename or remove the colons to use the default output." ); + + config.reporterSpecifications.push_back({ std::move(reporterName), std::move(outputFileName) }); + + // It would be enough to check this only once at the very end, but there is + // not a place where we could call this check, so do it every time it could fail. + // For valid inputs, this is still called at most once. + if (!containsFileName) { + int n_reporters_without_file = 0; + for (auto const& spec : config.reporterSpecifications) { + if (spec.outputFileName.none()) { + n_reporters_without_file++; + } + } + if (n_reporters_without_file > 1) { + return ParserResult::runtimeError( "Only one reporter may have unspecified output file." ); + } + } - if( factories.end() != result ) - config.reporterName = reporter; - else - return ParserResult::runtimeError( "Unrecognized reporter, '" + reporter + "'. Check available with --list-reporters" ); return ParserResult::ok( ParseResultType::Matched ); }; auto const setShardCount = [&]( std::string const& shardCount ) { @@ -202,10 +260,10 @@ namespace Catch { | Opt( config.showInvisibles ) ["-i"]["--invisibles"] ( "show invisibles (tabs, newlines)" ) - | Opt( config.outputFilename, "filename" ) + | Opt( config.defaultOutputFilename, "filename" ) ["-o"]["--out"] - ( "output filename" ) - | Opt( setReporter, "name" ) + ( "default output filename" ) + | Opt( accept_many, setReporter, "name[:output-file]" ) ["-r"]["--reporter"] ( "reporter to use (defaults to console)" ) | Opt( config.name, "name" ) diff --git a/src/catch2/internal/catch_console_colour.cpp b/src/catch2/internal/catch_console_colour.cpp index e524b0ba..069c5fbe 100644 --- a/src/catch2/internal/catch_console_colour.cpp +++ b/src/catch2/internal/catch_console_colour.cpp @@ -159,7 +159,7 @@ namespace { void setColour( const char* _escapeCode ) { // The escape sequence must be flushed to console, otherwise if // stdin and stderr are intermixed, we'd get accidentally coloured output. - getCurrentContext().getConfig()->stream() + getCurrentContext().getConfig()->defaultStream() << '\033' << _escapeCode << std::flush; } }; diff --git a/src/catch2/internal/catch_reporter_registry.cpp b/src/catch2/internal/catch_reporter_registry.cpp index 617c550a..26a328f8 100644 --- a/src/catch2/internal/catch_reporter_registry.cpp +++ b/src/catch2/internal/catch_reporter_registry.cpp @@ -36,11 +36,11 @@ namespace Catch { ReporterRegistry::~ReporterRegistry() = default; - IStreamingReporterPtr ReporterRegistry::create( std::string const& name, IConfig const* config ) const { + IStreamingReporterPtr ReporterRegistry::create( std::string const& name, ReporterConfig const& config ) const { auto it = m_factories.find( name ); if( it == m_factories.end() ) return nullptr; - return it->second->create( ReporterConfig( config ) ); + return it->second->create( config ); } void ReporterRegistry::registerReporter( std::string const& name, IReporterFactoryPtr factory ) { diff --git a/src/catch2/internal/catch_reporter_registry.hpp b/src/catch2/internal/catch_reporter_registry.hpp index 62c2023e..a761e7cf 100644 --- a/src/catch2/internal/catch_reporter_registry.hpp +++ b/src/catch2/internal/catch_reporter_registry.hpp @@ -21,7 +21,7 @@ namespace Catch { ReporterRegistry(); ~ReporterRegistry() override; // = default, out of line to allow fwd decl - IStreamingReporterPtr create( std::string const& name, IConfig const* config ) const override; + IStreamingReporterPtr create( std::string const& name, ReporterConfig const& config ) const override; void registerReporter( std::string const& name, IReporterFactoryPtr factory ); void registerListener( IReporterFactoryPtr factory ); diff --git a/src/catch2/internal/catch_stream.cpp b/src/catch2/internal/catch_stream.cpp index 745c6a2d..eca83dda 100644 --- a/src/catch2/internal/catch_stream.cpp +++ b/src/catch2/internal/catch_stream.cpp @@ -124,8 +124,9 @@ namespace Detail { /////////////////////////////////////////////////////////////////////////// auto makeStream( std::string const& filename ) -> Detail::unique_ptr<IStream const> { - if( filename.empty() ) + if ( filename.empty() || filename == "-" ) { return Detail::make_unique<Detail::CoutStream>(); + } else if( filename[0] == '%' ) { if( filename == "%debug" ) return Detail::make_unique<Detail::DebugOutStream>(); |