-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
ctrl-c.test.ts
103 lines (95 loc) · 2.46 KB
/
ctrl-c.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { expect, it, test } from "bun:test";
import { bunEnv, bunExe, isWindows, tempDirWithFiles } from "harness";
test.skipIf(isWindows)("verify that we forward SIGINT from parent to child in bun run", () => {
const dir = tempDirWithFiles("ctrlc", {
"index.js": `
let count = 0;
process.exitCode = 1;
process.once("SIGINT", () => {
process.kill(process.pid, "SIGKILL");
});
setTimeout(() => {}, 999999)
process.kill(process.ppid, "SIGINT");
`,
"package.json": `
{
"name": "ctrlc",
"scripts": {
"start": "${bunExe()} index.js"
}
}
`,
});
const result = Bun.spawnSync({
cmd: [bunExe(), "start"],
cwd: dir,
env: bunEnv,
stdout: "inherit",
stderr: "inherit",
});
expect(result.exitCode).toBe(null);
expect(result.signalCode).toBe("SIGKILL");
});
for (const mode of [
["vite"],
["dev"],
...(isWindows ? [] : [["./node_modules/.bin/vite"]]),
["--bun", "vite"],
["--bun", "dev"],
...(isWindows ? [] : [["--bun", "./node_modules/.bin/vite"]]),
]) {
it("kills on SIGINT in: 'bun " + mode.join(" ") + "'", async () => {
const dir = tempDirWithFiles("ctrlc", {
"package.json": JSON.stringify({
name: "ctrlc",
scripts: {
"dev": "vite",
},
devDependencies: {
"vite": "^6.0.1",
},
}),
});
expect(
Bun.spawnSync({
cmd: [bunExe(), "install"],
cwd: dir,
stdin: "inherit",
stdout: "inherit",
stderr: "inherit",
}).exitCode,
).toBe(0);
const proc = Bun.spawn({
cmd: [bunExe(), ...mode],
cwd: dir,
stdin: "inherit",
stdout: "pipe",
stderr: "inherit",
env: { ...bunEnv, PORT: "9876" },
});
// wait for vite to start
const reader = proc.stdout.getReader();
await reader.read(); // wait for first bit of stdout
reader.releaseLock();
expect(proc.killed).toBe(false);
// send sigint
process.kill(proc.pid, "SIGINT");
// wait for exit or 200ms
await Promise.race([proc.exited, Bun.sleep(200)]);
// wait to allow a moment to be killed
await Bun.sleep(100); // wait for kill
expect({
killed: proc.killed,
exitCode: proc.exitCode,
signalCode: proc.signalCode,
}).toEqual(isWindows ? {
killed: true,
exitCode: 1,
signalCode: null,
} : {
killed: true,
exitCode: null,
signalCode: "SIGINT",
});
});
}