Skip to content

Commit 3c5af1a

Browse files
ehussalexcrichton
authored andcommitted
Fix cargo rustc for test with implicit binary.
Fixes #5502
1 parent 9e53ac6 commit 3c5af1a

File tree

3 files changed

+78
-14
lines changed

3 files changed

+78
-14
lines changed

src/cargo/core/compiler/context/unit_dependencies.rs

+40
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,46 @@ fn compute_deps<'a, 'b, 'cfg>(
148148
}
149149
ret.extend(maybe_lib(unit, bcx, profile_for));
150150

151+
// If any integration tests/benches are being run, make sure that
152+
// binaries are built as well.
153+
if !unit.mode.is_check() && unit.mode.is_any_test()
154+
&& (unit.target.is_test() || unit.target.is_bench())
155+
{
156+
ret.extend(
157+
unit.pkg
158+
.targets()
159+
.iter()
160+
.filter(|t| {
161+
let no_required_features = Vec::new();
162+
163+
t.is_bin() &&
164+
// Skip binaries with required features that have not been selected.
165+
t.required_features().unwrap_or(&no_required_features).iter().all(|f| {
166+
bcx.resolve.features(id).contains(f)
167+
})
168+
})
169+
.map(|t| {
170+
(
171+
// TODO: Should not be using profile_for here. Should
172+
// instead use ProfileFor::Any so that bins are built
173+
// with panic, but this aggravates
174+
// https://github.com/rust-lang/cargo/issues/5444
175+
// Switching it will fix
176+
// https://github.com/rust-lang/cargo/issues/5435
177+
new_unit(
178+
bcx,
179+
unit.pkg,
180+
t,
181+
profile_for,
182+
unit.kind.for_target(t),
183+
CompileMode::Build,
184+
),
185+
profile_for,
186+
)
187+
}),
188+
);
189+
}
190+
151191
Ok(ret)
152192
}
153193

src/cargo/ops/cargo_compile.rs

-13
Original file line numberDiff line numberDiff line change
@@ -614,19 +614,6 @@ fn generate_targets<'a>(
614614
}
615615
}
616616

617-
// If any integration tests/benches are being run, make sure that
618-
// binaries are built as well.
619-
if !build_config.mode.is_check() && proposals.iter().any(|&(ref unit, _)| {
620-
unit.mode.is_any_test() && (unit.target.is_test() || unit.target.is_bench())
621-
}) {
622-
proposals.extend(
623-
pkg.targets()
624-
.iter()
625-
.filter(|t| t.is_bin())
626-
.map(|t| (new_unit(pkg, t, CompileMode::Build), false)),
627-
);
628-
}
629-
630617
// Only include targets that are libraries or have all required
631618
// features available.
632619
for (unit, required) in proposals {

tests/testsuite/rustc.rs

+38-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use cargotest::support::{basic_lib_manifest, execs, project};
1+
use cargotest::support::{basic_bin_manifest, basic_lib_manifest, execs, project};
22
use hamcrest::assert_that;
33

44
const CARGO_RUSTC_ERROR: &'static str =
@@ -649,3 +649,40 @@ fn rustc_fingerprint() {
649649
),
650650
);
651651
}
652+
653+
#[test]
654+
fn rustc_test_with_implicit_bin() {
655+
let p = project("foo")
656+
.file("Cargo.toml", &basic_bin_manifest("foo"))
657+
.file(
658+
"src/main.rs",
659+
r#"
660+
#[cfg(foo)]
661+
fn f() { compile_fail!("Foo shouldn't be set."); }
662+
fn main() {}
663+
"#,
664+
)
665+
.file(
666+
"tests/test1.rs",
667+
r#"
668+
#[cfg(not(foo))]
669+
fn f() { compile_fail!("Foo should be set."); } "#,
670+
)
671+
.build();
672+
673+
assert_that(
674+
p.cargo("rustc --test test1 -v -- --cfg foo"),
675+
execs()
676+
.with_status(0)
677+
.with_stderr_contains(
678+
"\
679+
[RUNNING] `rustc --crate-name test1 tests[/]test1.rs [..] --cfg foo [..]
680+
",
681+
)
682+
.with_stderr_contains(
683+
"\
684+
[RUNNING] `rustc --crate-name foo src[/]main.rs [..]
685+
",
686+
),
687+
);
688+
}

0 commit comments

Comments
 (0)