]> git.lizzy.rs Git - rust.git/blob - tests/compile-test.rs
Merge pull request #2730 from NiekGr/len_zero_use_of_one
[rust.git] / tests / compile-test.rs
1 #![feature(test)]
2
3 extern crate compiletest_rs as compiletest;
4 extern crate test;
5
6 use std::path::{Path, PathBuf};
7 use std::env::{set_var, var};
8
9 fn clippy_driver_path() -> PathBuf {
10     if let Some(path) = option_env!("CLIPPY_DRIVER_PATH") {
11         PathBuf::from(path)
12     } else {
13         PathBuf::from(concat!("target/", env!("PROFILE"), "/clippy-driver"))
14     }
15 }
16
17 fn host_libs() -> PathBuf {
18     if let Some(path) = option_env!("HOST_LIBS") {
19         PathBuf::from(path)
20     } else {
21         Path::new("target").join(env!("PROFILE"))
22     }
23 }
24
25 fn rustc_test_suite() -> Option<PathBuf> {
26     option_env!("RUSTC_TEST_SUITE").map(PathBuf::from)
27 }
28
29 fn rustc_lib_path() -> PathBuf {
30     option_env!("RUSTC_LIB_PATH").unwrap().into()
31 }
32
33 fn config(dir: &'static str, mode: &'static str) -> compiletest::Config {
34     let mut config = compiletest::Config::default();
35
36     let cfg_mode = mode.parse().expect("Invalid mode");
37     if let Ok(name) = var::<&str>("TESTNAME") {
38         let s: String = name.to_owned();
39         config.filter = Some(s)
40     }
41
42     if rustc_test_suite().is_some() {
43         config.run_lib_path = rustc_lib_path();
44         config.compile_lib_path = rustc_lib_path();
45     }
46     config.target_rustcflags = Some(format!(
47         "-L {0} -L {0}/deps -Dwarnings",
48         host_libs().display()
49     ));
50
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");
56         path
57     } else {
58         let mut path = std::env::current_dir().unwrap();
59         path.push("target/debug/test_build_base");
60         path
61     };
62     config.src_base = PathBuf::from(format!("tests/{}", dir));
63     config.rustc_path = clippy_driver_path();
64     config
65 }
66
67 fn run_mode(dir: &'static str, mode: &'static str) {
68     compiletest::run_tests(&config(dir, mode));
69 }
70
71 fn prepare_env() {
72     set_var("CLIPPY_DISABLE_DOCS_LINKS", "true");
73     set_var("CLIPPY_TESTS", "true");
74     //set_var("RUST_BACKTRACE", "0");
75 }
76
77 #[test]
78 fn compile_test() {
79     prepare_env();
80     run_mode("run-pass", "run-pass");
81     run_mode("ui", "ui");
82 }