Skip to content

Commit

Permalink
fix: Add check to pkg command to deal with empty values (#6902)
Browse files Browse the repository at this point in the history
  • Loading branch information
NeonArray authored Oct 16, 2023
1 parent b405da1 commit 35c92fe
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 5 deletions.
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

0 comments on commit 35c92fe

Please sign in to comment.