From 93f1c8d9b943790aa610a0cafa94ab350a3f3ab1 Mon Sep 17 00:00:00 2001 From: Jorijn van der Graaf Date: Fri, 27 Nov 2026 09:17:28 +0100 Subject: [PATCH] fix(test): run each test's command with a TMPDIR of its own Tests stage their fixtures under fixed names in the temp directory, fs::temp_directory_path() / "crafter-build-", 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 /, 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 -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/.log and .compositor.log, are named as its TMPDIR is, -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. --- AGENTS.template.md | 4 +- README.md | 4 +- implementations/Crafter.Build-Platform.cpp | 20 +++- implementations/Crafter.Build-Test.cpp | 102 ++++++++++++++++++--- interfaces/Crafter.Build-Platform.cppm | 20 ++-- interfaces/Crafter.Build-Test.cppm | 13 ++- tests/SysrootStdModule/main.cpp | 10 +- 7 files changed, 140 insertions(+), 33 deletions(-) diff --git a/AGENTS.template.md b/AGENTS.template.md index a78394f..0865356 100644 --- a/AGENTS.template.md +++ b/AGENTS.template.md @@ -90,7 +90,7 @@ Conventions the existing tests follow: - Open `main.cpp` with a comment that states the behaviour under test and the failure it guards against. `tests/CompilerWarning/main.cpp` shows the shape. - Collect failures with a `Check(cond, msg)` helper that prints `FAIL: ...` to stderr and counts, then return nonzero at the end. Do not stop at the first failure unless later steps are meaningless without it. - Drive the engine in-process: `Build(cfg, depResults, depMutex)`, `RunTests`, the lint driver. Do not shell out to `bin/crafter-build` and grep its stdout. -- Fixtures under `tests//fixture/` are inputs. A test that edits or touches files first stages a copy under `fs::temp_directory_path() / "crafter-build-"` and removes it on the next run. Variant sources are checked in as `*.in` files and copied over the original inside the staged copy. +- Fixtures under `tests//fixture/` are inputs. A test that edits or touches files first stages a copy under `fs::temp_directory_path() / "crafter-build-"`, after removing what an earlier run left there, which matters where the test runs by hand: under `crafter-build test` the copy is in the test's own `TMPDIR`, which goes when the test ends unless the test failed, crashed or timed out and left something in it: then it is kept until a later run removes it, a minute after the run that kept it ended, or, where that run was killed, a minute after its last change to the run's directory. Most tests here remove their copy themselves before they report, and so leave nothing to keep. Variant sources are checked in as `*.in` files and copied over the original inside the staged copy. - Use a fresh `depResults` map per build pass: the map memoises each configuration's result, and each external dependency's fetch and build, for one pass, so reusing it skips rebuilds and pulls you meant to observe. `Build()` itself is safe to call repeatedly on the same `Configuration`. - Set `cfg.path` before calling `GetInterfacesAndImplementations`, and spell `cFiles` as absolute paths: both resolve against the working directory, not the configuration. - `GetInterfacesAndImplementations` only lists the sources. A configuration's `interfaces` and `implementations`, its modules and units with what each imports, are made by `Build()` from what it reads in them, so a test that looks at them builds first. @@ -167,7 +167,7 @@ These were decided deliberately. Do not undo them without the maintainer's say-s - The linter loads `libclang` at runtime by name. `CRAFTER_BUILD_LIBCLANG` pins an exact path and must fail loudly when wrong, never fall back to another library. - crafter-build's Flatpak has `--talk-name=org.freedesktop.Flatpak`, and the permission is the whole sandbox's: anything that runs there can run commands on the host through `flatpak-spawn --host`. That is every process of crafter-build's sandbox: crafter-build itself, every `project.cpp` it loads with its scripts, the builds of its dependencies and pragma dependencies, the programs `-r` and `test` start, and whatever `flatpak run --command=` and `flatpak enter` start in it, the debuggers of the README's Debugging section and the programs under them included. crafter-build's own code uses it only through `HostCommand` in `:Platform`, which builds the command lines of `RunHostFlatpak`, `RunHostFlatpakBuilder` and `HostFlatpakUnavailable`'s probes, for the host's `flatpak` and `flatpak-builder` behind `install` and `bundle`; add no other host command to crafter-build. `RunHostFlatpak` is exported for the tests, and gives a `project.cpp` nothing the sandbox does not. The trust model allows the permission because `--filesystem=host` already lets sandboxed code write `~/.bashrc`, `~/.config/autostart` and `~/.config/systemd/user`: it makes running something on the host immediate, not newly possible, for everything the sandbox runs. - CI creates a release for each release tag with the workflow's own token, pushes nothing to git, and signs the Flatpak repo with the key mounted from the runner host. Do not add secrets to the workflow or echo the environment in it. -- Tests write only under `fs::temp_directory_path()` and the fixture's own `bin/` and `build/`, and in the caches of what they run, unless they point `XDG_CACHE_HOME` elsewhere: crafter-build's, what a build puts there, and wasmtime's, the modules the wasm tests run. Keep it that way. One test writes elsewhere, `tests/HeadlessCompositor`, which makes its compositors' directories in a directory of its own in `$XDG_RUNTIME_DIR`, or in `/tmp` when that is not a directory, and has one made in `/tmp` itself, as crafter-build makes it where there is no `$XDG_RUNTIME_DIR`, because a socket's path holds 107 bytes and a temp directory's path can use them up. It removes what it makes, and a later run removes what a killed one left once that has not changed for a minute (`PrivateDirectory`). +- Tests write only under `fs::temp_directory_path()` and the fixture's own `bin/` and `build/`, and in the caches of what they run, unless they point `XDG_CACHE_HOME` elsewhere: crafter-build's, what a build puts there, and wasmtime's, the modules the wasm tests run. Keep it that way. Two tests write elsewhere, and both remove what they make: `tests/HeadlessCompositor`, which makes its compositors' directories in a directory of its own in `$XDG_RUNTIME_DIR`, or in `/tmp` when that is not a directory, and has one made in `/tmp` itself, as crafter-build makes it where there is no `$XDG_RUNTIME_DIR`, because a socket's path holds 107 bytes and a temp directory's path can use them up, and a later run removes what a killed run of it left once that has not changed for a minute (`PrivateDirectory`); and `tests/HostFlatpak`, which in the Flatpak makes its two projects in the sandbox's `/tmp`, the place it asks the host about, under fixed names, so that its next run that makes them removes what a killed one left. ## CI and publishing diff --git a/README.md b/README.md index 6e8c085..ec26f89 100644 --- a/README.md +++ b/README.md @@ -300,7 +300,7 @@ Test conventions: - **Exit codes**: `0` = ✅ pass, nonzero = ❌ fail, **`77`** = ⏭ skipped (autoconf convention; use it for runtime opt-outs like "tool not on PATH"). - Each test runs in its own subprocess — a segfault doesn't take the runner down. - Default timeout is 60 s per test, runs in parallel up to `hardware_concurrency()` tests (`--jobs=`). The steps of their builds share one limit, a build's: as many at once as there are hardware threads. -- Stdout+stderr is captured and printed atomically on completion. Failed/crashed/timed-out tests also write to `build/test-logs/.log`. +- Stdout+stderr is captured and printed atomically on completion. A test that fails, crashes or times out once it is built also writes what it printed, and what crafter-build said of it, to `build/test-logs/.log`, when that is anything, and, where it sets `Test::wayland` and ran, what its compositor printed, if anything, to `.compositor.log` beside it (what a compositor that did not start printed is in `.log`); one that failed to build, or did not get that far, writes neither. `` is the test's name with `-2`, `-3` and on after it where the run has given that name out already, as two configurations or two targets may each declare a test of one name. - A broken fixture `project.cpp` is reported as a single failed test, not a fatal error — other tests still run. CLI: @@ -392,7 +392,7 @@ The compositor is `weston --backend=headless --renderer=gl --no-config --idle-ti crafter-build test --compositor=host 'Swapchain*' ``` -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 weston is missing declares it with `.Requires("tool:weston")`, and is then skipped there before it is built, as for any tool it declares. When a test that ran fails, crashes or times out, what its compositor printed, if anything, goes to `build/test-logs/.compositor.log`, and the test's output says so, and also when the compositor ended before the test did; what a compositor that did not start printed is in the test's own failure, in `build/test-logs/.log`. A crafter-build that is killed takes its compositors with it; the directories it leaves behind are removed by a test that starts a compositor after they have gone a minute unchanged. +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 weston is missing declares it with `.Requires("tool:weston")`, and is then skipped there before it is built, as for any tool it declares. When a test that ran fails, crashes or times out, what its compositor printed, if anything, goes to `build/test-logs/.compositor.log`, named as the test's own log is, and the test's output says so, and also when the compositor ended before the test did; what a compositor that did not start printed is in the test's own failure, in `build/test-logs/.log`. A crafter-build that is killed takes its compositors with it; the directories it leaves behind are removed by a test that starts a compositor after they have gone a minute unchanged. ### Linking the parent project's library diff --git a/implementations/Crafter.Build-Platform.cpp b/implementations/Crafter.Build-Platform.cpp index c62cbd3..58ddefe 100644 --- a/implementations/Crafter.Build-Platform.cpp +++ b/implementations/Crafter.Build-Platform.cpp @@ -990,12 +990,16 @@ namespace { for (const fs::directory_entry& entry : fs::directory_iterator(parent, ec)) { std::error_code entryError; if (!entry.path().filename().string().starts_with(prefix) || !entry.is_directory(entryError) || entry.is_symlink(entryError)) continue; - const auto changed = fs::last_write_time(entry.path(), entryError); - if (entryError || now - changed < std::chrono::minutes(1)) continue; // Another user's directory does not open: mkdtemp made it 0700. const std::int32_t held = ::open(entry.path().c_str(), O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); if (held < 0) continue; - if (::flock(held, LOCK_EX | LOCK_NB) == 0) fs::remove_all(entry.path(), entryError); + // Dated only once it is held: a directory that is kept is dated + // by its holder just before it lets go of it, so a date read + // before the lock can be one the holder has replaced since. + if (::flock(held, LOCK_EX | LOCK_NB) == 0) { + const auto changed = fs::last_write_time(entry.path(), entryError); + if (!entryError && now - changed >= std::chrono::minutes(1)) fs::remove_all(entry.path(), entryError); + } ::close(held); } } @@ -1022,7 +1026,15 @@ Crafter::PrivateDirectory::PrivateDirectory(const fs::path& parent, std::string_ Crafter::PrivateDirectory::~PrivateDirectory() { std::error_code ec; - fs::remove_all(path, ec); + if (kept) { + // Dated now, while it is still held: its date is that of what was + // last made or removed in it, which may be long before, as for the + // run of a test that failed after minutes, and a directory nobody + // holds is removed a minute after its date. + fs::last_write_time(path, fs::file_time_type::clock::now(), ec); + } else { + fs::remove_all(path, ec); + } if (fd >= 0) ::close(fd); } diff --git a/implementations/Crafter.Build-Test.cpp b/implementations/Crafter.Build-Test.cpp index 2ef3bad..3160dd9 100644 --- a/implementations/Crafter.Build-Test.cpp +++ b/implementations/Crafter.Build-Test.cpp @@ -86,7 +86,8 @@ namespace { // callbacks. constexpr std::string_view DefaultCompositor = "weston --backend=headless --renderer=gl --no-config --idle-time=0"; // The compositor named for none of the test's own: the test gets the - // environment crafter-build has, display included, to watch its windows. + // display crafter-build has, to watch its windows, and the TMPDIR it is + // given, as any test does. constexpr std::string_view HostDisplay = "host"; // How long a compositor may take to listen. weston needs a fraction of a // second; this is for a machine running a test on every hardware thread. @@ -324,7 +325,7 @@ TestRunner TestRunner::FromEnv(std::string_view target, TestRunner fallback) { return fallback; } -TestResult Crafter::RunSingleTest(const Test& test, const fs::path& binary, std::chrono::seconds timeout, const std::optional& compositorOverride) { +TestResult Crafter::RunSingleTest(const Test& test, const fs::path& binary, std::chrono::seconds timeout, const std::optional& compositorOverride, const fs::path& temporaryDirectory) { TestResult result; result.name = test.config.name; @@ -342,6 +343,12 @@ TestResult Crafter::RunSingleTest(const Test& test, const fs::path& binary, std: cmd = Substitute(test.runner.exec, ph); } + // What `env` changes for the test, its -u options and its assignments, + // which it takes in that order. + std::string unset; + std::string set; + if (!temporaryDirectory.empty()) set += std::format(" TMPDIR={}", ShellQuote(temporaryDirectory.string())); + // The test's own compositor, which no other test shares, so that no // two tests cover each other's windows or take each other's focus. Its // directory goes after it: declared first, it is destroyed last. @@ -391,12 +398,15 @@ TestResult Crafter::RunSingleTest(const Test& test, const fs::path& binary, std: if (ended == 127) result.output += "install weston, name another compositor with CRAFTER_BUILD_COMPOSITOR or --compositor=, or skip a test that may run without it with requires_, tool:weston for weston\n"; return result; } - // env's command is sh, which execs the test's words as they are: env - // takes every word with an '=' in it ahead of its command for an - // assignment, the binary's path among them where it has one (a test - // named a=b), and that test never ran. - cmd = std::format("env -u DISPLAY -u WAYLAND_SOCKET WAYLAND_DISPLAY={} sh -c 'exec \"$@\"' sh {}", ShellQuote(socket->string()), cmd); + unset += " -u DISPLAY -u WAYLAND_SOCKET"; + set += std::format(" WAYLAND_DISPLAY={}", ShellQuote(socket->string())); } + // env's command is sh, which execs the test's words as they are: env + // takes every word with an '=' in it ahead of its command for an + // assignment, the binary's path among them where it has one (a test + // named a=b), and with no command left it printed its environment and + // exited 0, a pass for a test that never ran. + if (!unset.empty() || !set.empty()) cmd = std::format("env{}{} sh -c 'exec \"$@\"' sh {}", unset, set, cmd); auto start = std::chrono::steady_clock::now(); CommandResult r = RunCommandWithTimeout(cmd, timeout); @@ -591,14 +601,30 @@ TestSummary Crafter::RunTests(Configuration& projectCfg, const RunTestsOptions& return RunTests(std::span(one), opts, projectArgs); } -TestSummary Crafter::RunTests(std::span configurations, const RunTestsOptions& opts, std::span projectArgs) { +namespace { + // The directory of one run of `crafter-build test`, in the temp + // directory, which each test's TMPDIR is made in, and the names given + // out in it. Every pass of a sweep over targets shares it: the first + // that runs a test makes it, and RunTests lets go of it once the last + // pass is done. A pass that made one of its own made it once the + // passes before had let go of theirs, and making it removed any of + // those that had not changed for a minute: a kept one after a pass + // that long, while its test's output said where it was kept. + struct RunTemporaries { + std::optional directory; + std::set used; + }; +} + +// RunTests, with the run's temporaries. +static TestSummary RunTestsWith(std::span configurations, const RunTestsOptions& opts, std::span projectArgs, RunTemporaries& run) { // Each of them once, however often the list names it: named twice, a // configuration would have each of its tests built twice at once. std::vector distinct; for (Configuration* cfg : configurations) { if (!std::ranges::contains(distinct, cfg)) distinct.push_back(cfg); } - if (distinct.size() != configurations.size()) return RunTests(distinct, opts, projectArgs); + if (distinct.size() != configurations.size()) return RunTestsWith(distinct, opts, projectArgs, run); for (Configuration* cfg : configurations) UnpackageTests(*cfg); // Multi-target sweep: when no --target= was given, the run covers every @@ -625,7 +651,7 @@ TestSummary Crafter::RunTests(std::span configurations, co Progress::Clear(); std::println("\n=== target: {} ===", target); } - TestSummary s = RunTests(configurations, perTarget, projectArgs); + TestSummary s = RunTestsWith(configurations, perTarget, projectArgs, run); aggregate.passed += s.passed; aggregate.failed += s.failed; aggregate.crashed += s.crashed; @@ -705,6 +731,28 @@ TestSummary Crafter::RunTests(std::span configurations, co } } + // Each test runs with TMPDIR naming a directory of its own, /, + // in the run's directory in the temp directory (RunTemporaries), so that + // fs::temp_directory_path() in the test names it, where its runner hands + // it TMPDIR (RunSingleTest). Tests stage their fixtures under + // fixed names in it (crafter-build-), 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. A second test of one name in the run, as two configurations + // or two targets may each have one, gets -2. What a test that + // passes or skips leaves there is removed when it ends; what one that + // fails, crashes or times out leaves is kept, its output says where, and + // so is the run's directory, for a later run to remove a minute after + // this one ends, or after its last change to it where this one is + // killed (PrivateDirectory::Keep). + if (!run.directory) run.directory.emplace(fs::temp_directory_path(), "crafter-build-tests-"); + std::vector temporary(filtered.size()); + for (std::size_t i = 0; i < filtered.size(); ++i) { + std::string name = filtered[i]->config.name; + for (std::int32_t n = 2; !run.used.insert(name).second; ++n) name = std::format("{}-{}", filtered[i]->config.name, n); + temporary[i] = run.directory->path / name; + } + std::int32_t jobs = opts.jobs > 0 ? opts.jobs : std::max(1u, std::thread::hardware_concurrency()); @@ -825,24 +873,47 @@ TestSummary Crafter::RunTests(std::span configurations, co // triple and nothing for wasm, so every wasm test was handed to // wasmtime without the .wasm it was linked as. fs::path binary = ExpectedOutput(t.config); + std::error_code temporaryError; + fs::create_directories(temporary[i], temporaryError); try { - r = RunSingleTest(t, binary, timeout, opts.compositorOverride); + if (temporaryError) throw std::runtime_error(std::format("cannot make the test's TMPDIR {}: {}", temporary[i].string(), temporaryError.message())); + r = RunSingleTest(t, binary, timeout, opts.compositorOverride, temporary[i]); } catch (const std::exception& e) { r.outcome = TestOutcome::Fail; r.output = std::format("runner threw: {}", e.what()); r.exitCode = -1; } + // The logs are named as the test's TMPDIR is, -2 for a later + // test of one name in the run, so that each has its own: with one + // for both, the second's was written over the first's, and with it + // where what the first left is kept. + const std::string logName = temporary[i].filename().string(); // Beside the test's own log rather than in it: weston prints some // 70 lines starting up, which would bury what the test said. if (!r.compositorLog.empty()) { - if (const std::optional log = WriteLog(owners[i]->path, std::format("{}.compositor", r.name), r.compositorLog)) { + if (const std::optional log = WriteLog(owners[i]->path, std::format("{}.compositor", logName), r.compositorLog)) { if (!r.output.empty() && !r.output.ends_with('\n')) r.output.push_back('\n'); r.output += std::format("the headless compositor's output is in {}\n", log->string()); } } + if (!temporaryError) { + std::error_code ec; + const bool failed = r.outcome != TestOutcome::Pass && r.outcome != TestOutcome::Skipped; + if (failed && !fs::is_empty(temporary[i], ec) && !ec) { + // Kept at once, so that it stays whatever ends the run: + // 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 unwinds past the end of RunTests. + run.directory->Keep(); + if (!r.output.empty() && !r.output.ends_with('\n')) r.output.push_back('\n'); + r.output += std::format("what it left in its TMPDIR is kept in {}\n", temporary[i].string()); + } else { + fs::remove_all(temporary[i], ec); + } + } if (r.outcome != TestOutcome::Pass && r.outcome != TestOutcome::Skipped && !r.output.empty()) { - WriteLog(owners[i]->path, r.name, r.output); + WriteLog(owners[i]->path, logName, r.output); } { @@ -888,3 +959,8 @@ TestSummary Crafter::RunTests(std::span configurations, co return summary; } + +TestSummary Crafter::RunTests(std::span configurations, const RunTestsOptions& opts, std::span projectArgs) { + RunTemporaries run; + return RunTestsWith(configurations, opts, projectArgs, run); +} diff --git a/interfaces/Crafter.Build-Platform.cppm b/interfaces/Crafter.Build-Platform.cppm index c7abe00..98f97a5 100644 --- a/interfaces/Crafter.Build-Platform.cppm +++ b/interfaces/Crafter.Build-Platform.cppm @@ -95,23 +95,29 @@ namespace Crafter { }; // A new directory in `parent`, named `prefix` and six random characters, // that only this user may enter (mkdtemp), removed with everything in it - // when this ends. A process that is killed runs no destructor, so this - // also holds an flock on the directory, which the OS drops with the - // process, and making one removes the directories of `prefix` in - // `parent` that nobody holds and that have not changed for a minute: - // left behind by a process that is gone. The minute covers one that is - // being made at that moment, between mkdtemp and its lock. Throws when - // the directory cannot be made. + // when this ends, unless it is kept. A process that is killed runs no + // destructor, so this also holds an flock on the directory, which the OS + // drops with the process, and making one removes the directories of + // `prefix` in `parent` that nobody holds and that have not changed for a + // minute: left behind by a process that is gone, or kept. The minute + // covers one that is being made at that moment, between mkdtemp and its + // lock. Throws when the directory cannot be made. // Exported for the tests that make a directory of their own outside the // temp directory. export class PrivateDirectory { std::int32_t fd = -1; + std::atomic kept{false}; public: const fs::path path; PrivateDirectory(const fs::path& parent, std::string_view prefix); ~PrivateDirectory(); PrivateDirectory(const PrivateDirectory&) = delete; PrivateDirectory& operator=(const PrivateDirectory&) = delete; + // The destructor leaves the directory where it is, unlocked and + // dated as it lets go of it, so that what is in it can be looked at + // for a minute at least, until it is removed as one that nobody + // holds. Several threads may call it at once. + void Keep() { kept = true; } }; // The link flags that record `dir` as a run path, with -Xlinker rather // than -Wl,: clang splits a -Wl, value at every comma, and `dir` may be a diff --git a/interfaces/Crafter.Build-Test.cppm b/interfaces/Crafter.Build-Test.cppm index 7c51b2e..32acd87 100644 --- a/interfaces/Crafter.Build-Test.cppm +++ b/interfaces/Crafter.Build-Test.cppm @@ -107,6 +107,12 @@ export namespace Crafter { TestSummary RunTests(std::span configurations, const RunTestsOptions& opts, std::span 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 @@ -123,9 +129,10 @@ export namespace Crafter { // 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 runs the test with - // this process's environment as it is. - TestResult RunSingleTest(const Test& test, const std::filesystem::path& binary, std::chrono::seconds timeout, const std::optional& compositorOverride = std::nullopt); + // 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& 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, diff --git a/tests/SysrootStdModule/main.cpp b/tests/SysrootStdModule/main.cpp index e5e984f..9a2d5e5 100644 --- a/tests/SysrootStdModule/main.cpp +++ b/tests/SysrootStdModule/main.cpp @@ -1,6 +1,7 @@ // SPDX-License-Identifier: LGPL-3.0-only // SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® +#include import std; import Crafter.Build; namespace fs = std::filesystem; @@ -52,6 +53,11 @@ int main() { std::error_code ec; fs::remove_all(scratch, ec); fs::create_directories(scratch); + // The cache the build below makes its std module's directory in, which + // is keyed on the sysroot's path: one under the temp directory, which + // is a new one every run of the suite, so the real cache would keep one + // more directory each run. Set while this is the only thread. + setenv("XDG_CACHE_HOME", (scratch / "cache").c_str(), 1); { StageHeaders(scratch / "headers", 230102); @@ -126,8 +132,8 @@ int main() { { // End to end with the real toolchain: a sysroot on a libc++ release // that does not exist fails the build with the one-line explanation - // before clang is run. The target is the host's so the std PCM cache - // directory is the one every other test already uses. + // before clang is run, for the host's target. The std module's cache + // directory is made in the scratch cache (above). const Toolchain& real = GetToolchain(); Check(real.libcxxVersion != 0, "the running toolchain's libc++ release is read from its headers");