Skip to content

Commit 9069180

Browse files
committed
Attribute guard-real-projects-dir leaks to this test run
Compare against the real ~/.corbits/projects only for entries this run's own project keys account for, instead of any new entry: a plain before/after snapshot also picks up sibling checkouts running their own bun run check concurrently, which is our normal multi-worktree workflow and not something this suite is responsible for. Point TMPDIR/TMP/TEMP at a per-invocation scratch dir carrying this run's id before spawning bun test. project-key.ts derives a project key from the realpath of a test's mkdtemp'd cwd/home, so a real leak's key inherits the run id as a substring; only those entries fail the guard.
1 parent 513133a commit 9069180

1 file changed

Lines changed: 36 additions & 4 deletions

File tree

‎scripts/guard-real-projects-dir.ts‎

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { homedir } from "node:os";
1+
import { randomUUID } from "node:crypto";
2+
import { homedir, tmpdir } from "node:os";
23
import { join } from "node:path";
3-
import { readdir } from "node:fs/promises";
4+
import { mkdir, readdir, rm } from "node:fs/promises";
45
import { spawn } from "node:child_process";
56

67
// Runs `bun test` and fails the run if any test wrote into the real
@@ -12,6 +13,18 @@ import { spawn } from "node:child_process";
1213
// This is a backstop, not a substitute for threading `home` correctly: a
1314
// leak is only caught after it already wrote into a real directory once,
1415
// which this script then reports and leaves in place for inspection.
16+
//
17+
// Attribution: a plain before/after snapshot of the whole directory also
18+
// picks up entries from other checkouts on this machine running their own
19+
// `bun run check` concurrently — a routine part of working across several
20+
// worktrees, and not something this run's suite is responsible for. To tell
21+
// the two apart, this run's own temp dirs are pointed at a unique,
22+
// per-invocation scratch directory (via TMPDIR) whose name carries this
23+
// run's id. `src/session/project-key.ts` derives a project key from the
24+
// realpath of the test's `cwd`/`home`, and since those are mkdtemp'd inside
25+
// our scratch dir here, a real leak's project key inherits our run id as a
26+
// substring. Only entries that carry it are ours to fail on; anything else
27+
// is a sibling checkout's own business.
1528

1629
const projectsDir = join(homedir(), ".corbits", "projects");
1730

@@ -27,14 +40,33 @@ async function listEntries(): Promise<Set<string>> {
2740
async function main(): Promise<void> {
2841
const before = await listEntries();
2942

43+
const runId = randomUUID();
44+
const runTmpDir = join(tmpdir(), `corbits-test-guard-${runId}`);
45+
await mkdir(runTmpDir, { recursive: true });
46+
3047
const args = process.argv.slice(2);
31-
const child = spawn("bun", ["test", ...args], { stdio: "inherit" });
48+
const child = spawn("bun", ["test", ...args], {
49+
stdio: "inherit",
50+
env: { ...process.env, TMPDIR: runTmpDir, TMP: runTmpDir, TEMP: runTmpDir },
51+
});
3252
const testExitCode = await new Promise<number>((resolve) => {
3353
child.on("exit", (code) => resolve(code ?? 1));
3454
});
3555

56+
await rm(runTmpDir, { recursive: true, force: true }).catch(() => {});
57+
3658
const after = await listEntries();
37-
const leaked = [...after].filter((name) => !before.has(name));
59+
const newEntries = [...after].filter((name) => !before.has(name));
60+
const leaked = newEntries.filter((name) => name.includes(runId));
61+
const unattributed = newEntries.filter((name) => !name.includes(runId));
62+
63+
if (unattributed.length > 0) {
64+
process.stderr.write(
65+
`\nguard-real-projects-dir: ignoring ${unattributed.length} new ${projectsDir} ` +
66+
"entries not created by this run (likely another checkout's concurrent " +
67+
`test/check run):\n${unattributed.map((name) => ` ${name}`).join("\n")}\n`,
68+
);
69+
}
3870

3971
if (leaked.length > 0) {
4072
process.stderr.write(

0 commit comments

Comments
 (0)