1 #![feature(custom_test_frameworks)]
2 // Custom test runner, to avoid libtest being wrapped around compiletest which wraps libtest.
3 #![test_runner(test_runner)]
6 use std::path::PathBuf;
9 use compiletest_rs as compiletest;
11 fn miri_path() -> PathBuf {
12 PathBuf::from(option_env!("MIRI").unwrap_or(env!("CARGO_BIN_EXE_miri")))
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());
25 flags.push("-Dwarnings -Dunused".to_owned()); // overwrite the -Aunused in compiletest-rs
27 if let Ok(sysroot) = std::env::var("MIRI_SYSROOT") {
28 flags.push(format!("--sysroot {}", sysroot));
30 if let Ok(extra_flags) = std::env::var("MIRIFLAGS") {
31 flags.push(extra_flags);
34 let flags = flags.join(" ");
35 eprintln!(" Compiler flags: {}", flags);
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);
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);
53 fn compile_fail(path: &str, target: &str) {
56 format!("## Running compile-fail tests in {} against miri for target {}", path, target)
61 run_tests("compile-fail", path, target);
64 fn miri_pass(path: &str, target: &str) {
67 format!("## Running run-pass tests in {} against miri for target {}", path, target)
72 run_tests("ui", path, target);
75 fn get_host() -> String {
77 rustc_version::VersionMeta::for_command(std::process::Command::new(miri_path()))
78 .expect("failed to parse rustc version info");
82 fn get_target() -> String {
83 std::env::var("MIRI_TEST_TARGET").unwrap_or_else(|_| get_host())
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());
92 let target = get_target();
93 miri_pass("tests/run-pass", &target);
94 compile_fail("tests/compile-fail", &target);