use std::{io::Write, path::PathBuf, process::ExitCode};
const ALL: &str = "1000000000";
pub fn command(arguments: &[String]) -> ExitCode {
let mut passed = Vec::new();
let mut output = None;
let mut format = None;
let mut index = 0;
loop {
match arguments[index].as_str() {
"--format" => {
index += 1;
format = arguments.get(index).cloned();
}
"--output" => {
index += 1;
output = arguments.get(index).map(PathBuf::from);
}
"--offset" | "--limit" => index += 1,
"--json" => {}
other => passed.push(other.to_owned()),
}
index += 1;
if index >= arguments.len() {
break;
}
}
let format = match format.as_deref() {
Some(format @ ("jsonl" | "markdown" | "stryker" | "pit")) => format.to_owned(),
_ => {
eprintln!("[supercov] mutants --format takes jsonl, markdown, stryker or pit");
return ExitCode::from(2);
}
};
let markdown = format == "markdown";
let run = passed[0].clone();
let mut mutants_arguments = passed[2..].to_vec();
mutants_arguments.extend(["--json", "--limit", ALL].map(str::to_owned));
let (request, _) =
match super::public_query::coverage_request(run, "mutants", &mutants_arguments, true) {
Ok(request) => request,
Err(error) => {
eprintln!("[supercov] {}", error.error.message);
return ExitCode::from(2);
}
};
let root = match std::env::current_dir() {
Ok(root) => root,
Err(error) => {
eprintln!("[supercov] could not resolve the current directory: {error}");
return ExitCode::from(2);
}
};
let (output_data, _) = match super::execute_coverage_query(&root, &request, None) {
Ok(output) => output,
Err(failure) => {
eprintln!("[supercov] {}", failure.error.message);
return ExitCode::from(2);
}
};
let data = output_data
.data
.mutants()
.expect("a mutants request is answered with mutants");
if let Some(note) = &data.note {
eprintln!("[supercov] {note}");
}
let mut lines = String::new();
if markdown {
lines = self::markdown(&request, data);
} else if format == "stryker" {
lines = stryker(&root, data);
} else if format == "pit" {
lines = supercov_engine::mutation_report::pit_xml(data);
} else {
for mutant in &data.mutants {
lines.push_str(&serde_json::to_string(mutant).expect("a mutant is plain data"));
lines.push('\n');
}
}
let written = match output {
Some(path) => {
if let Err(error) = supercov_engine::fault_fs::write(&path, &lines) {
eprintln!("[supercov] {}: {error}", path.display());
return ExitCode::from(2);
}
if markdown {
eprintln!("[supercov] wrote the mutants' table to {}", path.display());
} else if format != "jsonl" {
eprintln!(
"[supercov] wrote {} mutant(s) as a {format} report to {}",
data.mutants.len(),
path.display()
);
} else {
eprintln!(
"[supercov] wrote {} mutant(s) to {}",
data.mutants.len(),
path.display()
);
}
ExitCode::SUCCESS
}
None => printed(&mut std::io::stdout().lock(), &lines),
};
if written != ExitCode::SUCCESS {
return written;
}
if let Some(stryker) = &data.stryker
&& stryker.soundness_violations.total > 0
{
eprintln!(
"[supercov] {} soundness violation(s): mutants not infected or not reached here that StrykerJS killed",
stryker.soundness_violations.total
);
return ExitCode::from(1);
}
if let Some(gate) = crate::human_query::gate_failure(data) {
eprintln!("[supercov] {gate}");
return ExitCode::from(1);
}
ExitCode::SUCCESS
}
const MARKDOWN_ROWS: usize = 25;
fn cell(text: &str) -> String {
text.split_whitespace()
.collect::<Vec<_>>()
.join(" ")
.replace('|', "\\|")
}
fn code(text: &str) -> String {
let longest = text
.split(|character| character != '`')
.map(str::len)
.max()
.unwrap_or(0);
let fence = "`".repeat(longest + 1);
let padded = if text.starts_with('`') || text.ends_with('`') {
format!(" {text} ")
} else {
text.to_owned()
};
cell(&format!("{fence}{padded}{fence}"))
}
fn stryker(
root: &std::path::Path,
data: &supercov_engine::mutants_query::CoverageMutantsData,
) -> String {
let mut sources = std::collections::BTreeMap::new();
for mutant in &data.mutants {
if !sources.contains_key(&mutant.file)
&& let Ok(text) = supercov_engine::fault_fs::read_to_string(root.join(&mutant.file))
{
sources.insert(mutant.file.clone(), text);
}
}
let (report, missing) = supercov_engine::mutation_report::stryker_report(data, &sources);
for file in missing {
eprintln!("[supercov] {file} could not be read: its mutants are left out of the report");
}
let mut text = serde_json::to_string_pretty(&report).expect("a report is plain data");
text.push('\n');
text
}
pub(crate) fn markdown(
request: &supercov_engine::indexed_query::IndexedQueryRequest,
data: &supercov_engine::mutants_query::CoverageMutantsData,
) -> String {
use supercov_engine::coverage_index::MutantStatus;
use supercov_engine::mutation_report::{self as report, Outcome};
let summary = &data.summary;
let strong = data.mutants.iter().any(|mutant| mutant.strong.is_some());
let asked_verdicts = &request.mutants.strong_statuses;
let asked_statuses = !request.mutants.statuses.is_empty() || !asked_verdicts.is_empty();
let wanted = if asked_statuses {
request.mutants.statuses.clone()
} else {
vec![MutantStatus::NotInfected, MutantStatus::NotReached]
};
let mut rows = data
.mutants
.iter()
.filter(|mutant| {
if !asked_verdicts.is_empty() {
report::verdict(mutant)
.is_some_and(|verdict| asked_verdicts.contains(&verdict.status))
} else if strong && !asked_statuses {
report::alive(mutant)
} else {
wanted.contains(&mutant.status)
}
})
.collect::<Vec<_>>();
rows.sort_by_key(|mutant| {
let verdict = match report::outcome(mutant) {
Outcome::Survived => 0,
Outcome::NoCoverage => 1,
_ => 2,
};
(
verdict,
wanted.iter().position(|status| *status == mutant.status),
)
});
let score = summary
.weak_score
.map_or_else(|| "—".to_owned(), |score| format!("{:.1}%", score * 100.0));
let mut counts = MutantStatus::ALL
.into_iter()
.filter(|status| summary.status.of(*status) > 0)
.map(|status| {
format!(
"{} {}",
summary.status.of(status),
status.name().replace('-', " ")
)
})
.collect::<Vec<_>>()
.join(" · ");
if strong {
let tally = report::Tally::of(&data.mutants);
let verdicts = [
(tally.strong.killed, "killed"),
(tally.strong.timeout, "timed out"),
(tally.strong.survived, "survived"),
(tally.strong.no_coverage, "no coverage"),
(tally.strong.unviable, "unviable"),
(tally.strong.error, "error"),
(tally.strong.not_run, "not run (no driver)"),
(tally.strong.survived_so_far, "survived so far"),
(tally.strong.pending, "no strong verdict"),
]
.into_iter()
.filter(|(count, _)| *count > 0)
.map(|(count, name)| format!("{count} {name}"))
.collect::<Vec<_>>()
.join(" · ");
let strong = &tally.strong;
let basis = strong.killed + strong.timeout + strong.survived + strong.no_coverage;
let mutation = match tally.mutation_score {
Some(score) => format!(
"Mutation score **{:.1}%** (of {basis} of {})",
score * 100.0,
plural(tally.mutants, "mutant")
),
None => {
"No mutation score: no mutant was killed, timed out, survived or left uncovered"
.to_owned()
}
};
counts = format!("{counts}\n\n{mutation} · {verdicts}");
}
let mut text = String::new();
match &data.diff {
Some(diff) => {
text.push_str("### Mutants on this change\n\n");
text.push_str(&format!(
"**{}** on {} of {} changed lines since `{}` (from `{}`): weak score **{score}**{}{counts}\n",
plural(summary.mutants, "mutant"),
diff.lines_with_mutants,
diff.changed_lines,
diff.base,
diff.merge_base.chars().take(12).collect::<String>(),
if counts.is_empty() { "" } else { " · " },
));
text.push_str(&unrun_lines(&diff.unrun_lines));
}
None => {
text.push_str("### Mutants\n\n");
text.push_str(&format!(
"**{}**: weak score **{score}**{}{counts}\n",
plural(summary.mutants, "mutant"),
if counts.is_empty() { "" } else { " · " },
));
}
}
let names = if !asked_verdicts.is_empty() {
asked_verdicts
.iter()
.map(|status| status.replace('-', " "))
.collect::<Vec<_>>()
.join(" or ")
} else if strong && !asked_statuses {
"left alive by the tests".to_owned()
} else {
wanted
.iter()
.map(|status| status.name().replace('-', " "))
.collect::<Vec<_>>()
.join(" or ")
};
let (logged, rows): (Vec<_>, Vec<_>) = rows.into_iter().partition(|mutant| mutant.log_only);
let so_far = data
.mutants
.iter()
.filter(|mutant| report::outcome(mutant) == Outcome::SurvivedSoFar)
.collect::<Vec<_>>();
let weak_status = |mutant: &supercov_engine::mutants_query::MutantEntry| {
format!(
"{}{}",
mutant.status.name().replace('-', " "),
mutant
.weak
.untestable
.map_or_else(String::new, |kind| format!(" ({} boundary)", kind.name()))
)
};
let place = |mutant: &supercov_engine::mutants_query::MutantEntry| {
code(&format!(
"{}:{}:{}",
mutant.file, mutant.span.start[0], mutant.span.start[1]
))
};
let ran = |mutant: &supercov_engine::mutants_query::MutantEntry| {
mutant
.line_tests
.map_or_else(|| "—".to_owned(), |tests| tests.to_string())
};
let table = |rows: &[&supercov_engine::mutants_query::MutantEntry]| {
let mut table = String::new();
if strong {
table.push_str(
"\n| Where | Mutant | Weak | Strong | Tests on its line | Why |\n| --- | --- | --- | --- | --- | --- |\n",
);
table.extend(rows.iter().take(MARKDOWN_ROWS).map(|mutant| {
let outcome = report::outcome(mutant);
format!(
"| {} | {} | {} | {} | {} | {} |\n",
place(mutant),
code(&report::change(mutant)),
cell(&weak_status(mutant)),
cell(match outcome {
Outcome::Pending => "—",
other => other.name(),
}),
ran(mutant),
cell(&report::why(mutant, true)),
)
}));
} else {
table.push_str(
"\n| Where | Mutant | Status | Tests on its line | Why |\n| --- | --- | --- | --- | --- |\n",
);
table.extend(rows.iter().take(MARKDOWN_ROWS).map(|mutant| {
format!(
"| {} | {} | {} | {} | {} |\n",
place(mutant),
code(&report::change(mutant)),
cell(&weak_status(mutant)),
ran(mutant),
cell(&mutant.weak.reason),
)
}));
}
table
};
let folded = |summary: String, rows: &[&supercov_engine::mutants_query::MutantEntry]| {
let mut folded = format!("\n<details><summary>{summary}</summary>\n{}", table(rows));
if rows.len() > MARKDOWN_ROWS {
folded.push_str(&format!("\n_…and {} more_\n", rows.len() - MARKDOWN_ROWS));
}
folded.push_str("\n</details>\n");
folded
};
if rows.is_empty() {
let said = if asked_statuses || strong {
format!("No mutant is {names}.")
} else {
"No mutant is left alive: none is not infected or not reached.".to_owned()
};
text.push_str(&format!(
"\n{said}{}\n",
if logged.is_empty() {
""
} else {
" Those below change only what is logged."
}
));
} else {
text.push_str(&table(&rows));
if rows.len() > MARKDOWN_ROWS {
let mut asked = request.clone();
if !strong {
asked.mutants.statuses = wanted.clone();
}
text.push_str(&format!(
"\n_…and {} more: `{}`_\n",
rows.len() - MARKDOWN_ROWS,
crate::human_query::mutants_command(&asked, data, data.file.as_deref())
));
}
}
if !logged.is_empty() {
text.push_str(&folded(
format!(
"{} more that {} only what is logged: wholly in a logging call's arguments, or in what makes a logger",
logged.len(),
if logged.len() == 1 { "changes" } else { "change" },
),
&logged,
));
}
if !so_far.is_empty() {
text.push_str(&folded(
format!(
"{} survived so far: each lived through a first tier of the tests that ran it, and the rest have not run with it yet; not survivors, and not in the score",
plural(so_far.len(), "mutant"),
),
&so_far,
));
}
text
}
fn unrun_lines(unrun: &std::collections::BTreeMap<String, Vec<u32>>) -> String {
if unrun.is_empty() {
return String::new();
}
let total = unrun.values().map(Vec::len).sum::<usize>();
let mut text = format!(
"\n**{} no test ran**, which no mutant on them can tell apart:\n\n",
plural(total, "changed line")
);
unrun.iter().take(MARKDOWN_ROWS).for_each(|(file, lines)| {
let runs = lines
.iter()
.fold(Vec::<(u32, u32)>::new(), |mut runs, line| {
match runs.last_mut() {
Some((_, end)) if *end + 1 == *line => *end = *line,
_ => runs.push((*line, *line)),
}
runs
})
.into_iter()
.map(|(start, end)| {
if start == end {
start.to_string()
} else {
format!("{start}–{end}")
}
})
.collect::<Vec<_>>()
.join(", ");
text.push_str(&format!("- {}: {runs}\n", code(file)));
});
if unrun.len() > MARKDOWN_ROWS {
text.push_str(&format!(
"- _…and {}_\n",
plural(unrun.len() - MARKDOWN_ROWS, "more file")
));
}
text
}
fn plural(count: usize, noun: &str) -> String {
format!("{count} {noun}{}", if count == 1 { "" } else { "s" })
}
fn printed(out: &mut dyn Write, lines: &str) -> ExitCode {
if let Err(error) = out.write_all(lines.as_bytes()).and_then(|()| out.flush()) {
if error.kind() != std::io::ErrorKind::BrokenPipe {
eprintln!("[supercov] could not write the mutants to standard output: {error}");
}
return ExitCode::from(2);
}
ExitCode::SUCCESS
}
#[cfg(test)]
mod tests {
use super::*;
struct Failing(std::io::ErrorKind);
impl Write for Failing {
fn write(&mut self, _: &[u8]) -> std::io::Result<usize> {
Err(self.0.into())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
fn request() -> supercov_engine::indexed_query::IndexedQueryRequest {
supercov_engine::indexed_query::IndexedQueryRequest {
run_id: "run_1".into(),
filter: "all".into(),
command: "coverage.mutants".into(),
metric: supercov_engine::coverage_query::MinimizeMetric::All,
kind: None,
runner: None,
file: None,
line: None,
more_lines: Vec::new(),
selector: None,
sort: None,
valid: None,
test_exit_code: None,
stale: None,
stale_reasons: None,
offset: 0,
limit: 20,
target: None,
max_states: None,
group: None,
depth: None,
gained_only: false,
tags: Vec::new(),
titles: Vec::new(),
test_files: Vec::new(),
mutants: Default::default(),
}
}
fn data(
mutants: Vec<supercov_engine::mutants_query::MutantEntry>,
) -> supercov_engine::mutants_query::CoverageMutantsData {
use supercov_engine::coverage_index::MutantCounts;
use supercov_engine::mutants_query::{CoverageMutantsData, MutantLevels, MutantsSummary};
let mut counts = MutantCounts::default();
mutants.iter().for_each(|mutant| counts.add(mutant.status));
CoverageMutantsData {
run: "run_1".into(),
mutant_set: Some("stryker-js@10.1.1".into()),
levels: MutantLevels {
weak: true,
strong: true,
},
file: None,
line: None,
summary: MutantsSummary::of(counts, 0),
note: None,
mutants,
stryker: None,
files: Vec::new(),
diff: None,
fail_on: None,
}
}
fn mutant(
line: u32,
status: supercov_engine::coverage_index::MutantStatus,
strong: Option<serde_json::Value>,
) -> supercov_engine::mutants_query::MutantEntry {
use supercov_engine::mutants_query::{MutantEntry, MutantSpan, WeakResult};
MutantEntry {
log_only: false,
line_tests: None,
id: format!("src/a.py:{line}:3-{line}:9:ArithmeticOperator:-"),
file: "src/a.py".into(),
span: MutantSpan {
start: [line, 3],
end: [line, 9],
},
mutator: "ArithmeticOperator".into(),
original: "a + b".into(),
replacement: "a - b".into(),
changed: None,
status,
weak: WeakResult {
reach: "reached",
infection: Some(status.name()),
code: "c".into(),
reason: "weak reason".into(),
proxy: false,
rule: None,
untestable: None,
untestable_reason: None,
},
strong,
stryker: None,
}
}
#[test]
fn a_strong_pass_s_survivors_are_the_table_with_their_verdicts() {
use serde_json::json;
use supercov_engine::coverage_index::MutantStatus::*;
let mut mutants = (1..=28)
.map(|line| {
mutant(
line,
Infected,
Some(json!({"status": "survived", "tests": 2})),
)
})
.collect::<Vec<_>>();
mutants.push(mutant(
29,
NotReached,
Some(json!({"status": "no-coverage"})),
));
mutants.push(mutant(30, NotInfected, None));
mutants.push(mutant(
31,
Infected,
Some(json!({"status": "killed", "killedBy": "t"})),
));
mutants.push(mutant(32, Infected, Some(json!({"status": "timeout"}))));
mutants.push(mutant(33, Infected, Some(json!({"status": "unviable"}))));
mutants.push(mutant(34, Infected, Some(json!({"status": "error"}))));
mutants.push(mutant(
35,
Infected,
Some(json!({"status": "error", "reason": "no strong mutation driver for python yet"})),
));
mutants.push(mutant(36, Infected, None));
let text = markdown(&request(), &data(mutants.clone()));
assert!(
text.contains("\n\nMutation score **6.5%** (of 31 of 36 mutants) · 1 killed · 1 timed out · 28 survived · 1 no coverage · 1 unviable · 1 error · 1 not run (no driver) · 2 no strong verdict\n"),
"{text}"
);
assert!(
text.contains("| Where | Mutant | Weak | Strong | Tests on its line | Why |"),
"{text}"
);
assert!(
text.contains("| `src/a.py:1:3` | `a + b → a - b` | infected | survived | — | every test that reached it passed with it in place (2 tests ran) |"),
"{text}"
);
assert_eq!(text.matches("| `src/a.py:").count(), 25, "{text}");
assert!(
text.contains("_…and 5 more: `supercov runs 'run_1' mutants`_"),
"{text}"
);
let mut asked = request();
asked.mutants.statuses = vec![NotInfected];
let text = markdown(&asked, &data(mutants.clone()));
assert!(
text.contains(
"| `src/a.py:30:3` | `a + b → a - b` | not infected | — | — | weak reason |"
),
"{text}"
);
assert_eq!(text.matches("| `src/a.py:").count(), 1, "{text}");
let mut survived = request();
survived.mutants.strong_statuses = vec!["survived".into()];
let text = markdown(&survived, &data(mutants.clone()));
assert_eq!(text.matches(" | survived | ").count(), 25, "{text}");
assert!(!text.contains("| no-coverage |"), "{text}");
let mut none = request();
none.mutants.strong_statuses = vec!["no-coverage".into(), "killed".into()];
let text = markdown(
&none,
&data(vec![mutant(
1,
Infected,
Some(json!({"status": "survived"})),
)]),
);
assert!(
text.ends_with("\nNo mutant is no coverage or killed.\n"),
"{text}"
);
let text = markdown(
&request(),
&data(vec![mutant(1, Infected, Some(json!({"status": "killed"})))]),
);
assert!(
text.contains("Mutation score **100.0%** (of 1 of 1 mutant) · 1 killed\n"),
"{text}"
);
assert!(
text.ends_with("\nNo mutant is left alive by the tests.\n"),
"{text}"
);
let text = markdown(
&request(),
&data(vec![mutant(1, Infected, Some(json!({"status": "error"})))]),
);
assert!(text.contains("No mutation score: no mutant was killed, timed out, survived or left uncovered · 1 error\n"), "{text}");
}
#[test]
fn a_comment_folds_log_lines_and_open_mutants_and_names_unrun_lines() {
use serde_json::json;
use supercov_engine::coverage_index::MutantStatus::*;
let survivor = |line: u32| {
mutant(
line,
Infected,
Some(json!({"status": "survived", "tests": 3})),
)
};
let mut ran = survivor(1);
ran.line_tests = Some(7);
let mut mutants = vec![ran];
for line in 2..=28 {
let mut logged = survivor(line);
logged.log_only = true;
mutants.push(logged);
}
mutants.push(mutant(
40,
Infected,
Some(json!({"status": "survived-so-far", "tests": 32})),
));
let mut changed = data(mutants);
let unrun = (0..27)
.map(|file| (format!("src/f{file:02}.ts"), vec![3, 4, 5, 9]))
.collect();
changed.diff = Some(supercov_engine::mutants_query::DiffSummary {
base: "origin/main".into(),
merge_base: "0123456789abcdef".into(),
commit: None,
changed_lines: 120,
lines_with_mutants: 29,
unrun_lines: unrun,
});
let text = markdown(&request(), &changed);
for expected in [
" · 28 survived · 1 survived so far\n",
"\n**108 changed lines no test ran**, which no mutant on them can tell apart:\n\n- `src/f00.ts`: 3–5, 9\n",
"- `src/f24.ts`: 3–5, 9\n- _…and 2 more files_\n",
"| `src/a.py:1:3` | `a + b → a - b` | infected | survived | 7 | every test that reached it passed with it in place (3 tests ran) |\n",
"\n<details><summary>27 more that change only what is logged: wholly in a logging call's arguments, or in what makes a logger</summary>\n",
"\n_…and 2 more_\n\n</details>\n",
"<details><summary>1 mutant survived so far: each lived through a first tier of the tests that ran it, and the rest have not run with it yet; not survivors, and not in the score</summary>\n",
"| `src/a.py:40:3` | `a + b → a - b` | infected | survived-so-far | — | survived so far: 32 tests passed with it in place, and the rest of its candidates have not run with it yet |\n\n</details>\n",
] {
assert!(text.contains(expected), "{expected} in {text}");
}
assert!(!text.contains("src/f25.ts"), "{text}");
let mut logged = survivor(1);
logged.log_only = true;
let text = markdown(&request(), &data(vec![logged]));
assert!(
text.contains("\nNo mutant is left alive by the tests. Those below change only what is logged.\n\n<details><summary>1 more that changes only what is logged"),
"{text}"
);
let mut weak = mutant(2, NotInfected, None);
weak.line_tests = Some(0);
let text = markdown(&request(), &data(vec![weak]));
assert!(
text.contains("| Where | Mutant | Status | Tests on its line | Why |\n| --- | --- | --- | --- | --- |\n| `src/a.py:2:3` | `a + b → a - b` | not infected | 0 | weak reason |"),
"{text}"
);
assert_eq!(unrun_lines(&Default::default()), "");
assert_eq!(
unrun_lines(&std::collections::BTreeMap::from([(
"src/a.ts".to_owned(),
vec![7]
)])),
"\n**1 changed line no test ran**, which no mutant on them can tell apart:\n\n- `src/a.ts`: 7\n"
);
let killed = data(vec![mutant(3, Infected, None)]);
let text = markdown(&request(), &killed);
assert!(
text.ends_with("\nNo mutant is left alive: none is not infected or not reached.\n"),
"{text}"
);
let mut asked = request();
asked.mutants.statuses = vec![NotReached];
let text = markdown(&asked, &killed);
assert!(text.ends_with("\nNo mutant is not reached.\n"), "{text}");
}
#[test]
fn mutants_that_cannot_be_printed_end_the_export_with_two() {
for kind in [
std::io::ErrorKind::BrokenPipe,
std::io::ErrorKind::StorageFull,
] {
assert_eq!(printed(&mut Failing(kind), "{}\n"), ExitCode::from(2));
}
let mut written = Vec::new();
assert_eq!(printed(&mut written, "{}\n"), ExitCode::SUCCESS);
assert_eq!(written, b"{}\n");
}
}