22
22
'use strict' ;
23
23
const common = require ( '../common' ) ;
24
24
const assert = require ( 'assert' ) ;
25
-
26
- let returns = 0 ;
25
+ const { spawn } = require ( 'child_process' ) ;
27
26
28
27
/*
29
28
Spawns 'pwd' with given options, then test
30
- - whether the exit code equals forCode,
31
- - optionally whether the stdout result matches forData
32
- (after removing trailing whitespace)
29
+ - whether the exit code equals expectCode,
30
+ - optionally whether the trimmed stdout result matches expectData
33
31
*/
34
- function testCwd ( options , forCode , forData ) {
35
- let data = '' ;
36
-
37
- const child = common . spawnPwd ( options ) ;
32
+ function testCwd ( options , expectCode = 0 , expectData ) {
33
+ const child = spawn ( ...common . pwdCommand , options ) ;
38
34
39
35
child . stdout . setEncoding ( 'utf8' ) ;
40
36
37
+ // No need to assert callback since `data` is asserted.
38
+ let data = '' ;
41
39
child . stdout . on ( 'data' , function ( chunk ) {
42
40
data += chunk ;
43
41
} ) ;
44
42
43
+ // Can't assert callback, as stayed in to API:
44
+ // _The 'exit' event may or may not fire after an error has occurred._
45
45
child . on ( 'exit' , function ( code , signal ) {
46
- assert . strictEqual ( forCode , code ) ;
47
- } ) ;
48
-
49
- child . on ( 'close' , function ( ) {
50
- forData && assert . strictEqual ( forData , data . replace ( / [ \s \r \n ] + $ / , '' ) ) ;
51
- returns -- ;
46
+ assert . strictEqual ( expectCode , code ) ;
52
47
} ) ;
53
48
54
- returns ++ ;
49
+ child . on ( 'close' , common . mustCall ( function ( ) {
50
+ expectData && assert . strictEqual ( data . trim ( ) , expectData ) ;
51
+ } ) ) ;
55
52
56
53
return child ;
57
54
}
58
55
59
- // Assume these exist, and 'pwd' gives us the right directory back
60
- testCwd ( { cwd : common . rootDir } , 0 , common . rootDir ) ;
61
- if ( common . isWindows ) {
62
- testCwd ( { cwd : process . env . windir } , 0 , process . env . windir ) ;
63
- } else {
64
- testCwd ( { cwd : '/dev' } , 0 , '/dev' ) ;
65
- }
66
56
67
57
// Assume does-not-exist doesn't exist, expect exitCode=-1 and errno=ENOENT
68
58
{
@@ -72,15 +62,12 @@ if (common.isWindows) {
72
62
} ) ) ;
73
63
}
74
64
75
- // Spawn() shouldn't try to chdir() so this should just work
76
- testCwd ( undefined , 0 ) ;
77
- testCwd ( { } , 0 ) ;
78
- testCwd ( { cwd : '' } , 0 ) ;
79
- testCwd ( { cwd : undefined } , 0 ) ;
80
- testCwd ( { cwd : null } , 0 ) ;
65
+ // Assume these exist, and 'pwd' gives us the right directory back
66
+ testCwd ( { cwd : common . rootDir } , 0 , common . rootDir ) ;
67
+ const shouldExistDir = common . isWindows ? process . env . windir : '/dev' ;
68
+ testCwd ( { cwd : shouldExistDir } , 0 , shouldExistDir ) ;
81
69
82
- // Check whether all tests actually returned
83
- assert . notStrictEqual ( returns , 0 ) ;
84
- process . on ( 'exit' , function ( ) {
85
- assert . strictEqual ( returns , 0 ) ;
86
- } ) ;
70
+ // Spawn() shouldn't try to chdir() to invalid arg, so this should just work
71
+ testCwd ( { cwd : '' } ) ;
72
+ testCwd ( { cwd : undefined } ) ;
73
+ testCwd ( { cwd : null } ) ;
0 commit comments