2222'use strict' ;
2323const common = require ( '../common' ) ;
2424const assert = require ( 'assert' ) ;
25-
26- let returns = 0 ;
25+ const { spawn } = require ( 'child_process' ) ;
2726
2827/*
2928 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
3331*/
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 ) ;
3834
3935 child . stdout . setEncoding ( 'utf8' ) ;
4036
37+ // No need to assert callback since `data` is asserted.
38+ let data = '' ;
4139 child . stdout . on ( 'data' , function ( chunk ) {
4240 data += chunk ;
4341 } ) ;
4442
43+ // Can't assert callback, as stayed in to API:
44+ // _The 'exit' event may or may not fire after an error has occurred._
4545 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 ) ;
5247 } ) ;
5348
54- returns ++ ;
49+ child . on ( 'close' , common . mustCall ( function ( ) {
50+ expectData && assert . strictEqual ( data . trim ( ) , expectData ) ;
51+ } ) ) ;
5552
5653 return child ;
5754}
5855
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- }
6656
6757// Assume does-not-exist doesn't exist, expect exitCode=-1 and errno=ENOENT
6858{
@@ -72,15 +62,12 @@ if (common.isWindows) {
7262 } ) ) ;
7363}
7464
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 ) ;
8169
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