-
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathscratch-registry.mjs
More file actions
35 lines (32 loc) · 1.41 KB
/
Copy pathscratch-registry.mjs
File metadata and controls
35 lines (32 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Scratch dirs that survive the failure they exist to explain.
//
// A test body that throws never reaches its own cleanup, so every dir is
// registered here. Removal is gated on the FILE's outcome: after a green run the
// scratch is litter, after a red one it is the only record of what the case was
// looking at when it died.
//
// `process.on("exit")` and not `after()`, because the hook runs too early to
// know: measured on node 24, `process.exitCode` is `undefined` inside a
// file-level `after()` on a file that HAS a failure, and `1` by exit on the same
// file (and `undefined` at both points on a file without one).
//
// Sync removal for the same reason -- an exit handler cannot await, so a
// promise-based rm would be registered and never run.
import { mkdtemp } from "node:fs/promises";
import { rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
const registered = [];
export async function scratchDir(prefix) {
const d = await mkdtemp(join(tmpdir(), prefix));
registered.push(d);
return d;
}
process.on("exit", () => {
for (const d of registered) {
// Named on the way out, or keeping them helps nobody: the runner's output is
// the only place the caller will look for the path.
if (process.exitCode) { process.stderr.write(`[scratch kept] ${d}\n`); continue; }
try { rmSync(d, { recursive: true, force: true }); } catch { /* best effort */ }
}
});