Skip to content

Commit

Permalink
Fix npm list collaborators diff in npm v8 vs v9 in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Feb 28, 2023
1 parent 1e6dca8 commit 1935621
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
26 changes: 18 additions & 8 deletions test/npm.js
Expand Up @@ -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');
Expand All @@ -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();
});
Expand Down Expand Up @@ -168,7 +170,9 @@ test('should not throw if npm returns 404 for unsupported ping', async t => {
const pingError = 'npm ERR! <title>404 - No content for path /-/ping</title>';
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();
Expand Down Expand Up @@ -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();
});
Expand All @@ -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'
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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'] }));

Expand Down Expand Up @@ -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');
Expand Down
9 changes: 6 additions & 3 deletions 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';
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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'
]);
Expand Down Expand Up @@ -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'
]);
Expand Down Expand Up @@ -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'
]);
Expand Down

0 comments on commit 1935621

Please sign in to comment.