|
1 | | -import { expect, test } from "bun:test" |
| 1 | +import { describe, expect, test } from "bun:test" |
2 | 2 | import path from "path" |
3 | 3 |
|
4 | 4 | // The compiled binary embeds the Bun that built it, so every toolchain entry |
5 | 5 | // point must take the version from the root package.json `packageManager`. |
6 | 6 | const root = path.resolve(import.meta.dir, "../../../..") |
7 | 7 | const read = (file: string) => Bun.file(path.join(root, file)).text() |
8 | 8 |
|
| 9 | +type Step = { uses?: string; with?: Record<string, string> } |
| 10 | +type Workflow = { jobs: Record<string, { uses?: string; steps?: Step[] }> } |
| 11 | + |
| 12 | +const workflowFiles = Array.from(new Bun.Glob(".github/workflows/*.{yml,yaml}").scanSync({ cwd: root })).sort() |
| 13 | +const workflows = await Promise.all( |
| 14 | + workflowFiles.map(async (file) => ({ file, workflow: Bun.YAML.parse(await read(file)) as Workflow })), |
| 15 | +) |
| 16 | +const steps = (workflow: Workflow) => Object.values(workflow.jobs).flatMap((job) => job.steps ?? []) |
| 17 | +const isSetupBun = (step: Step) => step.uses?.startsWith("oven-sh/setup-bun@") ?? false |
| 18 | +const bunWorkflows = workflows.filter(({ workflow }) => steps(workflow).some(isSetupBun)) |
| 19 | + |
9 | 20 | test("packageManager pins an exact Bun version", async () => { |
10 | 21 | const pkg = JSON.parse(await read("package.json")) |
11 | 22 | expect(pkg.packageManager).toMatch(/^bun@\d+\.\d+\.\d+$/) |
12 | 23 | }) |
13 | 24 |
|
14 | | -test.each([".github/workflows/ci.yml", ".github/workflows/publish.yml"])( |
15 | | - "%s installs Bun from package.json", |
16 | | - async (file) => { |
17 | | - const workflow = Bun.YAML.parse(await read(file)) as { |
18 | | - jobs: Record<string, { steps: { uses?: string; with?: Record<string, string> }[] }> |
19 | | - } |
20 | | - const setups = Object.values(workflow.jobs) |
21 | | - .flatMap((job) => job.steps) |
22 | | - .filter((step) => step.uses?.startsWith("oven-sh/setup-bun@")) |
23 | | - expect(setups.length).toBeGreaterThan(0) |
24 | | - for (const step of setups) { |
| 25 | +test("CI and release workflows install Bun", () => { |
| 26 | + expect(bunWorkflows.map(({ file }) => file)).toEqual( |
| 27 | + expect.arrayContaining([".github/workflows/ci.yml", ".github/workflows/publish.yml"]), |
| 28 | + ) |
| 29 | +}) |
| 30 | + |
| 31 | +describe.each(bunWorkflows)("$file", ({ workflow }) => { |
| 32 | + test("installs Bun from package.json", () => { |
| 33 | + for (const step of steps(workflow).filter(isSetupBun)) { |
25 | 34 | expect(step.with).toEqual({ "bun-version-file": "package.json" }) |
26 | 35 | } |
27 | | - }, |
28 | | -) |
| 36 | + }) |
| 37 | + |
| 38 | + test("pins every action to a commit SHA", () => { |
| 39 | + const uses = [ |
| 40 | + ...Object.values(workflow.jobs).flatMap((job) => (job.uses ? [job.uses] : [])), |
| 41 | + ...steps(workflow).flatMap((step) => (step.uses ? [step.uses] : [])), |
| 42 | + ] |
| 43 | + for (const ref of uses.filter((ref) => !ref.startsWith("./"))) { |
| 44 | + expect(ref).toMatch(/@[0-9a-f]{40}$/) |
| 45 | + } |
| 46 | + }) |
| 47 | +}) |
29 | 48 |
|
30 | 49 | test("bun-node image has no Bun version of its own", async () => { |
31 | 50 | const dockerfile = await read("packages/containers/bun-node/Dockerfile") |
32 | 51 | expect(dockerfile).toMatch(/^ARG BUN_VERSION$/m) |
| 52 | + expect(dockerfile).not.toMatch(/^ARG\s+BUN_VERSION=/m) |
| 53 | + expect(dockerfile).toContain('"${BUN_VERSION:?') |
33 | 54 | }) |
0 commit comments