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