Skip to content

Commit

Permalink
Support --target option for nextest-archive
Browse files Browse the repository at this point in the history
Fixes #334
  • Loading branch information
taiki-e committed Jan 13, 2024
1 parent 1f46722 commit e056438
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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");
Expand Down
38 changes: 23 additions & 15 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -447,7 +447,7 @@ impl Args {
Subcommand::None
| Subcommand::Test
| Subcommand::Run
| Subcommand::Nextest
| Subcommand::Nextest { .. }
| Subcommand::NextestArchive
) =>
{
Expand All @@ -464,7 +464,7 @@ impl Args {
Subcommand::None
| Subcommand::Test
| Subcommand::Run
| Subcommand::Nextest
| Subcommand::Nextest { .. }
| Subcommand::NextestArchive
) =>
{
Expand All @@ -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`
Expand All @@ -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 => {}
Expand All @@ -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)?;
Expand Down Expand Up @@ -584,7 +590,7 @@ impl Args {
Subcommand::None
| Subcommand::Test
| Subcommand::Run
| Subcommand::Nextest
| Subcommand::Nextest { .. }
| Subcommand::NextestArchive => {}
_ => {
if !bin.is_empty() {
Expand All @@ -611,7 +617,7 @@ impl Args {
Subcommand::None
| Subcommand::Test
| Subcommand::Run
| Subcommand::Nextest
| Subcommand::Nextest { .. }
| Subcommand::NextestArchive
| Subcommand::ShowEnv => {}
_ => {
Expand All @@ -626,7 +632,7 @@ impl Args {
match subcommand {
Subcommand::None
| Subcommand::Test
| Subcommand::Nextest
| Subcommand::Nextest { .. }
| Subcommand::NextestArchive
| Subcommand::Clean => {}
_ => {
Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand All @@ -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
}
Expand All @@ -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)
}
}

Expand All @@ -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}"),
Expand Down
6 changes: 2 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down Expand Up @@ -721,9 +721,7 @@ fn object_files(cx: &Context) -> Result<Vec<OsString>> {
// 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
Expand Down

0 comments on commit e056438

Please sign in to comment.