pub const REASONS: [&str; 3] = ["bin-target", "re-export", "not-yet"];
#[cfg(test)]
mod tests {
use std::collections::{BTreeMap, BTreeSet};
use std::path::{Path, PathBuf};
fn repo_root() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR"))
.parent()
.and_then(Path::parent)
.expect("crates/varve-core is two levels below the repo root")
.to_path_buf()
}
fn gated(root: &Path) -> BTreeSet<String> {
let ci = std::fs::read_to_string(root.join(".github/workflows/ci.yml"))
.expect("the workflow that defines the gate must be readable");
let mut out = BTreeSet::new();
for (i, _) in ci.match_indices("-f ") {
let rest = &ci[i + 3..];
let end = rest.find(|c: char| c.is_whitespace()).unwrap_or(rest.len());
let path = rest[..end].trim();
if path.ends_with(".rs") {
out.insert(path.to_string());
}
}
assert!(
!out.is_empty(),
"no `-f <file>.rs` found in ci.yml — the gate cannot be empty, and a \
check that reads an empty list would pass by finding nothing"
);
out
}
fn sources(root: &Path) -> BTreeSet<String> {
fn walk(dir: &Path, root: &Path, out: &mut BTreeSet<String>) {
let Ok(entries) = std::fs::read_dir(dir) else {
return;
};
for e in entries.flatten() {
let p = e.path();
if p.is_dir() {
walk(&p, root, out);
} else if p.extension().is_some_and(|x| x == "rs") {
let rel = p.strip_prefix(root).expect("under the repo root");
out.insert(rel.to_string_lossy().replace('\\', "/"));
}
}
}
let mut out = BTreeSet::new();
for crate_dir in std::fs::read_dir(root.join("crates"))
.expect("crates/ exists")
.flatten()
{
walk(&crate_dir.path().join("src"), root, &mut out);
}
assert!(out.len() > 10, "suspiciously few sources: {}", out.len());
out
}
fn declared(root: &Path) -> BTreeMap<String, (String, String)> {
let text = std::fs::read_to_string(root.join("mutation-scope.toml"))
.expect("mutation-scope.toml must exist — it IS the declaration");
let doc: toml::Value = toml::from_str(&text).expect("mutation-scope.toml must parse");
let mut out = BTreeMap::new();
for reason in super::REASONS {
let Some(table) = doc.get(reason).and_then(|v| v.as_table()) else {
continue;
};
for (path, note) in table {
let note = note.as_str().unwrap_or("").trim().to_string();
assert!(
!note.is_empty(),
"{path} is excluded as `{reason}` with no reason written down — \
an empty reason is an omission wearing a decision's clothes"
);
if let Some((prev, _)) = out.insert(path.clone(), (reason.to_string(), note)) {
panic!("{path} is declared twice, as `{prev}` and as `{reason}`");
}
}
}
out
}
#[test]
fn mutation_scope_is_decided_not_drifted() {
let root = repo_root();
let gated = gated(&root);
let sources = sources(&root);
let declared = declared(&root);
let undecided: Vec<_> = sources
.iter()
.filter(|f| !gated.contains(*f) && !declared.contains_key(*f))
.collect();
assert!(
undecided.is_empty(),
"these source files are neither in the mutation gate nor declared in \
mutation-scope.toml:\n {}\n\nAdd them to the gate's shard matrix in \
ci.yml, or declare them with a reason. A file that is silently \
ungated is how a mutant permitting yank suppression survived \
v0.33.0.",
undecided
.iter()
.map(|s| s.as_str())
.collect::<Vec<_>>()
.join("\n ")
);
}
#[test]
fn nothing_is_both_gated_and_excused() {
let root = repo_root();
let gated = gated(&root);
let declared = declared(&root);
let both: Vec<_> = declared.keys().filter(|f| gated.contains(*f)).collect();
assert!(
both.is_empty(),
"declared as excluded AND named in the gate: {both:?} — remove it from \
one, since the two say opposite things about the same file"
);
}
#[test]
fn the_declaration_names_only_files_that_exist() {
let root = repo_root();
let sources = sources(&root);
let stale: Vec<_> = declared(&root)
.into_keys()
.filter(|f| !sources.contains(f))
.collect();
assert!(
stale.is_empty(),
"mutation-scope.toml names files that do not exist: {stale:?} — a stale \
exclusion is what a rename leaves behind, and it excuses nothing while \
appearing to"
);
}
#[test]
fn the_gate_names_only_files_that_exist() {
let root = repo_root();
let sources = sources(&root);
let missing: Vec<_> = gated(&root)
.into_iter()
.filter(|f| !sources.contains(f))
.collect();
assert!(
missing.is_empty(),
"ci.yml's mutation shards name files that do not exist: {missing:?} — \
cargo-mutants would test nothing for them while the list still reads \
as coverage"
);
}
#[test]
fn the_ungated_trust_relevant_backlog_is_counted_and_capped() {
let declared = declared(&repo_root());
let not_yet: Vec<_> = declared
.iter()
.filter(|(_, (reason, _))| reason == "not-yet")
.map(|(f, _)| f.as_str())
.collect();
eprintln!(
"mutation gate: {} file(s) trust-relevant and not yet gated:\n {}",
not_yet.len(),
not_yet.join("\n ")
);
const CAP: usize = 22;
assert!(
not_yet.len() <= CAP,
"{} files are declared `not-yet`, above the cap of {CAP}. The cap only \
ratchets DOWN: raising it is how a backlog becomes a permanent \
exemption, so gate a file instead.",
not_yet.len()
);
}
}