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")
40 print("=> captured stdout <=")
42 print("=> captured stderr <=")
46 fail("Non-zero exit status")
47 if stdout != open(stdout_ref).read():
48 fail("stdout does not match reference")
49 if stderr != open(stderr_ref).read():
50 fail("stderr does not match reference")
52 def test_cargo_miri_run():
53 test("cargo miri run",
55 "stdout.ref", "stderr.ref",
57 env={'MIRIFLAGS': "-Zmiri-disable-isolation"},
59 test("cargo miri run (with arguments and target)",
60 cargo_miri("run") + ["--bin", "cargo-miri-test", "--", "hello world", '"hello world"'],
61 "stdout.ref2", "stderr.ref2"
64 def test_cargo_miri_test():
65 test("cargo miri test",
67 "test.stdout.ref", "test.stderr.ref",
68 env={'MIRIFLAGS': "-Zmiri-seed=feed"},
70 test("cargo miri test (with filter)",
71 cargo_miri("test") + ["--", "--format=pretty", "le1"],
72 "test.stdout.ref2", "test.stderr.ref"
74 test("cargo miri test (without isolation)",
75 cargo_miri("test") + ["--", "--format=pretty", "num_cpus"],
76 "test.stdout.ref3", "test.stderr.ref",
77 env={'MIRIFLAGS': "-Zmiri-disable-isolation"},
79 test("cargo miri test (test target)",
80 cargo_miri("test") + ["--test", "test", "--", "--format=pretty"],
81 "test.stdout.ref4", "test.stderr.ref"
83 test("cargo miri test (bin target)",
84 cargo_miri("test") + ["--bin", "cargo-miri-test", "--", "--format=pretty"],
85 "test.stdout.ref5", "test.stderr.ref"
88 os.chdir(os.path.dirname(os.path.realpath(__file__)))
90 target_str = " for target {}".format(os.environ['MIRI_TEST_TARGET']) if 'MIRI_TEST_TARGET' in os.environ else ""
91 print(CGREEN + CBOLD + "## Running `cargo miri` tests{}".format(target_str) + CEND)
93 if not 'MIRI_SYSROOT' in os.environ:
94 # Make sure we got a working sysroot.
95 # (If the sysroot gets built later when output is compared, that leads to test failures.)
96 subprocess.run(cargo_miri("setup"), check=True)
98 test_cargo_miri_test()
100 print("\nTEST SUCCESSFUL!")