Skip to content

Commit

Permalink
Merge #144
Browse files Browse the repository at this point in the history
144: feat: add cargo nextest support r=taiki-e a=skyzh

Signed-off-by: Alex Chi <iskyzh@gmail.com>

[cargo-nextest](https://github.com/nextest-rs/nextest/) is quite popular recently, but it lacks coverage support. In this PR, I added a new `nextest` command to run tests.

I've tested it locally and it works. On my own project, it shows (nearly) the same coverage as normal `cargo llvm-cov`.

```
cargo llvm-cov nextest
```

Not sure if it is good to simply add a `nextest` command... Maybe it would be better to allow users to run arbitrary command in `cargo llvm-cov <user command>`?

Thanks for reviewing!

Co-authored-by: Alex Chi <iskyzh@gmail.com>
  • Loading branch information
bors[bot] and skyzh authored Mar 18, 2022
2 parents e8ae56d + 7185a10 commit e7dbbde
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ SUBCOMMANDS:
Output the environment set by cargo-llvm-cov to build Rust projects
clean
Remove artifacts that cargo-llvm-cov has generated in the past
nextest
Run tests with cargo nextest
help
Print this message or the help of the given subcommand(s)
```
Expand Down
13 changes: 13 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,19 @@ pub(crate) enum Subcommand {
)]
Clean(CleanOptions),

/// Run tests with cargo nextest
#[clap(
bin_name = "cargo llvm-cov nextest",
max_term_width(MAX_TERM_WIDTH),
setting(AppSettings::DeriveDisplayOrder),
trailing_var_arg = true,
allow_hyphen_values = true
)]
Nextest {
#[clap(multiple_values = true)]
passthrough_options: Vec<String>,
},

// internal (unstable)
#[clap(
bin_name = "cargo llvm-cov demangle",
Expand Down
51 changes: 51 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,37 @@ fn try_main() -> Result<()> {
writer.set("CARGO_LLVM_COV_TARGET_DIR", cx.ws.metadata.target_directory.as_str());
}

Some(Subcommand::Nextest { passthrough_options }) => {
let cx = &context_from_args(
&mut Args::try_parse_from(
[
// fake argv[0] to help clap parse
"nextest".to_string(),
]
.iter()
// real pass-through args
.chain(passthrough_options.iter()),
)?,
false,
)?;

clean::clean_partial(cx)?;
create_dirs(cx)?;
match (args.no_run, cx.cov.no_report) {
(false, false) => {
run_nextest(cx, &args)?;
generate_report(cx)?;
}
(false, true) => {
run_nextest(cx, &args)?;
}
(true, false) => {
generate_report(cx)?;
}
(true, true) => unreachable!(),
}
}

None => {
let cx = &context_from_args(&mut args, false)?;
let tmp = term::warn(); // The following warnings should not be promoted to an error.
Expand Down Expand Up @@ -274,6 +305,26 @@ fn run_test(cx: &Context, args: &Args) -> Result<()> {
Ok(())
}

fn run_nextest(cx: &Context, args: &Args) -> Result<()> {
let mut cargo = cx.cargo();

set_env(cx, &mut cargo);

cargo.arg("nextest").arg("run");

if cx.doctests {
return Err(anyhow::anyhow!("doctest is not supported for nextest"));
}

cargo::test_args(cx, args, &mut cargo);

if term::verbose() {
status!("Running", "{}", cargo);
}
cargo.stdout_to_stderr().run()?;
Ok(())
}

fn run_run(cx: &Context, args: &RunOptions) -> Result<()> {
let mut cargo = cx.cargo();

Expand Down
2 changes: 2 additions & 0 deletions tests/long-help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -212,5 +212,7 @@ SUBCOMMANDS:
Output the environment set by cargo-llvm-cov to build Rust projects
clean
Remove artifacts that cargo-llvm-cov has generated in the past
nextest
Run tests with cargo nextest
help
Print this message or the help of the given subcommand(s)
1 change: 1 addition & 0 deletions tests/short-help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,5 @@ SUBCOMMANDS:
run Run a binary or example and generate coverage report
show-env Output the environment set by cargo-llvm-cov to build Rust projects
clean Remove artifacts that cargo-llvm-cov has generated in the past
nextest Run tests with cargo nextest
help Print this message or the help of the given subcommand(s)

0 comments on commit e7dbbde

Please sign in to comment.