Files
piik/tests/build-workspace.test.ts
T

34 lines
1.7 KiB
TypeScript

import { execFileSync } from "node:child_process";
import { mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { expect, it } from "vitest";
import { assertCleanRevision } from "../scripts/build-workspace.mjs";
it("binds a package revision to its exact clean checkout", () => {
const root = realpathSync(mkdtempSync(join(tmpdir(), "piik-build-workspace-")));
const git = (...args: string[]) => execFileSync("git", ["-C", root, ...args], { encoding: "utf8" }).trim();
try {
git("init", "--quiet");
expect(realpathSync(git("rev-parse", "--show-toplevel"))).toBe(root);
writeFileSync(join(root, ".gitignore"), "/build/\n");
writeFileSync(join(root, "source.txt"), "checkout source\n");
git("add", "--", ".gitignore", "source.txt");
git("-c", "user.name=Piik Test", "-c", "user.email=test@example.invalid",
"-c", "commit.gpgsign=false", "-c", "core.hooksPath=.git/hooks", "commit", "--quiet", "-m", "Fixture");
const revision = git("rev-parse", "HEAD");
const archive = join(root, "build", "archive");
mkdirSync(archive, { recursive: true });
writeFileSync(join(archive, "source.txt"), "archived source\n");
expect(assertCleanRevision(root, revision)).toBe(revision);
expect(() => assertCleanRevision(archive, revision)).toThrow("Git checkout root");
git("config", "--local", "status.showUntrackedFiles", "no");
writeFileSync(join(root, "new-source.txt"), "new source\n");
expect(() => assertCleanRevision(root, revision)).toThrow("clean Git checkout");
} finally {
rmSync(root, { recursive: true, force: true });
}
});