Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: issue-6960 Correctly handle scenario where prefix is the current working directory #7208

Open
wants to merge 2 commits into
base: latest
Choose a base branch
from
Open
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) {
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 => {
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