diff options
author | Gar <gar+gh@danger.computer> | 2021-02-25 02:54:50 +0300 |
---|---|---|
committer | Ruy Adorno <ruyadorno@hotmail.com> | 2021-03-05 00:05:08 +0300 |
commit | 4a5dd3a5a200b3f4f7b47168497d8e03dca3a2ca (patch) | |
tree | d34a1ea229b719c3cfbdce85899ceaf67b43e7ab /test/lib/access.js | |
parent | b33c760cea7fe2696d35b5530abc1b455980fef1 (diff) |
fix(npm) pass npm context everywhere
Instead of files randomly requiring the npm singleton,
we pass it where it needs to go so that tests don't need
to do so much require mocking everywhere
PR-URL: https://github.com/npm/cli/pull/2772
Credit: @wraithgar
Close: #2772
Reviewed-by: @ruyadorno
Diffstat (limited to 'test/lib/access.js')
-rw-r--r-- | test/lib/access.js | 157 |
1 files changed, 72 insertions, 85 deletions
diff --git a/test/lib/access.js b/test/lib/access.js index fb799f2df..3a732ad0a 100644 --- a/test/lib/access.js +++ b/test/lib/access.js @@ -1,17 +1,12 @@ const { test } = require('tap') const requireInject = require('require-inject') -const access = requireInject('../../lib/access.js', { - '../../lib/npm.js': { - flatOptions: {}, - }, -}) +const Access = require('../../lib/access.js') test('completion', t => { - const { completion } = access - + const access = new Access({ flatOptions: {} }) const testComp = (argv, expect) => { - const res = completion({conf: {argv: {remain: argv}}}) + const res = access.completion({conf: {argv: {remain: argv}}}) t.resolves(res, expect, argv.join(' ')) } @@ -32,7 +27,7 @@ test('completion', t => { testComp(['npm', 'access', 'revoke'], []) t.rejects( - completion({conf: {argv: {remain: ['npm', 'access', 'foobar']}}}), + access.completion({conf: {argv: {remain: ['npm', 'access', 'foobar']}}}), { message: 'foobar not recognized' } ) @@ -40,14 +35,16 @@ test('completion', t => { }) test('subcommand required', t => { - access([], (err) => { + const access = new Access({ flatOptions: {} }) + access.exec([], (err) => { t.equal(err, '\nUsage: Subcommand is required.\n\n' + access.usage) t.end() }) }) test('unrecognized subcommand', (t) => { - access(['blerg'], (err) => { + const access = new Access({ flatOptions: {} }) + access.exec(['blerg'], (err) => { t.match( err, /Usage: blerg is not a recognized subcommand/, @@ -58,7 +55,8 @@ test('unrecognized subcommand', (t) => { }) test('edit', (t) => { - access([ + const access = new Access({ flatOptions: {} }) + access.exec([ 'edit', '@scoped/another', ], (err) => { @@ -77,10 +75,8 @@ test('access public on unscoped package', (t) => { name: 'npm-access-public-pkg', }), }) - const access = requireInject('../../lib/access.js', { - '../../lib/npm.js': { prefix }, - }) - access([ + const access = new Access({ prefix }) + access.exec([ 'public', ], (err) => { t.match( @@ -98,7 +94,7 @@ test('access public on scoped package', (t) => { const prefix = t.testdir({ 'package.json': JSON.stringify({ name }), }) - const access = requireInject('../../lib/access.js', { + const Access = requireInject('../../lib/access.js', { libnpmaccess: { public: (pkg, { registry }) => { t.equal(pkg, name, 'should use pkg name ref') @@ -110,14 +106,12 @@ test('access public on scoped package', (t) => { return true }, }, - '../../lib/npm.js': { - flatOptions: { - registry: 'https://registry.npmjs.org', - }, - prefix, - }, }) - access([ + const access = new Access({ + flatOptions: { registry: 'https://registry.npmjs.org' }, + prefix, + }) + access.exec([ 'public', ], (err) => { t.ifError(err, 'npm access') @@ -129,10 +123,8 @@ test('access public on missing package.json', (t) => { const prefix = t.testdir({ node_modules: {}, }) - const access = requireInject('../../lib/access.js', { - '../../lib/npm.js': { prefix }, - }) - access([ + const access = new Access({ prefix }) + access.exec([ 'public', ], (err) => { t.match( @@ -149,10 +141,8 @@ test('access public on invalid package.json', (t) => { 'package.json': '{\n', node_modules: {}, }) - const access = requireInject('../../lib/access.js', { - '../../lib/npm.js': { prefix }, - }) - access([ + const access = new Access({ prefix }) + access.exec([ 'public', ], (err) => { t.match( @@ -170,10 +160,8 @@ test('access restricted on unscoped package', (t) => { name: 'npm-access-restricted-pkg', }), }) - const access = requireInject('../../lib/access.js', { - '../../lib/npm.js': { prefix }, - }) - access([ + const access = new Access({ prefix }) + access.exec([ 'restricted', ], (err) => { t.match( @@ -191,7 +179,7 @@ test('access restricted on scoped package', (t) => { const prefix = t.testdir({ 'package.json': JSON.stringify({ name }), }) - const access = requireInject('../../lib/access.js', { + const Access = requireInject('../../lib/access.js', { libnpmaccess: { restricted: (pkg, { registry }) => { t.equal(pkg, name, 'should use pkg name ref') @@ -203,14 +191,12 @@ test('access restricted on scoped package', (t) => { return true }, }, - '../../lib/npm.js': { - flatOptions: { - registry: 'https://registry.npmjs.org', - }, - prefix, - }, }) - access([ + const access = new Access({ + flatOptions: { registry: 'https://registry.npmjs.org' }, + prefix, + }) + access.exec([ 'restricted', ], (err) => { t.ifError(err, 'npm access') @@ -222,10 +208,8 @@ test('access restricted on missing package.json', (t) => { const prefix = t.testdir({ node_modules: {}, }) - const access = requireInject('../../lib/access.js', { - '../../lib/npm.js': { prefix }, - }) - access([ + const access = new Access({ prefix }) + access.exec([ 'restricted', ], (err) => { t.match( @@ -242,10 +226,8 @@ test('access restricted on invalid package.json', (t) => { 'package.json': '{\n', node_modules: {}, }) - const access = requireInject('../../lib/access.js', { - '../../lib/npm.js': { prefix }, - }) - access([ + const access = new Access({ prefix }) + access.exec([ 'restricted', ], (err) => { t.match( @@ -259,7 +241,7 @@ test('access restricted on invalid package.json', (t) => { test('access grant read-only', (t) => { t.plan(5) - const access = requireInject('../../lib/access.js', { + const Access = requireInject('../../lib/access.js', { libnpmaccess: { grant: (spec, team, permissions) => { t.equal(spec, '@scoped/another', 'should use expected spec') @@ -268,9 +250,9 @@ test('access grant read-only', (t) => { return true }, }, - '../../lib/npm.js': {}, }) - access([ + const access = new Access({}) + access.exec([ 'grant', 'read-only', 'myorg:myteam', @@ -283,7 +265,7 @@ test('access grant read-only', (t) => { test('access grant read-write', (t) => { t.plan(5) - const access = requireInject('../../lib/access.js', { + const Access = requireInject('../../lib/access.js', { libnpmaccess: { grant: (spec, team, permissions) => { t.equal(spec, '@scoped/another', 'should use expected spec') @@ -292,9 +274,9 @@ test('access grant read-write', (t) => { return true }, }, - '../../lib/npm.js': {}, }) - access([ + const access = new Access({}) + access.exec([ 'grant', 'read-write', 'myorg:myteam', @@ -312,7 +294,7 @@ test('access grant current cwd', (t) => { name: 'yargs', }), }) - const access = requireInject('../../lib/access.js', { + const Access = requireInject('../../lib/access.js', { libnpmaccess: { grant: (spec, team, permissions) => { t.equal(spec, 'yargs', 'should use expected spec') @@ -321,9 +303,9 @@ test('access grant current cwd', (t) => { return true }, }, - '../../lib/npm.js': { prefix }, }) - access([ + const access = new Access({ prefix }) + access.exec([ 'grant', 'read-write', 'myorg:myteam', @@ -334,7 +316,8 @@ test('access grant current cwd', (t) => { }) test('access grant others', (t) => { - access([ + const access = new Access({ flatOptions: {} }) + access.exec([ 'grant', 'rerere', 'myorg:myteam', @@ -350,7 +333,8 @@ test('access grant others', (t) => { }) test('access grant missing team args', (t) => { - access([ + const access = new Access({ flatOptions: {} }) + access.exec([ 'grant', 'read-only', undefined, @@ -366,7 +350,8 @@ test('access grant missing team args', (t) => { }) test('access grant malformed team arg', (t) => { - access([ + const access = new Access({ flatOptions: {} }) + access.exec([ 'grant', 'read-only', 'foo', @@ -383,7 +368,7 @@ test('access grant malformed team arg', (t) => { test('access 2fa-required/2fa-not-required', t => { t.plan(2) - const access = requireInject('../../lib/access.js', { + const Access = requireInject('../../lib/access.js', { libnpmaccess: { tfaRequired: (spec) => { t.equal(spec, '@scope/pkg', 'should use expected spec') @@ -394,15 +379,15 @@ test('access 2fa-required/2fa-not-required', t => { return true }, }, - '../../lib/npm.js': {}, }) + const access = new Access({}) - access(['2fa-required', '@scope/pkg'], er => { + access.exec(['2fa-required', '@scope/pkg'], er => { if (er) throw er }) - access(['2fa-not-required', 'unscoped-pkg'], er => { + access.exec(['2fa-not-required', 'unscoped-pkg'], er => { if (er) throw er }) @@ -410,7 +395,7 @@ test('access 2fa-required/2fa-not-required', t => { test('access revoke', (t) => { t.plan(4) - const access = requireInject('../../lib/access.js', { + const Access = requireInject('../../lib/access.js', { libnpmaccess: { revoke: (spec, team) => { t.equal(spec, '@scoped/another', 'should use expected spec') @@ -418,9 +403,9 @@ test('access revoke', (t) => { return true }, }, - '../../lib/npm.js': {}, }) - access([ + const access = new Access({}) + access.exec([ 'revoke', 'myorg:myteam', '@scoped/another', @@ -431,7 +416,8 @@ test('access revoke', (t) => { }) test('access revoke missing team args', (t) => { - access([ + const access = new Access({ flatOptions: {} }) + access.exec([ 'revoke', undefined, '@scoped/another', @@ -446,7 +432,8 @@ test('access revoke missing team args', (t) => { }) test('access revoke malformed team arg', (t) => { - access([ + const access = new Access({ flatOptions: {} }) + access.exec([ 'revoke', 'foo', '@scoped/another', @@ -462,7 +449,7 @@ test('access revoke malformed team arg', (t) => { test('npm access ls-packages with no team', (t) => { t.plan(3) - const access = requireInject('../../lib/access.js', { + const Access = requireInject('../../lib/access.js', { libnpmaccess: { lsPackages: (entity) => { t.equal(entity, 'foo', 'should use expected entity') @@ -471,9 +458,9 @@ test('npm access ls-packages with no team', (t) => { }, '../../lib/utils/get-identity.js': () => Promise.resolve('foo'), '../../lib/utils/output.js': () => null, - '../../lib/npm.js': {}, }) - access([ + const access = new Access({}) + access.exec([ 'ls-packages', ], (err) => { t.ifError(err, 'npm access') @@ -483,7 +470,7 @@ test('npm access ls-packages with no team', (t) => { test('access ls-packages on team', (t) => { t.plan(3) - const access = requireInject('../../lib/access.js', { + const Access = requireInject('../../lib/access.js', { libnpmaccess: { lsPackages: (entity) => { t.equal(entity, 'myorg:myteam', 'should use expected entity') @@ -491,9 +478,9 @@ test('access ls-packages on team', (t) => { }, }, '../../lib/utils/output.js': () => null, - '../../lib/npm.js': {}, }) - access([ + const access = new Access({}) + access.exec([ 'ls-packages', 'myorg:myteam', ], (err) => { @@ -509,7 +496,7 @@ test('access ls-collaborators on current', (t) => { name: 'yargs', }), }) - const access = requireInject('../../lib/access.js', { + const Access = requireInject('../../lib/access.js', { libnpmaccess: { lsCollaborators: (spec) => { t.equal(spec, 'yargs', 'should use expected spec') @@ -517,9 +504,9 @@ test('access ls-collaborators on current', (t) => { }, }, '../../lib/utils/output.js': () => null, - '../../lib/npm.js': { prefix }, }) - access([ + const access = new Access({ prefix }) + access.exec([ 'ls-collaborators', ], (err) => { t.ifError(err, 'npm access') @@ -529,7 +516,7 @@ test('access ls-collaborators on current', (t) => { test('access ls-collaborators on spec', (t) => { t.plan(3) - const access = requireInject('../../lib/access.js', { + const Access = requireInject('../../lib/access.js', { libnpmaccess: { lsCollaborators: (spec) => { t.equal(spec, 'yargs', 'should use expected spec') @@ -537,9 +524,9 @@ test('access ls-collaborators on spec', (t) => { }, }, '../../lib/utils/output.js': () => null, - '../../lib/npm.js': {}, }) - access([ + const access = new Access({}) + access.exec([ 'ls-collaborators', 'yargs', ], (err) => { |