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