Skip to content

Commit 429e0c2

Browse files
committed
feat(package)!: set up isolated consumers for package tests
Install tarballs with scripts disabled, expose package binaries and support copied example projects. Keep archive-only defaults dependency-free. BREAKING CHANGE: custom package test commands run from the temporary consumer. Use GITHUB_WORKSPACE to access checked-out source.
1 parent c6c6c72 commit 429e0c2

9 files changed

Lines changed: 309 additions & 126 deletions

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

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,111 @@ jobs:
181181
assert.equal(process.env.OUTCOME, process.env.EXPECTED_OUTCOME);
182182
assert.equal(createHash('sha256').update(fs.readFileSync(process.env.TARBALL)).digest('hex'), process.env.EXPECTED_DIGEST);
183183
assert.deepEqual(fs.readdirSync(process.env.RUNNER_TEMP).filter(name => name.startsWith('package-test-')), []);
184+
185+
consumer:
186+
name: Installed consumer (${{ matrix.fixture.name }})
187+
runs-on: ubuntu-latest
188+
permissions:
189+
contents: read
190+
strategy:
191+
fail-fast: false
192+
matrix:
193+
fixture:
194+
- name: empty consumer
195+
project: ""
196+
outcome: success
197+
- name: example project
198+
project: consumer-fixture
199+
outcome: success
200+
- name: failed installation
201+
project: ""
202+
outcome: failure
203+
steps:
204+
- name: Arrange - Checkout
205+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
206+
with:
207+
persist-credentials: false
208+
- name: Arrange - Package and example project with failing install hooks
209+
id: fixture
210+
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
211+
env:
212+
FAIL_INSTALL: ${{ matrix.fixture.outcome == 'failure' }}
213+
with:
214+
script: |
215+
const fs = require('node:fs');
216+
const path = require('node:path');
217+
const { createHash } = require('node:crypto');
218+
const directory = fs.mkdtempSync(path.join(process.env.RUNNER_TEMP, 'consumer-fixture-'));
219+
const packageDirectory = path.join(directory, 'package');
220+
await io.mkdirP(packageDirectory);
221+
const scripts = { preinstall: 'exit 91', install: 'exit 91', postinstall: 'exit 91', prepare: 'exit 91' };
222+
fs.writeFileSync(path.join(packageDirectory, 'package.json'), JSON.stringify({
223+
name: '@example/consumer-fixture', version: '1.2.3', main: 'index.js',
224+
bin: { 'fixture-cli': 'cli.js' }, scripts,
225+
dependencies: process.env.FAIL_INSTALL === 'true' ? { missing: `file:${directory}/missing.tgz` } : {},
226+
}));
227+
fs.writeFileSync(path.join(packageDirectory, 'index.js'), 'module.exports = 42;');
228+
fs.writeFileSync(path.join(packageDirectory, 'cli.js'), '#!/usr/bin/env node\nconsole.log(require("./package.json").version);');
229+
const tarball = path.join(directory, 'fixture.tgz');
230+
await exec.exec('tar', ['-czf', tarball, '-C', directory, 'package']);
231+
const project = path.join(process.env.GITHUB_WORKSPACE, 'consumer-fixture');
232+
await io.mkdirP(path.join(project, 'node_modules', 'must-not-copy'));
233+
await io.mkdirP(path.join(project, '.git'));
234+
fs.writeFileSync(path.join(project, 'node_modules', 'must-not-copy', 'sentinel'), 'preserve');
235+
fs.writeFileSync(path.join(project, '.git', 'sentinel'), 'preserve');
236+
fs.writeFileSync(path.join(project, '.fixture-config'), 'copied');
237+
fs.writeFileSync(path.join(project, 'package.json'), JSON.stringify({
238+
name: 'example-consumer', private: true,
239+
dependencies: { '@example/consumer-fixture': 'file:../must-not-resolve' },
240+
scripts: { ...scripts, build: 'node build.cjs' },
241+
}));
242+
fs.writeFileSync(path.join(project, 'build.cjs'), 'require("node:assert/strict").equal(require("@example/consumer-fixture"), 42);');
243+
core.setOutput('tarball', tarball);
244+
core.setOutput('digest', createHash('sha256').update(fs.readFileSync(tarball)).digest('hex'));
245+
core.setOutput('project-manifest', fs.readFileSync(path.join(project, 'package.json'), 'utf8'));
246+
- name: Act - Exercise the installed package
247+
id: test
248+
continue-on-error: true
249+
uses: $/actions/package/test
250+
env:
251+
TEST_PROJECT: ${{ matrix.fixture.project }}
252+
with:
253+
tarball-path: ${{ steps.fixture.outputs.tarball }}
254+
test-project: ${{ matrix.fixture.project }}
255+
commands: |
256+
test "$PWD" != "$GITHUB_WORKSPACE"
257+
test "$PACKAGE_NAME" = @example/consumer-fixture
258+
test "$PACKAGE_VERSION" = 1.2.3
259+
test "$(fixture-cli)" = "$PACKAGE_VERSION"
260+
test -f "$PACKAGE_INSTALL_PATH/index.js"
261+
test ! -e node_modules/must-not-copy
262+
test ! -e .git
263+
node -e 'require("node:assert/strict").equal(require(process.env.PACKAGE_NAME), 42)'
264+
if [ -n "$TEST_PROJECT" ]; then
265+
test "$(cat .fixture-config)" = copied
266+
npm run build
267+
fi
268+
touch "$RUNNER_TEMP/consumer-command-ran"
269+
- name: Assert - Outcome, cleanup and unchanged source files
270+
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
271+
env:
272+
OUTCOME: ${{ steps.test.outcome }}
273+
EXPECTED_OUTCOME: ${{ matrix.fixture.outcome }}
274+
TARBALL: ${{ steps.fixture.outputs.tarball }}
275+
EXPECTED_DIGEST: ${{ steps.fixture.outputs.digest }}
276+
PROJECT_MANIFEST: ${{ steps.fixture.outputs.project-manifest }}
277+
with:
278+
script: |
279+
const assert = require('node:assert/strict');
280+
const fs = require('node:fs');
281+
const path = require('node:path');
282+
const { createHash } = require('node:crypto');
283+
assert.equal(process.env.OUTCOME, process.env.EXPECTED_OUTCOME);
284+
assert.equal(fs.existsSync(path.join(process.env.RUNNER_TEMP, 'consumer-command-ran')), process.env.OUTCOME === 'success');
285+
assert.equal(createHash('sha256').update(fs.readFileSync(process.env.TARBALL)).digest('hex'), process.env.EXPECTED_DIGEST);
286+
assert.deepEqual(fs.readdirSync(process.env.RUNNER_TEMP).filter(name => name.startsWith('package-test-')), []);
287+
const project = path.join(process.env.GITHUB_WORKSPACE, 'consumer-fixture');
288+
assert.equal(fs.readFileSync(path.join(project, 'package.json'), 'utf8'), process.env.PROJECT_MANIFEST);
289+
assert.equal(fs.readFileSync(path.join(project, 'node_modules', 'must-not-copy', 'sentinel'), 'utf8'), 'preserve');
290+
assert.equal(fs.readFileSync(path.join(project, '.git', 'sentinel'), 'utf8'), 'preserve');
291+
assert.equal(fs.existsSync(path.join(project, 'package-lock.json')), false);

‎.github/workflows/__test-release.yml‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,10 @@ jobs:
146146
working-directory: actions/package/test
147147
tarball-path: ${{ steps.fixture.outputs.tarball }}
148148
commands: |
149-
test "$PWD" = "$GITHUB_WORKSPACE/actions/package/test"
149+
test "$PWD" != "$GITHUB_WORKSPACE/actions/package/test"
150150
test "$PACKAGE_NAME" = "$EXPECTED_PACKAGE_NAME"
151151
test "$PACKAGE_VERSION" = 1.2.3
152-
consumer=$(mktemp -d)
153-
npm install --prefix "$consumer" --ignore-scripts --offline --no-audit --no-fund "$PACKAGE_TARBALL_PATH"
154-
test "$("$consumer/node_modules/.bin/release-fixture")" = "$PACKAGE_VERSION"
152+
test "$(release-fixture)" = "$PACKAGE_VERSION"
155153
touch "$RUNNER_TEMP/release-smoke-ran"
156154
- name: Act - Reject a failing package command
157155
id: failing-command

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

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ jobs:
2222
tar -xOf "$PACKAGE_TARBALL_PATH" package/dist/test.txt > "$RUNNER_TEMP/package-build.txt"
2323
test "$(cat "$RUNNER_TEMP/package-build.txt")" = 'build test'
2424
test "$PACKAGE_NAME" = test-for-ci-with-npm
25-
consumer=$(mktemp -d)
26-
npm install --prefix "$consumer" --ignore-scripts --offline --no-audit --no-fund "$PACKAGE_TARBALL_PATH"
27-
test -f "$consumer/node_modules/$PACKAGE_NAME/dist/test.txt"
25+
test -f "$PACKAGE_INSTALL_PATH/dist/test.txt"
2826
code-ql: |
2927
{
3028
"languages": "javascript-typescript",
@@ -117,22 +115,10 @@ jobs:
117115
tar -xOf "$TARBALL" package/dist/test.txt > "$RUNNER_TEMP/packaged-build.txt"
118116
cmp "$RUNNER_TEMP/prepared-fixture/dist/test.txt" "$RUNNER_TEMP/packaged-build.txt"
119117
test ! -d tests/npm/node_modules
120-
- name: Run package smoke checks with the project runtime
118+
- name: Check the package without installing its unavailable dependencies
121119
uses: $/actions/package/test
122120
with:
123-
working-directory: tests/npm
124121
tarball-path: ${{ steps.package.outputs.package-tarball-path }}
125-
commands: |
126-
node <<'NODE'
127-
const assert = require('node:assert/strict');
128-
const fs = require('node:fs');
129-
const expected = fs.readFileSync('.node-version', 'utf8').trim().split('.');
130-
assert.deepEqual(process.versions.node.split('.').slice(0, expected.length), expected);
131-
NODE
132-
test "$PACKAGE_NAME" = prepared-fixture
133-
test "$PACKAGE_VERSION" = 1.2.3
134-
tar -xOf "$PACKAGE_TARBALL_PATH" package/dist/test.txt > "$RUNNER_TEMP/smoke-output.txt"
135-
cmp "$RUNNER_TEMP/prepared-fixture/dist/test.txt" "$RUNNER_TEMP/smoke-output.txt"
136122
- name: Validate publishing the prepared tarball
137123
uses: $/actions/package/publish
138124
with:

0 commit comments

Comments
 (0)