use std::io::ErrorKind;
use std::path::{Path, PathBuf};
fn repo_root() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR"))
.ancestors()
.nth(2)
.expect("repo root is two levels above crates/roteiro")
.to_path_buf()
}
fn is_repository_checkout() -> bool {
let manifest = repo_root().join("Cargo.toml");
match std::fs::read_to_string(&manifest) {
Ok(text) => text.lines().any(|line| line.trim() == "[workspace]"),
Err(e) if e.kind() == ErrorKind::NotFound => false,
Err(e) => panic!(
"cannot read {} ({:?}: {e}). Without it this test cannot tell a \
packaged crate from a repository checkout, and guessing would make \
the guard skip in silence.",
manifest.display(),
e.kind(),
),
}
}
fn repo_file(rel: &str) -> Option<String> {
let path = repo_root().join(rel);
match std::fs::read_to_string(&path) {
Ok(text) => Some(text),
Err(e) if e.kind() == ErrorKind::NotFound && !is_repository_checkout() => None,
Err(e) => panic!(
"cannot read {} ({:?}: {e}). This guard asserts a property of that \
file, so skipping here would be a green that means \"could not \
look\". If the file moved or was deliberately deleted, update the \
list in this test in the same change.",
path.display(),
e.kind(),
),
}
}
fn ci_without_comments() -> Option<String> {
Some(
repo_file(".github/workflows/ci.yml")?
.lines()
.filter(|l| !l.trim_start().starts_with('#'))
.collect::<Vec<_>>()
.join("\n"),
)
}
#[test]
fn ci_measures_coverage_without_gating_it() {
let Some(ci) = ci_without_comments() else {
return; };
assert!(
ci.contains("cargo llvm-cov"),
"CI must still measure coverage: the docs say it does, and the whole \
point of #319 is that the docs stopped being true. If coverage \
measurement is being removed, remove the claim in \
docs/BUILD_PLAN.md §11 in the same change."
);
assert!(
ci.contains("continue-on-error: true"),
"the coverage job must stay non-blocking while the docs say the 85% \
floor is NOT enforced. Making it blocking is fine — but rewrite \
docs/BUILD_PLAN.md §11 in the same change, or the docs go back to \
describing a pipeline that does not exist."
);
}
#[test]
fn no_document_claims_the_coverage_floor_is_enforced() {
const FALSE_CLAIMS: &[&str] = &[
"**Coverage ratchet:** `cargo-llvm-cov` in CI, 85% per-file floor",
"85% per-file coverage ratchet, clippy",
"Coverage ratchet held at 85%",
"| Coverage | 85% per-file ratchet |",
];
const DOCS: &[&str] = &[
"docs/BUILD_PLAN.md",
"docs/BUILD_PLAN_V2.md",
"docs/adr/0001-build-roteiro-unified-codebase-knowledge-graph.md",
];
for doc in DOCS {
let Some(text) = repo_file(doc) else { continue };
for claim in FALSE_CLAIMS {
assert!(
!text.contains(claim),
"{doc} states {claim:?}, which asserts an enforced coverage gate. \
CI measures coverage and does not enforce a floor — say that, or \
make the gate real first."
);
}
}
}
#[test]
fn the_build_plan_records_what_is_actually_enforced() {
let Some(plan) = repo_file("docs/BUILD_PLAN.md") else {
return;
};
for gate in [
"cargo fmt --check",
"cargo clippy",
"cargo test --workspace",
"roteiro check",
"cargo audit",
"cargo deny",
] {
assert!(
plan.contains(gate),
"docs/BUILD_PLAN.md must list `{gate}` among the enforcing gates"
);
}
assert!(
plan.contains("87.51%"),
"the measured workspace baseline belongs in the plan — it is the input \
to deciding whether to switch a floor on"
);
}