-
-
Notifications
You must be signed in to change notification settings - Fork 224
/
test.js
591 lines (471 loc) · 17.7 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
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 delay from 'delay';
import tempfile from 'tempfile';
import execa from '.';
process.env.PATH = path.join(__dirname, 'fixtures') + path.delimiter + process.env.PATH;
process.env.FOO = 'foo';
const NO_NEWLINES_REGEXP = /^[^\n]*$/;
const STDERR_STDOUT_REGEXP = /stderr[^]*stdout/;
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('buffer', async t => {
const {stdout} = await execa('noop', ['foo'], {encoding: null});
t.true(Buffer.isBuffer(stdout));
t.is(stdout.toString(), 'foo');
});
test('execa.stdout()', async t => {
const stdout = await execa.stdout('noop', ['foo']);
t.is(stdout, 'foo');
});
test('execa.stderr()', async t => {
const stderr = await execa.stderr('noop-err', ['foo']);
t.is(stderr, 'foo');
});
test('stdout/stderr available on errors', async t => {
const err = await t.throwsAsync(execa('exit', ['2']), {message: getExitRegExp('2')});
t.is(typeof err.stdout, 'string');
t.is(typeof err.stderr, 'string');
});
test('include stdout and stderr in errors for improved debugging', async t => {
await t.throwsAsync(execa('fixtures/error-message.js'), {message: STDERR_STDOUT_REGEXP, code: 1});
});
test('do not include in errors when `stdio` is set to `inherit`', async t => {
await t.throwsAsync(execa('fixtures/error-message.js', {stdio: 'inherit'}), {message: NO_NEWLINES_REGEXP});
});
test('do not include `stderr` and `stdout` in errors when set to `inherit`', async t => {
await t.throwsAsync(execa('fixtures/error-message.js', {stdout: 'inherit', stderr: 'inherit'}), {message: NO_NEWLINES_REGEXP});
});
test('do not include `stderr` and `stdout` in errors when `stdio` is set to `inherit`', async t => {
await t.throwsAsync(execa('fixtures/error-message.js', {stdio: [null, 'inherit', 'inherit']}), {message: NO_NEWLINES_REGEXP});
});
test('do not include `stdout` in errors when set to `inherit`', async t => {
const err = await t.throwsAsync(execa('fixtures/error-message.js', {stdout: 'inherit'}), {message: /stderr/});
t.notRegex(err.message, /stdout/);
});
test('do not include `stderr` in errors when set to `inherit`', async t => {
const err = await t.throwsAsync(execa('fixtures/error-message.js', {stderr: 'inherit'}), {message: /stdout/});
t.notRegex(err.message, /stderr/);
});
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.shell()', async t => {
const {stdout} = await execa.shell('node fixtures/noop foo');
t.is(stdout, 'foo');
});
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' ? /'foo' is not recognized as an internal or external command/ : /spawnSync foo ENOENT/);
});
test('execa.sync() includes stdout and stderr in errors for improved debugging', t => {
t.throws(() => execa.sync('node', ['fixtures/error-message.js']), {message: STDERR_STDOUT_REGEXP, code: 1});
});
test('skip throwing when using reject option in execa.sync()', t => {
const err = execa.sync('node', ['fixtures/error-message.js'], {reject: false});
t.is(typeof err.stdout, 'string');
t.is(typeof err.stderr, 'string');
});
test('execa.shellSync()', t => {
const {stdout} = execa.shellSync('node fixtures/noop foo');
t.is(stdout, 'foo');
});
test('execa.shellSync() includes stdout and stderr in errors for improved debugging', t => {
t.throws(() => execa.shellSync('node fixtures/error-message.js'), {message: STDERR_STDOUT_REGEXP, code: 1});
});
test('skip throwing when using reject option in execa.shellSync()', t => {
const err = execa.shellSync('node fixtures/error-message.js', {reject: false});
t.is(typeof err.stdout, 'string');
t.is(typeof err.stderr, 'string');
});
test('stripEof option (legacy)', async t => {
const {stdout} = await execa('noop', ['foo'], {stripEof: false});
t.is(stdout, 'foo\n');
});
test('stripFinalNewline option', async t => {
const {stdout} = await execa('noop', ['foo'], {stripFinalNewline: false});
t.is(stdout, 'foo\n');
});
test.serial('preferLocal option', async t => {
t.true((await execa('cat-names')).stdout.length > 2);
if (process.platform === 'win32') {
// TODO: figure out how to make the below not hang on Windows
return;
}
// Account for npm adding local binaries to the PATH
const _path = process.env.PATH;
process.env.PATH = '';
await t.throwsAsync(execa('cat-names', {preferLocal: false}), /spawn .* ENOENT/);
process.env.PATH = _path;
});
test.serial('localDir option', async t => {
const cwd = 'fixtures/local-dir';
const bin = path.resolve(cwd, 'node_modules/.bin/self-path');
await execa('npm', ['install', '--no-package-lock'], {cwd});
const {stdout} = await execa(bin, {localDir: cwd});
t.is(path.relative(cwd, stdout), path.normalize('node_modules/self-path'));
});
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('opts.stdout:ignore - stdout will not collect data', async t => {
const {stdout} = await execa('stdin', {
input: 'hello',
stdio: [null, 'ignore', null]
});
t.is(stdout, null);
});
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('execa() returns a promise with kill() and pid', t => {
const promise = execa('noop', ['foo']);
t.is(typeof promise.kill, 'function');
t.is(typeof promise.pid, 'number');
});
test('maxBuffer affects stdout', async t => {
await t.throwsAsync(execa('max-buffer', ['stdout', '11'], {maxBuffer: 10}), /stdout maxBuffer exceeded/);
await t.notThrowsAsync(execa('max-buffer', ['stdout', '10'], {maxBuffer: 10}));
});
test('maxBuffer affects stderr', async t => {
await t.throwsAsync(execa('max-buffer', ['stderr', '13'], {maxBuffer: 12}), /stderr maxBuffer exceeded/);
await t.notThrowsAsync(execa('max-buffer', ['stderr', '12'], {maxBuffer: 12}));
});
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)
]);
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)
]);
t.is(result.stderr, undefined);
t.is(stderr, '.........\n');
});
test('skip throwing when using reject option', async t => {
const error = await execa('exit', ['2'], {reject: false});
t.is(typeof error.stdout, 'string');
t.is(typeof error.stderr, 'string');
});
test('allow unknown exit code', async t => {
await t.throwsAsync(execa('exit', ['255']), {message: /exit code 255 \(Unknown system error -255\)/});
});
test('execa() returns code and failed properties', async t => {
const {code, failed} = await execa('noop', ['foo']);
const error = await t.throwsAsync(execa('exit', ['2']), {code: 2, message: getExitRegExp('2')});
t.is(code, 0);
t.false(failed);
t.true(error.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('error.killed is true if process was killed directly', async t => {
const cp = execa('forever');
setTimeout(() => {
cp.kill();
}, 100);
const error = await t.throwsAsync(cp, {message: /was killed with SIGTERM/});
t.true(error.killed);
});
// TODO: Should this really be the case, or should we improve on child_process?
test('error.killed is false if process was killed indirectly', async t => {
const cp = execa('forever');
setTimeout(() => {
process.kill(cp.pid, 'SIGINT');
}, 100);
// `process.kill()` is emulated by Node.js on Windows
const message = process.platform === 'win32' ? /failed with exit code 1/ : /was killed with SIGINT/;
const error = await t.throwsAsync(cp, {message});
t.false(error.killed);
});
if (process.platform === 'darwin') {
test.cb('sanity check: child_process.exec also has killed.false if killed indirectly', t => {
const cp = childProcess.exec('forever', error => {
t.truthy(error);
t.false(error.killed);
t.end();
});
setTimeout(() => {
process.kill(cp.pid, 'SIGINT');
}, 100);
});
}
if (process.platform !== 'win32') {
test('error.signal is SIGINT', async t => {
const cp = execa('forever');
setTimeout(() => {
process.kill(cp.pid, 'SIGINT');
}, 100);
const error = await t.throwsAsync(cp, {message: /was killed with SIGINT/});
t.is(error.signal, 'SIGINT');
});
test('error.signal is SIGTERM', async t => {
const cp = execa('forever');
setTimeout(() => {
process.kill(cp.pid, 'SIGTERM');
}, 100);
const error = await t.throwsAsync(cp, {message: /was killed with SIGTERM/});
t.is(error.signal, 'SIGTERM');
});
test('custom error.signal', async t => {
const error = await t.throwsAsync(execa('delay', ['3000', '0'], {killSignal: 'SIGHUP', timeout: 1500, message: TIMEOUT_REGEXP}));
t.is(error.signal, 'SIGHUP');
});
}
test('result.signal is null for successful execution', async t => {
t.is((await execa('noop')).signal, null);
});
test('result.signal is null if process failed, but was not killed', async t => {
const error = await t.throwsAsync(execa('exit', [2]), {message: getExitRegExp('2')});
t.is(error.signal, null);
});
async function code(t, num) {
await t.throwsAsync(execa('exit', [`${num}`]), {code: num, message: getExitRegExp(num)});
}
test('error.code is 2', code, 2);
test('error.code is 3', code, 3);
test('error.code is 4', code, 4);
test('timeout will kill the process early', async t => {
const error = await t.throwsAsync(execa('delay', ['60000', '0'], {timeout: 1500, message: TIMEOUT_REGEXP}));
t.true(error.timedOut);
t.not(error.code, 22);
});
test('timeout will not kill the process early', async t => {
const error = await t.throwsAsync(execa('delay', ['3000', '22'], {timeout: 30000}), {code: 22, message: getExitRegExp('22')});
t.false(error.timedOut);
});
test('timedOut will be false if no timeout was set and zero exit code', async t => {
const result = await execa('delay', ['1000', '0']);
t.false(result.timedOut);
});
test('timedOut will be false if no timeout was set and non-zero exit code', async t => {
const error = await t.throwsAsync(execa('delay', ['1000', '3']), {message: getExitRegExp('3')});
t.false(error.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 cmd(t, expected, ...args) {
const error = await t.throwsAsync(execa('fail', args));
t.is(error.cmd, `fail${expected}`);
const result = await execa('noop', args);
t.is(result.cmd, `noop${expected}`);
}
cmd.title = (message, expected) => `cmd is: ${JSON.stringify(expected)}`;
test(cmd, ' foo bar', 'foo', 'bar');
test(cmd, ' baz quz', 'baz', 'quz');
test(cmd, '');
async function spawnAndKill(t, signal, cleanup) {
const name = cleanup ? 'sub-process' : 'sub-process-false';
const cp = execa(name);
let pid;
cp.stdout.setEncoding('utf8');
cp.stdout.on('data', chunk => {
pid = parseInt(chunk, 10);
t.is(typeof pid, 'number');
setTimeout(() => {
process.kill(cp.pid, signal);
}, 100);
});
await t.throwsAsync(cp);
// Give everybody some time to breath and kill things
await delay(200);
t.false(isRunning(cp.pid));
t.is(isRunning(pid), !cleanup);
}
test('cleanup - SIGINT', spawnAndKill, 'SIGINT', true);
test('cleanup - SIGKILL', spawnAndKill, 'SIGTERM', true);
if (process.platform !== 'win32') {
// On Windows the subprocesses are actually always killed
test('cleanup false - SIGINT', spawnAndKill, 'SIGTERM', false);
test('cleanup false - SIGKILL', spawnAndKill, 'SIGKILL', false);
}
test('execa.shell() supports the `shell` option', async t => {
const {stdout} = await execa.shell('node fixtures/noop foo', {
shell: process.platform === 'win32' ? 'cmd.exe' : '/bin/bash'
});
t.is(stdout, 'foo');
});
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.code, 32);
}
});
}
test('use environment variables by default', async t => {
const result = await execa.stdout('environment');
t.deepEqual(result.split('\n'), [
'foo',
'undefined'
]);
});
test('extend environment variables by default', async t => {
const result = await execa.stdout('environment', [], {env: {BAR: 'bar'}});
t.deepEqual(result.split('\n'), [
'foo',
'bar'
]);
});
test('do not extend environment with `extendEnv: false`', async t => {
const result = await execa.stdout('environment', [], {env: {BAR: 'bar', PATH: process.env.PATH}, extendEnv: false});
t.deepEqual(result.split('\n'), [
'undefined',
'bar'
]);
});
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.stdout(command, {shell: true, env: {}, extendEnv: true});
t.is(stdout, 'test');
delete process.env.TEST;
});
test('do not buffer when streaming', async t => {
const result = await getStream(execa('max-buffer', ['stdout', '21'], {maxBuffer: 10}).stdout);
t.is(result, '....................\n');
});
test('detach child process', async t => {
const file = tempfile('.txt');
await execa('detach', [file]);
await delay(5000);
t.is(fs.readFileSync(file, 'utf8'), 'foo\n');
});
// 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);
});
// TOOD: Remove the `if`-guard when targeting Node.js 10
if (Promise.prototype.finally) {
test('finally function is executed on success', async t => {
let called = false;
const {stdout} = await execa('noop', ['foo']).finally(() => {
called = true;
});
t.is(called, true);
t.is(stdout, 'foo');
});
test('finally function is executed on failure', async t => {
let called = false;
const err = await t.throwsAsync(execa('exit', ['2']).finally(() => {
called = true;
}));
t.is(called, true);
t.is(typeof err.stdout, 'string');
t.is(typeof err.stderr, 'string');
});
test('throw in finally function bubbles up on success', async t => {
const result = await t.throwsAsync(execa('noop', ['foo']).finally(() => {
throw new Error('called');
}));
t.is(result.message, 'called');
});
test('throw in finally bubbles up on error', async t => {
const result = await t.throwsAsync(execa('exit', ['2']).finally(() => {
throw new Error('called');
}));
t.is(result.message, 'called');
});
}
test('cancel method kills the spawned process', t => {
const spawned = execa('node');
spawned.cancel();
t.true(spawned.killed);
});
test('calling cancel method makes result.cancel true', async t => {
const spawned = execa('ls');
spawned.cancel();
const result = await spawned;
t.true(result.canceled);
});
test('result.cancel is false when spawned.cancel isn\'t called', async t => {
const spawned = execa('noop');
const result = await spawned;
t.false(result.canceled);
});