Skip to content

Commit

Permalink
Add --completions
Browse files Browse the repository at this point in the history
  • Loading branch information
sourcefrog committed Sep 20, 2022
1 parent bf829e2 commit 845ec84
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ tracing-subscriber = "0.3"
walkdir = "2.3"
whoami = "1.2"
clap = { version = "3.2.17", features = ["derive"] }
clap_complete = "3.2.5"

[dependencies.cp_r]
version = "0.5.1"
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## UNRELEASED

- New: `cargo mutants --completions` to generate shell completions using `clap_complete`.

- Changed: Since `cargo-mutants` now sets `RUSTFLAGS` to avoid false failures from warnings, it is unlikely to match the existing build products in the source directory `target/`, and in fact building there is just likely to cause rebuilds in the source. So, `cargo mutants` now never builds in the source directory, and never copies `target/`. The behavior now is as if `--no-copy-target` was always passed. That option is still accepted, but it has no effect.

- Changed: Find mutants before doing a baseline test, so that you will find out earlier if there's nothing to test.
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ the tests really check the code's behavior.
cargo install cargo-mutants
```

To install shell completions run for example:

```sh
cargo mutants --completions fish >~/.config/fish/completions/cargo-mutants.fish
```

```sh

## Using cargo-mutants

Just run `cargo mutants` in a Rust source directory, and it will point out
Expand Down
8 changes: 8 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ use std::time::Duration;
use anyhow::Result;
use camino::Utf8Path;
use camino::Utf8PathBuf;
use clap::CommandFactory;
use clap::Parser;
use clap_complete::{generate, Shell};
use path_slash::PathExt;
use serde_json::json;
use serde_json::Value;
Expand Down Expand Up @@ -79,6 +81,10 @@ struct Args {
#[clap(long)]
check: bool,

/// generate autocompletions for the given shell.
#[clap(long)]
completions: Option<Shell>,

/// show the mutation diffs.
#[clap(long)]
diff: bool,
Expand Down Expand Up @@ -174,6 +180,8 @@ fn main() -> Result<()> {
interrupt::install_handler();
if args.version {
println!("{} {}", NAME, VERSION);
} else if let Some(shell) = args.completions {
generate(shell, &mut Cargo::command(), "cargo", &mut io::stdout());
} else if args.list_files {
if args.json {
list_files_as_json(&source_tree, &options)?;
Expand Down
19 changes: 19 additions & 0 deletions tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1172,3 +1172,22 @@ fn check_text_list_output(dir: &Path, test_name: &str) {
insta::assert_snapshot!(format!("{test_name}__{name}.txt"), content);
}
}

/// `cargo mutants --completions SHELL` produces a shell script for some
/// well-known shells.
///
/// We won't check the content but let's just make sure that it succeeds
/// and produces some non-empty output.
#[test]
fn completions_option_generates_something() {
for shell in ["bash", "fish", "zsh", "powershell"] {
println!("completions for {shell}");
run_assert_cmd()
.arg("mutants")
.arg("--completions")
.arg(shell)
.assert()
.success()
.stdout(predicate::str::is_empty().not());
}
}

0 comments on commit 845ec84

Please sign in to comment.