From 35c92fec3d053d303cc8057faa0ff4fe6e7cdc8a Mon Sep 17 00:00:00 2001 From: Aaron <2738518+NeonArray@users.noreply.github.com> Date: Mon, 16 Oct 2023 14:10:13 -0400 Subject: [PATCH] fix: Add check to pkg command to deal with empty values (#6902) --- lib/utils/queryable.js | 6 +-- .../test/lib/commands/view.js.test.cjs | 4 ++ test/lib/commands/pkg.js | 44 +++++++++++++++++++ test/lib/commands/view.js | 6 +++ 4 files changed, 55 insertions(+), 5 deletions(-) diff --git a/lib/utils/queryable.js b/lib/utils/queryable.js index 5ddcf636e8383..69621d928e8dd 100644 --- a/lib/utils/queryable.js +++ b/lib/utils/queryable.js @@ -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] } diff --git a/tap-snapshots/test/lib/commands/view.js.test.cjs b/tap-snapshots/test/lib/commands/view.js.test.cjs index d5b7a3b4a7906..5248d439afad9 100644 --- a/tap-snapshots/test/lib/commands/view.js.test.cjs +++ b/tap-snapshots/test/lib/commands/view.js.test.cjs @@ -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' }, diff --git a/test/lib/commands/pkg.js b/test/lib/commands/pkg.js index e915ef942410f..f3401bde5226a 100644 --- a/test/lib/commands/pkg.js +++ b/test/lib/commands/pkg.js @@ -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: { diff --git a/test/lib/commands/view.js b/test/lib/commands/view.js index ca07ef9eec2ff..a99c8d6242212 100644 --- a/test/lib/commands/view.js +++ b/test/lib/commands/view.js @@ -101,6 +101,7 @@ const packument = (nv, opts) => { email: 'foo@yellow.com', twitter: 'foo', }, + empty: '', readme: 'a very useful readme', versions: { '1.0.0': { @@ -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 => {