1use std::collections::{BTreeMap, BTreeSet};
19use std::path::{Path, PathBuf};
20use std::process::Command;
21use std::sync::atomic::{AtomicU64, Ordering};
22
23use anyhow::{bail, Context, Result};
24use serde::Deserialize;
25
26#[derive(Debug, Clone, PartialEq, Eq)]
28pub struct Survivor {
29 pub file: String,
31 pub line: u32,
33 pub description: String,
35}
36
37pub type MutatedLines = BTreeSet<(String, u32)>;
41
42type RunOutcome = (Vec<Survivor>, MutatedLines);
45
46#[derive(Debug, Clone, Deserialize)]
49pub struct MutantsReport {
50 pub outcomes: Vec<MutantOutcome>,
51}
52
53#[derive(Debug, Clone, Deserialize)]
57pub struct MutantOutcome {
58 pub summary: String,
59 pub scenario: Scenario,
60}
61
62#[derive(Debug, Clone, Deserialize)]
65pub enum Scenario {
66 Baseline,
67 Mutant(MutantInfo),
68}
69
70#[derive(Debug, Clone, Deserialize)]
74pub struct MutantInfo {
75 pub file: String,
76 pub span: Span,
77 pub name: String,
78}
79
80#[derive(Debug, Clone, Deserialize)]
82pub struct Span {
83 pub start: LineCol,
84}
85
86#[derive(Debug, Clone, Deserialize)]
88pub struct LineCol {
89 pub line: u32,
90}
91
92pub fn parse_mutants_report(json: &str) -> Result<MutantsReport> {
94 serde_json::from_str(json).context("parsing cargo-mutants outcomes.json")
95}
96
97pub fn unexplained_survivors(report: &MutantsReport, exempt: &[String]) -> Vec<Survivor> {
106 evaluate(cargo_mutants_survivors(report), exempt)
107}
108
109fn cargo_mutants_survivors(report: &MutantsReport) -> Vec<Survivor> {
113 report
114 .outcomes
115 .iter()
116 .filter_map(|outcome| {
117 if outcome.summary != "MissedMutant" {
118 return None;
119 }
120 let Scenario::Mutant(mutant) = &outcome.scenario else {
121 return None;
122 };
123 Some(Survivor {
124 file: mutant.file.clone(),
125 line: mutant.span.start.line,
126 description: mutant.name.clone(),
127 })
128 })
129 .collect()
130}
131
132pub fn mutated_lines(report: &MutantsReport) -> MutatedLines {
138 report
139 .outcomes
140 .iter()
141 .filter_map(|outcome| {
142 if outcome.summary != "CaughtMutant" && outcome.summary != "MissedMutant" {
143 return None;
144 }
145 let Scenario::Mutant(mutant) = &outcome.scenario else {
146 return None;
147 };
148 Some((mutant.file.clone(), mutant.span.start.line))
149 })
150 .collect()
151}
152
153pub fn evaluate(survivors: Vec<Survivor>, exempt: &[String]) -> Vec<Survivor> {
158 survivors
159 .into_iter()
160 .filter(|survivor| !exempt.iter().any(|path| path == &survivor.file))
161 .collect()
162}
163
164pub fn evaluate_scoped(
176 survivors: Vec<Survivor>,
177 mutated: &MutatedLines,
178 whole_file: &[String],
179 line_scoped: &BTreeMap<String, BTreeSet<u32>>,
180) -> Result<Vec<Survivor>> {
181 let mut over: Vec<String> = Vec::new();
182 for (file, lines) in line_scoped {
183 for &line in lines {
184 let has_survivor = survivors
185 .iter()
186 .any(|survivor| survivor.file == *file && survivor.line == line);
187 if has_survivor {
188 continue;
189 }
190 if mutated.contains(&(file.clone(), line)) {
191 over.push(format!("\n {file}:{line}"));
192 }
193 }
194 }
195 if !over.is_empty() {
196 bail!(
197 "a line-scoped mutation exemption may only list a line with a surviving mutant, but \
198 these had mutants that were all caught:{}",
199 over.concat()
200 );
201 }
202 Ok(survivors
203 .into_iter()
204 .filter(|survivor| {
205 let whole = whole_file.iter().any(|path| path == &survivor.file);
206 let line = line_scoped
207 .get(&survivor.file)
208 .is_some_and(|lines| lines.contains(&survivor.line));
209 !(whole || line)
210 })
211 .collect())
212}
213
214pub fn measure_rust(
221 root: &Path,
222 exempt: &[String],
223 exempt_lines: &BTreeMap<String, BTreeSet<u32>>,
224 base: Option<&str>,
225) -> Result<Vec<Survivor>> {
226 let out = MutantsOut::new();
227 let diff = match base {
228 Some(base) => match write_base_diff(root, base, &out)? {
231 None => return Ok(Vec::new()),
232 Some(path) => Some(path),
233 },
234 None => None,
235 };
236 run_cargo_mutants(root, &out.0, diff.as_deref())?;
237 let outcomes = out.0.join("mutants.out").join("outcomes.json");
238 let json = match std::fs::read_to_string(&outcomes) {
242 Ok(json) => json,
243 Err(_) => return Ok(Vec::new()),
244 };
245 let report = parse_mutants_report(&json)?;
246 evaluate_scoped(
247 cargo_mutants_survivors(&report),
248 &mutated_lines(&report),
249 exempt,
250 exempt_lines,
251 )
252}
253
254#[derive(Debug, Clone, Deserialize)]
258pub struct StrykerReport {
259 pub files: BTreeMap<String, StrykerFile>,
261}
262
263#[derive(Debug, Clone, Deserialize)]
265pub struct StrykerFile {
266 #[serde(default)]
267 pub mutants: Vec<StrykerMutant>,
268}
269
270#[derive(Debug, Clone, Deserialize)]
273#[serde(rename_all = "camelCase")]
274pub struct StrykerMutant {
275 pub mutator_name: String,
276 #[serde(default)]
277 pub replacement: Option<String>,
278 pub status: String,
279 pub location: StrykerLocation,
280}
281
282#[derive(Debug, Clone, Deserialize)]
285pub struct StrykerLocation {
286 pub start: LineCol,
287}
288
289pub fn parse_stryker_report(json: &str) -> Result<StrykerReport> {
291 serde_json::from_str(json).context("parsing Stryker mutation.json")
292}
293
294pub fn stryker_survivors(report: &StrykerReport) -> Vec<Survivor> {
301 let mut survivors = Vec::new();
302 for (file, contents) in &report.files {
303 for mutant in &contents.mutants {
304 if mutant.status != "Survived" && mutant.status != "NoCoverage" {
305 continue;
306 }
307 let description = match &mutant.replacement {
308 Some(replacement) => {
309 format!("{} (-> {})", mutant.mutator_name, one_line(replacement))
310 }
311 None => mutant.mutator_name.clone(),
312 };
313 survivors.push(Survivor {
314 file: file.clone(),
315 line: mutant.location.start.line,
316 description,
317 });
318 }
319 }
320 survivors
321}
322
323fn one_line(replacement: &str) -> String {
326 let flat = replacement.split_whitespace().collect::<Vec<_>>().join(" ");
327 const MAX: usize = 60;
328 if flat.chars().count() > MAX {
329 format!("{}…", flat.chars().take(MAX).collect::<String>())
330 } else {
331 flat
332 }
333}
334
335pub fn measure_typescript(
345 root: &Path,
346 exempt: &[String],
347 exempt_lines: &BTreeMap<String, BTreeSet<u32>>,
348 base: Option<&str>,
349) -> Result<Vec<Survivor>> {
350 let mutate = match base {
351 Some(base) => {
352 let ranges = mutate_ranges(root, base)?;
353 if ranges.is_empty() {
355 return Ok(Vec::new());
356 }
357 Some(ranges)
358 }
359 None => None,
360 };
361 let json = run_stryker(root, mutate.as_deref())?;
362 let report = parse_stryker_report(&json)?;
363 evaluate_scoped(
364 stryker_survivors(&report),
365 &stryker_mutated_lines(&report),
366 exempt,
367 exempt_lines,
368 )
369}
370
371fn stryker_mutated_lines(report: &StrykerReport) -> MutatedLines {
376 let mut mutated = BTreeSet::new();
377 for (file, contents) in &report.files {
378 for mutant in &contents.mutants {
379 if matches!(
380 mutant.status.as_str(),
381 "Killed" | "Survived" | "NoCoverage" | "Timeout"
382 ) {
383 mutated.insert((file.clone(), mutant.location.start.line));
384 }
385 }
386 }
387 mutated
388}
389
390fn mutate_ranges(root: &Path, base: &str) -> Result<Vec<String>> {
396 let changed = crate::patch_coverage::changed_lines(root, base)?;
397 let mut specs = Vec::new();
398 for (file, lines) in changed {
399 if !is_mutatable_ts(&file) {
400 continue;
401 }
402 for (start, end) in contiguous_runs(&lines) {
403 specs.push(format!("{file}:{start}-{end}"));
404 }
405 }
406 Ok(specs)
407}
408
409fn is_mutatable_ts(file: &str) -> bool {
413 let is_source = [".ts", ".tsx", ".mts", ".cts", ".js", ".jsx", ".mjs", ".cjs"]
414 .iter()
415 .any(|ext| file.ends_with(ext));
416 let is_decl = file.ends_with(".d.ts");
417 let is_test = file.contains(".test.") || file.contains(".spec.");
418 is_source && !is_decl && !is_test
419}
420
421fn contiguous_runs(lines: &BTreeSet<u64>) -> Vec<(u64, u64)> {
423 let mut runs: Vec<(u64, u64)> = Vec::new();
424 for &line in lines {
425 match runs.last_mut() {
426 Some(run) if run.1 + 1 == line => run.1 = line,
427 _ => runs.push((line, line)),
428 }
429 }
430 runs
431}
432
433fn run_stryker(root: &Path, mutate: Option<&[String]>) -> Result<String> {
444 let report_path = root.join("reports").join("mutation").join("mutation.json");
445 let _cleanup = ReportCleanup(report_path.clone());
446 let _ = std::fs::remove_file(&report_path);
448
449 let mut command = Command::new("npx");
450 command
451 .current_dir(root)
452 .args(["--yes", "stryker", "run", "--reporters", "json"]);
453 if let Some(specs) = mutate {
454 command.arg("--mutate").arg(specs.join(","));
455 }
456 let output = command
457 .env("CI", "1")
458 .output()
459 .context("running `npx stryker run` (is @stryker-mutator/core installed?)")?;
460
461 std::fs::read_to_string(&report_path).map_err(|_| {
462 anyhow::anyhow!(
463 "Stryker produced no report in `{}` (did it run cleanly?):\n{}{}",
464 root.display(),
465 String::from_utf8_lossy(&output.stdout),
466 String::from_utf8_lossy(&output.stderr),
467 )
468 })
469}
470
471struct ReportCleanup(PathBuf);
474
475impl Drop for ReportCleanup {
476 fn drop(&mut self) {
477 let _ = std::fs::remove_file(&self.0);
478 if let Some(mutation_dir) = self.0.parent() {
479 let _ = std::fs::remove_dir(mutation_dir);
481 if let Some(reports_dir) = mutation_dir.parent() {
482 let _ = std::fs::remove_dir(reports_dir);
483 }
484 }
485 }
486}
487
488#[derive(Debug, Clone, Deserialize)]
491pub struct CosmicRayLine(pub CrWorkItem, pub Option<CrResult>);
492
493#[derive(Debug, Clone, Deserialize)]
496pub struct CrWorkItem {
497 pub mutations: Vec<CrMutation>,
498}
499
500#[derive(Debug, Clone, Deserialize)]
503pub struct CrMutation {
504 pub module_path: String,
505 pub operator_name: String,
506 pub start_pos: (u32, u32),
508 #[serde(default)]
509 pub definition_name: Option<String>,
510}
511
512#[derive(Debug, Clone, Deserialize)]
515pub struct CrResult {
516 #[serde(default)]
517 pub test_outcome: Option<String>,
518}
519
520pub fn parse_cosmic_ray_dump(dump: &str) -> Result<Vec<Survivor>> {
528 let mut survivors = Vec::new();
529 for line in dump.lines() {
530 if line.trim().is_empty() {
531 continue;
532 }
533 let CosmicRayLine(item, result) =
534 serde_json::from_str(line).context("parsing a cosmic-ray dump line")?;
535 let survived = matches!(result, Some(CrResult { test_outcome: Some(outcome) }) if outcome == "survived");
536 if !survived {
537 continue;
538 }
539 let Some(mutation) = item.mutations.first() else {
540 continue;
541 };
542 let definition = mutation.definition_name.as_deref().unwrap_or("<module>");
543 survivors.push(Survivor {
544 file: mutation.module_path.clone(),
545 line: mutation.start_pos.0,
546 description: format!("{} in {}", mutation.operator_name, definition),
547 });
548 }
549 Ok(survivors)
550}
551
552pub fn measure_python(
563 root: &Path,
564 exempt: &[String],
565 exempt_lines: &BTreeMap<String, BTreeSet<u32>>,
566 base: Option<&str>,
567) -> Result<Vec<Survivor>> {
568 let (survivors, mutated) = match base {
569 None => run_cosmic_ray(root, ".", &PY_TEST_EXCLUDES)?,
570 Some(base) => {
571 let changed = crate::patch_coverage::changed_lines(root, base)?;
572 let mut all_survivors = Vec::new();
573 let mut all_mutated = BTreeSet::new();
574 for (file, lines) in &changed {
575 if !is_mutatable_py(file) {
576 continue;
577 }
578 let (survivors, mutated) = run_cosmic_ray(root, file, &[])?;
580 for survivor in survivors {
581 if lines.contains(&(survivor.line as u64)) {
582 all_survivors.push(survivor);
583 }
584 }
585 for (mutated_file, line) in mutated {
586 if lines.contains(&u64::from(line)) {
587 all_mutated.insert((mutated_file, line));
588 }
589 }
590 }
591 (all_survivors, all_mutated)
592 }
593 };
594 evaluate_scoped(survivors, &mutated, exempt, exempt_lines)
595}
596
597const PY_TEST_EXCLUDES: [&str; 3] = ["*_test.py", "test_*.py", "conftest.py"];
600
601fn is_mutatable_py(file: &str) -> bool {
604 if !file.ends_with(".py") {
605 return false;
606 }
607 let base = file.rsplit('/').next().unwrap_or(file);
608 !(base.ends_with("_test.py") || base.starts_with("test_") || base == "conftest.py")
609}
610
611fn run_cosmic_ray(root: &Path, module_path: &str, excluded_modules: &[&str]) -> Result<RunOutcome> {
618 let dir = CosmicRayDir::new();
619 std::fs::create_dir_all(&dir.0).context("creating the cosmic-ray temp dir")?;
620 let config = dir.0.join("cr.toml");
621 let session = dir.0.join("session.sqlite");
622
623 let excludes = excluded_modules
624 .iter()
625 .map(|glob| format!("\"{glob}\""))
626 .collect::<Vec<_>>()
627 .join(", ");
628 std::fs::write(
629 &config,
630 format!(
631 "[cosmic-ray]\n\
632 module-path = \"{module_path}\"\n\
633 timeout = 30.0\n\
634 excluded-modules = [{excludes}]\n\
635 test-command = \"python3 -m pytest -q -p no:cacheprovider\"\n\
636 \n\
637 [cosmic-ray.distributor]\n\
638 name = \"local\"\n"
639 ),
640 )
641 .context("writing the cosmic-ray config")?;
642
643 let baseline = cosmic_ray(root, &["baseline", path_str(&config)])?;
646 if !baseline.status.success() {
647 bail!(
648 "the Python unit suite did not pass unmutated in `{}` (cosmic-ray baseline failed):\n{}{}",
649 root.display(),
650 String::from_utf8_lossy(&baseline.stdout),
651 String::from_utf8_lossy(&baseline.stderr),
652 );
653 }
654
655 let init = cosmic_ray(root, &["init", path_str(&config), path_str(&session)])?;
656 if !init.status.success() {
657 bail!(
658 "cosmic-ray init failed in `{}`:\n{}{}",
659 root.display(),
660 String::from_utf8_lossy(&init.stdout),
661 String::from_utf8_lossy(&init.stderr),
662 );
663 }
664 let exec = cosmic_ray(root, &["exec", path_str(&config), path_str(&session)])?;
665 if !exec.status.success() {
666 bail!(
667 "cosmic-ray exec failed in `{}`:\n{}{}",
668 root.display(),
669 String::from_utf8_lossy(&exec.stdout),
670 String::from_utf8_lossy(&exec.stderr),
671 );
672 }
673 let dump = cosmic_ray(root, &["dump", path_str(&session)])?;
674 if !dump.status.success() {
675 bail!(
676 "cosmic-ray dump failed in `{}`:\n{}",
677 root.display(),
678 String::from_utf8_lossy(&dump.stderr),
679 );
680 }
681 let stdout = String::from_utf8_lossy(&dump.stdout);
682 Ok((
683 parse_cosmic_ray_dump(&stdout)?,
684 cosmic_ray_mutated_lines(&stdout)?,
685 ))
686}
687
688pub fn cosmic_ray_mutated_lines(dump: &str) -> Result<MutatedLines> {
693 let mut mutated = BTreeSet::new();
694 for line in dump.lines() {
695 if line.trim().is_empty() {
696 continue;
697 }
698 let CosmicRayLine(item, result) =
699 serde_json::from_str(line).context("parsing a cosmic-ray dump line")?;
700 let outcome = result.and_then(|result| result.test_outcome);
701 if !matches!(outcome.as_deref(), Some("survived") | Some("killed")) {
702 continue;
703 }
704 if let Some(mutation) = item.mutations.first() {
705 mutated.insert((mutation.module_path.clone(), mutation.start_pos.0));
706 }
707 }
708 Ok(mutated)
709}
710
711fn cosmic_ray(root: &Path, args: &[&str]) -> Result<std::process::Output> {
714 Command::new("cosmic-ray")
715 .current_dir(root)
716 .args(args)
717 .env("PYTHONDONTWRITEBYTECODE", "1")
718 .output()
719 .context("running `cosmic-ray` (is it installed?)")
720}
721
722fn path_str(path: &Path) -> &str {
723 path.to_str().expect("temp path is valid UTF-8")
724}
725
726struct CosmicRayDir(PathBuf);
729
730impl CosmicRayDir {
731 fn new() -> Self {
732 static COUNTER: AtomicU64 = AtomicU64::new(0);
733 let name = format!(
734 "testing-conventions-cosmic-ray-{}-{}",
735 std::process::id(),
736 COUNTER.fetch_add(1, Ordering::Relaxed),
737 );
738 CosmicRayDir(std::env::temp_dir().join(name))
739 }
740}
741
742impl Drop for CosmicRayDir {
743 fn drop(&mut self) {
744 let _ = std::fs::remove_dir_all(&self.0);
745 }
746}
747
748struct MutantsOut(PathBuf);
751
752impl MutantsOut {
753 fn new() -> Self {
754 static COUNTER: AtomicU64 = AtomicU64::new(0);
755 let name = format!(
756 "testing-conventions-mutants-{}-{}",
757 std::process::id(),
758 COUNTER.fetch_add(1, Ordering::Relaxed),
759 );
760 MutantsOut(std::env::temp_dir().join(name))
761 }
762}
763
764impl Drop for MutantsOut {
765 fn drop(&mut self) {
766 let _ = std::fs::remove_dir_all(&self.0);
767 }
768}
769
770fn write_base_diff(root: &Path, base: &str, out: &MutantsOut) -> Result<Option<PathBuf>> {
779 let range = format!("{base}...HEAD");
780 let output = Command::new("git")
781 .current_dir(root)
782 .args(["diff", "--relative", &range])
783 .output()
784 .context("running `git diff` for `--base` (is git installed?)")?;
785 if !output.status.success() {
786 bail!(
787 "git diff {range} failed: {}",
788 String::from_utf8_lossy(&output.stderr)
789 );
790 }
791 if output.stdout.is_empty() {
792 return Ok(None);
793 }
794 std::fs::create_dir_all(&out.0).context("creating the mutants output dir")?;
795 let path = out.0.join("base.diff");
796 std::fs::write(&path, &output.stdout).context("writing the base diff")?;
797 Ok(Some(path))
798}
799
800fn run_cargo_mutants(root: &Path, out: &Path, in_diff: Option<&Path>) -> Result<()> {
809 let mut command = Command::new("cargo");
810 command
811 .current_dir(root)
812 .arg("mutants")
813 .arg("--output")
814 .arg(out);
815 if let Some(diff) = in_diff {
816 command.arg("--in-diff").arg(diff);
817 }
818 for var in [
819 "RUSTFLAGS",
820 "CARGO_ENCODED_RUSTFLAGS",
821 "RUSTDOCFLAGS",
822 "CARGO_ENCODED_RUSTDOCFLAGS",
823 "LLVM_PROFILE_FILE",
824 "CARGO_LLVM_COV",
825 "CARGO_LLVM_COV_SHOW_ENV",
826 "CARGO_LLVM_COV_TARGET_DIR",
827 "CARGO_LLVM_COV_BUILD_DIR",
828 "RUSTC_WRAPPER",
829 "RUSTC_WORKSPACE_WRAPPER",
830 "__CARGO_LLVM_COV_RUSTC_WRAPPER",
831 "__CARGO_LLVM_COV_RUSTC_WRAPPER_RUSTFLAGS",
832 "__CARGO_LLVM_COV_RUSTC_WRAPPER_CRATE_NAMES",
833 ] {
834 command.env_remove(var);
835 }
836 let output = command
837 .output()
838 .context("running `cargo mutants` (is cargo-mutants installed?)")?;
839 match output.status.code() {
840 Some(0) | Some(2) => Ok(()),
842 _ => bail!(
843 "cargo-mutants did not run cleanly in `{}` (baseline build/test failure?):\n{}{}",
844 root.display(),
845 String::from_utf8_lossy(&output.stdout),
846 String::from_utf8_lossy(&output.stderr),
847 ),
848 }
849}
850
851#[cfg(test)]
852mod tests {
853 use super::*;
854
855 const SAMPLE: &str = r#"{
858 "outcomes": [
859 {"scenario": "Baseline", "summary": "Success",
860 "phase_results": []},
861 {"scenario": {"Mutant": {"file": "src/lib.rs", "package": "p", "genre": "FnValue",
862 "replacement": "true", "name": "src/lib.rs:7:7: replace > with == in is_positive",
863 "function": {"function_name": "is_positive"},
864 "span": {"start": {"line": 7, "column": 7}, "end": {"line": 7, "column": 8}}}},
865 "summary": "MissedMutant"},
866 {"scenario": {"Mutant": {"file": "src/other.rs", "package": "p", "genre": "FnValue",
867 "replacement": "0", "name": "src/other.rs:3:5: replace add -> i32 with 0",
868 "span": {"start": {"line": 3, "column": 5}, "end": {"line": 3, "column": 9}}}},
869 "summary": "CaughtMutant"}
870 ],
871 "total_mutants": 2
872 }"#;
873
874 #[test]
875 fn parses_the_outcomes_export() {
876 let report = parse_mutants_report(SAMPLE).expect("valid outcomes.json");
877 assert_eq!(report.outcomes.len(), 3);
878 assert!(matches!(report.outcomes[0].scenario, Scenario::Baseline));
879 }
880
881 #[test]
882 fn collects_only_missed_mutants_as_survivors() {
883 let report = parse_mutants_report(SAMPLE).unwrap();
884 let survivors = unexplained_survivors(&report, &[]);
885 assert_eq!(survivors.len(), 1);
887 assert_eq!(survivors[0].file, "src/lib.rs");
888 assert_eq!(survivors[0].line, 7);
889 assert!(survivors[0].description.contains("replace > with =="));
890 }
891
892 #[test]
893 fn an_exemption_drops_a_survivor_in_that_file() {
894 let report = parse_mutants_report(SAMPLE).unwrap();
895 let exempt = vec!["src/lib.rs".to_string()];
896 assert!(unexplained_survivors(&report, &exempt).is_empty());
897 }
898
899 #[test]
900 fn an_exemption_on_another_file_leaves_the_survivor() {
901 let report = parse_mutants_report(SAMPLE).unwrap();
902 let exempt = vec!["src/elsewhere.rs".to_string()];
903 assert_eq!(unexplained_survivors(&report, &exempt).len(), 1);
904 }
905
906 const STRYKER_SAMPLE: &str = r#"{
909 "schemaVersion": "1.0",
910 "files": {
911 "src/index.ts": {
912 "language": "typescript",
913 "source": "...",
914 "mutants": [
915 {"id": "0", "mutatorName": "ConditionalExpression", "replacement": "true",
916 "status": "Survived", "coveredBy": ["t0"],
917 "location": {"start": {"line": 2, "column": 10}, "end": {"line": 2, "column": 15}}},
918 {"id": "1", "mutatorName": "ArithmeticOperator", "replacement": "a - b",
919 "status": "NoCoverage",
920 "location": {"start": {"line": 5, "column": 3}, "end": {"line": 5, "column": 8}}},
921 {"id": "2", "mutatorName": "BooleanLiteral", "replacement": "false",
922 "status": "Killed",
923 "location": {"start": {"line": 9, "column": 1}, "end": {"line": 9, "column": 6}}}
924 ]
925 }
926 }
927 }"#;
928
929 #[test]
930 fn parses_a_stryker_report() {
931 let report = parse_stryker_report(STRYKER_SAMPLE).expect("valid mutation.json");
932 assert_eq!(report.files["src/index.ts"].mutants.len(), 3);
933 }
934
935 #[test]
936 fn collects_survived_and_nocoverage_as_survivors() {
937 let report = parse_stryker_report(STRYKER_SAMPLE).unwrap();
938 let survivors = stryker_survivors(&report);
939 assert_eq!(survivors.len(), 2);
941 assert!(survivors.iter().all(|s| s.file == "src/index.ts"));
942 assert_eq!(survivors[0].line, 2);
943 assert!(survivors[0].description.contains("ConditionalExpression"));
944 assert!(survivors[0].description.contains("true"));
945 assert_eq!(survivors[1].line, 5);
946 }
947
948 #[test]
949 fn evaluate_drops_exempt_files_for_either_engine() {
950 let report = parse_stryker_report(STRYKER_SAMPLE).unwrap();
951 let survivors = stryker_survivors(&report);
952 let exempt = vec!["src/index.ts".to_string()];
953 assert!(evaluate(survivors, &exempt).is_empty());
954 }
955
956 #[test]
957 fn is_mutatable_ts_keeps_sources_and_drops_tests_and_decls() {
958 assert!(is_mutatable_ts("src/index.ts"));
959 assert!(is_mutatable_ts("src/util.tsx"));
960 assert!(is_mutatable_ts("src/util.js"));
961 assert!(!is_mutatable_ts("src/index.test.ts"));
962 assert!(!is_mutatable_ts("src/index.spec.ts"));
963 assert!(!is_mutatable_ts("src/types.d.ts"));
964 assert!(!is_mutatable_ts("README.md"));
965 }
966
967 #[test]
968 fn contiguous_runs_collapses_adjacent_lines() {
969 let lines: BTreeSet<u64> = [2u64, 3, 4, 7, 9, 10].into_iter().collect();
970 assert_eq!(contiguous_runs(&lines), vec![(2, 4), (7, 7), (9, 10)]);
971 assert!(contiguous_runs(&BTreeSet::new()).is_empty());
972 }
973
974 #[test]
975 fn one_line_flattens_and_caps() {
976 assert_eq!(one_line("a -\n b"), "a - b");
977 let long = "x".repeat(80);
978 let capped = one_line(&long);
979 assert!(capped.chars().count() <= 61 && capped.ends_with('…'));
980 }
981
982 const COSMIC_RAY_DUMP: &str = concat!(
985 r#"[{"job_id":"a","mutations":[{"module_path":"calc.py","operator_name":"core/ReplaceComparisonOperator_Gt_NotEq","occurrence":0,"start_pos":[6,11],"end_pos":[6,12],"operator_args":{},"definition_name":"is_positive"}]},{"worker_outcome":"normal","test_outcome":"survived"}]"#,
986 "\n",
987 r#"[{"job_id":"b","mutations":[{"module_path":"calc.py","operator_name":"core/ReplaceBinaryOperator_Add_Div","occurrence":0,"start_pos":[2,13],"end_pos":[2,14],"operator_args":{},"definition_name":"add"}]},{"worker_outcome":"normal","test_outcome":"killed"}]"#,
988 "\n",
989 );
990
991 #[test]
992 fn collects_only_survived_cosmic_ray_mutants() {
993 let survivors = parse_cosmic_ray_dump(COSMIC_RAY_DUMP).expect("valid dump");
994 assert_eq!(survivors.len(), 1);
996 assert_eq!(survivors[0].file, "calc.py");
997 assert_eq!(survivors[0].line, 6);
998 assert!(survivors[0]
999 .description
1000 .contains("ReplaceComparisonOperator"));
1001 assert!(survivors[0].description.contains("is_positive"));
1002 }
1003
1004 #[test]
1005 fn an_unexecuted_cosmic_ray_item_is_not_a_survivor() {
1006 let dump = r#"[{"mutations":[{"module_path":"calc.py","operator_name":"core/NumberReplacer","start_pos":[3,5],"end_pos":[3,6]}]},null]"#;
1008 assert!(parse_cosmic_ray_dump(dump).unwrap().is_empty());
1009 }
1010
1011 #[test]
1012 fn is_mutatable_py_keeps_sources_and_drops_tests() {
1013 assert!(is_mutatable_py("calc.py"));
1014 assert!(is_mutatable_py("pkg/util.py"));
1015 assert!(!is_mutatable_py("calc_test.py"));
1016 assert!(!is_mutatable_py("test_calc.py"));
1017 assert!(!is_mutatable_py("pkg/conftest.py"));
1018 assert!(!is_mutatable_py("README.md"));
1019 }
1020
1021 #[test]
1024 fn mutated_lines_collects_caught_and_missed() {
1025 let report = parse_mutants_report(SAMPLE).unwrap();
1028 assert_eq!(
1029 mutated_lines(&report),
1030 [
1031 ("src/lib.rs".to_string(), 7),
1032 ("src/other.rs".to_string(), 3)
1033 ]
1034 .into_iter()
1035 .collect()
1036 );
1037 }
1038
1039 #[test]
1040 fn evaluate_scoped_drops_a_survivor_on_an_exempt_line() {
1041 let report = parse_mutants_report(SAMPLE).unwrap();
1042 let line_scoped = BTreeMap::from([("src/lib.rs".to_string(), BTreeSet::from([7u32]))]);
1043 let kept = evaluate_scoped(
1044 cargo_mutants_survivors(&report),
1045 &mutated_lines(&report),
1046 &[],
1047 &line_scoped,
1048 )
1049 .unwrap();
1050 assert!(
1051 kept.is_empty(),
1052 "the src/lib.rs:7 survivor should be lifted"
1053 );
1054 }
1055
1056 #[test]
1057 fn evaluate_scoped_rejects_exempting_a_caught_line() {
1058 let report = parse_mutants_report(SAMPLE).unwrap();
1060 let line_scoped = BTreeMap::from([("src/other.rs".to_string(), BTreeSet::from([3u32]))]);
1061 let err = evaluate_scoped(
1062 cargo_mutants_survivors(&report),
1063 &mutated_lines(&report),
1064 &[],
1065 &line_scoped,
1066 )
1067 .unwrap_err();
1068 assert!(
1069 err.to_string().contains("all caught") && err.to_string().contains("src/other.rs:3"),
1070 "got: {err}"
1071 );
1072 }
1073
1074 #[test]
1075 fn evaluate_scoped_leaves_an_unmutated_listed_line_alone() {
1076 let report = parse_mutants_report(SAMPLE).unwrap();
1079 let line_scoped = BTreeMap::from([("src/lib.rs".to_string(), BTreeSet::from([99u32]))]);
1080 let kept = evaluate_scoped(
1081 cargo_mutants_survivors(&report),
1082 &mutated_lines(&report),
1083 &[],
1084 &line_scoped,
1085 )
1086 .unwrap();
1087 assert_eq!(kept.len(), 1);
1088 assert_eq!(kept[0].line, 7);
1089 }
1090
1091 #[test]
1092 fn evaluate_scoped_still_honors_a_whole_file_exemption() {
1093 let report = parse_mutants_report(SAMPLE).unwrap();
1094 let kept = evaluate_scoped(
1095 cargo_mutants_survivors(&report),
1096 &mutated_lines(&report),
1097 &["src/lib.rs".to_string()],
1098 &BTreeMap::new(),
1099 )
1100 .unwrap();
1101 assert!(kept.is_empty());
1102 }
1103
1104 #[test]
1105 fn stryker_mutated_lines_collects_every_viable_mutant() {
1106 let report = parse_stryker_report(STRYKER_SAMPLE).unwrap();
1108 assert_eq!(
1109 stryker_mutated_lines(&report),
1110 [
1111 ("src/index.ts".to_string(), 2),
1112 ("src/index.ts".to_string(), 5),
1113 ("src/index.ts".to_string(), 9),
1114 ]
1115 .into_iter()
1116 .collect()
1117 );
1118 }
1119
1120 #[test]
1121 fn cosmic_ray_mutated_lines_collects_executed_mutants() {
1122 let mutated = cosmic_ray_mutated_lines(COSMIC_RAY_DUMP).unwrap();
1124 assert_eq!(
1125 mutated,
1126 [("calc.py".to_string(), 2), ("calc.py".to_string(), 6)]
1127 .into_iter()
1128 .collect()
1129 );
1130 }
1131}