Skip to main content

testing_conventions/
mutation.rs

1//! Mutation testing for Rust (`unit mutation --language rust`, #201) — the rung
2//! above coverage. A test that *runs* a line still passes if you delete its
3//! assertions; a surviving mutant proves it. This module wraps
4//! [cargo-mutants](https://github.com/sourcefrog/cargo-mutants): it runs the engine,
5//! reads its `outcomes.json`, and reports the **surviving** mutants the suite failed
6//! to catch.
7//!
8//! The gate is **binary, not a percentage** (equivalent mutants make a fixed score
9//! unreachable, and a score isn't comparable across engines) and on by default: any
10//! *un-exempted* surviving mutant is a finding. This module stays a pure measurement —
11//! [`measure_rust`] returns the survivors and [`unexplained_survivors`] is the pure
12//! core over a parsed report; the CLI layer turns a non-empty result into the failure.
13//!
14//! Diff-scoping (`--base`) is delegated to cargo-mutants' own `--in-diff`: the
15//! `<base>...HEAD` diff is written out and passed through, so only mutants on changed
16//! lines are tested ("no unexplained surviving mutant on the lines you touched").
17
18use std::collections::{BTreeMap, BTreeSet};
19use std::path::{Path, PathBuf};
20use std::process::Command;
21use std::sync::atomic::{AtomicU64, Ordering};
22
23use anyhow::{bail, Context, Result};
24use serde::Deserialize;
25
26/// A surviving mutant — a mutation the unit suite ran but failed to catch.
27#[derive(Debug, Clone, PartialEq, Eq)]
28pub struct Survivor {
29    /// The mutated file, as cargo-mutants reports it (crate-root-relative, `/`-separated).
30    pub file: String,
31    /// The 1-based line the mutation starts on.
32    pub line: u32,
33    /// cargo-mutants' human description (e.g. `replace > with == in is_positive`).
34    pub description: String,
35}
36
37/// The `(file, line)` locations an engine produced a viable mutant for — the input the
38/// #226 line-scoped guard reads to tell an over-exemption (a listed line whose mutants
39/// were all caught) from an out-of-scope line (no mutant there).
40pub type MutatedLines = BTreeSet<(String, u32)>;
41
42/// One mutation run's raw output: the surviving mutants and every viable mutant's
43/// location ([`MutatedLines`]).
44type RunOutcome = (Vec<Survivor>, MutatedLines);
45
46/// A cargo-mutants `outcomes.json` export, pared to what the rule reads. Unmodeled
47/// fields (`total_mutants`, `caught`, timings, …) are ignored.
48#[derive(Debug, Clone, Deserialize)]
49pub struct MutantsReport {
50    pub outcomes: Vec<MutantOutcome>,
51}
52
53/// One scenario's outcome. `summary` is cargo-mutants' result word — `Success` for the
54/// unmutated baseline, `CaughtMutant` / `MissedMutant` (and `Timeout` / `Unviable`)
55/// for each mutant.
56#[derive(Debug, Clone, Deserialize)]
57pub struct MutantOutcome {
58    pub summary: String,
59    pub scenario: Scenario,
60}
61
62/// The scenario a result came from: the unmutated baseline, or one mutant. Matches
63/// cargo-mutants' externally-tagged JSON (`"Baseline"` vs `{"Mutant": {…}}`).
64#[derive(Debug, Clone, Deserialize)]
65pub enum Scenario {
66    Baseline,
67    Mutant(MutantInfo),
68}
69
70/// The mutant a scenario describes, pared to the location + description the report
71/// needs. cargo-mutants also carries `function`, `genre`, `package`, `replacement`;
72/// those are ignored.
73#[derive(Debug, Clone, Deserialize)]
74pub struct MutantInfo {
75    pub file: String,
76    pub span: Span,
77    pub name: String,
78}
79
80/// A source span; only the start line is read.
81#[derive(Debug, Clone, Deserialize)]
82pub struct Span {
83    pub start: LineCol,
84}
85
86/// A line/column position; only the line is read.
87#[derive(Debug, Clone, Deserialize)]
88pub struct LineCol {
89    pub line: u32,
90}
91
92/// Parse a cargo-mutants `outcomes.json` export.
93pub fn parse_mutants_report(json: &str) -> Result<MutantsReport> {
94    serde_json::from_str(json).context("parsing cargo-mutants outcomes.json")
95}
96
97/// The surviving mutants not lifted by a `mutation` exemption — the rule's findings.
98///
99/// A survivor is a `MissedMutant` outcome (the suite ran the mutated code but no test
100/// failed). `exempt` is the resolved set of `mutation`-rule exempt paths (crate-root
101/// relative); a survivor in an exempt file is dropped (an equivalent or deliberately
102/// defensive mutation, lifted with a reason). `Timeout` / `Unviable` are *not*
103/// survivors — a timeout is inconclusive, not a pass, and an unviable mutant never
104/// compiled.
105pub fn unexplained_survivors(report: &MutantsReport, exempt: &[String]) -> Vec<Survivor> {
106    evaluate(cargo_mutants_survivors(report), exempt)
107}
108
109/// The surviving mutants in a cargo-mutants report — the raw list before exemptions.
110/// A survivor is a `MissedMutant` outcome (the suite ran the mutated code but no test
111/// failed). `Timeout` / `Unviable` are not survivors.
112fn cargo_mutants_survivors(report: &MutantsReport) -> Vec<Survivor> {
113    report
114        .outcomes
115        .iter()
116        .filter_map(|outcome| {
117            if outcome.summary != "MissedMutant" {
118                return None;
119            }
120            let Scenario::Mutant(mutant) = &outcome.scenario else {
121                return None;
122            };
123            Some(Survivor {
124                file: mutant.file.clone(),
125                line: mutant.span.start.line,
126                description: mutant.name.clone(),
127            })
128        })
129        .collect()
130}
131
132/// The `(file, line)` locations cargo-mutants produced a **viable, conclusive** mutant
133/// for — caught or missed (`CaughtMutant` / `MissedMutant`), not the inconclusive
134/// `Timeout` / `Unviable`. The #226 line-scoped guard reads this to tell an
135/// over-exemption (a listed line whose mutants were all *caught*, no survivor) from an
136/// out-of-scope line (no mutant there at all — e.g. outside a `--base` diff).
137pub fn mutated_lines(report: &MutantsReport) -> MutatedLines {
138    report
139        .outcomes
140        .iter()
141        .filter_map(|outcome| {
142            if outcome.summary != "CaughtMutant" && outcome.summary != "MissedMutant" {
143                return None;
144            }
145            let Scenario::Mutant(mutant) = &outcome.scenario else {
146                return None;
147            };
148            Some((mutant.file.clone(), mutant.span.start.line))
149        })
150        .collect()
151}
152
153/// The shared whole-file evaluation core: drop the survivors lifted by a file-level
154/// `mutation` exemption (a file-path match), leaving the rule's findings. The
155/// line-scoped path ([`evaluate_scoped`]) generalizes this to per-line exemptions with
156/// a determinism guard.
157pub fn evaluate(survivors: Vec<Survivor>, exempt: &[String]) -> Vec<Survivor> {
158    survivors
159        .into_iter()
160        .filter(|survivor| !exempt.iter().any(|path| path == &survivor.file))
161        .collect()
162}
163
164/// Apply file- and line-scoped `mutation` exemptions to the raw `survivors`, with the
165/// #226 determinism guard. `mutated` is the set of `(file, line)` that produced a
166/// viable mutant (caught or survived); `whole_file` is the file-level exemptions and
167/// `line_scoped` the per-line ones.
168///
169/// Guard: a line-scoped exemption that names a line whose mutants were **all caught**
170/// (in `mutated`, but with no survivor) is over-exemption — a hard error, the
171/// counterpart to the stale-path rule. A listed line with **no** mutant at all is left
172/// alone (it may simply be outside a `--base` diff), neither an error nor a drop. Then
173/// every survivor whose file is whole-file-exempt, or whose `(file, line)` is
174/// line-exempt, is dropped; an unlisted survivor still fails the gate.
175pub fn evaluate_scoped(
176    survivors: Vec<Survivor>,
177    mutated: &MutatedLines,
178    whole_file: &[String],
179    line_scoped: &BTreeMap<String, BTreeSet<u32>>,
180) -> Result<Vec<Survivor>> {
181    let mut over: Vec<String> = Vec::new();
182    for (file, lines) in line_scoped {
183        for &line in lines {
184            let has_survivor = survivors
185                .iter()
186                .any(|survivor| survivor.file == *file && survivor.line == line);
187            if has_survivor {
188                continue;
189            }
190            if mutated.contains(&(file.clone(), line)) {
191                over.push(format!("\n  {file}:{line}"));
192            }
193        }
194    }
195    if !over.is_empty() {
196        bail!(
197            "a line-scoped mutation exemption may only list a line with a surviving mutant, but \
198             these had mutants that were all caught:{}",
199            over.concat()
200        );
201    }
202    Ok(survivors
203        .into_iter()
204        .filter(|survivor| {
205            let whole = whole_file.iter().any(|path| path == &survivor.file);
206            let line = line_scoped
207                .get(&survivor.file)
208                .is_some_and(|lines| lines.contains(&survivor.line));
209            !(whole || line)
210        })
211        .collect())
212}
213
214/// Run cargo-mutants over the crate at `root` and return its un-exempted survivors.
215///
216/// With `base` set, only mutants on the `<base>...HEAD` changed lines are tested (via
217/// cargo-mutants' `--in-diff`); without it, the whole crate. `exempt` is the file-level
218/// `mutation` exempt paths and `exempt_lines` the line-scoped ones (#226), applied with
219/// the determinism guard in [`evaluate_scoped`]. `cargo-mutants` must be installed.
220pub fn measure_rust(
221    root: &Path,
222    exempt: &[String],
223    exempt_lines: &BTreeMap<String, BTreeSet<u32>>,
224    base: Option<&str>,
225) -> Result<Vec<Survivor>> {
226    let out = MutantsOut::new();
227    let diff = match base {
228        // An empty diff (no changed lines under the crate — a PR that doesn't touch it)
229        // means nothing to mutate: no survivors, no cargo-mutants run.
230        Some(base) => match write_base_diff(root, base, &out)? {
231            None => return Ok(Vec::new()),
232            Some(path) => Some(path),
233        },
234        None => None,
235    };
236    run_cargo_mutants(root, &out.0, diff.as_deref())?;
237    let outcomes = out.0.join("mutants.out").join("outcomes.json");
238    // cargo-mutants writes no `outcomes.json` when a run produces no mutants (e.g. an
239    // `--in-diff` that matches none of the crate's lines). `run_cargo_mutants` already
240    // bailed on a fatal exit, so a missing report here is "no mutants" → no survivors.
241    let json = match std::fs::read_to_string(&outcomes) {
242        Ok(json) => json,
243        Err(_) => return Ok(Vec::new()),
244    };
245    let report = parse_mutants_report(&json)?;
246    evaluate_scoped(
247        cargo_mutants_survivors(&report),
248        &mutated_lines(&report),
249        exempt,
250        exempt_lines,
251    )
252}
253
254/// A Stryker `mutation.json` report (the mutation-testing-elements schema), pared to
255/// the fields the rule reads. Unmodeled keys (`schemaVersion`, `thresholds`, `source`,
256/// `testFiles`, `projectRoot`, `config`, …) are ignored.
257#[derive(Debug, Clone, Deserialize)]
258pub struct StrykerReport {
259    /// Per-file mutants, keyed by project-relative, `/`-separated path.
260    pub files: BTreeMap<String, StrykerFile>,
261}
262
263/// One file's mutants in a Stryker report.
264#[derive(Debug, Clone, Deserialize)]
265pub struct StrykerFile {
266    #[serde(default)]
267    pub mutants: Vec<StrykerMutant>,
268}
269
270/// One mutant, pared to the location + status + description the rule needs. Stryker
271/// also carries `id`, `coveredBy`, `static`, `testsCompleted`, …; those are ignored.
272#[derive(Debug, Clone, Deserialize)]
273#[serde(rename_all = "camelCase")]
274pub struct StrykerMutant {
275    pub mutator_name: String,
276    #[serde(default)]
277    pub replacement: Option<String>,
278    pub status: String,
279    pub location: StrykerLocation,
280}
281
282/// A mutant's source location; only the start line is read (reusing [`LineCol`], whose
283/// extra `column` field Stryker also provides and serde ignores).
284#[derive(Debug, Clone, Deserialize)]
285pub struct StrykerLocation {
286    pub start: LineCol,
287}
288
289/// Parse a Stryker `mutation.json` report.
290pub fn parse_stryker_report(json: &str) -> Result<StrykerReport> {
291    serde_json::from_str(json).context("parsing Stryker mutation.json")
292}
293
294/// The surviving mutants in a Stryker report — the raw list before exemptions.
295///
296/// A survivor is a `Survived` mutant (a test ran the mutated code but none failed) or a
297/// `NoCoverage` one (no test exercised it at all — worse). `Killed` / `Timeout` are
298/// caught; `CompileError` / `RuntimeError` never produced a viable mutant; `Ignored` /
299/// `Pending` are out of scope. (Mirrors the cargo-mutants `MissedMutant`-only rule.)
300pub fn stryker_survivors(report: &StrykerReport) -> Vec<Survivor> {
301    let mut survivors = Vec::new();
302    for (file, contents) in &report.files {
303        for mutant in &contents.mutants {
304            if mutant.status != "Survived" && mutant.status != "NoCoverage" {
305                continue;
306            }
307            let description = match &mutant.replacement {
308                Some(replacement) => {
309                    format!("{} (-> {})", mutant.mutator_name, one_line(replacement))
310                }
311                None => mutant.mutator_name.clone(),
312            };
313            survivors.push(Survivor {
314                file: file.clone(),
315                line: mutant.location.start.line,
316                description,
317            });
318        }
319    }
320    survivors
321}
322
323/// Collapse a (possibly multi-line) replacement to a single trimmed line, capped, so a
324/// survivor's one-line description stays readable.
325fn one_line(replacement: &str) -> String {
326    let flat = replacement.split_whitespace().collect::<Vec<_>>().join(" ");
327    const MAX: usize = 60;
328    if flat.chars().count() > MAX {
329        format!("{}…", flat.chars().take(MAX).collect::<String>())
330    } else {
331        flat
332    }
333}
334
335/// Run Stryker over the TypeScript project at `root` and return its un-exempted
336/// survivors — the TS arm of the mutation rule (#202), parity with [`measure_rust`].
337///
338/// With `base` set, only mutants on the `<base>...HEAD` changed lines are tested —
339/// Stryker has no native git-diff mode, so the changed lines become `--mutate
340/// <file>:<line>-<line>` ranges (line granularity, matching cargo-mutants' `--in-diff`).
341/// Without it, the project's configured `mutate` set runs. `exempt` is the file-level
342/// exempt paths and `exempt_lines` the line-scoped ones (#226). Stryker must be
343/// installed / resolvable.
344pub fn measure_typescript(
345    root: &Path,
346    exempt: &[String],
347    exempt_lines: &BTreeMap<String, BTreeSet<u32>>,
348    base: Option<&str>,
349) -> Result<Vec<Survivor>> {
350    let mutate = match base {
351        Some(base) => {
352            let ranges = mutate_ranges(root, base)?;
353            // Nothing mutatable changed on the diff: no run, no survivors.
354            if ranges.is_empty() {
355                return Ok(Vec::new());
356            }
357            Some(ranges)
358        }
359        None => None,
360    };
361    let json = run_stryker(root, mutate.as_deref())?;
362    let report = parse_stryker_report(&json)?;
363    evaluate_scoped(
364        stryker_survivors(&report),
365        &stryker_mutated_lines(&report),
366        exempt,
367        exempt_lines,
368    )
369}
370
371/// The `(file, line)` locations Stryker produced a **viable** mutant for — caught,
372/// survived, no-coverage, or timed out, but not the unviable `CompileError` /
373/// `RuntimeError` (or the skipped `Ignored` / `Pending`). The #226 guard reads this the
374/// same way as cargo-mutants' [`mutated_lines`].
375fn stryker_mutated_lines(report: &StrykerReport) -> MutatedLines {
376    let mut mutated = BTreeSet::new();
377    for (file, contents) in &report.files {
378        for mutant in &contents.mutants {
379            if matches!(
380                mutant.status.as_str(),
381                "Killed" | "Survived" | "NoCoverage" | "Timeout"
382            ) {
383                mutated.insert((file.clone(), mutant.location.start.line));
384            }
385        }
386    }
387    mutated
388}
389
390/// Build the Stryker `--mutate` specs scoping a run to the `<base>...HEAD` changed
391/// lines: each mutatable source file's contiguous runs of changed lines become a
392/// `<file>:<start>-<end>` range (Stryker's line-range form). Reuses the patch-coverage
393/// diff parser. Test and declaration files are filtered out — Stryker's configured
394/// `mutate` set normally excludes them, but passing `--mutate` replaces that set.
395fn mutate_ranges(root: &Path, base: &str) -> Result<Vec<String>> {
396    let changed = crate::patch_coverage::changed_lines(root, base)?;
397    let mut specs = Vec::new();
398    for (file, lines) in changed {
399        if !is_mutatable_ts(&file) {
400            continue;
401        }
402        for (start, end) in contiguous_runs(&lines) {
403            specs.push(format!("{file}:{start}-{end}"));
404        }
405    }
406    Ok(specs)
407}
408
409/// Whether a changed file is a TypeScript/JavaScript *source* Stryker should mutate — a
410/// `.ts`/`.tsx`/`.mts`/`.cts`/`.js`/`.jsx`/`.mjs`/`.cjs` file that is not a declaration
411/// (`.d.ts`) or a test (`.test.` / `.spec.`).
412fn is_mutatable_ts(file: &str) -> bool {
413    let is_source = [".ts", ".tsx", ".mts", ".cts", ".js", ".jsx", ".mjs", ".cjs"]
414        .iter()
415        .any(|ext| file.ends_with(ext));
416    let is_decl = file.ends_with(".d.ts");
417    let is_test = file.contains(".test.") || file.contains(".spec.");
418    is_source && !is_decl && !is_test
419}
420
421/// Fold a sorted set of line numbers into inclusive `(start, end)` contiguous runs.
422fn contiguous_runs(lines: &BTreeSet<u64>) -> Vec<(u64, u64)> {
423    let mut runs: Vec<(u64, u64)> = Vec::new();
424    for &line in lines {
425        match runs.last_mut() {
426            Some(run) if run.1 + 1 == line => run.1 = line,
427            _ => runs.push((line, line)),
428        }
429    }
430    runs
431}
432
433/// Run Stryker over `root` (resolving the project's own config) with the json reporter,
434/// returning the contents of its `mutation.json`. `mutate`, when set, scopes the run to
435/// `--mutate` line ranges. The report goes to Stryker's default
436/// `reports/mutation/mutation.json`; it's read and then pruned (the file and any empty
437/// parents) so the scanned tree stays pristine and a populated `reports/` is untouched.
438///
439/// Stryker's exit code is *not* trusted to mean "no survivors": a configured
440/// `thresholds.break` makes it exit non-zero on a low score, which is exactly the
441/// survivor case the rule reports on. So the report is read whenever it exists; only a
442/// missing report (a real run failure) is fatal.
443fn run_stryker(root: &Path, mutate: Option<&[String]>) -> Result<String> {
444    let report_path = root.join("reports").join("mutation").join("mutation.json");
445    let _cleanup = ReportCleanup(report_path.clone());
446    // Drop any stale report so a previous run's output is never mistaken for this one's.
447    let _ = std::fs::remove_file(&report_path);
448
449    let mut command = Command::new("npx");
450    command
451        .current_dir(root)
452        .args(["--yes", "stryker", "run", "--reporters", "json"]);
453    if let Some(specs) = mutate {
454        command.arg("--mutate").arg(specs.join(","));
455    }
456    let output = command
457        .env("CI", "1")
458        .output()
459        .context("running `npx stryker run` (is @stryker-mutator/core installed?)")?;
460
461    std::fs::read_to_string(&report_path).map_err(|_| {
462        anyhow::anyhow!(
463            "Stryker produced no report in `{}` (did it run cleanly?):\n{}{}",
464            root.display(),
465            String::from_utf8_lossy(&output.stdout),
466            String::from_utf8_lossy(&output.stderr),
467        )
468    })
469}
470
471/// Removes the Stryker json report on drop, pruning the `mutation/` and `reports/`
472/// parents only if they're left empty — so a user's own populated `reports/` survives.
473struct ReportCleanup(PathBuf);
474
475impl Drop for ReportCleanup {
476    fn drop(&mut self) {
477        let _ = std::fs::remove_file(&self.0);
478        if let Some(mutation_dir) = self.0.parent() {
479            // `remove_dir` only succeeds on an empty dir, so populated trees are safe.
480            let _ = std::fs::remove_dir(mutation_dir);
481            if let Some(reports_dir) = mutation_dir.parent() {
482                let _ = std::fs::remove_dir(reports_dir);
483            }
484        }
485    }
486}
487
488/// One line of `cosmic-ray dump` output: a `[work_item, result]` pair. The result is
489/// absent (`null`) for an un-executed work item.
490#[derive(Debug, Clone, Deserialize)]
491pub struct CosmicRayLine(pub CrWorkItem, pub Option<CrResult>);
492
493/// A cosmic-ray work item, pared to its one mutation's location. (cosmic-ray models a
494/// list of mutations per item, but the operators here produce one apiece.)
495#[derive(Debug, Clone, Deserialize)]
496pub struct CrWorkItem {
497    pub mutations: Vec<CrMutation>,
498}
499
500/// One mutation, pared to the location + operator the rule reads. cosmic-ray also
501/// carries `occurrence`, `end_pos`, `operator_args`; those are ignored.
502#[derive(Debug, Clone, Deserialize)]
503pub struct CrMutation {
504    pub module_path: String,
505    pub operator_name: String,
506    /// `[line, column]`, 1-based line.
507    pub start_pos: (u32, u32),
508    #[serde(default)]
509    pub definition_name: Option<String>,
510}
511
512/// A work item's result; only the test outcome is read (`survived` / `killed` /
513/// `incompetent`).
514#[derive(Debug, Clone, Deserialize)]
515pub struct CrResult {
516    #[serde(default)]
517    pub test_outcome: Option<String>,
518}
519
520/// Parse `cosmic-ray dump` output (JSON Lines) into the surviving mutants — the raw
521/// list before exemptions.
522///
523/// A survivor is a work item whose result is `survived` (the suite ran the mutated code
524/// but no test failed). `killed` / `incompetent` (the mutant didn't run — e.g. a syntax
525/// error) are not survivors. (Mirrors the cargo-mutants `MissedMutant` / Stryker
526/// `Survived` rules.)
527pub fn parse_cosmic_ray_dump(dump: &str) -> Result<Vec<Survivor>> {
528    let mut survivors = Vec::new();
529    for line in dump.lines() {
530        if line.trim().is_empty() {
531            continue;
532        }
533        let CosmicRayLine(item, result) =
534            serde_json::from_str(line).context("parsing a cosmic-ray dump line")?;
535        let survived = matches!(result, Some(CrResult { test_outcome: Some(outcome) }) if outcome == "survived");
536        if !survived {
537            continue;
538        }
539        let Some(mutation) = item.mutations.first() else {
540            continue;
541        };
542        let definition = mutation.definition_name.as_deref().unwrap_or("<module>");
543        survivors.push(Survivor {
544            file: mutation.module_path.clone(),
545            line: mutation.start_pos.0,
546            description: format!("{} in {}", mutation.operator_name, definition),
547        });
548    }
549    Ok(survivors)
550}
551
552/// Run cosmic-ray over the Python project at `root` and return its un-exempted
553/// survivors — the Python arm of the mutation rule (#203), parity with [`measure_rust`]
554/// and [`measure_typescript`].
555///
556/// With `base` set, only mutants on the `<base>...HEAD` changed lines are reported:
557/// cosmic-ray has no native git-diff mode, so the run is scoped to the changed `.py`
558/// files (one session each) and the survivors are then filtered to the changed lines —
559/// line granularity, matching the other arms. Without it, the whole project's sources
560/// run (tests excluded). `exempt` is the file-level exempt paths and `exempt_lines` the
561/// line-scoped ones (#226). cosmic-ray + pytest must be installed.
562pub fn measure_python(
563    root: &Path,
564    exempt: &[String],
565    exempt_lines: &BTreeMap<String, BTreeSet<u32>>,
566    base: Option<&str>,
567) -> Result<Vec<Survivor>> {
568    let (survivors, mutated) = match base {
569        None => run_cosmic_ray(root, ".", &PY_TEST_EXCLUDES)?,
570        Some(base) => {
571            let changed = crate::patch_coverage::changed_lines(root, base)?;
572            let mut all_survivors = Vec::new();
573            let mut all_mutated = BTreeSet::new();
574            for (file, lines) in &changed {
575                if !is_mutatable_py(file) {
576                    continue;
577                }
578                // The file is a single non-test module, so no test exclusions are needed.
579                let (survivors, mutated) = run_cosmic_ray(root, file, &[])?;
580                for survivor in survivors {
581                    if lines.contains(&(survivor.line as u64)) {
582                        all_survivors.push(survivor);
583                    }
584                }
585                for (mutated_file, line) in mutated {
586                    if lines.contains(&u64::from(line)) {
587                        all_mutated.insert((mutated_file, line));
588                    }
589                }
590            }
591            (all_survivors, all_mutated)
592        }
593    };
594    evaluate_scoped(survivors, &mutated, exempt, exempt_lines)
595}
596
597/// The test/conftest globs excluded from whole-project mutation (cosmic-ray would
598/// otherwise mutate the suite itself).
599const PY_TEST_EXCLUDES: [&str; 3] = ["*_test.py", "test_*.py", "conftest.py"];
600
601/// Whether a changed file is a mutatable Python *source* — a `.py` that is not a test
602/// (`*_test.py` / `test_*.py`) or `conftest.py`.
603fn is_mutatable_py(file: &str) -> bool {
604    if !file.ends_with(".py") {
605        return false;
606    }
607    let base = file.rsplit('/').next().unwrap_or(file);
608    !(base.ends_with("_test.py") || base.starts_with("test_") || base == "conftest.py")
609}
610
611/// Run one cosmic-ray session over `module_path` (relative to `root`) and return its
612/// surviving mutants **and** the `(file, line)` set of every viable (executed) mutant
613/// — the latter for the #226 line-scoped guard. A baseline check runs first so a suite
614/// that fails *unmutated* errors rather than reporting a false "all killed". The
615/// cosmic-ray config and session DB live in an out-of-tree temp dir; cosmic-ray mutates
616/// each file in place and reverts it, so the scanned tree is left as it was.
617fn run_cosmic_ray(root: &Path, module_path: &str, excluded_modules: &[&str]) -> Result<RunOutcome> {
618    let dir = CosmicRayDir::new();
619    std::fs::create_dir_all(&dir.0).context("creating the cosmic-ray temp dir")?;
620    let config = dir.0.join("cr.toml");
621    let session = dir.0.join("session.sqlite");
622
623    let excludes = excluded_modules
624        .iter()
625        .map(|glob| format!("\"{glob}\""))
626        .collect::<Vec<_>>()
627        .join(", ");
628    std::fs::write(
629        &config,
630        format!(
631            "[cosmic-ray]\n\
632             module-path = \"{module_path}\"\n\
633             timeout = 30.0\n\
634             excluded-modules = [{excludes}]\n\
635             test-command = \"python3 -m pytest -q -p no:cacheprovider\"\n\
636             \n\
637             [cosmic-ray.distributor]\n\
638             name = \"local\"\n"
639        ),
640    )
641    .context("writing the cosmic-ray config")?;
642
643    // Baseline: the suite must pass unmutated, or every mutant would "die" on the
644    // already-failing tests and we'd report a false pass.
645    let baseline = cosmic_ray(root, &["baseline", path_str(&config)])?;
646    if !baseline.status.success() {
647        bail!(
648            "the Python unit suite did not pass unmutated in `{}` (cosmic-ray baseline failed):\n{}{}",
649            root.display(),
650            String::from_utf8_lossy(&baseline.stdout),
651            String::from_utf8_lossy(&baseline.stderr),
652        );
653    }
654
655    let init = cosmic_ray(root, &["init", path_str(&config), path_str(&session)])?;
656    if !init.status.success() {
657        bail!(
658            "cosmic-ray init failed in `{}`:\n{}{}",
659            root.display(),
660            String::from_utf8_lossy(&init.stdout),
661            String::from_utf8_lossy(&init.stderr),
662        );
663    }
664    let exec = cosmic_ray(root, &["exec", path_str(&config), path_str(&session)])?;
665    if !exec.status.success() {
666        bail!(
667            "cosmic-ray exec failed in `{}`:\n{}{}",
668            root.display(),
669            String::from_utf8_lossy(&exec.stdout),
670            String::from_utf8_lossy(&exec.stderr),
671        );
672    }
673    let dump = cosmic_ray(root, &["dump", path_str(&session)])?;
674    if !dump.status.success() {
675        bail!(
676            "cosmic-ray dump failed in `{}`:\n{}",
677            root.display(),
678            String::from_utf8_lossy(&dump.stderr),
679        );
680    }
681    let stdout = String::from_utf8_lossy(&dump.stdout);
682    Ok((
683        parse_cosmic_ray_dump(&stdout)?,
684        cosmic_ray_mutated_lines(&stdout)?,
685    ))
686}
687
688/// The `(file, line)` locations cosmic-ray ran a **viable** (executed) mutant for —
689/// `survived` or `killed`, not the `incompetent` mutants that never ran (a syntax
690/// error) or the un-executed (`null`-result) work items. The #226 guard reads this the
691/// same way as the cargo-mutants / Stryker [`mutated_lines`].
692pub fn cosmic_ray_mutated_lines(dump: &str) -> Result<MutatedLines> {
693    let mut mutated = BTreeSet::new();
694    for line in dump.lines() {
695        if line.trim().is_empty() {
696            continue;
697        }
698        let CosmicRayLine(item, result) =
699            serde_json::from_str(line).context("parsing a cosmic-ray dump line")?;
700        let outcome = result.and_then(|result| result.test_outcome);
701        if !matches!(outcome.as_deref(), Some("survived") | Some("killed")) {
702            continue;
703        }
704        if let Some(mutation) = item.mutations.first() {
705            mutated.insert((mutation.module_path.clone(), mutation.start_pos.0));
706        }
707    }
708    Ok(mutated)
709}
710
711/// Run a `cosmic-ray` subcommand in `root`, capturing its output. `PYTHONDONTWRITEBYTECODE`
712/// keeps `__pycache__` out of the scanned tree.
713fn cosmic_ray(root: &Path, args: &[&str]) -> Result<std::process::Output> {
714    Command::new("cosmic-ray")
715        .current_dir(root)
716        .args(args)
717        .env("PYTHONDONTWRITEBYTECODE", "1")
718        .output()
719        .context("running `cosmic-ray` (is it installed?)")
720}
721
722fn path_str(path: &Path) -> &str {
723    path.to_str().expect("temp path is valid UTF-8")
724}
725
726/// A unique temp dir for one cosmic-ray session's config + SQLite, removed on drop so
727/// the scanned tree stays pristine and parallel runs don't collide.
728struct CosmicRayDir(PathBuf);
729
730impl CosmicRayDir {
731    fn new() -> Self {
732        static COUNTER: AtomicU64 = AtomicU64::new(0);
733        let name = format!(
734            "testing-conventions-cosmic-ray-{}-{}",
735            std::process::id(),
736            COUNTER.fetch_add(1, Ordering::Relaxed),
737        );
738        CosmicRayDir(std::env::temp_dir().join(name))
739    }
740}
741
742impl Drop for CosmicRayDir {
743    fn drop(&mut self) {
744        let _ = std::fs::remove_dir_all(&self.0);
745    }
746}
747
748/// A unique temp dir for one cargo-mutants run's `--output`, removed on drop so the
749/// scanned crate stays pristine and parallel runs don't collide.
750struct MutantsOut(PathBuf);
751
752impl MutantsOut {
753    fn new() -> Self {
754        static COUNTER: AtomicU64 = AtomicU64::new(0);
755        let name = format!(
756            "testing-conventions-mutants-{}-{}",
757            std::process::id(),
758            COUNTER.fetch_add(1, Ordering::Relaxed),
759        );
760        MutantsOut(std::env::temp_dir().join(name))
761    }
762}
763
764impl Drop for MutantsOut {
765    fn drop(&mut self) {
766        let _ = std::fs::remove_dir_all(&self.0);
767    }
768}
769
770/// Write the `<base>...HEAD` diff cargo-mutants' `--in-diff` scopes to, returning its
771/// path — or `None` when the diff is empty (no changed lines under the crate).
772///
773/// `--relative` restricts the diff to changes under `root` (the crate dir) and makes
774/// the paths relative to it. cargo-mutants runs *in* the crate dir and matches its
775/// `--in-diff` paths crate-relative, so without `--relative` the diff is repo-relative
776/// and matches nothing whenever the crate is a subdirectory of the git repo (the common
777/// case). Scoping also means a PR that doesn't touch the crate yields an empty diff.
778fn write_base_diff(root: &Path, base: &str, out: &MutantsOut) -> Result<Option<PathBuf>> {
779    let range = format!("{base}...HEAD");
780    let output = Command::new("git")
781        .current_dir(root)
782        .args(["diff", "--relative", &range])
783        .output()
784        .context("running `git diff` for `--base` (is git installed?)")?;
785    if !output.status.success() {
786        bail!(
787            "git diff {range} failed: {}",
788            String::from_utf8_lossy(&output.stderr)
789        );
790    }
791    if output.stdout.is_empty() {
792        return Ok(None);
793    }
794    std::fs::create_dir_all(&out.0).context("creating the mutants output dir")?;
795    let path = out.0.join("base.diff");
796    std::fs::write(&path, &output.stdout).context("writing the base diff")?;
797    Ok(Some(path))
798}
799
800/// Run `cargo mutants --output <out> [--in-diff <diff>]` in `root`.
801///
802/// cargo-mutants exits `0` when every mutant is caught and `2` when some survive (or
803/// time out / are unviable) — both are normal here, since survivors are the rule's
804/// *output*, not an error. Any other code (usage error, or a baseline that didn't
805/// build/pass) is fatal. As with the coverage run, the outer instrumentation env is
806/// stripped so a nested run (this rule's own tests under `cargo llvm-cov`) doesn't
807/// re-enter the rustc wrapper and hang.
808fn run_cargo_mutants(root: &Path, out: &Path, in_diff: Option<&Path>) -> Result<()> {
809    let mut command = Command::new("cargo");
810    command
811        .current_dir(root)
812        .arg("mutants")
813        .arg("--output")
814        .arg(out);
815    if let Some(diff) = in_diff {
816        command.arg("--in-diff").arg(diff);
817    }
818    for var in [
819        "RUSTFLAGS",
820        "CARGO_ENCODED_RUSTFLAGS",
821        "RUSTDOCFLAGS",
822        "CARGO_ENCODED_RUSTDOCFLAGS",
823        "LLVM_PROFILE_FILE",
824        "CARGO_LLVM_COV",
825        "CARGO_LLVM_COV_SHOW_ENV",
826        "CARGO_LLVM_COV_TARGET_DIR",
827        "CARGO_LLVM_COV_BUILD_DIR",
828        "RUSTC_WRAPPER",
829        "RUSTC_WORKSPACE_WRAPPER",
830        "__CARGO_LLVM_COV_RUSTC_WRAPPER",
831        "__CARGO_LLVM_COV_RUSTC_WRAPPER_RUSTFLAGS",
832        "__CARGO_LLVM_COV_RUSTC_WRAPPER_CRATE_NAMES",
833    ] {
834        command.env_remove(var);
835    }
836    let output = command
837        .output()
838        .context("running `cargo mutants` (is cargo-mutants installed?)")?;
839    match output.status.code() {
840        // 0 = all caught, 2 = some survived/timed out: both produce a report to read.
841        Some(0) | Some(2) => Ok(()),
842        _ => bail!(
843            "cargo-mutants did not run cleanly in `{}` (baseline build/test failure?):\n{}{}",
844            root.display(),
845            String::from_utf8_lossy(&output.stdout),
846            String::from_utf8_lossy(&output.stderr),
847        ),
848    }
849}
850
851#[cfg(test)]
852mod tests {
853    use super::*;
854
855    // A pared `outcomes.json`: the baseline, one missed mutant, and one caught — the
856    // real shape (externally-tagged `scenario`, extra fields the rule ignores).
857    const SAMPLE: &str = r#"{
858        "outcomes": [
859            {"scenario": "Baseline", "summary": "Success",
860             "phase_results": []},
861            {"scenario": {"Mutant": {"file": "src/lib.rs", "package": "p", "genre": "FnValue",
862                "replacement": "true", "name": "src/lib.rs:7:7: replace > with == in is_positive",
863                "function": {"function_name": "is_positive"},
864                "span": {"start": {"line": 7, "column": 7}, "end": {"line": 7, "column": 8}}}},
865             "summary": "MissedMutant"},
866            {"scenario": {"Mutant": {"file": "src/other.rs", "package": "p", "genre": "FnValue",
867                "replacement": "0", "name": "src/other.rs:3:5: replace add -> i32 with 0",
868                "span": {"start": {"line": 3, "column": 5}, "end": {"line": 3, "column": 9}}}},
869             "summary": "CaughtMutant"}
870        ],
871        "total_mutants": 2
872    }"#;
873
874    #[test]
875    fn parses_the_outcomes_export() {
876        let report = parse_mutants_report(SAMPLE).expect("valid outcomes.json");
877        assert_eq!(report.outcomes.len(), 3);
878        assert!(matches!(report.outcomes[0].scenario, Scenario::Baseline));
879    }
880
881    #[test]
882    fn collects_only_missed_mutants_as_survivors() {
883        let report = parse_mutants_report(SAMPLE).unwrap();
884        let survivors = unexplained_survivors(&report, &[]);
885        // Only the MissedMutant — the baseline and the CaughtMutant are not survivors.
886        assert_eq!(survivors.len(), 1);
887        assert_eq!(survivors[0].file, "src/lib.rs");
888        assert_eq!(survivors[0].line, 7);
889        assert!(survivors[0].description.contains("replace > with =="));
890    }
891
892    #[test]
893    fn an_exemption_drops_a_survivor_in_that_file() {
894        let report = parse_mutants_report(SAMPLE).unwrap();
895        let exempt = vec!["src/lib.rs".to_string()];
896        assert!(unexplained_survivors(&report, &exempt).is_empty());
897    }
898
899    #[test]
900    fn an_exemption_on_another_file_leaves_the_survivor() {
901        let report = parse_mutants_report(SAMPLE).unwrap();
902        let exempt = vec!["src/elsewhere.rs".to_string()];
903        assert_eq!(unexplained_survivors(&report, &exempt).len(), 1);
904    }
905
906    // A pared Stryker `mutation.json`: one Survived, one NoCoverage, one Killed — the
907    // real shape (per-file `files` map, extra fields the rule ignores).
908    const STRYKER_SAMPLE: &str = r#"{
909        "schemaVersion": "1.0",
910        "files": {
911            "src/index.ts": {
912                "language": "typescript",
913                "source": "...",
914                "mutants": [
915                    {"id": "0", "mutatorName": "ConditionalExpression", "replacement": "true",
916                     "status": "Survived", "coveredBy": ["t0"],
917                     "location": {"start": {"line": 2, "column": 10}, "end": {"line": 2, "column": 15}}},
918                    {"id": "1", "mutatorName": "ArithmeticOperator", "replacement": "a - b",
919                     "status": "NoCoverage",
920                     "location": {"start": {"line": 5, "column": 3}, "end": {"line": 5, "column": 8}}},
921                    {"id": "2", "mutatorName": "BooleanLiteral", "replacement": "false",
922                     "status": "Killed",
923                     "location": {"start": {"line": 9, "column": 1}, "end": {"line": 9, "column": 6}}}
924                ]
925            }
926        }
927    }"#;
928
929    #[test]
930    fn parses_a_stryker_report() {
931        let report = parse_stryker_report(STRYKER_SAMPLE).expect("valid mutation.json");
932        assert_eq!(report.files["src/index.ts"].mutants.len(), 3);
933    }
934
935    #[test]
936    fn collects_survived_and_nocoverage_as_survivors() {
937        let report = parse_stryker_report(STRYKER_SAMPLE).unwrap();
938        let survivors = stryker_survivors(&report);
939        // Survived + NoCoverage are survivors; the Killed mutant is not.
940        assert_eq!(survivors.len(), 2);
941        assert!(survivors.iter().all(|s| s.file == "src/index.ts"));
942        assert_eq!(survivors[0].line, 2);
943        assert!(survivors[0].description.contains("ConditionalExpression"));
944        assert!(survivors[0].description.contains("true"));
945        assert_eq!(survivors[1].line, 5);
946    }
947
948    #[test]
949    fn evaluate_drops_exempt_files_for_either_engine() {
950        let report = parse_stryker_report(STRYKER_SAMPLE).unwrap();
951        let survivors = stryker_survivors(&report);
952        let exempt = vec!["src/index.ts".to_string()];
953        assert!(evaluate(survivors, &exempt).is_empty());
954    }
955
956    #[test]
957    fn is_mutatable_ts_keeps_sources_and_drops_tests_and_decls() {
958        assert!(is_mutatable_ts("src/index.ts"));
959        assert!(is_mutatable_ts("src/util.tsx"));
960        assert!(is_mutatable_ts("src/util.js"));
961        assert!(!is_mutatable_ts("src/index.test.ts"));
962        assert!(!is_mutatable_ts("src/index.spec.ts"));
963        assert!(!is_mutatable_ts("src/types.d.ts"));
964        assert!(!is_mutatable_ts("README.md"));
965    }
966
967    #[test]
968    fn contiguous_runs_collapses_adjacent_lines() {
969        let lines: BTreeSet<u64> = [2u64, 3, 4, 7, 9, 10].into_iter().collect();
970        assert_eq!(contiguous_runs(&lines), vec![(2, 4), (7, 7), (9, 10)]);
971        assert!(contiguous_runs(&BTreeSet::new()).is_empty());
972    }
973
974    #[test]
975    fn one_line_flattens_and_caps() {
976        assert_eq!(one_line("a -\n  b"), "a - b");
977        let long = "x".repeat(80);
978        let capped = one_line(&long);
979        assert!(capped.chars().count() <= 61 && capped.ends_with('…'));
980    }
981
982    // A pared `cosmic-ray dump`: each line is `[work_item, result]` — one survived
983    // comparison-operator mutant and one killed binary-operator mutant.
984    const COSMIC_RAY_DUMP: &str = concat!(
985        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"}]"#,
986        "\n",
987        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"}]"#,
988        "\n",
989    );
990
991    #[test]
992    fn collects_only_survived_cosmic_ray_mutants() {
993        let survivors = parse_cosmic_ray_dump(COSMIC_RAY_DUMP).expect("valid dump");
994        // Only the survived mutant — the killed one is not a survivor.
995        assert_eq!(survivors.len(), 1);
996        assert_eq!(survivors[0].file, "calc.py");
997        assert_eq!(survivors[0].line, 6);
998        assert!(survivors[0]
999            .description
1000            .contains("ReplaceComparisonOperator"));
1001        assert!(survivors[0].description.contains("is_positive"));
1002    }
1003
1004    #[test]
1005    fn an_unexecuted_cosmic_ray_item_is_not_a_survivor() {
1006        // A work item with a null result (never run) must not count as a survivor.
1007        let dump = r#"[{"mutations":[{"module_path":"calc.py","operator_name":"core/NumberReplacer","start_pos":[3,5],"end_pos":[3,6]}]},null]"#;
1008        assert!(parse_cosmic_ray_dump(dump).unwrap().is_empty());
1009    }
1010
1011    #[test]
1012    fn is_mutatable_py_keeps_sources_and_drops_tests() {
1013        assert!(is_mutatable_py("calc.py"));
1014        assert!(is_mutatable_py("pkg/util.py"));
1015        assert!(!is_mutatable_py("calc_test.py"));
1016        assert!(!is_mutatable_py("test_calc.py"));
1017        assert!(!is_mutatable_py("pkg/conftest.py"));
1018        assert!(!is_mutatable_py("README.md"));
1019    }
1020
1021    // --- line-scoped exemptions (#226) ---
1022
1023    #[test]
1024    fn mutated_lines_collects_caught_and_missed() {
1025        // The MissedMutant (src/lib.rs:7) and the CaughtMutant (src/other.rs:3) are both
1026        // viable, conclusive mutants; the Baseline is not.
1027        let report = parse_mutants_report(SAMPLE).unwrap();
1028        assert_eq!(
1029            mutated_lines(&report),
1030            [
1031                ("src/lib.rs".to_string(), 7),
1032                ("src/other.rs".to_string(), 3)
1033            ]
1034            .into_iter()
1035            .collect()
1036        );
1037    }
1038
1039    #[test]
1040    fn evaluate_scoped_drops_a_survivor_on_an_exempt_line() {
1041        let report = parse_mutants_report(SAMPLE).unwrap();
1042        let line_scoped = BTreeMap::from([("src/lib.rs".to_string(), BTreeSet::from([7u32]))]);
1043        let kept = evaluate_scoped(
1044            cargo_mutants_survivors(&report),
1045            &mutated_lines(&report),
1046            &[],
1047            &line_scoped,
1048        )
1049        .unwrap();
1050        assert!(
1051            kept.is_empty(),
1052            "the src/lib.rs:7 survivor should be lifted"
1053        );
1054    }
1055
1056    #[test]
1057    fn evaluate_scoped_rejects_exempting_a_caught_line() {
1058        // src/other.rs:3 had only a caught mutant (no survivor) — over-exemption.
1059        let report = parse_mutants_report(SAMPLE).unwrap();
1060        let line_scoped = BTreeMap::from([("src/other.rs".to_string(), BTreeSet::from([3u32]))]);
1061        let err = evaluate_scoped(
1062            cargo_mutants_survivors(&report),
1063            &mutated_lines(&report),
1064            &[],
1065            &line_scoped,
1066        )
1067        .unwrap_err();
1068        assert!(
1069            err.to_string().contains("all caught") && err.to_string().contains("src/other.rs:3"),
1070            "got: {err}"
1071        );
1072    }
1073
1074    #[test]
1075    fn evaluate_scoped_leaves_an_unmutated_listed_line_alone() {
1076        // Line 99 has no mutant at all (e.g. outside a `--base` diff) — neither an error
1077        // nor a drop; the real survivor on line 7 still stands.
1078        let report = parse_mutants_report(SAMPLE).unwrap();
1079        let line_scoped = BTreeMap::from([("src/lib.rs".to_string(), BTreeSet::from([99u32]))]);
1080        let kept = evaluate_scoped(
1081            cargo_mutants_survivors(&report),
1082            &mutated_lines(&report),
1083            &[],
1084            &line_scoped,
1085        )
1086        .unwrap();
1087        assert_eq!(kept.len(), 1);
1088        assert_eq!(kept[0].line, 7);
1089    }
1090
1091    #[test]
1092    fn evaluate_scoped_still_honors_a_whole_file_exemption() {
1093        let report = parse_mutants_report(SAMPLE).unwrap();
1094        let kept = evaluate_scoped(
1095            cargo_mutants_survivors(&report),
1096            &mutated_lines(&report),
1097            &["src/lib.rs".to_string()],
1098            &BTreeMap::new(),
1099        )
1100        .unwrap();
1101        assert!(kept.is_empty());
1102    }
1103
1104    #[test]
1105    fn stryker_mutated_lines_collects_every_viable_mutant() {
1106        // Survived (2), NoCoverage (5), and Killed (9) all ran; nothing is unviable here.
1107        let report = parse_stryker_report(STRYKER_SAMPLE).unwrap();
1108        assert_eq!(
1109            stryker_mutated_lines(&report),
1110            [
1111                ("src/index.ts".to_string(), 2),
1112                ("src/index.ts".to_string(), 5),
1113                ("src/index.ts".to_string(), 9),
1114            ]
1115            .into_iter()
1116            .collect()
1117        );
1118    }
1119
1120    #[test]
1121    fn cosmic_ray_mutated_lines_collects_executed_mutants() {
1122        // The survived (line 6) and killed (line 2) work items both ran.
1123        let mutated = cosmic_ray_mutated_lines(COSMIC_RAY_DUMP).unwrap();
1124        assert_eq!(
1125            mutated,
1126            [("calc.py".to_string(), 2), ("calc.py".to_string(), 6)]
1127                .into_iter()
1128                .collect()
1129        );
1130    }
1131}