Skip to content

Commit 1a78ab8

Browse files
committed
make sure subcrate tests have the right cwd
1 parent f6ae886 commit 1a78ab8

File tree

13 files changed

+66
-43
lines changed

13 files changed

+66
-43
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,10 @@ different Miri binaries, and as such worth documenting:
244244
* `MIRI_BE_RUSTC` when set to any value tells the Miri driver to actually not
245245
interpret the code but compile it like rustc would. This is useful to be sure
246246
that the compiled `rlib`s are compatible with Miri.
247+
* `MIRI_CWD` when set to any value tells the Miri driver to change to the given
248+
directory after loading all the source files, but before commencing
249+
interpretation. This is useful if the interpreted program wants a different
250+
working directory at run-time than at build-time.
247251

248252
## Miri `extern` functions
249253

cargo-miri/bin.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,17 @@ struct CrateRunInfo {
4343
args: Vec<String>,
4444
/// The environment.
4545
env: Vec<(OsString, OsString)>,
46+
/// The current working directory.
47+
current_dir: OsString,
4648
}
4749

4850
impl CrateRunInfo {
4951
/// Gather all the information we need.
5052
fn collect(args: env::Args) -> Self {
5153
let args = args.collect();
5254
let env = env::vars_os().collect();
53-
CrateRunInfo { args, env }
55+
let current_dir = env::current_dir().unwrap().into_os_string();
56+
CrateRunInfo { args, env, current_dir }
5457
}
5558

5659
fn store(&self, filename: &Path) {
@@ -669,6 +672,11 @@ fn phase_cargo_runner(binary: &Path, binary_args: env::Args) {
669672
cmd.arg("--");
670673
cmd.args(binary_args);
671674

675+
// Make sure we use the build-time working directory for interpreting Miri/rustc arguments.
676+
// But then we need to switch to the run-time one, which we instruct Miri do do by setting `MIRI_CWD`.
677+
cmd.current_dir(info.current_dir);
678+
cmd.env("MIRI_CWD", env::current_dir().unwrap());
679+
672680
// Run it.
673681
if verbose {
674682
eprintln!("[cargo-miri runner] {:?}", cmd);

src/bin/miri.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ impl rustc_driver::Callbacks for MiriCompilerCalls {
4545
// Add filename to `miri` arguments.
4646
config.args.insert(0, compiler.input().filestem().to_string());
4747

48+
// Adjust working directory for interpretation.
49+
if let Some(cwd) = env::var_os("MIRI_CWD") {
50+
env::set_current_dir(cwd).unwrap();
51+
}
52+
4853
if let Some(return_code) = miri::eval_main(tcx, entry_def_id.to_def_id(), config) {
4954
std::process::exit(
5055
i32::try_from(return_code).expect("Return value was too large!"),

test-cargo-miri/Cargo.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,3 @@ num_cpus = "1.10.1"
1616

1717
[lib]
1818
test = false # test that this is respected (will show in the output)
19-
20-
[[test]]
21-
name = "no-harness"
22-
harness = false

test-cargo-miri/run-test.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def test(name, cmd, stdout_ref, stderr_ref, stdin=b'', env={}):
5050
fail("stderr does not match reference")
5151

5252
def test_cargo_miri_run():
53-
test("`cargo miri run` (without isolation)",
53+
test("`cargo miri run` (no isolation)",
5454
cargo_miri("run"),
5555
"stdout.ref1", "stderr.ref1",
5656
stdin=b'12\n21\n',
@@ -61,9 +61,9 @@ def test_cargo_miri_run():
6161
)
6262
test("`cargo miri run` (with arguments and target)",
6363
cargo_miri("run") + ["--bin", "cargo-miri-test", "--", "hello world", '"hello world"'],
64-
"stdout.ref2", "stderr.ref2"
64+
"stdout.ref2", "stderr.ref2",
6565
)
66-
test("`cargo miri run` (subcrate)",
66+
test("`cargo miri run` (subcrate, no ioslation)",
6767
cargo_miri("run") + ["-p", "subcrate"],
6868
"stdout.ref3", "stderr.ref3",
6969
env={'MIRIFLAGS': "-Zmiri-disable-isolation"},
@@ -76,25 +76,30 @@ def test_cargo_miri_test():
7676

7777
test("`cargo miri test`",
7878
cargo_miri("test"),
79-
"test.stdout.ref1",rustdoc_ref,
79+
"test.stdout.ref1", rustdoc_ref,
8080
env={'MIRIFLAGS': "-Zmiri-seed=feed"},
8181
)
82+
test("`cargo miri test` (no isolation)",
83+
cargo_miri("test"),
84+
"test.stdout.ref1", rustdoc_ref,
85+
env={'MIRIFLAGS': "-Zmiri-disable-isolation"},
86+
)
8287
test("`cargo miri test` (with filter)",
8388
cargo_miri("test") + ["--", "--format=pretty", "le1"],
84-
"test.stdout.ref2", rustdoc_ref
85-
)
86-
test("`cargo miri test` (without isolation)",
87-
cargo_miri("test") + ["--", "--format=pretty", "num_cpus"],
88-
"test.stdout.ref3", rustdoc_ref,
89-
env={'MIRIFLAGS': "-Zmiri-disable-isolation"},
89+
"test.stdout.ref2", rustdoc_ref,
9090
)
9191
test("`cargo miri test` (test target)",
9292
cargo_miri("test") + ["--test", "test", "--", "--format=pretty"],
93-
"test.stdout.ref4", "test.stderr.ref2"
93+
"test.stdout.ref3", "test.stderr.ref2",
9494
)
9595
test("`cargo miri test` (bin target)",
9696
cargo_miri("test") + ["--bin", "cargo-miri-test", "--", "--format=pretty"],
97-
"test.stdout.ref5", "test.stderr.ref2"
97+
"test.stdout.ref4", "test.stderr.ref2",
98+
)
99+
test("`cargo miri test` (subcrate)",
100+
cargo_miri("test") + ["-p", "subcrate"],
101+
"test.stdout.ref5", "test.stderr.ref2",
102+
env={'MIRIFLAGS': "-Zmiri-disable-isolation"},
98103
)
99104

100105
os.chdir(os.path.dirname(os.path.realpath(__file__)))

test-cargo-miri/subcrate/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,8 @@ edition = "2018"
77
[[bin]]
88
name = "subcrate"
99
path = "main.rs"
10+
11+
[[test]]
12+
name = "subtest"
13+
path = "test.rs"
14+
harness = false

test-cargo-miri/subcrate/test.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use std::env;
2+
use std::path::PathBuf;
3+
4+
fn main() {
5+
println!("subcrate testing");
6+
7+
let env_dir = env::current_dir().unwrap();
8+
let crate_dir = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
9+
// CWD should be crate root.
10+
assert_eq!(env_dir, crate_dir);
11+
}

test-cargo-miri/test.stdout.ref1

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ running 1 test
33
.
44
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
55

6-
no-harness test
76

87
running 8 tests
98
..i.....

test-cargo-miri/test.stdout.ref2

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ running 0 tests
33

44
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 1 filtered out
55

6-
no-harness test
76

87
running 1 test
98
test simple1 ... ok

test-cargo-miri/test.stdout.ref3

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11

2-
running 0 tests
3-
4-
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 1 filtered out
5-
6-
no-harness test
7-
8-
running 1 test
2+
running 8 tests
3+
test cargo_env ... ok
4+
test do_panic ... ok
5+
test does_not_work_on_miri ... ignored
6+
test entropy_rng ... ok
7+
test fail_index_check ... ok
98
test num_cpus ... ok
9+
test simple1 ... ok
10+
test simple2 ... ok
1011

11-
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 7 filtered out
12+
test result: ok. 7 passed; 0 failed; 1 ignored; 0 measured; 0 filtered out
1213

0 commit comments

Comments
 (0)