Skip to content

Commit b2210c8

Browse files
committed
test(process): make Linux adapter wiring portable
1 parent b5180b6 commit b2210c8

3 files changed

Lines changed: 27 additions & 2 deletions

File tree

‎src/process/supervisor/adapters/child.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { useAutoCleanupTempDirTracker } from "../../../../test/helpers/temp-dir.
1010
import {
1111
expectRealExitWinsOverSigkillFallback,
1212
expectWaitStaysPendingUntilSigkillFallback,
13+
mockLinuxOomWrapperShell,
1314
} from "./test-support.js";
1415

1516
const { spawnWithFallbackMock, signalProcessTreeMock, createWindowsOutputDecoderMock } = vi.hoisted(
@@ -742,6 +743,7 @@ describe("createChildAdapter", () => {
742743
const originalEnv = process.env.ENV;
743744
const originalCdpath = process.env.CDPATH;
744745
setPlatform("linux");
746+
const restoreLinuxShell = mockLinuxOomWrapperShell();
745747
process.env.BASH_ENV = "/tmp/bashenv";
746748
process.env.ENV = "/tmp/env";
747749
process.env.CDPATH = "/tmp";
@@ -752,6 +754,7 @@ describe("createChildAdapter", () => {
752754
});
753755
expect(adapter.oomScoreWrapperSelected).toBe(true);
754756
} finally {
757+
restoreLinuxShell();
755758
if (originalBashEnv === undefined) {
756759
delete process.env.BASH_ENV;
757760
} else {

‎src/process/supervisor/adapters/pty.test.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vite
33
import {
44
expectRealExitWinsOverSigkillFallback,
55
expectWaitStaysPendingUntilSigkillFallback,
6+
mockLinuxOomWrapperShell,
67
} from "./test-support.js";
78

89
const { spawnMock, ptyKillMock, signalProcessTreeMock } = vi.hoisted(() => ({
@@ -157,7 +158,7 @@ describe("createPtyAdapter", () => {
157158

158159
it("forwards non-SIGTERM explicit signals to node-pty kill on non-Windows", async () => {
159160
const originalPlatform = Object.getOwnPropertyDescriptor(process, "platform");
160-
Object.defineProperty(process, "platform", { value: "linux", configurable: true });
161+
Object.defineProperty(process, "platform", { value: "darwin", configurable: true });
161162
try {
162163
spawnMock.mockReturnValue(createStubPty());
163164

@@ -316,17 +317,21 @@ describe("createPtyAdapter", () => {
316317
it("wraps Linux PTY spawns so shell children inherit higher OOM score", async () => {
317318
const originalPlatform = Object.getOwnPropertyDescriptor(process, "platform");
318319
Object.defineProperty(process, "platform", { value: "linux", configurable: true });
320+
const restoreLinuxShell = mockLinuxOomWrapperShell();
321+
vi.resetModules();
319322
try {
323+
const { createPtyAdapter: createLinuxPtyAdapter } = await import("./pty.js");
320324
const stub = createStubPty();
321325
spawnMock.mockReturnValue(stub);
322326

323-
const adapter = await createPtyAdapter({
327+
const adapter = await createLinuxPtyAdapter({
324328
shell: "bash",
325329
args: ["-lc", "env"],
326330
env: { PATH: "/usr/bin", BASH_ENV: "/tmp/bashenv", TERM: "dumb" },
327331
});
328332
expect(adapter.oomScoreWrapperSelected).toBe(true);
329333
} finally {
334+
restoreLinuxShell();
330335
if (originalPlatform) {
331336
Object.defineProperty(process, "platform", originalPlatform);
332337
}

‎src/process/supervisor/adapters/test-support.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Supervisor adapter test support builds mock process handles for adapter tests.
2+
import fs from "node:fs";
23
import { expect, vi } from "vitest";
34

45
/**
@@ -11,6 +12,22 @@ type WaitResult = {
1112
signal: number | NodeJS.Signals | null;
1213
};
1314

15+
/** Keep Linux adapter wiring tests deterministic on hosts without `/bin/sh`. */
16+
export function mockLinuxOomWrapperShell(): () => void {
17+
const statSync = fs.statSync.bind(fs);
18+
const fileStats = statSync(process.execPath);
19+
const statSyncMock = vi
20+
.spyOn(fs, "statSync")
21+
.mockImplementation((target, options) =>
22+
String(target) === "/bin/sh" ? fileStats : statSync(target, options as never),
23+
);
24+
return () => {
25+
statSyncMock.mockRestore();
26+
// The production helper memoizes shell availability; clear it for later tests.
27+
vi.resetModules();
28+
};
29+
}
30+
1431
/** Assert fallback SIGKILL resolves only after the grace timer expires. */
1532
export async function expectWaitStaysPendingUntilSigkillFallback(
1633
waitPromise: Promise<WaitResult>,

0 commit comments

Comments
 (0)