Skip to content

Commit b7ef8d5

Browse files
committed
Auto merge of #6037 - dwijnand:stop-test--doc-from-accepting-other-target-options, r=alexcrichton
Stop test --doc from silently ignoring other target selecting options Fixes #5054
2 parents 7b943bc + ac600ae commit b7ef8d5

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

src/bin/cargo/commands/test.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use command_prelude::*;
22

3-
use cargo::ops;
3+
use cargo::ops::{self, CompileFilter};
44

55
pub fn cli() -> App {
66
subcommand("test")
@@ -92,8 +92,12 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
9292
let ws = args.workspace(config)?;
9393

9494
let mut compile_opts = args.compile_options(config, CompileMode::Test)?;
95+
9596
let doc = args.is_present("doc");
9697
if doc {
98+
if let CompileFilter::Only { .. } = compile_opts.filter {
99+
return Err(CliError::new(format_err!("Can't mix --doc with other target selecting options"), 101))
100+
}
97101
compile_opts.build_config.mode = CompileMode::Doctest;
98102
compile_opts.filter = ops::CompileFilter::new(
99103
true,

tests/testsuite/test.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3101,3 +3101,70 @@ fn doctest_skip_staticlib() {
31013101
[RUNNING] target/debug/deps/foo-[..]",
31023102
).run();
31033103
}
3104+
3105+
#[test]
3106+
fn can_not_mix_doc_tests_and_regular_tests() {
3107+
let p = project()
3108+
.file("src/lib.rs", "\
3109+
/// ```
3110+
/// assert_eq!(1, 1)
3111+
/// ```
3112+
pub fn foo() -> u8 { 1 }
3113+
3114+
#[cfg(test)] mod tests {
3115+
#[test] fn it_works() { assert_eq!(2 + 2, 4); }
3116+
}
3117+
")
3118+
.build();
3119+
3120+
p.cargo("test")
3121+
.with_stderr("\
3122+
[COMPILING] foo v0.0.1 ([CWD])
3123+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
3124+
[RUNNING] target/debug/deps/foo-[..]
3125+
[DOCTEST] foo
3126+
")
3127+
.with_stdout("
3128+
running 1 test
3129+
test tests::it_works ... ok
3130+
3131+
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
3132+
3133+
3134+
running 1 test
3135+
test src/lib.rs - foo (line 1) ... ok
3136+
3137+
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
3138+
\n")
3139+
.run();
3140+
3141+
p.cargo("test --lib")
3142+
.with_stderr("\
3143+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
3144+
[RUNNING] target/debug/deps/foo-[..]\n")
3145+
.with_stdout("
3146+
running 1 test
3147+
test tests::it_works ... ok
3148+
3149+
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
3150+
\n")
3151+
.run();
3152+
3153+
p.cargo("test --doc")
3154+
.with_stderr("\
3155+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
3156+
[DOCTEST] foo
3157+
")
3158+
.with_stdout("
3159+
running 1 test
3160+
test src/lib.rs - foo (line 1) ... ok
3161+
3162+
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
3163+
3164+
").run();
3165+
3166+
p.cargo("test --lib --doc")
3167+
.with_status(101)
3168+
.with_stderr("[ERROR] Can't mix --doc with other target selecting options\n")
3169+
.run();
3170+
}

0 commit comments

Comments
 (0)