]> git.lizzy.rs Git - rust.git/blob - tests/compile-test.rs
Auto merge of #3599 - xfix:use-hash-set-for-valid-idents, r=oli-obk
[rust.git] / tests / compile-test.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 #![feature(test)]
11
12 use compiletest_rs as compiletest;
13 extern crate test;
14
15 use std::env::{set_var, var};
16 use std::ffi::OsStr;
17 use std::fs;
18 use std::io;
19 use std::path::{Path, PathBuf};
20
21 fn clippy_driver_path() -> PathBuf {
22     if let Some(path) = option_env!("CLIPPY_DRIVER_PATH") {
23         PathBuf::from(path)
24     } else {
25         PathBuf::from(concat!("target/", env!("PROFILE"), "/clippy-driver"))
26     }
27 }
28
29 fn host_libs() -> PathBuf {
30     if let Some(path) = option_env!("HOST_LIBS") {
31         PathBuf::from(path)
32     } else {
33         Path::new("target").join(env!("PROFILE"))
34     }
35 }
36
37 fn rustc_test_suite() -> Option<PathBuf> {
38     option_env!("RUSTC_TEST_SUITE").map(PathBuf::from)
39 }
40
41 fn rustc_lib_path() -> PathBuf {
42     option_env!("RUSTC_LIB_PATH").unwrap().into()
43 }
44
45 fn config(mode: &str, dir: PathBuf) -> compiletest::Config {
46     let mut config = compiletest::Config::default();
47
48     let cfg_mode = mode.parse().expect("Invalid mode");
49     if let Ok(name) = var::<&str>("TESTNAME") {
50         let s: String = name.to_owned();
51         config.filter = Some(s)
52     }
53
54     if rustc_test_suite().is_some() {
55         config.run_lib_path = rustc_lib_path();
56         config.compile_lib_path = rustc_lib_path();
57     }
58     config.target_rustcflags = Some(format!(
59         "-L {0} -L {0}/deps -Dwarnings -Zui-testing",
60         host_libs().display()
61     ));
62
63     config.mode = cfg_mode;
64     config.build_base = if rustc_test_suite().is_some() {
65         // we don't need access to the stderr files on travis
66         let mut path = PathBuf::from(env!("OUT_DIR"));
67         path.push("test_build_base");
68         path
69     } else {
70         let mut path = std::env::current_dir().unwrap();
71         path.push("target/debug/test_build_base");
72         path
73     };
74     config.src_base = dir;
75     config.rustc_path = clippy_driver_path();
76     config
77 }
78
79 fn run_mode(mode: &str, dir: PathBuf) {
80     let cfg = config(mode, dir);
81     // clean rmeta data, otherwise "cargo check; cargo test" fails (#2896)
82     cfg.clean_rmeta();
83     compiletest::run_tests(&cfg);
84 }
85
86 fn run_ui_toml_tests(config: &compiletest::Config, mut tests: Vec<test::TestDescAndFn>) -> Result<bool, io::Error> {
87     let mut result = true;
88     let opts = compiletest::test_opts(config);
89     for dir in fs::read_dir(&config.src_base)? {
90         let dir = dir?;
91         if !dir.file_type()?.is_dir() {
92             continue;
93         }
94         let dir_path = dir.path();
95         set_var("CARGO_MANIFEST_DIR", &dir_path);
96         for file in fs::read_dir(&dir_path)? {
97             let file = file?;
98             let file_path = file.path();
99             if !file.file_type()?.is_file() {
100                 continue;
101             }
102             if file_path.extension() != Some(OsStr::new("rs")) {
103                 continue;
104             }
105             let paths = compiletest::common::TestPaths {
106                 file: file_path,
107                 base: config.src_base.clone(),
108                 relative_dir: dir_path.file_name().unwrap().into(),
109             };
110             let test_name = compiletest::make_test_name(&config, &paths);
111             let index = tests
112                 .iter()
113                 .position(|test| test.desc.name == test_name)
114                 .expect("The test should be in there");
115             result &= test::run_tests_console(&opts, vec![tests.swap_remove(index)])?;
116         }
117     }
118     Ok(result)
119 }
120
121 fn run_ui_toml() {
122     let path = PathBuf::from("tests/ui-toml").canonicalize().unwrap();
123     let config = config("ui", path);
124     let tests = compiletest::make_tests(&config);
125
126     let res = run_ui_toml_tests(&config, tests);
127     match res {
128         Ok(true) => {},
129         Ok(false) => panic!("Some tests failed"),
130         Err(e) => {
131             println!("I/O failure during tests: {:?}", e);
132         },
133     }
134 }
135
136 fn prepare_env() {
137     set_var("CLIPPY_DISABLE_DOCS_LINKS", "true");
138     set_var("CLIPPY_TESTS", "true");
139     //set_var("RUST_BACKTRACE", "0");
140 }
141
142 #[test]
143 fn compile_test() {
144     prepare_env();
145     run_mode("run-pass", "tests/run-pass".into());
146     run_mode("ui", "tests/ui".into());
147     run_ui_toml();
148 }