Skip to content

Commit e086c99

Browse files
byapparovclaude
andcommitted
test: make bun test --rerun-each usable (#200)
The preload removed its data directory in afterAll, but bun runs preload hooks after every --rerun-each pass, so every pass after the first failed with SQLITE_CANTOPEN. Recreating the directory is not enough: in-memory caches such as Instance still point at rows in the deleted database. Keep the directory for the whole run and instead remove the data directories of earlier runs whose process has exited. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015nwvfUHdM3bgMgD9Zpz7Mj
1 parent c4db2aa commit e086c99

1 file changed

Lines changed: 21 additions & 19 deletions

File tree

‎packages/cli/test/preload.ts‎

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,32 @@
33
import os from "os"
44
import path from "path"
55
import fs from "fs/promises"
6-
import { afterAll } from "bun:test"
76

87
// Set XDG env vars FIRST, before any src/ imports
9-
const dir = path.join(os.tmpdir(), "aictrl-test-data-" + process.pid)
8+
const DATA_PREFIX = "aictrl-test-data-"
9+
const dir = path.join(os.tmpdir(), DATA_PREFIX + process.pid)
1010
await fs.mkdir(dir, { recursive: true })
11-
afterAll(async () => {
12-
const { Database } = await import("../src/storage/db")
13-
Database.close()
14-
const busy = (error: unknown) =>
15-
typeof error === "object" && error !== null && "code" in error && error.code === "EBUSY"
16-
const rm = async (left: number): Promise<void> => {
17-
Bun.gc(true)
18-
await Bun.sleep(100)
19-
return fs.rm(dir, { recursive: true, force: true }).catch((error) => {
20-
if (!busy(error)) throw error
21-
if (left <= 1) throw error
22-
return rm(left - 1)
23-
})
24-
}
2511

26-
// Windows can keep SQLite WAL handles alive until GC finalizers run, so we
27-
// force GC and retry teardown to avoid flaky EBUSY in test cleanup.
28-
await rm(30)
12+
// bun has no hook that runs once at the end of a test run: preload hooks run
13+
// after every `--rerun-each` pass, and removing the directory there leaves the
14+
// next pass without its database. So each run removes the data directories of
15+
// earlier runs whose process has exited instead.
16+
function running(pid: number) {
17+
try {
18+
process.kill(pid, 0)
19+
return true
20+
} catch (error) {
21+
return (error as NodeJS.ErrnoException).code === "EPERM"
22+
}
23+
}
24+
const stale = (await fs.readdir(os.tmpdir())).filter((name) => {
25+
const pid = Number(name.slice(DATA_PREFIX.length))
26+
return name.startsWith(DATA_PREFIX) && Number.isInteger(pid) && pid !== process.pid && !running(pid)
2927
})
28+
// Best effort: a directory that cannot be removed now is retried by the next run.
29+
await Promise.all(
30+
stale.map((name) => fs.rm(path.join(os.tmpdir(), name), { recursive: true, force: true }).catch(() => {})),
31+
)
3032

3133
process.env["XDG_DATA_HOME"] = path.join(dir, "share")
3234
process.env["XDG_CACHE_HOME"] = path.join(dir, "cache")

0 commit comments

Comments
 (0)