Skip to content

Commit

Permalink
node: Add tests for custom registries
Browse files Browse the repository at this point in the history
This adds a test-time dependency on a running Verdaccio instance.

Signed-off-by: Ryan Gonzalez <[email protected]>
  • Loading branch information
refi64 committed Jan 13, 2023
1 parent 312dc8b commit b4ade5a
Show file tree
Hide file tree
Showing 15 changed files with 188 additions and 3 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/node.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,18}}}//22.08
- name: Setup the local npm registry
run: tools/setup-local-registry.sh
- name: Install dependencies
run: poetry install
- name: Run checks
Expand Down
15 changes: 15 additions & 0 deletions node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion node/flatpak_node_generator/providers/yarn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand Down
20 changes: 19 additions & 1 deletion node/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,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:
Expand Down Expand Up @@ -199,6 +207,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')
Expand Down Expand Up @@ -273,7 +286,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
):
Expand Down
1 change: 1 addition & 0 deletions node/tests/data/packages/custom-registry/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"@flatpak-node-generator-tests:registry" = "http://localhost:4873"
13 changes: 13 additions & 0 deletions node/tests/data/packages/custom-registry/package-lock.v1.json
Original file line number Diff line number Diff line change
@@ -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=="
}
}
}
27 changes: 27 additions & 0 deletions node/tests/data/packages/custom-registry/package-lock.v2.json
Original file line number Diff line number Diff line change
@@ -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=="
}
}
}
20 changes: 20 additions & 0 deletions node/tests/data/packages/custom-registry/package-lock.v3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "@flatpak-node-generator-tests/custom-registry",
"version": "1.0.0",
"lockfileVersion": 3,
"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=="
}
}
}
7 changes: 7 additions & 0 deletions node/tests/data/packages/custom-registry/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "@flatpak-node-generator-tests/custom-registry",
"version": "1.0.0",
"dependencies": {
"@flatpak-node-generator-tests/registry-package": "*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const fs = require('fs')

module.exports = {
sayHello: () => {
fs.writeFileSync('hello.txt', 'Hello!')
},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "@flatpak-node-generator-tests/registry-package",
"version": "1.0.0"
}
8 changes: 8 additions & 0 deletions node/tests/data/packages/custom-registry/yarn.lock
Original file line number Diff line number Diff line change
@@ -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==
23 changes: 23 additions & 0 deletions node/tests/test_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,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,
Expand Down
3 changes: 2 additions & 1 deletion node/tools/lockfile-utils.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env bash

set -e
shopt -s nullglob

die() {
echo "$@" >&2
Expand Down Expand Up @@ -62,7 +63,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"
Expand Down
31 changes: 31 additions & 0 deletions node/tools/setup-local-registry.sh
Original file line number Diff line number Diff line change
@@ -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" <<EOF
registry = http://$REGISTRY
_auth = $(echo -n test:test | base64)
EOF

(set -x; flatpak run \
--command=bash \
--cwd="$tmpdir" \
--filesystem="$tmpdir" \
--share=network \
org.freedesktop.Sdk//22.08 \
-c ". /usr/lib/sdk/node16/enable.sh \
&& npm publish --loglevel verbose --registry http://$REGISTRY")

0 comments on commit b4ade5a

Please sign in to comment.