/
concurrently.spec.ts
448 lines (401 loc) · 16.7 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
442
443
444
445
446
447
448
import * as readline from 'readline';
import _ from 'lodash';
import * as Rx from 'rxjs';
import { buffer, map } from 'rxjs/operators';
import spawn from 'spawn-command';
const isWindows = process.platform === 'win32';
const createKillMessage = (prefix: string) => new RegExp(
_.escapeRegExp(prefix) +
' exited with code ' +
(isWindows ? 1 : '(SIGTERM|143)'),
);
/**
* 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) => {
// TODO: This should only be transpiled once. Tests become 2.5x slower doing it in every `it`.
const child = spawn('ts-node --transpile-only ./concurrently.ts ' + args, {
cwd: __dirname,
env: Object.assign({}, 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 close = Rx.fromEvent<[number | null, NodeJS.Signals | null]>(child, 'close');
const log = Rx.merge(
Rx.fromEvent<Buffer>(stdout, 'line'),
Rx.fromEvent<Buffer>(stderr, 'line'),
).pipe(map(data => data.toString()));
return {
close,
log,
stdin: child.stdin,
pid: child.pid,
};
};
it('has help command', done => {
run('--help').close.subscribe(event => {
expect(event[0]).toBe(0);
done();
}, done);
});
it('has version command', done => {
Rx.combineLatest(
run('--version').close,
run('-V').close,
run('-v').close,
).subscribe(events => {
expect(events[0][0]).toBe(0);
expect(events[1][0]).toBe(0);
expect(events[2][0]).toBe(0);
done();
}, done);
});
describe('exiting conditions', () => {
it('is of success by default when running successful commands', done => {
run('"echo foo" "echo bar"')
.close
.subscribe(exit => {
expect(exit[0]).toBe(0);
done();
}, done);
});
it('is of failure by default when one of the command fails', done => {
run('"echo foo" "exit 1"')
.close
.subscribe(exit => {
expect(exit[0]).toBeGreaterThan(0);
done();
}, done);
});
it('is of success when --success=first and first command to exit succeeds', done => {
run('--success=first "echo foo" "sleep 0.5 && exit 1"')
.close
.subscribe(exit => {
expect(exit[0]).toBe(0);
done();
}, done);
});
it('is of failure when --success=first and first command to exit fails', done => {
run('--success=first "exit 1" "sleep 0.5 && echo foo"')
.close
.subscribe(exit => {
expect(exit[0]).toBeGreaterThan(0);
done();
}, done);
});
it('is of success when --success=last and last command to exit succeeds', done => {
run('--success=last "exit 1" "sleep 0.5 && echo foo"')
.close
.subscribe(exit => {
expect(exit[0]).toBe(0);
done();
}, done);
});
it('is of failure when --success=last and last command to exit fails', done => {
run('--success=last "echo foo" "sleep 0.5 && exit 1"')
.close
.subscribe(exit => {
expect(exit[0]).toBeGreaterThan(0);
done();
}, done);
});
it.skip('is of success when a SIGINT is sent', done => {
const child = run('"node fixtures/read-echo.js"');
child.close.subscribe(exit => {
// TODO This is null within Node, but should be 0 outside (eg from real terminal)
expect(exit[0]).toBe(0);
done();
}, done);
process.kill(child.pid, 'SIGINT');
});
it('is aliased to -s', done => {
run('-s last "exit 1" "sleep 0.5 && echo foo"')
.close
.subscribe(exit => {
expect(exit[0]).toBe(0);
done();
}, done);
});
});
describe('--raw', () => {
it('is aliased to -r', done => {
const child = run('-r "echo foo" "echo bar"');
child.log.pipe(buffer(child.close)).subscribe(lines => {
expect(lines).toHaveLength(2);
expect(lines).toContainEqual(expect.stringContaining('foo'));
expect(lines).toContainEqual(expect.stringContaining('bar'));
done();
}, done);
});
it('does not log any extra output', done => {
const child = run('--raw "echo foo" "echo bar"');
child.log.pipe(buffer(child.close)).subscribe(lines => {
expect(lines).toHaveLength(2);
expect(lines).toContainEqual(expect.stringContaining('foo'));
expect(lines).toContainEqual(expect.stringContaining('bar'));
done();
}, done);
});
});
describe('--hide', () => {
it('hides the output of a process by its index', done => {
const child = run('--hide 1 "echo foo" "echo bar"');
child.log.pipe(buffer(child.close)).subscribe(lines => {
expect(lines).toContainEqual(expect.stringContaining('foo'));
expect(lines).not.toContainEqual(expect.stringContaining('bar'));
done();
}, done);
});
it('hides the output of a process by its name', done => {
const child = run('-n foo,bar --hide bar "echo foo" "echo bar"');
child.log.pipe(buffer(child.close)).subscribe(lines => {
expect(lines).toContainEqual(expect.stringContaining('foo'));
expect(lines).not.toContainEqual(expect.stringContaining('bar'));
done();
}, done);
});
});
describe('--group', () => {
it('groups output per process', done => {
const child = run('--group "echo foo && sleep 1 && echo bar" "echo baz"');
child.log.pipe(buffer(child.close)).subscribe(lines => {
expect(lines.slice(0, 4)).toEqual([
expect.stringContaining('foo'),
expect.stringContaining('bar'),
expect.any(String),
expect.stringContaining('baz'),
]);
done();
}, done);
});
});
describe('--names', () => {
it('is aliased to -n', done => {
const child = run('-n foo,bar "echo foo" "echo bar"');
child.log.pipe(buffer(child.close)).subscribe(lines => {
expect(lines).toContainEqual(expect.stringContaining('[foo] foo'));
expect(lines).toContainEqual(expect.stringContaining('[bar] bar'));
done();
}, done);
});
it('prefixes with names', done => {
const child = run('--names foo,bar "echo foo" "echo bar"');
child.log.pipe(buffer(child.close)).subscribe(lines => {
expect(lines).toContainEqual(expect.stringContaining('[foo] foo'));
expect(lines).toContainEqual(expect.stringContaining('[bar] bar'));
done();
}, done);
});
it('is split using --name-separator arg', done => {
const child = run('--names "foo|bar" --name-separator "|" "echo foo" "echo bar"');
child.log.pipe(buffer(child.close)).subscribe(lines => {
expect(lines).toContainEqual(expect.stringContaining('[foo] foo'));
expect(lines).toContainEqual(expect.stringContaining('[bar] bar'));
done();
}, done);
});
});
describe('--prefix', () => {
it('is aliased to -p', done => {
const child = run('-p command "echo foo" "echo bar"');
child.log.pipe(buffer(child.close)).subscribe(lines => {
expect(lines).toContainEqual(expect.stringContaining('[echo foo] foo'));
expect(lines).toContainEqual(expect.stringContaining('[echo bar] bar'));
done();
}, done);
});
it('specifies custom prefix', done => {
const child = run('--prefix command "echo foo" "echo bar"');
child.log.pipe(buffer(child.close)).subscribe(lines => {
expect(lines).toContainEqual(expect.stringContaining('[echo foo] foo'));
expect(lines).toContainEqual(expect.stringContaining('[echo bar] bar'));
done();
}, done);
});
});
describe('--prefix-length', () => {
it('is aliased to -l', done => {
const child = run('-p command -l 5 "echo foo" "echo bar"');
child.log.pipe(buffer(child.close)).subscribe(lines => {
expect(lines).toContainEqual(expect.stringContaining('[ec..o] foo'));
expect(lines).toContainEqual(expect.stringContaining('[ec..r] bar'));
done();
}, done);
});
it('specifies custom prefix length', done => {
const child = run('--prefix command --prefix-length 5 "echo foo" "echo bar"');
child.log.pipe(buffer(child.close)).subscribe(lines => {
expect(lines).toContainEqual(expect.stringContaining('[ec..o] foo'));
expect(lines).toContainEqual(expect.stringContaining('[ec..r] bar'));
done();
}, done);
});
});
describe('--restart-tries', () => {
it('changes how many times a command will restart', done => {
const child = run('--restart-tries 1 "exit 1"');
child.log.pipe(buffer(child.close)).subscribe(lines => {
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'),
]);
done();
}, done);
});
});
describe('--kill-others', () => {
it('is aliased to -k', done => {
const child = run('-k "sleep 10" "exit 0"');
child.log.pipe(buffer(child.close)).subscribe(lines => {
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] sleep 10')));
done();
}, done);
});
it('kills on success', done => {
const child = run('--kill-others "sleep 10" "exit 0"');
child.log.pipe(buffer(child.close)).subscribe(lines => {
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] sleep 10')));
done();
}, done);
});
it('kills on failure', done => {
const child = run('--kill-others "sleep 10" "exit 1"');
child.log.pipe(buffer(child.close)).subscribe(lines => {
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] sleep 10')));
done();
}, done);
});
});
describe('--kill-others-on-fail', () => {
it('does not kill on success', done => {
const child = run('--kill-others-on-fail "sleep 0.5" "exit 0"');
child.log.pipe(buffer(child.close)).subscribe(lines => {
expect(lines).toContainEqual(expect.stringContaining('[1] exit 0 exited with code 0'));
expect(lines).toContainEqual(expect.stringContaining('[0] sleep 0.5 exited with code 0'));
done();
}, done);
});
it('kills on failure', done => {
const child = run('--kill-others-on-fail "sleep 10" "exit 1"');
child.log.pipe(buffer(child.close)).subscribe(lines => {
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] sleep 10')));
done();
}, done);
});
});
describe('--handle-input', () => {
it('is aliased to -i', done => {
const child = run('-i "node fixtures/read-echo.js"');
child.log.subscribe(line => {
if (/READING/.test(line)) {
child.stdin.write('stop\n');
}
if (/\[0\] stop/.test(line)) {
done();
}
}, done);
});
it('forwards input to first process by default', done => {
const child = run('--handle-input "node fixtures/read-echo.js"');
child.log.subscribe(line => {
if (/READING/.test(line)) {
child.stdin.write('stop\n');
}
if (/\[0\] stop/.test(line)) {
done();
}
}, done);
});
it('forwards input to process --default-input-target', done => {
const lines: string[] = [];
const child = run('-ki --default-input-target 1 "node fixtures/read-echo.js" "node fixtures/read-echo.js"');
child.log.subscribe(line => {
lines.push(line);
if (/\[1\] READING/.test(line)) {
child.stdin.write('stop\n');
}
}, done);
child.close.subscribe(exit => {
expect(exit[0]).toBeGreaterThan(0);
expect(lines).toContainEqual(expect.stringContaining('[1] stop'));
expect(lines).toContainEqual(expect.stringMatching(createKillMessage('[0] node fixtures/read-echo.js')));
done();
}, done);
});
it('forwards input to specified process', done => {
const lines: string[] = [];
const child = run('-ki "node fixtures/read-echo.js" "node fixtures/read-echo.js"');
child.log.subscribe(line => {
lines.push(line);
if (/\[1\] READING/.test(line)) {
child.stdin.write('1:stop\n');
}
}, done);
child.close.subscribe(exit => {
expect(exit[0]).toBeGreaterThan(0);
expect(lines).toContainEqual(expect.stringContaining('[1] stop'));
expect(lines).toContainEqual(expect.stringMatching(createKillMessage('[0] node fixtures/read-echo.js')));
done();
}, done);
});
});
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 expectLinesForProcessStartAndStop = (lines: string[], index: number, command: string) => {
const escapedCommand = _.escapeRegExp(command);
expect(lines).toContainEqual(expect.stringMatching(processStartedMessageRegex(index, escapedCommand)));
expect(lines).toContainEqual(expect.stringMatching(processStoppedMessageRegex(index, escapedCommand)));
};
const expectLinesForTimingsTable = (lines: string[]) => {
const tableTopBorderRegex = /┌[─┬]+┐/g;
expect(lines).toContainEqual(expect.stringMatching(tableTopBorderRegex));
const tableHeaderRowRegex = /(\W+(name|duration|exit code|killed|command)\W+){5}/g;
expect(lines).toContainEqual(expect.stringMatching(tableHeaderRowRegex));
const tableBottomBorderRegex = /└[─┴]+┘/g;
expect(lines).toContainEqual(expect.stringMatching(tableBottomBorderRegex));
};
it('shows timings on success', done => {
const child = run('--timings "sleep 0.5" "exit 0"');
child.log.pipe(buffer(child.close)).subscribe(lines => {
expectLinesForProcessStartAndStop(lines, 0, 'sleep 0.5');
expectLinesForProcessStartAndStop(lines, 1, 'exit 0');
expectLinesForTimingsTable(lines);
done();
}, done);
});
it('shows timings on failure', done => {
const child = run('--timings "sleep 0.75" "exit 1"');
child.log.pipe(buffer(child.close)).subscribe(lines => {
expectLinesForProcessStartAndStop(lines, 0, 'sleep 0.75');
expectLinesForProcessStartAndStop(lines, 1, 'exit 1');
expectLinesForTimingsTable(lines);
done();
}, done);
});
});