Skip to content

Commit 3543822

Browse files
committed
fix(package): validate unpublished workspace dependencies together
1 parent 18eb770 commit 3543822

13 files changed

Lines changed: 348 additions & 48 deletions

File tree

‎.github/workflows/__test-action-package-test.yml‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,97 @@ jobs:
289289
assert.equal(fs.readFileSync(path.join(project, 'node_modules', 'must-not-copy', 'sentinel'), 'utf8'), 'preserve');
290290
assert.equal(fs.readFileSync(path.join(project, '.git', 'sentinel'), 'utf8'), 'preserve');
291291
assert.equal(fs.existsSync(path.join(project, 'package-lock.json')), false);
292+
293+
workspace-dependencies:
294+
name: Workspace consumer (${{ matrix.scenario }})
295+
runs-on: ubuntu-latest
296+
permissions:
297+
contents: read
298+
strategy:
299+
fail-fast: false
300+
matrix:
301+
scenario:
302+
[
303+
transitive dependencies,
304+
incompatible version,
305+
duplicate package,
306+
modified dependency,
307+
]
308+
steps:
309+
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
310+
with:
311+
persist-credentials: false
312+
- id: fixture
313+
name: Arrange unpublished packages and an unrelated broken package
314+
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
315+
env:
316+
SCENARIO: ${{ matrix.scenario }}
317+
with:
318+
script: |
319+
const fs = require('node:fs');
320+
const path = require('node:path');
321+
const { createHash } = require('node:crypto');
322+
const directory = fs.mkdtempSync(path.join(process.env.RUNNER_TEMP, 'workspace-fixture-'));
323+
const pack = async (id, manifest, code) => {
324+
const source = path.join(directory, id, 'package');
325+
await io.mkdirP(source);
326+
fs.writeFileSync(path.join(source, 'package.json'), JSON.stringify({
327+
name: `@hoverkraft-workspace-fixture/${id}`, version: '1.2.3', main: 'index.cjs',
328+
scripts: { preinstall: 'exit 91', install: 'exit 91', postinstall: 'exit 91' }, ...manifest,
329+
}));
330+
fs.writeFileSync(path.join(source, 'index.cjs'), code);
331+
const tarball = path.join(directory, `${id}.tgz`);
332+
await exec.exec('tar', ['-czf', tarball, '-C', path.dirname(source), 'package']);
333+
return tarball;
334+
};
335+
const coreTarball = await pack('core', { version: process.env.SCENARIO === 'incompatible version' ? '2.0.0' : '1.2.3' }, 'module.exports = 42;');
336+
const bridge = await pack('bridge', { dependencies: { '@hoverkraft-workspace-fixture/core': '^1.2.3' } }, 'module.exports = require("@hoverkraft-workspace-fixture/core");');
337+
const unrelated = await pack('unrelated', { dependencies: { missing: 'file:/must-not-install/missing.tgz' } }, 'throw new Error("Unrelated package must not be loaded");');
338+
const target = await pack('target', {
339+
peerDependencies: { '@hoverkraft-workspace-fixture/bridge': '1.2.3' },
340+
devDependencies: { '@hoverkraft-workspace-fixture/unrelated': '1.2.3' },
341+
}, 'module.exports = require("@hoverkraft-workspace-fixture/bridge");');
342+
const dependencies = [unrelated, coreTarball, bridge];
343+
if (process.env.SCENARIO === 'duplicate package') {
344+
dependencies.push(await pack('duplicate', { name: '@hoverkraft-workspace-fixture/core' }, 'module.exports = 0;'));
345+
}
346+
core.setOutput('tarball', target);
347+
core.setOutput('dependencies', dependencies);
348+
core.setOutput('core', coreTarball);
349+
core.setOutput('digests', Object.fromEntries([target, ...dependencies].map(file => [file, createHash('sha256').update(fs.readFileSync(file)).digest('hex')])));
350+
- id: consumer
351+
name: Act - Install only declared workspace dependencies without registry access
352+
continue-on-error: true
353+
uses: $/actions/package/test
354+
env:
355+
npm_config_offline: "true"
356+
SCENARIO: ${{ matrix.scenario }}
357+
DEPENDENCY_TARBALL: ${{ steps.fixture.outputs.core }}
358+
with:
359+
tarball-path: ${{ steps.fixture.outputs.tarball }}
360+
dependency-tarballs: ${{ steps.fixture.outputs.dependencies }}
361+
commands: |
362+
node -e 'require("node:assert/strict").equal(require(process.env.PACKAGE_NAME), 42)'
363+
test ! -d node_modules/@hoverkraft-workspace-fixture/unrelated
364+
if [ "$SCENARIO" = "modified dependency" ]; then
365+
printf '\n' >> "$DEPENDENCY_TARBALL"
366+
fi
367+
- name: Assert outcomes, cleanup and package integrity
368+
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
369+
env:
370+
SCENARIO: ${{ matrix.scenario }}
371+
OUTCOME: ${{ steps.consumer.outcome }}
372+
DIGESTS: ${{ steps.fixture.outputs.digests }}
373+
DEPENDENCY_TARBALL: ${{ steps.fixture.outputs.core }}
374+
with:
375+
script: |
376+
const assert = require('node:assert/strict');
377+
const fs = require('node:fs');
378+
const { createHash } = require('node:crypto');
379+
assert.equal(process.env.OUTCOME, process.env.SCENARIO === 'transitive dependencies' ? 'success' : 'failure');
380+
assert.deepEqual(fs.readdirSync(process.env.RUNNER_TEMP).filter(name => name.startsWith('package-test-')), []);
381+
for (const [file, expected] of Object.entries(JSON.parse(process.env.DIGESTS))) {
382+
const digest = createHash('sha256').update(fs.readFileSync(file)).digest('hex');
383+
if (process.env.SCENARIO === 'modified dependency' && file === process.env.DEPENDENCY_TARBALL) assert.notEqual(digest, expected);
384+
else assert.equal(digest, expected);
385+
}

‎.github/workflows/__test-workflow-continuous-integration.yml‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,44 @@ on:
66
permissions: {}
77

88
jobs:
9+
workspace-packages:
10+
name: Validate unpublished workspace dependencies
11+
uses: $/.github/workflows/continuous-integration.yml
12+
permissions:
13+
contents: read
14+
id-token: write
15+
packages: read
16+
pull-requests: write
17+
security-events: write
18+
with:
19+
working-directory: tests/workspace-packages
20+
build: ""
21+
lint: ""
22+
test: ""
23+
code-ql: ""
24+
dependency-review: false
25+
package: |
26+
{"tests":"node -e 'require(\"node:assert/strict\").equal(require(process.env.PACKAGE_NAME), 42)'"}
27+
28+
assert-workspace-packages:
29+
name: Assert both unpublished packages were validated
30+
needs: workspace-packages
31+
runs-on: ubuntu-latest
32+
permissions: {}
33+
steps:
34+
- uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
35+
env:
36+
ARTIFACTS: ${{ needs.workspace-packages.outputs.package-tarball-artifacts }}
37+
SINGLE_ARTIFACT: ${{ needs.workspace-packages.outputs.package-tarball-artifact-id }}
38+
with:
39+
script: |
40+
const assert = require('node:assert/strict');
41+
const artifacts = JSON.parse(process.env.ARTIFACTS);
42+
assert.deepEqual(Object.keys(artifacts).sort(), ['packages/consumer', 'packages/core']);
43+
assert.equal(new Set(Object.values(artifacts)).size, 2);
44+
for (const id of Object.values(artifacts)) assert.match(id, /^[1-9][0-9]*$/);
45+
assert.equal(process.env.SINGLE_ARTIFACT, '');
46+
947
act-without-build:
1048
name: Act - Run contract tests without a build
1149
uses: $/.github/workflows/continuous-integration.yml

‎.github/workflows/continuous-integration.md‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ on:
5858
permissions: {}
5959
jobs:
6060
continuous-integration:
61-
uses: hoverkraft-tech/ci-github-nodejs/.github/workflows/continuous-integration.yml@fbd3522b68c55a6314aadbededaa487825ba9b35 # 3.0.0
61+
uses: hoverkraft-tech/ci-github-nodejs/.github/workflows/continuous-integration.yml@18eb770c5d63c9b9809c367a91c55fb02c138ad4 # 3.0.1
6262
permissions:
6363
contents: read
6464
id-token: write
@@ -134,7 +134,9 @@ jobs:
134134
# - `test-project`: Optional consumer fixture directory relative to working-directory. Its dependencies and scripts are copied without node_modules or Git metadata. Requires `tests`.
135135
#
136136
# PACKAGE_INSTALL_PATH, PACKAGE_TARBALL_PATH, PACKAGE_NAME and PACKAGE_VERSION identify the tested package.
137-
# The consumer is cleaned up on success or failure. Commands must not modify the tarball.
137+
# All workspace tarballs are created before consumer tests. Declared workspace dependencies are installed
138+
# from these local tarballs, so unpublished versions can be validated together.
139+
# The consumer is cleaned up on success or failure. Commands must not modify tarballs.
138140
# Requires non-container CI with the default Ubuntu artifact layout.
139141
# Unknown options and invalid field types are rejected before build or packaging.
140142
#
@@ -297,7 +299,9 @@ jobs:
297299
| | - `test-project`: Optional consumer fixture directory relative to working-directory. Its dependencies and scripts are copied without node_modules or Git metadata. Requires `tests`. | | | |
298300
| | | | | |
299301
| | PACKAGE_INSTALL_PATH, PACKAGE_TARBALL_PATH, PACKAGE_NAME and PACKAGE_VERSION identify the tested package. | | | |
300-
| | The consumer is cleaned up on success or failure. Commands must not modify the tarball. | | | |
302+
| | All workspace tarballs are created before consumer tests. Declared workspace dependencies are installed | | | |
303+
| | from these local tarballs, so unpublished versions can be validated together. | | | |
304+
| | The consumer is cleaned up on success or failure. Commands must not modify tarballs. | | | |
301305
| | Requires non-container CI with the default Ubuntu artifact layout. | | | |
302306
| | Unknown options and invalid field types are rejected before build or packaging. | | | |
303307
| | | | | |

0 commit comments

Comments
 (0)