Extract logic of lib target only filter to lib_only

This commit is contained in:
Weihang Lo 2022-01-20 11:33:11 +08:00
parent 9138c35d8c
commit 5aa985c42a
No known key found for this signature in database
GPG Key ID: D7DBF189825E82E7
2 changed files with 21 additions and 18 deletions

View File

@ -1,6 +1,6 @@
use crate::command_prelude::*; use crate::command_prelude::*;
use anyhow::Error; use anyhow::Error;
use cargo::ops::{self, CompileFilter, FilterRule, LibRule}; use cargo::ops;
pub fn cli() -> App { pub fn cli() -> App {
subcommand("test") 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 // `TESTNAME` is actually an argument of the test binary, but it's
// important, so we explicitly mention it and reconfigure. // 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 = args.value_of("TESTNAME").into_iter();
let test_args = test_args.chain(args.values_of("args").unwrap_or_default()); let test_args = test_args.chain(args.values_of("args").unwrap_or_default());
let test_args = test_args.collect::<Vec<_>>(); let test_args = test_args.collect::<Vec<_>>();
@ -85,26 +85,16 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
let no_run = args.is_present("no-run"); let no_run = args.is_present("no-run");
let doc = args.is_present("doc"); let doc = args.is_present("doc");
if doc { if doc {
if let CompileFilter::Only { .. } = compile_opts.filter { if compile_opts.filter.is_specific() {
return Err(CliError::new( return Err(
anyhow::format_err!("Can't mix --doc with other target selecting options"), anyhow::format_err!("Can't mix --doc with other target selecting options").into(),
101, );
));
} }
if no_run { if no_run {
return Err(CliError::new( return Err(anyhow::format_err!("Can't skip running doc tests with --no-run").into());
anyhow::format_err!("Can't skip running doc tests with --no-run"),
101,
));
} }
compile_opts.build_config.mode = CompileMode::Doctest; compile_opts.build_config.mode = CompileMode::Doctest;
compile_opts.filter = ops::CompileFilter::new( compile_opts.filter = ops::CompileFilter::lib_only();
LibRule::True,
FilterRule::none(),
FilterRule::none(),
FilterRule::none(),
FilterRule::none(),
);
} else if test_name.is_some() && !compile_opts.filter.is_specific() { } else if test_name.is_some() && !compile_opts.filter.is_specific() {
// If arg `TESTNAME` is provided, assumed that the user knows what // If arg `TESTNAME` is provided, assumed that the user knows what
// exactly they wants to test, so we use `all_test_targets` to // exactly they wants to test, so we use `all_test_targets` to

View File

@ -847,6 +847,19 @@ impl CompileFilter {
benches: FilterRule::none(), 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 { pub fn need_dev_deps(&self, mode: CompileMode) -> bool {
match mode { match mode {
CompileMode::Test | CompileMode::Doctest | CompileMode::Bench => true, CompileMode::Test | CompileMode::Doctest | CompileMode::Bench => true,