11const t = require ( 'tap' )
2+ const fs = require ( 'fs' )
23const requireInject = require ( 'require-inject' )
34const isWindows = require ( '../lib/is-windows.js' )
45
@@ -10,21 +11,62 @@ if (!process.env.__FAKE_TESTING_PLATFORM__) {
1011 } } )
1112}
1213
14+ const whichPaths = new Map ( )
15+ const which = {
16+ sync : ( req ) => {
17+ if ( whichPaths . has ( req ) ) {
18+ return whichPaths . get ( req )
19+ }
20+
21+ throw new Error ( 'not found' )
22+ } ,
23+ }
24+
25+ const path = require ( 'path' )
26+ const tmpdir = path . resolve ( t . testdir ( ) )
27+
1328const makeSpawnArgs = requireInject ( '../lib/make-spawn-args.js' , {
14- path : require ( 'path' ) [ isWindows ? 'win32' : 'posix' ] ,
29+ fs : {
30+ ...fs ,
31+ chmodSync ( _path , mode ) {
32+ if ( process . platform === 'win32' ) {
33+ _path = _path . replace ( / \/ / g, '\\' )
34+ } else {
35+ _path = _path . replace ( / \\ / g, '/' )
36+ }
37+ return fs . chmodSync ( _path , mode )
38+ } ,
39+ writeFileSync ( _path , content ) {
40+ if ( process . platform === 'win32' ) {
41+ _path = _path . replace ( / \/ / g, '\\' )
42+ } else {
43+ _path = _path . replace ( / \\ / g, '/' )
44+ }
45+ return fs . writeFileSync ( _path , content )
46+ } ,
47+ } ,
48+ which,
49+ os : {
50+ ...require ( 'os' ) ,
51+ tmpdir : ( ) => tmpdir ,
52+ } ,
1553} )
1654
1755if ( isWindows ) {
1856 t . test ( 'windows' , t => {
1957 // with no ComSpec
2058 delete process . env . ComSpec
59+ whichPaths . set ( 'cmd' , 'C:\\Windows\\System32\\cmd.exe' )
60+ t . teardown ( ( ) => {
61+ whichPaths . delete ( 'cmd' )
62+ } )
2163 t . match ( makeSpawnArgs ( {
2264 event : 'event' ,
2365 path : 'path' ,
2466 cmd : 'script "quoted parameter"; second command' ,
2567 } ) , [
2668 'cmd' ,
27- [ '/d' , '/s' , '/c' , `script "quoted parameter"; second command` ] ,
69+ [ '/d' , '/s' , '/c' , / \. c m d $ / ] ,
2870 {
2971 env : {
3072 npm_package_json : / p a c k a g e \. j s o n $ / ,
@@ -40,13 +82,17 @@ if (isWindows) {
4082
4183 // with a funky ComSpec
4284 process . env . ComSpec = 'blrorp'
85+ whichPaths . set ( 'blrorp' , '/bin/blrorp' )
86+ t . teardown ( ( ) => {
87+ whichPaths . delete ( 'blrorp' )
88+ } )
4389 t . match ( makeSpawnArgs ( {
4490 event : 'event' ,
4591 path : 'path' ,
4692 cmd : 'script "quoted parameter"; second command' ,
4793 } ) , [
4894 'blrorp' ,
49- [ '-c' , `script "quoted parameter"; second command` ] ,
95+ [ '-c' , / \. s h $ / ] ,
5096 {
5197 env : {
5298 npm_package_json : / p a c k a g e \. j s o n $ / ,
@@ -62,11 +108,12 @@ if (isWindows) {
62108 t . match ( makeSpawnArgs ( {
63109 event : 'event' ,
64110 path : 'path' ,
65- cmd : 'script "quoted parameter"; second command' ,
111+ cmd : 'script' ,
112+ args : [ '"quoted parameter";' , 'second command' ] ,
66113 scriptShell : 'cmd.exe' ,
67114 } ) , [
68115 'cmd.exe' ,
69- [ '/d' , '/s' , '/c' , `script "quoted parameter"; second command` ] ,
116+ [ '/d' , '/s' , '/c' , / \. c m d $ / ] ,
70117 {
71118 env : {
72119 npm_package_json : / p a c k a g e \. j s o n $ / ,
@@ -83,13 +130,18 @@ if (isWindows) {
83130 } )
84131} else {
85132 t . test ( 'posix' , t => {
133+ whichPaths . set ( 'sh' , '/bin/sh' )
134+ t . teardown ( ( ) => {
135+ whichPaths . delete ( 'sh' )
136+ } )
86137 t . match ( makeSpawnArgs ( {
87138 event : 'event' ,
88139 path : 'path' ,
89- cmd : 'script "quoted parameter"; second command' ,
140+ cmd : 'script' ,
141+ args : [ '"quoted parameter";' , 'second command' ] ,
90142 } ) , [
91143 'sh' ,
92- [ '-c' , `script "quoted parameter"; second command` ] ,
144+ [ '-c' , / \. s h $ / ] ,
93145 {
94146 env : {
95147 npm_package_json : / p a c k a g e \. j s o n $ / ,
@@ -111,7 +163,7 @@ if (isWindows) {
111163 scriptShell : 'cmd.exe' ,
112164 } ) , [
113165 'cmd.exe' ,
114- [ '/d' , '/s' , '/c' , `script "quoted parameter"; second command` ] ,
166+ [ '/d' , '/s' , '/c' , / \. c m d $ / ] ,
115167 {
116168 env : {
117169 npm_package_json : / p a c k a g e \. j s o n $ / ,
0 commit comments