-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathset-envs.js
More file actions
243 lines (230 loc) · 6.49 KB
/
Copy pathset-envs.js
File metadata and controls
243 lines (230 loc) · 6.49 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
const setEnvs = require('../lib/set-envs.js')
const mockGlobals = require('@npmcli/mock-globals')
const { join } = require('node:path')
const t = require('tap')
const { execPath } = process
const cwd = process.cwd()
const globalPrefix = join(cwd, 'global')
const localPrefix = join(cwd, 'local')
const NODE = execPath
const npmPath = '{path}'
const npmBin = join(npmPath, 'bin/npm-cli.js')
const nodeGypPath = require.resolve('node-gyp/bin/node-gyp.js')
const mockDefinitions = (t) => {
mockGlobals(t, { 'process.env': { EDITOR: 'vim' } })
const { definitions, defaults } = t.mock('../lib/definitions/index.js')
return { definitions, defaults }
}
t.test('set envs that are not defaults and not already in env', t => {
const { definitions, defaults } = mockDefinitions(t)
const envConf = Object.create(defaults)
const cliConf = Object.create(envConf)
const extras = {
NODE,
INIT_CWD: cwd,
EDITOR: 'vim',
HOME: undefined,
npm_config_node_gyp: nodeGypPath,
npm_config_npm_version: 'unknown',
npm_execpath: npmBin,
npm_node_execpath: execPath,
npm_config_global_prefix: globalPrefix,
npm_config_local_prefix: localPrefix,
}
const env = {}
const config = {
list: [cliConf, envConf],
env,
defaults,
definitions,
execPath,
globalPrefix,
localPrefix,
npmPath,
npmBin,
}
setEnvs(config)
t.strictSame(env, { ...extras }, 'no new environment vars to create')
envConf.call = 'me, maybe'
setEnvs(config)
t.strictSame(env, { ...extras }, 'no new environment vars to create, already in env')
delete envConf.call
cliConf.call = 'me, maybe'
setEnvs(config)
t.strictSame(env, {
...extras,
npm_config_call: 'me, maybe',
}, 'set in env, because changed from default in cli')
envConf.call = 'me, maybe'
cliConf.call = ''
cliConf['node-options'] = 'some options for node'
setEnvs(config)
t.strictSame(env, {
...extras,
npm_config_call: '',
npm_config_node_options: 'some options for node',
NODE_OPTIONS: 'some options for node',
}, 'set in env, because changed from default in env, back to default in cli')
t.end()
})
t.test('set envs that are not defaults and not already in env, array style', t => {
const { definitions, defaults } = mockDefinitions(t)
const envConf = Object.create(defaults)
const cliConf = Object.create(envConf)
const extras = {
NODE,
INIT_CWD: cwd,
EDITOR: 'vim',
HOME: undefined,
npm_config_node_gyp: nodeGypPath,
npm_config_npm_version: 'unknown',
npm_execpath: npmBin,
npm_node_execpath: execPath,
npm_config_global_prefix: globalPrefix,
npm_config_local_prefix: localPrefix,
}
// make sure it's not sticky
const env = { INIT_CWD: '/some/other/path' }
const config = {
list: [cliConf, envConf],
env,
defaults,
definitions,
execPath,
globalPrefix,
localPrefix,
npmPath,
npmBin,
}
setEnvs(config)
t.strictSame(env, { ...extras }, 'no new environment vars to create')
envConf.omit = ['dev']
setEnvs(config)
t.strictSame(env, { ...extras }, 'no new environment vars to create, already in env')
delete envConf.omit
cliConf.omit = ['dev', 'optional']
setEnvs(config)
t.strictSame(env, {
...extras,
npm_config_omit: 'dev\n\noptional',
}, 'set in env, because changed from default in cli')
envConf.omit = ['optional', 'peer']
cliConf.omit = []
setEnvs(config)
t.strictSame(env, {
...extras,
npm_config_omit: '',
}, 'set in env, because changed from default in env, back to default in cli')
t.end()
})
t.test('set envs that are not defaults and not already in env, boolean edition', t => {
const { definitions, defaults } = mockDefinitions(t)
const envConf = Object.create(defaults)
const cliConf = Object.create(envConf)
const extras = {
NODE,
INIT_CWD: cwd,
EDITOR: 'vim',
HOME: undefined,
npm_config_node_gyp: nodeGypPath,
npm_config_npm_version: 'unknown',
npm_execpath: npmBin,
npm_node_execpath: execPath,
npm_config_global_prefix: globalPrefix,
npm_config_local_prefix: localPrefix,
}
const env = {}
const config = {
list: [cliConf, envConf],
env,
defaults,
definitions,
execPath,
globalPrefix,
localPrefix,
npmPath,
npmBin,
}
setEnvs(config)
t.strictSame(env, { ...extras }, 'no new environment vars to create')
envConf.audit = false
setEnvs(config)
t.strictSame(env, { ...extras }, 'no new environment vars to create, already in env')
delete envConf.audit
cliConf.audit = false
cliConf.ignoreObjects = {
some: { object: 12345 },
}
setEnvs(config)
t.strictSame(env, {
...extras,
npm_config_audit: '',
}, 'set in env, because changed from default in cli')
envConf.audit = false
cliConf.audit = true
setEnvs(config)
t.strictSame(env, {
...extras,
npm_config_audit: 'true',
}, 'set in env, because changed from default in env, back to default in cli')
t.end()
})
t.test('set npm_execpath even if require.main.filename is not set', t => {
const { definitions, defaults } = mockDefinitions(t)
const { filename } = require.main
t.teardown(() => require.main.filename = filename)
require.main.filename = null
// also, don't set editor
const d = { ...defaults, editor: null }
const envConf = Object.create(d)
const cliConf = Object.create(envConf)
const env = { DESTDIR: '/some/dest' }
const config = {
list: [cliConf, envConf],
env,
defaults: d,
definitions,
execPath,
globalPrefix,
localPrefix,
npmBin,
}
setEnvs(config)
t.equal(env.npm_execpath, npmBin, 'did not set npm_execpath')
t.end()
})
t.test('dont set configs marked as envExport:false', t => {
const { definitions, defaults } = mockDefinitions(t)
const envConf = Object.create(defaults)
const cliConf = Object.create(envConf)
const extras = {
NODE,
INIT_CWD: cwd,
EDITOR: 'vim',
HOME: undefined,
npm_config_node_gyp: nodeGypPath,
npm_config_npm_version: 'unknown',
npm_execpath: npmBin,
npm_node_execpath: execPath,
npm_config_global_prefix: globalPrefix,
npm_config_local_prefix: localPrefix,
}
const env = {}
const config = {
list: [cliConf, envConf],
env,
defaults,
definitions,
execPath,
globalPrefix,
localPrefix,
npmPath,
npmBin,
}
setEnvs(config)
t.strictSame(env, { ...extras }, 'no new environment vars to create')
cliConf['include-workspace-root'] = true
setEnvs(config)
t.strictSame(env, { ...extras }, 'not exported, because envExport=false')
t.end()
})