This repository has been archived by the owner on Jan 6, 2021. It is now read-only.
/
index.js
213 lines (184 loc) Β· 5.73 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
const crossSpawnMock = require('cross-spawn')
const isWindowsMock = require('../is-windows')
jest.mock('../is-windows')
jest.mock('cross-spawn')
const crossEnv = require('../')
const getSpawned = (call = 0) => crossSpawnMock.spawn.mock.results[call].value
process.setMaxListeners(20)
beforeEach(() => {
jest.spyOn(process, 'exit').mockImplementation(() => {})
crossSpawnMock.spawn.mockReturnValue({on: jest.fn(), kill: jest.fn()})
})
afterEach(() => {
jest.clearAllMocks()
process.exit.mockRestore()
})
test(`sets environment variables and run the remaining command`, () => {
testEnvSetting({FOO_ENV: 'production'}, 'FOO_ENV=production')
})
test(`APPDATA is undefined and not string`, () => {
testEnvSetting({FOO_ENV: 'production', APPDATA: 2}, 'FOO_ENV=production')
})
test(`handles multiple env variables`, () => {
testEnvSetting(
{
FOO_ENV: 'production',
BAR_ENV: 'dev',
APPDATA: '0',
},
'FOO_ENV=production',
'BAR_ENV=dev',
'APPDATA=0',
)
})
test(`handles special characters`, () => {
testEnvSetting({FOO_ENV: './!?'}, 'FOO_ENV=./!?')
})
test(`handles single-quoted strings`, () => {
testEnvSetting({FOO_ENV: 'bar env'}, "FOO_ENV='bar env'")
})
test(`handles double-quoted strings`, () => {
testEnvSetting({FOO_ENV: 'bar env'}, 'FOO_ENV="bar env"')
})
test(`handles equality signs in quoted strings`, () => {
testEnvSetting({FOO_ENV: 'foo=bar'}, 'FOO_ENV="foo=bar"')
})
test(`handles empty single-quoted strings`, () => {
testEnvSetting({FOO_ENV: ''}, "FOO_ENV=''")
})
test(`handles empty double-quoted strings`, () => {
testEnvSetting({FOO_ENV: ''}, 'FOO_ENV=""')
})
test(`handles no value after the equals sign`, () => {
testEnvSetting({FOO_ENV: ''}, 'FOO_ENV=')
})
test(`handles quoted scripts`, () => {
crossEnv(['GREETING=Hi', 'NAME=Joe', 'echo $GREETING && echo $NAME'], {
shell: true,
})
expect(crossSpawnMock.spawn).toHaveBeenCalledWith(
'echo $GREETING && echo $NAME',
[],
{
stdio: 'inherit',
shell: true,
env: {...process.env, GREETING: 'Hi', NAME: 'Joe'},
},
)
})
test(`handles escaped characters`, () => {
// this escapes \,",' and $
crossEnv(
['GREETING=Hi', 'NAME=Joe', 'echo \\"\\\'\\$GREETING\\\'\\" && echo $NAME'],
{
shell: true,
},
)
expect(crossSpawnMock.spawn).toHaveBeenCalledWith(
'echo "\'$GREETING\'" && echo $NAME',
[],
{
stdio: 'inherit',
shell: true,
env: {...process.env, GREETING: 'Hi', NAME: 'Joe'},
},
)
})
test(`does nothing when given no command`, () => {
crossEnv([])
expect(crossSpawnMock.spawn).toHaveBeenCalledTimes(0)
})
test(`normalizes commands on windows`, () => {
isWindowsMock.mockReturnValue(true)
crossEnv(['./cmd.bat'])
expect(crossSpawnMock.spawn).toHaveBeenCalledWith('cmd.bat', [], {
stdio: 'inherit',
env: {...process.env},
})
})
test(`does not normalize command arguments on windows`, () => {
isWindowsMock.mockReturnValue(true)
crossEnv(['echo', 'http://example.com'])
expect(crossSpawnMock.spawn).toHaveBeenCalledWith(
'echo',
['http://example.com'],
{
stdio: 'inherit',
env: {...process.env},
},
)
})
test(`propagates kill signals`, () => {
testEnvSetting({FOO_ENV: 'foo=bar'}, 'FOO_ENV="foo=bar"')
process.emit('SIGTERM')
process.emit('SIGINT')
process.emit('SIGHUP')
process.emit('SIGBREAK')
const spawned = getSpawned()
expect(spawned.kill).toHaveBeenCalledWith('SIGTERM')
expect(spawned.kill).toHaveBeenCalledWith('SIGINT')
expect(spawned.kill).toHaveBeenCalledWith('SIGHUP')
expect(spawned.kill).toHaveBeenCalledWith('SIGBREAK')
})
test(`keeps backslashes`, () => {
isWindowsMock.mockReturnValue(true)
crossEnv(['echo', '\\\\\\\\someshare\\\\somefolder'])
expect(crossSpawnMock.spawn).toHaveBeenCalledWith(
'echo',
['\\\\someshare\\somefolder'],
{
stdio: 'inherit',
env: {...process.env},
},
)
})
test(`propagates unhandled exit signal`, () => {
const {spawned} = testEnvSetting({FOO_ENV: 'foo=bar'}, 'FOO_ENV="foo=bar"')
const spawnExitCallback = spawned.on.mock.calls[0][1]
const spawnExitCode = null
spawnExitCallback(spawnExitCode)
expect(process.exit).toHaveBeenCalledWith(1)
})
test(`exits cleanly with SIGINT with a null exit code`, () => {
const {spawned} = testEnvSetting({FOO_ENV: 'foo=bar'}, 'FOO_ENV="foo=bar"')
const spawnExitCallback = spawned.on.mock.calls[0][1]
const spawnExitCode = null
const spawnExitSignal = 'SIGINT'
spawnExitCallback(spawnExitCode, spawnExitSignal)
expect(process.exit).toHaveBeenCalledWith(0)
})
test(`propagates regular exit code`, () => {
const {spawned} = testEnvSetting({FOO_ENV: 'foo=bar'}, 'FOO_ENV="foo=bar"')
const spawnExitCallback = spawned.on.mock.calls[0][1]
const spawnExitCode = 0
spawnExitCallback(spawnExitCode)
expect(process.exit).toHaveBeenCalledWith(0)
})
function testEnvSetting(expected, ...envSettings) {
if (expected.APPDATA === 2) {
// kill the APPDATA to test both is undefined
const {env} = process
delete env.APPDATA
delete expected.APPDATA
} else if (!process.env.APPDATA && expected.APPDATA === '0') {
// set APPDATA and test it
process.env.APPDATA = '0'
}
const ret = crossEnv([...envSettings, 'echo', 'hello world'])
const env = {}
if (process.env.APPDATA) {
env.APPDATA = process.env.APPDATA
}
Object.assign(env, expected)
const spawned = getSpawned()
expect(ret).toBe(spawned)
expect(crossSpawnMock.spawn).toHaveBeenCalledTimes(1)
expect(crossSpawnMock.spawn).toHaveBeenCalledWith('echo', ['hello world'], {
stdio: 'inherit',
shell: undefined,
env: {...process.env, ...env},
})
expect(spawned.on).toHaveBeenCalledTimes(1)
expect(spawned.on).toHaveBeenCalledWith('exit', expect.any(Function))
return {spawned}
}