Pipe --list* through reporters

This allows us to provide machine-readable listings through the
XMLReporter and it will also allow users to define their own listing
format that does whatever their own tools need.
This commit is contained in:
Martin Hořeňovský 2019-06-21 23:15:08 +02:00
parent 0f39438aae
commit 85b129c741
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A
5 changed files with 149 additions and 88 deletions

View File

@ -6,7 +6,13 @@
*/ */
#include "catch_interfaces_reporter.h" #include "catch_interfaces_reporter.h"
#include "catch_console_colour.h"
#include "../reporters/catch_reporter_listening.h" #include "../reporters/catch_reporter_listening.h"
#include "catch_list.h"
#include "catch_text.h"
#include <algorithm>
#include <iomanip>
namespace Catch { namespace Catch {
@ -108,6 +114,86 @@ namespace Catch {
void IStreamingReporter::fatalErrorEncountered( StringRef ) {} void IStreamingReporter::fatalErrorEncountered( StringRef ) {}
bool IStreamingReporter::isMulti() const { return false; } bool IStreamingReporter::isMulti() const { return false; }
void IStreamingReporter::listReporters(std::vector<ReporterDescription> const& descriptions, Config const& config) {
Catch::cout() << "Available reporters:\n";
const auto maxNameLen = std::max_element(descriptions.begin(), descriptions.end(),
[](ReporterDescription const& lhs, ReporterDescription const& rhs) { return lhs.name.size() < rhs.name.size(); })
->name.size();
for (auto const& desc : descriptions) {
if (config.verbosity() == Verbosity::Quiet) {
Catch::cout()
<< Column(desc.name)
.indent(2)
.width(5 + maxNameLen) << '\n';
} else {
Catch::cout()
<< Column(desc.name + ":")
.indent(2)
.width(5 + maxNameLen)
+ Column(desc.description)
.initialIndent(0)
.indent(2)
.width(CATCH_CONFIG_CONSOLE_WIDTH - maxNameLen - 8)
<< '\n';
}
}
Catch::cout() << std::endl;
}
void IStreamingReporter::listTests(std::vector<TestCase> const& tests, Config const& config) {
if (config.hasTestFilters())
Catch::cout() << "Matching test cases:\n";
else {
Catch::cout() << "All available test cases:\n";
}
for (auto const& testCaseInfo : tests) {
Colour::Code colour = testCaseInfo.isHidden()
? Colour::SecondaryText
: Colour::None;
Colour colourGuard(colour);
Catch::cout() << Column(testCaseInfo.name).initialIndent(2).indent(4) << '\n';
if (config.verbosity() >= Verbosity::High) {
Catch::cout() << Column(Catch::Detail::stringify(testCaseInfo.lineInfo)).indent(4) << std::endl;
std::string description = testCaseInfo.description;
if (description.empty())
description = "(NO DESCRIPTION)";
Catch::cout() << Column(description).indent(4) << std::endl;
}
if (!testCaseInfo.tags.empty() && config.verbosity() > Verbosity::Quiet) {
Catch::cout() << Column(testCaseInfo.tagsAsString()).indent(6) << '\n';
}
}
if (!config.hasTestFilters()) {
Catch::cout() << pluralise(tests.size(), "test case") << '\n' << std::endl;
} else {
Catch::cout() << pluralise(tests.size(), "matching test case") << '\n' << std::endl;
}
}
void IStreamingReporter::listTags(std::vector<TagInfo> const& tags, Config const& config) {
if (config.hasTestFilters()) {
Catch::cout() << "Tags for matching test cases:\n";
} else {
Catch::cout() << "All available tags:\n";
}
for (auto const& tagCount : tags) {
ReusableStringStream rss;
rss << " " << std::setw(2) << tagCount.count << " ";
auto str = rss.str();
auto wrapper = Column(tagCount.all())
.initialIndent(0)
.indent(str.size())
.width(CATCH_CONFIG_CONSOLE_WIDTH - 10);
Catch::cout() << str << wrapper << '\n';
}
Catch::cout() << pluralise(tags.size(), "tag") << '\n' << std::endl;
}
IReporterFactory::~IReporterFactory() = default; IReporterFactory::~IReporterFactory() = default;
IReporterRegistry::~IReporterRegistry() = default; IReporterRegistry::~IReporterRegistry() = default;

View File

@ -33,6 +33,9 @@
namespace Catch { namespace Catch {
struct ReporterDescription;
struct TagInfo;
struct ReporterConfig { struct ReporterConfig {
explicit ReporterConfig( IConfigPtr const& _fullConfig ); explicit ReporterConfig( IConfigPtr const& _fullConfig );
@ -244,6 +247,12 @@ namespace Catch {
virtual void fatalErrorEncountered( StringRef name ); virtual void fatalErrorEncountered( StringRef name );
virtual bool isMulti() const; virtual bool isMulti() const;
// Listing support
virtual void listReporters(std::vector<ReporterDescription> const& descriptions, Config const& config);
virtual void listTests(std::vector<TestCase> const& tests, Config const& config);
virtual void listTags(std::vector<TagInfo> const& tags, Config const& config);
}; };
using IStreamingReporterPtr = std::unique_ptr<IStreamingReporter>; using IStreamingReporterPtr = std::unique_ptr<IStreamingReporter>;

View File

@ -23,65 +23,22 @@
#include <limits> #include <limits>
#include <algorithm> #include <algorithm>
#include <iomanip>
#include <set> #include <set>
namespace Catch { namespace Catch {
namespace { namespace {
struct TagInfo { void listTests(IStreamingReporter& reporter, Config const& config) {
void add(std::string const& spelling);
std::string all() const;
std::set<std::string> spellings;
std::size_t count = 0;
};
void listTests(Config const& config) {
TestSpec testSpec = config.testSpec(); TestSpec testSpec = config.testSpec();
if (config.hasTestFilters())
Catch::cout() << "Matching test cases:\n";
else {
Catch::cout() << "All available test cases:\n";
}
auto matchedTestCases = filterTests(getAllTestCasesSorted(config), testSpec, config); auto matchedTestCases = filterTests(getAllTestCasesSorted(config), testSpec, config);
for (auto const& testCaseInfo : matchedTestCases) { reporter.listTests(matchedTestCases, config);
Colour::Code colour = testCaseInfo.isHidden()
? Colour::SecondaryText
: Colour::None;
Colour colourGuard(colour);
Catch::cout() << Column(testCaseInfo.name).initialIndent(2).indent(4) << "\n";
if (config.verbosity() >= Verbosity::High) {
Catch::cout() << Column(Catch::Detail::stringify(testCaseInfo.lineInfo)).indent(4) << std::endl;
std::string description = testCaseInfo.description;
if (description.empty())
description = "(NO DESCRIPTION)";
Catch::cout() << Column(description).indent(4) << std::endl;
}
if (!testCaseInfo.tags.empty())
Catch::cout() << Column(testCaseInfo.tagsAsString()).indent(6) << "\n";
} }
if (!config.hasTestFilters()) void listTags(IStreamingReporter& reporter, Config const& config) {
Catch::cout() << pluralise(matchedTestCases.size(), "test case") << '\n' << std::endl;
else
Catch::cout() << pluralise(matchedTestCases.size(), "matching test case") << '\n' << std::endl;
}
void listTags(Config const& config) {
TestSpec testSpec = config.testSpec(); TestSpec testSpec = config.testSpec();
if (config.hasTestFilters()) std::vector<TestCase> matchedTestCases = filterTests(getAllTestCasesSorted(config), testSpec, config);
Catch::cout() << "Tags for matching test cases:\n";
else {
Catch::cout() << "All available tags:\n";
}
std::map<std::string, TagInfo> tagCounts; std::map<std::string, TagInfo> tagCounts;
std::vector<TestCase> matchedTestCases = filterTests(getAllTestCasesSorted(config), testSpec, config);
for (auto const& testCase : matchedTestCases) { for (auto const& testCase : matchedTestCases) {
for (auto const& tagName : testCase.getTestCaseInfo().tags) { for (auto const& tagName : testCase.getTestCaseInfo().tags) {
std::string lcaseTagName = toLower(tagName); std::string lcaseTagName = toLower(tagName);
@ -92,38 +49,24 @@ namespace Catch {
} }
} }
for (auto const& tagCount : tagCounts) { std::vector<TagInfo> infos; infos.reserve(tagCounts.size());
ReusableStringStream rss; for (auto const& tagc : tagCounts) {
rss << " " << std::setw(2) << tagCount.second.count << " "; infos.push_back(std::move(tagc.second));
auto str = rss.str();
auto wrapper = Column(tagCount.second.all())
.initialIndent(0)
.indent(str.size())
.width(CATCH_CONFIG_CONSOLE_WIDTH - 10);
Catch::cout() << str << wrapper << '\n';
}
Catch::cout() << pluralise(tagCounts.size(), "tag") << '\n' << std::endl;
} }
void listReporters() { reporter.listTags(infos, config);
Catch::cout() << "Available reporters:\n"; }
void listReporters(IStreamingReporter& reporter, Config const& config) {
std::vector<ReporterDescription> descriptions;
IReporterRegistry::FactoryMap const& factories = getRegistryHub().getReporterRegistry().getFactories(); IReporterRegistry::FactoryMap const& factories = getRegistryHub().getReporterRegistry().getFactories();
std::size_t maxNameLen = 0; descriptions.reserve(factories.size());
for (auto const& factoryKvp : factories) for (auto const& fac : factories) {
maxNameLen = (std::max)(maxNameLen, factoryKvp.first.size()); descriptions.push_back({ fac.first, fac.second->getDescription() });
for (auto const& factoryKvp : factories) {
Catch::cout()
<< Column(factoryKvp.first + ":")
.indent(2)
.width(5 + maxNameLen)
+ Column(factoryKvp.second->getDescription())
.initialIndent(0)
.indent(2)
.width(CATCH_CONFIG_CONSOLE_WIDTH - maxNameLen - 8)
<< "\n";
} }
Catch::cout() << std::endl;
reporter.listReporters(descriptions, config);
} }
} // end anonymous namespace } // end anonymous namespace
@ -149,20 +92,20 @@ namespace Catch {
return out; return out;
} }
bool list( std::shared_ptr<Config> const& config ) { bool list( IStreamingReporter& reporter, std::shared_ptr<Config> const& config ) {
bool listed = false; bool listed = false;
getCurrentMutableContext().setConfig( config ); getCurrentMutableContext().setConfig( config );
if (config->listTests()) { if (config->listTests()) {
listed = true; listed = true;
listTests(*config); listTests(reporter, *config);
} }
if (config->listTags()) { if (config->listTags()) {
listed = true; listed = true;
listTags(*config); listTags(reporter, *config);
} }
if (config->listReporters()) { if (config->listReporters()) {
listed = true; listed = true;
listReporters(); listReporters(reporter, *config);
} }
return listed; return listed;
} }

View File

@ -10,9 +10,29 @@
#include "catch_config.hpp" #include "catch_config.hpp"
#include <set>
#include <string>
namespace Catch { namespace Catch {
bool list( std::shared_ptr<Config> const& config ); struct IStreamingReporter;
struct ReporterDescription {
std::string name, description;
};
struct TagInfo {
void add(std::string const& spelling);
std::string all() const;
std::set<std::string> spellings;
std::size_t count = 0;
};
struct testClassInfo {};
bool list( IStreamingReporter& reporter, std::shared_ptr<Config> const& config );
} // end namespace Catch } // end namespace Catch

View File

@ -62,9 +62,9 @@ namespace Catch {
class TestGroup { class TestGroup {
public: public:
explicit TestGroup(std::shared_ptr<Config> const& config) explicit TestGroup(IStreamingReporterPtr&& reporter, std::shared_ptr<Config> const& config)
: m_config{config} : m_config{config}
, m_context{config, makeReporter(config)} , m_context{config, std::move(reporter)}
{ {
auto const& allTestCases = getAllTestCasesSorted(*m_config); auto const& allTestCases = getAllTestCasesSorted(*m_config);
m_matches = m_config->testSpec().matchesByFilter(allTestCases, *m_config); m_matches = m_config->testSpec().matchesByFilter(allTestCases, *m_config);
@ -287,12 +287,15 @@ namespace Catch {
if( m_configData.filenamesAsTags ) if( m_configData.filenamesAsTags )
applyFilenamesAsTags( *m_config ); applyFilenamesAsTags( *m_config );
// Create reporter(s) so we can route listings through them
auto reporter = makeReporter(m_config);
// Handle list request // Handle list request
if (list(m_config)) { if (list(*reporter, m_config)) {
return 0; return 0;
} }
TestGroup tests { m_config }; TestGroup tests { std::move(reporter), m_config };
auto const totals = tests.execute(); auto const totals = tests.execute();
if( m_config->warnAboutNoTests() && totals.error == -1 ) if( m_config->warnAboutNoTests() && totals.error == -1 )