Skip to main content

testing_conventions/
mutation.rs

1//! Mutation testing (`unit mutation`) — the rung above coverage: a test that *runs* a
2//! line still passes if you delete its assertions, and a surviving mutant proves it. Each
3//! language drives its engine through an adapter; this module measures, the CLI layer gates.
4
5use std::collections::{BTreeMap, BTreeSet};
6use std::ffi::OsString;
7use std::path::{Path, PathBuf};
8use std::process::{Command, Output};
9use std::sync::atomic::{AtomicU64, Ordering};
10
11use anyhow::{bail, Context, Result};
12use serde::Deserialize;
13
14use crate::colocated_test::Language;
15
16/// A surviving mutant — a mutation the unit suite ran but failed to catch.
17#[derive(Debug, Clone, PartialEq, Eq)]
18pub struct Survivor {
19    /// The mutated file, scan-path-relative and `/`-separated — cargo-mutants reports
20    /// workspace-root-relative paths, rebased onto the scan path before gating.
21    pub file: String,
22    /// The 1-based line the mutation starts on.
23    pub line: u32,
24    /// cargo-mutants' human description (e.g. `replace > with == in is_positive`).
25    pub description: String,
26}
27
28/// One mutation measurement: whether the engine ran, and what it found. Telling
29/// [`Measurement::EngineNotRun`] from an all-killed [`Measurement::Tested`] keeps a vacuous
30/// pass visible, and a counted pass carries its own evidence.
31#[derive(Debug, Clone, PartialEq, Eq)]
32pub enum Measurement {
33    /// The `--base` diff carried no mutatable changed lines; the engine never ran.
34    EngineNotRun,
35    /// The engine ran: `count` viable, conclusive mutants judged (caught or missed),
36    /// `survivors` the un-exempted surviving ones.
37    Tested {
38        count: usize,
39        survivors: Vec<Survivor>,
40    },
41}
42
43/// The `(file, line)` locations an engine produced a viable mutant for — the input the
44/// line-scoped guard reads to tell an over-exemption (a listed line whose mutants
45/// were all caught) from an out-of-scope line (no mutant there).
46pub type MutatedLines = BTreeSet<(String, u32)>;
47
48/// `true` when `file` (resolved against `base`) is a declaration-only module, per the same
49/// [`Language::is_subject`] predicate `colocated-test` uses. An unreadable file is never
50/// dropped, so it stays a survivor rather than vanishing silently.
51fn is_declaration_only(base: &Path, file: &str, language: Language) -> bool {
52    match std::fs::read_to_string(base.join(file)) {
53        Ok(source) => !language.is_subject(&source, Path::new(file)),
54        Err(_) => false,
55    }
56}
57
58/// A cargo-mutants `outcomes.json` export, pared to what the rule reads. Unmodeled
59/// fields (`total_mutants`, `caught`, timings, …) are ignored.
60#[derive(Debug, Clone, Deserialize)]
61pub struct MutantsReport {
62    pub outcomes: Vec<MutantOutcome>,
63}
64
65/// One scenario's outcome. `summary` is cargo-mutants' result word — `Success` for the
66/// unmutated baseline, `CaughtMutant` / `MissedMutant` (and `Timeout` / `Unviable`)
67/// for each mutant.
68#[derive(Debug, Clone, Deserialize)]
69pub struct MutantOutcome {
70    pub summary: String,
71    pub scenario: Scenario,
72}
73
74/// The scenario a result came from: the unmutated baseline, or one mutant. Matches
75/// cargo-mutants' externally-tagged JSON (`"Baseline"` vs `{"Mutant": {…}}`).
76#[derive(Debug, Clone, Deserialize)]
77pub enum Scenario {
78    Baseline,
79    Mutant(MutantInfo),
80}
81
82/// The mutant a scenario describes, pared to the location + description the report
83/// needs. cargo-mutants also carries `function`, `genre`, `package`, `replacement`;
84/// those are ignored.
85#[derive(Debug, Clone, Deserialize)]
86pub struct MutantInfo {
87    pub file: String,
88    pub span: Span,
89    pub name: String,
90}
91
92/// A source span; the start and end lines are read.
93#[derive(Debug, Clone, Deserialize)]
94pub struct Span {
95    pub start: LineCol,
96    pub end: LineCol,
97}
98
99/// A line/column position; only the line is read.
100#[derive(Debug, Clone, Deserialize)]
101pub struct LineCol {
102    pub line: u32,
103}
104
105/// Parse a cargo-mutants `outcomes.json` export.
106pub fn parse_mutants_report(json: &str) -> Result<MutantsReport> {
107    serde_json::from_str(json).context("parsing cargo-mutants outcomes.json")
108}
109
110/// Parse a `cargo mutants --list --json` export: the crate's discoverable mutants, each
111/// with its workspace-root-relative file and span.
112fn parse_mutants_list(json: &str) -> Result<Vec<MutantInfo>> {
113    serde_json::from_str(json).context("parsing the cargo-mutants mutant list")
114}
115
116/// The surviving mutants not lifted by a `mutation` exemption — the rule's findings.
117/// `exempt` is the resolved set of crate-root-relative exempt paths; a survivor in an
118/// exempt file is dropped.
119pub fn unexplained_survivors(report: &MutantsReport, exempt: &[String]) -> Vec<Survivor> {
120    evaluate(cargo_mutants_survivors(report), exempt)
121}
122
123/// The surviving mutants in a cargo-mutants report — the raw list before exemptions.
124/// A survivor is a `MissedMutant` outcome (the suite ran the mutated code but no test
125/// failed). `Timeout` / `Unviable` are not survivors.
126fn cargo_mutants_survivors(report: &MutantsReport) -> Vec<Survivor> {
127    report
128        .outcomes
129        .iter()
130        .filter_map(|outcome| {
131            if outcome.summary != "MissedMutant" {
132                return None;
133            }
134            let Scenario::Mutant(mutant) = &outcome.scenario else {
135                return None;
136            };
137            Some(Survivor {
138                file: mutant.file.clone(),
139                line: mutant.span.start.line,
140                description: strip_embedded_location(&mutant.name).to_string(),
141            })
142        })
143        .collect()
144}
145
146/// Strip the `file:line:col: ` prefix cargo-mutants embeds in a mutant's name; a rendered site
147/// already leads with `file:line:`, so keeping the prefix prints the location twice.
148fn strip_embedded_location(name: &str) -> &str {
149    let Some((location, description)) = name.split_once(": ") else {
150        return name;
151    };
152    let mut parts = location.rsplitn(3, ':');
153    let numeric = |part: Option<&str>| part.is_some_and(|p| p.parse::<u32>().is_ok());
154    if numeric(parts.next()) && numeric(parts.next()) && parts.next().is_some() {
155        description
156    } else {
157        name
158    }
159}
160
161/// The `(file, line)` locations cargo-mutants produced a **viable, conclusive** mutant for —
162/// caught or missed, not the inconclusive `Timeout` / `Unviable`. The line-scoped guard reads
163/// this to tell an over-exemption from a line that has no mutant at all.
164pub fn mutated_lines(report: &MutantsReport) -> MutatedLines {
165    report
166        .outcomes
167        .iter()
168        .filter_map(|outcome| {
169            if outcome.summary != "CaughtMutant" && outcome.summary != "MissedMutant" {
170                return None;
171            }
172            let Scenario::Mutant(mutant) = &outcome.scenario else {
173                return None;
174            };
175            Some((mutant.file.clone(), mutant.span.start.line))
176        })
177        .collect()
178}
179
180/// The number of viable, conclusive mutants in a cargo-mutants report — `CaughtMutant`
181/// plus `MissedMutant`, the same set [`mutated_lines`] reads. A passing run states this
182/// count as its evidence.
183fn conclusive_count(report: &MutantsReport) -> usize {
184    report
185        .outcomes
186        .iter()
187        .filter(|outcome| outcome.summary == "CaughtMutant" || outcome.summary == "MissedMutant")
188        .count()
189}
190
191/// The shared whole-file evaluation core: drop the survivors lifted by a file-level
192/// `mutation` exemption. [`evaluate_scoped`] generalizes this to per-line exemptions.
193pub fn evaluate(survivors: Vec<Survivor>, exempt: &[String]) -> Vec<Survivor> {
194    survivors
195        .into_iter()
196        .filter(|survivor| !exempt.iter().any(|path| path == &survivor.file))
197        .collect()
198}
199
200/// Apply file- and line-scoped `mutation` exemptions to the raw `survivors`, with the
201/// determinism guard: a listed line whose mutants were all *caught* is over-exemption and a
202/// hard error, while a listed line with no mutant is left alone (it may be off the diff).
203pub fn evaluate_scoped(
204    survivors: Vec<Survivor>,
205    mutated: &MutatedLines,
206    whole_file: &[String],
207    line_scoped: &BTreeMap<String, BTreeSet<u32>>,
208) -> Result<Vec<Survivor>> {
209    let mut over: Vec<String> = Vec::new();
210    for (file, lines) in line_scoped {
211        for &line in lines {
212            let has_survivor = survivors
213                .iter()
214                .any(|survivor| survivor.file == *file && survivor.line == line);
215            if has_survivor {
216                continue;
217            }
218            if mutated.contains(&(file.clone(), line)) {
219                over.push(format!("\n  {file}:{line}"));
220            }
221        }
222    }
223    if !over.is_empty() {
224        bail!(
225            "a line-scoped mutation exemption may only list a line with a surviving mutant, but \
226             these had mutants that were all caught:{}",
227            over.concat()
228        );
229    }
230    Ok(survivors
231        .into_iter()
232        .filter(|survivor| {
233            let whole = whole_file.iter().any(|path| path == &survivor.file);
234            let line = line_scoped
235                .get(&survivor.file)
236                .is_some_and(|lines| lines.contains(&survivor.line));
237            !(whole || line)
238        })
239        .collect())
240}
241
242/// A mutant's outcome, normalized across the engines (Stryker / cosmic-ray / cargo-mutants)
243/// so the Rust core gates on one representation instead of three report formats. The
244/// serialized form is `snake_case` (`no_coverage`, `compile_error`, …) — the adapters' wire contract.
245#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
246#[serde(rename_all = "snake_case")]
247pub enum MutantStatus {
248    /// A test ran the mutated code but none failed — a survivor.
249    Survived,
250    /// A test failed on the mutant — caught.
251    Killed,
252    /// No test exercised the mutant at all — a survivor (worse than `Survived`).
253    NoCoverage,
254    /// The mutant ran but the suite timed out — inconclusive, not a survivor (but viable).
255    Timeout,
256    /// The mutant never compiled — not a viable mutant.
257    CompileError,
258    /// The mutant errored at runtime before a test could judge it — not viable.
259    RuntimeError,
260}
261
262impl MutantStatus {
263    /// Whether this outcome is a **survivor** — a mutant the suite failed to catch
264    /// (`Survived` or `NoCoverage`). Mirrors the per-engine survivor rules.
265    fn is_survivor(self) -> bool {
266        matches!(self, MutantStatus::Survived | MutantStatus::NoCoverage)
267    }
268
269    /// Whether this came from a **viable, conclusive** mutant — one that actually ran, not one
270    /// that never compiled or errored out. The determinism guard reads this.
271    fn is_viable(self) -> bool {
272        matches!(
273            self,
274            MutantStatus::Survived
275                | MutantStatus::Killed
276                | MutantStatus::NoCoverage
277                | MutantStatus::Timeout
278        )
279    }
280
281    /// Whether the suite **judged** this mutant (`Survived` / `Killed` / `NoCoverage`) —
282    /// the conclusive set a passing run counts as its evidence. A `Timeout` ran but
283    /// judged nothing; `CompileError` / `RuntimeError` never produced a viable mutant.
284    fn is_conclusive(self) -> bool {
285        matches!(
286            self,
287            MutantStatus::Survived | MutantStatus::Killed | MutantStatus::NoCoverage
288        )
289    }
290}
291
292/// One mutant in the normalized result set: the engine-agnostic shape every language
293/// adapter emits. Extra fields an adapter includes are ignored.
294#[derive(Debug, Clone, Deserialize)]
295pub struct NormalizedMutant {
296    /// Project-relative, `/`-separated path of the mutated file.
297    pub file: String,
298    /// The 1-based line the mutant starts on.
299    pub line: u32,
300    /// The outcome, normalized across engines.
301    pub status: MutantStatus,
302    /// The engine's mutator/operator name (e.g. `ConditionalExpression`).
303    pub mutator: String,
304    /// The replacement text, when the engine reports one — used for a readable description.
305    #[serde(default)]
306    pub replacement: Option<String>,
307}
308
309/// Parse the normalized results an engine adapter emits — a flat JSON array of
310/// [`NormalizedMutant`].
311pub fn parse_normalized_results(json: &str) -> Result<Vec<NormalizedMutant>> {
312    serde_json::from_str(json).context("parsing normalized mutation results")
313}
314
315/// Gate a normalized result set: drop the survivors lifted by a file- or line-scoped
316/// `mutation` exemption (with the determinism guard), leaving the rule's findings. This is
317/// the engine-agnostic core each language arm feeds once its adapter has normalized.
318pub fn evaluate_normalized(
319    mutants: &[NormalizedMutant],
320    whole_file: &[String],
321    line_scoped: &BTreeMap<String, BTreeSet<u32>>,
322) -> Result<Vec<Survivor>> {
323    evaluate_scoped(
324        normalized_survivors(mutants),
325        &normalized_mutated_lines(mutants),
326        whole_file,
327        line_scoped,
328    )
329}
330
331/// The surviving mutants in a normalized result set — the raw list before exemptions.
332fn normalized_survivors(mutants: &[NormalizedMutant]) -> Vec<Survivor> {
333    mutants
334        .iter()
335        .filter(|mutant| mutant.status.is_survivor())
336        .map(|mutant| Survivor {
337            file: mutant.file.clone(),
338            line: mutant.line,
339            description: describe_normalized(mutant),
340        })
341        .collect()
342}
343
344/// The `(file, line)` of every viable, conclusive mutant in a normalized result set — the
345/// input the line-scoped guard in [`evaluate_scoped`] reads.
346fn normalized_mutated_lines(mutants: &[NormalizedMutant]) -> MutatedLines {
347    mutants
348        .iter()
349        .filter(|mutant| mutant.status.is_viable())
350        .map(|mutant| (mutant.file.clone(), mutant.line))
351        .collect()
352}
353
354/// The number of conclusive mutants in a normalized result set — the count a passing
355/// run states as its evidence, parity with [`conclusive_count`].
356fn normalized_conclusive_count(mutants: &[NormalizedMutant]) -> usize {
357    mutants
358        .iter()
359        .filter(|mutant| mutant.status.is_conclusive())
360        .count()
361}
362
363/// A one-line description for a normalized mutant: the mutator name, plus the replacement
364/// (flattened + capped via [`one_line`]) when the engine reported one.
365fn describe_normalized(mutant: &NormalizedMutant) -> String {
366    match &mutant.replacement {
367        Some(replacement) => format!("{} (-> {})", mutant.mutator, one_line(replacement)),
368        None => mutant.mutator.clone(),
369    }
370}
371
372/// Run cargo-mutants over the crate at `root` and return the [`Measurement`], or
373/// [`Measurement::EngineNotRun`] for a `base` diff that changes no lines — or no Rust source
374/// — under the crate. The tool provisions cargo-mutants itself ([`ensure_cargo_mutants`]).
375pub fn measure_rust(
376    root: &Path,
377    exempt: &[String],
378    exempt_lines: &BTreeMap<String, BTreeSet<u32>>,
379    base: Option<&str>,
380    features: &[String],
381) -> Result<Measurement> {
382    let out = MutantsOut::new();
383    // cargo-mutants addresses files relative to the crate's cargo workspace root, so both the
384    // `--in-diff` diff it consumes and the report paths it emits carry the scan path's
385    // workspace-relative prefix. A standalone crate is its own workspace root: no prefix.
386    let workspace_root = cargo_workspace_root(root)?;
387    let prefix = canonical_scan_prefix(root, &workspace_root);
388    let mut base_diff = None;
389    let diff = match base {
390        Some(base) => {
391            match write_base_diff(root, &workspace_root, prefix.as_deref(), base, &out)? {
392                None => return Ok(Measurement::EngineNotRun),
393                Some(path) => {
394                    let parsed = parse_base_diff(&read_base_diff(&path)?);
395                    if !parsed.files.iter().any(|file| file.ends_with(".rs")) {
396                        return Ok(Measurement::EngineNotRun);
397                    }
398                    base_diff = Some(parsed);
399                    Some(path)
400                }
401            }
402        }
403        None => None,
404    };
405    let engine = ensure_cargo_mutants()?;
406    let run = run_cargo_mutants(&engine, root, &out.0, diff.as_deref(), features)?;
407    let outcomes = out.0.join("mutants.out").join("outcomes.json");
408    // cargo-mutants writes no `outcomes.json` when a run produces no mutants, so a missing
409    // report here is a run that judged zero — legitimate only if none of the crate's mutants
410    // sits on the diff, which [`zero_mutant_verdict`] proves before the zero can stand.
411    let json = match std::fs::read_to_string(&outcomes) {
412        Ok(json) => json,
413        Err(_) => {
414            if let Some(diff) = &base_diff {
415                let listed: Vec<MutantInfo> =
416                    list_cargo_mutants(&engine, root, features, |command| command.output())?
417                        .into_iter()
418                        .filter(|mutant| {
419                            !is_declaration_only(&workspace_root, &mutant.file, Language::Rust)
420                        })
421                        .collect();
422                zero_mutant_verdict(&listed, diff, &run)?;
423            }
424            return Ok(Measurement::Tested {
425                count: 0,
426                survivors: Vec::new(),
427            });
428        }
429    };
430    let mut report = rebase_report_paths(parse_mutants_report(&json)?, prefix.as_deref());
431    report.outcomes.retain(|outcome| match &outcome.scenario {
432        Scenario::Baseline => true,
433        Scenario::Mutant(mutant) => !is_declaration_only(root, &mutant.file, Language::Rust),
434    });
435    let survivors = evaluate_scoped(
436        cargo_mutants_survivors(&report),
437        &mutated_lines(&report),
438        exempt,
439        exempt_lines,
440    )?;
441    Ok(Measurement::Tested {
442        count: conclusive_count(&report),
443        survivors,
444    })
445}
446
447/// Collapse a (possibly multi-line) replacement to a single trimmed line, capped, so a
448/// survivor's one-line description stays readable.
449fn one_line(replacement: &str) -> String {
450    let flat = replacement.split_whitespace().collect::<Vec<_>>().join(" ");
451    const MAX: usize = 60;
452    if flat.chars().count() > MAX {
453        format!("{}…", flat.chars().take(MAX).collect::<String>())
454    } else {
455        flat
456    }
457}
458
459/// Run the bundled TypeScript mutation adapter over the scan path at `root` and return the
460/// [`Measurement`] — the TS arm, parity with [`measure_rust`]. The adapter runs at the package
461/// root and its results are rebased scan-path-relative, so exemption paths match every check.
462pub fn measure_typescript(
463    root: &Path,
464    exempt: &[String],
465    exempt_lines: &BTreeMap<String, BTreeSet<u32>>,
466    base: Option<&str>,
467    adapter: &Path,
468) -> Result<Measurement> {
469    let package_root =
470        crate::tiers::package_root(root, "package.json").unwrap_or_else(|| root.to_path_buf());
471    let prefix = scan_prefix(root, &package_root);
472    let mutate = match base {
473        Some(base) => {
474            let ranges = mutate_ranges(root, base)?;
475            if ranges.is_empty() {
476                return Ok(Measurement::EngineNotRun);
477            }
478            Some(prefix_mutate_specs(ranges, prefix.as_deref()))
479        }
480        None => prefix.as_deref().map(scan_scoped_mutate_globs),
481    };
482    let test_files = prefix.as_deref().map(scan_scoped_test_file_globs);
483    let json = run_ts_adapter(
484        &package_root,
485        adapter,
486        mutate.as_deref(),
487        test_files.as_deref(),
488    )?;
489    let mut mutants = to_scan_relative(parse_normalized_results(&json)?, prefix.as_deref());
490    mutants.retain(|mutant| !is_declaration_only(root, &mutant.file, Language::TypeScript));
491    let survivors = evaluate_normalized(&mutants, exempt, exempt_lines)?;
492    Ok(Measurement::Tested {
493        count: normalized_conclusive_count(&mutants),
494        survivors,
495    })
496}
497
498/// The scan path relative to its package root, as a `/`-joined string. `None` when the scan
499/// path *is* the package root, which also covers a loose tree with no manifest.
500fn scan_prefix(root: &Path, package_root: &Path) -> Option<String> {
501    let rel = root.strip_prefix(package_root).ok()?;
502    let parts: Vec<String> = rel
503        .components()
504        .map(|part| part.as_os_str().to_string_lossy().into_owned())
505        .collect();
506    if parts.is_empty() {
507        None
508    } else {
509        Some(parts.join("/"))
510    }
511}
512
513/// Prefix diff-scoped mutate specs (`<file>:<start>-<end>`, scan-path-relative) with the
514/// scan prefix, so they address the same files from the package root the adapter runs at.
515fn prefix_mutate_specs(specs: Vec<String>, prefix: Option<&str>) -> Vec<String> {
516    match prefix {
517        None => specs,
518        Some(prefix) => specs
519            .into_iter()
520            .map(|spec| format!("{prefix}/{spec}"))
521            .collect(),
522    }
523}
524
525/// Stryker's default `mutate` set re-rooted at the scan path: every source under it except
526/// test files and `__tests__` trees — the same shape Stryker itself defaults to for
527/// `{src,lib}`, addressed from the package root the adapter runs at.
528fn scan_scoped_mutate_globs(prefix: &str) -> Vec<String> {
529    const EXTENSIONS: &str = "+(cjs|mjs|js|ts|mts|cts|jsx|tsx|html|vue|svelte)";
530    vec![
531        format!("{prefix}/**/!(*.+(s|S)pec|*.+(t|T)est).{EXTENSIONS}"),
532        format!("!{prefix}/**/__tests__/**/*.{EXTENSIONS}"),
533    ]
534}
535
536/// The test files under the scan path, addressed from the package root the adapter runs at.
537/// Stryker matches these against the project's input files and hands the runner that subset,
538/// so vitest stays rooted at the package root and its own `include` resolves unchanged.
539fn scan_scoped_test_file_globs(prefix: &str) -> Vec<String> {
540    vec![format!("{prefix}/**")]
541}
542
543/// Rebase package-root-relative mutant paths onto the scan path: strip the scan prefix so
544/// exemption matching and the reported survivors address scan-path-relative files, as every
545/// other check does. A mutant outside the scan path is outside the gate's scope and dropped.
546fn to_scan_relative(mutants: Vec<NormalizedMutant>, prefix: Option<&str>) -> Vec<NormalizedMutant> {
547    let Some(prefix) = prefix else {
548        return mutants;
549    };
550    let prefix = format!("{prefix}/");
551    mutants
552        .into_iter()
553        .filter_map(|mut mutant| {
554            mutant.file = mutant.file.strip_prefix(&prefix)?.to_string();
555            Some(mutant)
556        })
557        .collect()
558}
559
560/// The checked working directory for an adapter run rooted at `root`, for the named `engine`.
561/// [`crate::tiers::package_root`] hands back `""` for a relative scan path like `src`, and
562/// `Command::current_dir("")` fails with the same ENOENT a missing interpreter gives.
563fn adapter_cwd<'a>(root: &'a Path, engine: &str) -> Result<&'a Path> {
564    let cwd = if root.as_os_str().is_empty() {
565        Path::new(".")
566    } else {
567        root
568    };
569    if !cwd.is_dir() {
570        bail!(
571            "the {engine} mutation adapter's working directory `{}` is not a directory",
572            cwd.display()
573        );
574    }
575    Ok(cwd)
576}
577
578/// The context a failed adapter spawn carries. `Command::output()` surfaces a bare ENOENT
579/// that names nothing, so the message names every path the spawn used: the interpreter, the
580/// entry point it was handed, and the directory it ran in.
581fn spawn_context(interpreter: &str, entry: &str, cwd: &Path) -> String {
582    format!(
583        "spawning `{interpreter} {entry}` in `{}` (is `{interpreter}` on PATH?)",
584        cwd.display()
585    )
586}
587
588/// Run the bundled TS mutation `adapter` at `package_root` and return the normalized-results
589/// JSON it writes. Results go to a temp file the adapter names via `--out`, so Stryker's own
590/// stdout logging can't corrupt them; a non-zero adapter exit surfaces its captured output.
591fn run_ts_adapter(
592    package_root: &Path,
593    adapter: &Path,
594    mutate: Option<&[String]>,
595    test_files: Option<&[String]>,
596) -> Result<String> {
597    let out = AdapterOut::new();
598    std::fs::create_dir_all(&out.0).context("creating the mutation adapter output dir")?;
599    let results = out.0.join("results.json");
600
601    let cwd = adapter_cwd(package_root, "TypeScript")?;
602
603    let mut command = Command::new("node");
604    command
605        .current_dir(cwd)
606        .arg(adapter)
607        .arg("--out")
608        .arg(&results);
609    if let Some(specs) = mutate {
610        command.arg("--mutate").arg(specs.join(","));
611    }
612    if let Some(globs) = test_files {
613        command.arg("--test-files").arg(globs.join(","));
614    }
615    let output =
616        command
617            .output()
618            .context(spawn_context("node", &adapter.display().to_string(), cwd))?;
619    if !output.status.success() {
620        bail!(
621            "the TypeScript mutation adapter failed in `{}`:\n{}{}",
622            cwd.display(),
623            String::from_utf8_lossy(&output.stdout),
624            String::from_utf8_lossy(&output.stderr),
625        );
626    }
627    read_adapter_results(&results, "TypeScript")
628}
629
630/// The adapter's written results JSON, read back after a successful run.
631fn read_adapter_results(results: &Path, engine: &str) -> Result<String> {
632    std::fs::read_to_string(results).with_context(|| {
633        format!(
634            "reading the {engine} mutation adapter's results from `{}`",
635            results.display()
636        )
637    })
638}
639
640/// A unique temp dir for one TS mutation adapter run's `--out` JSON, removed on drop so
641/// the scanned project stays pristine and parallel runs don't collide.
642struct AdapterOut(PathBuf);
643
644impl AdapterOut {
645    fn new() -> Self {
646        static COUNTER: AtomicU64 = AtomicU64::new(0);
647        let name = format!(
648            "testing-conventions-ts-adapter-{}-{}",
649            std::process::id(),
650            COUNTER.fetch_add(1, Ordering::Relaxed),
651        );
652        AdapterOut(std::env::temp_dir().join(name))
653    }
654}
655
656impl Drop for AdapterOut {
657    fn drop(&mut self) {
658        let _ = std::fs::remove_dir_all(&self.0);
659    }
660}
661
662/// Build the Stryker `--mutate` specs scoping a run to the `<base>...HEAD` changed lines, as
663/// `<file>:<start>-<end>` ranges. Test and declaration files are filtered out here because
664/// passing `--mutate` replaces Stryker's configured set rather than narrowing it.
665fn mutate_ranges(root: &Path, base: &str) -> Result<Vec<String>> {
666    let changed = crate::patch_coverage::changed_lines(root, base)?;
667    let mut specs = Vec::new();
668    for (file, lines) in changed {
669        if !is_mutatable_ts(&file) || is_declaration_only(root, &file, Language::TypeScript) {
670            continue;
671        }
672        for (start, end) in contiguous_runs(&lines) {
673            specs.push(format!("{file}:{start}-{end}"));
674        }
675    }
676    Ok(specs)
677}
678
679/// Whether a changed file is a TypeScript/JavaScript *source* Stryker should mutate — a
680/// `.ts`/`.tsx`/`.mts`/`.cts`/`.js`/`.jsx`/`.mjs`/`.cjs` file that is not a declaration
681/// (`.d.ts`) or a test (`.test.` / `.spec.`).
682fn is_mutatable_ts(file: &str) -> bool {
683    let is_source = [".ts", ".tsx", ".mts", ".cts", ".js", ".jsx", ".mjs", ".cjs"]
684        .iter()
685        .any(|ext| file.ends_with(ext));
686    let is_decl = file.ends_with(".d.ts");
687    let is_test = file.contains(".test.") || file.contains(".spec.");
688    is_source && !is_decl && !is_test
689}
690
691/// Fold a sorted set of line numbers into inclusive `(start, end)` contiguous runs.
692fn contiguous_runs(lines: &BTreeSet<u64>) -> Vec<(u64, u64)> {
693    let mut runs: Vec<(u64, u64)> = Vec::new();
694    for &line in lines {
695        match runs.last_mut() {
696            Some(run) if run.1 + 1 == line => run.1 = line,
697            _ => runs.push((line, line)),
698        }
699    }
700    runs
701}
702
703/// Run the bundled Python mutation adapter over the project at `root` and return the
704/// [`Measurement`] — the Python arm, parity with [`measure_rust`]. maturin ships the binary
705/// directly, so it invokes the adapter as a module resolved from the wheel's own environment.
706pub fn measure_python(
707    root: &Path,
708    exempt: &[String],
709    exempt_lines: &BTreeMap<String, BTreeSet<u32>>,
710    base: Option<&str>,
711) -> Result<Measurement> {
712    let changed = match base {
713        Some(base) => Some(crate::patch_coverage::changed_lines(root, base)?),
714        None => None,
715    };
716    let modules: Vec<String> = match &changed {
717        None => Vec::new(),
718        Some(changed) => {
719            let modules: Vec<String> = changed
720                .keys()
721                .filter(|file| is_mutatable_py(file))
722                .cloned()
723                .collect();
724            if modules.is_empty() {
725                return Ok(Measurement::EngineNotRun);
726            }
727            modules
728        }
729    };
730    let json = run_py_adapter(root, &modules)?;
731    let mut mutants = parse_normalized_results(&json)?;
732    if let Some(changed) = &changed {
733        mutants.retain(|mutant| {
734            changed
735                .get(&mutant.file)
736                .is_some_and(|lines| lines.contains(&u64::from(mutant.line)))
737        });
738    }
739    mutants.retain(|mutant| !is_declaration_only(root, &mutant.file, Language::Python));
740    let survivors = evaluate_normalized(&mutants, exempt, exempt_lines)?;
741    Ok(Measurement::Tested {
742        count: normalized_conclusive_count(&mutants),
743        survivors,
744    })
745}
746
747/// Run the bundled Python mutation adapter over `root` and return the normalized-results
748/// JSON it writes. `modules`, when non-empty, scopes the run to those source files; empty
749/// runs the whole project. `PYTHONDONTWRITEBYTECODE` keeps `__pycache__` out of the tree.
750fn run_py_adapter(root: &Path, modules: &[String]) -> Result<String> {
751    let out = AdapterOut::new();
752    std::fs::create_dir_all(&out.0).context("creating the mutation adapter output dir")?;
753    let results = out.0.join("results.json");
754
755    let cwd = adapter_cwd(root, "Python")?;
756
757    const ENTRY: &str = "-m testing_conventions.mutation.main";
758    let mut command = Command::new("python3");
759    command
760        .current_dir(cwd)
761        .args(["-m", "testing_conventions.mutation.main", "--out"])
762        .arg(&results)
763        .env("PYTHONDONTWRITEBYTECODE", "1");
764    for module in modules {
765        command.arg("--module").arg(module);
766    }
767    let output = command
768        .output()
769        .context(spawn_context("python3", ENTRY, cwd))?;
770    if !output.status.success() {
771        bail!(
772            "the Python mutation adapter failed in `{}`:\n{}{}",
773            cwd.display(),
774            String::from_utf8_lossy(&output.stdout),
775            String::from_utf8_lossy(&output.stderr),
776        );
777    }
778    read_adapter_results(&results, "Python")
779}
780
781/// Whether a changed file is a mutatable Python *source* — a `.py` that is not a test
782/// (`*_test.py` / `test_*.py`) or `conftest.py`.
783fn is_mutatable_py(file: &str) -> bool {
784    if !file.ends_with(".py") {
785        return false;
786    }
787    let base = file.rsplit('/').next().unwrap_or(file);
788    !(base.ends_with("_test.py") || base.starts_with("test_") || base == "conftest.py")
789}
790
791/// A unique temp dir for one cargo-mutants run's `--output`, removed on drop so the
792/// scanned crate stays pristine and parallel runs don't collide.
793struct MutantsOut(PathBuf);
794
795impl MutantsOut {
796    fn new() -> Self {
797        static COUNTER: AtomicU64 = AtomicU64::new(0);
798        let name = format!(
799            "testing-conventions-mutants-{}-{}",
800            std::process::id(),
801            COUNTER.fetch_add(1, Ordering::Relaxed),
802        );
803        MutantsOut(std::env::temp_dir().join(name))
804    }
805}
806
807impl Drop for MutantsOut {
808    fn drop(&mut self) {
809        let _ = std::fs::remove_dir_all(&self.0);
810    }
811}
812
813/// The directory of the cargo workspace `root` belongs to. `cargo locate-project --workspace`
814/// is the authoritative lookup: membership involves member globs and `exclude` lists a
815/// manifest walk can't settle.
816fn cargo_workspace_root(root: &Path) -> Result<PathBuf> {
817    let output = Command::new("cargo")
818        .current_dir(root)
819        .args(["locate-project", "--workspace", "--message-format", "plain"])
820        .output()
821        .context("running `cargo locate-project` (is cargo installed?)")?;
822    if !output.status.success() {
823        bail!(
824            "cargo locate-project failed in `{}`: {}",
825            root.display(),
826            String::from_utf8_lossy(&output.stderr)
827        );
828    }
829    let manifest = PathBuf::from(String::from_utf8_lossy(&output.stdout).trim());
830    manifest_dir(&manifest)
831}
832
833/// The directory holding the workspace manifest `cargo locate-project` reported.
834fn manifest_dir(manifest: &Path) -> Result<PathBuf> {
835    manifest.parent().map(Path::to_path_buf).with_context(|| {
836        format!(
837            "no parent dir for the workspace manifest `{}`",
838            manifest.display()
839        )
840    })
841}
842
843/// The scan path's prefix relative to the workspace root ([`scan_prefix`]), over
844/// canonicalized paths so a relative CLI scan path resolves against the absolute path
845/// `cargo locate-project` reports. `None` when the scan path *is* the workspace root.
846fn canonical_scan_prefix(root: &Path, workspace_root: &Path) -> Option<String> {
847    let root = root.canonicalize().ok()?;
848    let workspace_root = workspace_root.canonicalize().ok()?;
849    scan_prefix(&root, &workspace_root)
850}
851
852/// Write the `<base>...HEAD` diff cargo-mutants' `--in-diff` scopes to, returning its path —
853/// or `None` when the diff is empty. cargo-mutants matches `--in-diff` paths relative to the
854/// cargo workspace root, so the diff is generated there, `--relative`, with `prefix` as a pathspec.
855fn write_base_diff(
856    root: &Path,
857    workspace_root: &Path,
858    prefix: Option<&str>,
859    base: &str,
860    out: &MutantsOut,
861) -> Result<Option<PathBuf>> {
862    let range = format!("{base}...HEAD");
863    let (dir, args) = match prefix {
864        None => (root, vec!["diff", "--relative", &range]),
865        Some(prefix) => (
866            workspace_root,
867            vec!["diff", "--relative", &range, "--", prefix],
868        ),
869    };
870    let output = Command::new("git")
871        .current_dir(dir)
872        .args(&args)
873        .output()
874        .context("running `git diff` for `--base` (is git installed?)")?;
875    if !output.status.success() {
876        bail!(
877            "git diff {range} failed: {}",
878            String::from_utf8_lossy(&output.stderr)
879        );
880    }
881    if output.stdout.is_empty() {
882        return Ok(None);
883    }
884    std::fs::create_dir_all(&out.0).context("creating the mutants output dir")?;
885    let path = out.0.join("base.diff");
886    std::fs::write(&path, &output.stdout).context("writing the base diff")?;
887    Ok(Some(path))
888}
889
890/// The written base diff, read back for parsing.
891fn read_base_diff(path: &Path) -> Result<String> {
892    std::fs::read_to_string(path)
893        .with_context(|| format!("reading the written base diff `{}`", path.display()))
894}
895
896/// The tool's own reading of a base diff: the changed files (new-side paths, `b/` stripped)
897/// and the inserted line numbers per file. Paths stay workspace-root-relative, the basis
898/// cargo-mutants addresses mutants on.
899struct BaseDiff {
900    files: Vec<String>,
901    inserted: BTreeMap<String, BTreeSet<u32>>,
902}
903
904/// Parse a unified diff into a [`BaseDiff`]. Each hunk body is consumed by the counts its `@@`
905/// header declares, so a content line beginning `+++` or `---` never reads as a file header.
906/// A deleted file (`+++ /dev/null`) carries neither a changed file nor inserted lines.
907fn parse_base_diff(diff: &str) -> BaseDiff {
908    let mut files = Vec::new();
909    let mut inserted: BTreeMap<String, BTreeSet<u32>> = BTreeMap::new();
910    let mut current: Option<String> = None;
911    let mut lines = diff.lines();
912    while let Some(line) = lines.next() {
913        if let Some(path) = line.strip_prefix("+++ ") {
914            current = (path != "/dev/null").then(|| {
915                let path = path.strip_prefix("b/").unwrap_or(path).to_string();
916                files.push(path.clone());
917                path
918            });
919        } else if let Some(header) = line.strip_prefix("@@ ") {
920            let Some((new_start, old_count, new_count)) = parse_hunk_header(header) else {
921                continue;
922            };
923            let mut new_line = new_start;
924            let (mut old_left, mut new_left) = (old_count, new_count);
925            while old_left > 0 || new_left > 0 {
926                let Some(line) = lines.next() else { break };
927                if line.starts_with('\\') {
928                    // "\ No newline at end of file" annotates the previous line and
929                    // counts against neither side.
930                } else if line.starts_with('+') {
931                    if let Some(file) = &current {
932                        inserted.entry(file.clone()).or_default().insert(new_line);
933                    }
934                    new_line += 1;
935                    new_left = new_left.saturating_sub(1);
936                } else if line.starts_with('-') {
937                    old_left = old_left.saturating_sub(1);
938                } else {
939                    new_line += 1;
940                    old_left = old_left.saturating_sub(1);
941                    new_left = new_left.saturating_sub(1);
942                }
943            }
944        }
945    }
946    BaseDiff { files, inserted }
947}
948
949/// The `(new_start, old_count, new_count)` of a hunk header's `-a[,b] +c[,d]` part.
950fn parse_hunk_header(header: &str) -> Option<(u32, u32, u32)> {
951    let mut parts = header.split(' ');
952    let (_, old_count) = parse_range(parts.next()?.strip_prefix('-')?)?;
953    let (new_start, new_count) = parse_range(parts.next()?.strip_prefix('+')?)?;
954    Some((new_start, old_count, new_count))
955}
956
957/// A hunk range `start[,count]`; the count defaults to 1.
958fn parse_range(range: &str) -> Option<(u32, u32)> {
959    match range.split_once(',') {
960        Some((start, count)) => Some((start.parse().ok()?, count.parse().ok()?)),
961        None => Some((range.parse().ok()?, 1)),
962    }
963}
964
965/// Rebase a cargo-mutants report's workspace-root-relative mutant paths onto the scan path, so
966/// exemption matching and survivor reporting address scan-path-relative files. A baseline
967/// outcome carries no path and passes through; a mutant outside the scan path is dropped.
968fn rebase_report_paths(report: MutantsReport, prefix: Option<&str>) -> MutantsReport {
969    let Some(prefix) = prefix else {
970        return report;
971    };
972    let prefix = format!("{prefix}/");
973    MutantsReport {
974        outcomes: report
975            .outcomes
976            .into_iter()
977            .filter_map(|mut outcome| {
978                if let Scenario::Mutant(mutant) = &mut outcome.scenario {
979                    mutant.file = mutant.file.strip_prefix(&prefix)?.to_string();
980                }
981                Some(outcome)
982            })
983            .collect(),
984    }
985}
986
987/// The cargo-mutants version the Rust arm provisions and pins to. Bumping this points the
988/// cache at a fresh version-scoped directory, so the next run installs the new release.
989const CARGO_MUTANTS_VERSION: &str = "27.1.0";
990
991/// Ensure the pinned cargo-mutants is available and return the absolute path to its binary,
992/// provisioning it on first use. cargo ships no library form, so — unlike the in-process
993/// TS/Python adapters — a pinned `cargo install` runs into the tool's own cache directory.
994fn ensure_cargo_mutants() -> Result<PathBuf> {
995    provision_pinned(&cargo_mutants_cache_root(), execute)
996}
997
998/// Provision the pinned cargo-mutants under `root`, executing its `cargo install` with `run`.
999fn provision_pinned(
1000    root: &Path,
1001    run: impl FnOnce(&mut Command) -> std::io::Result<Output>,
1002) -> Result<PathBuf> {
1003    let bin = root.join("bin").join(CARGO_MUTANTS_BIN_NAME);
1004    let lock_path = root.join(".install.lock");
1005    provision(&bin, &lock_path, || run_install(root, run))
1006}
1007
1008/// Execute a prepared command, capturing its output.
1009fn execute(command: &mut Command) -> std::io::Result<Output> {
1010    command.output()
1011}
1012
1013/// The cargo-mutants binary's file name (`.exe` on Windows), as `cargo install --root`
1014/// lays it out under `<root>/bin/`.
1015const CARGO_MUTANTS_BIN_NAME: &str = if cfg!(windows) {
1016    "cargo-mutants.exe"
1017} else {
1018    "cargo-mutants"
1019};
1020
1021/// The tool-owned, version-scoped cache directory cargo-mutants is installed under, so a
1022/// version bump provisions cleanly beside the old one and never clobbers a user's own
1023/// `~/.cargo/bin`.
1024fn cargo_mutants_cache_root() -> PathBuf {
1025    cache_base()
1026        .join("testing-conventions")
1027        .join(format!("cargo-mutants-{CARGO_MUTANTS_VERSION}"))
1028}
1029
1030/// The base cache directory, read from OS-owned config. Split from [`resolve_cache_base`]
1031/// so the resolution logic is unit-tested without touching the process environment.
1032fn cache_base() -> PathBuf {
1033    resolve_cache_base(std::env::var_os("XDG_CACHE_HOME"), std::env::var_os("HOME"))
1034}
1035
1036/// Resolve the base cache dir: `XDG_CACHE_HOME` when set and non-empty, else `$HOME/.cache`,
1037/// else the temp dir. Pure over its inputs.
1038fn resolve_cache_base(xdg: Option<OsString>, home: Option<OsString>) -> PathBuf {
1039    if let Some(dir) = xdg.filter(|value| !value.is_empty()) {
1040        return PathBuf::from(dir);
1041    }
1042    if let Some(dir) = home.filter(|value| !value.is_empty()) {
1043        return PathBuf::from(dir).join(".cache");
1044    }
1045    std::env::temp_dir()
1046}
1047
1048/// Return `bin` if it already exists, otherwise take an exclusive advisory lock at
1049/// `lock_path`, re-check, and run `install` if still absent. The lock keeps N concurrent
1050/// callers to one from-source compile instead of N. An install producing no binary is an error.
1051fn provision(
1052    bin: &Path,
1053    lock_path: &Path,
1054    install: impl FnOnce() -> Result<()>,
1055) -> Result<PathBuf> {
1056    if bin.exists() {
1057        return Ok(bin.to_path_buf());
1058    }
1059    if let Some(parent) = lock_path.parent() {
1060        std::fs::create_dir_all(parent).context("creating the provisioning lock's parent dir")?;
1061    }
1062    let lock_file = std::fs::OpenOptions::new()
1063        .create(true)
1064        .truncate(false)
1065        .write(true)
1066        .open(lock_path)
1067        .context("opening the provisioning lock file")?;
1068    lock_file
1069        .lock()
1070        .context("acquiring the provisioning lock")?;
1071    // Re-check: another caller may have installed while this one waited for the lock.
1072    if bin.exists() {
1073        return Ok(bin.to_path_buf());
1074    }
1075    install()?;
1076    if !bin.exists() {
1077        bail!(
1078            "provisioning reported success but cargo-mutants is not at `{}`",
1079            bin.display()
1080        );
1081    }
1082    Ok(bin.to_path_buf())
1083}
1084
1085/// The argv provisioning the pinned cargo-mutants into `root` (`cargo install cargo-mutants
1086/// --locked --version <X> --root <root>`). Split from execution so a test asserts the pin
1087/// and the isolated `--root` without a real install.
1088fn install_argv(root: &Path) -> Vec<OsString> {
1089    vec![
1090        OsString::from("install"),
1091        OsString::from("cargo-mutants"),
1092        OsString::from("--locked"),
1093        OsString::from("--version"),
1094        OsString::from(CARGO_MUTANTS_VERSION),
1095        OsString::from("--root"),
1096        root.as_os_str().to_os_string(),
1097    ]
1098}
1099
1100/// Provision cargo-mutants into `root`, executing the built `cargo install` with `run`, which
1101/// is injected so a test drives both branches with a fake. The coverage-instrumentation env is
1102/// stripped so the compile doesn't re-enter a `cargo llvm-cov` rustc wrapper.
1103fn run_install(
1104    root: &Path,
1105    run: impl FnOnce(&mut Command) -> std::io::Result<Output>,
1106) -> Result<()> {
1107    let mut command = Command::new("cargo");
1108    command.args(install_argv(root));
1109    strip_llvm_cov_env(&mut command);
1110    let output = run(&mut command)
1111        .context("provisioning cargo-mutants via `cargo install` (is cargo installed?)")?;
1112    if !output.status.success() {
1113        bail!(
1114            "failed to provision cargo-mutants {CARGO_MUTANTS_VERSION}:\n{}{}",
1115            String::from_utf8_lossy(&output.stdout),
1116            String::from_utf8_lossy(&output.stderr),
1117        );
1118    }
1119    Ok(())
1120}
1121
1122/// Strip the outer coverage-instrumentation env from a nested cargo invocation (the
1123/// cargo-mutants run, or the `cargo install` that provisions it) so it doesn't re-enter the
1124/// `cargo llvm-cov` rustc wrapper and hang, as when this rule's own tests run under coverage.
1125fn strip_llvm_cov_env(command: &mut Command) {
1126    for var in [
1127        "RUSTFLAGS",
1128        "CARGO_ENCODED_RUSTFLAGS",
1129        "RUSTDOCFLAGS",
1130        "CARGO_ENCODED_RUSTDOCFLAGS",
1131        "LLVM_PROFILE_FILE",
1132        "CARGO_LLVM_COV",
1133        "CARGO_LLVM_COV_SHOW_ENV",
1134        "CARGO_LLVM_COV_TARGET_DIR",
1135        "CARGO_LLVM_COV_BUILD_DIR",
1136        "RUSTC_WRAPPER",
1137        "RUSTC_WORKSPACE_WRAPPER",
1138        "__CARGO_LLVM_COV_RUSTC_WRAPPER",
1139        "__CARGO_LLVM_COV_RUSTC_WRAPPER_RUSTFLAGS",
1140        "__CARGO_LLVM_COV_RUSTC_WRAPPER_CRATE_NAMES",
1141    ] {
1142        command.env_remove(var);
1143    }
1144}
1145
1146/// Run the cargo-mutants argv ([`mutants_argv`]) in `root`, where `engine` is the provisioned
1147/// binary invoked by absolute path, returning its [`Output`]. The outer instrumentation env is
1148/// stripped so a nested run (this rule's own tests under coverage) can't re-enter the wrapper.
1149fn run_cargo_mutants(
1150    engine: &Path,
1151    root: &Path,
1152    out: &Path,
1153    in_diff: Option<&Path>,
1154    features: &[String],
1155) -> Result<Output> {
1156    let mut command = Command::new(engine);
1157    command
1158        .current_dir(root)
1159        .args(mutants_argv(out, in_diff, features));
1160    strip_llvm_cov_env(&mut command);
1161    let output = command.output().context("running cargo-mutants")?;
1162    classify_mutants_exit(root, &output)?;
1163    Ok(output)
1164}
1165
1166/// Decide whether an engine run that judged zero mutants is legitimate: `listed` is the crate's
1167/// full mutant list and `diff` the tool's own reading of the diff the engine filtered by. A
1168/// listed mutant whose span touches an inserted line proves the filter dropped real mutants.
1169fn zero_mutant_verdict(listed: &[MutantInfo], diff: &BaseDiff, run: &Output) -> Result<()> {
1170    let dropped: Vec<&MutantInfo> = listed
1171        .iter()
1172        .filter(|mutant| {
1173            diff.inserted.get(&mutant.file).is_some_and(|lines| {
1174                lines
1175                    .range(mutant.span.start.line..=mutant.span.end.line)
1176                    .next()
1177                    .is_some()
1178            })
1179        })
1180        .collect();
1181    if dropped.is_empty() {
1182        return Ok(());
1183    }
1184    let sites: Vec<String> = dropped
1185        .iter()
1186        .map(|mutant| {
1187            format!(
1188                "  {}:{}: {}",
1189                mutant.file,
1190                mutant.span.start.line,
1191                strip_embedded_location(&mutant.name)
1192            )
1193        })
1194        .collect();
1195    bail!(
1196        "cargo-mutants tested no mutants, but {} of the crate's {} mutant site(s) sit on the diff's inserted lines — the changed-line filter dropped real mutants:\n{}\nengine output:\n{}{}",
1197        dropped.len(),
1198        listed.len(),
1199        sites.join("\n"),
1200        String::from_utf8_lossy(&run.stdout),
1201        String::from_utf8_lossy(&run.stderr),
1202    )
1203}
1204
1205/// The argv for one cargo-mutants mutant listing: `mutants --list --json
1206/// [--features <list>]`, mirroring the run's own feature selection so both see the same
1207/// mutant set.
1208fn list_argv(features: &[String]) -> Vec<OsString> {
1209    let mut argv = vec![
1210        OsString::from("mutants"),
1211        OsString::from("--list"),
1212        OsString::from("--json"),
1213    ];
1214    if !features.is_empty() {
1215        argv.push(OsString::from("--features"));
1216        argv.push(OsString::from(features.join(",")));
1217    }
1218    argv
1219}
1220
1221/// List the crate's discoverable mutants via `cargo mutants --list --json`, executing the
1222/// built command with `run`. `run` is injected so a test drives the success and failure
1223/// branches with a fake (no real engine).
1224fn list_cargo_mutants(
1225    engine: &Path,
1226    root: &Path,
1227    features: &[String],
1228    run: impl FnOnce(&mut Command) -> std::io::Result<Output>,
1229) -> Result<Vec<MutantInfo>> {
1230    let mut command = Command::new(engine);
1231    command.current_dir(root).args(list_argv(features));
1232    strip_llvm_cov_env(&mut command);
1233    let output = run(&mut command).context("listing the crate's mutants with cargo-mutants")?;
1234    if !output.status.success() {
1235        bail!(
1236            "cargo-mutants --list failed in `{}`:\n{}{}",
1237            root.display(),
1238            String::from_utf8_lossy(&output.stdout),
1239            String::from_utf8_lossy(&output.stderr),
1240        );
1241    }
1242    parse_mutants_list(&String::from_utf8_lossy(&output.stdout))
1243}
1244
1245/// The argv for one cargo-mutants run: `mutants --output <out> --cargo-test-arg --lib
1246/// [--in-diff <diff>] [--features <list>]`. `--cargo-test-arg --lib` reaches only the judging
1247/// `cargo test` invocation, scoping it to the same target `unit coverage` measures. `features`
1248/// rides on the engine's own `--features` so it reaches every cargo invocation, judging build
1249/// included; after a `--` it would reach `cargo test` alone and the baseline build would fail.
1250fn mutants_argv(out: &Path, in_diff: Option<&Path>, features: &[String]) -> Vec<OsString> {
1251    let mut argv = vec![
1252        OsString::from("mutants"),
1253        OsString::from("--output"),
1254        out.as_os_str().to_os_string(),
1255        OsString::from("--cargo-test-arg"),
1256        OsString::from("--lib"),
1257    ];
1258    if let Some(diff) = in_diff {
1259        argv.push(OsString::from("--in-diff"));
1260        argv.push(diff.as_os_str().to_os_string());
1261    }
1262    if !features.is_empty() {
1263        argv.push(OsString::from("--features"));
1264        argv.push(OsString::from(features.join(",")));
1265    }
1266    argv
1267}
1268
1269/// Classify a finished cargo-mutants run's exit code as a normal outcome or a fatal error.
1270/// `0` (all caught), `2` (some missed) and `3` (some timed out, none missed) each write an
1271/// `outcomes.json` the gate reads. Any other code — a baseline that didn't build (4) — is fatal.
1272fn classify_mutants_exit(root: &Path, output: &Output) -> Result<()> {
1273    match output.status.code() {
1274        Some(0) | Some(2) | Some(3) => Ok(()),
1275        _ => bail!(
1276            "cargo-mutants did not run cleanly in `{}` (baseline build/test failure?):\n{}{}",
1277            root.display(),
1278            String::from_utf8_lossy(&output.stdout),
1279            String::from_utf8_lossy(&output.stderr),
1280        ),
1281    }
1282}
1283
1284#[cfg(test)]
1285mod tests {
1286    use super::*;
1287
1288    const NORMALIZED: &str = r#"[
1289        {"file": "src/a.ts", "line": 2, "status": "survived",
1290         "mutator": "ConditionalExpression", "replacement": "true", "id": "ignored"},
1291        {"file": "src/a.ts", "line": 5, "status": "no_coverage", "mutator": "ArithmeticOperator"},
1292        {"file": "src/a.ts", "line": 9, "status": "killed",
1293         "mutator": "BooleanLiteral", "replacement": "false"},
1294        {"file": "src/a.ts", "line": 12, "status": "timeout", "mutator": "BlockStatement"},
1295        {"file": "src/a.ts", "line": 15, "status": "compile_error", "mutator": "OptionalChaining"},
1296        {"file": "src/a.ts", "line": 18, "status": "runtime_error", "mutator": "StringLiteral"}
1297    ]"#;
1298
1299    #[test]
1300    fn parses_the_normalized_schema() {
1301        let mutants = parse_normalized_results(NORMALIZED).expect("valid normalized results");
1302        assert_eq!(mutants.len(), 6);
1303        assert_eq!(mutants[0].status, MutantStatus::Survived);
1304        assert_eq!(mutants[1].status, MutantStatus::NoCoverage);
1305        assert_eq!(mutants[0].replacement.as_deref(), Some("true"));
1306        assert_eq!(mutants[1].replacement, None);
1307    }
1308
1309    #[test]
1310    fn normalized_survivors_are_survived_and_nocoverage_only() {
1311        let mutants = parse_normalized_results(NORMALIZED).unwrap();
1312        let survivors = normalized_survivors(&mutants);
1313        assert_eq!(survivors.len(), 2);
1314        assert_eq!((survivors[0].line, survivors[1].line), (2, 5));
1315        assert!(survivors[0].description.contains("ConditionalExpression"));
1316        assert!(survivors[0].description.contains("-> true"));
1317        assert_eq!(survivors[1].description, "ArithmeticOperator");
1318    }
1319
1320    #[test]
1321    fn normalized_mutated_lines_collects_only_viable_mutants() {
1322        let mutants = parse_normalized_results(NORMALIZED).unwrap();
1323        assert_eq!(
1324            normalized_mutated_lines(&mutants),
1325            [2u32, 5, 9, 12]
1326                .into_iter()
1327                .map(|line| ("src/a.ts".to_string(), line))
1328                .collect()
1329        );
1330    }
1331
1332    #[test]
1333    fn normalized_conclusive_count_is_survived_killed_and_nocoverage() {
1334        let mutants = parse_normalized_results(NORMALIZED).unwrap();
1335        assert_eq!(normalized_conclusive_count(&mutants), 3);
1336        assert_eq!(normalized_conclusive_count(&[]), 0);
1337    }
1338
1339    #[test]
1340    fn evaluate_normalized_reports_unexempted_survivors() {
1341        let mutants = parse_normalized_results(NORMALIZED).unwrap();
1342        let kept = evaluate_normalized(&mutants, &[], &BTreeMap::new()).unwrap();
1343        assert_eq!(kept.len(), 2, "both survivors stand with no exemptions");
1344    }
1345
1346    #[test]
1347    fn evaluate_normalized_drops_a_whole_file_exemption() {
1348        let mutants = parse_normalized_results(NORMALIZED).unwrap();
1349        let kept =
1350            evaluate_normalized(&mutants, &["src/a.ts".to_string()], &BTreeMap::new()).unwrap();
1351        assert!(
1352            kept.is_empty(),
1353            "the whole-file exemption lifts both survivors"
1354        );
1355    }
1356
1357    #[test]
1358    fn evaluate_normalized_drops_a_line_scoped_exemption() {
1359        let mutants = parse_normalized_results(NORMALIZED).unwrap();
1360        let line_scoped = BTreeMap::from([("src/a.ts".to_string(), BTreeSet::from([2u32]))]);
1361        let kept = evaluate_normalized(&mutants, &[], &line_scoped).unwrap();
1362        assert_eq!(kept.len(), 1);
1363        assert_eq!(kept[0].line, 5);
1364    }
1365
1366    #[test]
1367    fn evaluate_normalized_rejects_exempting_a_caught_line() {
1368        let mutants = parse_normalized_results(NORMALIZED).unwrap();
1369        let line_scoped = BTreeMap::from([("src/a.ts".to_string(), BTreeSet::from([9u32]))]);
1370        let err = evaluate_normalized(&mutants, &[], &line_scoped).unwrap_err();
1371        assert!(
1372            err.to_string().contains("all caught") && err.to_string().contains("src/a.ts:9"),
1373            "got: {err}"
1374        );
1375    }
1376
1377    #[test]
1378    fn evaluate_normalized_leaves_an_unviable_listed_line_alone() {
1379        let mutants = parse_normalized_results(NORMALIZED).unwrap();
1380        let line_scoped = BTreeMap::from([("src/a.ts".to_string(), BTreeSet::from([15u32]))]);
1381        let kept = evaluate_normalized(&mutants, &[], &line_scoped).unwrap();
1382        assert_eq!(kept.len(), 2);
1383    }
1384
1385    const SAMPLE: &str = r#"{
1386        "outcomes": [
1387            {"scenario": "Baseline", "summary": "Success",
1388             "phase_results": []},
1389            {"scenario": {"Mutant": {"file": "src/lib.rs", "package": "p", "genre": "FnValue",
1390                "replacement": "true", "name": "src/lib.rs:7:7: replace > with == in is_positive",
1391                "function": {"function_name": "is_positive"},
1392                "span": {"start": {"line": 7, "column": 7}, "end": {"line": 7, "column": 8}}}},
1393             "summary": "MissedMutant"},
1394            {"scenario": {"Mutant": {"file": "src/other.rs", "package": "p", "genre": "FnValue",
1395                "replacement": "0", "name": "src/other.rs:3:5: replace add -> i32 with 0",
1396                "span": {"start": {"line": 3, "column": 5}, "end": {"line": 3, "column": 9}}}},
1397             "summary": "CaughtMutant"}
1398        ],
1399        "total_mutants": 2
1400    }"#;
1401
1402    #[test]
1403    fn parses_the_outcomes_export() {
1404        let report = parse_mutants_report(SAMPLE).expect("valid outcomes.json");
1405        assert_eq!(report.outcomes.len(), 3);
1406        assert!(matches!(report.outcomes[0].scenario, Scenario::Baseline));
1407    }
1408
1409    #[test]
1410    fn collects_only_missed_mutants_as_survivors() {
1411        let report = parse_mutants_report(SAMPLE).unwrap();
1412        let survivors = unexplained_survivors(&report, &[]);
1413        assert_eq!(survivors.len(), 1);
1414        assert_eq!(survivors[0].file, "src/lib.rs");
1415        assert_eq!(survivors[0].line, 7);
1416        assert!(survivors[0].description.contains("replace > with =="));
1417    }
1418
1419    #[test]
1420    fn a_survivor_description_carries_no_location_prefix() {
1421        let report = parse_mutants_report(SAMPLE).unwrap();
1422        let survivors = unexplained_survivors(&report, &[]);
1423        assert_eq!(
1424            survivors[0].description, "replace > with == in is_positive",
1425            "the name's embedded `file:line:col:` prefix is stripped"
1426        );
1427    }
1428
1429    #[test]
1430    fn strip_embedded_location_removes_a_file_line_col_prefix() {
1431        assert_eq!(
1432            strip_embedded_location("src/lib.rs:7:5: replace > with == in is_positive"),
1433            "replace > with == in is_positive"
1434        );
1435    }
1436
1437    #[test]
1438    fn strip_embedded_location_keeps_a_name_without_one() {
1439        for name in [
1440            "replace add -> 0",
1441            "note: no location segment",
1442            "7:5: no file segment",
1443            "src/lib.rs:7:x: non-numeric column",
1444            "src/lib.rs:x:5: non-numeric line",
1445        ] {
1446            assert_eq!(strip_embedded_location(name), name);
1447        }
1448    }
1449
1450    #[test]
1451    fn conclusive_count_is_caught_plus_missed() {
1452        let report = parse_mutants_report(SAMPLE).unwrap();
1453        assert_eq!(conclusive_count(&report), 2);
1454        assert_eq!(conclusive_count(&MutantsReport { outcomes: vec![] }), 0);
1455    }
1456
1457    #[test]
1458    fn an_exemption_drops_a_survivor_in_that_file() {
1459        let report = parse_mutants_report(SAMPLE).unwrap();
1460        let exempt = vec!["src/lib.rs".to_string()];
1461        assert!(unexplained_survivors(&report, &exempt).is_empty());
1462    }
1463
1464    #[test]
1465    fn an_exemption_on_another_file_leaves_the_survivor() {
1466        let report = parse_mutants_report(SAMPLE).unwrap();
1467        let exempt = vec!["src/elsewhere.rs".to_string()];
1468        assert_eq!(unexplained_survivors(&report, &exempt).len(), 1);
1469    }
1470
1471    const BASELINE_ONLY: &str = r#"{
1472        "outcomes": [
1473            {"scenario": "Baseline", "summary": "MissedMutant", "phase_results": []},
1474            {"scenario": "Baseline", "summary": "CaughtMutant", "phase_results": []}
1475        ],
1476        "total_mutants": 0
1477    }"#;
1478
1479    #[test]
1480    fn a_baseline_outcome_is_never_a_survivor() {
1481        let report = parse_mutants_report(BASELINE_ONLY).unwrap();
1482        assert!(unexplained_survivors(&report, &[]).is_empty());
1483    }
1484
1485    #[test]
1486    fn a_baseline_outcome_is_never_a_mutated_line() {
1487        let report = parse_mutants_report(BASELINE_ONLY).unwrap();
1488        assert!(mutated_lines(&report).is_empty());
1489    }
1490
1491    #[test]
1492    fn parse_base_diff_skips_a_malformed_hunk_header() {
1493        let diff = "\
1494diff --git a/src/lib.rs b/src/lib.rs
1495--- a/src/lib.rs
1496+++ b/src/lib.rs
1497@@ junk @@
1498";
1499        let parsed = parse_base_diff(diff);
1500        assert_eq!(parsed.files, vec!["src/lib.rs"]);
1501        assert!(parsed.inserted.is_empty());
1502    }
1503
1504    #[test]
1505    fn a_missing_base_diff_read_reports_its_path() {
1506        let missing = unique_tmp().join("base.diff");
1507        let err = read_base_diff(&missing).unwrap_err();
1508        let msg = format!("{err:#}");
1509        assert!(msg.contains("reading the written base diff"), "{msg}");
1510    }
1511
1512    #[test]
1513    fn a_missing_adapter_results_read_names_the_engine_and_path() {
1514        let missing = unique_tmp().join("results.json");
1515        let err = read_adapter_results(&missing, "TypeScript").unwrap_err();
1516        let msg = format!("{err:#}");
1517        assert!(
1518            msg.contains("TypeScript mutation adapter's results"),
1519            "{msg}"
1520        );
1521    }
1522
1523    #[test]
1524    fn manifest_dir_is_the_manifest_parent() {
1525        let dir = manifest_dir(Path::new("/w/Cargo.toml")).unwrap();
1526        assert_eq!(dir, Path::new("/w"));
1527    }
1528
1529    #[test]
1530    fn a_rootless_manifest_path_is_an_error() {
1531        let err = manifest_dir(Path::new("/")).unwrap_err();
1532        let msg = format!("{err:#}");
1533        assert!(msg.contains("no parent dir"), "{msg}");
1534    }
1535
1536    #[test]
1537    fn a_directory_outside_any_workspace_fails_locate_project() {
1538        let dir = unique_tmp();
1539        let err = cargo_workspace_root(&dir).unwrap_err();
1540        let msg = format!("{err:#}");
1541        assert!(msg.contains("cargo locate-project failed"), "{msg}");
1542        std::fs::remove_dir_all(&dir).ok();
1543    }
1544
1545    #[test]
1546    fn a_bad_base_ref_fails_the_base_diff() {
1547        let dir = unique_tmp();
1548        let init = Command::new("git")
1549            .current_dir(&dir)
1550            .args(["init", "-q"])
1551            .output()
1552            .unwrap();
1553        assert!(init.status.success());
1554        let out = MutantsOut::new();
1555        let err = write_base_diff(&dir, &dir, None, "tc-no-such-ref", &out).unwrap_err();
1556        let msg = format!("{err:#}");
1557        assert!(msg.contains("git diff"), "{msg}");
1558        std::fs::remove_dir_all(&dir).ok();
1559    }
1560
1561    #[test]
1562    fn a_python_adapter_failure_reports_the_adapter_output() {
1563        let dir = unique_tmp();
1564        let err = run_py_adapter(&dir, &[]).unwrap_err();
1565        let msg = format!("{err:#}");
1566        assert!(msg.contains("the Python mutation adapter failed"), "{msg}");
1567        std::fs::remove_dir_all(&dir).ok();
1568    }
1569
1570    #[test]
1571    fn is_declaration_only_is_true_for_a_const_only_rust_file() {
1572        let dir = unique_tmp();
1573        std::fs::write(
1574            dir.join("settings.rs"),
1575            "pub const TIMEOUT: u64 = 30 * 60;\n",
1576        )
1577        .unwrap();
1578        assert!(is_declaration_only(&dir, "settings.rs", Language::Rust));
1579        std::fs::remove_dir_all(&dir).ok();
1580    }
1581
1582    #[test]
1583    fn is_declaration_only_is_false_for_a_rust_file_with_a_function() {
1584        let dir = unique_tmp();
1585        std::fs::write(dir.join("lib.rs"), "pub fn run() {}\n").unwrap();
1586        assert!(!is_declaration_only(&dir, "lib.rs", Language::Rust));
1587        std::fs::remove_dir_all(&dir).ok();
1588    }
1589
1590    #[test]
1591    fn is_declaration_only_covers_python_and_typescript_too() {
1592        let dir = unique_tmp();
1593        std::fs::write(dir.join("settings.py"), "TIMEOUT = 30 * 60\n").unwrap();
1594        std::fs::write(dir.join("settings.ts"), "export const TIMEOUT = 30 * 60;\n").unwrap();
1595        assert!(is_declaration_only(&dir, "settings.py", Language::Python));
1596        assert!(is_declaration_only(
1597            &dir,
1598            "settings.ts",
1599            Language::TypeScript
1600        ));
1601        std::fs::remove_dir_all(&dir).ok();
1602    }
1603
1604    #[test]
1605    fn is_declaration_only_is_false_for_an_unreadable_file() {
1606        let dir = unique_tmp();
1607        assert!(!is_declaration_only(&dir, "missing.rs", Language::Rust));
1608        std::fs::remove_dir_all(&dir).ok();
1609    }
1610
1611    #[test]
1612    fn rebase_report_paths_strips_the_workspace_prefix() {
1613        let report = parse_mutants_report(SAMPLE).unwrap();
1614        let prefixed = MutantsReport {
1615            outcomes: report
1616                .outcomes
1617                .iter()
1618                .cloned()
1619                .map(|mut outcome| {
1620                    if let Scenario::Mutant(mutant) = &mut outcome.scenario {
1621                        mutant.file = format!("member/{}", mutant.file);
1622                    }
1623                    outcome
1624                })
1625                .collect(),
1626        };
1627        let rebased = rebase_report_paths(prefixed, Some("member"));
1628        let survivors = unexplained_survivors(&rebased, &[]);
1629        assert_eq!(survivors.len(), 1);
1630        assert_eq!(survivors[0].file, "src/lib.rs");
1631        assert_eq!(rebased.outcomes.len(), 3);
1632    }
1633
1634    #[test]
1635    fn rebase_report_paths_drops_an_out_of_scope_mutant_and_keeps_none_identity() {
1636        let report = parse_mutants_report(SAMPLE).unwrap();
1637        let rebased = rebase_report_paths(report.clone(), Some("member"));
1638        assert_eq!(
1639            rebased.outcomes.len(),
1640            1,
1641            "only the pathless baseline outcome remains"
1642        );
1643        let unchanged = rebase_report_paths(report, None);
1644        assert_eq!(unchanged.outcomes.len(), 3);
1645        assert_eq!(unexplained_survivors(&unchanged, &[])[0].file, "src/lib.rs");
1646    }
1647
1648    #[test]
1649    fn adapter_cwd_normalises_the_empty_package_root_to_the_current_dir() {
1650        // `tiers::package_root` yields `""` for a relative scan path such as `src`, and
1651        // `Command::current_dir("")` fails with ENOENT — which the adapter's error context
1652        // mislabelled as a missing `node`, hitting every TypeScript consumer of the gate.
1653        assert_eq!(
1654            adapter_cwd(Path::new(""), "TypeScript").unwrap(),
1655            Path::new(".")
1656        );
1657        assert_eq!(
1658            adapter_cwd(Path::new("src"), "TypeScript").unwrap(),
1659            Path::new("src")
1660        );
1661    }
1662
1663    #[test]
1664    fn adapter_cwd_rejects_a_directory_that_is_not_there() {
1665        // `Command::output()` reports a missing working directory with the same ENOENT as a
1666        // missing interpreter, so an unchecked spawn blames the interpreter for a wrong path.
1667        let err = adapter_cwd(Path::new("no/such/dir"), "Python")
1668            .expect_err("a directory that is not there is an error");
1669        assert_eq!(
1670            err.to_string(),
1671            "the Python mutation adapter's working directory `no/such/dir` is not a directory"
1672        );
1673    }
1674
1675    #[test]
1676    fn spawn_context_names_the_interpreter_the_entry_and_the_working_directory() {
1677        assert_eq!(
1678            spawn_context("node", "/pkg/dist/mutation/main.js", Path::new("/pkg")),
1679            "spawning `node /pkg/dist/mutation/main.js` in `/pkg` (is `node` on PATH?)"
1680        );
1681    }
1682
1683    #[test]
1684    fn scan_prefix_is_the_scan_path_relative_to_the_package_root() {
1685        assert_eq!(
1686            scan_prefix(Path::new("/repo/pkg/src"), Path::new("/repo/pkg")),
1687            Some("src".to_string())
1688        );
1689        assert_eq!(
1690            scan_prefix(Path::new("/repo/pkg/src/nested"), Path::new("/repo/pkg")),
1691            Some("src/nested".to_string())
1692        );
1693        assert_eq!(
1694            scan_prefix(Path::new("/repo/pkg"), Path::new("/repo/pkg")),
1695            None
1696        );
1697        assert_eq!(
1698            scan_prefix(Path::new("pkg/src"), Path::new("pkg")),
1699            Some("src".to_string())
1700        );
1701    }
1702
1703    #[test]
1704    fn prefix_mutate_specs_rebases_diff_ranges_onto_the_package_root() {
1705        let specs = vec!["index.ts:8-11".to_string(), "a/b.ts:2-2".to_string()];
1706        assert_eq!(
1707            prefix_mutate_specs(specs.clone(), Some("src")),
1708            vec![
1709                "src/index.ts:8-11".to_string(),
1710                "src/a/b.ts:2-2".to_string()
1711            ]
1712        );
1713        assert_eq!(prefix_mutate_specs(specs.clone(), None), specs);
1714    }
1715
1716    #[test]
1717    fn scan_scoped_mutate_globs_mirror_strykers_default_under_the_scan_path() {
1718        assert_eq!(
1719            scan_scoped_mutate_globs("src"),
1720            vec![
1721                "src/**/!(*.+(s|S)pec|*.+(t|T)est).+(cjs|mjs|js|ts|mts|cts|jsx|tsx|html|vue|svelte)"
1722                    .to_string(),
1723                "!src/**/__tests__/**/*.+(cjs|mjs|js|ts|mts|cts|jsx|tsx|html|vue|svelte)"
1724                    .to_string(),
1725            ]
1726        );
1727    }
1728
1729    #[test]
1730    fn scan_scoped_test_file_globs_narrow_the_run_without_moving_the_runner_root() {
1731        assert_eq!(
1732            scan_scoped_test_file_globs("src"),
1733            vec!["src/**".to_string()]
1734        );
1735        assert_eq!(
1736            scan_scoped_test_file_globs("packages/core/src"),
1737            vec!["packages/core/src/**".to_string()]
1738        );
1739    }
1740
1741    #[test]
1742    fn to_scan_relative_strips_the_prefix_and_drops_out_of_scope_mutants() {
1743        let mutants = parse_normalized_results(
1744            r#"[
1745                {"file": "src/a.ts", "line": 2, "status": "survived", "mutator": "X"},
1746                {"file": "tests/e2e/t.ts", "line": 9, "status": "survived", "mutator": "X"}
1747            ]"#,
1748        )
1749        .unwrap();
1750        let rebased = to_scan_relative(mutants.clone(), Some("src"));
1751        assert_eq!(rebased.len(), 1, "the out-of-scan-path mutant is dropped");
1752        assert_eq!(rebased[0].file, "a.ts");
1753        let unchanged = to_scan_relative(mutants, None);
1754        assert_eq!(unchanged.len(), 2);
1755        assert_eq!(unchanged[0].file, "src/a.ts");
1756    }
1757
1758    #[test]
1759    fn is_mutatable_ts_keeps_sources_and_drops_tests_and_decls() {
1760        assert!(is_mutatable_ts("src/index.ts"));
1761        assert!(is_mutatable_ts("src/util.tsx"));
1762        assert!(is_mutatable_ts("src/util.js"));
1763        assert!(!is_mutatable_ts("src/index.test.ts"));
1764        assert!(!is_mutatable_ts("src/index.spec.ts"));
1765        assert!(!is_mutatable_ts("src/types.d.ts"));
1766        assert!(!is_mutatable_ts("README.md"));
1767    }
1768
1769    #[test]
1770    fn contiguous_runs_collapses_adjacent_lines() {
1771        let lines: BTreeSet<u64> = [2u64, 3, 4, 7, 9, 10].into_iter().collect();
1772        assert_eq!(contiguous_runs(&lines), vec![(2, 4), (7, 7), (9, 10)]);
1773        assert!(contiguous_runs(&BTreeSet::new()).is_empty());
1774    }
1775
1776    #[test]
1777    fn one_line_flattens_and_caps() {
1778        assert_eq!(one_line("a -\n  b"), "a - b");
1779        let long = "x".repeat(80);
1780        let capped = one_line(&long);
1781        assert!(capped.chars().count() <= 61 && capped.ends_with('…'));
1782    }
1783
1784    #[test]
1785    fn is_mutatable_py_keeps_sources_and_drops_tests() {
1786        assert!(is_mutatable_py("calc.py"));
1787        assert!(is_mutatable_py("pkg/util.py"));
1788        assert!(!is_mutatable_py("calc_test.py"));
1789        assert!(!is_mutatable_py("test_calc.py"));
1790        assert!(!is_mutatable_py("pkg/conftest.py"));
1791        assert!(!is_mutatable_py("README.md"));
1792    }
1793
1794    #[test]
1795    fn mutated_lines_collects_caught_and_missed() {
1796        let report = parse_mutants_report(SAMPLE).unwrap();
1797        assert_eq!(
1798            mutated_lines(&report),
1799            [
1800                ("src/lib.rs".to_string(), 7),
1801                ("src/other.rs".to_string(), 3)
1802            ]
1803            .into_iter()
1804            .collect()
1805        );
1806    }
1807
1808    #[test]
1809    fn evaluate_scoped_drops_a_survivor_on_an_exempt_line() {
1810        let report = parse_mutants_report(SAMPLE).unwrap();
1811        let line_scoped = BTreeMap::from([("src/lib.rs".to_string(), BTreeSet::from([7u32]))]);
1812        let kept = evaluate_scoped(
1813            cargo_mutants_survivors(&report),
1814            &mutated_lines(&report),
1815            &[],
1816            &line_scoped,
1817        )
1818        .unwrap();
1819        assert!(
1820            kept.is_empty(),
1821            "the src/lib.rs:7 survivor should be lifted"
1822        );
1823    }
1824
1825    #[test]
1826    fn evaluate_scoped_rejects_exempting_a_caught_line() {
1827        let report = parse_mutants_report(SAMPLE).unwrap();
1828        let line_scoped = BTreeMap::from([("src/other.rs".to_string(), BTreeSet::from([3u32]))]);
1829        let err = evaluate_scoped(
1830            cargo_mutants_survivors(&report),
1831            &mutated_lines(&report),
1832            &[],
1833            &line_scoped,
1834        )
1835        .unwrap_err();
1836        assert!(
1837            err.to_string().contains("all caught") && err.to_string().contains("src/other.rs:3"),
1838            "got: {err}"
1839        );
1840    }
1841
1842    #[test]
1843    fn evaluate_scoped_leaves_an_unmutated_listed_line_alone() {
1844        let report = parse_mutants_report(SAMPLE).unwrap();
1845        let line_scoped = BTreeMap::from([("src/lib.rs".to_string(), BTreeSet::from([99u32]))]);
1846        let kept = evaluate_scoped(
1847            cargo_mutants_survivors(&report),
1848            &mutated_lines(&report),
1849            &[],
1850            &line_scoped,
1851        )
1852        .unwrap();
1853        assert_eq!(kept.len(), 1);
1854        assert_eq!(kept[0].line, 7);
1855    }
1856
1857    #[test]
1858    fn evaluate_scoped_still_honors_a_whole_file_exemption() {
1859        let report = parse_mutants_report(SAMPLE).unwrap();
1860        let kept = evaluate_scoped(
1861            cargo_mutants_survivors(&report),
1862            &mutated_lines(&report),
1863            &["src/lib.rs".to_string()],
1864            &BTreeMap::new(),
1865        )
1866        .unwrap();
1867        assert!(kept.is_empty());
1868    }
1869
1870    fn unique_tmp() -> PathBuf {
1871        static COUNTER: AtomicU64 = AtomicU64::new(0);
1872        let dir = std::env::temp_dir().join(format!(
1873            "tc-provision-test-{}-{}",
1874            std::process::id(),
1875            COUNTER.fetch_add(1, Ordering::Relaxed)
1876        ));
1877        std::fs::create_dir_all(&dir).unwrap();
1878        dir
1879    }
1880
1881    enum Install {
1882        MustNotRun,
1883        WritesNothing,
1884        WritesBin,
1885        Fails,
1886        CountsSleepsAndWritesBin(std::sync::Arc<AtomicU64>),
1887    }
1888
1889    fn write_bin(bin: &Path) {
1890        std::fs::create_dir_all(bin.parent().unwrap()).unwrap();
1891        std::fs::write(bin, b"binary").unwrap();
1892    }
1893
1894    fn drive_provision(bin: &Path, lock: &Path, install: Install) -> Result<PathBuf> {
1895        provision(bin, lock, || match install {
1896            Install::MustNotRun => panic!("must not reinstall"),
1897            Install::WritesNothing => Ok(()),
1898            Install::WritesBin => {
1899                write_bin(bin);
1900                Ok(())
1901            }
1902            Install::Fails => bail!("install blew up"),
1903            Install::CountsSleepsAndWritesBin(count) => {
1904                count.fetch_add(1, Ordering::SeqCst);
1905                std::thread::sleep(std::time::Duration::from_millis(50));
1906                write_bin(bin);
1907                Ok(())
1908            }
1909        })
1910    }
1911
1912    #[test]
1913    fn provision_returns_an_existing_binary_without_installing() {
1914        let tmp = unique_tmp();
1915        let bin = tmp.join("bin").join("cargo-mutants");
1916        let lock = tmp.join(".install.lock");
1917        write_bin(&bin);
1918        let got = drive_provision(&bin, &lock, Install::MustNotRun).unwrap();
1919        assert_eq!(got, bin);
1920        std::fs::remove_dir_all(&tmp).unwrap();
1921    }
1922
1923    #[test]
1924    fn the_must_not_run_sentinel_panics_when_installation_runs() {
1925        let tmp = unique_tmp();
1926        std::fs::create_dir_all(&tmp).unwrap();
1927        let bin = tmp.join("bin").join("cargo-mutants");
1928        let lock = tmp.join(".install.lock");
1929        let panicked =
1930            std::panic::catch_unwind(|| drive_provision(&bin, &lock, Install::MustNotRun)).is_err();
1931        std::fs::remove_dir_all(&tmp).unwrap();
1932        assert!(panicked);
1933    }
1934
1935    #[test]
1936    fn provision_with_a_rootless_lock_path_fails_to_open_the_lock() {
1937        let bin = unique_tmp().join("bin").join("cargo-mutants");
1938        let err = drive_provision(&bin, Path::new("/"), Install::WritesNothing).unwrap_err();
1939        let msg = format!("{err:#}");
1940        assert!(msg.contains("opening the provisioning lock"), "{msg}");
1941    }
1942
1943    #[test]
1944    fn provision_installs_when_the_binary_is_absent() {
1945        let tmp = unique_tmp();
1946        let bin = tmp.join("bin").join("cargo-mutants");
1947        let lock = tmp.join(".install.lock");
1948        let got = drive_provision(&bin, &lock, Install::WritesBin).unwrap();
1949        assert_eq!(got, bin);
1950        assert_eq!(
1951            std::fs::read(&bin).unwrap(),
1952            b"binary",
1953            "an absent binary must be installed"
1954        );
1955        std::fs::remove_dir_all(&tmp).unwrap();
1956    }
1957
1958    #[test]
1959    fn provision_errors_when_install_produces_no_binary() {
1960        let tmp = unique_tmp();
1961        let bin = tmp.join("bin").join("cargo-mutants");
1962        let lock = tmp.join(".install.lock");
1963        let err = drive_provision(&bin, &lock, Install::WritesNothing).unwrap_err();
1964        assert!(
1965            err.to_string().contains("cargo-mutants is not at"),
1966            "got: {err}"
1967        );
1968        std::fs::remove_dir_all(&tmp).unwrap();
1969    }
1970
1971    #[test]
1972    fn provision_propagates_an_install_failure() {
1973        let tmp = unique_tmp();
1974        let bin = tmp.join("bin").join("cargo-mutants");
1975        let lock = tmp.join(".install.lock");
1976        let err = drive_provision(&bin, &lock, Install::Fails).unwrap_err();
1977        assert!(err.to_string().contains("install blew up"), "got: {err}");
1978        std::fs::remove_dir_all(&tmp).unwrap();
1979    }
1980
1981    #[test]
1982    fn provision_does_not_duplicate_the_install_under_concurrent_callers() {
1983        // On a cold cache, N concurrent callers must share one install: cargo-mutants' compile
1984        // duplicated N times turned a ~1-minute cold-cache cost into ~7 minutes. The barrier and
1985        // the sleeping installer widen the race window so this reproduces deterministically.
1986        use std::sync::{Arc, Barrier};
1987        use std::thread;
1988
1989        let tmp = unique_tmp();
1990        let bin = tmp.join("bin").join("cargo-mutants");
1991        let lock = tmp.join(".install.lock");
1992        let install_count = Arc::new(AtomicU64::new(0));
1993        let barrier = Arc::new(Barrier::new(2));
1994
1995        let handles: Vec<_> = (0..2)
1996            .map(|_| {
1997                let bin = bin.clone();
1998                let lock = lock.clone();
1999                let install_count = Arc::clone(&install_count);
2000                let barrier = Arc::clone(&barrier);
2001                thread::spawn(move || {
2002                    barrier.wait();
2003                    drive_provision(
2004                        &bin,
2005                        &lock,
2006                        Install::CountsSleepsAndWritesBin(install_count),
2007                    )
2008                })
2009            })
2010            .collect();
2011
2012        for h in handles {
2013            h.join()
2014                .expect("provisioning thread must not panic")
2015                .unwrap();
2016        }
2017
2018        assert_eq!(
2019            install_count.load(Ordering::SeqCst),
2020            1,
2021            "two concurrent callers on a cold cache must share one install, not each run their own"
2022        );
2023        std::fs::remove_dir_all(&tmp).unwrap();
2024    }
2025
2026    #[test]
2027    fn resolve_cache_base_prefers_xdg_then_home_then_temp() {
2028        let xdg = |s: &str| Some(OsString::from(s));
2029        assert_eq!(
2030            resolve_cache_base(xdg("/xdg"), xdg("/home")),
2031            PathBuf::from("/xdg")
2032        );
2033        assert_eq!(
2034            resolve_cache_base(xdg(""), xdg("/home")),
2035            PathBuf::from("/home/.cache")
2036        );
2037        assert_eq!(
2038            resolve_cache_base(None, xdg("/home")),
2039            PathBuf::from("/home/.cache")
2040        );
2041        assert_eq!(resolve_cache_base(None, None), std::env::temp_dir());
2042        assert_eq!(
2043            resolve_cache_base(xdg(""), Some(OsString::new())),
2044            std::env::temp_dir()
2045        );
2046    }
2047
2048    #[test]
2049    fn cache_root_is_absolute_and_version_scoped() {
2050        let root = cargo_mutants_cache_root();
2051        assert!(
2052            root.ends_with(format!("cargo-mutants-{CARGO_MUTANTS_VERSION}")),
2053            "version-scoped; got {root:?}"
2054        );
2055        assert!(
2056            root.to_string_lossy().contains("testing-conventions"),
2057            "tool-namespaced; got {root:?}"
2058        );
2059        assert!(
2060            root.is_absolute(),
2061            "expected an absolute path; got {root:?}"
2062        );
2063    }
2064
2065    #[test]
2066    fn install_argv_pins_the_version_and_isolates_the_root() {
2067        let argv: Vec<String> = install_argv(Path::new("/cache/cargo-mutants-27"))
2068            .iter()
2069            .map(|arg| arg.to_string_lossy().into_owned())
2070            .collect();
2071        assert_eq!(
2072            argv,
2073            vec![
2074                "install",
2075                "cargo-mutants",
2076                "--locked",
2077                "--version",
2078                CARGO_MUTANTS_VERSION,
2079                "--root",
2080                "/cache/cargo-mutants-27",
2081            ]
2082        );
2083    }
2084
2085    #[test]
2086    fn mutants_argv_enables_features_on_the_engine_itself() {
2087        let argv = |diff, features: &[&str]| -> Vec<String> {
2088            mutants_argv(
2089                Path::new("/out"),
2090                diff,
2091                &features.iter().map(|f| f.to_string()).collect::<Vec<_>>(),
2092            )
2093            .iter()
2094            .map(|arg| arg.to_string_lossy().into_owned())
2095            .collect()
2096        };
2097        assert_eq!(
2098            argv(None, &["cli", "boost"]),
2099            vec![
2100                "mutants",
2101                "--output",
2102                "/out",
2103                "--cargo-test-arg",
2104                "--lib",
2105                "--features",
2106                "cli,boost"
2107            ]
2108        );
2109        assert_eq!(
2110            argv(Some(Path::new("/out/base.diff")), &["cli"]),
2111            vec![
2112                "mutants",
2113                "--output",
2114                "/out",
2115                "--cargo-test-arg",
2116                "--lib",
2117                "--in-diff",
2118                "/out/base.diff",
2119                "--features",
2120                "cli",
2121            ]
2122        );
2123        assert_eq!(
2124            argv(None, &[]),
2125            vec!["mutants", "--output", "/out", "--cargo-test-arg", "--lib"]
2126        );
2127    }
2128
2129    #[test]
2130    fn list_argv_mirrors_the_run_feature_selection() {
2131        let argv = |features: &[&str]| -> Vec<String> {
2132            list_argv(&features.iter().map(|f| f.to_string()).collect::<Vec<_>>())
2133                .iter()
2134                .map(|arg| arg.to_string_lossy().into_owned())
2135                .collect()
2136        };
2137        assert_eq!(argv(&[]), vec!["mutants", "--list", "--json"]);
2138        assert_eq!(
2139            argv(&["cli", "boost"]),
2140            vec!["mutants", "--list", "--json", "--features", "cli,boost"]
2141        );
2142    }
2143
2144    #[test]
2145    fn parse_base_diff_maps_inserted_lines_per_hunk() {
2146        let diff = "\
2147diff --git a/src/lib.rs b/src/lib.rs
2148--- a/src/lib.rs
2149+++ b/src/lib.rs
2150@@ -1,4 +1,5 @@
2151 fn a() {}
2152+fn b() {}
2153 fn c() {}
2154-fn d() {}
2155+fn e() {}
2156 fn f() {}
2157@@ -10,2 +11,4 @@
2158 tail
2159+one
2160+two
2161 more
2162";
2163        let parsed = parse_base_diff(diff);
2164        assert_eq!(parsed.files, vec!["src/lib.rs"]);
2165        assert_eq!(
2166            parsed.inserted.get("src/lib.rs"),
2167            Some(&BTreeSet::from([2, 4, 12, 13]))
2168        );
2169    }
2170
2171    #[test]
2172    fn parse_base_diff_leaves_a_deletion_only_file_without_inserted_lines() {
2173        let diff = "\
2174--- a/src/gone.rs
2175+++ b/src/gone.rs
2176@@ -5,2 +4,0 @@
2177-x
2178-y
2179";
2180        let parsed = parse_base_diff(diff);
2181        assert_eq!(parsed.files, vec!["src/gone.rs"]);
2182        assert!(parsed.inserted.is_empty());
2183    }
2184
2185    #[test]
2186    fn parse_base_diff_skips_a_deleted_file() {
2187        let diff = "\
2188--- a/src/dead.rs
2189+++ /dev/null
2190@@ -1,2 +0,0 @@
2191-a
2192-b
2193";
2194        let parsed = parse_base_diff(diff);
2195        assert!(parsed.files.is_empty());
2196        assert!(parsed.inserted.is_empty());
2197    }
2198
2199    #[test]
2200    fn parse_base_diff_consumes_hunk_bodies_by_count_so_content_never_reads_as_a_header() {
2201        // The inserted content line begins with `+++`; consuming the hunk by its declared
2202        // counts keeps it a body line, not a second file header.
2203        let diff = "\
2204+++ b/notes.txt
2205@@ -1,1 +1,2 @@
2206 keep
2207++++ not a header
2208";
2209        let parsed = parse_base_diff(diff);
2210        assert_eq!(parsed.files, vec!["notes.txt"]);
2211        assert_eq!(parsed.inserted.get("notes.txt"), Some(&BTreeSet::from([2])));
2212    }
2213
2214    #[test]
2215    fn parse_base_diff_defaults_an_elided_hunk_count_to_one() {
2216        let diff = "\
2217+++ b/one.txt
2218@@ -1 +1 @@
2219-old
2220+new
2221";
2222        let parsed = parse_base_diff(diff);
2223        assert_eq!(parsed.inserted.get("one.txt"), Some(&BTreeSet::from([1])));
2224    }
2225
2226    #[test]
2227    fn parse_base_diff_skips_no_newline_annotations_mid_hunk() {
2228        let diff = "\
2229+++ b/n.txt
2230@@ -1 +1 @@
2231-old
2232\\ No newline at end of file
2233+new
2234\\ No newline at end of file
2235";
2236        let parsed = parse_base_diff(diff);
2237        assert_eq!(parsed.inserted.get("n.txt"), Some(&BTreeSet::from([1])));
2238    }
2239
2240    #[cfg(unix)]
2241    fn fake_output(code: i32, stderr: &str) -> Output {
2242        use std::os::unix::process::ExitStatusExt;
2243        Output {
2244            status: std::process::ExitStatus::from_raw(code << 8),
2245            stdout: Vec::new(),
2246            stderr: stderr.as_bytes().to_vec(),
2247        }
2248    }
2249
2250    #[cfg(unix)]
2251    enum FakeRun {
2252        AssertsVersionAndSucceeds,
2253        FailsWith(&'static str),
2254        SpawnError,
2255    }
2256
2257    #[cfg(unix)]
2258    fn drive_install(root: &Path, run: FakeRun) -> Result<()> {
2259        run_install(root, |command| match run {
2260            FakeRun::AssertsVersionAndSucceeds => {
2261                let argv: Vec<String> = command
2262                    .get_args()
2263                    .map(|arg| arg.to_string_lossy().into_owned())
2264                    .collect();
2265                assert!(argv.contains(&CARGO_MUTANTS_VERSION.to_string()));
2266                Ok(fake_output(0, ""))
2267            }
2268            FakeRun::FailsWith(stderr) => Ok(fake_output(1, stderr)),
2269            FakeRun::SpawnError => Err(std::io::Error::new(
2270                std::io::ErrorKind::NotFound,
2271                "no cargo",
2272            )),
2273        })
2274    }
2275
2276    #[cfg(unix)]
2277    #[test]
2278    fn run_install_succeeds_on_a_zero_exit() {
2279        drive_install(Path::new("/cache/root"), FakeRun::AssertsVersionAndSucceeds).unwrap();
2280    }
2281
2282    #[cfg(unix)]
2283    #[test]
2284    fn run_install_reports_a_nonzero_exit_with_the_engine_output() {
2285        let err = drive_install(
2286            Path::new("/cache/root"),
2287            FakeRun::FailsWith("error: could not compile cargo-mutants"),
2288        )
2289        .unwrap_err();
2290        assert!(
2291            err.to_string()
2292                .contains("failed to provision cargo-mutants")
2293                && err.to_string().contains("could not compile"),
2294            "got: {err}"
2295        );
2296    }
2297
2298    #[cfg(unix)]
2299    #[test]
2300    fn run_install_propagates_a_spawn_failure() {
2301        let err = drive_install(Path::new("/cache/root"), FakeRun::SpawnError).unwrap_err();
2302        assert!(
2303            err.to_string().contains("is cargo installed?"),
2304            "got: {err}"
2305        );
2306    }
2307
2308    #[cfg(unix)]
2309    #[test]
2310    fn provision_pinned_installs_via_the_injected_runner() {
2311        let root = unique_tmp();
2312        let bin = root.join("bin").join(CARGO_MUTANTS_BIN_NAME);
2313        let expected = bin.clone();
2314        let got = provision_pinned(&root, |_| {
2315            write_bin(&bin);
2316            Ok(fake_output(0, ""))
2317        })
2318        .unwrap();
2319        assert_eq!(got, expected);
2320        std::fs::remove_dir_all(&root).unwrap();
2321    }
2322
2323    #[test]
2324    fn execute_surfaces_a_spawn_failure() {
2325        let err = execute(&mut Command::new("/nonexistent-tc-cargo")).unwrap_err();
2326        assert_eq!(err.kind(), std::io::ErrorKind::NotFound);
2327    }
2328
2329    #[cfg(unix)]
2330    fn fake_stdout(code: i32, stdout: &str) -> Output {
2331        use std::os::unix::process::ExitStatusExt;
2332        Output {
2333            status: std::process::ExitStatus::from_raw(code << 8),
2334            stdout: stdout.as_bytes().to_vec(),
2335            stderr: Vec::new(),
2336        }
2337    }
2338
2339    #[cfg(unix)]
2340    enum FakeList {
2341        AssertsArgvAndReturns(&'static str, Vec<&'static str>),
2342        FailsWith(&'static str),
2343        SpawnError,
2344    }
2345
2346    #[cfg(unix)]
2347    fn drive_list(features: &[String], run: FakeList) -> Result<Vec<MutantInfo>> {
2348        list_cargo_mutants(
2349            Path::new("/cache/bin/cargo-mutants"),
2350            Path::new("/crate"),
2351            features,
2352            |command| match run {
2353                FakeList::AssertsArgvAndReturns(json, expected) => {
2354                    let argv: Vec<String> = command
2355                        .get_args()
2356                        .map(|arg| arg.to_string_lossy().into_owned())
2357                        .collect();
2358                    assert_eq!(argv, expected);
2359                    assert_eq!(command.get_current_dir(), Some(Path::new("/crate")));
2360                    Ok(fake_stdout(0, json))
2361                }
2362                FakeList::FailsWith(stderr) => Ok(fake_output(1, stderr)),
2363                FakeList::SpawnError => Err(std::io::Error::new(
2364                    std::io::ErrorKind::NotFound,
2365                    "no engine",
2366                )),
2367            },
2368        )
2369    }
2370
2371    #[cfg(unix)]
2372    #[test]
2373    fn list_cargo_mutants_parses_the_listing_from_a_clean_run() {
2374        let json = r#"[{"file": "src/lib.rs", "name": "replace add -> 0",
2375            "span": {"start": {"line": 3, "column": 1}, "end": {"line": 5, "column": 2}}}]"#;
2376        let listed = drive_list(
2377            &["cli".to_string()],
2378            FakeList::AssertsArgvAndReturns(
2379                json,
2380                vec!["mutants", "--list", "--json", "--features", "cli"],
2381            ),
2382        )
2383        .unwrap();
2384        assert_eq!(listed.len(), 1);
2385        assert_eq!(listed[0].file, "src/lib.rs");
2386        assert_eq!(listed[0].span.start.line, 3);
2387        assert_eq!(listed[0].span.end.line, 5);
2388        assert_eq!(listed[0].name, "replace add -> 0");
2389    }
2390
2391    #[cfg(unix)]
2392    #[test]
2393    fn list_cargo_mutants_reports_a_nonzero_exit_with_the_engine_output() {
2394        let err = drive_list(&[], FakeList::FailsWith("error: no such option")).unwrap_err();
2395        assert!(
2396            err.to_string().contains("cargo-mutants --list failed")
2397                && err.to_string().contains("no such option"),
2398            "got: {err}"
2399        );
2400    }
2401
2402    #[cfg(unix)]
2403    #[test]
2404    fn list_cargo_mutants_propagates_a_spawn_failure() {
2405        let err = drive_list(&[], FakeList::SpawnError).unwrap_err();
2406        assert!(
2407            err.to_string()
2408                .contains("listing the crate's mutants with cargo-mutants"),
2409            "got: {err}"
2410        );
2411    }
2412
2413    #[cfg(unix)]
2414    fn listed_mutant(file: &str, start: u32, end: u32, name: &str) -> MutantInfo {
2415        MutantInfo {
2416            file: file.to_string(),
2417            span: Span {
2418                start: LineCol { line: start },
2419                end: LineCol { line: end },
2420            },
2421            name: name.to_string(),
2422        }
2423    }
2424
2425    #[cfg(unix)]
2426    fn diff_with_inserted(file: &str, lines: &[u32]) -> BaseDiff {
2427        BaseDiff {
2428            files: vec![file.to_string()],
2429            inserted: BTreeMap::from([(file.to_string(), lines.iter().copied().collect())]),
2430        }
2431    }
2432
2433    #[cfg(unix)]
2434    #[test]
2435    fn zero_mutant_verdict_accepts_a_zero_with_no_mutant_on_the_inserted_lines() {
2436        let run = fake_output(0, "");
2437        zero_mutant_verdict(&[], &diff_with_inserted("src/lib.rs", &[5]), &run).unwrap();
2438        let listed = [listed_mutant("src/lib.rs", 5, 8, "replace add -> 0")];
2439        zero_mutant_verdict(&listed, &diff_with_inserted("src/lib.rs", &[4, 9]), &run).unwrap();
2440        zero_mutant_verdict(&listed, &diff_with_inserted("src/other.rs", &[6]), &run).unwrap();
2441    }
2442
2443    #[cfg(unix)]
2444    #[test]
2445    fn zero_mutant_verdict_is_fatal_on_a_mutant_at_either_span_boundary() {
2446        let listed = [listed_mutant("src/lib.rs", 5, 8, "replace add -> 0")];
2447        let run = fake_stdout(0, "0 mutants tested");
2448        for line in [5, 8] {
2449            let err =
2450                zero_mutant_verdict(&listed, &diff_with_inserted("src/lib.rs", &[line]), &run)
2451                    .unwrap_err();
2452            let message = err.to_string();
2453            assert!(
2454                message.contains("1 of the crate's 1 mutant site(s)")
2455                    && message.contains("src/lib.rs:5: replace add -> 0")
2456                    && message.contains("0 mutants tested"),
2457                "got: {message}"
2458            );
2459        }
2460    }
2461
2462    #[cfg(unix)]
2463    #[test]
2464    fn zero_mutant_verdict_names_each_dropped_site_once() {
2465        let listed = [
2466            listed_mutant(
2467                "src/lib.rs",
2468                7,
2469                7,
2470                "src/lib.rs:7:7: replace > with == in is_positive",
2471            ),
2472            listed_mutant("src/lib.rs", 7, 7, "replace add -> 0"),
2473        ];
2474        let run = fake_stdout(0, "0 mutants tested");
2475        let message = zero_mutant_verdict(&listed, &diff_with_inserted("src/lib.rs", &[7]), &run)
2476            .unwrap_err()
2477            .to_string();
2478        assert!(
2479            message.contains("  src/lib.rs:7: replace > with == in is_positive"),
2480            "the name's embedded `file:line:col:` prefix is stripped; got: {message}"
2481        );
2482        assert!(
2483            !message.contains(": src/lib.rs:7:7:"),
2484            "a dropped site carries one location; got: {message}"
2485        );
2486        assert!(
2487            message.contains("  src/lib.rs:7: replace add -> 0"),
2488            "a name with no embedded location keeps its rendered location; got: {message}"
2489        );
2490    }
2491
2492    #[cfg(unix)]
2493    #[test]
2494    fn classify_mutants_exit_accepts_the_caught_and_survivor_exits() {
2495        classify_mutants_exit(Path::new("/crate"), &fake_output(0, "")).unwrap();
2496        classify_mutants_exit(Path::new("/crate"), &fake_output(2, "")).unwrap();
2497    }
2498
2499    #[cfg(unix)]
2500    #[test]
2501    fn classify_mutants_exit_accepts_a_timeout_exit_3() {
2502        classify_mutants_exit(Path::new("/crate"), &fake_output(3, ""))
2503            .expect("a timeout (exit 3) is inconclusive, not fatal");
2504    }
2505
2506    #[cfg(unix)]
2507    #[test]
2508    fn classify_mutants_exit_is_fatal_on_a_baseline_failure() {
2509        let err = classify_mutants_exit(Path::new("/crate"), &fake_output(4, "baseline broke"))
2510            .unwrap_err();
2511        assert!(
2512            err.to_string().contains("did not run cleanly")
2513                && err.to_string().contains("baseline broke"),
2514            "got: {err}"
2515        );
2516    }
2517
2518    #[test]
2519    fn cargo_mutants_bin_name_matches_the_platform() {
2520        #[cfg(windows)]
2521        assert_eq!(CARGO_MUTANTS_BIN_NAME, "cargo-mutants.exe");
2522        #[cfg(not(windows))]
2523        assert_eq!(CARGO_MUTANTS_BIN_NAME, "cargo-mutants");
2524    }
2525}