import { execFileSync } from "node:child_process"; import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { afterEach, beforeEach, describe, expect, it } from "vitest"; import { ALLOW_LABEL } from "../../tools/verifyGuard"; import { runVerifyGuard } from "../../tools/verifyGuardCli"; /** P6-8.b without the GitLab part: a real git history, the same code path the CI job uses. */ const SCRIPT = `test "t" start http://127.0.0.1:4780/x.html step "send" click text:"Send" verify text:"Sent" verify css:"#out" text:"Balance: 99" `; let dir = ""; const git = (...a: string[]) => execFileSync("git", a, { cwd: dir, encoding: "utf8" }).trim(); const write = (p: string, text: string) => { mkdirSync(join(dir, p, ".."), { recursive: true }); writeFileSync(join(dir, p), text); }; const commit = (msg: string) => { git("add", "-A"); git("commit", "-q", "-m", msg); return git("rev-parse", "HEAD"); }; const guard = (base: string, labels = "") => runVerifyGuard(["--base", base, "--labels", labels], {}, dir); beforeEach(() => { dir = mkdtempSync(join(tmpdir(), "aite-guard-")); git("init", "-q", "-b", "main"); git("config", "user.email", "t@aite.invalid"); git("config", "user.name", "t"); }); afterEach(() => rmSync(dir, { recursive: true, force: true })); describe("verify guard on a real repository", () => { it("a normal edit passes", () => { write("scripts/a.aite", SCRIPT); const base = commit("base"); write("scripts/a.aite", SCRIPT.replace('click text:"Send"', 'click text:"Go"') + ' verify text:"More"\n'); write("README.md", "hi"); commit("edit"); expect(guard(base)).toMatchObject({ exitCode: 0 }); }); it("removing a verify fails, names the file, and the label lets it pass", () => { write("scripts/a.aite", SCRIPT); const base = commit("base"); write("scripts/a.aite", SCRIPT.replace(' verify text:"Sent"\n', "")); commit("weaken"); const r = guard(base); expect(r.exitCode).toBe(1); expect(r.output).toContain("scripts/a.aite:"); expect(guard(base, `bug, ${ALLOW_LABEL}`).exitCode).toBe(0); expect(guard(base, "bug").exitCode).toBe(1); }); it("a renamed file with a weaker verify fails; a renamed file otherwise passes; a deleted script fails", () => { write("scripts/a.aite", SCRIPT); write("scripts/gone.aite", SCRIPT); const base = commit("base"); git("mv", "scripts/a.aite", "scripts/b.aite"); git("rm", "-q", "scripts/gone.aite"); commit("move"); const r = guard(base); expect(r.exitCode).toBe(1); expect(r.output).toContain("gone.aite"); expect(r.output).not.toContain("b.aite:"); }); it("a script added in the change has nothing to compare", () => { write("README.md", "x"); const base = commit("base"); write("scripts/new.aite", SCRIPT); commit("add"); expect(guard(base).exitCode).toBe(0); }); it("without a base, or with one git does not know, it says it could not run (exit 3), never passes", () => { write("README.md", "x"); commit("base"); expect(runVerifyGuard([], {}, dir).exitCode).toBe(3); expect(guard("deadbeefdeadbeef").exitCode).toBe(3); }); it("reads the base and the labels from GitLab's variables", () => { write("scripts/a.aite", SCRIPT); const base = commit("base"); write("scripts/a.aite", SCRIPT.replace(' verify text:"Sent"\n', "")); commit("weaken"); const env = { CI_MERGE_REQUEST_DIFF_BASE_SHA: base }; expect(runVerifyGuard([], env, dir).exitCode).toBe(1); expect(runVerifyGuard([], { ...env, CI_MERGE_REQUEST_LABELS: `a,${ALLOW_LABEL}` }, dir).exitCode).toBe(0); }); });