From e0d483b1d0b4cf77de2fce317b953c2bfb8d3102 Mon Sep 17 00:00:00 2001 From: Michael Gilardi <11632652+mgilardi@users.noreply.github.com> Date: Mon, 16 Nov 2026 12:08:34 -0700 Subject: [PATCH] Clean up test servers when a run ends, pass, fail or interrupted D226: tests remove everything they created and touch nothing else. The harness now records each server folder and Java process it makes and removes them at exit, on Ctrl-C, SIGTERM and SIGHUP too. A folder left by an earlier run stops the run instead of being deleted. KEEP_TEST_SERVERS=1 keeps the folders. Checked on MobShowdown with a full run and an interrupted one; both left nothing behind. The other harnesses share the code but weren't run here. Co-Authored-By: Claude Opus 5.5 --- docs/test-audit.md | 6 ++++++ test/harness.mjs | 13 +++++++++++++ 2 files changed, 19 insertions(+) diff --git a/docs/test-audit.md b/docs/test-audit.md index 28df0ab..2a8ffbc 100644 --- a/docs/test-audit.md +++ b/docs/test-audit.md @@ -55,3 +55,9 @@ Rewild changes world files, so its safety promises come first. "Acceptance" is ` **Skipped, with reasons:** - How a reset area looks and blends in a real client: real client only, in the README's untested list. - LanternGraves as a real keep provider: it doesn't register one yet; `keep.mjs` uses a stand-in with the same name. + +## 2026-11-16: cleanup (D226) + +- **Covered:** the harness records each server folder it creates and each Java process it starts. When the run ends (pass, fail, an uncaught error, Ctrl-C, SIGTERM or SIGHUP) it kills those processes and deletes those folders, and nothing else. A folder from an earlier run that's in the way stops the run; it isn't deleted. `KEEP_TEST_SERVERS=1` keeps the folders for debugging. +- **Checked by hand:** on MobShowdown, a full run (42/42) and a run interrupted after 45 seconds both left the work folder empty, with no Java left and the test port closed. The other harnesses use the same code. +- **Gap skipped:** the suite can't check its own leftovers, because cleanup runs as the process exits. A wrapper that checks after the suite exits would add more machinery than the check is worth here. diff --git a/test/harness.mjs b/test/harness.mjs index 6e8ee1e..0cf0a53 100644 --- a/test/harness.mjs +++ b/test/harness.mjs @@ -34,9 +34,21 @@ export function report(file) { return failed.length } +// D226: a run removes everything it created, pass, fail or interrupted, and nothing else. Folders made by freshServer +// are recorded and deleted at exit, after any server still running is killed. KEEP_TEST_SERVERS=1 keeps them. +const created = new Set(), procs = new Set() +function cleanupAll() { + for (const p of procs) try { if (p.exitCode === null && p.signalCode === null) p.kill('SIGKILL') } catch {} + if (process.env.KEEP_TEST_SERVERS === '1') return + for (const d of created) try { rmSync(d, { recursive: true, force: true, maxRetries: 10, retryDelay: 300 }) } catch {} +} +process.on('exit', cleanupAll) +for (const sig of ['SIGINT', 'SIGTERM', 'SIGHUP']) process.on(sig, () => { cleanupAll(); process.exit(128 + ({ SIGHUP: 1, SIGINT: 2, SIGTERM: 15 })[sig]) }) + /** A fresh server folder. Paper's downloaded libraries are cloned from a primed folder to save time. */ export function freshServer(version, { seed = 424242, plugins = [], config = null, props = {} } = {}) { const dir = mkdtempSync(path.join(os.tmpdir(), `rewild-test-${version}-`)) + created.add(dir) copyFileSync(path.join(CACHE, `paper-${version}.jar`), path.join(dir, 'paper.jar')) const prime = path.join(CACHE, `prime-${version}`) for (const f of ['libraries', 'versions', 'cache']) { @@ -74,6 +86,7 @@ export class Server { this.exit = new Promise(resolve => { this.proc = spawn(JAVA, ['-Xms1G', '-Xmx3G', '-Dterminal.jline=false', '-Dterminal.ansi=false', ...jvm, '-jar', 'paper.jar', 'nogui'], { cwd: this.dir, stdio: ['pipe', 'pipe', 'pipe'] }) + procs.add(this.proc) const add = d => { this.log += d.toString() } this.proc.stdout.on('data', add) this.proc.stderr.on('data', add)