Skip to content

Commit 0dc9964

Browse files
committed
Update console output and add some tests
1 parent bd7f324 commit 0dc9964

File tree

3 files changed

+183
-44
lines changed

3 files changed

+183
-44
lines changed

lib/commands/audit.js

+35-25
Original file line numberDiff line numberDiff line change
@@ -79,45 +79,45 @@ class VerifySignatures {
7979
this.appendOutput(`${verifiedPrefix}${timing}\n`)
8080

8181
if (this.verified && !verified) {
82-
this.appendOutput(
83-
`${this.verified} packages have ${this.npm.color ? chalk.bold('verified') : 'verified'}` +
84-
` registry signatures\n`
85-
)
82+
const verifiedClr = this.npm.color ? chalk.bold('verified') : 'verified'
83+
const msg = this.verified === 1 ?
84+
`${this.verified} package has a ${verifiedClr} registry signature\n` :
85+
`${this.verified} packages have ${verifiedClr} registry signatures\n`
86+
this.appendOutput(msg)
8687
}
8788

8889
if (missing.length) {
90+
const logMissing = this.npm.config.get('log-missing-names')
91+
const missingClr = this.npm.color ? chalk.bold(chalk.magenta('missing')) : 'missing'
8992
const msg = missing.length === 1 ?
90-
`package has a ${this.npm.color ? chalk.bold(chalk.magenta('missing')) : 'missing'}` +
91-
` registry signature` :
92-
`packages have ${this.npm.color ? chalk.bold(chalk.magenta('missing')) : 'missing'}` +
93-
` registry signatures`
93+
`package has a ${missingClr} registry signature` :
94+
`packages have ${missingClr} registry signatures`
9495
this.appendOutput(
9596
`${missing.length} ${msg} but the registry is ` +
96-
`providing signing keys${this.npm.config.get('missing') ? ':\n' : ''}`
97+
`providing signing keys${logMissing ? ':\n' : ''}`
9798
)
98-
// TODO: This might not be the right option for this
99-
if (this.npm.config.get('missing')) {
99+
if (logMissing) {
100100
this.appendOutput(this.humanOutput(missing))
101101
} else {
102-
this.appendOutput(` run \`npm audit signatures --missing\` for details`)
102+
this.appendOutput(` run \`npm audit signatures --log-missing-names\` for details`)
103103
}
104104
}
105105

106106
if (invalid.length) {
107+
const invalidClr = this.npm.color ? chalk.bold(chalk.red('invalid')) : 'invalid'
107108
const msg = invalid.length === 1 ?
108-
`package has an ${this.npm.color ? chalk.bold(chalk.red('invalid')) : 'invalid'}` +
109-
` registry signature` :
110-
`packages have ${this.npm.color ? chalk.bold(chalk.red('invalid')) : 'invalid'}` +
111-
` registry signatures`
109+
`${invalid.length} package has an ${invalidClr} registry signature:\n` :
110+
`${invalid.length} packages have ${invalidClr} registry signatures:\n`
112111
this.appendOutput(
113-
`${missing.length ? '\n' : ''}${invalid.length} ${msg}:\n`
112+
`${missing.length ? '\n' : ''}${msg}`
114113
)
115114
this.appendOutput(this.humanOutput(invalid))
116-
const invPlural = invalid.length === 1 ? '' : 's'
117-
this.appendOutput(
118-
`\nSomeone might have tampered with the package${invPlural} ` +
119-
`since it was published on the registry (monster-in-the-middle attack)!\n`
120-
)
115+
const tamperMsg = invalid.length === 1 ?
116+
`\nSomeone might have tampered with this package since it was ` +
117+
`published on the registry!\n` :
118+
`\nSomeone might have tampered with these packages since they where ` +
119+
`published on the registry!\n`
120+
this.appendOutput(tamperMsg)
121121
}
122122
}
123123
}
@@ -204,7 +204,7 @@ class VerifySignatures {
204204
const parsedRegistry = new URL(registry)
205205
const regKey = `//${parsedRegistry.host}${parsedRegistry.pathname}`
206206
return {
207-
`${regKey}:_keys`: keys
207+
[`${regKey}:_keys`]: keys,
208208
}
209209
}
210210

@@ -369,7 +369,9 @@ class Audit extends ArboristWorkspaceCmd {
369369
case 'fix':
370370
return []
371371
default:
372-
throw new Error(argv[2] + ' not recognized')
372+
throw Object.assign(new Error(argv[2] + ' not recognized'), {
373+
code: 'EUSAGE',
374+
})
373375
}
374376
}
375377

@@ -406,7 +408,15 @@ class Audit extends ArboristWorkspaceCmd {
406408
}
407409

408410
async auditSignatures () {
409-
log.newItem('loading intalled packages')
411+
if (this.npm.config.get('global')) {
412+
throw Object.assign(
413+
new Error('`npm audit signatures` does not support global packages'), {
414+
code: 'EAUDITGLOBAL',
415+
}
416+
)
417+
}
418+
419+
log.newItem('loading intalled dependencies')
410420
const reporter = this.npm.config.get('json') ? 'json' : 'detail'
411421
const opts = {
412422
...this.npm.flatOptions,

tap-snapshots/test/lib/commands/audit.js.test.cjs

+28-15
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,15 @@ audited 1 package in 0s
109109
110110
@npmcli/arborist@1.0.14 (https://verdaccio-clone.org)
111111
112-
Someone might have tampered with the package since it was published on the registry (monster-in-the-middle attack)!
112+
Someone might have tampered with this package since it was published on the registry!
113113
114114
`
115115

116116
exports[`test/lib/commands/audit.js TAP audit signatures third-party registry with keys and missing signatures errors > must match snapshot 1`] = `
117117
audited 1 package in 0s
118118
119119
1 package has a missing registry signature but the registry is providing signing keys
120-
run \`npm audit signatures --missing\` for details
120+
run \`npm audit signatures --log-missing-names\` for details
121121
`
122122

123123
exports[`test/lib/commands/audit.js TAP audit signatures third-party registry with keys and signatures > must match snapshot 1`] = `
@@ -133,13 +133,13 @@ exports[`test/lib/commands/audit.js TAP audit signatures with both invalid and m
133133
audited 2 packages in xxx
134134
135135
1 package has a missing registry signature but the registry is providing signing keys
136-
run \`npm audit signatures --missing\` for details
136+
run \`npm audit signatures --log-missing-names\` for details
137137
138138
1 package has an invalid registry signature:
139139
140140
kms-demo@1.0.0
141141
142-
Someone might have tampered with the package since it was published on the registry (monster-in-the-middle attack)!
142+
Someone might have tampered with this package since it was published on the registry!
143143
144144
`
145145

@@ -151,10 +151,10 @@ verified registry signatures, audited 1 package in 0s
151151
exports[`test/lib/commands/audit.js TAP audit signatures with color output enabled with both valid and missing signatures > must match snapshot 1`] = `
152152
audited 2 packages in xxx
153153
154-
1 packages have [1mverified[22m registry signatures
154+
1 package has a [1mverified[22m registry signature
155155
156156
1 package has a missing registry signature but the registry is providing signing keys
157-
run \`npm audit signatures --missing\` for details
157+
run \`npm audit signatures --log-missing-names\` for details
158158
`
159159

160160
exports[`test/lib/commands/audit.js TAP audit signatures with color output enabled with invalid signatures > must match snapshot 1`] = `
@@ -164,7 +164,7 @@ audited 1 package in 0s
164164
165165
kms-demo@1.0.0
166166
167-
Someone might have tampered with the package since it was published on the registry (monster-in-the-middle attack)!
167+
Someone might have tampered with this package since it was published on the registry!
168168
169169
`
170170

@@ -176,15 +176,15 @@ audited 2 packages in xxx
176176
async@1.1.1
177177
kms-demo@1.0.0
178178
179-
Someone might have tampered with the packages since it was published on the registry (monster-in-the-middle attack)!
179+
Someone might have tampered with these packages since they where published on the registry!
180180
181181
`
182182

183183
exports[`test/lib/commands/audit.js TAP audit signatures with color output enabled with multiple missing signatures > must match snapshot 1`] = `
184184
audited 2 packages in xxx
185185
186186
2 packages have missing registry signatures but the registry is providing signing keys
187-
run \`npm audit signatures --missing\` for details
187+
run \`npm audit signatures --log-missing-names\` for details
188188
`
189189

190190
exports[`test/lib/commands/audit.js TAP audit signatures with invalid signatures > must match snapshot 1`] = `
@@ -194,15 +194,15 @@ audited 1 package in 0s
194194
195195
kms-demo@1.0.0
196196
197-
Someone might have tampered with the package since it was published on the registry (monster-in-the-middle attack)!
197+
Someone might have tampered with this package since it was published on the registry!
198198
199199
`
200200

201201
exports[`test/lib/commands/audit.js TAP audit signatures with keys but missing signature > must match snapshot 1`] = `
202202
audited 1 package in 0s
203203
204204
1 package has a missing registry signature but the registry is providing signing keys
205-
run \`npm audit signatures --missing\` for details
205+
run \`npm audit signatures --log-missing-names\` for details
206206
`
207207

208208
exports[`test/lib/commands/audit.js TAP audit signatures with multiple invalid signatures > must match snapshot 1`] = `
@@ -213,24 +213,37 @@ audited 2 packages in xxx
213213
async@1.1.1
214214
kms-demo@1.0.0
215215
216-
Someone might have tampered with the packages since it was published on the registry (monster-in-the-middle attack)!
216+
Someone might have tampered with these packages since they where published on the registry!
217217
218218
`
219219

220220
exports[`test/lib/commands/audit.js TAP audit signatures with multiple missing signatures > must match snapshot 1`] = `
221221
audited 2 packages in xxx
222222
223223
2 packages have missing registry signatures but the registry is providing signing keys
224-
run \`npm audit signatures --missing\` for details
224+
run \`npm audit signatures --log-missing-names\` for details
225+
`
226+
227+
exports[`test/lib/commands/audit.js TAP audit signatures with multiple valid signatures and one invalid > must match snapshot 1`] = `
228+
audited 3 packages in xxx
229+
230+
2 packages have verified registry signatures
231+
232+
1 package has an invalid registry signature:
233+
234+
node-fetch@1.6.0
235+
236+
Someone might have tampered with this package since it was published on the registry!
237+
225238
`
226239

227240
exports[`test/lib/commands/audit.js TAP audit signatures with valid and missing signatures > must match snapshot 1`] = `
228241
audited 2 packages in xxx
229242
230-
1 packages have verified registry signatures
243+
1 package has a verified registry signature
231244
232245
1 package has a missing registry signature but the registry is providing signing keys
233-
run \`npm audit signatures --missing\` for details
246+
run \`npm audit signatures --log-missing-names\` for details
234247
`
235248

236249
exports[`test/lib/commands/audit.js TAP audit signatures with valid signatures > must match snapshot 1`] = `

0 commit comments

Comments
 (0)