]>
Commit | Line | Data |
---|---|---|
f20569fa XL |
1 | #![feature(test)] // compiletest_rs requires this attribute |
2 | #![feature(once_cell)] | |
3 | ||
4 | use compiletest_rs as compiletest; | |
5 | use compiletest_rs::common::Mode as TestMode; | |
6 | ||
7 | use std::env::{self, set_var, var}; | |
8 | use std::ffi::OsStr; | |
9 | use std::fs; | |
10 | use std::io; | |
11 | use std::path::{Path, PathBuf}; | |
12 | ||
13 | mod cargo; | |
14 | ||
15 | // whether to run internal tests or not | |
16 | const RUN_INTERNAL_TESTS: bool = cfg!(feature = "internal-lints"); | |
17 | ||
18 | fn host_lib() -> PathBuf { | |
19 | option_env!("HOST_LIBS").map_or(cargo::CARGO_TARGET_DIR.join(env!("PROFILE")), PathBuf::from) | |
20 | } | |
21 | ||
22 | fn clippy_driver_path() -> PathBuf { | |
23 | option_env!("CLIPPY_DRIVER_PATH").map_or(cargo::TARGET_LIB.join("clippy-driver"), PathBuf::from) | |
24 | } | |
25 | ||
26 | // When we'll want to use `extern crate ..` for a dependency that is used | |
27 | // both by the crate and the compiler itself, we can't simply pass -L flags | |
28 | // as we'll get a duplicate matching versions. Instead, disambiguate with | |
29 | // `--extern dep=path`. | |
30 | // See https://github.com/rust-lang/rust-clippy/issues/4015. | |
31 | // | |
32 | // FIXME: We cannot use `cargo build --message-format=json` to resolve to dependency files. | |
33 | // Because it would force-rebuild if the options passed to `build` command is not the same | |
34 | // as what we manually pass to `cargo` invocation | |
35 | fn third_party_crates() -> String { | |
36 | use std::collections::HashMap; | |
37 | static CRATES: &[&str] = &["serde", "serde_derive", "regex", "clippy_lints", "syn", "quote"]; | |
38 | let dep_dir = cargo::TARGET_LIB.join("deps"); | |
39 | let mut crates: HashMap<&str, PathBuf> = HashMap::with_capacity(CRATES.len()); | |
40 | for entry in fs::read_dir(dep_dir).unwrap() { | |
41 | let path = match entry { | |
42 | Ok(entry) => entry.path(), | |
43 | Err(_) => continue, | |
44 | }; | |
45 | if let Some(name) = path.file_name().and_then(OsStr::to_str) { | |
46 | for dep in CRATES { | |
47 | if name.starts_with(&format!("lib{}-", dep)) | |
48 | && name.rsplit('.').next().map(|ext| ext.eq_ignore_ascii_case("rlib")) == Some(true) | |
49 | { | |
50 | if let Some(old) = crates.insert(dep, path.clone()) { | |
51 | panic!("Found multiple rlibs for crate `{}`: `{:?}` and `{:?}", dep, old, path); | |
52 | } | |
53 | break; | |
54 | } | |
55 | } | |
56 | } | |
57 | } | |
58 | ||
59 | let v: Vec<_> = crates | |
60 | .into_iter() | |
61 | .map(|(dep, path)| format!("--extern {}={}", dep, path.display())) | |
62 | .collect(); | |
63 | v.join(" ") | |
64 | } | |
65 | ||
66 | fn default_config() -> compiletest::Config { | |
67 | let mut config = compiletest::Config::default(); | |
68 | ||
69 | if let Ok(filters) = env::var("TESTNAME") { | |
70 | config.filters = filters.split(',').map(std::string::ToString::to_string).collect(); | |
71 | } | |
72 | ||
73 | if let Some(path) = option_env!("RUSTC_LIB_PATH") { | |
74 | let path = PathBuf::from(path); | |
75 | config.run_lib_path = path.clone(); | |
76 | config.compile_lib_path = path; | |
77 | } | |
78 | ||
79 | config.target_rustcflags = Some(format!( | |
80 | "--emit=metadata -L {0} -L {1} -Dwarnings -Zui-testing {2}", | |
81 | host_lib().join("deps").display(), | |
82 | cargo::TARGET_LIB.join("deps").display(), | |
83 | third_party_crates(), | |
84 | )); | |
85 | ||
86 | config.build_base = if cargo::is_rustc_test_suite() { | |
87 | // This make the stderr files go to clippy OUT_DIR on rustc repo build dir | |
88 | let mut path = PathBuf::from(env!("OUT_DIR")); | |
89 | path.push("test_build_base"); | |
90 | path | |
91 | } else { | |
92 | host_lib().join("test_build_base") | |
93 | }; | |
94 | config.rustc_path = clippy_driver_path(); | |
95 | config | |
96 | } | |
97 | ||
98 | fn run_mode(cfg: &mut compiletest::Config) { | |
99 | cfg.mode = TestMode::Ui; | |
100 | cfg.src_base = Path::new("tests").join("ui"); | |
101 | compiletest::run_tests(&cfg); | |
102 | } | |
103 | ||
104 | fn run_internal_tests(cfg: &mut compiletest::Config) { | |
105 | // only run internal tests with the internal-tests feature | |
106 | if !RUN_INTERNAL_TESTS { | |
107 | return; | |
108 | } | |
109 | cfg.mode = TestMode::Ui; | |
110 | cfg.src_base = Path::new("tests").join("ui-internal"); | |
111 | compiletest::run_tests(&cfg); | |
112 | } | |
113 | ||
114 | fn run_ui_toml(config: &mut compiletest::Config) { | |
115 | fn run_tests(config: &compiletest::Config, mut tests: Vec<tester::TestDescAndFn>) -> Result<bool, io::Error> { | |
116 | let mut result = true; | |
117 | let opts = compiletest::test_opts(config); | |
118 | for dir in fs::read_dir(&config.src_base)? { | |
119 | let dir = dir?; | |
120 | if !dir.file_type()?.is_dir() { | |
121 | continue; | |
122 | } | |
123 | let dir_path = dir.path(); | |
124 | set_var("CARGO_MANIFEST_DIR", &dir_path); | |
125 | for file in fs::read_dir(&dir_path)? { | |
126 | let file = file?; | |
127 | let file_path = file.path(); | |
128 | if file.file_type()?.is_dir() { | |
129 | continue; | |
130 | } | |
131 | if file_path.extension() != Some(OsStr::new("rs")) { | |
132 | continue; | |
133 | } | |
134 | let paths = compiletest::common::TestPaths { | |
135 | file: file_path, | |
136 | base: config.src_base.clone(), | |
137 | relative_dir: dir_path.file_name().unwrap().into(), | |
138 | }; | |
139 | let test_name = compiletest::make_test_name(&config, &paths); | |
140 | let index = tests | |
141 | .iter() | |
142 | .position(|test| test.desc.name == test_name) | |
143 | .expect("The test should be in there"); | |
144 | result &= tester::run_tests_console(&opts, vec![tests.swap_remove(index)])?; | |
145 | } | |
146 | } | |
147 | Ok(result) | |
148 | } | |
149 | ||
150 | config.mode = TestMode::Ui; | |
151 | config.src_base = Path::new("tests").join("ui-toml").canonicalize().unwrap(); | |
152 | ||
153 | let tests = compiletest::make_tests(&config); | |
154 | ||
155 | let manifest_dir = var("CARGO_MANIFEST_DIR").unwrap_or_default(); | |
156 | let res = run_tests(&config, tests); | |
157 | set_var("CARGO_MANIFEST_DIR", &manifest_dir); | |
158 | match res { | |
159 | Ok(true) => {}, | |
160 | Ok(false) => panic!("Some tests failed"), | |
161 | Err(e) => { | |
162 | panic!("I/O failure during tests: {:?}", e); | |
163 | }, | |
164 | } | |
165 | } | |
166 | ||
167 | fn run_ui_cargo(config: &mut compiletest::Config) { | |
168 | fn run_tests( | |
169 | config: &compiletest::Config, | |
170 | filters: &[String], | |
171 | mut tests: Vec<tester::TestDescAndFn>, | |
172 | ) -> Result<bool, io::Error> { | |
173 | let mut result = true; | |
174 | let opts = compiletest::test_opts(config); | |
175 | ||
176 | for dir in fs::read_dir(&config.src_base)? { | |
177 | let dir = dir?; | |
178 | if !dir.file_type()?.is_dir() { | |
179 | continue; | |
180 | } | |
181 | ||
182 | // Use the filter if provided | |
183 | let dir_path = dir.path(); | |
184 | for filter in filters { | |
185 | if !dir_path.ends_with(filter) { | |
186 | continue; | |
187 | } | |
188 | } | |
189 | ||
190 | for case in fs::read_dir(&dir_path)? { | |
191 | let case = case?; | |
192 | if !case.file_type()?.is_dir() { | |
193 | continue; | |
194 | } | |
195 | ||
196 | let src_path = case.path().join("src"); | |
197 | ||
198 | // When switching between branches, if the previous branch had a test | |
199 | // that the current branch does not have, the directory is not removed | |
200 | // because an ignored Cargo.lock file exists. | |
201 | if !src_path.exists() { | |
202 | continue; | |
203 | } | |
204 | ||
205 | env::set_current_dir(&src_path)?; | |
206 | for file in fs::read_dir(&src_path)? { | |
207 | let file = file?; | |
208 | if file.file_type()?.is_dir() { | |
209 | continue; | |
210 | } | |
211 | ||
212 | // Search for the main file to avoid running a test for each file in the project | |
213 | let file_path = file.path(); | |
214 | match file_path.file_name().and_then(OsStr::to_str) { | |
215 | Some("main.rs") => {}, | |
216 | _ => continue, | |
217 | } | |
218 | set_var("CLIPPY_CONF_DIR", case.path()); | |
219 | let paths = compiletest::common::TestPaths { | |
220 | file: file_path, | |
221 | base: config.src_base.clone(), | |
222 | relative_dir: src_path.strip_prefix(&config.src_base).unwrap().into(), | |
223 | }; | |
224 | let test_name = compiletest::make_test_name(&config, &paths); | |
225 | let index = tests | |
226 | .iter() | |
227 | .position(|test| test.desc.name == test_name) | |
228 | .expect("The test should be in there"); | |
229 | result &= tester::run_tests_console(&opts, vec![tests.swap_remove(index)])?; | |
230 | } | |
231 | } | |
232 | } | |
233 | Ok(result) | |
234 | } | |
235 | ||
236 | if cargo::is_rustc_test_suite() { | |
237 | return; | |
238 | } | |
239 | ||
240 | config.mode = TestMode::Ui; | |
241 | config.src_base = Path::new("tests").join("ui-cargo").canonicalize().unwrap(); | |
242 | ||
243 | let tests = compiletest::make_tests(&config); | |
244 | ||
245 | let current_dir = env::current_dir().unwrap(); | |
246 | let conf_dir = var("CLIPPY_CONF_DIR").unwrap_or_default(); | |
247 | let res = run_tests(&config, &config.filters, tests); | |
248 | env::set_current_dir(current_dir).unwrap(); | |
249 | set_var("CLIPPY_CONF_DIR", conf_dir); | |
250 | ||
251 | match res { | |
252 | Ok(true) => {}, | |
253 | Ok(false) => panic!("Some tests failed"), | |
254 | Err(e) => { | |
255 | panic!("I/O failure during tests: {:?}", e); | |
256 | }, | |
257 | } | |
258 | } | |
259 | ||
260 | fn prepare_env() { | |
261 | set_var("CLIPPY_DISABLE_DOCS_LINKS", "true"); | |
262 | set_var("__CLIPPY_INTERNAL_TESTS", "true"); | |
263 | //set_var("RUST_BACKTRACE", "0"); | |
264 | } | |
265 | ||
266 | #[test] | |
267 | fn compile_test() { | |
268 | prepare_env(); | |
269 | let mut config = default_config(); | |
270 | run_mode(&mut config); | |
271 | run_ui_toml(&mut config); | |
272 | run_ui_cargo(&mut config); | |
273 | run_internal_tests(&mut config); | |
274 | } |