| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import { jobOrder, parseWorkflow, type Workflow } from "@diffinery/workflow-yaml"; |
| 3 | import { ReusableError, expandReusable, parseCallRef, type Loader } from "../../src/repo/reusable.js"; |
| 4 | |
| 5 | const wf = (yaml: string, path = ".github/workflows/x.yml"): Workflow => { |
| 6 | const { workflow, errors } = parseWorkflow(yaml, path); |
| 7 | if (!workflow) throw new Error(errors.map((e) => e.message).join("; ")); |
| 8 | return workflow; |
| 9 | }; |
| 10 | |
| 11 | const LIB = wf(`on: |
| 12 | workflow_call: |
| 13 | inputs: |
| 14 | who: { type: string } |
| 15 | jobs: |
| 16 | a: |
| 17 | runs-on: ubuntu-latest |
| 18 | steps: [{ run: echo a }] |
| 19 | b: |
| 20 | needs: a |
| 21 | runs-on: ubuntu-latest |
| 22 | steps: [{ run: echo b }] |
| 23 | `); |
| 24 | |
| 25 | const files: Record<string, Workflow> = { "./.github/workflows/lib.yml": LIB }; |
| 26 | const load: Loader = async (uses, from) => { |
| 27 | const w = files[uses]; |
| 28 | if (!w) throw new Error(`${uses} not found`); |
| 29 | return { workflow: w, source: from }; |
| 30 | }; |
| 31 | const root = { repoId: null, sha: "abc" }; |
| 32 | |
| 33 | describe("expandReusable", () => { |
| 34 | it("flattens a call into prefixed jobs and orders around it", async () => { |
| 35 | const main = wf(`on: push |
| 36 | jobs: |
| 37 | first: |
| 38 | runs-on: ubuntu-latest |
| 39 | steps: [{ run: echo }] |
| 40 | call: |
| 41 | needs: first |
| 42 | uses: ./.github/workflows/lib.yml |
| 43 | with: { who: me } |
| 44 | last: |
| 45 | needs: call |
| 46 | runs-on: ubuntu-latest |
| 47 | steps: [{ run: echo }] |
| 48 | `); |
| 49 | const exp = await expandReusable(main, root, load); |
| 50 | expect(Object.keys(exp.workflow.jobs)).toEqual(["first", "call/a", "call/b", "last"]); |
| 51 | expect(exp.workflow.jobs["call/a"]!.needs).toEqual(["first"]); |
| 52 | expect(exp.workflow.jobs["call/b"]!.needs).toEqual(["call/a"]); |
| 53 | expect(exp.workflow.jobs["last"]!.needs.sort()).toEqual(["call/a", "call/b"]); |
| 54 | expect(exp.needs.get("last")).toEqual(["call"]); |
| 55 | expect(exp.parentOf.get("call/b")).toBe("call"); |
| 56 | expect(jobOrder(exp.workflow).order).toEqual([["first"], ["call/a"], ["call/b"], ["last"]]); |
| 57 | }); |
| 58 | |
| 59 | it("nests calls and stops at four levels", async () => { |
| 60 | const level = (n: number) => wf(`on: workflow_call |
| 61 | jobs: |
| 62 | j: |
| 63 | uses: ./.github/workflows/l${n + 1}.yml |
| 64 | `); |
| 65 | for (let i = 1; i <= 5; i++) files[`./.github/workflows/l${i}.yml`] = level(i); |
| 66 | const main = wf(`on: push |
| 67 | jobs: |
| 68 | top: |
| 69 | uses: ./.github/workflows/l1.yml |
| 70 | `); |
| 71 | await expect(expandReusable(main, root, load)).rejects.toThrow(ReusableError); |
| 72 | files["./.github/workflows/l3.yml"] = LIB; |
| 73 | const exp = await expandReusable(main, root, load); |
| 74 | expect(Object.keys(exp.workflow.jobs)).toEqual(["top/j/j/a", "top/j/j/b"]); |
| 75 | }); |
| 76 | |
| 77 | it("rejects unknown inputs and workflows without workflow_call", async () => { |
| 78 | files["./.github/workflows/plain.yml"] = wf(`on: push |
| 79 | jobs: |
| 80 | x: |
| 81 | runs-on: ubuntu-latest |
| 82 | steps: [{ run: echo }] |
| 83 | `); |
| 84 | const bad = (uses: string, w = "") => wf(`on: push |
| 85 | jobs: |
| 86 | c: |
| 87 | uses: ${uses} |
| 88 | ${w}`); |
| 89 | await expect(expandReusable(bad("./.github/workflows/plain.yml"), root, load)).rejects.toThrow(/not a reusable workflow/); |
| 90 | await expect(expandReusable(bad("./.github/workflows/lib.yml", " with: { nope: 1 }\n"), root, load)).rejects.toThrow(/no input named 'nope'/); |
| 91 | await expect(expandReusable(bad("./.github/workflows/missing.yml"), root, load)).rejects.toThrow(/not found/); |
| 92 | }); |
| 93 | }); |
| 94 | |
| 95 | describe("parseCallRef", () => { |
| 96 | it("parses local and remote references", () => { |
| 97 | expect(parseCallRef("./.github/workflows/a.yml")).toEqual({ kind: "local", path: ".github/workflows/a.yml" }); |
| 98 | expect(parseCallRef("octo/tools/.github/workflows/b.yaml@v1")).toEqual({ kind: "remote", owner: "octo", repo: "tools", path: ".github/workflows/b.yaml", ref: "v1" }); |
| 99 | expect(() => parseCallRef("octo/tools/.github/workflows/b.yaml")).toThrow(); |
| 100 | expect(parseCallRef("./.diffinery/workflows/c.yml")).toEqual({ kind: "local", path: ".diffinery/workflows/c.yml" }); |
| 101 | // A called workflow outside the workflow directories would dodge the guards on workflow changes. |
| 102 | expect(() => parseCallRef("./ci/build.yml")).toThrow(/workflows/); |
| 103 | expect(() => parseCallRef("./.github/workflows/../../ci/build.yml")).toThrow(/workflows/); |
| 104 | expect(() => parseCallRef("octo/tools/ci/build.yml@main")).toThrow(/workflows/); |
| 105 | expect(() => parseCallRef("octo/tools/.github/workflows/../ci/build.yml@main")).toThrow(/workflows/); |
| 106 | expect(parseCallRef("octo/tools/.diffinery/workflows/d.yml@main")).toEqual({ kind: "remote", owner: "octo", repo: "tools", path: ".diffinery/workflows/d.yml", ref: "main" }); |
| 107 | }); |
| 108 | }); |