test propagating env vars from build.rs to binary

This commit is contained in:
Ralf Jung 2020-09-12 13:10:23 +02:00
parent 33c669679e
commit 113a335c3e
4 changed files with 13 additions and 3 deletions

View File

@ -612,7 +612,9 @@ fn phase_cargo_runner(binary: &Path, binary_args: env::Args) {
let info: CrateRunInfo = serde_json::from_reader(file)
.unwrap_or_else(|_| show_error(format!("File {:?} does not contain valid JSON", binary)));
// Set missing env vars.
// Set missing env vars. Looks like `build.rs` vars are still set at run-time, but
// `CARGO_BIN_EXE_*` are not. This means we can give the run-time environment precedence,
// to rather do too little than too much.
for (name, val) in info.env {
if env::var_os(&name).is_none() {
env::set_var(name, val);

View File

@ -12,4 +12,6 @@ fn not_in_miri() -> i32 {
fn main() {
not_in_miri();
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-env-changed=MIRITESTVAR");
println!("cargo:rustc-env=MIRITESTVAR=testval");
}

View File

@ -50,11 +50,14 @@ def test(name, cmd, stdout_ref, stderr_ref, stdin=b'', env={}):
fail("stderr does not match reference")
def test_cargo_miri_run():
test("`cargo miri run`",
test("`cargo miri run` (without isolation)",
cargo_miri("run"),
"stdout.ref1", "stderr.ref1",
stdin=b'12\n21\n',
env={'MIRIFLAGS': "-Zmiri-disable-isolation"},
env={
'MIRIFLAGS': "-Zmiri-disable-isolation",
'MIRITESTVAR': "wrongval", # make sure the build.rs value takes precedence
},
)
test("`cargo miri run` (with arguments and target)",
cargo_miri("run") + ["--bin", "cargo-miri-test", "--", "hello world", '"hello world"'],

View File

@ -3,6 +3,9 @@ use byteorder::{BigEndian, ByteOrder};
use std::io::{self, BufRead};
fn main() {
// Check env var set by `build.rs`.
assert_eq!(env!("MIRITESTVAR"), "testval");
// Exercise external crate, printing to stdout.
let buf = &[1,2,3,4];
let n = <BigEndian as ByteOrder>::read_u32(buf);