use std::collections::{BTreeMap, BTreeSet};
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,
}
pub type MutatedLines = BTreeSet<(String, u32)>;
type RunOutcome = (Vec<Survivor>, MutatedLines);
#[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> {
evaluate(cargo_mutants_survivors(report), exempt)
}
fn cargo_mutants_survivors(report: &MutantsReport) -> Vec<Survivor> {
report
.outcomes
.iter()
.filter_map(|outcome| {
if outcome.summary != "MissedMutant" {
return None;
}
let Scenario::Mutant(mutant) = &outcome.scenario else {
return None;
};
Some(Survivor {
file: mutant.file.clone(),
line: mutant.span.start.line,
description: mutant.name.clone(),
})
})
.collect()
}
pub fn mutated_lines(report: &MutantsReport) -> MutatedLines {
report
.outcomes
.iter()
.filter_map(|outcome| {
if outcome.summary != "CaughtMutant" && outcome.summary != "MissedMutant" {
return None;
}
let Scenario::Mutant(mutant) = &outcome.scenario else {
return None;
};
Some((mutant.file.clone(), mutant.span.start.line))
})
.collect()
}
pub fn evaluate(survivors: Vec<Survivor>, exempt: &[String]) -> Vec<Survivor> {
survivors
.into_iter()
.filter(|survivor| !exempt.iter().any(|path| path == &survivor.file))
.collect()
}
pub fn evaluate_scoped(
survivors: Vec<Survivor>,
mutated: &MutatedLines,
whole_file: &[String],
line_scoped: &BTreeMap<String, BTreeSet<u32>>,
) -> Result<Vec<Survivor>> {
let mut over: Vec<String> = Vec::new();
for (file, lines) in line_scoped {
for &line in lines {
let has_survivor = survivors
.iter()
.any(|survivor| survivor.file == *file && survivor.line == line);
if has_survivor {
continue;
}
if mutated.contains(&(file.clone(), line)) {
over.push(format!("\n {file}:{line}"));
}
}
}
if !over.is_empty() {
bail!(
"a line-scoped mutation exemption may only list a line with a surviving mutant, but \
these had mutants that were all caught:{}",
over.concat()
);
}
Ok(survivors
.into_iter()
.filter(|survivor| {
let whole = whole_file.iter().any(|path| path == &survivor.file);
let line = line_scoped
.get(&survivor.file)
.is_some_and(|lines| lines.contains(&survivor.line));
!(whole || line)
})
.collect())
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum MutantStatus {
Survived,
Killed,
NoCoverage,
Timeout,
CompileError,
RuntimeError,
}
impl MutantStatus {
fn is_survivor(self) -> bool {
matches!(self, MutantStatus::Survived | MutantStatus::NoCoverage)
}
fn is_viable(self) -> bool {
matches!(
self,
MutantStatus::Survived
| MutantStatus::Killed
| MutantStatus::NoCoverage
| MutantStatus::Timeout
)
}
}
#[derive(Debug, Clone, Deserialize)]
pub struct NormalizedMutant {
pub file: String,
pub line: u32,
pub status: MutantStatus,
pub mutator: String,
#[serde(default)]
pub replacement: Option<String>,
}
pub fn parse_normalized_results(json: &str) -> Result<Vec<NormalizedMutant>> {
serde_json::from_str(json).context("parsing normalized mutation results")
}
pub fn evaluate_normalized(
mutants: &[NormalizedMutant],
whole_file: &[String],
line_scoped: &BTreeMap<String, BTreeSet<u32>>,
) -> Result<Vec<Survivor>> {
evaluate_scoped(
normalized_survivors(mutants),
&normalized_mutated_lines(mutants),
whole_file,
line_scoped,
)
}
fn normalized_survivors(mutants: &[NormalizedMutant]) -> Vec<Survivor> {
mutants
.iter()
.filter(|mutant| mutant.status.is_survivor())
.map(|mutant| Survivor {
file: mutant.file.clone(),
line: mutant.line,
description: describe_normalized(mutant),
})
.collect()
}
fn normalized_mutated_lines(mutants: &[NormalizedMutant]) -> MutatedLines {
mutants
.iter()
.filter(|mutant| mutant.status.is_viable())
.map(|mutant| (mutant.file.clone(), mutant.line))
.collect()
}
fn describe_normalized(mutant: &NormalizedMutant) -> String {
match &mutant.replacement {
Some(replacement) => format!("{} (-> {})", mutant.mutator, one_line(replacement)),
None => mutant.mutator.clone(),
}
}
pub fn measure_rust(
root: &Path,
exempt: &[String],
exempt_lines: &BTreeMap<String, BTreeSet<u32>>,
base: Option<&str>,
) -> Result<Vec<Survivor>> {
let out = MutantsOut::new();
let diff = match base {
Some(base) => match write_base_diff(root, base, &out)? {
None => return Ok(Vec::new()),
Some(path) => Some(path),
},
None => None,
};
run_cargo_mutants(root, &out.0, diff.as_deref())?;
let outcomes = out.0.join("mutants.out").join("outcomes.json");
let json = match std::fs::read_to_string(&outcomes) {
Ok(json) => json,
Err(_) => return Ok(Vec::new()),
};
let report = parse_mutants_report(&json)?;
evaluate_scoped(
cargo_mutants_survivors(&report),
&mutated_lines(&report),
exempt,
exempt_lines,
)
}
#[derive(Debug, Clone, Deserialize)]
pub struct StrykerReport {
pub files: BTreeMap<String, StrykerFile>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct StrykerFile {
#[serde(default)]
pub mutants: Vec<StrykerMutant>,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct StrykerMutant {
pub mutator_name: String,
#[serde(default)]
pub replacement: Option<String>,
pub status: String,
pub location: StrykerLocation,
}
#[derive(Debug, Clone, Deserialize)]
pub struct StrykerLocation {
pub start: LineCol,
}
pub fn parse_stryker_report(json: &str) -> Result<StrykerReport> {
serde_json::from_str(json).context("parsing Stryker mutation.json")
}
pub fn stryker_survivors(report: &StrykerReport) -> Vec<Survivor> {
let mut survivors = Vec::new();
for (file, contents) in &report.files {
for mutant in &contents.mutants {
if mutant.status != "Survived" && mutant.status != "NoCoverage" {
continue;
}
let description = match &mutant.replacement {
Some(replacement) => {
format!("{} (-> {})", mutant.mutator_name, one_line(replacement))
}
None => mutant.mutator_name.clone(),
};
survivors.push(Survivor {
file: file.clone(),
line: mutant.location.start.line,
description,
});
}
}
survivors
}
fn one_line(replacement: &str) -> String {
let flat = replacement.split_whitespace().collect::<Vec<_>>().join(" ");
const MAX: usize = 60;
if flat.chars().count() > MAX {
format!("{}…", flat.chars().take(MAX).collect::<String>())
} else {
flat
}
}
pub fn measure_typescript(
root: &Path,
exempt: &[String],
exempt_lines: &BTreeMap<String, BTreeSet<u32>>,
base: Option<&str>,
) -> Result<Vec<Survivor>> {
let mutate = match base {
Some(base) => {
let ranges = mutate_ranges(root, base)?;
if ranges.is_empty() {
return Ok(Vec::new());
}
Some(ranges)
}
None => None,
};
let json = run_stryker(root, mutate.as_deref())?;
let report = parse_stryker_report(&json)?;
evaluate_scoped(
stryker_survivors(&report),
&stryker_mutated_lines(&report),
exempt,
exempt_lines,
)
}
fn stryker_mutated_lines(report: &StrykerReport) -> MutatedLines {
let mut mutated = BTreeSet::new();
for (file, contents) in &report.files {
for mutant in &contents.mutants {
if matches!(
mutant.status.as_str(),
"Killed" | "Survived" | "NoCoverage" | "Timeout"
) {
mutated.insert((file.clone(), mutant.location.start.line));
}
}
}
mutated
}
fn mutate_ranges(root: &Path, base: &str) -> Result<Vec<String>> {
let changed = crate::patch_coverage::changed_lines(root, base)?;
let mut specs = Vec::new();
for (file, lines) in changed {
if !is_mutatable_ts(&file) {
continue;
}
for (start, end) in contiguous_runs(&lines) {
specs.push(format!("{file}:{start}-{end}"));
}
}
Ok(specs)
}
fn is_mutatable_ts(file: &str) -> bool {
let is_source = [".ts", ".tsx", ".mts", ".cts", ".js", ".jsx", ".mjs", ".cjs"]
.iter()
.any(|ext| file.ends_with(ext));
let is_decl = file.ends_with(".d.ts");
let is_test = file.contains(".test.") || file.contains(".spec.");
is_source && !is_decl && !is_test
}
fn contiguous_runs(lines: &BTreeSet<u64>) -> Vec<(u64, u64)> {
let mut runs: Vec<(u64, u64)> = Vec::new();
for &line in lines {
match runs.last_mut() {
Some(run) if run.1 + 1 == line => run.1 = line,
_ => runs.push((line, line)),
}
}
runs
}
fn run_stryker(root: &Path, mutate: Option<&[String]>) -> Result<String> {
let report_path = root.join("reports").join("mutation").join("mutation.json");
let _cleanup = ReportCleanup(report_path.clone());
let _ = std::fs::remove_file(&report_path);
let mut command = Command::new("npx");
command
.current_dir(root)
.args(["--no-install", "stryker", "run", "--reporters", "json"]);
if let Some(specs) = mutate {
command.arg("--mutate").arg(specs.join(","));
}
let output = command
.env("CI", "1")
.output()
.context("running `npx --no-install stryker run`")?;
std::fs::read_to_string(&report_path).map_err(|_| {
anyhow::anyhow!(
"Stryker produced no report in `{}`. The rule runs the project's own Stryker via \
`npx --no-install` and never downloads it, so `@stryker-mutator/core` (plus a \
test-runner plugin) must be installed in the project. Stryker output:\n{}{}",
root.display(),
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr),
)
})
}
struct ReportCleanup(PathBuf);
impl Drop for ReportCleanup {
fn drop(&mut self) {
let _ = std::fs::remove_file(&self.0);
if let Some(mutation_dir) = self.0.parent() {
let _ = std::fs::remove_dir(mutation_dir);
if let Some(reports_dir) = mutation_dir.parent() {
let _ = std::fs::remove_dir(reports_dir);
}
}
}
}
#[derive(Debug, Clone, Deserialize)]
pub struct CosmicRayLine(pub CrWorkItem, pub Option<CrResult>);
#[derive(Debug, Clone, Deserialize)]
pub struct CrWorkItem {
pub mutations: Vec<CrMutation>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct CrMutation {
pub module_path: String,
pub operator_name: String,
pub start_pos: (u32, u32),
#[serde(default)]
pub definition_name: Option<String>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct CrResult {
#[serde(default)]
pub test_outcome: Option<String>,
}
pub fn parse_cosmic_ray_dump(dump: &str) -> Result<Vec<Survivor>> {
let mut survivors = Vec::new();
for line in dump.lines() {
if line.trim().is_empty() {
continue;
}
let CosmicRayLine(item, result) =
serde_json::from_str(line).context("parsing a cosmic-ray dump line")?;
let survived = matches!(result, Some(CrResult { test_outcome: Some(outcome) }) if outcome == "survived");
if !survived {
continue;
}
let Some(mutation) = item.mutations.first() else {
continue;
};
let definition = mutation.definition_name.as_deref().unwrap_or("<module>");
survivors.push(Survivor {
file: mutation.module_path.clone(),
line: mutation.start_pos.0,
description: format!("{} in {}", mutation.operator_name, definition),
});
}
Ok(survivors)
}
pub fn measure_python(
root: &Path,
exempt: &[String],
exempt_lines: &BTreeMap<String, BTreeSet<u32>>,
base: Option<&str>,
) -> Result<Vec<Survivor>> {
let (survivors, mutated) = match base {
None => run_cosmic_ray(root, ".", &PY_TEST_EXCLUDES)?,
Some(base) => {
let changed = crate::patch_coverage::changed_lines(root, base)?;
let mut all_survivors = Vec::new();
let mut all_mutated = BTreeSet::new();
for (file, lines) in &changed {
if !is_mutatable_py(file) {
continue;
}
let (survivors, mutated) = run_cosmic_ray(root, file, &[])?;
for survivor in survivors {
if lines.contains(&(survivor.line as u64)) {
all_survivors.push(survivor);
}
}
for (mutated_file, line) in mutated {
if lines.contains(&u64::from(line)) {
all_mutated.insert((mutated_file, line));
}
}
}
(all_survivors, all_mutated)
}
};
evaluate_scoped(survivors, &mutated, exempt, exempt_lines)
}
const PY_TEST_EXCLUDES: [&str; 3] = ["*_test.py", "test_*.py", "conftest.py"];
fn is_mutatable_py(file: &str) -> bool {
if !file.ends_with(".py") {
return false;
}
let base = file.rsplit('/').next().unwrap_or(file);
!(base.ends_with("_test.py") || base.starts_with("test_") || base == "conftest.py")
}
fn run_cosmic_ray(root: &Path, module_path: &str, excluded_modules: &[&str]) -> Result<RunOutcome> {
let dir = CosmicRayDir::new();
std::fs::create_dir_all(&dir.0).context("creating the cosmic-ray temp dir")?;
let config = dir.0.join("cr.toml");
let session = dir.0.join("session.sqlite");
let excludes = excluded_modules
.iter()
.map(|glob| format!("\"{glob}\""))
.collect::<Vec<_>>()
.join(", ");
std::fs::write(
&config,
format!(
"[cosmic-ray]\n\
module-path = \"{module_path}\"\n\
timeout = 30.0\n\
excluded-modules = [{excludes}]\n\
test-command = \"python3 -m pytest -q -p no:cacheprovider\"\n\
\n\
[cosmic-ray.distributor]\n\
name = \"local\"\n"
),
)
.context("writing the cosmic-ray config")?;
let baseline = cosmic_ray(root, &["baseline", path_str(&config)])?;
if !baseline.status.success() {
bail!(
"the Python unit suite did not pass unmutated in `{}` (cosmic-ray baseline failed):\n{}{}",
root.display(),
String::from_utf8_lossy(&baseline.stdout),
String::from_utf8_lossy(&baseline.stderr),
);
}
let init = cosmic_ray(root, &["init", path_str(&config), path_str(&session)])?;
if !init.status.success() {
bail!(
"cosmic-ray init failed in `{}`:\n{}{}",
root.display(),
String::from_utf8_lossy(&init.stdout),
String::from_utf8_lossy(&init.stderr),
);
}
let exec = cosmic_ray(root, &["exec", path_str(&config), path_str(&session)])?;
if !exec.status.success() {
bail!(
"cosmic-ray exec failed in `{}`:\n{}{}",
root.display(),
String::from_utf8_lossy(&exec.stdout),
String::from_utf8_lossy(&exec.stderr),
);
}
let dump = cosmic_ray(root, &["dump", path_str(&session)])?;
if !dump.status.success() {
bail!(
"cosmic-ray dump failed in `{}`:\n{}",
root.display(),
String::from_utf8_lossy(&dump.stderr),
);
}
let stdout = String::from_utf8_lossy(&dump.stdout);
Ok((
parse_cosmic_ray_dump(&stdout)?,
cosmic_ray_mutated_lines(&stdout)?,
))
}
pub fn cosmic_ray_mutated_lines(dump: &str) -> Result<MutatedLines> {
let mut mutated = BTreeSet::new();
for line in dump.lines() {
if line.trim().is_empty() {
continue;
}
let CosmicRayLine(item, result) =
serde_json::from_str(line).context("parsing a cosmic-ray dump line")?;
let outcome = result.and_then(|result| result.test_outcome);
if !matches!(outcome.as_deref(), Some("survived") | Some("killed")) {
continue;
}
if let Some(mutation) = item.mutations.first() {
mutated.insert((mutation.module_path.clone(), mutation.start_pos.0));
}
}
Ok(mutated)
}
fn cosmic_ray(root: &Path, args: &[&str]) -> Result<std::process::Output> {
Command::new("cosmic-ray")
.current_dir(root)
.args(args)
.env("PYTHONDONTWRITEBYTECODE", "1")
.output()
.context("running `cosmic-ray` (is it installed?)")
}
fn path_str(path: &Path) -> &str {
path.to_str().expect("temp path is valid UTF-8")
}
struct CosmicRayDir(PathBuf);
impl CosmicRayDir {
fn new() -> Self {
static COUNTER: AtomicU64 = AtomicU64::new(0);
let name = format!(
"testing-conventions-cosmic-ray-{}-{}",
std::process::id(),
COUNTER.fetch_add(1, Ordering::Relaxed),
);
CosmicRayDir(std::env::temp_dir().join(name))
}
}
impl Drop for CosmicRayDir {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}
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<Option<PathBuf>> {
let range = format!("{base}...HEAD");
let output = Command::new("git")
.current_dir(root)
.args(["diff", "--relative", &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)
);
}
if output.stdout.is_empty() {
return Ok(None);
}
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(Some(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 NORMALIZED: &str = r#"[
{"file": "src/a.ts", "line": 2, "status": "survived",
"mutator": "ConditionalExpression", "replacement": "true", "id": "ignored"},
{"file": "src/a.ts", "line": 5, "status": "no_coverage", "mutator": "ArithmeticOperator"},
{"file": "src/a.ts", "line": 9, "status": "killed",
"mutator": "BooleanLiteral", "replacement": "false"},
{"file": "src/a.ts", "line": 12, "status": "timeout", "mutator": "BlockStatement"},
{"file": "src/a.ts", "line": 15, "status": "compile_error", "mutator": "OptionalChaining"},
{"file": "src/a.ts", "line": 18, "status": "runtime_error", "mutator": "StringLiteral"}
]"#;
#[test]
fn parses_the_normalized_schema() {
let mutants = parse_normalized_results(NORMALIZED).expect("valid normalized results");
assert_eq!(mutants.len(), 6);
assert_eq!(mutants[0].status, MutantStatus::Survived);
assert_eq!(mutants[1].status, MutantStatus::NoCoverage);
assert_eq!(mutants[0].replacement.as_deref(), Some("true"));
assert_eq!(mutants[1].replacement, None);
}
#[test]
fn normalized_survivors_are_survived_and_nocoverage_only() {
let mutants = parse_normalized_results(NORMALIZED).unwrap();
let survivors = normalized_survivors(&mutants);
assert_eq!(survivors.len(), 2);
assert_eq!((survivors[0].line, survivors[1].line), (2, 5));
assert!(survivors[0].description.contains("ConditionalExpression"));
assert!(survivors[0].description.contains("-> true"));
assert_eq!(survivors[1].description, "ArithmeticOperator");
}
#[test]
fn normalized_mutated_lines_collects_only_viable_mutants() {
let mutants = parse_normalized_results(NORMALIZED).unwrap();
assert_eq!(
normalized_mutated_lines(&mutants),
[2u32, 5, 9, 12]
.into_iter()
.map(|line| ("src/a.ts".to_string(), line))
.collect()
);
}
#[test]
fn evaluate_normalized_reports_unexempted_survivors() {
let mutants = parse_normalized_results(NORMALIZED).unwrap();
let kept = evaluate_normalized(&mutants, &[], &BTreeMap::new()).unwrap();
assert_eq!(kept.len(), 2, "both survivors stand with no exemptions");
}
#[test]
fn evaluate_normalized_drops_a_whole_file_exemption() {
let mutants = parse_normalized_results(NORMALIZED).unwrap();
let kept =
evaluate_normalized(&mutants, &["src/a.ts".to_string()], &BTreeMap::new()).unwrap();
assert!(
kept.is_empty(),
"the whole-file exemption lifts both survivors"
);
}
#[test]
fn evaluate_normalized_drops_a_line_scoped_exemption() {
let mutants = parse_normalized_results(NORMALIZED).unwrap();
let line_scoped = BTreeMap::from([("src/a.ts".to_string(), BTreeSet::from([2u32]))]);
let kept = evaluate_normalized(&mutants, &[], &line_scoped).unwrap();
assert_eq!(kept.len(), 1);
assert_eq!(kept[0].line, 5);
}
#[test]
fn evaluate_normalized_rejects_exempting_a_caught_line() {
let mutants = parse_normalized_results(NORMALIZED).unwrap();
let line_scoped = BTreeMap::from([("src/a.ts".to_string(), BTreeSet::from([9u32]))]);
let err = evaluate_normalized(&mutants, &[], &line_scoped).unwrap_err();
assert!(
err.to_string().contains("all caught") && err.to_string().contains("src/a.ts:9"),
"got: {err}"
);
}
#[test]
fn evaluate_normalized_leaves_an_unviable_listed_line_alone() {
let mutants = parse_normalized_results(NORMALIZED).unwrap();
let line_scoped = BTreeMap::from([("src/a.ts".to_string(), BTreeSet::from([15u32]))]);
let kept = evaluate_normalized(&mutants, &[], &line_scoped).unwrap();
assert_eq!(kept.len(), 2);
}
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);
}
const STRYKER_SAMPLE: &str = r#"{
"schemaVersion": "1.0",
"files": {
"src/index.ts": {
"language": "typescript",
"source": "...",
"mutants": [
{"id": "0", "mutatorName": "ConditionalExpression", "replacement": "true",
"status": "Survived", "coveredBy": ["t0"],
"location": {"start": {"line": 2, "column": 10}, "end": {"line": 2, "column": 15}}},
{"id": "1", "mutatorName": "ArithmeticOperator", "replacement": "a - b",
"status": "NoCoverage",
"location": {"start": {"line": 5, "column": 3}, "end": {"line": 5, "column": 8}}},
{"id": "2", "mutatorName": "BooleanLiteral", "replacement": "false",
"status": "Killed",
"location": {"start": {"line": 9, "column": 1}, "end": {"line": 9, "column": 6}}}
]
}
}
}"#;
#[test]
fn parses_a_stryker_report() {
let report = parse_stryker_report(STRYKER_SAMPLE).expect("valid mutation.json");
assert_eq!(report.files["src/index.ts"].mutants.len(), 3);
}
#[test]
fn collects_survived_and_nocoverage_as_survivors() {
let report = parse_stryker_report(STRYKER_SAMPLE).unwrap();
let survivors = stryker_survivors(&report);
assert_eq!(survivors.len(), 2);
assert!(survivors.iter().all(|s| s.file == "src/index.ts"));
assert_eq!(survivors[0].line, 2);
assert!(survivors[0].description.contains("ConditionalExpression"));
assert!(survivors[0].description.contains("true"));
assert_eq!(survivors[1].line, 5);
}
#[test]
fn evaluate_drops_exempt_files_for_either_engine() {
let report = parse_stryker_report(STRYKER_SAMPLE).unwrap();
let survivors = stryker_survivors(&report);
let exempt = vec!["src/index.ts".to_string()];
assert!(evaluate(survivors, &exempt).is_empty());
}
#[test]
fn is_mutatable_ts_keeps_sources_and_drops_tests_and_decls() {
assert!(is_mutatable_ts("src/index.ts"));
assert!(is_mutatable_ts("src/util.tsx"));
assert!(is_mutatable_ts("src/util.js"));
assert!(!is_mutatable_ts("src/index.test.ts"));
assert!(!is_mutatable_ts("src/index.spec.ts"));
assert!(!is_mutatable_ts("src/types.d.ts"));
assert!(!is_mutatable_ts("README.md"));
}
#[test]
fn contiguous_runs_collapses_adjacent_lines() {
let lines: BTreeSet<u64> = [2u64, 3, 4, 7, 9, 10].into_iter().collect();
assert_eq!(contiguous_runs(&lines), vec![(2, 4), (7, 7), (9, 10)]);
assert!(contiguous_runs(&BTreeSet::new()).is_empty());
}
#[test]
fn one_line_flattens_and_caps() {
assert_eq!(one_line("a -\n b"), "a - b");
let long = "x".repeat(80);
let capped = one_line(&long);
assert!(capped.chars().count() <= 61 && capped.ends_with('…'));
}
const COSMIC_RAY_DUMP: &str = concat!(
r#"[{"job_id":"a","mutations":[{"module_path":"calc.py","operator_name":"core/ReplaceComparisonOperator_Gt_NotEq","occurrence":0,"start_pos":[6,11],"end_pos":[6,12],"operator_args":{},"definition_name":"is_positive"}]},{"worker_outcome":"normal","test_outcome":"survived"}]"#,
"\n",
r#"[{"job_id":"b","mutations":[{"module_path":"calc.py","operator_name":"core/ReplaceBinaryOperator_Add_Div","occurrence":0,"start_pos":[2,13],"end_pos":[2,14],"operator_args":{},"definition_name":"add"}]},{"worker_outcome":"normal","test_outcome":"killed"}]"#,
"\n",
);
#[test]
fn collects_only_survived_cosmic_ray_mutants() {
let survivors = parse_cosmic_ray_dump(COSMIC_RAY_DUMP).expect("valid dump");
assert_eq!(survivors.len(), 1);
assert_eq!(survivors[0].file, "calc.py");
assert_eq!(survivors[0].line, 6);
assert!(survivors[0]
.description
.contains("ReplaceComparisonOperator"));
assert!(survivors[0].description.contains("is_positive"));
}
#[test]
fn an_unexecuted_cosmic_ray_item_is_not_a_survivor() {
let dump = r#"[{"mutations":[{"module_path":"calc.py","operator_name":"core/NumberReplacer","start_pos":[3,5],"end_pos":[3,6]}]},null]"#;
assert!(parse_cosmic_ray_dump(dump).unwrap().is_empty());
}
#[test]
fn is_mutatable_py_keeps_sources_and_drops_tests() {
assert!(is_mutatable_py("calc.py"));
assert!(is_mutatable_py("pkg/util.py"));
assert!(!is_mutatable_py("calc_test.py"));
assert!(!is_mutatable_py("test_calc.py"));
assert!(!is_mutatable_py("pkg/conftest.py"));
assert!(!is_mutatable_py("README.md"));
}
#[test]
fn mutated_lines_collects_caught_and_missed() {
let report = parse_mutants_report(SAMPLE).unwrap();
assert_eq!(
mutated_lines(&report),
[
("src/lib.rs".to_string(), 7),
("src/other.rs".to_string(), 3)
]
.into_iter()
.collect()
);
}
#[test]
fn evaluate_scoped_drops_a_survivor_on_an_exempt_line() {
let report = parse_mutants_report(SAMPLE).unwrap();
let line_scoped = BTreeMap::from([("src/lib.rs".to_string(), BTreeSet::from([7u32]))]);
let kept = evaluate_scoped(
cargo_mutants_survivors(&report),
&mutated_lines(&report),
&[],
&line_scoped,
)
.unwrap();
assert!(
kept.is_empty(),
"the src/lib.rs:7 survivor should be lifted"
);
}
#[test]
fn evaluate_scoped_rejects_exempting_a_caught_line() {
let report = parse_mutants_report(SAMPLE).unwrap();
let line_scoped = BTreeMap::from([("src/other.rs".to_string(), BTreeSet::from([3u32]))]);
let err = evaluate_scoped(
cargo_mutants_survivors(&report),
&mutated_lines(&report),
&[],
&line_scoped,
)
.unwrap_err();
assert!(
err.to_string().contains("all caught") && err.to_string().contains("src/other.rs:3"),
"got: {err}"
);
}
#[test]
fn evaluate_scoped_leaves_an_unmutated_listed_line_alone() {
let report = parse_mutants_report(SAMPLE).unwrap();
let line_scoped = BTreeMap::from([("src/lib.rs".to_string(), BTreeSet::from([99u32]))]);
let kept = evaluate_scoped(
cargo_mutants_survivors(&report),
&mutated_lines(&report),
&[],
&line_scoped,
)
.unwrap();
assert_eq!(kept.len(), 1);
assert_eq!(kept[0].line, 7);
}
#[test]
fn evaluate_scoped_still_honors_a_whole_file_exemption() {
let report = parse_mutants_report(SAMPLE).unwrap();
let kept = evaluate_scoped(
cargo_mutants_survivors(&report),
&mutated_lines(&report),
&["src/lib.rs".to_string()],
&BTreeMap::new(),
)
.unwrap();
assert!(kept.is_empty());
}
#[test]
fn stryker_mutated_lines_collects_every_viable_mutant() {
let report = parse_stryker_report(STRYKER_SAMPLE).unwrap();
assert_eq!(
stryker_mutated_lines(&report),
[
("src/index.ts".to_string(), 2),
("src/index.ts".to_string(), 5),
("src/index.ts".to_string(), 9),
]
.into_iter()
.collect()
);
}
#[test]
fn cosmic_ray_mutated_lines_collects_executed_mutants() {
let mutated = cosmic_ray_mutated_lines(COSMIC_RAY_DUMP).unwrap();
assert_eq!(
mutated,
[("calc.py".to_string(), 2), ("calc.py".to_string(), 6)]
.into_iter()
.collect()
);
}
}