diff --git a/lib/cache.js b/lib/cache.js index 8179037f07ba9..aed2cce31e63f 100644 --- a/lib/cache.js +++ b/lib/cache.js @@ -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}`) } @@ -84,7 +82,7 @@ class Cache extends BaseCommand { 'add ', 'add @', 'clean []', - 'ls', + 'ls [@]', 'verify', ] } @@ -203,22 +201,21 @@ class Cache extends BaseCommand { } // npm cache ls [--package ...] - 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)) } } diff --git a/test/lib/cache.js b/test/lib/cache.js index f4827e2a166c7..c6405303202b8 100644 --- a/test/lib/cache.js +++ b/test/lib/cache.js @@ -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 = {} @@ -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', @@ -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', @@ -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', @@ -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', @@ -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() }) @@ -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() }) @@ -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) @@ -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, [