Skip to content

Commit 6e03e55

Browse files
claudiahdzisaacs
authored andcommitted
tests: add npm-audit
PR-URL: #1672 Credit: @claudiahdz Close: #1672 Reviewed-by: @isaacs
1 parent 85027f4 commit 6e03e55

2 files changed

Lines changed: 125 additions & 1 deletion

File tree

lib/audit.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,12 @@ const usage = usageUtil(
3838
const completion = (opts, cb) => {
3939
const argv = opts.conf.argv.remain
4040

41+
if (argv.length === 2) {
42+
return cb(null, ['fix'])
43+
}
44+
4145
switch (argv[2]) {
42-
case 'audit':
46+
case 'fix':
4347
return cb(null, [])
4448
default:
4549
return cb(new Error(argv[2] + ' not recognized'))

test/lib/audit.js

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
const { test } = require('tap')
2+
const requireInject = require('require-inject')
3+
const audit = require('../../lib/audit.js')
4+
5+
test('should audit using Arborist', t => {
6+
let ARB_ARGS = null
7+
let AUDIT_CALLED = false
8+
let REIFY_OUTPUT_CALLED = false
9+
let AUDIT_REPORT_CALLED = false
10+
let OUTPUT_CALLED = false
11+
let ARB_OBJ = null
12+
13+
const audit = requireInject('../../lib/audit.js', {
14+
'../../lib/npm.js': {
15+
prefix: 'foo',
16+
flatOptions: {
17+
json: false
18+
},
19+
},
20+
'npm-audit-report': () => {
21+
AUDIT_REPORT_CALLED = true
22+
return {
23+
report: 'there are vulnerabilities',
24+
exitCode: 0
25+
}
26+
},
27+
'@npmcli/arborist': function (args) {
28+
ARB_ARGS = args
29+
ARB_OBJ = this
30+
this.audit = () => {
31+
AUDIT_CALLED = true
32+
}
33+
},
34+
'../../lib/utils/reify-output.js': arb => {
35+
if (arb !== ARB_OBJ) {
36+
throw new Error('got wrong object passed to reify-output')
37+
}
38+
REIFY_OUTPUT_CALLED = true
39+
},
40+
'../../lib/utils/output.js': () => {
41+
OUTPUT_CALLED = true
42+
}
43+
})
44+
45+
t.test('audit', t => {
46+
audit([], () => {
47+
t.match(ARB_ARGS, { audit: true, path: 'foo' })
48+
t.equal(AUDIT_CALLED, true, 'called audit')
49+
t.equal(AUDIT_REPORT_CALLED, true, 'called audit report')
50+
t.equal(OUTPUT_CALLED, true, 'called audit report')
51+
t.end()
52+
})
53+
})
54+
55+
t.test('audit fix', t => {
56+
audit(['fix'], () => {
57+
t.equal(REIFY_OUTPUT_CALLED, true, 'called reify output')
58+
t.end()
59+
})
60+
})
61+
62+
t.end()
63+
})
64+
65+
test('should audit - json', t => {
66+
const audit = requireInject('../../lib/audit.js', {
67+
'../../lib/npm.js': {
68+
prefix: 'foo',
69+
flatOptions: {
70+
json: true
71+
},
72+
},
73+
'npm-audit-report': () => ({
74+
report: 'there are vulnerabilities',
75+
exitCode: 0
76+
}),
77+
'@npmcli/arborist': function () {
78+
this.audit = () => {}
79+
},
80+
'../../lib/utils/reify-output.js': () => {},
81+
'../../lib/utils/output.js': () => {}
82+
})
83+
84+
audit([], (err) => {
85+
t.notOk(err, 'no errors')
86+
t.end()
87+
})
88+
})
89+
90+
test('completion', t => {
91+
t.test('fix', t => {
92+
audit.completion({
93+
conf: { argv: { remain: ['npm', 'audit'] } }
94+
}, (err, res) => {
95+
const subcmd = res.pop()
96+
t.equals('fix', subcmd, 'completes to fix')
97+
t.end()
98+
})
99+
})
100+
101+
t.test('subcommand fix', t => {
102+
audit.completion({
103+
conf: { argv: { remain: ['npm', 'audit', 'fix'] } }
104+
}, (err) => {
105+
t.notOk(err, 'no errors')
106+
t.end()
107+
})
108+
})
109+
110+
t.test('subcommand not recognized', t => {
111+
audit.completion({
112+
conf: { argv: { remain: ['npm', 'audit', 'repare'] } }
113+
}, (err) => {
114+
t.ok(err, 'not recognized')
115+
t.end()
116+
})
117+
})
118+
119+
t.end()
120+
})

0 commit comments

Comments
 (0)