Skip to content

Commit 719eb59

Browse files
author
Jon Gjengset
committed
Add tests for rustc context build script env vars
1 parent 44491ee commit 719eb59

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

tests/testsuite/build_script.rs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use cargo_test_support::compare::assert_match_exact;
44
use cargo_test_support::paths::CargoPathExt;
55
use cargo_test_support::registry::Package;
6+
use cargo_test_support::tools;
67
use cargo_test_support::{basic_manifest, cross_compile, is_coarse_mtime, project};
78
use cargo_test_support::{rustc_host, sleep_ms, slow_cpu_multiplier, symlink_supported};
89
use cargo_util::paths::remove_dir_all;
@@ -115,7 +116,12 @@ fn custom_build_env_vars() {
115116
let rustdoc = env::var("RUSTDOC").unwrap();
116117
assert_eq!(rustdoc, "rustdoc");
117118
119+
assert!(env::var("RUSTC_WRAPPER").is_err());
120+
118121
assert!(env::var("RUSTC_LINKER").is_err());
122+
123+
let rustflags = env::var("CARGO_RUSTFLAGS").unwrap();
124+
assert_eq!(rustflags, "");
119125
}}
120126
"#,
121127
p.root()
@@ -130,6 +136,82 @@ fn custom_build_env_vars() {
130136
p.cargo("build --features bar_feat").run();
131137
}
132138

139+
#[cargo_test]
140+
fn custom_build_env_var_rustflags() {
141+
let rustflags = "--cfg=special";
142+
let rustflags_alt = "--cfg=notspecial";
143+
let p = project()
144+
.file(
145+
".cargo/config",
146+
&format!(
147+
r#"
148+
[build]
149+
rustflags = ["{}"]
150+
"#,
151+
rustflags
152+
),
153+
)
154+
.file(
155+
"build.rs",
156+
&format!(
157+
r#"
158+
use std::env;
159+
160+
fn main() {{
161+
// Static assertion that exactly one of the cfg paths is always taken.
162+
let x;
163+
#[cfg(special)]
164+
{{ assert_eq!(env::var("CARGO_RUSTFLAGS").unwrap(), "{}"); x = String::new(); }}
165+
#[cfg(notspecial)]
166+
{{ assert_eq!(env::var("CARGO_RUSTFLAGS").unwrap(), "{}"); x = String::new(); }}
167+
let _ = x;
168+
}}
169+
"#,
170+
rustflags, rustflags_alt,
171+
),
172+
)
173+
.file("src/lib.rs", "")
174+
.build();
175+
176+
p.cargo("check").run();
177+
178+
// RUSTFLAGS overrides build.rustflags, so --cfg=special shouldn't be passed
179+
p.cargo("check").env("RUSTFLAGS", rustflags_alt).run();
180+
}
181+
182+
#[cargo_test]
183+
fn custom_build_env_var_rustc_wrapper() {
184+
let wrapper = tools::echo_wrapper();
185+
let p = project()
186+
.file(
187+
".cargo/config",
188+
&format!(
189+
r#"
190+
[build]
191+
rustc-wrapper = "{}"
192+
"#,
193+
wrapper.display()
194+
),
195+
)
196+
.file(
197+
"build.rs",
198+
&format!(
199+
r#"
200+
use std::env;
201+
202+
fn main() {{
203+
assert!(env::var("CARGO_RUSTC_WRAPPER").unwrap().ends_with("{}"));
204+
}}
205+
"#,
206+
wrapper.display()
207+
),
208+
)
209+
.file("src/lib.rs", "")
210+
.build();
211+
212+
p.cargo("check").run();
213+
}
214+
133215
#[cargo_test]
134216
fn custom_build_env_var_rustc_linker() {
135217
if cross_compile::disabled() {

0 commit comments

Comments
 (0)