diff --git a/scripts/pre-merge-check.spec.ts b/scripts/pre-merge-check.spec.ts index 79a7536cc2..6027d0c60a 100644 --- a/scripts/pre-merge-check.spec.ts +++ b/scripts/pre-merge-check.spec.ts @@ -1,25 +1,8 @@ -import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs' +import { mkdtempSync, rmSync } from 'node:fs' import { tmpdir } from 'node:os' -import { dirname, join } from 'node:path' +import { join } from 'node:path' import { describe, expect, it } from 'vitest' -import { checkMode, failureDecision, preMergeCheck, preMergeSteps, type PreMergeOutcome, type PreMergeStep } from './pre-merge-check.ts' - -/** - * Build a repository holding the given manifests. - * @param files - Manifest path to version. - * @param directories - Extra directories to create. - * @returns The temporary root. - */ -function repository(files: Record, directories: readonly string[] = []): string { - const root = mkdtempSync(join(tmpdir(), 'dsh-pre-merge-')) - for (const directory of directories) mkdirSync(join(root, directory), { recursive: true }) - for (const [path, version] of Object.entries(files)) { - const absolute = join(root, path) - mkdirSync(dirname(absolute), { recursive: true }) - writeFileSync(absolute, JSON.stringify({ name: 'x', version })) - } - return root -} +import { checkMode, failureDecision, preMergeCheck, preMergeSteps, type PreMergeStep } from './pre-merge-check.ts' /** * Steps that exit with the given codes, so the check runs without pnpm. @@ -30,50 +13,23 @@ function steps(codes: readonly number[]): PreMergeStep[] { return codes.map((code, index) => ({ name: `step-${index}`, command: process.execPath, args: ['-e', `process.exit(${code})`] })) } -/** What one run reported. */ -interface CheckRun { - readonly outcome: PreMergeOutcome - readonly lines: string[] -} - -/** - * Run the check on a repository and collect its lines. - * @param root - Repository root. - * @param codes - Exit code of each step. - * @returns Outcome and reported lines. - */ -async function check(root: string, codes: readonly number[]): Promise { - const lines: string[] = [] - const outcome = await preMergeCheck(root, { steps: steps(codes), report: line => void lines.push(line) }) - return { outcome, lines } -} - describe('pre-merge check', () => { - it('passes the steps and the version check when they all succeed', async () => { - const root = repository({ 'package.json': '1.0.0', 'packages/core/session/package.json': '1.0.0', 'apps/web/package.json': '1.0.0' }) - try { - const { outcome, lines } = await check(root, [0, 0, 0]) - expect(outcome).toEqual({ ok: true }) - expect(lines[0]).toBe('verify-version-sync: 2 released manifest(s) carry 1.0.0') - expect(lines.slice(1)).toEqual(['pre-merge-check: running step-0', 'pre-merge-check: running step-1', 'pre-merge-check: running step-2']) - } finally { - rmSync(root, { recursive: true, force: true }) - } + it('runs every step in order and passes when they all succeed', async () => { + const lines: string[] = [] + const outcome = await preMergeCheck(process.cwd(), { steps: steps([0, 0, 0]), report: line => void lines.push(line) }) + expect(outcome).toEqual({ ok: true }) + expect(lines).toEqual(['pre-merge-check: running step-0', 'pre-merge-check: running step-1', 'pre-merge-check: running step-2']) }) it('stops at the first step that fails', async () => { - const root = repository({ 'package.json': '1.0.0', 'packages/core/session/package.json': '1.0.0', 'apps/web/package.json': '1.0.0' }) - try { - const { outcome, lines } = await check(root, [1, 0]) - expect(outcome).toEqual({ ok: false, reason: 'the step-0 step failed' }) - expect(lines).not.toContain('pre-merge-check: running step-1') - } finally { - rmSync(root, { recursive: true, force: true }) - } + const lines: string[] = [] + const outcome = await preMergeCheck(process.cwd(), { steps: steps([1, 0]), report: line => void lines.push(line) }) + expect(outcome).toEqual({ ok: false, reason: 'the step-0 step failed' }) + expect(lines).not.toContain('pre-merge-check: running step-1') }) it('reports a command that cannot start', async () => { - const root = repository({ 'package.json': '1.0.0', 'packages/core/session/package.json': '1.0.0', 'apps/web/package.json': '1.0.0' }) + const root = mkdtempSync(join(tmpdir(), 'dsh-pre-merge-')) try { const outcome = await preMergeCheck(root, { steps: [{ name: 'missing', command: join(root, 'no-such-command'), args: [] }], @@ -86,26 +42,8 @@ describe('pre-merge check', () => { } }) - it('fails a tree whose released manifests carry another version', async () => { - const root = repository({ 'package.json': '2.0.0', 'packages/core/session/package.json': '1.0.0', 'apps/web/package.json': '2.0.0' }) - try { - const { outcome } = await check(root, [0, 0]) - expect(outcome).toEqual({ ok: false, reason: 'the released manifests do not carry the root version' }) - } finally { - rmSync(root, { recursive: true, force: true }) - } - }) - - it('fails when a manifest directory cannot be read', async () => { - const root = repository({ 'package.json': '1.0.0', 'packages/core/session/package.json': '1.0.0' }) - try { - const { outcome, lines } = await check(root, [0]) - expect(outcome.ok).toBe(false) - expect(outcome.reason).toContain('cannot read the released manifests') - expect(lines[0]).toContain('cannot read the released manifests') - } finally { - rmSync(root, { recursive: true, force: true }) - } + it('checks the versions before it installs, and installs before the builds', () => { + expect(preMergeSteps('/repo').map(step => step.name)).toEqual(['versions', 'install', 'host contracts', 'client']) }) it('annotates a failure as a warning and passes in report mode', () => { @@ -124,8 +62,4 @@ describe('pre-merge check', () => { }) expect(failureDecision('off', 'the client step failed').exitCode).toBe(0) }) - - it('installs before it builds the host contracts and the client', () => { - expect(preMergeSteps().map(step => step.name)).toEqual(['install', 'host contracts', 'client']) - }) }) diff --git a/scripts/pre-merge-check.ts b/scripts/pre-merge-check.ts index 697be8b5aa..832cac8735 100644 --- a/scripts/pre-merge-check.ts +++ b/scripts/pre-merge-check.ts @@ -1,17 +1,16 @@ /** - * Validate the tree a pull request produces once merged with its base branch: the install, the - * version synchronization of the released manifests, and the Host and Client builds. + * Validate the tree a pull request produces once merged with its base branch: the version + * synchronization of the released manifests, the install, and the Host and Client builds. * * The `Policy / Pre-Merge Check` job runs this file with `node`, and knows nothing else about the * merged tree, so what that tree needs changes here without touching the check. The file runs under - * plain Node and spawns pnpm itself, so no dependency is installed before it starts. + * plain Node and spawns its steps itself, so no dependency is installed before it starts. */ import { spawn } from 'node:child_process' -import { resolve } from 'node:path' +import { join, resolve } from 'node:path' import process from 'node:process' import { pathToFileURL } from 'node:url' -import { describeVersions, readVersions, versionsSynchronized, type VersionReport } from './verify-version-sync.ts' /** One command of the check. */ export interface PreMergeStep { @@ -21,23 +20,21 @@ export interface PreMergeStep { readonly args: readonly string[] } -/** No step can run before the others without the install, so it opens the list. */ -export function preMergeSteps(): PreMergeStep[] { +/** + * The steps that check the merged tree, in order. The versions come first because reading the + * manifests needs no install, so a mistyped release stops the check before anything is built. + * @param root - Repository root. + * @returns Steps in run order. + */ +export function preMergeSteps(root: string): PreMergeStep[] { return [ + { name: 'versions', command: process.execPath, args: [join(root, 'scripts', 'verify-version-sync.ts')] }, { name: 'install', command: 'pnpm', args: ['install', '--frozen-lockfile'] }, { name: 'host contracts', command: 'pnpm', args: ['exec', 'tsx', 'scripts/compile-referenced-projects.ts', 'host-contracts'] }, { name: 'client', command: 'pnpm', args: ['exec', 'tsx', 'scripts/compile-referenced-projects.ts', 'client'] }, ] } -/** Options of the check. */ -export interface PreMergeOptions { - /** Sink for the check's own lines. */ - readonly report?: (line: string) => void - /** Steps to run; the repository's own steps when absent. */ - readonly steps?: readonly PreMergeStep[] -} - /** How the job that runs this file reacts to a failure. */ export type CheckMode = 'off' | 'report' | 'block' @@ -69,6 +66,14 @@ export function failureDecision(mode: CheckMode, reason: string): FailureDecisio return { line: `pre-merge-check: ${summary}`, exitCode: 0 } } +/** Options of the check. */ +export interface PreMergeOptions { + /** Sink for the check's own lines. */ + readonly report?: (line: string) => void + /** Steps to run; the repository's own steps when absent. */ + readonly steps?: readonly PreMergeStep[] +} + /** What the check decided. */ export interface PreMergeOutcome { readonly ok: boolean @@ -95,7 +100,7 @@ function run(step: PreMergeStep, root: string): Promise { } /** - * Check the merged tree, reporting why it failed. + * Run every step of the merged tree in order, stopping at the first failure. * @param root - Repository root. * @param options - Sink for the check's own lines, and the steps to run. * @returns Whether the tree passed, with the reason when it did not. @@ -104,23 +109,11 @@ export async function preMergeCheck(root: string, options: PreMergeOptions = {}) const report = options.report ?? ((line: string): void => { process.stdout.write(`${line}\n`) }) - // The manifests are read before the steps, so the version check needs no installed dependency. - let versions: VersionReport | undefined - let versionsError = 'unknown error' - try { - versions = readVersions(root) - for (const line of describeVersions(versions)) report(`verify-version-sync: ${line}`) - } catch (error) { - versionsError = error instanceof Error ? error.message : String(error) - report(`verify-version-sync: cannot read the released manifests: ${versionsError}`) - } - for (const step of options.steps ?? preMergeSteps()) { + for (const step of options.steps ?? preMergeSteps(root)) { report(`pre-merge-check: running ${step.name}`) const failure = await run(step, root) if (failure !== undefined) return { ok: false, reason: failure } } - if (versions === undefined) return { ok: false, reason: `cannot read the released manifests: ${versionsError}` } - if (!versionsSynchronized(versions)) return { ok: false, reason: 'the released manifests do not carry the root version' } return { ok: true } }