]> git.lizzy.rs Git - rust.git/blob - tests/compile-test.rs
33b410dbd99aed12f6401c70f711751e81a15e20
[rust.git] / tests / compile-test.rs
1 #![feature(test)]
2
3 use compiletest_rs as compiletest;
4 use libtest::TestDescAndFn;
5
6 use std::env::{set_var, var};
7 use std::ffi::OsStr;
8 use std::fs;
9 use std::io;
10 use std::path::{Path, PathBuf};
11
12 fn clippy_driver_path() -> PathBuf {
13     if let Some(path) = option_env!("CLIPPY_DRIVER_PATH") {
14         PathBuf::from(path)
15     } else {
16         PathBuf::from(concat!("target/", env!("PROFILE"), "/clippy-driver"))
17     }
18 }
19
20 fn host_libs() -> PathBuf {
21     if let Some(path) = option_env!("HOST_LIBS") {
22         PathBuf::from(path)
23     } else {
24         Path::new("target").join(env!("PROFILE"))
25     }
26 }
27
28 fn rustc_test_suite() -> Option<PathBuf> {
29     option_env!("RUSTC_TEST_SUITE").map(PathBuf::from)
30 }
31
32 fn rustc_lib_path() -> PathBuf {
33     option_env!("RUSTC_LIB_PATH").unwrap().into()
34 }
35
36 fn config(mode: &str, dir: PathBuf) -> compiletest::Config {
37     let mut config = compiletest::Config::default();
38
39     let cfg_mode = mode.parse().expect("Invalid mode");
40     if let Ok(name) = var::<&str>("TESTNAME") {
41         let s: String = name.to_owned();
42         config.filter = Some(s)
43     }
44
45     if rustc_test_suite().is_some() {
46         config.run_lib_path = rustc_lib_path();
47         config.compile_lib_path = rustc_lib_path();
48     }
49     config.target_rustcflags = Some(format!(
50         "-L {0} -L {0}/deps -Dwarnings -Zui-testing",
51         host_libs().display()
52     ));
53
54     config.mode = cfg_mode;
55     config.build_base = if rustc_test_suite().is_some() {
56         // we don't need access to the stderr files on travis
57         let mut path = PathBuf::from(env!("OUT_DIR"));
58         path.push("test_build_base");
59         path
60     } else {
61         let mut path = std::env::current_dir().unwrap();
62         path.push("target/debug/test_build_base");
63         path
64     };
65     config.src_base = dir;
66     config.rustc_path = clippy_driver_path();
67     config
68 }
69
70 fn run_mode(mode: &str, dir: PathBuf) {
71     let cfg = config(mode, dir);
72     // clean rmeta data, otherwise "cargo check; cargo test" fails (#2896)
73     cfg.clean_rmeta();
74     compiletest::run_tests(&cfg);
75 }
76
77 fn run_ui_toml_tests(config: &compiletest::Config, mut tests: Vec<TestDescAndFn>) -> Result<bool, io::Error> {
78     let mut result = true;
79     let opts = compiletest::test_opts(config);
80     for dir in fs::read_dir(&config.src_base)? {
81         let dir_path = dir.unwrap().path();
82         set_var("CARGO_MANIFEST_DIR", &dir_path);
83         for file in fs::read_dir(&dir_path)? {
84             let file = file?;
85             let file_path = file.path();
86             if !file.file_type()?.is_file() {
87                 continue;
88             }
89             if file_path.extension() != Some(OsStr::new("rs")) {
90                 continue;
91             }
92             let paths = compiletest::common::TestPaths {
93                 file: file_path,
94                 base: config.src_base.clone(),
95                 relative_dir: dir_path.file_name().unwrap().into(),
96             };
97             let test_name = compiletest::make_test_name(&config, &paths);
98             let index = tests
99                 .iter()
100                 .position(|test| test.desc.name.to_string() == test_name.to_string())
101                 .expect("The test should be in there");
102             let opts = libtest::TestOpts {
103                 list: opts.list,
104                 filter: opts.filter.clone(),
105                 filter_exact: opts.filter_exact,
106                 exclude_should_panic: Default::default(),
107                 run_ignored: libtest::RunIgnored::No,
108                 run_tests: opts.run_tests,
109                 bench_benchmarks: opts.bench_benchmarks,
110                 logfile: opts.logfile.clone(),
111                 nocapture: opts.nocapture,
112                 color: libtest::ColorConfig::AutoColor,
113                 format: libtest::OutputFormat::Pretty,
114                 test_threads: opts.test_threads,
115                 skip: opts.skip.clone(),
116                 options: libtest::Options::new(),
117             };
118             result &= libtest::run_tests_console(&opts, vec![tests.swap_remove(index)])?;
119         }
120     }
121     Ok(result)
122 }
123
124 fn run_ui_toml() {
125     let path = PathBuf::from("tests/ui-toml").canonicalize().unwrap();
126     let config = config("ui", path);
127     let tests = compiletest::make_tests(&config);
128
129     let tests = tests
130         .into_iter()
131         .map(|test| {
132             libtest::TestDescAndFn {
133                 desc: libtest::TestDesc {
134                     name: libtest::TestName::DynTestName(test.desc.name.to_string()),
135                     ignore: test.desc.ignore,
136                     allow_fail: test.desc.allow_fail,
137                     should_panic: libtest::ShouldPanic::No,
138                 },
139                 // oli obk giving up
140                 testfn: unsafe { std::mem::transmute(test.testfn) },
141             }
142         })
143         .collect();
144
145     let res = run_ui_toml_tests(&config, tests);
146     match res {
147         Ok(true) => {},
148         Ok(false) => panic!("Some tests failed"),
149         Err(e) => {
150             println!("I/O failure during tests: {:?}", e);
151         },
152     }
153 }
154
155 fn prepare_env() {
156     set_var("CLIPPY_DISABLE_DOCS_LINKS", "true");
157     set_var("CLIPPY_TESTS", "true");
158     //set_var("RUST_BACKTRACE", "0");
159 }
160
161 #[test]
162 fn compile_test() {
163     prepare_env();
164     run_mode("ui", "tests/ui".into());
165     run_ui_toml();
166 }