import { access, mkdtemp, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import { afterEach, describe, expect, it } from "vitest"; import { SHARED_FIXTURE_BUILD_ROOT_VARIABLE, sharedFixtureBuild, startSharedFixtureBuilds, } from "../e2e/support/shared-fixture-builds.ts"; const roots: string[] = []; afterEach(async () => { await Promise.all( roots.splice(0).map((root) => rm(root, { recursive: true, force: true })), ); }); async function sharedEnvironment(): Promise { const root = await mkdtemp(path.join(tmpdir(), "shared-builds-test-")); roots.push(root); return { [SHARED_FIXTURE_BUILD_ROOT_VARIABLE]: root }; } describe("one fixture build per e2e run", () => { it("builds a key once and hands every caller the same copy", async () => { const environment = await sharedEnvironment(); let builds = 0; const create = async (root: string) => { builds += 1; await new Promise((resolve) => setTimeout(resolve, 50)); await writeFile(path.join(root, "dist.txt"), "built"); return { dist: path.join(root, "dist.txt") }; }; const results = await Promise.all( [1, 2, 3].map(() => sharedFixtureBuild("fixture-a", "same", create, environment)), ); expect(builds).toBe(1); expect(new Set(results.map((result) => result.root)).size).toBe(1); for (const result of results) { expect(result.shared).toBe(true); expect(result.value).toEqual({ dist: path.join(result.root, "dist.txt") }); } await access(results[0]!.value.dist); }); it("refuses a second definition under the same key", async () => { const environment = await sharedEnvironment(); await sharedFixtureBuild("fixture-b", "first", async () => ({}), environment); await expect( sharedFixtureBuild("fixture-b", "second", async () => ({}), environment), ).rejects.toThrow(/fixture-b was built from another definition/u); }); it("fails the waiting callers with the builder's failure", async () => { const environment = await sharedEnvironment(); const owner = sharedFixtureBuild( "fixture-c", "same", async () => { await new Promise((resolve) => setTimeout(resolve, 50)); throw new Error("vite build exited 1"); }, environment, ); const waiter = sharedFixtureBuild("fixture-c", "same", async () => ({}), environment); await expect(owner).rejects.toThrow(/vite build exited 1/u); await expect(waiter).rejects.toThrow( /fixture-c failed in another worker:[\s\S]*vite build exited 1/u, ); }); it("builds a private copy and removes it on failure without a shared root", async () => { const result = await sharedFixtureBuild("fixture-d", "x", async (root) => root, {}); roots.push(result.root); expect(result).toMatchObject({ shared: false, value: result.root }); expect(path.dirname(result.root)).toBe(tmpdir()); let failedRoot = ""; await expect( sharedFixtureBuild("fixture-d", "x", async (root) => { failedRoot = root; throw new Error("broken copy"); }, {}), ).rejects.toThrow(/broken copy/u); await expect(access(failedRoot)).rejects.toMatchObject({ code: "ENOENT" }); }); it("refuses a key that is not one lower-case path segment", async () => { for (const key of ["", "../x", "a/b", "Fixture", `a${"b".repeat(64)}`]) { await expect(sharedFixtureBuild(key, "x", async () => ({}), {})).rejects.toThrow( /Invalid fixture build key/u, ); } }); it("gives the run a root and removes it with the teardown", async () => { const environment: NodeJS.ProcessEnv = {}; const teardown = await startSharedFixtureBuilds(environment); const root = environment[SHARED_FIXTURE_BUILD_ROOT_VARIABLE]; expect(root).toBeTruthy(); await sharedFixtureBuild("fixture-e", "x", async () => ({}), environment); await teardown(); expect(environment[SHARED_FIXTURE_BUILD_ROOT_VARIABLE]).toBeUndefined(); await expect(access(root!)).rejects.toMatchObject({ code: "ENOENT" }); }); });