Skip to content

Commit a14d6d5

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, as a labelled list, e.g. Filters: "foo" Path filters: - Section: "A" - Generator: "0" The filters are listed one per line, in the order they were specified. This makes it obvious what has actually been selected, which is especially helpful when a filter combination ends up running no assertions. Related to #3099
1 parent 675f9ea commit a14d6d5

24 files changed

Lines changed: 438 additions & 18 deletions

docs/filtering-execution-path.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,22 @@ 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+
```text
27+
Filters: "foo"
28+
Path filters:
29+
- Section: "A"
30+
- Generator: "0"
31+
```
32+
33+
The filters are listed one per line, labelled as `Section` or `Generator`,
34+
in the order in which they were specified. This makes it easy to see exactly
35+
what has been selected when a filter combination ends up running no
36+
assertions.
37+
2238
Both the new and old filter behaviours include some potentially surprising
2339
things:
2440
* 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:\n"
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:\n"
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: 23 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,28 @@ 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 << '\n';
111+
}
112+
first = false;
113+
rss << " - ";
114+
switch ( filter.type ) {
115+
case PathFilter::For::Section:
116+
rss << "Section: ";
117+
break;
118+
case PathFilter::For::Generator:
119+
rss << "Generator: ";
120+
break;
121+
}
122+
rss << '"' << filter.filter << '"';
123+
}
124+
return rss.str();
125+
}
126+
104127
std::ostream& operator<<( std::ostream& out, lineOfChars value ) {
105128
for ( size_t idx = 0; idx < CATCH_CONFIG_CONSOLE_WIDTH - 1; ++idx ) {
106129
out.put( value.c );

src/catch2/reporters/catch_reporter_helpers.hpp

Lines changed: 13 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,18 @@ 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
36+
* human-readable, indented list, one filter per line and in the order
37+
* in which they were specified, e.g.
38+
*
39+
* - Section: "A"
40+
* - Generator: "0"
41+
*
42+
* The returned string has no trailing newline.
43+
*/
44+
std::string serializePathFilters( std::vector<PathFilter> const& filters );
45+
3346
struct lineOfChars {
3447
char c;
3548
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+
# The exact serialization is unit-tested in `serializePathFilters ...`, so
672+
# here we only check that both reporters actually emit the line, using a
673+
# section filter for one and a generator filter for the other.
674+
add_test(NAME "Reporters:PathFilters:Section:compact"
675+
COMMAND
676+
$<TARGET_FILE:SelfTest> "Tracker"
677+
--reporter compact
678+
--section "section name"
679+
)
680+
set_tests_properties("Reporters:PathFilters:Section:compact"
681+
PROPERTIES
682+
PASS_REGULAR_EXPRESSION "- Section: \"section name\""
683+
)
684+
685+
add_test(NAME "Reporters:PathFilters:Generator:console"
686+
COMMAND
687+
$<TARGET_FILE:SelfTest> "Generators internals"
688+
--reporter console
689+
--generator-index 0
690+
)
691+
set_tests_properties("Reporters:PathFilters:Generator:console"
692+
PROPERTIES
693+
PASS_REGULAR_EXPRESSION "- Generator: \"0\""
694+
)
695+
669696
add_test(NAME "Bazel::RngSeedEnvVar::JustEnv"
670697
COMMAND
671698
$<TARGET_FILE:SelfTest> "Factorials are computed"

tests/SelfTest/Baselines/automake.sw.approved.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ b1!
409409
:test-result: SKIP sections can be skipped dynamically at runtime
410410
:test-result: FAIL send a single char to INFO
411411
:test-result: FAIL sends information to INFO
412+
:test-result: PASS serializePathFilters serializes the section/generator selection
412413
:test-result: PASS shortened hide tags are split apart
413414
:test-result: SKIP skipped tests can optionally provide a reason
414415
:test-result: PASS splitString

tests/SelfTest/Baselines/automake.sw.multi.approved.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@
398398
:test-result: SKIP sections can be skipped dynamically at runtime
399399
:test-result: FAIL send a single char to INFO
400400
:test-result: FAIL sends information to INFO
401+
:test-result: PASS serializePathFilters serializes the section/generator selection
401402
:test-result: PASS shortened hide tags are split apart
402403
:test-result: SKIP skipped tests can optionally provide a reason
403404
:test-result: PASS splitString

tests/SelfTest/Baselines/compact.sw.approved.txt

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2831,6 +2831,20 @@ Skip.tests.cpp:<line number>: skipped:
28312831
Skip.tests.cpp:<line number>: passed:
28322832
Misc.tests.cpp:<line number>: failed: false with 1 message: '3'
28332833
Message.tests.cpp:<line number>: failed: false with 2 messages: 'hi' and 'i := 7'
2834+
Reporters.tests.cpp:<line number>: passed: serializePathFilters( {} ).empty() for: true
2835+
Reporters.tests.cpp:<line number>: passed: serializePathFilters( filters ) == " - Section: \"a section\"" for: " - Section: "a section""
2836+
==
2837+
" - Section: "a section""
2838+
Reporters.tests.cpp:<line number>: passed: serializePathFilters( filters ) == " - Generator: \"0\"" for: " - Generator: "0""
2839+
==
2840+
" - Generator: "0""
2841+
Reporters.tests.cpp:<line number>: passed: serializePathFilters( filters ) == " - Section: \"A\"\n" " - Generator: \"1\"\n" " - Section: \"B\"" for: " - Section: "A"
2842+
- Generator: "1"
2843+
- Section: "B""
2844+
==
2845+
" - Section: "A"
2846+
- Generator: "1"
2847+
- Section: "B""
28342848
Tag.tests.cpp:<line number>: passed: testcase.tags, VectorContains( Tag( "magic-tag" ) ) && VectorContains( Tag( "."_catch_sr ) ) for: { {?}, {?} } ( Contains: {?} and Contains: {?} )
28352849
Skip.tests.cpp:<line number>: skipped: 'skipping because answer = 43'
28362850
StringManip.tests.cpp:<line number>: passed: splitStringRef("", ','), Equals(std::vector<StringRef>()) for: { } Equals: { }
@@ -3000,7 +3014,7 @@ InternalBenchmark.tests.cpp:<line number>: passed: med == 18. for: 18.0 == 18.0
30003014
InternalBenchmark.tests.cpp:<line number>: passed: q3 == 23. for: 23.0 == 23.0
30013015
Misc.tests.cpp:<line number>: passed:
30023016
Misc.tests.cpp:<line number>: passed:
3003-
test cases: 451 | 331 passed | 96 failed | 6 skipped | 18 failed as expected
3004-
assertions: 2416 | 2215 passed | 158 failed | 43 failed as expected
3017+
test cases: 452 | 332 passed | 96 failed | 6 skipped | 18 failed as expected
3018+
assertions: 2420 | 2219 passed | 158 failed | 43 failed as expected
30053019

30063020

tests/SelfTest/Baselines/compact.sw.multi.approved.txt

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2820,6 +2820,20 @@ Skip.tests.cpp:<line number>: skipped:
28202820
Skip.tests.cpp:<line number>: passed:
28212821
Misc.tests.cpp:<line number>: failed: false with 1 message: '3'
28222822
Message.tests.cpp:<line number>: failed: false with 2 messages: 'hi' and 'i := 7'
2823+
Reporters.tests.cpp:<line number>: passed: serializePathFilters( {} ).empty() for: true
2824+
Reporters.tests.cpp:<line number>: passed: serializePathFilters( filters ) == " - Section: \"a section\"" for: " - Section: "a section""
2825+
==
2826+
" - Section: "a section""
2827+
Reporters.tests.cpp:<line number>: passed: serializePathFilters( filters ) == " - Generator: \"0\"" for: " - Generator: "0""
2828+
==
2829+
" - Generator: "0""
2830+
Reporters.tests.cpp:<line number>: passed: serializePathFilters( filters ) == " - Section: \"A\"\n" " - Generator: \"1\"\n" " - Section: \"B\"" for: " - Section: "A"
2831+
- Generator: "1"
2832+
- Section: "B""
2833+
==
2834+
" - Section: "A"
2835+
- Generator: "1"
2836+
- Section: "B""
28232837
Tag.tests.cpp:<line number>: passed: testcase.tags, VectorContains( Tag( "magic-tag" ) ) && VectorContains( Tag( "."_catch_sr ) ) for: { {?}, {?} } ( Contains: {?} and Contains: {?} )
28242838
Skip.tests.cpp:<line number>: skipped: 'skipping because answer = 43'
28252839
StringManip.tests.cpp:<line number>: passed: splitStringRef("", ','), Equals(std::vector<StringRef>()) for: { } Equals: { }
@@ -2989,7 +3003,7 @@ InternalBenchmark.tests.cpp:<line number>: passed: med == 18. for: 18.0 == 18.0
29893003
InternalBenchmark.tests.cpp:<line number>: passed: q3 == 23. for: 23.0 == 23.0
29903004
Misc.tests.cpp:<line number>: passed:
29913005
Misc.tests.cpp:<line number>: passed:
2992-
test cases: 451 | 331 passed | 96 failed | 6 skipped | 18 failed as expected
2993-
assertions: 2416 | 2215 passed | 158 failed | 43 failed as expected
3006+
test cases: 452 | 332 passed | 96 failed | 6 skipped | 18 failed as expected
3007+
assertions: 2420 | 2219 passed | 158 failed | 43 failed as expected
29943008

29953009

0 commit comments

Comments
 (0)