1
+ 'use strict'
2
+
3
+ const { writeFileSync : writeFile , unlinkSync : unlink , chmodSync : chmod } = require ( 'fs' )
4
+ const { join } = require ( 'path' )
1
5
const t = require ( 'tap' )
6
+ const promiseSpawn = require ( '@npmcli/promise-spawn' )
2
7
3
8
const escape = require ( '../lib/escape.js' )
9
+ const isWindows = process . platform === 'win32'
4
10
5
11
t . test ( 'sh' , ( t ) => {
6
12
const expectations = [
@@ -17,11 +23,29 @@ t.test('sh', (t) => {
17
23
[ `'--arg=npm exec -c "$1"'` , `\\''--arg=npm exec -c "$1"'\\'` ] ,
18
24
]
19
25
20
- t . plan ( expectations . length )
21
26
for ( const [ input , expectation ] of expectations ) {
22
27
t . equal ( escape . sh ( input ) , expectation ,
23
28
`expected to escape \`${ input } \` to \`${ expectation } \`` )
24
29
}
30
+
31
+ t . test ( 'integration' , { skip : isWindows && 'posix only' } , async ( t ) => {
32
+ const dir = t . testdir ( )
33
+
34
+ for ( const [ input ] of expectations ) {
35
+ const filename = join ( dir , 'posix.sh' )
36
+ const script = `#!/usr/bin/env sh\nnode -p process.argv[1] -- ${ escape . sh ( input ) } `
37
+ writeFile ( filename , script )
38
+ chmod ( filename , '0755' )
39
+ const p = await promiseSpawn ( 'sh' , [ '-c' , filename ] , { stdioString : true } )
40
+ const stdout = p . stdout . trim ( )
41
+ t . equal ( input , stdout , 'actual output matches input' )
42
+ unlink ( filename )
43
+ }
44
+
45
+ t . end ( )
46
+ } )
47
+
48
+ t . end ( )
25
49
} )
26
50
27
51
t . test ( 'cmd' , ( t ) => {
@@ -72,9 +96,34 @@ t.test('cmd', (t) => {
72
96
[ 'hello %PATH%' , '^^^"hello %%PATH%%^^^"' , true ] ,
73
97
]
74
98
75
- t . plan ( expectations . length )
76
99
for ( const [ input , expectation , double ] of expectations ) {
77
100
const msg = `expected to${ double ? ' double' : '' } escape \`${ input } \` to \`${ expectation } \``
78
101
t . equal ( escape . cmd ( input , double ) , expectation , msg )
79
102
}
103
+
104
+ t . test ( 'integration' , { skip : ! isWindows && 'Windows only' } , async ( t ) => {
105
+ const dir = t . testdir ( )
106
+
107
+ for ( const [ input , , double ] of expectations ) {
108
+ const filename = join ( dir , 'win.cmd' )
109
+ if ( double ) {
110
+ const shimFile = join ( dir , 'shim.cmd' )
111
+ const shim = `@echo off\nnode -p process.argv[1] -- %*`
112
+ writeFile ( shimFile , shim )
113
+ const script = `@echo off\n.\\\\shim.cmd ${ escape . cmd ( input , double ) } `
114
+ writeFile ( filename , script )
115
+ } else {
116
+ const script = `@echo off\nnode -p process.argv[1] -- ${ escape . cmd ( input ) } `
117
+ writeFile ( filename , script )
118
+ }
119
+ const p = await promiseSpawn ( 'cmd' , [ '/d' , '/s' , '/c' , filename ] , { stdioString : true } )
120
+ const stdout = p . stdout . trim ( )
121
+ t . equal ( input , stdout , 'actual output matches input' )
122
+ unlink ( filename )
123
+ }
124
+
125
+ t . end ( )
126
+ } )
127
+
128
+ t . end ( )
80
129
} )
0 commit comments