Document the lib if a lib and bin have the same name
authorCarol (Nichols || Goulding) <carol.nichols@gmail.com>
Mon, 16 Oct 2017 16:30:57 +0000 (12:30 -0400)
committerCarol (Nichols || Goulding) <carol.nichols@gmail.com>
Wed, 25 Oct 2017 14:25:22 +0000 (10:25 -0400)
Fixes #4341.

- Removes the check that bailed if there was a  bin and lib with the
  same name
- Exclude bins with the same name as libs from the proposed targets to
  build when compiling docs
- Adjust tests to expect this behavior

src/cargo/ops/cargo_compile.rs
src/cargo/ops/cargo_doc.rs
tests/doc.rs
tests/rustdoc.rs

index 685911203dec60f46340aff865ff4c10d555b5df..2de0739c62872987b3b5a0dd393e052d603ab804 100644 (file)
@@ -489,7 +489,10 @@ fn generate_auto_targets<'a>(mode: CompileMode, targets: &'a [Target],
         }
         CompileMode::Doc { .. } => {
             targets.iter().filter(|t| {
-                t.documented()
+                t.documented() && (
+                    !t.is_bin() ||
+                    !targets.iter().any(|l| l.is_lib() && l.name() == t.name())
+                )
             }).map(|t| BuildProposal {
                 target: t,
                 profile: profile,
index d4d5620367b9dcc25401102bb7b4cfb9820791f9..150a3a99608c0dc22429ad3c1a2bfa41a5f5824f 100644 (file)
@@ -52,20 +52,6 @@ pub fn doc(ws: &Workspace, options: &DocOptions) -> CargoResult<()> {
                 }
             }
         }
-        for (bin, bin_package) in bin_names.iter() {
-            if let Some(lib_package) = lib_names.get(bin) {
-                bail!("The target `{}` is specified as a library {}. It can be \
-                       documented only once. Consider renaming or marking one \
-                       of the targets as `doc = false`.",
-                       bin,
-                       if lib_package == bin_package {
-                           format!("and as a binary by package `{}`", lib_package)
-                       } else {
-                           format!("by package `{}` and as a binary by \
-                                    package `{}`", lib_package, bin_package)
-                       });
-            }
-        }
     }
 
     ops::compile(ws, &options.compile_opts)?;
index 0d215c91a98d19e3c92471daf0fbe152fafeb5ee..c61428befc5a8113207286f3b45a9a5447dd8ed0 100644 (file)
@@ -3,7 +3,8 @@ extern crate hamcrest;
 extern crate cargo;
 
 use std::str;
-use std::fs;
+use std::fs::{self, File};
+use std::io::Read;
 
 use cargotest::rustc_host;
 use cargotest::support::{project, execs, path2url};
@@ -255,6 +256,7 @@ fn doc_multiple_targets_same_name() {
             version = "0.1.0"
             [[bin]]
             name = "foo_lib"
+            path = "src/foo_lib.rs"
         "#)
         .file("foo/src/foo_lib.rs", "")
         .file("bar/Cargo.toml", r#"
@@ -267,12 +269,17 @@ fn doc_multiple_targets_same_name() {
         .file("bar/src/lib.rs", "")
         .build();
 
+        let root = path2url(p.root());
+
         assert_that(p.cargo("doc").arg("--all"),
                     execs()
-                    .with_status(101)
-                    .with_stderr_contains("[..] target `foo_lib` [..]")
-                    .with_stderr_contains("[..] binary by package `foo v0.1.0[..]`[..]")
-                    .with_stderr_contains("[..] library by package `bar v0.1.0[..]` [..]"));
+                    .with_status(0)
+                    .with_stderr_contains(&format!("[DOCUMENTING] foo v0.1.0 ({}/foo)", root))
+                    .with_stderr_contains(&format!("[DOCUMENTING] bar v0.1.0 ({}/bar)", root))
+                    .with_stderr_contains("[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]"));
+        assert_that(&p.root().join("target/doc"), existing_dir());
+        let doc_file = p.root().join("target/doc/foo_lib/index.html");
+        assert_that(&doc_file, existing_file());
 }
 
 #[test]
@@ -339,7 +346,7 @@ fn doc_multiple_targets_same_name_undoced() {
 }
 
 #[test]
-fn doc_lib_bin_same_name() {
+fn doc_lib_bin_same_name_documents_lib() {
     let p = project("foo")
         .file("Cargo.toml", r#"
             [package]
@@ -347,15 +354,31 @@ fn doc_lib_bin_same_name() {
             version = "0.0.1"
             authors = []
         "#)
-        .file("src/main.rs", "fn main() {}")
-        .file("src/lib.rs", "fn foo() {}")
+        .file("src/main.rs", r#"
+            //! Binary documentation
+            extern crate foo;
+            fn main() {
+                foo::foo();
+            }
+        "#)
+        .file("src/lib.rs", r#"
+            //! Library documentation
+            pub fn foo() {}
+        "#)
         .build();
 
     assert_that(p.cargo("doc"),
-                execs().with_status(101)
-                       .with_stderr("\
-[ERROR] The target `foo` is specified as a library and as a binary by package \
-`foo [..]`. It can be documented[..]"));
+                execs().with_status(0).with_stderr(&format!("\
+[..] foo v0.0.1 ({dir})
+[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
+", dir = path2url(p.root()))));
+    assert_that(&p.root().join("target/doc"), existing_dir());
+    let doc_file = p.root().join("target/doc/foo/index.html");
+    assert_that(&doc_file, existing_file());
+    let mut doc_html = String::new();
+    File::open(&doc_file).unwrap().read_to_string(&mut doc_html).unwrap();
+    assert!(doc_html.contains("Library"));
+    assert!(!doc_html.contains("Binary"));
 }
 
 #[test]
@@ -503,7 +526,7 @@ fn output_not_captured() {
     if let CargoError(CargoErrorKind::ProcessErrorKind(perr), ..) = error {
         let output = perr.output.unwrap();
         let stderr = str::from_utf8(&output.stderr).unwrap();
-    
+
         assert!(stderr.contains("☃"), "no snowman\n{}", stderr);
         assert!(stderr.contains("unknown start of token"), "no message{}", stderr);
     } else {
index 371937ccb0189cb3efb6b2f474c056ad333382b1..570ce6ad8fd9b223d03d15994052309214fced8c 100644 (file)
@@ -147,7 +147,7 @@ fn rustdoc_only_bar_dependency() {
 
 
 #[test]
-fn rustdoc_same_name_err() {
+fn rustdoc_same_name_documents_lib() {
     let p = project("foo")
         .file("Cargo.toml", r#"
             [package]
@@ -164,7 +164,13 @@ fn rustdoc_same_name_err() {
     assert_that(p.cargo("rustdoc").arg("-v")
                  .arg("--").arg("--cfg=foo"),
                 execs()
-                .with_status(101)
-                .with_stderr("[ERROR] The target `foo` is specified as a \
-library and as a binary by package `foo [..]`. It can be documented[..]"));
+                .with_status(0)
+                .with_stderr(format!("\
+[DOCUMENTING] foo v0.0.1 ([..])
+[RUNNING] `rustdoc --crate-name foo src[/]lib.rs \
+        -o {dir}[/]target[/]doc \
+        --cfg=foo \
+        -L dependency={dir}[/]target[/]debug[/]deps`
+[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
+", dir = p.root().display())));
 }