feat(test): give a test that sets Test::wayland a compositor of its own

A test that opens a window opened it on the display of whoever ran the
suite: every `crafter-build test` of Crafter.Graphics put the windows
of its window tests on the desktop, and its other GPU tests connect to
the session's compositor too, since Device::Initialize calls
wl_display_connect whether a window follows or not.

A test that sets the new Test::wayland (TestBuilder::Wayland) now runs
with a headless compositor started for it alone. RunSingleTest starts
the compositor through sh, with XDG_RUNTIME_DIR naming a directory made
for it and without WAYLAND_DISPLAY, WAYLAND_SOCKET or DISPLAY, waits up
to 30 s for a wayland-<n> socket in that directory, runs the test with
WAYLAND_DISPLAY set to the socket's path and with DISPLAY and
WAYLAND_SOCKET unset, then stops the compositor's process group and
removes the directory. The test's command is env with those changes,
and env's command is sh, which execs the test's words: env takes every
word with an '=' in it ahead of its command for an assignment, a
binary's path among them. The compositor runs under timeout(1) as long
as the test may, the wait and 30 s more, and with no limit for a test
that has none, a timeout of 0. It is weston on its headless backend
with its GL renderer, unless CRAFTER_BUILD_COMPOSITOR or the new
--compositor= names another command; `host` gives the tests the
session's own display instead, to watch them. A compositor that ends
before it listens, sh not finding it included, or does not listen in
time fails the test with what it printed. A test that may run where its
compositor is missing declares it in requires_ (tool:weston for the
default), the check RunTests makes before the build, so no skip of its
own is added, and no probe of the command: the command is any sh
command, whose first word need not be the compositor. A test that
fails, crashes or times out keeps what its compositor printed
(TestResult::compositorLog), which RunTests writes to
build/test-logs/<name>.compositor.log, and says when the compositor
ended while it ran.

One compositor per test rather than one per run: no two tests share
one, so tests that run at once cannot cover each other's windows or
take each other's focus, for weston's start-up on every test. weston's
GL renderer, because it imports the dmabufs that a GPU's Vulkan driver
presents, as Mesa's and NVIDIA's do, and the headless backend's default
renderer and pixman import none: against pixman, the NVIDIA driver
reports no format for a window's surface, and Crafter.Graphics's
FrameLoopSync stops at its formatCount > 0 assertion. weston cannot
run the test itself, as `weston -- <test>` exits 0 whatever the test
returned, so crafter-build runs the compositor beside it.

:Platform gains BackgroundCommand, the compositor's process, and
PrivateDirectory, its directory. The process is forked with
PR_SET_PDEATHSIG and runs under `timeout`, which passes the SIGTERM it
gets when crafter-build dies on to the compositor's group, so a
crafter-build killed by Ctrl-C leaves no compositor running. The
directory is made in $XDG_RUNTIME_DIR, or in /tmp where that is not a
directory, rather than in the temp directory, since a socket's path
holds 107 bytes, and is flocked while in use; one that a killed
crafter-build left is removed when the next is made beside it, once it
has not changed for a minute.

New members and a parameter with a default, nothing removed: a minor
release.
This commit is contained in:
Jorijn van der Graaf 2026-11-26 10:37:09 +01:00
commit dc8e9e6f9e
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
7 changed files with 418 additions and 13 deletions

View file

@ -176,6 +176,11 @@ export namespace Crafter {
std::int32_t signal = 0;
std::chrono::milliseconds duration{0};
std::string output;
// What the headless compositor of a test that sets Test::wayland
// printed, kept when the test ran and failed, crashed or timed out,
// for RunTests to write beside the test's own log. A compositor that
// did not start has what it printed in `output`.
std::string compositorLog;
};
// One lint diagnostic. Produced by LintContext::Report — or derived by
@ -817,6 +822,19 @@ export namespace Crafter {
// unavailable runner becomes a Fail instead of a silent Skip — the
// dependency has to be declared to be allowed to be missing.
std::vector<std::string> requires_;
// The test talks to a Wayland compositor, so every run of it gets one
// of its own: a headless compositor started for it alone and stopped
// when it ends. The test finds it through WAYLAND_DISPLAY, the full
// path of its socket, and runs with DISPLAY unset, so the windows it
// opens through WAYLAND_DISPLAY do not reach the session's display.
// The compositor is weston on
// its headless backend unless --compositor= or CRAFTER_BUILD_COMPOSITOR
// names another, or names `host` to give the test the session's own
// display. A test whose compositor cannot be started fails, unless it
// declares the compositor in requires_ (tool:weston for the default),
// which skips it where that is missing. README, "Tests that open
// windows".
bool wayland = false;
};
// Directory holding the std module PCM for this Configuration,

View file

@ -58,6 +58,59 @@ namespace Crafter {
// and not to crafter-build, which then sees it as exit code 130.
// Project scripts reach this through ScriptContext::Run.
std::int32_t RunCommandAttached(std::string_view command);
// One change to the environment a command inherits from this process:
// the variable set to `value`, or taken out when there is none.
struct EnvironmentChange {
std::string name;
std::optional<std::string> value;
};
// A command that runs beside its caller, through sh, until Stop() or the
// destructor ends it: the compositor of a test that sets Test::wayland.
// Its stdin is /dev/null and its stdout and stderr go to `log`. It is
// the leader of a process group of its own, so that Stop() reaches what
// it starts in turn, as long as that stays in the group, and runs under `timeout`, which ends that group
// after `lifetime` if nothing else has, a `lifetime` of 0 being none,
// as for timeout(1). The timeout process is also sent
// SIGTERM, which it passes on to the group, when the thread that
// constructed this ends, so a crafter-build that is killed, by Ctrl-C
// or otherwise, leaves no command of this kind running. That is why the
// object has to stay on the thread that made it. Throws when the command
// cannot be started.
class BackgroundCommand {
std::int32_t pid = -1;
std::optional<std::int32_t> exitCode;
bool reaped = false;
public:
BackgroundCommand(std::string_view command, std::span<const EnvironmentChange> environment, const fs::path& log, std::chrono::seconds lifetime);
~BackgroundCommand();
BackgroundCommand(const BackgroundCommand&) = delete;
BackgroundCommand& operator=(const BackgroundCommand&) = delete;
// The command's exit code once it has ended on its own, 128+signal
// when a signal ended it; nullopt while it runs. Does not wait.
std::optional<std::int32_t> Exited();
// Sends the group SIGTERM, then SIGKILL to whatever of it is left
// once the command has ended or `grace` has passed, and waits for the
// command. Further calls do nothing.
void Stop(std::chrono::milliseconds grace);
};
// 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.
class PrivateDirectory {
std::int32_t fd = -1;
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 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
// project's own directory, named whatever its owner chose. Quoted for

View file

@ -53,6 +53,9 @@ export namespace Crafter {
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 {
@ -75,6 +78,10 @@ export namespace Crafter {
// 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 {
@ -98,7 +105,27 @@ export namespace Crafter {
// 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 = {});
TestResult RunSingleTest(const Test& test, const std::filesystem::path& binary, std::chrono::seconds timeout);
// Runs `binary` as `test` with its runner, its arguments and `timeout`,
// and reads the outcome from how it ended (exit 0 passes, 77 skips).
//
// 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 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<std::string>& compositorOverride = std::nullopt);
// The configurations of the project crafter-build was pointed at, by
// name. Run() hands SetParentProject what the project file returned,