diff --git a/src/bin/bench.rs b/src/bin/bench.rs index a4591c9112f..ca8653442b5 100644 --- a/src/bin/bench.rs +++ b/src/bin/bench.rs @@ -70,6 +70,7 @@ pub fn execute(options: Options, config: &Config) -> CliResult> { let ops = ops::TestOptions { no_run: options.flag_no_run, no_fail_fast: false, + only_doc: false, compile_opts: ops::CompileOptions { config: config, jobs: options.flag_jobs, diff --git a/src/bin/test.rs b/src/bin/test.rs index 65f82490fb3..e2602b1a2c1 100644 --- a/src/bin/test.rs +++ b/src/bin/test.rs @@ -13,6 +13,7 @@ pub struct Options { flag_package: Vec, flag_target: Option, flag_lib: bool, + flag_doc: bool, flag_bin: Vec, flag_example: Vec, flag_test: Vec, @@ -33,6 +34,7 @@ Usage: Options: -h, --help Print this message --lib Test only this package's library + --doc Test only this library's documentation --bin NAME Test only the specified binary --example NAME Test only the specified example --test NAME Test only the specified integration test target @@ -79,9 +81,24 @@ pub fn execute(options: Options, config: &Config) -> CliResult> { &options.flag_color)); let root = try!(find_root_manifest_for_wd(options.flag_manifest_path, config.cwd())); + let empty = Vec::new(); + let (mode, filter); + if options.flag_doc { + mode = ops::CompileMode::Build; + filter = ops::CompileFilter::new(true, &empty, &empty, &empty, &empty); + } else { + mode = ops::CompileMode::Test; + filter = ops::CompileFilter::new(options.flag_lib, + &options.flag_bin, + &options.flag_test, + &options.flag_example, + &options.flag_bench); + } + let ops = ops::TestOptions { no_run: options.flag_no_run, no_fail_fast: options.flag_no_fail_fast, + only_doc: options.flag_doc, compile_opts: ops::CompileOptions { config: config, jobs: options.flag_jobs, @@ -91,12 +108,8 @@ pub fn execute(options: Options, config: &Config) -> CliResult> { spec: &options.flag_package, exec_engine: None, release: options.flag_release, - mode: ops::CompileMode::Test, - filter: ops::CompileFilter::new(options.flag_lib, - &options.flag_bin, - &options.flag_test, - &options.flag_example, - &options.flag_bench), + mode: mode, + filter: filter, target_rustdoc_args: None, target_rustc_args: None, }, diff --git a/src/cargo/ops/cargo_test.rs b/src/cargo/ops/cargo_test.rs index d4d20fea7d7..8548087986c 100644 --- a/src/cargo/ops/cargo_test.rs +++ b/src/cargo/ops/cargo_test.rs @@ -8,10 +8,9 @@ pub struct TestOptions<'a> { pub compile_opts: ops::CompileOptions<'a>, pub no_run: bool, pub no_fail_fast: bool, + pub only_doc: bool, } - - pub fn run_tests(manifest_path: &Path, options: &TestOptions, test_args: &[String]) -> CargoResult> { @@ -20,7 +19,11 @@ pub fn run_tests(manifest_path: &Path, if options.no_run { return Ok(None) } - let mut errors = try!(run_unit_tests(options, test_args, &compilation)); + let mut errors = if options.only_doc { + Vec::new() + } else { + try!(run_unit_tests(options, test_args, &compilation)) + }; // If we have an error and want to fail fast, return if !errors.is_empty() && !options.no_fail_fast { diff --git a/tests/test_cargo_test.rs b/tests/test_cargo_test.rs index 317743045db..81cadd43494 100644 --- a/tests/test_cargo_test.rs +++ b/tests/test_cargo_test.rs @@ -2082,3 +2082,30 @@ test!(selective_test_optional_dep { {running} `rustc a[..]src[..]lib.rs [..]` ", compiling = COMPILING, running = RUNNING))); }); + +test!(only_test_docs { + let p = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + authors = [] + "#) + .file("src/lib.rs", r#" + #[test] + fn foo() { + let a: u32 = "hello"; + } + + /// ``` + /// println!("ok"); + /// ``` + pub fn bar() { + } + "#) + .file("tests/foo.rs", "this is not rust"); + p.build(); + + assert_that(p.cargo("test").arg("--doc"), + execs().with_status(0)); +});