diff --git a/changes/fix-test-sandbox-on-tmpfs.md b/changes/fix-test-sandbox-on-tmpfs.md new file mode 100644 index 0000000..8842962 --- /dev/null +++ b/changes/fix-test-sandbox-on-tmpfs.md @@ -0,0 +1,6 @@ +### Fixed + +- **On Linux the test sandbox starts in the runtime folder (`$XDG_RUNTIME_DIR`), which is in memory.** Every commit of + a store waits for `fsync`, and on a disk other jobs keep busy a CRM sync in a test took 14 s instead of 1.5 s, past + the 15 s test timeout, so a CLI's local release check failed three times in a row. With no runtime folder to write + to, the sandbox starts in the temporary folder as before. diff --git a/src/testing/sandbox.test.ts b/src/testing/sandbox.test.ts index b24f9ad..a061d0a 100644 --- a/src/testing/sandbox.test.ts +++ b/src/testing/sandbox.test.ts @@ -44,12 +44,26 @@ describe("enterSandbox", () => { sandbox.restore() }) - it("elsewhere starts from the temporary folder", () => { - const sandbox = enterSandbox({}, "linux") - expect(sandbox.dir.startsWith(tmpdir())).toBe(true) + it("on Linux starts from the runtime folder, which is in memory, when it is there to write to", () => { + const runtime = join(tmpdir(), "runtime") + mkdirSync(runtime) + const sandbox = enterSandbox({ XDG_RUNTIME_DIR: runtime }, "linux") + expect(sandbox.dir.startsWith(join(runtime, "wirecat-test-"))).toBe(true) sandbox.restore() }) + it("elsewhere, or with no runtime folder to write to, starts from the temporary folder", () => { + for (const [env, platform] of [ + [{}, "linux"], + [{ XDG_RUNTIME_DIR: join(tmpdir(), "missing") }, "linux"], + [{ XDG_RUNTIME_DIR: tmpdir() }, "win32"], + ] as const) { + const sandbox = enterSandbox({ ...env }, platform) + expect(sandbox.dir.startsWith(join(tmpdir(), "wirecat-test-"))).toBe(true) + sandbox.restore() + } + }) + it("removes the folder when a file fails while loading or in its afterAll, and not when one imitates SIGTERM", () => { const parent = join(tmpdir(), "child-run") mkdirSync(parent) @@ -57,7 +71,15 @@ describe("enterSandbox", () => { const config = fileURLToPath(new URL("../../test/fixtures/sandbox-left/vitest.config.ts", import.meta.url)) const run = spawnSync(process.execPath, [vitest, "run", "--reporter=verbose", "--config", config], { // CI sets FORCE_COLOR; colour codes would split the reporter's lines this test reads. - env: { ...process.env, TMPDIR: parent, TMP: parent, TEMP: parent, NO_COLOR: "1", FORCE_COLOR: "0" }, + env: { + ...process.env, + TMPDIR: parent, + TMP: parent, + TEMP: parent, + XDG_RUNTIME_DIR: parent, + NO_COLOR: "1", + FORCE_COLOR: "0", + }, encoding: "utf8", }) diff --git a/src/testing/sandbox.ts b/src/testing/sandbox.ts index 88596c8..e39a1d3 100644 --- a/src/testing/sandbox.ts +++ b/src/testing/sandbox.ts @@ -1,4 +1,4 @@ -import { mkdirSync, mkdtempSync, rmSync } from "node:fs" +import { accessSync, constants, mkdirSync, mkdtempSync, rmSync } from "node:fs" import { createRequire } from "node:module" import { tmpdir } from "node:os" import { join } from "node:path" @@ -11,8 +11,24 @@ export interface Sandbox { const OVERRIDE = /_(CONFIG|STATE|CACHE)_DIR$/ -// macOS keeps a Unix socket path under 104 bytes, and its per-user temporary folder alone takes about 50. -const baseFor = (platform: NodeJS.Platform): string => (platform === "darwin" ? "/tmp" : tmpdir()) +const writable = (dir: string | undefined): dir is string => { + if (!dir) return false + try { + accessSync(dir, constants.W_OK) + return true + } catch { + return false + } +} + +// macOS keeps a Unix socket path under 104 bytes, and its per-user temporary folder alone takes about 50. On Linux the +// runtime folder is in memory: a store's every commit waits for fsync, and on a disk other jobs keep busy that made a +// CRM sync in a test take 14 s instead of 1.5 s, past the test timeout (attio 0.5.1's release, 2026-11-24). +const baseFor = (platform: NodeJS.Platform, env: NodeJS.ProcessEnv): string => { + if (platform === "darwin") return "/tmp" + if (platform === "linux" && writable(env.XDG_RUNTIME_DIR)) return env.XDG_RUNTIME_DIR + return tmpdir() +} const LEFT = Symbol.for("@wirecat/cli-base/testing/sandboxes") @@ -58,7 +74,7 @@ export const enterSandbox = ( env: NodeJS.ProcessEnv = process.env, platform: NodeJS.Platform = process.platform, ): Sandbox => { - const dir = mkdtempSync(join(baseFor(platform), "wirecat-test-")) + const dir = mkdtempSync(join(baseFor(platform, env), "wirecat-test-")) const forget = removeOnExit(dir) const home = join(dir, "home") mkdirSync(home)