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