2018-10-30 15:07:40 +01:00
|
|
|
#!/usr/bin/env python3
|
2018-10-30 11:26:53 +01:00
|
|
|
'''
|
|
|
|
Test whether cargo-miri works properly.
|
|
|
|
Assumes the `MIRI_SYSROOT` env var to be set appropriately,
|
|
|
|
and the working directory to contain the cargo-miri-test project.
|
|
|
|
'''
|
|
|
|
|
2019-04-21 22:35:47 +02:00
|
|
|
import sys, subprocess, os
|
2018-10-30 11:26:53 +01:00
|
|
|
|
2020-03-22 09:04:10 +01:00
|
|
|
CGREEN = '\33[32m'
|
|
|
|
CBOLD = '\33[1m'
|
|
|
|
CEND = '\33[0m'
|
|
|
|
|
2018-12-18 21:49:38 +01:00
|
|
|
def fail(msg):
|
2019-06-29 13:33:47 +02:00
|
|
|
print("\nTEST FAIL: {}".format(msg))
|
2018-12-18 21:49:38 +01:00
|
|
|
sys.exit(1)
|
|
|
|
|
2019-04-21 22:35:47 +02:00
|
|
|
def cargo_miri(cmd):
|
|
|
|
args = ["cargo", "miri", cmd, "-q"]
|
|
|
|
if 'MIRI_TEST_TARGET' in os.environ:
|
|
|
|
args += ["--target", os.environ['MIRI_TEST_TARGET']]
|
|
|
|
return args
|
|
|
|
|
2020-09-09 09:18:10 +02:00
|
|
|
def test(name, cmd, stdout_ref, stderr_ref, stdin=b'', env={}):
|
2020-09-12 13:57:49 +02:00
|
|
|
print("Testing {}...".format(name))
|
2018-10-30 11:26:53 +01:00
|
|
|
## Call `cargo miri`, capture all output
|
2020-09-09 08:58:29 +02:00
|
|
|
p_env = os.environ.copy()
|
|
|
|
p_env.update(env)
|
2018-10-30 11:26:53 +01:00
|
|
|
p = subprocess.Popen(
|
2018-11-27 15:06:51 +01:00
|
|
|
cmd,
|
2020-09-09 09:18:10 +02:00
|
|
|
stdin=subprocess.PIPE,
|
2018-10-30 11:26:53 +01:00
|
|
|
stdout=subprocess.PIPE,
|
2020-09-09 08:58:29 +02:00
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
env=p_env,
|
2018-10-30 11:26:53 +01:00
|
|
|
)
|
2020-09-09 09:18:10 +02:00
|
|
|
(stdout, stderr) = p.communicate(input=stdin)
|
2018-10-30 11:26:53 +01:00
|
|
|
stdout = stdout.decode("UTF-8")
|
|
|
|
stderr = stderr.decode("UTF-8")
|
2020-09-12 13:57:49 +02:00
|
|
|
if p.returncode == 0 and stdout == open(stdout_ref).read() and stderr == open(stderr_ref).read():
|
|
|
|
# All good!
|
|
|
|
return
|
2018-10-30 11:26:53 +01:00
|
|
|
# Show output
|
2020-09-12 13:57:49 +02:00
|
|
|
print("--- BEGIN stdout ---")
|
2018-10-30 11:26:53 +01:00
|
|
|
print(stdout, end="")
|
2020-09-12 13:57:49 +02:00
|
|
|
print("--- END stdout ---")
|
|
|
|
print("--- BEGIN stderr ---")
|
2018-10-30 11:26:53 +01:00
|
|
|
print(stderr, end="")
|
2020-09-12 13:57:49 +02:00
|
|
|
print("--- END stderr ---")
|
|
|
|
fail("exit code was {}".format(p.returncode))
|
2018-10-30 11:26:53 +01:00
|
|
|
|
2018-11-27 15:06:51 +01:00
|
|
|
def test_cargo_miri_run():
|
2020-09-12 13:52:05 +02:00
|
|
|
test("`cargo miri run` (no isolation)",
|
2019-04-21 22:35:47 +02:00
|
|
|
cargo_miri("run"),
|
2020-09-12 12:52:14 +02:00
|
|
|
"stdout.ref1", "stderr.ref1",
|
2020-09-09 09:18:10 +02:00
|
|
|
stdin=b'12\n21\n',
|
2020-09-12 13:10:23 +02:00
|
|
|
env={
|
|
|
|
'MIRIFLAGS': "-Zmiri-disable-isolation",
|
|
|
|
'MIRITESTVAR': "wrongval", # make sure the build.rs value takes precedence
|
|
|
|
},
|
2019-04-21 22:35:47 +02:00
|
|
|
)
|
2020-09-12 12:52:14 +02:00
|
|
|
test("`cargo miri run` (with arguments and target)",
|
2020-09-09 09:18:10 +02:00
|
|
|
cargo_miri("run") + ["--bin", "cargo-miri-test", "--", "hello world", '"hello world"'],
|
2020-09-12 13:52:05 +02:00
|
|
|
"stdout.ref2", "stderr.ref2",
|
2019-02-09 12:42:16 +01:00
|
|
|
)
|
2020-09-12 13:52:05 +02:00
|
|
|
test("`cargo miri run` (subcrate, no ioslation)",
|
2020-09-12 13:33:30 +02:00
|
|
|
cargo_miri("run") + ["-p", "subcrate"],
|
|
|
|
"stdout.ref3", "stderr.ref3",
|
|
|
|
env={'MIRIFLAGS': "-Zmiri-disable-isolation"},
|
|
|
|
)
|
2018-11-27 15:06:51 +01:00
|
|
|
|
2018-10-30 11:26:53 +01:00
|
|
|
def test_cargo_miri_test():
|
2020-09-11 18:20:41 +02:00
|
|
|
# rustdoc is not run on foreign targets
|
|
|
|
is_foreign = 'MIRI_TEST_TARGET' in os.environ
|
2020-09-12 12:52:14 +02:00
|
|
|
rustdoc_ref = "test.stderr.ref2" if is_foreign else "test.stderr.ref1"
|
2020-09-11 18:20:41 +02:00
|
|
|
|
2020-09-12 12:52:14 +02:00
|
|
|
test("`cargo miri test`",
|
2020-09-09 08:58:29 +02:00
|
|
|
cargo_miri("test"),
|
2020-09-12 13:52:05 +02:00
|
|
|
"test.stdout.ref1", rustdoc_ref,
|
2020-09-09 08:58:29 +02:00
|
|
|
env={'MIRIFLAGS': "-Zmiri-seed=feed"},
|
2019-04-21 22:35:47 +02:00
|
|
|
)
|
2020-09-12 13:52:05 +02:00
|
|
|
test("`cargo miri test` (no isolation)",
|
|
|
|
cargo_miri("test"),
|
|
|
|
"test.stdout.ref1", rustdoc_ref,
|
|
|
|
env={'MIRIFLAGS': "-Zmiri-disable-isolation"},
|
|
|
|
)
|
2020-09-12 12:52:14 +02:00
|
|
|
test("`cargo miri test` (with filter)",
|
2020-09-09 08:58:29 +02:00
|
|
|
cargo_miri("test") + ["--", "--format=pretty", "le1"],
|
2020-09-12 13:52:05 +02:00
|
|
|
"test.stdout.ref2", rustdoc_ref,
|
2019-08-29 04:09:34 -05:00
|
|
|
)
|
2020-09-12 12:52:14 +02:00
|
|
|
test("`cargo miri test` (test target)",
|
2020-09-09 08:58:29 +02:00
|
|
|
cargo_miri("test") + ["--test", "test", "--", "--format=pretty"],
|
2020-09-12 13:52:05 +02:00
|
|
|
"test.stdout.ref3", "test.stderr.ref2",
|
2020-08-28 23:12:11 -05:00
|
|
|
)
|
2020-09-12 12:52:14 +02:00
|
|
|
test("`cargo miri test` (bin target)",
|
2020-09-09 08:58:29 +02:00
|
|
|
cargo_miri("test") + ["--bin", "cargo-miri-test", "--", "--format=pretty"],
|
2020-09-12 13:52:05 +02:00
|
|
|
"test.stdout.ref4", "test.stderr.ref2",
|
|
|
|
)
|
2020-09-12 13:57:49 +02:00
|
|
|
test("`cargo miri test` (subcrate, no isolation)",
|
2020-09-12 13:52:05 +02:00
|
|
|
cargo_miri("test") + ["-p", "subcrate"],
|
|
|
|
"test.stdout.ref5", "test.stderr.ref2",
|
|
|
|
env={'MIRIFLAGS': "-Zmiri-disable-isolation"},
|
2020-08-28 23:12:11 -05:00
|
|
|
)
|
2018-10-30 11:26:53 +01:00
|
|
|
|
2019-04-22 11:11:06 +02:00
|
|
|
os.chdir(os.path.dirname(os.path.realpath(__file__)))
|
2020-09-12 14:24:56 +02:00
|
|
|
os.environ["RUST_TEST_NOCAPTURE"] = "0" # this affects test output, so make sure it is not set
|
2019-04-22 11:11:06 +02:00
|
|
|
|
2020-03-22 09:04:10 +01:00
|
|
|
target_str = " for target {}".format(os.environ['MIRI_TEST_TARGET']) if 'MIRI_TEST_TARGET' in os.environ else ""
|
|
|
|
print(CGREEN + CBOLD + "## Running `cargo miri` tests{}".format(target_str) + CEND)
|
|
|
|
|
2019-12-01 12:57:09 +01:00
|
|
|
if not 'MIRI_SYSROOT' in os.environ:
|
|
|
|
# Make sure we got a working sysroot.
|
|
|
|
# (If the sysroot gets built later when output is compared, that leads to test failures.)
|
|
|
|
subprocess.run(cargo_miri("setup"), check=True)
|
2018-11-27 15:06:51 +01:00
|
|
|
test_cargo_miri_run()
|
2018-10-30 11:26:53 +01:00
|
|
|
test_cargo_miri_test()
|
2019-04-22 11:11:06 +02:00
|
|
|
|
2019-06-29 13:33:47 +02:00
|
|
|
print("\nTEST SUCCESSFUL!")
|
2018-10-30 11:26:53 +01:00
|
|
|
sys.exit(0)
|