1 #![feature(test)] // compiletest_rs requires this attribute
3 use compiletest_rs as compiletest;
4 use compiletest_rs::common::Mode as TestMode;
6 use std::env::{self, set_var};
10 use std::path::{Path, PathBuf};
14 fn host_lib() -> PathBuf {
15 option_env!("HOST_LIBS").map_or(cargo::CARGO_TARGET_DIR.join(env!("PROFILE")), PathBuf::from)
18 fn clippy_driver_path() -> PathBuf {
19 option_env!("CLIPPY_DRIVER_PATH").map_or(cargo::TARGET_LIB.join("clippy-driver"), PathBuf::from)
22 // When we'll want to use `extern crate ..` for a dependency that is used
23 // both by the crate and the compiler itself, we can't simply pass -L flags
24 // as we'll get a duplicate matching versions. Instead, disambiguate with
25 // `--extern dep=path`.
26 // See https://github.com/rust-lang/rust-clippy/issues/4015.
28 // FIXME: We cannot use `cargo build --message-format=json` to resolve to dependency files.
29 // Because it would force-rebuild if the options passed to `build` command is not the same
30 // as what we manually pass to `cargo` invocation
31 fn third_party_crates() -> String {
32 use std::collections::HashMap;
33 static CRATES: &[&str] = &["serde", "serde_derive", "regex", "clippy_lints", "syn", "quote"];
34 let dep_dir = cargo::TARGET_LIB.join("deps");
35 let mut crates: HashMap<&str, PathBuf> = HashMap::with_capacity(CRATES.len());
36 for entry in fs::read_dir(dep_dir).unwrap() {
37 let path = match entry {
38 Ok(entry) => entry.path(),
41 if let Some(name) = path.file_name().and_then(OsStr::to_str) {
43 if name.starts_with(&format!("lib{}-", dep)) && name.ends_with(".rlib") {
44 if let Some(old) = crates.insert(dep, path.clone()) {
45 panic!("Found multiple rlibs for crate `{}`: `{:?}` and `{:?}", dep, old, path);
53 let v: Vec<_> = crates
55 .map(|(dep, path)| format!("--extern {}={}", dep, path.display()))
60 fn default_config() -> compiletest::Config {
61 let mut config = compiletest::Config::default();
63 if let Ok(name) = env::var("TESTNAME") {
64 config.filter = Some(name);
67 if let Some(path) = option_env!("RUSTC_LIB_PATH") {
68 let path = PathBuf::from(path);
69 config.run_lib_path = path.clone();
70 config.compile_lib_path = path;
73 config.target_rustcflags = Some(format!(
74 "-L {0} -L {1} -Dwarnings -Zui-testing {2}",
75 host_lib().join("deps").display(),
76 cargo::TARGET_LIB.join("deps").display(),
80 config.build_base = if cargo::is_rustc_test_suite() {
81 // This make the stderr files go to clippy OUT_DIR on rustc repo build dir
82 let mut path = PathBuf::from(env!("OUT_DIR"));
83 path.push("test_build_base");
86 host_lib().join("test_build_base")
88 config.rustc_path = clippy_driver_path();
92 fn run_mode(cfg: &mut compiletest::Config) {
93 cfg.mode = TestMode::Ui;
94 cfg.src_base = Path::new("tests").join("ui");
95 compiletest::run_tests(&cfg);
98 fn run_ui_toml(config: &mut compiletest::Config) {
99 fn run_tests(config: &compiletest::Config, mut tests: Vec<tester::TestDescAndFn>) -> Result<bool, io::Error> {
100 let mut result = true;
101 let opts = compiletest::test_opts(config);
102 for dir in fs::read_dir(&config.src_base)? {
104 if !dir.file_type()?.is_dir() {
107 let dir_path = dir.path();
108 set_var("CARGO_MANIFEST_DIR", &dir_path);
109 for file in fs::read_dir(&dir_path)? {
111 let file_path = file.path();
112 if file.file_type()?.is_dir() {
115 if file_path.extension() != Some(OsStr::new("rs")) {
118 let paths = compiletest::common::TestPaths {
120 base: config.src_base.clone(),
121 relative_dir: dir_path.file_name().unwrap().into(),
123 let test_name = compiletest::make_test_name(&config, &paths);
126 .position(|test| test.desc.name == test_name)
127 .expect("The test should be in there");
128 result &= tester::run_tests_console(&opts, vec![tests.swap_remove(index)])?;
134 config.mode = TestMode::Ui;
135 config.src_base = Path::new("tests").join("ui-toml").canonicalize().unwrap();
137 let tests = compiletest::make_tests(&config);
139 let res = run_tests(&config, tests);
142 Ok(false) => panic!("Some tests failed"),
144 panic!("I/O failure during tests: {:?}", e);
149 fn run_ui_cargo(config: &mut compiletest::Config) {
150 if cargo::is_rustc_test_suite() {
154 config: &compiletest::Config,
155 filter: &Option<String>,
156 mut tests: Vec<tester::TestDescAndFn>,
157 ) -> Result<bool, io::Error> {
158 let mut result = true;
159 let opts = compiletest::test_opts(config);
161 for dir in fs::read_dir(&config.src_base)? {
163 if !dir.file_type()?.is_dir() {
167 // Use the filter if provided
168 let dir_path = dir.path();
170 Some(name) if !dir_path.ends_with(name) => continue,
174 for case in fs::read_dir(&dir_path)? {
176 if !case.file_type()?.is_dir() {
180 let src_path = case.path().join("src");
182 // When switching between branches, if the previous branch had a test
183 // that the current branch does not have, the directory is not removed
184 // because an ignored Cargo.lock file exists.
185 if !src_path.exists() {
189 env::set_current_dir(&src_path)?;
190 for file in fs::read_dir(&src_path)? {
192 if file.file_type()?.is_dir() {
196 // Search for the main file to avoid running a test for each file in the project
197 let file_path = file.path();
198 match file_path.file_name().and_then(OsStr::to_str) {
199 Some("main.rs") => {},
203 let paths = compiletest::common::TestPaths {
205 base: config.src_base.clone(),
206 relative_dir: src_path.strip_prefix(&config.src_base).unwrap().into(),
208 let test_name = compiletest::make_test_name(&config, &paths);
211 .position(|test| test.desc.name == test_name)
212 .expect("The test should be in there");
213 result &= tester::run_tests_console(&opts, vec![tests.swap_remove(index)])?;
220 config.mode = TestMode::Ui;
221 config.src_base = Path::new("tests").join("ui-cargo").canonicalize().unwrap();
223 let tests = compiletest::make_tests(&config);
225 let current_dir = env::current_dir().unwrap();
226 let filter = env::var("TESTNAME").ok();
227 let res = run_tests(&config, &filter, tests);
228 env::set_current_dir(current_dir).unwrap();
232 Ok(false) => panic!("Some tests failed"),
234 panic!("I/O failure during tests: {:?}", e);
240 set_var("CLIPPY_DISABLE_DOCS_LINKS", "true");
241 set_var("CLIPPY_TESTS", "true");
242 //set_var("RUST_BACKTRACE", "0");
248 let mut config = default_config();
249 run_mode(&mut config);
250 run_ui_toml(&mut config);
251 run_ui_cargo(&mut config);