]> git.lizzy.rs Git - rust.git/blob - tests/compiletest.rs
create a miri-pass test that allows us to run miri for arbitrary targets
[rust.git] / tests / compiletest.rs
1 extern crate compiletest_rs as compiletest;
2
3 use std::path::{PathBuf, Path};
4 use std::io::Write;
5
6 fn run_mode(dir: &'static str, mode: &'static str, sysroot: &str) {
7     // Disable rustc's new error fomatting. It breaks these tests.
8     std::env::remove_var("RUST_NEW_ERROR_FORMAT");
9     let flags = format!("--sysroot {} -Dwarnings", sysroot);
10     for_all_targets(sysroot, |target| {
11         let mut config = compiletest::default_config();
12         config.host_rustcflags = Some(flags.clone());
13         config.mode = mode.parse().expect("Invalid mode");
14         config.run_lib_path = Path::new(sysroot).join("lib").join("rustlib").join(&target).join("lib");
15         config.rustc_path = "target/debug/miri".into();
16         config.src_base = PathBuf::from(format!("tests/{}", dir));
17         config.target = target.to_owned();
18         config.target_rustcflags = Some(flags.clone());
19         compiletest::run_tests(&config);
20     });
21 }
22
23 fn for_all_targets<F: Fn(String)>(sysroot: &str, f: F) {
24     for target in std::fs::read_dir(format!("{}/lib/rustlib/", sysroot)).unwrap() {
25         let target = target.unwrap();
26         if !target.metadata().unwrap().is_dir() {
27             continue;
28         }
29         let target = target.path().iter().rev().next().unwrap().to_str().unwrap().to_owned();
30         if target == "etc" {
31             continue;
32         }
33         let stderr = std::io::stderr();
34         writeln!(stderr.lock(), "running tests for target {}", target).unwrap();
35         f(target);
36     }
37 }
38
39 #[test]
40 fn compile_test() {
41     // Taken from https://github.com/Manishearth/rust-clippy/pull/911.
42     let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));
43     let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN"));
44     let sysroot = match (home, toolchain) {
45         (Some(home), Some(toolchain)) => format!("{}/toolchains/{}", home, toolchain),
46         _ => option_env!("RUST_SYSROOT")
47             .expect("need to specify RUST_SYSROOT env var or use rustup or multirust")
48             .to_owned(),
49     };
50     run_mode("compile-fail", "compile-fail", &sysroot);
51     for_all_targets(&sysroot, |target| {
52         for file in std::fs::read_dir("tests/run-pass").unwrap() {
53             let file = file.unwrap();
54             if !file.metadata().unwrap().is_file() {
55                 continue;
56             }
57             let file = file.path();
58             let stderr = std::io::stderr();
59             writeln!(stderr.lock(), "test [miri-pass] {}", file.to_str().unwrap()).unwrap();
60             let mut cmd = std::process::Command::new("target/debug/miri");
61             cmd.arg(file);
62             cmd.arg(format!("--sysroot={}", sysroot));
63             cmd.arg("-Dwarnings");
64             cmd.arg(format!("-target={}", target));
65             let libs = Path::new(&sysroot).join("lib");
66             let sysroot = libs.join("rustlib").join(&target).join("lib");
67             let paths = std::env::join_paths(&[libs, sysroot]).unwrap();
68             cmd.env(compiletest::procsrv::dylib_env_var(), paths);
69         }
70         let stderr = std::io::stderr();
71         writeln!(stderr.lock(), "").unwrap();
72     })
73 }