Skip to content

Commit 4e9a08b

Browse files
authored
Merge pull request #2825 from Nemo157/test-toml
Get compile-test tests for configuration working
2 parents bb2f6a5 + edcb8f6 commit 4e9a08b

19 files changed

+197
-6
lines changed

tests/compile-test.rs

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
extern crate compiletest_rs as compiletest;
44
extern crate test;
55

6+
use std::io;
7+
use std::ffi::OsStr;
8+
use std::fs;
69
use std::env::{set_var, var};
710
use std::path::{Path, PathBuf};
811

@@ -30,7 +33,7 @@ fn rustc_lib_path() -> PathBuf {
3033
option_env!("RUSTC_LIB_PATH").unwrap().into()
3134
}
3235

33-
fn config(dir: &'static str, mode: &'static str) -> compiletest::Config {
36+
fn config(mode: &str, dir: PathBuf) -> compiletest::Config {
3437
let mut config = compiletest::Config::default();
3538

3639
let cfg_mode = mode.parse().expect("Invalid mode");
@@ -56,13 +59,64 @@ fn config(dir: &'static str, mode: &'static str) -> compiletest::Config {
5659
path.push("target/debug/test_build_base");
5760
path
5861
};
59-
config.src_base = PathBuf::from(format!("tests/{}", dir));
62+
config.src_base = dir;
6063
config.rustc_path = clippy_driver_path();
6164
config
6265
}
6366

64-
fn run_mode(dir: &'static str, mode: &'static str) {
65-
compiletest::run_tests(&config(dir, mode));
67+
fn run_mode(mode: &str, dir: PathBuf) {
68+
compiletest::run_tests(&config(mode, dir));
69+
}
70+
71+
fn run_ui_toml_tests(config: &compiletest::Config, mut tests: Vec<test::TestDescAndFn>) -> Result<bool, io::Error> {
72+
let mut result = true;
73+
let opts = compiletest::test_opts(config);
74+
for dir in fs::read_dir(&config.src_base)? {
75+
let dir = dir?;
76+
if !dir.file_type()?.is_dir() {
77+
continue;
78+
}
79+
let dir_path = dir.path();
80+
set_var("CARGO_MANIFEST_DIR", &dir_path);
81+
for file in fs::read_dir(&dir_path)? {
82+
let file = file?;
83+
let file_path = file.path();
84+
if !file.file_type()?.is_file() {
85+
continue;
86+
}
87+
if file_path.extension() != Some(OsStr::new("rs")) {
88+
continue;
89+
}
90+
let paths = compiletest::common::TestPaths {
91+
file: file_path,
92+
base: config.src_base.clone(),
93+
relative_dir: dir_path.file_name().unwrap().into(),
94+
};
95+
let test_name = compiletest::make_test_name(&config, &paths);
96+
let index = tests.iter()
97+
.position(|test| test.desc.name == test_name)
98+
.expect("The test should be in there");
99+
result &= test::run_tests_console(
100+
&opts,
101+
vec![tests.swap_remove(index)])?;
102+
}
103+
}
104+
Ok(result)
105+
}
106+
107+
fn run_ui_toml() {
108+
let path = PathBuf::from("tests/ui-toml").canonicalize().unwrap();
109+
let config = config("ui", path);
110+
let tests = compiletest::make_tests(&config);
111+
112+
let res = run_ui_toml_tests(&config, tests);
113+
match res {
114+
Ok(true) => {}
115+
Ok(false) => panic!("Some tests failed"),
116+
Err(e) => {
117+
println!("I/O failure during tests: {:?}", e);
118+
}
119+
}
66120
}
67121

68122
fn prepare_env() {
@@ -74,6 +128,7 @@ fn prepare_env() {
74128
#[test]
75129
fn compile_test() {
76130
prepare_env();
77-
run_mode("run-pass", "run-pass");
78-
run_mode("ui", "ui");
131+
run_mode("run-pass", "tests/run-pass".into());
132+
run_mode("ui", "tests/ui".into());
133+
run_ui_toml();
79134
}
File renamed without changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
error: error reading Clippy's configuration file `$DIR/clippy.toml`: expected an equals, found an identifier at line 1
2+
3+
error: aborting due to previous error
4+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
error: error reading Clippy's configuration file `$DIR/clippy.toml`: invalid type: integer `42`, expected a sequence
2+
3+
error: aborting due to previous error
4+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
error: use of a blacklisted/placeholder name `toto`
2+
--> $DIR/conf_french_blacklisted_name.rs:9:9
3+
|
4+
9 | fn test(toto: ()) {}
5+
| ^^^^
6+
|
7+
= note: `-D blacklisted-name` implied by `-D warnings`
8+
9+
error: use of a blacklisted/placeholder name `toto`
10+
--> $DIR/conf_french_blacklisted_name.rs:12:9
11+
|
12+
12 | let toto = 42;
13+
| ^^^^
14+
15+
error: use of a blacklisted/placeholder name `tata`
16+
--> $DIR/conf_french_blacklisted_name.rs:13:9
17+
|
18+
13 | let tata = 42;
19+
| ^^^^
20+
21+
error: use of a blacklisted/placeholder name `titi`
22+
--> $DIR/conf_french_blacklisted_name.rs:14:9
23+
|
24+
14 | let titi = 42;
25+
| ^^^^
26+
27+
error: use of a blacklisted/placeholder name `toto`
28+
--> $DIR/conf_french_blacklisted_name.rs:20:10
29+
|
30+
20 | (toto, Some(tata), titi @ Some(_)) => (),
31+
| ^^^^
32+
33+
error: use of a blacklisted/placeholder name `tata`
34+
--> $DIR/conf_french_blacklisted_name.rs:20:21
35+
|
36+
20 | (toto, Some(tata), titi @ Some(_)) => (),
37+
| ^^^^
38+
39+
error: use of a blacklisted/placeholder name `titi`
40+
--> $DIR/conf_french_blacklisted_name.rs:20:28
41+
|
42+
20 | (toto, Some(tata), titi @ Some(_)) => (),
43+
| ^^^^
44+
45+
error: aborting due to 7 previous errors
46+

0 commit comments

Comments
 (0)