]> git.lizzy.rs Git - rust.git/blob - tests/compile-test.rs
Update trivially_copy_pass_by_ref with Trait examples
[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
11 #![feature(test)]
12
13 extern crate compiletest_rs as compiletest;
14 extern crate test;
15
16 use std::env::{set_var, var};
17 use std::ffi::OsStr;
18 use std::fs;
19 use std::io;
20 use std::path::{Path, PathBuf};
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 config(mode: &str, dir: PathBuf) -> compiletest::Config {
47     let mut config = compiletest::Config::default();
48
49     let cfg_mode = mode.parse().expect("Invalid mode");
50     if let Ok(name) = var::<&str>("TESTNAME") {
51         let s: String = name.to_owned();
52         config.filter = Some(s)
53     }
54
55     if rustc_test_suite().is_some() {
56         config.run_lib_path = rustc_lib_path();
57         config.compile_lib_path = rustc_lib_path();
58     }
59     config.target_rustcflags = Some(format!("-L {0} -L {0}/deps -Dwarnings", host_libs().display()));
60
61     config.mode = cfg_mode;
62     config.build_base = if rustc_test_suite().is_some() {
63         // we don't need access to the stderr files on travis
64         let mut path = PathBuf::from(env!("OUT_DIR"));
65         path.push("test_build_base");
66         path
67     } else {
68         let mut path = std::env::current_dir().unwrap();
69         path.push("target/debug/test_build_base");
70         path
71     };
72     config.src_base = dir;
73     config.rustc_path = clippy_driver_path();
74     config
75 }
76
77 fn run_mode(mode: &str, dir: PathBuf) {
78     let cfg = config(mode, dir);
79     // clean rmeta data, otherwise "cargo check; cargo test" fails (#2896)
80     cfg.clean_rmeta();
81     compiletest::run_tests(&cfg);
82 }
83
84 fn run_ui_toml_tests(config: &compiletest::Config, mut tests: Vec<test::TestDescAndFn>) -> Result<bool, io::Error> {
85     let mut result = true;
86     let opts = compiletest::test_opts(config);
87     for dir in fs::read_dir(&config.src_base)? {
88         let dir = dir?;
89         if !dir.file_type()?.is_dir() {
90             continue;
91         }
92         let dir_path = dir.path();
93         set_var("CARGO_MANIFEST_DIR", &dir_path);
94         for file in fs::read_dir(&dir_path)? {
95             let file = file?;
96             let file_path = file.path();
97             if !file.file_type()?.is_file() {
98                 continue;
99             }
100             if file_path.extension() != Some(OsStr::new("rs")) {
101                 continue;
102             }
103             let paths = compiletest::common::TestPaths {
104                 file: file_path,
105                 base: config.src_base.clone(),
106                 relative_dir: dir_path.file_name().unwrap().into(),
107             };
108             let test_name = compiletest::make_test_name(&config, &paths);
109             let index = tests
110                 .iter()
111                 .position(|test| test.desc.name == test_name)
112                 .expect("The test should be in there");
113             result &= test::run_tests_console(&opts, vec![tests.swap_remove(index)])?;
114         }
115     }
116     Ok(result)
117 }
118
119 fn run_ui_toml() {
120     let path = PathBuf::from("tests/ui-toml").canonicalize().unwrap();
121     let config = config("ui", path);
122     let tests = compiletest::make_tests(&config);
123
124     let res = run_ui_toml_tests(&config, tests);
125     match res {
126         Ok(true) => {},
127         Ok(false) => panic!("Some tests failed"),
128         Err(e) => {
129             println!("I/O failure during tests: {:?}", e);
130         },
131     }
132 }
133
134 fn prepare_env() {
135     set_var("CLIPPY_DISABLE_DOCS_LINKS", "true");
136     set_var("CLIPPY_TESTS", "true");
137     //set_var("RUST_BACKTRACE", "0");
138 }
139
140 #[test]
141 fn compile_test() {
142     prepare_env();
143     run_mode("run-pass", "tests/run-pass".into());
144     run_mode("ui", "tests/ui".into());
145     run_ui_toml();
146 }