|
1 | 1 | const t = require('tap') |
| 2 | +const { load: loadMockNpm } = require('../../fixtures/mock-npm') |
2 | 3 |
|
3 | | -let getIdentityImpl = () => 'someperson' |
4 | | -let npmFetchBody = null |
| 4 | +const MockRegistry = require('../../fixtures/mock-registry.js') |
5 | 5 |
|
6 | | -const npmFetch = async (uri, opts) => { |
7 | | - npmFetchBody = opts.body |
8 | | -} |
| 6 | +const user = 'test-user' |
| 7 | +const token = 'test-auth-token' |
| 8 | +const auth = { '//registry.npmjs.org/:_authToken': token } |
| 9 | +const versions = ['1.0.0', '1.0.1', '1.0.1-pre'] |
9 | 10 |
|
10 | | -npmFetch.json = async (uri, opts) => { |
11 | | - return { |
12 | | - versions: { |
13 | | - '1.0.0': {}, |
14 | | - '1.0.1': {}, |
15 | | - '1.0.1-pre': {}, |
16 | | - }, |
17 | | - } |
18 | | -} |
19 | | - |
20 | | -const Deprecate = t.mock('../../../lib/commands/deprecate.js', { |
21 | | - '../../../lib/utils/get-identity.js': async () => getIdentityImpl(), |
22 | | - libnpmaccess: { |
23 | | - lsPackages: async () => ({ foo: 'write', bar: 'write', baz: 'write', buzz: 'read' }), |
24 | | - }, |
25 | | - 'npm-registry-fetch': npmFetch, |
26 | | -}) |
27 | | - |
28 | | -const deprecate = new Deprecate({ |
29 | | - flatOptions: { registry: 'https://registry.npmjs.org' }, |
30 | | -}) |
| 11 | +// libnpmaccess maps these to read-write and read-only |
| 12 | +const packages = { foo: 'write', bar: 'write', baz: 'write', buzz: 'read' } |
31 | 13 |
|
32 | 14 | t.test('completion', async t => { |
33 | | - const defaultIdentityImpl = getIdentityImpl |
34 | | - t.teardown(() => { |
35 | | - getIdentityImpl = defaultIdentityImpl |
| 15 | + const { npm } = await loadMockNpm(t, { |
| 16 | + config: { |
| 17 | + ...auth, |
| 18 | + }, |
36 | 19 | }) |
37 | 20 |
|
| 21 | + const deprecate = await npm.cmd('deprecate') |
38 | 22 | const testComp = async (argv, expect) => { |
39 | 23 | const res = |
40 | 24 | await deprecate.completion({ conf: { argv: { remain: argv } } }) |
41 | 25 | t.strictSame(res, expect, `completion: ${argv}`) |
42 | 26 | } |
43 | 27 |
|
| 28 | + const registry = new MockRegistry({ |
| 29 | + tap: t, |
| 30 | + registry: npm.config.get('registry'), |
| 31 | + authorization: token, |
| 32 | + }) |
| 33 | + |
| 34 | + registry.whoami({ username: user, times: 4 }) |
| 35 | + registry.lsPackages({ team: user, packages, times: 4 }) |
44 | 36 | await Promise.all([ |
45 | 37 | testComp([], ['foo', 'bar', 'baz']), |
46 | 38 | testComp(['b'], ['bar', 'baz']), |
47 | 39 | testComp(['fo'], ['foo']), |
48 | 40 | testComp(['g'], []), |
49 | | - testComp(['foo', 'something'], []), |
50 | 41 | ]) |
51 | 42 |
|
52 | | - getIdentityImpl = () => { |
53 | | - throw new Error('deprecate test failure') |
54 | | - } |
| 43 | + await testComp(['foo', 'something'], []) |
| 44 | + |
| 45 | + registry.whoami({ statusCode: 404, body: {} }) |
55 | 46 |
|
56 | | - t.rejects(testComp([], []), { message: 'deprecate test failure' }) |
| 47 | + t.rejects(testComp([], []), { code: 'ENEEDAUTH' }) |
57 | 48 | }) |
58 | 49 |
|
59 | 50 | t.test('no args', async t => { |
| 51 | + const { npm } = await loadMockNpm(t) |
60 | 52 | await t.rejects( |
61 | | - deprecate.exec([]), |
62 | | - /Usage:/, |
| 53 | + npm.exec('deprecate', []), |
| 54 | + { code: 'EUSAGE' }, |
63 | 55 | 'logs usage' |
64 | 56 | ) |
65 | 57 | }) |
66 | 58 |
|
67 | 59 | t.test('only one arg', async t => { |
| 60 | + const { npm } = await loadMockNpm(t) |
68 | 61 | await t.rejects( |
69 | | - deprecate.exec(['foo']), |
70 | | - /Usage:/, |
| 62 | + npm.exec('deprecate', ['foo']), |
| 63 | + { code: 'EUSAGE' }, |
71 | 64 | 'logs usage' |
72 | 65 | ) |
73 | 66 | }) |
74 | 67 |
|
75 | 68 | t.test('invalid semver range', async t => { |
| 69 | + const { npm } = await loadMockNpm(t) |
76 | 70 | await t.rejects( |
77 | | - deprecate.exec(['foo@notaversion', 'this will fail']), |
| 71 | + npm.exec('deprecate', ['foo@notaversion', 'this will fail']), |
78 | 72 | /invalid version range/, |
79 | 73 | 'logs semver error' |
80 | 74 | ) |
81 | 75 | }) |
82 | 76 |
|
83 | 77 | t.test('undeprecate', async t => { |
84 | | - t.teardown(() => { |
85 | | - npmFetchBody = null |
| 78 | + const { npm, joinedOutput } = await loadMockNpm(t, { config: { ...auth } }) |
| 79 | + const registry = new MockRegistry({ |
| 80 | + tap: t, |
| 81 | + registry: npm.config.get('registry'), |
| 82 | + authorization: token, |
86 | 83 | }) |
87 | | - await deprecate.exec(['foo', '']) |
88 | | - t.match(npmFetchBody, { |
89 | | - versions: { |
90 | | - '1.0.0': { deprecated: '' }, |
91 | | - '1.0.1': { deprecated: '' }, |
92 | | - '1.0.1-pre': { deprecated: '' }, |
93 | | - }, |
94 | | - }, 'undeprecates everything') |
| 84 | + const manifest = registry.manifest({ |
| 85 | + name: 'foo', |
| 86 | + versions, |
| 87 | + }) |
| 88 | + registry.package({ manifest, query: { write: true } }) |
| 89 | + registry.nock.put('/foo', body => { |
| 90 | + for (const version of versions) { |
| 91 | + if (body.versions[version].deprecated !== '') { |
| 92 | + return false |
| 93 | + } |
| 94 | + } |
| 95 | + return true |
| 96 | + }).reply(200, {}) |
| 97 | + |
| 98 | + await npm.exec('deprecate', ['foo', '']) |
| 99 | + t.match(joinedOutput(), '') |
95 | 100 | }) |
96 | 101 |
|
97 | 102 | t.test('deprecates given range', async t => { |
98 | | - t.teardown(() => { |
99 | | - npmFetchBody = null |
| 103 | + const { npm, joinedOutput } = await loadMockNpm(t, { config: { ...auth } }) |
| 104 | + const registry = new MockRegistry({ |
| 105 | + tap: t, |
| 106 | + registry: npm.config.get('registry'), |
| 107 | + authorization: token, |
100 | 108 | }) |
101 | | - |
102 | | - await deprecate.exec(['[email protected]', 'this version is deprecated']) |
103 | | - t.match(npmFetchBody, { |
104 | | - versions: { |
105 | | - '1.0.0': { |
106 | | - deprecated: 'this version is deprecated', |
107 | | - }, |
108 | | - '1.0.1': { |
109 | | - // the undefined here is necessary to ensure that we absolutely |
110 | | - // did not assign this property |
111 | | - deprecated: undefined, |
112 | | - }, |
113 | | - }, |
| 109 | + const manifest = registry.manifest({ |
| 110 | + name: 'foo', |
| 111 | + versions, |
114 | 112 | }) |
| 113 | + registry.package({ manifest, query: { write: true } }) |
| 114 | + const message = 'test deprecation message' |
| 115 | + registry.nock.put('/foo', body => { |
| 116 | + if (body.versions['1.0.1'].deprecated) { |
| 117 | + return false |
| 118 | + } |
| 119 | + if (body.versions['1.0.1-pre'].deprecated) { |
| 120 | + return false |
| 121 | + } |
| 122 | + return body.versions['1.0.0'].deprecated === message |
| 123 | + }).reply(200, {}) |
| 124 | + await npm.exec('deprecate', ['[email protected]', message]) |
| 125 | + t.match(joinedOutput(), '') |
115 | 126 | }) |
116 | 127 |
|
117 | 128 | t.test('deprecates all versions when no range is specified', async t => { |
118 | | - t.teardown(() => { |
119 | | - npmFetchBody = null |
| 129 | + const { npm, joinedOutput } = await loadMockNpm(t, { config: { ...auth } }) |
| 130 | + const registry = new MockRegistry({ |
| 131 | + tap: t, |
| 132 | + registry: npm.config.get('registry'), |
| 133 | + authorization: token, |
120 | 134 | }) |
121 | | - |
122 | | - await deprecate.exec(['foo', 'this version is deprecated']) |
123 | | - |
124 | | - t.match(npmFetchBody, { |
125 | | - versions: { |
126 | | - '1.0.0': { |
127 | | - deprecated: 'this version is deprecated', |
128 | | - }, |
129 | | - '1.0.1': { |
130 | | - deprecated: 'this version is deprecated', |
131 | | - }, |
132 | | - '1.0.1-pre': { |
133 | | - deprecated: 'this version is deprecated', |
134 | | - }, |
135 | | - }, |
| 135 | + const manifest = registry.manifest({ |
| 136 | + name: 'foo', |
| 137 | + versions, |
136 | 138 | }) |
| 139 | + registry.package({ manifest, query: { write: true } }) |
| 140 | + const message = 'test deprecation message' |
| 141 | + registry.nock.put('/foo', body => { |
| 142 | + for (const version of versions) { |
| 143 | + if (body.versions[version].deprecated !== message) { |
| 144 | + return false |
| 145 | + } |
| 146 | + } |
| 147 | + return true |
| 148 | + }).reply(200, {}) |
| 149 | + |
| 150 | + await npm.exec('deprecate', ['foo', message]) |
| 151 | + t.match(joinedOutput(), '') |
137 | 152 | }) |
0 commit comments