From 1525a5e7fba4c996ac3bed5fdb75da275c537da8 Mon Sep 17 00:00:00 2001 From: Gar Date: Tue, 24 Jan 2023 10:14:29 -0800 Subject: [PATCH] fix: unpublish with scoped registry Unpublish now works if you have a scoped registry config --- lib/commands/unpublish.js | 5 ++++- test/fixtures/mock-npm.js | 2 +- test/lib/commands/unpublish.js | 40 +++++++++++++++++++++++++++++----- 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/lib/commands/unpublish.js b/lib/commands/unpublish.js index 9985e2e39f140..f1bcded192e5a 100644 --- a/lib/commands/unpublish.js +++ b/lib/commands/unpublish.js @@ -26,7 +26,10 @@ class Unpublish extends BaseCommand { async getKeysOfVersions (name, opts) { const pkgUri = npa(name).escapedName - const json = await npmFetch.json(`${pkgUri}?write=true`, opts) + const json = await npmFetch.json(`${pkgUri}?write=true`, { + ...opts, + spec: name, + }) return Object.keys(json.versions) } diff --git a/test/fixtures/mock-npm.js b/test/fixtures/mock-npm.js index 2cada1354878c..a2d35c2479d73 100644 --- a/test/fixtures/mock-npm.js +++ b/test/fixtures/mock-npm.js @@ -206,7 +206,7 @@ const setupMockNpm = async (t, { acc.env[`process.env."npm_config_${key}"`] = value } else { const values = [].concat(value) - acc.argv.push(...values.flatMap(v => [`--${key}`, v.toString()])) + acc.argv.push(...values.flatMap(v => `--${key}=${v.toString()}`)) } acc.config[key] = value return acc diff --git a/test/lib/commands/unpublish.js b/test/lib/commands/unpublish.js index cba7298475133..96c06bf3ffee6 100644 --- a/test/lib/commands/unpublish.js +++ b/test/lib/commands/unpublish.js @@ -27,7 +27,7 @@ t.test('no args --force success', async t => { }) const manifest = registry.manifest({ name: pkg }) await registry.package({ manifest, query: { write: true } }) - registry.nock.delete(`/${pkg}/-rev/${manifest._rev}`).reply(201) + registry.unpublish({ manifest }) await npm.exec('unpublish', []) t.equal(joinedOutput(), '- test-package@1.0.0') }) @@ -148,7 +148,7 @@ t.test('no version found in package.json', async t => { }) const manifest = registry.manifest({ name: pkg }) await registry.package({ manifest, query: { write: true } }) - registry.nock.delete(`/${pkg}/-rev/${manifest._rev}`).reply(201) + registry.unpublish({ manifest }) await npm.exec('unpublish', []) t.equal(joinedOutput(), '- test-package') @@ -168,7 +168,7 @@ t.test('unpublish --force no version set', async t => { }) const manifest = registry.manifest({ name: pkg }) await registry.package({ manifest, query: { write: true }, times: 2 }) - registry.nock.delete(`/${pkg}/-rev/${manifest._rev}`).reply(201) + registry.unpublish({ manifest }) await npm.exec('unpublish', ['test-package']) t.equal(joinedOutput(), '- test-package') @@ -361,7 +361,7 @@ t.test('publishConfig no spec', async t => { }) const manifest = registry.manifest({ name: pkg }) await registry.package({ manifest, query: { write: true } }) - registry.nock.delete(`/${pkg}/-rev/${manifest._rev}`).reply(201) + registry.unpublish({ manifest }) await npm.exec('unpublish', []) t.equal(joinedOutput(), '- test-package@1.0.0') }) @@ -391,11 +391,41 @@ t.test('publishConfig with spec', async t => { }) const manifest = registry.manifest({ name: pkg }) await registry.package({ manifest, query: { write: true }, times: 2 }) - registry.nock.delete(`/${pkg}/-rev/${manifest._rev}`).reply(201) + registry.unpublish({ manifest }) await npm.exec('unpublish', ['test-package']) t.equal(joinedOutput(), '- test-package') }) +t.test('scoped registry config', async t => { + const scopedPkg = `@npm/test-package` + const alternateRegistry = 'https://other.registry.npmjs.org' + const { npm } = await loadMockNpm(t, { + config: { + force: true, + '@npm:registry': alternateRegistry, + '//other.registry.npmjs.org/:_authToken': 'test-other-token', + }, + prefixDir: { + 'package.json': JSON.stringify({ + name: pkg, + version: '1.0.0', + publishConfig: { + registry: alternateRegistry, + }, + }, null, 2), + }, + }) + const registry = new MockRegistry({ + tap: t, + registry: alternateRegistry, + authorization: 'test-other-token', + }) + const manifest = registry.manifest({ name: scopedPkg }) + await registry.package({ manifest, query: { write: true } }) + registry.unpublish({ manifest }) + await npm.exec('unpublish', [scopedPkg]) +}) + t.test('completion', async t => { const { npm } = await loadMockNpm(t, { config: {