Skip to content

Commit

Permalink
chore(libnpmexec): refactor tests with mock registry
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys committed Nov 12, 2022
1 parent cc00efe commit a7d4af4
Show file tree
Hide file tree
Showing 16 changed files with 1,211 additions and 1,802 deletions.
11 changes: 6 additions & 5 deletions DEPENDENCIES.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ graph LR;
libnpmdiff-->npmcli-template-oss["@npmcli/template-oss"];
libnpmdiff-->pacote;
libnpmexec-->bin-links;
libnpmexec-->minify-registry-metadata;
libnpmexec-->npm-package-arg;
libnpmexec-->npmcli-arborist["@npmcli/arborist"];
libnpmexec-->npmcli-ci-detect["@npmcli/ci-detect"];
libnpmexec-->npmcli-eslint-config["@npmcli/eslint-config"];
libnpmexec-->npmcli-mock-registry["@npmcli/mock-registry"];
libnpmexec-->npmcli-run-script["@npmcli/run-script"];
libnpmexec-->npmcli-template-oss["@npmcli/template-oss"];
libnpmexec-->npmlog;
Expand Down Expand Up @@ -353,12 +353,13 @@ graph LR;
libnpmdiff-->tar;
libnpmexec-->bin-links;
libnpmexec-->chalk;
libnpmexec-->minify-registry-metadata;
libnpmexec-->mkdirp;
libnpmexec-->just-extend;
libnpmexec-->just-safe-set;
libnpmexec-->npm-package-arg;
libnpmexec-->npmcli-arborist["@npmcli/arborist"];
libnpmexec-->npmcli-ci-detect["@npmcli/ci-detect"];
libnpmexec-->npmcli-eslint-config["@npmcli/eslint-config"];
libnpmexec-->npmcli-mock-registry["@npmcli/mock-registry"];
libnpmexec-->npmcli-run-script["@npmcli/run-script"];
libnpmexec-->npmcli-template-oss["@npmcli/template-oss"];
libnpmexec-->npmlog;
Expand Down Expand Up @@ -774,8 +775,8 @@ Each group depends on packages lower down the chain, nothing depends on
packages higher up the chain.

- npm
- @npmcli/smoke-tests, libnpmpublish
- @npmcli/mock-registry, libnpmdiff, libnpmexec, libnpmfund, libnpmpack
- @npmcli/smoke-tests, libnpmexec, libnpmpublish
- @npmcli/mock-registry, libnpmdiff, libnpmfund, libnpmpack
- @npmcli/arborist
- @npmcli/metavuln-calculator
- pacote, libnpmaccess, libnpmhook, libnpmorg, libnpmsearch, libnpmteam, npm-profile
Expand Down
66 changes: 44 additions & 22 deletions mock-registry/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class MockRegistry {
#authorization
#basic
#debug
#strict

constructor (opts) {
if (!opts.registry) {
Expand All @@ -19,20 +20,33 @@ class MockRegistry {
this.#authorization = opts.authorization
this.#basic = opts.basic
this.#debug = opts.debug
this.#strict = opts.strict
// Required for this.package
this.#tap = opts.tap
if (this.#tap) {
this.startNock()
}
}

static tnock (t, host, opts, { debug = false } = {}) {
if (debug) {
Nock.emitter.on('no match', req => console.error('NO MATCH', req.options))
static tnock (t, host, opts, { debug = false, strict } = {}) {
const noMatch = (req) => {
if (strict) {
t.fail(`Unmatched request: ${JSON.stringify(req.options, null, 2)}`)
}
if (debug) {
console.error('NO MATCH', t.name, req.options)
}
}

Nock.emitter.on('no match', noMatch)
Nock.disableNetConnect()
const server = Nock(host, opts)
t.teardown(() => {
Nock.enableNetConnect()
server.done()
Nock.emitter.off('no match', noMatch)
})

return server
}

Expand All @@ -41,31 +55,38 @@ class MockRegistry {
}

get nock () {
if (!this.#nock) {
if (!this.#tap) {
throw new Error('cannot mock packages without a tap fixture')
}
const reqheaders = {}
if (this.#authorization) {
reqheaders.authorization = `Bearer ${this.#authorization}`
}
if (this.#basic) {
reqheaders.authorization = `Basic ${this.#basic}`
}
this.#nock = MockRegistry.tnock(
this.#tap,
this.#registry,
{ reqheaders },
{ debug: this.#debug }
)
}
return this.#nock
}

set nock (nock) {
this.#nock = nock
}

startNock () {
if (this.nock) {
return
}

if (!this.#tap) {
throw new Error('cannot mock packages without a tap fixture')
}

const reqheaders = {}
if (this.#authorization) {
reqheaders.authorization = `Bearer ${this.#authorization}`
}
if (this.#basic) {
reqheaders.authorization = `Basic ${this.#basic}`
}

this.nock = MockRegistry.tnock(
this.#tap,
this.#registry,
{ reqheaders },
{ debug: this.#debug, strict: this.#strict }
)
}

search ({ responseCode = 200, results = [], error }) {
// the flags, score, and searchScore parts of the response are never used
// by npm, only package is used
Expand Down Expand Up @@ -296,13 +317,14 @@ class MockRegistry {
manifest.users = users
}
for (const packument of packuments) {
const unscoped = name.includes('/') ? name.split('/')[1] : name
manifest.versions[packument.version] = {
_id: `${name}@${packument.version}`,
name,
description: 'test package mock manifest',
dependencies: {},
dist: {
tarball: `${this.#registry}/${name}/-/${name}-${packument.version}.tgz`,
tarball: `${this.#registry}/${name}/-/${unscoped}-${packument.version}.tgz`,
},
maintainers: [],
...packument,
Expand Down
6 changes: 4 additions & 2 deletions package-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -15117,9 +15117,11 @@
},
"devDependencies": {
"@npmcli/eslint-config": "^4.0.0",
"@npmcli/mock-registry": "^1.0.0",
"@npmcli/template-oss": "4.9.0",
"minify-registry-metadata": "^2.2.0",
"mkdirp": "^1.0.4",
"bin-links": "^4.0.1",
"just-extend": "^6.1.1",
"just-safe-set": "^4.1.1",
"tap": "^16.0.1"
},
"engines": {
Expand Down
8 changes: 5 additions & 3 deletions workspaces/libnpmexec/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,19 @@
},
"tap": {
"color": true,
"files": "test/*.js",
"test-ignore": "fixtures/*",
"nyc-arg": [
"--exclude",
"tap-snapshots/**"
]
},
"devDependencies": {
"@npmcli/eslint-config": "^4.0.0",
"@npmcli/mock-registry": "^1.0.0",
"@npmcli/template-oss": "4.9.0",
"minify-registry-metadata": "^2.2.0",
"mkdirp": "^1.0.4",
"bin-links": "^4.0.1",
"just-extend": "^6.1.1",
"just-safe-set": "^4.1.1",
"tap": "^16.0.1"
},
"dependencies": {
Expand Down

0 comments on commit a7d4af4

Please sign in to comment.