From ed9cd937c859a82637d91cf72776329c8f7748e8 Mon Sep 17 00:00:00 2001 From: Michael Smith Date: Tue, 11 Feb 2025 10:57:53 -0800 Subject: [PATCH] added tests for new npx functionality --- lib/commands/cache.js | 15 +- .../test/lib/commands/cache.js.test.cjs | 76 ++++ test/lib/commands/cache.js | 404 ++++++++++++++++++ 3 files changed, 490 insertions(+), 5 deletions(-) diff --git a/lib/commands/cache.js b/lib/commands/cache.js index ed085cd0390d1..507fe6a080e62 100644 --- a/lib/commands/cache.js +++ b/lib/commands/cache.js @@ -1,5 +1,5 @@ const fs = require('node:fs/promises') -const { join, dirname, resolve } = require('node:path') +const { join } = require('node:path') const cacache = require('cacache') const pacote = require('pacote') const semver = require('semver') @@ -253,7 +253,7 @@ class Cache extends BaseCommand { cache[e] = { hash: e, path: pkgPath, - valid: false + valid: false, } try { const pkgJson = await PkgJson.load(pkgPath) @@ -298,18 +298,23 @@ class Cache extends BaseCommand { if (!this.npm.config.get('force')) { throw this.usageError('Please use --force to remove entire npx cache') } + const { npxCache } = this.npm.flatOptions + if (!this.npm.config.get('dry-run')) { + return fs.rm(npxCache, { recursive: true, force: true }) + } } + const cache = await this.#npxCache(keys) for (const key in cache) { const { path: cachePath } = cache[key] output.standard(`Removing npx key at ${cachePath}`) if (!this.npm.config.get('dry-run')) { - return fs.rm(cachePath, { recursive: true }) + await fs.rm(cachePath, { recursive: true }) } } } - async npxInfo(keys) { + async npxInfo (keys) { const chalk = this.npm.chalk if (!keys.length) { throw this.usageError() @@ -348,7 +353,7 @@ class Cache extends BaseCommand { } } } - } catch { + } catch (ex) { valid = false } const v = valid ? chalk.green('valid') : chalk.red('invalid') diff --git a/tap-snapshots/test/lib/commands/cache.js.test.cjs b/tap-snapshots/test/lib/commands/cache.js.test.cjs index 1cd699453478e..7e2a2047701c1 100644 --- a/tap-snapshots/test/lib/commands/cache.js.test.cjs +++ b/tap-snapshots/test/lib/commands/cache.js.test.cjs @@ -41,6 +41,82 @@ 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 npx info: valid and invalid entry > shows invalid package info 1`] = ` +invalid npx cache entry with key deadbeef +location: /Users/owlstronaut/Documents/npmjs/cli/test/lib/commands/tap-testdir-cache-cache-npx-info-valid-and-invalid-entry/cache/_npx/deadbeef + +invalid npx cache entry with key badc0de +location: /Users/owlstronaut/Documents/npmjs/cli/test/lib/commands/tap-testdir-cache-cache-npx-info-valid-and-invalid-entry/cache/_npx/badc0de +` + +exports[`test/lib/commands/cache.js TAP cache npx info: valid and invalid entry > shows valid package info 1`] = ` +invalid npx cache entry with key deadbeef +location: /Users/owlstronaut/Documents/npmjs/cli/test/lib/commands/tap-testdir-cache-cache-npx-info-valid-and-invalid-entry/cache/_npx/deadbeef +` + +exports[`test/lib/commands/cache.js TAP cache npx info: valid entry with _npx directory package > shows valid package info with _npx directory package 1`] = ` +valid npx cache entry with key valid123 +location: /Users/owlstronaut/Documents/npmjs/cli/test/lib/commands/tap-testdir-cache-cache-npx-info-valid-entry-with-_npx-directory-package/cache/_npx/valid123 +packages: +- /path/to/valid-package +` + +exports[`test/lib/commands/cache.js TAP cache npx info: valid entry with _npx packages > shows valid package info with _npx packages 1`] = ` +valid npx cache entry with key valid123 +location: /Users/owlstronaut/Documents/npmjs/cli/test/lib/commands/tap-testdir-cache-cache-npx-info-valid-entry-with-_npx-packages/cache/_npx/valid123 +packages: +- valid-package@1.0.0 (valid-package@1.0.0) +` + +exports[`test/lib/commands/cache.js TAP cache npx info: valid entry with a link dependency > shows link dependency realpath (child.isLink branch) 1`] = ` +valid npx cache entry with key link123 +location: /Users/owlstronaut/Documents/npmjs/cli/test/lib/commands/tap-testdir-cache-cache-npx-info-valid-entry-with-a-link-dependency/cache/_npx/link123 +packages: (unknown) +dependencies: +- /Users/owlstronaut/Documents/npmjs/cli/test/lib/commands/tap-testdir-cache-cache-npx-info-valid-entry-with-a-link-dependency/cache/_npx/some-other-loc +` + +exports[`test/lib/commands/cache.js TAP cache npx info: valid entry with dependencies > shows valid package info with dependencies 1`] = ` +valid npx cache entry with key valid456 +location: /Users/owlstronaut/Documents/npmjs/cli/test/lib/commands/tap-testdir-cache-cache-npx-info-valid-entry-with-dependencies/cache/_npx/valid456 +packages: (unknown) +dependencies: +- dep-package@1.0.0 +` + +exports[`test/lib/commands/cache.js TAP cache npx ls: empty cache > logs message for empty npx cache 1`] = ` +npx cache does not exist +` + +exports[`test/lib/commands/cache.js TAP cache npx ls: entry with unknown package > lists entry with unknown package 1`] = ` +unknown123: (unknown) +` + +exports[`test/lib/commands/cache.js TAP cache npx ls: some entries > lists one valid and one invalid entry 1`] = ` +abc123: fake-npx-package@1.0.0 +z9y8x7: (empty/invalid) +` + +exports[`test/lib/commands/cache.js TAP cache npx rm: remove single entry > logs removing single npx cache entry 1`] = ` +Removing npx key at /Users/owlstronaut/Documents/npmjs/cli/test/lib/commands/tap-testdir-cache-cache-npx-rm-remove-single-entry/cache/_npx/123removeme +Removing npx key at /Users/owlstronaut/Documents/npmjs/cli/test/lib/commands/tap-testdir-cache-cache-npx-rm-remove-single-entry/cache/_npx/123removeme +` + +exports[`test/lib/commands/cache.js TAP cache npx rm: removing all with --force works > logs removing everything 1`] = ` +Removing npx key at /Users/owlstronaut/Documents/npmjs/cli/test/lib/commands/tap-testdir-cache-cache-npx-rm-removing-all-with---force-works/cache/_npx/remove-all-yes-force +` + +exports[`test/lib/commands/cache.js TAP cache npx rm: removing all without --force fails > logs usage error when removing all without --force 1`] = ` + +` + +exports[`test/lib/commands/cache.js TAP cache npx rm: removing more than 1, less than all entries > logs removing 2 of 3 entries 1`] = ` +Removing npx key at /Users/owlstronaut/Documents/npmjs/cli/test/lib/commands/tap-testdir-cache-cache-npx-rm-removing-more-than-1-less-than-all-entries/cache/_npx/123removeme +Removing npx key at /Users/owlstronaut/Documents/npmjs/cli/test/lib/commands/tap-testdir-cache-cache-npx-rm-removing-more-than-1-less-than-all-entries/cache/_npx/456removeme +Removing npx key at /Users/owlstronaut/Documents/npmjs/cli/test/lib/commands/tap-testdir-cache-cache-npx-rm-removing-more-than-1-less-than-all-entries/cache/_npx/123removeme +Removing npx key at /Users/owlstronaut/Documents/npmjs/cli/test/lib/commands/tap-testdir-cache-cache-npx-rm-removing-more-than-1-less-than-all-entries/cache/_npx/456removeme +` + 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 ` diff --git a/test/lib/commands/cache.js b/test/lib/commands/cache.js index a1f2a8fbfda02..4c0db3d91d659 100644 --- a/test/lib/commands/cache.js +++ b/test/lib/commands/cache.js @@ -8,6 +8,20 @@ const path = require('node:path') const pkg = 'test-package' +const createNpxCacheEntry = (npxCacheDir, hash, pkgJson, shrinkwrapJson) => { + fs.mkdirSync(path.join(npxCacheDir, hash)) + fs.writeFileSync( + path.join(npxCacheDir, hash, 'package.json'), + JSON.stringify(pkgJson) + ) + if (shrinkwrapJson) { + fs.writeFileSync( + path.join(npxCacheDir, hash, 'npm-shrinkwrap.json'), + JSON.stringify(shrinkwrapJson) + ) + } +} + t.cleanSnapshot = str => { return str .replace(/Finished in [0-9.s]+/g, 'Finished in xxxs') @@ -310,3 +324,393 @@ t.test('cache completion', async t => { testComp(['npm', 'cache', 'verify'], []), ]) }) + +t.test('cache npx ls: empty cache', async t => { + const { npm, joinedOutput } = await loadMockNpm(t) + await npm.exec('cache', ['npx', 'ls']) + t.matchSnapshot(joinedOutput(), 'logs message for empty npx cache') +}) + +t.test('cache npx ls: some entries', async t => { + const { npm, joinedOutput } = await loadMockNpm(t) + const npxCacheDir = path.join(npm.flatOptions.npxCache || path.join(npm.cache, '..', '_npx')) + fs.mkdirSync(npxCacheDir, { recursive: true }) + + // Make two fake entries: one valid, one invalid + const hash1 = 'abc123' + const hash2 = 'z9y8x7' + fs.mkdirSync(path.join(npxCacheDir, hash1)) + fs.writeFileSync( + path.join(npxCacheDir, hash1, 'package.json'), + JSON.stringify({ + name: 'fake-npx-package', + version: '1.0.0', + _npx: { packages: ['fake-npx-package@1.0.0'] }, + }) + ) + // invalid (missing or broken package.json) directory + fs.mkdirSync(path.join(npxCacheDir, hash2)) + + await npm.exec('cache', ['npx', 'ls']) + t.matchSnapshot(joinedOutput(), 'lists one valid and one invalid entry') +}) + +t.test('cache npx info: valid and invalid entry', async t => { + const { npm, joinedOutput } = await loadMockNpm(t) + const npxCacheDir = path.join(npm.flatOptions.npxCache || path.join(npm.cache, '..', '_npx')) + fs.mkdirSync(npxCacheDir, { recursive: true }) + + const goodHash = 'deadbeef' + fs.mkdirSync(path.join(npxCacheDir, goodHash)) + fs.writeFileSync( + path.join(npxCacheDir, goodHash, 'package.json'), + JSON.stringify({ + name: 'good-npx-package', + version: '2.0.0', + dependencies: { + rimraf: '^3.0.0', + }, + _npx: { packages: ['good-npx-package@2.0.0'] }, + }) + ) + + const badHash = 'badc0de' + fs.mkdirSync(path.join(npxCacheDir, badHash)) + + await npm.exec('cache', ['npx', 'info', goodHash]) + t.matchSnapshot(joinedOutput(), 'shows valid package info') + + await npm.exec('cache', ['npx', 'info', badHash]) + t.matchSnapshot(joinedOutput(), 'shows invalid package info') +}) + +t.test('cache npx rm: remove single entry', async t => { + const { npm, joinedOutput } = await loadMockNpm(t) + const npxCacheDir = path.join(npm.flatOptions.npxCache || path.join(npm.cache, '..', '_npx')) + fs.mkdirSync(npxCacheDir, { recursive: true }) + + const removableHash = '123removeme' + fs.mkdirSync(path.join(npxCacheDir, removableHash)) + fs.writeFileSync( + path.join(npxCacheDir, removableHash, 'package.json'), + JSON.stringify({ name: 'removable-package', _npx: { packages: ['removable-package@1.0.0'] } }) + ) + + npm.config.set('dry-run', true) + await npm.exec('cache', ['npx', 'rm', removableHash]) + t.ok(fs.existsSync(path.join(npxCacheDir, removableHash)), 'entry folder remains') + npm.config.set('dry-run', false) + + await npm.exec('cache', ['npx', 'rm', removableHash]) + t.matchSnapshot(joinedOutput(), 'logs removing single npx cache entry') + t.notOk(fs.existsSync(path.join(npxCacheDir, removableHash)), 'entry folder removed') +}) + +t.test('cache npx rm: removing all without --force fails', async t => { + const { npm, joinedOutput } = await loadMockNpm(t) + const npxCacheDir = path.join(npm.flatOptions.npxCache || path.join(npm.cache, '..', '_npx')) + fs.mkdirSync(npxCacheDir, { recursive: true }) + + const testHash = 'remove-all-no-force' + fs.mkdirSync(path.join(npxCacheDir, testHash)) + fs.writeFileSync( + path.join(npxCacheDir, testHash, 'package.json'), + JSON.stringify({ name: 'no-force-pkg', _npx: { packages: ['no-force-pkg@1.0.0'] } }) + ) + + await t.rejects( + npm.exec('cache', ['npx', 'rm']), + /Please use --force to remove entire npx cache/, + 'fails without --force' + ) + t.matchSnapshot(joinedOutput(), 'logs usage error when removing all without --force') + + t.ok(fs.existsSync(path.join(npxCacheDir, testHash)), 'folder still exists') +}) + +t.test('cache npx rm: removing all with --force works', async t => { + const { npm, joinedOutput } = await loadMockNpm(t, { + config: { force: true }, + }) + const npxCacheDir = path.join(npm.flatOptions.npxCache || path.join(npm.cache, '..', '_npx')) + fs.mkdirSync(npxCacheDir, { recursive: true }) + + const testHash = 'remove-all-yes-force' + fs.mkdirSync(path.join(npxCacheDir, testHash)) + fs.writeFileSync( + path.join(npxCacheDir, testHash, 'package.json'), + JSON.stringify({ name: 'yes-force-pkg', _npx: { packages: ['yes-force-pkg@1.0.0'] } }) + ) + + npm.config.set('dry-run', true) + await npm.exec('cache', ['npx', 'rm']) + t.ok(fs.existsSync(npxCacheDir), 'npx cache directory remains') + npm.config.set('dry-run', false) + + await npm.exec('cache', ['npx', 'rm']) + + t.matchSnapshot(joinedOutput(), 'logs removing everything') + t.notOk(fs.existsSync(npxCacheDir), 'npx cache directory removed') +}) + +t.test('cache npx rm: removing more than 1, less than all entries', async t => { + const { npm, joinedOutput } = await loadMockNpm(t) + const npxCacheDir = path.join(npm.flatOptions.npxCache || path.join(npm.cache, '..', '_npx')) + fs.mkdirSync(npxCacheDir, { recursive: true }) + + // Removable folder + const removableHash = '123removeme' + fs.mkdirSync(path.join(npxCacheDir, removableHash)) + fs.writeFileSync( + path.join(npxCacheDir, removableHash, 'package.json'), + JSON.stringify({ name: 'removable-package', _npx: { packages: ['removable-package@1.0.0'] } }) + ) + + // Another Removable folder + const anotherRemovableHash = '456removeme' + fs.mkdirSync(path.join(npxCacheDir, anotherRemovableHash)) + fs.writeFileSync( + path.join(npxCacheDir, anotherRemovableHash, 'package.json'), + JSON.stringify({ name: 'another-removable-package', _npx: { packages: ['another-removable-package@1.0.0'] } }) + ) + + // Another folder that should remain + const keepHash = '999keep' + fs.mkdirSync(path.join(npxCacheDir, keepHash)) + fs.writeFileSync( + path.join(npxCacheDir, keepHash, 'package.json'), + JSON.stringify({ name: 'keep-package', _npx: { packages: ['keep-package@1.0.0'] } }) + ) + + npm.config.set('dry-run', true) + await npm.exec('cache', ['npx', 'rm', removableHash, anotherRemovableHash]) + t.ok(fs.existsSync(path.join(npxCacheDir, removableHash)), 'entry folder remains') + t.ok(fs.existsSync(path.join(npxCacheDir, anotherRemovableHash)), 'entry folder remains') + t.ok(fs.existsSync(path.join(npxCacheDir, keepHash)), 'entry folder remains') + npm.config.set('dry-run', false) + + await npm.exec('cache', ['npx', 'rm', removableHash, anotherRemovableHash]) + t.matchSnapshot(joinedOutput(), 'logs removing 2 of 3 entries') + + t.notOk(fs.existsSync(path.join(npxCacheDir, removableHash)), 'removed folder no longer exists') + t.notOk(fs.existsSync(path.join(npxCacheDir, anotherRemovableHash)), 'the other folder no longer exists') + t.ok(fs.existsSync(path.join(npxCacheDir, keepHash)), 'the other folder remains') +}) + +t.test('cache npx should throw usage error', async t => { + const { npm } = await loadMockNpm(t) + await t.rejects( + npm.exec('cache', ['npx', 'badcommand']), + { code: 'EUSAGE' }, + 'should throw usage error' + ) +}) + +t.test('cache npx should throw usage error for invalid key', async t => { + const { npm } = await loadMockNpm(t) + const npxCacheDir = path.join(npm.flatOptions.npxCache || path.join(npm.cache, '..', '_npx')) + fs.mkdirSync(npxCacheDir, { recursive: true }) + + const key = 'badkey' + await t.rejects( + npm.exec('cache', ['npx', 'rm', key]), + { code: 'EUSAGE' }, + `Invalid npx key ${key}` + ) +}) + +t.test('cache npx ls: entry with unknown package', async t => { + const { npm, joinedOutput } = await loadMockNpm(t) + const npxCacheDir = path.join(npm.flatOptions.npxCache || path.join(npm.cache, '..', '_npx')) + fs.mkdirSync(npxCacheDir, { recursive: true }) + + // Create an entry without the _npx property + const unknownHash = 'unknown123' + fs.mkdirSync(path.join(npxCacheDir, unknownHash)) + fs.writeFileSync( + path.join(npxCacheDir, unknownHash, 'package.json'), + JSON.stringify({ + name: 'unknown-package', + version: '1.0.0', + }) + ) + + await npm.exec('cache', ['npx', 'ls']) + t.matchSnapshot(joinedOutput(), 'lists entry with unknown package') +}) + +t.test('cache npx info: should throw usage error when no keys are provided', async t => { + const { npm } = await loadMockNpm(t) + await t.rejects( + npm.exec('cache', ['npx', 'info']), + { code: 'EUSAGE' }, + 'should throw usage error when no keys are provided' + ) +}) + +t.test('cache npx info: valid entry with _npx packages', async t => { + const { npm, joinedOutput } = await loadMockNpm(t) + const npxCacheDir = path.join(npm.flatOptions.npxCache || path.join(npm.cache, '..', '_npx')) + fs.mkdirSync(npxCacheDir, { recursive: true }) + + const validHash = 'valid123' + createNpxCacheEntry(npxCacheDir, validHash, { + name: 'valid-package', + version: '1.0.0', + _npx: { packages: ['valid-package@1.0.0'] }, + }, { + name: 'valid-package', + version: '1.0.0', + dependencies: { + 'valid-package': { + version: '1.0.0', + resolved: 'https://registry.npmjs.org/valid-package/-/valid-package-1.0.0.tgz', + integrity: 'sha512-...', + }, + }, + }) + + const nodeModulesDir = path.join(npxCacheDir, validHash, 'node_modules') + fs.mkdirSync(nodeModulesDir, { recursive: true }) + fs.mkdirSync(path.join(nodeModulesDir, 'valid-package')) + fs.writeFileSync( + path.join(nodeModulesDir, 'valid-package', 'package.json'), + JSON.stringify({ + name: 'valid-package', + version: '1.0.0', + }) + ) + + await npm.exec('cache', ['npx', 'info', validHash]) + t.matchSnapshot(joinedOutput(), 'shows valid package info with _npx packages') +}) + +t.test('cache npx info: valid entry with dependencies', async t => { + const { npm, joinedOutput } = await loadMockNpm(t) + const npxCacheDir = path.join(npm.flatOptions.npxCache || path.join(npm.cache, '..', '_npx')) + fs.mkdirSync(npxCacheDir, { recursive: true }) + + const validHash = 'valid456' + createNpxCacheEntry(npxCacheDir, validHash, { + name: 'valid-package', + version: '1.0.0', + dependencies: { + 'dep-package': '1.0.0', + }, + }, { + name: 'valid-package', + version: '1.0.0', + dependencies: { + 'dep-package': { + version: '1.0.0', + resolved: 'https://registry.npmjs.org/dep-package/-/dep-package-1.0.0.tgz', + integrity: 'sha512-...', + }, + }, + }) + + const nodeModulesDir = path.join(npxCacheDir, validHash, 'node_modules') + fs.mkdirSync(nodeModulesDir, { recursive: true }) + fs.mkdirSync(path.join(nodeModulesDir, 'dep-package')) + fs.writeFileSync( + path.join(nodeModulesDir, 'dep-package', 'package.json'), + JSON.stringify({ + name: 'dep-package', + version: '1.0.0', + }) + ) + + await npm.exec('cache', ['npx', 'info', validHash]) + t.matchSnapshot(joinedOutput(), 'shows valid package info with dependencies') +}) + +t.test('cache npx info: valid entry with _npx directory package', async t => { + const { npm, joinedOutput } = await loadMockNpm(t) + const npxCacheDir = path.join(npm.flatOptions.npxCache || path.join(npm.cache, '..', '_npx')) + fs.mkdirSync(npxCacheDir, { recursive: true }) + + const validHash = 'valid123' + createNpxCacheEntry(npxCacheDir, validHash, { + name: 'valid-package', + version: '1.0.0', + _npx: { packages: ['/path/to/valid-package'] }, + }, { + name: 'valid-package', + version: '1.0.0', + dependencies: { + 'valid-package': { + version: '1.0.0', + resolved: 'https://registry.npmjs.org/valid-package/-/valid-package-1.0.0.tgz', + integrity: 'sha512-...', + }, + }, + }) + + const nodeModulesDir = path.join(npxCacheDir, validHash, 'node_modules') + fs.mkdirSync(nodeModulesDir, { recursive: true }) + fs.mkdirSync(path.join(nodeModulesDir, 'valid-package')) + fs.writeFileSync( + path.join(nodeModulesDir, 'valid-package', 'package.json'), + JSON.stringify({ + name: 'valid-package', + version: '1.0.0', + }) + ) + + await npm.exec('cache', ['npx', 'info', validHash]) + t.matchSnapshot(joinedOutput(), 'shows valid package info with _npx directory package') +}) + +t.test('cache npx info: valid entry with a link dependency', async t => { + const { npm, joinedOutput } = await loadMockNpm(t) + const npxCacheDir = path.join( + npm.flatOptions.npxCache || path.join(npm.cache, '..', '_npx') + ) + fs.mkdirSync(npxCacheDir, { recursive: true }) + + const validHash = 'link123' + const pkgDir = path.join(npxCacheDir, validHash) + fs.mkdirSync(pkgDir) + + fs.writeFileSync( + path.join(pkgDir, 'package.json'), + JSON.stringify({ + name: 'link-package', + version: '1.0.0', + dependencies: { + 'linked-dep': 'file:./some-other-loc', + }, + }) + ) + + fs.writeFileSync( + path.join(pkgDir, 'npm-shrinkwrap.json'), + JSON.stringify({ + name: 'link-package', + version: '1.0.0', + dependencies: { + 'linked-dep': { + version: 'file:../some-other-loc', + }, + }, + }) + ) + + const nodeModulesDir = path.join(pkgDir, 'node_modules') + fs.mkdirSync(nodeModulesDir, { recursive: true }) + + const linkTarget = path.join(pkgDir, 'some-other-loc') + fs.mkdirSync(linkTarget) + fs.writeFileSync( + path.join(linkTarget, 'package.json'), + JSON.stringify({ name: 'linked-dep', version: '1.0.0' }) + ) + + fs.symlinkSync('../some-other-loc', path.join(nodeModulesDir, 'linked-dep')) + await npm.exec('cache', ['npx', 'info', validHash]) + + t.matchSnapshot( + joinedOutput(), + 'shows link dependency realpath (child.isLink branch)' + ) +})