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

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

#include "Hash.hpp"

#include <algorithm>
#include <cctype>
#include <map>

namespace Progmasoft::Catch3::Runner
{
    namespace
    {
        /// Replaces what differs between two runs of the same failure:
        /// numbers, hexadecimal addresses and the temporary directory.
        std::string
        Normalise(std::string_view text)
        {
            std::string result;
            bool inNumber = false;
            for (std::size_t index = 0; index < text.size(); ++index)
            {
                const char character = text[index];
                const bool digit =
                    std::isdigit(static_cast<unsigned char>(character)) != 0;
                const bool hexLetter =
                    inNumber &&
                    std::isxdigit(static_cast<unsigned char>(character)) != 0;
                if (digit || hexLetter ||
                    (inNumber && (character == 'x' || character == '.')))
                {
                    if (!inNumber)
                    {
                        result.push_back('#');
                    }
                    inNumber = true;
                    continue;
                }
                inNumber = false;
                result.push_back(character);
            }
            return result;
        }

        const Attempt *
        FirstFailure(const TestResult &test)
        {
            for (const auto &attempt : test.attempts)
            {
                if (IsFailure(attempt.outcome))
                {
                    return &attempt;
                }
            }
            return nullptr;
        }

        std::string
        Seconds(std::int64_t microseconds)
        {
            const auto tenths = (microseconds + 50000) / 100000;
            return std::to_string(tenths / 10) + "." +
                   std::to_string(tenths % 10) + " s";
        }

        std::string
        OpenSections(const Attempt &attempt)
        {
            std::vector<std::string> open;
            for (const auto &section : attempt.sections)
            {
                if (section.durationMicroseconds < 0)
                {
                    open.resize(section.depth);
                    open.push_back(section.name);
                }
            }
            std::string text;
            for (const auto &name : open)
            {
                text += text.empty() ? "" : " / ";
                text += name;
            }
            return text;
        }

        std::string
        QuoteArgument(const std::string &argument)
        {
            if (argument.find_first_of(" \t\"'\\$`&|;<>()[]*?~!#") ==
                    std::string::npos &&
                !argument.empty())
            {
                return argument;
            }
            std::string quoted = "\"";
            for (const char character : argument)
            {
                if (character == '"' || character == '\\' || character == '$' ||
                    character == '`')
                {
                    quoted.push_back('\\');
                }
                quoted.push_back(character);
            }
            quoted.push_back('"');
            return quoted;
        }
    } // namespace

    std::string
    QuoteTestSpec(const std::string &name)
    {
        // Catch2's spec syntax gives these characters a meaning; a backslash
        // makes them literal.
        std::string spec;
        for (const char character : name)
        {
            if (character == ',' || character == '[' || character == ']' ||
                character == '*' || character == '\\' || character == '~' ||
                character == '"')
            {
                spec.push_back('\\');
            }
            spec.push_back(character);
        }
        return spec;
    }

    std::string
    FailureSignature(const Attempt &attempt)
    {
        std::string text = CategoryName(attempt.category);
        text += '|';
        bool described = false;
        for (const auto &assertion : attempt.assertions)
        {
            if (assertion.passed || assertion.allowed)
            {
                continue;
            }
            text += assertion.file + ":" + std::to_string(assertion.line) +
                    "|" + Normalise(assertion.message) + "|" +
                    assertion.expression;
            described = true;
            break;
        }
        if (!described)
        {
            text += Normalise(attempt.fatalMessage) + "|" +
                    attempt.lastLocation + "|" + OpenSections(attempt);
        }
        return "sig-" + ToHex(Fnv1a64(text)).substr(0, 12);
    }

    void
    Analyze(RunResult &run, const AnalysisInputs &inputs)
    {
        std::map<std::string, std::vector<std::size_t>> bySignature;
        for (std::size_t index = 0; index < run.tests.size(); ++index)
        {
            TestResult &test = run.tests[index];
            const Attempt *failure = FirstFailure(test);
            if (failure == nullptr)
            {
                continue;
            }
            test.signature = FailureSignature(*failure);
            bySignature[test.signature].push_back(index);
        }

        for (std::size_t index = 0; index < run.tests.size(); ++index)
        {
            TestResult &test = run.tests[index];
            const Attempt *failure = FirstFailure(test);
            if (failure == nullptr)
            {
                continue;
            }
            std::vector<std::string> findings;

            // How it relates to earlier runs.
            const TestHistory *past =
                inputs.history == nullptr ? nullptr
                                          : inputs.history->Find(test.id);
            const TestRecord *last = past == nullptr ? nullptr : past->Last();
            if (last == nullptr)
            {
                test.history = FailureHistory::kUnknown;
            }
            else if (test.flaky || past->FlakyEvents(20) > 0)
            {
                test.history = FailureHistory::kIntermittent;
                const auto events = past->FlakyEvents(20) + (test.flaky ? 1 : 0);
                findings.push_back("intermittent: " + std::to_string(events) +
                                   " flaky result(s) in the last 20 runs");
            }
            else if (IsFailure(last->outcome))
            {
                test.history = FailureHistory::kPersistent;
                std::size_t streak = 0;
                for (auto record = past->records.rbegin();
                     record != past->records.rend() &&
                     IsFailure(record->outcome);
                     ++record)
                {
                    ++streak;
                }
                std::string finding = "persistent: failed in the last " +
                                      std::to_string(streak) + " run(s)";
                if (!last->signature.empty() && last->signature != test.signature)
                {
                    finding += ", but differently last time";
                }
                findings.push_back(finding);
            }
            else
            {
                test.history = FailureHistory::kNew;
                std::string finding = "new failure after " +
                                      std::to_string(past->PassStreak()) +
                                      " passing run(s)";
                if (inputs.history != nullptr)
                {
                    for (const auto &runRecord : inputs.history->Runs())
                    {
                        if (runRecord.id == last->run && !runRecord.commit.empty())
                        {
                            finding += "; it last passed at commit " +
                                       runRecord.commit.substr(0, 12);
                        }
                    }
                }
                findings.push_back(finding);
            }

            // Other tests that fail the same way.
            const auto &group = bySignature[test.signature];
            if (group.size() > 1)
            {
                std::string others;
                std::size_t named = 0;
                for (const std::size_t other : group)
                {
                    if (other == index)
                    {
                        continue;
                    }
                    if (named < 3)
                    {
                        others += (named > 0 ? ", '" : "'") +
                                  run.tests[other].name + "'";
                    }
                    ++named;
                }
                if (named > 3)
                {
                    others += " and " + std::to_string(named - 3) + " more";
                }
                findings.push_back("fails the same way as " + others +
                                   ": probably one cause");
            }

            // Where it stopped.
            if (failure->outcome == Outcome::kCrashed)
            {
                std::string finding = "crashed";
                if (!failure->exitDescription.empty())
                {
                    finding += " (" + failure->exitDescription + ")";
                }
                if (!failure->lastLocation.empty())
                {
                    finding += " after the assertion at " + failure->lastLocation;
                }
                const std::string sections = OpenSections(*failure);
                if (!sections.empty())
                {
                    finding += ", in section " + sections;
                }
                findings.push_back(finding);
            }
            else if (failure->outcome == Outcome::kTimedOut)
            {
                std::string finding =
                    "stopped after " + Seconds(failure->durationMicroseconds);
                if (test.predictedDurationMicroseconds > 0 && last != nullptr)
                {
                    finding += "; it usually takes " +
                               Seconds(test.predictedDurationMicroseconds);
                }
                const std::string sections = OpenSections(*failure);
                if (!sections.empty())
                {
                    finding += "; it was in section " + sections;
                }
                if (!failure->lastLocation.empty())
                {
                    finding += "; last assertion started at " +
                               failure->lastLocation;
                }
                findings.push_back(finding);
            }

            // The change it follows.
            if (inputs.changes != nullptr)
            {
                std::vector<std::string> changedFiles;
                const auto changed = [&](const std::string &file) {
                    return std::any_of(inputs.changes->files.begin(),
                                       inputs.changes->files.end(),
                                       [&](const auto &path) {
                                           return SameSourceFile(path, file);
                                       });
                };
                if (changed(test.file))
                {
                    changedFiles.push_back(test.file);
                }
                for (const auto &assertion : failure->assertions)
                {
                    if (!assertion.passed && assertion.file != test.file &&
                        changed(assertion.file) &&
                        std::find(changedFiles.begin(),
                                  changedFiles.end(),
                                  assertion.file) == changedFiles.end())
                    {
                        changedFiles.push_back(assertion.file);
                    }
                }
                for (const auto &file : changedFiles)
                {
                    findings.push_back(
                        "changed since " +
                        (inputs.changesSince.empty() ? std::string{"HEAD"}
                                                     : inputs.changesSince) +
                        ": " + file);
                }
            }

            if (!failure->temporaryDirectory.empty())
            {
                findings.push_back("its temporary files are kept in " +
                                   failure->temporaryDirectory);
            }

            // Findings of the scheduler (order dependence, flakiness,
            // quarantine) come first.
            test.analysis.insert(
                test.analysis.end(), findings.begin(), findings.end());

            std::string replay = QuoteArgument(inputs.program) + " " +
                                 QuoteArgument(QuoteTestSpec(test.name)) +
                                 " --seed=" + std::to_string(failure->seed);
            if (!failure->precededBy.empty() && !test.flaky)
            {
                // The replay mode runs the same tests first, in the same
                // process, as the failure did.
                replay = QuoteArgument(inputs.program) + " --replay";
            }
            test.replayCommand = replay;
        }
    }
} // namespace Progmasoft::Catch3::Runner