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

summaryrefslogtreecommitdiffhomepage
path: root/src/Progmasoft/Runner/ResultImportJson.cpp
blob: 1651f9384a0dfa83a7369b685036216fd6840673 (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
// Copyright (c) 2026 Progmasoft.
// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0

// The JSON reports: Catch3's own and CTRF.
#include <Progmasoft/Runner/Json.hpp>
#include <Progmasoft/Runner/ResultImport.hpp>
#include <Progmasoft/Runner/ResultsJson.hpp>
#include <utility>

namespace Progmasoft::Catch3::Runner
{
    namespace
    {
        namespace ImportJson
        {
            /// A format of Catch3's own or CTRF, from the parsed document.
            std::optional<ImportFormat>
            FormatOf(const JsonValue &value)
            {
                if (value["format"].AsString() == "catch3-run"
                    || value["format"].AsString() == "catch3-results")
                {
                    return ImportFormat::kCatch3;
                }
                if (value["results"]["tests"].IsArray())
                {
                    return ImportFormat::kCtrf;
                }
                return std::nullopt;
            }

            /// What a test owes the verdict of an earlier run is worked out
            /// again here, from this run's point of view.
            void
            Forget(TestResult &test)
            {
                test.quarantined = false;
                test.history = FailureHistory::kUnknown;
                test.selectionReason.clear();
                test.signature.clear();
                test.analysis.clear();
                test.replayCommand.clear();
                test.predictedDurationMicroseconds = 0;
                test.predictedFailureProbability = 0.0;
            }

            Expected<TestResult>
            ReadLine(const JsonValue &value)
            {
                TestResult test = TestResultFromJson(value);
                Forget(test);
                if (test.name.empty())
                {
                    test.name = test.id;
                }
                if (test.name.empty())
                {
                    return Error{ "a test needs an 'id' or a 'name'" };
                }
                const std::string &outcome = value["outcome"].AsString();
                if (!OutcomeFromName(outcome))
                {
                    return Error{ "'" + test.name + "' has the outcome '"
                                  + outcome + "'" };
                }
                if (test.attempts.empty())
                {
                    Attempt attempt = ImportedAttempt(
                        test.outcome,
                        value["durationMicroseconds"].AsInteger(),
                        value["message"].AsString(),
                        value["details"].AsString(),
                        test.category);
                    attempt.standardOutput = value["standardOutput"].AsString();
                    attempt.standardError = value["standardError"].AsString();
                    test.attempts.push_back(std::move(attempt));
                }
                return test;
            }

            Expected<std::vector<TestResult>>
            ParseLines(std::string_view text)
            {
                std::vector<TestResult> tests;
                std::size_t number = 0;
                bool started = false;
                for (const auto line : ImportLines(text))
                {
                    ++number;
                    if (ImportTrim(line).empty())
                    {
                        continue;
                    }
                    const std::string where = "line " + std::to_string(number);
                    auto value = ParseJson(line);
                    if (!value)
                    {
                        return value.GetError().Context(where);
                    }
                    if (!started)
                    {
                        started = true;
                        if ((*value)["format"].AsString() != "catch3-results")
                        {
                            return Error{ "the first line must name the format "
                                          "catch3-results" };
                        }
                        if ((*value)["version"].AsInteger()
                            > kResultsFormatVersion)
                        {
                            return Error{ "the format is newer than this "
                                          "version of Catch3 reads" };
                        }
                        continue;
                    }
                    auto test = ReadLine(*value);
                    if (!test)
                    {
                        return test.GetError().Context(where);
                    }
                    tests.push_back(std::move(*test));
                }
                return tests;
            }

            Expected<std::vector<TestResult>>
            ParseCatch3(std::string_view text)
            {
                auto whole = ParseJson(text);
                if (!whole)
                {
                    return ParseLines(text);
                }
                if ((*whole)["format"].AsString() != "catch3-run")
                {
                    return ParseLines(text);
                }
                auto run = RunResultFromJson(*whole);
                if (!run)
                {
                    return run.GetError();
                }
                for (auto &test : run->tests)
                {
                    Forget(test);
                }
                return std::move(run->tests);
            }

            std::string
            TextOf(const JsonValue &value)
            {
                if (!value.IsArray())
                {
                    return value.AsString();
                }
                std::string text;
                for (const auto &item : value.Items())
                {
                    text += item.AsString() + "\n";
                }
                return text;
            }

            Outcome
            CtrfOutcome(const std::string &status)
            {
                if (status == "passed")
                {
                    return Outcome::kPassed;
                }
                if (status == "failed")
                {
                    return Outcome::kFailed;
                }
                if (status == "skipped" || status == "pending")
                {
                    return Outcome::kSkipped;
                }
                return Outcome::kNotRun;
            }

            /// A test, or one retry of it: the two share these fields.
            Attempt
            CtrfAttempt(const JsonValue &value)
            {
                Attempt attempt = ImportedAttempt(
                    CtrfOutcome(value["status"].AsString()),
                    ImportedMicroseconds(value["duration"].AsNumber() / 1e3),
                    value["message"].AsString(),
                    value["trace"].AsString());
                attempt.standardOutput = TextOf(value["stdout"]);
                attempt.standardError = TextOf(value["stderr"]);
                return attempt;
            }

            std::string
            CtrfSuite(const JsonValue &suite)
            {
                if (!suite.IsArray())
                {
                    return suite.AsString();
                }
                std::string text;
                for (const auto &item : suite.Items())
                {
                    text += (text.empty() ? "" : " > ") + item.AsString();
                }
                return text;
            }

            Expected<std::vector<TestResult>>
            ParseCtrf(std::string_view text)
            {
                auto document = ParseJson(text);
                if (!document)
                {
                    return document.GetError();
                }
                const JsonValue &tests = (*document)["results"]["tests"];
                if (!tests.IsArray())
                {
                    return Error{ "a CTRF report has results.tests" };
                }
                std::vector<TestResult> results;
                for (const auto &item : tests.Items())
                {
                    const std::string &name = item["name"].AsString();
                    if (name.empty())
                    {
                        return Error{ "a CTRF test has no name" };
                    }
                    TestResult test
                        = ImportedTest(name, CtrfSuite(item["suite"]));
                    test.file = item["filePath"].AsString();
                    test.line
                        = static_cast<std::size_t>(item["line"].AsUnsigned());
                    test.flaky = item["flaky"].AsBoolean();
                    for (const auto &tag : item["tags"].Items())
                    {
                        test.tags.push_back(tag.AsString());
                    }
                    for (const auto &retry : item["retryAttempts"].Items())
                    {
                        test.attempts.push_back(CtrfAttempt(retry));
                    }
                    // The retries end before the last attempt, which is the
                    // test itself.
                    test.attempts.push_back(CtrfAttempt(item));
                    results.push_back(std::move(test));
                }
                return results;
            }
        } // namespace ImportJson
    } // namespace

    std::optional<ImportFormat>
    ImportJsonFormat(const JsonValue &value)
    {
        return ImportJson::FormatOf(value);
    }

    Expected<std::vector<TestResult>>
    ParseCatch3Report(std::string_view text)
    {
        return ImportJson::ParseCatch3(text);
    }

    Expected<std::vector<TestResult>>
    ParseCtrf(std::string_view text)
    {
        return ImportJson::ParseCtrf(text);
    }
} // namespace Progmasoft::Catch3::Runner