|
| 1 | +/* |
| 2 | + * Created by Phil Nash on 19th December 2014 |
| 3 | + * Copyright 2014 Two Blue Cubes Ltd. All rights reserved. |
| 4 | + * |
| 5 | + * Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 6 | + * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 7 | + */ |
| 8 | +#ifndef TWOBLUECUBES_CATCH_REPORTER_TEAMCITY_HPP_INCLUDED |
| 9 | +#define TWOBLUECUBES_CATCH_REPORTER_TEAMCITY_HPP_INCLUDED |
| 10 | + |
| 11 | +#include "catch_reporter_bases.hpp" |
| 12 | + |
| 13 | +#include "../internal/catch_reporter_registrars.hpp" |
| 14 | + |
| 15 | +#include <cstring> |
| 16 | + |
| 17 | +namespace Catch { |
| 18 | + |
| 19 | + struct TeamCityReporter : StreamingReporterBase { |
| 20 | + TeamCityReporter( ReporterConfig const& _config ) |
| 21 | + : StreamingReporterBase( _config ) |
| 22 | + {} |
| 23 | + |
| 24 | + static bool replace( std::string& str, std::string const& replaceThis, std::string const& withThis ) { |
| 25 | + std::size_t i = str.find( replaceThis ); |
| 26 | + if( i != std::string::npos ) { |
| 27 | + str = str.substr( 0, i ) + withThis + str.substr( i+replaceThis.size() ); |
| 28 | + return true; |
| 29 | + } |
| 30 | + return false; |
| 31 | + } |
| 32 | + static std::string escape( std::string const& str ) { |
| 33 | + std::string escaped = str; |
| 34 | + while( replace( escaped, "\'", "|\'" ) || |
| 35 | + replace( escaped, "\n", "|n" ) || |
| 36 | + replace( escaped, "\r", "|r" ) || |
| 37 | + replace( escaped, "|", "||" ) || |
| 38 | + replace( escaped, "[", "|[" ) || |
| 39 | + replace( escaped, "]", "|]" ) ); |
| 40 | + return escaped; |
| 41 | + } |
| 42 | + virtual ~TeamCityReporter(); |
| 43 | + |
| 44 | + static std::string getDescription() { |
| 45 | + return "Reports test results as TeamCity service messages"; |
| 46 | + } |
| 47 | + virtual ReporterPreferences getPreferences() const { |
| 48 | + ReporterPreferences prefs; |
| 49 | + prefs.shouldRedirectStdOut = true; |
| 50 | + return prefs; |
| 51 | + } |
| 52 | + |
| 53 | + // !TBD: ignored tests |
| 54 | + |
| 55 | + virtual void noMatchingTestCases( std::string const& /* spec */ ) {} |
| 56 | + |
| 57 | + virtual void testGroupStarting( GroupInfo const& groupInfo ) { |
| 58 | + StreamingReporterBase::testGroupStarting( groupInfo ); |
| 59 | + stream << "##teamcity[testSuiteStarted name='" |
| 60 | + << escape( groupInfo.name ) << "']\n"; |
| 61 | + } |
| 62 | + virtual void testGroupEnded( TestGroupStats const& testGroupStats ) { |
| 63 | + StreamingReporterBase::testGroupEnded( testGroupStats ); |
| 64 | + stream << "##teamcity[testSuiteFinished name='" |
| 65 | + << escape( testGroupStats.groupInfo.name ) << "']\n"; |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + virtual void assertionStarting( AssertionInfo const& ) { |
| 70 | + } |
| 71 | + |
| 72 | + virtual bool assertionEnded( AssertionStats const& assertionStats ) { |
| 73 | + if( !assertionStats.assertionResult.isOk() ) { |
| 74 | + stream << "##teamcity[testFailed" |
| 75 | + << " name='" << escape( currentTestCaseInfo->name )<< "'" |
| 76 | + << " message='message here'" // !TBD |
| 77 | + << " details='details?'" // !TBD |
| 78 | + << "]\n"; |
| 79 | + } |
| 80 | + return true; |
| 81 | + } |
| 82 | + |
| 83 | +// virtual void sectionStarting( SectionInfo const& _sectionInfo ) { |
| 84 | +// // !TBD |
| 85 | +// } |
| 86 | +// virtual void sectionEnded( SectionStats const& _sectionStats ) { |
| 87 | +// // !TBD |
| 88 | +// } |
| 89 | + |
| 90 | + virtual void testCaseStarting( TestCaseInfo const& testInfo ) { |
| 91 | + StreamingReporterBase::testCaseStarting( testInfo ); |
| 92 | + stream << "##teamcity[testStarted name='" |
| 93 | + << escape( testInfo.name ) << "']\n"; |
| 94 | + } |
| 95 | + virtual void testCaseEnded( TestCaseStats const& testCaseStats ) { |
| 96 | + StreamingReporterBase::testCaseEnded( testCaseStats ); |
| 97 | + if( !testCaseStats.stdOut.empty() ) |
| 98 | + stream << "##teamcity[testStdOut name='" |
| 99 | + << escape( testCaseStats.testInfo.name ) |
| 100 | + << "' out='" << escape( testCaseStats.stdOut ) << "']\n"; |
| 101 | + if( !testCaseStats.stdErr.empty() ) |
| 102 | + stream << "##teamcity[testStdErr name='" |
| 103 | + << escape( testCaseStats.testInfo.name ) |
| 104 | + << "' out='" << escape( testCaseStats.stdErr ) << "']\n"; |
| 105 | + stream << "##teamcity[testFinished name='" |
| 106 | + << escape( testCaseStats.testInfo.name ) << "']\n"; |
| 107 | + } |
| 108 | +// virtual void testRunEnded( TestRunStats const& _testRunStats ) { |
| 109 | +// // !TBD |
| 110 | +// } |
| 111 | + |
| 112 | + private: |
| 113 | + |
| 114 | + }; |
| 115 | + |
| 116 | +#ifdef CATCH_IMPL |
| 117 | + TeamCityReporter::~TeamCityReporter() {} |
| 118 | +#endif |
| 119 | + |
| 120 | + INTERNAL_CATCH_REGISTER_REPORTER( "teamcity", TeamCityReporter ) |
| 121 | + |
| 122 | +} // end namespace Catch |
| 123 | + |
| 124 | +#endif // TWOBLUECUBES_CATCH_REPORTER_TEAMCITY_HPP_INCLUDED |
0 commit comments