Skip to content

Commit 3ef295f

Browse files
isaacsclaudiahdz
authored andcommittedNov 18, 2019
fix: print quick audit report for human output
This was broken when the support/funding functionality changed the return value to no longer track the promise for the quick audit printing. It was not caught by tests, because they were only running against the --json output, and not verifying the quick audit results in any way. Added a test to track the --json quick audit results (which were not broken, but someday could become so) and the human printed quick audit results (which were broken). Paired with @ruyadorno @mikemimik PR-URL: #486 Credit: @isaacs Close: #486 Reviewed-by: @claudiahdz
1 parent b150eae commit 3ef295f

File tree

5 files changed

+141
-23
lines changed

5 files changed

+141
-23
lines changed
 

‎lib/install.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -878,9 +878,6 @@ Installer.prototype.printInstalledForHuman = function (diffs, auditResult) {
878878
report += ' in ' + ((Date.now() - this.started) / 1000) + 's'
879879

880880
output(report)
881-
if (auditResult) {
882-
audit.printInstallReport(auditResult)
883-
}
884881

885882
function packages (num) {
886883
return num + ' package' + (num > 1 ? 's' : '')
@@ -911,6 +908,10 @@ Installer.prototype.printInstalledForHuman = function (diffs, auditResult) {
911908
if (printFundingReport.length) {
912909
output(printFundingReport)
913910
}
911+
912+
if (auditResult) {
913+
return audit.printInstallReport(auditResult)
914+
}
914915
}
915916

916917
Installer.prototype.printInstalledForJSON = function (diffs, auditResult) {

‎lib/install/fund.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ function getPrintFundingReport ({ fund, idealTree }, opts) {
3939

4040
return padding('') + length + ' ' +
4141
packageQuantity(length) +
42-
' looking for funding.' +
43-
padding('Run "npm fund" to find out more.')
42+
' looking for funding' +
43+
padding(' run `npm fund` for details\n')
4444
}
4545

4646
function getPrintFundingReportJSON ({ fund, idealTree }) {

‎test/tap/audit.js

+119-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,66 @@ function tmock (t) {
2727
})
2828
}
2929

30+
const quickAuditResult = {
31+
actions: [],
32+
advisories: {
33+
'1316': {
34+
findings: [
35+
{
36+
version: '1.0.0',
37+
paths: [
38+
'baddep'
39+
]
40+
}
41+
],
42+
'id': 1316,
43+
'created': '2019-11-14T15:29:41.991Z',
44+
'updated': '2019-11-14T19:35:30.677Z',
45+
'deleted': null,
46+
'title': 'Arbitrary Code Execution',
47+
'found_by': {
48+
'link': '',
49+
'name': 'François Lajeunesse-Robert',
50+
'email': ''
51+
},
52+
'reported_by': {
53+
'link': '',
54+
'name': 'François Lajeunesse-Robert',
55+
'email': ''
56+
},
57+
'module_name': 'baddep',
58+
'cves': [],
59+
'vulnerable_versions': '<4.5.2',
60+
'patched_versions': '>=4.5.2',
61+
'overview': 'a nice overview of the advisory',
62+
'recommendation': 'how you should fix it',
63+
'references': '',
64+
'access': 'public',
65+
'severity': 'high',
66+
'cwe': 'CWE-79',
67+
'metadata': {
68+
'module_type': '',
69+
'exploitability': 6,
70+
'affected_components': ''
71+
},
72+
'url': 'https://npmjs.com/advisories/1234542069'
73+
}
74+
},
75+
'muted': [],
76+
'metadata': {
77+
'vulnerabilities': {
78+
'info': 0,
79+
'low': 0,
80+
'moderate': 0,
81+
'high': 1,
82+
'critical': 0
83+
},
84+
'dependencies': 1,
85+
'devDependencies': 0,
86+
'totalDependencies': 1
87+
}
88+
}
89+
3090
test('exits with zero exit code for vulnerabilities below the `audit-level` flag', t => {
3191
const fixture = new Tacks(new Dir({
3292
'package.json': new File({
@@ -40,7 +100,7 @@ test('exits with zero exit code for vulnerabilities below the `audit-level` flag
40100
fixture.create(testDir)
41101
return tmock(t).then(srv => {
42102
srv.filteringRequestBody(req => 'ok')
43-
srv.post('/-/npm/v1/security/audits/quick', 'ok').reply(200, 'yeah')
103+
srv.post('/-/npm/v1/security/audits/quick', 'ok').reply(200, quickAuditResult)
44104
srv.get('/baddep').twice().reply(200, {
45105
name: 'baddep',
46106
'dist-tags': {
@@ -75,6 +135,8 @@ test('exits with zero exit code for vulnerabilities below the `audit-level` flag
75135
'--registry', common.registry,
76136
'--cache', path.join(testDir, 'npm-cache')
77137
], EXEC_OPTS).then(([code, stdout, stderr]) => {
138+
const result = JSON.parse(stdout)
139+
t.same(result.audit, quickAuditResult, 'printed quick audit result')
78140
srv.filteringRequestBody(req => 'ok')
79141
srv.post('/-/npm/v1/security/audits', 'ok').reply(200, {
80142
actions: [{
@@ -102,6 +164,62 @@ test('exits with zero exit code for vulnerabilities below the `audit-level` flag
102164
})
103165
})
104166

167+
test('shows quick audit results summary for human', t => {
168+
const fixture = new Tacks(new Dir({
169+
'package.json': new File({
170+
name: 'foo',
171+
version: '1.0.0',
172+
dependencies: {
173+
baddep: '1.0.0'
174+
}
175+
})
176+
}))
177+
fixture.create(testDir)
178+
return tmock(t).then(srv => {
179+
srv.filteringRequestBody(req => 'ok')
180+
srv.post('/-/npm/v1/security/audits/quick', 'ok').reply(200, quickAuditResult)
181+
srv.get('/baddep').twice().reply(200, {
182+
name: 'baddep',
183+
'dist-tags': {
184+
'latest': '1.2.3'
185+
},
186+
versions: {
187+
'1.0.0': {
188+
name: 'baddep',
189+
version: '1.0.0',
190+
_hasShrinkwrap: false,
191+
dist: {
192+
shasum: 'deadbeef',
193+
tarball: common.registry + '/idk/-/idk-1.0.0.tgz'
194+
}
195+
},
196+
'1.2.3': {
197+
name: 'baddep',
198+
version: '1.2.3',
199+
_hasShrinkwrap: false,
200+
dist: {
201+
shasum: 'deadbeef',
202+
tarball: common.registry + '/idk/-/idk-1.2.3.tgz'
203+
}
204+
}
205+
}
206+
})
207+
return common.npm([
208+
'install',
209+
'--audit',
210+
'--no-json',
211+
'--package-lock-only',
212+
'--registry', common.registry,
213+
'--cache', path.join(testDir, 'npm-cache')
214+
], EXEC_OPTS).then(([code, stdout, stderr]) => {
215+
t.match(stdout, new RegExp('added 1 package and audited 1 package in .*\\n' +
216+
'found 1 high severity vulnerability\\n' +
217+
' run `npm audit fix` to fix them, or `npm audit` for details\\n'),
218+
'shows quick audit result')
219+
})
220+
})
221+
})
222+
105223
test('exits with non-zero exit code for vulnerabilities at the `audit-level` flag', t => {
106224
const fixture = new Tacks(new Dir({
107225
'package.json': new File({

‎test/tap/install-mention-funding.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ test('mention npm fund upon installing single dependency', function (t) {
6868
if (err) throw err
6969
t.is(code, 0, 'installed successfully')
7070
t.is(stderr, '', 'no warnings')
71-
t.includes(stdout, '1 package is looking for funding.', 'should print amount of packages needing funding')
72-
t.includes(stdout, 'Run "npm fund" to find out more.', 'should print npm fund mention')
71+
t.includes(stdout, '1 package is looking for funding', 'should print amount of packages needing funding')
72+
t.includes(stdout, ' run `npm fund` for details', 'should print npm fund mention')
7373
t.end()
7474
})
7575
})
@@ -80,8 +80,8 @@ test('mention npm fund upon installing multiple dependencies', function (t) {
8080
if (err) throw err
8181
t.is(code, 0, 'installed successfully')
8282
t.is(stderr, '', 'no warnings')
83-
t.includes(stdout, '4 packages are looking for funding.', 'should print amount of packages needing funding')
84-
t.includes(stdout, 'Run "npm fund" to find out more.', 'should print npm fund mention')
83+
t.includes(stdout, '4 packages are looking for funding', 'should print amount of packages needing funding')
84+
t.includes(stdout, ' run `npm fund` for details', 'should print npm fund mention')
8585
t.end()
8686
})
8787
})
@@ -92,8 +92,8 @@ test('skips mention npm fund using --no-fund option', function (t) {
9292
if (err) throw err
9393
t.is(code, 0, 'installed successfully')
9494
t.is(stderr, '', 'no warnings')
95-
t.doesNotHave(stdout, '4 packages are looking for funding.', 'should print amount of packages needing funding')
96-
t.doesNotHave(stdout, 'Run "npm fund" to find out more.', 'should print npm fund mention')
95+
t.doesNotHave(stdout, '4 packages are looking for funding', 'should print amount of packages needing funding')
96+
t.doesNotHave(stdout, ' run `npm fund` for details', 'should print npm fund mention')
9797
t.end()
9898
})
9999
})
@@ -105,7 +105,7 @@ test('mention packages looking for funding using --json', function (t) {
105105
t.is(code, 0, 'installed successfully')
106106
t.is(stderr, '', 'no warnings')
107107
const res = JSON.parse(stdout)
108-
t.match(res.funding, '4 packages are looking for funding.', 'should print amount of packages needing funding')
108+
t.match(res.funding, '4 packages are looking for funding', 'should print amount of packages needing funding')
109109
t.end()
110110
})
111111
})

‎test/tap/install.fund.js

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
'use strict'
22

3-
const { EOL } = require('os')
43
const { test } = require('tap')
54
const { getPrintFundingReport } = require('../../lib/install/fund')
65

76
test('message when there are no funding found', (t) => {
8-
t.deepEqual(
7+
t.equal(
98
getPrintFundingReport({}),
109
'',
1110
'should not print any message if missing info'
1211
)
13-
t.deepEqual(
12+
t.equal(
1413
getPrintFundingReport({
1514
name: 'foo',
1615
version: '1.0.0',
@@ -19,7 +18,7 @@ test('message when there are no funding found', (t) => {
1918
'',
2019
'should not print any message if package has no dependencies'
2120
)
22-
t.deepEqual(
21+
t.equal(
2322
getPrintFundingReport({
2423
fund: true,
2524
idealTree: {
@@ -38,7 +37,7 @@ test('message when there are no funding found', (t) => {
3837
})
3938

4039
test('print appropriate message for a single package', (t) => {
41-
t.deepEqual(
40+
t.equal(
4241
getPrintFundingReport({
4342
fund: true,
4443
idealTree: {
@@ -54,15 +53,15 @@ test('print appropriate message for a single package', (t) => {
5453
}
5554
]
5655
}
57-
}),
58-
`${EOL}1 package is looking for funding.${EOL}Run "npm fund" to find out more.`,
56+
}).replace(/[\r\n]+/g, '\n'),
57+
`\n1 package is looking for funding\n run \`npm fund\` for details\n`,
5958
'should print single package message'
6059
)
6160
t.end()
6261
})
6362

6463
test('print appropriate message for many packages', (t) => {
65-
t.deepEqual(
64+
t.equal(
6665
getPrintFundingReport({
6766
fund: true,
6867
idealTree: {
@@ -92,8 +91,8 @@ test('print appropriate message for many packages', (t) => {
9291
}
9392
]
9493
}
95-
}),
96-
`${EOL}3 packages are looking for funding.${EOL}Run "npm fund" to find out more.`,
94+
}).replace(/[\r\n]+/g, '\n'),
95+
`\n3 packages are looking for funding\n run \`npm fund\` for details\n`,
9796
'should print many package message'
9897
)
9998
t.end()

0 commit comments

Comments
 (0)
Please sign in to comment.