]> git.lizzy.rs Git - rust.git/blob - tests/compile-test.rs
Get compile-test tests for configuration working
[rust.git] / tests / compile-test.rs
1 #![feature(test)]
2
3 extern crate compiletest_rs as compiletest;
4 extern crate test;
5
6 use std::ffi::OsStr;
7 use std::fs;
8 use std::error::Error;
9 use std::env::{set_var, var};
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: &str) -> 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!("-L {0} -L {0}/deps -Dwarnings", host_libs().display()));
50
51     config.mode = cfg_mode;
52     config.build_base = if rustc_test_suite().is_some() {
53         // we don't need access to the stderr files on travis
54         let mut path = PathBuf::from(env!("OUT_DIR"));
55         path.push("test_build_base");
56         path
57     } else {
58         let mut path = std::env::current_dir().unwrap();
59         path.push("target/debug/test_build_base");
60         path
61     };
62     config.src_base = PathBuf::from(format!("tests/{}", dir));
63     config.rustc_path = clippy_driver_path();
64     config
65 }
66
67 fn run_mode(mode: &str, dir: &str) {
68     compiletest::run_tests(&config(mode, dir));
69 }
70
71 fn run_ui_toml() -> Result<(), Box<Error>> {
72     let base = PathBuf::from("tests/ui-toml/").canonicalize()?;
73     for dir in fs::read_dir(&base)? {
74         let dir = dir?;
75         if !dir.file_type()?.is_dir() {
76             continue;
77         }
78         let dir_path = dir.path();
79         set_var("CARGO_MANIFEST_DIR", &dir_path);
80         let config = config("ui", "ui-toml");
81         for file in fs::read_dir(&dir_path)? {
82             let file = file?;
83             let file_path = file.path();
84             if !file.file_type()?.is_file() {
85                 continue;
86             }
87             if file_path.extension() != Some(OsStr::new("rs")) {
88                 continue;
89             }
90             let paths = compiletest::common::TestPaths {
91                 file: file_path,
92                 base: base.clone(),
93                 relative_dir: dir_path.file_name().unwrap().into(),
94             };
95             compiletest::runtest::run(config.clone(), &paths);
96         }
97     }
98     Ok(())
99 }
100
101 fn prepare_env() {
102     set_var("CLIPPY_DISABLE_DOCS_LINKS", "true");
103     set_var("CLIPPY_TESTS", "true");
104     //set_var("RUST_BACKTRACE", "0");
105 }
106
107 #[test]
108 fn compile_test() {
109     prepare_env();
110     run_mode("run-pass", "run-pass");
111     run_mode("ui", "ui");
112     run_ui_toml().unwrap();
113 }