make sure subcrate tests have the right cwd

This commit is contained in:
Ralf Jung 2020-09-12 13:52:05 +02:00
parent c99fb102b8
commit b244a2ddaa
13 changed files with 66 additions and 43 deletions

View File

@ -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

View File

@ -43,6 +43,8 @@ struct CrateRunInfo {
args: Vec<String>,
/// 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);

View File

@ -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!"),

View File

@ -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

View File

@ -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__)))

View File

@ -7,3 +7,8 @@ edition = "2018"
[[bin]]
name = "subcrate"
path = "main.rs"
[[test]]
name = "subtest"
path = "test.rs"
harness = false

View File

@ -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);
}

View File

@ -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.....

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -1,3 +0,0 @@
fn main() {
println!("no-harness test");
}