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 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::<Vec<_>>();
@ -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

View File

@ -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,