]> git.lizzy.rs Git - rust.git/blob - tests/compiletest.rs
use custom test runner so that we can get proper test filtering
[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::io::Write;
7 use std::env;
8
9 use compiletest_rs as compiletest;
10 use colored::*;
11
12 fn miri_path() -> PathBuf {
13     if rustc_test_suite().is_some() {
14         PathBuf::from(option_env!("MIRI_PATH").unwrap())
15     } else {
16         PathBuf::from(concat!("target/", env!("PROFILE"), "/miri"))
17     }
18 }
19
20 fn rustc_test_suite() -> Option<PathBuf> {
21     option_env!("RUSTC_TEST_SUITE").map(PathBuf::from)
22 }
23
24 fn rustc_lib_path() -> PathBuf {
25     option_env!("RUSTC_LIB_PATH").unwrap().into()
26 }
27
28 fn have_fullmir() -> bool {
29     // We assume we have full MIR when MIRI_SYSROOT is set or when we are in rustc
30     std::env::var("MIRI_SYSROOT").is_ok() || rustc_test_suite().is_some()
31 }
32
33 fn mk_config(mode: &str) -> compiletest::Config {
34     let mut config = compiletest::Config::default();
35     config.mode = mode.parse().expect("Invalid mode");
36     config.rustc_path = miri_path();
37     if rustc_test_suite().is_some() {
38         config.run_lib_path = rustc_lib_path();
39         config.compile_lib_path = rustc_lib_path();
40     }
41     config.filter = env::args().nth(1);
42     config
43 }
44
45 fn compile_fail(sysroot: &Path, path: &str, target: &str, host: &str, need_fullmir: bool, opt: bool) {
46     if need_fullmir && !have_fullmir() {
47         eprintln!("{}\n", format!(
48             "## Skipping compile-fail tests in {} against miri for target {} due to missing mir",
49             path,
50             target
51         ).yellow().bold());
52         return;
53     }
54
55     let opt_str = if opt { " with optimizations" } else { "" };
56     eprintln!("{}", format!(
57         "## Running compile-fail tests in {} against miri for target {}{}",
58         path,
59         target,
60         opt_str
61     ).green().bold());
62
63     let mut flags = Vec::new();
64     flags.push(format!("--sysroot {}", sysroot.display()));
65     flags.push("-Dwarnings -Dunused".to_owned()); // overwrite the -Aunused in compiletest-rs
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     let mut config = mk_config("compile-fail");
74     config.src_base = PathBuf::from(path);
75     config.target = target.to_owned();
76     config.host = host.to_owned();
77     config.target_rustcflags = Some(flags.join(" "));
78     compiletest::run_tests(&config.tempdir()); // FIXME: `tempdir` can be done by `mk_config` once `ConfigWithTemp` is exposed as type from compiletest
79 }
80
81 fn miri_pass(sysroot: &Path, path: &str, target: &str, host: &str, need_fullmir: bool, opt: bool) {
82     if need_fullmir && !have_fullmir() {
83         eprintln!("{}\n", format!(
84             "## Skipping run-pass tests in {} against miri for target {} due to missing mir",
85             path,
86             target
87         ).yellow().bold());
88         return;
89     }
90
91     let opt_str = if opt { " with optimizations" } else { "" };
92     eprintln!("{}", format!(
93         "## Running run-pass tests in {} against miri for target {}{}",
94         path,
95         target,
96         opt_str
97     ).green().bold());
98
99     let mut flags = Vec::new();
100     flags.push(format!("--sysroot {}", sysroot.display()));
101     flags.push("-Dwarnings -Dunused".to_owned()); // overwrite the -Aunused in compiletest-rs
102     if opt {
103         flags.push("-Zmir-opt-level=3".to_owned());
104     }
105
106     let mut config = mk_config("ui");
107     config.src_base = PathBuf::from(path);
108     config.target = target.to_owned();
109     config.host = host.to_owned();
110     config.target_rustcflags = Some(flags.join(" "));
111     compiletest::run_tests(&config.tempdir()); // FIXME: `tempdir` can be done by `mk_config` once `ConfigWithTemp` is exposed as type from compiletest
112 }
113
114 fn is_target_dir<P: Into<PathBuf>>(path: P) -> bool {
115     let mut path = path.into();
116     path.push("lib");
117     path.metadata().map(|m| m.is_dir()).unwrap_or(false)
118 }
119
120 fn for_all_targets<F: FnMut(String)>(sysroot: &Path, mut f: F) {
121     let target_dir = sysroot.join("lib").join("rustlib");
122     for entry in std::fs::read_dir(target_dir).expect("invalid sysroot") {
123         let entry = entry.unwrap();
124         if !is_target_dir(entry.path()) {
125             continue;
126         }
127         let target = entry.file_name().into_string().unwrap();
128         f(target);
129     }
130 }
131
132 fn get_sysroot() -> PathBuf {
133     let sysroot = std::env::var("MIRI_SYSROOT").unwrap_or_else(|_| {
134         let sysroot = std::process::Command::new("rustc")
135             .arg("--print")
136             .arg("sysroot")
137             .output()
138             .expect("rustc not found")
139             .stdout;
140         String::from_utf8(sysroot).expect("sysroot is not utf8")
141     });
142     PathBuf::from(sysroot.trim())
143 }
144
145 fn get_host() -> String {
146     let rustc = rustc_test_suite().unwrap_or(PathBuf::from("rustc"));
147     let host = std::process::Command::new(rustc)
148         .arg("-vV")
149         .output()
150         .expect("rustc not found for -vV")
151         .stdout;
152     let host = std::str::from_utf8(&host).expect("sysroot is not utf8");
153     let host = host.split("\nhost: ").nth(1).expect(
154         "no host: part in rustc -vV",
155     );
156     let host = host.split('\n').next().expect("no \n after host");
157     String::from(host)
158 }
159
160 fn run_pass_miri(opt: bool) {
161     let sysroot = get_sysroot();
162     let host = get_host();
163
164     for_all_targets(&sysroot, |target| {
165         miri_pass(&sysroot, "tests/run-pass", &target, &host, false, opt);
166     });
167     miri_pass(&sysroot, "tests/run-pass-fullmir", &host, &host, true, opt);
168 }
169
170 fn compile_fail_miri(opt: bool) {
171     let sysroot = get_sysroot();
172     let host = get_host();
173
174     // FIXME: run tests for other targets, too
175     compile_fail(&sysroot, "tests/compile-fail", &host, &host, false, opt);
176     compile_fail(&sysroot, "tests/compile-fail-fullmir", &host, &host, true, opt);
177 }
178
179 fn test_runner(_tests: &[&()]) {
180     // We put everything into a single test to avoid the parallelism `cargo test`
181     // introduces.  We still get parallelism within our tests because `compiletest`
182     // uses `libtest` which runs jobs in parallel.
183
184     run_pass_miri(false);
185     run_pass_miri(true);
186
187     compile_fail_miri(false);
188     compile_fail_miri(true);
189 }