From b244a2ddaa5bbc7eb0b50c018e0e578c0b230283 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 12 Sep 2020 13:52:05 +0200 Subject: [PATCH] make sure subcrate tests have the right cwd --- README.md | 4 ++++ cargo-miri/bin.rs | 10 +++++++++- src/bin/miri.rs | 5 +++++ test-cargo-miri/Cargo.toml | 4 ---- test-cargo-miri/run-test.py | 29 +++++++++++++++++------------ test-cargo-miri/subcrate/Cargo.toml | 5 +++++ test-cargo-miri/subcrate/test.rs | 11 +++++++++++ test-cargo-miri/test.stdout.ref1 | 1 - test-cargo-miri/test.stdout.ref2 | 1 - test-cargo-miri/test.stdout.ref3 | 17 +++++++++-------- test-cargo-miri/test.stdout.ref4 | 13 +++---------- test-cargo-miri/test.stdout.ref5 | 6 +++--- test-cargo-miri/tests/no-harness.rs | 3 --- 13 files changed, 66 insertions(+), 43 deletions(-) create mode 100644 test-cargo-miri/subcrate/test.rs delete mode 100644 test-cargo-miri/tests/no-harness.rs diff --git a/README.md b/README.md index 67161974b18..6654de10abf 100644 --- a/README.md +++ b/README.md @@ -244,6 +244,10 @@ different Miri binaries, and as such worth documenting: * `MIRI_BE_RUSTC` when set to any value tells the Miri driver to actually not interpret the code but compile it like rustc would. This is useful to be sure that the compiled `rlib`s are compatible with Miri. +* `MIRI_CWD` when set to any value tells the Miri driver to change to the given + directory after loading all the source files, but before commencing + interpretation. This is useful if the interpreted program wants a different + working directory at run-time than at build-time. ## Miri `extern` functions diff --git a/cargo-miri/bin.rs b/cargo-miri/bin.rs index d3594089719..a4898e1a2cf 100644 --- a/cargo-miri/bin.rs +++ b/cargo-miri/bin.rs @@ -43,6 +43,8 @@ struct CrateRunInfo { args: Vec, /// The environment. env: Vec<(OsString, OsString)>, + /// The current working directory. + current_dir: OsString, } impl CrateRunInfo { @@ -50,7 +52,8 @@ impl CrateRunInfo { fn collect(args: env::Args) -> Self { let args = args.collect(); let env = env::vars_os().collect(); - CrateRunInfo { args, env } + let current_dir = env::current_dir().unwrap().into_os_string(); + CrateRunInfo { args, env, current_dir } } fn store(&self, filename: &Path) { @@ -672,6 +675,11 @@ fn phase_cargo_runner(binary: &Path, binary_args: env::Args) { cmd.arg("--"); cmd.args(binary_args); + // Make sure we use the build-time working directory for interpreting Miri/rustc arguments. + // But then we need to switch to the run-time one, which we instruct Miri do do by setting `MIRI_CWD`. + cmd.current_dir(info.current_dir); + cmd.env("MIRI_CWD", env::current_dir().unwrap()); + // Run it. if verbose { eprintln!("[cargo-miri runner] {:?}", cmd); diff --git a/src/bin/miri.rs b/src/bin/miri.rs index 13470204752..4363f9a150a 100644 --- a/src/bin/miri.rs +++ b/src/bin/miri.rs @@ -45,6 +45,11 @@ impl rustc_driver::Callbacks for MiriCompilerCalls { // Add filename to `miri` arguments. config.args.insert(0, compiler.input().filestem().to_string()); + // Adjust working directory for interpretation. + if let Some(cwd) = env::var_os("MIRI_CWD") { + env::set_current_dir(cwd).unwrap(); + } + if let Some(return_code) = miri::eval_main(tcx, entry_def_id.to_def_id(), config) { std::process::exit( i32::try_from(return_code).expect("Return value was too large!"), diff --git a/test-cargo-miri/Cargo.toml b/test-cargo-miri/Cargo.toml index 131e1849853..4900ce9675d 100644 --- a/test-cargo-miri/Cargo.toml +++ b/test-cargo-miri/Cargo.toml @@ -16,7 +16,3 @@ num_cpus = "1.10.1" [lib] test = false # test that this is respected (will show in the output) - -[[test]] -name = "no-harness" -harness = false diff --git a/test-cargo-miri/run-test.py b/test-cargo-miri/run-test.py index 430c8a75830..78b10d1f2bf 100755 --- a/test-cargo-miri/run-test.py +++ b/test-cargo-miri/run-test.py @@ -50,7 +50,7 @@ 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` (without isolation)", + test("`cargo miri run` (no isolation)", cargo_miri("run"), "stdout.ref1", "stderr.ref1", stdin=b'12\n21\n', @@ -61,9 +61,9 @@ def test_cargo_miri_run(): ) test("`cargo miri run` (with arguments and target)", cargo_miri("run") + ["--bin", "cargo-miri-test", "--", "hello world", '"hello world"'], - "stdout.ref2", "stderr.ref2" + "stdout.ref2", "stderr.ref2", ) - test("`cargo miri run` (subcrate)", + test("`cargo miri run` (subcrate, no ioslation)", cargo_miri("run") + ["-p", "subcrate"], "stdout.ref3", "stderr.ref3", env={'MIRIFLAGS': "-Zmiri-disable-isolation"}, @@ -76,25 +76,30 @@ def test_cargo_miri_test(): test("`cargo miri test`", cargo_miri("test"), - "test.stdout.ref1",rustdoc_ref, + "test.stdout.ref1", rustdoc_ref, env={'MIRIFLAGS': "-Zmiri-seed=feed"}, ) + test("`cargo miri test` (no isolation)", + cargo_miri("test"), + "test.stdout.ref1", rustdoc_ref, + env={'MIRIFLAGS': "-Zmiri-disable-isolation"}, + ) test("`cargo miri test` (with filter)", cargo_miri("test") + ["--", "--format=pretty", "le1"], - "test.stdout.ref2", rustdoc_ref - ) - test("`cargo miri test` (without isolation)", - cargo_miri("test") + ["--", "--format=pretty", "num_cpus"], - "test.stdout.ref3", rustdoc_ref, - env={'MIRIFLAGS': "-Zmiri-disable-isolation"}, + "test.stdout.ref2", rustdoc_ref, ) test("`cargo miri test` (test target)", cargo_miri("test") + ["--test", "test", "--", "--format=pretty"], - "test.stdout.ref4", "test.stderr.ref2" + "test.stdout.ref3", "test.stderr.ref2", ) test("`cargo miri test` (bin target)", cargo_miri("test") + ["--bin", "cargo-miri-test", "--", "--format=pretty"], - "test.stdout.ref5", "test.stderr.ref2" + "test.stdout.ref4", "test.stderr.ref2", + ) + test("`cargo miri test` (subcrate)", + cargo_miri("test") + ["-p", "subcrate"], + "test.stdout.ref5", "test.stderr.ref2", + env={'MIRIFLAGS': "-Zmiri-disable-isolation"}, ) os.chdir(os.path.dirname(os.path.realpath(__file__))) diff --git a/test-cargo-miri/subcrate/Cargo.toml b/test-cargo-miri/subcrate/Cargo.toml index 13e9aa4c1af..78552e6aedf 100644 --- a/test-cargo-miri/subcrate/Cargo.toml +++ b/test-cargo-miri/subcrate/Cargo.toml @@ -7,3 +7,8 @@ edition = "2018" [[bin]] name = "subcrate" path = "main.rs" + +[[test]] +name = "subtest" +path = "test.rs" +harness = false diff --git a/test-cargo-miri/subcrate/test.rs b/test-cargo-miri/subcrate/test.rs new file mode 100644 index 00000000000..16c63411ce8 --- /dev/null +++ b/test-cargo-miri/subcrate/test.rs @@ -0,0 +1,11 @@ +use std::env; +use std::path::PathBuf; + +fn main() { + println!("subcrate testing"); + + let env_dir = env::current_dir().unwrap(); + let crate_dir = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap()); + // CWD should be crate root. + assert_eq!(env_dir, crate_dir); +} diff --git a/test-cargo-miri/test.stdout.ref1 b/test-cargo-miri/test.stdout.ref1 index 76144513c54..1eb18fe8876 100644 --- a/test-cargo-miri/test.stdout.ref1 +++ b/test-cargo-miri/test.stdout.ref1 @@ -3,7 +3,6 @@ running 1 test . test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out -no-harness test running 8 tests ..i..... diff --git a/test-cargo-miri/test.stdout.ref2 b/test-cargo-miri/test.stdout.ref2 index 1264c6da7ff..d426bdf6db6 100644 --- a/test-cargo-miri/test.stdout.ref2 +++ b/test-cargo-miri/test.stdout.ref2 @@ -3,7 +3,6 @@ running 0 tests test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 1 filtered out -no-harness test running 1 test test simple1 ... ok diff --git a/test-cargo-miri/test.stdout.ref3 b/test-cargo-miri/test.stdout.ref3 index a5edee2c5f1..32bbcf9bf27 100644 --- a/test-cargo-miri/test.stdout.ref3 +++ b/test-cargo-miri/test.stdout.ref3 @@ -1,12 +1,13 @@ -running 0 tests - -test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 1 filtered out - -no-harness test - -running 1 test +running 8 tests +test cargo_env ... ok +test do_panic ... ok +test does_not_work_on_miri ... ignored +test entropy_rng ... ok +test fail_index_check ... ok test num_cpus ... ok +test simple1 ... ok +test simple2 ... ok -test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 7 filtered out +test result: ok. 7 passed; 0 failed; 1 ignored; 0 measured; 0 filtered out diff --git a/test-cargo-miri/test.stdout.ref4 b/test-cargo-miri/test.stdout.ref4 index 32bbcf9bf27..4caa30a7f0e 100644 --- a/test-cargo-miri/test.stdout.ref4 +++ b/test-cargo-miri/test.stdout.ref4 @@ -1,13 +1,6 @@ -running 8 tests -test cargo_env ... ok -test do_panic ... ok -test does_not_work_on_miri ... ignored -test entropy_rng ... ok -test fail_index_check ... ok -test num_cpus ... ok -test simple1 ... ok -test simple2 ... ok +running 1 test +test test::rng ... ok -test result: ok. 7 passed; 0 failed; 1 ignored; 0 measured; 0 filtered out +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out diff --git a/test-cargo-miri/test.stdout.ref5 b/test-cargo-miri/test.stdout.ref5 index 4caa30a7f0e..67e5c7f8e92 100644 --- a/test-cargo-miri/test.stdout.ref5 +++ b/test-cargo-miri/test.stdout.ref5 @@ -1,6 +1,6 @@ -running 1 test -test test::rng ... ok +running 0 tests -test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out +test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out +subcrate testing diff --git a/test-cargo-miri/tests/no-harness.rs b/test-cargo-miri/tests/no-harness.rs deleted file mode 100644 index 8d1c5c34626..00000000000 --- a/test-cargo-miri/tests/no-harness.rs +++ /dev/null @@ -1,3 +0,0 @@ -fn main() { - println!("no-harness test"); -}