#![forbid(unsafe_code)]
use std::fs;
use std::path::PathBuf;
const RUNNER: &str = "check_all_gates.sh";
const EXCLUSIONS_OPEN: &str = "# Declared non-gates";
const EXCLUSIONS_CLOSE: &str = "# Sequential by design";
const GATES_OPEN: &str = "GATES=(";
fn workspace_root() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
}
fn read(rel: &str) -> String {
fs::read_to_string(workspace_root().join(rel)).unwrap_or_else(|e| panic!("read {rel}: {e}"))
}
fn shell_scripts() -> Vec<String> {
let dir = workspace_root().join("scripts");
let entries = fs::read_dir(&dir).unwrap_or_else(|e| panic!("read_dir scripts: {e}"));
let mut names = Vec::with_capacity(16);
for entry in entries {
let name = entry.expect("dir entry").file_name();
let name = name.to_string_lossy().into_owned();
if name.ends_with(".sh") {
names.push(name);
}
}
names.sort_unstable();
names
}
fn split_runner(runner: &str) -> (&str, &str) {
let at = runner
.find(GATES_OPEN)
.unwrap_or_else(|| panic!("runner must declare its gates in a `{GATES_OPEN}` array"));
runner.split_at(at)
}
fn exclusions_block(header: &str) -> &str {
let open = header.find(EXCLUSIONS_OPEN).unwrap_or_else(|| {
panic!(
"the runner must carry a `{EXCLUSIONS_OPEN}` block: a battery that omits \
scripts in silence reads as total coverage"
)
});
let rest = &header[open..];
let close = rest.find(EXCLUSIONS_CLOSE).unwrap_or_else(|| {
panic!("the `{EXCLUSIONS_OPEN}` block must be terminated by `{EXCLUSIONS_CLOSE}`")
});
&rest[..close]
}
#[test]
fn every_check_script_is_a_gate_in_the_runner() {
let runner = read("scripts/check_all_gates.sh");
let (_, gate_region) = split_runner(&runner);
let mut missing = Vec::new();
for name in shell_scripts() {
if !name.starts_with("check_") || name == RUNNER {
continue;
}
if !gate_region.contains(&name) {
missing.push(name);
}
}
assert!(
missing.is_empty(),
"these check scripts exist but no gate in scripts/{RUNNER} runs them: {missing:?} — \
a gate outside the battery is a gate nobody executes"
);
}
#[test]
fn every_script_is_either_a_gate_or_a_declared_non_gate() {
let runner = read("scripts/check_all_gates.sh");
let (header, gate_region) = split_runner(&runner);
let declared = exclusions_block(header);
let mut unaccounted = Vec::new();
let mut ambiguous = Vec::new();
for name in shell_scripts() {
if name == RUNNER {
continue;
}
let is_gate = gate_region.contains(&name);
let is_excluded = declared.contains(&name);
match (is_gate, is_excluded) {
(false, false) => unaccounted.push(name),
(true, true) => ambiguous.push(name),
_ => {}
}
}
assert!(
unaccounted.is_empty(),
"these scripts are neither a gate in scripts/{RUNNER} nor a declared non-gate: \
{unaccounted:?} — add them to the battery, or name them in the \
`{EXCLUSIONS_OPEN}` block with the reason they are excluded"
);
assert!(
ambiguous.is_empty(),
"these scripts are both a gate and a declared exclusion, so the declaration \
contradicts the battery: {ambiguous:?}"
);
}
#[test]
fn the_declared_exclusions_name_each_script() {
let runner = read("scripts/check_all_gates.sh");
let (header, _) = split_runner(&runner);
let declared = exclusions_block(header);
for name in [
"dist_multiarch.sh",
"generate_sbom.sh",
"release_attest.sh",
"e2e_real_ssh.sh",
] {
assert!(
declared.contains(name),
"the declared-exclusions block must name {name}: an unnamed omission is \
indistinguishable from an oversight"
);
}
}
#[test]
fn the_test_gate_stays_locked() {
let runner = read("scripts/check_all_gates.sh");
let (_, gate_region) = split_runner(&runner);
let test_gate = gate_region
.lines()
.find(|line| line.trim_start().starts_with("\"test|"))
.expect("the battery must contain a `test` gate");
assert!(
test_gate.contains("--locked"),
"the test gate must pass --locked so the battery measures the pinned graph: {test_gate}"
);
}
#[test]
fn the_runner_reports_what_it_skipped() {
let runner = read("scripts/check_all_gates.sh");
assert!(
runner.contains("skipped"),
"the summary record must report skipped gates: --only would otherwise produce \
a green board indistinguishable from a full run"
);
}
#[test]
fn the_battery_is_declared_in_both_languages_of_the_maintainer_docs() {
for doc in [
"CONTRIBUTING.md",
"CONTRIBUTING.pt-BR.md",
"docs/TESTING.md",
"docs/TESTING.pt-BR.md",
"docs/RELEASE_CHECKLIST.md",
"docs/RELEASE_CHECKLIST.pt-BR.md",
] {
let text = read(doc);
assert!(
text.contains("check_all_gates.sh"),
"{doc} must tell the reader to run the gate battery: an undocumented \
runner is the same blind spot as no runner at all"
);
}
}
#[test]
fn the_advisory_freshness_gate_has_a_documented_home() {
for doc in [
"docs/TESTING.md",
"docs/TESTING.pt-BR.md",
"docs/RELEASE_CHECKLIST.md",
"docs/RELEASE_CHECKLIST.pt-BR.md",
] {
let text = read(doc);
assert!(
text.contains("check_advisory_freshness.sh"),
"{doc} must name the advisory freshness gate: the battery is its only \
consumer, so an undocumented battery hides it completely"
);
}
}