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.
This commit is contained in:
Alex Crichton 2014-07-09 13:08:44 -07:00
parent a5bcfa3746
commit 3550dd5040
2 changed files with 33 additions and 7 deletions

View 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(),

View 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));
})