From 9a1b9856764e3445b1b5f4c2445384d713b9f3fc Mon Sep 17 00:00:00 2001 From: Gar Date: Tue, 23 Jan 2024 13:09:58 -0800 Subject: [PATCH 1/2] fix: inline diff table code w/ summary code --- lib/utils/reify-output.js | 86 ++++++++----------- .../test/lib/utils/reify-output.js.test.cjs | 4 +- 2 files changed, 37 insertions(+), 53 deletions(-) diff --git a/lib/utils/reify-output.js b/lib/utils/reify-output.js index ae7f4396c2c45..ff63f845e458b 100644 --- a/lib/utils/reify-output.js +++ b/lib/utils/reify-output.js @@ -42,8 +42,32 @@ const reifyOutput = (npm, arb) => { } if (diff) { + let diffTable if (npm.config.get('dry-run')) { - printDiff(npm, diff) + diffTable = new Table({ + chars: { + top: '', + 'top-mid': '', + 'top-left': '', + 'top-right': '', + bottom: '', + 'bottom-mid': '', + 'bottom-left': '', + 'bottom-right': '', + left: '', + 'left-mid': '', + mid: '', + 'mid-mid': '', + right: '', + 'right-mid': '', + middle: ' ', + }, + style: { + 'padding-left': 0, + 'padding-right': 0, + border: 0, + }, + }) } depth({ @@ -51,12 +75,18 @@ const reifyOutput = (npm, arb) => { visit: d => { switch (d.action) { case 'REMOVE': + diffTable?.push(['remove', d.actual.name, d.actual.package.version]) summary.removed++ break case 'ADD': + diffTable?.push(['add', d.ideal.name, d.ideal.package.version]) actualTree.inventory.has(d.ideal) && summary.added++ break case 'CHANGE': + diffTable?.push(['change', + d.actual.name, + d.actual.package.version + ' -> ' + d.ideal.package.version, + ]) summary.changed++ break default: @@ -67,6 +97,10 @@ const reifyOutput = (npm, arb) => { }, getChildren: d => d.children, }) + + if (diffTable) { + npm.output('\n' + diffTable.toString()) + } } if (npm.flatOptions.fund) { @@ -103,56 +137,6 @@ const printAuditReport = (npm, report) => { npm.output(`\n${res.report}`) } -// print the diff tree of actions that would be taken -const printDiff = (npm, diff) => { - const table = new Table({ - chars: { - top: '', - 'top-mid': '', - 'top-left': '', - 'top-right': '', - bottom: '', - 'bottom-mid': '', - 'bottom-left': '', - 'bottom-right': '', - left: '', - 'left-mid': '', - mid: '', - 'mid-mid': '', - right: '', - 'right-mid': '', - middle: ' ', - }, - style: { - 'padding-left': 0, - 'padding-right': 0, - border: 0, - }, - }) - - for (let i = 0; i < diff.children.length; ++i) { - const child = diff.children[i] - table[i] = [child.action.toLowerCase()] - - switch (child.action) { - case 'ADD': - table[i].push(child.ideal.name, child.ideal.package.version) - break - case 'REMOVE': - table[i].push(child.actual.name, child.actual.package.version) - break - case 'CHANGE': - table[i].push( - child.actual.name, - child.actual.package.version + ' -> ' + child.ideal.package.version - ) - break - } - } - - npm.output('\n' + table.toString()) -} - const getAuditReport = (npm, report) => { if (!report) { return diff --git a/tap-snapshots/test/lib/utils/reify-output.js.test.cjs b/tap-snapshots/test/lib/utils/reify-output.js.test.cjs index c698255588d66..09307271d6054 100644 --- a/tap-snapshots/test/lib/utils/reify-output.js.test.cjs +++ b/tap-snapshots/test/lib/utils/reify-output.js.test.cjs @@ -1635,9 +1635,9 @@ exports[`test/lib/utils/reify-output.js TAP packages changed message > {"added" exports[`test/lib/utils/reify-output.js TAP prints dedupe difference > diff table 1`] = ` -add foo 1.0.0 -remove bar 1.0.0 change bar 1.0.0 -> 2.1.0 +remove bar 1.0.0 +add foo 1.0.0 removed 1 package, and changed 1 package in {TIME} ` From 52c90eeb6824525ad7ca696911d9d33b5d5aa9bc Mon Sep 17 00:00:00 2001 From: Gar Date: Tue, 23 Jan 2024 13:10:11 -0800 Subject: [PATCH 2/2] feat: display tree diff on `--long` --- lib/utils/reify-output.js | 2 +- .../test/lib/utils/reify-output.js.test.cjs | 11 ++++++- test/lib/utils/reify-output.js | 30 ++++++++++++++++++- 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/lib/utils/reify-output.js b/lib/utils/reify-output.js index ff63f845e458b..44c913812a8ef 100644 --- a/lib/utils/reify-output.js +++ b/lib/utils/reify-output.js @@ -43,7 +43,7 @@ const reifyOutput = (npm, arb) => { if (diff) { let diffTable - if (npm.config.get('dry-run')) { + if (npm.config.get('dry-run') || npm.config.get('long')) { diffTable = new Table({ chars: { top: '', diff --git a/tap-snapshots/test/lib/utils/reify-output.js.test.cjs b/tap-snapshots/test/lib/utils/reify-output.js.test.cjs index 09307271d6054..5983ac224e26e 100644 --- a/tap-snapshots/test/lib/utils/reify-output.js.test.cjs +++ b/tap-snapshots/test/lib/utils/reify-output.js.test.cjs @@ -1633,7 +1633,16 @@ exports[`test/lib/utils/reify-output.js TAP packages changed message > {"added" } ` -exports[`test/lib/utils/reify-output.js TAP prints dedupe difference > diff table 1`] = ` +exports[`test/lib/utils/reify-output.js TAP prints dedupe difference on dry-run > diff table 1`] = ` + +change bar 1.0.0 -> 2.1.0 +remove bar 1.0.0 +add foo 1.0.0 + +removed 1 package, and changed 1 package in {TIME} +` + +exports[`test/lib/utils/reify-output.js TAP prints dedupe difference on long > diff table 1`] = ` change bar 1.0.0 -> 2.1.0 remove bar 1.0.0 diff --git a/test/lib/utils/reify-output.js b/test/lib/utils/reify-output.js index 732034ed2fb28..fd15e25a74984 100644 --- a/test/lib/utils/reify-output.js +++ b/test/lib/utils/reify-output.js @@ -381,7 +381,7 @@ t.test('added packages should be looked up within returned tree', async t => { }) }) -t.test('prints dedupe difference', async t => { +t.test('prints dedupe difference on dry-run', async t => { const mock = { actualTree: { name: 'foo', @@ -408,3 +408,31 @@ t.test('prints dedupe difference', async t => { t.matchSnapshot(out, 'diff table') }) + +t.test('prints dedupe difference on long', async t => { + const mock = { + actualTree: { + name: 'foo', + inventory: { + has: () => false, + }, + }, + diff: { + children: [ + { action: 'ADD', ideal: { name: 'foo', package: { version: '1.0.0' } } }, + { action: 'REMOVE', actual: { name: 'bar', package: { version: '1.0.0' } } }, + { + action: 'CHANGE', + actual: { name: 'bar', package: { version: '1.0.0' } }, + ideal: { package: { version: '2.1.0' } }, + }, + ], + }, + } + + const out = await mockReify(t, mock, { + long: true, + }) + + t.matchSnapshot(out, 'diff table') +})