Skip to content

Commit

Permalink
fix(install): do not install invalid package name
Browse files Browse the repository at this point in the history
Throws an usage error if finding an invalid argument in global install.

Fixes: #3029
  • Loading branch information
ruyadorno authored and lukekarrys committed Apr 19, 2022
1 parent f3d7fff commit b06e89f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/commands/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ class Install extends ArboristWorkspaceCmd {
args = ['.']
}

// throw usage error if trying to install empty package
// name to global space, e.g: `npm i -g ""`
if (where === globalTop && !args.every(Boolean)) {
throw this.usageError()
}

const opts = {
...this.npm.flatOptions,
auditLevel: null,
Expand Down
17 changes: 17 additions & 0 deletions test/lib/commands/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,23 @@ t.test('should install globally using Arborist', async t => {
t.strictSame(SCRIPTS, [], 'no scripts when installing globally')
})

t.test('should not install invalid global package name', async t => {
const { npm } = await loadMockNpm(t, {
'@npmcli/run-script': () => {},
'../../lib/utils/reify-finish.js': async () => {},
'@npmcli/arborist': function (args) {
throw new Error('should not reify')
},
})
npm.config.set('global', true)
npm.globalPrefix = path.resolve(t.testdir({}))
await t.rejects(
npm.exec('install', ['']),
/Usage:/,
'should not install invalid package name'
)
})

t.test('npm i -g npm engines check success', async t => {
const { npm } = await loadMockNpm(t, {
'../../lib/utils/reify-finish.js': async () => {},
Expand Down

0 comments on commit b06e89f

Please sign in to comment.