-
Notifications
You must be signed in to change notification settings - Fork 339
/
Copy pathbin.test.ts
386 lines (339 loc) · 14.9 KB
/
bin.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import fs from 'fs/promises'
import os from 'os'
import path from 'path'
import spawn from 'spawn-please'
import { Index } from '../src/types/IndexType'
import { Version } from '../src/types/Version'
import chaiSetup from './helpers/chaiSetup'
import stubVersions from './helpers/stubVersions'
chaiSetup()
const bin = path.join(__dirname, '../build/cli.js')
describe('bin', async function () {
it('fetch latest version from registry (not stubbed)', async () => {
const { stdout } = await spawn('node', [bin, '--jsonUpgraded', '--stdin'], {
stdin: JSON.stringify({ dependencies: { 'ncu-test-v2': '1.0.0' } }),
})
const pkgData = JSON.parse(stdout)
pkgData.should.have.property('ncu-test-v2')
})
it('output only upgraded with --jsonUpgraded', async () => {
const stub = stubVersions('99.9.9', { spawn: true })
const { stdout } = await spawn('node', [bin, '--jsonUpgraded', '--stdin'], {
stdin: JSON.stringify({ dependencies: { 'ncu-test-v2': '1.0.0' } }),
})
const pkgData = JSON.parse(stdout)
pkgData.should.have.property('ncu-test-v2')
stub.restore()
})
it('--loglevel verbose', async () => {
const stub = stubVersions('99.9.9', { spawn: true })
const { stdout } = await spawn('node', [bin, '--loglevel', 'verbose'], {
stdin: JSON.stringify({ dependencies: { 'ncu-test-v2': '1.0.0' } }),
})
stdout.should.containIgnoreCase('Initializing')
stdout.should.containIgnoreCase('Running in local mode')
stdout.should.containIgnoreCase('Finding package file data')
stub.restore()
})
it('--verbose', async () => {
const stub = stubVersions('99.9.9', { spawn: true })
const { stdout } = await spawn('node', [bin, '--verbose'], {
stdin: JSON.stringify({ dependencies: { 'ncu-test-v2': '1.0.0' } }),
})
stdout.should.containIgnoreCase('Initializing')
stdout.should.containIgnoreCase('Running in local mode')
stdout.should.containIgnoreCase('Finding package file data')
stub.restore()
})
it('accept stdin', async () => {
const stub = stubVersions('99.9.9', { spawn: true })
const { stdout } = await spawn('node', [bin, '--stdin'], {
stdin: JSON.stringify({ dependencies: { express: '1' } }),
})
stdout.trim().should.startWith('express')
stub.restore()
})
it('reject out-of-date stdin with errorLevel 2', async () => {
const stub = stubVersions('99.9.9', { spawn: true })
await spawn('node', [bin, '--stdin', '--errorLevel', '2'], {
stdin: JSON.stringify({ dependencies: { express: '1' } }),
}).should.eventually.be.rejectedWith('Dependencies not up-to-date')
stub.restore()
})
it('fall back to package.json search when receiving empty content on stdin', async () => {
const stub = stubVersions('99.9.9', { spawn: true })
const { stdout } = await spawn('node', [bin, '--stdin'])
stdout
.toString()
.trim()
.should.match(/^Checking .+package.json/)
stub.restore()
})
it('use package.json in cwd by default', async () => {
const stub = stubVersions('99.9.9', { spawn: true })
const { stdout } = await spawn('node', [bin, '--jsonUpgraded'], {}, { cwd: path.join(__dirname, 'test-data/ncu') })
const pkgData = JSON.parse(stdout)
pkgData.should.have.property('express')
stub.restore()
})
it('throw error if there is no package', async () => {
// run from tmp dir to avoid ncu analyzing the project's package.json
return spawn('node', [bin], {}, { cwd: os.tmpdir() }).should.eventually.be.rejectedWith('No package.json')
})
it('throw error if there is no package in --cwd', async () => {
return spawn('node', [bin, '--cwd', os.tmpdir()]).should.eventually.be.rejectedWith('No package.json')
})
it('throw error if --cwd does not exist', async () => {
return spawn('node', [bin, '--cwd', 'fnuoathufoawhtufonwauto']).should.eventually.be.rejectedWith(
'No such directory: fnuoathufoawhtufonwauto',
)
})
it('read --packageFile', async () => {
const stub = stubVersions('99.9.9', { spawn: true })
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'npm-check-updates-'))
const pkgFile = path.join(tempDir, 'package.json')
await fs.writeFile(pkgFile, JSON.stringify({ dependencies: { express: '1' } }), 'utf-8')
try {
const { stdout } = await spawn('node', [bin, '--jsonUpgraded', '--packageFile', pkgFile])
const pkgData = JSON.parse(stdout)
pkgData.should.have.property('express')
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
stub.restore()
}
})
it('write to --packageFile', async () => {
const stub = stubVersions('99.9.9', { spawn: true })
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'npm-check-updates-'))
const pkgFile = path.join(tempDir, 'package.json')
await fs.writeFile(pkgFile, JSON.stringify({ dependencies: { express: '1' } }), 'utf-8')
try {
await spawn('node', [bin, '-u', '--packageFile', pkgFile])
const upgradedPkg = JSON.parse(await fs.readFile(pkgFile, 'utf-8'))
upgradedPkg.should.have.property('dependencies')
upgradedPkg.dependencies.should.have.property('express')
upgradedPkg.dependencies.express.should.not.equal('1')
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
stub.restore()
}
})
it('write to --packageFile if errorLevel=2 and upgrades', async () => {
const stub = stubVersions('99.9.9', { spawn: true })
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'npm-check-updates-'))
const pkgFile = path.join(tempDir, 'package.json')
await fs.writeFile(pkgFile, JSON.stringify({ dependencies: { express: '1' } }), 'utf-8')
try {
await spawn('node', [bin, '-u', '--errorLevel', '2', '--packageFile', pkgFile]).should.eventually.be.rejectedWith(
'Dependencies not up-to-date',
)
const upgradedPkg = JSON.parse(await fs.readFile(pkgFile, 'utf-8'))
upgradedPkg.should.have.property('dependencies')
upgradedPkg.dependencies.should.have.property('express')
upgradedPkg.dependencies.express.should.not.equal('1')
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
stub.restore()
}
})
it('write to --packageFile with jsonUpgraded flag', async () => {
const stub = stubVersions('99.9.9', { spawn: true })
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'npm-check-updates-'))
const pkgFile = path.join(tempDir, 'package.json')
await fs.writeFile(pkgFile, JSON.stringify({ dependencies: { express: '1' } }), 'utf-8')
try {
await spawn('node', [bin, '-u', '--jsonUpgraded', '--packageFile', pkgFile])
const ugradedPkg = JSON.parse(await fs.readFile(pkgFile, 'utf-8'))
ugradedPkg.should.have.property('dependencies')
ugradedPkg.dependencies.should.have.property('express')
ugradedPkg.dependencies.express.should.not.equal('1')
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
stub.restore()
}
})
it('ignore stdin if --packageFile is specified', async () => {
const stub = stubVersions('99.9.9', { spawn: true })
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'npm-check-updates-'))
const pkgFile = path.join(tempDir, 'package.json')
await fs.writeFile(pkgFile, JSON.stringify({ dependencies: { express: '1' } }), 'utf-8')
try {
await spawn('node', [bin, '-u', '--stdin', '--packageFile', pkgFile], {
stdin: JSON.stringify({ dependencies: {} }),
})
const upgradedPkg = JSON.parse(await fs.readFile(pkgFile, 'utf-8'))
upgradedPkg.should.have.property('dependencies')
upgradedPkg.dependencies.should.have.property('express')
upgradedPkg.dependencies.express.should.not.equal('1')
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
stub.restore()
}
})
it('suppress stdout when --silent is provided', async () => {
const stub = stubVersions('99.9.9', { spawn: true })
const { stdout } = await spawn('node', [bin, '--silent'], {
stdin: JSON.stringify({ dependencies: { express: '1' } }),
})
stdout.trim().should.equal('')
stub.restore()
})
it('quote arguments with spaces in upgrade hint', async () => {
const stub = stubVersions('99.9.9', { spawn: true })
const pkgData = {
dependencies: {
'ncu-test-v2': '^1.0.0',
'ncu-test-tag': '^1.0.0',
},
}
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'npm-check-updates-'))
const pkgFile = path.join(tempDir, 'package.json')
await fs.writeFile(pkgFile, JSON.stringify(pkgData), 'utf-8')
try {
const { stdout } = await spawn('node', [bin, '--packageFile', pkgFile, '--filter', 'ncu-test-v2 ncu-test-tag'])
stdout.should.include('"ncu-test-v2 ncu-test-tag"')
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
stub.restore()
}
})
it('ignore file: and link: protocols', async () => {
const stub = stubVersions('99.9.9', { spawn: true })
const { default: stripAnsi } = await import('strip-ansi')
const dependencies = {
editor: 'file:../editor',
event: 'link:../link',
workspace: 'workspace:../workspace',
}
const { stdout } = await spawn('node', [bin, '--stdin'], { stdin: JSON.stringify({ dependencies }) })
stripAnsi(stdout)!.should.not.include('No package versions were returned.')
stub.restore()
})
it('combine boolean flags with arguments', async () => {
const stub = stubVersions('99.9.9', { spawn: true })
const { stdout } = await spawn('node', [bin, '--stdin', '--jsonUpgraded', 'ncu-test-v2'], {
stdin: JSON.stringify({ dependencies: { 'ncu-test-v2': '1.0.0', 'ncu-test-tag': '0.1.0' } }),
})
const upgraded = JSON.parse(stdout) as Index<Version>
upgraded.should.deep.equal({
'ncu-test-v2': '99.9.9',
})
stub.restore()
})
it('combine short boolean options with long options', async () => {
const stub = stubVersions('99.9.9', { spawn: true })
const promise = spawn('node', [bin, '-mp', 'foo'])
await promise.should.eventually.be.rejectedWith('Invalid package manager: foo')
stub.restore()
})
describe('embedded versions', () => {
it('strip url from Github url in "to" output', async () => {
// use dynamic import for ESM module
const { default: stripAnsi } = await import('strip-ansi')
const dependencies = {
'ncu-test-v2': 'https://github.com/raineorshine/ncu-test-v2.git#v1.0.0',
}
const { stdout } = await spawn('node', [bin, '--stdin'], { stdin: JSON.stringify({ dependencies }) })
stripAnsi(stdout)
.trim()
.should.equal('ncu-test-v2 https://github.com/raineorshine/ncu-test-v2.git#v1.0.0 → v2.0.0')
})
it('strip prefix from npm alias in "to" output', async () => {
const stub = stubVersions('99.9.9', { spawn: true })
// use dynamic import for ESM module
const { default: stripAnsi } = await import('strip-ansi')
const dependencies = {
request: 'npm:ncu-test-v2@1.0.0',
}
const { stdout } = await spawn('node', [bin, '--stdin'], { stdin: JSON.stringify({ dependencies }) })
stripAnsi(stdout).trim().should.equal('request npm:ncu-test-v2@1.0.0 → 99.9.9')
stub.restore()
})
})
describe('option-specific help', () => {
it('long option', async () => {
const { stdout } = await spawn('node', [bin, '--help', '--filter'])
stdout.trim().should.match(/^Usage:\s+ncu --filter/)
})
it('long option without "--" prefix', async () => {
const { stdout } = await spawn('node', [bin, '--help', 'filter'])
stdout.trim().should.match(/^Usage:\s+ncu --filter/)
})
it('short option', async () => {
const { stdout } = await spawn('node', [bin, '--help', '-f'])
stdout.trim().should.match(/^Usage:\s+ncu --filter/)
})
it('short option without "-" prefix', async () => {
const { stdout } = await spawn('node', [bin, '--help', 'f'])
stdout.trim().should.match(/^Usage:\s+ncu --filter/)
})
it('option with default', async () => {
const { stdout } = await spawn('node', [bin, '--help', '--concurrency'])
stdout.trim().should.containIgnoreCase('Default:')
})
it('option with extended help', async () => {
const { stdout } = await spawn('node', [bin, '--help', '--target'])
stdout.trim().should.containIgnoreCase('Upgrade to the highest version number')
})
it('-h', async () => {
const { stdout } = await spawn('node', [bin, '-h', '--filter'])
stdout.trim().should.match(/^Usage:\s+ncu --filter/)
})
it('unknown option', async () => {
const { stdout } = await spawn('node', [bin, '--help', '--foo'])
stdout.trim().should.containIgnoreCase('Unknown option')
})
it('multiple options', async () => {
const { stdout } = await spawn('node', [bin, '--help', '--interactive', '--minimal'])
stdout.trim().should.containIgnoreCase('ncu --interactive')
stdout.trim().should.containIgnoreCase('ncu --minimal')
})
// version is a special case since it is not included in cli-options.ts
it('--version', async () => {
const { stdout } = await spawn('node', [bin, '-h', '--version'])
stdout.trim().should.match(/^Usage:\s+ncu --version/)
})
it('-V', async () => {
const { stdout } = await spawn('node', [bin, '-h', '--version'])
stdout.trim().should.match(/^Usage:\s+ncu --version/)
})
it('-v', async () => {
const { stdout } = await spawn('node', [bin, '-h', '--version'])
stdout.trim().should.match(/^Usage:\s+ncu --version/)
})
describe('special --help help', () => {
it('--help --help', async () => {
const { stdout } = await spawn('node', [bin, '--help', '--help'])
stdout.trim().should.not.include('Usage')
})
it('--help help', async () => {
const { stdout } = await spawn('node', [bin, '--help', 'help'])
stdout.trim().should.not.include('Usage')
})
it('--help -h', async () => {
const { stdout } = await spawn('node', [bin, '--help', '-h'])
stdout.trim().should.not.include('Usage')
})
it('--help h', async () => {
const { stdout } = await spawn('node', [bin, '--help', 'h'])
stdout.trim().should.not.include('Usage')
})
it('-h --help', async () => {
const { stdout } = await spawn('node', [bin, '-h', '--help'])
stdout.trim().should.not.include('Usage')
})
it('-h help', async () => {
const { stdout } = await spawn('node', [bin, '-h', 'help'])
stdout.trim().should.not.include('Usage')
})
it('-h -h', async () => {
const { stdout } = await spawn('node', [bin, '-h', '-h'])
stdout.trim().should.not.include('Usage')
})
it('-h h', async () => {
const { stdout } = await spawn('node', [bin, '-h', 'h'])
stdout.trim().should.not.include('Usage')
})
})
})
})