From 3550dd5040894d0a9239e1d6b86335c862ac93e2 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Wed, 9 Jul 2014 13:08:44 -0700 Subject: [PATCH] Fix testing bins with lib deps If a package had both bin and lib deps, `cargo test` was not building the `lib` dependency when building the bins with `--test`. This commit adds an extra "test" profile (not compiled with --test) for situations such as this which is filtered out normally but kept around for the `cargo test` case. --- src/cargo/util/toml.rs | 23 ++++++++++++++++------- tests/test_cargo_test.rs | 17 +++++++++++++++++ 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/cargo/util/toml.rs b/src/cargo/util/toml.rs index d7713194a..8e5391845 100644 --- a/src/cargo/util/toml.rs +++ b/src/cargo/util/toml.rs @@ -386,25 +386,34 @@ fn normalize(lib: Option<&[TomlLibTarget]>, { log!(4, "normalizing toml targets; lib={}; bin={}", lib, bin); - fn target_profiles(target: &TomlTarget) -> Vec { + enum TestDep { Needed, NotNeeded } + + fn target_profiles(target: &TomlTarget, + dep: Option) -> Vec { let mut ret = vec!(Profile::default_dev(), Profile::default_release()); match target.test { Some(true) | None => ret.push(Profile::default_test()), + Some(false) => {} + } + + match dep { + Some(Needed) => ret.push(Profile::default_test().test(false)), _ => {} - }; + } ret } - fn lib_targets(dst: &mut Vec, libs: &[TomlLibTarget], metadata: &Metadata) { + fn lib_targets(dst: &mut Vec, libs: &[TomlLibTarget], + dep: TestDep, metadata: &Metadata) { let l = &libs[0]; let path = l.path.clone().unwrap_or_else(|| format!("src/{}.rs", l.name)); let crate_types = l.crate_type.clone().and_then(|kinds| { LibKind::from_strs(kinds).ok() }).unwrap_or_else(|| vec!(Lib)); - for profile in target_profiles(l).iter() { + for profile in target_profiles(l, Some(dep)).iter() { dst.push(Target::lib_target(l.name.as_slice(), crate_types.clone(), &Path::new(path.as_slice()), profile, metadata)); @@ -416,7 +425,7 @@ fn normalize(lib: Option<&[TomlLibTarget]>, for bin in bins.iter() { let path = bin.path.clone().unwrap_or_else(|| default(bin)); - for profile in target_profiles(bin).iter() { + for profile in target_profiles(bin, None).iter() { dst.push(Target::bin_target(bin.name.as_slice(), &Path::new(path.as_slice()), profile)); @@ -428,12 +437,12 @@ fn normalize(lib: Option<&[TomlLibTarget]>, match (lib, bin) { (Some(ref libs), Some(ref bins)) => { - lib_targets(&mut ret, libs.as_slice(), metadata); + lib_targets(&mut ret, libs.as_slice(), Needed, metadata); bin_targets(&mut ret, bins.as_slice(), |bin| format!("src/bin/{}.rs", bin.name)); }, (Some(ref libs), None) => { - lib_targets(&mut ret, libs.as_slice(), metadata); + lib_targets(&mut ret, libs.as_slice(), NotNeeded, metadata); }, (None, Some(ref bins)) => { bin_targets(&mut ret, bins.as_slice(), diff --git a/tests/test_cargo_test.rs b/tests/test_cargo_test.rs index 426f0f2e4..62116160b 100644 --- a/tests/test_cargo_test.rs +++ b/tests/test_cargo_test.rs @@ -38,3 +38,20 @@ test!(cargo_test_simple { assert_that(&p.bin("test/foo"), existing_file()); }) + +test!(test_with_lib_dep { + let p = project("foo") + .file("Cargo.toml", r#" + [project] + name = "foo" + version = "0.0.1" + authors = [] + "#) + .file("src/lib.rs", "pub fn foo(){}") + .file("src/main.rs", " + extern crate foo; + fn main() {} + "); + + assert_that(p.cargo_process("cargo-test"), execs().with_status(0)); +})