]> git.lizzy.rs Git - rust.git/blob - tests/compiletest.rs
cargo-miri: allow overwriting miri command, and make that consistent with compiletest
[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     PathBuf::from(option_env!("MIRI").unwrap_or(env!("CARGO_BIN_EXE_miri")))
13 }
14
15 fn run_tests(mode: &str, path: &str, target: &str) {
16     let in_rustc_test_suite = option_env!("RUSTC_STAGE").is_some();
17     // Add some flags we always want.
18     let mut flags = Vec::new();
19     flags.push("--edition 2018".to_owned());
20     if in_rustc_test_suite {
21         // Less aggressive warnings to make the rustc toolstate management less painful.
22         // (We often get warnings when e.g. a feature gets stabilized or some lint gets added/improved.)
23         flags.push("-Astable-features".to_owned());
24     } else {
25         flags.push("-Dwarnings -Dunused".to_owned()); // overwrite the -Aunused in compiletest-rs
26     }
27     if let Ok(sysroot) = std::env::var("MIRI_SYSROOT") {
28         flags.push(format!("--sysroot {}", sysroot));
29     }
30     if let Ok(extra_flags) = std::env::var("MIRI_TEST_FLAGS") {
31         flags.push(extra_flags);
32     }
33
34     let flags = flags.join(" ");
35     eprintln!("   Compiler flags: {}", flags);
36
37     // The rest of the configuration.
38     let mut config = compiletest::Config::default().tempdir();
39     config.mode = mode.parse().expect("Invalid mode");
40     config.rustc_path = miri_path();
41     if let Some(lib_path) = option_env!("RUSTC_LIB_PATH") {
42         config.run_lib_path = PathBuf::from(lib_path);
43         config.compile_lib_path = PathBuf::from(lib_path);
44     }
45     config.filter = env::args().nth(1);
46     config.host = get_host();
47     config.src_base = PathBuf::from(path);
48     config.target = target.to_owned();
49     config.target_rustcflags = Some(flags);
50     compiletest::run_tests(&config);
51 }
52
53 fn compile_fail(path: &str, target: &str) {
54     eprintln!(
55         "{}",
56         format!("## Running compile-fail tests in {} against miri for target {}", path, target)
57             .green()
58             .bold()
59     );
60
61     run_tests("compile-fail", path, target);
62 }
63
64 fn miri_pass(path: &str, target: &str) {
65     eprintln!(
66         "{}",
67         format!("## Running run-pass tests in {} against miri for target {}", path, target)
68             .green()
69             .bold()
70     );
71
72     run_tests("ui", path, target);
73 }
74
75 fn get_host() -> String {
76     let version_meta =
77         rustc_version::VersionMeta::for_command(std::process::Command::new(miri_path()))
78             .expect("failed to parse rustc version info");
79     version_meta.host
80 }
81
82 fn get_target() -> String {
83     std::env::var("MIRI_TEST_TARGET").unwrap_or_else(|_| get_host())
84 }
85
86 fn test_runner(_tests: &[&()]) {
87     // Add a test env var to do environment communication tests.
88     std::env::set_var("MIRI_ENV_VAR_TEST", "0");
89     // Let the tests know where to store temp files (they might run for a different target, which can make this hard to find).
90     std::env::set_var("MIRI_TEMP", std::env::temp_dir());
91
92     let target = get_target();
93     miri_pass("tests/run-pass", &target);
94     compile_fail("tests/compile-fail", &target);
95 }