import { requires } from "./requires"; // The scripts run leaves nothing in the machine's temp directory. // // The suite leaked about 200,000 directories onto the CI host before this // (`vitest.tmpdir.ts` says how). import { execFileSync } from "node:child_process"; import { existsSync, mkdtempSync, readFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { basename, dirname, join } from "node:path"; import { describe, expect, it } from "vitest"; import setup, { RUN_TMP_PREFIX } from "../../vitest.tmpdir"; describe("the scripts run's temp directory", () => { // RULE: A scripts test run makes its scratch space in one private directory that is removed when the run ends, so nothing it makes outlives it in the machine's /tmp. it("is a private one for this run, not the machine's", () => { expect(basename(tmpdir()).startsWith(RUN_TMP_PREFIX)).toBe(true); }); // GNU mktemp (the Linux CI host) reads TMPDIR; macOS mktemp takes the per-user Darwin temp directory and ignores it, so this case holds on Linux only. it("is what a child script's mktemp uses too", ctx => { requires(ctx, { probe: () => process.platform !== "darwin", reason: "GNU mktemp TMPDIR behavior is unavailable on macOS" }); const made = execFileSync("mktemp", ["-d"], { encoding: "utf8" }).trim(); expect(dirname(made)).toBe(tmpdir()); }); it("is removed, contents and all, when the run ends", () => { const saved = { TMPDIR: process.env.TMPDIR, TMP: process.env.TMP, TEMP: process.env.TEMP }; try { const teardown = setup(); const dir = process.env.TMPDIR as string; expect(dirname(dir)).toBe(saved.TMPDIR); const leaked = mkdtempSync(join(tmpdir(), "cotton-fake-")); expect(existsSync(leaked)).toBe(true); teardown(); expect(existsSync(dir)).toBe(false); } finally { Object.assign(process.env, saved); } }); it("is wired into the config the run uses", () => { const text = readFileSync(join(import.meta.dirname, "..", "..", "vitest.scripts.config.ts"), "utf8"); expect(text).toMatch(/globalSetup:\s*\["vitest\.tmpdir\.ts"\]/); }); });