]> git.proxmox.com Git - cargo.git/commitdiff
Fix testing bins with lib deps
authorAlex Crichton <alex@alexcrichton.com>
Wed, 9 Jul 2014 20:08:44 +0000 (13:08 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Wed, 9 Jul 2014 20:08:44 +0000 (13:08 -0700)
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
tests/test_cargo_test.rs

index d7713194a47a1feae2a6868da00294fa38764569..8e53918459881e57b9fd3314023e00f3c3e9a4df 100644 (file)
@@ -386,25 +386,34 @@ fn normalize(lib: Option<&[TomlLibTarget]>,
 {
     log!(4, "normalizing toml targets; lib={}; bin={}", lib, bin);
 
-    fn target_profiles(target: &TomlTarget) -> Vec<Profile> {
+    enum TestDep { Needed, NotNeeded }
+
+    fn target_profiles(target: &TomlTarget,
+                       dep: Option<TestDep>) -> Vec<Profile> {
         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<Target>, libs: &[TomlLibTarget], metadata: &Metadata) {
+    fn lib_targets(dst: &mut Vec<Target>, 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(),
index 426f0f2e4fb5f9c179f56a42b38c8ac55befe458..62116160bb218ff7461192559a904e4eee7920aa 100644 (file)
@@ -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));
+})