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,63 @@ 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' ) [ isWindows ? 'win32' : 'posix' ]
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+ path,
49+ which,
50+ os : {
51+ ...require ( 'os' ) ,
52+ tmpdir : ( ) => tmpdir ,
53+ } ,
1554} )
1655
1756if ( isWindows ) {
1857 t . test ( 'windows' , t => {
1958 // with no ComSpec
2059 delete process . env . ComSpec
60+ whichPaths . set ( 'cmd' , 'C:\\Windows\\System32\\cmd.exe' )
61+ t . teardown ( ( ) => {
62+ whichPaths . delete ( 'cmd' )
63+ } )
2164 t . match ( makeSpawnArgs ( {
2265 event : 'event' ,
2366 path : 'path' ,
2467 cmd : 'script "quoted parameter"; second command' ,
2568 } ) , [
2669 'cmd' ,
27- [ '/d' , '/s' , '/c' , `script "quoted parameter"; second command` ] ,
70+ [ '/d' , '/s' , '/c' , / \. c m d $ / ] ,
2871 {
2972 env : {
3073 npm_package_json : / p a c k a g e \. j s o n $ / ,
@@ -40,13 +83,17 @@ if (isWindows) {
4083
4184 // with a funky ComSpec
4285 process . env . ComSpec = 'blrorp'
86+ whichPaths . set ( 'blrorp' , '/bin/blrorp' )
87+ t . teardown ( ( ) => {
88+ whichPaths . delete ( 'blrorp' )
89+ } )
4390 t . match ( makeSpawnArgs ( {
4491 event : 'event' ,
4592 path : 'path' ,
4693 cmd : 'script "quoted parameter"; second command' ,
4794 } ) , [
4895 'blrorp' ,
49- [ '-c' , `script "quoted parameter"; second command` ] ,
96+ [ '-c' , / \. s h $ / ] ,
5097 {
5198 env : {
5299 npm_package_json : / p a c k a g e \. j s o n $ / ,
@@ -62,11 +109,12 @@ if (isWindows) {
62109 t . match ( makeSpawnArgs ( {
63110 event : 'event' ,
64111 path : 'path' ,
65- cmd : 'script "quoted parameter"; second command' ,
112+ cmd : 'script' ,
113+ args : [ '"quoted parameter";' , 'second command' ] ,
66114 scriptShell : 'cmd.exe' ,
67115 } ) , [
68116 'cmd.exe' ,
69- [ '/d' , '/s' , '/c' , `script "quoted parameter"; second command` ] ,
117+ [ '/d' , '/s' , '/c' , / \. c m d $ / ] ,
70118 {
71119 env : {
72120 npm_package_json : / p a c k a g e \. j s o n $ / ,
@@ -83,13 +131,18 @@ if (isWindows) {
83131 } )
84132} else {
85133 t . test ( 'posix' , t => {
134+ whichPaths . set ( 'sh' , '/bin/sh' )
135+ t . teardown ( ( ) => {
136+ whichPaths . delete ( 'sh' )
137+ } )
86138 t . match ( makeSpawnArgs ( {
87139 event : 'event' ,
88140 path : 'path' ,
89- cmd : 'script "quoted parameter"; second command' ,
141+ cmd : 'script' ,
142+ args : [ '"quoted parameter";' , 'second command' ] ,
90143 } ) , [
91144 'sh' ,
92- [ '-c' , `script "quoted parameter"; second command` ] ,
145+ [ '-c' , / \. s h $ / ] ,
93146 {
94147 env : {
95148 npm_package_json : / p a c k a g e \. j s o n $ / ,
@@ -111,7 +164,7 @@ if (isWindows) {
111164 scriptShell : 'cmd.exe' ,
112165 } ) , [
113166 'cmd.exe' ,
114- [ '/d' , '/s' , '/c' , `script "quoted parameter"; second command` ] ,
167+ [ '/d' , '/s' , '/c' , / \. c m d $ / ] ,
115168 {
116169 env : {
117170 npm_package_json : / p a c k a g e \. j s o n $ / ,
0 commit comments