@@ -14,6 +14,11 @@ process.env.FOO = 'foo';
14
14
15
15
const NO_NEWLINES_REGEXP = / ^ [ ^ \n ] * $ / ;
16
16
const STDERR_STDOUT_REGEXP = / s t d e r r [ ^ ] * s t d o u t / ;
17
+ const TIMEOUT_REGEXP = / t i m e d o u t a f t e r / ;
18
+
19
+ function getExitRegExp ( exitMessage ) {
20
+ return new RegExp ( `failed with exit code ${ exitMessage } ` ) ;
21
+ }
17
22
18
23
test ( 'execa()' , async t => {
19
24
const { stdout} = await m ( 'noop' , [ 'foo' ] ) ;
@@ -45,7 +50,7 @@ test('execa.stderr()', async t => {
45
50
} ) ;
46
51
47
52
test ( 'stdout/stderr available on errors' , async t => {
48
- const err = await t . throwsAsync ( m ( 'exit' , [ '2' ] ) ) ;
53
+ const err = await t . throwsAsync ( m ( 'exit' , [ '2' ] ) , { message : getExitRegExp ( '2' ) } ) ;
49
54
t . is ( typeof err . stdout , 'string' ) ;
50
55
t . is ( typeof err . stderr , 'string' ) ;
51
56
} ) ;
@@ -99,7 +104,7 @@ test('execa.sync()', t => {
99
104
} ) ;
100
105
101
106
test ( 'execa.sync() throws error if written to stderr' , t => {
102
- t . throws ( ( ) => m . sync ( 'foo' ) , process . platform === 'win32' ? / ' f o o ' i s n o t r e c o g n i z e d a s a n i n t e r n a l o r e x t e r n a l c o m m a n d / : ' spawnSync foo ENOENT' ) ;
107
+ t . throws ( ( ) => m . sync ( 'foo' ) , process . platform === 'win32' ? / ' f o o ' i s n o t r e c o g n i z e d a s a n i n t e r n a l o r e x t e r n a l c o m m a n d / : / s p a w n S y n c f o o E N O E N T / ) ;
103
108
} ) ;
104
109
105
110
test ( 'execa.sync() includes stdout and stderr in errors for improved debugging' , t => {
@@ -256,9 +261,13 @@ test('skip throwing when using reject option', async t => {
256
261
t . is ( typeof error . stderr , 'string' ) ;
257
262
} ) ;
258
263
264
+ test ( 'allow unknown exit code' , async t => {
265
+ await t . throwsAsync ( m ( 'exit' , [ '255' ] ) , { message : / e x i t c o d e 2 5 5 \( U n k n o w n s y s t e m e r r o r - 2 5 5 \) / } ) ;
266
+ } ) ;
267
+
259
268
test ( 'execa() returns code and failed properties' , async t => {
260
269
const { code, failed} = await m ( 'noop' , [ 'foo' ] ) ;
261
- const error = await t . throwsAsync ( m ( 'exit' , [ '2' ] ) , { code : 2 } ) ;
270
+ const error = await t . throwsAsync ( m ( 'exit' , [ '2' ] ) , { code : 2 , message : getExitRegExp ( '2' ) } ) ;
262
271
t . is ( code , 0 ) ;
263
272
t . false ( failed ) ;
264
273
t . true ( error . failed ) ;
@@ -284,7 +293,7 @@ test('error.killed is true if process was killed directly', async t => {
284
293
cp . kill ( ) ;
285
294
} , 100 ) ;
286
295
287
- const error = await t . throwsAsync ( cp ) ;
296
+ const error = await t . throwsAsync ( cp , { message : / w a s k i l l e d w i t h S I G T E R M / } ) ;
288
297
t . true ( error . killed ) ;
289
298
} ) ;
290
299
@@ -296,7 +305,9 @@ test('error.killed is false if process was killed indirectly', async t => {
296
305
process . kill ( cp . pid , 'SIGINT' ) ;
297
306
} , 100 ) ;
298
307
299
- const error = await t . throwsAsync ( cp ) ;
308
+ // `process.kill()` is emulated by Node.js on Windows
309
+ const message = process . platform === 'win32' ? / f a i l e d w i t h e x i t c o d e 1 / : / w a s k i l l e d w i t h S I G I N T / ;
310
+ const error = await t . throwsAsync ( cp , { message} ) ;
300
311
t . false ( error . killed ) ;
301
312
} ) ;
302
313
@@ -322,7 +333,7 @@ if (process.platform !== 'win32') {
322
333
process . kill ( cp . pid , 'SIGINT' ) ;
323
334
} , 100 ) ;
324
335
325
- const error = await t . throwsAsync ( cp ) ;
336
+ const error = await t . throwsAsync ( cp , { message : / w a s k i l l e d w i t h S I G I N T / } ) ;
326
337
t . is ( error . signal , 'SIGINT' ) ;
327
338
} ) ;
328
339
@@ -333,12 +344,12 @@ if (process.platform !== 'win32') {
333
344
process . kill ( cp . pid , 'SIGTERM' ) ;
334
345
} , 100 ) ;
335
346
336
- const error = await t . throwsAsync ( cp ) ;
347
+ const error = await t . throwsAsync ( cp , { message : / w a s k i l l e d w i t h S I G T E R M / } ) ;
337
348
t . is ( error . signal , 'SIGTERM' ) ;
338
349
} ) ;
339
350
340
351
test ( 'custom error.signal' , async t => {
341
- const error = await t . throwsAsync ( m ( 'delay' , [ '3000' , '0' ] , { killSignal : 'SIGHUP' , timeout : 1500 } ) ) ;
352
+ const error = await t . throwsAsync ( m ( 'delay' , [ '3000' , '0' ] , { killSignal : 'SIGHUP' , timeout : 1500 , message : TIMEOUT_REGEXP } ) ) ;
342
353
t . is ( error . signal , 'SIGHUP' ) ;
343
354
} ) ;
344
355
}
@@ -348,27 +359,27 @@ test('result.signal is null for successful execution', async t => {
348
359
} ) ;
349
360
350
361
test ( 'result.signal is null if process failed, but was not killed' , async t => {
351
- const error = await t . throwsAsync ( m ( 'exit' , [ 2 ] ) ) ;
362
+ const error = await t . throwsAsync ( m ( 'exit' , [ 2 ] ) , { message : getExitRegExp ( '2' ) } ) ;
352
363
t . is ( error . signal , null ) ;
353
364
} ) ;
354
365
355
366
async function code ( t , num ) {
356
- await t . throwsAsync ( m ( 'exit' , [ `${ num } ` ] ) , { code : num } ) ;
367
+ await t . throwsAsync ( m ( 'exit' , [ `${ num } ` ] ) , { code : num , message : getExitRegExp ( num ) } ) ;
357
368
}
358
369
359
370
test ( 'error.code is 2' , code , 2 ) ;
360
371
test ( 'error.code is 3' , code , 3 ) ;
361
372
test ( 'error.code is 4' , code , 4 ) ;
362
373
363
374
test ( 'timeout will kill the process early' , async t => {
364
- const error = await t . throwsAsync ( m ( 'delay' , [ '60000' , '0' ] , { timeout : 1500 } ) ) ;
375
+ const error = await t . throwsAsync ( m ( 'delay' , [ '60000' , '0' ] , { timeout : 1500 , message : TIMEOUT_REGEXP } ) ) ;
365
376
366
377
t . true ( error . timedOut ) ;
367
378
t . not ( error . code , 22 ) ;
368
379
} ) ;
369
380
370
381
test ( 'timeout will not kill the process early' , async t => {
371
- const error = await t . throwsAsync ( m ( 'delay' , [ '3000' , '22' ] , { timeout : 30000 } ) , { code : 22 } ) ;
382
+ const error = await t . throwsAsync ( m ( 'delay' , [ '3000' , '22' ] , { timeout : 30000 } ) , { code : 22 , message : getExitRegExp ( '22' ) } ) ;
372
383
t . false ( error . timedOut ) ;
373
384
} ) ;
374
385
@@ -378,7 +389,7 @@ test('timedOut will be false if no timeout was set and zero exit code', async t
378
389
} ) ;
379
390
380
391
test ( 'timedOut will be false if no timeout was set and non-zero exit code' , async t => {
381
- const error = await t . throwsAsync ( m ( 'delay' , [ '1000' , '3' ] ) ) ;
392
+ const error = await t . throwsAsync ( m ( 'delay' , [ '1000' , '3' ] ) , { message : getExitRegExp ( '3' ) } ) ;
382
393
t . false ( error . timedOut ) ;
383
394
} ) ;
384
395
@@ -388,8 +399,8 @@ async function errorMessage(t, expected, ...args) {
388
399
389
400
errorMessage . title = ( message , expected ) => `error.message matches: ${ expected } ` ;
390
401
391
- test ( errorMessage , / C o m m a n d f a i l e d : e x i t 2 f o o b a r / , 2 , 'foo' , 'bar' ) ;
392
- test ( errorMessage , / C o m m a n d f a i l e d : e x i t 3 b a z q u z / , 3 , 'baz' , 'quz' ) ;
402
+ test ( errorMessage , / C o m m a n d f a i l e d w i t h e x i t c o d e 2 . * : e x i t 2 f o o b a r / , 2 , 'foo' , 'bar' ) ;
403
+ test ( errorMessage , / C o m m a n d f a i l e d w i t h e x i t c o d e 3 . * : e x i t 3 b a z q u z / , 3 , 'baz' , 'quz' ) ;
393
404
394
405
async function cmd ( t , expected , ...args ) {
395
406
const error = await t . throwsAsync ( m ( 'fail' , args ) ) ;
@@ -453,7 +464,7 @@ if (process.platform !== 'win32') {
453
464
await m ( `fast-exit-${ process . platform } ` , [ ] , { input : 'data' } ) ;
454
465
t . pass ( ) ;
455
466
} catch ( error ) {
456
- t . is ( error . code , 'EPIPE' ) ;
467
+ t . is ( error . code , 32 ) ;
457
468
}
458
469
} ) ;
459
470
}
0 commit comments