From df302eb5f4c236e20c83665b2b33f83067b45de5 Mon Sep 17 00:00:00 2001 From: Ryan Gonzalez Date: Fri, 30 Sep 2022 18:15:59 -0500 Subject: [PATCH] node: Add tests for custom registries This adds a test-time dependency on a running Verdaccio instance. Signed-off-by: Ryan Gonzalez --- .github/workflows/node.yaml | 7 +++++ node/README.md | 15 +++++++++ node/flatpak_node_generator/providers/yarn.py | 5 ++- node/tests/conftest.py | 20 +++++++++++- .../data/packages/custom-registry/.npmrc | 1 + .../custom-registry/package-lock.v1.json | 13 ++++++++ .../custom-registry/package-lock.v2.json | 27 ++++++++++++++++ .../packages/custom-registry/package.json | 7 +++++ .../custom-registry/registry-package/index.js | 7 +++++ .../registry-package/package.json | 4 +++ .../data/packages/custom-registry/yarn.lock | 8 +++++ node/tests/test_providers.py | 23 ++++++++++++++ node/tools/lockfile-utils.sh | 3 +- node/tools/setup-local-registry.sh | 31 +++++++++++++++++++ 14 files changed, 168 insertions(+), 3 deletions(-) create mode 100644 node/tests/data/packages/custom-registry/.npmrc create mode 100644 node/tests/data/packages/custom-registry/package-lock.v1.json create mode 100644 node/tests/data/packages/custom-registry/package-lock.v2.json create mode 100644 node/tests/data/packages/custom-registry/package.json create mode 100644 node/tests/data/packages/custom-registry/registry-package/index.js create mode 100644 node/tests/data/packages/custom-registry/registry-package/package.json create mode 100644 node/tests/data/packages/custom-registry/yarn.lock create mode 100755 node/tools/setup-local-registry.sh diff --git a/.github/workflows/node.yaml b/.github/workflows/node.yaml index 15b057fe..b4fbf9fd 100644 --- a/.github/workflows/node.yaml +++ b/.github/workflows/node.yaml @@ -26,6 +26,11 @@ jobs: poetry-version: - '1.1.13' runs-on: ubuntu-22.04 + services: + verdaccio: + image: verdaccio/verdaccio + ports: + - 4873:4873 steps: - uses: actions/checkout@v3 - name: Configure git @@ -45,6 +50,8 @@ jobs: flatpak --user remote-add flathub https://flathub.org/repo/flathub.flatpakrepo flatpak --user install -y flathub \ org.freedesktop.{Platform,Sdk{,.Extension.node{14,16}}}//21.08 + - name: Setup the local npm registry + run: tools/setup-local-registry.sh - name: Install dependencies run: poetry install - name: Run checks diff --git a/node/README.md b/node/README.md index 06c2cca4..8ad3ae72 100644 --- a/node/README.md +++ b/node/README.md @@ -321,6 +321,19 @@ $ poetry run pytest -n auto Note that these tests can take up quite a bit of space in /tmp, so if you hit `No space left on device` errors, try expanding `/tmp` or changing `$TMPDIR`. +### Local Registry + +Some of the tests require a local npm registry to work. For this purpose, you can use +[Verdaccio](https://verdaccio.org/), preferably via Docker / podman: + +```bash +$ docker run --rm -it -p 4873:4873 verdaccio/verdaccio +``` + +Then run `tools/setup-local-registry.sh` to set up this registry with a pre-published +package. (Note that running it twice will result in an error, since it tries to publish +the same package twice.) + ### Utility Scripts A few utility scripts are included in the `tools` directory: @@ -333,6 +346,8 @@ A few utility scripts are included in the `tools` directory: - `lockfile-utils.sh peek-cache PACKAGE-MANAGER PACKAGE` will install the dependencies from the corresponding lockfile and then extract the resulting package cache (npm) or mirror directory (yarn), for closer examination. +- `setup-local-registry.sh` will set up a local npm registry as [described + above](#local-registry). - `b64-to-hex.sh` will convert a base64 hash value from npm into hex, e.g.: ``` $ echo x+sXyT4RLLEIb6bY5R+wZnt5pfk= | tools/b64-to-hex.sh diff --git a/node/flatpak_node_generator/providers/yarn.py b/node/flatpak_node_generator/providers/yarn.py index b6b5a0ec..39bbe951 100644 --- a/node/flatpak_node_generator/providers/yarn.py +++ b/node/flatpak_node_generator/providers/yarn.py @@ -110,6 +110,9 @@ def process_lockfile(self, lockfile: Path) -> Iterator[Package]: class YarnConfigProvider(ConfigProvider): + def __init__(self) -> None: + self._npm_config_provider = NpmConfigProvider() + @property def _filename(self) -> str: return '.yarnrc' @@ -121,7 +124,7 @@ def parse_config(self, path: Path) -> Dict[str, Any]: def load_config(self, lockfile: Path) -> Config: config = super().load_config(lockfile) - npm_config = NpmConfigProvider().load_config(lockfile) + npm_config = self._npm_config_provider.load_config(lockfile) config.merge_new_keys_only(npm_config.data) return config diff --git a/node/tests/conftest.py b/node/tests/conftest.py index dbec767d..fa171a0c 100644 --- a/node/tests/conftest.py +++ b/node/tests/conftest.py @@ -170,6 +170,14 @@ class ProviderPaths: def package_json(self) -> Path: return self.root / 'package.json' + @property + def npmrc(self) -> Path: + return self.root / '.npmrc' + + @property + def yarnrc(self) -> Path: + return self.root / '.yarnrc' + @property def lockfile_source(self) -> Path: if self.type == ProviderFactoryType.NPM: @@ -192,6 +200,11 @@ def lockfile_dest(self) -> str: def add_to_manifest(self, gen: ManifestGenerator) -> None: gen.add_local_file_source(self.package_json) gen.add_local_file_source(self.lockfile_source, Path(self.lockfile_dest)) + + for rc in self.npmrc, self.yarnrc: + if rc.exists(): + gen.add_local_file_source(rc) + if self.type == ProviderFactoryType.YARN: gen.add_data_source( f'yarn-offline-mirror "./flatpak-node/yarn-mirror"', Path('.yarnrc') @@ -266,7 +279,12 @@ async def generate_modules( ) special = SpecialSourceProvider(gen, self.special) - with factory.create_module_provider(gen, special) as module: + config_provider = factory.create_config_provider() + lockfile_configs = { + paths.lockfile_source: config_provider.load_config(paths.lockfile_source), + } + + with factory.create_module_provider(gen, special, lockfile_configs) as module: for package in factory.create_lockfile_provider().process_lockfile( paths.lockfile_source ): diff --git a/node/tests/data/packages/custom-registry/.npmrc b/node/tests/data/packages/custom-registry/.npmrc new file mode 100644 index 00000000..adb0501e --- /dev/null +++ b/node/tests/data/packages/custom-registry/.npmrc @@ -0,0 +1 @@ +"@flatpak-node-generator-tests:registry" = "http://localhost:4873" diff --git a/node/tests/data/packages/custom-registry/package-lock.v1.json b/node/tests/data/packages/custom-registry/package-lock.v1.json new file mode 100644 index 00000000..615b0656 --- /dev/null +++ b/node/tests/data/packages/custom-registry/package-lock.v1.json @@ -0,0 +1,13 @@ +{ + "name": "@flatpak-node-generator-tests/custom-registry", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "@flatpak-node-generator-tests/registry-package": { + "version": "1.0.0", + "resolved": "http://localhost:4873/@flatpak-node-generator-tests%2fregistry-package/-/registry-package-1.0.0.tgz", + "integrity": "sha512-dyHOPsVMVjCUtyL7jeqs+ISkpRnXV88lkObwNJvYl33f/nWiKj/luzfoE4wgYOcmRuYwLNgAV/Y2SD19aHWDng==" + } + } +} diff --git a/node/tests/data/packages/custom-registry/package-lock.v2.json b/node/tests/data/packages/custom-registry/package-lock.v2.json new file mode 100644 index 00000000..2cdd7e89 --- /dev/null +++ b/node/tests/data/packages/custom-registry/package-lock.v2.json @@ -0,0 +1,27 @@ +{ + "name": "@flatpak-node-generator-tests/custom-registry", + "version": "1.0.0", + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "name": "@flatpak-node-generator-tests/custom-registry", + "version": "1.0.0", + "dependencies": { + "@flatpak-node-generator-tests/registry-package": "*" + } + }, + "node_modules/@flatpak-node-generator-tests/registry-package": { + "version": "1.0.0", + "resolved": "http://localhost:4873/@flatpak-node-generator-tests%2fregistry-package/-/registry-package-1.0.0.tgz", + "integrity": "sha512-dyHOPsVMVjCUtyL7jeqs+ISkpRnXV88lkObwNJvYl33f/nWiKj/luzfoE4wgYOcmRuYwLNgAV/Y2SD19aHWDng==" + } + }, + "dependencies": { + "@flatpak-node-generator-tests/registry-package": { + "version": "1.0.0", + "resolved": "http://localhost:4873/@flatpak-node-generator-tests%2fregistry-package/-/registry-package-1.0.0.tgz", + "integrity": "sha512-dyHOPsVMVjCUtyL7jeqs+ISkpRnXV88lkObwNJvYl33f/nWiKj/luzfoE4wgYOcmRuYwLNgAV/Y2SD19aHWDng==" + } + } +} diff --git a/node/tests/data/packages/custom-registry/package.json b/node/tests/data/packages/custom-registry/package.json new file mode 100644 index 00000000..5628efb8 --- /dev/null +++ b/node/tests/data/packages/custom-registry/package.json @@ -0,0 +1,7 @@ +{ + "name": "@flatpak-node-generator-tests/custom-registry", + "version": "1.0.0", + "dependencies": { + "@flatpak-node-generator-tests/registry-package": "*" + } +} diff --git a/node/tests/data/packages/custom-registry/registry-package/index.js b/node/tests/data/packages/custom-registry/registry-package/index.js new file mode 100644 index 00000000..12d6ec24 --- /dev/null +++ b/node/tests/data/packages/custom-registry/registry-package/index.js @@ -0,0 +1,7 @@ +const fs = require('fs') + +module.exports = { + sayHello: () => { + fs.writeFileSync('hello.txt', 'Hello!') + }, +} diff --git a/node/tests/data/packages/custom-registry/registry-package/package.json b/node/tests/data/packages/custom-registry/registry-package/package.json new file mode 100644 index 00000000..2f21b269 --- /dev/null +++ b/node/tests/data/packages/custom-registry/registry-package/package.json @@ -0,0 +1,4 @@ +{ + "name": "@flatpak-node-generator-tests/registry-package", + "version": "1.0.0" +} diff --git a/node/tests/data/packages/custom-registry/yarn.lock b/node/tests/data/packages/custom-registry/yarn.lock new file mode 100644 index 00000000..491c7945 --- /dev/null +++ b/node/tests/data/packages/custom-registry/yarn.lock @@ -0,0 +1,8 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +"@flatpak-node-generator-tests/registry-package@*": + version "1.0.0" + resolved "http://localhost:4873/@flatpak-node-generator-tests%2fregistry-package/-/registry-package-1.0.0.tgz#1c677ccdfe9442572d73b64b4f01268fa3f302a9" + integrity sha512-dyHOPsVMVjCUtyL7jeqs+ISkpRnXV88lkObwNJvYl33f/nWiKj/luzfoE4wgYOcmRuYwLNgAV/Y2SD19aHWDng== diff --git a/node/tests/test_providers.py b/node/tests/test_providers.py index f0c9efb4..c8c7daf4 100644 --- a/node/tests/test_providers.py +++ b/node/tests/test_providers.py @@ -37,6 +37,29 @@ def parse_config(self, path: Path) -> Dict[str, Any]: } +async def test_custom_registry( + flatpak_builder: FlatpakBuilder, + provider_factory_spec: ProviderFactorySpec, + node_version: int, +) -> None: + with ManifestGenerator() as gen: + await provider_factory_spec.generate_modules( + 'custom-registry', gen, node_version + ) + + flatpak_builder.build( + sources=gen.ordered_sources(), + commands=[ + provider_factory_spec.install_command, + """node -e 'require("@flatpak-node-generator-tests/registry-package").sayHello()'""", + ], + use_node=node_version, + ) + + hello_txt = flatpak_builder.module_dir / 'hello.txt' + assert hello_txt.read_text() == 'Hello!' + + async def test_minimal_git( flatpak_builder: FlatpakBuilder, provider_factory_spec: ProviderFactorySpec, diff --git a/node/tools/lockfile-utils.sh b/node/tools/lockfile-utils.sh index 7e7a84e6..253a2c0b 100755 --- a/node/tools/lockfile-utils.sh +++ b/node/tools/lockfile-utils.sh @@ -1,6 +1,7 @@ #!/usr/bin/env bash set -e +shopt -s nullglob die() { echo "$@" >&2 @@ -54,7 +55,7 @@ package_path="$(dirname "$0")/../tests/data/packages/$package_arg" tmpdir=$(mktemp -d) trap 'rm -rf -- "$tmpdir"' EXIT -cp "$package_path/package.json" "$tmpdir" +cp "$package_path/package.json" "$package_path/".*rc "$tmpdir" # Special-case handling for our test of a local package. [[ -d "$package_path/subdir" ]] && cp -r "$package_path/subdir" "$tmpdir" diff --git a/node/tools/setup-local-registry.sh b/node/tools/setup-local-registry.sh new file mode 100755 index 00000000..f6857b0c --- /dev/null +++ b/node/tools/setup-local-registry.sh @@ -0,0 +1,31 @@ +#!/usr/bin/env bash + +set -e + +REGISTRY=localhost:4873 + +curl -X PUT http://$REGISTRY/-/user/org.couchdb.user:test \ + -H 'Content-Type: application/json' \ + -d '{"name": "test", "password": "test"}' +echo + +pkg_path="$(dirname "$0")/../tests/data/packages/custom-registry/registry-package" + +tmpdir=$(mktemp -d) +trap 'rm -rf -- "$tmpdir"' EXIT + +cp -r "$pkg_path/"* "$tmpdir" + +cat > "$tmpdir/.npmrc" <