This repository has been archived by the owner on Jan 6, 2021. It is now read-only.
/
index.js
165 lines (143 loc) Β· 4.46 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
import crossSpawnMock from 'cross-spawn'
import isWindowsMock from 'is-windows'
const crossEnv = require('../')
process.setMaxListeners(20)
beforeEach(() => {
crossSpawnMock.__mock.reset()
})
it(`should set environment variables and run the remaining command`, () => {
testEnvSetting({FOO_ENV: 'production'}, 'FOO_ENV=production')
})
it(`should APPDATA be undefined and not string`, () => {
testEnvSetting({FOO_ENV: 'production', APPDATA: 2}, 'FOO_ENV=production')
})
it(`should handle multiple env variables`, () => {
testEnvSetting(
{
FOO_ENV: 'production',
BAR_ENV: 'dev',
APPDATA: '0',
},
'FOO_ENV=production',
'BAR_ENV=dev',
'APPDATA=0',
)
})
it(`should handle special characters`, () => {
testEnvSetting({FOO_ENV: './!?'}, 'FOO_ENV=./!?')
})
it(`should handle single-quoted strings`, () => {
testEnvSetting({FOO_ENV: 'bar env'}, "FOO_ENV='bar env'")
})
it(`should handle double-quoted strings`, () => {
testEnvSetting({FOO_ENV: 'bar env'}, 'FOO_ENV="bar env"')
})
it(`should handle equality signs in quoted strings`, () => {
testEnvSetting({FOO_ENV: 'foo=bar'}, 'FOO_ENV="foo=bar"')
})
it(`should handle empty single-quoted strings`, () => {
testEnvSetting({FOO_ENV: ''}, "FOO_ENV=''")
})
it(`should handle empty double-quoted strings`, () => {
testEnvSetting({FOO_ENV: ''}, 'FOO_ENV=""')
})
it(`should handle no value after the equals sign`, () => {
testEnvSetting({FOO_ENV: ''}, 'FOO_ENV=')
})
it(`should handle 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: Object.assign({}, process.env, {
GREETING: 'Hi',
NAME: 'Joe',
}),
})
})
it(`should handle 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: Object.assign({}, process.env, {
GREETING: 'Hi',
NAME: 'Joe',
}),
})
})
it(`should do nothing given no command`, () => {
crossEnv([])
expect(crossSpawnMock.spawn).toHaveBeenCalledTimes(0)
})
it(`should normalize command on windows`, () => {
isWindowsMock.__mock.returnValue = true
crossEnv(['./cmd.bat'])
expect(crossSpawnMock.spawn).toHaveBeenCalledWith('cmd.bat', [], {
stdio: 'inherit',
env: Object.assign({}, process.env),
})
isWindowsMock.__mock.reset()
})
it(`should not normalize command arguments on windows`, () => {
isWindowsMock.__mock.returnValue = true
crossEnv(['echo', 'http://example.com'])
expect(crossSpawnMock.spawn).toHaveBeenCalledWith(
'echo',
['http://example.com'],
{
stdio: 'inherit',
env: Object.assign({}, process.env),
},
)
isWindowsMock.__mock.reset()
})
it(`should propagate kill signals`, () => {
testEnvSetting({FOO_ENV: 'foo=bar'}, 'FOO_ENV="foo=bar"')
process.emit('SIGTERM')
process.emit('SIGINT')
process.emit('SIGHUP')
process.emit('SIGBREAK')
expect(crossSpawnMock.__mock.spawned.kill).toHaveBeenCalledWith('SIGTERM')
expect(crossSpawnMock.__mock.spawned.kill).toHaveBeenCalledWith('SIGINT')
expect(crossSpawnMock.__mock.spawned.kill).toHaveBeenCalledWith('SIGHUP')
expect(crossSpawnMock.__mock.spawned.kill).toHaveBeenCalledWith('SIGBREAK')
})
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)
expect(ret).toBe(crossSpawnMock.__mock.spawned)
expect(crossSpawnMock.spawn).toHaveBeenCalledTimes(1)
expect(crossSpawnMock.spawn).toHaveBeenCalledWith('echo', ['hello world'], {
stdio: 'inherit',
shell: undefined,
env: Object.assign({}, process.env, env),
})
expect(crossSpawnMock.__mock.spawned.on).toHaveBeenCalledTimes(1)
expect(crossSpawnMock.__mock.spawned.on).toHaveBeenCalledWith(
'exit',
expect.any(Function),
)
}