1 #![feature(test)] // compiletest_rs requires this attribute
4 use compiletest_rs as compiletest;
5 use compiletest_rs::common::Mode as TestMode;
7 use std::env::{self, set_var, var};
11 use std::path::{Path, PathBuf};
15 fn host_lib() -> PathBuf {
16 option_env!("HOST_LIBS").map_or(cargo::CARGO_TARGET_DIR.join(env!("PROFILE")), PathBuf::from)
19 fn clippy_driver_path() -> PathBuf {
20 option_env!("CLIPPY_DRIVER_PATH").map_or(cargo::TARGET_LIB.join("clippy-driver"), PathBuf::from)
23 // When we'll want to use `extern crate ..` for a dependency that is used
24 // both by the crate and the compiler itself, we can't simply pass -L flags
25 // as we'll get a duplicate matching versions. Instead, disambiguate with
26 // `--extern dep=path`.
27 // See https://github.com/rust-lang/rust-clippy/issues/4015.
29 // FIXME: We cannot use `cargo build --message-format=json` to resolve to dependency files.
30 // Because it would force-rebuild if the options passed to `build` command is not the same
31 // as what we manually pass to `cargo` invocation
32 fn third_party_crates() -> String {
33 use std::collections::HashMap;
34 static CRATES: &[&str] = &["serde", "serde_derive", "regex", "clippy_lints", "syn", "quote"];
35 let dep_dir = cargo::TARGET_LIB.join("deps");
36 let mut crates: HashMap<&str, PathBuf> = HashMap::with_capacity(CRATES.len());
37 for entry in fs::read_dir(dep_dir).unwrap() {
38 let path = match entry {
39 Ok(entry) => entry.path(),
42 if let Some(name) = path.file_name().and_then(OsStr::to_str) {
44 if name.starts_with(&format!("lib{}-", dep)) && name.ends_with(".rlib") {
45 if let Some(old) = crates.insert(dep, path.clone()) {
46 panic!("Found multiple rlibs for crate `{}`: `{:?}` and `{:?}", dep, old, path);
54 let v: Vec<_> = crates
56 .map(|(dep, path)| format!("--extern {}={}", dep, path.display()))
61 fn default_config() -> compiletest::Config {
62 let mut config = compiletest::Config::default();
64 if let Ok(name) = env::var("TESTNAME") {
65 config.filter = Some(name);
68 if let Some(path) = option_env!("RUSTC_LIB_PATH") {
69 let path = PathBuf::from(path);
70 config.run_lib_path = path.clone();
71 config.compile_lib_path = path;
74 config.target_rustcflags = Some(format!(
75 "--emit=metadata -L {0} -L {1} -Dwarnings -Zui-testing {2}",
76 host_lib().join("deps").display(),
77 cargo::TARGET_LIB.join("deps").display(),
81 config.build_base = if cargo::is_rustc_test_suite() {
82 // This make the stderr files go to clippy OUT_DIR on rustc repo build dir
83 let mut path = PathBuf::from(env!("OUT_DIR"));
84 path.push("test_build_base");
87 host_lib().join("test_build_base")
89 config.rustc_path = clippy_driver_path();
93 fn run_mode(cfg: &mut compiletest::Config) {
94 cfg.mode = TestMode::Ui;
95 cfg.src_base = Path::new("tests").join("ui");
96 compiletest::run_tests(&cfg);
99 fn run_ui_toml(config: &mut compiletest::Config) {
100 fn run_tests(config: &compiletest::Config, mut tests: Vec<tester::TestDescAndFn>) -> Result<bool, io::Error> {
101 let mut result = true;
102 let opts = compiletest::test_opts(config);
103 for dir in fs::read_dir(&config.src_base)? {
105 if !dir.file_type()?.is_dir() {
108 let dir_path = dir.path();
109 set_var("CARGO_MANIFEST_DIR", &dir_path);
110 for file in fs::read_dir(&dir_path)? {
112 let file_path = file.path();
113 if file.file_type()?.is_dir() {
116 if file_path.extension() != Some(OsStr::new("rs")) {
119 let paths = compiletest::common::TestPaths {
121 base: config.src_base.clone(),
122 relative_dir: dir_path.file_name().unwrap().into(),
124 let test_name = compiletest::make_test_name(&config, &paths);
127 .position(|test| test.desc.name == test_name)
128 .expect("The test should be in there");
129 result &= tester::run_tests_console(&opts, vec![tests.swap_remove(index)])?;
135 config.mode = TestMode::Ui;
136 config.src_base = Path::new("tests").join("ui-toml").canonicalize().unwrap();
138 let tests = compiletest::make_tests(&config);
140 let manifest_dir = var("CARGO_MANIFEST_DIR").unwrap_or_default();
141 let res = run_tests(&config, tests);
142 set_var("CARGO_MANIFEST_DIR", &manifest_dir);
145 Ok(false) => panic!("Some tests failed"),
147 panic!("I/O failure during tests: {:?}", e);
152 fn run_ui_cargo(config: &mut compiletest::Config) {
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 if cargo::is_rustc_test_suite() {
224 config.mode = TestMode::Ui;
225 config.src_base = Path::new("tests").join("ui-cargo").canonicalize().unwrap();
227 let tests = compiletest::make_tests(&config);
229 let current_dir = env::current_dir().unwrap();
230 let filter = env::var("TESTNAME").ok();
231 let res = run_tests(&config, &filter, tests);
232 env::set_current_dir(current_dir).unwrap();
236 Ok(false) => panic!("Some tests failed"),
238 panic!("I/O failure during tests: {:?}", e);
244 set_var("CLIPPY_DISABLE_DOCS_LINKS", "true");
245 set_var("CLIPPY_TESTS", "true");
246 //set_var("RUST_BACKTRACE", "0");
252 let mut config = default_config();
253 run_mode(&mut config);
254 run_ui_toml(&mut config);
255 run_ui_cargo(&mut config);