]> git.lizzy.rs Git - rust.git/blob - test-cargo-miri/run-test.py
877a2a5706196e3d9864c6600aa21b4c0f8c0dba
[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, stdin=b'', env={}):
25     print("==> Testing `{}` <==".format(name))
26     ## Call `cargo miri`, capture all output
27     p_env = os.environ.copy()
28     p_env.update(env)
29     p = subprocess.Popen(
30         cmd,
31         stdin=subprocess.PIPE,
32         stdout=subprocess.PIPE,
33         stderr=subprocess.PIPE,
34         env=p_env,
35     )
36     (stdout, stderr) = p.communicate(input=stdin)
37     stdout = stdout.decode("UTF-8")
38     stderr = stderr.decode("UTF-8")
39     # Show output
40     print("=> captured stdout <=")
41     print(stdout, end="")
42     print("=> captured stderr <=")
43     print(stderr, end="")
44     # Test for failures
45     if p.returncode != 0:
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")
51
52 def test_cargo_miri_run():
53     test("cargo miri run",
54         cargo_miri("run"),
55         "stdout.ref", "stderr.ref",
56         stdin=b'12\n21\n',
57         env={'MIRIFLAGS': "-Zmiri-disable-isolation"},
58     )
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"
62     )
63
64 def test_cargo_miri_test():
65     test("cargo miri test",
66         cargo_miri("test"),
67         "test.stdout.ref", "test.stderr.ref",
68         env={'MIRIFLAGS': "-Zmiri-seed=feed"},
69     )
70     test("cargo miri test (with filter)",
71         cargo_miri("test") + ["--", "--format=pretty", "le1"],
72         "test.stdout.ref2", "test.stderr.ref"
73     )
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"},
78     )
79     test("cargo miri test (test target)",
80         cargo_miri("test") + ["--test", "test", "--", "--format=pretty"],
81         "test.stdout.ref4", "test.stderr.ref"
82     )
83     test("cargo miri test (bin target)",
84         cargo_miri("test") + ["--bin", "cargo-miri-test", "--", "--format=pretty"],
85         "test.stdout.ref5", "test.stderr.ref"
86     )
87
88 os.chdir(os.path.dirname(os.path.realpath(__file__)))
89
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)
92
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)
97 test_cargo_miri_run()
98 test_cargo_miri_test()
99
100 print("\nTEST SUCCESSFUL!")
101 sys.exit(0)