Added sections support to TeamCity reporter

This commit is contained in:
Mateusz Piesta 2023-11-14 15:09:15 +01:00
parent 01cac90c62
commit 38bb657d41
2 changed files with 136 additions and 91 deletions

View File

@ -5,35 +5,23 @@
// https://www.boost.org/LICENSE_1_0.txt) // https://www.boost.org/LICENSE_1_0.txt)
// SPDX-License-Identifier: BSL-1.0 // SPDX-License-Identifier: BSL-1.0
#include <catch2/catch_test_case_info.hpp>
#include <catch2/internal/catch_enforce.hpp>
#include <catch2/internal/catch_string_manip.hpp>
#include <catch2/internal/catch_textflow.hpp>
#include <catch2/reporters/catch_reporter_helpers.hpp>
#include <catch2/reporters/catch_reporter_teamcity.hpp> #include <catch2/reporters/catch_reporter_teamcity.hpp>
#include <catch2/reporters/catch_reporter_helpers.hpp> #include <algorithm>
#include <catch2/internal/catch_string_manip.hpp>
#include <catch2/internal/catch_enforce.hpp>
#include <catch2/internal/catch_textflow.hpp>
#include <catch2/catch_test_case_info.hpp>
#include <cassert> #include <cassert>
#include <ostream> #include <ostream>
namespace Catch { namespace Catch {
namespace { namespace {
// if string has a : in first line will set indent to follow it on
// subsequent lines
void printHeaderString(std::ostream& os, std::string const& _string, std::size_t indent = 0) {
std::size_t i = _string.find(": ");
if (i != std::string::npos)
i += 2;
else
i = 0;
os << TextFlow::Column(_string)
.indent(indent + i)
.initialIndent(indent) << '\n';
}
std::string escape( StringRef str ) { std::string escape( StringRef str ) {
std::string escaped = static_cast<std::string>( str ); std::string escaped = static_cast<std::string>( str );
replaceInPlace( escaped, " ", "_" );
replaceInPlace( escaped, "|", "||" ); replaceInPlace( escaped, "|", "||" );
replaceInPlace( escaped, "'", "|'" ); replaceInPlace( escaped, "'", "|'" );
replaceInPlace( escaped, "\n", "|n" ); replaceInPlace( escaped, "\n", "|n" );
@ -44,15 +32,24 @@ namespace Catch {
} }
} // end anonymous namespace } // end anonymous namespace
TeamCityReporter::TeamCityReporter( ReporterConfig&& _config ):
StreamingReporterBase( CATCH_MOVE( _config ) ) {
m_preferences.shouldRedirectStdOut = true;
parseCustomOptions();
}
TeamCityReporter::~TeamCityReporter() = default; TeamCityReporter::~TeamCityReporter() = default;
void TeamCityReporter::testRunStarting( TestRunInfo const& runInfo ) { void TeamCityReporter::testRunStarting( TestRunInfo const& runInfo ) {
m_stream << "##teamcity[testSuiteStarted name='" << escape( runInfo.name ) StreamingReporterBase::testRunStarting( runInfo );
<< "']\n"; m_stream << "##teamcity[testSuiteStarted name='"
<< escape( runInfo.name ) << "']\n";
} }
void TeamCityReporter::testRunEnded( TestRunStats const& runStats ) { void TeamCityReporter::testRunEnded( TestRunStats const& runStats ) {
StreamingReporterBase::testRunEnded( runStats );
m_stream << "##teamcity[testSuiteFinished name='" m_stream << "##teamcity[testSuiteFinished name='"
<< escape( runStats.runInfo.name ) << "']\n"; << escape( runStats.runInfo.name ) << "']\n";
} }
@ -62,11 +59,12 @@ namespace Catch {
if ( !result.isOk() || if ( !result.isOk() ||
result.getResultType() == ResultWas::ExplicitSkip ) { result.getResultType() == ResultWas::ExplicitSkip ) {
ReusableStringStream msg; if ( m_printSections ) {
if (!m_headerPrintedForThisSection) m_stream << createTestCaseHeader( printSectionName() );
printSectionHeader(msg.get());
m_headerPrintedForThisSection = true; m_headerPrintedForThisSection = true;
}
ReusableStringStream msg;
msg << result.getSourceInfo() << '\n'; msg << result.getSourceInfo() << '\n';
switch (result.getResultType()) { switch (result.getResultType()) {
@ -107,12 +105,12 @@ namespace Catch {
for (auto const& messageInfo : assertionStats.infoMessages) for (auto const& messageInfo : assertionStats.infoMessages)
msg << "\n \"" << messageInfo.message << '"'; msg << "\n \"" << messageInfo.message << '"';
if (result.hasExpression()) { if (result.hasExpression()) {
msg << msg << "\n " << result.getExpressionInMacro()
"\n " << result.getExpressionInMacro() << "\n" << "\n"
"with expansion:\n" "with expansion:\n"
" " << result.getExpandedExpression() << '\n'; " "
<< result.getExpandedExpression() << '\n';
} }
if ( result.getResultType() == ResultWas::ExplicitSkip ) { if ( result.getResultType() == ResultWas::ExplicitSkip ) {
@ -123,55 +121,103 @@ namespace Catch {
} else { } else {
m_stream << "##teamcity[testFailed"; m_stream << "##teamcity[testFailed";
} }
m_stream << " name='" << escape( currentTestCaseInfo->name ) << '\''
<< " message='" << escape( msg.str() ) << '\'' << "]\n"; if ( m_printSections ) {
m_stream << " name='" << escape( printSectionName() ) << '\'';
} else {
m_stream << " name='" << escape( currentTestCaseInfo->name )
<< '\'';
}
m_stream << " message='" << escape( msg.str() ) << "']\n";
} }
m_stream.flush();
} }
void TeamCityReporter::testCaseStarting( TestCaseInfo const& testInfo ) { void TeamCityReporter::testCaseStarting( TestCaseInfo const& testInfo ) {
m_testTimer.start();
StreamingReporterBase::testCaseStarting( testInfo ); StreamingReporterBase::testCaseStarting( testInfo );
m_stream << "##teamcity[testStarted name='"
<< escape(testInfo.name) << "']\n"; if ( !m_printSections ) {
m_stream.flush(); m_testCaseTimer.start();
m_stream << createTestCaseHeader( testInfo.name );
}
} }
void TeamCityReporter::testCaseEnded( TestCaseStats const& testCaseStats ) { void TeamCityReporter::testCaseEnded( TestCaseStats const& testCaseStats ) {
StreamingReporterBase::testCaseEnded( testCaseStats ); StreamingReporterBase::testCaseEnded( testCaseStats );
auto const& testCaseInfo = *testCaseStats.testInfo;
if (!testCaseStats.stdOut.empty()) if ( !m_printSections ) {
m_stream << "##teamcity[testStdOut name='" m_stream << createTestCaseFooter(
<< escape(testCaseInfo.name) testCaseStats.testInfo->name,
<< "' out='" << escape(testCaseStats.stdOut) << "']\n"; m_testCaseTimer.getElapsedSeconds() );
if (!testCaseStats.stdErr.empty()) }
m_stream << "##teamcity[testStdErr name='"
<< escape(testCaseInfo.name)
<< "' out='" << escape(testCaseStats.stdErr) << "']\n";
m_stream << "##teamcity[testFinished name='"
<< escape(testCaseInfo.name) << "' duration='"
<< m_testTimer.getElapsedMilliseconds() << "']\n";
m_stream.flush();
} }
void TeamCityReporter::printSectionHeader(std::ostream& os) { std::string TeamCityReporter::printSectionName() {
assert(!m_sectionStack.empty()); std::string output;
if (m_sectionStack.size() > 1) { for ( const auto& entry : m_sectionStack ) {
os << lineOfChars('-') << '\n'; output += entry.name + m_sectionSeparator;
std::vector<SectionInfo>::const_iterator
it = m_sectionStack.begin() + 1, // Skip first section (test case)
itEnd = m_sectionStack.end();
for (; it != itEnd; ++it)
printHeaderString(os, it->name);
os << lineOfChars('-') << '\n';
} }
SourceLineInfo lineInfo = m_sectionStack.front().lineInfo; const auto endPos = output.find_last_not_of( m_sectionSeparator );
if ( endPos != std::string::npos ) { output.resize( endPos + 1 ); }
os << lineInfo << '\n'; return output;
os << lineOfChars('.') << "\n\n"; }
void TeamCityReporter::sectionStarting( SectionInfo const& sectionInfo ) {
StreamingReporterBase::sectionStarting( sectionInfo );
if ( !m_printSections ) { return; }
m_headerPrintedForThisSection = false;
}
void TeamCityReporter::sectionEnded( SectionStats const& sectionStats ) {
if ( !m_printSections ) { return; }
if ( !m_headerPrintedForThisSection ) {
m_headerPrintedForThisSection = true;
m_stream << createTestCaseHeader( printSectionName() );
m_stream << createTestCaseFooter( printSectionName(),
sectionStats.durationInSeconds );
}
StreamingReporterBase::sectionEnded( sectionStats );
}
std::string TeamCityReporter::createTestCaseHeader( std::string name ) {
std::string result{ "##teamcity[testStarted name='" };
result += escape( name );
result += "']\n";
return result;
}
std::string TeamCityReporter::createTestCaseFooter( std::string name,
double duration ) {
std::string result{ "##teamcity[testFinished name='" };
result += escape( name );
if ( m_config->showDurations() == ShowDurations::Always ) {
result +=
"' duration='" +
std::to_string( static_cast<uint32_t>( duration * 1000 ) );
}
result += "']\n";
return result;
}
void TeamCityReporter::parseCustomOptions() {
auto result = m_customOptions.find( "Xsections" );
if ( result != m_customOptions.end() ) {
m_printSections = result->second == "true";
}
result = m_customOptions.find( "Xseparator" );
if ( result != m_customOptions.end() ) {
m_sectionSeparator = result->second;
}
} }
} // end namespace Catch } // end namespace Catch

View File

@ -12,6 +12,7 @@
#include <catch2/catch_timer.hpp> #include <catch2/catch_timer.hpp>
#include <cstring> #include <cstring>
#include <string>
#ifdef __clang__ #ifdef __clang__
# pragma clang diagnostic push # pragma clang diagnostic push
@ -22,11 +23,7 @@ namespace Catch {
class TeamCityReporter final : public StreamingReporterBase { class TeamCityReporter final : public StreamingReporterBase {
public: public:
TeamCityReporter( ReporterConfig&& _config ) TeamCityReporter( ReporterConfig&& _config );
: StreamingReporterBase( CATCH_MOVE(_config) )
{
m_preferences.shouldRedirectStdOut = true;
}
~TeamCityReporter() override; ~TeamCityReporter() override;
@ -38,23 +35,25 @@ namespace Catch {
void testRunStarting( TestRunInfo const& groupInfo ) override; void testRunStarting( TestRunInfo const& groupInfo ) override;
void testRunEnded( TestRunStats const& testGroupStats ) override; void testRunEnded( TestRunStats const& testGroupStats ) override;
void assertionEnded(AssertionStats const& assertionStats) override; void assertionEnded(AssertionStats const& assertionStats) override;
void sectionStarting(SectionInfo const& sectionInfo) override { void sectionStarting( SectionInfo const& sectionInfo ) override;
m_headerPrintedForThisSection = false; void sectionEnded( SectionStats const& sectionStats ) override;
StreamingReporterBase::sectionStarting( sectionInfo );
}
void testCaseStarting(TestCaseInfo const& testInfo) override; void testCaseStarting(TestCaseInfo const& testInfo) override;
void testCaseEnded(TestCaseStats const& testCaseStats) override; void testCaseEnded(TestCaseStats const& testCaseStats) override;
private: private:
void printSectionHeader(std::ostream& os); std::string printSectionName();
std::string createTestCaseHeader( std::string name );
std::string createTestCaseFooter( std::string name, double duration );
void parseCustomOptions();
bool m_headerPrintedForThisSection = false; Timer m_testCaseTimer;
Timer m_testTimer; bool m_headerPrintedForThisSection{ false };
bool m_printSections{ false };
std::string m_sectionSeparator{ "." };
}; };
} // end namespace Catch } // end namespace Catch