]> git.lizzy.rs Git - rust.git/blob - tests/compiletest.rs
test harness informs tests about suitable temp dir
[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) {
28     let in_rustc_test_suite = rustc_test_suite().is_some();
29     // Add some flags we always want.
30     let mut flags = Vec::new();
31     flags.push("--edition 2018".to_owned());
32     if in_rustc_test_suite {
33         // Less aggressive warnings to make the rustc toolstate management less painful.
34         // (We often get warnings when e.g. a feature gets stabilized or some lint gets added/improved.)
35         flags.push("-Astable-features".to_owned());
36     } else {
37         flags.push("-Dwarnings -Dunused".to_owned()); // overwrite the -Aunused in compiletest-rs
38     }
39     if let Ok(sysroot) = std::env::var("MIRI_SYSROOT") {
40         flags.push(format!("--sysroot {}", sysroot));
41     }
42     if let Ok(extra_flags) = std::env::var("MIRI_TEST_FLAGS") {
43         flags.push(extra_flags);
44     }
45
46     let flags = flags.join(" ");
47     eprintln!("   Compiler flags: {}", flags);
48
49     // The rest of the configuration.
50     let mut config = compiletest::Config::default().tempdir();
51     config.mode = mode.parse().expect("Invalid mode");
52     config.rustc_path = miri_path();
53     if in_rustc_test_suite {
54         config.run_lib_path = rustc_lib_path();
55         config.compile_lib_path = rustc_lib_path();
56     }
57     config.filter = env::args().nth(1);
58     config.host = get_host();
59     config.src_base = PathBuf::from(path);
60     config.target = target.to_owned();
61     config.target_rustcflags = Some(flags);
62     compiletest::run_tests(&config);
63 }
64
65 fn compile_fail(path: &str, target: &str) {
66     eprintln!(
67         "{}",
68         format!(
69             "## Running compile-fail tests in {} against miri for target {}",
70             path, target
71         )
72         .green()
73         .bold()
74     );
75
76     run_tests("compile-fail", path, target);
77 }
78
79 fn miri_pass(path: &str, target: &str) {
80     eprintln!(
81         "{}",
82         format!(
83             "## Running run-pass tests in {} against miri for target {}",
84             path, target
85         )
86         .green()
87         .bold()
88     );
89
90     run_tests("ui", path, target);
91 }
92
93 fn get_host() -> String {
94     let rustc = rustc_test_suite().unwrap_or(PathBuf::from("rustc"));
95     let rustc_version = std::process::Command::new(rustc)
96         .arg("-vV")
97         .output()
98         .expect("rustc not found for -vV")
99         .stdout;
100     let rustc_version = std::str::from_utf8(&rustc_version).expect("rustc -vV is not utf8");
101     let version_meta = rustc_version::version_meta_for(&rustc_version)
102         .expect("failed to parse rustc version info");
103     version_meta.host
104 }
105
106 fn get_target() -> String {
107     std::env::var("MIRI_TEST_TARGET").unwrap_or_else(|_| get_host())
108 }
109
110 fn test_runner(_tests: &[&()]) {
111     // Add a test env var to do environment communication tests.
112     std::env::set_var("MIRI_ENV_VAR_TEST", "0");
113     // Let the tests know where to store temp files (they might run for a different target, which can make this hard to find).
114     std::env::set_var("MIRI_TEMP", std::env::temp_dir());
115
116     let target = get_target();
117     miri_pass("tests/run-pass", &target);
118     compile_fail("tests/compile-fail", &target);
119 }