|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { execFile } from 'node:child_process'; |
| 3 | +import { mkdtemp, mkdir, readFile, rename, rm, symlink, writeFile } from 'node:fs/promises'; |
| 4 | +import os from 'node:os'; |
| 5 | +import path from 'node:path'; |
| 6 | +import test from 'node:test'; |
| 7 | +import { fileURLToPath } from 'node:url'; |
| 8 | +import { promisify } from 'node:util'; |
| 9 | + |
| 10 | +// The production helper is also invoked directly by GitHub Actions without tsx. |
| 11 | +// @ts-expect-error The dependency-free Node helper has no declaration file. |
| 12 | +import { computeTestKey } from '../scripts/ci-test-key.mjs'; |
| 13 | + |
| 14 | +const execute = promisify(execFile); |
| 15 | +const helper = fileURLToPath(new URL('../scripts/ci-test-key.mjs', import.meta.url)); |
| 16 | +const runtime = { version: 'v22.14.0', platform: 'linux', arch: 'x64' }; |
| 17 | + |
| 18 | +async function fixture(context: { after: (callback: () => Promise<void>) => void }) { |
| 19 | + const root = await mkdtemp(path.join(os.tmpdir(), 'protolab-ci-key-')); |
| 20 | + context.after(() => rm(root, { recursive: true, force: true })); |
| 21 | + await execute('git', ['init', '--quiet'], { cwd: root }); |
| 22 | + const write = async (name: string, content = name) => { |
| 23 | + await mkdir(path.dirname(path.join(root, name)), { recursive: true }); |
| 24 | + await writeFile(path.join(root, name), content); |
| 25 | + }; |
| 26 | + const stage = () => execute('git', ['add', '--all'], { cwd: root }); |
| 27 | + await write('src/core/example.ts', 'export const example = 1;'); |
| 28 | + await stage(); |
| 29 | + return { root, write, stage, key: () => computeTestKey(root, runtime) as Promise<string> }; |
| 30 | +} |
| 31 | + |
| 32 | +test('the full-test key tracks contents, additions, removals and path changes', async (context) => { |
| 33 | + const { root, write, stage, key } = await fixture(context); |
| 34 | + const original = await key(); |
| 35 | + assert.match(original, /^protolab-full-tests-v1-[a-f0-9]{64}$/); |
| 36 | + assert.equal(await key(), original); |
| 37 | + await write('src/core/example.ts', 'export const example = 2;'); |
| 38 | + const changed = await key(); |
| 39 | + assert.notEqual(changed, original); |
| 40 | + await write('src/core/extra.ts', 'export const extra = true;'); |
| 41 | + assert.equal(await key(), changed, 'Only tracked files are published and fingerprinted.'); |
| 42 | + await stage(); |
| 43 | + const added = await key(); |
| 44 | + assert.notEqual(added, changed); |
| 45 | + await rename(path.join(root, 'src/core/extra.ts'), path.join(root, 'src/core/renamed.ts')); |
| 46 | + await stage(); |
| 47 | + assert.notEqual( |
| 48 | + await key(), |
| 49 | + added, |
| 50 | + 'A rename changes the input identity, even with identical bytes.', |
| 51 | + ); |
| 52 | + await rm(path.join(root, 'src/core/renamed.ts')); |
| 53 | + await stage(); |
| 54 | + assert.equal(await key(), changed, 'Removing the extra input restores the prior content key.'); |
| 55 | +}); |
| 56 | + |
| 57 | +test('all test input roots and exact runtime versions invalidate the key', async (context) => { |
| 58 | + const { root, write, stage, key } = await fixture(context); |
| 59 | + for (const name of [ |
| 60 | + 'src/parts/example/presets.json', |
| 61 | + 'tests/example.test.ts', |
| 62 | + 'scripts/part-modules/example.ts', |
| 63 | + 'data/promtehimport-inventory.json', |
| 64 | + 'public/references/dimensions.png', |
| 65 | + '.github/workflows/pages.yml', |
| 66 | + 'package.json', |
| 67 | + 'package-lock.json', |
| 68 | + 'tsconfig.json', |
| 69 | + 'tsconfig.tests.json', |
| 70 | + ]) { |
| 71 | + const before = await key(); |
| 72 | + await write(name); |
| 73 | + await stage(); |
| 74 | + assert.notEqual(await key(), before, name); |
| 75 | + } |
| 76 | + const original = await key(); |
| 77 | + for (const patch of [{ version: 'v22.14.1' }, { platform: 'darwin' }, { arch: 'arm64' }]) |
| 78 | + assert.notEqual(await computeTestKey(root, { ...runtime, ...patch }), original); |
| 79 | + await assert.rejects(computeTestKey(root, { ...runtime, version: '' }), /runtime version/); |
| 80 | +}); |
| 81 | + |
| 82 | +test('documentation and deployment-only entry points do not rerun unchanged tests', async (context) => { |
| 83 | + const { write, stage, key } = await fixture(context); |
| 84 | + const original = await key(); |
| 85 | + for (const name of ['README.md', 'docs/deployment.md', 'vite.config.ts', 'index.html', 'CNAME']) |
| 86 | + await write(name, 'Changed deployment settings or documentation.'); |
| 87 | + await stage(); |
| 88 | + assert.equal(await key(), original); |
| 89 | +}); |
| 90 | + |
| 91 | +test('missing inputs, symlinks, empty repositories and nested working directories fail closed', async (context) => { |
| 92 | + const { root, key, stage } = await fixture(context); |
| 93 | + const input = path.join(root, 'src/core/example.ts'); |
| 94 | + await assert.rejects(computeTestKey(path.join(root, 'src'), runtime), /repository root/); |
| 95 | + await rm(input); |
| 96 | + await assert.rejects(key(), /ENOENT/); |
| 97 | + await symlink('missing.ts', input); |
| 98 | + await assert.rejects(key(), /regular files/); |
| 99 | + await rm(input); |
| 100 | + await stage(); |
| 101 | + await assert.rejects(key(), /No tracked full-test inputs/); |
| 102 | +}); |
| 103 | + |
| 104 | +test('CLI emits a reusable GitHub output and rejects unsupported arguments', async (context) => { |
| 105 | + const { root } = await fixture(context); |
| 106 | + const output = path.join(root, 'github-output'); |
| 107 | + const expected = await computeTestKey(root); |
| 108 | + const result = await execute(process.execPath, [helper, '--github-output'], { |
| 109 | + cwd: root, |
| 110 | + env: { ...process.env, GITHUB_OUTPUT: output }, |
| 111 | + }); |
| 112 | + assert.equal(result.stdout, `${expected}\n`); |
| 113 | + assert.equal(await readFile(output, 'utf8'), `key=${expected}\n`); |
| 114 | + const plain = await execute(process.execPath, [helper], { cwd: root }); |
| 115 | + assert.equal(plain.stdout, `${expected}\n`); |
| 116 | + await assert.rejects(execute(process.execPath, [helper, '--unknown'], { cwd: root }), /Usage:/); |
| 117 | + await assert.rejects( |
| 118 | + execute(process.execPath, [helper, '--github-output'], { |
| 119 | + cwd: root, |
| 120 | + env: { ...process.env, GITHUB_OUTPUT: '' }, |
| 121 | + }), |
| 122 | + /requires GITHUB_OUTPUT/, |
| 123 | + ); |
| 124 | +}); |
0 commit comments