sieobex/tests/globalSetup.ts
Konstantin Pereiaslov 0a3c3c8075
Initial slop
2026-09-19 13:04:16 -05:00

47 lines
1.4 KiB
TypeScript

// QEMU opens a GTK window even when nothing looks at it, so the emulators need
// an X server. One Xvfb is started here for the whole e2e run, unless the
// machine already has a display.
import { spawn, spawnSync, ChildProcess } from "node:child_process";
let xvfb: ChildProcess | undefined;
export async function setup(): Promise<void> {
if (process.env.DISPLAY)
return;
if (!spawnSync("which", ["Xvfb"], { encoding: "utf8" }).stdout.trim()) {
console.warn("No DISPLAY and no Xvfb: the emulators will not start. Install Xvfb or run under an X server.");
return;
}
// -displayfd lets Xvfb pick a free display number and report it back
const child = spawn("Xvfb", ["-displayfd", "3", "-screen", "0", "1280x1024x24", "-nolisten", "tcp"], {
detached: true,
stdio: ["ignore", "ignore", "ignore", "pipe"],
});
const display = await new Promise<string>((resolve, reject) => {
let out = "";
child.stdio[3]!.on("data", (chunk: Buffer) => {
out += chunk.toString();
if (out.includes("\n"))
resolve(out.trim());
});
child.once("error", reject);
child.once("exit", (code) => reject(new Error(`Xvfb exited with ${code}`)));
});
xvfb = child;
process.env.DISPLAY = `:${display}`;
console.log(`Xvfb running on DISPLAY=${process.env.DISPLAY}`);
}
export async function teardown(): Promise<void> {
if (!xvfb)
return;
try {
process.kill(-xvfb.pid!, "SIGTERM");
} catch {
xvfb.kill("SIGTERM");
}
xvfb = undefined;
}