diff --git a/lib/commands/install.js b/lib/commands/install.js index d04a35fbec2a7..c357bb1485578 100644 --- a/lib/commands/install.js +++ b/lib/commands/install.js @@ -131,7 +131,7 @@ class Install extends ArboristWorkspaceCmd { args = args.filter(a => resolve(a) !== this.npm.prefix) // `npm i -g` => "install this package globally" - if (where === globalTop && !args.length) { + if (isGlobalInstall && where === globalTop && !args.length) { args = ['.'] } diff --git a/test/fixtures/mock-npm.js b/test/fixtures/mock-npm.js index 4646e79146e86..f6b6dbbf29059 100644 --- a/test/fixtures/mock-npm.js +++ b/test/fixtures/mock-npm.js @@ -131,6 +131,7 @@ const setupMockNpm = async (t, { setCmd = false, // test dirs prefixDir = {}, + prefixOverride = null, // sets global and local prefix to this, the same as the `--prefix` flag homeDir = {}, cacheDir = {}, globalPrefixDir = { node_modules: {} }, @@ -195,9 +196,9 @@ const setupMockNpm = async (t, { const dirs = { testdir: dir, - prefix: path.join(dir, 'prefix'), + prefix: prefixOverride ?? path.join(dir, 'prefix'), cache: path.join(dir, 'cache'), - globalPrefix: path.join(dir, 'global'), + globalPrefix: prefixOverride ?? path.join(dir, 'global'), home: path.join(dir, 'home'), other: path.join(dir, 'other'), } diff --git a/test/lib/commands/install.js b/test/lib/commands/install.js index 8bb84bfff581f..5fc8c3b5def72 100644 --- a/test/lib/commands/install.js +++ b/test/lib/commands/install.js @@ -129,6 +129,25 @@ t.test('exec commands', async t => { await npm.exec('install') }) + await t.test('should not self-install package if prefix is the same as PWD', async t => { + let REIFY_CALLED_WITH = null + const { npm } = await loadMockNpm(t, { + mocks: { + '{LIB}/utils/reify-finish.js': async () => {}, + '@npmcli/run-script': () => {}, + '@npmcli/arborist': function () { + this.reify = (opts) => { + REIFY_CALLED_WITH = opts + } + }, + }, + prefixOverride: process.cwd(), + }) + + await npm.exec('install') + t.equal(REIFY_CALLED_WITH.add.length, 0, 'did not install current directory as a dependency') + }) + await t.test('should not install invalid global package name', async t => { const { npm } = await loadMockNpm(t, { config: {