9dd0e14733
Address cubic and oracle review feedback to make the Node/Electron fallback faithful to Bun.spawn behavior so cross-runtime callers behave identically. Changes: - resolveStdio() default stdio is now ["ignore", "pipe", "inherit"] to match Bun.spawn defaults (was ["pipe", "pipe", "pipe"]). Prevents hangs in dispatcher and on-complete-hook callers that did not explicitly set stdin and would otherwise wait forever for input on Node. - spawn-with-windows-hide.ts uses the same defaults so the Windows Node helper aligns with the rest of the shim. - wrapNodeProcess now rejects proc.exited with the original error on "error" events (previously swallowed the error and resolved to exit code 1, hiding ENOENT and friends). - spawnSync result returns the real result.pid instead of -1 and exposes stdout/stderr as Buffer | undefined to match Node's spawnSync result shape when those streams are not piped. Tests cover the new defaults, real pid surfacing, and missing executable error propagation. Refs cubic review and oracle audit on #3798.
92 lines
2.9 KiB
TypeScript
92 lines
2.9 KiB
TypeScript
import { describe, expect, test } from "bun:test"
|
|
|
|
import { spawn, spawnSync } from "./bun-spawn-shim"
|
|
|
|
describe("bun-spawn-shim", () => {
|
|
test("#given array command #when spawn exits successfully #then exited resolves to zero", async () => {
|
|
const proc = spawn(["bun", "--version"], { stdout: "pipe", stderr: "pipe" })
|
|
|
|
const exitCode = await proc.exited
|
|
|
|
expect(exitCode).toBe(0)
|
|
expect(proc.exitCode).toBe(0)
|
|
})
|
|
|
|
test("#given piped stdout #when spawn writes output #then stdout is readable", async () => {
|
|
const proc = spawn(["bun", "--print", "'shim-ok'"], { stdout: "pipe", stderr: "pipe" })
|
|
|
|
const [exitCode, stdout] = await Promise.all([
|
|
proc.exited,
|
|
new Response(proc.stdout).text(),
|
|
])
|
|
|
|
expect(exitCode).toBe(0)
|
|
expect(stdout.trim()).toBe("shim-ok")
|
|
})
|
|
|
|
test("#given detached object command #when spawn starts #then process exposes daemon controls", async () => {
|
|
const proc = spawn({
|
|
cmd: ["bun", "--print", "'detached-ok'"],
|
|
stdout: "pipe",
|
|
stderr: "pipe",
|
|
detached: true,
|
|
})
|
|
|
|
proc.unref()
|
|
const exitCode = await proc.exited
|
|
|
|
expect(exitCode).toBe(0)
|
|
expect(typeof proc.ref).toBe("function")
|
|
expect(typeof proc.unref).toBe("function")
|
|
expect(proc.pid).toBeGreaterThan(0)
|
|
})
|
|
|
|
test("#given stdio tuple #when spawn runs #then ignored streams are still safe to read", async () => {
|
|
const proc = spawn({
|
|
cmd: ["bun", "--print", "'ignored'"],
|
|
stdio: ["ignore", "ignore", "ignore"],
|
|
})
|
|
|
|
const exitCode = await proc.exited
|
|
const stdout = await new Response(proc.stdout).text()
|
|
|
|
expect(exitCode).toBe(0)
|
|
expect(stdout).toBe("")
|
|
})
|
|
|
|
test("#given spawnSync command #when it writes output #then stdout and exit code match", () => {
|
|
const result = spawnSync(["bun", "--print", "'sync-ok'"], { stdout: "pipe", stderr: "pipe" })
|
|
|
|
expect(result.exitCode).toBe(0)
|
|
expect(result.success).toBe(true)
|
|
expect(result.stdout).toBeDefined()
|
|
expect(Buffer.from(result.stdout!).toString().trim()).toBe("sync-ok")
|
|
})
|
|
|
|
test("#given spawnSync command #when it completes #then result.pid is a positive number", () => {
|
|
const result = spawnSync(["bun", "--version"], { stdout: "pipe", stderr: "pipe" })
|
|
|
|
expect(result.pid).toBeGreaterThan(0)
|
|
})
|
|
|
|
test("#given default stdio #when child reads stdin #then it does not hang waiting for input", async () => {
|
|
const proc = spawn(["cat"], { stdout: "pipe", stderr: "pipe" })
|
|
|
|
const exitCode = await proc.exited
|
|
|
|
expect(exitCode).toBe(0)
|
|
})
|
|
|
|
test("#given missing executable #when spawn invoked #then the error is surfaced to the caller", async () => {
|
|
let observedError: unknown
|
|
try {
|
|
const proc = spawn(["__omo-shim-missing-binary__"], { stdout: "pipe", stderr: "pipe" })
|
|
await proc.exited
|
|
} catch (error) {
|
|
observedError = error
|
|
}
|
|
|
|
expect(observedError).toBeDefined()
|
|
})
|
|
})
|