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

summaryrefslogtreecommitdiffhomepage
path: root/tests/Progmasoft/RunnerAnalysis.tests.cpp
blob: a3e6476b9cc02ed47bc83388d3630dec04cb0719 (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
// Copyright (c) 2026 Progmasoft.
// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0

// What the runner writes down about a failure: how it relates to earlier
// runs and to other failures, where it stopped, what changed and how to
// run it again.

#include <Progmasoft/Runner/Analysis.hpp>
#include <Progmasoft/Runner/Files.hpp>
#include <Progmasoft/Runner/History.hpp>

#include <Progmasoft/Catch3/TestMacros.hpp>
#include <Progmasoft/Catch3/Matchers/String.hpp>
#include <Progmasoft/Catch3/Matchers/Vector.hpp>

#include <filesystem>
#include <string>
#include <vector>

namespace
{
    namespace Runner = Progmasoft::Catch3::Runner;
    using Catch::Matchers::ContainsSubstring;
    using Progmasoft::Catch3::AssertionRecord;
    using Progmasoft::Catch3::Attempt;
    using Progmasoft::Catch3::FailureCategory;
    using Progmasoft::Catch3::FailureHistory;
    using Progmasoft::Catch3::Outcome;
    using Progmasoft::Catch3::RunResult;
    using Progmasoft::Catch3::SectionRecord;
    using Progmasoft::Catch3::TestResult;

    /// A directory of its own, removed at the end of the test.
    class Scratch final
    {
    public:
        Scratch()
        {
            std::error_code error;
            path_ = std::filesystem::temp_directory_path(error) /
                    ("catch3-analysis-" + Runner::UniqueName());
            std::filesystem::create_directories(path_, error);
        }
        Scratch(const Scratch &) = delete;
        Scratch &
        operator=(const Scratch &) = delete;
        ~Scratch()
        {
            std::error_code error;
            std::filesystem::remove_all(path_, error);
        }

        [[nodiscard]] const std::filesystem::path &
        Path() const noexcept
        {
            return path_;
        }

    private:
        std::filesystem::path path_;
    };

    /// A test that failed one assertion.
    TestResult
    Failed(const std::string &name,
           const std::string &message = "wrong",
           std::size_t line = 10)
    {
        TestResult test;
        test.id = name;
        test.name = name;
        test.file = "src/test.cpp";
        test.line = 1;
        test.outcome = Outcome::kFailed;
        test.category = FailureCategory::kAssertion;
        Attempt attempt;
        attempt.outcome = Outcome::kFailed;
        attempt.category = FailureCategory::kAssertion;
        attempt.seed = 7;
        AssertionRecord assertion;
        assertion.file = "src/test.cpp";
        assertion.line = line;
        assertion.expression = "a == b";
        assertion.message = message;
        attempt.assertions.push_back(assertion);
        test.attempts.push_back(attempt);
        return test;
    }

    TestResult
    Passed(const std::string &name)
    {
        TestResult test;
        test.id = name;
        test.name = name;
        test.outcome = Outcome::kPassed;
        Attempt attempt;
        attempt.outcome = Outcome::kPassed;
        test.attempts.push_back(attempt);
        return test;
    }

    /// Commits runs of `tests` to a history, the last commit last.
    Runner::History
    HistoryOf(const std::filesystem::path &file,
              const std::vector<std::vector<TestResult>> &runs)
    {
        Runner::History history;
        int number = 0;
        for (const auto &tests : runs)
        {
            RunResult run;
            run.tests = tests;
            for (auto &test : run.tests)
            {
                // As the runner does before it commits a run.
                for (const auto &attempt : test.attempts)
                {
                    if (Progmasoft::Catch3::IsFailure(attempt.outcome))
                    {
                        test.signature = Runner::FailureSignature(attempt);
                        break;
                    }
                }
            }
            Runner::RunRecord record;
            record.commit = "commit" + std::to_string(++number) + "0123456789";
            auto committed =
                Runner::History::Commit(file, run, record, {}, {});
            REQUIRE(committed);
            history = *committed;
        }
        return history;
    }

    std::string
    Findings(const TestResult &test)
    {
        std::string text;
        for (const auto &finding : test.analysis)
        {
            text += finding + "\n";
        }
        return text;
    }
} // namespace

TEST_CASE("A failure is placed in the test's history",
          "[progmasoft][runner][analysis]")
{
    const Scratch scratch;
    const auto file = Runner::HistoryFile(scratch.Path(), "classes");
    auto flaky = Passed("flaky");
    flaky.flaky = true;
    flaky.attempts.front().outcome = Outcome::kFailed;
    const auto history = HistoryOf(
        file,
        {{Passed("fresh"), Failed("stuck", "first"), Passed("wobbly")},
         {Passed("fresh"), Failed("stuck", "first"), flaky}});

    RunResult run;
    run.tests = {Failed("unknown"),
                 Failed("fresh", "one"),
                 Failed("stuck", "second", 20),
                 Failed("flaky", "three", 30)};
    Runner::AnalysisInputs inputs;
    inputs.history = &history;
    inputs.program = "tests";
    Runner::Analyze(run, inputs);

    CHECK(run.tests[0].history == FailureHistory::kUnknown);

    CHECK(run.tests[1].history == FailureHistory::kNew);
    CHECK_THAT(Findings(run.tests[1]),
               ContainsSubstring("new failure after 2 passing run(s); it "
                                 "last passed at commit commit201234"));

    CHECK(run.tests[2].history == FailureHistory::kPersistent);
    CHECK_THAT(Findings(run.tests[2]),
               ContainsSubstring("persistent: failed in the last 2 run(s), "
                                 "but differently last time"));

    CHECK(run.tests[3].history == FailureHistory::kIntermittent);
    CHECK_THAT(Findings(run.tests[3]),
               ContainsSubstring("intermittent: 1 flaky result(s)"));
}

TEST_CASE("Failures with one signature are named together",
          "[progmasoft][runner][analysis]")
{
    RunResult run;
    for (const char *name : {"a", "b", "c", "d", "e"})
    {
        // Numbers in the message do not split the group.
        run.tests.push_back(Failed(
            name,
            "connection refused after " +
                std::to_string((run.tests.size() + 1) * 100) + " ms"));
    }
    run.tests.push_back(Failed("elsewhere", "other", 99));
    Runner::AnalysisInputs inputs;
    inputs.program = "tests";
    Runner::Analyze(run, inputs);

    CHECK(run.tests[0].signature == run.tests[4].signature);
    CHECK(run.tests[0].signature != run.tests[5].signature);
    CHECK_THAT(Findings(run.tests[0]),
               ContainsSubstring("fails the same way as 'b', 'c', 'd' and 1 "
                                 "more: probably one cause"));
    CHECK_THAT(Findings(run.tests[5]),
               !ContainsSubstring("fails the same way"));
}

TEST_CASE("A crash and a hang say where they stopped",
          "[progmasoft][runner][analysis]")
{
    SectionRecord outer;
    outer.name = "outer";
    outer.depth = 0;
    outer.durationMicroseconds = -1;
    SectionRecord done;
    done.name = "done";
    done.depth = 1;
    done.durationMicroseconds = 5;
    SectionRecord inner;
    inner.name = "inner";
    inner.depth = 1;
    inner.durationMicroseconds = -1;

    TestResult crash = Failed("crash");
    crash.outcome = Outcome::kCrashed;
    Attempt &crashed = crash.attempts.front();
    crashed.outcome = Outcome::kCrashed;
    crashed.category = FailureCategory::kCrash;
    crashed.assertions.clear();
    crashed.exitDescription = "signal 11 (SIGSEGV)";
    crashed.lastLocation = "src/test.cpp:44";
    crashed.sections = {outer, done, inner};

    TestResult hang = Failed("hang");
    hang.outcome = Outcome::kTimedOut;
    hang.predictedDurationMicroseconds = 200000;
    Attempt &hung = hang.attempts.front();
    hung.outcome = Outcome::kTimedOut;
    hung.category = FailureCategory::kTimeout;
    hung.assertions.clear();
    hung.durationMicroseconds = 5000000;
    hung.lastLocation = "src/test.cpp:50";
    hung.sections = {outer};

    RunResult run;
    run.tests = {crash, hang};
    Runner::AnalysisInputs inputs;
    inputs.program = "tests";
    Runner::Analyze(run, inputs);

    CHECK_THAT(Findings(run.tests[0]),
               ContainsSubstring("crashed (signal 11 (SIGSEGV)) after the "
                                 "assertion at src/test.cpp:44, in section "
                                 "outer / inner"));
    CHECK_THAT(Findings(run.tests[1]),
               ContainsSubstring("stopped after 5.0 s"));
    CHECK_THAT(Findings(run.tests[1]),
               ContainsSubstring("it was in section outer"));
    CHECK_THAT(Findings(run.tests[1]),
               ContainsSubstring("last assertion started at src/test.cpp:50"));
    // Two crashes in different places are different failures.
    CHECK(run.tests[0].signature != run.tests[1].signature);
}

TEST_CASE("A failure names the changed files it touches",
          "[progmasoft][runner][analysis]")
{
    const Scratch scratch;
    Runner::Changes changes;
    changes.root = scratch.Path();
    changes.files = {scratch.Path() / "src" / "test.cpp",
                     scratch.Path() / "src" / "helper.hpp"};
    TestResult test = Failed("touched");
    AssertionRecord inHelper;
    inHelper.file = "src/helper.hpp";
    inHelper.line = 3;
    inHelper.passed = false;
    test.attempts.front().assertions.push_back(inHelper);
    test.attempts.front().temporaryDirectory = "/tmp/kept";

    RunResult run;
    run.tests = {test};
    Runner::AnalysisInputs inputs;
    inputs.changes = &changes;
    inputs.changesSince = "main";
    inputs.program = "tests";
    Runner::Analyze(run, inputs);

    const auto findings = Findings(run.tests[0]);
    CHECK_THAT(findings, ContainsSubstring("changed since main: src/test.cpp"));
    CHECK_THAT(findings, ContainsSubstring("changed since main: src/helper.hpp"));
    CHECK_THAT(findings,
               ContainsSubstring("its temporary files are kept in /tmp/kept"));
}

TEST_CASE("Every failure gets a command that runs it again",
          "[progmasoft][runner][analysis]")
{
    RunResult run;
    run.tests = {Failed("plain, with [specials]"), Failed("after another"),
                 Passed("fine")};
    run.tests[1].attempts.front().precededBy = {"polluter"};
    Runner::AnalysisInputs inputs;
    inputs.program = "build dir/tests";
    Runner::Analyze(run, inputs);

    CHECK_THAT(run.tests[0].replayCommand,
               ContainsSubstring("plain\\\\, with \\\\[specials\\\\]"));
    CHECK_THAT(run.tests[0].replayCommand, ContainsSubstring("--seed=7"));
    CHECK_THAT(run.tests[0].replayCommand, ContainsSubstring("build dir/tests"));
    // A failure that needed other tests first is replayed with them.
    CHECK_THAT(run.tests[1].replayCommand, ContainsSubstring("--replay"));
    CHECK(run.tests[2].replayCommand.empty());
    CHECK(run.tests[2].analysis.empty());
}