Pass RUSTDOCFLAGS to rustdoc spawned through cargo test (fixes #4738)
authorAnthony Ramine <n.oxyde@gmail.com>
Wed, 22 Nov 2017 13:49:10 +0000 (14:49 +0100)
committerAnthony Ramine <n.oxyde@gmail.com>
Fri, 1 Dec 2017 15:21:52 +0000 (16:21 +0100)
src/cargo/ops/cargo_rustc/compilation.rs
src/cargo/ops/cargo_rustc/mod.rs
src/cargo/ops/cargo_test.rs
tests/rustdocflags.rs

index b3643b6bf29347bf0787c602a9ea3b32bce12971..a40c310a12fe055f2dbe93f93a03286b09721055 100644 (file)
@@ -50,6 +50,9 @@ pub struct Compilation<'cfg> {
     /// Features per package enabled during this compilation.
     pub cfgs: HashMap<PackageId, HashSet<String>>,
 
+    /// Flags to pass to rustdoc when invoked from cargo test, per package.
+    pub rustdocflags: HashMap<PackageId, Vec<String>>,
+
     pub target: String,
 
     config: &'cfg Config,
@@ -72,6 +75,7 @@ impl<'cfg> Compilation<'cfg> {
             extra_env: HashMap::new(),
             to_doc_test: Vec::new(),
             cfgs: HashMap::new(),
+            rustdocflags: HashMap::new(),
             config: config,
             target: String::new(),
             target_runner: LazyCell::new(),
index 8dce388c055b176fbabe6bf726191bec5ab4d1bb..bc0c812ca208f995f7b8878b15c7959e43b1397e 100644 (file)
@@ -229,6 +229,12 @@ pub fn compile_targets<'a, 'cfg: 'a>(ws: &Workspace<'cfg>,
         cx.compilation.cfgs.entry(unit.pkg.package_id().clone())
             .or_insert_with(HashSet::new)
             .extend(feats.iter().map(|feat| format!("feature=\"{}\"", feat)));
+        let rustdocflags = cx.rustdocflags_args(&unit)?;
+        if !rustdocflags.is_empty() {
+            cx.compilation.rustdocflags.entry(unit.pkg.package_id().clone())
+                .or_insert_with(Vec::new)
+                .extend(rustdocflags);
+        }
 
         output_depinfo(&mut cx, unit)?;
     }
index f808ff5e1b393c645cb2339c07cccb936760106c..ad9ea32e327e4b38c752fd3a23c9a4648d8d931f 100644 (file)
@@ -199,6 +199,10 @@ fn run_doc_tests(options: &TestOptions,
                 p.arg("--extern").arg(&arg);
             }
 
+            if let Some(flags) = compilation.rustdocflags.get(package.package_id()) {
+                p.args(flags);
+            }
+
             config.shell().verbose(|shell| {
                 shell.status("Running", p.to_string())
             })?;
index e1385d3e6aac1fd7729dc3467fc85bd1ecf4535b..e8bc2fad9dd61521f578eebb16287642e758e69c 100644 (file)
@@ -86,3 +86,22 @@ fn rerun() {
 [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
 "));
 }
+
+#[test]
+fn rustdocflags_passed_to_rustdoc_through_cargo_test() {
+    let p = project("foo")
+        .file("Cargo.toml", r#"
+            [package]
+            name = "foo"
+            version = "0.0.1"
+        "#)
+        .file("src/lib.rs", r#"
+            //! ```
+            //! assert!(cfg!(do_not_choke));
+            //! ```
+        "#)
+        .build();
+
+    assert_that(p.cargo("test").arg("--doc").env("RUSTDOCFLAGS", "--cfg do_not_choke"),
+                execs().with_status(0));
+}