|
| 1 | +// @effect-diagnostics-next-line nodeBuiltinImport:off -- Exercise synchronous spawn failures at the native probe boundary. |
| 2 | +import * as NodeChildProcess from "node:child_process"; |
1 | 3 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
2 | 4 | import { it as effectIt } from "@effect/vitest"; |
3 | 5 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
@@ -37,6 +39,11 @@ import { |
37 | 39 | type WindowsShellEnvironmentReader, |
38 | 40 | } from "./shell.ts"; |
39 | 41 |
|
| 42 | +vi.mock("node:child_process", async (importOriginal) => { |
| 43 | + const original = await importOriginal<typeof NodeChildProcess>(); |
| 44 | + return { ...original, execFile: vi.fn(original.execFile) }; |
| 45 | +}); |
| 46 | + |
40 | 47 | const withWindowsEnvironmentMocks = <A, E, R>( |
41 | 48 | effect: Effect.Effect<A, E, R>, |
42 | 49 | readEnvironment: ( |
@@ -246,6 +253,33 @@ describe("readEnvironmentFromWindowsShell", () => { |
246 | 253 | }), |
247 | 254 | ); |
248 | 255 |
|
| 256 | + effectIt.layer(NodeServices.layer)("synchronous spawn failures", (it) => { |
| 257 | + it.effect("tries both shells and preserves baseline PATH repair", () => |
| 258 | + Effect.gen(function* () { |
| 259 | + const execFile = vi.mocked(NodeChildProcess.execFile); |
| 260 | + execFile.mockImplementationOnce(() => { |
| 261 | + throw new Error("spawn EPERM"); |
| 262 | + }); |
| 263 | + execFile.mockImplementationOnce(() => { |
| 264 | + throw new Error("spawn EINVAL"); |
| 265 | + }); |
| 266 | + try { |
| 267 | + const patch = yield* resolveWindowsEnvironment({ |
| 268 | + PATH: "C:\\Windows", |
| 269 | + APPDATA: "C:\\Roaming", |
| 270 | + }).pipe(Effect.provideService(CommandAvailability, () => Effect.succeed(true))); |
| 271 | + expect(patch).toEqual({ PATH: "C:\\Windows;C:\\Roaming\\npm" }); |
| 272 | + expect(execFile.mock.calls.slice(-2).map(([file]) => file)).toEqual([ |
| 273 | + "pwsh.exe", |
| 274 | + "powershell.exe", |
| 275 | + ]); |
| 276 | + } finally { |
| 277 | + execFile.mockReset(); |
| 278 | + } |
| 279 | + }), |
| 280 | + ); |
| 281 | + }); |
| 282 | + |
249 | 283 | effectIt.effect("returns an empty environment after both shell candidates fail", () => |
250 | 284 | Effect.gen(function* () { |
251 | 285 | const execFile = vi.fn(() => Effect.succeed(Option.none<string>())); |
|
0 commit comments