]> git.lizzy.rs Git - rust.git/blob - tests/compiletest.rs
reenable all tests on Windows
[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::path::PathBuf;
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 run_tests(mode: &str, path: &str, target: &str, mut flags: Vec<String>) {
28     // Some flags we always want.
29     flags.push("-Dwarnings -Dunused".to_owned()); // overwrite the -Aunused in compiletest-rs
30     flags.push("--edition 2018".to_owned());
31     if let Ok(sysroot) = std::env::var("MIRI_SYSROOT") {
32         flags.push(format!("--sysroot {}", sysroot));
33     }
34
35     // The rest of the configuration.
36     let mut config = compiletest::Config::default().tempdir();
37     config.mode = mode.parse().expect("Invalid mode");
38     config.rustc_path = miri_path();
39     if rustc_test_suite().is_some() {
40         config.run_lib_path = rustc_lib_path();
41         config.compile_lib_path = rustc_lib_path();
42     }
43     config.filter = env::args().nth(1);
44     config.host = get_host();
45     config.src_base = PathBuf::from(path);
46     config.target = target.to_owned();
47     config.target_rustcflags = Some(flags.join(" "));
48     compiletest::run_tests(&config);
49 }
50
51 fn compile_fail(path: &str, target: &str, opt: bool) {
52     let opt_str = if opt { " with optimizations" } else { "" };
53     eprintln!("{}", format!(
54         "## Running compile-fail tests in {} against miri for target {}{}",
55         path,
56         target,
57         opt_str
58     ).green().bold());
59
60     let mut flags = Vec::new();
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     run_tests("compile-fail", path, target, flags);
69 }
70
71 fn miri_pass(path: &str, target: &str, opt: bool, noseed: bool) {
72     let opt_str = if opt { " with optimizations" } else { "" };
73     eprintln!("{}", format!(
74         "## Running run-pass tests in {} against miri for target {}{}",
75         path,
76         target,
77         opt_str
78     ).green().bold());
79
80     let mut flags = Vec::new();
81     if opt {
82         flags.push("-Zmir-opt-level=3".to_owned());
83     } else if !noseed {
84         // Run with intptrcast.  Avoid test matrix explosion by doing either this or opt-level=3.
85         flags.push("-Zmiri-seed=".to_owned());
86     }
87
88     run_tests("ui", path, target, flags);
89 }
90
91 fn get_host() -> String {
92     let rustc = rustc_test_suite().unwrap_or(PathBuf::from("rustc"));
93     let rustc_version = std::process::Command::new(rustc)
94         .arg("-vV")
95         .output()
96         .expect("rustc not found for -vV")
97         .stdout;
98     let rustc_version = std::str::from_utf8(&rustc_version).expect("rustc -vV is not utf8");
99     let version_meta = rustc_version::version_meta_for(&rustc_version)
100         .expect("failed to parse rustc version info");
101     version_meta.host
102 }
103
104 fn get_target() -> String {
105     std::env::var("MIRI_TEST_TARGET").unwrap_or_else(|_| get_host())
106 }
107
108 fn run_pass_miri(opt: bool) {
109     miri_pass("tests/run-pass", &get_target(), opt, false);
110     miri_pass("tests/run-pass-noseed", &get_target(), opt, true);
111 }
112
113 fn compile_fail_miri(opt: bool) {
114     compile_fail("tests/compile-fail", &get_target(), opt);
115 }
116
117 fn test_runner(_tests: &[&()]) {
118     run_pass_miri(false);
119     run_pass_miri(true);
120
121     compile_fail_miri(false);
122     compile_fail_miri(true);
123 }