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 Dec 17, 2022
1 parent 0fd523f commit 85a4382
Show file tree
Hide file tree
Showing 13 changed files with 164 additions and 2 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 @@ -40,6 +45,8 @@ jobs:
poetry-version: ${{ matrix.poetry-version }}
- name: Install OS dependencies
run: sudo apt-get update && sudo apt-get install -y flatpak-builder
- name: Setup the local npm register
run: tools/setup-local-registry.sh
- name: Install Flatpak dependencies
run: |
flatpak --user remote-add flathub https://flathub.org/repo/flathub.flatpakrepo
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
20 changes: 19 additions & 1 deletion node/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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')
Expand Down Expand Up @@ -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
):
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 @@
"@custom: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": {
"@custom/registry-package": {
"version": "1.0.0",
"resolved": "http://localhost:4873/@custom%2fregistry-package/-/registry-package-1.0.0.tgz",
"integrity": "sha512-O6a3tZ0qHF/7tqJRu7DeUAwkMPyVcX3PlNQU+nAxGptTjetipiGQ3jq7Jo7mJNAgMzyv6Psy5riVqMuNjvup5Q=="
}
}
}
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": {
"@custom/registry-package": "*"
}
},
"node_modules/@custom/registry-package": {
"version": "1.0.0",
"resolved": "http://localhost:4873/@custom%2fregistry-package/-/registry-package-1.0.0.tgz",
"integrity": "sha512-O6a3tZ0qHF/7tqJRu7DeUAwkMPyVcX3PlNQU+nAxGptTjetipiGQ3jq7Jo7mJNAgMzyv6Psy5riVqMuNjvup5Q=="
}
},
"dependencies": {
"@custom/registry-package": {
"version": "1.0.0",
"resolved": "http://localhost:4873/@custom%2fregistry-package/-/registry-package-1.0.0.tgz",
"integrity": "sha512-O6a3tZ0qHF/7tqJRu7DeUAwkMPyVcX3PlNQU+nAxGptTjetipiGQ3jq7Jo7mJNAgMzyv6Psy5riVqMuNjvup5Q=="
}
}
}
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": {
"@custom/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": "@custom/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


"@custom/registry-package@*":
version "1.0.0"
resolved "http://localhost:4873/@custom%2fregistry-package/-/registry-package-1.0.0.tgz#b632e33e611a829ea94d0a03c53f7c1ee99b219a"
integrity sha512-O6a3tZ0qHF/7tqJRu7DeUAwkMPyVcX3PlNQU+nAxGptTjetipiGQ3jq7Jo7mJNAgMzyv6Psy5riVqMuNjvup5Q==
23 changes: 23 additions & 0 deletions node/tests/test_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("@custom/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 @@ -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"
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//21.08 \
-c ". /usr/lib/sdk/node16/enable.sh \
&& npm publish --loglevel verbose --registry http://$REGISTRY")

0 comments on commit 85a4382

Please sign in to comment.