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

summaryrefslogtreecommitdiffhomepage
path: root/tests/Progmasoft/RunnerFixtureClient.hpp
blob: 3085176dff764d6775e1706660f33e6ef7e3db43 (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
// Copyright (c) 2026 Progmasoft.
// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-1.1
#pragma once

// What the runner's integration tests share: finding the fixture binary,
// running it in a directory of its own and reading its JSON report.

#include <Progmasoft/Runner/Files.hpp>
#include <Progmasoft/Runner/Json.hpp>
#include <Progmasoft/Runner/Process.hpp>
#include <Progmasoft/Catch3/TestMacros.hpp>
#include <chrono>
#include <cstdint>
#include <filesystem>
#include <string>
#include <utility>
#include <vector>

namespace FixtureClient
{
    namespace Runner = Progmasoft::Catch3::Runner;

    struct FixtureRun final
    {
        Runner::ExitStatus status;
        std::string output;
        Runner::JsonValue report;
        std::chrono::milliseconds wall{ 0 };

        /// The report entry of a test.
        [[nodiscard]] const Runner::JsonValue &
        Test(const std::string &name) const
        {
            for (const auto &test : report["tests"].Items())
            {
                if (test["name"].AsString() == name)
                {
                    return test;
                }
            }
            FAIL("no result for '" << name << "' in:\n" << output);
            return report;
        }

        [[nodiscard]] std::string
        Analysis(const std::string &name) const
        {
            std::string text;
            for (const auto &finding : Test(name)["analysis"].Items())
            {
                text += finding.AsString() + "\n";
            }
            return text;
        }
    };

    /// Finds a file Bazel provides as runfiles: beside the test, in the
    /// runfiles tree, or through the manifest Bazel writes on Windows.
    inline std::filesystem::path
    FromRunfiles(const std::string &path)
    {
        std::error_code error;
        if (std::filesystem::exists(path, error))
        {
            return std::filesystem::absolute(path, error);
        }
        for (const char *variable : { "RUNFILES_DIR", "TEST_SRCDIR" })
        {
            const auto directory = Runner::ReadEnvironment(variable);
            if (!directory.empty())
            {
                const auto candidate
                    = std::filesystem::path{ directory } / "_main" / path;
                if (std::filesystem::exists(candidate, error))
                {
                    return candidate;
                }
            }
        }
        const auto manifest = Runner::ReadEnvironment("RUNFILES_MANIFEST_FILE");
        if (!manifest.empty())
        {
            auto text = Runner::ReadFile(manifest);
            const std::string key = "_main/" + path + " ";
            if (text)
            {
                const auto found = text->find(key);
                if (found != std::string::npos)
                {
                    const auto start = found + key.size();
                    auto end = text->find('\n', start);
                    std::string target = text->substr(start,
                                                      end == std::string::npos
                                                          ? std::string::npos
                                                          : end - start);
                    if (!target.empty() && target.back() == '\r')
                    {
                        target.pop_back();
                    }
                    return target;
                }
            }
        }
        return path;
    }

    inline std::string
    Fixture()
    {
        const auto path = Runner::ReadEnvironment("CATCH3_RUNNER_FIXTURE");
        if (path.empty())
        {
            SKIP("CATCH3_RUNNER_FIXTURE names no fixture binary");
        }
        return FromRunfiles(path).string();
    }

    /// A fresh directory for one test.
    class Scratch final
    {
    public:
        Scratch()
        {
            std::error_code error;
            path_ = std::filesystem::temp_directory_path(error)
                    / ("catch3-runner-test-" + 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_;
    };

    inline FixtureRun
    RunFixture(const Scratch &scratch,
               std::vector<std::string> arguments,
               std::vector<std::pair<std::string, std::string>> environment
               = {})
    {
        Runner::ChildOptions options;
        options.arguments.push_back(Fixture());
        const auto report
            = scratch.Path() / ("report-" + Runner::UniqueName() + ".json");
        for (auto &argument : arguments)
        {
            options.arguments.push_back(std::move(argument));
        }
        options.arguments.push_back("--json=" + report.string());
        options.arguments.emplace_back("--color=never");
        options.environment = std::move(environment);
        options.environment.emplace_back("CATCH3_FIXTURE_STATE",
                                         scratch.Path().string());
        const auto started = std::chrono::steady_clock::now();
        auto run = Runner::RunAndCapture(options);
        INFO((run ? std::string{} : run.GetError().Message()));
        REQUIRE(run);
        FixtureRun result;
        result.wall = std::chrono::duration_cast<std::chrono::milliseconds>(
            std::chrono::steady_clock::now() - started);
        result.status = run->status;
        result.output = run->output + run->errors;
        if (std::filesystem::exists(report))
        {
            auto text = Runner::ReadFile(report);
            REQUIRE(text);
            auto json = Runner::ParseJson(*text);
            REQUIRE(json);
            result.report = std::move(*json);
        }
        return result;
    }

    inline std::int64_t
    ExitCode(const FixtureRun &run)
    {
        INFO(run.output);
        REQUIRE(run.status.kind == Runner::ExitStatus::Kind::kExited);
        return run.status.code;
    }

    /// Runs the fixture with `arguments` as they are, for the modes that
    /// write no report.
    inline Runner::CapturedRun
    RunPlain(std::vector<std::string> arguments,
             std::vector<std::pair<std::string, std::string>> environment = {})
    {
        Runner::ChildOptions options;
        options.arguments.push_back(Fixture());
        for (auto &argument : arguments)
        {
            options.arguments.push_back(std::move(argument));
        }
        options.environment = std::move(environment);
        auto run = Runner::RunAndCapture(options);
        INFO((run ? std::string{} : run.GetError().Message()));
        REQUIRE(run);
        return std::move(*run);
    }
} // namespace FixtureClient