]> git.lizzy.rs Git - rust.git/blob - tests/compile-test.rs
Merge pull request #3257 from o01eg/remove-sysroot
[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 extern crate 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 use std::process::Command;
21
22 fn clippy_driver_path() -> PathBuf {
23     if let Some(path) = option_env!("CLIPPY_DRIVER_PATH") {
24         PathBuf::from(path)
25     } else {
26         PathBuf::from(concat!("target/", env!("PROFILE"), "/clippy-driver"))
27     }
28 }
29
30 fn host_libs() -> PathBuf {
31     if let Some(path) = option_env!("HOST_LIBS") {
32         PathBuf::from(path)
33     } else {
34         Path::new("target").join(env!("PROFILE"))
35     }
36 }
37
38 fn rustc_test_suite() -> Option<PathBuf> {
39     option_env!("RUSTC_TEST_SUITE").map(PathBuf::from)
40 }
41
42 fn rustc_lib_path() -> PathBuf {
43     option_env!("RUSTC_LIB_PATH").unwrap().into()
44 }
45
46 fn rustc_sysroot_path() -> PathBuf {
47     option_env!("SYSROOT")
48         .map(String::from)
49         .or_else(|| std::env::var("SYSROOT").ok())
50         .or_else(|| {
51             let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));
52             let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN"));
53             home.and_then(|home| toolchain.map(|toolchain| format!("{}/toolchains/{}", home, toolchain)))
54         })
55         .or_else(|| {
56             Command::new("rustc")
57                 .arg("--print")
58                 .arg("sysroot")
59                 .output()
60                 .ok()
61                 .and_then(|out| String::from_utf8(out.stdout).ok())
62                 .map(|s| s.trim().to_owned())
63         })
64         .expect("need to specify SYSROOT env var during clippy compilation, or use rustup or multirust")
65         .into()
66 }
67
68 fn config(mode: &str, dir: PathBuf) -> compiletest::Config {
69     let mut config = compiletest::Config::default();
70
71     let cfg_mode = mode.parse().expect("Invalid mode");
72     if let Ok(name) = var::<&str>("TESTNAME") {
73         let s: String = name.to_owned();
74         config.filter = Some(s)
75     }
76
77     if rustc_test_suite().is_some() {
78         config.run_lib_path = rustc_lib_path();
79         config.compile_lib_path = rustc_lib_path();
80     }
81     config.target_rustcflags = Some(format!(
82         "-L {0} -L {0}/deps -Dwarnings --sysroot {1}",
83         host_libs().display(),
84         rustc_sysroot_path().display()
85     ));
86
87     config.mode = cfg_mode;
88     config.build_base = if rustc_test_suite().is_some() {
89         // we don't need access to the stderr files on travis
90         let mut path = PathBuf::from(env!("OUT_DIR"));
91         path.push("test_build_base");
92         path
93     } else {
94         let mut path = std::env::current_dir().unwrap();
95         path.push("target/debug/test_build_base");
96         path
97     };
98     config.src_base = dir;
99     config.rustc_path = clippy_driver_path();
100     config
101 }
102
103 fn run_mode(mode: &str, dir: PathBuf) {
104     let cfg = config(mode, dir);
105     // clean rmeta data, otherwise "cargo check; cargo test" fails (#2896)
106     cfg.clean_rmeta();
107     compiletest::run_tests(&cfg);
108 }
109
110 fn run_ui_toml_tests(config: &compiletest::Config, mut tests: Vec<test::TestDescAndFn>) -> Result<bool, io::Error> {
111     let mut result = true;
112     let opts = compiletest::test_opts(config);
113     for dir in fs::read_dir(&config.src_base)? {
114         let dir = dir?;
115         if !dir.file_type()?.is_dir() {
116             continue;
117         }
118         let dir_path = dir.path();
119         set_var("CARGO_MANIFEST_DIR", &dir_path);
120         for file in fs::read_dir(&dir_path)? {
121             let file = file?;
122             let file_path = file.path();
123             if !file.file_type()?.is_file() {
124                 continue;
125             }
126             if file_path.extension() != Some(OsStr::new("rs")) {
127                 continue;
128             }
129             let paths = compiletest::common::TestPaths {
130                 file: file_path,
131                 base: config.src_base.clone(),
132                 relative_dir: dir_path.file_name().unwrap().into(),
133             };
134             let test_name = compiletest::make_test_name(&config, &paths);
135             let index = tests
136                 .iter()
137                 .position(|test| test.desc.name == test_name)
138                 .expect("The test should be in there");
139             result &= test::run_tests_console(&opts, vec![tests.swap_remove(index)])?;
140         }
141     }
142     Ok(result)
143 }
144
145 fn run_ui_toml() {
146     let path = PathBuf::from("tests/ui-toml").canonicalize().unwrap();
147     let config = config("ui", path);
148     let tests = compiletest::make_tests(&config);
149
150     let res = run_ui_toml_tests(&config, tests);
151     match res {
152         Ok(true) => {},
153         Ok(false) => panic!("Some tests failed"),
154         Err(e) => {
155             println!("I/O failure during tests: {:?}", e);
156         },
157     }
158 }
159
160 fn prepare_env() {
161     set_var("CLIPPY_DISABLE_DOCS_LINKS", "true");
162     set_var("CLIPPY_TESTS", "true");
163     //set_var("RUST_BACKTRACE", "0");
164 }
165
166 #[test]
167 fn compile_test() {
168     prepare_env();
169     run_mode("run-pass", "tests/run-pass".into());
170     run_mode("ui", "tests/ui".into());
171     run_ui_toml();
172 }