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: npm hook ls duplicates hook name prefixes #5295

Merged
merged 3 commits into from
Oct 19, 2022
Merged
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
3 changes: 0 additions & 3 deletions lib/commands/hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
56 changes: 49 additions & 7 deletions test/lib/commands/hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -50,15 +50,15 @@ const libnpmhook = {
const pkg = Object.keys(pkgTypes)[0]
return {
id: 1,
name: pkg.replace(/^@/, ''),
name: pkg,
type: pkgTypes[pkg],
endpoint: 'https://google.com',
}
},
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 }
},
}

Expand Down Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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',
},
Expand Down Expand Up @@ -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'
)
})
Expand Down Expand Up @@ -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'
Expand Down