Skip to content

Commit 56db829

Browse files
author
Jon Gjengset
committed
Avoid #[cfg] duplicated main in tests
It makes the logic really hard to follow.
1 parent 54674d3 commit 56db829

File tree

1 file changed

+20
-36
lines changed

1 file changed

+20
-36
lines changed

tests/testsuite/rustflags.rs

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ fn env_rustflags_normal_source() {
5555
fn env_rustflags_build_script() {
5656
// RUSTFLAGS should be passed to rustc for build scripts
5757
// when --target is not specified.
58-
// In this test if --cfg foo is passed the build will fail.
5958
let p = project()
6059
.file(
6160
"Cargo.toml",
@@ -70,9 +69,7 @@ fn env_rustflags_build_script() {
7069
.file(
7170
"build.rs",
7271
r#"
73-
fn main() { }
74-
#[cfg(not(foo))]
75-
fn main() { }
72+
fn main() { assert!(cfg!(foo)); }
7673
"#,
7774
)
7875
.build();
@@ -243,7 +240,6 @@ fn env_rustflags_normal_source_with_target() {
243240
fn env_rustflags_build_script_with_target() {
244241
// RUSTFLAGS should not be passed to rustc for build scripts
245242
// when --target is specified.
246-
// In this test if --cfg foo is passed the build will fail.
247243
let p = project()
248244
.file(
249245
"Cargo.toml",
@@ -258,9 +254,7 @@ fn env_rustflags_build_script_with_target() {
258254
.file(
259255
"build.rs",
260256
r#"
261-
fn main() { }
262-
#[cfg(foo)]
263-
fn main() { }
257+
fn main() { assert!(!cfg!(foo)); }
264258
"#,
265259
)
266260
.build();
@@ -276,7 +270,6 @@ fn env_rustflags_build_script_with_target() {
276270
fn env_rustflags_build_script_with_target_doesnt_apply_to_host_kind() {
277271
// RUSTFLAGS should *not* be passed to rustc for build scripts when --target is specified as the
278272
// host triple even if target-applies-to-host-kind is enabled, to match legacy Cargo behavior.
279-
// In this test if --cfg foo is passed the build will fail.
280273
let p = project()
281274
.file(
282275
"Cargo.toml",
@@ -291,9 +284,7 @@ fn env_rustflags_build_script_with_target_doesnt_apply_to_host_kind() {
291284
.file(
292285
"build.rs",
293286
r#"
294-
fn main() { }
295-
#[cfg(foo)]
296-
fn main() { }
287+
fn main() { assert!(!cfg!(foo)); }
297288
"#,
298289
)
299290
.file(
@@ -519,7 +510,6 @@ fn build_rustflags_normal_source() {
519510
fn build_rustflags_build_script() {
520511
// RUSTFLAGS should be passed to rustc for build scripts
521512
// when --target is not specified.
522-
// In this test if --cfg foo is passed the build will fail.
523513
let p = project()
524514
.file(
525515
"Cargo.toml",
@@ -534,9 +524,7 @@ fn build_rustflags_build_script() {
534524
.file(
535525
"build.rs",
536526
r#"
537-
fn main() { }
538-
#[cfg(not(foo))]
539-
fn main() { }
527+
fn main() { assert!(cfg!(foo)); }
540528
"#,
541529
)
542530
.file(
@@ -737,7 +725,6 @@ fn build_rustflags_normal_source_with_target() {
737725
fn build_rustflags_build_script_with_target() {
738726
// RUSTFLAGS should not be passed to rustc for build scripts
739727
// when --target is specified.
740-
// In this test if --cfg foo is passed the build will fail.
741728
let p = project()
742729
.file(
743730
"Cargo.toml",
@@ -752,9 +739,7 @@ fn build_rustflags_build_script_with_target() {
752739
.file(
753740
"build.rs",
754741
r#"
755-
fn main() { }
756-
#[cfg(foo)]
757-
fn main() { }
742+
fn main() { assert!(!cfg!(foo)); }
758743
"#,
759744
)
760745
.file(
@@ -1041,9 +1026,7 @@ fn target_rustflags_also_for_build_scripts() {
10411026
.file(
10421027
"build.rs",
10431028
r#"
1044-
fn main() { }
1045-
#[cfg(not(foo))]
1046-
fn main() { }
1029+
fn main() { assert!(cfg!(foo)); }
10471030
"#,
10481031
)
10491032
.file(
@@ -1069,9 +1052,7 @@ fn target_rustflags_not_for_build_scripts_with_target() {
10691052
.file(
10701053
"build.rs",
10711054
r#"
1072-
fn main() { }
1073-
#[cfg(foo)]
1074-
fn main() { }
1055+
fn main() { assert!(!cfg!(foo)); }
10751056
"#,
10761057
)
10771058
.file(
@@ -1124,9 +1105,7 @@ fn build_rustflags_for_build_scripts() {
11241105
.file(
11251106
"build.rs",
11261107
r#"
1127-
fn main() { }
1128-
#[cfg(foo)]
1129-
fn main() { }
1108+
fn main() { assert!(cfg!(foo)); }
11301109
"#,
11311110
)
11321111
.file(
@@ -1139,25 +1118,26 @@ fn build_rustflags_for_build_scripts() {
11391118
.build();
11401119

11411120
// With "legacy" behavior, build.rustflags should apply to build scripts without --target
1142-
p.cargo("build")
1143-
.with_status(101)
1144-
.with_stderr_contains("[..]previous definition of the value `main` here[..]")
1145-
.run();
1121+
p.cargo("build").run();
11461122

11471123
// But should _not_ apply _with_ --target
1148-
p.cargo("build --target").arg(host).run();
1124+
p.cargo("build --target")
1125+
.arg(host)
1126+
.with_status(101)
1127+
.with_stderr_contains("[..]assertion failed[..]")
1128+
.run();
11491129

11501130
// Enabling -Ztarget-applies-to-host should not make a difference without the config setting
11511131
p.cargo("build")
11521132
.masquerade_as_nightly_cargo()
11531133
.arg("-Ztarget-applies-to-host")
1154-
.with_status(101)
1155-
.with_stderr_contains("[..]previous definition of the value `main` here[..]")
11561134
.run();
11571135
p.cargo("build --target")
11581136
.arg(host)
11591137
.masquerade_as_nightly_cargo()
11601138
.arg("-Ztarget-applies-to-host")
1139+
.with_status(101)
1140+
.with_stderr_contains("[..]assertion failed[..]")
11611141
.run();
11621142

11631143
// When set to false though, the "proper" behavior where host artifacts _only_ pick up on
@@ -1174,11 +1154,15 @@ fn build_rustflags_for_build_scripts() {
11741154
p.cargo("build")
11751155
.masquerade_as_nightly_cargo()
11761156
.arg("-Ztarget-applies-to-host")
1157+
.with_status(101)
1158+
.with_stderr_contains("[..]assertion failed[..]")
11771159
.run();
11781160
p.cargo("build --target")
11791161
.arg(host)
11801162
.masquerade_as_nightly_cargo()
11811163
.arg("-Ztarget-applies-to-host")
1164+
.with_status(101)
1165+
.with_stderr_contains("[..]assertion failed[..]")
11821166
.run();
11831167
}
11841168

0 commit comments

Comments
 (0)