2014-12-19 18:52:33 +01:00
|
|
|
/*
|
|
|
|
* Created by Phil Nash on 19th December 2014
|
|
|
|
* Copyright 2014 Two Blue Cubes Ltd. All rights reserved.
|
|
|
|
*
|
|
|
|
* Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
*/
|
|
|
|
#ifndef TWOBLUECUBES_CATCH_REPORTER_TEAMCITY_HPP_INCLUDED
|
|
|
|
#define TWOBLUECUBES_CATCH_REPORTER_TEAMCITY_HPP_INCLUDED
|
|
|
|
|
2014-12-22 20:45:16 +01:00
|
|
|
// Don't #include any Catch headers here - we can assume they are already
|
|
|
|
// included before this header.
|
|
|
|
// This is not good practice in general but is necessary in this case so this
|
|
|
|
// file can be distributed as a single header that works with the main
|
|
|
|
// Catch single header.
|
2014-12-19 18:52:33 +01:00
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
|
2014-12-21 01:17:45 +01:00
|
|
|
#ifdef __clang__
|
2015-08-07 09:20:56 +02:00
|
|
|
# pragma clang diagnostic push
|
|
|
|
# pragma clang diagnostic ignored "-Wpadded"
|
2014-12-21 01:17:45 +01:00
|
|
|
#endif
|
|
|
|
|
2014-12-19 18:52:33 +01:00
|
|
|
namespace Catch {
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2017-07-09 11:46:53 +02:00
|
|
|
struct TeamCityReporter : StreamingReporterBase<TeamCityReporter> {
|
2014-12-19 18:52:33 +01:00
|
|
|
TeamCityReporter( ReporterConfig const& _config )
|
2017-04-25 19:56:53 +02:00
|
|
|
: StreamingReporterBase( _config )
|
2015-08-07 09:20:56 +02:00
|
|
|
{
|
|
|
|
m_reporterPrefs.shouldRedirectStdOut = true;
|
|
|
|
}
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2014-12-19 18:52:33 +01:00
|
|
|
static std::string escape( std::string const& str ) {
|
|
|
|
std::string escaped = str;
|
2014-12-19 20:24:41 +01:00
|
|
|
replaceInPlace( escaped, "|", "||" );
|
2014-12-29 21:04:54 +01:00
|
|
|
replaceInPlace( escaped, "'", "|'" );
|
2014-12-19 19:16:19 +01:00
|
|
|
replaceInPlace( escaped, "\n", "|n" );
|
|
|
|
replaceInPlace( escaped, "\r", "|r" );
|
|
|
|
replaceInPlace( escaped, "[", "|[" );
|
|
|
|
replaceInPlace( escaped, "]", "|]" );
|
2014-12-19 18:52:33 +01:00
|
|
|
return escaped;
|
|
|
|
}
|
2017-07-20 16:57:17 +02:00
|
|
|
~TeamCityReporter() override;
|
2014-12-19 18:52:33 +01:00
|
|
|
|
|
|
|
static std::string getDescription() {
|
|
|
|
return "Reports test results as TeamCity service messages";
|
|
|
|
}
|
2014-12-22 21:10:33 +01:00
|
|
|
|
2017-07-20 16:57:17 +02:00
|
|
|
void skipTest( TestCaseInfo const& /* testInfo */ ) override {
|
2014-12-22 21:10:33 +01:00
|
|
|
}
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2017-07-20 16:57:17 +02:00
|
|
|
void noMatchingTestCases( std::string const& /* spec */ ) override {}
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2017-07-20 16:57:17 +02:00
|
|
|
void testGroupStarting( GroupInfo const& groupInfo ) override {
|
2014-12-19 18:52:33 +01:00
|
|
|
StreamingReporterBase::testGroupStarting( groupInfo );
|
|
|
|
stream << "##teamcity[testSuiteStarted name='"
|
|
|
|
<< escape( groupInfo.name ) << "']\n";
|
|
|
|
}
|
2017-07-20 16:57:17 +02:00
|
|
|
void testGroupEnded( TestGroupStats const& testGroupStats ) override {
|
2014-12-19 18:52:33 +01:00
|
|
|
StreamingReporterBase::testGroupEnded( testGroupStats );
|
|
|
|
stream << "##teamcity[testSuiteFinished name='"
|
|
|
|
<< escape( testGroupStats.groupInfo.name ) << "']\n";
|
|
|
|
}
|
|
|
|
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2017-07-20 16:57:17 +02:00
|
|
|
void assertionStarting( AssertionInfo const& ) override {}
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2017-07-20 16:57:17 +02:00
|
|
|
bool assertionEnded( AssertionStats const& assertionStats ) override {
|
2014-12-19 20:05:24 +01:00
|
|
|
AssertionResult const& result = assertionStats.assertionResult;
|
|
|
|
if( !result.isOk() ) {
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2017-11-07 19:01:10 +01:00
|
|
|
ReusableStringStream msg;
|
2014-12-20 01:46:02 +01:00
|
|
|
if( !m_headerPrintedForThisSection )
|
2017-11-07 19:01:10 +01:00
|
|
|
printSectionHeader( msg.get() );
|
2014-12-20 01:46:02 +01:00
|
|
|
m_headerPrintedForThisSection = true;
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2014-12-20 02:02:17 +01:00
|
|
|
msg << result.getSourceInfo() << "\n";
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2014-12-19 20:05:24 +01:00
|
|
|
switch( result.getResultType() ) {
|
|
|
|
case ResultWas::ExpressionFailed:
|
2014-12-20 01:46:02 +01:00
|
|
|
msg << "expression failed";
|
2014-12-19 20:05:24 +01:00
|
|
|
break;
|
|
|
|
case ResultWas::ThrewException:
|
2014-12-20 01:46:02 +01:00
|
|
|
msg << "unexpected exception";
|
2014-12-19 20:05:24 +01:00
|
|
|
break;
|
|
|
|
case ResultWas::FatalErrorCondition:
|
2014-12-20 01:46:02 +01:00
|
|
|
msg << "fatal error condition";
|
2014-12-19 20:05:24 +01:00
|
|
|
break;
|
|
|
|
case ResultWas::DidntThrowException:
|
2014-12-20 01:46:02 +01:00
|
|
|
msg << "no exception was thrown where one was expected";
|
2014-12-19 20:05:24 +01:00
|
|
|
break;
|
|
|
|
case ResultWas::ExplicitFailure:
|
2014-12-20 01:46:02 +01:00
|
|
|
msg << "explicit failure";
|
2014-12-19 20:05:24 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
// We shouldn't get here because of the isOk() test
|
|
|
|
case ResultWas::Ok:
|
|
|
|
case ResultWas::Info:
|
|
|
|
case ResultWas::Warning:
|
2017-11-30 13:47:54 +01:00
|
|
|
throw std::domain_error( "Internal error in TeamCity reporter" );
|
2014-12-19 20:05:24 +01:00
|
|
|
// These cases are here to prevent compiler warnings
|
|
|
|
case ResultWas::Unknown:
|
|
|
|
case ResultWas::FailureBit:
|
|
|
|
case ResultWas::Exception:
|
2017-11-30 13:47:54 +01:00
|
|
|
throw std::domain_error( "Not implemented" );
|
2014-12-19 20:05:24 +01:00
|
|
|
}
|
|
|
|
if( assertionStats.infoMessages.size() == 1 )
|
2014-12-20 01:46:02 +01:00
|
|
|
msg << " with message:";
|
2014-12-19 20:05:24 +01:00
|
|
|
if( assertionStats.infoMessages.size() > 1 )
|
2014-12-20 01:46:02 +01:00
|
|
|
msg << " with messages:";
|
2017-04-25 12:06:52 +02:00
|
|
|
for( auto const& messageInfo : assertionStats.infoMessages )
|
|
|
|
msg << "\n \"" << messageInfo.message << "\"";
|
2015-11-04 19:01:28 +01:00
|
|
|
|
|
|
|
|
2014-12-19 20:25:53 +01:00
|
|
|
if( result.hasExpression() ) {
|
2014-12-20 01:46:02 +01:00
|
|
|
msg <<
|
|
|
|
"\n " << result.getExpressionInMacro() << "\n"
|
|
|
|
"with expansion:\n" <<
|
|
|
|
" " << result.getExpandedExpression() << "\n";
|
2014-12-19 20:24:41 +01:00
|
|
|
}
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2017-04-07 12:15:37 +02:00
|
|
|
if( currentTestCaseInfo->okToFail() ) {
|
|
|
|
msg << "- failure ignore as test marked as 'ok to fail'\n";
|
|
|
|
stream << "##teamcity[testIgnored"
|
|
|
|
<< " name='" << escape( currentTestCaseInfo->name )<< "'"
|
|
|
|
<< " message='" << escape( msg.str() ) << "'"
|
|
|
|
<< "]\n";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
stream << "##teamcity[testFailed"
|
|
|
|
<< " name='" << escape( currentTestCaseInfo->name )<< "'"
|
|
|
|
<< " message='" << escape( msg.str() ) << "'"
|
|
|
|
<< "]\n";
|
|
|
|
}
|
2014-12-19 18:52:33 +01:00
|
|
|
}
|
2017-10-16 08:55:51 +02:00
|
|
|
stream.flush();
|
2014-12-19 18:52:33 +01:00
|
|
|
return true;
|
|
|
|
}
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2017-07-20 16:57:17 +02:00
|
|
|
void sectionStarting( SectionInfo const& sectionInfo ) override {
|
2014-12-20 01:46:02 +01:00
|
|
|
m_headerPrintedForThisSection = false;
|
|
|
|
StreamingReporterBase::sectionStarting( sectionInfo );
|
|
|
|
}
|
2014-12-21 01:17:45 +01:00
|
|
|
|
2017-07-20 16:57:17 +02:00
|
|
|
void testCaseStarting( TestCaseInfo const& testInfo ) override {
|
2017-05-16 16:09:51 +02:00
|
|
|
m_testTimer.start();
|
2014-12-19 18:52:33 +01:00
|
|
|
StreamingReporterBase::testCaseStarting( testInfo );
|
|
|
|
stream << "##teamcity[testStarted name='"
|
|
|
|
<< escape( testInfo.name ) << "']\n";
|
2017-10-16 08:55:51 +02:00
|
|
|
stream.flush();
|
2014-12-19 18:52:33 +01:00
|
|
|
}
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2017-07-20 16:57:17 +02:00
|
|
|
void testCaseEnded( TestCaseStats const& testCaseStats ) override {
|
2014-12-19 18:52:33 +01:00
|
|
|
StreamingReporterBase::testCaseEnded( testCaseStats );
|
|
|
|
if( !testCaseStats.stdOut.empty() )
|
|
|
|
stream << "##teamcity[testStdOut name='"
|
|
|
|
<< escape( testCaseStats.testInfo.name )
|
|
|
|
<< "' out='" << escape( testCaseStats.stdOut ) << "']\n";
|
|
|
|
if( !testCaseStats.stdErr.empty() )
|
|
|
|
stream << "##teamcity[testStdErr name='"
|
|
|
|
<< escape( testCaseStats.testInfo.name )
|
|
|
|
<< "' out='" << escape( testCaseStats.stdErr ) << "']\n";
|
|
|
|
stream << "##teamcity[testFinished name='"
|
2017-03-06 16:55:00 +01:00
|
|
|
<< escape( testCaseStats.testInfo.name ) << "' duration='"
|
2017-05-16 16:09:51 +02:00
|
|
|
<< m_testTimer.getElapsedMilliseconds() << "']\n";
|
2017-10-16 08:55:51 +02:00
|
|
|
stream.flush();
|
2014-12-19 18:52:33 +01:00
|
|
|
}
|
2014-12-21 01:17:45 +01:00
|
|
|
|
2014-12-19 18:52:33 +01:00
|
|
|
private:
|
2014-12-20 02:02:17 +01:00
|
|
|
void printSectionHeader( std::ostream& os ) {
|
2014-12-20 01:46:02 +01:00
|
|
|
assert( !m_sectionStack.empty() );
|
2014-12-20 02:02:17 +01:00
|
|
|
|
2014-12-20 01:46:02 +01:00
|
|
|
if( m_sectionStack.size() > 1 ) {
|
2014-12-20 02:02:17 +01:00
|
|
|
os << getLineOfChars<'-'>() << "\n";
|
|
|
|
|
2014-12-20 01:46:02 +01:00
|
|
|
std::vector<SectionInfo>::const_iterator
|
|
|
|
it = m_sectionStack.begin()+1, // Skip first section (test case)
|
|
|
|
itEnd = m_sectionStack.end();
|
|
|
|
for( ; it != itEnd; ++it )
|
2014-12-20 02:02:17 +01:00
|
|
|
printHeaderString( os, it->name );
|
|
|
|
os << getLineOfChars<'-'>() << "\n";
|
2014-12-20 01:46:02 +01:00
|
|
|
}
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2014-12-20 01:46:02 +01:00
|
|
|
SourceLineInfo lineInfo = m_sectionStack.front().lineInfo;
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2014-12-20 02:02:17 +01:00
|
|
|
if( !lineInfo.empty() )
|
2014-12-20 01:46:02 +01:00
|
|
|
os << lineInfo << "\n";
|
|
|
|
os << getLineOfChars<'.'>() << "\n\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
// if string has a : in first line will set indent to follow it on
|
|
|
|
// subsequent lines
|
2017-07-25 17:16:28 +02:00
|
|
|
static void printHeaderString( std::ostream& os, std::string const& _string, std::size_t indent = 0 ) {
|
2014-12-20 01:46:02 +01:00
|
|
|
std::size_t i = _string.find( ": " );
|
|
|
|
if( i != std::string::npos )
|
|
|
|
i+=2;
|
|
|
|
else
|
|
|
|
i = 0;
|
2017-07-18 21:27:42 +02:00
|
|
|
os << Column( _string )
|
|
|
|
.indent( indent+i)
|
|
|
|
.initialIndent( indent ) << "\n";
|
2014-12-20 01:46:02 +01:00
|
|
|
}
|
|
|
|
private:
|
2017-04-25 19:56:53 +02:00
|
|
|
bool m_headerPrintedForThisSection = false;
|
2017-05-16 16:09:51 +02:00
|
|
|
Timer m_testTimer;
|
2014-12-19 18:52:33 +01:00
|
|
|
};
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2014-12-19 18:52:33 +01:00
|
|
|
#ifdef CATCH_IMPL
|
|
|
|
TeamCityReporter::~TeamCityReporter() {}
|
|
|
|
#endif
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2017-08-17 18:07:24 +02:00
|
|
|
CATCH_REGISTER_REPORTER( "teamcity", TeamCityReporter )
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2014-12-19 18:52:33 +01:00
|
|
|
} // end namespace Catch
|
|
|
|
|
2014-12-21 01:17:45 +01:00
|
|
|
#ifdef __clang__
|
2015-08-07 09:20:56 +02:00
|
|
|
# pragma clang diagnostic pop
|
2014-12-21 01:17:45 +01:00
|
|
|
#endif
|
|
|
|
|
2014-12-19 18:52:33 +01:00
|
|
|
#endif // TWOBLUECUBES_CATCH_REPORTER_TEAMCITY_HPP_INCLUDED
|