diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index 1f8df9626aa..2adc96ea5d2 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -593,21 +593,8 @@ fn generate_targets<'a>( requires_features: !required_features_filterable, mode: build_config.mode, })); - if build_config.mode == CompileMode::Test { - if let Some(t) = pkg - .targets() - .iter() - .find(|t| t.is_lib() && t.doctested() && t.doctestable()) - { - proposals.push(Proposal { - pkg, - target: t, - requires_features: false, - mode: CompileMode::Doctest, - }); - } - } } + proposals.extend(list_doc_test_targets(packages, build_config)); } CompileFilter::Only { all_targets, @@ -696,6 +683,9 @@ fn generate_targets<'a>( bench_filter, bench_mode, )?); + if all_targets { + proposals.extend(list_doc_test_targets(packages, build_config)) + } } } @@ -787,6 +777,30 @@ fn filter_default_targets(targets: &[Target], mode: CompileMode) -> Vec<&Target> } } +fn list_doc_test_targets<'a>( + packages: &[&'a Package], + build_config: &BuildConfig, +) -> Vec> { + let mut proposals = Vec::new(); + if build_config.mode == CompileMode::Test { + for pkg in packages { + if let Some(t) = pkg + .targets() + .iter() + .find(|t| t.is_lib() && t.doctested() && t.doctestable()) + { + proposals.push(Proposal { + pkg, + target: t, + requires_features: false, + mode: CompileMode::Doctest, + }); + } + } + } + proposals +} + /// Returns a list of proposed targets based on command-line target selection flags. fn list_rule_targets<'a>( packages: &[&'a Package], diff --git a/tests/testsuite/test.rs b/tests/testsuite/test.rs index 4d0d96f7c6a..76513aa3b22 100644 --- a/tests/testsuite/test.rs +++ b/tests/testsuite/test.rs @@ -3469,6 +3469,46 @@ pub fn foo() -> u8 { 1 } .run(); } +#[test] +fn all_targets_includes_doc_tests() { + let p = project() + .file( + "src/lib.rs", + " +/// ``` +/// assert_eq!(1, 1) +/// ``` +pub fn foo() -> u8 { 1 } +", + ) + .build(); + + p.cargo("test --all-targets") + .with_stderr( + "\ +[COMPILING] foo v0.0.1 ([CWD]) +[FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +[RUNNING] target/debug/deps/foo-[..] +[DOCTEST] foo +", + ) + .with_stdout( + " +running 0 tests + +test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out + + +running 1 test +test src/lib.rs - foo (line 2) ... ok + +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out + +", + ) + .run(); +} + #[test] fn test_all_targets_lib() { let p = project().file("src/lib.rs", "").build(); @@ -3479,6 +3519,7 @@ fn test_all_targets_lib() { [COMPILING] foo [..] [FINISHED] dev [..] [RUNNING] [..]foo[..] +[DOCTEST] foo ", ) .run();