-#![feature(test)]
+#![feature(test)] // compiletest_rs requires this attribute
-extern crate compiletest_rs as compiletest;
-extern crate test;
+use compiletest_rs as compiletest;
+use compiletest_rs::common::Mode as TestMode;
-use std::io;
+use std::env::{self, set_var};
use std::ffi::OsStr;
use std::fs;
-use std::env::{set_var, var};
+use std::io;
use std::path::{Path, PathBuf};
-fn clippy_driver_path() -> PathBuf {
- if let Some(path) = option_env!("CLIPPY_DRIVER_PATH") {
+mod cargo;
+
+fn host_lib() -> PathBuf {
+ if let Some(path) = option_env!("HOST_LIBS") {
PathBuf::from(path)
} else {
- PathBuf::from(concat!("target/", env!("PROFILE"), "/clippy-driver"))
+ cargo::CARGO_TARGET_DIR.join(env!("PROFILE"))
}
}
-fn host_libs() -> PathBuf {
- if let Some(path) = option_env!("HOST_LIBS") {
+fn clippy_driver_path() -> PathBuf {
+ if let Some(path) = option_env!("CLIPPY_DRIVER_PATH") {
PathBuf::from(path)
} else {
- Path::new("target").join(env!("PROFILE"))
+ cargo::TARGET_LIB.join("clippy-driver")
}
}
-fn rustc_test_suite() -> Option<PathBuf> {
- option_env!("RUSTC_TEST_SUITE").map(PathBuf::from)
-}
+// When we'll want to use `extern crate ..` for a dependency that is used
+// both by the crate and the compiler itself, we can't simply pass -L flags
+// as we'll get a duplicate matching versions. Instead, disambiguate with
+// `--extern dep=path`.
+// See https://github.com/rust-lang/rust-clippy/issues/4015.
+//
+// FIXME: We cannot use `cargo build --message-format=json` to resolve to dependency files.
+// Because it would force-rebuild if the options passed to `build` command is not the same
+// as what we manually pass to `cargo` invocation
+fn third_party_crates() -> String {
+ use std::collections::HashMap;
+ static CRATES: &[&str] = &["serde", "serde_derive", "regex", "clippy_lints"];
+ let dep_dir = cargo::TARGET_LIB.join("deps");
+ let mut crates: HashMap<&str, PathBuf> = HashMap::with_capacity(CRATES.len());
+ for entry in fs::read_dir(dep_dir).unwrap() {
+ let path = match entry {
+ Ok(entry) => entry.path(),
+ _ => continue,
+ };
+ if let Some(name) = path.file_name().and_then(OsStr::to_str) {
+ for dep in CRATES {
+ if name.starts_with(&format!("lib{}-", dep)) && name.ends_with(".rlib") {
+ crates.entry(dep).or_insert(path);
+ break;
+ }
+ }
+ }
+ }
-fn rustc_lib_path() -> PathBuf {
- option_env!("RUSTC_LIB_PATH").unwrap().into()
+ let v: Vec<_> = crates
+ .into_iter()
+ .map(|(dep, path)| format!("--extern {}={}", dep, path.display()))
+ .collect();
+ v.join(" ")
}
-fn config(mode: &str, dir: PathBuf) -> compiletest::Config {
+fn default_config() -> compiletest::Config {
let mut config = compiletest::Config::default();
- let cfg_mode = mode.parse().expect("Invalid mode");
- if let Ok(name) = var::<&str>("TESTNAME") {
- let s: String = name.to_owned();
- config.filter = Some(s)
+ if let Ok(name) = env::var("TESTNAME") {
+ config.filter = Some(name);
}
- if rustc_test_suite().is_some() {
- config.run_lib_path = rustc_lib_path();
- config.compile_lib_path = rustc_lib_path();
+ if let Some(path) = option_env!("RUSTC_LIB_PATH") {
+ let path = PathBuf::from(path);
+ config.run_lib_path = path.clone();
+ config.compile_lib_path = path;
}
- config.target_rustcflags = Some(format!("-L {0} -L {0}/deps -Dwarnings", host_libs().display()));
- config.mode = cfg_mode;
- config.build_base = if rustc_test_suite().is_some() {
- // we don't need access to the stderr files on travis
+ config.target_rustcflags = Some(format!(
+ "-L {0} -L {1} -Dwarnings -Zui-testing {2}",
+ host_lib().join("deps").display(),
+ cargo::TARGET_LIB.join("deps").display(),
+ third_party_crates(),
+ ));
+
+ config.build_base = if cargo::is_rustc_test_suite() {
+ // This make the stderr files go to clippy OUT_DIR on rustc repo build dir
let mut path = PathBuf::from(env!("OUT_DIR"));
path.push("test_build_base");
path
} else {
- let mut path = std::env::current_dir().unwrap();
- path.push("target/debug/test_build_base");
- path
+ host_lib().join("test_build_base")
};
- config.src_base = dir;
config.rustc_path = clippy_driver_path();
config
}
-fn run_mode(mode: &str, dir: PathBuf) {
- compiletest::run_tests(&config(mode, dir));
+fn run_mode(cfg: &mut compiletest::Config) {
+ cfg.mode = TestMode::Ui;
+ cfg.src_base = Path::new("tests").join("ui");
+ compiletest::run_tests(&cfg);
}
-fn run_ui_toml_tests(config: &compiletest::Config, mut tests: Vec<test::TestDescAndFn>) -> Result<bool, io::Error> {
+#[allow(clippy::identity_conversion)]
+fn run_ui_toml_tests(config: &compiletest::Config, mut tests: Vec<tester::TestDescAndFn>) -> Result<bool, io::Error> {
let mut result = true;
let opts = compiletest::test_opts(config);
for dir in fs::read_dir(&config.src_base)? {
for file in fs::read_dir(&dir_path)? {
let file = file?;
let file_path = file.path();
- if !file.file_type()?.is_file() {
+ if file.file_type()?.is_dir() {
continue;
}
if file_path.extension() != Some(OsStr::new("rs")) {
relative_dir: dir_path.file_name().unwrap().into(),
};
let test_name = compiletest::make_test_name(&config, &paths);
- let index = tests.iter()
+ let index = tests
+ .iter()
.position(|test| test.desc.name == test_name)
.expect("The test should be in there");
- result &= test::run_tests_console(
- &opts,
- vec![tests.swap_remove(index)])?;
+ result &= tester::run_tests_console(&opts, vec![tests.swap_remove(index)])?;
}
}
Ok(result)
}
-fn run_ui_toml() {
- let path = PathBuf::from("tests/ui-toml").canonicalize().unwrap();
- let config = config("ui", path);
+fn run_ui_toml(config: &mut compiletest::Config) {
+ config.mode = TestMode::Ui;
+ config.src_base = Path::new("tests").join("ui-toml").canonicalize().unwrap();
+
let tests = compiletest::make_tests(&config);
let res = run_ui_toml_tests(&config, tests);
match res {
- Ok(true) => {}
+ Ok(true) => {},
Ok(false) => panic!("Some tests failed"),
Err(e) => {
println!("I/O failure during tests: {:?}", e);
- }
+ },
}
}
#[test]
fn compile_test() {
prepare_env();
- run_mode("run-pass", "tests/run-pass".into());
- run_mode("ui", "tests/ui".into());
- run_ui_toml();
+ let mut config = default_config();
+ run_mode(&mut config);
+ run_ui_toml(&mut config);
}