Add `cargo test --doc`
authorAlex Crichton <alex@alexcrichton.com>
Thu, 14 Apr 2016 17:37:16 +0000 (10:37 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Fri, 15 Apr 2016 23:56:35 +0000 (16:56 -0700)
Supports testing only the documentation (like `--lib`, `--bin`, etc).

src/bin/bench.rs
src/bin/test.rs
src/cargo/ops/cargo_test.rs
tests/test_cargo_test.rs

index a4591c9112fdf5db5a4954f143c3920df3424c51..ca8653442b5186b02e345a756673bba62a89e1b6 100644 (file)
@@ -70,6 +70,7 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
     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,
index 65f82490fb357e9bc021c6b9afa8133d6a17cfbc..e2602b1a2c11771c4e0891c654a4d42ed35cfc35 100644 (file)
@@ -13,6 +13,7 @@ pub struct Options {
     flag_package: Vec<String>,
     flag_target: Option<String>,
     flag_lib: bool,
+    flag_doc: bool,
     flag_bin: Vec<String>,
     flag_example: Vec<String>,
     flag_test: Vec<String>,
@@ -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<Option<()>> {
                                 &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<Option<()>> {
             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,
         },
index d4d20fea7d7060286acbae5c2134c6e7bd12672c..8548087986c20816424a27c906107cb98f32997e 100644 (file)
@@ -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<Option<CargoTestError>> {
@@ -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 {
index 317743045dbcfde9824c379bada8bdabc6f777b8..81cadd43494979fdbebb962ab55c1a1a36b2509c 100644 (file)
@@ -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));
+});