-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
deprecate.js
171 lines (152 loc) · 4.51 KB
/
deprecate.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
166
167
168
169
170
171
const t = require('tap')
const { load: loadMockNpm } = require('../../fixtures/mock-npm')
const MockRegistry = require('@npmcli/mock-registry')
const user = 'test-user'
const token = 'test-auth-token'
const auth = { '//registry.npmjs.org/:_authToken': token }
const versions = ['1.0.0', '1.0.1', '1.0.1-pre']
// libnpmaccess maps these to read-write and read-only
const packages = { foo: 'write', bar: 'write', baz: 'write', buzz: 'read' }
t.test('completion', async t => {
const { npm, deprecate } = await loadMockNpm(t, {
command: 'deprecate',
config: {
...auth,
},
})
const testComp = async (argv, expect) => {
const res =
await deprecate.completion({ conf: { argv: { remain: argv } } })
t.strictSame(res, expect, `completion: ${argv}`)
}
const registry = new MockRegistry({
tap: t,
registry: npm.config.get('registry'),
authorization: token,
})
registry.whoami({ username: user, times: 4 })
registry.getPackages({ team: user, packages, times: 4 })
await Promise.all([
testComp([], ['foo', 'bar', 'baz']),
testComp(['b'], ['bar', 'baz']),
testComp(['fo'], ['foo']),
testComp(['g'], []),
])
await testComp(['foo', 'something'], [])
registry.whoami({ responseCode: 401, body: {} })
await t.rejects(
testComp([], []),
{ code: 'E401' }
)
})
t.test('no args', async t => {
const { npm } = await loadMockNpm(t)
await t.rejects(
npm.exec('deprecate', []),
{ code: 'EUSAGE' },
'logs usage'
)
})
t.test('only one arg', async t => {
const { npm } = await loadMockNpm(t)
await t.rejects(
npm.exec('deprecate', ['foo']),
{ code: 'EUSAGE' },
'logs usage'
)
})
t.test('invalid semver range', async t => {
const { npm } = await loadMockNpm(t)
await t.rejects(
npm.exec('deprecate', ['foo@notaversion', 'this will fail']),
/invalid version range/,
'logs semver error'
)
})
t.test('undeprecate', async t => {
const { npm, joinedOutput } = await loadMockNpm(t, { config: { ...auth } })
const registry = new MockRegistry({
tap: t,
registry: npm.config.get('registry'),
authorization: token,
})
const manifest = registry.manifest({
name: 'foo',
versions,
})
await registry.package({ manifest, query: { write: true } })
registry.nock.put('/foo', body => {
for (const version of versions) {
if (body.versions[version].deprecated !== '') {
return false
}
}
return true
}).reply(200, {})
await npm.exec('deprecate', ['foo', ''])
t.match(joinedOutput(), '')
})
t.test('deprecates given range', async t => {
const { npm, joinedOutput } = await loadMockNpm(t, { config: { ...auth } })
const registry = new MockRegistry({
tap: t,
registry: npm.config.get('registry'),
authorization: token,
})
const manifest = registry.manifest({
name: 'foo',
versions,
})
await registry.package({ manifest, query: { write: true } })
const message = 'test deprecation message'
registry.nock.put('/foo', body => {
if (body.versions['1.0.1'].deprecated) {
return false
}
if (body.versions['1.0.1-pre'].deprecated) {
return false
}
return body.versions['1.0.0'].deprecated === message
}).reply(200, {})
await npm.exec('deprecate', ['[email protected]', message])
t.match(joinedOutput(), '')
})
t.test('deprecates all versions when no range is specified', async t => {
const { npm, joinedOutput } = await loadMockNpm(t, { config: { ...auth } })
const registry = new MockRegistry({
tap: t,
registry: npm.config.get('registry'),
authorization: token,
})
const manifest = registry.manifest({
name: 'foo',
versions,
})
await registry.package({ manifest, query: { write: true } })
const message = 'test deprecation message'
registry.nock.put('/foo', body => {
for (const version of versions) {
if (body.versions[version].deprecated !== message) {
return false
}
}
return true
}).reply(200, {})
await npm.exec('deprecate', ['foo', message])
t.match(joinedOutput(), '')
})
t.test('does nothing if version does not actually exist', async t => {
const { npm, joinedOutput } = await loadMockNpm(t, { config: { ...auth } })
const registry = new MockRegistry({
tap: t,
registry: npm.config.get('registry'),
authorization: token,
})
const manifest = registry.manifest({
name: 'foo',
versions,
})
await registry.package({ manifest, query: { write: true } })
await npm.exec('deprecate', ['[email protected]', 'this should be ignored'])
t.match(joinedOutput(), '')
})