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

summaryrefslogtreecommitdiffhomepage
path: root/src/Progmasoft/XmlWriter.cpp
blob: f01d4c450ee4605ac4b0c48e10862c640ea16618 (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
// Copyright (c) 2026 Progmasoft.
// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-1.1
#include <Progmasoft/Catch3/Results.hpp>
#include <Progmasoft/Catch3/XmlWriter.hpp>

#include <ostream>
#include <sstream>
#include <string_view>

namespace Progmasoft::Catch3
{
    namespace
    {
        struct DecodedCharacter final
        {
            char32_t value = 0;
            std::size_t length = 0;
        };

        DecodedCharacter
        DecodeUtf8(std::string_view text, std::size_t offset) noexcept
        {
            const auto first = static_cast<unsigned char>(text[offset]);
            if (first < 0x80)
            {
                return {first, 1};
            }
            std::size_t length = 0;
            char32_t value = 0;
            char32_t minimum = 0;
            if (first >= 0xC2 && first <= 0xDF)
            {
                length = 2;
                value = first & 0x1F;
                minimum = 0x80;
            }
            else if (first >= 0xE0 && first <= 0xEF)
            {
                length = 3;
                value = first & 0x0F;
                minimum = 0x800;
            }
            else if (first >= 0xF0 && first <= 0xF4)
            {
                length = 4;
                value = first & 0x07;
                minimum = 0x10000;
            }
            else
            {
                return {0xFFFD, 0};
            }
            if (offset + length > text.size())
            {
                return {0xFFFD, 0};
            }
            for (std::size_t index = 1; index < length; ++index)
            {
                const auto continuation =
                    static_cast<unsigned char>(text[offset + index]);
                if ((continuation & 0xC0) != 0x80)
                {
                    return {0xFFFD, 0};
                }
                value = static_cast<char32_t>((value << 6) |
                                              (continuation & 0x3F));
            }
            if (value < minimum || value > 0x10FFFF ||
                (value >= 0xD800 && value <= 0xDFFF))
            {
                return {0xFFFD, 0};
            }
            return {value, length};
        }

        bool
        IsXmlCharacter(char32_t value) noexcept
        {
            return value == 0x09 || value == 0x0A || value == 0x0D ||
                   (value >= 0x20 && value <= 0xD7FF) ||
                   (value >= 0xE000 && value <= 0xFFFD) ||
                   (value >= 0x10000 && value <= 0x10FFFF);
        }

        void
        WriteEscaped(std::string_view value, std::ostream &output, bool attribute)
        {
            for (std::size_t index = 0; index < value.size();)
            {
                const DecodedCharacter character = DecodeUtf8(value, index);
                if (character.length == 0 || !IsXmlCharacter(character.value))
                {
                    // A character XML cannot hold is replaced, not dropped,
                    // so the reader sees that something was there.
                    output << "&#xFFFD;";
                    ++index;
                    continue;
                }
                if (character.value < 0x80)
                {
                    switch (static_cast<char>(character.value))
                    {
                        case '&':
                            output << "&amp;";
                            break;
                        case '<':
                            output << "&lt;";
                            break;
                        case '>':
                            output << "&gt;";
                            break;
                        case '"':
                            output << (attribute ? "&quot;" : "\"");
                            break;
                        case '\'':
                            output << (attribute ? "&apos;" : "'");
                            break;
                        case '\n':
                            output << (attribute ? "&#10;" : "\n");
                            break;
                        default:
                            output.put(static_cast<char>(character.value));
                    }
                }
                else
                {
                    output.write(value.data() + index,
                                 static_cast<std::streamsize>(character.length));
                }
                index += character.length;
            }
        }

        void
        WriteAttribute(std::ostream &output,
                       std::string_view name,
                       std::string_view value)
        {
            output << ' ' << name << "=\"";
            WriteEscaped(value, output, true);
            output << '"';
        }

        std::string
        JUnitSeconds(std::int64_t microseconds)
        {
            const bool negative = microseconds < 0;
            const auto magnitude = negative ? -microseconds : microseconds;
            std::string fraction = std::to_string(magnitude % 1000000);
            fraction.insert(0, 6 - fraction.size(), '0');
            return (negative ? "-" : "") + std::to_string(magnitude / 1000000) +
                   "." + fraction;
        }

        void
        WriteIndent(std::ostream &output, unsigned level)
        {
            for (unsigned index = 0; index < level; ++index)
            {
                output << "  ";
            }
        }

        void
        WriteTextElement(std::ostream &output,
                         unsigned indent,
                         std::string_view name,
                         std::string_view value)
        {
            WriteIndent(output, indent);
            output << '<' << name << '>';
            WriteEscaped(value, output, false);
            output << "</" << name << ">\n";
        }

        std::string
        Output(const Attempt &attempt, bool errors)
        {
            if (errors)
            {
                return attempt.standardError + attempt.processOutput;
            }
            return attempt.standardOutput;
        }

        /// An earlier attempt: `<rerunFailure>` before a final failure,
        /// `<flakyFailure>` before a final pass.
        void
        WriteEarlierAttempt(std::ostream &output,
                            const Attempt &attempt,
                            std::string_view element)
        {
            WriteIndent(output, 3);
            output << '<' << element;
            WriteAttribute(output, "message", SummarizeFailure(attempt));
            WriteAttribute(output, "type", CategoryName(attempt.category));
            output << ">\n";
            WriteTextElement(output, 4, "stackTrace", DescribeFailure(attempt));
            if (!attempt.standardOutput.empty())
            {
                WriteTextElement(
                    output, 4, "system-out", Output(attempt, false));
            }
            if (!Output(attempt, true).empty())
            {
                WriteTextElement(output, 4, "system-err", Output(attempt, true));
            }
            WriteIndent(output, 3);
            output << "</" << element << ">\n";
        }

        std::string
        SkipReason(const TestResult &test)
        {
            switch (test.outcome)
            {
                case Outcome::kBlocked:
                    return test.analysis.empty() ? "a dependency did not pass"
                                                 : test.analysis.front();
                case Outcome::kNotRun:
                    return "the run stopped before this test";
                case Outcome::kUpToDate:
                    return "passed before; nothing it depends on changed";
                default:
                    return "skipped";
            }
        }

        void
        WriteTestCase(std::ostream &output,
                      const TestResult &test,
                      std::string_view suiteName)
        {
            WriteIndent(output, 2);
            output << "<testcase";
            WriteAttribute(output,
                           "classname",
                           test.className.empty() ? suiteName : test.className);
            WriteAttribute(output, "name", test.name);
            WriteAttribute(output, "time", JUnitSeconds(test.DurationMicroseconds()));
            if (!test.file.empty())
            {
                WriteAttribute(output, "file", test.file);
                WriteAttribute(output, "line", std::to_string(test.line));
            }
            output << ">\n";

            const Attempt *last =
                test.attempts.empty() ? nullptr : &test.attempts.back();
            const bool failedRun = IsFailure(test.outcome);
            if (failedRun && last != nullptr)
            {
                const bool error = test.outcome != Outcome::kFailed ||
                                   last->category ==
                                       FailureCategory::kInfrastructure;
                const std::string_view element = error ? "error" : "failure";
                WriteIndent(output, 3);
                output << '<' << element;
                WriteAttribute(output, "message", SummarizeFailure(*last));
                WriteAttribute(output, "type", CategoryName(test.category));
                output << '>';
                std::string details = DescribeFailure(*last);
                for (const auto &finding : test.analysis)
                {
                    details += "analysis: " + finding + "\n";
                }
                if (!test.replayCommand.empty())
                {
                    details += "replay: " + test.replayCommand + "\n";
                }
                WriteEscaped(details, output, false);
                output << "</" << element << ">\n";
            }
            else if (test.outcome == Outcome::kSkipped ||
                     test.outcome == Outcome::kBlocked ||
                     test.outcome == Outcome::kNotRun ||
                     test.outcome == Outcome::kUpToDate)
            {
                WriteIndent(output, 3);
                output << "<skipped";
                WriteAttribute(output, "message", SkipReason(test));
                output << "/>\n";
            }
            // Every earlier failed attempt, so retries stay visible.
            for (std::size_t index = 0; index + 1 < test.attempts.size();
                 ++index)
            {
                const Attempt &attempt = test.attempts[index];
                if (!IsFailure(attempt.outcome))
                {
                    continue;
                }
                WriteEarlierAttempt(output,
                                    attempt,
                                    failedRun ? "rerunFailure" : "flakyFailure");
            }
            if (last != nullptr && !last->standardOutput.empty())
            {
                WriteTextElement(output, 3, "system-out", Output(*last, false));
            }
            if (last != nullptr && !Output(*last, true).empty())
            {
                WriteTextElement(output, 3, "system-err", Output(*last, true));
            }
            WriteIndent(output, 2);
            output << "</testcase>\n";
        }
    } // namespace

    bool
    JUnitXmlWriter::Write(const RunResult &run, std::ostream &output)
    {
        const RunSummary summary = Summarize(run);
        const std::size_t errors = summary.crashed + summary.timedOut;
        const std::size_t failures = summary.failed;
        const std::size_t skipped =
            summary.skipped + summary.blocked + summary.notRun +
            summary.upToDate;
        const std::string time = JUnitSeconds(run.wallMicroseconds);
        output << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<testsuites";
        WriteAttribute(output, "name", run.binary);
        WriteAttribute(output, "tests", std::to_string(summary.tests));
        WriteAttribute(output, "failures", std::to_string(failures));
        WriteAttribute(output, "errors", std::to_string(errors));
        WriteAttribute(output, "skipped", std::to_string(skipped));
        WriteAttribute(output, "time", time);
        output << ">\n";
        WriteIndent(output, 1);
        output << "<testsuite";
        WriteAttribute(output, "name", run.binary);
        WriteAttribute(output, "tests", std::to_string(summary.tests));
        WriteAttribute(output, "failures", std::to_string(failures));
        WriteAttribute(output, "errors", std::to_string(errors));
        WriteAttribute(output, "skipped", std::to_string(skipped));
        WriteAttribute(output, "time", time);
        output << ">\n";
        WriteIndent(output, 2);
        output << "<properties>\n";
        const auto property = [&](std::string_view name,
                                  const std::string &value) {
            WriteIndent(output, 3);
            output << "<property";
            WriteAttribute(output, "name", name);
            WriteAttribute(output, "value", value);
            output << "/>\n";
        };
        property("random-seed", std::to_string(run.seed));
        property("workers", std::to_string(run.workers));
        if (run.shardCount > 1)
        {
            property("shard",
                     std::to_string(run.shardIndex) + "/" +
                         std::to_string(run.shardCount));
        }
        property("flaky", std::to_string(summary.flaky));
        property("quarantined", std::to_string(summary.quarantined));
        WriteIndent(output, 2);
        output << "</properties>\n";
        for (const auto &test : run.tests)
        {
            WriteTestCase(output, test, run.binary);
        }
        WriteIndent(output, 1);
        output << "</testsuite>\n</testsuites>\n";
        return static_cast<bool>(output);
    }

    std::string
    JUnitXmlWriter::ToString(const RunResult &run)
    {
        std::ostringstream output;
        static_cast<void>(Write(run, output));
        return output.str();
    }
} // namespace Progmasoft::Catch3