Skip to content

Commit

Permalink
feat: dedupe - display difference when --dry-run is enabled (#7133)
Browse files Browse the repository at this point in the history
* Added pretty print for dedupe command with --dry-run flag

* pretty print test coverage
  • Loading branch information
Blaumaus committed Jan 17, 2024
1 parent a50b03b commit 6d5f9ac
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
33 changes: 33 additions & 0 deletions lib/utils/reify-output.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ const reifyOutput = (npm, arb) => {
}

if (diff) {
if (npm.config.get('dry-run')) {
printDiff(npm, diff)
}

depth({
tree: diff,
visit: d => {
Expand Down Expand Up @@ -98,6 +102,35 @@ const printAuditReport = (npm, report) => {
npm.output(`\n${res.report}`)
}

// print the diff tree of actions that would be taken
const printDiff = (npm, diff) => {
const msg = []

for (let i = 0; i < diff.children.length; ++i) {
const child = diff.children[i]
msg.push('\n', child.action.toLowerCase(), '\t')

switch (child.action) {
case 'ADD':
msg.push([child.ideal.name, child.ideal.package.version].join('\t'))
break
case 'REMOVE':
msg.push([child.actual.name, child.actual.package.version].join('\t'))
break
case 'CHANGE':
msg.push(
[
child.actual.name,
child.actual.package.version + ' -> ' + child.ideal.package.version,
].join('\t')
)
break
}
}

npm.output(msg.join(''))
}

const getAuditReport = (npm, report) => {
if (!report) {
return
Expand Down
44 changes: 44 additions & 0 deletions test/lib/utils/reify-output.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,3 +380,47 @@ t.test('added packages should be looked up within returned tree', async t => {
t.matchSnapshot(out)
})
})

t.test('prints dedupe difference', 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, {
'dry-run': true,
})

t.match(
out,
'add\tfoo\t1.0.0',
'should print added package'
)

t.match(
out,
'remove\tbar\t1.0.0',
'should print removed package'
)

t.match(
out,
'change\tbar\t1.0.0 -> 2.1.0',
'should print changed package'
)
})

0 comments on commit 6d5f9ac

Please sign in to comment.