Skip to content

Commit 1cfc1b5

Browse files
Print the active section/generator selection
When a section/generator selection (`-c`, `-g`, or `-p`) is in effect, the console and compact reporters now print the active path filters next to the existing `Filters:` line, e.g. Filters: "foo" Path filters: c:"A" g:0 Section filters are serialized as `c:"<filter>"` and generator filters as `g:<filter>`, mirroring the `-p`/`--path-filter` syntax. This makes it obvious what has actually been selected, which is especially helpful when a filter combination ends up running no assertions. Closes #3099
1 parent 675f9ea commit 1cfc1b5

6 files changed

Lines changed: 84 additions & 0 deletions

File tree

docs/filtering-execution-path.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@ behaviour, which does not affect generators at all. If you also use either
1919
`-g`/`--generator-index`, or `-p`/`--path-filter`, you will get the new
2020
behaviour, which can also filter generator elements.
2121

22+
When path filters are in effect, the console and compact reporters print
23+
the active selection at the start of the run, next to the `Filters:` line,
24+
e.g.
25+
26+
```
27+
Filters: "foo"
28+
Path filters: c:"A" g:0
29+
```
30+
31+
Section filters are shown as `c:"<filter>"` and generator filters as
32+
`g:<filter>`, mirroring the `-p`/`--path-filter` syntax. This makes it
33+
easy to see exactly what has been selected when a filter combination ends
34+
up running no assertions.
35+
2236
Both the new and old filter behaviours include some potentially surprising
2337
things:
2438
* Code outside of sections being skipped will still be executed. E.g.

src/catch2/reporters/catch_reporter_compact.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,12 @@ class AssertionPrinter {
215215
<< m_config->testSpec()
216216
<< '\n';
217217
}
218+
if ( !m_config->getPathFilters().empty() ) {
219+
m_stream << m_colour->guardColour( Colour::BrightYellow )
220+
<< "Path filters: "
221+
<< serializePathFilters( m_config->getPathFilters() )
222+
<< '\n';
223+
}
218224
m_stream << "RNG seed: " << getSeed() << '\n'
219225
<< std::flush;
220226
}

src/catch2/reporters/catch_reporter_console.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,11 @@ void ConsoleReporter::testRunStarting(TestRunInfo const& _testRunInfo) {
530530
m_stream << m_colour->guardColour( Colour::BrightYellow ) << "Filters: "
531531
<< m_config->testSpec() << '\n';
532532
}
533+
if ( !m_config->getPathFilters().empty() ) {
534+
m_stream << m_colour->guardColour( Colour::BrightYellow )
535+
<< "Path filters: "
536+
<< serializePathFilters( m_config->getPathFilters() ) << '\n';
537+
}
533538
m_stream << "Randomness seeded to: " << getSeed() << '\n'
534539
<< std::flush;
535540
}

src/catch2/reporters/catch_reporter_helpers.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <catch2/reporters/catch_reporter_helpers.hpp>
1010
#include <catch2/interfaces/catch_interfaces_config.hpp>
1111
#include <catch2/internal/catch_console_width.hpp>
12+
#include <catch2/internal/catch_path_filter.hpp>
1213
#include <catch2/internal/catch_errno_guard.hpp>
1314
#include <catch2/internal/catch_textflow.hpp>
1415
#include <catch2/internal/catch_reusable_string_stream.hpp>
@@ -101,6 +102,26 @@ namespace Catch {
101102
return serialized;
102103
}
103104

105+
std::string serializePathFilters( std::vector<PathFilter> const& filters ) {
106+
ReusableStringStream rss;
107+
bool first = true;
108+
for ( auto const& filter : filters ) {
109+
if ( !first ) {
110+
rss << ' ';
111+
}
112+
first = false;
113+
switch ( filter.type ) {
114+
case PathFilter::For::Section:
115+
rss << "c:\"" << filter.filter << '"';
116+
break;
117+
case PathFilter::For::Generator:
118+
rss << "g:" << filter.filter;
119+
break;
120+
}
121+
}
122+
return rss.str();
123+
}
124+
104125
std::ostream& operator<<( std::ostream& out, lineOfChars value ) {
105126
for ( size_t idx = 0; idx < CATCH_CONFIG_CONSOLE_WIDTH - 1; ++idx ) {
106127
out.put( value.c );

src/catch2/reporters/catch_reporter_helpers.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ namespace Catch {
2121
class IConfig;
2222
class TestCaseHandle;
2323
class ColourImpl;
24+
struct PathFilter;
2425

2526
// Returns double formatted as %.3f (format expected on output)
2627
std::string getFormattedDuration( double duration );
@@ -30,6 +31,16 @@ namespace Catch {
3031

3132
std::string serializeFilters( std::vector<std::string> const& filters );
3233

34+
/**
35+
* Serializes the active section/generator path filters into a string
36+
* that resembles the `-p`/`--path-filter` command line syntax.
37+
*
38+
* Section filters are serialized as `c:"<filter>"`, generator filters
39+
* as `g:<filter>`. The filters are separated by a single space and kept
40+
* in the order in which they were specified.
41+
*/
42+
std::string serializePathFilters( std::vector<PathFilter> const& filters );
43+
3344
struct lineOfChars {
3445
char c;
3546
constexpr lineOfChars( char c_ ): c( c_ ) {}

tests/CMakeLists.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,33 @@ foreach(reporterName # "Automake" - the simple .trs format does not support any
666666
)
667667
endforeach()
668668

669+
# The console and compact reporters print the active section/generator
670+
# selection (path filters), so that it is obvious what has been selected.
671+
foreach(reporterName "compact"
672+
"console")
673+
add_test(NAME "Reporters:PathFilters:Section:${reporterName}"
674+
COMMAND
675+
$<TARGET_FILE:SelfTest> "Tracker"
676+
--reporter ${reporterName}
677+
--section "section name"
678+
)
679+
set_tests_properties("Reporters:PathFilters:Section:${reporterName}"
680+
PROPERTIES
681+
PASS_REGULAR_EXPRESSION "Path filters: c:\"section name\""
682+
)
683+
684+
add_test(NAME "Reporters:PathFilters:Generator:${reporterName}"
685+
COMMAND
686+
$<TARGET_FILE:SelfTest> "Generators internals"
687+
--reporter ${reporterName}
688+
--generator-index 0
689+
)
690+
set_tests_properties("Reporters:PathFilters:Generator:${reporterName}"
691+
PROPERTIES
692+
PASS_REGULAR_EXPRESSION "Path filters: g:0"
693+
)
694+
endforeach()
695+
669696
add_test(NAME "Bazel::RngSeedEnvVar::JustEnv"
670697
COMMAND
671698
$<TARGET_FILE:SelfTest> "Factorials are computed"

0 commit comments

Comments
 (0)