Fix passing --extern and -L to doc tests

Previously --extern was passed for *all* upstream dependencies, causing
conflicts if some had duplicate names. Now cargo only passes --extern for
libraries that were built including immediate dependencies. Cargo also
additionally now properly passes `-L dependency=` instead of just a plain `-L`.

Closes #1449
This commit is contained in:
Alex Crichton 2015-03-25 12:46:21 -07:00
parent 19008f5803
commit 5033dab9b3
6 changed files with 85 additions and 19 deletions

View file

@ -14,7 +14,7 @@ pub struct Compilation {
///
/// This is currently used for passing --extern flags to rustdoc tests later
/// on.
pub libraries: HashMap<PackageId, Vec<PathBuf>>,
pub libraries: HashMap<PackageId, Vec<(String, PathBuf)>>,
/// An array of all tests created during this compilation.
pub tests: Vec<(String, PathBuf)>,

View file

@ -58,18 +58,7 @@ pub fn prepare_target<'a, 'b>(cx: &mut Context<'a, 'b>,
let mut missing_outputs = false;
if !profile.doc {
for filename in try!(cx.target_filenames(target, profile)).iter() {
let dst = root.join(filename);
missing_outputs |= fs::metadata(&dst).is_err();
if profile.test {
cx.compilation.tests.push((target.name().to_string(), dst));
} else if target.is_bin() || target.is_example() {
cx.compilation.binaries.push(dst);
} else if target.is_lib() {
let pkgid = pkg.package_id().clone();
cx.compilation.libraries.entry(pkgid).or_insert(Vec::new())
.push(dst);
}
missing_outputs |= fs::metadata(root.join(filename)).is_err();
}
}

View file

@ -132,6 +132,38 @@ pub fn compile_targets<'a, 'b>(targets: &[(&'a Target, &'a Profile)],
.display().to_string();
cx.compilation.extra_env.insert("OUT_DIR".to_string(), out_dir);
for &(target, profile) in targets {
for filename in try!(cx.target_filenames(target, profile)).iter() {
let dst = cx.out_dir(pkg, Kind::Target, target).join(filename);
if profile.test {
cx.compilation.tests.push((target.name().to_string(), dst));
} else if target.is_bin() || target.is_example() {
cx.compilation.binaries.push(dst);
} else if target.is_lib() {
let pkgid = pkg.package_id().clone();
cx.compilation.libraries.entry(pkgid).or_insert(Vec::new())
.push((target.crate_name(), dst));
if !target.is_lib() { continue }
// Include immediate lib deps as well
for dep in cx.dep_targets(pkg, target, profile).iter() {
let (pkg, target, profile) = *dep;
let pkgid = pkg.package_id();
if !target.is_lib() { continue }
if profile.doc { continue }
if cx.compilation.libraries.contains_key(&pkgid) { continue }
let v = try!(cx.target_filenames(target, profile));
let v = v.into_iter().map(|f| {
(target.crate_name(),
cx.out_dir(pkg, Kind::Target, target).join(f))
}).collect::<Vec<_>>();
cx.compilation.libraries.insert(pkgid.clone(), v);
}
}
}
}
if let Some(feats) = cx.resolve.features(pkg.package_id()) {
cx.compilation.features.extend(feats.iter().cloned());
}

View file

@ -31,8 +31,11 @@ pub fn run_tests(manifest_path: &Path,
let mut p = try!(compile.rustdoc_process(&compile.package));
p.arg("--test").arg(lib)
.arg("--crate-name").arg(&crate_name)
.arg("-L").arg(&compile.root_output)
.arg("-L").arg(&compile.deps_output)
.arg("-L").arg(&{
let mut arg = OsString::from("dependency=");
arg.push(&compile.deps_output);
arg
})
.cwd(compile.package.root());
if test_args.len() > 0 {
@ -43,9 +46,9 @@ pub fn run_tests(manifest_path: &Path,
p.arg("--cfg").arg(&format!("feature=\"{}\"", feat));
}
for (pkg, libs) in compile.libraries.iter() {
for lib in libs.iter() {
let mut arg = OsString::from(pkg.name());
for (_, libs) in compile.libraries.iter() {
for &(ref name, ref lib) in libs.iter() {
let mut arg = OsString::from(name);
arg.push("=");
arg.push(lib);
p.arg("--extern").arg(&arg);

View file

@ -1674,3 +1674,45 @@ test!(dont_require_submodules_are_checked_out {
assert_that(git1.cargo("build").arg("-v").cwd(&dst),
execs().with_status(0));
});
test!(doctest_same_name {
let a2 = git_repo("a2", |p| {
p.file("Cargo.toml", r#"
[project]
name = "a"
version = "0.5.0"
authors = []
"#)
.file("src/lib.rs", "pub fn a2() {}")
}).unwrap();
let a1 = git_repo("a1", |p| {
p.file("Cargo.toml", &format!(r#"
[project]
name = "a"
version = "0.5.0"
authors = []
[dependencies]
a = {{ git = '{}' }}
"#, a2.url()))
.file("src/lib.rs", "extern crate a; pub fn a1() {}")
}).unwrap();
let p = project("foo")
.file("Cargo.toml", &format!(r#"
[package]
name = "foo"
version = "0.0.1"
authors = []
[dependencies]
a = {{ git = '{}' }}
"#, a1.url()))
.file("src/lib.rs", r#"
#[macro_use]
extern crate a;
"#);
assert_that(p.cargo_process("test").arg("-v"),
execs().with_status(0));
});

View file

@ -1416,6 +1416,6 @@ test!(dashes_to_underscores {
pub fn foo() -> i32 { 1 }
"#);
assert_that(p.cargo_process("test"),
assert_that(p.cargo_process("test").arg("-v"),
execs().with_status(0));
});