From fc8229824fedcd6ba79608b854e97e30d54958f4 Mon Sep 17 00:00:00 2001 From: Gennadiy Gashev <63790536+gennadiygashev@users.noreply.github.com> Date: Wed, 19 Oct 2022 21:52:07 +0500 Subject: [PATCH] fix: `npm hook ls` duplicates hook name prefixes (#5295) * fix: duplicate hook names * fix: incorrect names in mocks --- lib/commands/hook.js | 3 --- test/lib/commands/hook.js | 56 ++++++++++++++++++++++++++++++++++----- 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/lib/commands/hook.js b/lib/commands/hook.js index bb3a34b8d2d1b..084741c0c5eea 100644 --- a/lib/commands/hook.js +++ b/lib/commands/hook.js @@ -126,9 +126,6 @@ class Hook extends BaseCommand { hookName (hook) { let target = hook.name - if (hook.type === 'scope') { - target = '@' + target - } if (hook.type === 'owner') { target = '~' + target } diff --git a/test/lib/commands/hook.js b/test/lib/commands/hook.js index 95bac83d7b6eb..0cd6a7490dda2 100644 --- a/test/lib/commands/hook.js +++ b/test/lib/commands/hook.js @@ -28,7 +28,7 @@ let hookArgs = null const libnpmhook = { add: async (pkg, uri, secret, opts) => { hookArgs = { pkg, uri, secret, opts } - return { id: 1, name: pkg.replace(/^@/, ''), type: pkgTypes[pkg], endpoint: uri } + return { id: 1, name: pkg, type: pkgTypes[pkg], endpoint: uri } }, ls: async opts => { hookArgs = opts @@ -39,7 +39,7 @@ const libnpmhook = { return Object.keys(pkgTypes).map(name => ({ id: ++id, - name: name.replace(/^@/, ''), + name, type: pkgTypes[name], endpoint: 'https://google.com', last_delivery: id % 2 === 0 ? now : undefined, @@ -50,7 +50,7 @@ const libnpmhook = { const pkg = Object.keys(pkgTypes)[0] return { id: 1, - name: pkg.replace(/^@/, ''), + name: pkg, type: pkgTypes[pkg], endpoint: 'https://google.com', } @@ -58,7 +58,7 @@ const libnpmhook = { update: async (id, uri, secret, opts) => { hookArgs = { id, uri, secret, opts } const pkg = Object.keys(pkgTypes)[0] - return { id, name: pkg.replace(/^@/, ''), type: pkgTypes[pkg], endpoint: uri } + return { id, name: pkg, type: pkgTypes[pkg], endpoint: uri } }, } @@ -92,6 +92,48 @@ t.test('npm hook add', async t => { t.strictSame(output, ['+ semver -> https://google.com'], 'prints the correct output') }) +t.test('npm hook add - correct owner hook output', async t => { + t.teardown(() => { + hookArgs = null + output.length = 0 + }) + + await hook.exec(['add', '~npm', 'https://google.com', 'some-secret']) + + t.match( + hookArgs, + { + pkg: '~npm', + uri: 'https://google.com', + secret: 'some-secret', + opts: npm.flatOptions, + }, + 'provided the correct arguments to libnpmhook' + ) + t.strictSame(output, ['+ ~npm -> https://google.com'], 'prints the correct output') +}) + +t.test('npm hook add - correct scope hook output', async t => { + t.teardown(() => { + hookArgs = null + output.length = 0 + }) + + await hook.exec(['add', '@npmcli', 'https://google.com', 'some-secret']) + + t.match( + hookArgs, + { + pkg: '@npmcli', + uri: 'https://google.com', + secret: 'some-secret', + opts: npm.flatOptions, + }, + 'provided the correct arguments to libnpmhook' + ) + t.strictSame(output, ['+ @npmcli -> https://google.com'], 'prints the correct output') +}) + t.test('npm hook add - unicode output', async t => { npm.flatOptions.unicode = true t.teardown(() => { @@ -139,7 +181,7 @@ t.test('npm hook add - json output', async t => { JSON.parse(output[0]), { id: 1, - name: 'npmcli', + name: '@npmcli', endpoint: 'https://google.com', type: 'scope', }, @@ -174,7 +216,7 @@ t.test('npm hook add - parseable output', async t => { ) t.strictSame( output[1].split(/\t/), - ['1', 'npmcli', 'scope', 'https://google.com'], + ['1', '@npmcli', 'scope', 'https://google.com'], 'prints the correct parseable values' ) }) @@ -345,7 +387,7 @@ t.test('npm hook ls - parseable output', async t => { [ ['id', 'name', 'type', 'endpoint', 'last_delivery'], ['1', 'semver', 'package', 'https://google.com', ''], - ['2', 'npmcli', 'scope', 'https://google.com', `${now}`], + ['2', '@npmcli', 'scope', 'https://google.com', `${now}`], ['3', 'npm', 'owner', 'https://google.com', ''], ], 'prints the correct result'