mirror of
https://github.com/catchorg/Catch2.git
synced 2024-11-25 14:56:10 +01:00
Improve error handling in parsing reporter specs
This commit is contained in:
parent
899554bff2
commit
fae0be25b3
@ -135,6 +135,10 @@ namespace Catch {
|
|||||||
return ParserResult::ok( ParseResultType::Matched );
|
return ParserResult::ok( ParseResultType::Matched );
|
||||||
};
|
};
|
||||||
auto const setReporter = [&]( std::string const& reporterSpec ) {
|
auto const setReporter = [&]( std::string const& reporterSpec ) {
|
||||||
|
if ( reporterSpec.empty() ) {
|
||||||
|
return ParserResult::runtimeError( "Received empty reporter spec." );
|
||||||
|
}
|
||||||
|
|
||||||
IReporterRegistry::FactoryMap const& factories = getRegistryHub().getReporterRegistry().getFactories();
|
IReporterRegistry::FactoryMap const& factories = getRegistryHub().getReporterRegistry().getFactories();
|
||||||
|
|
||||||
// clear the default reporter
|
// clear the default reporter
|
||||||
@ -143,21 +147,33 @@ namespace Catch {
|
|||||||
config._nonDefaultReporterSpecifications = true;
|
config._nonDefaultReporterSpecifications = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Exactly one of the reporters may be specified without an output
|
// Exactly one of the reporters may be specified without an output
|
||||||
// file, in which case it defaults to the output specified by "-o"
|
// file, in which case it defaults to the output specified by "-o"
|
||||||
// (or standard output).
|
// (or standard output).
|
||||||
static constexpr auto separator = "::";
|
static constexpr auto separator = "::";
|
||||||
static constexpr size_t separatorSize = 2;
|
static constexpr size_t separatorSize = 2;
|
||||||
auto separatorPos = reporterSpec.find( separator );
|
auto fileNameSeparatorPos = reporterSpec.find( separator );
|
||||||
const bool containsFileName = separatorPos != reporterSpec.npos;
|
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;
|
std::string reporterName;
|
||||||
Optional<std::string> outputFileName;
|
Optional<std::string> outputFileName;
|
||||||
if (!containsFileName) {
|
reporterName = reporterSpec.substr( 0, fileNameSeparatorPos );
|
||||||
reporterName = reporterSpec;
|
if ( reporterName.empty() ) {
|
||||||
} else {
|
return ParserResult::runtimeError( "Reporter name cannot be empty." );
|
||||||
reporterName = reporterSpec.substr( 0, separatorPos );
|
}
|
||||||
|
|
||||||
|
if ( containsFileName ) {
|
||||||
outputFileName = reporterSpec.substr(
|
outputFileName = reporterSpec.substr(
|
||||||
separatorPos + separatorSize, reporterSpec.size() );
|
fileNameSeparatorPos + separatorSize, reporterSpec.size() );
|
||||||
}
|
}
|
||||||
|
|
||||||
auto result = factories.find( reporterName );
|
auto result = factories.find( reporterName );
|
||||||
|
@ -682,3 +682,20 @@ TEST_CASE("Test with special, characters \"in name", "[cli][regression]") {
|
|||||||
// This test case succeeds if we can invoke it from the CLI
|
// This test case succeeds if we can invoke it from the CLI
|
||||||
SUCCEED();
|
SUCCEED();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Various suspicious reporter specs are rejected",
|
||||||
|
"[cli][reporter-spec][approvals]") {
|
||||||
|
Catch::ConfigData config;
|
||||||
|
auto cli = Catch::makeCommandLineParser( config );
|
||||||
|
|
||||||
|
auto spec = GENERATE( as<std::string>{},
|
||||||
|
"",
|
||||||
|
"::console",
|
||||||
|
"console::",
|
||||||
|
"console::some-file::",
|
||||||
|
"::console::some-file::" );
|
||||||
|
CAPTURE( spec );
|
||||||
|
|
||||||
|
auto result = cli.parse( { "test", "--reporter", spec } );
|
||||||
|
REQUIRE_FALSE( result );
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user