Skip to content

Commit

Permalink
chore: move mock-registry to workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys committed Nov 9, 2022
1 parent 6a15d8a commit f36df46
Show file tree
Hide file tree
Showing 24 changed files with 1,105 additions and 1,570 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = {
ignorePatterns: [
'docs/**',
'smoke-tests/**',
'mock-registry/**',
'workspaces/**',
],
extends: [
Expand Down
94 changes: 94 additions & 0 deletions .github/workflows/ci-npmcli-mock-registry.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# This file is automatically added by @npmcli/template-oss. Do not edit.

name: CI - @npmcli/mock-registry

on:
workflow_dispatch:
pull_request:
paths:
- mock-registry/**
push:
branches:
- main
- latest
paths:
- mock-registry/**
schedule:
# "At 09:00 UTC (02:00 PT) on Monday" https://crontab.guru/#0_9_*_*_1
- cron: "0 9 * * 1"

jobs:
lint:
name: Lint
if: github.repository_owner == 'npm'
runs-on: ubuntu-latest
defaults:
run:
shell: bash
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup Git User
run: |
git config --global user.email "[email protected]"
git config --global user.name "npm CLI robot"
- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: 18.x
cache: npm
- name: Reset Deps
run: node . run resetdeps
- name: Lint
run: node . run lint --ignore-scripts -w @npmcli/mock-registry
- name: Post Lint
run: node . run postlint --ignore-scripts -w @npmcli/mock-registry

test:
name: Test - ${{ matrix.platform.name }} - ${{ matrix.node-version }}
if: github.repository_owner == 'npm'
strategy:
fail-fast: false
matrix:
platform:
- name: Linux
os: ubuntu-latest
shell: bash
- name: macOS
os: macos-latest
shell: bash
- name: Windows
os: windows-latest
shell: cmd
node-version:
- 14.17.0
- 14.x
- 16.13.0
- 16.x
- 18.0.0
- 18.x
runs-on: ${{ matrix.platform.os }}
defaults:
run:
shell: ${{ matrix.platform.shell }}
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup Git User
run: |
git config --global user.email "[email protected]"
git config --global user.name "npm CLI robot"
- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: npm
- name: Reset Deps
run: node . run resetdeps
- name: Add Problem Matcher
run: echo "::add-matcher::.github/matchers/tap.json"
- name: Test
run: node . test --ignore-scripts -w @npmcli/mock-registry
- name: Check Git Status
if: matrix && matrix.platform.os != 'windows-latest'
run: node scripts/git-dirty.js
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ on:
paths-ignore:
- docs/**
- smoke-tests/**
- mock-registry/**
- workspaces/**
push:
branches:
Expand All @@ -16,6 +17,7 @@ on:
paths-ignore:
- docs/**
- smoke-tests/**
- mock-registry/**
- workspaces/**
schedule:
# "At 09:00 UTC (02:00 PT) on Monday" https://crontab.guru/#0_9_*_*_1
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
!/test/
!/docs/
!/smoke-tests/
!/mock-registry/
!/workspaces/
/workspaces/*
!/workspaces/arborist/
Expand Down
27 changes: 23 additions & 4 deletions DEPENDENCIES.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ graph LR;
npm-->npmcli-fs["@npmcli/fs"];
npm-->npmcli-git["@npmcli/git"];
npm-->npmcli-map-workspaces["@npmcli/map-workspaces"];
npm-->npmcli-mock-registry["@npmcli/mock-registry"];
npm-->npmcli-package-json["@npmcli/package-json"];
npm-->npmcli-promise-spawn["@npmcli/promise-spawn"];
npm-->npmcli-run-script["@npmcli/run-script"];
Expand Down Expand Up @@ -207,12 +208,18 @@ graph LR;
npmcli-metavuln-calculator-->json-parse-even-better-errors;
npmcli-metavuln-calculator-->pacote;
npmcli-metavuln-calculator-->semver;
npmcli-mock-registry-->npm-package-arg;
npmcli-mock-registry-->npmcli-arborist["@npmcli/arborist"];
npmcli-mock-registry-->npmcli-eslint-config["@npmcli/eslint-config"];
npmcli-mock-registry-->npmcli-template-oss["@npmcli/template-oss"];
npmcli-mock-registry-->pacote;
npmcli-package-json-->json-parse-even-better-errors;
npmcli-run-script-->npmcli-node-gyp["@npmcli/node-gyp"];
npmcli-run-script-->npmcli-promise-spawn["@npmcli/promise-spawn"];
npmcli-run-script-->read-package-json-fast;
npmcli-smoke-tests-->minify-registry-metadata;
npmcli-smoke-tests-->npmcli-eslint-config["@npmcli/eslint-config"];
npmcli-smoke-tests-->npmcli-mock-registry["@npmcli/mock-registry"];
npmcli-smoke-tests-->npmcli-promise-spawn["@npmcli/promise-spawn"];
npmcli-smoke-tests-->npmcli-template-oss["@npmcli/template-oss"];
npmlog-->are-we-there-yet;
Expand Down Expand Up @@ -520,6 +527,7 @@ graph LR;
npm-->npmcli-fs["@npmcli/fs"];
npm-->npmcli-git["@npmcli/git"];
npm-->npmcli-map-workspaces["@npmcli/map-workspaces"];
npm-->npmcli-mock-registry["@npmcli/mock-registry"];
npm-->npmcli-package-json["@npmcli/package-json"];
npm-->npmcli-promise-spawn["@npmcli/promise-spawn"];
npm-->npmcli-run-script["@npmcli/run-script"];
Expand Down Expand Up @@ -657,6 +665,13 @@ graph LR;
npmcli-metavuln-calculator-->json-parse-even-better-errors;
npmcli-metavuln-calculator-->pacote;
npmcli-metavuln-calculator-->semver;
npmcli-mock-registry-->nock;
npmcli-mock-registry-->npm-package-arg;
npmcli-mock-registry-->npmcli-arborist["@npmcli/arborist"];
npmcli-mock-registry-->npmcli-eslint-config["@npmcli/eslint-config"];
npmcli-mock-registry-->npmcli-template-oss["@npmcli/template-oss"];
npmcli-mock-registry-->pacote;
npmcli-mock-registry-->tap;
npmcli-move-file-->mkdirp;
npmcli-move-file-->rimraf;
npmcli-package-json-->json-parse-even-better-errors;
Expand All @@ -667,8 +682,12 @@ graph LR;
npmcli-run-script-->npmcli-promise-spawn["@npmcli/promise-spawn"];
npmcli-run-script-->read-package-json-fast;
npmcli-run-script-->which;
npmcli-smoke-tests-->http-proxy;
npmcli-smoke-tests-->just-extend;
npmcli-smoke-tests-->just-safe-set;
npmcli-smoke-tests-->minify-registry-metadata;
npmcli-smoke-tests-->npmcli-eslint-config["@npmcli/eslint-config"];
npmcli-smoke-tests-->npmcli-mock-registry["@npmcli/mock-registry"];
npmcli-smoke-tests-->npmcli-promise-spawn["@npmcli/promise-spawn"];
npmcli-smoke-tests-->npmcli-template-oss["@npmcli/template-oss"];
npmcli-smoke-tests-->tap;
Expand Down Expand Up @@ -757,13 +776,13 @@ Each group depends on packages lower down the chain, nothing depends on
packages higher up the chain.

- npm
- libnpmpublish
- libnpmdiff, libnpmexec, libnpmfund, libnpmpack
- @npmcli/smoke-tests, libnpmpublish
- @npmcli/mock-registry, libnpmdiff, libnpmexec, libnpmfund, libnpmpack
- @npmcli/arborist
- @npmcli/metavuln-calculator
- pacote, libnpmaccess, libnpmhook, libnpmorg, libnpmsearch, libnpmteam, npm-profile
- npm-registry-fetch, libnpmversion
- @npmcli/git, make-fetch-happen, @npmcli/config, init-package-json
- @npmcli/installed-package-contents, @npmcli/map-workspaces, cacache, npm-pick-manifest, @npmcli/run-script, read-package-json, promzard
- @npmcli/docs, @npmcli/smoke-tests, @npmcli/fs, npm-bundled, read-package-json-fast, unique-filename, npm-install-checks, npm-package-arg, npm-packlist, normalize-package-data, @npmcli/package-json, bin-links, nopt, npmlog, parse-conflict-json, read
- @npmcli/eslint-config, @npmcli/template-oss, ignore-walk, @npmcli/promise-spawn, minify-registry-metadata, semver, npm-normalize-package-bin, @npmcli/name-from-folder, json-parse-even-better-errors, fs-minipass, ssri, unique-slug, hosted-git-info, proc-log, validate-npm-package-name, @npmcli/node-gyp, minipass-fetch, @npmcli/query, cmd-shim, read-cmd-shim, write-file-atomic, abbrev, are-we-there-yet, gauge, treeverse, ini, @npmcli/disparity-colors, @npmcli/ci-detect, mute-stream, npm-audit-report, npm-user-validate
- @npmcli/docs, @npmcli/fs, npm-bundled, read-package-json-fast, unique-filename, npm-install-checks, npm-package-arg, npm-packlist, normalize-package-data, @npmcli/package-json, bin-links, nopt, npmlog, parse-conflict-json, read
- @npmcli/eslint-config, @npmcli/template-oss, ignore-walk, semver, npm-normalize-package-bin, @npmcli/name-from-folder, json-parse-even-better-errors, fs-minipass, ssri, unique-slug, @npmcli/promise-spawn, hosted-git-info, proc-log, validate-npm-package-name, @npmcli/node-gyp, minipass-fetch, @npmcli/query, cmd-shim, read-cmd-shim, write-file-atomic, abbrev, are-we-there-yet, gauge, treeverse, minify-registry-metadata, ini, @npmcli/disparity-colors, @npmcli/ci-detect, mute-stream, npm-audit-report, npm-user-validate
17 changes: 17 additions & 0 deletions mock-registry/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* This file is automatically added by @npmcli/template-oss. Do not edit. */

'use strict'

const { readdirSync: readdir } = require('fs')

const localConfigs = readdir(__dirname)
.filter((file) => file.startsWith('.eslintrc.local.'))
.map((file) => `./${file}`)

module.exports = {
root: true,
extends: [
'@npmcli',
...localConfigs,
],
}
21 changes: 21 additions & 0 deletions mock-registry/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# This file is automatically added by @npmcli/template-oss. Do not edit.

# ignore everything in the root
/*

# keep these
!**/.gitignore
!/.eslintrc.js
!/.eslintrc.local.*
!/.gitignore
!/bin/
!/CHANGELOG*
!/docs/
!/lib/
!/LICENSE*
!/map.js
!/package.json
!/README*
!/scripts/
!/tap-snapshots/
!/test/
41 changes: 28 additions & 13 deletions test/fixtures/mock-registry.js → mock-registry/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
/*
* Mock registry class
*
* This should end up as the centralized place where we generate test fixtures
* for tests against any registry data.
*/
const pacote = require('pacote')
const Arborist = require('@npmcli/arborist')
const npa = require('npm-package-arg')
const Nock = require('nock')

class MockRegistry {
#tap
#nock
#registry
#authorization
#basic
#debug

constructor (opts) {
if (!opts.registry) {
Expand All @@ -21,24 +18,46 @@ class MockRegistry {
this.#registry = (new URL(opts.registry)).origin
this.#authorization = opts.authorization
this.#basic = opts.basic
this.#debug = opts.debug
// Required for this.package
this.#tap = opts.tap
}

static tnock (t, host, opts, { debug = false } = {}) {
if (debug) {
Nock.emitter.on('no match', req => console.error('NO MATCH', req.options))
}
Nock.disableNetConnect()
const server = Nock(host, opts)
t.teardown(() => {
Nock.enableNetConnect()
server.done()
})
return server
}

get origin () {
return this.#registry
}

get nock () {
if (!this.#nock) {
if (!this.#tap) {
throw new Error('cannot mock packages without a tap fixture')
}
const tnock = require('./tnock.js')
const reqheaders = {}
if (this.#authorization) {
reqheaders.authorization = `Bearer ${this.#authorization}`
}
if (this.#basic) {
reqheaders.authorization = `Basic ${this.#basic}`
}
this.#nock = tnock(this.#tap, this.#registry, { reqheaders })
this.#nock = MockRegistry.tnock(
this.#tap,
this.#registry,
{ reqheaders },
{ debug: this.#debug }
)
}
return this.#nock
}
Expand Down Expand Up @@ -260,7 +279,7 @@ class MockRegistry {
// or an array of versions
// the last packument in the packuments or versions array will be tagged latest
manifest ({ name = 'test-package', users, packuments, versions } = {}) {
packuments = this.packuments(packuments, name)
packuments = this.packuments(versions || packuments, name)
const latest = packuments.slice(-1)[0]
const manifest = {
_id: `${name}@${latest.version}`,
Expand All @@ -276,10 +295,6 @@ class MockRegistry {
if (users) {
manifest.users = users
}
if (versions) {
packuments = versions.map(version => ({ version }))
}

for (const packument of packuments) {
manifest.versions[packument.version] = {
_id: `${name}@${packument.version}`,
Expand Down
55 changes: 55 additions & 0 deletions mock-registry/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"name": "@npmcli/mock-registry",
"version": "1.0.0",
"description": "",
"main": "lib/index.js",
"private": true,
"scripts": {
"test": "tap",
"lint": "eslint \"**/*.js\"",
"postlint": "template-oss-check",
"template-oss-apply": "template-oss-apply --force",
"lintfix": "node .. run lint -- --fix",
"snap": "tap",
"posttest": "node .. run lint"
},
"repository": {
"type": "git",
"url": "https://github.com/npm/cli.git",
"directory": "mock-registry"
},
"keywords": [],
"author": "GitHub Inc.",
"license": "ISC",
"bugs": {
"url": "https://github.com/npm/cli/issues"
},
"homepage": "https://github.com/npm/cli#readme",
"files": [
"bin/",
"lib/"
],
"engines": {
"node": "^14.17.0 || ^16.13.0 || >=18.0.0"
},
"templateOSS": {
"//@npmcli/template-oss": "This file is partially managed by @npmcli/template-oss. Edits may be overwritten.",
"version": "4.8.0"
},
"tap": {
"no-coverage": true,
"nyc-arg": [
"--exclude",
"tap-snapshots/**"
]
},
"devDependencies": {
"@npmcli/arborist": "^6.1.1",
"@npmcli/eslint-config": "^4.0.1",
"@npmcli/template-oss": "4.8.0",
"nock": "^13.2.9",
"npm-package-arg": "^10.0.0",
"pacote": "^15.0.6",
"tap": "^16.3.0"
}
}
Loading

0 comments on commit f36df46

Please sign in to comment.