Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions changes/fix-test-sandbox-on-tmpfs.md
Original file line number Diff line number Diff line change
@@ -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.
30 changes: 26 additions & 4 deletions src/testing/sandbox.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,42 @@ 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)
const vitest = fileURLToPath(new URL("../../node_modules/vitest/vitest.mjs", import.meta.url))
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",
})

Expand Down
24 changes: 20 additions & 4 deletions src/testing/sandbox.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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")

Expand Down Expand Up @@ -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)
Expand Down