Skip to content

Commit 571c324

Browse files
committed
fix(install): do not install invalid package name
Throws an usage error if finding an invalid argument in global install. Fixes: #3029
1 parent f37f7d2 commit 571c324

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

lib/commands/install.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ class Install extends ArboristWorkspaceCmd {
140140
args = ['.']
141141
}
142142

143+
// throw usage error if trying to install empty package
144+
// name to global space, e.g: `npm i -g ""`
145+
if (where === globalTop && !args.every(Boolean)) {
146+
throw this.usageError()
147+
}
148+
143149
// TODO: Add warnings for other deprecated flags? or remove this one?
144150
if (isDev) {
145151
log.warn(

test/lib/commands/install.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,26 @@ t.test('should install globally using Arborist', async t => {
142142
t.strictSame(SCRIPTS, [], 'no scripts when installing globally')
143143
})
144144

145+
t.test('should not install invalid global package name', async t => {
146+
const SCRIPTS = []
147+
let ARB_ARGS = null
148+
let REIFY_CALLED
149+
const { npm } = await loadMockNpm(t, {
150+
'@npmcli/run-script': () => {},
151+
'../../lib/utils/reify-finish.js': async () => {},
152+
'@npmcli/arborist': function (args) {
153+
throw new Error('should not reify')
154+
},
155+
})
156+
npm.config.set('global', true)
157+
npm.globalPrefix = path.resolve(t.testdir({}))
158+
await t.rejects(
159+
npm.exec('install', ['']),
160+
/Usage:/,
161+
'should not install invalid package name'
162+
)
163+
})
164+
145165
t.test('npm i -g npm engines check success', async t => {
146166
const { npm } = await loadMockNpm(t, {
147167
'../../lib/utils/reify-finish.js': async () => {},

0 commit comments

Comments
 (0)