]> git.lizzy.rs Git - rust.git/blob - test-cargo-miri/run-test.py
Auto merge of #1842 - hyd-dev:target-dir, r=RalfJung
[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, re
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, quiet = True):
19     args = ["cargo", "miri", cmd]
20     if quiet:
21         args += ["-q"]
22     if 'MIRI_TEST_TARGET' in os.environ:
23         args += ["--target", os.environ['MIRI_TEST_TARGET']]
24     return args
25
26 def normalize_stdout(str):
27     str = str.replace("src\\", "src/") # normalize paths across platforms
28     return re.sub("finished in \d+\.\d\ds", "finished in $TIME", str)
29
30 def test(name, cmd, stdout_ref, stderr_ref, stdin=b'', env={}):
31     print("Testing {}...".format(name))
32     ## Call `cargo miri`, capture all output
33     p_env = os.environ.copy()
34     p_env.update(env)
35     p = subprocess.Popen(
36         cmd,
37         stdin=subprocess.PIPE,
38         stdout=subprocess.PIPE,
39         stderr=subprocess.PIPE,
40         env=p_env,
41     )
42     (stdout, stderr) = p.communicate(input=stdin)
43     stdout = stdout.decode("UTF-8")
44     stderr = stderr.decode("UTF-8")
45     if p.returncode == 0 and normalize_stdout(stdout) == open(stdout_ref).read() and stderr == open(stderr_ref).read():
46         # All good!
47         return
48     # Show output
49     print("--- BEGIN stdout ---")
50     print(stdout, end="")
51     print("--- END stdout ---")
52     print("--- BEGIN stderr ---")
53     print(stderr, end="")
54     print("--- END stderr ---")
55     fail("exit code was {}".format(p.returncode))
56
57 def test_no_rebuild(name, cmd, env={}):
58     print("Testing {}...".format(name))
59     p_env = os.environ.copy()
60     p_env.update(env)
61     p = subprocess.Popen(
62         cmd,
63         stdout=subprocess.PIPE,
64         stderr=subprocess.PIPE,
65         env=p_env,
66     )
67     (stdout, stderr) = p.communicate()
68     stdout = stdout.decode("UTF-8")
69     stderr = stderr.decode("UTF-8")
70     if p.returncode != 0:
71         fail("rebuild failed");
72     # Also check for 'Running' as a sanity check.
73     if stderr.count(" Compiling ") > 0 or stderr.count(" Running ") == 0:
74         print("--- BEGIN stderr ---")
75         print(stderr, end="")
76         print("--- END stderr ---")
77         fail("Something was being rebuilt when it should not be (or we got no output)");
78
79 def test_cargo_miri_run():
80     test("`cargo miri run` (no isolation)",
81         cargo_miri("run"),
82         "run.default.stdout.ref", "run.default.stderr.ref",
83         stdin=b'12\n21\n',
84         env={
85             'MIRIFLAGS': "-Zmiri-disable-isolation",
86             'MIRITESTVAR': "wrongval", # make sure the build.rs value takes precedence
87         },
88     )
89     # Special test: run it again *without* `-q` to make sure nothing is being rebuilt (Miri issue #1722)
90     test_no_rebuild("`cargo miri run` (no rebuild)",
91         cargo_miri("run", quiet=False) + ["--", ""],
92         env={'MIRITESTVAR': "wrongval"}, # changing the env var causes a rebuild (re-runs build.rs),
93                                          # so keep it set
94     )
95     test("`cargo miri run` (with arguments and target)",
96         cargo_miri("run") + ["--bin", "cargo-miri-test", "--", "hello world", '"hello world"'],
97         "run.args.stdout.ref", "run.args.stderr.ref",
98     )
99     test("`cargo miri run` (subcrate, no ioslation)",
100         cargo_miri("run") + ["-p", "subcrate"],
101         "run.subcrate.stdout.ref", "run.subcrate.stderr.ref",
102         env={'MIRIFLAGS': "-Zmiri-disable-isolation"},
103     )
104     test("`cargo miri run` (custom target dir)",
105         # Attempt to confuse the argument parser.
106         cargo_miri("run") + ["--target-dir=custom-run", "--", "--target-dir=target/custom-run"],
107         "run.args.stdout.ref", "run.custom-target-dir.stderr.ref",
108     )
109
110 def test_cargo_miri_test():
111     # rustdoc is not run on foreign targets
112     is_foreign = 'MIRI_TEST_TARGET' in os.environ
113     default_ref = "test.cross-target.stdout.ref" if is_foreign else "test.default.stdout.ref"
114     filter_ref = "test.filter.cross-target.stdout.ref" if is_foreign else "test.filter.stdout.ref"
115
116     test("`cargo miri test`",
117         cargo_miri("test"),
118         default_ref, "test.stderr-empty.ref",
119         env={'MIRIFLAGS': "-Zmiri-seed=feed"},
120     )
121     test("`cargo miri test` (no isolation, no doctests)",
122         cargo_miri("test") + ["--bins", "--tests"], # no `--lib`, we disabled that in `Cargo.toml`
123         "test.cross-target.stdout.ref", "test.stderr-empty.ref",
124         env={'MIRIFLAGS': "-Zmiri-disable-isolation"},
125     )
126     test("`cargo miri test` (raw-ptr tracking)",
127         cargo_miri("test"),
128         default_ref, "test.stderr-empty.ref",
129         env={'MIRIFLAGS': "-Zmiri-track-raw-pointers"},
130     )
131     test("`cargo miri test` (with filter)",
132         cargo_miri("test") + ["--", "--format=pretty", "le1"],
133         filter_ref, "test.stderr-empty.ref",
134     )
135     test("`cargo miri test` (test target)",
136         cargo_miri("test") + ["--test", "test", "--", "--format=pretty"],
137         "test.test-target.stdout.ref", "test.stderr-empty.ref",
138     )
139     test("`cargo miri test` (bin target)",
140         cargo_miri("test") + ["--bin", "cargo-miri-test", "--", "--format=pretty"],
141         "test.bin-target.stdout.ref", "test.stderr-empty.ref",
142     )
143     test("`cargo miri test` (subcrate, no isolation)",
144         cargo_miri("test") + ["-p", "subcrate"],
145         "test.subcrate.stdout.ref", "test.stderr-proc-macro.ref",
146         env={'MIRIFLAGS': "-Zmiri-disable-isolation"},
147     )
148     test("`cargo miri test` (subcrate, doctests)",
149         cargo_miri("test") + ["-p", "subcrate", "--doc"],
150         "test.stdout-empty.ref", "test.stderr-proc-macro-doctest.ref",
151     )
152     test("`cargo miri test` (custom target dir)",
153         cargo_miri("test") + ["--target-dir=custom-test"],
154         default_ref, "test.stderr-empty.ref",
155     )
156     del os.environ["CARGO_TARGET_DIR"] # this overrides `build.target-dir` passed by `--config`, so unset it
157     test("`cargo miri test` (config-cli)",
158         cargo_miri("test") + ["--config=build.target-dir=\"config-cli\"", "-Zunstable-options"],
159         default_ref, "test.stderr-empty.ref",
160     )
161
162 os.chdir(os.path.dirname(os.path.realpath(__file__)))
163 os.environ["CARGO_TARGET_DIR"] = "target" # this affects the location of the target directory that we need to check
164 os.environ["RUST_TEST_NOCAPTURE"] = "0" # this affects test output, so make sure it is not set
165 os.environ["RUST_TEST_THREADS"] = "1" # avoid non-deterministic output due to concurrent test runs
166
167 target_str = " for target {}".format(os.environ['MIRI_TEST_TARGET']) if 'MIRI_TEST_TARGET' in os.environ else ""
168 print(CGREEN + CBOLD + "## Running `cargo miri` tests{}".format(target_str) + CEND)
169
170 if not 'MIRI_SYSROOT' in os.environ:
171     # Make sure we got a working sysroot.
172     # (If the sysroot gets built later when output is compared, that leads to test failures.)
173     subprocess.run(cargo_miri("setup"), check=True)
174 test_cargo_miri_run()
175 test_cargo_miri_test()
176 # Ensure we did not create anything outside the expected target dir.
177 for target_dir in ["target", "custom-run", "custom-test", "config-cli"]:
178     if os.listdir(target_dir) != ["miri"]:
179         fail(f"`{target_dir}` contains unexpected files")
180     # Ensure something exists inside that target dir.
181     os.access(os.path.join(target_dir, "miri", "debug", "deps"), os.F_OK)
182
183 print("\nTEST SUCCESSFUL!")
184 sys.exit(0)