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: Add check to pkg command to deal with empty values #6902

Merged
merged 14 commits into from
Oct 16, 2023
6 changes: 1 addition & 5 deletions lib/utils/queryable.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,9 @@ const getter = ({ data, key }) => {
}, {})
return _data
} else {
// if can't find any more values, it means it's just over
// and there's nothing to return
if (!_data[k]) {
if (!Object.hasOwn(_data, k)) {
return undefined
}

// otherwise sets the next value
_data = _data[k]
}

Expand Down
4 changes: 4 additions & 0 deletions tap-snapshots/test/lib/commands/view.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,10 @@ maintainers[0].name = 'claudia'
maintainers[1].name = 'isaacs'
`

exports[`test/lib/commands/view.js TAP specific field names fields with empty values > must match snapshot 1`] = `

`

exports[`test/lib/commands/view.js TAP specific field names maintainers with email > must match snapshot 1`] = `
maintainers = [
{ name: 'claudia', email: 'c@yellow.com', twitter: 'cyellow' },
Expand Down
44 changes: 44 additions & 0 deletions test/lib/commands/pkg.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,50 @@ t.test('get single arg', async t => {
)
})

t.test('get multiple arg', async t => {
const { pkg, OUTPUT } = await mockNpm(t, {
prefixDir: {
'package.json': JSON.stringify({
name: 'foo',
version: '1.1.1',
}),
},
})

await pkg('get', 'name', 'version')

t.strictSame(
JSON.parse(OUTPUT()),
{
name: 'foo',
version: '1.1.1',
},
'should print retrieved package.json field'
)
})

t.test('get multiple arg with empty value', async t => {
const { pkg, OUTPUT } = await mockNpm(t, {
prefixDir: {
'package.json': JSON.stringify({
name: 'foo',
author: '',
}),
},
})

await pkg('get', 'name', 'author')

t.strictSame(
JSON.parse(OUTPUT()),
{
name: 'foo',
author: '',
},
'should print retrieved package.json field regardless of empty value'
)
})

t.test('get nested arg', async t => {
const { pkg, OUTPUT } = await mockNpm(t, {
prefixDir: {
Expand Down
6 changes: 6 additions & 0 deletions test/lib/commands/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ const packument = (nv, opts) => {
email: 'foo@yellow.com',
twitter: 'foo',
},
empty: '',
readme: 'a very useful readme',
versions: {
'1.0.0': {
Expand Down Expand Up @@ -425,6 +426,11 @@ t.test('specific field names', async t => {
await view.exec(['yellow@1.x.x', 'maintainers.name'])
t.matchSnapshot(outputs.join('\n'))
})

t.test('fields with empty values', async t => {
await view.exec(['yellow', 'empty'])
t.matchSnapshot(outputs.join('\n'))
})
})

t.test('throw error if global mode', async t => {
Expand Down
Loading