Skip to content

Commit 35c92fe

Browse files
authored
fix: Add check to pkg command to deal with empty values (#6902)
1 parent b405da1 commit 35c92fe

File tree

4 files changed

+55
-5
lines changed

4 files changed

+55
-5
lines changed

lib/utils/queryable.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,9 @@ const getter = ({ data, key }) => {
111111
}, {})
112112
return _data
113113
} else {
114-
// if can't find any more values, it means it's just over
115-
// and there's nothing to return
116-
if (!_data[k]) {
114+
if (!Object.hasOwn(_data, k)) {
117115
return undefined
118116
}
119-
120-
// otherwise sets the next value
121117
_data = _data[k]
122118
}
123119

tap-snapshots/test/lib/commands/view.js.test.cjs

+4
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,10 @@ maintainers[0].name = 'claudia'
315315
maintainers[1].name = 'isaacs'
316316
`
317317

318+
exports[`test/lib/commands/view.js TAP specific field names fields with empty values > must match snapshot 1`] = `
319+
320+
`
321+
318322
exports[`test/lib/commands/view.js TAP specific field names maintainers with email > must match snapshot 1`] = `
319323
maintainers = [
320324
{ name: 'claudia', email: '[email protected]', twitter: 'cyellow' },

test/lib/commands/pkg.js

+44
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,50 @@ t.test('get single arg', async t => {
8383
)
8484
})
8585

86+
t.test('get multiple arg', async t => {
87+
const { pkg, OUTPUT } = await mockNpm(t, {
88+
prefixDir: {
89+
'package.json': JSON.stringify({
90+
name: 'foo',
91+
version: '1.1.1',
92+
}),
93+
},
94+
})
95+
96+
await pkg('get', 'name', 'version')
97+
98+
t.strictSame(
99+
JSON.parse(OUTPUT()),
100+
{
101+
name: 'foo',
102+
version: '1.1.1',
103+
},
104+
'should print retrieved package.json field'
105+
)
106+
})
107+
108+
t.test('get multiple arg with empty value', async t => {
109+
const { pkg, OUTPUT } = await mockNpm(t, {
110+
prefixDir: {
111+
'package.json': JSON.stringify({
112+
name: 'foo',
113+
author: '',
114+
}),
115+
},
116+
})
117+
118+
await pkg('get', 'name', 'author')
119+
120+
t.strictSame(
121+
JSON.parse(OUTPUT()),
122+
{
123+
name: 'foo',
124+
author: '',
125+
},
126+
'should print retrieved package.json field regardless of empty value'
127+
)
128+
})
129+
86130
t.test('get nested arg', async t => {
87131
const { pkg, OUTPUT } = await mockNpm(t, {
88132
prefixDir: {

test/lib/commands/view.js

+6
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ const packument = (nv, opts) => {
101101
102102
twitter: 'foo',
103103
},
104+
empty: '',
104105
readme: 'a very useful readme',
105106
versions: {
106107
'1.0.0': {
@@ -425,6 +426,11 @@ t.test('specific field names', async t => {
425426
await view.exec(['[email protected]', 'maintainers.name'])
426427
t.matchSnapshot(outputs.join('\n'))
427428
})
429+
430+
t.test('fields with empty values', async t => {
431+
await view.exec(['yellow', 'empty'])
432+
t.matchSnapshot(outputs.join('\n'))
433+
})
428434
})
429435

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

0 commit comments

Comments
 (0)