diff options
author | Nathan Fritz <fritzy@github.com> | 2022-03-02 18:40:52 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-02 18:40:52 +0300 |
commit | 45fc297f12e63c026715945a186ba0ec4efbdedb (patch) | |
tree | 0cabfb92770d73d1526716f0348095c1d3dc1de4 /test | |
parent | defe79ad6f2f4216bf5e0188256c77b49164eb94 (diff) |
fix: ignore implict workspace for some commands (#4479)
Closes #4404
Special thanks to @mshima for submitting a similar PR #4439
Diffstat (limited to 'test')
-rw-r--r-- | test/lib/load-all-commands.js | 1 | ||||
-rw-r--r-- | test/lib/npm.js | 104 |
2 files changed, 104 insertions, 1 deletions
diff --git a/test/lib/load-all-commands.js b/test/lib/load-all-commands.js index 248c81a30..ec1957529 100644 --- a/test/lib/load-all-commands.js +++ b/test/lib/load-all-commands.js @@ -28,6 +28,7 @@ t.test('load each command', async t => { ) t.ok(impl.description, 'implementation has a description') t.ok(impl.name, 'implementation has a name') + t.ok(impl.ignoreImplicitWorkspace !== undefined, 'implementation has ignoreImplictWorkspace') t.match(impl.usage, cmd, 'usage contains the command') await npm.exec(cmd, []) t.match(outputs[0][0], impl.usage, 'usage is what is output') diff --git a/test/lib/npm.js b/test/lib/npm.js index 2a0c5a89d..b2eedde72 100644 --- a/test/lib/npm.js +++ b/test/lib/npm.js @@ -1,5 +1,5 @@ const t = require('tap') -const { resolve, dirname } = require('path') +const { resolve, dirname, join } = require('path') const { load: loadMockNpm } = require('../fixtures/mock-npm.js') const mockGlobals = require('../fixtures/mock-globals') @@ -520,3 +520,105 @@ t.test('unknown command', async t => { { code: 'EUNKNOWNCOMMAND' } ) }) + +t.test('explicit workspace rejection', async t => { + mockGlobals(t, { + 'process.argv': [ + process.execPath, + process.argv[1], + '--color', 'false', + '--workspace', './packages/a', + ], + }) + const mock = await loadMockNpm(t, { + testdir: { + packages: { + a: { + 'package.json': JSON.stringify({ + name: 'a', + version: '1.0.0', + scripts: { test: 'echo test a' }, + }), + }, + }, + 'package.json': JSON.stringify({ + name: 'root', + version: '1.0.0', + workspaces: ['./packages/a'], + }), + }, + }) + await t.rejects( + mock.npm.exec('ping', []), + /This command does not support workspaces/ + ) +}) + +t.test('implicit workspace rejection', async t => { + const mock = await loadMockNpm(t, { + testdir: { + packages: { + a: { + 'package.json': JSON.stringify({ + name: 'a', + version: '1.0.0', + scripts: { test: 'echo test a' }, + }), + }, + }, + 'package.json': JSON.stringify({ + name: 'root', + version: '1.0.0', + workspaces: ['./packages/a'], + }), + }, + }) + const cwd = join(mock.npm.config.localPrefix, 'packages', 'a') + mock.npm.config.set('workspace', [cwd], 'default') + mockGlobals(t, { + 'process.argv': [ + process.execPath, + process.argv[1], + '--color', 'false', + ], + }) + await t.rejects( + mock.npm.exec('owner', []), + /This command does not support workspaces/ + ) +}) + +t.test('implicit workspace accept', async t => { + const mock = await loadMockNpm(t, { + testdir: { + packages: { + a: { + 'package.json': JSON.stringify({ + name: 'a', + version: '1.0.0', + scripts: { test: 'echo test a' }, + }), + }, + }, + 'package.json': JSON.stringify({ + name: 'root', + version: '1.0.0', + workspaces: ['./packages/a'], + }), + }, + }) + const cwd = join(mock.npm.config.localPrefix, 'packages', 'a') + mock.npm.config.set('workspace', [cwd], 'default') + mockGlobals(t, { + 'process.cwd': () => mock.npm.config.cwd, + 'process.argv': [ + process.execPath, + process.argv[1], + '--color', 'false', + ], + }) + await t.rejects( + mock.npm.exec('org', []), + /.*Usage/ + ) +}) |