]> git.lizzy.rs Git - rust.git/blob - tests/compile-test.rs
Add example to a perf lint
[rust.git] / tests / compile-test.rs
1 #![feature(test)]
2
3 use compiletest_rs as compiletest;
4 extern crate test;
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
50     // When we'll want to use `extern crate ..` for a dependency that is used
51     // both by the crate and the compiler itself, we can't simply pass -L flags
52     // as we'll get a duplicate matching versions. Instead, disambiguate with
53     // `--extern dep=path`.
54     // See https://github.com/rust-lang/rust-clippy/issues/4015.
55     let needs_disambiguation = ["serde", "regex", "clippy_lints"];
56     // This assumes that deps are compiled (they are for Cargo integration tests).
57     let deps = std::fs::read_dir(host_libs().join("deps")).unwrap();
58     let disambiguated = deps
59         .filter_map(|dep| {
60             let path = dep.ok()?.path();
61             let name = path.file_name()?.to_string_lossy();
62             // NOTE: This only handles a single dep
63             // https://github.com/laumann/compiletest-rs/issues/101
64             needs_disambiguation.iter().find_map(|dep| {
65                 if name.starts_with(&format!("lib{}-", dep)) && name.ends_with(".rlib") {
66                     Some(format!("--extern {}={}", dep, path.display()))
67                 } else {
68                     None
69                 }
70             })
71         })
72         .collect::<Vec<_>>();
73
74     config.target_rustcflags = Some(format!(
75         "-L {0} -L {0}/deps -Dwarnings -Zui-testing {1}",
76         host_libs().display(),
77         disambiguated.join(" ")
78     ));
79
80     config.mode = cfg_mode;
81     config.build_base = if rustc_test_suite().is_some() {
82         // we don't need access to the stderr files on travis
83         let mut path = PathBuf::from(env!("OUT_DIR"));
84         path.push("test_build_base");
85         path
86     } else {
87         let mut path = std::env::current_dir().unwrap();
88         path.push("target/debug/test_build_base");
89         path
90     };
91     config.src_base = dir;
92     config.rustc_path = clippy_driver_path();
93     config
94 }
95
96 fn run_mode(mode: &str, dir: PathBuf) {
97     let cfg = config(mode, dir);
98     compiletest::run_tests(&cfg);
99 }
100
101 #[allow(clippy::identity_conversion)]
102 fn run_ui_toml_tests(config: &compiletest::Config, mut tests: Vec<test::TestDescAndFn>) -> Result<bool, io::Error> {
103     let mut result = true;
104     let opts = compiletest::test_opts(config);
105     for dir in fs::read_dir(&config.src_base)? {
106         let dir = dir?;
107         if !dir.file_type()?.is_dir() {
108             continue;
109         }
110         let dir_path = dir.path();
111         set_var("CARGO_MANIFEST_DIR", &dir_path);
112         for file in fs::read_dir(&dir_path)? {
113             let file = file?;
114             let file_path = file.path();
115             if !file.file_type()?.is_file() {
116                 continue;
117             }
118             if file_path.extension() != Some(OsStr::new("rs")) {
119                 continue;
120             }
121             let paths = compiletest::common::TestPaths {
122                 file: file_path,
123                 base: config.src_base.clone(),
124                 relative_dir: dir_path.file_name().unwrap().into(),
125             };
126             let test_name = compiletest::make_test_name(&config, &paths);
127             let index = tests
128                 .iter()
129                 .position(|test| test.desc.name == test_name)
130                 .expect("The test should be in there");
131             result &= test::run_tests_console(&opts, vec![tests.swap_remove(index)])?;
132         }
133     }
134     Ok(result)
135 }
136
137 fn run_ui_toml() {
138     let path = PathBuf::from("tests/ui-toml").canonicalize().unwrap();
139     let config = config("ui", path);
140     let tests = compiletest::make_tests(&config);
141
142     let res = run_ui_toml_tests(&config, tests);
143     match res {
144         Ok(true) => {},
145         Ok(false) => panic!("Some tests failed"),
146         Err(e) => {
147             println!("I/O failure during tests: {:?}", e);
148         },
149     }
150 }
151
152 fn prepare_env() {
153     set_var("CLIPPY_DISABLE_DOCS_LINKS", "true");
154     set_var("CLIPPY_TESTS", "true");
155     //set_var("RUST_BACKTRACE", "0");
156 }
157
158 #[test]
159 fn compile_test() {
160     prepare_env();
161     run_mode("ui", "tests/ui".into());
162     run_ui_toml();
163 }