Skip to content

Commit

Permalink
fix: clean up npm cache tests (#4910)
Browse files Browse the repository at this point in the history
The tests use real data now, a bare throw that is not a usageError was
also found and changed to a usageError
  • Loading branch information
wraithgar committed May 18, 2022
1 parent 400c80f commit 124df81
Show file tree
Hide file tree
Showing 3 changed files with 283 additions and 351 deletions.
14 changes: 8 additions & 6 deletions lib/commands/cache.js
Expand Up @@ -10,11 +10,7 @@ const jsonParse = require('json-parse-even-better-errors')
const localeCompare = require('@isaacs/string-locale-compare')('en')
const log = require('../utils/log-shim')

const searchCachePackage = async (path, spec, cacheKeys) => {
const parsed = npa(spec)
if (parsed.rawSpec !== '' && parsed.type === 'tag') {
throw new Error(`Cannot list cache keys for a tagged package.`)
}
const searchCachePackage = async (path, parsed, cacheKeys) => {
/* eslint-disable-next-line max-len */
const searchMFH = new RegExp(`^make-fetch-happen:request-cache:.*(?<!/[@a-zA-Z]+)/${parsed.name}/-/(${parsed.name}[^/]+.tgz)$`)
const searchPack = new RegExp(`^make-fetch-happen:request-cache:.*/${parsed.escapedName}$`)
Expand Down Expand Up @@ -50,6 +46,7 @@ const searchCachePackage = async (path, spec, cacheKeys) => {
if (!packument.versions || typeof packument.versions !== 'object') {
continue
}

// assuming this is a packument
for (const ver of Object.keys(packument.versions)) {
if (semver.satisfies(ver, parsed.rawSpec)) {
Expand Down Expand Up @@ -148,6 +145,7 @@ class Cache extends BaseCommand {
}
this.npm.output(`Deleted: ${key}`)
await cacache.rm.entry(cachePath, key)
// XXX this could leave other entries without content!
await cacache.rm.content(cachePath, entry.integrity)
}
}
Expand Down Expand Up @@ -204,7 +202,11 @@ class Cache extends BaseCommand {
// get results for each package spec specified
const results = new Set()
for (const spec of specs) {
const keySet = await searchCachePackage(cachePath, spec, cacheKeys)
const parsed = npa(spec)
if (parsed.rawSpec !== '' && parsed.type === 'tag') {
throw this.usageError('Cannot list cache keys for a tagged package.')
}
const keySet = await searchCachePackage(cachePath, parsed, cacheKeys)
for (const key of keySet) {
results.add(key)
}
Expand Down
63 changes: 63 additions & 0 deletions tap-snapshots/test/lib/commands/cache.js.test.cjs
@@ -0,0 +1,63 @@
/* IMPORTANT
* This snapshot file is auto-generated, but designed for humans.
* It should be checked into source control and tracked carefully.
* Re-generate by setting TAP_SNAPSHOT=1 and running tests.
* Make sure to inspect the output below. Do not ignore changes!
*/
'use strict'
exports[`test/lib/commands/cache.js TAP cache ls > logs cache entries 1`] = `
make-fetch-happen:request-cache:https://registry.npmjs.org/test-package
make-fetch-happen:request-cache:https://registry.npmjs.org/test-package/-/test-package-1.0.0.tgz
`

exports[`test/lib/commands/cache.js TAP cache ls corrupted > logs cache entries with bad data 1`] = `
make-fetch-happen:request-cache:https://registry.npmjs.org/corrupted
make-fetch-happen:request-cache:https://registry.npmjs.org/corrupted/-/corrupted-3.1.0.tgz
`

exports[`test/lib/commands/cache.js TAP cache ls missing packument version not an object > logs cache entry for packument 1`] = `
make-fetch-happen:request-cache:https://registry.npmjs.org/missing-version
`

exports[`test/lib/commands/cache.js TAP cache ls nonpublic registry > logs cache entry for extemporaneously and its tarball 1`] = `
make-fetch-happen:request-cache:https://somerepo.github.org/aabbcc/
make-fetch-happen:request-cache:https://somerepo.github.org/extemporaneously
`

exports[`test/lib/commands/cache.js TAP cache ls pkgs > logs cache entries for npm and webpack and one webpack tgz 1`] = `
make-fetch-happen:request-cache:https://registry.npmjs.org/npm
make-fetch-happen:request-cache:https://registry.npmjs.org/npm/-/npm-1.2.0.tgz
make-fetch-happen:request-cache:https://registry.npmjs.org/webpack
make-fetch-happen:request-cache:https://registry.npmjs.org/webpack/-/webpack-4.47.0.tgz
`

exports[`test/lib/commands/cache.js TAP cache ls scoped and scoped slash > logs cache entries for @gar and @fritzy 1`] = `
make-fetch-happen:request-cache:https://registry.npmjs.org/@fritzy%2fstaydown
make-fetch-happen:request-cache:https://registry.npmjs.org/@gar%2fnpm-expansion
`

exports[`test/lib/commands/cache.js TAP cache ls special > logs cache entries for foo 1`] = `
make-fetch-happen:request-cache:https://registry.npmjs.org/foo
make-fetch-happen:request-cache:https://registry.npmjs.org/foo/-/foo-1.2.3-beta.tgz
`

exports[`test/lib/commands/cache.js TAP cache rm > logs deleting single entry 1`] = `
Deleted: make-fetch-happen:request-cache:https://registry.npmjs.org/test-package/-/test-package-1.0.0.tgz
`

exports[`test/lib/commands/cache.js TAP cache verify > shows verified cache output 1`] = `
Cache verified and compressed ({PATH})
Content verified: 0 (0 bytes)
Index entries: 0
Finished in xxxs
`

exports[`test/lib/commands/cache.js TAP cache verify w/ extra output > shows extra output 1`] = `
Cache verified and compressed ({PATH})
Content verified: 17057 (1644485260 bytes)
Corrupted content removed: 12345
Content garbage-collected: 1144 (248164665 bytes)
Missing content: 92
Index entries: 20175
Finished in xxxs
`

0 comments on commit 124df81

Please sign in to comment.