Intrepid/Prometeu/Runtime/pipeline/pr-master There was a failure building this commit
212 lines
8.0 KiB
Rust
212 lines
8.0 KiB
Rust
//! 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(),
|
|
);
|
|
}
|
|
|
|
#[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:?}");
|
|
}
|