Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/src/catch2/internal
diff options
context:
space:
mode:
authorMartin Jeřábek2021-02-06 20:12:07 +0100
committerMartin Hořeňovský2022-01-01 14:02:23 +0100
commitccd67b293d7595a0cf2dc6c4bfb2017fc18c1f14 (patch)
treeba5f166d1cf867ad97617656083354fbddfc5d9f /src/catch2/internal
parent6b55f5d780c5d8e935720f9d7d8ed28a3ba8617f (diff)
downloadcatch3-ccd67b293d7595a0cf2dc6c4bfb2017fc18c1f14.tar.gz
Add support for multiple parallel reporters
This requires a bunch of different changes across the reporter subsystem. * We need to handle multiple reporters and their differing preferences in `ListeningReporter`, e.g. what to do when we mix reporters that capture and don't capture stdout. * We need to change how the reporter is given output and how we parse reporter's output destination from CLI. * Approval tests need to handle multireporter option
Diffstat (limited to 'src/catch2/internal')
-rw-r--r--src/catch2/internal/catch_commandline.cpp60
-rw-r--r--src/catch2/internal/catch_console_colour.cpp2
-rw-r--r--src/catch2/internal/catch_reporter_registry.cpp4
-rw-r--r--src/catch2/internal/catch_reporter_registry.hpp2
4 files changed, 55 insertions, 13 deletions
diff --git a/src/catch2/internal/catch_commandline.cpp b/src/catch2/internal/catch_commandline.cpp
index de27e899..4ce085ca 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,56 @@ 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 ) {
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 separatorPos = reporterSpec.find( separator );
+ const bool containsFileName = separatorPos != reporterSpec.npos;
+ std::string reporterName;
+ Optional<std::string> outputFileName;
+ if (!containsFileName) {
+ reporterName = reporterSpec;
+ } else {
+ reporterName = reporterSpec.substr( 0, separatorPos );
+ outputFileName = reporterSpec.substr(
+ separatorPos + 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 +244,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 );