Skip to content

Commit

Permalink
[npm#6960] Correctly handle scenario where prefix is the current work…
Browse files Browse the repository at this point in the history
…ing directory
  • Loading branch information
ficocelliguy committed Feb 7, 2024
1 parent d04111d commit ae7dae8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/commands/install.js
Expand Up @@ -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 = ['.']
}

Expand Down
5 changes: 3 additions & 2 deletions test/fixtures/mock-npm.js
Expand Up @@ -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: {} },
Expand Down Expand Up @@ -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'),
}
Expand Down
19 changes: 19 additions & 0 deletions test/lib/commands/install.js
Expand Up @@ -114,6 +114,25 @@ t.test('exec commands', async t => {
t.strictSame(SCRIPTS, [], 'no scripts when adding dep')
})

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 install globally using Arborist', async t => {
const SCRIPTS = []
let ARB_ARGS = null
Expand Down

0 comments on commit ae7dae8

Please sign in to comment.