Tests stage their fixtures under fixed names in the temp directory, fs::temp_directory_path() / "crafter-build-<name>", and two runs at once staged into the same ones: natively every process shares the machine's /tmp, and every sandbox of crafter-build's Flatpak shares the one the app has, whatever checkout it runs in. A test of one run could find what it had staged removed or written over by the same test of the other run, which removes the directory as it starts and stages its copy anew. RunTests now makes a directory for the run in the temp directory, a PrivateDirectory, and runs each test with TMPDIR naming <run>/<name>, so that fs::temp_directory_path() in the test names it and the fixed names are the run's alone. The second test of one name in a run, as two configurations or two targets may each have one, gets <name>-2, so that removing one's directory does not take the other's. RunSingleTest takes the directory as a new last parameter and puts TMPDIR into the env prefix of the test's command, beside a Wayland test's WAYLAND_DISPLAY. env's command is sh, which execs the test's words, as for a Wayland test's: env takes a word with an '=' in it ahead of its command for an assignment, a binary's path among them, and with no command left it printed its environment and exited 0, a pass for a test that never ran. What a test that passes or skips leaves is removed when it ends. What one that fails, crashes or times out leaves is kept for a look, its log says where, as does what is printed of a test that fails or crashes, and the run's directory then outlives the run, unlocked, for a later run to remove a minute after, or, where the run was killed, a minute after its last change to it; PrivateDirectory gains Keep() for that. A test's logs, build/test-logs/<name>.log and <name>.compositor.log, are named as its TMPDIR is, <name>-2 for a later test of one name, so that two failing tests of one name each keep their own, and with it where what each left is kept: with one log for both, the second's was written over the first's. The README's line on the logs says so. The directory is dated as it is let go of, since the removal of abandoned directories goes by its date, which is that of what was last made or removed in it: for the run of a test that fails after a minute, as one that times out at the default 60 s does, a minute old already, and the next run would remove it at once. The removal now reads a directory's date only once it holds the directory's lock: a date read before could be one the holder replaced as it let go. The directory is the run's, not a pass's: every pass of a sweep over targets shares it, so it is made and its surroundings swept once, and let go of once the last pass is done. A pass that made one of its own would have made it once the passes before had let go of theirs, and making it would have removed any of those that had not changed for a minute: a kept one, after a pass of that long, while its test's output said where it was. RunTests becomes the wrapper that holds the run's directory around RunTestsWith, the passes it recurses into, and the directory is kept the moment a test keeps something in it, by the worker that ran the test, so that a later pass that throws, as one does for a target whose runner CRAFTER_BUILD_RUNNER_ gives wrong, or a worker that cannot be started does not take it with it; Keep() may be called from several workers at once. TMPDIR reaches the test where its runner hands it on, as running it directly and qemu-user do. Under wine a Windows test does not get TMPDIR, and takes the Wine prefix's TMP, TEMP and temp directory, and wasmtime gives a test no environment at all; RunSingleTest's comment says so. A test under `host` gets its TMPDIR as any test does, so that comment and HostDisplay's now say that `host` leaves the test the display, not the whole environment, as it is. In the runner rather than in each of the 74 tests whose main.cpp uses the temp directory at this commit: a project's own tests are covered the same way, where their runner hands them TMPDIR. Of the 74 only SysrootStdModule has to change. It builds against a sysroot it stages there, and the std module's cache directory is keyed on the sysroot's path, which is a new one every run now, so every run left one more directory, with its lock, in the real cache; it keeps its cache in its scratch directory now, as the tests that have crafter-build fetch a repository they stage do. AGENTS.template.md says what changes for the tests: HostFlatpak, whose /tmp is no longer the temp directory a test is given, is a second test that writes elsewhere, and under `crafter-build test` a staged copy goes with the test's TMPDIR rather than at the test's next run.
157 lines
8.6 KiB
C++
157 lines
8.6 KiB
C++
// SPDX-License-Identifier: LGPL-3.0-only
|
|
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
|
|
|
export module Crafter.Build:Test;
|
|
import std;
|
|
import :Clang;
|
|
|
|
export namespace Crafter {
|
|
// One row in a SIMD-march fan-out: a single Test variant compiles the
|
|
// shared source with `-march=<march> -mtune=<mtune>`. Passed by span to
|
|
// Configuration::AddMarchVariants — see Crafter.Math/project.cpp.
|
|
struct MarchTier {
|
|
std::string march;
|
|
std::string mtune;
|
|
};
|
|
|
|
// Fluent helper returned by Configuration::AddTest. Holds a back-pointer
|
|
// to the parent Configuration and the index of the just-pushed Test, so
|
|
// mutations survive any vector reallocation that subsequent AddTest
|
|
// calls might trigger. Each setter returns `*this` to support chaining.
|
|
//
|
|
// Note: source files (interfaces + implementations) are fixed at
|
|
// AddTest-time — pass them via the AddTest overload that takes
|
|
// interfaces. The builder only mutates compile state, deps, runtime
|
|
// args, and requires; it deliberately does NOT expose Sources() to
|
|
// avoid the double-`GetInterfacesAndImplementations` footgun (calling
|
|
// it twice would list the same .cppm files twice, and the build would
|
|
// make a Module entry of each listing).
|
|
struct TestBuilder {
|
|
Configuration* parent;
|
|
std::size_t index;
|
|
|
|
Test& Ref() const { return parent->tests[index]; }
|
|
|
|
// Override the path the test's sources resolve against. Defaults to
|
|
// "./" (project root, where tests/<name>/main.cpp lives). Override
|
|
// only when the test source layout sits under a subdir.
|
|
TestBuilder& Path(std::filesystem::path p);
|
|
TestBuilder& Target(std::string t);
|
|
TestBuilder& March(std::string m);
|
|
TestBuilder& Mtune(std::string m);
|
|
TestBuilder& Sysroot(std::filesystem::path s);
|
|
TestBuilder& Debug(bool d = true);
|
|
TestBuilder& Define(std::string name, std::string value = "");
|
|
TestBuilder& Timeout(std::chrono::seconds s);
|
|
// Replaces any previously-set args.
|
|
TestBuilder& Args(std::vector<std::string> a);
|
|
// Appends one require entry ("tool:foo" / "file:..." / "env:VAR").
|
|
TestBuilder& Requires(std::string r);
|
|
// Replaces the dependency list. Pointers must outlive the build.
|
|
TestBuilder& Dependencies(std::vector<Configuration*> deps);
|
|
// Append a single linker flag (-l..., -L..., etc.).
|
|
TestBuilder& LinkFlag(std::string f);
|
|
// Append a single compile flag.
|
|
TestBuilder& CompileFlag(std::string f);
|
|
// Sets Test::wayland: the test runs with a headless Wayland
|
|
// compositor of its own.
|
|
TestBuilder& Wayland(bool w = true);
|
|
};
|
|
|
|
struct RunTestsOptions {
|
|
std::vector<std::string> globs;
|
|
// Tests built and run at once, from --jobs=; 0 is one per hardware
|
|
// thread. It counts tests, not build steps: the steps of their builds
|
|
// run on JobPool::Process() under that pool's own limit, and a test
|
|
// that is running is not one of them.
|
|
std::int32_t jobs = 0;
|
|
std::optional<std::chrono::seconds> timeoutOverride;
|
|
bool listOnly = false;
|
|
// Single-target run: only tests whose Configuration::target matches
|
|
// are included. Empty (default) = run every distinct target declared
|
|
// across discovered tests, plus the host target. Set from --target=...
|
|
// (when omitted, the harness sweeps all declared targets so cross-arch
|
|
// tests run by default without the user having to know which targets
|
|
// exist).
|
|
std::string targetFilter;
|
|
// CLI override for --runner=<spec>: applies to every test in the run.
|
|
// Target scoping is unnecessary because targetFilter ensures the run
|
|
// contains only one target's tests.
|
|
std::optional<std::string> runnerOverride;
|
|
// From --compositor=: the compositor every test of the run that sets
|
|
// Test::wayland runs with, in place of CRAFTER_BUILD_COMPOSITOR and
|
|
// the default. See RunSingleTest.
|
|
std::optional<std::string> compositorOverride;
|
|
};
|
|
|
|
struct TestSummary {
|
|
std::int32_t passed = 0;
|
|
std::int32_t failed = 0;
|
|
std::int32_t crashed = 0;
|
|
std::int32_t timedOut = 0;
|
|
std::int32_t skipped = 0;
|
|
std::vector<TestResult> results;
|
|
bool AllPassed() const { return failed == 0 && crashed == 0 && timedOut == 0; }
|
|
};
|
|
|
|
TestSummary RunTests(Configuration& projectCfg, const RunTestsOptions& opts, std::span<const std::string_view> projectArgs = {});
|
|
// The tests of several configurations, those a project file returns, as
|
|
// one run: one sweep over the targets they declare, one summary, and
|
|
// one build pass per target, so a library that tests of two of them link
|
|
// is built once. The log a failed, crashed or timed-out test leaves
|
|
// goes under the path of the configuration that declares it. A test
|
|
// belongs to one configuration: where two of them each declare one that
|
|
// would be built into the same directory, which two tests of one name
|
|
// compiled alike are, both fail without being built and name the
|
|
// configurations. A configuration the list names twice counts once.
|
|
TestSummary RunTests(std::span<Configuration* const> configurations, const RunTestsOptions& opts, std::span<const std::string_view> projectArgs = {});
|
|
// Runs `binary` as `test` with its runner, its arguments and `timeout`,
|
|
// and reads the outcome from how it ended (exit 0 passes, 77 skips).
|
|
// With a `temporaryDirectory` the test's command runs with TMPDIR naming
|
|
// it; RunTests gives each test a directory of its own, made for the run.
|
|
// fs::temp_directory_path() in the test names it where the runner hands
|
|
// the test TMPDIR, as running it directly and qemu-user do. Under wine a
|
|
// Windows test does not get TMPDIR, and takes the Wine prefix's TMP, TEMP
|
|
// and temp directory; wasmtime gives a test no environment at all.
|
|
//
|
|
// When the test sets Test::wayland, a compositor is started for it
|
|
// first: `compositorOverride` when it holds a command, else the one
|
|
// CRAFTER_BUILD_COMPOSITOR names, else weston on its headless backend.
|
|
// The command runs through sh in a process group of its own, with
|
|
// XDG_RUNTIME_DIR naming a directory made for it alone and without
|
|
// WAYLAND_DISPLAY, WAYLAND_SOCKET or DISPLAY, and is waited for until
|
|
// a wayland-<n> socket appears in that directory, with a limit of the
|
|
// test's, the wait and 30 s more, or none when `timeout` is 0, which is
|
|
// no limit for the test either (timeout(1)). The test then runs
|
|
// with WAYLAND_DISPLAY set to the socket's path and DISPLAY and
|
|
// WAYLAND_SOCKET unset; afterwards the compositor's group is stopped and
|
|
// the directory removed. A compositor that ends before it listens, sh
|
|
// not finding it included, or does not listen within 30 s fails the
|
|
// test, with what it printed; a test that may run where its compositor
|
|
// is missing declares it in requires_ (tool:weston), which RunTests
|
|
// checks before the build. `host` starts nothing and leaves the test the
|
|
// display this process has, its WAYLAND_DISPLAY, WAYLAND_SOCKET, DISPLAY
|
|
// and XDG_RUNTIME_DIR; a `temporaryDirectory` is its TMPDIR all the same.
|
|
TestResult RunSingleTest(const Test& test, const std::filesystem::path& binary, std::chrono::seconds timeout, const std::optional<std::string>& compositorOverride = std::nullopt, const std::filesystem::path& temporaryDirectory = {});
|
|
|
|
// The configurations of the project crafter-build was pointed at, by
|
|
// name. Run() hands SetParentProject what the project file returned,
|
|
// once it has loaded. ParentLib() then finds a Configuration* by
|
|
// Configuration::name among them and what they depend on, searching the
|
|
// dependency graph of each in the order the file returned them, and
|
|
// throws when none has that name or no project is set.
|
|
void SetParentProject(Configuration* parent);
|
|
void SetParentProject(std::span<Configuration* const> parents);
|
|
Configuration* ParentLib(std::string_view name);
|
|
}
|
|
|
|
namespace Crafter {
|
|
// A test is never packaged, whatever its Configuration asks for: the
|
|
// runner starts the binary itself. Sets every test of `cfg` so, for one
|
|
// from AddTest or AddMarchVariants and one a project.cpp put into
|
|
// cfg.tests itself alike. Every reader of cfg.tests that builds a test
|
|
// or works out its compile command, RunTests and RunLint, calls it
|
|
// first, so that they agree on the test's VariantId, and so on where its
|
|
// module PCMs are.
|
|
void UnpackageTests(Configuration& cfg);
|
|
}
|