/
catch_reporter_teamcity.cpp
177 lines (150 loc) · 6.6 KB
/
catch_reporter_teamcity.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright Catch2 Authors
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)
// SPDX-License-Identifier: BSL-1.0
#include <catch2/reporters/catch_reporter_teamcity.hpp>
#include <catch2/reporters/catch_reporter_helpers.hpp>
#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 <ostream>
namespace Catch {
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 escaped = static_cast<std::string>(str);
replaceInPlace(escaped, "|", "||");
replaceInPlace(escaped, "'", "|'");
replaceInPlace(escaped, "\n", "|n");
replaceInPlace(escaped, "\r", "|r");
replaceInPlace(escaped, "[", "|[");
replaceInPlace(escaped, "]", "|]");
return escaped;
}
} // end anonymous namespace
TeamCityReporter::~TeamCityReporter() {}
void TeamCityReporter::testRunStarting( TestRunInfo const& runInfo ) {
m_stream << "##teamcity[testSuiteStarted name='" << escape( runInfo.name )
<< "']\n";
}
void TeamCityReporter::testRunEnded( TestRunStats const& runStats ) {
m_stream << "##teamcity[testSuiteFinished name='"
<< escape( runStats.runInfo.name ) << "']\n";
}
void TeamCityReporter::assertionEnded(AssertionStats const& assertionStats) {
AssertionResult const& result = assertionStats.assertionResult;
if ( !result.isOk() ||
result.getResultType() == ResultWas::ExplicitSkip ) {
ReusableStringStream msg;
if (!m_headerPrintedForThisSection)
printSectionHeader(msg.get());
m_headerPrintedForThisSection = true;
msg << result.getSourceInfo() << '\n';
switch (result.getResultType()) {
case ResultWas::ExpressionFailed:
msg << "expression failed";
break;
case ResultWas::ThrewException:
msg << "unexpected exception";
break;
case ResultWas::FatalErrorCondition:
msg << "fatal error condition";
break;
case ResultWas::DidntThrowException:
msg << "no exception was thrown where one was expected";
break;
case ResultWas::ExplicitFailure:
msg << "explicit failure";
break;
case ResultWas::ExplicitSkip:
msg << "explicit skip";
break;
// We shouldn't get here because of the isOk() test
case ResultWas::Ok:
case ResultWas::Info:
case ResultWas::Warning:
CATCH_ERROR("Internal error in TeamCity reporter");
// These cases are here to prevent compiler warnings
case ResultWas::Unknown:
case ResultWas::FailureBit:
case ResultWas::Exception:
CATCH_ERROR("Not implemented");
}
if (assertionStats.infoMessages.size() == 1)
msg << " with message:";
if (assertionStats.infoMessages.size() > 1)
msg << " with messages:";
for (auto const& messageInfo : assertionStats.infoMessages)
msg << "\n \"" << messageInfo.message << '"';
if (result.hasExpression()) {
msg <<
"\n " << result.getExpressionInMacro() << "\n"
"with expansion:\n"
" " << result.getExpandedExpression() << '\n';
}
if ( result.getResultType() == ResultWas::ExplicitSkip ) {
m_stream << "##teamcity[testIgnored";
} else if ( currentTestCaseInfo->okToFail() ) {
msg << "- failure ignore as test marked as 'ok to fail'\n";
m_stream << "##teamcity[testIgnored";
} else {
m_stream << "##teamcity[testFailed";
}
m_stream << " name='" << escape( currentTestCaseInfo->name ) << '\''
<< " message='" << escape( msg.str() ) << '\'' << "]\n";
}
m_stream.flush();
}
void TeamCityReporter::testCaseStarting(TestCaseInfo const& testInfo) {
m_testTimer.start();
StreamingReporterBase::testCaseStarting(testInfo);
m_stream << "##teamcity[testStarted name='"
<< escape(testInfo.name) << "']\n";
m_stream.flush();
}
void TeamCityReporter::testCaseEnded(TestCaseStats const& testCaseStats) {
StreamingReporterBase::testCaseEnded(testCaseStats);
auto const& testCaseInfo = *testCaseStats.testInfo;
if (!testCaseStats.stdOut.empty())
m_stream << "##teamcity[testStdOut name='"
<< escape(testCaseInfo.name)
<< "' out='" << escape(testCaseStats.stdOut) << "']\n";
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) {
assert(!m_sectionStack.empty());
if (m_sectionStack.size() > 1) {
os << lineOfChars('-') << '\n';
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;
os << lineInfo << '\n';
os << lineOfChars('.') << "\n\n";
}
} // end namespace Catch