1use std::collections::{BTreeMap, BTreeSet};
19use std::ffi::OsString;
20use std::path::{Path, PathBuf};
21use std::process::{Command, Output};
22use std::sync::atomic::{AtomicU64, Ordering};
23
24use anyhow::{bail, Context, Result};
25use serde::Deserialize;
26
27#[derive(Debug, Clone, PartialEq, Eq)]
29pub struct Survivor {
30 pub file: String,
32 pub line: u32,
34 pub description: String,
36}
37
38pub type MutatedLines = BTreeSet<(String, u32)>;
42
43#[derive(Debug, Clone, Deserialize)]
46pub struct MutantsReport {
47 pub outcomes: Vec<MutantOutcome>,
48}
49
50#[derive(Debug, Clone, Deserialize)]
54pub struct MutantOutcome {
55 pub summary: String,
56 pub scenario: Scenario,
57}
58
59#[derive(Debug, Clone, Deserialize)]
62pub enum Scenario {
63 Baseline,
64 Mutant(MutantInfo),
65}
66
67#[derive(Debug, Clone, Deserialize)]
71pub struct MutantInfo {
72 pub file: String,
73 pub span: Span,
74 pub name: String,
75}
76
77#[derive(Debug, Clone, Deserialize)]
79pub struct Span {
80 pub start: LineCol,
81}
82
83#[derive(Debug, Clone, Deserialize)]
85pub struct LineCol {
86 pub line: u32,
87}
88
89pub fn parse_mutants_report(json: &str) -> Result<MutantsReport> {
91 serde_json::from_str(json).context("parsing cargo-mutants outcomes.json")
92}
93
94pub fn unexplained_survivors(report: &MutantsReport, exempt: &[String]) -> Vec<Survivor> {
103 evaluate(cargo_mutants_survivors(report), exempt)
104}
105
106fn cargo_mutants_survivors(report: &MutantsReport) -> Vec<Survivor> {
110 report
111 .outcomes
112 .iter()
113 .filter_map(|outcome| {
114 if outcome.summary != "MissedMutant" {
115 return None;
116 }
117 let Scenario::Mutant(mutant) = &outcome.scenario else {
118 return None;
119 };
120 Some(Survivor {
121 file: mutant.file.clone(),
122 line: mutant.span.start.line,
123 description: mutant.name.clone(),
124 })
125 })
126 .collect()
127}
128
129pub fn mutated_lines(report: &MutantsReport) -> MutatedLines {
135 report
136 .outcomes
137 .iter()
138 .filter_map(|outcome| {
139 if outcome.summary != "CaughtMutant" && outcome.summary != "MissedMutant" {
140 return None;
141 }
142 let Scenario::Mutant(mutant) = &outcome.scenario else {
143 return None;
144 };
145 Some((mutant.file.clone(), mutant.span.start.line))
146 })
147 .collect()
148}
149
150pub fn evaluate(survivors: Vec<Survivor>, exempt: &[String]) -> Vec<Survivor> {
155 survivors
156 .into_iter()
157 .filter(|survivor| !exempt.iter().any(|path| path == &survivor.file))
158 .collect()
159}
160
161pub fn evaluate_scoped(
173 survivors: Vec<Survivor>,
174 mutated: &MutatedLines,
175 whole_file: &[String],
176 line_scoped: &BTreeMap<String, BTreeSet<u32>>,
177) -> Result<Vec<Survivor>> {
178 let mut over: Vec<String> = Vec::new();
179 for (file, lines) in line_scoped {
180 for &line in lines {
181 let has_survivor = survivors
182 .iter()
183 .any(|survivor| survivor.file == *file && survivor.line == line);
184 if has_survivor {
185 continue;
186 }
187 if mutated.contains(&(file.clone(), line)) {
188 over.push(format!("\n {file}:{line}"));
189 }
190 }
191 }
192 if !over.is_empty() {
193 bail!(
194 "a line-scoped mutation exemption may only list a line with a surviving mutant, but \
195 these had mutants that were all caught:{}",
196 over.concat()
197 );
198 }
199 Ok(survivors
200 .into_iter()
201 .filter(|survivor| {
202 let whole = whole_file.iter().any(|path| path == &survivor.file);
203 let line = line_scoped
204 .get(&survivor.file)
205 .is_some_and(|lines| lines.contains(&survivor.line));
206 !(whole || line)
207 })
208 .collect())
209}
210
211#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
217#[serde(rename_all = "snake_case")]
218pub enum MutantStatus {
219 Survived,
221 Killed,
223 NoCoverage,
225 Timeout,
227 CompileError,
229 RuntimeError,
231}
232
233impl MutantStatus {
234 fn is_survivor(self) -> bool {
237 matches!(self, MutantStatus::Survived | MutantStatus::NoCoverage)
238 }
239
240 fn is_viable(self) -> bool {
245 matches!(
246 self,
247 MutantStatus::Survived
248 | MutantStatus::Killed
249 | MutantStatus::NoCoverage
250 | MutantStatus::Timeout
251 )
252 }
253}
254
255#[derive(Debug, Clone, Deserialize)]
258pub struct NormalizedMutant {
259 pub file: String,
261 pub line: u32,
263 pub status: MutantStatus,
265 pub mutator: String,
267 #[serde(default)]
269 pub replacement: Option<String>,
270}
271
272pub fn parse_normalized_results(json: &str) -> Result<Vec<NormalizedMutant>> {
275 serde_json::from_str(json).context("parsing normalized mutation results")
276}
277
278pub fn evaluate_normalized(
286 mutants: &[NormalizedMutant],
287 whole_file: &[String],
288 line_scoped: &BTreeMap<String, BTreeSet<u32>>,
289) -> Result<Vec<Survivor>> {
290 evaluate_scoped(
291 normalized_survivors(mutants),
292 &normalized_mutated_lines(mutants),
293 whole_file,
294 line_scoped,
295 )
296}
297
298fn normalized_survivors(mutants: &[NormalizedMutant]) -> Vec<Survivor> {
300 mutants
301 .iter()
302 .filter(|mutant| mutant.status.is_survivor())
303 .map(|mutant| Survivor {
304 file: mutant.file.clone(),
305 line: mutant.line,
306 description: describe_normalized(mutant),
307 })
308 .collect()
309}
310
311fn normalized_mutated_lines(mutants: &[NormalizedMutant]) -> MutatedLines {
314 mutants
315 .iter()
316 .filter(|mutant| mutant.status.is_viable())
317 .map(|mutant| (mutant.file.clone(), mutant.line))
318 .collect()
319}
320
321fn describe_normalized(mutant: &NormalizedMutant) -> String {
324 match &mutant.replacement {
325 Some(replacement) => format!("{} (-> {})", mutant.mutator, one_line(replacement)),
326 None => mutant.mutator.clone(),
327 }
328}
329
330pub fn measure_rust(
338 root: &Path,
339 exempt: &[String],
340 exempt_lines: &BTreeMap<String, BTreeSet<u32>>,
341 base: Option<&str>,
342 features: &[String],
343) -> Result<Vec<Survivor>> {
344 let out = MutantsOut::new();
345 let diff = match base {
346 Some(base) => match write_base_diff(root, base, &out)? {
349 None => return Ok(Vec::new()),
350 Some(path) => Some(path),
351 },
352 None => None,
353 };
354 let engine = ensure_cargo_mutants()?;
355 run_cargo_mutants(&engine, root, &out.0, diff.as_deref(), features)?;
356 let outcomes = out.0.join("mutants.out").join("outcomes.json");
357 let json = match std::fs::read_to_string(&outcomes) {
361 Ok(json) => json,
362 Err(_) => return Ok(Vec::new()),
363 };
364 let report = parse_mutants_report(&json)?;
365 evaluate_scoped(
366 cargo_mutants_survivors(&report),
367 &mutated_lines(&report),
368 exempt,
369 exempt_lines,
370 )
371}
372
373fn one_line(replacement: &str) -> String {
376 let flat = replacement.split_whitespace().collect::<Vec<_>>().join(" ");
377 const MAX: usize = 60;
378 if flat.chars().count() > MAX {
379 format!("{}…", flat.chars().take(MAX).collect::<String>())
380 } else {
381 flat
382 }
383}
384
385pub fn measure_typescript(
404 root: &Path,
405 exempt: &[String],
406 exempt_lines: &BTreeMap<String, BTreeSet<u32>>,
407 base: Option<&str>,
408 adapter: &Path,
409) -> Result<Vec<Survivor>> {
410 let mutate = match base {
411 Some(base) => {
412 let ranges = mutate_ranges(root, base)?;
413 if ranges.is_empty() {
415 return Ok(Vec::new());
416 }
417 Some(ranges)
418 }
419 None => None,
420 };
421 let json = run_ts_adapter(root, adapter, mutate.as_deref())?;
422 let mutants = parse_normalized_results(&json)?;
423 evaluate_normalized(&mutants, exempt, exempt_lines)
424}
425
426fn run_ts_adapter(root: &Path, adapter: &Path, mutate: Option<&[String]>) -> Result<String> {
437 let out = AdapterOut::new();
438 std::fs::create_dir_all(&out.0).context("creating the mutation adapter output dir")?;
439 let results = out.0.join("results.json");
440
441 let mut command = Command::new("node");
442 command
443 .current_dir(root)
444 .arg(adapter)
445 .arg("--out")
446 .arg(&results);
447 if let Some(specs) = mutate {
448 command.arg("--mutate").arg(specs.join(","));
449 }
450 let output = command
451 .output()
452 .context("running the TypeScript mutation adapter (is `node` installed?)")?;
453 if !output.status.success() {
454 bail!(
455 "the TypeScript mutation adapter failed in `{}`:\n{}{}",
456 root.display(),
457 String::from_utf8_lossy(&output.stdout),
458 String::from_utf8_lossy(&output.stderr),
459 );
460 }
461 std::fs::read_to_string(&results).with_context(|| {
462 format!(
463 "reading the TypeScript mutation adapter's results from `{}`",
464 results.display()
465 )
466 })
467}
468
469struct AdapterOut(PathBuf);
472
473impl AdapterOut {
474 fn new() -> Self {
475 static COUNTER: AtomicU64 = AtomicU64::new(0);
476 let name = format!(
477 "testing-conventions-ts-adapter-{}-{}",
478 std::process::id(),
479 COUNTER.fetch_add(1, Ordering::Relaxed),
480 );
481 AdapterOut(std::env::temp_dir().join(name))
482 }
483}
484
485impl Drop for AdapterOut {
486 fn drop(&mut self) {
487 let _ = std::fs::remove_dir_all(&self.0);
488 }
489}
490
491fn mutate_ranges(root: &Path, base: &str) -> Result<Vec<String>> {
497 let changed = crate::patch_coverage::changed_lines(root, base)?;
498 let mut specs = Vec::new();
499 for (file, lines) in changed {
500 if !is_mutatable_ts(&file) {
501 continue;
502 }
503 for (start, end) in contiguous_runs(&lines) {
504 specs.push(format!("{file}:{start}-{end}"));
505 }
506 }
507 Ok(specs)
508}
509
510fn is_mutatable_ts(file: &str) -> bool {
514 let is_source = [".ts", ".tsx", ".mts", ".cts", ".js", ".jsx", ".mjs", ".cjs"]
515 .iter()
516 .any(|ext| file.ends_with(ext));
517 let is_decl = file.ends_with(".d.ts");
518 let is_test = file.contains(".test.") || file.contains(".spec.");
519 is_source && !is_decl && !is_test
520}
521
522fn contiguous_runs(lines: &BTreeSet<u64>) -> Vec<(u64, u64)> {
524 let mut runs: Vec<(u64, u64)> = Vec::new();
525 for &line in lines {
526 match runs.last_mut() {
527 Some(run) if run.1 + 1 == line => run.1 = line,
528 _ => runs.push((line, line)),
529 }
530 }
531 runs
532}
533
534pub fn measure_python(
552 root: &Path,
553 exempt: &[String],
554 exempt_lines: &BTreeMap<String, BTreeSet<u32>>,
555 base: Option<&str>,
556) -> Result<Vec<Survivor>> {
557 let changed = match base {
558 Some(base) => Some(crate::patch_coverage::changed_lines(root, base)?),
559 None => None,
560 };
561 let modules: Vec<String> = match &changed {
562 None => Vec::new(),
563 Some(changed) => {
564 let modules: Vec<String> = changed
565 .keys()
566 .filter(|file| is_mutatable_py(file))
567 .cloned()
568 .collect();
569 if modules.is_empty() {
571 return Ok(Vec::new());
572 }
573 modules
574 }
575 };
576 let json = run_py_adapter(root, &modules)?;
577 let mut mutants = parse_normalized_results(&json)?;
578 if let Some(changed) = &changed {
579 mutants.retain(|mutant| {
581 changed
582 .get(&mutant.file)
583 .is_some_and(|lines| lines.contains(&u64::from(mutant.line)))
584 });
585 }
586 evaluate_normalized(&mutants, exempt, exempt_lines)
587}
588
589fn run_py_adapter(root: &Path, modules: &[String]) -> 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 mut command = Command::new("python3");
602 command
603 .current_dir(root)
604 .args(["-m", "testing_conventions.mutation.main", "--out"])
605 .arg(&results)
606 .env("PYTHONDONTWRITEBYTECODE", "1");
607 for module in modules {
608 command.arg("--module").arg(module);
609 }
610 let output = command
611 .output()
612 .context("running the Python mutation adapter (is `python3` installed?)")?;
613 if !output.status.success() {
614 bail!(
615 "the Python mutation adapter failed in `{}`:\n{}{}",
616 root.display(),
617 String::from_utf8_lossy(&output.stdout),
618 String::from_utf8_lossy(&output.stderr),
619 );
620 }
621 std::fs::read_to_string(&results).with_context(|| {
622 format!(
623 "reading the Python mutation adapter's results from `{}`",
624 results.display()
625 )
626 })
627}
628
629fn is_mutatable_py(file: &str) -> bool {
632 if !file.ends_with(".py") {
633 return false;
634 }
635 let base = file.rsplit('/').next().unwrap_or(file);
636 !(base.ends_with("_test.py") || base.starts_with("test_") || base == "conftest.py")
637}
638
639struct MutantsOut(PathBuf);
642
643impl MutantsOut {
644 fn new() -> Self {
645 static COUNTER: AtomicU64 = AtomicU64::new(0);
646 let name = format!(
647 "testing-conventions-mutants-{}-{}",
648 std::process::id(),
649 COUNTER.fetch_add(1, Ordering::Relaxed),
650 );
651 MutantsOut(std::env::temp_dir().join(name))
652 }
653}
654
655impl Drop for MutantsOut {
656 fn drop(&mut self) {
657 let _ = std::fs::remove_dir_all(&self.0);
658 }
659}
660
661fn write_base_diff(root: &Path, base: &str, out: &MutantsOut) -> Result<Option<PathBuf>> {
670 let range = format!("{base}...HEAD");
671 let output = Command::new("git")
672 .current_dir(root)
673 .args(["diff", "--relative", &range])
674 .output()
675 .context("running `git diff` for `--base` (is git installed?)")?;
676 if !output.status.success() {
677 bail!(
678 "git diff {range} failed: {}",
679 String::from_utf8_lossy(&output.stderr)
680 );
681 }
682 if output.stdout.is_empty() {
683 return Ok(None);
684 }
685 std::fs::create_dir_all(&out.0).context("creating the mutants output dir")?;
686 let path = out.0.join("base.diff");
687 std::fs::write(&path, &output.stdout).context("writing the base diff")?;
688 Ok(Some(path))
689}
690
691const CARGO_MUTANTS_VERSION: &str = "27.1.0";
694
695fn ensure_cargo_mutants() -> Result<PathBuf> {
705 let root = cargo_mutants_cache_root();
706 let bin = root.join("bin").join(cargo_mutants_bin_name());
707 let lock_path = root.join(".install.lock");
708 provision(&bin, &lock_path, || {
709 run_install(&root, |command| command.output())
710 })
711}
712
713fn cargo_mutants_bin_name() -> &'static str {
716 if cfg!(windows) {
717 "cargo-mutants.exe"
718 } else {
719 "cargo-mutants"
720 }
721}
722
723fn cargo_mutants_cache_root() -> PathBuf {
727 cache_base()
728 .join("testing-conventions")
729 .join(format!("cargo-mutants-{CARGO_MUTANTS_VERSION}"))
730}
731
732fn cache_base() -> PathBuf {
735 resolve_cache_base(std::env::var_os("XDG_CACHE_HOME"), std::env::var_os("HOME"))
736}
737
738fn resolve_cache_base(xdg: Option<OsString>, home: Option<OsString>) -> PathBuf {
741 if let Some(dir) = xdg.filter(|value| !value.is_empty()) {
742 return PathBuf::from(dir);
743 }
744 if let Some(dir) = home.filter(|value| !value.is_empty()) {
745 return PathBuf::from(dir).join(".cache");
746 }
747 std::env::temp_dir()
748}
749
750fn provision(
765 bin: &Path,
766 lock_path: &Path,
767 install: impl FnOnce() -> Result<()>,
768) -> Result<PathBuf> {
769 if bin.exists() {
770 return Ok(bin.to_path_buf());
771 }
772 if let Some(parent) = lock_path.parent() {
773 std::fs::create_dir_all(parent).context("creating the provisioning lock's parent dir")?;
774 }
775 let lock_file = std::fs::OpenOptions::new()
776 .create(true)
777 .truncate(false)
778 .write(true)
779 .open(lock_path)
780 .context("opening the provisioning lock file")?;
781 lock_file
782 .lock()
783 .context("acquiring the provisioning lock")?;
784 if bin.exists() {
786 return Ok(bin.to_path_buf());
787 }
788 install()?;
789 if !bin.exists() {
790 bail!(
791 "provisioning reported success but cargo-mutants is not at `{}`",
792 bin.display()
793 );
794 }
795 Ok(bin.to_path_buf())
796}
797
798fn install_argv(root: &Path) -> Vec<OsString> {
802 vec![
803 OsString::from("install"),
804 OsString::from("cargo-mutants"),
805 OsString::from("--locked"),
806 OsString::from("--version"),
807 OsString::from(CARGO_MUTANTS_VERSION),
808 OsString::from("--root"),
809 root.as_os_str().to_os_string(),
810 ]
811}
812
813fn run_install(
818 root: &Path,
819 run: impl FnOnce(&mut Command) -> std::io::Result<Output>,
820) -> Result<()> {
821 let mut command = Command::new("cargo");
822 command.args(install_argv(root));
823 strip_llvm_cov_env(&mut command);
824 let output = run(&mut command)
825 .context("provisioning cargo-mutants via `cargo install` (is cargo installed?)")?;
826 if !output.status.success() {
827 bail!(
828 "failed to provision cargo-mutants {CARGO_MUTANTS_VERSION}:\n{}{}",
829 String::from_utf8_lossy(&output.stdout),
830 String::from_utf8_lossy(&output.stderr),
831 );
832 }
833 Ok(())
834}
835
836fn strip_llvm_cov_env(command: &mut Command) {
840 for var in [
841 "RUSTFLAGS",
842 "CARGO_ENCODED_RUSTFLAGS",
843 "RUSTDOCFLAGS",
844 "CARGO_ENCODED_RUSTDOCFLAGS",
845 "LLVM_PROFILE_FILE",
846 "CARGO_LLVM_COV",
847 "CARGO_LLVM_COV_SHOW_ENV",
848 "CARGO_LLVM_COV_TARGET_DIR",
849 "CARGO_LLVM_COV_BUILD_DIR",
850 "RUSTC_WRAPPER",
851 "RUSTC_WORKSPACE_WRAPPER",
852 "__CARGO_LLVM_COV_RUSTC_WRAPPER",
853 "__CARGO_LLVM_COV_RUSTC_WRAPPER_RUSTFLAGS",
854 "__CARGO_LLVM_COV_RUSTC_WRAPPER_CRATE_NAMES",
855 ] {
856 command.env_remove(var);
857 }
858}
859
860fn run_cargo_mutants(
870 engine: &Path,
871 root: &Path,
872 out: &Path,
873 in_diff: Option<&Path>,
874 features: &[String],
875) -> Result<()> {
876 let mut command = Command::new(engine);
877 command
878 .current_dir(root)
879 .arg("mutants")
880 .arg("--output")
881 .arg(out);
882 if let Some(diff) = in_diff {
883 command.arg("--in-diff").arg(diff);
884 }
885 if !features.is_empty() {
886 command.args(["--", "--features"]).arg(features.join(","));
887 }
888 strip_llvm_cov_env(&mut command);
889 let output = command.output().context("running cargo-mutants")?;
890 classify_mutants_exit(root, &output)
891}
892
893fn classify_mutants_exit(root: &Path, output: &Output) -> Result<()> {
903 match output.status.code() {
904 Some(0) | Some(2) | Some(3) => Ok(()),
907 _ => bail!(
908 "cargo-mutants did not run cleanly in `{}` (baseline build/test failure?):\n{}{}",
909 root.display(),
910 String::from_utf8_lossy(&output.stdout),
911 String::from_utf8_lossy(&output.stderr),
912 ),
913 }
914}
915
916#[cfg(test)]
917mod tests {
918 use super::*;
919
920 const NORMALIZED: &str = r#"[
924 {"file": "src/a.ts", "line": 2, "status": "survived",
925 "mutator": "ConditionalExpression", "replacement": "true", "id": "ignored"},
926 {"file": "src/a.ts", "line": 5, "status": "no_coverage", "mutator": "ArithmeticOperator"},
927 {"file": "src/a.ts", "line": 9, "status": "killed",
928 "mutator": "BooleanLiteral", "replacement": "false"},
929 {"file": "src/a.ts", "line": 12, "status": "timeout", "mutator": "BlockStatement"},
930 {"file": "src/a.ts", "line": 15, "status": "compile_error", "mutator": "OptionalChaining"},
931 {"file": "src/a.ts", "line": 18, "status": "runtime_error", "mutator": "StringLiteral"}
932 ]"#;
933
934 #[test]
935 fn parses_the_normalized_schema() {
936 let mutants = parse_normalized_results(NORMALIZED).expect("valid normalized results");
937 assert_eq!(mutants.len(), 6);
938 assert_eq!(mutants[0].status, MutantStatus::Survived);
939 assert_eq!(mutants[1].status, MutantStatus::NoCoverage);
940 assert_eq!(mutants[0].replacement.as_deref(), Some("true"));
941 assert_eq!(mutants[1].replacement, None);
942 }
943
944 #[test]
945 fn normalized_survivors_are_survived_and_nocoverage_only() {
946 let mutants = parse_normalized_results(NORMALIZED).unwrap();
947 let survivors = normalized_survivors(&mutants);
948 assert_eq!(survivors.len(), 2);
950 assert_eq!((survivors[0].line, survivors[1].line), (2, 5));
951 assert!(survivors[0].description.contains("ConditionalExpression"));
953 assert!(survivors[0].description.contains("-> true"));
954 assert_eq!(survivors[1].description, "ArithmeticOperator");
955 }
956
957 #[test]
958 fn normalized_mutated_lines_collects_only_viable_mutants() {
959 let mutants = parse_normalized_results(NORMALIZED).unwrap();
960 assert_eq!(
963 normalized_mutated_lines(&mutants),
964 [2u32, 5, 9, 12]
965 .into_iter()
966 .map(|line| ("src/a.ts".to_string(), line))
967 .collect()
968 );
969 }
970
971 #[test]
972 fn evaluate_normalized_reports_unexempted_survivors() {
973 let mutants = parse_normalized_results(NORMALIZED).unwrap();
974 let kept = evaluate_normalized(&mutants, &[], &BTreeMap::new()).unwrap();
975 assert_eq!(kept.len(), 2, "both survivors stand with no exemptions");
976 }
977
978 #[test]
979 fn evaluate_normalized_drops_a_whole_file_exemption() {
980 let mutants = parse_normalized_results(NORMALIZED).unwrap();
981 let kept =
982 evaluate_normalized(&mutants, &["src/a.ts".to_string()], &BTreeMap::new()).unwrap();
983 assert!(
984 kept.is_empty(),
985 "the whole-file exemption lifts both survivors"
986 );
987 }
988
989 #[test]
990 fn evaluate_normalized_drops_a_line_scoped_exemption() {
991 let mutants = parse_normalized_results(NORMALIZED).unwrap();
992 let line_scoped = BTreeMap::from([("src/a.ts".to_string(), BTreeSet::from([2u32]))]);
993 let kept = evaluate_normalized(&mutants, &[], &line_scoped).unwrap();
994 assert_eq!(kept.len(), 1);
996 assert_eq!(kept[0].line, 5);
997 }
998
999 #[test]
1000 fn evaluate_normalized_rejects_exempting_a_caught_line() {
1001 let mutants = parse_normalized_results(NORMALIZED).unwrap();
1004 let line_scoped = BTreeMap::from([("src/a.ts".to_string(), BTreeSet::from([9u32]))]);
1005 let err = evaluate_normalized(&mutants, &[], &line_scoped).unwrap_err();
1006 assert!(
1007 err.to_string().contains("all caught") && err.to_string().contains("src/a.ts:9"),
1008 "got: {err}"
1009 );
1010 }
1011
1012 #[test]
1013 fn evaluate_normalized_leaves_an_unviable_listed_line_alone() {
1014 let mutants = parse_normalized_results(NORMALIZED).unwrap();
1017 let line_scoped = BTreeMap::from([("src/a.ts".to_string(), BTreeSet::from([15u32]))]);
1018 let kept = evaluate_normalized(&mutants, &[], &line_scoped).unwrap();
1019 assert_eq!(kept.len(), 2);
1020 }
1021
1022 const SAMPLE: &str = r#"{
1025 "outcomes": [
1026 {"scenario": "Baseline", "summary": "Success",
1027 "phase_results": []},
1028 {"scenario": {"Mutant": {"file": "src/lib.rs", "package": "p", "genre": "FnValue",
1029 "replacement": "true", "name": "src/lib.rs:7:7: replace > with == in is_positive",
1030 "function": {"function_name": "is_positive"},
1031 "span": {"start": {"line": 7, "column": 7}, "end": {"line": 7, "column": 8}}}},
1032 "summary": "MissedMutant"},
1033 {"scenario": {"Mutant": {"file": "src/other.rs", "package": "p", "genre": "FnValue",
1034 "replacement": "0", "name": "src/other.rs:3:5: replace add -> i32 with 0",
1035 "span": {"start": {"line": 3, "column": 5}, "end": {"line": 3, "column": 9}}}},
1036 "summary": "CaughtMutant"}
1037 ],
1038 "total_mutants": 2
1039 }"#;
1040
1041 #[test]
1042 fn parses_the_outcomes_export() {
1043 let report = parse_mutants_report(SAMPLE).expect("valid outcomes.json");
1044 assert_eq!(report.outcomes.len(), 3);
1045 assert!(matches!(report.outcomes[0].scenario, Scenario::Baseline));
1046 }
1047
1048 #[test]
1049 fn collects_only_missed_mutants_as_survivors() {
1050 let report = parse_mutants_report(SAMPLE).unwrap();
1051 let survivors = unexplained_survivors(&report, &[]);
1052 assert_eq!(survivors.len(), 1);
1054 assert_eq!(survivors[0].file, "src/lib.rs");
1055 assert_eq!(survivors[0].line, 7);
1056 assert!(survivors[0].description.contains("replace > with =="));
1057 }
1058
1059 #[test]
1060 fn an_exemption_drops_a_survivor_in_that_file() {
1061 let report = parse_mutants_report(SAMPLE).unwrap();
1062 let exempt = vec!["src/lib.rs".to_string()];
1063 assert!(unexplained_survivors(&report, &exempt).is_empty());
1064 }
1065
1066 #[test]
1067 fn an_exemption_on_another_file_leaves_the_survivor() {
1068 let report = parse_mutants_report(SAMPLE).unwrap();
1069 let exempt = vec!["src/elsewhere.rs".to_string()];
1070 assert_eq!(unexplained_survivors(&report, &exempt).len(), 1);
1071 }
1072
1073 #[test]
1074 fn is_mutatable_ts_keeps_sources_and_drops_tests_and_decls() {
1075 assert!(is_mutatable_ts("src/index.ts"));
1076 assert!(is_mutatable_ts("src/util.tsx"));
1077 assert!(is_mutatable_ts("src/util.js"));
1078 assert!(!is_mutatable_ts("src/index.test.ts"));
1079 assert!(!is_mutatable_ts("src/index.spec.ts"));
1080 assert!(!is_mutatable_ts("src/types.d.ts"));
1081 assert!(!is_mutatable_ts("README.md"));
1082 }
1083
1084 #[test]
1085 fn contiguous_runs_collapses_adjacent_lines() {
1086 let lines: BTreeSet<u64> = [2u64, 3, 4, 7, 9, 10].into_iter().collect();
1087 assert_eq!(contiguous_runs(&lines), vec![(2, 4), (7, 7), (9, 10)]);
1088 assert!(contiguous_runs(&BTreeSet::new()).is_empty());
1089 }
1090
1091 #[test]
1092 fn one_line_flattens_and_caps() {
1093 assert_eq!(one_line("a -\n b"), "a - b");
1094 let long = "x".repeat(80);
1095 let capped = one_line(&long);
1096 assert!(capped.chars().count() <= 61 && capped.ends_with('…'));
1097 }
1098
1099 #[test]
1100 fn is_mutatable_py_keeps_sources_and_drops_tests() {
1101 assert!(is_mutatable_py("calc.py"));
1102 assert!(is_mutatable_py("pkg/util.py"));
1103 assert!(!is_mutatable_py("calc_test.py"));
1104 assert!(!is_mutatable_py("test_calc.py"));
1105 assert!(!is_mutatable_py("pkg/conftest.py"));
1106 assert!(!is_mutatable_py("README.md"));
1107 }
1108
1109 #[test]
1110 fn mutated_lines_collects_caught_and_missed() {
1111 let report = parse_mutants_report(SAMPLE).unwrap();
1114 assert_eq!(
1115 mutated_lines(&report),
1116 [
1117 ("src/lib.rs".to_string(), 7),
1118 ("src/other.rs".to_string(), 3)
1119 ]
1120 .into_iter()
1121 .collect()
1122 );
1123 }
1124
1125 #[test]
1126 fn evaluate_scoped_drops_a_survivor_on_an_exempt_line() {
1127 let report = parse_mutants_report(SAMPLE).unwrap();
1128 let line_scoped = BTreeMap::from([("src/lib.rs".to_string(), BTreeSet::from([7u32]))]);
1129 let kept = evaluate_scoped(
1130 cargo_mutants_survivors(&report),
1131 &mutated_lines(&report),
1132 &[],
1133 &line_scoped,
1134 )
1135 .unwrap();
1136 assert!(
1137 kept.is_empty(),
1138 "the src/lib.rs:7 survivor should be lifted"
1139 );
1140 }
1141
1142 #[test]
1143 fn evaluate_scoped_rejects_exempting_a_caught_line() {
1144 let report = parse_mutants_report(SAMPLE).unwrap();
1146 let line_scoped = BTreeMap::from([("src/other.rs".to_string(), BTreeSet::from([3u32]))]);
1147 let err = evaluate_scoped(
1148 cargo_mutants_survivors(&report),
1149 &mutated_lines(&report),
1150 &[],
1151 &line_scoped,
1152 )
1153 .unwrap_err();
1154 assert!(
1155 err.to_string().contains("all caught") && err.to_string().contains("src/other.rs:3"),
1156 "got: {err}"
1157 );
1158 }
1159
1160 #[test]
1161 fn evaluate_scoped_leaves_an_unmutated_listed_line_alone() {
1162 let report = parse_mutants_report(SAMPLE).unwrap();
1165 let line_scoped = BTreeMap::from([("src/lib.rs".to_string(), BTreeSet::from([99u32]))]);
1166 let kept = evaluate_scoped(
1167 cargo_mutants_survivors(&report),
1168 &mutated_lines(&report),
1169 &[],
1170 &line_scoped,
1171 )
1172 .unwrap();
1173 assert_eq!(kept.len(), 1);
1174 assert_eq!(kept[0].line, 7);
1175 }
1176
1177 #[test]
1178 fn evaluate_scoped_still_honors_a_whole_file_exemption() {
1179 let report = parse_mutants_report(SAMPLE).unwrap();
1180 let kept = evaluate_scoped(
1181 cargo_mutants_survivors(&report),
1182 &mutated_lines(&report),
1183 &["src/lib.rs".to_string()],
1184 &BTreeMap::new(),
1185 )
1186 .unwrap();
1187 assert!(kept.is_empty());
1188 }
1189
1190 fn unique_tmp() -> PathBuf {
1191 static COUNTER: AtomicU64 = AtomicU64::new(0);
1192 let dir = std::env::temp_dir().join(format!(
1193 "tc-provision-test-{}-{}",
1194 std::process::id(),
1195 COUNTER.fetch_add(1, Ordering::Relaxed)
1196 ));
1197 std::fs::create_dir_all(&dir).unwrap();
1198 dir
1199 }
1200
1201 #[test]
1202 fn provision_returns_an_existing_binary_without_installing() {
1203 let tmp = unique_tmp();
1204 let bin = tmp.join("bin").join("cargo-mutants");
1205 let lock = tmp.join(".install.lock");
1206 std::fs::create_dir_all(bin.parent().unwrap()).unwrap();
1207 std::fs::write(&bin, b"binary").unwrap();
1208 let mut installed = false;
1209 let got = provision(&bin, &lock, || {
1210 installed = true;
1211 Ok(())
1212 })
1213 .unwrap();
1214 assert_eq!(got, bin);
1215 assert!(!installed, "a present binary must not be reinstalled");
1216 std::fs::remove_dir_all(&tmp).unwrap();
1217 }
1218
1219 #[test]
1220 fn provision_installs_when_the_binary_is_absent() {
1221 let tmp = unique_tmp();
1222 let bin = tmp.join("bin").join("cargo-mutants");
1223 let lock = tmp.join(".install.lock");
1224 let mut installed = false;
1225 let got = provision(&bin, &lock, || {
1226 installed = true;
1227 std::fs::create_dir_all(bin.parent().unwrap()).unwrap();
1228 std::fs::write(&bin, b"binary").unwrap();
1229 Ok(())
1230 })
1231 .unwrap();
1232 assert!(installed, "an absent binary must be installed");
1233 assert_eq!(got, bin);
1234 std::fs::remove_dir_all(&tmp).unwrap();
1235 }
1236
1237 #[test]
1238 fn provision_errors_when_install_produces_no_binary() {
1239 let tmp = unique_tmp();
1240 let bin = tmp.join("bin").join("cargo-mutants");
1241 let lock = tmp.join(".install.lock");
1242 let err = provision(&bin, &lock, || Ok(())).unwrap_err();
1243 assert!(
1244 err.to_string().contains("cargo-mutants is not at"),
1245 "got: {err}"
1246 );
1247 std::fs::remove_dir_all(&tmp).unwrap();
1248 }
1249
1250 #[test]
1251 fn provision_propagates_an_install_failure() {
1252 let tmp = unique_tmp();
1253 let bin = tmp.join("bin").join("cargo-mutants");
1254 let lock = tmp.join(".install.lock");
1255 let err = provision(&bin, &lock, || bail!("install blew up")).unwrap_err();
1256 assert!(err.to_string().contains("install blew up"), "got: {err}");
1257 std::fs::remove_dir_all(&tmp).unwrap();
1258 }
1259
1260 #[test]
1261 fn provision_does_not_duplicate_the_install_under_concurrent_callers() {
1262 use std::sync::{Arc, Barrier};
1269 use std::thread;
1270 use std::time::Duration;
1271
1272 let tmp = unique_tmp();
1273 let bin = tmp.join("bin").join("cargo-mutants");
1274 let lock = tmp.join(".install.lock");
1275 let install_count = Arc::new(AtomicU64::new(0));
1276 let barrier = Arc::new(Barrier::new(2));
1277
1278 let handles: Vec<_> = (0..2)
1279 .map(|_| {
1280 let bin = bin.clone();
1281 let lock = lock.clone();
1282 let install_count = Arc::clone(&install_count);
1283 let barrier = Arc::clone(&barrier);
1284 thread::spawn(move || {
1285 barrier.wait();
1286 provision(&bin, &lock, || {
1287 install_count.fetch_add(1, Ordering::SeqCst);
1288 thread::sleep(Duration::from_millis(50));
1289 std::fs::create_dir_all(bin.parent().unwrap()).unwrap();
1290 std::fs::write(&bin, b"binary").unwrap();
1291 Ok(())
1292 })
1293 })
1294 })
1295 .collect();
1296
1297 for h in handles {
1298 h.join()
1299 .expect("provisioning thread must not panic")
1300 .unwrap();
1301 }
1302
1303 assert_eq!(
1304 install_count.load(Ordering::SeqCst),
1305 1,
1306 "two concurrent callers on a cold cache must share one install, not each run their own"
1307 );
1308 std::fs::remove_dir_all(&tmp).unwrap();
1309 }
1310
1311 #[test]
1312 fn resolve_cache_base_prefers_xdg_then_home_then_temp() {
1313 let xdg = |s: &str| Some(OsString::from(s));
1314 assert_eq!(
1316 resolve_cache_base(xdg("/xdg"), xdg("/home")),
1317 PathBuf::from("/xdg")
1318 );
1319 assert_eq!(
1321 resolve_cache_base(xdg(""), xdg("/home")),
1322 PathBuf::from("/home/.cache")
1323 );
1324 assert_eq!(
1326 resolve_cache_base(None, xdg("/home")),
1327 PathBuf::from("/home/.cache")
1328 );
1329 assert_eq!(resolve_cache_base(None, None), std::env::temp_dir());
1331 assert_eq!(
1332 resolve_cache_base(xdg(""), Some(OsString::new())),
1333 std::env::temp_dir()
1334 );
1335 }
1336
1337 #[test]
1338 fn cache_root_is_absolute_and_version_scoped() {
1339 let root = cargo_mutants_cache_root();
1340 assert!(
1341 root.ends_with(format!("cargo-mutants-{CARGO_MUTANTS_VERSION}")),
1342 "version-scoped; got {root:?}"
1343 );
1344 assert!(
1345 root.to_string_lossy().contains("testing-conventions"),
1346 "tool-namespaced; got {root:?}"
1347 );
1348 assert!(
1350 root.is_absolute(),
1351 "expected an absolute path; got {root:?}"
1352 );
1353 }
1354
1355 #[test]
1356 fn install_argv_pins_the_version_and_isolates_the_root() {
1357 let argv: Vec<String> = install_argv(Path::new("/cache/cargo-mutants-27"))
1358 .iter()
1359 .map(|arg| arg.to_string_lossy().into_owned())
1360 .collect();
1361 assert_eq!(
1362 argv,
1363 vec![
1364 "install",
1365 "cargo-mutants",
1366 "--locked",
1367 "--version",
1368 CARGO_MUTANTS_VERSION,
1369 "--root",
1370 "/cache/cargo-mutants-27",
1371 ]
1372 );
1373 }
1374
1375 #[cfg(unix)]
1376 fn fake_output(code: i32, stderr: &str) -> Output {
1377 use std::os::unix::process::ExitStatusExt;
1378 Output {
1379 status: std::process::ExitStatus::from_raw(code << 8),
1380 stdout: Vec::new(),
1381 stderr: stderr.as_bytes().to_vec(),
1382 }
1383 }
1384
1385 #[cfg(unix)]
1386 #[test]
1387 fn run_install_succeeds_on_a_zero_exit() {
1388 let mut ran = false;
1389 run_install(Path::new("/cache/root"), |command| {
1390 ran = true;
1391 let argv: Vec<String> = command
1393 .get_args()
1394 .map(|arg| arg.to_string_lossy().into_owned())
1395 .collect();
1396 assert!(argv.contains(&CARGO_MUTANTS_VERSION.to_string()));
1397 Ok(fake_output(0, ""))
1398 })
1399 .unwrap();
1400 assert!(ran);
1401 }
1402
1403 #[cfg(unix)]
1404 #[test]
1405 fn run_install_reports_a_nonzero_exit_with_the_engine_output() {
1406 let err = run_install(Path::new("/cache/root"), |_| {
1407 Ok(fake_output(1, "error: could not compile cargo-mutants"))
1408 })
1409 .unwrap_err();
1410 assert!(
1411 err.to_string()
1412 .contains("failed to provision cargo-mutants")
1413 && err.to_string().contains("could not compile"),
1414 "got: {err}"
1415 );
1416 }
1417
1418 #[cfg(unix)]
1419 #[test]
1420 fn run_install_propagates_a_spawn_failure() {
1421 let err = run_install(Path::new("/cache/root"), |_| {
1422 Err(std::io::Error::new(
1423 std::io::ErrorKind::NotFound,
1424 "no cargo",
1425 ))
1426 })
1427 .unwrap_err();
1428 assert!(
1429 err.to_string().contains("is cargo installed?"),
1430 "got: {err}"
1431 );
1432 }
1433
1434 #[cfg(unix)]
1435 #[test]
1436 fn classify_mutants_exit_accepts_the_caught_and_survivor_exits() {
1437 classify_mutants_exit(Path::new("/crate"), &fake_output(0, "")).unwrap();
1439 classify_mutants_exit(Path::new("/crate"), &fake_output(2, "")).unwrap();
1440 }
1441
1442 #[cfg(unix)]
1443 #[test]
1444 fn classify_mutants_exit_accepts_a_timeout_exit_3() {
1445 classify_mutants_exit(Path::new("/crate"), &fake_output(3, ""))
1449 .expect("a timeout (exit 3) is inconclusive, not fatal");
1450 }
1451
1452 #[cfg(unix)]
1453 #[test]
1454 fn classify_mutants_exit_is_fatal_on_a_baseline_failure() {
1455 let err = classify_mutants_exit(Path::new("/crate"), &fake_output(4, "baseline broke"))
1457 .unwrap_err();
1458 assert!(
1459 err.to_string().contains("did not run cleanly")
1460 && err.to_string().contains("baseline broke"),
1461 "got: {err}"
1462 );
1463 }
1464
1465 #[test]
1466 fn cargo_mutants_bin_name_matches_the_platform() {
1467 let name = cargo_mutants_bin_name();
1468 if cfg!(windows) {
1469 assert_eq!(name, "cargo-mutants.exe");
1470 } else {
1471 assert_eq!(name, "cargo-mutants");
1472 }
1473 }
1474}