#![allow(unused)]
use std::os::unix::fs::PermissionsExt;
use std::path::{Path, PathBuf};
use std::process::{Command, Output};
mod gh;
pub(crate) use gh::*;
pub(crate) const VERSION: &str = "0.0.2";
pub(crate) const TAG: &str = "v0.0.2";
pub(crate) const PREV_TAG: &str = "v0.0.1";
pub(crate) const PREV_VERSION: &str = "0.0.1";
pub(crate) const PREV_PREV_TAG: &str = "v0.0.0";
pub(crate) const PR: u32 = 77;
pub(crate) const HEAD_REF: &str = "feat/pr-77";
pub(crate) fn slug() -> String {
HEAD_REF.replace('/', "-")
}
pub(crate) fn git(cwd: &Path, args: &[&str]) -> Output {
let out = Command::new("git")
.args(args)
.current_dir(cwd)
.env("GIT_AUTHOR_NAME", "tester")
.env("GIT_AUTHOR_EMAIL", "tester@example.com")
.env("GIT_COMMITTER_NAME", "tester")
.env("GIT_COMMITTER_EMAIL", "tester@example.com")
.output()
.expect("git must run");
assert!(
out.status.success(),
"git {args:?} failed: {}",
String::from_utf8_lossy(&out.stderr)
);
out
}
pub(crate) fn stdout_of(out: &Output) -> String {
String::from_utf8_lossy(&out.stdout).trim().to_string()
}
pub(crate) fn write(root: &Path, rel: &str, body: &str) {
let p = root.join(rel);
std::fs::create_dir_all(p.parent().expect("a parent")).expect("mkdir");
std::fs::write(p, body).expect("write");
}
pub(crate) fn stub_gh(dir: &Path, name: &str, json: &str) -> String {
let p = dir.join(name);
std::fs::write(
&p,
format!("#!/usr/bin/env bash\ncat <<'FIXTURE_JSON'\n{json}\nFIXTURE_JSON\n"),
)
.expect("write stub");
std::fs::set_permissions(&p, std::fs::Permissions::from_mode(0o755)).expect("chmod");
p.to_string_lossy().into_owned()
}
pub(crate) fn good_receipt() -> &'static str {
r#"{"quorum": {"lanes": ["lane-a", "lane-b", "lane-c"], "judges": 3, "refuters_per_claim": 3, "claims_refuted": 1}, "evidence": {"files": [{"path": "x"}]}}"#
}
pub(crate) fn nested_override_receipt() -> &'static str {
r#"{"quorum": {"lanes": ["lane-a", "lane-b", "lane-c"], "judges": 3, "refuters_per_claim": 3, "claims_refuted": 1, "override": {"by": "author"}}, "evidence": {"files": [{"path": "x"}]}}"#
}
pub(crate) fn unhunted_receipt() -> &'static str {
r#"{"quorum": {"lanes": ["lane-a", "lane-b", "lane-c"], "judges": 3, "refuters_per_claim": 3, "claims_refuted": 0}, "evidence": {"files": [{"path": "x"}]}}"#
}
pub(crate) fn waived_receipt() -> &'static str {
r#"{"quorum": {"lanes": ["lane-a", "lane-b", "lane-c"], "judges": 3, "refuters_per_claim": 3, "claims_refuted": 1}, "evidence": {"files": [{"path": "x"}]}, "waived": {"reason": "no credits"}}"#
}
pub(crate) fn thin_receipt() -> &'static str {
r#"{"quorum": {"lanes": ["lane-a", "lane-b"], "judges": 3, "refuters_per_claim": 3, "claims_refuted": 1}, "evidence": {"files": [{"path": "x"}]}}"#
}
pub(crate) struct Fixture {
pub(crate) _dir: tempfile::TempDir,
pub(crate) root: PathBuf,
pub(crate) head: String,
}
impl Fixture {
pub(crate) fn gh_reporting_the_pr(&self) -> String {
stub_gh(
self.root.parent().expect("tempdir"),
"gh-one-pr",
&format!(
r#"[{{"number":{PR},"mergedAt":"2026-09-05T00:00:00Z","mergeCommit":{{"oid":"{}"}},"headRefName":"{HEAD_REF}"}}]"#,
self.head
),
)
}
}
pub(crate) struct Run {
pub(crate) code: i32,
pub(crate) text: String,
}
impl Run {
pub(crate) fn assert_not_green(&self, why: &str) {
assert_ne!(self.code, 0, "{why}; the gate exited 0:\n{}", self.text);
assert!(
!self.text.contains("GATE R PASS"),
"{why}; the gate printed a PASS line:\n{}",
self.text
);
assert!(
self.text.contains("GATE R FAIL"),
"{why}; the gate exited {} without a GATE R FAIL line, which is a \
death rather than a verdict:\n{}",
self.code,
self.text
);
}
pub(crate) fn assert_says(&self, needle: &str) {
assert!(
self.text.contains(needle),
"the verdict does not name {needle:?}, so a reader cannot act on it:\n{}",
self.text
);
}
}
pub(crate) fn run(fx: &Fixture, gh: &str) -> Run {
let out = Command::new("bash")
.arg(fx.root.join("scripts/dogfood/release-check.sh"))
.current_dir(&fx.root)
.env("GH", gh)
.output()
.expect("bash must run");
let mut text = String::from_utf8_lossy(&out.stdout).into_owned();
text.push_str(&String::from_utf8_lossy(&out.stderr));
Run {
code: out.status.code().unwrap_or(-1),
text,
}
}
pub(crate) fn fixture(tag_on_origin: bool, receipt: Option<&str>) -> Fixture {
let dir = tempfile::tempdir().expect("tempdir");
let root = dir.path().join("repo");
std::fs::create_dir_all(&root).expect("mkdir repo");
let nohooks = dir.path().join("nohooks");
std::fs::create_dir_all(&nohooks).expect("mkdir nohooks");
git(&root, &["init", "-q", "-b", "main"]);
git(
&root,
&["config", "core.hooksPath", &nohooks.to_string_lossy()],
);
git(&root, &["config", "user.email", "tester@example.com"]);
git(&root, &["config", "user.name", "tester"]);
git(&root, &["config", "commit.gpgsign", "false"]);
let script = std::fs::read_to_string(
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("scripts/dogfood/release-check.sh"),
)
.expect("the gate under test must exist");
write(&root, "scripts/dogfood/release-check.sh", &script);
let receipt_lib = std::fs::read_to_string(
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("scripts/dogfood/lib/receipt.sh"),
)
.expect(
"scripts/dogfood/lib/receipt.sh must exist — Arm 5 sources the shared receipt predicate",
);
write(&root, "scripts/dogfood/lib/receipt.sh", &receipt_lib);
let crux_script = std::fs::read_to_string(
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("scripts/dogfood/crux-reconcile.sh"),
)
.expect("scripts/dogfood/crux-reconcile.sh must exist — Arm 6 calls it directly");
write(&root, "scripts/dogfood/crux-reconcile.sh", &crux_script);
write(
&root,
"Cargo.toml",
&format!("[package]\nname = \"fixture\"\nversion = \"{VERSION}\"\n"),
);
write(
&root,
"CHANGELOG.md",
"## [Unreleased]\n\n**Fixture behaviour bullet.** Exercises the crux reconciliation for this fixture.\n",
);
write(
&root,
&format!("docs/audits/crux-{VERSION}.md"),
"# crux (fixture)\n\n| Fixture behaviour bullet. | Ansible, Terraform, Nix | matches |\n",
);
git(
&root,
&[
"add",
"scripts/dogfood/release-check.sh",
"scripts/dogfood/crux-reconcile.sh",
"Cargo.toml",
"CHANGELOG.md",
&format!("docs/audits/crux-{VERSION}.md"),
],
);
git(&root, &["commit", "-qm", "the previous release"]);
git(&root, &["tag", PREV_TAG]);
write(&root, "shipped.txt", "work that reached this release\n");
git(&root, &["add", "shipped.txt"]);
if let Some(body) = receipt {
write(&root, &format!(".quorum/{}.json", slug()), body);
git(&root, &["add", &format!(".quorum/{}.json", slug())]);
}
git(&root, &["commit", "-qm", "squash-merged work (#77)"]);
let head = stdout_of(&git(&root, &["rev-parse", "HEAD"]));
if tag_on_origin {
git(&root, &["tag", TAG]);
}
let origin = dir.path().join("origin.git");
git(
dir.path(),
&[
"clone",
"-q",
"--bare",
&root.to_string_lossy(),
&origin.to_string_lossy(),
],
);
git(
&root,
&["remote", "add", "origin", &origin.to_string_lossy()],
);
if tag_on_origin {
git(&root, &["tag", "-d", TAG]);
}
Fixture {
_dir: dir,
root,
head,
}
}
pub(crate) fn published_fixture() -> Fixture {
let dir = tempfile::tempdir().expect("tempdir");
let root = dir.path().join("repo");
std::fs::create_dir_all(&root).expect("mkdir repo");
let nohooks = dir.path().join("nohooks");
std::fs::create_dir_all(&nohooks).expect("mkdir nohooks");
git(&root, &["init", "-q", "-b", "main"]);
git(
&root,
&["config", "core.hooksPath", &nohooks.to_string_lossy()],
);
git(&root, &["config", "user.email", "tester@example.com"]);
git(&root, &["config", "user.name", "tester"]);
git(&root, &["config", "commit.gpgsign", "false"]);
for rel in [
"scripts/dogfood/release-check.sh",
"scripts/dogfood/lib/receipt.sh",
"scripts/dogfood/crux-reconcile.sh",
] {
let body = std::fs::read_to_string(PathBuf::from(env!("CARGO_MANIFEST_DIR")).join(rel))
.unwrap_or_else(|e| panic!("the gate under test must exist at {rel}: {e}"));
write(&root, rel, &body);
}
write(
&root,
"Cargo.toml",
&format!("[package]\nname = \"fixture\"\nversion = \"{PREV_VERSION}\"\n"),
);
write(&root, "CHANGELOG.md", "## [Unreleased]\n\n");
git(&root, &["add", "-A"]);
git(&root, &["commit", "-qm", "the release before this one"]);
git(&root, &["tag", PREV_PREV_TAG]);
write(&root, "shipped.txt", "work that reached this release\n");
write(&root, &format!(".quorum/{}.json", slug()), good_receipt());
git(&root, &["add", "-A"]);
git(&root, &["commit", "-qm", "squash-merged work (#77)"]);
let head = stdout_of(&git(&root, &["rev-parse", "HEAD"]));
git(&root, &["tag", PREV_TAG]);
let origin = dir.path().join("origin.git");
git(
dir.path(),
&[
"clone",
"-q",
"--bare",
&root.to_string_lossy(),
&origin.to_string_lossy(),
],
);
git(
&root,
&["remote", "add", "origin", &origin.to_string_lossy()],
);
git(&root, &["fetch", "-q", "origin"]);
Fixture {
_dir: dir,
root,
head,
}
}
pub(crate) fn stub_release_tools(dir: &Path, crate_version: &str, doc_status: &str) -> PathBuf {
let bin = dir.join("bin");
std::fs::create_dir_all(&bin).expect("mkdir bin");
std::fs::write(
bin.join("cargo"),
format!("#!/usr/bin/env bash\nprintf 'forjar = \"{crate_version}\" # fixture\\n'\n"),
)
.expect("write cargo stub");
std::fs::write(
bin.join("curl"),
format!("#!/usr/bin/env bash\nprintf '{{\"doc_status\": {doc_status}}}'\n"),
)
.expect("write curl stub");
for f in ["cargo", "curl"] {
std::fs::set_permissions(bin.join(f), std::fs::Permissions::from_mode(0o755))
.expect("chmod");
}
bin
}
pub(crate) fn run_published(fx: &Fixture, gh: &str, bin: &Path) -> Run {
let path = format!(
"{}:{}",
bin.display(),
std::env::var("PATH").unwrap_or_default()
);
let out = Command::new("bash")
.arg(fx.root.join("scripts/dogfood/release-check.sh"))
.current_dir(&fx.root)
.env("GH", gh)
.env("PATH", path)
.output()
.expect("bash must run");
let mut text = String::from_utf8_lossy(&out.stdout).into_owned();
text.push_str(&String::from_utf8_lossy(&out.stderr));
Run {
code: out.status.code().unwrap_or(-1),
text,
}
}
impl Run {
pub(crate) fn assert_green(&self, why: &str) {
assert_eq!(
self.code, 0,
"{why}; the gate exited {}:\n{}",
self.code, self.text
);
assert!(
self.text.contains("GATE R PASS"),
"{why}; exit 0 without a GATE R PASS line:\n{}",
self.text
);
}
pub(crate) fn assert_never_says(&self, needle: &str, why: &str) {
assert!(
!self.text.contains(needle),
"{why}; the verdict says {needle:?}:\n{}",
self.text
);
}
}