]> git.lizzy.rs Git - rust.git/blob - test-cargo-miri/run-test.py
panicing now works with -Zmiri-track-raw-pointers
[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     if p.returncode == 0 and stdout == open(stdout_ref).read() and stderr == open(stderr_ref).read():
40         # All good!
41         return
42     # Show output
43     print("--- BEGIN stdout ---")
44     print(stdout, end="")
45     print("--- END stdout ---")
46     print("--- BEGIN stderr ---")
47     print(stderr, end="")
48     print("--- END stderr ---")
49     fail("exit code was {}".format(p.returncode))
50
51 def test_cargo_miri_run():
52     test("`cargo miri run` (no isolation)",
53         cargo_miri("run"),
54         "run.default.stdout.ref", "run.default.stderr.ref",
55         stdin=b'12\n21\n',
56         env={
57             'MIRIFLAGS': "-Zmiri-disable-isolation",
58             'MIRITESTVAR': "wrongval", # make sure the build.rs value takes precedence
59         },
60     )
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",
64     )
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"},
69     )
70
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"
75
76     test("`cargo miri test`",
77         cargo_miri("test"),
78         "test.default.stdout.ref", rustdoc_ref,
79         env={'MIRIFLAGS': "-Zmiri-seed=feed"},
80     )
81     test("`cargo miri test` (no isolation)",
82         cargo_miri("test"),
83         "test.default.stdout.ref", rustdoc_ref,
84         env={'MIRIFLAGS': "-Zmiri-disable-isolation"},
85     )
86     test("`cargo miri test` (raw-ptr tracking)",
87         cargo_miri("test"),
88         "test.default.stdout.ref", rustdoc_ref,
89         env={'MIRIFLAGS': "-Zmiri-track-raw-pointers"},
90     )
91     test("`cargo miri test` (with filter)",
92         cargo_miri("test") + ["--", "--format=pretty", "le1"],
93         "test.filter.stdout.ref", rustdoc_ref,
94     )
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",
98     )
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",
102     )
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"},
107     )
108
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
111
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)
114
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()
121
122 print("\nTEST SUCCESSFUL!")
123 sys.exit(0)