Files
bun.sh/test/js/bun/spawn/spawn-kill-signal.test.ts
2024-09-03 21:32:52 -07:00

54 lines
1.7 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import { shellExe } from "harness";
import { constants } from "os";
const inputs = {
SIGTERM: [["SIGTERM"], [undefined], [""], [null], [], [constants.signals.SIGTERM], [NaN]],
SIGKILL: [["SIGKILL"], [constants.signals.SIGKILL]],
} as const;
const fails = [["SIGGOD"], [{}], [() => {}], [Infinity], [-Infinity], [Symbol("what")]] as const;
describe("subprocess.kill", () => {
for (const key in inputs) {
describe(key, () => {
for (let input of inputs[key as keyof typeof inputs]) {
test(Bun.inspect(input).replaceAll("\n", "\\n"), async () => {
const proc = Bun.spawn({
cmd: [shellExe(), "-c", "sleep 1000"],
stdio: ["inherit", "inherit", "inherit"],
});
const { promise, resolve, reject } = Promise.withResolvers();
proc.exited.then(resolve, reject);
proc.kill(...input);
await promise;
expect(proc.exitCode).toBe(null);
expect(proc.signalCode).toBe(key as any);
});
}
});
}
describe("input validation", () => {
for (let input of fails) {
test(Bun.inspect(input).replaceAll("\n", "\\n"), async () => {
const proc = Bun.spawn({
cmd: [shellExe(), "-c", "sleep 1000"],
stdio: ["inherit", "inherit", "inherit"],
});
expect(() => proc.kill(...(input as any))).toThrow();
const { promise, resolve, reject } = Promise.withResolvers();
proc.exited.then(resolve, reject);
proc.kill();
await promise;
expect(proc.exitCode).toBe(null);
expect(proc.signalCode).toBe("SIGTERM");
});
}
});
});