|
| 1 | +/** |
| 2 | + * Node.js Polyfill Tests — Bun.spawn |
| 3 | + * |
| 4 | + * Tests the spawn logic used by the Node.js polyfill in script/node-polyfills.ts. |
| 5 | + * |
| 6 | + * We can't import the polyfill directly (it overwrites globalThis.Bun and has |
| 7 | + * side effects), so we reproduce the exact spawn implementation and verify its |
| 8 | + * contract: exited promise, stdin piping, env passthrough, and inherit stdio. |
| 9 | + * |
| 10 | + * Fixes CLI-68: the original polyfill returned no `exited` property, causing |
| 11 | + * `await proc.exited` to resolve to `undefined` and the upgrade command to |
| 12 | + * throw "Setup failed with exit code undefined". |
| 13 | + */ |
| 14 | + |
| 15 | +import { describe, expect, test } from "bun:test"; |
| 16 | +import { spawn as nodeSpawn } from "node:child_process"; |
| 17 | + |
| 18 | +/** |
| 19 | + * Reproduces the exact spawn logic from script/node-polyfills.ts. |
| 20 | + * Kept in sync manually — if the polyfill changes, update this too. |
| 21 | + */ |
| 22 | +function polyfillSpawn( |
| 23 | + cmd: string[], |
| 24 | + opts?: { |
| 25 | + stdin?: "pipe" | "ignore" | "inherit"; |
| 26 | + stdout?: "pipe" | "ignore" | "inherit"; |
| 27 | + stderr?: "pipe" | "ignore" | "inherit"; |
| 28 | + env?: Record<string, string | undefined>; |
| 29 | + } |
| 30 | +) { |
| 31 | + const [command, ...args] = cmd; |
| 32 | + const proc = nodeSpawn(command, args, { |
| 33 | + stdio: [ |
| 34 | + opts?.stdin ?? "ignore", |
| 35 | + opts?.stdout ?? "ignore", |
| 36 | + opts?.stderr ?? "ignore", |
| 37 | + ], |
| 38 | + env: opts?.env, |
| 39 | + }); |
| 40 | + |
| 41 | + const exited = new Promise<number>((resolve) => { |
| 42 | + proc.on("close", (code) => resolve(code ?? 1)); |
| 43 | + proc.on("error", () => resolve(1)); |
| 44 | + }); |
| 45 | + |
| 46 | + return { |
| 47 | + stdin: proc.stdin, |
| 48 | + exited, |
| 49 | + unref() { |
| 50 | + proc.unref(); |
| 51 | + }, |
| 52 | + }; |
| 53 | +} |
| 54 | + |
| 55 | +describe("spawn polyfill", () => { |
| 56 | + test("exited resolves with exit code 0 for successful command", async () => { |
| 57 | + const proc = polyfillSpawn(["node", "-e", "process.exit(0)"]); |
| 58 | + const exitCode = await proc.exited; |
| 59 | + expect(exitCode).toBe(0); |
| 60 | + }); |
| 61 | + |
| 62 | + test("exited resolves with non-zero exit code for failed command", async () => { |
| 63 | + const proc = polyfillSpawn(["node", "-e", "process.exit(42)"]); |
| 64 | + const exitCode = await proc.exited; |
| 65 | + expect(exitCode).toBe(42); |
| 66 | + }); |
| 67 | + |
| 68 | + test("stdin is writable when stdin: pipe", async () => { |
| 69 | + // Pipe text through cat, verify it exits cleanly |
| 70 | + const proc = polyfillSpawn( |
| 71 | + [ |
| 72 | + "node", |
| 73 | + "-e", |
| 74 | + "process.stdin.resume(); process.stdin.on('end', () => process.exit(0));", |
| 75 | + ], |
| 76 | + { |
| 77 | + stdin: "pipe", |
| 78 | + } |
| 79 | + ); |
| 80 | + |
| 81 | + proc.stdin!.write("hello"); |
| 82 | + proc.stdin!.end(); |
| 83 | + |
| 84 | + const exitCode = await proc.exited; |
| 85 | + expect(exitCode).toBe(0); |
| 86 | + }); |
| 87 | + |
| 88 | + test("env is passed to child process", async () => { |
| 89 | + const proc = polyfillSpawn( |
| 90 | + [ |
| 91 | + "node", |
| 92 | + "-e", |
| 93 | + "process.exit(process.env.POLYFILL_TEST === 'works' ? 0 : 1)", |
| 94 | + ], |
| 95 | + { |
| 96 | + env: { ...process.env, POLYFILL_TEST: "works" }, |
| 97 | + } |
| 98 | + ); |
| 99 | + |
| 100 | + const exitCode = await proc.exited; |
| 101 | + expect(exitCode).toBe(0); |
| 102 | + }); |
| 103 | + |
| 104 | + test("inherit stdio does not throw", async () => { |
| 105 | + const proc = polyfillSpawn(["node", "-e", "process.exit(0)"], { |
| 106 | + stdout: "inherit", |
| 107 | + stderr: "inherit", |
| 108 | + }); |
| 109 | + |
| 110 | + const exitCode = await proc.exited; |
| 111 | + expect(exitCode).toBe(0); |
| 112 | + }); |
| 113 | + |
| 114 | + test("exited resolves to 1 for non-existent command", async () => { |
| 115 | + const proc = polyfillSpawn(["__nonexistent_command_polyfill_test__"]); |
| 116 | + const exitCode = await proc.exited; |
| 117 | + // error event fires → resolves to 1 |
| 118 | + expect(exitCode).toBe(1); |
| 119 | + }); |
| 120 | + |
| 121 | + test("unref is callable", () => { |
| 122 | + const proc = polyfillSpawn(["node", "-e", "setTimeout(() => {}, 5000)"], { |
| 123 | + stdout: "ignore", |
| 124 | + stderr: "ignore", |
| 125 | + }); |
| 126 | + |
| 127 | + // Should not throw |
| 128 | + expect(() => proc.unref()).not.toThrow(); |
| 129 | + }); |
| 130 | +}); |
0 commit comments