3 Test whether cargo-miri works properly.
4 Assumes the `MIRI_SYSROOT` env var to be set appropriately,
5 and the working directory to contain the cargo-miri-test project.
8 import sys, subprocess, os
15 print("\nTEST FAIL: {}".format(msg))
19 args = ["cargo", "miri", cmd, "-q"]
20 if 'MIRI_TEST_TARGET' in os.environ:
21 args += ["--target", os.environ['MIRI_TEST_TARGET']]
24 def test(name, cmd, stdout_ref, stderr_ref, stdin=b'', env={}):
25 print("Testing {}...".format(name))
26 ## Call `cargo miri`, capture all output
27 p_env = os.environ.copy()
31 stdin=subprocess.PIPE,
32 stdout=subprocess.PIPE,
33 stderr=subprocess.PIPE,
36 (stdout, stderr) = p.communicate(input=stdin)
37 stdout = stdout.decode("UTF-8")
38 stderr = stderr.decode("UTF-8")
39 if p.returncode == 0 and stdout == open(stdout_ref).read() and stderr == open(stderr_ref).read():
43 print("--- BEGIN stdout ---")
45 print("--- END stdout ---")
46 print("--- BEGIN stderr ---")
48 print("--- END stderr ---")
49 fail("exit code was {}".format(p.returncode))
51 def test_cargo_miri_run():
52 test("`cargo miri run` (no isolation)",
54 "run.default.stdout.ref", "run.default.stderr.ref",
57 'MIRIFLAGS': "-Zmiri-disable-isolation",
58 'MIRITESTVAR': "wrongval", # make sure the build.rs value takes precedence
61 test("`cargo miri run` (with arguments and target)",
62 cargo_miri("run") + ["--bin", "cargo-miri-test", "--", "hello world", '"hello world"'],
63 "run.args.stdout.ref", "run.args.stderr.ref",
65 test("`cargo miri run` (subcrate, no ioslation)",
66 cargo_miri("run") + ["-p", "subcrate"],
67 "run.subcrate.stdout.ref", "run.subcrate.stderr.ref",
68 env={'MIRIFLAGS': "-Zmiri-disable-isolation"},
71 def test_cargo_miri_test():
72 # rustdoc is not run on foreign targets
73 is_foreign = 'MIRI_TEST_TARGET' in os.environ
74 rustdoc_ref = "test.stderr-empty.ref" if is_foreign else "test.stderr-rustdoc.ref"
76 test("`cargo miri test`",
78 "test.default.stdout.ref", rustdoc_ref,
79 env={'MIRIFLAGS': "-Zmiri-seed=feed"},
81 test("`cargo miri test` (no isolation)",
83 "test.default.stdout.ref", rustdoc_ref,
84 env={'MIRIFLAGS': "-Zmiri-disable-isolation"},
86 test("`cargo miri test` (raw-ptr tracking)",
88 "test.default.stdout.ref", rustdoc_ref,
89 env={'MIRIFLAGS': "-Zmiri-track-raw-pointers"},
91 test("`cargo miri test` (with filter)",
92 cargo_miri("test") + ["--", "--format=pretty", "le1"],
93 "test.filter.stdout.ref", rustdoc_ref,
95 test("`cargo miri test` (test target)",
96 cargo_miri("test") + ["--test", "test", "--", "--format=pretty"],
97 "test.test-target.stdout.ref", "test.stderr-empty.ref",
99 test("`cargo miri test` (bin target)",
100 cargo_miri("test") + ["--bin", "cargo-miri-test", "--", "--format=pretty"],
101 "test.bin-target.stdout.ref", "test.stderr-empty.ref",
103 test("`cargo miri test` (subcrate, no isolation)",
104 cargo_miri("test") + ["-p", "subcrate"],
105 "test.subcrate.stdout.ref", "test.stderr-empty.ref",
106 env={'MIRIFLAGS': "-Zmiri-disable-isolation"},
109 os.chdir(os.path.dirname(os.path.realpath(__file__)))
110 os.environ["RUST_TEST_NOCAPTURE"] = "0" # this affects test output, so make sure it is not set
112 target_str = " for target {}".format(os.environ['MIRI_TEST_TARGET']) if 'MIRI_TEST_TARGET' in os.environ else ""
113 print(CGREEN + CBOLD + "## Running `cargo miri` tests{}".format(target_str) + CEND)
115 if not 'MIRI_SYSROOT' in os.environ:
116 # Make sure we got a working sysroot.
117 # (If the sysroot gets built later when output is compared, that leads to test failures.)
118 subprocess.run(cargo_miri("setup"), check=True)
119 test_cargo_miri_run()
120 test_cargo_miri_test()
122 print("\nTEST SUCCESSFUL!")