]> git.lizzy.rs Git - rust.git/blob - tests/compile-test.rs
Rollup merge of #4509 - sinkuu:redundant_clone_fix, r=llogiq
[rust.git] / tests / compile-test.rs
1 #![feature(test)]
2
3 use compiletest_rs as compiletest;
4 extern crate test;
5
6 use std::env::{set_var, var};
7 use std::ffi::OsStr;
8 use std::fs;
9 use std::io;
10 use std::path::{Path, PathBuf};
11
12 fn clippy_driver_path() -> PathBuf {
13     if let Some(path) = option_env!("CLIPPY_DRIVER_PATH") {
14         PathBuf::from(path)
15     } else {
16         PathBuf::from(concat!("target/", env!("PROFILE"), "/clippy-driver"))
17     }
18 }
19
20 fn host_libs() -> PathBuf {
21     if let Some(path) = option_env!("HOST_LIBS") {
22         PathBuf::from(path)
23     } else {
24         Path::new("target").join(env!("PROFILE"))
25     }
26 }
27
28 fn rustc_test_suite() -> Option<PathBuf> {
29     option_env!("RUSTC_TEST_SUITE").map(PathBuf::from)
30 }
31
32 fn rustc_lib_path() -> PathBuf {
33     option_env!("RUSTC_LIB_PATH").unwrap().into()
34 }
35
36 fn config(mode: &str, dir: PathBuf) -> compiletest::Config {
37     let mut config = compiletest::Config::default();
38
39     let cfg_mode = mode.parse().expect("Invalid mode");
40     if let Ok(name) = var::<&str>("TESTNAME") {
41         config.filter = Some(name)
42     }
43
44     if rustc_test_suite().is_some() {
45         config.run_lib_path = rustc_lib_path();
46         config.compile_lib_path = rustc_lib_path();
47     }
48
49     // When we'll want to use `extern crate ..` for a dependency that is used
50     // both by the crate and the compiler itself, we can't simply pass -L flags
51     // as we'll get a duplicate matching versions. Instead, disambiguate with
52     // `--extern dep=path`.
53     // See https://github.com/rust-lang/rust-clippy/issues/4015.
54     let needs_disambiguation = ["serde", "regex", "clippy_lints"];
55     // This assumes that deps are compiled (they are for Cargo integration tests).
56     let deps = std::fs::read_dir(host_libs().join("deps")).unwrap();
57     let disambiguated = deps
58         .filter_map(|dep| {
59             let path = dep.ok()?.path();
60             let name = path.file_name()?.to_string_lossy();
61             // NOTE: This only handles a single dep
62             // https://github.com/laumann/compiletest-rs/issues/101
63             needs_disambiguation.iter().find_map(|dep| {
64                 if name.starts_with(&format!("lib{}-", dep)) && name.ends_with(".rlib") {
65                     Some(format!("--extern {}={}", dep, path.display()))
66                 } else {
67                     None
68                 }
69             })
70         })
71         .collect::<Vec<_>>();
72
73     config.target_rustcflags = Some(format!(
74         "-L {0} -L {0}/deps -Dwarnings -Zui-testing {1}",
75         host_libs().display(),
76         disambiguated.join(" ")
77     ));
78
79     config.mode = cfg_mode;
80     config.build_base = if rustc_test_suite().is_some() {
81         // we don't need access to the stderr files on travis
82         let mut path = PathBuf::from(env!("OUT_DIR"));
83         path.push("test_build_base");
84         path
85     } else {
86         let mut path = std::env::current_dir().unwrap();
87         path.push("target/debug/test_build_base");
88         path
89     };
90     config.src_base = dir;
91     config.rustc_path = clippy_driver_path();
92     config
93 }
94
95 fn run_mode(mode: &str, dir: PathBuf) {
96     let cfg = config(mode, dir);
97     compiletest::run_tests(&cfg);
98 }
99
100 #[allow(clippy::identity_conversion)]
101 fn run_ui_toml_tests(config: &compiletest::Config, mut tests: Vec<test::TestDescAndFn>) -> Result<bool, io::Error> {
102     let mut result = true;
103     let opts = compiletest::test_opts(config);
104     for dir in fs::read_dir(&config.src_base)? {
105         let dir = dir?;
106         if !dir.file_type()?.is_dir() {
107             continue;
108         }
109         let dir_path = dir.path();
110         set_var("CARGO_MANIFEST_DIR", &dir_path);
111         for file in fs::read_dir(&dir_path)? {
112             let file = file?;
113             let file_path = file.path();
114             if !file.file_type()?.is_file() {
115                 continue;
116             }
117             if file_path.extension() != Some(OsStr::new("rs")) {
118                 continue;
119             }
120             let paths = compiletest::common::TestPaths {
121                 file: file_path,
122                 base: config.src_base.clone(),
123                 relative_dir: dir_path.file_name().unwrap().into(),
124             };
125             let test_name = compiletest::make_test_name(&config, &paths);
126             let index = tests
127                 .iter()
128                 .position(|test| test.desc.name == test_name)
129                 .expect("The test should be in there");
130             result &= test::run_tests_console(&opts, vec![tests.swap_remove(index)])?;
131         }
132     }
133     Ok(result)
134 }
135
136 fn run_ui_toml() {
137     let path = PathBuf::from("tests/ui-toml").canonicalize().unwrap();
138     let config = config("ui", path);
139     let tests = compiletest::make_tests(&config);
140
141     let res = run_ui_toml_tests(&config, tests);
142     match res {
143         Ok(true) => {},
144         Ok(false) => panic!("Some tests failed"),
145         Err(e) => {
146             println!("I/O failure during tests: {:?}", e);
147         },
148     }
149 }
150
151 fn prepare_env() {
152     set_var("CLIPPY_DISABLE_DOCS_LINKS", "true");
153     set_var("CLIPPY_TESTS", "true");
154     //set_var("RUST_BACKTRACE", "0");
155 }
156
157 #[test]
158 fn compile_test() {
159     prepare_env();
160     run_mode("ui", "tests/ui".into());
161     run_ui_toml();
162 }