Skip to content

Commit

Permalink
changed package search to positional arg, locale sorting, improved co…
Browse files Browse the repository at this point in the history
…verage
  • Loading branch information
fritzy committed Aug 18, 2021
1 parent 2adabd1 commit 5ef3d82
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 34 deletions.
17 changes: 7 additions & 10 deletions lib/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,16 @@ const searchCachePackage = async (path, spec, cacheKeys) => {
packument = jsonParse(details.data)
} catch (_) {
// if we couldn't parse the packument, abort
/* istanbul ignore next */
continue
}
/* istanbul ignore next */
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)) {
/* istanbul ignore else */
if (packument.versions[ver].dist
&& typeof packument.versions[ver].dist === 'object'
&& packument.versions[ver].dist.tarball !== undefined
&& cacheKeys.has(`make-fetch-happen:request-cache:${packument.versions[ver].dist.tarball}`))
results.add(`make-fetch-happen:request-cache:${packument.versions[ver].dist.tarball}`)
}
Expand Down Expand Up @@ -84,7 +82,7 @@ class Cache extends BaseCommand {
'add <git url>',
'add <name>@<version>',
'clean [<key>]',
'ls',
'ls [<name>@<version>]',
'verify',
]
}
Expand Down Expand Up @@ -203,22 +201,21 @@ class Cache extends BaseCommand {
}

// npm cache ls [--package <spec> ...]
async ls (args) {
async ls (specs) {
const cachePath = path.join(this.npm.cache, '_cacache')
const cacheKeys = Object.keys(await cacache.ls(cachePath))
if (Array.isArray(this.npm.flatOptions.package)
&& this.npm.flatOptions.package.length > 0) {
if (specs.length > 0) {
// get results for each package spec specified
const results = new Set()
for (const spec of this.npm.flatOptions.package) {
for (const spec of specs) {
const keySet = await searchCachePackage(cachePath, spec, cacheKeys)
for (const key of keySet)
results.add(key)
}
[...results].sort().forEach(key => this.npm.output(key))
[...results].sort((a, b) => a.localeCompare(b, 'en')).forEach(key => this.npm.output(key))
return
}
cacheKeys.sort().forEach(key => this.npm.output(key))
cacheKeys.sort((a, b) => a.localeCompare(b, 'en')).forEach(key => this.npm.output(key))
}
}

Expand Down
96 changes: 72 additions & 24 deletions test/lib/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,23 @@ const setupCacacheFixture = () => {
['@gar/npm-expansion@2.1.0', 'https://registry.npmjs.org', true],
['@gar/npm-expansion@3.0.0-beta', 'https://registry.npmjs.org', true],
['extemporaneously@44.2.2', 'https://somerepo.github.org', false],
['corrupted@3.1.0', 'https://registry.npmjs.org', true],
['missing-dist@23.0.0', 'https://registry.npmjs.org', true],
['missing-version@16.2.0', 'https://registry.npmjs.org', true],
]
pkgs.forEach(pkg => addCacachePkg(...pkg))
// corrupt the packument
cacacheContent[
[cacacheEntries['make-fetch-happen:request-cache:https://registry.npmjs.org/corrupted'].integrity]
].data = Buffer.from('<>>>}"')
// nuke the version dist
cacacheContent[
[cacacheEntries['make-fetch-happen:request-cache:https://registry.npmjs.org/missing-dist'].integrity]
].data = Buffer.from(JSON.stringify({ versions: { '23.0.0': {} } }))
// make the version a non-object
cacacheContent[
[cacacheEntries['make-fetch-happen:request-cache:https://registry.npmjs.org/missing-version'].integrity]
].data = Buffer.from(JSON.stringify({ versions: 'hello' }))
}

const packuments = {}
Expand Down Expand Up @@ -229,22 +244,27 @@ t.test('cache add multiple pkgs', t => {
t.test('cache ls', t => {
t.teardown(() => {
outputOutput = []
npm.flatOptions.package = undefined
logOutput = []
})
setupCacacheFixture()
cache.exec(['ls'], err => {
t.error(err)
t.strictSame(outputOutput, [
'make-fetch-happen:request-cache:https://registry.npmjs.org/@fritzy%2fstaydown',
'make-fetch-happen:request-cache:https://registry.npmjs.org/@fritzy/staydown/-/@fritzy/staydown-3.1.1.tgz',
'make-fetch-happen:request-cache:https://registry.npmjs.org/@gar%2fnpm-expansion',
'make-fetch-happen:request-cache:https://registry.npmjs.org/@fritzy%2fstaydown',
'make-fetch-happen:request-cache:https://registry.npmjs.org/@gar/npm-expansion/-/@gar/npm-expansion-2.1.0.tgz',
'make-fetch-happen:request-cache:https://registry.npmjs.org/@gar/npm-expansion/-/@gar/npm-expansion-3.0.0-beta.tgz',
'make-fetch-happen:request-cache:https://registry.npmjs.org/@gar%2fnpm-expansion',
'make-fetch-happen:request-cache:https://registry.npmjs.org/ape-ecs',
'make-fetch-happen:request-cache:https://registry.npmjs.org/ape-ecs/-/ape-ecs-2.1.7.tgz',
'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',
'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',
'make-fetch-happen:request-cache:https://registry.npmjs.org/missing-dist',
'make-fetch-happen:request-cache:https://registry.npmjs.org/missing-dist/-/missing-dist-23.0.0.tgz',
'make-fetch-happen:request-cache:https://registry.npmjs.org/missing-version',
'make-fetch-happen:request-cache:https://registry.npmjs.org/missing-version/-/missing-version-16.2.0.tgz',
'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',
Expand All @@ -260,10 +280,8 @@ t.test('cache ls', t => {
t.test('cache ls pkgs', t => {
t.teardown(() => {
outputOutput = []
npm.flatOptions.package = undefined
})
npm.flatOptions.package = ['webpack@>4.44.1', 'npm']
cache.exec(['ls'], err => {
cache.exec(['ls', 'webpack@>4.44.1', 'npm'], err => {
t.error(err)
t.strictSame(outputOutput, [
'make-fetch-happen:request-cache:https://registry.npmjs.org/npm',
Expand All @@ -278,10 +296,8 @@ t.test('cache ls pkgs', t => {
t.test('cache ls special', t => {
t.teardown(() => {
outputOutput = []
npm.flatOptions.package = undefined
})
npm.flatOptions.package = ['foo@1.2.3-beta']
cache.exec(['ls'], err => {
cache.exec(['ls', 'foo@1.2.3-beta'], err => {
t.error(err)
t.strictSame(outputOutput, [
'make-fetch-happen:request-cache:https://registry.npmjs.org/foo',
Expand All @@ -294,10 +310,8 @@ t.test('cache ls special', t => {
t.test('cache ls nonpublic registry', t => {
t.teardown(() => {
outputOutput = []
npm.flatOptions.package = undefined
})
npm.flatOptions.package = ['extemporaneously']
cache.exec(['ls'], err => {
cache.exec(['ls', 'extemporaneously'], err => {
t.error(err)
t.strictSame(outputOutput, [
'make-fetch-happen:request-cache:https://somerepo.github.org/aabbcc/14',
Expand All @@ -310,10 +324,8 @@ t.test('cache ls nonpublic registry', t => {
t.test('cache ls tagged', t => {
t.teardown(() => {
outputOutput = []
npm.flatOptions.package = undefined
})
npm.flatOptions.package = ['webpack@latest']
cache.exec(['ls'], err => {
cache.exec(['ls', 'webpack@latest'], err => {
t.match(err.message, 'tagged package', 'should throw warning')
t.end()
})
Expand All @@ -322,16 +334,56 @@ t.test('cache ls tagged', t => {
t.test('cache ls scoped and scoped slash', t => {
t.teardown(() => {
outputOutput = []
npm.flatOptions.package = undefined
})
npm.flatOptions.package = ['@fritzy/staydown', '@gar/npm-expansion']
cache.exec(['ls'], err => {
cache.exec(['ls', '@fritzy/staydown', '@gar/npm-expansion'], err => {
t.error(err)
t.strictSame(outputOutput, [
'make-fetch-happen:request-cache:https://registry.npmjs.org/@fritzy%2fstaydown',
'make-fetch-happen:request-cache:https://registry.npmjs.org/@fritzy/staydown/-/@fritzy/staydown-3.1.1.tgz',
'make-fetch-happen:request-cache:https://registry.npmjs.org/@gar%2fnpm-expansion',
'make-fetch-happen:request-cache:https://registry.npmjs.org/@fritzy%2fstaydown',
'make-fetch-happen:request-cache:https://registry.npmjs.org/@gar/npm-expansion/-/@gar/npm-expansion-2.1.0.tgz',
'make-fetch-happen:request-cache:https://registry.npmjs.org/@gar%2fnpm-expansion',
])
t.end()
})
})

t.test('cache ls corrupted', t => {
t.teardown(() => {
outputOutput = []
})
cache.exec(['ls', 'corrupted'], err => {
t.error(err)
t.strictSame(outputOutput, [
'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',
])
t.end()
})
})

t.test('cache ls missing packument dist', t => {
t.teardown(() => {
outputOutput = []
})
cache.exec(['ls', 'missing-dist'], err => {
t.error(err)
t.strictSame(outputOutput, [
'make-fetch-happen:request-cache:https://registry.npmjs.org/missing-dist',
'make-fetch-happen:request-cache:https://registry.npmjs.org/missing-dist/-/missing-dist-23.0.0.tgz',
])
t.end()
})
})

t.test('cache ls missing packument version not an object', t => {
t.teardown(() => {
outputOutput = []
})
cache.exec(['ls', 'missing-version'], err => {
t.error(err)
t.strictSame(outputOutput, [
'make-fetch-happen:request-cache:https://registry.npmjs.org/missing-version',
'make-fetch-happen:request-cache:https://registry.npmjs.org/missing-version/-/missing-version-16.2.0.tgz',
])
t.end()
})
Expand All @@ -340,9 +392,7 @@ t.test('cache ls scoped and scoped slash', t => {
t.test('cache rm', t => {
t.teardown(() => {
outputOutput = []
npm.flatOptions.package = undefined
})
npm.flatOptions.package = ['webpack']
cache.exec(['rm',
'make-fetch-happen:request-cache:https://registry.npmjs.org/webpack/-/webpack-4.44.1.tgz'], err => {
t.error(err)
Expand All @@ -356,10 +406,8 @@ t.test('cache rm', t => {
t.test('cache rm unfound', t => {
t.teardown(() => {
outputOutput = []
npm.flatOptions.package = undefined
logOutput = []
})
npm.flatOptions.package = ['webpack']
cache.exec(['rm', 'made-up-key'], err => {
t.error(err)
t.strictSame(logOutput, [
Expand Down

0 comments on commit 5ef3d82

Please sign in to comment.