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
|
const { test } = require('tap')
const requireInject = require('require-inject')
let getIdentityImpl = () => 'someperson'
let npmFetchBody = null
const npmFetch = async (uri, opts) => {
npmFetchBody = opts.body
}
npmFetch.json = async (uri, opts) => {
return {
versions: {
'1.0.0': {},
'1.0.1': {},
'1.0.1-pre': {},
},
}
}
const deprecate = requireInject('../../lib/deprecate.js', {
'../../lib/npm.js': {
flatOptions: { registry: 'https://registry.npmjs.org' },
},
'../../lib/utils/get-identity.js': async () => getIdentityImpl(),
'../../lib/utils/otplease.js': async (opts, fn) => fn(opts),
libnpmaccess: {
lsPackages: async () => ({ foo: 'write', bar: 'write', baz: 'write', buzz: 'read' }),
},
'npm-registry-fetch': npmFetch,
})
test('completion', async t => {
const defaultIdentityImpl = getIdentityImpl
t.teardown(() => {
getIdentityImpl = defaultIdentityImpl
})
const { completion } = deprecate
const testComp = (argv, expect) => {
return new Promise((resolve, reject) => {
completion({ conf: { argv: { remain: argv } } }, (err, res) => {
if (err)
return reject(err)
t.strictSame(res, expect, `completion: ${argv}`)
resolve()
})
})
}
await testComp([], ['foo', 'bar', 'baz'])
await testComp(['b'], ['bar', 'baz'])
await testComp(['fo'], ['foo'])
await testComp(['g'], [])
await testComp(['foo', 'something'], [])
getIdentityImpl = () => {
throw new Error('unknown failure')
}
t.rejects(testComp([], []), /unknown failure/)
})
test('no args', t => {
deprecate([], (err) => {
t.match(err, /Usage: npm deprecate/, 'logs usage')
t.end()
})
})
test('only one arg', t => {
deprecate(['foo'], (err) => {
t.match(err, /Usage: npm deprecate/, 'logs usage')
t.end()
})
})
test('invalid semver range', t => {
deprecate(['foo@notaversion', 'this will fail'], (err) => {
t.match(err, /invalid version range/, 'logs semver error')
t.end()
})
})
test('deprecates given range', t => {
t.teardown(() => {
npmFetchBody = null
})
deprecate(['foo@1.0.0', 'this version is deprecated'], (err) => {
if (err)
throw err
t.match(npmFetchBody, {
versions: {
'1.0.0': {
deprecated: 'this version is deprecated',
},
'1.0.1': {
// the undefined here is necessary to ensure that we absolutely
// did not assign this property
deprecated: undefined,
},
},
})
t.end()
})
})
test('deprecates all versions when no range is specified', t => {
t.teardown(() => {
npmFetchBody = null
})
deprecate(['foo', 'this version is deprecated'], (err) => {
if (err)
throw err
t.match(npmFetchBody, {
versions: {
'1.0.0': {
deprecated: 'this version is deprecated',
},
'1.0.1': {
deprecated: 'this version is deprecated',
},
'1.0.1-pre': {
deprecated: 'this version is deprecated',
},
},
})
t.end()
})
})
|