Skip to content

Commit ef2a8a3

Browse files
committed
chore: remove get-idendity mock from whoami tests
1 parent 47438ff commit ef2a8a3

File tree

4 files changed

+127
-55
lines changed

4 files changed

+127
-55
lines changed

test/fixtures/mock-registry.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ class MockRegistry {
2323

2424
get nock () {
2525
if (!this.#nock) {
26+
if (!this.#tap) {
27+
throw new Error('cannot mock packages without a tap fixture')
28+
}
2629
const tnock = require('./tnock.js')
2730
const reqheaders = {}
2831
if (this.#authorization) {
@@ -37,11 +40,12 @@ class MockRegistry {
3740
this.#nock = nock
3841
}
3942

43+
async whoami ({ username }) {
44+
this.nock.get('/-/whoami').reply(200, { username })
45+
}
46+
4047
async package ({ manifest, times = 1, query, tarballs }) {
4148
let nock = this.nock
42-
if (!nock) {
43-
throw new Error('cannot mock packages without a tap fixture')
44-
}
4549
nock = nock.get(`/${manifest.name}`).times(times)
4650
if (query) {
4751
nock = nock.query(query)

test/lib/commands/dedupe.js

+31-30
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const t = require('tap')
2-
const { load: loadMockNpm } = require('../../fixtures/mock-npm')
32
const path = require('path')
43
const fs = require('fs')
54

5+
const { load: loadMockNpm } = require('../../fixtures/mock-npm')
66
const MockRegistry = require('../../fixtures/mock-registry.js')
77

88
t.test('should throw in global mode', async (t) => {
@@ -18,45 +18,43 @@ t.test('should throw in global mode', async (t) => {
1818
)
1919
})
2020

21-
const testTop = {
22-
name: 'test-top',
23-
version: '1.0.0',
24-
dependencies: {
25-
'test-dep-a': '*',
26-
'test-dep-b': '*',
27-
},
28-
}
29-
const testDepA = {
30-
name: 'test-dep-a',
31-
version: '1.0.1',
32-
dependencies: { 'test-sub': '*' },
33-
}
34-
const testDepB = {
35-
name: 'test-dep-b',
36-
version: '1.0.0',
37-
dependencies: { 'test-sub': '*' },
38-
}
39-
const testSub = {
40-
name: 'test-sub',
41-
version: '1.0.0',
42-
}
43-
4421
const treeWithDupes = {
45-
'package.json': JSON.stringify(testTop),
22+
'package.json': JSON.stringify({
23+
name: 'test-top',
24+
version: '1.0.0',
25+
dependencies: {
26+
'test-dep-a': '*',
27+
'test-dep-b': '*',
28+
},
29+
}),
4630
node_modules: {
4731
'test-dep-a': {
48-
'package.json': JSON.stringify(testDepA),
32+
'package.json': JSON.stringify({
33+
name: 'test-dep-a',
34+
version: '1.0.1',
35+
dependencies: { 'test-sub': '*' },
36+
}),
4937
node_modules: {
5038
'test-sub': {
51-
'package.json': JSON.stringify(testSub),
39+
'package.json': JSON.stringify({
40+
name: 'test-sub',
41+
version: '1.0.0',
42+
}),
5243
},
5344
},
5445
},
5546
'test-dep-b': {
56-
'package.json': JSON.stringify(testDepB),
47+
'package.json': JSON.stringify({
48+
name: 'test-dep-b',
49+
version: '1.0.0',
50+
dependencies: { 'test-sub': '*' },
51+
}),
5752
node_modules: {
5853
'test-sub': {
59-
'package.json': JSON.stringify(testSub),
54+
'package.json': JSON.stringify({
55+
name: 'test-sub',
56+
version: '1.0.0',
57+
}),
6058
},
6159
},
6260
},
@@ -84,7 +82,10 @@ t.test('dedupe', async (t) => {
8482
})
8583
await npm.exec('dedupe', [])
8684
t.match(joinedOutput(), /added 1 package, and removed 2 packages/)
87-
t.ok(fs.existsSync(path.join(npm.prefix, 'node_modules', 'test-sub')), 'test-sub was hoisted')
85+
t.ok(
86+
fs.existsSync(path.join(npm.prefix, 'node_modules', 'test-sub')),
87+
'test-sub was hoisted'
88+
)
8889
t.notOk(
8990
fs.existsSync(path.join(npm.prefix, 'node_modules', 'test-dep-a', 'node_modules', 'test-sub')),
9091
'test-dep-a/test-sub was removed'

test/lib/commands/find-dupes.js

+69-13
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,84 @@
11
const t = require('tap')
2+
const path = require('path')
3+
const fs = require('fs')
24

35
const { load: loadMockNpm } = require('../../fixtures/mock-npm')
6+
const MockRegistry = require('../../fixtures/mock-registry.js')
47

5-
t.test('should run dedupe in dryRun mode', async (t) => {
6-
t.plan(5)
7-
const { npm } = await loadMockNpm(t, {
8-
mocks: {
9-
'@npmcli/arborist': function (args) {
10-
t.ok(args, 'gets options object')
11-
t.ok(args.path, 'gets path option')
12-
t.ok(args.dryRun, 'is called in dryRun mode')
13-
this.dedupe = () => {
14-
t.ok(true, 'dedupe is called')
15-
}
8+
const treeWithDupes = {
9+
'package.json': JSON.stringify({
10+
name: 'test-top',
11+
version: '1.0.0',
12+
dependencies: {
13+
'test-dep-a': '*',
14+
'test-dep-b': '*',
15+
},
16+
}),
17+
node_modules: {
18+
'test-dep-a': {
19+
'package.json': JSON.stringify({
20+
name: 'test-dep-a',
21+
version: '1.0.1',
22+
dependencies: { 'test-sub': '*' },
23+
}),
24+
node_modules: {
25+
'test-sub': {
26+
'package.json': JSON.stringify({
27+
name: 'test-sub',
28+
version: '1.0.0',
29+
}),
30+
},
1631
},
17-
'../../lib/utils/reify-finish.js': (npm, arb) => {
18-
t.ok(arb, 'gets arborist tree')
32+
},
33+
'test-dep-b': {
34+
'package.json': JSON.stringify({
35+
name: 'test-dep-b',
36+
version: '1.0.0',
37+
dependencies: { 'test-sub': '*' },
38+
}),
39+
node_modules: {
40+
'test-sub': {
41+
'package.json': JSON.stringify({
42+
name: 'test-sub',
43+
version: '1.0.0',
44+
}),
45+
},
1946
},
2047
},
48+
},
49+
}
50+
51+
t.test('should run dedupe in dryRun mode', async (t) => {
52+
const { npm, joinedOutput } = await loadMockNpm(t, {
53+
prefixDir: treeWithDupes,
2154
config: {
2255
// explicitly set to false so we can be 100% sure it's always true when it
2356
// hits arborist
2457
'dry-run': false,
2558
},
2659
})
60+
const registry = new MockRegistry({
61+
tap: t,
62+
registry: npm.config.get('registry'),
63+
})
64+
65+
const manifestSub = registry.manifest({
66+
name: 'test-sub',
67+
manifests: [{ name: 'test-sub', version: '1.0.0' }],
68+
})
69+
70+
await registry.package({ manifest: manifestSub })
2771
await npm.exec('find-dupes', [])
72+
t.match(joinedOutput(), /added 1 package, and removed 2 packages/)
73+
t.notOk(
74+
fs.existsSync(path.join(npm.prefix, 'node_modules', 'test-sub')),
75+
'test-sub was not hoisted'
76+
)
77+
t.ok(
78+
fs.existsSync(path.join(npm.prefix, 'node_modules', 'test-dep-a', 'node_modules', 'test-sub')),
79+
'test-dep-a/test-sub was not removed'
80+
)
81+
t.ok(
82+
fs.existsSync(path.join(npm.prefix, 'node_modules', 'test-dep-b', 'node_modules', 'test-sub')),
83+
'test-dep-b/test-sub was not removed')
2884
})

test/lib/commands/whoami.js

+20-9
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,35 @@
11
const t = require('tap')
2-
const { load: _loadMockNpm } = require('../../fixtures/mock-npm')
2+
const { load: loadMockNpm } = require('../../fixtures/mock-npm')
3+
const MockRegistry = require('../../fixtures/mock-registry.js')
34

45
const username = 'foo'
5-
const loadMockNpm = (t, options) => _loadMockNpm(t, {
6-
mocks: {
7-
'../../lib/utils/get-identity.js': () => Promise.resolve(username),
8-
},
9-
...options,
10-
})
6+
const auth = { '//registry.npmjs.org/:_authToken': 'test-auth-token' }
117

128
t.test('npm whoami', async (t) => {
13-
const { npm, joinedOutput } = await loadMockNpm(t)
9+
const { npm, joinedOutput } = await loadMockNpm(t, { config: auth })
10+
const registry = new MockRegistry({
11+
tap: t,
12+
registry: npm.config.get('registry'),
13+
authorization: 'test-auth-token',
14+
})
15+
registry.whoami({ username })
1416
await npm.exec('whoami', [])
1517
t.equal(joinedOutput(), username, 'should print username')
1618
})
1719

1820
t.test('npm whoami --json', async (t) => {
1921
const { npm, joinedOutput } = await loadMockNpm(t, {
20-
config: { json: true },
22+
config: {
23+
json: true,
24+
...auth,
25+
},
26+
})
27+
const registry = new MockRegistry({
28+
tap: t,
29+
registry: npm.config.get('registry'),
30+
authorization: 'test-auth-token',
2131
})
32+
registry.whoami({ username })
2233
await npm.exec('whoami', [])
2334
t.equal(JSON.parse(joinedOutput()), username, 'should print username')
2435
})

0 commit comments

Comments
 (0)