@@ -20,6 +20,9 @@ const BIN = join(ROOT, 'bin')
20
20
const SHIMS = readNonJsFiles ( BIN )
21
21
const NODE_GYP = readNonJsFiles ( join ( BIN , 'node-gyp-bin' ) )
22
22
const SHIM_EXTS = [ ...new Set ( Object . keys ( SHIMS ) . map ( p => extname ( p ) ) ) ]
23
+ const PACKAGE_NAME = 'test'
24
+ const PACKAGE_VERSION = '1.0.0'
25
+ const SCRIPT_NAME = 'args.js'
23
26
24
27
t . test ( 'shim contents' , t => {
25
28
// these scripts should be kept in sync so this tests the contents of each
@@ -99,6 +102,18 @@ t.test('run shims', t => {
99
102
} ,
100
103
} ,
101
104
} ,
105
+ // test script returning all command line arguments
106
+ [ SCRIPT_NAME ] : `#!/usr/bin/env node\n\nprocess.argv.slice(2).forEach((arg) => console.log(arg))` ,
107
+ // package.json for the test script
108
+ 'package.json' : `
109
+ {
110
+ "name": "${ PACKAGE_NAME } ",
111
+ "version": "${ PACKAGE_VERSION } ",
112
+ "scripts": {
113
+ "test": "node ${ SCRIPT_NAME } "
114
+ },
115
+ "bin": "${ SCRIPT_NAME } "
116
+ }` ,
102
117
} )
103
118
104
119
// The removal of this fixture causes this test to fail when done with
@@ -112,6 +127,12 @@ t.test('run shims', t => {
112
127
// only cygwin *requires* the -l, but the others are ok with it
113
128
args . unshift ( '-l' )
114
129
}
130
+ if ( cmd . toLowerCase ( ) . endsWith ( 'powershell.exe' ) || cmd . toLowerCase ( ) . endsWith ( 'pwsh.exe' ) ) {
131
+ // pwsh *requires* the -Command, Windows PowerShell defaults to it
132
+ args . unshift ( '-Command' )
133
+ // powershell requires escaping double-quotes for this test
134
+ args = args . map ( elem => elem . replaceAll ( '"' , '\\"' ) )
135
+ }
115
136
const result = spawnSync ( `"${ cmd } "` , args , {
116
137
// don't hit the registry for the update check
117
138
env : { PATH : path , npm_config_update_notifier : 'false' } ,
@@ -162,6 +183,7 @@ t.test('run shims', t => {
162
183
163
184
const shells = Object . entries ( {
164
185
cmd : 'cmd' ,
186
+ powershell : 'powershell' ,
165
187
pwsh : 'pwsh' ,
166
188
git : join ( ProgramFiles , 'Git' , 'bin' , 'bash.exe' ) ,
167
189
'user git' : join ( ProgramFiles , 'Git' , 'usr' , 'bin' , 'bash.exe' ) ,
@@ -216,7 +238,7 @@ t.test('run shims', t => {
216
238
}
217
239
} )
218
240
219
- const matchCmd = ( t , cmd , bin , match ) => {
241
+ const matchCmd = ( t , cmd , bin , match , params , expected ) => {
220
242
const args = [ ]
221
243
const opts = { }
222
244
@@ -227,25 +249,40 @@ t.test('run shims', t => {
227
249
case 'bash.exe' :
228
250
args . push ( bin )
229
251
break
252
+ case 'powershell.exe' :
230
253
case 'pwsh.exe' :
231
254
args . push ( `${ bin } .ps1` )
232
255
break
233
256
default :
234
257
throw new Error ( 'unknown shell' )
235
258
}
236
259
237
- const isNpm = bin === 'npm'
238
- const result = spawnPath ( cmd , [ ...args , isNpm ? 'help' : '--version' ] , opts )
260
+ const result = spawnPath ( cmd , [ ...args , ...params ] , opts )
261
+
262
+ // skip the first 3 lines of "npm test" to get the actual script output
263
+ if ( params [ 0 ] . startsWith ( 'test' ) ) {
264
+ result . stdout = result . stdout ?. toString ( ) . split ( '\n' ) . slice ( 3 ) . join ( '\n' ) . trim ( )
265
+ }
239
266
240
267
t . match ( result , {
241
268
status : 0 ,
242
269
signal : null ,
243
270
stderr : '' ,
244
- stdout : isNpm ? `npm@ ${ version } ${ ROOT } ` : version ,
271
+ stdout : expected ,
245
272
...match ,
246
- } , `${ cmd } ${ bin } ` )
273
+ } , `${ cmd } ${ bin } ${ params [ 0 ] } ` )
247
274
}
248
275
276
+ // Array with command line parameters and expected output
277
+ const tests = [
278
+ { bin : 'npm' , params : [ 'help' ] , expected : `npm@${ version } ${ ROOT } ` } ,
279
+ { bin : 'npx' , params : [ '--version' ] , expected : version } ,
280
+ { bin : 'npm' , params : [ 'test' ] , expected : '' } ,
281
+ { bin : 'npm' , params : [ `test -- hello -p1 world -p2 "hello world" --q1=hello world --q2="hello world"` ] , expected : `hello\n-p1\nworld\n-p2\nhello world\n--q1=hello\nworld\n--q2=hello world` } ,
282
+ { bin : 'npm' , params : [ 'test -- a=1,b=2,c=3' ] , expected : `a=1,b=2,c=3` } ,
283
+ { bin : 'npx' , params : [ '. -- a=1,b=2,c=3' ] , expected : `a=1,b=2,c=3` } ,
284
+ ]
285
+
249
286
// ensure that all tests are either run or skipped
250
287
t . plan ( shells . length )
251
288
@@ -259,9 +296,17 @@ t.test('run shims', t => {
259
296
}
260
297
return t . end ( )
261
298
}
262
- t . plan ( 2 )
263
- matchCmd ( t , cmd , 'npm' , match )
264
- matchCmd ( t , cmd , 'npx' , match )
299
+ t . plan ( tests . length )
300
+ for ( const { bin, params, expected } of tests ) {
301
+ if ( name === 'cygwin bash' && (
302
+ ( bin === 'npm' && params [ 0 ] . startsWith ( 'test' ) ) ||
303
+ ( bin === 'npx' && params [ 0 ] . startsWith ( '.' ) )
304
+ ) ) {
305
+ t . skip ( "`cygwin bash` doesn't respect option `{ cwd: path }` when calling `spawnSync`" )
306
+ } else {
307
+ matchCmd ( t , cmd , bin , match , params , expected )
308
+ }
309
+ }
265
310
} )
266
311
}
267
312
} )
0 commit comments