use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::atomic::{AtomicU64, Ordering};
use anyhow::{bail, Context, Result};
use serde::Deserialize;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Survivor {
pub file: String,
pub line: u32,
pub description: String,
}
#[derive(Debug, Clone, Deserialize)]
pub struct MutantsReport {
pub outcomes: Vec<MutantOutcome>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct MutantOutcome {
pub summary: String,
pub scenario: Scenario,
}
#[derive(Debug, Clone, Deserialize)]
pub enum Scenario {
Baseline,
Mutant(MutantInfo),
}
#[derive(Debug, Clone, Deserialize)]
pub struct MutantInfo {
pub file: String,
pub span: Span,
pub name: String,
}
#[derive(Debug, Clone, Deserialize)]
pub struct Span {
pub start: LineCol,
}
#[derive(Debug, Clone, Deserialize)]
pub struct LineCol {
pub line: u32,
}
pub fn parse_mutants_report(json: &str) -> Result<MutantsReport> {
serde_json::from_str(json).context("parsing cargo-mutants outcomes.json")
}
pub fn unexplained_survivors(report: &MutantsReport, exempt: &[String]) -> Vec<Survivor> {
report
.outcomes
.iter()
.filter_map(|outcome| {
if outcome.summary != "MissedMutant" {
return None;
}
let Scenario::Mutant(mutant) = &outcome.scenario else {
return None;
};
if exempt.iter().any(|path| path == &mutant.file) {
return None;
}
Some(Survivor {
file: mutant.file.clone(),
line: mutant.span.start.line,
description: mutant.name.clone(),
})
})
.collect()
}
pub fn measure_rust(root: &Path, exempt: &[String], base: Option<&str>) -> Result<Vec<Survivor>> {
let out = MutantsOut::new();
let diff = match base {
Some(base) => Some(write_base_diff(root, base, &out)?),
None => None,
};
run_cargo_mutants(root, &out.0, diff.as_deref())?;
let outcomes = out.0.join("mutants.out").join("outcomes.json");
let json = std::fs::read_to_string(&outcomes).with_context(|| {
format!(
"reading cargo-mutants outcomes at `{}` — the run wrote none",
outcomes.display()
)
})?;
let report = parse_mutants_report(&json)?;
Ok(unexplained_survivors(&report, exempt))
}
struct MutantsOut(PathBuf);
impl MutantsOut {
fn new() -> Self {
static COUNTER: AtomicU64 = AtomicU64::new(0);
let name = format!(
"testing-conventions-mutants-{}-{}",
std::process::id(),
COUNTER.fetch_add(1, Ordering::Relaxed),
);
MutantsOut(std::env::temp_dir().join(name))
}
}
impl Drop for MutantsOut {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}
fn write_base_diff(root: &Path, base: &str, out: &MutantsOut) -> Result<PathBuf> {
let range = format!("{base}...HEAD");
let output = Command::new("git")
.current_dir(root)
.args(["diff", &range])
.output()
.context("running `git diff` for `--base` (is git installed?)")?;
if !output.status.success() {
bail!(
"git diff {range} failed: {}",
String::from_utf8_lossy(&output.stderr)
);
}
std::fs::create_dir_all(&out.0).context("creating the mutants output dir")?;
let path = out.0.join("base.diff");
std::fs::write(&path, &output.stdout).context("writing the base diff")?;
Ok(path)
}
fn run_cargo_mutants(root: &Path, out: &Path, in_diff: Option<&Path>) -> Result<()> {
let mut command = Command::new("cargo");
command
.current_dir(root)
.arg("mutants")
.arg("--output")
.arg(out);
if let Some(diff) = in_diff {
command.arg("--in-diff").arg(diff);
}
for var in [
"RUSTFLAGS",
"CARGO_ENCODED_RUSTFLAGS",
"RUSTDOCFLAGS",
"CARGO_ENCODED_RUSTDOCFLAGS",
"LLVM_PROFILE_FILE",
"CARGO_LLVM_COV",
"CARGO_LLVM_COV_SHOW_ENV",
"CARGO_LLVM_COV_TARGET_DIR",
"CARGO_LLVM_COV_BUILD_DIR",
"RUSTC_WRAPPER",
"RUSTC_WORKSPACE_WRAPPER",
"__CARGO_LLVM_COV_RUSTC_WRAPPER",
"__CARGO_LLVM_COV_RUSTC_WRAPPER_RUSTFLAGS",
"__CARGO_LLVM_COV_RUSTC_WRAPPER_CRATE_NAMES",
] {
command.env_remove(var);
}
let output = command
.output()
.context("running `cargo mutants` (is cargo-mutants installed?)")?;
match output.status.code() {
Some(0) | Some(2) => Ok(()),
_ => bail!(
"cargo-mutants did not run cleanly in `{}` (baseline build/test failure?):\n{}{}",
root.display(),
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr),
),
}
}
#[cfg(test)]
mod tests {
use super::*;
const SAMPLE: &str = r#"{
"outcomes": [
{"scenario": "Baseline", "summary": "Success",
"phase_results": []},
{"scenario": {"Mutant": {"file": "src/lib.rs", "package": "p", "genre": "FnValue",
"replacement": "true", "name": "src/lib.rs:7:7: replace > with == in is_positive",
"function": {"function_name": "is_positive"},
"span": {"start": {"line": 7, "column": 7}, "end": {"line": 7, "column": 8}}}},
"summary": "MissedMutant"},
{"scenario": {"Mutant": {"file": "src/other.rs", "package": "p", "genre": "FnValue",
"replacement": "0", "name": "src/other.rs:3:5: replace add -> i32 with 0",
"span": {"start": {"line": 3, "column": 5}, "end": {"line": 3, "column": 9}}}},
"summary": "CaughtMutant"}
],
"total_mutants": 2
}"#;
#[test]
fn parses_the_outcomes_export() {
let report = parse_mutants_report(SAMPLE).expect("valid outcomes.json");
assert_eq!(report.outcomes.len(), 3);
assert!(matches!(report.outcomes[0].scenario, Scenario::Baseline));
}
#[test]
fn collects_only_missed_mutants_as_survivors() {
let report = parse_mutants_report(SAMPLE).unwrap();
let survivors = unexplained_survivors(&report, &[]);
assert_eq!(survivors.len(), 1);
assert_eq!(survivors[0].file, "src/lib.rs");
assert_eq!(survivors[0].line, 7);
assert!(survivors[0].description.contains("replace > with =="));
}
#[test]
fn an_exemption_drops_a_survivor_in_that_file() {
let report = parse_mutants_report(SAMPLE).unwrap();
let exempt = vec!["src/lib.rs".to_string()];
assert!(unexplained_survivors(&report, &exempt).is_empty());
}
#[test]
fn an_exemption_on_another_file_leaves_the_survivor() {
let report = parse_mutants_report(SAMPLE).unwrap();
let exempt = vec!["src/elsewhere.rs".to_string()];
assert_eq!(unexplained_survivors(&report, &exempt).len(), 1);
}
}