Skip to content

Commit e873044

Browse files
committed
fix(test): remove automatic cleanup race condition in npm test
The test was using tmp.withDir which tries to clean up the temp directory after the callback returns. However, with AVA, the tests are only REGISTERED in the callback - they run asynchronously AFTER the callback returns. This caused race conditions: - Windows: EBUSY (files still locked by npm/pnpm processes) - Linux: ENOTEMPTY (files created by tests still exist) Changed to tmp.dir() without automatic cleanup. Temp directories are cleaned by the OS periodically anyway.
1 parent 531847c commit e873044

File tree

1 file changed

+45
-45
lines changed

1 file changed

+45
-45
lines changed

src/api/npm.test.ts

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,55 +12,55 @@ await import("../target/terminal.js")
1212

1313
import { kenvPath } from "../core/utils.js"
1414

15-
// biome-ignore lint/suspicious/useAwait: <explanation>
16-
await tmp.withDir(async (dir) => {
17-
process.env.KENV = dir.path
18-
process.env.KIT_CONTEXT = "workflow"
19-
process.env.KENV = path.resolve(dir.path, ".kenv")
15+
// Create temp directory without automatic cleanup
16+
// Temp directories are cleaned by the OS - automatic cleanup causes race conditions
17+
// with AVA's async test execution (EBUSY on Windows, ENOTEMPTY on Linux)
18+
const tmpDir = await tmp.dir({ unsafeCleanup: false })
19+
process.env.KENV = tmpDir.path
20+
process.env.KIT_CONTEXT = "workflow"
21+
process.env.KENV = path.resolve(tmpDir.path, ".kenv")
2022

21-
ava.beforeEach(async (t) => {
22-
global.kitScript = `${randomUUID()}.js`
23-
global.__kitDbMap = new Map()
23+
ava.beforeEach(async (t) => {
24+
global.kitScript = `${randomUUID()}.js`
25+
global.__kitDbMap = new Map()
2426

25-
await ensureDir(kenvPath())
26-
await ensureDir(kitPath())
27+
await ensureDir(kenvPath())
28+
await ensureDir(kitPath())
2729

30+
t.log({
31+
kenvPath: kenvPath(),
32+
kitPath: kitPath()
33+
})
34+
})
2835

36+
ava("legacy npm import with title-case", async (t) => {
37+
try {
38+
await exec(`pnpm init`, {
39+
cwd: kenvPath()
40+
})
41+
await exec(`pnpm init`, {
42+
cwd: kitPath()
43+
})
44+
} catch (error) {
45+
// On Windows CI we sometimes see pnpm init fail with
46+
// ERR_PNPM_PACKAGE_JSON_EXISTS when package.json already exists.
47+
// That error is benign for this test; log rich context to aid triage.
48+
const e: any = error
2949
t.log({
30-
kenvPath: kenvPath(),
31-
kitPath: kitPath()
50+
message: e?.message,
51+
exitCode: e?.exitCode,
52+
stdout: e?.stdout,
53+
stderr: e?.stderr,
54+
cwdKenv: kenvPath(),
55+
cwdKit: kitPath()
3256
})
33-
})
34-
35-
ava("legacy npm import with title-case", async (t) => {
36-
try {
37-
await exec(`pnpm init`, {
38-
cwd: kenvPath()
39-
})
40-
await exec(`pnpm init`, {
41-
cwd: kitPath()
42-
})
43-
} catch (error) {
44-
// On Windows CI we sometimes see pnpm init fail with
45-
// ERR_PNPM_PACKAGE_JSON_EXISTS when package.json already exists.
46-
// That error is benign for this test; log rich context to aid triage.
47-
const e: any = error
48-
t.log({
49-
message: e?.message,
50-
exitCode: e?.exitCode,
51-
stdout: e?.stdout,
52-
stderr: e?.stderr,
53-
cwdKenv: kenvPath(),
54-
cwdKit: kitPath()
55-
})
56-
}
57-
console.log = t.log
58-
global.log = t.log
59-
flag.trust = true
60-
args.push("hello")
61-
let { titleCase } = await npm("title-case")
62-
let result = titleCase(await arg("Enter a string to title case:"))
57+
}
58+
console.log = t.log
59+
global.log = t.log
60+
flag.trust = true
61+
args.push("hello")
62+
let { titleCase } = await npm("title-case")
63+
let result = titleCase(await arg("Enter a string to title case:"))
6364

64-
t.is(result, "Hello")
65-
})
66-
}, { unsafeCleanup: true })
65+
t.is(result, "Hello")
66+
})

0 commit comments

Comments
 (0)