@@ -23,7 +23,18 @@ const which = {
23
23
}
24
24
25
25
const path = require ( 'path' )
26
- const tmpdir = path . resolve ( t . testdir ( ) )
26
+ // we make our fake temp dir contain spaces for extra safety in paths with spaces
27
+ const tmpdir = path . resolve ( t . testdir ( { 'with spaces' : { } } ) , 'with spaces' )
28
+
29
+ // used for unescaping windows path to script file
30
+ const unescapeCmd = ( input ) => input
31
+ . replace ( / ^ \^ " / , '' )
32
+ . replace ( / \^ " $ / , '' )
33
+ . replace ( / \^ ( .) / g, '$1' )
34
+
35
+ const unescapeSh = ( input ) => input
36
+ . replace ( / ^ ' / , '' )
37
+ . replace ( / ' $ / , '' )
27
38
28
39
const makeSpawnArgs = requireInject ( '../lib/make-spawn-args.js' , {
29
40
fs : {
@@ -68,7 +79,7 @@ if (isWindows) {
68
79
cmd : 'script "quoted parameter"; second command' ,
69
80
} )
70
81
t . equal ( shell , 'cmd' , 'default shell applies' )
71
- t . match ( args , [ '/d' , '/s' , '/c' , / \. c m d $ / ] , 'got expected args' )
82
+ t . match ( args , [ '/d' , '/s' , '/c' , / \. c m d \^ " $ / ] , 'got expected args' )
72
83
t . match ( opts , {
73
84
env : {
74
85
npm_package_json : / p a c k a g e \. j s o n $ / ,
@@ -81,11 +92,12 @@ if (isWindows) {
81
92
windowsVerbatimArguments : true ,
82
93
} , 'got expected options' )
83
94
84
- const contents = fs . readFileSync ( args [ args . length - 1 ] , { encoding : 'utf8' } )
95
+ const filename = unescapeCmd ( args [ args . length - 1 ] )
96
+ const contents = fs . readFileSync ( filename , { encoding : 'utf8' } )
85
97
t . equal ( contents , `@echo off\nscript "quoted parameter"; second command` )
86
- t . ok ( fs . existsSync ( args [ args . length - 1 ] ) , 'script file was written' )
98
+ t . ok ( fs . existsSync ( filename ) , 'script file was written' )
87
99
cleanup ( )
88
- t . not ( fs . existsSync ( args [ args . length - 1 ] ) , 'cleanup removes script file' )
100
+ t . not ( fs . existsSync ( filename ) , 'cleanup removes script file' )
89
101
90
102
t . end ( )
91
103
} )
@@ -103,7 +115,7 @@ if (isWindows) {
103
115
cmd : 'script "quoted parameter"; second command' ,
104
116
} )
105
117
t . equal ( shell , 'blrorp' , 'used ComSpec as default shell' )
106
- t . match ( args , [ '-c' , / \. s h $ / ] , 'got expected args' )
118
+ t . match ( args , [ '-c' , / \. s h ' $ / ] , 'got expected args' )
107
119
t . match ( opts , {
108
120
env : {
109
121
npm_package_json : / p a c k a g e \. j s o n $ / ,
@@ -115,9 +127,10 @@ if (isWindows) {
115
127
windowsVerbatimArguments : undefined ,
116
128
} , 'got expected options' )
117
129
118
- t . ok ( fs . existsSync ( args [ args . length - 1 ] ) , 'script file was written' )
130
+ const filename = unescapeSh ( args [ args . length - 1 ] )
131
+ t . ok ( fs . existsSync ( filename ) , 'script file was written' )
119
132
cleanup ( )
120
- t . not ( fs . existsSync ( args [ args . length - 1 ] ) , 'cleanup removes script file' )
133
+ t . not ( fs . existsSync ( filename ) , 'cleanup removes script file' )
121
134
122
135
t . end ( )
123
136
} )
@@ -131,7 +144,7 @@ if (isWindows) {
131
144
scriptShell : 'cmd.exe' ,
132
145
} )
133
146
t . equal ( shell , 'cmd.exe' , 'kept cmd.exe' )
134
- t . match ( args , [ '/d' , '/s' , '/c' , / \. c m d $ / ] , 'got expected args' )
147
+ t . match ( args , [ '/d' , '/s' , '/c' , / \. c m d \^ " $ / ] , 'got expected args' )
135
148
t . match ( opts , {
136
149
env : {
137
150
npm_package_json : / p a c k a g e \. j s o n $ / ,
@@ -143,9 +156,10 @@ if (isWindows) {
143
156
windowsVerbatimArguments : true ,
144
157
} , 'got expected options' )
145
158
146
- t . ok ( fs . existsSync ( args [ args . length - 1 ] ) , 'script file was written' )
159
+ const filename = unescapeCmd ( args [ args . length - 1 ] )
160
+ t . ok ( fs . existsSync ( filename ) , 'script file was written' )
147
161
cleanup ( )
148
- t . not ( fs . existsSync ( args [ args . length - 1 ] ) , 'cleanup removes script file' )
162
+ t . not ( fs . existsSync ( filename ) , 'cleanup removes script file' )
149
163
150
164
t . end ( )
151
165
} )
@@ -161,7 +175,7 @@ if (isWindows) {
161
175
args : [ '"quoted parameter";' , 'second command' ] ,
162
176
} )
163
177
t . equal ( shell , 'cmd' , 'default shell applies' )
164
- t . match ( args , [ '/d' , '/s' , '/c' , / \. c m d $ / ] , 'got expected args' )
178
+ t . match ( args , [ '/d' , '/s' , '/c' , / \. c m d \^ " $ / ] , 'got expected args' )
165
179
t . match ( opts , {
166
180
env : {
167
181
npm_package_json : / p a c k a g e \. j s o n $ / ,
@@ -174,11 +188,12 @@ if (isWindows) {
174
188
windowsVerbatimArguments : true ,
175
189
} , 'got expected options' )
176
190
177
- const contents = fs . readFileSync ( args [ args . length - 1 ] , { encoding : 'utf8' } )
178
- t . equal ( contents , `@echo off\nscript ^"\\^"quoted parameter\\^";^" ^"second command^"` )
179
- t . ok ( fs . existsSync ( args [ args . length - 1 ] ) , 'script file was written' )
191
+ const filename = unescapeCmd ( args [ args . length - 1 ] )
192
+ const contents = fs . readFileSync ( filename , { encoding : 'utf8' } )
193
+ t . equal ( contents , `@echo off\nscript ^"\\^"quoted^ parameter\\^";^" ^"second^ command^"` )
194
+ t . ok ( fs . existsSync ( filename ) , 'script file was written' )
180
195
cleanup ( )
181
- t . not ( fs . existsSync ( args [ args . length - 1 ] ) , 'cleanup removes script file' )
196
+ t . not ( fs . existsSync ( filename ) , 'cleanup removes script file' )
182
197
183
198
t . end ( )
184
199
} )
@@ -194,7 +209,7 @@ if (isWindows) {
194
209
args : [ '"quoted parameter";' , 'second command' ] ,
195
210
} )
196
211
t . equal ( shell , 'cmd' , 'default shell applies' )
197
- t . match ( args , [ '/d' , '/s' , '/c' , / \. c m d $ / ] , 'got expected args' )
212
+ t . match ( args , [ '/d' , '/s' , '/c' , / \. c m d \^ " $ / ] , 'got expected args' )
198
213
t . match ( opts , {
199
214
env : {
200
215
npm_package_json : / p a c k a g e \. j s o n $ / ,
@@ -207,14 +222,15 @@ if (isWindows) {
207
222
windowsVerbatimArguments : true ,
208
223
} , 'got expected options' )
209
224
210
- const contents = fs . readFileSync ( args [ args . length - 1 ] , { encoding : 'utf8' } )
225
+ const filename = unescapeCmd ( args [ args . length - 1 ] )
226
+ const contents = fs . readFileSync ( filename , { encoding : 'utf8' } )
211
227
t . equal ( contents , [
212
228
'@echo off' ,
213
- `script ^^^"\\^^^"quoted parameter\\^^^";^^^" ^^^"second command^^^"` ,
229
+ `script ^^^"\\^^^"quoted^^^ parameter\\^^^";^^^" ^^^"second^^^ command^^^"` ,
214
230
] . join ( '\n' ) )
215
- t . ok ( fs . existsSync ( args [ args . length - 1 ] ) , 'script file was written' )
231
+ t . ok ( fs . existsSync ( filename ) , 'script file was written' )
216
232
cleanup ( )
217
- t . not ( fs . existsSync ( args [ args . length - 1 ] ) , 'cleanup removes script file' )
233
+ t . not ( fs . existsSync ( filename ) , 'cleanup removes script file' )
218
234
219
235
t . end ( )
220
236
} )
@@ -232,7 +248,7 @@ if (isWindows) {
232
248
args : [ '"quoted parameter";' , 'second command' ] ,
233
249
} )
234
250
t . equal ( shell , 'cmd' , 'default shell applies' )
235
- t . match ( args , [ '/d' , '/s' , '/c' , / \. c m d $ / ] , 'got expected args' )
251
+ t . match ( args , [ '/d' , '/s' , '/c' , / \. c m d \^ " $ / ] , 'got expected args' )
236
252
t . match ( opts , {
237
253
env : {
238
254
npm_package_json : / p a c k a g e \. j s o n $ / ,
@@ -245,15 +261,16 @@ if (isWindows) {
245
261
windowsVerbatimArguments : true ,
246
262
} , 'got expected options' )
247
263
248
- const contents = fs . readFileSync ( args [ args . length - 1 ] , { encoding : 'utf8' } )
264
+ const filename = unescapeCmd ( args [ args . length - 1 ] )
265
+ const contents = fs . readFileSync ( filename , { encoding : 'utf8' } )
249
266
t . equal ( contents , [
250
267
'@echo off' ,
251
268
// eslint-disable-next-line max-len
252
- `"my script" ^^^"\\^^^"quoted parameter\\^^^";^^^" ^^^"second command^^^"` ,
269
+ `"my script" ^^^"\\^^^"quoted^^^ parameter\\^^^";^^^" ^^^"second^^^ command^^^"` ,
253
270
] . join ( '\n' ) )
254
- t . ok ( fs . existsSync ( args [ args . length - 1 ] ) , 'script file was written' )
271
+ t . ok ( fs . existsSync ( filename ) , 'script file was written' )
255
272
cleanup ( )
256
- t . not ( fs . existsSync ( args [ args . length - 1 ] ) , 'cleanup removes script file' )
273
+ t . not ( fs . existsSync ( filename ) , 'cleanup removes script file' )
257
274
258
275
t . end ( )
259
276
} )
@@ -275,7 +292,7 @@ if (isWindows) {
275
292
args : [ '"quoted parameter";' , 'second command' ] ,
276
293
} )
277
294
t . equal ( shell , 'sh' , 'defaults to sh' )
278
- t . match ( args , [ '-c' , / \. s h $ / ] , 'got expected args' )
295
+ t . match ( args , [ '-c' , / \. s h ' $ / ] , 'got expected args' )
279
296
t . match ( opts , {
280
297
env : {
281
298
npm_package_json : / p a c k a g e \. j s o n $ / ,
@@ -287,11 +304,12 @@ if (isWindows) {
287
304
windowsVerbatimArguments : undefined ,
288
305
} , 'got expected options' )
289
306
290
- const contents = fs . readFileSync ( args [ args . length - 1 ] , { encoding : 'utf8' } )
307
+ const filename = unescapeSh ( args [ args . length - 1 ] )
308
+ const contents = fs . readFileSync ( filename , { encoding : 'utf8' } )
291
309
t . equal ( contents , `#!/usr/bin/env sh\nscript '"quoted parameter";' 'second command'` )
292
- t . ok ( fs . existsSync ( args [ args . length - 1 ] ) , 'script file was written' )
310
+ t . ok ( fs . existsSync ( filename ) , 'script file was written' )
293
311
cleanup ( )
294
- t . not ( fs . existsSync ( args [ args . length - 1 ] ) , 'cleanup removes script file' )
312
+ t . not ( fs . existsSync ( filename ) , 'cleanup removes script file' )
295
313
296
314
t . end ( )
297
315
} )
@@ -305,7 +323,7 @@ if (isWindows) {
305
323
scriptShell : '/bin/sh' ,
306
324
} )
307
325
t . equal ( shell , '/bin/sh' , 'kept provided setting' )
308
- t . match ( args , [ '-c' , / \. s h $ / ] , 'got expected args' )
326
+ t . match ( args , [ '-c' , / \. s h ' $ / ] , 'got expected args' )
309
327
t . match ( opts , {
310
328
env : {
311
329
npm_package_json : / p a c k a g e \. j s o n $ / ,
@@ -317,11 +335,12 @@ if (isWindows) {
317
335
windowsVerbatimArguments : undefined ,
318
336
} , 'got expected options' )
319
337
320
- const contents = fs . readFileSync ( args [ args . length - 1 ] , { encoding : 'utf8' } )
338
+ const filename = unescapeSh ( args [ args . length - 1 ] )
339
+ const contents = fs . readFileSync ( filename , { encoding : 'utf8' } )
321
340
t . equal ( contents , `#!/bin/sh\nscript '"quoted parameter";' 'second command'` )
322
- t . ok ( fs . existsSync ( args [ args . length - 1 ] ) , 'script file was written' )
341
+ t . ok ( fs . existsSync ( filename ) , 'script file was written' )
323
342
cleanup ( )
324
- t . not ( fs . existsSync ( args [ args . length - 1 ] ) , 'cleanup removes script file' )
343
+ t . not ( fs . existsSync ( filename ) , 'cleanup removes script file' )
325
344
326
345
t . end ( )
327
346
} )
@@ -336,7 +355,7 @@ if (isWindows) {
336
355
scriptShell : 'cmd.exe' ,
337
356
} )
338
357
t . equal ( shell , 'cmd.exe' , 'kept cmd.exe' )
339
- t . match ( args , [ '/d' , '/s' , '/c' , / \. c m d $ / ] , 'got expected args' )
358
+ t . match ( args , [ '/d' , '/s' , '/c' , / \. c m d \^ " $ / ] , 'got expected args' )
340
359
t . match ( opts , {
341
360
env : {
342
361
npm_package_json : / p a c k a g e \. j s o n $ / ,
@@ -348,9 +367,10 @@ if (isWindows) {
348
367
windowsVerbatimArguments : true ,
349
368
} , 'got expected options' )
350
369
351
- t . ok ( fs . existsSync ( args [ args . length - 1 ] ) , 'script file was written' )
370
+ const filename = unescapeCmd ( args [ args . length - 1 ] )
371
+ t . ok ( fs . existsSync ( filename ) , 'script file was written' )
352
372
cleanup ( )
353
- t . not ( fs . existsSync ( args [ args . length - 1 ] ) , 'cleanup removes script file' )
373
+ t . not ( fs . existsSync ( filename ) , 'cleanup removes script file' )
354
374
355
375
t . end ( )
356
376
} )
0 commit comments