]> git.lizzy.rs Git - rust.git/blob - tests/compiletest.rs
Auto merge of #1124 - RalfJung:sysroot-check, r=oli-obk
[rust.git] / tests / compiletest.rs
1 #![feature(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::env;
6 use std::path::PathBuf;
7
8 use colored::*;
9 use compiletest_rs as compiletest;
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 run_tests(mode: &str, path: &str, target: &str, mut flags: Vec<String>) {
28     let in_rustc_test_suite = rustc_test_suite().is_some();
29     // Add some flags we always want.
30     flags.push("--edition 2018".to_owned());
31     if in_rustc_test_suite {
32         // Less aggressive warnings to make the rustc toolstate management less painful.
33         // (We often get warnings when e.g. a feature gets stabilized or some lint gets added/improved.)
34         flags.push("-Astable-features".to_owned());
35     } else {
36         flags.push("-Dwarnings -Dunused".to_owned()); // overwrite the -Aunused in compiletest-rs
37     }
38     if let Ok(sysroot) = std::env::var("MIRI_SYSROOT") {
39         flags.push(format!("--sysroot {}", sysroot));
40     }
41
42     // The rest of the configuration.
43     let mut config = compiletest::Config::default().tempdir();
44     config.mode = mode.parse().expect("Invalid mode");
45     config.rustc_path = miri_path();
46     if in_rustc_test_suite {
47         config.run_lib_path = rustc_lib_path();
48         config.compile_lib_path = rustc_lib_path();
49     }
50     config.filter = env::args().nth(1);
51     config.host = get_host();
52     config.src_base = PathBuf::from(path);
53     config.target = target.to_owned();
54     config.target_rustcflags = Some(flags.join(" "));
55     compiletest::run_tests(&config);
56 }
57
58 fn compile_fail(path: &str, target: &str, opt: bool) {
59     let opt_str = if opt { " with optimizations" } else { "" };
60     eprintln!(
61         "{}",
62         format!(
63             "## Running compile-fail tests in {} against miri for target {}{}",
64             path, target, opt_str
65         )
66         .green()
67         .bold()
68     );
69
70     let mut flags = Vec::new();
71     if opt {
72         // FIXME: Opt level 2 ICEs during stack trace generation.
73         // See https://github.com/rust-lang/rust/issues/66077.
74         flags.push("-Zmir-opt-level=1".to_owned());
75     }
76
77     run_tests("compile-fail", path, target, flags);
78 }
79
80 fn miri_pass(path: &str, target: &str, opt: bool) {
81     let opt_str = if opt { " with optimizations" } else { "" };
82     eprintln!(
83         "{}",
84         format!(
85             "## Running run-pass tests in {} against miri for target {}{}",
86             path, target, opt_str
87         )
88         .green()
89         .bold()
90     );
91
92     let mut flags = Vec::new();
93     if opt {
94         flags.push("-Zmir-opt-level=3".to_owned());
95     }
96
97     run_tests("ui", path, target, flags);
98 }
99
100 fn get_host() -> String {
101     let rustc = rustc_test_suite().unwrap_or(PathBuf::from("rustc"));
102     let rustc_version = std::process::Command::new(rustc)
103         .arg("-vV")
104         .output()
105         .expect("rustc not found for -vV")
106         .stdout;
107     let rustc_version = std::str::from_utf8(&rustc_version).expect("rustc -vV is not utf8");
108     let version_meta = rustc_version::version_meta_for(&rustc_version)
109         .expect("failed to parse rustc version info");
110     version_meta.host
111 }
112
113 fn get_target() -> String {
114     std::env::var("MIRI_TEST_TARGET").unwrap_or_else(|_| get_host())
115 }
116
117 fn run_pass_miri(opt: bool) {
118     miri_pass("tests/run-pass", &get_target(), opt);
119 }
120
121 fn compile_fail_miri(opt: bool) {
122     compile_fail("tests/compile-fail", &get_target(), opt);
123 }
124
125 fn test_runner(_tests: &[&()]) {
126     // Add a test env var to do environment communication tests
127     std::env::set_var("MIRI_ENV_VAR_TEST", "0");
128
129     run_pass_miri(false);
130     run_pass_miri(true);
131
132     compile_fail_miri(false);
133     compile_fail_miri(true);
134 }