|
3 | 3 | * Copyright 2026 Autohand AI LLC |
4 | 4 | * SPDX-License-Identifier: Apache-2.0 |
5 | 5 | */ |
6 | | -import { readFileSync } from 'node:fs'; |
| 6 | +import { readdirSync, readFileSync } from 'node:fs'; |
7 | 7 | import path from 'node:path'; |
8 | 8 | import { describe, expect, it } from 'vitest'; |
9 | 9 |
|
10 | | -const WORKFLOW_PATH = path.resolve(import.meta.dirname, '../../.github/workflows/ci.yml'); |
| 10 | +const WORKFLOW_DIR = path.resolve(import.meta.dirname, '../../.github/workflows'); |
11 | 11 |
|
12 | | -function readWorkflow(): string { |
13 | | - return readFileSync(WORKFLOW_PATH, 'utf8'); |
| 12 | +interface WorkflowJob { |
| 13 | + workflow: string; |
| 14 | + name: string; |
| 15 | + body: string; |
| 16 | +} |
| 17 | + |
| 18 | +/** Split every workflow into its top-level jobs, keyed by `<file>:<job>`. */ |
| 19 | +function readWorkflowJobs(): WorkflowJob[] { |
| 20 | + const jobs: WorkflowJob[] = []; |
| 21 | + |
| 22 | + for (const file of readdirSync(WORKFLOW_DIR).filter((name) => name.endsWith('.yml'))) { |
| 23 | + const contents = readFileSync(path.join(WORKFLOW_DIR, file), 'utf8'); |
| 24 | + const jobsIndex = contents.indexOf('\njobs:'); |
| 25 | + if (jobsIndex === -1) continue; |
| 26 | + |
| 27 | + const body = contents.slice(jobsIndex); |
| 28 | + // Job names sit at exactly two spaces of indentation under `jobs:`. |
| 29 | + const headers = [...body.matchAll(/^ {2}([A-Za-z0-9_-]+):$/gmu)]; |
| 30 | + for (const [index, header] of headers.entries()) { |
| 31 | + const start = header.index!; |
| 32 | + const end = headers[index + 1]?.index ?? body.length; |
| 33 | + jobs.push({ workflow: file, name: header[1]!, body: body.slice(start, end) }); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + return jobs; |
| 38 | +} |
| 39 | + |
| 40 | +function hasFullHistoryCheckout(job: WorkflowJob): boolean { |
| 41 | + return /actions\/checkout@v\d+\s*\n\s*with:\s*\n(?:\s*#[^\n]*\n)*\s*fetch-depth:\s*0/u.test(job.body); |
14 | 42 | } |
15 | 43 |
|
16 | 44 | /** |
17 | 45 | * Regression: the Tuistory suite asserts the CLI renders the latest stable |
18 | 46 | * release tag, which it discovers with `git tag --merged HEAD`. actions/checkout |
19 | | - * fetches no tags by default, so the job failed in CI while passing locally |
20 | | - * against a full clone. |
| 47 | + * fetches no tags by default, so those jobs failed in CI while passing locally |
| 48 | + * against a full clone. This was originally fixed in ci.yml alone, and the |
| 49 | + * release workflow kept failing because it runs the same suite from its own job. |
21 | 50 | */ |
22 | 51 | describe('CI workflow checkout', () => { |
23 | | - it('fetches full history for the job that runs the built terminal tests', () => { |
24 | | - const workflow = readWorkflow(); |
25 | | - const testJob = workflow.slice( |
26 | | - workflow.indexOf(' test:'), |
27 | | - workflow.indexOf(' build-test:'), |
28 | | - ); |
29 | | - |
30 | | - expect(testJob).toContain('bun run test:tuistory'); |
31 | | - expect(testJob).toMatch(/actions\/checkout@v\d+\s*\n\s*with:\s*\n(?:\s*#[^\n]*\n)*\s*fetch-depth:\s*0/); |
| 52 | + it('finds at least one job running the built terminal tests', () => { |
| 53 | + const tuistoryJobs = readWorkflowJobs().filter((job) => job.body.includes('test:tuistory')); |
| 54 | + expect(tuistoryJobs.length).toBeGreaterThan(0); |
32 | 55 | }); |
33 | 56 |
|
34 | | - it('keeps every checkout in the workflow pinned to a major version', () => { |
35 | | - const checkouts = readWorkflow().match(/actions\/checkout@v\d+/g) ?? []; |
| 57 | + it('fetches full history in every job that runs the built terminal tests', () => { |
| 58 | + const offenders = readWorkflowJobs() |
| 59 | + .filter((job) => job.body.includes('test:tuistory')) |
| 60 | + .filter((job) => !hasFullHistoryCheckout(job)) |
| 61 | + .map((job) => `${job.workflow}:${job.name}`); |
| 62 | + |
| 63 | + expect(offenders).toEqual([]); |
| 64 | + }); |
36 | 65 |
|
37 | | - expect(checkouts.length).toBeGreaterThan(0); |
38 | | - for (const checkout of checkouts) { |
39 | | - expect(checkout).toMatch(/actions\/checkout@v\d+$/); |
| 66 | + it('keeps every checkout pinned to a major version', () => { |
| 67 | + for (const file of readdirSync(WORKFLOW_DIR).filter((name) => name.endsWith('.yml'))) { |
| 68 | + const contents = readFileSync(path.join(WORKFLOW_DIR, file), 'utf8'); |
| 69 | + for (const checkout of contents.match(/actions\/checkout@[^\s]+/gu) ?? []) { |
| 70 | + expect(checkout, `${file} pins ${checkout}`).toMatch(/actions\/checkout@v\d+$/u); |
| 71 | + } |
40 | 72 | } |
41 | 73 | }); |
42 | 74 | }); |
0 commit comments