]> git.lizzy.rs Git - rust.git/blob - tests/compiletest.rs
Auto merge of #871 - RalfJung:miri-rustc-cleanup, r=RalfJung
[rust.git] / tests / compiletest.rs
1 #![feature(custom_test_frameworks)]
2 // Custom test runner, to avoid libtest being wrapped around compiletest which wraps libtest.
3 #![test_runner(test_runner)]
4
5 use std::path::PathBuf;
6 use std::env;
7
8 use compiletest_rs as compiletest;
9 use colored::*;
10
11 fn miri_path() -> PathBuf {
12     if rustc_test_suite().is_some() {
13         PathBuf::from(option_env!("MIRI_PATH").unwrap())
14     } else {
15         PathBuf::from(concat!("target/", env!("PROFILE"), "/miri"))
16     }
17 }
18
19 fn rustc_test_suite() -> Option<PathBuf> {
20     option_env!("RUSTC_TEST_SUITE").map(PathBuf::from)
21 }
22
23 fn rustc_lib_path() -> PathBuf {
24     option_env!("RUSTC_LIB_PATH").unwrap().into()
25 }
26
27 fn run_tests(mode: &str, path: &str, target: &str, mut flags: Vec<String>) {
28     let in_rustc_test_suite = rustc_test_suite().is_some();
29     // Add some flags we always want.
30     flags.push("--edition 2018".to_owned());
31     if !in_rustc_test_suite {
32       // Only `-Dwarnings` on the Miri side to make the rustc toolstate management less painful.
33       // (We often get warnings when e.g. a feature gets stabilized or some lint gets added/improved.)
34       flags.push("-Dwarnings -Dunused".to_owned()); // overwrite the -Aunused in compiletest-rs
35     }
36     if let Ok(sysroot) = std::env::var("MIRI_SYSROOT") {
37         flags.push(format!("--sysroot {}", sysroot));
38     }
39
40     // The rest of the configuration.
41     let mut config = compiletest::Config::default().tempdir();
42     config.mode = mode.parse().expect("Invalid mode");
43     config.rustc_path = miri_path();
44     if in_rustc_test_suite {
45         config.run_lib_path = rustc_lib_path();
46         config.compile_lib_path = rustc_lib_path();
47     }
48     config.filter = env::args().nth(1);
49     config.host = get_host();
50     config.src_base = PathBuf::from(path);
51     config.target = target.to_owned();
52     config.target_rustcflags = Some(flags.join(" "));
53     compiletest::run_tests(&config);
54 }
55
56 fn compile_fail(path: &str, target: &str, opt: bool) {
57     let opt_str = if opt { " with optimizations" } else { "" };
58     eprintln!("{}", format!(
59         "## Running compile-fail tests in {} against miri for target {}{}",
60         path,
61         target,
62         opt_str
63     ).green().bold());
64
65     let mut flags = Vec::new();
66     if opt {
67         // Optimizing too aggressivley makes UB detection harder, but test at least
68         // the default value.
69         // FIXME: Opt level 3 ICEs during stack trace generation.
70         flags.push("-Zmir-opt-level=1".to_owned());
71     }
72
73     run_tests("compile-fail", path, target, flags);
74 }
75
76 fn miri_pass(path: &str, target: &str, opt: bool) {
77     let opt_str = if opt { " with optimizations" } else { "" };
78     eprintln!("{}", format!(
79         "## Running run-pass tests in {} against miri for target {}{}",
80         path,
81         target,
82         opt_str
83     ).green().bold());
84
85     let mut flags = Vec::new();
86     if opt {
87         flags.push("-Zmir-opt-level=3".to_owned());
88     }
89
90     run_tests("ui", path, target, flags);
91 }
92
93 fn get_host() -> String {
94     let rustc = rustc_test_suite().unwrap_or(PathBuf::from("rustc"));
95     let rustc_version = std::process::Command::new(rustc)
96         .arg("-vV")
97         .output()
98         .expect("rustc not found for -vV")
99         .stdout;
100     let rustc_version = std::str::from_utf8(&rustc_version).expect("rustc -vV is not utf8");
101     let version_meta = rustc_version::version_meta_for(&rustc_version)
102         .expect("failed to parse rustc version info");
103     version_meta.host
104 }
105
106 fn get_target() -> String {
107     std::env::var("MIRI_TEST_TARGET").unwrap_or_else(|_| get_host())
108 }
109
110 fn run_pass_miri(opt: bool) {
111     miri_pass("tests/run-pass", &get_target(), opt);
112 }
113
114 fn compile_fail_miri(opt: bool) {
115     compile_fail("tests/compile-fail", &get_target(), opt);
116 }
117
118 fn test_runner(_tests: &[&()]) {
119     run_pass_miri(false);
120     run_pass_miri(true);
121
122     compile_fail_miri(false);
123     compile_fail_miri(true);
124 }