fix: set localstorage file for test planner workers

This commit is contained in:
Peter Steinberger
2026-03-28 12:45:19 +00:00
parent 41cf93efff
commit 5df53a99b1
2 changed files with 91 additions and 0 deletions

View File

@@ -169,6 +169,19 @@ export function createExecutionArtifacts(env = process.env) {
const ensureNodeOptionFlag = (nodeOptions, flagPrefix, nextValue) =>
nodeOptions.includes(flagPrefix) ? nodeOptions : `${nodeOptions} ${nextValue}`.trim();
const ensureNodeOptionFilePathFlag = (nodeOptions, flag, filePath) => {
const normalized = nodeOptions.trim();
const emptyAssignmentPattern = new RegExp(`(^|\\s)${flag}=(?=\\s|$)`, "u");
if (emptyAssignmentPattern.test(normalized)) {
return normalized.replace(emptyAssignmentPattern, `$1${flag}=${filePath}`);
}
const bareFlagPattern = new RegExp(`(^|\\s)${flag}(?=\\s|$)`, "u");
if (bareFlagPattern.test(normalized)) {
return normalized.replace(bareFlagPattern, `$1${flag}=${filePath}`);
}
return ensureNodeOptionFlag(normalized, `${flag}=`, `${flag}=${filePath}`);
};
const isNodeLikeProcess = (command) => /(?:^|\/)node(?:$|\.exe$)/iu.test(command);
const getShardLabel = (args) => {
@@ -429,6 +442,15 @@ export async function executePlan(plan, options = {}) {
maxOldSpaceSizeMb && !nextNodeOptions.includes("--max-old-space-size=")
? `${nextNodeOptions} --max-old-space-size=${maxOldSpaceSizeMb}`.trim()
: nextNodeOptions;
const localStorageFilePath = path.join(
artifacts.ensureTempArtifactDir(),
`${artifactStem}.localstorage.json`,
);
resolvedNodeOptions = ensureNodeOptionFilePathFlag(
resolvedNodeOptions,
"--localstorage-file",
localStorageFilePath,
);
if (heapSnapshotEnabled && heapSnapshotDir) {
try {
fs.mkdirSync(heapSnapshotDir, { recursive: true });

View File

@@ -139,4 +139,73 @@ describe("test planner executor", () => {
artifacts.cleanupTempArtifacts();
});
it("injects a valid localstorage file path into child NODE_OPTIONS", async () => {
vi.useRealTimers();
const stdout = new PassThrough();
const stderr = new PassThrough();
const fakeChild = Object.assign(new EventEmitter(), {
stdout,
stderr,
pid: 123,
kill: vi.fn(),
});
let capturedEnv;
const spawnMock = vi.fn((_command, _args, options) => {
capturedEnv = options?.env;
setTimeout(() => {
fakeChild.emit("exit", 0, null);
fakeChild.emit("close", 0, null);
}, 0);
return fakeChild;
});
vi.doMock("node:child_process", () => ({
spawn: spawnMock,
}));
const { executePlan, createExecutionArtifacts } = await importFreshModule<
typeof import("../../scripts/test-planner/executor.mjs")
>(import.meta.url, "../../scripts/test-planner/executor.mjs?scope=localstorage-file");
const artifacts = createExecutionArtifacts({
NODE_OPTIONS: "--max_old_space_size=4096 --localstorage-file",
});
await expect(
executePlan(
{
failurePolicy: "fail-fast",
passthroughMetadataOnly: false,
passthroughOptionArgs: [],
targetedUnits: [],
parallelUnits: [{ id: "unit-a", args: ["vitest", "run", "src/alpha.test.ts"] }],
serialUnits: [],
serialPrefixUnits: [],
shardCount: 1,
shardIndexOverride: null,
topLevelSingleShardAssignments: new Map(),
runtimeCapabilities: { isWindowsCi: false, isCI: false, isWindows: false },
topLevelParallelEnabled: false,
topLevelParallelLimit: 1,
deferredRunConcurrency: 1,
passthroughRequiresSingleRun: false,
},
{
env: {
NODE_OPTIONS: "--max_old_space_size=4096 --localstorage-file",
},
artifacts,
},
),
).resolves.toMatchObject({
exitCode: 0,
});
expect(spawnMock).toHaveBeenCalledTimes(1);
expect(capturedEnv?.NODE_OPTIONS).toContain("--max_old_space_size=4096");
expect(capturedEnv?.NODE_OPTIONS).toMatch(
/--localstorage-file=[^\s]+\.localstorage\.json(?:\s|$)/u,
);
expect(capturedEnv?.NODE_OPTIONS).not.toMatch(/(^|\s)--localstorage-file(?=\s|$)/u);
artifacts.cleanupTempArtifacts();
});
});