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-<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.
This commit is contained in:
parent
fcfac0d9cf
commit
93f1c8d9b9
7 changed files with 140 additions and 33 deletions
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue