Skip to content

Commit

Permalink
Support --list --json --diff
Browse files Browse the repository at this point in the history
Keys within --list --json elements are now sorted.
  • Loading branch information
sourcefrog committed Sep 16, 2023
1 parent afb4c95 commit 51350fe
Show file tree
Hide file tree
Showing 9 changed files with 686 additions and 666 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
- Mutate `(A, B, C, ...)` into the product of all replacements for
`a, b, c, ...`

- The combination of options `--list --diff --json` is now supported, and emits
a `diff` key in the JSON.

## 23.9.0

- Fixed a bug causing an assertion failure when cargo-mutants was run from a
Expand Down
1 change: 0 additions & 1 deletion book/src/list.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@
(The same format is written to `mutants.out/mutants.json` when running tests.)

`--check`: Run `cargo check` on all generated mutants to find out which ones are viable, but don't actually run the tests. (This is primarily useful when debugging cargo-mutants.)

15 changes: 10 additions & 5 deletions src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use std::fmt;
use std::io;

use anyhow::bail;
use camino::Utf8Path;
use serde_json::{json, Value};

Expand Down Expand Up @@ -36,11 +35,17 @@ pub(crate) fn list_mutants<W: fmt::Write>(
) -> Result<()> {
let discovered = walk_tree(tool, source_tree_root, options)?;
if options.emit_json {
if options.emit_diffs {
// TODO: Include diffs in json.
bail!("--list --diff --json is not (yet) supported");
let mut list: Vec<serde_json::Value> = Vec::new();
for mutant in discovered.mutants {
let mut obj = serde_json::to_value(&mutant)?;
if options.emit_diffs {
obj.as_object_mut()
.unwrap()
.insert("diff".to_owned(), json!(mutant.diff()));
}
list.push(obj);
}
out.write_str(&serde_json::to_string_pretty(&discovered.mutants)?)?;
out.write_str(&serde_json::to_string_pretty(&list)?)?;
} else {
for mutant in &discovered.mutants {
if options.colors {
Expand Down
29 changes: 21 additions & 8 deletions tests/cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,28 @@ fn uses_cargo_env_var_to_run_cargo_so_invalid_value_fails() {
}

#[test]
fn list_diff_json_not_yet_supported() {
run()
.args(["mutants", "--list", "--json", "--diff"])
fn list_diff_json_contains_diffs() {
let cmd = run()
.args([
"mutants",
"--list",
"--json",
"--diff",
"-d",
"testdata/tree/factorial",
])
.assert()
.code(1)
.stderr(predicates::str::contains(
"Error: --list --diff --json is not (yet) supported\n",
))
.stdout("");
.success(); // needed for lifetime
let out = cmd.get_output();
assert_eq!(String::from_utf8_lossy(&out.stderr), "");
println!("{}", String::from_utf8_lossy(&out.stdout));
let out_json = serde_json::from_slice::<serde_json::Value>(&out.stdout).unwrap();
let mutants_json = out_json.as_array().expect("json output is array");
assert_eq!(mutants_json.len(), 3);
assert!(mutants_json.iter().all(|e| e.as_object().unwrap()["diff"]
.as_str()
.unwrap()
.contains("--- src/bin/factorial.rs")));
}

/// Return paths to all testdata trees, in order, excluding leftover git
Expand Down
Loading

0 comments on commit 51350fe

Please sign in to comment.