diff options
author | Gar <gar+gh@danger.computer> | 2022-04-01 19:17:01 +0300 |
---|---|---|
committer | Gar <wraithgar@github.com> | 2022-04-04 21:35:15 +0300 |
commit | ef2a8a312f0f6784a84c7f4659ef2601a650d04e (patch) | |
tree | f0a0d927d24ecb4b52a02f40a0c5f8a6ab47f249 /test | |
parent | 47438ff19f4b6e84a0325ed73b97999ce34bc789 (diff) |
chore: remove get-idendity mock from whoami tests
Diffstat (limited to 'test')
-rw-r--r-- | test/fixtures/mock-registry.js | 10 | ||||
-rw-r--r-- | test/lib/commands/dedupe.js | 61 | ||||
-rw-r--r-- | test/lib/commands/find-dupes.js | 82 | ||||
-rw-r--r-- | test/lib/commands/whoami.js | 29 |
4 files changed, 127 insertions, 55 deletions
diff --git a/test/fixtures/mock-registry.js b/test/fixtures/mock-registry.js index ec4ebc2a4..34969b65f 100644 --- a/test/fixtures/mock-registry.js +++ b/test/fixtures/mock-registry.js @@ -23,6 +23,9 @@ class MockRegistry { get nock () { if (!this.#nock) { + if (!this.#tap) { + throw new Error('cannot mock packages without a tap fixture') + } const tnock = require('./tnock.js') const reqheaders = {} if (this.#authorization) { @@ -37,11 +40,12 @@ class MockRegistry { this.#nock = nock } + async whoami ({ username }) { + this.nock.get('/-/whoami').reply(200, { username }) + } + async package ({ manifest, times = 1, query, tarballs }) { let nock = this.nock - if (!nock) { - throw new Error('cannot mock packages without a tap fixture') - } nock = nock.get(`/${manifest.name}`).times(times) if (query) { nock = nock.query(query) diff --git a/test/lib/commands/dedupe.js b/test/lib/commands/dedupe.js index a985f4a71..0ca51245c 100644 --- a/test/lib/commands/dedupe.js +++ b/test/lib/commands/dedupe.js @@ -1,8 +1,8 @@ const t = require('tap') -const { load: loadMockNpm } = require('../../fixtures/mock-npm') const path = require('path') const fs = require('fs') +const { load: loadMockNpm } = require('../../fixtures/mock-npm') const MockRegistry = require('../../fixtures/mock-registry.js') t.test('should throw in global mode', async (t) => { @@ -18,45 +18,43 @@ t.test('should throw in global mode', async (t) => { ) }) -const testTop = { - name: 'test-top', - version: '1.0.0', - dependencies: { - 'test-dep-a': '*', - 'test-dep-b': '*', - }, -} -const testDepA = { - name: 'test-dep-a', - version: '1.0.1', - dependencies: { 'test-sub': '*' }, -} -const testDepB = { - name: 'test-dep-b', - version: '1.0.0', - dependencies: { 'test-sub': '*' }, -} -const testSub = { - name: 'test-sub', - version: '1.0.0', -} - const treeWithDupes = { - 'package.json': JSON.stringify(testTop), + 'package.json': JSON.stringify({ + name: 'test-top', + version: '1.0.0', + dependencies: { + 'test-dep-a': '*', + 'test-dep-b': '*', + }, + }), node_modules: { 'test-dep-a': { - 'package.json': JSON.stringify(testDepA), + 'package.json': JSON.stringify({ + name: 'test-dep-a', + version: '1.0.1', + dependencies: { 'test-sub': '*' }, + }), node_modules: { 'test-sub': { - 'package.json': JSON.stringify(testSub), + 'package.json': JSON.stringify({ + name: 'test-sub', + version: '1.0.0', + }), }, }, }, 'test-dep-b': { - 'package.json': JSON.stringify(testDepB), + 'package.json': JSON.stringify({ + name: 'test-dep-b', + version: '1.0.0', + dependencies: { 'test-sub': '*' }, + }), node_modules: { 'test-sub': { - 'package.json': JSON.stringify(testSub), + 'package.json': JSON.stringify({ + name: 'test-sub', + version: '1.0.0', + }), }, }, }, @@ -84,7 +82,10 @@ t.test('dedupe', async (t) => { }) await npm.exec('dedupe', []) t.match(joinedOutput(), /added 1 package, and removed 2 packages/) - t.ok(fs.existsSync(path.join(npm.prefix, 'node_modules', 'test-sub')), 'test-sub was hoisted') + t.ok( + fs.existsSync(path.join(npm.prefix, 'node_modules', 'test-sub')), + 'test-sub was hoisted' + ) t.notOk( fs.existsSync(path.join(npm.prefix, 'node_modules', 'test-dep-a', 'node_modules', 'test-sub')), 'test-dep-a/test-sub was removed' diff --git a/test/lib/commands/find-dupes.js b/test/lib/commands/find-dupes.js index 06bd097b6..228ac662b 100644 --- a/test/lib/commands/find-dupes.js +++ b/test/lib/commands/find-dupes.js @@ -1,28 +1,84 @@ const t = require('tap') +const path = require('path') +const fs = require('fs') const { load: loadMockNpm } = require('../../fixtures/mock-npm') +const MockRegistry = require('../../fixtures/mock-registry.js') -t.test('should run dedupe in dryRun mode', async (t) => { - t.plan(5) - const { npm } = await loadMockNpm(t, { - mocks: { - '@npmcli/arborist': function (args) { - t.ok(args, 'gets options object') - t.ok(args.path, 'gets path option') - t.ok(args.dryRun, 'is called in dryRun mode') - this.dedupe = () => { - t.ok(true, 'dedupe is called') - } +const treeWithDupes = { + 'package.json': JSON.stringify({ + name: 'test-top', + version: '1.0.0', + dependencies: { + 'test-dep-a': '*', + 'test-dep-b': '*', + }, + }), + node_modules: { + 'test-dep-a': { + 'package.json': JSON.stringify({ + name: 'test-dep-a', + version: '1.0.1', + dependencies: { 'test-sub': '*' }, + }), + node_modules: { + 'test-sub': { + 'package.json': JSON.stringify({ + name: 'test-sub', + version: '1.0.0', + }), + }, }, - '../../lib/utils/reify-finish.js': (npm, arb) => { - t.ok(arb, 'gets arborist tree') + }, + 'test-dep-b': { + 'package.json': JSON.stringify({ + name: 'test-dep-b', + version: '1.0.0', + dependencies: { 'test-sub': '*' }, + }), + node_modules: { + 'test-sub': { + 'package.json': JSON.stringify({ + name: 'test-sub', + version: '1.0.0', + }), + }, }, }, + }, +} + +t.test('should run dedupe in dryRun mode', async (t) => { + const { npm, joinedOutput } = await loadMockNpm(t, { + prefixDir: treeWithDupes, config: { // explicitly set to false so we can be 100% sure it's always true when it // hits arborist 'dry-run': false, }, }) + const registry = new MockRegistry({ + tap: t, + registry: npm.config.get('registry'), + }) + + const manifestSub = registry.manifest({ + name: 'test-sub', + manifests: [{ name: 'test-sub', version: '1.0.0' }], + }) + + await registry.package({ manifest: manifestSub }) await npm.exec('find-dupes', []) + t.match(joinedOutput(), /added 1 package, and removed 2 packages/) + t.notOk( + fs.existsSync(path.join(npm.prefix, 'node_modules', 'test-sub')), + 'test-sub was not hoisted' + ) + t.ok( + fs.existsSync(path.join(npm.prefix, 'node_modules', 'test-dep-a', 'node_modules', 'test-sub')), + 'test-dep-a/test-sub was not removed' + ) + t.ok( + fs.existsSync(path.join(npm.prefix, 'node_modules', 'test-dep-b', 'node_modules', 'test-sub')), + 'test-dep-b/test-sub was not removed') }) diff --git a/test/lib/commands/whoami.js b/test/lib/commands/whoami.js index 66c3f0c6b..f483bd46d 100644 --- a/test/lib/commands/whoami.js +++ b/test/lib/commands/whoami.js @@ -1,24 +1,35 @@ const t = require('tap') -const { load: _loadMockNpm } = require('../../fixtures/mock-npm') +const { load: loadMockNpm } = require('../../fixtures/mock-npm') +const MockRegistry = require('../../fixtures/mock-registry.js') const username = 'foo' -const loadMockNpm = (t, options) => _loadMockNpm(t, { - mocks: { - '../../lib/utils/get-identity.js': () => Promise.resolve(username), - }, - ...options, -}) +const auth = { '//registry.npmjs.org/:_authToken': 'test-auth-token' } t.test('npm whoami', async (t) => { - const { npm, joinedOutput } = await loadMockNpm(t) + const { npm, joinedOutput } = await loadMockNpm(t, { config: auth }) + const registry = new MockRegistry({ + tap: t, + registry: npm.config.get('registry'), + authorization: 'test-auth-token', + }) + registry.whoami({ username }) await npm.exec('whoami', []) t.equal(joinedOutput(), username, 'should print username') }) t.test('npm whoami --json', async (t) => { const { npm, joinedOutput } = await loadMockNpm(t, { - config: { json: true }, + config: { + json: true, + ...auth, + }, + }) + const registry = new MockRegistry({ + tap: t, + registry: npm.config.get('registry'), + authorization: 'test-auth-token', }) + registry.whoami({ username }) await npm.exec('whoami', []) t.equal(JSON.parse(joinedOutput()), username, 'should print username') }) |