|
1 | 1 | extern crate compiletest_rs as compiletest;
|
2 | 2 |
|
3 |
| -use std::path::PathBuf; |
| 3 | +use std::path::{PathBuf, Path}; |
| 4 | +use std::io::Write; |
4 | 5 |
|
5 |
| -fn run_mode(mode: &'static str) { |
| 6 | +fn run_mode(dir: &'static str, mode: &'static str, sysroot: &str) { |
6 | 7 | // Disable rustc's new error fomatting. It breaks these tests.
|
7 | 8 | std::env::remove_var("RUST_NEW_ERROR_FORMAT");
|
8 |
| - |
9 |
| - // Taken from https://github.com/Manishearth/rust-clippy/pull/911. |
10 |
| - let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME")); |
11 |
| - let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN")); |
12 |
| - let sysroot = match (home, toolchain) { |
13 |
| - (Some(home), Some(toolchain)) => format!("{}/toolchains/{}", home, toolchain), |
14 |
| - _ => option_env!("RUST_SYSROOT") |
15 |
| - .expect("need to specify RUST_SYSROOT env var or use rustup or multirust") |
16 |
| - .to_owned(), |
17 |
| - }; |
18 | 9 | let flags = format!("--sysroot {} -Dwarnings", sysroot);
|
19 |
| - |
20 |
| - // FIXME: read directories in sysroot/lib/rustlib and generate the test targets from that |
21 |
| - let targets = &["x86_64-unknown-linux-gnu", "i686-unknown-linux-gnu"]; |
22 |
| - |
23 |
| - for &target in targets { |
24 |
| - use std::io::Write; |
25 |
| - let stderr = std::io::stderr(); |
26 |
| - write!(stderr.lock(), "running tests for target {}", target).unwrap(); |
| 10 | + for_all_targets(sysroot, |target| { |
27 | 11 | let mut config = compiletest::default_config();
|
28 | 12 | config.host_rustcflags = Some(flags.clone());
|
29 | 13 | config.mode = mode.parse().expect("Invalid mode");
|
30 |
| - config.run_lib_path = format!("{}/lib/rustlib/{}/lib", sysroot, target); |
| 14 | + config.run_lib_path = Path::new(sysroot).join("lib").join("rustlib").join(&target).join("lib"); |
31 | 15 | config.rustc_path = "target/debug/miri".into();
|
32 |
| - config.src_base = PathBuf::from(format!("tests/{}", mode)); |
| 16 | + config.src_base = PathBuf::from(format!("tests/{}", dir)); |
33 | 17 | config.target = target.to_owned();
|
34 | 18 | config.target_rustcflags = Some(flags.clone());
|
35 | 19 | compiletest::run_tests(&config);
|
| 20 | + }); |
| 21 | +} |
| 22 | + |
| 23 | +fn for_all_targets<F: FnMut(String)>(sysroot: &str, mut 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.file_name().into_string().unwrap(); |
| 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 | 36 | }
|
37 | 37 | }
|
38 | 38 |
|
39 | 39 | #[test]
|
40 | 40 | fn compile_test() {
|
41 |
| - run_mode("compile-fail"); |
42 |
| - run_mode("run-pass"); |
| 41 | + let mut failed = false; |
| 42 | + // Taken from https://github.com/Manishearth/rust-clippy/pull/911. |
| 43 | + let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME")); |
| 44 | + let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN")); |
| 45 | + let sysroot = match (home, toolchain) { |
| 46 | + (Some(home), Some(toolchain)) => format!("{}/toolchains/{}", home, toolchain), |
| 47 | + _ => option_env!("RUST_SYSROOT") |
| 48 | + .expect("need to specify RUST_SYSROOT env var or use rustup or multirust") |
| 49 | + .to_owned(), |
| 50 | + }; |
| 51 | + run_mode("compile-fail", "compile-fail", &sysroot); |
| 52 | + for_all_targets(&sysroot, |target| { |
| 53 | + for file in std::fs::read_dir("tests/run-pass").unwrap() { |
| 54 | + let file = file.unwrap(); |
| 55 | + if !file.metadata().unwrap().is_file() { |
| 56 | + continue; |
| 57 | + } |
| 58 | + let file = file.path(); |
| 59 | + let stderr = std::io::stderr(); |
| 60 | + write!(stderr.lock(), "test [miri-pass] {} ", file.to_str().unwrap()).unwrap(); |
| 61 | + let mut cmd = std::process::Command::new("target/debug/miri"); |
| 62 | + cmd.arg(file); |
| 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 | + match cmd.output() { |
| 70 | + Ok(ref output) if output.status.success() => writeln!(stderr.lock(), "ok").unwrap(), |
| 71 | + Ok(output) => { |
| 72 | + failed = true; |
| 73 | + writeln!(stderr.lock(), "FAILED with exit code {}", output.status.code().unwrap_or(0)).unwrap(); |
| 74 | + writeln!(stderr.lock(), "stdout: \n {}", std::str::from_utf8(&output.stdout).unwrap()).unwrap(); |
| 75 | + writeln!(stderr.lock(), "stderr: \n {}", std::str::from_utf8(&output.stderr).unwrap()).unwrap(); |
| 76 | + } |
| 77 | + Err(e) => { |
| 78 | + failed = true; |
| 79 | + writeln!(stderr.lock(), "FAILED: {}", e).unwrap(); |
| 80 | + }, |
| 81 | + } |
| 82 | + } |
| 83 | + let stderr = std::io::stderr(); |
| 84 | + writeln!(stderr.lock(), "").unwrap(); |
| 85 | + }); |
| 86 | + if failed { |
| 87 | + panic!("some tests failed"); |
| 88 | + } |
43 | 89 | }
|
0 commit comments