diff --git a/src/bin/cargo/commands/test.rs b/src/bin/cargo/commands/test.rs index 63ac24c5311..11f4d87e106 100644 --- a/src/bin/cargo/commands/test.rs +++ b/src/bin/cargo/commands/test.rs @@ -94,6 +94,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult { let mut compile_opts = args.compile_options(config, CompileMode::Test, Some(&ws))?; + let no_run = args.is_present("no-run"); let doc = args.is_present("doc"); if doc { if let CompileFilter::Only { .. } = compile_opts.filter { @@ -102,6 +103,12 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult { 101, )); } + if no_run { + return Err(CliError::new( + failure::format_err!("Can't skip running doc tests with --no-run"), + 101, + )); + } compile_opts.build_config.mode = CompileMode::Doctest; compile_opts.filter = ops::CompileFilter::new( true, @@ -118,7 +125,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult { } let ops = ops::TestOptions { - no_run: args.is_present("no-run"), + no_run, no_fail_fast: args.is_present("no-fail-fast"), compile_opts, }; diff --git a/tests/testsuite/test.rs b/tests/testsuite/test.rs index 60daa8226a3..4d0d96f7c6a 100644 --- a/tests/testsuite/test.rs +++ b/tests/testsuite/test.rs @@ -3449,6 +3449,26 @@ test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out .run(); } +#[test] +fn can_not_no_run_doc_tests() { + let p = project() + .file( + "src/lib.rs", + r#" +/// ``` +/// let _x = 1 + "foo"; +/// ``` +pub fn foo() -> u8 { 1 } +"#, + ) + .build(); + + p.cargo("test --doc --no-run") + .with_status(101) + .with_stderr("[ERROR] Can't skip running doc tests with --no-run") + .run(); +} + #[test] fn test_all_targets_lib() { let p = project().file("src/lib.rs", "").build();