]> git.proxmox.com Git - cargo.git/commitdiff
Extract logic of lib target only filter to `lib_only`
authorWeihang Lo <me@weihanglo.tw>
Thu, 20 Jan 2022 03:33:11 +0000 (11:33 +0800)
committerWeihang Lo <me@weihanglo.tw>
Thu, 20 Jan 2022 05:02:05 +0000 (13:02 +0800)
src/bin/cargo/commands/test.rs
src/cargo/ops/cargo_compile.rs

index cb35ab5aeebe2a9dff19dbad2cf19cbae3174923..a02bdb57443622d59e1bef37a731bb7f74b749ba 100644 (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
index 11ff38b6a1b8a3ac8d3801a48249abf0fe13415e..699a6be3960c52a3099b629876d91720864cfe5b 100644 (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,