-
-
Notifications
You must be signed in to change notification settings - Fork 222
/
concurrently.spec.ts
441 lines (369 loc) · 15.8 KB
/
concurrently.spec.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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
import { subscribeSpyTo } from '@hirez_io/observer-spy';
import { spawn } from 'child_process';
import { build } from 'esbuild';
import fs from 'fs';
import { escapeRegExp } from 'lodash';
import os from 'os';
import path from 'path';
import * as readline from 'readline';
import * as Rx from 'rxjs';
import { map } from 'rxjs/operators';
import stringArgv from 'string-argv';
const isWindows = process.platform === 'win32';
const createKillMessage = (prefix: string) =>
new RegExp(escapeRegExp(prefix) + ' exited with code ' + (isWindows ? 1 : '(SIGTERM|143)'));
let tmpDir: string;
beforeAll(async () => {
// Build 'concurrently' and store it in a temporary directory
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'concurrently-'));
await build({
entryPoints: [path.join(__dirname, 'concurrently.ts')],
platform: 'node',
bundle: true,
outfile: path.join(tmpDir, 'concurrently.js'),
});
}, 8000);
afterAll(() => {
// Remove the temporary directory where 'concurrently' was stored
if (tmpDir) {
fs.rmdirSync(tmpDir, { recursive: true });
}
});
/**
* Creates a child process running 'concurrently' with the given args.
* Returns observables for its combined stdout + stderr output, close events, pid, and stdin stream.
*/
const run = (args: string) => {
const child = spawn('node', [path.join(tmpDir, 'concurrently.js'), ...stringArgv(args)], {
cwd: __dirname,
env: {
...process.env,
// When upgrading from jest 23 -> 24, colors started printing in the test output.
// They are forcibly disabled here.
FORCE_COLOR: '0',
},
});
const stdout = readline.createInterface({
input: child.stdout,
output: null,
});
const stderr = readline.createInterface({
input: child.stderr,
output: null,
});
const log = new Rx.Observable<string>((observer) => {
stdout.on('line', (line) => {
observer.next(line);
});
stderr.on('line', (line) => {
observer.next(line);
});
child.on('close', () => {
observer.complete();
});
});
const exit = Rx.firstValueFrom(
Rx.fromEvent(child, 'exit').pipe(
map((exit: [number | null, NodeJS.Signals | null]) => {
return {
/** The exit code if the child exited on its own. */
code: exit[0],
/** The signal by which the child process was terminated. */
signal: exit[1],
};
})
)
);
const getLogLines = async (): Promise<string[]> => {
const observerSpy = subscribeSpyTo(log);
await observerSpy.onComplete();
observerSpy.unsubscribe();
return observerSpy.getValues();
};
return {
stdin: child.stdin,
pid: child.pid,
log,
getLogLines,
exit,
};
};
it('has help command', async () => {
const exit = await run('--help').exit;
expect(exit.code).toBe(0);
});
describe('has version command', () => {
it.each(['--version', '-V', '-v'])('%s', async (arg) => {
const exit = await run(arg).exit;
expect(exit.code).toBe(0);
});
});
describe('exiting conditions', () => {
it('is of success by default when running successful commands', async () => {
const exit = await run('"echo foo" "echo bar"').exit;
expect(exit.code).toBe(0);
});
it('is of failure by default when one of the command fails', async () => {
const exit = await run('"echo foo" "exit 1"').exit;
expect(exit.code).toBeGreaterThan(0);
});
it('is of success when --success=first and first command to exit succeeds', async () => {
const exit = await run('--success=first "echo foo" "node fixtures/sleep.mjs 0.5 && exit 1"')
.exit;
expect(exit.code).toBe(0);
});
it('is of failure when --success=first and first command to exit fails', async () => {
const exit = await run('--success=first "exit 1" "node fixtures/sleep.mjs 0.5 && echo foo"')
.exit;
expect(exit.code).toBeGreaterThan(0);
});
describe('is of success when --success=last and last command to exit succeeds', () => {
it.each(['--success=last', '-s last'])('%s', async (arg) => {
const exit = await run(`${arg} "exit 1" "node fixtures/sleep.mjs 0.5 && echo foo"`)
.exit;
expect(exit.code).toBe(0);
});
});
it('is of failure when --success=last and last command to exit fails', async () => {
const exit = await run('--success=last "echo foo" "node fixtures/sleep.mjs 0.5 && exit 1"')
.exit;
expect(exit.code).toBeGreaterThan(0);
});
it('is of success when a SIGINT is sent', async () => {
const child = run('"node fixtures/read-echo.js"');
// Wait for command to have started before sending SIGINT
child.log.subscribe((line) => {
if (/READING/.test(line)) {
process.kill(child.pid, 'SIGINT');
}
});
const exit = await child.exit;
// TODO
// Windows doesn't support sending signals like on POSIX platforms.
// In a console, processes can be interrupted with CTRL+C (SIGINT).
// However, there is no easy way to simulate this event.
// Calling 'process.kill' on a process in Windows means it
// is getting killed forcefully and abruptly (similar to SIGKILL),
// which then results in the exit code of '1'.
expect(exit.code).toBe(isWindows ? 1 : 0);
});
});
describe('does not log any extra output', () => {
it.each(['--raw', '-r'])('%s', async (arg) => {
const lines = await run(`${arg} "echo foo" "echo bar"`).getLogLines();
expect(lines).toHaveLength(2);
expect(lines).toContainEqual(expect.stringContaining('foo'));
expect(lines).toContainEqual(expect.stringContaining('bar'));
});
});
describe('--hide', () => {
it('hides the output of a process by its index', async () => {
const lines = await run('--hide 1 "echo foo" "echo bar"').getLogLines();
expect(lines).toContainEqual(expect.stringContaining('foo'));
expect(lines).not.toContainEqual(expect.stringContaining('bar'));
});
it('hides the output of a process by its name', async () => {
const lines = await run('-n foo,bar --hide bar "echo foo" "echo bar"').getLogLines();
expect(lines).toContainEqual(expect.stringContaining('foo'));
expect(lines).not.toContainEqual(expect.stringContaining('bar'));
});
});
describe('--group', () => {
it('groups output per process', async () => {
const lines = await run(
'--group "echo foo && node fixtures/sleep.mjs 1 && echo bar" "echo baz"'
).getLogLines();
expect(lines.slice(0, 4)).toEqual([
expect.stringContaining('foo'),
expect.stringContaining('bar'),
expect.any(String),
expect.stringContaining('baz'),
]);
});
});
describe('--names', () => {
describe('prefixes with names', () => {
it.each(['--names', '-n'])('%s', async (arg) => {
const lines = await run(`${arg} foo,bar "echo foo" "echo bar"`).getLogLines();
expect(lines).toContainEqual(expect.stringContaining('[foo] foo'));
expect(lines).toContainEqual(expect.stringContaining('[bar] bar'));
});
});
it('is split using --name-separator arg', async () => {
const lines = await run(
'--names "foo|bar" --name-separator "|" "echo foo" "echo bar"'
).getLogLines();
expect(lines).toContainEqual(expect.stringContaining('[foo] foo'));
expect(lines).toContainEqual(expect.stringContaining('[bar] bar'));
});
});
describe('specifies custom prefix', () => {
it.each(['--prefix', '-p'])('%s', async (arg) => {
const lines = await run(`${arg} command "echo foo" "echo bar"`).getLogLines();
expect(lines).toContainEqual(expect.stringContaining('[echo foo] foo'));
expect(lines).toContainEqual(expect.stringContaining('[echo bar] bar'));
});
});
describe('specifies custom prefix length', () => {
it.each(['--prefix command --prefix-length 5', '-p command -l 5'])('%s', async (arg) => {
const lines = await run(`${arg} "echo foo" "echo bar"`).getLogLines();
expect(lines).toContainEqual(expect.stringContaining('[ec..o] foo'));
expect(lines).toContainEqual(expect.stringContaining('[ec..r] bar'));
});
});
describe('--restart-tries', () => {
it('changes how many times a command will restart', async () => {
const lines = await run('--restart-tries 1 "exit 1"').getLogLines();
expect(lines).toEqual([
expect.stringContaining('[0] exit 1 exited with code 1'),
expect.stringContaining('[0] exit 1 restarted'),
expect.stringContaining('[0] exit 1 exited with code 1'),
]);
});
});
describe('--kill-others', () => {
describe('kills on success', () => {
it.each(['--kill-others', '-k'])('%s', async (arg) => {
const lines = await run(`${arg} "node fixtures/sleep.mjs 10" "exit 0"`).getLogLines();
expect(lines).toContainEqual(expect.stringContaining('[1] exit 0 exited with code 0'));
expect(lines).toContainEqual(
expect.stringContaining('Sending SIGTERM to other processes')
);
expect(lines).toContainEqual(
expect.stringMatching(createKillMessage('[0] node fixtures/sleep.mjs 10'))
);
});
});
it('kills on failure', async () => {
const lines = await run(
'--kill-others "node fixtures/sleep.mjs 10" "exit 1"'
).getLogLines();
expect(lines).toContainEqual(expect.stringContaining('[1] exit 1 exited with code 1'));
expect(lines).toContainEqual(expect.stringContaining('Sending SIGTERM to other processes'));
expect(lines).toContainEqual(
expect.stringMatching(createKillMessage('[0] node fixtures/sleep.mjs 10'))
);
});
});
describe('--kill-others-on-fail', () => {
it('does not kill on success', async () => {
const lines = await run(
'--kill-others-on-fail "node fixtures/sleep.mjs 0.5" "exit 0"'
).getLogLines();
expect(lines).toContainEqual(expect.stringContaining('[1] exit 0 exited with code 0'));
expect(lines).toContainEqual(
expect.stringContaining('[0] node fixtures/sleep.mjs 0.5 exited with code 0')
);
});
it('kills on failure', async () => {
const lines = await run(
'--kill-others-on-fail "node fixtures/sleep.mjs 10" "exit 1"'
).getLogLines();
expect(lines).toContainEqual(expect.stringContaining('[1] exit 1 exited with code 1'));
expect(lines).toContainEqual(expect.stringContaining('Sending SIGTERM to other processes'));
expect(lines).toContainEqual(
expect.stringMatching(createKillMessage('[0] node fixtures/sleep.mjs 10'))
);
});
});
describe('--handle-input', () => {
describe('forwards input to first process by default', () => {
it.each(['--handle-input', '-i'])('%s', async (arg) => {
const child = run(`${arg} "node fixtures/read-echo.js"`);
child.log.subscribe((line) => {
if (/READING/.test(line)) {
child.stdin.write('stop\n');
}
});
const lines = await child.getLogLines();
const exit = await child.exit;
expect(exit.code).toBe(0);
expect(lines).toContainEqual(expect.stringContaining('[0] stop'));
expect(lines).toContainEqual(
expect.stringContaining('[0] node fixtures/read-echo.js exited with code 0')
);
});
});
it('forwards input to process --default-input-target', async () => {
const child = run(
'-ki --default-input-target 1 "node fixtures/read-echo.js" "node fixtures/read-echo.js"'
);
child.log.subscribe((line) => {
if (/\[1\] READING/.test(line)) {
child.stdin.write('stop\n');
}
});
const lines = await child.getLogLines();
const exit = await child.exit;
expect(exit.code).toBeGreaterThan(0);
expect(lines).toContainEqual(expect.stringContaining('[1] stop'));
expect(lines).toContainEqual(
expect.stringMatching(createKillMessage('[0] node fixtures/read-echo.js'))
);
});
it('forwards input to specified process', async () => {
const child = run('-ki "node fixtures/read-echo.js" "node fixtures/read-echo.js"');
child.log.subscribe((line) => {
if (/\[1\] READING/.test(line)) {
child.stdin.write('1:stop\n');
}
});
const lines = await child.getLogLines();
const exit = await child.exit;
expect(exit.code).toBeGreaterThan(0);
expect(lines).toContainEqual(expect.stringContaining('[1] stop'));
expect(lines).toContainEqual(
expect.stringMatching(createKillMessage('[0] node fixtures/read-echo.js'))
);
});
});
describe('--timings', () => {
const defaultTimestampFormatRegex = /\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d{3}/;
const processStartedMessageRegex = (index: number, command: string) => {
return new RegExp(
`^\\[${index}] ${command} started at ${defaultTimestampFormatRegex.source}$`
);
};
const processStoppedMessageRegex = (index: number, command: string) => {
return new RegExp(
`^\\[${index}] ${command} stopped at ${defaultTimestampFormatRegex.source} after (\\d|,)+ms$`
);
};
const tableTopBorderRegex = /┌[─┬]+┐/g;
const tableHeaderRowRegex = /(\W+(name|duration|exit code|killed|command)\W+){5}/g;
const tableBottomBorderRegex = /└[─┴]+┘/g;
const timingsTests = {
'shows timings on success': ['node fixtures/sleep.mjs 0.5', 'exit 0'],
'shows timings on failure': ['node fixtures/sleep.mjs 0.75', 'exit 1'],
};
it.each(Object.entries(timingsTests))('%s', async (_, commands) => {
const lines = await run(
`--timings ${commands.map((command) => `"${command}"`).join(' ')}`
).getLogLines();
// Expect output to contain process start / stop messages for each command
commands.forEach((command, index) => {
const escapedCommand = escapeRegExp(command);
expect(lines).toContainEqual(
expect.stringMatching(processStartedMessageRegex(index, escapedCommand))
);
expect(lines).toContainEqual(
expect.stringMatching(processStoppedMessageRegex(index, escapedCommand))
);
});
// Expect output to contain timings table
expect(lines).toContainEqual(expect.stringMatching(tableTopBorderRegex));
expect(lines).toContainEqual(expect.stringMatching(tableHeaderRowRegex));
expect(lines).toContainEqual(expect.stringMatching(tableBottomBorderRegex));
});
});
describe('--passthrough-arguments', () => {
it('argument placeholders are properly replaced when passthrough-arguments is enabled', async () => {
const lines = await run('--passthrough-arguments "echo {1}" -- echo').getLogLines();
expect(lines).toContainEqual(expect.stringContaining('[0] echo echo exited with code 0'));
});
it('argument placeholders are not replaced when passthrough-arguments is disabled', async () => {
const lines = await run('"echo {1}" -- echo').getLogLines();
expect(lines).toContainEqual(expect.stringContaining('[0] echo {1} exited with code 0'));
expect(lines).toContainEqual(expect.stringContaining('[1] echo exited with code 0'));
});
});