Skip to content

Commit

Permalink
added tests for new npx functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
owlstronaut committed Feb 11, 2025
1 parent 32ba17f commit ed9cd93
Show file tree
Hide file tree
Showing 3 changed files with 490 additions and 5 deletions.
15 changes: 10 additions & 5 deletions lib/commands/cache.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -348,7 +353,7 @@ class Cache extends BaseCommand {
}
}
}
} catch {
} catch (ex) {
valid = false
}
const v = valid ? chalk.green('valid') : chalk.red('invalid')
Expand Down
76 changes: 76 additions & 0 deletions tap-snapshots/test/lib/commands/cache.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
- [email protected] ([email protected])
`

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:
- [email protected]
`

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: [email protected]
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
`
Expand Down
Loading

0 comments on commit ed9cd93

Please sign in to comment.