Skip to content

Commit 9138c35

Browse files
committed
Extract logic of all test target filter to all_test_targets
1 parent bee3534 commit 9138c35

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

src/bin/cargo/commands/test.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,12 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
105105
FilterRule::none(),
106106
FilterRule::none(),
107107
);
108-
} else if test_name.is_some() {
109-
if let CompileFilter::Default { .. } = compile_opts.filter {
110-
compile_opts.filter = ops::CompileFilter::new(
111-
LibRule::Default, // compile the library, so the unit tests can be run filtered
112-
FilterRule::none(), // binaries without `test = false` are included by default
113-
FilterRule::All, // compile the tests, so the integration tests can be run filtered
114-
FilterRule::none(), // specify --examples to unit test binaries filtered
115-
FilterRule::none(), // specify --benches to unit test benchmarks filtered
116-
); // also, specify --doc to run doc tests filtered
117-
}
108+
} else if test_name.is_some() && !compile_opts.filter.is_specific() {
109+
// If arg `TESTNAME` is provided, assumed that the user knows what
110+
// exactly they wants to test, so we use `all_test_targets` to
111+
// avoid compiling unnecessary targets such as examples, which are
112+
// included by the logic of default target filter.
113+
compile_opts.filter = ops::CompileFilter::all_test_targets();
118114
}
119115

120116
let ops = ops::TestOptions {

src/cargo/ops/cargo_compile.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,25 @@ impl CompileFilter {
828828
}
829829
}
830830

831+
/// Constructs a filter that includes all test targets.
832+
///
833+
/// Being different from the behavior of [`CompileFilter::Default`], this
834+
/// function only recongnizes test targets, which means cargo might compile
835+
/// all targets with `tested` flag on, whereas [`CompileFilter::Default`]
836+
/// may include additional example targets to ensure they can be compiled.
837+
///
838+
/// Note that the actual behavior is subject to `filter_default_targets`
839+
/// and `generate_targets` though.
840+
pub fn all_test_targets() -> Self {
841+
Self::Only {
842+
all_targets: false,
843+
lib: LibRule::Default,
844+
bins: FilterRule::none(),
845+
examples: FilterRule::none(),
846+
tests: FilterRule::All,
847+
benches: FilterRule::none(),
848+
}
849+
}
831850
pub fn need_dev_deps(&self, mode: CompileMode) -> bool {
832851
match mode {
833852
CompileMode::Test | CompileMode::Doctest | CompileMode::Bench => true,

0 commit comments

Comments
 (0)