Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop test --doc from silently ignoring other target selecting options #6037

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}