Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/src/Progmasoft/Runner/ResultImportXml.cpp
blob: ddbed989e1c07415e4a07852d152c2c2c2d5bce6 (plain)
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
// Copyright (c) 2026 Progmasoft.
// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0

// The XML reports: JUnit, xUnit.net v2 and Visual Studio's TRX.
#include <Progmasoft/Runner/ResultImport.hpp>
#include <cstdlib>
#include <map>
#include <string_view>
#include <utility>

#include "../PugiXml.inl"

namespace Progmasoft::Catch3::Runner
{
    namespace
    {
        namespace ImportXml
        {
            Expected<bool>
            Load(pugi::xml_document &document, std::string_view text)
            {
                const pugi::xml_parse_result parsed
                    = document.load_buffer(text.data(), text.size());
                if (!parsed)
                {
                    return Error{ std::string{ "it is not valid XML: " }
                                  + parsed.description() + " at byte "
                                  + std::to_string(parsed.offset) };
                }
                return true;
            }

            double
            NumberOf(const char *text)
            {
                std::string digits;
                for (; *text != '\0'; ++text)
                {
                    if (*text != ',')
                    {
                        digits.push_back(*text);
                    }
                }
                return std::strtod(digits.c_str(), nullptr);
            }

            std::string
            TextOf(pugi::xml_node node)
            {
                return node.text().get();
            }

            /// `error` and `failure` of JUnit and what Surefire writes for
            /// retries, which have the same shape.
            Attempt
            JUnitFailure(pugi::xml_node node, FailureCategory category)
            {
                std::string details = TextOf(node);
                if (const auto trace = node.child("stackTrace"))
                {
                    details = TextOf(trace);
                }
                Attempt attempt
                    = ImportedAttempt(Outcome::kFailed,
                                      ImportedMicroseconds(NumberOf(
                                          node.attribute("time").value())),
                                      node.attribute("message").value(),
                                      std::move(details),
                                      category);
                attempt.assertions.front().macro
                    = node.attribute("type").value();
                attempt.standardOutput = TextOf(node.child("system-out"));
                attempt.standardError = TextOf(node.child("system-err"));
                return attempt;
            }

            bool
            NameIs(pugi::xml_node node, std::string_view name)
            {
                return name == node.name();
            }

            bool
            NameEndsWithError(pugi::xml_node node)
            {
                return NameIs(node, "flakyError") || NameIs(node, "rerunError");
            }

            bool
            NotRunStatus(pugi::xml_node testCase)
            {
                const std::string_view status
                    = testCase.attribute("status").value();
                const std::string_view result
                    = testCase.attribute("result").value();
                return status == "notrun" || status == "skipped"
                       || status == "disabled" || result == "skipped"
                       || result == "suppressed";
            }

            void
            ReadJUnitCase(pugi::xml_node testCase,
                          pugi::xml_node suite,
                          std::vector<TestResult> &tests)
            {
                TestResult test
                    = ImportedTest(testCase.attribute("name").value(),
                                   testCase.attribute("classname").value());
                test.file = testCase.attribute("file").value();
                for (auto outer = suite; test.file.empty() && outer;
                     outer = outer.parent())
                {
                    test.file = outer.attribute("file").value();
                }
                test.line = testCase.attribute("line").as_ullong();

                std::vector<Attempt> failures;
                std::optional<Attempt> skipped;
                for (const auto child : testCase.children())
                {
                    if (NameIs(child, "flakyFailure")
                        || NameIs(child, "rerunFailure")
                        || NameEndsWithError(child))
                    {
                        test.attempts.push_back(JUnitFailure(
                            child,
                            NameEndsWithError(child)
                                ? FailureCategory::kUnexpectedException
                                : FailureCategory::kAssertion));
                    }
                    else if (NameIs(child, "failure"))
                    {
                        failures.push_back(
                            JUnitFailure(child, FailureCategory::kAssertion));
                    }
                    else if (NameIs(child, "error"))
                    {
                        failures.push_back(JUnitFailure(
                            child,
                            FailureCategory::kUnexpectedException));
                    }
                    else if (NameIs(child, "skipped"))
                    {
                        std::string reason = child.attribute("message").value();
                        skipped = ImportedAttempt(
                            Outcome::kSkipped,
                            0,
                            reason.empty() ? TextOf(child) : std::move(reason));
                    }
                }

                const std::int64_t duration = ImportedMicroseconds(
                    NumberOf(testCase.attribute("time").value()));
                Attempt verdict;
                if (!failures.empty())
                {
                    // Several failures are one attempt with several findings.
                    verdict = std::move(failures.front());
                    for (std::size_t index = 1; index < failures.size();
                         ++index)
                    {
                        auto &record = failures[index].assertions.front();
                        verdict.assertions.push_back(std::move(record));
                        ++verdict.assertionsFailed;
                    }
                    verdict.durationMicroseconds = duration;
                }
                else if (skipped)
                {
                    verdict = std::move(*skipped);
                }
                else if (NotRunStatus(testCase))
                {
                    verdict = ImportedAttempt(Outcome::kSkipped, 0);
                }
                else
                {
                    verdict = ImportedAttempt(Outcome::kPassed, duration);
                }
                if (verdict.standardOutput.empty())
                {
                    verdict.standardOutput
                        = TextOf(testCase.child("system-out"));
                }
                if (verdict.standardError.empty())
                {
                    verdict.standardError
                        = TextOf(testCase.child("system-err"));
                }
                test.attempts.push_back(std::move(verdict));
                tests.push_back(std::move(test));
            }

            void
            ReadJUnitSuite(pugi::xml_node suite, std::vector<TestResult> &tests)
            {
                for (const auto child : suite.children())
                {
                    if (NameIs(child, "testcase"))
                    {
                        ReadJUnitCase(child, suite, tests);
                    }
                    else if (NameIs(child, "testsuite"))
                    {
                        ReadJUnitSuite(child, tests);
                    }
                }
            }

            void
            ReadXUnitTest(pugi::xml_node node, std::vector<TestResult> &tests)
            {
                TestResult test = ImportedTest(node.attribute("name").value(),
                                               node.attribute("type").value());
                test.file = node.attribute("source-file").value();
                test.line = node.attribute("source-line").as_ullong();
                for (const auto trait : node.child("traits").children("trait"))
                {
                    test.tags.push_back(
                        std::string{ trait.attribute("name").value() } + "="
                        + trait.attribute("value").value());
                }
                const std::string_view result
                    = node.attribute("result").value();
                const std::int64_t duration = ImportedMicroseconds(
                    NumberOf(node.attribute("time").value()));
                Attempt attempt;
                if (result == "Pass")
                {
                    attempt = ImportedAttempt(Outcome::kPassed, duration);
                }
                else if (result == "Fail")
                {
                    const auto failure = node.child("failure");
                    attempt
                        = ImportedAttempt(Outcome::kFailed,
                                          duration,
                                          TextOf(failure.child("message")),
                                          TextOf(failure.child("stack-trace")));
                    attempt.assertions.front().macro
                        = failure.attribute("exception-type").value();
                }
                else if (result == "Skip")
                {
                    attempt = ImportedAttempt(Outcome::kSkipped,
                                              0,
                                              TextOf(node.child("reason")));
                }
                else
                {
                    attempt = ImportedAttempt(Outcome::kNotRun, 0);
                }
                if (attempt.standardOutput.empty())
                {
                    attempt.standardOutput = TextOf(node.child("output"));
                }
                test.attempts.push_back(std::move(attempt));
                tests.push_back(std::move(test));
            }

            /// A failure outside any test, such as in a fixture.
            void
            ReadXUnitErrors(pugi::xml_node assembly,
                            std::vector<TestResult> &tests)
            {
                for (const auto error :
                     assembly.child("errors").children("error"))
                {
                    const auto failure = error.child("failure");
                    TestResult test = ImportedTest(
                        std::string{ error.attribute("type").value() } + " "
                            + error.attribute("name").value(),
                        assembly.attribute("name").value());
                    test.attempts.push_back(
                        ImportedAttempt(Outcome::kFailed,
                                        0,
                                        TextOf(failure.child("message")),
                                        TextOf(failure.child("stack-trace")),
                                        FailureCategory::kInfrastructure));
                    tests.push_back(std::move(test));
                }
            }

            void
            ReadXUnitAssembly(pugi::xml_node assembly,
                              std::vector<TestResult> &tests)
            {
                ReadXUnitErrors(assembly, tests);
                for (const auto child : assembly.children())
                {
                    if (NameIs(child, "test"))
                    {
                        ReadXUnitTest(child, tests);
                    }
                    else if (NameIs(child, "collection"))
                    {
                        for (const auto test : child.children("test"))
                        {
                            ReadXUnitTest(test, tests);
                        }
                    }
                }
            }

            /// `[days.]hours:minutes:seconds[.fraction]`.
            std::int64_t
            TrxDuration(std::string_view text)
            {
                double seconds = 0.0;
                std::string field;
                const auto addField = [&] {
                    seconds = seconds * 60.0 + NumberOf(field.c_str());
                    field.clear();
                };
                double days = 0.0;
                for (std::size_t index = 0; index < text.size(); ++index)
                {
                    if (text[index] == ':')
                    {
                        addField();
                    }
                    else if (text[index] == '.'
                             && text.find(':', index) != std::string_view::npos)
                    {
                        days = NumberOf(field.c_str());
                        field.clear();
                    }
                    else
                    {
                        field.push_back(text[index]);
                    }
                }
                addField();
                return ImportedMicroseconds(seconds + days * 86400.0);
            }

            struct TrxOutcome final
            {
                Outcome outcome = Outcome::kNotRun;
                FailureCategory category = FailureCategory::kNone;
            };

            TrxOutcome
            TrxOutcomeOf(std::string_view outcome)
            {
                if (outcome == "Passed" || outcome == "PassedButRunAborted"
                    || outcome == "Warning" || outcome == "Completed")
                {
                    return { Outcome::kPassed, FailureCategory::kNone };
                }
                if (outcome == "Failed")
                {
                    return { Outcome::kFailed, FailureCategory::kAssertion };
                }
                if (outcome == "Error")
                {
                    return { Outcome::kFailed,
                             FailureCategory::kUnexpectedException };
                }
                if (outcome == "Timeout")
                {
                    return { Outcome::kTimedOut, FailureCategory::kTimeout };
                }
                if (outcome == "Aborted")
                {
                    return { Outcome::kCrashed, FailureCategory::kCrash };
                }
                if (outcome == "Disconnected")
                {
                    return { Outcome::kCrashed,
                             FailureCategory::kInfrastructure };
                }
                if (outcome == "NotExecuted" || outcome == "Inconclusive"
                    || outcome == "NotRunnable")
                {
                    return { Outcome::kSkipped, FailureCategory::kNone };
                }
                return {};
            }

            /// The class of a test: the type without its assembly.
            std::string
            TrxClass(std::string_view className)
            {
                return std::string{ className.substr(0, className.find(',')) };
            }

            void
            ReadTrxResult(pugi::xml_node result,
                          const std::map<std::string, std::string> &classes,
                          std::vector<TestResult> &tests)
            {
                // A data-driven test: the rows are what ran.
                if (const auto inner = result.child("InnerResults"))
                {
                    for (const auto row : inner.children())
                    {
                        ReadTrxResult(row, classes, tests);
                    }
                    return;
                }
                const auto found
                    = classes.find(result.attribute("testId").value());
                TestResult test = ImportedTest(
                    result.attribute("testName").value(),
                    found == classes.end() ? std::string{} : found->second);
                const auto [outcome, category]
                    = TrxOutcomeOf(result.attribute("outcome").value());
                const auto output = result.child("Output");
                const auto error = output.child("ErrorInfo");
                std::string message = TextOf(error.child("Message"));
                Attempt attempt = ImportedAttempt(
                    outcome,
                    TrxDuration(result.attribute("duration").value()),
                    outcome == Outcome::kSkipped
                        ? TextOf(output.child("StdOut"))
                        : std::move(message),
                    TextOf(error.child("StackTrace")),
                    category);
                if (outcome != Outcome::kSkipped)
                {
                    attempt.standardOutput = TextOf(output.child("StdOut"));
                }
                attempt.standardError = TextOf(output.child("StdErr"));
                test.attempts.push_back(std::move(attempt));
                tests.push_back(std::move(test));
            }
        } // namespace ImportXml
    } // namespace

    Expected<std::vector<TestResult>>
    ParseJUnit(std::string_view text)
    {
        pugi::xml_document document;
        if (auto loaded = ImportXml::Load(document, text); !loaded)
        {
            return loaded.GetError();
        }
        const pugi::xml_node root = document.document_element();
        std::vector<TestResult> tests;
        if (ImportXml::NameIs(root, "testsuites")
            || ImportXml::NameIs(root, "testsuite"))
        {
            ImportXml::ReadJUnitSuite(root, tests);
            return tests;
        }
        return Error{ "a JUnit report has testsuites or testsuite as its root, "
                      "not "
                      + std::string{ root.name() } };
    }

    Expected<std::vector<TestResult>>
    ParseXUnit(std::string_view text)
    {
        pugi::xml_document document;
        if (auto loaded = ImportXml::Load(document, text); !loaded)
        {
            return loaded.GetError();
        }
        const pugi::xml_node root = document.document_element();
        std::vector<TestResult> tests;
        if (ImportXml::NameIs(root, "assembly"))
        {
            ImportXml::ReadXUnitAssembly(root, tests);
            return tests;
        }
        if (ImportXml::NameIs(root, "assemblies"))
        {
            for (const auto assembly : root.children("assembly"))
            {
                ImportXml::ReadXUnitAssembly(assembly, tests);
            }
            return tests;
        }
        return Error{ "an xUnit report has assemblies or assembly as its root, "
                      "not "
                      + std::string{ root.name() } };
    }

    Expected<std::vector<TestResult>>
    ParseTrx(std::string_view text)
    {
        pugi::xml_document document;
        if (auto loaded = ImportXml::Load(document, text); !loaded)
        {
            return loaded.GetError();
        }
        const pugi::xml_node root = document.document_element();
        if (!ImportXml::NameIs(root, "TestRun"))
        {
            return Error{ "a TRX report has TestRun as its root, not "
                          + std::string{ root.name() } };
        }
        std::map<std::string, std::string> classes;
        for (const auto definition : root.child("TestDefinitions").children())
        {
            classes[definition.attribute("id").value()] = ImportXml::TrxClass(
                definition.child("TestMethod").attribute("className").value());
        }
        std::vector<TestResult> tests;
        for (const auto result : root.child("Results").children())
        {
            ImportXml::ReadTrxResult(result, classes, tests);
        }
        return tests;
    }
} // namespace Progmasoft::Catch3::Runner