Skip to content

[BETA] Fix cargo rustc for test with implicit binary. #5510

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/cargo/core/compiler/context/unit_dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,46 @@ fn compute_deps<'a, 'b, 'cfg>(
}
ret.extend(maybe_lib(unit, bcx, profile_for));

// If any integration tests/benches are being run, make sure that
// binaries are built as well.
if !unit.mode.is_check() && unit.mode.is_any_test()
&& (unit.target.is_test() || unit.target.is_bench())
{
ret.extend(
unit.pkg
.targets()
.iter()
.filter(|t| {
let no_required_features = Vec::new();

t.is_bin() &&
// Skip binaries with required features that have not been selected.
t.required_features().unwrap_or(&no_required_features).iter().all(|f| {
bcx.resolve.features(id).contains(f)
})
})
.map(|t| {
(
// TODO: Should not be using profile_for here. Should
// instead use ProfileFor::Any so that bins are built
// with panic, but this aggravates
// https://github.com/rust-lang/cargo/issues/5444
// Switching it will fix
// https://github.com/rust-lang/cargo/issues/5435
new_unit(
bcx,
unit.pkg,
t,
profile_for,
unit.kind.for_target(t),
CompileMode::Build,
),
profile_for,
)
}),
);
}

Ok(ret)
}

Expand Down
13 changes: 0 additions & 13 deletions src/cargo/ops/cargo_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -615,19 +615,6 @@ fn generate_targets<'a>(
}
}

// If any integration tests/benches are being run, make sure that
// binaries are built as well.
if !build_config.mode.is_check() && proposals.iter().any(|&(ref unit, _)| {
unit.mode.is_any_test() && (unit.target.is_test() || unit.target.is_bench())
}) {
proposals.extend(
pkg.targets()
.iter()
.filter(|t| t.is_bin())
.map(|t| (new_unit(pkg, t, CompileMode::Build), false)),
);
}

// Only include targets that are libraries or have all required
// features available.
for (unit, required) in proposals {
Expand Down
39 changes: 38 additions & 1 deletion tests/testsuite/rustc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cargotest::support::{basic_lib_manifest, execs, project};
use cargotest::support::{basic_bin_manifest, basic_lib_manifest, execs, project};
use hamcrest::assert_that;

const CARGO_RUSTC_ERROR: &'static str =
Expand Down Expand Up @@ -649,3 +649,40 @@ fn rustc_fingerprint() {
),
);
}

#[test]
fn rustc_test_with_implicit_bin() {
let p = project("foo")
.file("Cargo.toml", &basic_bin_manifest("foo"))
.file(
"src/main.rs",
r#"
#[cfg(foo)]
fn f() { compile_fail!("Foo shouldn't be set."); }
fn main() {}
"#,
)
.file(
"tests/test1.rs",
r#"
#[cfg(not(foo))]
fn f() { compile_fail!("Foo should be set."); } "#,
)
.build();

assert_that(
p.cargo("rustc --test test1 -v -- --cfg foo"),
execs()
.with_status(0)
.with_stderr_contains(
"\
[RUNNING] `rustc --crate-name test1 tests[/]test1.rs [..] --cfg foo [..]
",
)
.with_stderr_contains(
"\
[RUNNING] `rustc --crate-name foo src[/]main.rs [..]
",
),
);
}