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

summaryrefslogtreecommitdiffhomepage
path: root/src/catch2/internal/catch_commandline.cpp
diff options
context:
space:
mode:
authorMartin Hořeňovský2022-01-01 16:19:31 +0100
committerGitHub2022-01-01 16:19:31 +0100
commitf2f585b4788b61ed202e78b73b105f099d9cce7b (patch)
tree174f72ba09e22e564a571f96c21a175a68d85103 /src/catch2/internal/catch_commandline.cpp
parent6b55f5d780c5d8e935720f9d7d8ed28a3ba8617f (diff)
parentb5547f2ef6be18f70d2e9638d8f1eb0d8d42d2ba (diff)
downloadcatch3-f2f585b4788b61ed202e78b73b105f099d9cce7b.tar.gz
Merge pull request #2183 from mjerabek/dev-multiple-reporters
Add support for multiple reporters
Diffstat (limited to 'src/catch2/internal/catch_commandline.cpp')
-rw-r--r--src/catch2/internal/catch_commandline.cpp76
1 files changed, 67 insertions, 9 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" )