]> git.lizzy.rs Git - rust.git/blob - tests/compile-test.rs
Auto merge of #7379 - popzxc:issue-7305, r=flip1995
[rust.git] / tests / compile-test.rs
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, remove_var, set_var, var_os};
8 use std::ffi::{OsStr, OsString};
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                         // Check which action should be done in order to remove compiled deps.
52                         // If pre-installed version of compiler is used, `cargo clean` will do.
53                         // Otherwise (for bootstrapped compiler), the dependencies directory
54                         // must be removed manually.
55                         let suggested_action = if std::env::var_os("RUSTC_BOOTSTRAP").is_some() {
56                             "remove the stageN-tools directory"
57                         } else {
58                             "run `cargo clean`"
59                         };
60
61                         panic!(
62                             "\n---------------------------------------------------\n\n \
63                             Found multiple rlibs for crate `{}`: `{:?}` and `{:?}`.\n \
64                             Probably, you need to {} before running tests again.\n \
65                             \nFor details on that error see https://github.com/rust-lang/rust-clippy/issues/7343 \
66                             \n---------------------------------------------------\n",
67                             dep, old, path, suggested_action
68                         );
69                     }
70                     break;
71                 }
72             }
73         }
74     }
75
76     let v: Vec<_> = crates
77         .into_iter()
78         .map(|(dep, path)| format!("--extern {}={}", dep, path.display()))
79         .collect();
80     v.join(" ")
81 }
82
83 fn default_config() -> compiletest::Config {
84     let mut config = compiletest::Config::default();
85
86     if let Ok(filters) = env::var("TESTNAME") {
87         config.filters = filters.split(',').map(std::string::ToString::to_string).collect();
88     }
89
90     if let Some(path) = option_env!("RUSTC_LIB_PATH") {
91         let path = PathBuf::from(path);
92         config.run_lib_path = path.clone();
93         config.compile_lib_path = path;
94     }
95
96     config.target_rustcflags = Some(format!(
97         "--emit=metadata -L {0} -L {1} -Dwarnings -Zui-testing {2}",
98         host_lib().join("deps").display(),
99         cargo::TARGET_LIB.join("deps").display(),
100         third_party_crates(),
101     ));
102
103     config.build_base = host_lib().join("test_build_base");
104     config.rustc_path = clippy_driver_path();
105     config
106 }
107
108 fn run_ui(cfg: &mut compiletest::Config) {
109     cfg.mode = TestMode::Ui;
110     cfg.src_base = Path::new("tests").join("ui");
111     // use tests/clippy.toml
112     let _g = VarGuard::set("CARGO_MANIFEST_DIR", std::fs::canonicalize("tests").unwrap());
113     compiletest::run_tests(cfg);
114 }
115
116 fn run_internal_tests(cfg: &mut compiletest::Config) {
117     // only run internal tests with the internal-tests feature
118     if !RUN_INTERNAL_TESTS {
119         return;
120     }
121     cfg.mode = TestMode::Ui;
122     cfg.src_base = Path::new("tests").join("ui-internal");
123     compiletest::run_tests(cfg);
124 }
125
126 fn run_ui_toml(config: &mut compiletest::Config) {
127     fn run_tests(config: &compiletest::Config, mut tests: Vec<tester::TestDescAndFn>) -> Result<bool, io::Error> {
128         let mut result = true;
129         let opts = compiletest::test_opts(config);
130         for dir in fs::read_dir(&config.src_base)? {
131             let dir = dir?;
132             if !dir.file_type()?.is_dir() {
133                 continue;
134             }
135             let dir_path = dir.path();
136             let _g = VarGuard::set("CARGO_MANIFEST_DIR", &dir_path);
137             for file in fs::read_dir(&dir_path)? {
138                 let file = file?;
139                 let file_path = file.path();
140                 if file.file_type()?.is_dir() {
141                     continue;
142                 }
143                 if file_path.extension() != Some(OsStr::new("rs")) {
144                     continue;
145                 }
146                 let paths = compiletest::common::TestPaths {
147                     file: file_path,
148                     base: config.src_base.clone(),
149                     relative_dir: dir_path.file_name().unwrap().into(),
150                 };
151                 let test_name = compiletest::make_test_name(config, &paths);
152                 let index = tests
153                     .iter()
154                     .position(|test| test.desc.name == test_name)
155                     .expect("The test should be in there");
156                 result &= tester::run_tests_console(&opts, vec![tests.swap_remove(index)])?;
157             }
158         }
159         Ok(result)
160     }
161
162     config.mode = TestMode::Ui;
163     config.src_base = Path::new("tests").join("ui-toml").canonicalize().unwrap();
164
165     let tests = compiletest::make_tests(config);
166
167     let res = run_tests(config, tests);
168     match res {
169         Ok(true) => {},
170         Ok(false) => panic!("Some tests failed"),
171         Err(e) => {
172             panic!("I/O failure during tests: {:?}", e);
173         },
174     }
175 }
176
177 fn run_ui_cargo(config: &mut compiletest::Config) {
178     fn run_tests(
179         config: &compiletest::Config,
180         filters: &[String],
181         mut tests: Vec<tester::TestDescAndFn>,
182     ) -> Result<bool, io::Error> {
183         let mut result = true;
184         let opts = compiletest::test_opts(config);
185
186         for dir in fs::read_dir(&config.src_base)? {
187             let dir = dir?;
188             if !dir.file_type()?.is_dir() {
189                 continue;
190             }
191
192             // Use the filter if provided
193             let dir_path = dir.path();
194             for filter in filters {
195                 if !dir_path.ends_with(filter) {
196                     continue;
197                 }
198             }
199
200             for case in fs::read_dir(&dir_path)? {
201                 let case = case?;
202                 if !case.file_type()?.is_dir() {
203                     continue;
204                 }
205
206                 let src_path = case.path().join("src");
207
208                 // When switching between branches, if the previous branch had a test
209                 // that the current branch does not have, the directory is not removed
210                 // because an ignored Cargo.lock file exists.
211                 if !src_path.exists() {
212                     continue;
213                 }
214
215                 env::set_current_dir(&src_path)?;
216                 for file in fs::read_dir(&src_path)? {
217                     let file = file?;
218                     if file.file_type()?.is_dir() {
219                         continue;
220                     }
221
222                     // Search for the main file to avoid running a test for each file in the project
223                     let file_path = file.path();
224                     match file_path.file_name().and_then(OsStr::to_str) {
225                         Some("main.rs") => {},
226                         _ => continue,
227                     }
228                     let _g = VarGuard::set("CLIPPY_CONF_DIR", case.path());
229                     let paths = compiletest::common::TestPaths {
230                         file: file_path,
231                         base: config.src_base.clone(),
232                         relative_dir: src_path.strip_prefix(&config.src_base).unwrap().into(),
233                     };
234                     let test_name = compiletest::make_test_name(config, &paths);
235                     let index = tests
236                         .iter()
237                         .position(|test| test.desc.name == test_name)
238                         .expect("The test should be in there");
239                     result &= tester::run_tests_console(&opts, vec![tests.swap_remove(index)])?;
240                 }
241             }
242         }
243         Ok(result)
244     }
245
246     if cargo::is_rustc_test_suite() {
247         return;
248     }
249
250     config.mode = TestMode::Ui;
251     config.src_base = Path::new("tests").join("ui-cargo").canonicalize().unwrap();
252
253     let tests = compiletest::make_tests(config);
254
255     let current_dir = env::current_dir().unwrap();
256     let res = run_tests(config, &config.filters, tests);
257     env::set_current_dir(current_dir).unwrap();
258
259     match res {
260         Ok(true) => {},
261         Ok(false) => panic!("Some tests failed"),
262         Err(e) => {
263             panic!("I/O failure during tests: {:?}", e);
264         },
265     }
266 }
267
268 fn prepare_env() {
269     set_var("CLIPPY_DISABLE_DOCS_LINKS", "true");
270     set_var("__CLIPPY_INTERNAL_TESTS", "true");
271     //set_var("RUST_BACKTRACE", "0");
272 }
273
274 #[test]
275 fn compile_test() {
276     prepare_env();
277     let mut config = default_config();
278     run_ui(&mut config);
279     run_ui_toml(&mut config);
280     run_ui_cargo(&mut config);
281     run_internal_tests(&mut config);
282 }
283
284 /// Restores an env var on drop
285 #[must_use]
286 struct VarGuard {
287     key: &'static str,
288     value: Option<OsString>,
289 }
290
291 impl VarGuard {
292     fn set(key: &'static str, val: impl AsRef<OsStr>) -> Self {
293         let value = var_os(key);
294         set_var(key, val);
295         Self { key, value }
296     }
297 }
298
299 impl Drop for VarGuard {
300     fn drop(&mut self) {
301         match self.value.as_deref() {
302             None => remove_var(self.key),
303             Some(value) => set_var(self.key, value),
304         }
305     }
306 }