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, 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 manifest_dir = var("CARGO_MANIFEST_DIR").unwrap_or_default();
140 let res = run_tests(&config, tests);
141 set_var("CARGO_MANIFEST_DIR", &manifest_dir);
144 Ok(false) => panic!("Some tests failed"),
146 panic!("I/O failure during tests: {:?}", e);
151 fn run_ui_cargo(config: &mut compiletest::Config) {
153 config: &compiletest::Config,
154 filter: &Option<String>,
155 mut tests: Vec<tester::TestDescAndFn>,
156 ) -> Result<bool, io::Error> {
157 let mut result = true;
158 let opts = compiletest::test_opts(config);
160 for dir in fs::read_dir(&config.src_base)? {
162 if !dir.file_type()?.is_dir() {
166 // Use the filter if provided
167 let dir_path = dir.path();
169 Some(name) if !dir_path.ends_with(name) => continue,
173 for case in fs::read_dir(&dir_path)? {
175 if !case.file_type()?.is_dir() {
179 let src_path = case.path().join("src");
181 // When switching between branches, if the previous branch had a test
182 // that the current branch does not have, the directory is not removed
183 // because an ignored Cargo.lock file exists.
184 if !src_path.exists() {
188 env::set_current_dir(&src_path)?;
189 for file in fs::read_dir(&src_path)? {
191 if file.file_type()?.is_dir() {
195 // Search for the main file to avoid running a test for each file in the project
196 let file_path = file.path();
197 match file_path.file_name().and_then(OsStr::to_str) {
198 Some("main.rs") => {},
202 let paths = compiletest::common::TestPaths {
204 base: config.src_base.clone(),
205 relative_dir: src_path.strip_prefix(&config.src_base).unwrap().into(),
207 let test_name = compiletest::make_test_name(&config, &paths);
210 .position(|test| test.desc.name == test_name)
211 .expect("The test should be in there");
212 result &= tester::run_tests_console(&opts, vec![tests.swap_remove(index)])?;
219 if cargo::is_rustc_test_suite() {
223 config.mode = TestMode::Ui;
224 config.src_base = Path::new("tests").join("ui-cargo").canonicalize().unwrap();
226 let tests = compiletest::make_tests(&config);
228 let current_dir = env::current_dir().unwrap();
229 let filter = env::var("TESTNAME").ok();
230 let res = run_tests(&config, &filter, tests);
231 env::set_current_dir(current_dir).unwrap();
235 Ok(false) => panic!("Some tests failed"),
237 panic!("I/O failure during tests: {:?}", e);
243 set_var("CLIPPY_DISABLE_DOCS_LINKS", "true");
244 set_var("CLIPPY_TESTS", "true");
245 //set_var("RUST_BACKTRACE", "0");
251 let mut config = default_config();
252 run_mode(&mut config);
253 run_ui_toml(&mut config);
254 run_ui_cargo(&mut config);