mirror of
https://github.com/rust-lang/cargo.git
synced 2025-09-28 11:20:36 +00:00
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:
parent
a5bcfa3746
commit
3550dd5040
@ -386,25 +386,34 @@ fn normalize(lib: Option<&[TomlLibTarget]>,
|
|||||||
{
|
{
|
||||||
log!(4, "normalizing toml targets; lib={}; bin={}", lib, bin);
|
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());
|
let mut ret = vec!(Profile::default_dev(), Profile::default_release());
|
||||||
|
|
||||||
match target.test {
|
match target.test {
|
||||||
Some(true) | None => ret.push(Profile::default_test()),
|
Some(true) | None => ret.push(Profile::default_test()),
|
||||||
|
Some(false) => {}
|
||||||
|
}
|
||||||
|
|
||||||
|
match dep {
|
||||||
|
Some(Needed) => ret.push(Profile::default_test().test(false)),
|
||||||
_ => {}
|
_ => {}
|
||||||
};
|
}
|
||||||
|
|
||||||
ret
|
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 l = &libs[0];
|
||||||
let path = l.path.clone().unwrap_or_else(|| format!("src/{}.rs", l.name));
|
let path = l.path.clone().unwrap_or_else(|| format!("src/{}.rs", l.name));
|
||||||
let crate_types = l.crate_type.clone().and_then(|kinds| {
|
let crate_types = l.crate_type.clone().and_then(|kinds| {
|
||||||
LibKind::from_strs(kinds).ok()
|
LibKind::from_strs(kinds).ok()
|
||||||
}).unwrap_or_else(|| vec!(Lib));
|
}).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(),
|
dst.push(Target::lib_target(l.name.as_slice(), crate_types.clone(),
|
||||||
&Path::new(path.as_slice()), profile,
|
&Path::new(path.as_slice()), profile,
|
||||||
metadata));
|
metadata));
|
||||||
@ -416,7 +425,7 @@ fn normalize(lib: Option<&[TomlLibTarget]>,
|
|||||||
for bin in bins.iter() {
|
for bin in bins.iter() {
|
||||||
let path = bin.path.clone().unwrap_or_else(|| default(bin));
|
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(),
|
dst.push(Target::bin_target(bin.name.as_slice(),
|
||||||
&Path::new(path.as_slice()),
|
&Path::new(path.as_slice()),
|
||||||
profile));
|
profile));
|
||||||
@ -428,12 +437,12 @@ fn normalize(lib: Option<&[TomlLibTarget]>,
|
|||||||
|
|
||||||
match (lib, bin) {
|
match (lib, bin) {
|
||||||
(Some(ref libs), Some(ref bins)) => {
|
(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_targets(&mut ret, bins.as_slice(),
|
||||||
|bin| format!("src/bin/{}.rs", bin.name));
|
|bin| format!("src/bin/{}.rs", bin.name));
|
||||||
},
|
},
|
||||||
(Some(ref libs), None) => {
|
(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)) => {
|
(None, Some(ref bins)) => {
|
||||||
bin_targets(&mut ret, bins.as_slice(),
|
bin_targets(&mut ret, bins.as_slice(),
|
||||||
|
@ -38,3 +38,20 @@ test!(cargo_test_simple {
|
|||||||
|
|
||||||
assert_that(&p.bin("test/foo"), existing_file());
|
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));
|
||||||
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user