]> git.proxmox.com Git - cargo.git/commitdiff
Fix `test --doc` with incompatible lib types.
authorEric Huss <eric@huss.org>
Thu, 26 Jul 2018 05:40:46 +0000 (22:40 -0700)
committerEric Huss <eric@huss.org>
Thu, 26 Jul 2018 05:40:46 +0000 (22:40 -0700)
When I recently changed the doctest handling, I forgot to check the lib type in
the `test --doc` scenario.

I also added the package name to a nearby error message, since it can be
confusing in a workspace setting.

src/cargo/core/manifest.rs
src/cargo/ops/cargo_compile.rs
tests/testsuite/build_lib.rs
tests/testsuite/test.rs

index 13ca1b3d26aa55d1379077ff2e398e0376225837..da29590479d8335fd968042521fdd04ce5f73b10 100644 (file)
@@ -628,9 +628,12 @@ impl Target {
     pub fn benched(&self) -> bool {
         self.benched
     }
-
     pub fn doctested(&self) -> bool {
-        self.doctest && match self.kind {
+        self.doctest
+    }
+
+    pub fn doctestable(&self) -> bool {
+        match self.kind {
             TargetKind::Lib(ref kinds) => kinds
                 .iter()
                 .any(|k| *k == LibKind::Rlib || *k == LibKind::Lib || *k == LibKind::ProcMacro),
index b8e30ca5d153d2a71b070e2996f3a8bcde95f533..45d2afef10157dd5df41eacd2bcb5209c3b8d82b 100644 (file)
@@ -545,7 +545,11 @@ fn generate_targets<'a>(
                 proposals.extend(default_units);
                 if build_config.mode == CompileMode::Test {
                     // Include doctest for lib.
-                    if let Some(t) = pkg.targets().iter().find(|t| t.is_lib() && t.doctested()) {
+                    if let Some(t) = pkg
+                        .targets()
+                        .iter()
+                        .find(|t| t.is_lib() && t.doctested() && t.doctestable())
+                    {
                         proposals.push((new_unit(pkg, t, CompileMode::Doctest), false));
                     }
                 }
@@ -560,9 +564,16 @@ fn generate_targets<'a>(
             } => {
                 if lib {
                     if let Some(target) = pkg.targets().iter().find(|t| t.is_lib()) {
+                        if build_config.mode == CompileMode::Doctest && !target.doctestable() {
+                            bail!(
+                                "doc tests are not supported for crate type(s) `{}` in package `{}`",
+                                target.rustc_crate_types().join(", "),
+                                pkg.name()
+                            );
+                        }
                         proposals.push((new_unit(pkg, target, build_config.mode), false));
                     } else if !all_targets {
-                        bail!("no library targets found")
+                        bail!("no library targets found in package `{}`", pkg.name())
                     }
                 }
                 // If --tests was specified, add all targets that would be
index 28265000009cfe0c401e48631c68172488b8c861..213ac9cc1fd4e7abc53561456959de49623c61ee 100644 (file)
@@ -45,7 +45,7 @@ fn build_with_no_lib() {
         p.cargo("build").arg("--lib"),
         execs()
             .with_status(101)
-            .with_stderr("[ERROR] no library targets found"),
+            .with_stderr("[ERROR] no library targets found in package `foo`"),
     );
 }
 
index cf3c62095d85f99a9add04f89d00824381f3bd47..84718b32634f22694f796b7af82b3dec18c7f5f0 100644 (file)
@@ -3547,3 +3547,45 @@ fn test_build_script_links() {
         execs().with_status(0),
     );
 }
+
+#[test]
+fn doctest_skip_staticlib() {
+    let p = project()
+        .file(
+            "Cargo.toml",
+            r#"
+                [package]
+                name = "foo"
+                version = "0.0.1"
+
+                [lib]
+                crate-type = ["staticlib"]
+            "#,
+        )
+        .file(
+            "src/lib.rs",
+            r#"
+            //! ```
+            //! assert_eq!(1,2);
+            //! ```
+            "#,
+        )
+        .build();
+
+    assert_that(
+        p.cargo("test --doc"),
+        execs().with_status(101).with_stderr(
+            "[ERROR] doc tests are not supported for crate type(s) `staticlib` in package `foo`",
+        ),
+    );
+
+    assert_that(
+        p.cargo("test"),
+        execs().with_status(0).with_stderr(
+            "\
+[COMPILING] foo [..]
+[FINISHED] dev [..]
+[RUNNING] target[/]debug[/]deps[/]foo-[..]",
+        ),
+    )
+}