-
Notifications
You must be signed in to change notification settings - Fork 339
/
Copy pathrc-config.test.ts
354 lines (333 loc) · 15.3 KB
/
rc-config.test.ts
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import fs from 'fs/promises'
import os from 'os'
import path from 'path'
import spawn from 'spawn-please'
import chaiSetup from './helpers/chaiSetup'
import stubVersions from './helpers/stubVersions'
chaiSetup()
const bin = path.join(__dirname, '../build/cli.js')
describe('rc-config', () => {
// before/after must be placed within the describe block, otherwise they will apply to tests in other files
let stub: { restore: () => void }
before(() => (stub = stubVersions('99.9.9', { spawn: true })))
after(() => stub.restore())
it('print rcConfigPath when there is a non-empty rc config file', async () => {
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'npm-check-updates-'))
const tempConfigFile = path.join(tempDir, '.ncurc.json')
await fs.writeFile(tempConfigFile, JSON.stringify({ filter: 'ncu-test-v2' }), 'utf-8')
try {
const { stdout } = await spawn('node', [bin, '--stdin', '--configFilePath', tempDir], {
stdin: JSON.stringify({ dependencies: { 'ncu-test-v2': '1.0.0', 'ncu-test-tag': '0.1.0' } }),
})
stdout.should.containIgnoreCase(`Using config file ${tempConfigFile}`)
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
}
})
it('do not print rcConfigPath when there is no rc config file', async () => {
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'npm-check-updates-'))
try {
const { stdout } = await spawn('node', [bin, '--stdin', '--cwd', tempDir], {
stdin: JSON.stringify({ dependencies: { 'ncu-test-v2': '1.0.0' } }),
})
stdout.should.not.include('Using config file')
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
}
})
it('do not print rcConfigPath when there is an empty rc config file', async () => {
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'npm-check-updates-'))
const tempConfigFile = path.join(tempDir, '.ncurc.json')
await fs.writeFile(tempConfigFile, '{}', 'utf-8')
try {
const { stdout } = await spawn('node', [bin, '--stdin', '--configFilePath', tempDir], {
stdin: JSON.stringify({ dependencies: { 'ncu-test-v2': '1', 'ncu-test-tag': '0.1.0' } }),
})
stdout.should.not.include('Using config file')
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
}
})
it('error on missing --configFileName', async () => {
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'npm-check-updates-'))
const configFileName = '.ncurc_missing.json'
try {
const result = spawn('node', [bin, '--stdin', '--configFilePath', tempDir, '--configFileName', configFileName], {
stdin: JSON.stringify({ dependencies: { 'ncu-test-v2': '1', 'ncu-test-tag': '0.1.0' } }),
})
await result.should.eventually.be.rejectedWith(`Config file ${configFileName} not found in ${tempDir}`)
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
}
})
it('read --configFilePath', async () => {
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'npm-check-updates-'))
const tempConfigFile = path.join(tempDir, '.ncurc.json')
await fs.writeFile(tempConfigFile, JSON.stringify({ jsonUpgraded: true, filter: 'ncu-test-v2' }), 'utf-8')
try {
const { stdout } = await spawn('node', [bin, '--stdin', '--configFilePath', tempDir], {
stdin: JSON.stringify({ dependencies: { 'ncu-test-v2': '1', 'ncu-test-tag': '0.1.0' } }),
})
const pkgData = JSON.parse(stdout)
pkgData.should.have.property('ncu-test-v2')
pkgData.should.not.have.property('ncu-test-tag')
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
}
})
it('read --configFileName', async () => {
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'npm-check-updates-'))
const tempConfigFileName = '.rctemp.json'
const tempConfigFile = path.join(tempDir, tempConfigFileName)
await fs.writeFile(tempConfigFile, JSON.stringify({ jsonUpgraded: true, filter: 'ncu-test-v2' }), 'utf-8')
try {
const { stdout } = await spawn(
'node',
[bin, '--stdin', '--configFilePath', tempDir, '--configFileName', tempConfigFileName],
{ stdin: JSON.stringify({ dependencies: { 'ncu-test-v2': '1', 'ncu-test-tag': '0.1.0' } }) },
)
const pkgData = JSON.parse(stdout)
pkgData.should.have.property('ncu-test-v2')
pkgData.should.not.have.property('ncu-test-tag')
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
}
})
it('override config with arguments', async () => {
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'npm-check-updates-'))
const tempConfigFile = path.join(tempDir, '.ncurc.json')
await fs.writeFile(tempConfigFile, JSON.stringify({ jsonUpgraded: true, filter: 'ncu-test-v2' }), 'utf-8')
try {
const { stdout } = await spawn(
'node',
[bin, '--stdin', '--configFilePath', tempDir, '--filter', 'ncu-test-tag'],
{ stdin: JSON.stringify({ dependencies: { 'ncu-test-v2': '1', 'ncu-test-tag': '0.1.0' } }) },
)
const pkgData = JSON.parse(stdout)
pkgData.should.have.property('ncu-test-tag')
pkgData.should.not.have.property('ncu-test-v2')
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
}
})
it('override true in config with false in the cli', async () => {
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'npm-check-updates-'))
const tempConfigFile = path.join(tempDir, '.ncurc.json')
await fs.writeFile(tempConfigFile, JSON.stringify({ jsonUpgraded: true }), 'utf-8')
try {
const { stdout } = await spawn('node', [bin, '--stdin', '--configFilePath', tempDir, '--no-jsonUpgraded'], {
stdin: JSON.stringify({ dependencies: { 'ncu-test-v2': '1', 'ncu-test-tag': '0.1.0' } }),
})
// if the output contains "Using config file", then we know that jsonUpgraded was overridden
stdout.should.include('Using config file')
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
}
})
it('handle boolean arguments', async () => {
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'npm-check-updates-'))
const tempConfigFile = path.join(tempDir, '.ncurc.json')
// if boolean arguments are not handled as a special case, ncu will incorrectly pass "--deep false" to commander, which will interpret it as two args, i.e. --deep and --filter false
await fs.writeFile(tempConfigFile, JSON.stringify({ jsonUpgraded: true, deep: false }), 'utf-8')
try {
const { stdout } = await spawn('node', [bin, '--stdin', '--configFilePath', tempDir], {
stdin: JSON.stringify({ dependencies: { 'ncu-test-tag': '0.1.0' } }),
})
const pkgData = JSON.parse(stdout)
pkgData.should.have.property('ncu-test-tag')
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
}
})
it('auto detect .ncurc.json', async () => {
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'npm-check-updates-'))
const configFile = path.join(tempDir, '.ncurc.json')
const pkgFile = path.join(tempDir, 'package.json')
await fs.writeFile(configFile, JSON.stringify({ filter: 'ncu-test-v2' }), 'utf-8')
await fs.writeFile(
pkgFile,
JSON.stringify({ dependencies: { 'ncu-test-v2': '1.0.0', 'ncu-test-tag': '0.1.0' } }),
'utf-8',
)
try {
// awkwardly, we have to set mergeConfig to enable autodetecting the rcconfig because otherwise it is explicitly disabled for tests
const { stdout } = await spawn('node', [bin, '--mergeConfig'], {}, { cwd: tempDir })
const firstLine = stdout.split('\n')[0]
// On OSX tempDir is /var/folders/cb/12345, but npm-check-updates recieves /private/var/folders/cb/12345.
// Apparently OSX symlinks /tmp to /private/tmp for historical reasons.
// Therefore, ignore any directories prepended to the config file path.
firstLine.should.contains('Using config file')
firstLine.should.contains(configFile)
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
}
})
it('auto detect .ncurc.cjs', async () => {
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'npm-check-updates-'))
const configFile = path.join(tempDir, '.ncurc.cjs')
const pkgFile = path.join(tempDir, 'package.json')
await fs.writeFile(configFile, 'module.exports = { "filter": "ncu-test-v2" }', 'utf-8')
await fs.writeFile(
pkgFile,
JSON.stringify({ dependencies: { 'ncu-test-v2': '1.0.0', 'ncu-test-tag': '0.1.0' } }),
'utf-8',
)
try {
// awkwardly, we have to set mergeConfig to enable autodetecting the rcconfig because otherwise it is explicitly disabled for tests
const { stdout } = await spawn('node', [bin, '--mergeConfig'], {}, { cwd: tempDir })
const firstLine = stdout.split('\n')[0]
// On OSX tempDir is /var/folders/cb/12345, but npm-check-updates recieves /private/var/folders/cb/12345.
// Apparently OSX symlinks /tmp to /private/tmp for historical reasons.
// Therefore, ignore any directories prepended to the config file path.
firstLine.should.contains('Using config file')
firstLine.should.contains(configFile)
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
}
})
it('should not crash if because of $schema property', async () => {
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'npm-check-updates-'))
const configFile = path.join(tempDir, '.ncurc.json')
const pkgFile = path.join(tempDir, 'package.json')
await fs.writeFile(configFile, JSON.stringify({ $schema: 'schema url' }), 'utf-8')
await fs.writeFile(pkgFile, JSON.stringify({ dependencies: { axios: '1.0.0' } }), 'utf-8')
try {
// awkwardly, we have to set mergeConfig to enable autodetecting the rcconfig because otherwise it is explicitly disabled for tests
await spawn('node', [bin, '--mergeConfig'], {}, { cwd: tempDir })
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
}
})
describe('config functions', () => {
it('filter function', async () => {
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'npm-check-updates-'))
const configFile = path.join(tempDir, '.ncurc.js')
const pkgFile = path.join(tempDir, 'package.json')
await fs.writeFile(
configFile,
`module.exports = {
filter: name => name.endsWith('tag')
}`,
'utf-8',
)
await fs.writeFile(
pkgFile,
JSON.stringify({ dependencies: { 'ncu-test-v2': '1.0.0', 'ncu-test-tag': '0.1.0' } }),
'utf-8',
)
try {
// awkwardly, we have to set mergeConfig to enable autodetecting the rcconfig because otherwise it is explicitly disabled for tests
const { stdout } = await spawn('node', [bin, '--mergeConfig', '--jsonUpgraded'], {}, { cwd: tempDir })
const pkgData = JSON.parse(stdout)
pkgData.should.not.have.property('ncu-test-v2')
pkgData.should.have.property('ncu-test-tag')
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
}
})
it('filterVersion function', async () => {
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'npm-check-updates-'))
const configFile = path.join(tempDir, '.ncurc.js')
const pkgFile = path.join(tempDir, 'package.json')
await fs.writeFile(
configFile,
`module.exports = {
filterVersion: version => version === '1.0.0'
}`,
'utf-8',
)
await fs.writeFile(
pkgFile,
JSON.stringify({ dependencies: { 'ncu-test-v2': '1.0.0', 'ncu-test-tag': '0.1.0' } }),
'utf-8',
)
try {
// awkwardly, we have to set mergeConfig to enable autodetecting the rcconfig because otherwise it is explicitly disabled for tests
const { stdout } = await spawn('node', [bin, '--mergeConfig', '--jsonUpgraded'], {}, { cwd: tempDir })
const pkgData = JSON.parse(stdout)
pkgData.should.have.property('ncu-test-v2')
pkgData.should.not.have.property('ncu-test-tag')
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
}
})
it('filterResults function', async () => {
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'npm-check-updates-'))
const configFile = path.join(tempDir, '.ncurc.js')
const pkgFile = path.join(tempDir, 'package.json')
await fs.writeFile(
configFile,
`module.exports = {
filterResults: (name, { upgradedVersion }) => upgradedVersion === '99.9.9'
}`,
'utf-8',
)
await fs.writeFile(
pkgFile,
JSON.stringify({ dependencies: { 'ncu-test-v2': '1.0.0', 'ncu-test-tag': '0.1.0' } }),
'utf-8',
)
try {
// awkwardly, we have to set mergeConfig to enable autodetecting the rcconfig because otherwise it is explicitly disabled for tests
const { stdout } = await spawn('node', [bin, '--mergeConfig', '--jsonUpgraded'], {}, { cwd: tempDir })
const pkgData = JSON.parse(stdout)
pkgData.should.have.property('ncu-test-v2')
pkgData.should.have.property('ncu-test-tag')
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
}
})
it('reject function', async () => {
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'npm-check-updates-'))
const configFile = path.join(tempDir, '.ncurc.js')
const pkgFile = path.join(tempDir, 'package.json')
await fs.writeFile(
configFile,
`module.exports = {
reject: name => name.endsWith('tag')
}`,
'utf-8',
)
await fs.writeFile(
pkgFile,
JSON.stringify({ dependencies: { 'ncu-test-v2': '1.0.0', 'ncu-test-tag': '0.1.0' } }),
'utf-8',
)
try {
// awkwardly, we have to set mergeConfig to enable autodetecting the rcconfig because otherwise it is explicitly disabled for tests
const { stdout } = await spawn('node', [bin, '--mergeConfig', '--jsonUpgraded'], {}, { cwd: tempDir })
const pkgData = JSON.parse(stdout)
pkgData.should.have.property('ncu-test-v2')
pkgData.should.not.have.property('ncu-test-tag')
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
}
})
it('rejectVersion function', async () => {
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'npm-check-updates-'))
const configFile = path.join(tempDir, '.ncurc.js')
const pkgFile = path.join(tempDir, 'package.json')
await fs.writeFile(
configFile,
`module.exports = {
rejectVersion: version => version === '1.0.0'
}`,
'utf-8',
)
await fs.writeFile(
pkgFile,
JSON.stringify({ dependencies: { 'ncu-test-v2': '1.0.0', 'ncu-test-tag': '0.1.0' } }),
'utf-8',
)
try {
// awkwardly, we have to set mergeConfig to enable autodetecting the rcconfig because otherwise it is explicitly disabled for tests
const { stdout } = await spawn('node', [bin, '--mergeConfig', '--jsonUpgraded'], {}, { cwd: tempDir })
const pkgData = JSON.parse(stdout)
pkgData.should.not.have.property('ncu-test-v2')
pkgData.should.have.property('ncu-test-tag')
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
}
})
})
})