From 5aa985c42a7a746fb954bb2ba0e659f58ee29fdb Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Thu, 20 Jan 2022 11:33:11 +0800 Subject: [PATCH] Extract logic of lib target only filter to `lib_only` --- src/bin/cargo/commands/test.rs | 26 ++++++++------------------ src/cargo/ops/cargo_compile.rs | 13 +++++++++++++ 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/bin/cargo/commands/test.rs b/src/bin/cargo/commands/test.rs index cb35ab5ae..a02bdb574 100644 --- a/src/bin/cargo/commands/test.rs +++ b/src/bin/cargo/commands/test.rs @@ -1,6 +1,6 @@ use crate::command_prelude::*; use anyhow::Error; -use cargo::ops::{self, CompileFilter, FilterRule, LibRule}; +use cargo::ops; pub fn cli() -> App { subcommand("test") @@ -77,7 +77,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult { // `TESTNAME` is actually an argument of the test binary, but it's // important, so we explicitly mention it and reconfigure. - let test_name: Option<&str> = args.value_of("TESTNAME"); + let test_name = args.value_of("TESTNAME"); let test_args = args.value_of("TESTNAME").into_iter(); let test_args = test_args.chain(args.values_of("args").unwrap_or_default()); let test_args = test_args.collect::>(); @@ -85,26 +85,16 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult { let no_run = args.is_present("no-run"); let doc = args.is_present("doc"); if doc { - if let CompileFilter::Only { .. } = compile_opts.filter { - return Err(CliError::new( - anyhow::format_err!("Can't mix --doc with other target selecting options"), - 101, - )); + if compile_opts.filter.is_specific() { + return Err( + anyhow::format_err!("Can't mix --doc with other target selecting options").into(), + ); } if no_run { - return Err(CliError::new( - anyhow::format_err!("Can't skip running doc tests with --no-run"), - 101, - )); + return Err(anyhow::format_err!("Can't skip running doc tests with --no-run").into()); } compile_opts.build_config.mode = CompileMode::Doctest; - compile_opts.filter = ops::CompileFilter::new( - LibRule::True, - FilterRule::none(), - FilterRule::none(), - FilterRule::none(), - FilterRule::none(), - ); + compile_opts.filter = ops::CompileFilter::lib_only(); } else if test_name.is_some() && !compile_opts.filter.is_specific() { // If arg `TESTNAME` is provided, assumed that the user knows what // exactly they wants to test, so we use `all_test_targets` to diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index 11ff38b6a..699a6be39 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -847,6 +847,19 @@ impl CompileFilter { benches: FilterRule::none(), } } + + /// Constructs a filter that includes lib target only. + pub fn lib_only() -> Self { + Self::Only { + all_targets: false, + lib: LibRule::True, + bins: FilterRule::none(), + examples: FilterRule::none(), + tests: FilterRule::none(), + benches: FilterRule::none(), + } + } + pub fn need_dev_deps(&self, mode: CompileMode) -> bool { match mode { CompileMode::Test | CompileMode::Doctest | CompileMode::Bench => true,