1 #![feature(slice_concat_ext, custom_test_frameworks)]
2 // Custom test runner, to avoid libtest being wrapped around compiletest which wraps libtest.
3 #![test_runner(test_runner)]
5 use std::slice::SliceConcatExt;
6 use std::path::PathBuf;
9 use compiletest_rs as compiletest;
12 fn miri_path() -> PathBuf {
13 if rustc_test_suite().is_some() {
14 PathBuf::from(option_env!("MIRI_PATH").unwrap())
16 PathBuf::from(concat!("target/", env!("PROFILE"), "/miri"))
20 fn rustc_test_suite() -> Option<PathBuf> {
21 option_env!("RUSTC_TEST_SUITE").map(PathBuf::from)
24 fn rustc_lib_path() -> PathBuf {
25 option_env!("RUSTC_LIB_PATH").unwrap().into()
28 fn mk_config(mode: &str) -> compiletest::common::ConfigWithTemp {
29 let mut config = compiletest::Config::default().tempdir();
30 config.mode = mode.parse().expect("Invalid mode");
31 config.rustc_path = miri_path();
32 if rustc_test_suite().is_some() {
33 config.run_lib_path = rustc_lib_path();
34 config.compile_lib_path = rustc_lib_path();
36 config.filter = env::args().nth(1);
37 config.host = get_host();
41 fn compile_fail(path: &str, target: &str, opt: bool) {
42 let opt_str = if opt { " with optimizations" } else { "" };
43 eprintln!("{}", format!(
44 "## Running compile-fail tests in {} against miri for target {}{}",
50 let mut flags = Vec::new();
51 flags.push("-Dwarnings -Dunused".to_owned()); // overwrite the -Aunused in compiletest-rs
52 flags.push("--edition 2018".to_owned());
54 // Optimizing too aggressivley makes UB detection harder, but test at least
56 // FIXME: Opt level 3 ICEs during stack trace generation.
57 flags.push("-Zmir-opt-level=1".to_owned());
60 let mut config = mk_config("compile-fail");
61 config.src_base = PathBuf::from(path);
62 config.target = target.to_owned();
63 config.target_rustcflags = Some(flags.join(" "));
64 compiletest::run_tests(&config);
67 fn miri_pass(path: &str, target: &str, opt: bool) {
68 let opt_str = if opt { " with optimizations" } else { "" };
69 eprintln!("{}", format!(
70 "## Running run-pass tests in {} against miri for target {}{}",
76 let mut flags = Vec::new();
77 flags.push("-Dwarnings -Dunused".to_owned()); // overwrite the -Aunused in compiletest-rs
78 flags.push("--edition 2018".to_owned());
80 flags.push("-Zmir-opt-level=3".to_owned());
83 let mut config = mk_config("ui");
84 config.src_base = PathBuf::from(path);
85 config.target = target.to_owned();
86 config.target_rustcflags = Some(flags.join(" "));
87 compiletest::run_tests(&config);
90 /// Make sure the MIRI_SYSROOT env var is set
92 if std::env::var("MIRI_SYSROOT").is_ok() {
96 let sysroot = std::process::Command::new("rustc")
100 .expect("rustc not found")
102 let sysroot = String::from_utf8(sysroot).expect("sysroot is not utf8");
103 std::env::set_var("MIRI_SYSROOT", sysroot.trim());
106 fn get_host() -> String {
107 let rustc = rustc_test_suite().unwrap_or(PathBuf::from("rustc"));
108 let rustc_version = std::process::Command::new(rustc)
111 .expect("rustc not found for -vV")
113 let rustc_version = std::str::from_utf8(&rustc_version).expect("rustc -vV is not utf8");
114 let version_meta = rustc_version::version_meta_for(&rustc_version)
115 .expect("failed to parse rustc version info");
119 fn get_target() -> String {
120 std::env::var("MIRI_TARGET").unwrap_or_else(|_| get_host())
123 fn run_pass_miri(opt: bool) {
124 miri_pass("tests/run-pass", &get_target(), opt);
127 fn compile_fail_miri(opt: bool) {
128 compile_fail("tests/compile-fail", &get_target(), opt);
131 fn test_runner(_tests: &[&()]) {
134 run_pass_miri(false);
137 compile_fail_miri(false);
138 compile_fail_miri(true);