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