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

show reify diff on --long #7175

Merged
merged 2 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
88 changes: 36 additions & 52 deletions lib/utils/reify-output.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,51 @@ const reifyOutput = (npm, arb) => {
}

if (diff) {
if (npm.config.get('dry-run')) {
printDiff(npm, diff)
let diffTable
if (npm.config.get('dry-run') || npm.config.get('long')) {
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({
tree: diff,
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:
Expand All @@ -67,6 +97,10 @@ const reifyOutput = (npm, arb) => {
},
getChildren: d => d.children,
})

if (diffTable) {
npm.output('\n' + diffTable.toString())
}
}

if (npm.flatOptions.fund) {
Expand Down Expand Up @@ -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
Expand Down
13 changes: 11 additions & 2 deletions tap-snapshots/test/lib/utils/reify-output.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -1633,11 +1633,20 @@ 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`] = `

add foo 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}
`

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
add foo 1.0.0

removed 1 package, and changed 1 package in {TIME}
`
30 changes: 29 additions & 1 deletion test/lib/utils/reify-output.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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')
})