Skip to content

Commit

Permalink
forward build-time env vars to binary, and test that we do
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Sep 9, 2020
1 parent f56cd06 commit 1f42b4b
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 17 deletions.
27 changes: 17 additions & 10 deletions cargo-miri/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,16 @@ path = "lib.rs"
}

fn phase_cargo_miri(mut args: env::Args) {
// Check for version and help flags even when invoked as `cargo-miri`.
if has_arg_flag("--help") || has_arg_flag("-h") {
show_help();
return;
}
if has_arg_flag("--version") || has_arg_flag("-V") {
show_version();
return;
}

// Require a subcommand before any flags.
// We cannot know which of those flags take arguments and which do not,
// so we cannot detect subcommands later.
Expand Down Expand Up @@ -567,6 +577,13 @@ fn phase_cargo_runner(binary: &str, 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.
for (name, val) in info.env {
if env::var_os(&name).is_none() {
env::set_var(name, val);
}
}

let mut cmd = miri();
// Forward rustc arguments. We need to patch "--extern" filenames because
// we forced a check-only build without cargo knowing about that: replace `.rlib` suffix by `.rmeta`.
Expand Down Expand Up @@ -610,16 +627,6 @@ fn phase_cargo_runner(binary: &str, binary_args: env::Args) {
}

fn main() {
// Check for version and help flags even when invoked as `cargo-miri`.
if has_arg_flag("--help") || has_arg_flag("-h") {
show_help();
return;
}
if has_arg_flag("--version") || has_arg_flag("-V") {
show_version();
return;
}

// Rustc does not support non-UTF-8 arguments so we make no attempt either.
// (We do support non-UTF-8 environment variables though.)
let mut args = std::env::args();
Expand Down
6 changes: 3 additions & 3 deletions test-cargo-miri/test.stdout.ref
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ running 1 test
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out


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

2 changes: 1 addition & 1 deletion test-cargo-miri/test.stdout.ref2
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 1 filtered out
running 1 test
test simple1 ... ok

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

2 changes: 1 addition & 1 deletion test-cargo-miri/test.stdout.ref3
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 1 filtered out
running 1 test
test num_cpus ... ok

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

5 changes: 3 additions & 2 deletions test-cargo-miri/test.stdout.ref4
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

running 7 tests
running 8 tests
test cargo_env ... ok
test do_panic ... ok
test does_not_work_on_miri ... ignored
test entropy_rng ... ok
Expand All @@ -8,5 +9,5 @@ test num_cpus ... ok
test simple1 ... ok
test simple2 ... ok

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

5 changes: 5 additions & 0 deletions test-cargo-miri/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ fn num_cpus() {
assert_eq!(num_cpus::get(), 1);
}

#[test]
fn cargo_env() {
assert_eq!(env!("CARGO_PKG_NAME"), "cargo-miri-test");
env!("CARGO_BIN_EXE_cargo-miri-test"); // Asserts that this exists.
}

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

0 comments on commit 1f42b4b

Please sign in to comment.