From e056438cd6d98b333d33ddd1f4e2a4acce67fdec Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Sat, 13 Jan 2024 14:33:18 +0900 Subject: [PATCH] Support --target option for nextest-archive Fixes #334 --- .github/workflows/ci.yml | 5 +++++ src/cargo.rs | 10 +++++++--- src/cli.rs | 38 +++++++++++++++++++++++--------------- src/main.rs | 6 ++---- 4 files changed, 37 insertions(+), 22 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7ca58ebe..1fa1555b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -85,6 +85,11 @@ jobs: cd ../real1 cargo llvm-cov nextest-archive --archive-file a.tar.zst cargo llvm-cov nextest --archive-file a.tar.zst --text --fail-under-lines 70 + rm a.tar.zst + cargo clean + host=$(rustc -Vv | grep host | sed 's/host: //') + cargo llvm-cov nextest-archive --archive-file a.tar.zst --target "${host}" + cargo llvm-cov nextest --archive-file a.tar.zst --text --fail-under-lines 70 --target "${host}" working-directory: tests/fixtures/crates/bin_crate - run: | set -eEuxo pipefail diff --git a/src/cargo.rs b/src/cargo.rs index 9c9b4208..45c8a104 100644 --- a/src/cargo.rs +++ b/src/cargo.rs @@ -215,6 +215,12 @@ pub(crate) fn test_or_run_args(cx: &Context, cmd: &mut ProcessBuilder) { cmd.arg("--exclude"); cmd.arg(exclude); } + if !matches!(cx.args.subcommand, Subcommand::Nextest { archive_file: true }) { + if let Some(target) = &cx.args.target { + cmd.arg("--target"); + cmd.arg(target); + } + } cmd.arg("--manifest-path"); cmd.arg(&cx.ws.current_manifest); @@ -265,9 +271,7 @@ pub(crate) fn clean_args(cx: &Context, cmd: &mut ProcessBuilder) { // https://github.com/taiki-e/cargo-llvm-cov/issues/265 fn add_target_dir(args: &Args, cmd: &mut ProcessBuilder, target_dir: &Utf8Path) { - if args.subcommand == Subcommand::Nextest - && args.cargo_args.contains(&"--archive-file".to_string()) - { + if matches!(args.subcommand, Subcommand::Nextest { archive_file: true }) { cmd.arg("--extract-to"); } else { cmd.arg("--target-dir"); diff --git a/src/cli.rs b/src/cli.rs index 9e0add36..1f25c678 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -374,7 +374,7 @@ impl Args { Long("cargo-profile") if subcommand.is_nextest_based() => { parse_opt_passthrough!(profile); } - Long("target") => parse_opt_passthrough!(target), + Long("target") => parse_opt!(target), Long("coverage-target-only") => parse_flag!(coverage_target_only), Long("remap-path-prefix") => parse_flag!(remap_path_prefix), Long("include-ffi") => parse_flag!(include_ffi), @@ -447,7 +447,7 @@ impl Args { Subcommand::None | Subcommand::Test | Subcommand::Run - | Subcommand::Nextest + | Subcommand::Nextest { .. } | Subcommand::NextestArchive ) => { @@ -464,7 +464,7 @@ impl Args { Subcommand::None | Subcommand::Test | Subcommand::Run - | Subcommand::Nextest + | Subcommand::Nextest { .. } | Subcommand::NextestArchive ) => { @@ -489,7 +489,7 @@ impl Args { after_subcommand = true; } else { if after_subcommand - && subcommand == Subcommand::Nextest + && matches!(subcommand, Subcommand::Nextest { .. }) && matches!( val.as_str(), // from `cargo nextest --help` @@ -514,6 +514,12 @@ impl Args { term::set_coloring(&mut color); + if matches!(subcommand, Subcommand::Nextest { .. }) { + subcommand = Subcommand::Nextest { + archive_file: cargo_args.iter().any(|a| a == "--archive-file"), + }; + } + // unexpected options match subcommand { Subcommand::ShowEnv => {} @@ -528,14 +534,14 @@ impl Args { match subcommand { Subcommand::None | Subcommand::Test => {} Subcommand::ShowEnv | Subcommand::Report if doctests => {} - Subcommand::Nextest | Subcommand::NextestArchive => { + Subcommand::Nextest { .. } | Subcommand::NextestArchive => { bail!("doctest is not supported for nextest") } _ => unexpected(flag, subcommand)?, } } match subcommand { - Subcommand::None | Subcommand::Nextest | Subcommand::NextestArchive => {} + Subcommand::None | Subcommand::Nextest { .. } | Subcommand::NextestArchive => {} Subcommand::Test => { if no_run { unexpected("--no-run", subcommand)?; @@ -584,7 +590,7 @@ impl Args { Subcommand::None | Subcommand::Test | Subcommand::Run - | Subcommand::Nextest + | Subcommand::Nextest { .. } | Subcommand::NextestArchive => {} _ => { if !bin.is_empty() { @@ -611,7 +617,7 @@ impl Args { Subcommand::None | Subcommand::Test | Subcommand::Run - | Subcommand::Nextest + | Subcommand::Nextest { .. } | Subcommand::NextestArchive | Subcommand::ShowEnv => {} _ => { @@ -626,7 +632,7 @@ impl Args { match subcommand { Subcommand::None | Subcommand::Test - | Subcommand::Nextest + | Subcommand::Nextest { .. } | Subcommand::NextestArchive | Subcommand::Clean => {} _ => { @@ -917,7 +923,9 @@ pub(crate) enum Subcommand { ShowEnv, /// Run tests with cargo nextest - Nextest, + Nextest { + archive_file: bool, + }, /// Build and archive tests with cargo nextest NextestArchive, @@ -938,7 +946,7 @@ static CARGO_LLVM_COV_NEXTEST_ARCHIVE_USAGE: &str = impl Subcommand { fn can_passthrough(subcommand: Self) -> bool { - matches!(subcommand, Self::Test | Self::Run | Self::Nextest | Self::NextestArchive) + matches!(subcommand, Self::Test | Self::Run | Self::Nextest { .. } | Self::NextestArchive) } fn help_text(subcommand: Self) -> &'static str { @@ -949,7 +957,7 @@ impl Subcommand { Self::Report => CARGO_LLVM_COV_REPORT_USAGE, Self::Clean => CARGO_LLVM_COV_CLEAN_USAGE, Self::ShowEnv => CARGO_LLVM_COV_SHOW_ENV_USAGE, - Self::Nextest => CARGO_LLVM_COV_NEXTEST_USAGE, + Self::Nextest { .. } => CARGO_LLVM_COV_NEXTEST_USAGE, Self::NextestArchive => CARGO_LLVM_COV_NEXTEST_ARCHIVE_USAGE, Self::Demangle => "", // internal API } @@ -963,14 +971,14 @@ impl Subcommand { Self::Report => "report", Self::Clean => "clean", Self::ShowEnv => "show-env", - Self::Nextest => "nextest", + Self::Nextest { .. } => "nextest", Self::NextestArchive => "nextest-archive", Self::Demangle => "demangle", } } pub(crate) fn is_nextest_based(self) -> bool { - matches!(self, Self::Nextest | Self::NextestArchive) + matches!(self, Self::Nextest { .. } | Self::NextestArchive) } } @@ -984,7 +992,7 @@ impl FromStr for Subcommand { "report" => Ok(Self::Report), "clean" => Ok(Self::Clean), "show-env" => Ok(Self::ShowEnv), - "nextest" => Ok(Self::Nextest), + "nextest" => Ok(Self::Nextest { archive_file: false }), "nextest-archive" => Ok(Self::NextestArchive), "demangle" => Ok(Self::Demangle), _ => bail!("unrecognized subcommand {s}"), diff --git a/src/main.rs b/src/main.rs index 4d9de6d0..973e3bd1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -90,7 +90,7 @@ fn try_main() -> Result<()> { generate_report(cx)?; } } - Subcommand::Nextest => { + Subcommand::Nextest { .. } => { let cx = &Context::new(args)?; clean::clean_partial(cx)?; create_dirs(cx)?; @@ -721,9 +721,7 @@ fn object_files(cx: &Context) -> Result> { // This is not the ideal way, but the way unstable book says it is cannot support them. // https://doc.rust-lang.org/nightly/rustc/instrument-coverage.html#tips-for-listing-the-binaries-automatically let mut target_dir = cx.ws.target_dir.clone(); - if cx.args.subcommand == Subcommand::Nextest - && cx.args.cargo_args.iter().any(|a| a == "--archive-file") - { + if matches!(cx.args.subcommand, Subcommand::Nextest { archive_file: true }) { target_dir.push("target"); } // https://doc.rust-lang.org/nightly/cargo/guide/build-cache.html