//! Coverage policy tests drive repository scripts with independently built evidence. use std::fs; use std::path::{Path, PathBuf}; use std::process::{Command, Output}; use std::sync::atomic::{AtomicU64, Ordering}; use std::time::{SystemTime, UNIX_EPOCH}; static NEXT_FIXTURE: AtomicU64 = AtomicU64::new(0); const SOURCE: &str = "crates/console/probe/src/"; const CONFIG: &str = r#"{ "version": 1, "domains": { "core": { "baseline_percent": 80, "paths": ["crates/console/probe/src/"], "exclude_paths": ["crates/console/probe/src/bank"] }, "bank": { "baseline_percent": 90, "paths": ["crates/console/probe/src/bank"] } } }"#; fn repository() -> PathBuf { Path::new(env!("CARGO_MANIFEST_DIR")).join("../../..").canonicalize().unwrap() } struct Fixture(PathBuf); impl Fixture { fn new() -> Self { let nonce = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_nanos(); let root = std::env::temp_dir().join(format!( "prometeu-coverage-{}-{nonce}-{}", std::process::id(), NEXT_FIXTURE.fetch_add(1, Ordering::Relaxed) )); fs::create_dir(&root).unwrap(); let this = Self(root); for name in ["main.rs", "bank.rs", "bank/cache.rs"] { this.write(&format!("{SOURCE}{name}"), "pub fn value() -> u32 { 1 }\n"); } this.write("files/config/runtime-edge-coverage-domains.json", CONFIG); this.report(&[ (format!("{SOURCE}main.rs"), 10, 10), (format!("./{SOURCE}bank.rs"), 10, 10), (this.0.join(format!("{SOURCE}bank/cache.rs")).display().to_string(), 10, 10), ]); this } fn write(&self, name: &str, content: &str) { let path = self.0.join(name); fs::create_dir_all(path.parent().unwrap()).unwrap(); fs::write(path, content).unwrap(); } fn report(&self, rows: &[(String, u32, u32)]) { let records: Vec<_> = rows.iter().map(|(filename, count, covered)| { format!(r#"{{"filename":"{filename}","summary":{{"lines":{{"count":{count},"covered":{covered}}}}}}}"#) }).collect(); self.write("report.json", &format!(r#"{{"data":[{{"files":[{}]}}]}}"#, records.join(","))); } fn audit(&self, report: bool) -> Output { let mut cmd = Command::new("python3"); cmd.arg(repository().join("scripts/audit-coverage-domains.py")).arg("--root").arg(&self.0); if report { cmd.arg("--report").arg(self.0.join("report.json")); } cmd.output().unwrap() } fn helper(&self, args: &[&str]) -> Output { let init = Command::new("git").args(["init", "--quiet"]).current_dir(&self.0).output().unwrap(); assert!(init.status.success(), "{init:?}"); fs::create_dir_all(self.0.join("scripts")).unwrap(); for name in ["coverage-domain-evidence.sh", "audit-coverage-domains.py"] { fs::copy(repository().join("scripts").join(name), self.0.join("scripts").join(name)) .unwrap(); } Command::new("bash") .arg(self.0.join("scripts/coverage-domain-evidence.sh")) .args(args) .current_dir(&self.0) .output() .unwrap() } } impl Drop for Fixture { fn drop(&mut self) { let _ = fs::remove_dir_all(&self.0); } } fn success(output: Output) { assert!( output.status.success(), "stdout={}\nstderr={}", String::from_utf8_lossy(&output.stdout), String::from_utf8_lossy(&output.stderr) ); } fn refuses(output: Output, diagnostic: &str) { assert!(!output.status.success(), "unexpected success: {output:?}"); assert!(String::from_utf8_lossy(&output.stderr).contains(diagnostic), "{output:?}"); } #[test] fn repository_sources_have_exactly_one_coverage_owner() { success( Command::new("python3") .arg(repository().join("scripts/audit-coverage-domains.py")) .arg("--root") .arg(repository()) .output() .unwrap(), ); } fn repository_policy(script: &str) -> Output { Command::new("python3") .arg("-B") .arg("-c") .arg(format!( r#" import importlib.util import json import sys from pathlib import Path root = Path(sys.argv[1]) spec = importlib.util.spec_from_file_location("coverage_auditor", root / "scripts/audit-coverage-domains.py") auditor = importlib.util.module_from_spec(spec) spec.loader.exec_module(auditor) config = json.loads((root / "files/config/runtime-edge-coverage-domains.json").read_text()) domains = config["domains"] trees = [ ("crates/console/prometeu-system/src/services/memcard", "system/fs", "system/core"), ("crates/console/prometeu-drivers/src/asset", "asset/bank", "drivers/core"), ] {script} "# )) .arg(repository()) .output() .unwrap() } #[test] fn specialized_entry_files_and_all_descendants_have_the_expected_owner() { success(repository_policy( r#" for stem, expected, residual in trees: names = [stem + ".rs"] descendants = sorted((root / stem).rglob("*.rs")) assert descendants, stem names += [path.relative_to(root).as_posix() for path in descendants] names += [stem + "/future.rs", stem + "/future/nested.rs"] for name in names: actual = auditor.owners(name, domains) assert actual == [expected], (name, expected, actual) # Exclusive residual fallback would pass the generic audit but is wrong. import copy fallback = copy.deepcopy(domains) fallback[expected]["paths"].remove(stem + "/") fallback[residual]["exclude_paths"].remove(stem + "/") name = names[-1] assert auditor.owners(name, fallback) == [residual] assert auditor.owners(name, fallback) != [expected] overlap = copy.deepcopy(domains) overlap[residual]["exclude_paths"].remove(stem + "/") assert set(auditor.owners(name, overlap)) == {expected, residual} "#, )); } #[test] fn specialized_ownership_survives_absent_and_zero_instrumentation() { success(repository_policy( r#" import tempfile with tempfile.TemporaryDirectory(prefix="prometeu-domain-trees-") as directory: fixture = Path(directory) records = [] # Use the real map and source layout, with independent synthetic evidence. for source in (root / "crates").glob("**/src/**/*.rs"): name = source.relative_to(root).as_posix() path = fixture / name path.parent.mkdir(parents=True, exist_ok=True) path.write_text("// ownership fixture\n") records.append({"filename": name, "summary": {"lines": {"count": 1, "covered": 1}}}) for stem, expected, residual in trees: for suffix in ["future/absent.rs", "future/zero.rs"]: path = fixture / stem / suffix path.parent.mkdir(parents=True, exist_ok=True) path.write_text("// no executable lines\n") records.append({"filename": stem + "/future/zero.rs", "summary": {"lines": {"count": 0, "covered": 0}}}) config_path = fixture / "config.json" config_path.write_text(json.dumps(config)) report_path = fixture / "report.json" report_path.write_text(json.dumps({"data": [{"files": records}]})) before = auditor.audit(root, root / "files/config/runtime-edge-coverage-domains.json") result = auditor.audit(fixture, config_path, report_path) assert len(result["domains"]) == 15 assert all(domain["gate_passed"] for domain in result["domains"].values()) for stem, expected, residual in trees: domain = result["domains"][expected] assert domain["source_files"] == before["domains"][expected]["source_files"] + 2 assert stem + "/future/zero.rs" in domain["files"] assert stem + "/future/absent.rs" not in domain["files"] assert result["domains"][residual]["source_files"] == before["domains"][residual]["source_files"] # Missing residual exclusions must fail even for sources absent from reports. for stem, expected, residual in trees: domains[residual]["exclude_paths"].remove(stem + "/") config_path.write_text(json.dumps(config)) try: auditor.audit(fixture, config_path, report_path) except auditor.AuditError as error: assert "expected exactly one domain" in str(error), error else: raise AssertionError("overlapping module trees passed ownership audit") "#, )); } #[test] fn exclusions_and_absolute_relative_report_names_have_exclusive_ownership() { let fixture = Fixture::new(); success(fixture.audit(false)); success(fixture.audit(true)); success(fixture.helper(&["--check-all", "report.json"])); let output = fixture.helper(&["--show-summary-details", "bank", "report.json"]); assert!(String::from_utf8_lossy(&output.stdout).contains("(20/20)"), "{output:?}"); success(output); } #[test] fn new_source_without_an_owner_is_rejected_even_if_uninstrumented() { let fixture = Fixture::new(); fixture.write("crates/console/unowned/src/new.rs", "pub fn new() {}\n"); refuses(fixture.audit(false), "expected exactly one domain"); } #[test] fn overlapping_domains_are_rejected_before_percentage_comparison() { let fixture = Fixture::new(); fixture.write( "files/config/runtime-edge-coverage-domains.json", &CONFIG.replace(",\n \"exclude_paths\": [\"crates/console/probe/src/bank\"]", ""), ); refuses(fixture.audit(true), "expected exactly one domain"); refuses(fixture.helper(&["--check", "core", "report.json"]), "expected exactly one domain"); } #[test] fn unknown_or_duplicate_report_sources_cannot_add_coverage() { let fixture = Fixture::new(); fixture.report(&[(format!("{SOURCE}ghost.rs"), 10, 10)]); refuses(fixture.audit(true), "report source not present"); fixture.report(&[(format!("{SOURCE}main.rs"), 10, 10), (format!("./{SOURCE}main.rs"), 10, 10)]); refuses(fixture.audit(true), "duplicate source"); } #[test] fn missing_empty_or_zero_line_evidence_never_passes_zero_baselines() { let fixture = Fixture::new(); fixture.write( "files/config/runtime-edge-coverage-domains.json", &CONFIG.replace(": 80", ": 0").replace(": 90", ": 0"), ); fixture.report(&[(format!("{SOURCE}main.rs"), 0, 0), (format!("{SOURCE}bank.rs"), 0, 0)]); refuses(fixture.helper(&["--check", "core", "report.json"]), "no instrumented lines"); fixture.report(&[]); refuses(fixture.audit(true), "no instrumented lines"); fixture.report(&[(format!("{SOURCE}main.rs"), 10, 10)]); refuses(fixture.audit(true), "bank: no instrumented lines"); fs::remove_file(fixture.0.join("report.json")).unwrap(); refuses(fixture.audit(true), "cannot read"); let output = fixture.helper(&["--check", "core", "report.json"]); assert!(!output.status.success(), "{output:?}"); assert!( String::from_utf8_lossy(&output.stdout).contains("Coverage report not found"), "{output:?}" ); } #[test] fn malformed_config_and_impossible_line_counts_fail_with_diagnostics() { let fixture = Fixture::new(); fixture .write("files/config/runtime-edge-coverage-domains.json", &CONFIG.replace(": 80", ": NaN")); refuses(fixture.audit(false), "baseline_percent must be finite"); fixture.write("files/config/runtime-edge-coverage-domains.json", CONFIG); fixture.report(&[(format!("{SOURCE}main.rs"), 10, 11)]); refuses(fixture.audit(true), "invalid line counts"); } #[test] fn residual_gate_excludes_specialized_domain_and_still_rejects_regression() { let fixture = Fixture::new(); fixture.report(&[ (format!("{SOURCE}main.rs"), 10, 8), (format!("{SOURCE}bank.rs"), 10, 0), (format!("{SOURCE}bank/cache.rs"), 10, 0), ]); let output = fixture.helper(&[ "--check", "--show-summary-details", "--show-matched-files", "core", "report.json", ]); assert!(String::from_utf8_lossy(&output.stdout).contains("(8/10)"), "{output:?}"); assert!(!String::from_utf8_lossy(&output.stdout).contains("bank/cache.rs"), "{output:?}"); success(output); fixture.report(&[(format!("{SOURCE}main.rs"), 10, 7), (format!("{SOURCE}bank.rs"), 10, 10)]); let output = fixture.helper(&["--check", "core", "report.json"]); assert!(!output.status.success(), "{output:?}"); assert!(String::from_utf8_lossy(&output.stdout).contains("below baseline"), "{output:?}"); }