Skip to content

Commit cd9cbf9

Browse files
committed
test: preserve pnpm bootstrap in report fixtures
1 parent 1fc1f92 commit cd9cbf9

2 files changed

Lines changed: 150 additions & 3 deletions

File tree

‎test/scripts/vitest-report-fixture.ts‎

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import assert from "node:assert/strict";
12
import fs from "node:fs";
23
import path from "node:path";
34
import { spawnOwnedVitestProcess } from "../../scripts/lib/vitest-process.mts";
5+
import { createPnpmRunnerSpawnSpec } from "../../scripts/pnpm-runner.mts";
46
import { waitForFile } from "../helpers/process-wait.js";
57

68
const repoRoot = path.resolve(import.meta.dirname, "../..");
@@ -90,6 +92,64 @@ export function createVitestReportFixture(root: string, evidence = path.join(roo
9092
mode: ReportFixtureMode,
9193
options: { nativeArgs?: string[]; entry?: "projects" | "batch-cli"; report?: boolean } = {},
9294
) => {
95+
// Bootstrap and native execution share the fixture's existing timeout budget.
96+
const deadline = performance.now() + 45000;
97+
if (mode.startsWith("batch") && options.entry !== "batch-cli" && !env.npm_execpath) {
98+
// Corepack needs the parent's prepared resources before HOME isolation. Ask
99+
// pnpm for its executable; carry that fact, never the parent's writable cache.
100+
const bootstrap = path.join(root, "pnpm-bootstrap");
101+
write(
102+
path.join(bootstrap, "package.json"),
103+
JSON.stringify({
104+
private: true,
105+
scripts: { "pnpm-path": "node -p process.env.npm_execpath" },
106+
}),
107+
);
108+
const spec = createPnpmRunnerSpawnSpec({
109+
cwd: repoRoot,
110+
env: { ...process.env, COREPACK_ENABLE_NETWORK: "0" },
111+
// Preserve pnpm-workspace.yaml's no-reconciliation policy in the owned package.
112+
pnpmArgs: [
113+
"--dir",
114+
bootstrap,
115+
"--config.verify-deps-before-run=false",
116+
"--silent",
117+
"run",
118+
"pnpm-path",
119+
],
120+
stdio: ["ignore", "pipe", "pipe"],
121+
});
122+
const { child, completion } = spawnOwnedVitestProcess(spec);
123+
let stdout = "",
124+
stderr = "";
125+
child.stdout!.on("data", (chunk) => {
126+
stdout += chunk;
127+
});
128+
child.stderr!.on("data", (chunk) => {
129+
stderr += chunk;
130+
});
131+
const timeout = setTimeout(
132+
() => child.kill("SIGTERM"),
133+
Math.max(0, deadline - performance.now()),
134+
);
135+
try {
136+
const result = await completion;
137+
write(
138+
path.join(evidence, "bootstrap.json"),
139+
JSON.stringify(
140+
{ command: [spec.command, ...spec.args], ...result, stdout, stderr, joined: true },
141+
null,
142+
2,
143+
),
144+
);
145+
assert.equal(result.code, 0, stderr);
146+
assert.equal(result.signal, null, stderr);
147+
env.npm_execpath = stdout.trim();
148+
assert(path.isAbsolute(env.npm_execpath), "pnpm did not expose its executable path");
149+
} finally {
150+
clearTimeout(timeout);
151+
}
152+
}
93153
const output = path.join(evidence, "result.json");
94154
const ready = path.join(root, "ready");
95155
const done = path.join(root, "beta.done");
@@ -233,7 +293,10 @@ ${index === 0 ? "test('alpha/two',()=>expect(2).toBe(2));" : "test.skip('beta/sk
233293
child.stderr!.on("data", (chunk) => {
234294
stderr += chunk;
235295
});
236-
const timeout = setTimeout(() => child.kill("SIGTERM"), 45000);
296+
const timeout = setTimeout(
297+
() => child.kill("SIGTERM"),
298+
Math.max(0, deadline - performance.now()),
299+
);
237300
try {
238301
if (["cancel", "batch-cancel"].includes(mode)) {
239302
await waitForFile(ready, 15000);

‎test/scripts/vitest-report-owner.test.ts‎

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,12 @@ describe.skipIf(process.platform === "win32")("native multi-invocation report ow
8282
const index = json(path.join(result.reportSet!, "index.json"));
8383
expect(index.complete).toBe(true);
8484
expect(index.entries[1].attempts[0].outcome.code).toBe(1);
85-
if (mode === "unhandled") expect(json(index.entries[1].attempts[0].json).success).toBe(true);
85+
if (mode === "unhandled") {
86+
const part = index.entries[1].attempts[0].json;
87+
expect(json(part).success).toBe(true);
88+
expect(json(`${part}.capture.json`).ended.unhandledErrors).toBe(1);
89+
expect(result.stderr).toContain("owned unhandled rejection");
90+
}
8691
},
8792
);
8893

@@ -108,12 +113,91 @@ describe.skipIf(process.platform === "win32")("native multi-invocation report ow
108113
const result = await run(mode);
109114
expect(result.code !== 0 || result.signal !== null, result.stderr).toBe(true);
110115
const index = json(path.join(result.reportSet!, "index.json"));
116+
const first = index.entries[0].attempts[0];
117+
const staged = path.join(result.reportSet!, "aggregate.json");
111118
if (mode === "cancel" || mode === "batch-cancel") {
112119
expect(result.signal).toBe("SIGTERM");
113-
expect(index.entries[0].attempts[0].outcome.signal).toBe("SIGTERM");
120+
expect(first.outcome.signal).toBe("SIGTERM");
121+
const events = fs
122+
.readFileSync(path.join(path.dirname(result.output), "executed.jsonl"), "utf8")
123+
.trim()
124+
.split("\n")
125+
.map((line) => JSON.parse(line));
126+
expect(events.map((event) => event.name)).toEqual(["alpha/one"]);
127+
const capture = json(`${first.json}.capture.json`);
128+
expect(fs.readFileSync(path.join(capture.root, "ready"), "utf8")).toBe(
129+
String(events[0].pid),
130+
);
131+
expect(() => process.kill(events[0].pid, 0)).toThrow(
132+
expect.objectContaining({ code: "ESRCH" }),
133+
);
134+
} else if (mode === "fail-fast" || mode === "batch-fail-fast") {
135+
const report = json(first.json);
136+
expect(inventory(report)).toEqual([
137+
["alpha/one", "failed"],
138+
["alpha/two", "passed"],
139+
]);
140+
expect(report.testResults[0].assertionResults[0].failureMessages.join("\n")).toContain(
141+
"expected 1 to be 2",
142+
);
143+
expect(first.outcome.code).toBe(1);
144+
expect(json(`${first.json}.capture.json`).ended.reason).toBe("failed");
145+
} else if (mode === "tuple") {
146+
expect(result.stderr).toContain("cannot preserve config-owned reporter options");
147+
} else {
148+
const capture = json(`${first.json}.capture.json`);
149+
expect(capture.ended.reason).toBe("passed");
150+
if (mode === "child-write") {
151+
expect(fs.statSync(first.json).isDirectory()).toBe(true);
152+
expect(first.outcome.code).toBe(1);
153+
expect(result.stderr).toContain("EISDIR");
154+
} else {
155+
const original = mode === "missing" ? `${first.json}.native-original` : first.json;
156+
expect(inventory(json(original))).toEqual(expected.slice(0, 2));
157+
expect(first.outcome.code).toBe(0);
158+
}
159+
if (mode === "missing") {
160+
expect(fs.existsSync(first.json)).toBe(false);
161+
expect(index.error).toContain("missing Vitest JSON report");
162+
} else if (mode === "coverage-missing") {
163+
const lcov = path.join(capture.coverageDirectory, "lcov.info");
164+
expect(fs.readFileSync(`${lcov}.native-original`, "utf8")).toContain("covered.ts");
165+
expect(fs.existsSync(lcov)).toBe(false);
166+
expect(index.error).toContain(lcov);
167+
} else if (mode === "teardown-timeout") {
168+
expect(capture.processTimedOut).toBe(true);
169+
expect(index.error).toContain("completion evidence missing or interrupted");
170+
} else if (mode !== "child-write") {
171+
const second = index.entries[1].attempts[0];
172+
expect(inventory(json(second.json))).toEqual(expected.slice(2));
173+
expect(second.outcome.code).toBe(0);
174+
expect(index.merge.code).toBe(mode === "publish-write" ? 0 : 1);
175+
if (mode === "corrupt") {
176+
expect(fs.readFileSync(first.blob, "utf8")).toBe("owned corruption");
177+
expect(() => json(`${first.blob}.native-original`)).not.toThrow();
178+
expect(result.stderr).toContain('"owned corruption" is not valid JSON');
179+
} else if (mode === "merge-failure") {
180+
expect(result.stderr).toContain("owned native merge failure");
181+
} else if (mode === "identity") {
182+
expect(result.stderr).toContain("Native merge project identity changed");
183+
} else if (mode === "final-write") {
184+
expect(fs.statSync(staged).isDirectory()).toBe(true);
185+
expect(index.error).toContain("EISDIR");
186+
} else if (mode === "publish-write") {
187+
expect(inventory(json(staged))).toEqual(expected);
188+
expect(fs.readFileSync(path.join(result.output, "old"), "utf8")).toBe("old");
189+
expect(fs.readFileSync(path.join(index.publication, "report.json"))).toEqual(
190+
fs.readFileSync(staged),
191+
);
192+
expect(index.error).toMatch(/rename/);
193+
}
194+
}
114195
}
115196
expect(index.complete).toBe(false);
116197
expect(index.error).not.toBe("");
198+
expect(index.aggregate).toBe("");
199+
if (!["corrupt", "merge-failure", "identity", "final-write", "publish-write"].includes(mode))
200+
expect(index.merge).toBeNull();
117201
if (["missing", "corrupt", "merge-failure", "final-write", "identity"].includes(mode))
118202
expect(fs.readFileSync(result.output, "utf8")).toBe("old report");
119203
if (["fail-fast", "cancel", "batch-fail-fast", "batch-cancel"].includes(mode))

0 commit comments

Comments
 (0)