/
test.js
837 lines (683 loc) · 26.3 KB
/
test.js
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
import path from 'path';
import fs from 'fs';
import stream from 'stream';
import childProcess from 'child_process';
import test from 'ava';
import getStream from 'get-stream';
import isRunning from 'is-running';
import tempfile from 'tempfile';
import pEvent from 'p-event';
import execa from '.';
process.env.PATH = path.join(__dirname, 'fixtures') + path.delimiter + process.env.PATH;
process.env.FOO = 'foo';
const TIMEOUT_REGEXP = /timed out after/;
const getExitRegExp = exitMessage => new RegExp(`failed with exit code ${exitMessage}`);
test('execa()', async t => {
const {stdout} = await execa('noop', ['foo']);
t.is(stdout, 'foo');
});
if (process.platform === 'win32') {
test('execa() - cmd file', async t => {
const {stdout} = await execa('hello.cmd');
t.is(stdout, 'Hello World');
});
test('execa() - run cmd command', async t => {
const {stdout} = await execa('cmd', ['/c', 'hello.cmd']);
t.is(stdout, 'Hello World');
});
}
test('buffer', async t => {
const {stdout} = await execa('noop', ['foo'], {encoding: null});
t.true(Buffer.isBuffer(stdout));
t.is(stdout.toString(), 'foo');
});
test.serial('result.all shows both `stdout` and `stderr` intermixed', async t => {
const {all} = await execa('noop-132');
t.is(all, '132');
});
test('stdout/stderr/all available on errors', async t => {
const {stdout, stderr, all} = await t.throwsAsync(execa('exit', ['2']), {message: getExitRegExp('2')});
t.is(typeof stdout, 'string');
t.is(typeof stderr, 'string');
t.is(typeof all, 'string');
});
test('stdout/stderr/all are undefined if ignored', async t => {
const {stdout, stderr, all} = await execa('noop', {stdio: 'ignore'});
t.is(stdout, undefined);
t.is(stderr, undefined);
t.is(all, undefined);
});
test('stdout/stderr/all are undefined if ignored in sync mode', t => {
const {stdout, stderr, all} = execa.sync('noop', {stdio: 'ignore'});
t.is(stdout, undefined);
t.is(stderr, undefined);
t.is(all, undefined);
});
const WRONG_COMMAND = process.platform === 'win32' ?
'\'wrong\' is not recognized as an internal or external command,\r\noperable program or batch file.' :
'';
test('stdout/stderr/all on process errors', async t => {
const {stdout, stderr, all} = await t.throwsAsync(execa('wrong command'));
t.is(stdout, '');
t.is(stderr, WRONG_COMMAND);
t.is(all, WRONG_COMMAND);
});
test('stdout/stderr/all on process errors, in sync mode', t => {
const {stdout, stderr, all} = t.throws(() => {
execa.sync('wrong command');
});
t.is(stdout, '');
t.is(stderr, WRONG_COMMAND);
t.is(all, undefined);
});
test('pass `stdout` to a file descriptor', async t => {
const file = tempfile('.txt');
await execa('fixtures/noop', ['foo bar'], {stdout: fs.openSync(file, 'w')});
t.is(fs.readFileSync(file, 'utf8'), 'foo bar\n');
});
test('pass `stderr` to a file descriptor', async t => {
const file = tempfile('.txt');
await execa('fixtures/noop-err', ['foo bar'], {stderr: fs.openSync(file, 'w')});
t.is(fs.readFileSync(file, 'utf8'), 'foo bar\n');
});
test('execa.sync()', t => {
const {stdout} = execa.sync('noop', ['foo']);
t.is(stdout, 'foo');
});
test('execa.sync() throws error if written to stderr', t => {
t.throws(() => {
execa.sync('foo');
}, process.platform === 'win32' ? /failed with exit code 1/ : /spawnSync foo ENOENT/);
});
test('skip throwing when using reject option', async t => {
const {exitCode} = await execa('fail', {reject: false});
t.is(exitCode, 2);
});
test('skip throwing when using reject option in sync mode', t => {
const {exitCode} = execa.sync('fail', {reject: false});
t.is(exitCode, 2);
});
test('kill("SIGKILL") should terminate cleanly', async t => {
const subprocess = execa('node', ['fixtures/no-killable'], {stdio: ['ipc']});
await pEvent(subprocess, 'message');
subprocess.kill('SIGKILL');
const {signal} = await t.throwsAsync(subprocess);
t.is(signal, 'SIGKILL');
});
// `SIGTERM` cannot be caught on Windows, and it always aborts the process (like `SIGKILL` on Unix).
// Therefore, this feature and those tests do not make sense on Windows.
if (process.platform !== 'win32') {
test('`forceKillAfterTimeout: false` should not kill after a timeout', async t => {
const subprocess = execa('node', ['fixtures/no-killable'], {stdio: ['ipc']});
await pEvent(subprocess, 'message');
subprocess.kill('SIGTERM', {forceKillAfterTimeout: false});
t.true(isRunning(subprocess.pid));
subprocess.kill('SIGKILL');
});
test('`forceKillAfterTimeout: number` should kill after a timeout', async t => {
const subprocess = execa('node', ['fixtures/no-killable'], {stdio: ['ipc']});
await pEvent(subprocess, 'message');
subprocess.kill('SIGTERM', {forceKillAfterTimeout: 50});
const {signal} = await t.throwsAsync(subprocess);
t.is(signal, 'SIGKILL');
});
test('`forceKillAfterTimeout: true` should kill after a timeout', async t => {
const subprocess = execa('node', ['fixtures/no-killable'], {stdio: ['ipc']});
await pEvent(subprocess, 'message');
subprocess.kill('SIGTERM', {forceKillAfterTimeout: true});
const {signal} = await t.throwsAsync(subprocess);
t.is(signal, 'SIGKILL');
});
test('kill() with no arguments should kill after a timeout', async t => {
const subprocess = execa('node', ['fixtures/no-killable'], {stdio: ['ipc']});
await pEvent(subprocess, 'message');
subprocess.kill();
const {signal} = await t.throwsAsync(subprocess);
t.is(signal, 'SIGKILL');
});
test('`forceKillAfterTimeout` should not be a float', t => {
t.throws(() => {
execa('noop').kill('SIGTERM', {forceKillAfterTimeout: 0.5});
}, {instanceOf: TypeError, message: /non-negative integer/});
});
test('`forceKillAfterTimeout` should not be negative', t => {
t.throws(() => {
execa('noop').kill('SIGTERM', {forceKillAfterTimeout: -1});
}, {instanceOf: TypeError, message: /non-negative integer/});
});
}
test('stripFinalNewline: true', async t => {
const {stdout} = await execa('noop', ['foo']);
t.is(stdout, 'foo');
});
test('stripFinalNewline: false', async t => {
const {stdout} = await execa('noop', ['foo'], {stripFinalNewline: false});
t.is(stdout, 'foo\n');
});
test('stripFinalNewline on failure', async t => {
const {stderr} = await t.throwsAsync(execa('noop-throw', ['foo'], {stripFinalNewline: true}));
t.is(stderr, 'foo');
});
test('stripFinalNewline in sync mode', t => {
const {stdout} = execa.sync('noop', ['foo'], {stripFinalNewline: true});
t.is(stdout, 'foo');
});
test('stripFinalNewline in sync mode on failure', t => {
const {stderr} = t.throws(() => {
execa.sync('noop-throw', ['foo'], {stripFinalNewline: true});
});
t.is(stderr, 'foo');
});
test('preferLocal option', async t => {
await execa('ava', ['--version'], {env: {PATH: ''}});
const errorRegExp = process.platform === 'win32' ? /failed with exit code 1/ : /spawn ava ENOENT/;
await t.throwsAsync(execa('ava', ['--version'], {preferLocal: false, env: {PATH: ''}}), errorRegExp);
});
test('localDir option', async t => {
const command = process.platform === 'win32' ? 'echo %PATH%' : 'echo $PATH';
const {stdout} = await execa(command, {shell: true, localDir: '/test'});
const envPaths = stdout.split(path.delimiter).map(envPath =>
envPath.replace(/\\/g, '/').replace(/^[^/]+/, '')
);
t.true(envPaths.some(envPath => envPath === '/test/node_modules/.bin'));
});
test('input option can be a String', async t => {
const {stdout} = await execa('stdin', {input: 'foobar'});
t.is(stdout, 'foobar');
});
test('input option can be a Buffer', async t => {
const {stdout} = await execa('stdin', {input: 'testing12'});
t.is(stdout, 'testing12');
});
test('input can be a Stream', async t => {
const s = new stream.PassThrough();
s.write('howdy');
s.end();
const {stdout} = await execa('stdin', {input: s});
t.is(stdout, 'howdy');
});
test('you can write to child.stdin', async t => {
const child = execa('stdin');
child.stdin.end('unicorns');
t.is((await child).stdout, 'unicorns');
});
test('input option can be a String - sync', t => {
const {stdout} = execa.sync('stdin', {input: 'foobar'});
t.is(stdout, 'foobar');
});
test('input option can be a Buffer - sync', t => {
const {stdout} = execa.sync('stdin', {input: Buffer.from('testing12', 'utf8')});
t.is(stdout, 'testing12');
});
test('stdin errors are handled', async t => {
const child = execa('noop');
child.stdin.emit('error', new Error('test'));
await t.throwsAsync(child, /test/);
});
test('child process errors are handled', async t => {
const child = execa('noop');
child.emit('error', new Error('test'));
await t.throwsAsync(child, /test/);
});
test('opts.stdout:ignore - stdout will not collect data', async t => {
const {stdout} = await execa('stdin', {
input: 'hello',
stdio: [undefined, 'ignore', undefined]
});
t.is(stdout, undefined);
});
test('helpful error trying to provide an input stream in sync mode', t => {
t.throws(
() => {
execa.sync('stdin', {input: new stream.PassThrough()});
},
/The `input` option cannot be a stream in sync mode/
);
});
test('child process errors rejects promise right away', async t => {
const child = execa('forever');
child.emit('error', new Error('test'));
await t.throwsAsync(child, /test/);
});
test('execa() returns a promise with kill() and pid', t => {
const {kill, pid} = execa('noop', ['foo']);
t.is(typeof kill, 'function');
t.is(typeof pid, 'number');
});
test('child_process.spawn() propagated errors have correct shape', t => {
const cp = execa('noop', {uid: -1});
t.notThrows(() => {
cp.catch(() => {});
cp.unref();
cp.on('error', () => {});
});
});
test('child_process.spawn() errors are propagated', async t => {
const {failed} = await t.throwsAsync(execa('noop', {uid: -1}));
t.true(failed);
});
test('child_process.spawnSync() errors are propagated with a correct shape', t => {
const {failed} = t.throws(() => {
execa.sync('noop', {timeout: -1});
});
t.true(failed);
});
test('maxBuffer affects stdout', async t => {
await t.notThrowsAsync(execa('max-buffer', ['stdout', '10'], {maxBuffer: 10}));
const {stdout, all} = await t.throwsAsync(execa('max-buffer', ['stdout', '11'], {maxBuffer: 10}), /max-buffer stdout/);
t.is(stdout, '.'.repeat(10));
t.is(all, '.'.repeat(10));
});
test('maxBuffer affects stderr', async t => {
await t.notThrowsAsync(execa('max-buffer', ['stderr', '10'], {maxBuffer: 10}));
const {stderr, all} = await t.throwsAsync(execa('max-buffer', ['stderr', '11'], {maxBuffer: 10}), /max-buffer stderr/);
t.is(stderr, '.'.repeat(10));
t.is(all, '.'.repeat(10));
});
test('do not buffer stdout when `buffer` set to `false`', async t => {
const promise = execa('max-buffer', ['stdout', '10'], {buffer: false});
const [result, stdout] = await Promise.all([
promise,
getStream(promise.stdout),
getStream(promise.all)
]);
t.is(result.stdout, undefined);
t.is(stdout, '.........\n');
});
test('do not buffer stderr when `buffer` set to `false`', async t => {
const promise = execa('max-buffer', ['stderr', '10'], {buffer: false});
const [result, stderr] = await Promise.all([
promise,
getStream(promise.stderr),
getStream(promise.all)
]);
t.is(result.stderr, undefined);
t.is(stderr, '.........\n');
});
test('allow unknown exit code', async t => {
const {exitCode, exitCodeName} = await t.throwsAsync(execa('exit', ['255']), {message: /exit code 255 \(Unknown system error -255\)/});
t.is(exitCode, 255);
t.is(exitCodeName, 'Unknown system error -255');
});
test('execa() does not return code and failed properties on success', async t => {
const {exitCode, exitCodeName, failed} = await execa('noop', ['foo']);
t.is(exitCode, 0);
t.is(exitCodeName, 'SUCCESS');
t.false(failed);
});
test('execa() returns code and failed properties', async t => {
const {exitCode, exitCodeName, failed} = await t.throwsAsync(execa('exit', ['2']), {message: getExitRegExp('2')});
t.is(exitCode, 2);
const expectedName = process.platform === 'win32' ? 'Unknown system error -2' : 'ENOENT';
t.is(exitCodeName, expectedName);
t.true(failed);
});
test('use relative path with \'..\' chars', async t => {
const pathViaParentDir = path.join('..', path.basename(__dirname), 'fixtures', 'noop');
const {stdout} = await execa(pathViaParentDir, ['foo']);
t.is(stdout, 'foo');
});
if (process.platform !== 'win32') {
test('execa() rejects if running non-executable', async t => {
const cp = execa('non-executable');
await t.throwsAsync(cp);
});
test('execa() rejects with correct error and doesn\'t throw if running non-executable with input', async t => {
await t.throwsAsync(execa('non-executable', {input: 'Hey!'}), /EACCES/);
});
}
test('error.killed is true if process was killed directly', async t => {
const cp = execa('forever');
cp.kill();
const {killed} = await t.throwsAsync(cp, {message: /was killed with SIGTERM/});
t.true(killed);
});
test('error.killed is false if process was killed indirectly', async t => {
const cp = execa('forever');
process.kill(cp.pid, 'SIGINT');
// `process.kill()` is emulated by Node.js on Windows
const message = process.platform === 'win32' ? /failed with exit code 1/ : /was killed with SIGINT/;
const {killed} = await t.throwsAsync(cp, {message});
t.false(killed);
});
test('result.killed is false if not killed', async t => {
const {killed} = await execa('noop');
t.false(killed);
});
test('result.killed is false if not killed, in sync mode', t => {
const {killed} = execa.sync('noop');
t.false(killed);
});
test('result.killed is false on process error', async t => {
const {killed} = await t.throwsAsync(execa('wrong command'));
t.false(killed);
});
test('result.killed is false on process error, in sync mode', t => {
const {killed} = t.throws(() => {
execa.sync('wrong command');
});
t.false(killed);
});
if (process.platform === 'darwin') {
test.cb('sanity check: child_process.exec also has killed.false if killed indirectly', t => {
const {pid} = childProcess.exec('forever', error => {
t.truthy(error);
t.false(error.killed);
t.end();
});
process.kill(pid, 'SIGINT');
});
}
if (process.platform !== 'win32') {
test('error.signal is SIGINT', async t => {
const cp = execa('forever');
process.kill(cp.pid, 'SIGINT');
const {signal} = await t.throwsAsync(cp, {message: /was killed with SIGINT/});
t.is(signal, 'SIGINT');
});
test('error.signal is SIGTERM', async t => {
const cp = execa('forever');
process.kill(cp.pid, 'SIGTERM');
const {signal} = await t.throwsAsync(cp, {message: /was killed with SIGTERM/});
t.is(signal, 'SIGTERM');
});
test('custom error.signal', async t => {
const {signal} = await t.throwsAsync(execa('forever', {killSignal: 'SIGHUP', timeout: 1, message: TIMEOUT_REGEXP}));
t.is(signal, 'SIGHUP');
});
}
test('result.signal is undefined for successful execution', async t => {
const {signal} = await execa('noop');
t.is(signal, undefined);
});
test('result.signal is undefined if process failed, but was not killed', async t => {
const {signal} = await t.throwsAsync(execa('exit', [2]), {message: getExitRegExp('2')});
t.is(signal, undefined);
});
async function testExitCode(t, num) {
const {exitCode} = await t.throwsAsync(execa('exit', [`${num}`]), {message: getExitRegExp(num)});
t.is(exitCode, num);
}
test('error.exitCode is 2', testExitCode, 2);
test('error.exitCode is 3', testExitCode, 3);
test('error.exitCode is 4', testExitCode, 4);
test('timeout kills the process if it times out', async t => {
const {killed, timedOut} = await t.throwsAsync(execa('forever', {timeout: 1, message: TIMEOUT_REGEXP}));
t.false(killed);
t.true(timedOut);
});
test('timeout kills the process if it times out, in sync mode', async t => {
const {killed, timedOut} = await t.throws(() => {
execa.sync('forever', {timeout: 1, message: TIMEOUT_REGEXP});
});
t.false(killed);
t.true(timedOut);
});
test('timeout does not kill the process if it does not time out', async t => {
const {timedOut} = await execa('delay', ['500'], {timeout: 1e8});
t.false(timedOut);
});
test('timedOut is false if no timeout was set', async t => {
const {timedOut} = await execa('noop');
t.false(timedOut);
});
test('timedOut will be false if no timeout was set and zero exit code in sync mode', t => {
const {timedOut} = execa.sync('noop');
t.false(timedOut);
});
async function errorMessage(t, expected, ...args) {
await t.throwsAsync(execa('exit', args), {message: expected});
}
errorMessage.title = (message, expected) => `error.message matches: ${expected}`;
test(errorMessage, /Command failed with exit code 2.*: exit 2 foo bar/, 2, 'foo', 'bar');
test(errorMessage, /Command failed with exit code 3.*: exit 3 baz quz/, 3, 'baz', 'quz');
async function command(t, expected, ...args) {
const {command: failCommand} = await t.throwsAsync(execa('fail', args));
t.is(failCommand, `fail${expected}`);
const {command} = await execa('noop', args);
t.is(command, `noop${expected}`);
}
command.title = (message, expected) => `command is: ${JSON.stringify(expected)}`;
test(command, ' foo bar', 'foo', 'bar');
test(command, ' baz quz', 'baz', 'quz');
test(command, '');
// When child process exits before parent process
async function spawnAndExit(t, cleanup, detached) {
await t.notThrowsAsync(execa('sub-process-exit', [cleanup, detached]));
}
test('spawnAndExit', spawnAndExit, false, false);
test('spawnAndExit cleanup', spawnAndExit, true, false);
test('spawnAndExit detached', spawnAndExit, false, true);
test('spawnAndExit cleanup detached', spawnAndExit, true, true);
// When parent process exits before child process
async function spawnAndKill(t, signal, cleanup, detached, isKilled) {
const subprocess = execa('sub-process', [cleanup, detached], {stdio: ['ignore', 'ignore', 'ignore', 'ipc']});
const pid = await pEvent(subprocess, 'message');
t.true(Number.isInteger(pid));
t.true(isRunning(pid));
process.kill(subprocess.pid, signal);
await t.throwsAsync(subprocess);
t.false(isRunning(subprocess.pid));
t.is(isRunning(pid), !isKilled);
if (!isKilled) {
process.kill(pid, 'SIGKILL');
}
}
// Without `options.cleanup`:
// - on Windows subprocesses are killed if `options.detached: false`, but not
// if `options.detached: true`
// - on Linux subprocesses are never killed regardless of `options.detached`
// With `options.cleanup`, subprocesses are always killed
// - `options.cleanup` with SIGKILL is a noop, since it cannot be handled
const exitIfWindows = process.platform === 'win32';
test('spawnAndKill SIGTERM', spawnAndKill, 'SIGTERM', false, false, exitIfWindows);
test('spawnAndKill SIGKILL', spawnAndKill, 'SIGKILL', false, false, exitIfWindows);
test('spawnAndKill cleanup SIGTERM', spawnAndKill, 'SIGTERM', true, false, true);
test('spawnAndKill cleanup SIGKILL', spawnAndKill, 'SIGKILL', true, false, exitIfWindows);
test('spawnAndKill detached SIGTERM', spawnAndKill, 'SIGTERM', false, true, false);
test('spawnAndKill detached SIGKILL', spawnAndKill, 'SIGKILL', false, true, false);
test('spawnAndKill cleanup detached SIGTERM', spawnAndKill, 'SIGTERM', true, true, false);
test('spawnAndKill cleanup detached SIGKILL', spawnAndKill, 'SIGKILL', true, true, false);
if (process.platform !== 'win32') {
test('write to fast-exit process', async t => {
// Try-catch here is necessary, because this test is not 100% accurate
// Sometimes process can manage to accept input before exiting
try {
await execa(`fast-exit-${process.platform}`, [], {input: 'data'});
t.pass();
} catch (error) {
t.is(error.exitCode, 32);
}
});
}
test('use environment variables by default', async t => {
const {stdout} = await execa('environment');
t.deepEqual(stdout.split('\n'), ['foo', 'undefined']);
});
test('extend environment variables by default', async t => {
const {stdout} = await execa('environment', [], {env: {BAR: 'bar'}});
t.deepEqual(stdout.split('\n'), ['foo', 'bar']);
});
test('do not extend environment with `extendEnv: false`', async t => {
const {stdout} = await execa('environment', [], {env: {BAR: 'bar', PATH: process.env.PATH}, extendEnv: false});
t.deepEqual(stdout.split('\n'), ['undefined', 'bar']);
});
test('can use `options.shell: true`', async t => {
const {stdout} = await execa('node fixtures/noop foo', {shell: true});
t.is(stdout, 'foo');
});
test('can use `options.shell: string`', async t => {
const shell = process.platform === 'win32' ? 'cmd.exe' : '/bin/bash';
const {stdout} = await execa('node fixtures/noop foo', {shell});
t.is(stdout, 'foo');
});
test('use extend environment with `extendEnv: true` and `shell: true`', async t => {
process.env.TEST = 'test';
const command = process.platform === 'win32' ? 'echo %TEST%' : 'echo $TEST';
const {stdout} = await execa(command, {shell: true, env: {}, extendEnv: true});
t.is(stdout, 'test');
delete process.env.TEST;
});
test('do not buffer when streaming', async t => {
const {stdout} = execa('max-buffer', ['stdout', '21'], {maxBuffer: 10});
const result = await getStream(stdout);
t.is(result, '....................\n');
});
test('detach child process', async t => {
const {stdout} = await execa('detach');
const pid = Number(stdout);
t.true(Number.isInteger(pid));
t.true(isRunning(pid));
process.kill(pid, 'SIGKILL');
});
// See #128
test('removes exit handler on exit', async t => {
// FIXME: This relies on `signal-exit` internals
const ee = process.__signal_exit_emitter__;
const child = execa('noop');
const listener = ee.listeners('exit').pop();
await new Promise((resolve, reject) => {
child.on('error', reject);
child.on('exit', resolve);
});
const included = ee.listeners('exit').includes(listener);
t.false(included);
});
test('promise methods are not enumerable', t => {
const descriptors = Object.getOwnPropertyDescriptors(execa('noop'));
// eslint-disable-next-line promise/prefer-await-to-then
t.false(descriptors.then.enumerable);
t.false(descriptors.catch.enumerable);
// TOOD: Remove the `if`-guard when targeting Node.js 10
if (Promise.prototype.finally) {
t.false(descriptors.finally.enumerable);
}
});
// TOOD: Remove the `if`-guard when targeting Node.js 10
if (Promise.prototype.finally) {
test('finally function is executed on success', async t => {
let isCalled = false;
const {stdout} = await execa('noop', ['foo']).finally(() => {
isCalled = true;
});
t.is(isCalled, true);
t.is(stdout, 'foo');
});
test('finally function is executed on failure', async t => {
let isError = false;
const {stdout, stderr} = await t.throwsAsync(execa('exit', ['2']).finally(() => {
isError = true;
}));
t.is(isError, true);
t.is(typeof stdout, 'string');
t.is(typeof stderr, 'string');
});
test('throw in finally function bubbles up on success', async t => {
const {message} = await t.throwsAsync(execa('noop', ['foo']).finally(() => {
throw new Error('called');
}));
t.is(message, 'called');
});
test('throw in finally bubbles up on error', async t => {
const {message} = await t.throwsAsync(execa('exit', ['2']).finally(() => {
throw new Error('called');
}));
t.is(message, 'called');
});
}
test('cancel method kills the subprocess', t => {
const subprocess = execa('node');
subprocess.cancel();
t.true(subprocess.killed);
});
test('result.isCanceled is false when spawned.cancel() isn\'t called (success)', async t => {
const {isCanceled} = await execa('noop');
t.false(isCanceled);
});
test('result.isCanceled is false when spawned.cancel() isn\'t called (failure)', async t => {
const {isCanceled} = await t.throwsAsync(execa('fail'));
t.false(isCanceled);
});
test('result.isCanceled is false when spawned.cancel() isn\'t called in sync mode (success)', t => {
const {isCanceled} = execa.sync('noop');
t.false(isCanceled);
});
test('result.isCanceled is false when spawned.cancel() isn\'t called in sync mode (failure)', t => {
const {isCanceled} = t.throws(() => {
execa.sync('fail');
});
t.false(isCanceled);
});
test('calling cancel method throws an error with message "Command was canceled"', async t => {
const subprocess = execa('noop');
subprocess.cancel();
await t.throwsAsync(subprocess, {message: /Command was canceled/});
});
test('error.isCanceled is true when cancel method is used', async t => {
const subprocess = execa('noop');
subprocess.cancel();
const {isCanceled} = await t.throwsAsync(subprocess);
t.true(isCanceled);
});
test('error.isCanceled is false when kill method is used', async t => {
const subprocess = execa('noop');
subprocess.kill();
const {isCanceled} = await t.throwsAsync(subprocess);
t.false(isCanceled);
});
test('calling cancel method twice should show the same behaviour as calling it once', async t => {
const subprocess = execa('noop');
subprocess.cancel();
subprocess.cancel();
const {isCanceled} = await t.throwsAsync(subprocess);
t.true(isCanceled);
t.true(subprocess.killed);
});
test('calling cancel method on a successfuly completed process does not make result.isCanceled true', async t => {
const subprocess = execa('noop');
const {isCanceled} = await subprocess;
subprocess.cancel();
t.false(isCanceled);
});
test('calling cancel method on a process which has been killed does not make error.isCanceled true', async t => {
const subprocess = execa('noop');
subprocess.kill();
const {isCanceled} = await t.throwsAsync(subprocess);
t.false(isCanceled);
});
test('allow commands with spaces and no array arguments', async t => {
const {stdout} = await execa('./fixtures/command with space');
t.is(stdout, '');
});
test('allow commands with spaces and array arguments', async t => {
const {stdout} = await execa('./fixtures/command with space', ['foo', 'bar']);
t.is(stdout, 'foo\nbar');
});
test('execa.command()', async t => {
const {stdout} = await execa.command('node fixtures/echo foo bar');
t.is(stdout, 'foo\nbar');
});
test('execa.command() ignores consecutive spaces', async t => {
const {stdout} = await execa.command('node fixtures/echo foo bar');
t.is(stdout, 'foo\nbar');
});
test('execa.command() allows escaping spaces in commands', async t => {
const {stdout} = await execa.command('./fixtures/command\\ with\\ space foo bar');
t.is(stdout, 'foo\nbar');
});
test('execa.command() allows escaping spaces in arguments', async t => {
const {stdout} = await execa.command('node fixtures/echo foo\\ bar');
t.is(stdout, 'foo bar');
});
test('execa.command() escapes other whitespaces', async t => {
const {stdout} = await execa.command('node fixtures/echo foo\tbar');
t.is(stdout, 'foo\tbar');
});
test('execa.command() trims', async t => {
const {stdout} = await execa.command(' node fixtures/echo foo bar ');
t.is(stdout, 'foo\nbar');
});
test('execa.command.sync()', t => {
const {stdout} = execa.commandSync('node fixtures/echo foo bar');
t.is(stdout, 'foo\nbar');
});