Skip to content

Commit a0eba50

Browse files
committed
Make verbosity per-reporter
1 parent 8b08d4d commit a0eba50

23 files changed

Lines changed: 182 additions & 97 deletions

docs/command-line.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,13 @@ as many times as you want, e.g. `--reporter xml::out=someFile.xml` or
202202

203203
The keys must either be prefixed by "X", in which case they are not parsed
204204
by Catch2 and are only passed down to the reporter, or one of options
205-
hardcoded into Catch2. Currently there are only 2,
206-
["out"](#sending-output-to-a-file), and ["colour-mode"](#colour-mode).
205+
hardcoded into Catch2. Currently there are 3 supported options:
206+
207+
* ["out"](#sending-output-to-a-file)
208+
* ["colour-mode"](#colour-mode)
209+
* ["verbosity"](#output-verbosity)
210+
211+
> Support for per-reporter verbosity option was added in Catch2 vX.Y.Z
207212
208213
_Note that the reporter might still check the X-prefixed options for
209214
validity, and throw an error if they are wrong._

docs/reporters.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ them write into different destinations. The two main uses of this are
4343

4444
Specifying multiple reporter looks like this:
4545
```
46-
--reporter JUnit::out=result-junit.xml --reporter console::out=-::colour-mode=ansi
46+
--reporter JUnit::out=result-junit.xml --reporter console::out=-::colour-mode=ansi::verbosity=quiet
4747
```
4848

4949
This tells Catch2 to use two reporters, `JUnit` reporter that writes
5050
its machine-readable XML output to file `result-junit.xml`, and the
51-
`console` reporter that writes its user-friendly output to stdout and
52-
uses ANSI colour codes for colouring the output.
51+
`console` reporter that writes its user-friendly output to stdout, uses
52+
ANSI colour codes for colouring the output and is set to "quiet" verbosity.
5353

5454
Using multiple reporters (or one reporter and one-or-more [event
5555
listeners](event-listeners.md#top)) can have surprisingly complex semantics
@@ -110,7 +110,7 @@ passing and failing assertions.
110110

111111
_Generally we recommend that if you override a member function from either
112112
of the bases, you call into the base's implementation first. This is not
113-
necessarily in all cases, but it is safer and easier._
113+
necessary in all cases, but it is safer and easier._
114114

115115

116116
Writing your own reporter then looks like this:

src/catch2/catch_config.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ namespace Catch {
8989
return lhs.name == rhs.name &&
9090
lhs.outputFilename == rhs.outputFilename &&
9191
lhs.colourMode == rhs.colourMode &&
92+
lhs.verbosity == rhs.verbosity &&
9293
lhs.customOptions == rhs.customOptions;
9394
}
9495

@@ -157,6 +158,7 @@ namespace Catch {
157158
reporterSpec.outputFile() ? *reporterSpec.outputFile()
158159
: data.defaultOutputFilename,
159160
reporterSpec.colourMode().valueOr( data.defaultColourMode ),
161+
reporterSpec.verbosity().valueOr( data.verbosity ),
160162
reporterSpec.customOptions() } );
161163
}
162164
}
@@ -232,7 +234,7 @@ namespace Catch {
232234

233235
if ( bazelOutputFile ) {
234236
m_data.reporterSpecifications.push_back(
235-
{ "junit", std::string( bazelOutputFile ), {}, {} } );
237+
{ "junit", std::string( bazelOutputFile ), {}, {}, {} } );
236238
}
237239

238240
const auto bazelTestSpec = Detail::getEnv( "TESTBRIDGE_TEST_ONLY" );

src/catch2/catch_config.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ namespace Catch {
3535
std::string name;
3636
std::string outputFilename;
3737
ColourMode colourMode;
38+
Verbosity verbosity;
3839
std::map<std::string, std::string> customOptions;
3940
friend bool operator==( ProcessedReporterSpec const& lhs,
4041
ProcessedReporterSpec const& rhs );

src/catch2/catch_session.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ namespace Catch {
5252
ReporterConfig( config,
5353
makeStream( spec.outputFilename ),
5454
spec.colourMode,
55+
spec.verbosity,
5556
spec.customOptions ) );
5657
}
5758

@@ -68,6 +69,7 @@ namespace Catch {
6869
ReporterConfig( config,
6970
makeStream( reporterSpec.outputFilename ),
7071
reporterSpec.colourMode,
72+
reporterSpec.verbosity,
7173
reporterSpec.customOptions ) ) );
7274
}
7375

src/catch2/interfaces/catch_interfaces_reporter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ namespace Catch {
1919
IConfig const* _fullConfig,
2020
Detail::unique_ptr<IStream> _stream,
2121
ColourMode colourMode,
22+
Verbosity verbosity,
2223
std::map<std::string, std::string> customOptions ):
2324
m_stream( CATCH_MOVE(_stream) ),
2425
m_fullConfig( _fullConfig ),
2526
m_colourMode( colourMode ),
27+
m_verbosity( verbosity ),
2628
m_customOptions( CATCH_MOVE( customOptions ) ) {}
2729

2830
Detail::unique_ptr<IStream> ReporterConfig::takeStream() && {
@@ -31,6 +33,7 @@ namespace Catch {
3133
}
3234
IConfig const * ReporterConfig::fullConfig() const { return m_fullConfig; }
3335
ColourMode ReporterConfig::colourMode() const { return m_colourMode; }
36+
Verbosity ReporterConfig::verbosity() const { return m_verbosity; }
3437

3538
std::map<std::string, std::string> const&
3639
ReporterConfig::customOptions() const {

src/catch2/interfaces/catch_interfaces_reporter.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <catch2/catch_test_run_info.hpp>
1313
#include <catch2/catch_totals.hpp>
1414
#include <catch2/catch_assertion_result.hpp>
15+
#include <catch2/interfaces/catch_interfaces_config.hpp>
1516
#include <catch2/internal/catch_message_info.hpp>
1617
#include <catch2/internal/catch_stringref.hpp>
1718
#include <catch2/internal/catch_unique_ptr.hpp>
@@ -36,6 +37,7 @@ namespace Catch {
3637
ReporterConfig( IConfig const* _fullConfig,
3738
Detail::unique_ptr<IStream> _stream,
3839
ColourMode colourMode,
40+
Verbosity verbosity,
3941
std::map<std::string, std::string> customOptions );
4042

4143
ReporterConfig( ReporterConfig&& ) = default;
@@ -45,12 +47,14 @@ namespace Catch {
4547
Detail::unique_ptr<IStream> takeStream() &&;
4648
IConfig const* fullConfig() const;
4749
ColourMode colourMode() const;
50+
Verbosity verbosity() const;
4851
std::map<std::string, std::string> const& customOptions() const;
4952

5053
private:
5154
Detail::unique_ptr<IStream> m_stream;
5255
IConfig const* m_fullConfig;
5356
ColourMode m_colourMode;
57+
Verbosity m_verbosity;
5458
std::map<std::string, std::string> m_customOptions;
5559
};
5660

src/catch2/internal/catch_reporter_spec_parser.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,26 @@ namespace Catch {
9393
return {};
9494
}
9595
}
96+
97+
Optional<Verbosity> stringToVerbosity( StringRef verbosity ) {
98+
if (verbosity == "quiet") { return Verbosity::Quiet;
99+
} else if ( verbosity == "normal" ) {
100+
return Verbosity::Normal;
101+
} else if ( verbosity == "high" ) {
102+
return Verbosity::High;
103+
} else {
104+
return {};
105+
}
106+
}
107+
96108
} // namespace Detail
97109

98110

99111
bool operator==( ReporterSpec const& lhs, ReporterSpec const& rhs ) {
100112
return lhs.m_name == rhs.m_name &&
101113
lhs.m_outputFileName == rhs.m_outputFileName &&
102114
lhs.m_colourMode == rhs.m_colourMode &&
115+
lhs.m_verbosity == rhs.m_verbosity &&
103116
lhs.m_customOptions == rhs.m_customOptions;
104117
}
105118

@@ -111,6 +124,7 @@ namespace Catch {
111124
std::map<std::string, std::string> kvPairs;
112125
Optional<std::string> outputFileName;
113126
Optional<ColourMode> colourMode;
127+
Optional<Verbosity> verbosity;
114128

115129
// First part is always reporter name, so we skip it
116130
for ( size_t i = 1; i < parts.size(); ++i ) {
@@ -148,6 +162,12 @@ namespace Catch {
148162
if ( !colourMode ) {
149163
return {};
150164
}
165+
} else if ( key == "verbosity" ) {
166+
// Duplicated key
167+
if ( verbosity ) { return {}; }
168+
verbosity = Detail::stringToVerbosity( value );
169+
// Parsing failed
170+
if ( !verbosity ) { return {}; }
151171
} else {
152172
// Unrecognized option
153173
return {};
@@ -157,17 +177,20 @@ namespace Catch {
157177
return ReporterSpec{ CATCH_MOVE( parts[0] ),
158178
CATCH_MOVE( outputFileName ),
159179
CATCH_MOVE( colourMode ),
180+
CATCH_MOVE( verbosity),
160181
CATCH_MOVE( kvPairs ) };
161182
}
162183

163184
ReporterSpec::ReporterSpec(
164185
std::string name,
165186
Optional<std::string> outputFileName,
166187
Optional<ColourMode> colourMode,
188+
Optional<Verbosity> verbosity,
167189
std::map<std::string, std::string> customOptions ):
168190
m_name( CATCH_MOVE( name ) ),
169191
m_outputFileName( CATCH_MOVE( outputFileName ) ),
170192
m_colourMode( CATCH_MOVE( colourMode ) ),
193+
m_verbosity( CATCH_MOVE( verbosity ) ),
171194
m_customOptions( CATCH_MOVE( customOptions ) ) {}
172195

173196
} // namespace Catch

src/catch2/internal/catch_reporter_spec_parser.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ namespace Catch {
2525
std::vector<std::string> splitReporterSpec( StringRef reporterSpec );
2626

2727
Optional<ColourMode> stringToColourMode( StringRef colourMode );
28+
Optional<Verbosity> stringToVerbosity( StringRef verbosity );
2829
}
2930

3031
/**
@@ -39,6 +40,7 @@ namespace Catch {
3940
std::string m_name;
4041
Optional<std::string> m_outputFileName;
4142
Optional<ColourMode> m_colourMode;
43+
Optional<Verbosity> m_verbosity;
4244
std::map<std::string, std::string> m_customOptions;
4345

4446
friend bool operator==( ReporterSpec const& lhs,
@@ -53,6 +55,7 @@ namespace Catch {
5355
std::string name,
5456
Optional<std::string> outputFileName,
5557
Optional<ColourMode> colourMode,
58+
Optional<Verbosity> verbosity,
5659
std::map<std::string, std::string> customOptions );
5760

5861
std::string const& name() const { return m_name; }
@@ -63,13 +66,15 @@ namespace Catch {
6366

6467
Optional<ColourMode> const& colourMode() const { return m_colourMode; }
6568

69+
Optional<Verbosity> const& verbosity() const { return m_verbosity; }
70+
6671
std::map<std::string, std::string> const& customOptions() const {
6772
return m_customOptions;
6873
}
6974
};
7075

7176
/**
72-
* Parses provided reporter spec string into
77+
* Parses provided reporter spec string into actual `ReporterSpec`
7378
*
7479
* Returns empty optional on errors, e.g.
7580
* * field that is not first and not a key+value pair

src/catch2/reporters/catch_reporter_common_base.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,32 @@ namespace Catch {
1919
m_wrapped_stream( CATCH_MOVE(config).takeStream() ),
2020
m_stream( m_wrapped_stream->stream() ),
2121
m_colour( makeColourImpl( config.colourMode(), m_wrapped_stream.get() ) ),
22+
m_verbosity( config.verbosity() ),
2223
m_customOptions( config.customOptions() )
2324
{}
2425

2526
ReporterBase::~ReporterBase() = default;
2627

2728
void ReporterBase::listReporters(
2829
std::vector<ReporterDescription> const& descriptions ) {
29-
defaultListReporters( m_stream, descriptions, m_config->verbosity() );
30+
defaultListReporters( m_stream, descriptions, m_verbosity );
3031
}
3132

3233
void ReporterBase::listListeners(
3334
std::vector<ListenerDescription> const& descriptions ) {
34-
defaultListListeners( m_stream, descriptions, m_config->verbosity() );
35+
defaultListListeners( m_stream, descriptions, m_verbosity );
3536
}
3637

3738
void ReporterBase::listTests(std::vector<TestCaseHandle> const& tests) {
3839
defaultListTests(m_stream,
3940
m_colour.get(),
4041
tests,
4142
m_config->hasTestFilters(),
42-
m_config->verbosity());
43+
m_verbosity);
4344
}
4445

4546
void ReporterBase::listTags(std::vector<TagInfo> const& tags) {
46-
defaultListTags( m_stream, tags, m_config->hasTestFilters(), m_config->verbosity() );
47+
defaultListTags( m_stream, tags, m_config->hasTestFilters(), m_verbosity );
4748
}
4849

4950
} // namespace Catch

0 commit comments

Comments
 (0)