Skip to content

Commit 58675fc

Browse files
committed
chore: update test to use a real npx inventory setup
1 parent 3c955fb commit 58675fc

File tree

2 files changed

+26
-40
lines changed

2 files changed

+26
-40
lines changed

workspaces/libnpmexec/test/fixtures/setup.js

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const binLinks = require('bin-links')
66
const MockRegistry = require('@npmcli/mock-registry')
77
const justExtend = require('just-extend')
88
const set = require('just-safe-set')
9+
const crypto = require('node:crypto')
910

1011
const merge = (...args) => justExtend(true, ...args)
1112

@@ -118,35 +119,19 @@ const createTestdir = (...objs) => {
118119
return testdirHelper(merge(...objs))
119120
}
120121

121-
const mockNpxInventoryCache = (packageName, fakeVersion) => {
122-
const Arborist = require('@npmcli/arborist')
123-
const originalLoadActual = Arborist.prototype.loadActual
124-
125-
Arborist.prototype.loadActual = async function () {
126-
const tree = await originalLoadActual.call(this)
127-
if (this.path.includes('npxCache')) {
128-
const originalQuery = tree.inventory.query.bind(tree.inventory)
129-
tree.inventory.query = (key, name) => {
130-
if (key === 'packageName' && name === packageName) {
131-
return [{
132-
package: {
133-
version: fakeVersion,
134-
name: packageName,
135-
resolved: 'dummy-resolved',
136-
},
137-
pkgid: `${packageName}@${fakeVersion}`,
138-
depth: 0,
139-
}]
140-
}
141-
return originalQuery(key, name)
142-
}
143-
}
144-
return tree
145-
}
146-
147-
return () => {
148-
Arborist.prototype.loadActual = originalLoadActual
122+
const setupNpxInventory = async (basePath, name, version, packageSpec) => {
123+
const npxCachePath = resolve(basePath, 'npxCache')
124+
const hash = crypto.createHash('sha512')
125+
.update(packageSpec)
126+
.digest('hex')
127+
.slice(0, 16)
128+
const installDir = resolve(npxCachePath, hash)
129+
await fs.mkdir(installDir, { recursive: true })
130+
const cachedPkg = {
131+
name,
132+
version,
149133
}
134+
await fs.writeFile(resolve(installDir, 'package.json'), JSON.stringify(cachedPkg, null, 2))
150135
}
151136

152137
const setup = (t, {
@@ -285,4 +270,4 @@ const setup = (t, {
285270
module.exports.setup = setup
286271
module.exports.createPkg = createPkg
287272
module.exports.merge = merge
288-
module.exports.mockNpxInventoryCache = mockNpxInventoryCache
273+
module.exports.setupNpxInventory = setupNpxInventory

workspaces/libnpmexec/test/registry.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const { resolve } = require('node:path')
22
const t = require('tap')
3-
const { setup, createPkg, merge, mockNpxInventoryCache } = require('./fixtures/setup.js')
3+
const { setup, createPkg, merge, setupNpxInventory } = require('./fixtures/setup.js')
44

55
t.test('run from registry - no local packages', async t => {
66
const { fixtures, package } = createPkg({ versions: ['2.0.0'] })
@@ -246,16 +246,11 @@ t.test('run from registry - non existant global path', async t => {
246246
})
247247
})
248248

249-
t.test('npx tree triggers manifest fetch when local version does satisfy range', async t => {
250-
// Outdated npx inventory version meets semver range
251-
const restore = mockNpxInventoryCache('@npmcli/create-index', '2.0.0')
252-
t.teardown(() => {
253-
restore()
254-
})
255-
249+
t.test('npx tree triggers manifest fetch when local version does satisfy range using real npx cache inventory', async t => {
250+
// The local installation is version 1.0.0, which does NOT satisfy the spec ^2.0.0.
256251
const pkgData = createPkg({
257-
localVersion: '1.0.1',
258-
versions: ['1.0.1', '2.0.0', '2.0.1'],
252+
localVersion: '1.0.0',
253+
versions: ['1.0.0', '2.0.0', '2.0.1'],
259254
name: '@npmcli/create-index',
260255
})
261256
const { fixtures, package: pkg } = pkgData
@@ -264,14 +259,20 @@ t.test('npx tree triggers manifest fetch when local version does satisfy range',
264259
testdir: { ...fixtures },
265260
})
266261

262+
// We need an npx inventory with a node that would match semver, but is disregarded because we always update the npx tree
263+
await setupNpxInventory(path, '@npmcli/create-index', '2.0.0', '@npmcli/create-index@^2.0.0')
264+
265+
// Set up the registry package so that a manifest fetch returns version 2.0.1.
267266
await pkg({
268267
registry,
269268
path,
270269
tarballs: ['2.0.1'],
271270
})
272271
await binLinks()
273272

274-
// npx is true, but semver range is also satisfied, it still triggers a manifest fetch
273+
// Execute in npx mode with the spec ^2.0.0.
274+
// The local tree returns a manifest because its version (1.0.0) does not satisfy ^2.0.0.
275+
// Then the npx tree returns a manifest because its version (2.0.0) is not the most up-to-date version (2.0.1).
275276
await execFn({
276277
args: ['create-index'],
277278
packages: ['@npmcli/create-index@^2.0.0'],

0 commit comments

Comments
 (0)