Skip to content

Commit

Permalink
Stop test --doc from silently ignoring other target selecting options
Browse files Browse the repository at this point in the history
  • Loading branch information
dwijnand committed Sep 16, 2018
1 parent b881799 commit ac600ae
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/bin/cargo/commands/test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use command_prelude::*;

use cargo::ops;
use cargo::ops::{self, CompileFilter};

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

let mut compile_opts = args.compile_options(config, CompileMode::Test)?;

let doc = args.is_present("doc");
if doc {
if let CompileFilter::Only { .. } = compile_opts.filter {
return Err(CliError::new(format_err!("Can't mix --doc with other target selecting options"), 101))
}
compile_opts.build_config.mode = CompileMode::Doctest;
compile_opts.filter = ops::CompileFilter::new(
true,
Expand Down
67 changes: 67 additions & 0 deletions tests/testsuite/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3101,3 +3101,70 @@ fn doctest_skip_staticlib() {
[RUNNING] target/debug/deps/foo-[..]",
).run();
}

#[test]
fn can_not_mix_doc_tests_and_regular_tests() {
let p = project()
.file("src/lib.rs", "\
/// ```
/// assert_eq!(1, 1)
/// ```
pub fn foo() -> u8 { 1 }
#[cfg(test)] mod tests {
#[test] fn it_works() { assert_eq!(2 + 2, 4); }
}
")
.build();

p.cargo("test")
.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 1 test
test tests::it_works ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
running 1 test
test src/lib.rs - foo (line 1) ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
\n")
.run();

p.cargo("test --lib")
.with_stderr("\
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
[RUNNING] target/debug/deps/foo-[..]\n")
.with_stdout("
running 1 test
test tests::it_works ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
\n")
.run();

p.cargo("test --doc")
.with_stderr("\
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
[DOCTEST] foo
")
.with_stdout("
running 1 test
test src/lib.rs - foo (line 1) ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
").run();

p.cargo("test --lib --doc")
.with_status(101)
.with_stderr("[ERROR] Can't mix --doc with other target selecting options\n")
.run();
}

0 comments on commit ac600ae

Please sign in to comment.