]> git.lizzy.rs Git - rust.git/blob - test-cargo-miri/run-test.py
a4086bcc8c99dcacc4188bd50646cc00dd07ecd8
[rust.git] / test-cargo-miri / run-test.py
1 #!/usr/bin/env python3
2 '''
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.
6 '''
7
8 import sys, subprocess, os
9
10 def fail(msg):
11     print("\nTEST FAIL: {}".format(msg))
12     sys.exit(1)
13
14 def cargo_miri(cmd):
15     args = ["cargo", "miri", cmd, "-q"]
16     if 'MIRI_TEST_TARGET' in os.environ:
17         args += ["--target", os.environ['MIRI_TEST_TARGET']]
18     return args
19
20 def test(name, cmd, stdout_ref, stderr_ref):
21     print("==> Testing `{}` <==".format(name))
22     ## Call `cargo miri`, capture all output
23     p = subprocess.Popen(
24         cmd,
25         stdout=subprocess.PIPE,
26         stderr=subprocess.PIPE
27     )
28     (stdout, stderr) = p.communicate()
29     stdout = stdout.decode("UTF-8")
30     stderr = stderr.decode("UTF-8")
31     # Show output
32     print("=> captured stdout <=")
33     print(stdout, end="")
34     print("=> captured stderr <=")
35     print(stderr, end="")
36     # Test for failures
37     if p.returncode != 0:
38         fail("Non-zero exit status")
39     if stdout != open(stdout_ref).read():
40         fail("stdout does not match reference")
41     if stderr != open(stderr_ref).read():
42         fail("stderr does not match reference")
43
44 def test_cargo_miri_run():
45     test("cargo miri run",
46         cargo_miri("run"),
47         "stdout.ref", "stderr.ref"
48     )
49     test("cargo miri run (with arguments)",
50         cargo_miri("run") + ["--", "--", "hello world", '"hello world"'],
51         "stdout.ref", "stderr.ref2"
52     )
53
54 def test_cargo_miri_test():
55     test("cargo miri test",
56         cargo_miri("test") + ["--", "-Zmiri-seed=feed"],
57         "test.stdout.ref", "test.stderr.ref"
58     )
59     test("cargo miri test (with filter)",
60         cargo_miri("test") + ["--", "--", "le1"],
61         "test.stdout.ref2", "test.stderr.ref"
62     )
63     test("cargo miri test (without isolation)",
64         cargo_miri("test") + ["--", "-Zmiri-disable-isolation", "--", "num_cpus"],
65         "test.stdout.ref3", "test.stderr.ref"
66     )
67
68 os.chdir(os.path.dirname(os.path.realpath(__file__)))
69
70 if not 'MIRI_SYSROOT' in os.environ:
71     # Make sure we got a working sysroot.
72     # (If the sysroot gets built later when output is compared, that leads to test failures.)
73     subprocess.run(cargo_miri("setup"), check=True)
74 test_cargo_miri_run()
75 test_cargo_miri_test()
76
77 print("\nTEST SUCCESSFUL!")
78 sys.exit(0)