Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/commands/install.js
Original file line number Diff line number Diff line change
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) {
Copy link
Member

@wraithgar wraithgar Apr 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where is already coupled to isGlobalInstall:

    const isGlobalInstall = this.npm.global
    const where = isGlobalInstall ? globalTop : this.npm.prefix

I suspect we don't need both tested here.

args = ['.']
}

Expand Down
5 changes: 3 additions & 2 deletions test/fixtures/mock-npm.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean "CWD" instead of "PWD" here?

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: {
Expand Down