From 19356213519776fde18773a2825572a97ddf320c Mon Sep 17 00:00:00 2001 From: Lars Kappert Date: Tue, 28 Feb 2023 12:46:51 +0100 Subject: [PATCH] Fix `npm list collaborators` diff in npm v8 vs v9 in tests --- test/npm.js | 26 ++++++++++++++++++-------- test/tasks.js | 9 ++++++--- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/test/npm.js b/test/npm.js index 8bd02384..e3f28b43 100644 --- a/test/npm.js +++ b/test/npm.js @@ -108,7 +108,7 @@ test('should add registry to commands when specified', async t => { const exec = sinon.stub(npmClient.shell, 'exec').resolves(); exec.withArgs('npm whoami --registry registry.example.org').resolves('john'); exec - .withArgs('npm access ls-collaborators release-it --registry registry.example.org') + .withArgs(/npm access (list collaborators --json|ls-collaborators) release-it --registry registry.example.org/) .resolves(JSON.stringify({ john: ['write'] })); await runTasks(npmClient); t.is(exec.args[0][0], 'npm ping --registry registry.example.org'); @@ -121,7 +121,9 @@ test('should not throw when executing tasks', async t => { const npmClient = factory(npm); const exec = sinon.stub(npmClient.shell, 'exec').resolves(); exec.withArgs('npm whoami').resolves('john'); - exec.withArgs('npm access ls-collaborators release-it').resolves(JSON.stringify({ john: ['write'] })); + exec + .withArgs(/npm access (list collaborators --json|ls-collaborators) release-it/) + .resolves(JSON.stringify({ john: ['write'] })); await t.notThrowsAsync(runTasks(npmClient)); exec.restore(); }); @@ -168,7 +170,9 @@ test('should not throw if npm returns 404 for unsupported ping', async t => { const pingError = 'npm ERR! 404 - No content for path /-/ping'; exec.withArgs('npm ping').rejects(new Error(pingError)); exec.withArgs('npm whoami').resolves('john'); - exec.withArgs('npm access ls-collaborators release-it').resolves(JSON.stringify({ john: ['write'] })); + exec + .withArgs(/npm access (list collaborators --json|ls-collaborators) release-it/) + .resolves(JSON.stringify({ john: ['write'] })); await runTasks(npmClient); t.is(exec.lastCall.args[0].trim(), 'npm publish . --tag latest'); exec.restore(); @@ -197,7 +201,9 @@ test('should throw if user is not a collaborator (v8)', async t => { const exec = sinon.stub(npmClient.shell, 'exec').resolves(); exec.withArgs('npm whoami').resolves('ada'); exec.withArgs('npm --version').resolves('8.2.0'); - exec.withArgs('npm access ls-collaborators release-it').resolves(JSON.stringify({ john: ['write'] })); + exec + .withArgs(/npm access (list collaborators --json|ls-collaborators) release-it/) + .resolves(JSON.stringify({ john: ['write'] })); await t.throwsAsync(runTasks(npmClient), { message: /^User ada is not a collaborator for release-it/ }); exec.restore(); }); @@ -207,7 +213,7 @@ test('should not throw if user is not a collaborator on a new package', async t const exec = sinon.stub(npmClient.shell, 'exec').resolves(); exec.withArgs('npm whoami').resolves('ada'); exec - .withArgs('npm access ls-collaborators release-it') + .withArgs(/npm access (list collaborators --json|ls-collaborators) release-it/) .rejects( new Error( 'npm ERR! code E404\nnpm ERR! 404 Not Found - GET https://registry.npmjs.org/-/package/release-it/collaborators?format=cli - File not found' @@ -268,7 +274,9 @@ test('should publish', async t => { const npmClient = factory(npm); const exec = sinon.stub(npmClient.shell, 'exec').resolves(); exec.withArgs('npm whoami').resolves('john'); - exec.withArgs('npm access ls-collaborators release-it').resolves(JSON.stringify({ john: ['write'] })); + exec + .withArgs(/npm access (list collaborators --json|ls-collaborators) release-it/) + .resolves(JSON.stringify({ john: ['write'] })); await runTasks(npmClient); t.is(exec.lastCall.args[0].trim(), 'npm publish . --tag latest'); exec.restore(); @@ -308,7 +316,7 @@ test('should publish to a different/scoped registry', async t => { .resolves('john'); exec .withArgs( - 'npm access ls-collaborators @my-scope/my-pkg --registry https://gitlab.com/api/v4/projects/my-scope%2Fmy-pkg/packages/npm/' + /npm access (list collaborators --json|ls-collaborators) @my-scope\/my-pkg --registry https:\/\/gitlab\.com\/api\/v4\/projects\/my-scope%2Fmy-pkg\/packages\/npm\// ) .resolves(JSON.stringify({ john: ['write'] })); @@ -337,7 +345,9 @@ test('should not publish when `npm version` fails', async t => { const npmClient = factory(npm, { options }); const exec = sinon.stub(npmClient.shell, 'exec').resolves(); exec.withArgs('npm whoami').resolves('john'); - exec.withArgs('npm access ls-collaborators @my-scope/my-pkg').resolves(JSON.stringify({ john: ['write'] })); + exec + .withArgs(/npm access (list collaborators --json|ls-collaborators) @my-scope\/my-pkg/) + .resolves(JSON.stringify({ john: ['write'] })); exec .withArgs('npm version 1.0.1 --no-git-tag-version') .rejects('npm ERR! Version not changed, might want --allow-same-version'); diff --git a/test/tasks.js b/test/tasks.js index 9d4328ff..44dacb8b 100644 --- a/test/tasks.js +++ b/test/tasks.js @@ -1,5 +1,6 @@ import path from 'node:path'; import test from 'ava'; +import semver from 'semver'; import sh from 'shelljs'; import _ from 'lodash'; import sinon from 'sinon'; @@ -28,6 +29,8 @@ const noop = Promise.resolve(); const sandbox = sinon.createSandbox(); +const npmMajorVersion = semver.major(process.env.npm_config_user_agent.match(/npm\/([^ ]+)/)[1]); + const testConfig = { ci: true, config: false @@ -175,7 +178,7 @@ test.serial('should release all the things (basic)', async t => { 'npm whoami', `npm show ${pkgName}@latest version`, 'npm --version', - `npm access ls-collaborators ${pkgName}`, + `npm access ${npmMajorVersion >= 9 ? 'list collaborators --json' : 'ls-collaborators'} ${pkgName}`, 'npm version 1.0.1 --no-git-tag-version', 'npm publish . --tag latest' ]); @@ -309,7 +312,7 @@ test.serial('should release all the things (pre-release, github, gitlab)', async 'npm whoami', `npm show ${pkgName}@latest version`, 'npm --version', - `npm access ls-collaborators ${pkgName}`, + `npm access ${npmMajorVersion >= 9 ? 'list collaborators --json' : 'ls-collaborators'} ${pkgName}`, 'npm version 1.1.0-alpha.0 --no-git-tag-version', 'npm publish . --tag alpha' ]); @@ -349,7 +352,7 @@ test.serial('should publish pre-release without pre-id with different npm.tag', 'npm whoami', `npm show ${pkgName}@latest version`, 'npm --version', - `npm access ls-collaborators ${pkgName}`, + `npm access ${npmMajorVersion >= 9 ? 'list collaborators --json' : 'ls-collaborators'} ${pkgName}`, 'npm version 2.0.0-0 --no-git-tag-version', 'npm publish . --tag next' ]);