process.test.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { describe, expect, test } from "bun:test"
  2. import fs from "fs/promises"
  3. import path from "path"
  4. import { Process } from "../../src/util/process"
  5. import { tmpdir } from "../fixture/fixture"
  6. function node(script: string) {
  7. return [process.execPath, "-e", script]
  8. }
  9. describe("util.process", () => {
  10. test("captures stdout and stderr", async () => {
  11. const out = await Process.run(node('process.stdout.write("out");process.stderr.write("err")'))
  12. expect(out.code).toBe(0)
  13. expect(out.stdout.toString()).toBe("out")
  14. expect(out.stderr.toString()).toBe("err")
  15. })
  16. test("returns code when nothrow is enabled", async () => {
  17. const out = await Process.run(node("process.exit(7)"), { nothrow: true })
  18. expect(out.code).toBe(7)
  19. })
  20. test("throws RunFailedError on non-zero exit", async () => {
  21. const err = await Process.run(node('process.stderr.write("bad");process.exit(3)')).catch((error) => error)
  22. expect(err).toBeInstanceOf(Process.RunFailedError)
  23. if (!(err instanceof Process.RunFailedError)) throw err
  24. expect(err.code).toBe(3)
  25. expect(err.stderr.toString()).toBe("bad")
  26. })
  27. test("aborts a running process", async () => {
  28. const abort = new AbortController()
  29. const started = Date.now()
  30. setTimeout(() => abort.abort(), 25)
  31. const out = await Process.run(node("setInterval(() => {}, 1000)"), {
  32. abort: abort.signal,
  33. nothrow: true,
  34. })
  35. expect(out.code).not.toBe(0)
  36. expect(Date.now() - started).toBeLessThan(1000)
  37. }, 3000)
  38. test("kills after timeout when process ignores terminate signal", async () => {
  39. if (process.platform === "win32") return
  40. const abort = new AbortController()
  41. const started = Date.now()
  42. setTimeout(() => abort.abort(), 25)
  43. const out = await Process.run(node('process.on("SIGTERM", () => {}); setInterval(() => {}, 1000)'), {
  44. abort: abort.signal,
  45. nothrow: true,
  46. timeout: 25,
  47. })
  48. expect(out.code).not.toBe(0)
  49. expect(Date.now() - started).toBeLessThan(1000)
  50. }, 3000)
  51. test("uses cwd when spawning commands", async () => {
  52. await using tmp = await tmpdir()
  53. const out = await Process.run(node("process.stdout.write(process.cwd())"), {
  54. cwd: tmp.path,
  55. })
  56. expect(out.stdout.toString()).toBe(tmp.path)
  57. })
  58. test("merges environment overrides", async () => {
  59. const out = await Process.run(node('process.stdout.write(process.env.KILO_TEST ?? "")'), {
  60. env: {
  61. KILO_TEST: "set",
  62. },
  63. })
  64. expect(out.stdout.toString()).toBe("set")
  65. })
  66. test("uses shell in run on Windows", async () => {
  67. if (process.platform !== "win32") return
  68. const out = await Process.run(["set", "KILO_TEST_SHELL"], {
  69. shell: true,
  70. env: {
  71. KILO_TEST_SHELL: "ok",
  72. },
  73. })
  74. expect(out.code).toBe(0)
  75. expect(out.stdout.toString()).toContain("KILO_TEST_SHELL=ok")
  76. })
  77. test("runs cmd scripts with spaces on Windows without shell", async () => {
  78. if (process.platform !== "win32") return
  79. await using tmp = await tmpdir()
  80. const dir = path.join(tmp.path, "with space")
  81. const file = path.join(dir, "echo cmd.cmd")
  82. await fs.mkdir(dir, { recursive: true })
  83. await Bun.write(file, "@echo off\r\nif %~1==--stdio exit /b 0\r\nexit /b 7\r\n")
  84. const proc = Process.spawn([file, "--stdio"], {
  85. stdin: "pipe",
  86. stdout: "pipe",
  87. stderr: "pipe",
  88. })
  89. expect(await proc.exited).toBe(0)
  90. })
  91. test("rejects missing commands without leaking unhandled errors", async () => {
  92. await using tmp = await tmpdir()
  93. const cmd = path.join(tmp.path, "missing" + (process.platform === "win32" ? ".cmd" : ""))
  94. const err = await Process.spawn([cmd], {
  95. stdin: "pipe",
  96. stdout: "pipe",
  97. stderr: "pipe",
  98. }).exited.catch((err) => err)
  99. expect(err).toBeInstanceOf(Error)
  100. if (!(err instanceof Error)) throw err
  101. expect(err).toMatchObject({
  102. code: "ENOENT",
  103. })
  104. })
  105. })