-
-
Notifications
You must be signed in to change notification settings - Fork 262
Expand file tree
/
Copy pathindex.spec.ts
More file actions
509 lines (429 loc) · 18.7 KB
/
index.spec.ts
File metadata and controls
509 lines (429 loc) · 18.7 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
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
import { spawn } from 'node:child_process';
import fs from 'node:fs';
import path from 'node:path';
import readline from 'node:readline';
import { subscribeSpyTo } from '@hirez_io/observer-spy';
import { sendCtrlC, spawnWithWrapper } from 'ctrlc-wrapper';
import Rx from 'rxjs';
import { map } from 'rxjs/operators';
import stringArgv from 'string-argv';
import { describe, expect, it } from 'vitest';
import { escapeRegExp } from '../../lib/utils.js';
const isWindows = process.platform === 'win32';
const createKillMessage = (prefix: string, signal: 'SIGTERM' | 'SIGINT' | string) => {
const map: Record<string, string | number> = {
SIGTERM: isWindows ? 1 : '(SIGTERM|143)',
// Could theoretically be anything (e.g. 0) if process has SIGINT handler
SIGINT: isWindows ? '(3221225786|0)' : '(SIGINT|130|0)',
};
return new RegExp(`${escapeRegExp(prefix)} exited with code ${map[signal] ?? signal}`);
};
/**
* 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, ctrlcWrapper?: boolean) => {
const spawnFn = ctrlcWrapper ? spawnWithWrapper : spawn;
const child = spawnFn('node', ['../../dist/bin/index.js', ...stringArgv(args)], {
cwd: __dirname,
env: {
...process.env,
},
});
const stdout = readline.createInterface({
input: child.stdout,
});
const stderr = readline.createInterface({
input: child.stderr,
});
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((event) => {
const exit = event as [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 {
process: child,
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);
});
it('prints help when no arguments are passed', async () => {
const exit = await run('').exit;
expect(exit.code).toBe(0);
});
describe('has version command', () => {
const pkg = fs.readFileSync(path.join(__dirname, '..', '..', 'package.json'), 'utf-8');
const { version } = JSON.parse(pkg);
it.each(['--version', '-V', '-v'])('%s', async (arg) => {
const child = run(arg);
const log = await child.getLogLines();
expect(log).toContain(version);
const { code } = await child.exit;
expect(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.js 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.js 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.js 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.js 0.5 && exit 1"',
).exit;
expect(exit.code).toBeGreaterThan(0);
});
it('is of success when a SIGINT is sent', async () => {
// Windows doesn't support sending signals like on POSIX platforms.
// However, in a console, processes can be interrupted with CTRL+C (like a SIGINT).
// This is what we simulate here with the help of a wrapper application.
const child = run('"node __fixtures__/read-echo.js"', isWindows);
// Wait for command to have started before sending SIGINT
child.log.subscribe((line) => {
if (/READING/.test(line)) {
if (isWindows) {
// Instruct the wrapper to send CTRL+C to its child
sendCtrlC(child.process);
} else {
process.kill(Number(child.pid), 'SIGINT');
}
}
});
const lines = await child.getLogLines();
const exit = await child.exit;
expect(exit.code).toBe(0);
expect(lines).toContainEqual(
expect.stringMatching(
createKillMessage(
'[0] node __fixtures__/read-echo.js',
// TODO: Flappy value due to race condition, sometimes killed by concurrently (exit code 1),
// sometimes terminated on its own (exit code 0).
// Related issue: https://github.com/open-cli-tools/concurrently/issues/283
isWindows ? '(3221225786|0|1)' : 'SIGINT',
),
),
);
});
});
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'));
});
it('hides the output of a process by its index in raw mode', async () => {
const lines = await run('--hide 1 --raw "echo foo" "echo bar"').getLogLines();
expect(lines).toHaveLength(1);
expect(lines).toContainEqual(expect.stringContaining('foo'));
expect(lines).not.toContainEqual(expect.stringContaining('bar'));
});
it('hides the output of a process by its name in raw mode', async () => {
const lines = await run('-n foo,bar --hide bar --raw "echo foo" "echo bar"').getLogLines();
expect(lines).toHaveLength(1);
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.js 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('--pad-prefix', () => {
it('pads prefixes with spaces', async () => {
const lines = await run('--pad-prefix -n foo,barbaz "echo foo" "echo bar"').getLogLines();
expect(lines).toContainEqual(expect.stringContaining('[foo ]'));
expect(lines).toContainEqual(expect.stringContaining('[barbaz]'));
});
});
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.js 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.js 10', 'SIGTERM'),
),
);
});
});
it('kills on failure', async () => {
const lines = await run(
'--kill-others "node __fixtures__/sleep.js 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.js 10', 'SIGTERM'),
),
);
});
});
describe('--kill-others-on-fail', () => {
it('does not kill on success', async () => {
const lines = await run(
'--kill-others-on-fail "node __fixtures__/sleep.js 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.js 0.5 exited with code 0'),
);
});
it('kills on failure', async () => {
const lines = await run(
'--kill-others-on-fail "node __fixtures__/sleep.js 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.js 10', 'SIGTERM'),
),
);
});
});
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', 'SIGTERM'),
),
);
});
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', 'SIGTERM'),
),
);
});
});
describe('--teardown', () => {
it('runs teardown commands when input commands exit', async () => {
const lines = await run('--teardown "echo bye" "echo hey"').getLogLines();
expect(lines).toEqual([
expect.stringContaining('[0] hey'),
expect.stringContaining('[0] echo hey exited with code 0'),
expect.stringContaining('--> Running teardown command "echo bye"'),
expect.stringContaining('bye'),
expect.stringContaining('--> Teardown command "echo bye" exited with code 0'),
]);
});
it('runs multiple teardown commands', async () => {
const lines = await run(
'--teardown "echo bye" --teardown "echo bye2" "echo hey"',
).getLogLines();
expect(lines).toContain('bye');
expect(lines).toContain('bye2');
});
});
describe('--timings', () => {
const defaultTimestampFormatRegex = /\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d{3}/;
const tableTopBorderRegex = /^--> ┌[─┬]+┐$/;
const tableHeaderRowRegex = /^--> │ name +│ duration +│ exit code +│ killed +│ command +│$/;
const tableBottomBorderRegex = /^--> └[─┴]+┘$/;
const timingsTests = {
'shows timings on success': ['node __fixtures__/sleep.js 0.5', 'exit 0'],
'shows timings on failure': ['node __fixtures__/sleep.js 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(
new RegExp(
`^\\[${index}] ${escapedCommand} started at ${defaultTimestampFormatRegex.source}$`,
),
),
);
expect(lines).toContainEqual(
expect.stringMatching(
new RegExp(
`^\\[${index}] ${escapedCommand} stopped at ${defaultTimestampFormatRegex.source} after (\\d|,)+ms$`,
),
),
);
});
// 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'));
});
});