fork/rust
git clone https://git.julia.blue/fork/rust
3bcad65fbf6
master
1#!/usr/bin/env python3 2''' 3Test whether cargo-miri works properly. 4Assumes the `MIRI_SYSROOT` env var to be set appropriately, 5and the working directory to contain the cargo-miri-test project. 6''' 7 8import difflib 9import os 10import re 11import subprocess 12import sys 13 14CGREEN = '\33[32m' 15CBOLD = '\33[1m' 16CEND = '\33[0m' 17 18CARGO_EXTRA_FLAGS = os.environ.get("CARGO_EXTRA_FLAGS", "").split() 19 20def fail(msg): 21 print("\nTEST FAIL: {}".format(msg)) 22 sys.exit(1) 23 24def cargo_miri(cmd, quiet = True): 25 args = ["cargo", "miri", cmd] + CARGO_EXTRA_FLAGS 26 if quiet: 27 args += ["-q"] 28 if 'MIRI_TEST_TARGET' in os.environ: 29 args += ["--target", os.environ['MIRI_TEST_TARGET']] 30 return args 31 32def normalize_stdout(str): 33 str = str.replace("src\\", "src/") # normalize paths across platforms 34 str = re.sub("finished in \d+\.\d\ds", "finished in $TIME", str) # the time keeps changing, obviously 35 return str 36 37def normalize_stderr(str): 38 str = re.sub("Preparing a sysroot for Miri \(target: [a-z0-9_-]+\)\.\.\. done\n", "", str) # remove leading cargo-miri setup output 39 return str 40 41def check_output(actual, path, name): 42 if os.environ.get("RUSTC_BLESS", "0") != "0": 43 # Write the output only if bless is set 44 open(path, mode='w').write(actual) 45 return True 46 expected = open(path).read() 47 if expected == actual: 48 return True 49 print(f"{name} output did not match reference in {path}!") 50 print(f"--- BEGIN diff {name} ---") 51 for text in difflib.unified_diff(expected.split("\n"), actual.split("\n")): 52 print(text) 53 print(f"--- END diff {name} ---") 54 return False 55 56def test(name, cmd, stdout_ref, stderr_ref, stdin=b'', env=None): 57 if env is None: 58 env = {} 59 print("Testing {}...".format(name)) 60 ## Call `cargo miri`, capture all output 61 p_env = os.environ.copy() 62 p_env.update(env) 63 p = subprocess.Popen( 64 cmd, 65 stdin=subprocess.PIPE, 66 stdout=subprocess.PIPE, 67 stderr=subprocess.PIPE, 68 env=p_env, 69 ) 70 (stdout, stderr) = p.communicate(input=stdin) 71 stdout = normalize_stdout(stdout.decode("UTF-8")) 72 stderr = normalize_stderr(stderr.decode("UTF-8")) 73 74 stdout_matches = check_output(stdout, stdout_ref, "stdout") 75 stderr_matches = check_output(stderr, stderr_ref, "stderr") 76 77 if p.returncode == 0 and stdout_matches and stderr_matches: 78 # All good! 79 return 80 fail("exit code was {}".format(p.returncode)) 81 82def test_no_rebuild(name, cmd, env=None): 83 if env is None: 84 env = {} 85 print("Testing {}...".format(name)) 86 p_env = os.environ.copy() 87 p_env.update(env) 88 p = subprocess.Popen( 89 cmd, 90 stdout=subprocess.PIPE, 91 stderr=subprocess.PIPE, 92 env=p_env, 93 ) 94 (stdout, stderr) = p.communicate() 95 stdout = stdout.decode("UTF-8") 96 stderr = stderr.decode("UTF-8") 97 if p.returncode != 0: 98 fail("rebuild failed") 99 # Also check for 'Running' as a sanity check. 100 if stderr.count(" Compiling ") > 0 or stderr.count(" Running ") == 0: 101 print("--- BEGIN stderr ---") 102 print(stderr, end="") 103 print("--- END stderr ---") 104 fail("Something was being rebuilt when it should not be (or we got no output)") 105 106def test_cargo_miri_run(): 107 test("`cargo miri run` (no isolation)", 108 cargo_miri("run"), 109 "run.default.stdout.ref", "run.default.stderr.ref", 110 stdin=b'12\n21\n', 111 env={ 112 'MIRIFLAGS': "-Zmiri-disable-isolation", 113 'MIRITESTVAR': "wrongval", # make sure the build.rs value takes precedence 114 }, 115 ) 116 # Special test: run it again *without* `-q` to make sure nothing is being rebuilt (Miri issue #1722) 117 test_no_rebuild("`cargo miri run` (no rebuild)", 118 cargo_miri("run", quiet=False) + ["--", ""], 119 env={'MIRITESTVAR': "wrongval"}, # changing the env var causes a rebuild (re-runs build.rs), 120 # so keep it set 121 ) 122 # This also covers passing arguments without `--`: Cargo will forward unused positional arguments to the program. 123 test("`cargo miri run` (with arguments and target)", 124 cargo_miri("run") + ["--bin", "cargo-miri-test", "hello world", '"hello world"', r'he\\llo\"world'], 125 "run.args.stdout.ref", "run.args.stderr.ref", 126 ) 127 test("`cargo miri r` (subcrate, no isolation)", 128 cargo_miri("r") + ["-p", "subcrate"], 129 "run.subcrate.stdout.ref", "run.subcrate.stderr.ref", 130 env={'MIRIFLAGS': "-Zmiri-disable-isolation"}, 131 ) 132 test("`cargo miri run` (custom target dir)", 133 # Attempt to confuse the argument parser. 134 cargo_miri("run") + ["--target-dir=custom-run", "--", "--target-dir=target/custom-run"], 135 "run.args.stdout.ref", "run.custom-target-dir.stderr.ref", 136 ) 137 138def test_cargo_miri_test(): 139 # rustdoc is not run on foreign targets 140 is_foreign = 'MIRI_TEST_TARGET' in os.environ 141 default_ref = "test.cross-target.stdout.ref" if is_foreign else "test.default.stdout.ref" 142 filter_ref = "test.filter.cross-target.stdout.ref" if is_foreign else "test.filter.stdout.ref" 143 144 # macOS needs permissive provenance inside getrandom_1. 145 test("`cargo miri test`", 146 cargo_miri("test"), 147 default_ref, "test.stderr-empty.ref", 148 env={'MIRIFLAGS': "-Zmiri-permissive-provenance -Zmiri-seed=4242"}, 149 ) 150 test("`cargo miri test` (no isolation, no doctests)", 151 cargo_miri("test") + ["--bins", "--tests"], # no `--lib`, we disabled that in `Cargo.toml` 152 "test.cross-target.stdout.ref", "test.stderr-empty.ref", 153 env={'MIRIFLAGS': "-Zmiri-permissive-provenance -Zmiri-disable-isolation"}, 154 ) 155 test("`cargo miri test` (with filter)", 156 cargo_miri("test") + ["--", "--format=pretty", "pl"], 157 filter_ref, "test.stderr-empty.ref", 158 ) 159 test("`cargo miri test` (test target)", 160 cargo_miri("test") + ["--test", "test", "--", "--format=pretty"], 161 "test.test-target.stdout.ref", "test.stderr-empty.ref", 162 env={'MIRIFLAGS': "-Zmiri-permissive-provenance"}, 163 ) 164 test("`cargo miri test` (bin target)", 165 cargo_miri("test") + ["--bin", "cargo-miri-test", "--", "--format=pretty"], 166 "test.bin-target.stdout.ref", "test.stderr-empty.ref", 167 ) 168 test("`cargo miri t` (subcrate, no isolation)", 169 cargo_miri("t") + ["-p", "subcrate"], 170 "test.subcrate.stdout.ref", "test.stderr-proc-macro.ref", 171 env={'MIRIFLAGS': "-Zmiri-disable-isolation"}, 172 ) 173 test("`cargo miri test` (subcrate, doctests)", 174 cargo_miri("test") + ["-p", "subcrate", "--doc"], 175 "test.stdout-empty.ref", "test.stderr-proc-macro-doctest.ref", 176 ) 177 test("`cargo miri test` (custom target dir)", 178 cargo_miri("test") + ["--target-dir=custom-test"], 179 default_ref, "test.stderr-empty.ref", 180 env={'MIRIFLAGS': "-Zmiri-permissive-provenance"}, 181 ) 182 del os.environ["CARGO_TARGET_DIR"] # this overrides `build.target-dir` passed by `--config`, so unset it 183 test("`cargo miri test` (config-cli)", 184 cargo_miri("test") + ["--config=build.target-dir=\"config-cli\"", "-Zunstable-options"], 185 default_ref, "test.stderr-empty.ref", 186 env={'MIRIFLAGS': "-Zmiri-permissive-provenance"}, 187 ) 188 189os.chdir(os.path.dirname(os.path.realpath(__file__))) 190os.environ["CARGO_TARGET_DIR"] = "target" # this affects the location of the target directory that we need to check 191os.environ["RUST_TEST_NOCAPTURE"] = "0" # this affects test output, so make sure it is not set 192os.environ["RUST_TEST_THREADS"] = "1" # avoid non-deterministic output due to concurrent test runs 193 194target_str = " for target {}".format(os.environ['MIRI_TEST_TARGET']) if 'MIRI_TEST_TARGET' in os.environ else "" 195print(CGREEN + CBOLD + "## Running `cargo miri` tests{}".format(target_str) + CEND) 196 197test_cargo_miri_run() 198test_cargo_miri_test() 199# Ensure we did not create anything outside the expected target dir. 200for target_dir in ["target", "custom-run", "custom-test", "config-cli"]: 201 if os.listdir(target_dir) != ["miri"]: 202 fail(f"`{target_dir}` contains unexpected files") 203 # Ensure something exists inside that target dir. 204 os.access(os.path.join(target_dir, "miri", "debug", "deps"), os.F_OK) 205 206print("\nTEST SUCCESSFUL!") 207sys.exit(0)