Skip to content

Commit 124df81

Browse files
authored
fix: clean up npm cache tests (#4910)
The tests use real data now, a bare throw that is not a usageError was also found and changed to a usageError
1 parent 400c80f commit 124df81

File tree

3 files changed

+283
-351
lines changed

3 files changed

+283
-351
lines changed

lib/commands/cache.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ const jsonParse = require('json-parse-even-better-errors')
1010
const localeCompare = require('@isaacs/string-locale-compare')('en')
1111
const log = require('../utils/log-shim')
1212

13-
const searchCachePackage = async (path, spec, cacheKeys) => {
14-
const parsed = npa(spec)
15-
if (parsed.rawSpec !== '' && parsed.type === 'tag') {
16-
throw new Error(`Cannot list cache keys for a tagged package.`)
17-
}
13+
const searchCachePackage = async (path, parsed, cacheKeys) => {
1814
/* eslint-disable-next-line max-len */
1915
const searchMFH = new RegExp(`^make-fetch-happen:request-cache:.*(?<!/[@a-zA-Z]+)/${parsed.name}/-/(${parsed.name}[^/]+.tgz)$`)
2016
const searchPack = new RegExp(`^make-fetch-happen:request-cache:.*/${parsed.escapedName}$`)
@@ -50,6 +46,7 @@ const searchCachePackage = async (path, spec, cacheKeys) => {
5046
if (!packument.versions || typeof packument.versions !== 'object') {
5147
continue
5248
}
49+
5350
// assuming this is a packument
5451
for (const ver of Object.keys(packument.versions)) {
5552
if (semver.satisfies(ver, parsed.rawSpec)) {
@@ -148,6 +145,7 @@ class Cache extends BaseCommand {
148145
}
149146
this.npm.output(`Deleted: ${key}`)
150147
await cacache.rm.entry(cachePath, key)
148+
// XXX this could leave other entries without content!
151149
await cacache.rm.content(cachePath, entry.integrity)
152150
}
153151
}
@@ -204,7 +202,11 @@ class Cache extends BaseCommand {
204202
// get results for each package spec specified
205203
const results = new Set()
206204
for (const spec of specs) {
207-
const keySet = await searchCachePackage(cachePath, spec, cacheKeys)
205+
const parsed = npa(spec)
206+
if (parsed.rawSpec !== '' && parsed.type === 'tag') {
207+
throw this.usageError('Cannot list cache keys for a tagged package.')
208+
}
209+
const keySet = await searchCachePackage(cachePath, parsed, cacheKeys)
208210
for (const key of keySet) {
209211
results.add(key)
210212
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/* IMPORTANT
2+
* This snapshot file is auto-generated, but designed for humans.
3+
* It should be checked into source control and tracked carefully.
4+
* Re-generate by setting TAP_SNAPSHOT=1 and running tests.
5+
* Make sure to inspect the output below. Do not ignore changes!
6+
*/
7+
'use strict'
8+
exports[`test/lib/commands/cache.js TAP cache ls > logs cache entries 1`] = `
9+
make-fetch-happen:request-cache:https://registry.npmjs.org/test-package
10+
make-fetch-happen:request-cache:https://registry.npmjs.org/test-package/-/test-package-1.0.0.tgz
11+
`
12+
13+
exports[`test/lib/commands/cache.js TAP cache ls corrupted > logs cache entries with bad data 1`] = `
14+
make-fetch-happen:request-cache:https://registry.npmjs.org/corrupted
15+
make-fetch-happen:request-cache:https://registry.npmjs.org/corrupted/-/corrupted-3.1.0.tgz
16+
`
17+
18+
exports[`test/lib/commands/cache.js TAP cache ls missing packument version not an object > logs cache entry for packument 1`] = `
19+
make-fetch-happen:request-cache:https://registry.npmjs.org/missing-version
20+
`
21+
22+
exports[`test/lib/commands/cache.js TAP cache ls nonpublic registry > logs cache entry for extemporaneously and its tarball 1`] = `
23+
make-fetch-happen:request-cache:https://somerepo.github.org/aabbcc/
24+
make-fetch-happen:request-cache:https://somerepo.github.org/extemporaneously
25+
`
26+
27+
exports[`test/lib/commands/cache.js TAP cache ls pkgs > logs cache entries for npm and webpack and one webpack tgz 1`] = `
28+
make-fetch-happen:request-cache:https://registry.npmjs.org/npm
29+
make-fetch-happen:request-cache:https://registry.npmjs.org/npm/-/npm-1.2.0.tgz
30+
make-fetch-happen:request-cache:https://registry.npmjs.org/webpack
31+
make-fetch-happen:request-cache:https://registry.npmjs.org/webpack/-/webpack-4.47.0.tgz
32+
`
33+
34+
exports[`test/lib/commands/cache.js TAP cache ls scoped and scoped slash > logs cache entries for @gar and @fritzy 1`] = `
35+
make-fetch-happen:request-cache:https://registry.npmjs.org/@fritzy%2fstaydown
36+
make-fetch-happen:request-cache:https://registry.npmjs.org/@gar%2fnpm-expansion
37+
`
38+
39+
exports[`test/lib/commands/cache.js TAP cache ls special > logs cache entries for foo 1`] = `
40+
make-fetch-happen:request-cache:https://registry.npmjs.org/foo
41+
make-fetch-happen:request-cache:https://registry.npmjs.org/foo/-/foo-1.2.3-beta.tgz
42+
`
43+
44+
exports[`test/lib/commands/cache.js TAP cache rm > logs deleting single entry 1`] = `
45+
Deleted: make-fetch-happen:request-cache:https://registry.npmjs.org/test-package/-/test-package-1.0.0.tgz
46+
`
47+
48+
exports[`test/lib/commands/cache.js TAP cache verify > shows verified cache output 1`] = `
49+
Cache verified and compressed ({PATH})
50+
Content verified: 0 (0 bytes)
51+
Index entries: 0
52+
Finished in xxxs
53+
`
54+
55+
exports[`test/lib/commands/cache.js TAP cache verify w/ extra output > shows extra output 1`] = `
56+
Cache verified and compressed ({PATH})
57+
Content verified: 17057 (1644485260 bytes)
58+
Corrupted content removed: 12345
59+
Content garbage-collected: 1144 (248164665 bytes)
60+
Missing content: 92
61+
Index entries: 20175
62+
Finished in xxxs
63+
`

0 commit comments

Comments
 (0)