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
|
|
|
|
|
|
|
|
#include "catch_reporter_bases.hpp"
|
|
|
|
|
|
|
|
#include "../internal/catch_reporter_registrars.hpp"
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
namespace Catch {
|
|
|
|
|
|
|
|
struct TeamCityReporter : StreamingReporterBase {
|
|
|
|
TeamCityReporter( ReporterConfig const& _config )
|
2014-12-20 01:46:02 +01:00
|
|
|
: StreamingReporterBase( _config ),
|
|
|
|
m_headerPrintedForThisSection( false )
|
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-19 19:16:19 +01:00
|
|
|
replaceInPlace( escaped, "\'", "|\'" );
|
|
|
|
replaceInPlace( escaped, "\n", "|n" );
|
|
|
|
replaceInPlace( escaped, "\r", "|r" );
|
|
|
|
replaceInPlace( escaped, "[", "|[" );
|
|
|
|
replaceInPlace( escaped, "]", "|]" );
|
2014-12-19 18:52:33 +01:00
|
|
|
return escaped;
|
|
|
|
}
|
|
|
|
virtual ~TeamCityReporter();
|
|
|
|
|
|
|
|
static std::string getDescription() {
|
|
|
|
return "Reports test results as TeamCity service messages";
|
|
|
|
}
|
|
|
|
virtual ReporterPreferences getPreferences() const {
|
|
|
|
ReporterPreferences prefs;
|
|
|
|
prefs.shouldRedirectStdOut = true;
|
|
|
|
return prefs;
|
|
|
|
}
|
|
|
|
|
|
|
|
// !TBD: ignored tests
|
|
|
|
|
|
|
|
virtual void noMatchingTestCases( std::string const& /* spec */ ) {}
|
|
|
|
|
|
|
|
virtual void testGroupStarting( GroupInfo const& groupInfo ) {
|
|
|
|
StreamingReporterBase::testGroupStarting( groupInfo );
|
|
|
|
stream << "##teamcity[testSuiteStarted name='"
|
|
|
|
<< escape( groupInfo.name ) << "']\n";
|
|
|
|
}
|
|
|
|
virtual void testGroupEnded( TestGroupStats const& testGroupStats ) {
|
|
|
|
StreamingReporterBase::testGroupEnded( testGroupStats );
|
|
|
|
stream << "##teamcity[testSuiteFinished name='"
|
|
|
|
<< escape( testGroupStats.groupInfo.name ) << "']\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virtual void assertionStarting( AssertionInfo const& ) {
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool assertionEnded( AssertionStats const& assertionStats ) {
|
2014-12-19 20:05:24 +01:00
|
|
|
AssertionResult const& result = assertionStats.assertionResult;
|
|
|
|
if( !result.isOk() ) {
|
|
|
|
|
2014-12-20 01:46:02 +01:00
|
|
|
std::ostringstream msg;
|
|
|
|
if( !m_headerPrintedForThisSection )
|
|
|
|
printTestCaseAndSectionHeader( msg );
|
|
|
|
m_headerPrintedForThisSection = true;
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
// These cases are here to prevent compiler warnings
|
|
|
|
case ResultWas::Unknown:
|
|
|
|
case ResultWas::FailureBit:
|
|
|
|
case ResultWas::Exception:
|
|
|
|
CATCH_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
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:";
|
2014-12-19 20:05:24 +01:00
|
|
|
for( std::vector<MessageInfo>::const_iterator
|
|
|
|
it = assertionStats.infoMessages.begin(),
|
|
|
|
itEnd = assertionStats.infoMessages.end();
|
|
|
|
it != itEnd;
|
|
|
|
++it )
|
2014-12-20 01:46:02 +01:00
|
|
|
msg << "\n \"" << it->message << "\"";
|
2014-12-19 20:05:24 +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
|
|
|
}
|
2014-12-20 01:46:02 +01:00
|
|
|
msg << "\n" << result.getSourceInfo() << "\n";
|
|
|
|
msg << "---------------------------------------";
|
2014-12-19 20:05:24 +01:00
|
|
|
|
2014-12-19 18:52:33 +01:00
|
|
|
stream << "##teamcity[testFailed"
|
|
|
|
<< " name='" << escape( currentTestCaseInfo->name )<< "'"
|
2014-12-20 01:46:02 +01:00
|
|
|
<< " message='" << escape( msg.str() ) << "'"
|
2014-12-19 18:52:33 +01:00
|
|
|
<< "]\n";
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-12-20 01:46:02 +01:00
|
|
|
virtual void sectionStarting( SectionInfo const& sectionInfo ) {
|
|
|
|
m_headerPrintedForThisSection = false;
|
|
|
|
StreamingReporterBase::sectionStarting( sectionInfo );
|
|
|
|
}
|
2014-12-19 18:52:33 +01:00
|
|
|
// virtual void sectionEnded( SectionStats const& _sectionStats ) {
|
|
|
|
// // !TBD
|
|
|
|
// }
|
|
|
|
|
|
|
|
virtual void testCaseStarting( TestCaseInfo const& testInfo ) {
|
|
|
|
StreamingReporterBase::testCaseStarting( testInfo );
|
|
|
|
stream << "##teamcity[testStarted name='"
|
|
|
|
<< escape( testInfo.name ) << "']\n";
|
|
|
|
}
|
|
|
|
virtual void testCaseEnded( TestCaseStats const& testCaseStats ) {
|
|
|
|
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='"
|
|
|
|
<< escape( testCaseStats.testInfo.name ) << "']\n";
|
|
|
|
}
|
|
|
|
// virtual void testRunEnded( TestRunStats const& _testRunStats ) {
|
|
|
|
// // !TBD
|
|
|
|
// }
|
|
|
|
|
|
|
|
private:
|
2014-12-20 01:46:02 +01:00
|
|
|
void printTestCaseAndSectionHeader( std::ostream& os ) {
|
|
|
|
assert( !m_sectionStack.empty() );
|
|
|
|
printOpenHeader( os, currentTestCaseInfo->name );
|
|
|
|
|
|
|
|
if( m_sectionStack.size() > 1 ) {
|
|
|
|
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, 2 );
|
|
|
|
}
|
|
|
|
|
|
|
|
SourceLineInfo lineInfo = m_sectionStack.front().lineInfo;
|
|
|
|
|
|
|
|
if( !lineInfo.empty() ){
|
|
|
|
os << getLineOfChars<'-'>() << "\n";
|
|
|
|
os << lineInfo << "\n";
|
|
|
|
}
|
|
|
|
os << getLineOfChars<'.'>() << "\n\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void printOpenHeader( std::ostream& os, std::string const& _name ) {
|
|
|
|
os << getLineOfChars<'-'>() << "\n";
|
|
|
|
printHeaderString( os, _name );
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 << Text( _string, TextAttributes()
|
|
|
|
.setIndent( indent+i)
|
|
|
|
.setInitialIndent( indent ) ) << "\n";
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
bool m_headerPrintedForThisSection;
|
2014-12-19 18:52:33 +01:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef CATCH_IMPL
|
|
|
|
TeamCityReporter::~TeamCityReporter() {}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
INTERNAL_CATCH_REGISTER_REPORTER( "teamcity", TeamCityReporter )
|
|
|
|
|
|
|
|
} // end namespace Catch
|
|
|
|
|
|
|
|
#endif // TWOBLUECUBES_CATCH_REPORTER_TEAMCITY_HPP_INCLUDED
|