-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathctrl-c-basic.test.mjs
More file actions
194 lines (144 loc) · 5.43 KB
/
ctrl-c-basic.test.mjs
File metadata and controls
194 lines (144 loc) · 5.43 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { describe, it, expect } from 'bun:test';
import './test-helper.mjs'; // Automatically sets up beforeEach/afterEach cleanup
import { $ } from '../src/$.mjs';
describe('CTRL+C Basic Handling', () => {
it('should be able to kill a long-running process', async () => {
// Start a long-running process
const runner = $`sleep 10`;
// Start it without awaiting completion
const promise = runner.start();
// Give it time to start
await new Promise(resolve => setTimeout(resolve, 100));
// Kill it manually (simulating what CTRL+C would do)
runner.kill();
// The process should complete with a non-zero exit code
const result = await promise;
// Should have a non-zero exit code (143 for SIGTERM)
expect(result.code).toBeGreaterThan(0);
expect(result.code).toBe(143); // 128 + 15 (SIGTERM)
});
it('should spawn processes with detached flag on Unix', async () => {
if (process.platform === 'win32') {
// Skip on Windows
return;
}
// Use /bin/sleep to get a real system process, not virtual command
const runner = $`/bin/sleep 1`;
// Start the process
const promise = runner.start();
// Give it a moment to spawn
await new Promise(resolve => setTimeout(resolve, 50));
// Check that the child process exists and has a PID
expect(runner.child).toBeDefined();
expect(runner.child.pid).toBeGreaterThan(0);
// Kill and wait for completion
runner.kill();
try {
await promise;
} catch (error) {
// Expected to error
}
});
it('should handle CTRL+C character in raw mode', async () => {
// Test that the _forwardTTYStdin method properly handles CTRL+C
// This is a unit test of the specific functionality
const runner = $`cat`; // Use cat to read stdin
// Start the process
const promise = runner.start();
// Give it time to set up
await new Promise(resolve => setTimeout(resolve, 100));
// Kill the process
runner.kill();
// Should complete with an error
let errorCode = null;
try {
await promise;
} catch (error) {
errorCode = error.code;
}
expect(errorCode).toBeDefined();
});
});
describe('CTRL+C Virtual Commands', () => {
it('should cancel virtual command with AbortController', async () => {
const runner = $`sleep 5`; // Virtual sleep command
const promise = runner.start();
// Give virtual command time to start
await new Promise(resolve => setTimeout(resolve, 100));
// Kill the virtual command
runner.kill();
const result = await promise;
// Virtual commands return SIGTERM exit code when cancelled
expect(result.code).toBe(143);
}, { timeout: 5000 });
it('should cancel virtual async generator', async () => {
// Test cancelling a streaming virtual command
const runner = $`yes hello`;
// Start streaming
const streamPromise = (async () => {
let chunks = 0;
for await (const _chunk of runner.stream()) {
chunks++;
if (chunks >= 3) {
break; // Break should trigger cancellation
}
}
return chunks;
})();
const chunks = await streamPromise;
expect(chunks).toBe(3);
expect(runner.finished).toBe(true);
}, { timeout: 5000 });
});
describe('CTRL+C Different stdin Modes', () => {
it('should handle CTRL+C with string stdin', async () => {
// Use a long-running command that will actually be killed
const runner = $({ stdin: 'test input\n' })`sleep 10`;
const promise = runner.start();
await new Promise(resolve => setTimeout(resolve, 200));
runner.kill();
const result = await promise;
expect(result.code).toBe(143);
});
it('should handle CTRL+C with Buffer stdin', async () => {
// Use a long-running command that will actually be killed
const runner = $({ stdin: Buffer.from('test input\n') })`sleep 10`;
const promise = runner.start();
await new Promise(resolve => setTimeout(resolve, 200));
runner.kill();
const result = await promise;
expect(result.code).toBe(143);
});
it('should handle CTRL+C with ignore stdin', async () => {
const runner = $({ stdin: 'ignore' })`sleep 5`;
const promise = runner.start();
await new Promise(resolve => setTimeout(resolve, 100));
runner.kill();
const result = await promise;
expect(result.code).toBe(143);
});
});
describe('CTRL+C Pipeline Interruption', () => {
it('should interrupt simple pipeline', async () => {
// Use a very simple approach that we know works
const runner = $`sleep 10`;
const promise = runner.start();
await new Promise(resolve => setTimeout(resolve, 200));
runner.kill();
const result = await promise;
// Should be interrupted with SIGTERM code
expect(result.code).toBe(143);
}, { timeout: 3000 });
});
describe('CTRL+C Process Groups', () => {
it('should handle process group termination on Unix', async () => {
if (process.platform === 'win32') return; // Skip on Windows
// Use a command that spawns subprocesses
const runner = $`sh -c 'sleep 10 & sleep 10 & wait'`;
const promise = runner.start();
await new Promise(resolve => setTimeout(resolve, 200));
runner.kill();
const result = await promise;
expect(result.code).toBe(143);
}, { timeout: 5000 });
});