]> git.lizzy.rs Git - rust.git/blob - tests/compiletest.rs
Merge branch 'master' into stacked-borrows-2-phase
[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     flags.push("--edition 2018".to_owned());
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);
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     flags.push("--edition 2018".to_owned());
103     if opt {
104         // FIXME: We use opt level 1 because MIR inlining defeats the validation
105         // whitelist.
106         flags.push("-Zmir-opt-level=1".to_owned());
107     }
108     if !have_fullmir() {
109         // Validation relies on the EscapeToRaw statements being emitted
110         flags.push("-Zmiri-disable-validation".to_owned());
111     }
112
113     let mut config = mk_config("ui");
114     config.src_base = PathBuf::from(path);
115     config.target = target.to_owned();
116     config.host = host.to_owned();
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 target_has_std<P: Into<PathBuf>>(path: P) -> bool {
128     let mut path = path.into();
129     path.push("lib");
130     std::fs::read_dir(path)
131         .expect("invalid target")
132         .map(|entry| entry.unwrap())
133         .filter(|entry| entry.file_type().unwrap().is_file())
134         .filter_map(|entry| entry.file_name().into_string().ok())
135         .any(|file_name| file_name.starts_with("libstd") && file_name.ends_with(".rlib"))
136 }
137
138
139 fn for_all_targets<F: FnMut(String)>(sysroot: &Path, f: F) {
140     let target_dir = sysroot.join("lib").join("rustlib");
141     let mut targets = std::fs::read_dir(target_dir)
142         .expect("invalid sysroot")
143         .map(|entry| entry.unwrap())
144         .filter(|entry| is_target_dir(entry.path()))
145         .filter(|entry| target_has_std(entry.path()))
146         .map(|entry| entry.file_name().into_string().unwrap())
147         .peekable();
148
149     if targets.peek().is_none() {
150         panic!("No valid targets found");
151     }
152
153     targets.for_each(f);
154 }
155
156 fn get_sysroot() -> PathBuf {
157     let sysroot = std::env::var("MIRI_SYSROOT").unwrap_or_else(|_| {
158         let sysroot = std::process::Command::new("rustc")
159             .arg("--print")
160             .arg("sysroot")
161             .output()
162             .expect("rustc not found")
163             .stdout;
164         String::from_utf8(sysroot).expect("sysroot is not utf8")
165     });
166     PathBuf::from(sysroot.trim())
167 }
168
169 fn get_host() -> String {
170     let rustc = rustc_test_suite().unwrap_or(PathBuf::from("rustc"));
171     let host = std::process::Command::new(rustc)
172         .arg("-vV")
173         .output()
174         .expect("rustc not found for -vV")
175         .stdout;
176     let host = std::str::from_utf8(&host).expect("sysroot is not utf8");
177     let host = host.split("\nhost: ").nth(1).expect(
178         "no host: part in rustc -vV",
179     );
180     let host = host.split('\n').next().expect("no \n after host");
181     String::from(host)
182 }
183
184 fn run_pass_miri(opt: bool) {
185     let sysroot = get_sysroot();
186     let host = get_host();
187
188     for_all_targets(&sysroot, |target| {
189         miri_pass(&sysroot, "tests/run-pass", &target, &host, false, opt);
190     });
191     miri_pass(&sysroot, "tests/run-pass-fullmir", &host, &host, true, opt);
192 }
193
194 fn compile_fail_miri(opt: bool) {
195     let sysroot = get_sysroot();
196     let host = get_host();
197
198     // FIXME: run tests for other targets, too
199     compile_fail(&sysroot, "tests/compile-fail", &host, &host, false, opt);
200     compile_fail(&sysroot, "tests/compile-fail-fullmir", &host, &host, true, opt);
201 }
202
203 fn test_runner(_tests: &[&()]) {
204     // We put everything into a single test to avoid the parallelism `cargo test`
205     // introduces.  We still get parallelism within our tests because `compiletest`
206     // uses `libtest` which runs jobs in parallel.
207
208     run_pass_miri(false);
209     run_pass_miri(true);
210
211     compile_fail_miri(false);
212     compile_fail_miri(true);
213 }