Skip to content

Commit

Permalink
fix: tests, strict nock, add packument calls
Browse files Browse the repository at this point in the history
  • Loading branch information
reggi committed Dec 4, 2024
1 parent f77f255 commit 5c25815
Show file tree
Hide file tree
Showing 4 changed files with 289 additions and 527 deletions.
8 changes: 4 additions & 4 deletions lib/commands/dist-tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class DistTag extends BaseCommand {
throw new Error('Tag name must not be a valid SemVer range: ' + t)
}

const tags = await DistTag.fetchTags(spec, opts)
const tags = await this.fetchTags(spec, opts)
if (tags[t] === version) {
log.warn('dist-tag add', t, 'is already set to version', version)
return
Expand Down Expand Up @@ -131,7 +131,7 @@ class DistTag extends BaseCommand {
throw this.usageError()
}

const tags = await DistTag.fetchTags(spec, opts)
const tags = await this.fetchTags(spec, opts)
if (!tags[tag]) {
log.info('dist-tag del', tag, 'is not a dist-tag on', spec.name)
throw new Error(tag + ' is not a dist-tag on ' + spec.name)
Expand Down Expand Up @@ -164,7 +164,7 @@ class DistTag extends BaseCommand {
spec = npa(spec)

try {
const tags = await DistTag.fetchTags(spec, opts)
const tags = await this.fetchTags(spec, opts)
const msg =
Object.keys(tags).map(k => `${k}: ${tags[k]}`).sort().join('\n')
output.standard(msg)
Expand All @@ -190,7 +190,7 @@ class DistTag extends BaseCommand {
}
}

static async fetchTags (spec, opts) {
async fetchTags (spec, opts) {
const data = await npmFetch.json(
`/-/package/${spec.escapedName}/dist-tags`,
{ ...opts, 'prefer-online': true, spec }
Expand Down
12 changes: 6 additions & 6 deletions lib/commands/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,9 @@ class Publish extends BaseCommand {
}

const resolved = npa.resolve(manifest.name, manifest.version)
const registry = npmFetch.pickRegistry(resolved, opts)

const latestVersion = await this.#latestPublishedVersion(resolved)
const latestVersion = await this.#latestPublishedVersion(resolved, registry)
const latestTagIsGreater = !!latestVersion && semver.gte(latestVersion, manifest.version)

if (latestTagIsGreater && isDefaultTag) {
Expand All @@ -141,7 +142,6 @@ class Publish extends BaseCommand {
// make sure tag is valid, this will throw if invalid
npa(`${manifest.name}@${defaultTag}`)

const registry = npmFetch.pickRegistry(resolved, opts)
const creds = this.npm.config.getCredentialsByURI(registry)
const noCreds = !(creds.token || creds.username || creds.certfile && creds.keyfile)
const outputRegistry = replaceInfo(registry)
Expand Down Expand Up @@ -203,12 +203,12 @@ class Publish extends BaseCommand {
}
}

async #latestPublishedVersion (spec) {
async #latestPublishedVersion (spec, registry) {
try {
const uri = '/' + spec.escapedName
const packument = await npmFetch.json(uri, {
const packument = await pacote.packument(spec, {
...this.npm.flatOptions,
spec,
preferOnline: true,
registry,
})
if (typeof packument?.versions === 'undefined') {
return null
Expand Down
74 changes: 50 additions & 24 deletions test/fixtures/mock-npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,26 @@ const setupMockNpm = async (t, {

const loadNpmWithRegistry = async (t, opts) => {
const mock = await setupMockNpm(t, opts)
return {
...mock,
...loadRegistry(t, mock, opts),
...loadFsAssertions(t, mock),
}
}

const loadRegistry = (t, mock, opts) => {
const registry = new MockRegistry({
tap: t,
registry: mock.npm.config.get('registry'),
strict: true,
registry: opts.registry ?? mock.npm.config.get('registry'),
authorization: opts.authorization,
basic: opts.basic,
debug: opts.debugRegistry ?? false,
strict: opts.strictRegistryNock ?? true,
})
return { registry }
}

const loadFsAssertions = (t, mock) => {
const fileShouldExist = (filePath) => {
t.equal(
fsSync.existsSync(path.join(mock.npm.prefix, filePath)), true, `${filePath} should exist`
Expand Down Expand Up @@ -353,7 +367,7 @@ const loadNpmWithRegistry = async (t, opts) => {
packageDirty,
}

return { registry, assert, ...mock }
return { assert }
}

/** breaks down a spec "[email protected]" into different parts for mocking */
Expand Down Expand Up @@ -478,29 +492,41 @@ const mockNpmRegistryFetch = (tags) => {
return { mocks, mock, fetchOpts, getOpts }
}

const putPackagePayload = ({ pkg, alternateRegistry, version }) => ({
_id: pkg,
name: pkg,
'dist-tags': { latest: version },
access: null,
versions: {
[version]: {
name: pkg,
version: version,
_id: `${pkg}@${version}`,
dist: {
shasum: /\.*/,
tarball: `http:${alternateRegistry.slice(6)}/test-package/-/test-package-${version}.tgz`,
},
publishConfig: {
registry: alternateRegistry,
const putPackagePayload = (opts) => {
const package = opts.packageJson
const name = opts.name || package?.name
const registry = opts.registry || package?.publishConfig?.registry || 'https://registry.npmjs.org'
const access = opts.access || null

const nameProperties = !name ? {} : {
_id: name,
name: name,
}

const packageProperties = !package ? {} : {
'dist-tags': { latest: package.version },
versions: {
[package.version]: {
_id: `${package.name}@${package.version}`,
dist: {
shasum: /\.*/,
tarball:
`http://${new URL(registry).host}/${package.name}/-/${package.name}-${package.version}.tgz`,
},
...package,
},
},
},
_attachments: {
[`${pkg}-${version}.tgz`]: {},
},
})
_attachments: {
[`${package.name}-${package.version}.tgz`]: {},
},
}

return {
access,
...nameProperties,
...packageProperties,
}
}

module.exports = setupMockNpm
module.exports.load = setupMockNpm
Expand Down
Loading

0 comments on commit 5c25815

Please sign in to comment.