From 0b239d163aa39ac362e5dada6f5247079df56823 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 20 Apr 2026 15:59:29 +0100 Subject: [PATCH] test: share build cache fixture --- test/scripts/build-all.test.ts | 99 ++++++++++++++++------------------ 1 file changed, 45 insertions(+), 54 deletions(-) diff --git a/test/scripts/build-all.test.ts b/test/scripts/build-all.test.ts index 3d013cb513d..2a48c07b0b1 100644 --- a/test/scripts/build-all.test.ts +++ b/test/scripts/build-all.test.ts @@ -12,6 +12,45 @@ import { writeBuildAllStepCacheStamp, } from "../../scripts/build-all.mjs"; +function withBuildCacheFixture( + run: (fixture: { + rootDir: string; + inputPath: string; + outputPath: string; + step: { + label: string; + cache: { + inputs: string[]; + outputs: string[]; + }; + }; + }) => void, +) { + const rootDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-build-cache-")); + try { + const inputPath = path.join(rootDir, "src/input.ts"); + const outputPath = path.join(rootDir, "dist/output.js"); + fs.mkdirSync(path.dirname(inputPath), { recursive: true }); + fs.mkdirSync(path.dirname(outputPath), { recursive: true }); + fs.writeFileSync(inputPath, "input"); + fs.writeFileSync(outputPath, "output"); + run({ + rootDir, + inputPath, + outputPath, + step: { + label: "cached", + cache: { + inputs: ["src"], + outputs: ["dist"], + }, + }, + }); + } finally { + fs.rmSync(rootDir, { force: true, recursive: true }); + } +} + describe("resolveBuildAllStep", () => { it("routes pnpm steps through the npm_execpath pnpm runner on Windows", () => { const step = BUILD_ALL_STEPS.find((entry) => entry.label === "canvas:a2ui:bundle"); @@ -111,21 +150,7 @@ describe("resolveBuildAllSteps", () => { describe("resolveBuildAllStepCacheState", () => { it("marks cacheable steps fresh when the input signature matches", () => { - const rootDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-build-cache-")); - try { - const inputPath = path.join(rootDir, "src/input.ts"); - const outputPath = path.join(rootDir, "dist/output.js"); - fs.mkdirSync(path.dirname(inputPath), { recursive: true }); - fs.mkdirSync(path.dirname(outputPath), { recursive: true }); - fs.writeFileSync(inputPath, "input"); - fs.writeFileSync(outputPath, "output"); - const step = { - label: "cached", - cache: { - inputs: ["src"], - outputs: ["dist"], - }, - }; + withBuildCacheFixture(({ rootDir, step }) => { const cacheState = resolveBuildAllStepCacheState(step, { rootDir }); writeBuildAllStepCacheStamp(step, cacheState, { rootDir }); @@ -136,27 +161,11 @@ describe("resolveBuildAllStepCacheState", () => { inputFiles: 1, outputFiles: 1, }); - } finally { - fs.rmSync(rootDir, { force: true, recursive: true }); - } + }); }); it("marks cacheable steps stale when an input changes", () => { - const rootDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-build-cache-")); - try { - const inputPath = path.join(rootDir, "src/input.ts"); - const outputPath = path.join(rootDir, "dist/output.js"); - fs.mkdirSync(path.dirname(inputPath), { recursive: true }); - fs.mkdirSync(path.dirname(outputPath), { recursive: true }); - fs.writeFileSync(inputPath, "input"); - fs.writeFileSync(outputPath, "output"); - const step = { - label: "cached", - cache: { - inputs: ["src"], - outputs: ["dist"], - }, - }; + withBuildCacheFixture(({ rootDir, inputPath, step }) => { const cacheState = resolveBuildAllStepCacheState(step, { rootDir }); writeBuildAllStepCacheStamp(step, cacheState, { rootDir }); fs.writeFileSync(inputPath, "changed"); @@ -166,27 +175,11 @@ describe("resolveBuildAllStepCacheState", () => { fresh: false, reason: "stale", }); - } finally { - fs.rmSync(rootDir, { force: true, recursive: true }); - } + }); }); it("restores cached outputs when generated files were removed", () => { - const rootDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-build-cache-")); - try { - const inputPath = path.join(rootDir, "src/input.ts"); - const outputPath = path.join(rootDir, "dist/output.js"); - fs.mkdirSync(path.dirname(inputPath), { recursive: true }); - fs.mkdirSync(path.dirname(outputPath), { recursive: true }); - fs.writeFileSync(inputPath, "input"); - fs.writeFileSync(outputPath, "output"); - const step = { - label: "cached", - cache: { - inputs: ["src"], - outputs: ["dist"], - }, - }; + withBuildCacheFixture(({ rootDir, outputPath, step }) => { const cacheState = resolveBuildAllStepCacheState(step, { rootDir }); writeBuildAllStepCacheStamp(step, cacheState, { rootDir }); fs.rmSync(path.join(rootDir, "dist"), { force: true, recursive: true }); @@ -200,8 +193,6 @@ describe("resolveBuildAllStepCacheState", () => { }); expect(restoreBuildAllStepCacheOutputs(restorable, { rootDir })).toBe(true); expect(fs.readFileSync(outputPath, "utf8")).toBe("output"); - } finally { - fs.rmSync(rootDir, { force: true, recursive: true }); - } + }); }); });