]> git.lizzy.rs Git - rust.git/blob - tests/compiletest.rs
Merge pull request #710 from RalfJung/travis
[rust.git] / tests / compiletest.rs
1 #![feature(slice_concat_ext, 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::slice::SliceConcatExt;
6 use std::path::PathBuf;
7 use std::env;
8
9 use compiletest_rs as compiletest;
10 use colored::*;
11
12 fn miri_path() -> PathBuf {
13     if rustc_test_suite().is_some() {
14         PathBuf::from(option_env!("MIRI_PATH").unwrap())
15     } else {
16         PathBuf::from(concat!("target/", env!("PROFILE"), "/miri"))
17     }
18 }
19
20 fn rustc_test_suite() -> Option<PathBuf> {
21     option_env!("RUSTC_TEST_SUITE").map(PathBuf::from)
22 }
23
24 fn rustc_lib_path() -> PathBuf {
25     option_env!("RUSTC_LIB_PATH").unwrap().into()
26 }
27
28 fn mk_config(mode: &str) -> compiletest::common::ConfigWithTemp {
29     let mut config = compiletest::Config::default().tempdir();
30     config.mode = mode.parse().expect("Invalid mode");
31     config.rustc_path = miri_path();
32     if rustc_test_suite().is_some() {
33         config.run_lib_path = rustc_lib_path();
34         config.compile_lib_path = rustc_lib_path();
35     }
36     config.filter = env::args().nth(1);
37     config.host = get_host();
38     config
39 }
40
41 fn compile_fail(path: &str, target: &str, opt: bool) {
42     let opt_str = if opt { " with optimizations" } else { "" };
43     eprintln!("{}", format!(
44         "## Running compile-fail tests in {} against miri for target {}{}",
45         path,
46         target,
47         opt_str
48     ).green().bold());
49
50     let mut flags = Vec::new();
51     flags.push("-Dwarnings -Dunused".to_owned()); // overwrite the -Aunused in compiletest-rs
52     flags.push("--edition 2018".to_owned());
53     if opt {
54         // Optimizing too aggressivley makes UB detection harder, but test at least
55         // the default value.
56         // FIXME: Opt level 3 ICEs during stack trace generation.
57         flags.push("-Zmir-opt-level=1".to_owned());
58     }
59
60     let mut config = mk_config("compile-fail");
61     config.src_base = PathBuf::from(path);
62     config.target = target.to_owned();
63     config.target_rustcflags = Some(flags.join(" "));
64     compiletest::run_tests(&config);
65 }
66
67 fn miri_pass(path: &str, target: &str, opt: bool) {
68     let opt_str = if opt { " with optimizations" } else { "" };
69     eprintln!("{}", format!(
70         "## Running run-pass tests in {} against miri for target {}{}",
71         path,
72         target,
73         opt_str
74     ).green().bold());
75
76     let mut flags = Vec::new();
77     flags.push("-Dwarnings -Dunused".to_owned()); // overwrite the -Aunused in compiletest-rs
78     flags.push("--edition 2018".to_owned());
79     if opt {
80         flags.push("-Zmir-opt-level=3".to_owned());
81     }
82
83     let mut config = mk_config("ui");
84     config.src_base = PathBuf::from(path);
85     config.target = target.to_owned();
86     config.target_rustcflags = Some(flags.join(" "));
87     compiletest::run_tests(&config);
88 }
89
90 fn get_host() -> String {
91     let rustc = rustc_test_suite().unwrap_or(PathBuf::from("rustc"));
92     let rustc_version = std::process::Command::new(rustc)
93         .arg("-vV")
94         .output()
95         .expect("rustc not found for -vV")
96         .stdout;
97     let rustc_version = std::str::from_utf8(&rustc_version).expect("rustc -vV is not utf8");
98     let version_meta = rustc_version::version_meta_for(&rustc_version)
99         .expect("failed to parse rustc version info");
100     version_meta.host
101 }
102
103 fn get_target() -> String {
104     std::env::var("MIRI_TEST_TARGET").unwrap_or_else(|_| get_host())
105 }
106
107 fn run_pass_miri(opt: bool) {
108     miri_pass("tests/run-pass", &get_target(), opt);
109 }
110
111 fn compile_fail_miri(opt: bool) {
112     compile_fail("tests/compile-fail", &get_target(), opt);
113 }
114
115 fn test_runner(_tests: &[&()]) {
116     run_pass_miri(false);
117     run_pass_miri(true);
118
119     compile_fail_miri(false);
120     compile_fail_miri(true);
121 }