Skip to content

Commit 2340e23

Browse files
committed
fix: tests, strict nock, add packument calls
1 parent eae9974 commit 2340e23

File tree

4 files changed

+288
-527
lines changed

4 files changed

+288
-527
lines changed

lib/commands/dist-tag.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class DistTag extends BaseCommand {
102102
throw new Error('Tag name must not be a valid SemVer range: ' + t)
103103
}
104104

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

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

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

193-
static async fetchTags (spec, opts) {
193+
async fetchTags (spec, opts) {
194194
const data = await npmFetch.json(
195195
`/-/package/${spec.escapedName}/dist-tags`,
196196
{ ...opts, 'prefer-online': true, spec }

lib/commands/publish.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,9 @@ class Publish extends BaseCommand {
130130
}
131131

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

134-
const latestVersion = await this.#latestPublishedVersion(resolved)
135+
const latestVersion = await this.#latestPublishedVersion(resolved, registry)
135136
const latestTagIsGreater = !!latestVersion && semver.gte(latestVersion, manifest.version)
136137

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

144-
const registry = npmFetch.pickRegistry(resolved, opts)
145145
const creds = this.npm.config.getCredentialsByURI(registry)
146146
const noCreds = !(creds.token || creds.username || creds.certfile && creds.keyfile)
147147
const outputRegistry = replaceInfo(registry)
@@ -203,12 +203,12 @@ class Publish extends BaseCommand {
203203
}
204204
}
205205

206-
async #latestPublishedVersion (spec) {
206+
async #latestPublishedVersion (spec, registry) {
207207
try {
208-
const uri = '/' + spec.escapedName
209-
const packument = await npmFetch.json(uri, {
208+
const packument = await pacote.packument(spec, {
210209
...this.npm.flatOptions,
211-
spec,
210+
preferOnline: true,
211+
registry,
212212
})
213213
if (typeof packument?.versions === 'undefined') {
214214
return null

test/fixtures/mock-npm.js

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -293,12 +293,26 @@ const setupMockNpm = async (t, {
293293

294294
const loadNpmWithRegistry = async (t, opts) => {
295295
const mock = await setupMockNpm(t, opts)
296+
return {
297+
...mock,
298+
...loadRegistry(t, mock, opts),
299+
...loadFsAssertions(t, mock),
300+
}
301+
}
302+
303+
const loadRegistry = (t, mock, opts) => {
296304
const registry = new MockRegistry({
297305
tap: t,
298-
registry: mock.npm.config.get('registry'),
299-
strict: true,
306+
registry: opts.registry ?? mock.npm.config.get('registry'),
307+
authorization: opts.authorization,
308+
basic: opts.basic,
309+
debug: opts.debugRegistry ?? false,
310+
strict: opts.strictRegistryNock ?? true,
300311
})
312+
return { registry }
313+
}
301314

315+
const loadFsAssertions = (t, mock) => {
302316
const fileShouldExist = (filePath) => {
303317
t.equal(
304318
fsSync.existsSync(path.join(mock.npm.prefix, filePath)), true, `${filePath} should exist`
@@ -353,7 +367,7 @@ const loadNpmWithRegistry = async (t, opts) => {
353367
packageDirty,
354368
}
355369

356-
return { registry, assert, ...mock }
370+
return { assert }
357371
}
358372

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

481-
const putPackagePayload = ({ pkg, alternateRegistry, version }) => ({
482-
_id: pkg,
483-
name: pkg,
484-
'dist-tags': { latest: version },
485-
access: null,
486-
versions: {
487-
[version]: {
488-
name: pkg,
489-
version: version,
490-
_id: `${pkg}@${version}`,
491-
dist: {
492-
shasum: /\.*/,
493-
tarball: `http:${alternateRegistry.slice(6)}/test-package/-/test-package-${version}.tgz`,
494-
},
495-
publishConfig: {
496-
registry: alternateRegistry,
495+
const putPackagePayload = (opts) => {
496+
const package = opts.packageJson
497+
const name = opts.name || package?.name
498+
const registry = opts.registry || package?.publishConfig?.registry || 'https://registry.npmjs.org'
499+
const access = opts.access || null
500+
501+
const nameProperties = !name ? {} : {
502+
_id: name,
503+
name: name,
504+
}
505+
506+
const packageProperties = !package ? {} : {
507+
'dist-tags': { latest: package.version },
508+
versions: {
509+
[package.version]: {
510+
_id: `${package.name}@${package.version}`,
511+
dist: {
512+
shasum: /\.*/,
513+
tarball:
514+
`http://${new URL(registry).host}/${package.name}/-/${package.name}-${package.version}.tgz`,
515+
},
516+
...package,
497517
},
498518
},
499-
},
500-
_attachments: {
501-
[`${pkg}-${version}.tgz`]: {},
502-
},
503-
})
519+
_attachments: {
520+
[`${package.name}-${package.version}.tgz`]: {},
521+
},
522+
}
523+
524+
return {
525+
access,
526+
...nameProperties,
527+
...packageProperties,
528+
}
529+
}
504530

505531
module.exports = setupMockNpm
506532
module.exports.load = setupMockNpm

0 commit comments

Comments
 (0)