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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
// Copyright (c) 2026 Progmasoft.
// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0
#include <Progmasoft/Runner/History.hpp>
#include <Progmasoft/Runner/Json.hpp>
#include <Progmasoft/Runner/ResultImport.hpp>
#include <algorithm>
#include <cctype>
#include <cmath>
#include <iterator>
#include <map>
#include <utility>
namespace Progmasoft::Catch3::Runner
{
std::string_view
ImportTrim(std::string_view text)
{
const auto blank = [](char character) {
return std::isspace(static_cast<unsigned char>(character)) != 0;
};
while (!text.empty() && blank(text.front()))
{
text.remove_prefix(1);
}
while (!text.empty() && blank(text.back()))
{
text.remove_suffix(1);
}
return text;
}
bool
ImportStartsWith(std::string_view text, std::string_view prefix)
{
return text.substr(0, prefix.size()) == prefix;
}
std::vector<std::string_view>
ImportLines(std::string_view text)
{
std::vector<std::string_view> lines;
while (!text.empty())
{
const auto end = text.find('\n');
auto line = text.substr(0, end);
if (!line.empty() && line.back() == '\r')
{
line.remove_suffix(1);
}
lines.push_back(line);
if (end == std::string_view::npos)
{
break;
}
text.remove_prefix(end + 1);
}
return lines;
}
namespace
{
namespace ImportText
{
/// Without a byte order mark and the white space before the
/// first character that counts.
std::string_view
Contents(std::string_view text)
{
if (ImportStartsWith(text, "\xEF\xBB\xBF"))
{
text.remove_prefix(3);
}
return ImportTrim(text);
}
/// The name of the root element of an XML document.
std::string_view
RootElement(std::string_view text)
{
while (!text.empty())
{
const auto open = text.find('<');
if (open == std::string_view::npos
|| open + 1 >= text.size())
{
return {};
}
text.remove_prefix(open + 1);
if (text.front() == '?' || text.front() == '!')
{
const auto close = text.find('>');
if (close == std::string_view::npos)
{
return {};
}
text.remove_prefix(close + 1);
continue;
}
return text.substr(0, text.find_first_of(" \t\r\n/>"));
}
return {};
}
} // namespace ImportText
} // namespace
std::optional<ImportFormat>
DetectImportFormat(std::string_view text)
{
const std::string_view contents = ImportText::Contents(text);
if (contents.empty())
{
return std::nullopt;
}
if (contents.front() == '<')
{
const std::string_view root = ImportText::RootElement(contents);
if (root == "testsuites" || root == "testsuite")
{
return ImportFormat::kJUnit;
}
if (root == "assemblies" || root == "assembly")
{
return ImportFormat::kXUnit;
}
if (root == "TestRun")
{
return ImportFormat::kTrx;
}
return std::nullopt;
}
if (contents.front() == '{')
{
auto whole = ParseJson(contents);
if (!whole)
{
// JSON Lines: the first line says what the others are.
whole = ParseJson(contents.substr(0, contents.find('\n')));
}
return whole ? ImportJsonFormat(*whole) : std::nullopt;
}
for (const auto raw : ImportLines(contents))
{
const std::string_view line = ImportTrim(raw);
if (line.empty() || line.front() == '#')
{
continue;
}
if (ImportStartsWith(line, "TAP version")
|| ImportStartsWith(line, "1..") || IsTapTestLine(line))
{
return ImportFormat::kTap;
}
break;
}
return std::nullopt;
}
Expected<std::vector<TestResult>>
ParseImport(std::string_view text,
ImportFormat format,
const std::string &source)
{
const std::string_view contents = ImportText::Contents(text);
switch (format)
{
case ImportFormat::kAuto:
break;
case ImportFormat::kCatch3:
return ParseCatch3Report(contents);
case ImportFormat::kJUnit:
return ParseJUnit(contents);
case ImportFormat::kXUnit:
return ParseXUnit(contents);
case ImportFormat::kTrx:
return ParseTrx(contents);
case ImportFormat::kTap:
return ParseTap(contents, source);
case ImportFormat::kCtrf:
return ParseCtrf(contents);
}
const auto detected = DetectImportFormat(contents);
if (!detected)
{
return Error{ "its format is not one Catch3 knows; "
"--import-format names it" };
}
return ParseImport(contents, *detected, source);
}
std::vector<TestResult>
MergeImported(std::vector<TestResult> results)
{
std::vector<TestResult> tests;
std::map<std::string, std::size_t> positions;
for (auto &result : results)
{
if (result.id.empty())
{
result.id = History::KeyOf(result);
}
const auto [position, added]
= positions.emplace(result.id, tests.size());
if (added)
{
tests.push_back(std::move(result));
continue;
}
TestResult &test = tests[position->second];
test.flaky = test.flaky || result.flaky;
std::move(result.attempts.begin(),
result.attempts.end(),
std::back_inserter(test.attempts));
}
for (auto &test : tests)
{
std::size_t passes = 0;
std::size_t failures = 0;
for (std::size_t index = 0; index < test.attempts.size(); ++index)
{
Attempt &attempt = test.attempts[index];
attempt.number = index + 1;
passes += attempt.outcome == Outcome::kPassed ? 1 : 0;
failures += IsFailure(attempt.outcome) ? 1 : 0;
}
if (test.attempts.empty())
{
continue;
}
// A report of a skip says nothing about an earlier failure.
const auto verdict
= std::find_if(test.attempts.rbegin(),
test.attempts.rend(),
[](const Attempt &attempt) {
return IsFailure(attempt.outcome);
});
const Attempt &last = verdict != test.attempts.rend()
? *verdict
: test.attempts.back();
test.outcome = last.outcome;
test.category = last.category;
if (passes > 0 && failures > 0)
{
test.outcome = Outcome::kPassed;
test.category = FailureCategory::kNone;
test.flaky = true;
test.analysis.push_back(
"flaky: failed " + std::to_string(failures) + " of "
+ std::to_string(test.attempts.size()) + " attempts");
}
}
return tests;
}
TestResult
ImportedTest(std::string name, std::string className)
{
TestResult test;
test.name = std::move(name);
test.className = std::move(className);
test.id = History::KeyOf(test);
return test;
}
Attempt
ImportedAttempt(Outcome outcome,
std::int64_t durationMicroseconds,
std::string message,
std::string details,
FailureCategory category)
{
Attempt attempt;
attempt.outcome = outcome;
attempt.durationMicroseconds
= std::max<std::int64_t>(durationMicroseconds, 0);
if (outcome == Outcome::kSkipped)
{
attempt.standardOutput = std::move(message);
return attempt;
}
if (outcome != Outcome::kFailedAsExpected && !IsFailure(outcome))
{
return attempt;
}
if (category == FailureCategory::kNone)
{
category = outcome == Outcome::kCrashed ? FailureCategory::kCrash
: outcome == Outcome::kTimedOut
? FailureCategory::kTimeout
: FailureCategory::kAssertion;
}
if (message.empty())
{
message = details.substr(0, details.find('\n'));
}
attempt.category = category;
attempt.assertionsFailed = 1;
AssertionRecord assertion;
assertion.message = std::move(message);
assertion.expansion = std::move(details);
assertion.category = category;
assertion.allowed = outcome == Outcome::kFailedAsExpected;
attempt.assertions.push_back(std::move(assertion));
return attempt;
}
std::int64_t
ImportedMicroseconds(double seconds)
{
// About thirty years; a longer duration is not a duration.
constexpr double kLongest = 1e9;
if (!std::isfinite(seconds) || seconds <= 0.0 || seconds > kLongest)
{
return 0;
}
return static_cast<std::int64_t>(std::llround(seconds * 1e6));
}
} // namespace Progmasoft::Catch3::Runner
|