]> git.lizzy.rs Git - rust.git/blob - tests/compiletest.rs
colored test output!
[rust.git] / tests / compiletest.rs
1 #![feature(slice_concat_ext)]
2
3 extern crate compiletest_rs as compiletest;
4 extern crate colored;
5
6 use colored::*;
7
8 use std::slice::SliceConcatExt;
9 use std::path::{PathBuf, Path};
10 use std::io::Write;
11 use std::env;
12
13 macro_rules! eprintln {
14     ($($arg:tt)*) => {
15         let stderr = std::io::stderr();
16         writeln!(stderr.lock(), $($arg)*).unwrap();
17     }
18 }
19
20 fn miri_path() -> PathBuf {
21     if rustc_test_suite().is_some() {
22         PathBuf::from(option_env!("MIRI_PATH").unwrap())
23     } else {
24         PathBuf::from(concat!("target/", env!("PROFILE"), "/miri"))
25     }
26 }
27
28 fn rustc_test_suite() -> Option<PathBuf> {
29     option_env!("RUSTC_TEST_SUITE").map(PathBuf::from)
30 }
31
32 fn rustc_lib_path() -> PathBuf {
33     option_env!("RUSTC_LIB_PATH").unwrap().into()
34 }
35
36 fn have_fullmir() -> bool {
37     // We assume we have full MIR when MIRI_SYSROOT is set or when we are in rustc
38     std::env::var("MIRI_SYSROOT").is_ok() || rustc_test_suite().is_some()
39 }
40
41 fn compile_fail(sysroot: &Path, path: &str, target: &str, host: &str, need_fullmir: bool) {
42     if need_fullmir && !have_fullmir() {
43         eprintln!("{}", format!(
44             "## Skipping compile-fail tests in {} against miri for target {} due to missing mir",
45             path,
46             target
47         ).yellow().bold());
48         return;
49     }
50
51     eprintln!("{}", format!(
52         "## Running compile-fail tests in {} against miri for target {}",
53         path,
54         target
55     ).green().bold());
56     let mut config = compiletest::Config::default().tempdir();
57     config.mode = "compile-fail".parse().expect("Invalid mode");
58     config.rustc_path = miri_path();
59     let mut flags = Vec::new();
60     if rustc_test_suite().is_some() {
61         config.run_lib_path = rustc_lib_path();
62         config.compile_lib_path = rustc_lib_path();
63     }
64     flags.push(format!("--sysroot {}", sysroot.display()));
65     config.src_base = PathBuf::from(path.to_string());
66     flags.push("-Zmir-emit-validate=1".to_owned());
67     config.target_rustcflags = Some(flags.join(" "));
68     config.target = target.to_owned();
69     config.host = host.to_owned();
70     compiletest::run_tests(&config);
71 }
72
73 fn rustc_pass(sysroot: &Path, path: &str) {
74     eprintln!("{}", format!("## Running run-pass tests in {} against rustc", path).green().bold());
75     let mut config = compiletest::Config::default().tempdir();
76     config.mode = "run-pass".parse().expect("Invalid mode");
77     config.src_base = PathBuf::from(path);
78     if let Some(rustc_path) = rustc_test_suite() {
79         config.rustc_path = rustc_path;
80         config.run_lib_path = rustc_lib_path();
81         config.compile_lib_path = rustc_lib_path();
82         config.target_rustcflags = Some(format!("-Dwarnings --sysroot {}", sysroot.display()));
83     } else {
84         config.target_rustcflags = Some("-Dwarnings".to_owned());
85     }
86     config.host_rustcflags = Some("-Dwarnings".to_string());
87     compiletest::run_tests(&config);
88 }
89
90 fn miri_pass(sysroot: &Path, path: &str, target: &str, host: &str, need_fullmir: bool, opt: bool) {
91     if need_fullmir && !have_fullmir() {
92         eprintln!("{}", format!(
93             "## Skipping run-pass tests in {} against miri for target {} due to missing mir",
94             path,
95             target
96         ).yellow().bold());
97         return;
98     }
99
100     let opt_str = if opt { " with optimizations" } else { "" };
101     eprintln!("{}", format!(
102         "## Running run-pass tests in {} against miri for target {}{}",
103         path,
104         target,
105         opt_str
106     ).green().bold());
107     let mut config = compiletest::Config::default().tempdir();
108     config.mode = "ui".parse().expect("Invalid mode");
109     config.src_base = PathBuf::from(path);
110     config.target = target.to_owned();
111     config.host = host.to_owned();
112     config.rustc_path = miri_path();
113     if rustc_test_suite().is_some() {
114         config.run_lib_path = rustc_lib_path();
115         config.compile_lib_path = rustc_lib_path();
116     }
117     let mut flags = Vec::new();
118     flags.push(format!("--sysroot {}", sysroot.display()));
119     if have_fullmir() {
120         flags.push("-Zmiri-start-fn".to_owned());
121     }
122     if opt {
123         flags.push("-Zmir-opt-level=3".to_owned());
124     } else {
125         flags.push("-Zmir-opt-level=0".to_owned());
126         // For now, only validate without optimizations.  Inlining breaks validation.
127         flags.push("-Zmir-emit-validate=1".to_owned());
128     }
129     // Control miri logging. This is okay despite concurrent test execution as all tests
130     // will set this env var to the same value.
131     env::set_var("MIRI_LOG", "warn");
132     config.target_rustcflags = Some(flags.join(" "));
133     compiletest::run_tests(&config);
134 }
135
136 fn is_target_dir<P: Into<PathBuf>>(path: P) -> bool {
137     let mut path = path.into();
138     path.push("lib");
139     path.metadata().map(|m| m.is_dir()).unwrap_or(false)
140 }
141
142 fn for_all_targets<F: FnMut(String)>(sysroot: &Path, mut f: F) {
143     let target_dir = sysroot.join("lib").join("rustlib");
144     for entry in std::fs::read_dir(target_dir).expect("invalid sysroot") {
145         let entry = entry.unwrap();
146         if !is_target_dir(entry.path()) {
147             continue;
148         }
149         let target = entry.file_name().into_string().unwrap();
150         f(target);
151     }
152 }
153
154 fn get_sysroot() -> PathBuf {
155     let sysroot = std::env::var("MIRI_SYSROOT").unwrap_or_else(|_| {
156         let sysroot = std::process::Command::new("rustc")
157             .arg("--print")
158             .arg("sysroot")
159             .output()
160             .expect("rustc not found")
161             .stdout;
162         String::from_utf8(sysroot).expect("sysroot is not utf8")
163     });
164     PathBuf::from(sysroot.trim())
165 }
166
167 fn get_host() -> String {
168     let rustc = rustc_test_suite().unwrap_or(PathBuf::from("rustc"));
169     println!("using rustc at {}", rustc.display());
170     let host = std::process::Command::new(rustc)
171         .arg("-vV")
172         .output()
173         .expect("rustc not found for -vV")
174         .stdout;
175     let host = std::str::from_utf8(&host).expect("sysroot is not utf8");
176     let host = host.split("\nhost: ").nth(1).expect(
177         "no host: part in rustc -vV",
178     );
179     let host = host.split('\n').next().expect("no \n after host");
180     String::from(host)
181 }
182
183 fn run_pass_miri(opt: bool) {
184     let sysroot = get_sysroot();
185     let host = get_host();
186
187     for_all_targets(&sysroot, |target| {
188         miri_pass(&sysroot, "tests/run-pass", &target, &host, false, opt);
189     });
190     miri_pass(&sysroot, "tests/run-pass-fullmir", &host, &host, true, opt);
191 }
192
193 #[test]
194 fn run_pass_miri_noopt() {
195     run_pass_miri(false);
196 }
197
198 #[test]
199 #[ignore]
200 // FIXME: Disabled for now, as the optimizer is pretty broken and crashes...
201 // See https://github.com/rust-lang/rust/issues/50411
202 fn run_pass_miri_opt() {
203     run_pass_miri(true);
204 }
205
206 #[test]
207 fn run_pass_rustc() {
208     let sysroot = get_sysroot();
209     rustc_pass(&sysroot, "tests/run-pass");
210     rustc_pass(&sysroot, "tests/run-pass-fullmir");
211 }
212
213 #[test]
214 fn compile_fail_miri() {
215     let sysroot = get_sysroot();
216     let host = get_host();
217
218     // FIXME: run tests for other targets, too
219     compile_fail(&sysroot, "tests/compile-fail", &host, &host, false);
220     compile_fail(&sysroot, "tests/compile-fail-fullmir", &host, &host, true);
221 }