Skip to content

Commit b58a0bc

Browse files
committed
chore(libnpmexec): refactor tests with mock registry
1 parent e2a73dc commit b58a0bc

File tree

17 files changed

+1229
-1814
lines changed

17 files changed

+1229
-1814
lines changed

DEPENDENCIES.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ graph LR;
2929
libnpmdiff-->npmcli-template-oss["@npmcli/template-oss"];
3030
libnpmdiff-->pacote;
3131
libnpmexec-->bin-links;
32-
libnpmexec-->minify-registry-metadata;
3332
libnpmexec-->npm-package-arg;
3433
libnpmexec-->npmcli-arborist["@npmcli/arborist"];
3534
libnpmexec-->npmcli-ci-detect["@npmcli/ci-detect"];
3635
libnpmexec-->npmcli-eslint-config["@npmcli/eslint-config"];
36+
libnpmexec-->npmcli-mock-registry["@npmcli/mock-registry"];
3737
libnpmexec-->npmcli-run-script["@npmcli/run-script"];
3838
libnpmexec-->npmcli-template-oss["@npmcli/template-oss"];
3939
libnpmexec-->npmlog;
@@ -353,12 +353,13 @@ graph LR;
353353
libnpmdiff-->tar;
354354
libnpmexec-->bin-links;
355355
libnpmexec-->chalk;
356-
libnpmexec-->minify-registry-metadata;
357-
libnpmexec-->mkdirp;
356+
libnpmexec-->just-extend;
357+
libnpmexec-->just-safe-set;
358358
libnpmexec-->npm-package-arg;
359359
libnpmexec-->npmcli-arborist["@npmcli/arborist"];
360360
libnpmexec-->npmcli-ci-detect["@npmcli/ci-detect"];
361361
libnpmexec-->npmcli-eslint-config["@npmcli/eslint-config"];
362+
libnpmexec-->npmcli-mock-registry["@npmcli/mock-registry"];
362363
libnpmexec-->npmcli-run-script["@npmcli/run-script"];
363364
libnpmexec-->npmcli-template-oss["@npmcli/template-oss"];
364365
libnpmexec-->npmlog;
@@ -774,8 +775,8 @@ Each group depends on packages lower down the chain, nothing depends on
774775
packages higher up the chain.
775776

776777
- npm
777-
- @npmcli/smoke-tests, libnpmpublish
778-
- @npmcli/mock-registry, libnpmdiff, libnpmexec, libnpmfund, libnpmpack
778+
- @npmcli/smoke-tests, libnpmexec, libnpmpublish
779+
- @npmcli/mock-registry, libnpmdiff, libnpmfund, libnpmpack
779780
- @npmcli/arborist
780781
- @npmcli/metavuln-calculator
781782
- pacote, libnpmaccess, libnpmhook, libnpmorg, libnpmsearch, libnpmteam, npm-profile

mock-registry/lib/index.js

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class MockRegistry {
1010
#authorization
1111
#basic
1212
#debug
13+
#strict
1314

1415
constructor (opts) {
1516
if (!opts.registry) {
@@ -19,20 +20,33 @@ class MockRegistry {
1920
this.#authorization = opts.authorization
2021
this.#basic = opts.basic
2122
this.#debug = opts.debug
23+
this.#strict = opts.strict
2224
// Required for this.package
2325
this.#tap = opts.tap
26+
if (this.#tap) {
27+
this.startNock()
28+
}
2429
}
2530

26-
static tnock (t, host, opts, { debug = false } = {}) {
27-
if (debug) {
28-
Nock.emitter.on('no match', req => console.error('NO MATCH', req.options))
31+
static tnock (t, host, opts, { debug = false, strict } = {}) {
32+
const noMatch = (req) => {
33+
if (strict) {
34+
t.fail(`Unmatched request: ${JSON.stringify(req.options, null, 2)}`)
35+
}
36+
if (debug) {
37+
console.error('NO MATCH', t.name, req.options)
38+
}
2939
}
40+
41+
Nock.emitter.on('no match', noMatch)
3042
Nock.disableNetConnect()
3143
const server = Nock(host, opts)
3244
t.teardown(() => {
3345
Nock.enableNetConnect()
3446
server.done()
47+
Nock.emitter.off('no match', noMatch)
3548
})
49+
3650
return server
3751
}
3852

@@ -41,31 +55,38 @@ class MockRegistry {
4155
}
4256

4357
get nock () {
44-
if (!this.#nock) {
45-
if (!this.#tap) {
46-
throw new Error('cannot mock packages without a tap fixture')
47-
}
48-
const reqheaders = {}
49-
if (this.#authorization) {
50-
reqheaders.authorization = `Bearer ${this.#authorization}`
51-
}
52-
if (this.#basic) {
53-
reqheaders.authorization = `Basic ${this.#basic}`
54-
}
55-
this.#nock = MockRegistry.tnock(
56-
this.#tap,
57-
this.#registry,
58-
{ reqheaders },
59-
{ debug: this.#debug }
60-
)
61-
}
6258
return this.#nock
6359
}
6460

6561
set nock (nock) {
6662
this.#nock = nock
6763
}
6864

65+
startNock () {
66+
if (this.nock) {
67+
return
68+
}
69+
70+
if (!this.#tap) {
71+
throw new Error('cannot mock packages without a tap fixture')
72+
}
73+
74+
const reqheaders = {}
75+
if (this.#authorization) {
76+
reqheaders.authorization = `Bearer ${this.#authorization}`
77+
}
78+
if (this.#basic) {
79+
reqheaders.authorization = `Basic ${this.#basic}`
80+
}
81+
82+
this.nock = MockRegistry.tnock(
83+
this.#tap,
84+
this.#registry,
85+
{ reqheaders },
86+
{ debug: this.#debug, strict: this.#strict }
87+
)
88+
}
89+
6990
search ({ responseCode = 200, results = [], error }) {
7091
// the flags, score, and searchScore parts of the response are never used
7192
// by npm, only package is used
@@ -296,13 +317,14 @@ class MockRegistry {
296317
manifest.users = users
297318
}
298319
for (const packument of packuments) {
320+
const unscoped = name.includes('/') ? name.split('/')[1] : name
299321
manifest.versions[packument.version] = {
300322
_id: `${name}@${packument.version}`,
301323
name,
302324
description: 'test package mock manifest',
303325
dependencies: {},
304326
dist: {
305-
tarball: `${this.#registry}/${name}/-/${name}-${packument.version}.tgz`,
327+
tarball: `${this.#registry}/${name}/-/${unscoped}-${packument.version}.tgz`,
306328
},
307329
maintainers: [],
308330
...packument,

package-lock.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15116,10 +15116,11 @@
1511615116
},
1511715117
"devDependencies": {
1511815118
"@npmcli/eslint-config": "^4.0.0",
15119+
"@npmcli/mock-registry": "^1.0.0",
1511915120
"@npmcli/template-oss": "4.9.0",
1512015121
"bin-links": "^4.0.1",
15121-
"minify-registry-metadata": "^2.2.0",
15122-
"mkdirp": "^1.0.4",
15122+
"just-extend": "^6.1.1",
15123+
"just-safe-set": "^4.1.1",
1512315124
"tap": "^16.0.1"
1512415125
},
1512515126
"engines": {

workspaces/libnpmexec/lib/index.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,23 @@ const exec = async (opts) => {
9595

9696
let yes = opts.yes
9797
const runCall = call
98-
const run = () => runScript({
99-
args,
100-
call: runCall,
101-
flatOptions,
102-
locationMsg,
103-
output,
104-
path,
105-
binPaths,
106-
runPath,
107-
scriptShell,
108-
})
98+
const run = () => {
99+
const runOpts = {
100+
args,
101+
call: runCall,
102+
locationMsg,
103+
output,
104+
path,
105+
binPaths,
106+
runPath,
107+
scriptShell,
108+
}
109+
log.silly('libnpmexec', 'run', runOpts)
110+
return runScript({
111+
...runOpts,
112+
flatOptions,
113+
})
114+
}
109115

110116
// interactive mode
111117
if (!call && !args.length && !packages.length) {
@@ -200,7 +206,9 @@ const exec = async (opts) => {
200206
const globalArb = new Arborist({ ...flatOptions, path: globalPath, global: true })
201207
const globalTree = await globalArb.loadActual()
202208
const { manifest: globalManifest } =
203-
await missingFromTree({ spec, tree: globalTree, flatOptions })
209+
await missingFromTree({ spec, tree: globalTree, flatOptions })
210+
console.error({ g: !globalManifest }, await fileExists(`${globalBin}/${args[0]}`))
211+
204212
if (!globalManifest && await fileExists(`${globalBin}/${args[0]}`)) {
205213
binPaths.push(globalBin)
206214
return await run()

workspaces/libnpmexec/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,19 @@
4343
},
4444
"tap": {
4545
"color": true,
46-
"files": "test/*.js",
46+
"test-ignore": "fixtures/*",
4747
"nyc-arg": [
4848
"--exclude",
4949
"tap-snapshots/**"
5050
]
5151
},
5252
"devDependencies": {
5353
"@npmcli/eslint-config": "^4.0.0",
54+
"@npmcli/mock-registry": "^1.0.0",
5455
"@npmcli/template-oss": "4.9.0",
5556
"bin-links": "^4.0.1",
56-
"minify-registry-metadata": "^2.2.0",
57-
"mkdirp": "^1.0.4",
57+
"just-extend": "^6.1.1",
58+
"just-safe-set": "^4.1.1",
5859
"tap": "^16.0.1"
5960
},
6061
"dependencies": {

0 commit comments

Comments
 (0)