]> git.lizzy.rs Git - rust.git/blob - tests/compiletest.rs
Merge branch 'miri-backtrace' into mut-visitor
[rust.git] / tests / compiletest.rs
1 #![feature(slice_concat_ext, custom_test_frameworks)]
2 #![test_runner(test_runner)]
3
4 use std::slice::SliceConcatExt;
5 use std::path::{PathBuf, Path};
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 have_fullmir() -> bool {
28     // We assume we have full MIR when MIRI_SYSROOT is set or when we are in rustc
29     std::env::var("MIRI_SYSROOT").is_ok() || rustc_test_suite().is_some()
30 }
31
32 fn mk_config(mode: &str) -> compiletest::common::ConfigWithTemp {
33     let mut config = compiletest::Config::default().tempdir();
34     config.mode = mode.parse().expect("Invalid mode");
35     config.rustc_path = miri_path();
36     if rustc_test_suite().is_some() {
37         config.run_lib_path = rustc_lib_path();
38         config.compile_lib_path = rustc_lib_path();
39     }
40     config.filter = env::args().nth(1);
41     config
42 }
43
44 fn compile_fail(sysroot: &Path, path: &str, target: &str, host: &str, need_fullmir: bool, opt: bool) {
45     if need_fullmir && !have_fullmir() {
46         eprintln!("{}\n", format!(
47             "## Skipping compile-fail tests in {} against miri for target {} due to missing mir",
48             path,
49             target
50         ).yellow().bold());
51         return;
52     }
53
54     let opt_str = if opt { " with optimizations" } else { "" };
55     eprintln!("{}", format!(
56         "## Running compile-fail tests in {} against miri for target {}{}",
57         path,
58         target,
59         opt_str
60     ).green().bold());
61
62     let mut flags = Vec::new();
63     flags.push(format!("--sysroot {}", sysroot.display()));
64     flags.push("-Dwarnings -Dunused".to_owned()); // overwrite the -Aunused in compiletest-rs
65     if opt {
66         // Optimizing too aggressivley makes UB detection harder, but test at least
67         // the default value.
68         // FIXME: Opt level 3 ICEs during stack trace generation.
69         flags.push("-Zmir-opt-level=1".to_owned());
70     }
71
72     let mut config = mk_config("compile-fail");
73     config.src_base = PathBuf::from(path);
74     config.target = target.to_owned();
75     config.host = host.to_owned();
76     config.target_rustcflags = Some(flags.join(" "));
77     compiletest::run_tests(&config);
78 }
79
80 fn miri_pass(sysroot: &Path, path: &str, target: &str, host: &str, need_fullmir: bool, opt: bool) {
81     if need_fullmir && !have_fullmir() {
82         eprintln!("{}\n", format!(
83             "## Skipping run-pass tests in {} against miri for target {} due to missing mir",
84             path,
85             target
86         ).yellow().bold());
87         return;
88     }
89
90     let opt_str = if opt { " with optimizations" } else { "" };
91     eprintln!("{}", format!(
92         "## Running run-pass tests in {} against miri for target {}{}",
93         path,
94         target,
95         opt_str
96     ).green().bold());
97
98     let mut flags = Vec::new();
99     flags.push(format!("--sysroot {}", sysroot.display()));
100     flags.push("-Dwarnings -Dunused".to_owned()); // overwrite the -Aunused in compiletest-rs
101     if opt {
102         // FIXME: We use opt level 1 because MIR inlining defeats the validation
103         // whitelist.
104         flags.push("-Zmir-opt-level=1".to_owned());
105     }
106     if !have_fullmir() {
107         // Validation relies on the EscapeToRaw statements being emitted
108         flags.push("-Zmiri-disable-validation".to_owned());
109     }
110
111     let mut config = mk_config("ui");
112     config.src_base = PathBuf::from(path);
113     config.target = target.to_owned();
114     config.host = host.to_owned();
115     config.target_rustcflags = Some(flags.join(" "));
116     compiletest::run_tests(&config);
117 }
118
119 fn is_target_dir<P: Into<PathBuf>>(path: P) -> bool {
120     let mut path = path.into();
121     path.push("lib");
122     path.metadata().map(|m| m.is_dir()).unwrap_or(false)
123 }
124
125 fn for_all_targets<F: FnMut(String)>(sysroot: &Path, mut f: F) {
126     let target_dir = sysroot.join("lib").join("rustlib");
127     for entry in std::fs::read_dir(target_dir).expect("invalid sysroot") {
128         let entry = entry.unwrap();
129         if !is_target_dir(entry.path()) {
130             continue;
131         }
132         let target = entry.file_name().into_string().unwrap();
133         f(target);
134     }
135 }
136
137 fn get_sysroot() -> PathBuf {
138     let sysroot = std::env::var("MIRI_SYSROOT").unwrap_or_else(|_| {
139         let sysroot = std::process::Command::new("rustc")
140             .arg("--print")
141             .arg("sysroot")
142             .output()
143             .expect("rustc not found")
144             .stdout;
145         String::from_utf8(sysroot).expect("sysroot is not utf8")
146     });
147     PathBuf::from(sysroot.trim())
148 }
149
150 fn get_host() -> String {
151     let rustc = rustc_test_suite().unwrap_or(PathBuf::from("rustc"));
152     let host = std::process::Command::new(rustc)
153         .arg("-vV")
154         .output()
155         .expect("rustc not found for -vV")
156         .stdout;
157     let host = std::str::from_utf8(&host).expect("sysroot is not utf8");
158     let host = host.split("\nhost: ").nth(1).expect(
159         "no host: part in rustc -vV",
160     );
161     let host = host.split('\n').next().expect("no \n after host");
162     String::from(host)
163 }
164
165 fn run_pass_miri(opt: bool) {
166     let sysroot = get_sysroot();
167     let host = get_host();
168
169     for_all_targets(&sysroot, |target| {
170         miri_pass(&sysroot, "tests/run-pass", &target, &host, false, opt);
171     });
172     miri_pass(&sysroot, "tests/run-pass-fullmir", &host, &host, true, opt);
173 }
174
175 fn compile_fail_miri(opt: bool) {
176     let sysroot = get_sysroot();
177     let host = get_host();
178
179     // FIXME: run tests for other targets, too
180     compile_fail(&sysroot, "tests/compile-fail", &host, &host, false, opt);
181     compile_fail(&sysroot, "tests/compile-fail-fullmir", &host, &host, true, opt);
182 }
183
184 fn test_runner(_tests: &[&()]) {
185     // We put everything into a single test to avoid the parallelism `cargo test`
186     // introduces.  We still get parallelism within our tests because `compiletest`
187     // uses `libtest` which runs jobs in parallel.
188
189     run_pass_miri(false);
190     run_pass_miri(true);
191
192     compile_fail_miri(false);
193     compile_fail_miri(true);
194 }