Skip to content

Commit 7fd3201

Browse files
cliffhallclaude
andcommitted
fix: no lifecycle hooks around the gate; workspaces drift; POSIX-only signal tests
Copilot round 7 on #4920: - The script tests assert that no pre/post npm hook exists for local:gate or local:gate:stages. - verify:install-fresh compares the root's `workspaces` patterns with the lockfile's record of them. - The lease's signal tests are skipped on Windows, which cannot send those signals. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 6010ef0 commit 7fd3201

4 files changed

Lines changed: 106 additions & 53 deletions

File tree

‎scripts/gate-lease.test.mjs‎

Lines changed: 61 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -663,57 +663,65 @@ for (const [signal, code] of [
663663
["SIGTERM", 143],
664664
["SIGQUIT", 131],
665665
]) {
666-
test(`${signal} to the wrapper stops the whole tree, releases the lease and exits ${code}`, async () => {
667-
const dir = freshDir();
668-
const pidFile = join(dir, "grandchild.pid");
669-
// The gate stand-in: starts a descendant of its own (as `npm run` does,
670-
// four levels deep), records both pids, then idles forever unless
671-
// signalled. The descendant is what proves the whole *tree* is stopped —
672-
// a regression to `child.kill()` or a non-detached spawn would leave it
673-
// running and fail below.
674-
const idle =
675-
`const d = require("child_process").spawn(process.execPath, ["-e", "setInterval(() => {}, 1000)"], { stdio: "ignore" });` +
676-
`require("fs").writeFileSync(${JSON.stringify(pidFile)}, process.pid + " " + d.pid);` +
677-
`setInterval(() => {}, 1000)`;
678-
const wrapper = spawn(
679-
process.execPath,
680-
[SCRIPT, process.execPath, "-e", idle],
681-
{
682-
env: { ...process.env, [DIR_ENV]: dir },
683-
stdio: ["ignore", "ignore", "pipe"],
684-
},
685-
);
686-
let stderr = "";
687-
wrapper.stderr.on("data", (chunk) => (stderr += chunk));
688-
const exited = new Promise((resolve) =>
689-
wrapper.once("exit", (code, signal) => resolve({ code, signal })),
690-
);
691-
692-
let tree;
693-
try {
694-
await waitFor(() => existsSync(pidFile));
695-
tree = readFileSync(pidFile, "utf8").split(" ").map(Number);
696-
assert.equal(tree.length, 2);
697-
for (const pid of tree) assert.ok(isAlive(pid), `pid ${pid} started`);
698-
assert.ok(existsSync(lockPathOf(dir)), "the wrapper held the lease");
699-
} finally {
700-
// On every path — a failed setup assertion included — the wrapper is
701-
// told to stop, so an idle grandchild can never outlive the suite.
702-
wrapper.kill(signal);
703-
}
704-
const outcome = await exited;
705-
assert.deepEqual(outcome, { code, signal: null }, stderr);
706-
for (const pid of tree)
707-
await waitFor(() => !isAlive(pid)).catch(() => {
708-
process.kill(pid, "SIGKILL");
709-
assert.fail(
710-
`pid ${pid} outlived the wrapper — the tree was not stopped`,
711-
);
712-
});
713-
assert.ok(
714-
!existsSync(lockPathOf(dir)),
715-
"the lease was released on the way out",
716-
);
717-
assert.match(stderr, new RegExp(`received ${signal}; stopping the gate`));
718-
});
666+
// POSIX only: Windows has no such signals to send (`kill("SIGQUIT")` throws
667+
// there) and no process groups; its tree is ended with `taskkill` instead,
668+
// which nothing here can exercise without a Windows runner.
669+
const posixOnly = { skip: process.platform === "win32" };
670+
test(
671+
`${signal} to the wrapper stops the whole tree, releases the lease and exits ${code}`,
672+
posixOnly,
673+
async () => {
674+
const dir = freshDir();
675+
const pidFile = join(dir, "grandchild.pid");
676+
// The gate stand-in: starts a descendant of its own (as `npm run` does,
677+
// four levels deep), records both pids, then idles forever unless
678+
// signalled. The descendant is what proves the whole *tree* is stopped —
679+
// a regression to `child.kill()` or a non-detached spawn would leave it
680+
// running and fail below.
681+
const idle =
682+
`const d = require("child_process").spawn(process.execPath, ["-e", "setInterval(() => {}, 1000)"], { stdio: "ignore" });` +
683+
`require("fs").writeFileSync(${JSON.stringify(pidFile)}, process.pid + " " + d.pid);` +
684+
`setInterval(() => {}, 1000)`;
685+
const wrapper = spawn(
686+
process.execPath,
687+
[SCRIPT, process.execPath, "-e", idle],
688+
{
689+
env: { ...process.env, [DIR_ENV]: dir },
690+
stdio: ["ignore", "ignore", "pipe"],
691+
},
692+
);
693+
let stderr = "";
694+
wrapper.stderr.on("data", (chunk) => (stderr += chunk));
695+
const exited = new Promise((resolve) =>
696+
wrapper.once("exit", (code, signal) => resolve({ code, signal })),
697+
);
698+
699+
let tree;
700+
try {
701+
await waitFor(() => existsSync(pidFile));
702+
tree = readFileSync(pidFile, "utf8").split(" ").map(Number);
703+
assert.equal(tree.length, 2);
704+
for (const pid of tree) assert.ok(isAlive(pid), `pid ${pid} started`);
705+
assert.ok(existsSync(lockPathOf(dir)), "the wrapper held the lease");
706+
} finally {
707+
// On every path — a failed setup assertion included — the wrapper is
708+
// told to stop, so an idle grandchild can never outlive the suite.
709+
wrapper.kill(signal);
710+
}
711+
const outcome = await exited;
712+
assert.deepEqual(outcome, { code, signal: null }, stderr);
713+
for (const pid of tree)
714+
await waitFor(() => !isAlive(pid)).catch(() => {
715+
process.kill(pid, "SIGKILL");
716+
assert.fail(
717+
`pid ${pid} outlived the wrapper — the tree was not stopped`,
718+
);
719+
});
720+
assert.ok(
721+
!existsSync(lockPathOf(dir)),
722+
"the lease was released on the way out",
723+
);
724+
assert.match(stderr, new RegExp(`received ${signal}; stopping the gate`));
725+
},
726+
);
719727
}

‎scripts/lib/workflow-gate.test.mjs‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,19 @@ describe("the gate's name", () => {
357357
);
358358
});
359359

360+
it("has no npm lifecycle hooks around it", () => {
361+
// npm runs `pre<name>` and `post<name>` implicitly. A hook on `local:gate`
362+
// would run outside the lease, and one on `local:gate:stages` would be a
363+
// stage that the stage list does not show.
364+
for (const hook of [
365+
"prelocal:gate",
366+
"postlocal:gate",
367+
"prelocal:gate:stages",
368+
"postlocal:gate:stages",
369+
])
370+
assert.equal(scripts[hook], undefined, `${hook} must not exist`);
371+
});
372+
360373
it("checks the install before anything is tested against it", () => {
361374
assert.match(
362375
scripts["local:gate:stages"],

‎scripts/verify-install-fresh.mjs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,18 @@ export function compareManifests(lock, readManifest) {
123123
if (isInstalledCopy(dir) || entry?.link) continue;
124124
const manifest = readManifest(dir);
125125
if (manifest === undefined) continue;
126+
// The root's `workspaces` patterns are mirrored too: narrowing or removing
127+
// one without reinstalling leaves the old workspace links in place.
128+
const declaredWs = JSON.stringify(manifest.workspaces ?? null);
129+
const lockedWs = JSON.stringify(entry.workspaces ?? null);
130+
if (declaredWs !== lockedWs)
131+
drift.push({
132+
dir,
133+
section: "workspaces",
134+
name: "patterns",
135+
manifest: declaredWs,
136+
lock: lockedWs,
137+
});
126138
for (const section of MANIFEST_SECTIONS) {
127139
const declared = manifest[section] ?? {};
128140
const locked = entry[section] ?? {};

‎scripts/verify-install-fresh.test.mjs‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,26 @@ test("compareManifests: an added, removed or re-ranged dependency is drift", ()
159159
);
160160
});
161161

162+
test("compareManifests: a changed workspaces declaration is drift", () => {
163+
const lock = { packages: { "": { workspaces: ["src/*"] } } };
164+
assert.deepEqual(
165+
compareManifests(lock, () => ({ workspaces: ["src/*"] })),
166+
[],
167+
);
168+
assert.deepEqual(
169+
compareManifests(lock, () => ({ workspaces: ["src/everything"] })),
170+
[
171+
{
172+
dir: "",
173+
section: "workspaces",
174+
name: "patterns",
175+
manifest: '["src/everything"]',
176+
lock: '["src/*"]',
177+
},
178+
],
179+
);
180+
});
181+
162182
test("main fails when a manifest declares what the lockfile does not", (t) => {
163183
const { root, write } = fixture(t);
164184
t.mock.method(console, "log", () => {});

0 commit comments

Comments
 (0)