Skip to content

Commit 3266d12

Browse files
committed
test: guard multi-project Vitest cache ownership
1 parent 50b675f commit 3266d12

1 file changed

Lines changed: 151 additions & 19 deletions

File tree

‎test/vitest-performance-config.test.ts‎

Lines changed: 151 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -106,17 +106,41 @@ describe("loadVitestExperimentalConfig", () => {
106106

107107
describe("filesystem module cache ownership", () => {
108108
const tempDirs = useAutoCleanupTempDirTracker(afterEach);
109+
const cli = path.join(
110+
path.dirname(createRequire(import.meta.url).resolve("vitest/package.json")),
111+
"vitest.mjs",
112+
);
113+
const run = (
114+
root: string,
115+
checkout: string,
116+
args: string[] = [],
117+
env: NodeJS.ProcessEnv = {},
118+
) => {
119+
const result = spawnSync(
120+
process.execPath,
121+
[cli, "run", "--config", path.join(checkout, "vitest.config.mjs"), ...args],
122+
{
123+
cwd: checkout,
124+
encoding: "utf8",
125+
env: {
126+
PATH: process.env.PATH,
127+
SystemRoot: process.env.SystemRoot,
128+
CI: "1",
129+
HOME: root,
130+
...env,
131+
},
132+
timeout: 15_000,
133+
},
134+
);
135+
expect(result.status, `${result.error ?? ""}\n${result.stdout}\n${result.stderr}`).toBe(0);
136+
};
109137

110138
it("preserves another checkout's cache when shared dependencies change", () => {
111139
const root = tempDirs.make("oc-vitest-cache-ownership-");
112140
const sharedModules = path.join(root, "shared", "node_modules");
113141
fs.mkdirSync(path.join(sharedModules, ".pnpm"), { recursive: true });
114142
const lockfile = path.join(sharedModules, ".pnpm", "lock.yaml");
115143
fs.writeFileSync(lockfile, "lockfileVersion: 1\n");
116-
const cli = path.join(
117-
path.dirname(createRequire(import.meta.url).resolve("vitest/package.json")),
118-
"vitest.mjs",
119-
);
120144
const prepareCheckout = (name: string) => {
121145
const checkout = path.join(root, name);
122146
fs.mkdirSync(checkout);
@@ -147,29 +171,137 @@ describe("filesystem module cache ownership", () => {
147171
);
148172
return { checkout, cacheConfig };
149173
};
150-
const run = (checkout: string) => {
151-
const result = spawnSync(
152-
process.execPath,
153-
[cli, "run", "--config", path.join(checkout, "vitest.config.mjs")],
154-
{
155-
cwd: checkout,
156-
encoding: "utf8",
157-
env: { PATH: process.env.PATH, SystemRoot: process.env.SystemRoot, CI: "1", HOME: root },
158-
timeout: 15_000,
159-
},
160-
);
161-
expect(result.status, `${result.error ?? ""}\n${result.stdout}\n${result.stderr}`).toBe(0);
162-
};
163174
const first = prepareCheckout("first");
164175
const second = prepareCheckout("second");
165-
run(first.checkout);
176+
run(root, first.checkout);
166177
const firstCache =
167178
first.cacheConfig.experimental?.fsModuleCachePath ??
168179
path.join(sharedModules, ".experimental-vitest-cache");
169180
const sentinel = path.join(firstCache, "first-checkout-sentinel");
170181
fs.writeFileSync(sentinel, "owned by first checkout");
171182
fs.writeFileSync(lockfile, "lockfileVersion: 2\n");
172-
run(second.checkout);
183+
run(root, second.checkout);
173184
expect(fs.readFileSync(sentinel, "utf8")).toBe("owned by first checkout");
174185
});
186+
187+
it("reuses shared project transforms after a lock transition without serving a stale later project", () => {
188+
const root = fs.realpathSync(tempDirs.make("oc-vitest-cache-projects-"));
189+
fs.mkdirSync(path.join(root, "node_modules"));
190+
fs.writeFileSync(
191+
path.join(root, "package.json"),
192+
JSON.stringify({ name: "cache-projects", type: "module", workspaces: [] }),
193+
);
194+
const generation = path.join(root, "dependency-generation.txt");
195+
const transitionLock = (version: string) => {
196+
// bun.lock is only a recognized hash input; no package manager is invoked.
197+
fs.writeFileSync(path.join(root, "bun.lock"), JSON.stringify({ version }));
198+
fs.writeFileSync(generation, version);
199+
};
200+
transitionLock("1.0.0");
201+
for (const name of ["A", "B"]) {
202+
const project = path.join(root, name);
203+
fs.mkdirSync(project);
204+
fs.writeFileSync(path.join(project, "transforms.txt"), "");
205+
fs.writeFileSync(
206+
path.join(project, "subject.js"),
207+
'export const version = "__DEPENDENCY_VERSION__";\n',
208+
);
209+
fs.copyFileSync(
210+
path.join(project, "subject.js"),
211+
path.join(project, "configured-subject.js"),
212+
);
213+
fs.writeFileSync(
214+
path.join(project, "fixture.test.js"),
215+
`import { readFileSync } from "node:fs";
216+
import { version } from "fixture-subject";
217+
test("executes the current dependency generation", () => {
218+
expect(version).toBe(readFileSync(${JSON.stringify(generation)}, "utf8"));
219+
});
220+
`,
221+
);
222+
}
223+
const shardOwner = new URL("./vitest/vitest.project-shard-config.ts", import.meta.url).href;
224+
const scopedOwner = new URL("./vitest/vitest.scoped-config.ts", import.meta.url).href;
225+
const configFile = path.join(root, "vitest.config.mjs");
226+
fs.writeFileSync(
227+
configFile,
228+
`import { appendFileSync, readFileSync } from "node:fs";
229+
import path from "node:path";
230+
import { createProjectShardVitestConfig } from ${JSON.stringify(shardOwner)};
231+
import { createScopedVitestConfig } from ${JSON.stringify(scopedOwner)};
232+
const root = ${JSON.stringify(root)};
233+
const aggregate = createProjectShardVitestConfig([]);
234+
const scoped = createScopedVitestConfig([], { env: {}, argv: [] });
235+
const subjectFile = "subject.js";
236+
export default {
237+
root,
238+
test: {
239+
experimental: aggregate.test.experimental,
240+
projects: ["A", "B"].map((name) => ({
241+
extends: false,
242+
root: path.join(root, name),
243+
resolve: { alias: { "fixture-subject": path.join(root, name, subjectFile) } },
244+
plugins: [{
245+
name: "fixture-external-plugin",
246+
transform(code, id) {
247+
if (id !== path.join(root, name, subjectFile).replaceAll("\\\\", "/")) return;
248+
// External plugin generations are outside the config/source graph;
249+
// the paired lock change must invalidate their old transform output.
250+
const version = readFileSync(${JSON.stringify(generation)}, "utf8");
251+
appendFileSync(path.join(root, name, "transforms.txt"), version + "\\n");
252+
return { code: code.replace("__DEPENDENCY_VERSION__", version), map: null };
253+
},
254+
}],
255+
test: {
256+
name,
257+
globals: true,
258+
include: ["fixture.test.js"],
259+
experimental: scoped.test.experimental,
260+
},
261+
})),
262+
},
263+
};
264+
`,
265+
);
266+
const cacheConfig = loadVitestExperimentalConfig({}, "linux", root);
267+
const env = {
268+
OPENCLAW_VITEST_FS_MODULE_CACHE: "1",
269+
OPENCLAW_VITEST_FS_MODULE_CACHE_PATH: cacheConfig.experimental?.fsModuleCachePath,
270+
};
271+
const check = (projects: string[], expected: [number, number]) => {
272+
run(
273+
root,
274+
root,
275+
projects.flatMap((name) => ["--project", name]),
276+
env,
277+
);
278+
const counts = ["A", "B"].map(
279+
(name) =>
280+
fs.readFileSync(path.join(root, name, "transforms.txt"), "utf8").split("\n").length - 1,
281+
);
282+
expect(counts, `subject transforms after selecting ${projects.join("+")}`).toEqual(expected);
283+
};
284+
check(["A", "B"], [1, 1]);
285+
check(["A", "B"], [1, 1]);
286+
transitionLock("2.0.0");
287+
check(["A"], [2, 1]);
288+
check(["A"], [2, 1]);
289+
check(["B"], [2, 2]);
290+
check(["B"], [2, 2]);
291+
292+
// Reuse must still respect ordinary source and config invalidation.
293+
fs.appendFileSync(path.join(root, "A", "subject.js"), "\n// source edit\n");
294+
check(["A", "B"], [3, 2]);
295+
fs.writeFileSync(
296+
configFile,
297+
fs
298+
.readFileSync(configFile, "utf8")
299+
.replace(
300+
'const subjectFile = "subject.js";',
301+
'const subjectFile = "configured-subject.js";',
302+
),
303+
);
304+
check(["A", "B"], [4, 3]);
305+
check(["A", "B"], [4, 3]);
306+
});
175307
});

0 commit comments

Comments
 (0)