3 extern crate compiletest_rs as compiletest;
9 use std::env::{set_var, var};
10 use std::path::{Path, PathBuf};
12 fn clippy_driver_path() -> PathBuf {
13 if let Some(path) = option_env!("CLIPPY_DRIVER_PATH") {
16 PathBuf::from(concat!("target/", env!("PROFILE"), "/clippy-driver"))
20 fn host_libs() -> PathBuf {
21 if let Some(path) = option_env!("HOST_LIBS") {
24 Path::new("target").join(env!("PROFILE"))
28 fn rustc_test_suite() -> Option<PathBuf> {
29 option_env!("RUSTC_TEST_SUITE").map(PathBuf::from)
32 fn rustc_lib_path() -> PathBuf {
33 option_env!("RUSTC_LIB_PATH").unwrap().into()
36 fn config(mode: &str, dir: PathBuf) -> compiletest::Config {
37 let mut config = compiletest::Config::default();
39 let cfg_mode = mode.parse().expect("Invalid mode");
40 if let Ok(name) = var::<&str>("TESTNAME") {
41 let s: String = name.to_owned();
42 config.filter = Some(s)
45 if rustc_test_suite().is_some() {
46 config.run_lib_path = rustc_lib_path();
47 config.compile_lib_path = rustc_lib_path();
49 config.target_rustcflags = Some(format!("-L {0} -L {0}/deps -Dwarnings", host_libs().display()));
51 config.mode = cfg_mode;
52 config.build_base = if rustc_test_suite().is_some() {
53 // we don't need access to the stderr files on travis
54 let mut path = PathBuf::from(env!("OUT_DIR"));
55 path.push("test_build_base");
58 let mut path = std::env::current_dir().unwrap();
59 path.push("target/debug/test_build_base");
62 config.src_base = dir;
63 config.rustc_path = clippy_driver_path();
67 fn run_mode(mode: &str, dir: PathBuf) {
68 compiletest::run_tests(&config(mode, dir));
71 fn run_ui_toml_tests(config: &compiletest::Config, mut tests: Vec<test::TestDescAndFn>) -> Result<bool, io::Error> {
72 let mut result = true;
73 let opts = compiletest::test_opts(config);
74 for dir in fs::read_dir(&config.src_base)? {
76 if !dir.file_type()?.is_dir() {
79 let dir_path = dir.path();
80 set_var("CARGO_MANIFEST_DIR", &dir_path);
81 for file in fs::read_dir(&dir_path)? {
83 let file_path = file.path();
84 if !file.file_type()?.is_file() {
87 if file_path.extension() != Some(OsStr::new("rs")) {
90 let paths = compiletest::common::TestPaths {
92 base: config.src_base.clone(),
93 relative_dir: dir_path.file_name().unwrap().into(),
95 let test_name = compiletest::make_test_name(&config, &paths);
96 let index = tests.iter()
97 .position(|test| test.desc.name == test_name)
98 .expect("The test should be in there");
99 result &= test::run_tests_console(
101 vec![tests.swap_remove(index)])?;
108 let path = PathBuf::from("tests/ui-toml").canonicalize().unwrap();
109 let config = config("ui", path);
110 let tests = compiletest::make_tests(&config);
112 let res = run_ui_toml_tests(&config, tests);
115 Ok(false) => panic!("Some tests failed"),
117 println!("I/O failure during tests: {:?}", e);
123 set_var("CLIPPY_DISABLE_DOCS_LINKS", "true");
124 set_var("CLIPPY_TESTS", "true");
125 //set_var("RUST_BACKTRACE", "0");
131 run_mode("run-pass", "tests/run-pass".into());
132 run_mode("ui", "tests/ui".into());