Skip to content

Commit a6cd89f

Browse files
committed
forward build-time env vars to binary, and test that we do
1 parent f56cd06 commit a6cd89f

File tree

6 files changed

+29
-16
lines changed

6 files changed

+29
-16
lines changed

cargo-miri/bin.rs

+17-10
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,16 @@ path = "lib.rs"
372372
}
373373

374374
fn phase_cargo_miri(mut args: env::Args) {
375+
// Check for version and help flags even when invoked as `cargo-miri`.
376+
if has_arg_flag("--help") || has_arg_flag("-h") {
377+
show_help();
378+
return;
379+
}
380+
if has_arg_flag("--version") || has_arg_flag("-V") {
381+
show_version();
382+
return;
383+
}
384+
375385
// Require a subcommand before any flags.
376386
// We cannot know which of those flags take arguments and which do not,
377387
// so we cannot detect subcommands later.
@@ -567,6 +577,13 @@ fn phase_cargo_runner(binary: &str, binary_args: env::Args) {
567577
let info: CrateRunInfo = serde_json::from_reader(file)
568578
.unwrap_or_else(|_| show_error(format!("File {:?} does not contain valid JSON", binary)));
569579

580+
// Set missing env vars.
581+
for (name, val) in info.env {
582+
if env::var_os(&name).is_none() {
583+
env::set_var(name, val);
584+
}
585+
}
586+
570587
let mut cmd = miri();
571588
// Forward rustc arguments. We need to patch "--extern" filenames because
572589
// we forced a check-only build without cargo knowing about that: replace `.rlib` suffix by `.rmeta`.
@@ -610,16 +627,6 @@ fn phase_cargo_runner(binary: &str, binary_args: env::Args) {
610627
}
611628

612629
fn main() {
613-
// Check for version and help flags even when invoked as `cargo-miri`.
614-
if has_arg_flag("--help") || has_arg_flag("-h") {
615-
show_help();
616-
return;
617-
}
618-
if has_arg_flag("--version") || has_arg_flag("-V") {
619-
show_version();
620-
return;
621-
}
622-
623630
// Rustc does not support non-UTF-8 arguments so we make no attempt either.
624631
// (We do support non-UTF-8 environment variables though.)
625632
let mut args = std::env::args();

test-cargo-miri/test.stdout.ref

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ running 1 test
44
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
55

66

7-
running 7 tests
8-
.i.....
9-
test result: ok. 6 passed; 0 failed; 1 ignored; 0 measured; 0 filtered out
7+
running 8 tests
8+
..i.....
9+
test result: ok. 7 passed; 0 failed; 1 ignored; 0 measured; 0 filtered out
1010

test-cargo-miri/test.stdout.ref2

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 1 filtered out
77
running 1 test
88
test simple1 ... ok
99

10-
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 6 filtered out
10+
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 7 filtered out
1111

test-cargo-miri/test.stdout.ref3

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 1 filtered out
77
running 1 test
88
test num_cpus ... ok
99

10-
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 6 filtered out
10+
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 7 filtered out
1111

test-cargo-miri/test.stdout.ref4

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

2-
running 7 tests
2+
running 8 tests
3+
test cargo_env ... ok
34
test do_panic ... ok
45
test does_not_work_on_miri ... ignored
56
test entropy_rng ... ok

test-cargo-miri/tests/test.rs

+5
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ fn num_cpus() {
4242
assert_eq!(num_cpus::get(), 1);
4343
}
4444

45+
#[test]
46+
fn cargo_env() {
47+
assert_eq!(env!("CARGO_PKG_NAME"), "cargo-miri-test");
48+
env!("CARGO_BIN_EXE_cargo-miri-test"); // Asserts that this exists.
49+
}
4550

4651
// FIXME: Remove this `cfg` once we fix https://github.com/rust-lang/miri/issues/1059.
4752
// We cfg-gate the `should_panic` attribute and the `panic!` itself, so that the test

0 commit comments

Comments
 (0)