Skip to content

Commit 113a335

Browse files
committed
test propagating env vars from build.rs to binary
1 parent 33c6696 commit 113a335

File tree

4 files changed

+13
-3
lines changed

4 files changed

+13
-3
lines changed

cargo-miri/bin.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,9 @@ fn phase_cargo_runner(binary: &Path, binary_args: env::Args) {
612612
let info: CrateRunInfo = serde_json::from_reader(file)
613613
.unwrap_or_else(|_| show_error(format!("File {:?} does not contain valid JSON", binary)));
614614

615-
// Set missing env vars.
615+
// Set missing env vars. Looks like `build.rs` vars are still set at run-time, but
616+
// `CARGO_BIN_EXE_*` are not. This means we can give the run-time environment precedence,
617+
// to rather do too little than too much.
616618
for (name, val) in info.env {
617619
if env::var_os(&name).is_none() {
618620
env::set_var(name, val);

test-cargo-miri/build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ fn not_in_miri() -> i32 {
1212
fn main() {
1313
not_in_miri();
1414
println!("cargo:rerun-if-changed=build.rs");
15+
println!("cargo:rerun-if-env-changed=MIRITESTVAR");
16+
println!("cargo:rustc-env=MIRITESTVAR=testval");
1517
}

test-cargo-miri/run-test.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,14 @@ def test(name, cmd, stdout_ref, stderr_ref, stdin=b'', env={}):
5050
fail("stderr does not match reference")
5151

5252
def test_cargo_miri_run():
53-
test("`cargo miri run`",
53+
test("`cargo miri run` (without isolation)",
5454
cargo_miri("run"),
5555
"stdout.ref1", "stderr.ref1",
5656
stdin=b'12\n21\n',
57-
env={'MIRIFLAGS': "-Zmiri-disable-isolation"},
57+
env={
58+
'MIRIFLAGS': "-Zmiri-disable-isolation",
59+
'MIRITESTVAR': "wrongval", # make sure the build.rs value takes precedence
60+
},
5861
)
5962
test("`cargo miri run` (with arguments and target)",
6063
cargo_miri("run") + ["--bin", "cargo-miri-test", "--", "hello world", '"hello world"'],

test-cargo-miri/src/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ use byteorder::{BigEndian, ByteOrder};
33
use std::io::{self, BufRead};
44

55
fn main() {
6+
// Check env var set by `build.rs`.
7+
assert_eq!(env!("MIRITESTVAR"), "testval");
8+
69
// Exercise external crate, printing to stdout.
710
let buf = &[1,2,3,4];
811
let n = <BigEndian as ByteOrder>::read_u32(buf);

0 commit comments

Comments
 (0)