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
10 changes: 6 additions & 4 deletions lib/utils/queryable.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,14 @@ const getter = ({ data, key }) => {
} 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) && !_data[k]) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The falsey check here was a bad idea in the first place. What if the value of node-gyp is false? Shouldn't I get that back?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I initially didn't have that check there, but without it a bunch of other tests fail.

Screenshot 2023-10-16 at 10 46 20 AM

_data = ''
} else if (!_data[k]) {
return undefined
} else {
// otherwise sets the next value
_data = _data[k]
}

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

label += k
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: '[email protected]',
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(['[email protected]', 'maintainers.name'])
t.matchSnapshot(outputs.join('\n'))
})

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

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