1
1
const t = require ( 'tap' )
2
+ const fs = require ( 'fs' )
2
3
const requireInject = require ( 'require-inject' )
3
4
const isWindows = require ( '../lib/is-windows.js' )
4
5
@@ -10,21 +11,62 @@ if (!process.env.__FAKE_TESTING_PLATFORM__) {
10
11
} } )
11
12
}
12
13
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
+
13
28
const 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
+ } ,
15
53
} )
16
54
17
55
if ( isWindows ) {
18
56
t . test ( 'windows' , t => {
19
57
// with no ComSpec
20
58
delete process . env . ComSpec
59
+ whichPaths . set ( 'cmd' , 'C:\\Windows\\System32\\cmd.exe' )
60
+ t . teardown ( ( ) => {
61
+ whichPaths . delete ( 'cmd' )
62
+ } )
21
63
t . match ( makeSpawnArgs ( {
22
64
event : 'event' ,
23
65
path : 'path' ,
24
66
cmd : 'script "quoted parameter"; second command' ,
25
67
} ) , [
26
68
'cmd' ,
27
- [ '/d' , '/s' , '/c' , `script "quoted parameter"; second command` ] ,
69
+ [ '/d' , '/s' , '/c' , / \. c m d $ / ] ,
28
70
{
29
71
env : {
30
72
npm_package_json : / p a c k a g e \. j s o n $ / ,
@@ -40,13 +82,17 @@ if (isWindows) {
40
82
41
83
// with a funky ComSpec
42
84
process . env . ComSpec = 'blrorp'
85
+ whichPaths . set ( 'blrorp' , '/bin/blrorp' )
86
+ t . teardown ( ( ) => {
87
+ whichPaths . delete ( 'blrorp' )
88
+ } )
43
89
t . match ( makeSpawnArgs ( {
44
90
event : 'event' ,
45
91
path : 'path' ,
46
92
cmd : 'script "quoted parameter"; second command' ,
47
93
} ) , [
48
94
'blrorp' ,
49
- [ '-c' , `script "quoted parameter"; second command` ] ,
95
+ [ '-c' , / \. s h $ / ] ,
50
96
{
51
97
env : {
52
98
npm_package_json : / p a c k a g e \. j s o n $ / ,
@@ -62,11 +108,12 @@ if (isWindows) {
62
108
t . match ( makeSpawnArgs ( {
63
109
event : 'event' ,
64
110
path : 'path' ,
65
- cmd : 'script "quoted parameter"; second command' ,
111
+ cmd : 'script' ,
112
+ args : [ '"quoted parameter";' , 'second command' ] ,
66
113
scriptShell : 'cmd.exe' ,
67
114
} ) , [
68
115
'cmd.exe' ,
69
- [ '/d' , '/s' , '/c' , `script "quoted parameter"; second command` ] ,
116
+ [ '/d' , '/s' , '/c' , / \. c m d $ / ] ,
70
117
{
71
118
env : {
72
119
npm_package_json : / p a c k a g e \. j s o n $ / ,
@@ -83,13 +130,18 @@ if (isWindows) {
83
130
} )
84
131
} else {
85
132
t . test ( 'posix' , t => {
133
+ whichPaths . set ( 'sh' , '/bin/sh' )
134
+ t . teardown ( ( ) => {
135
+ whichPaths . delete ( 'sh' )
136
+ } )
86
137
t . match ( makeSpawnArgs ( {
87
138
event : 'event' ,
88
139
path : 'path' ,
89
- cmd : 'script "quoted parameter"; second command' ,
140
+ cmd : 'script' ,
141
+ args : [ '"quoted parameter";' , 'second command' ] ,
90
142
} ) , [
91
143
'sh' ,
92
- [ '-c' , `script "quoted parameter"; second command` ] ,
144
+ [ '-c' , / \. s h $ / ] ,
93
145
{
94
146
env : {
95
147
npm_package_json : / p a c k a g e \. j s o n $ / ,
@@ -111,7 +163,7 @@ if (isWindows) {
111
163
scriptShell : 'cmd.exe' ,
112
164
} ) , [
113
165
'cmd.exe' ,
114
- [ '/d' , '/s' , '/c' , `script "quoted parameter"; second command` ] ,
166
+ [ '/d' , '/s' , '/c' , / \. c m d $ / ] ,
115
167
{
116
168
env : {
117
169
npm_package_json : / p a c k a g e \. j s o n $ / ,
0 commit comments