Skip to content

Commit bd9b09e

Browse files
committed
Adapt compile-test to run tests for cargo lints
1 parent 1831385 commit bd9b09e

File tree

3 files changed

+164
-32
lines changed

3 files changed

+164
-32
lines changed

tests/compile-test.rs

Lines changed: 108 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -101,49 +101,124 @@ fn run_mode(cfg: &mut compiletest::Config) {
101101
compiletest::run_tests(&cfg);
102102
}
103103

104-
#[allow(clippy::identity_conversion)]
105-
fn run_ui_toml_tests(config: &compiletest::Config, mut tests: Vec<tester::TestDescAndFn>) -> Result<bool, io::Error> {
106-
let mut result = true;
107-
let opts = compiletest::test_opts(config);
108-
for dir in fs::read_dir(&config.src_base)? {
109-
let dir = dir?;
110-
if !dir.file_type()?.is_dir() {
111-
continue;
112-
}
113-
let dir_path = dir.path();
114-
set_var("CARGO_MANIFEST_DIR", &dir_path);
115-
for file in fs::read_dir(&dir_path)? {
116-
let file = file?;
117-
let file_path = file.path();
118-
if file.file_type()?.is_dir() {
104+
fn run_ui_toml(config: &mut compiletest::Config) {
105+
fn run_tests(config: &compiletest::Config, mut tests: Vec<tester::TestDescAndFn>) -> Result<bool, io::Error> {
106+
let mut result = true;
107+
let opts = compiletest::test_opts(config);
108+
for dir in fs::read_dir(&config.src_base)? {
109+
let dir = dir?;
110+
if !dir.file_type()?.is_dir() {
119111
continue;
120112
}
121-
if file_path.extension() != Some(OsStr::new("rs")) {
122-
continue;
113+
let dir_path = dir.path();
114+
set_var("CARGO_MANIFEST_DIR", &dir_path);
115+
for file in fs::read_dir(&dir_path)? {
116+
let file = file?;
117+
let file_path = file.path();
118+
if file.file_type()?.is_dir() {
119+
continue;
120+
}
121+
if file_path.extension() != Some(OsStr::new("rs")) {
122+
continue;
123+
}
124+
let paths = compiletest::common::TestPaths {
125+
file: file_path,
126+
base: config.src_base.clone(),
127+
relative_dir: dir_path.file_name().unwrap().into(),
128+
};
129+
let test_name = compiletest::make_test_name(&config, &paths);
130+
let index = tests
131+
.iter()
132+
.position(|test| test.desc.name == test_name)
133+
.expect("The test should be in there");
134+
result &= tester::run_tests_console(&opts, vec![tests.swap_remove(index)])?;
123135
}
124-
let paths = compiletest::common::TestPaths {
125-
file: file_path,
126-
base: config.src_base.clone(),
127-
relative_dir: dir_path.file_name().unwrap().into(),
128-
};
129-
let test_name = compiletest::make_test_name(&config, &paths);
130-
let index = tests
131-
.iter()
132-
.position(|test| test.desc.name == test_name)
133-
.expect("The test should be in there");
134-
result &= tester::run_tests_console(&opts, vec![tests.swap_remove(index)])?;
135136
}
137+
Ok(result)
136138
}
137-
Ok(result)
138-
}
139139

140-
fn run_ui_toml(config: &mut compiletest::Config) {
141140
config.mode = TestMode::Ui;
142141
config.src_base = Path::new("tests").join("ui-toml").canonicalize().unwrap();
143142

144143
let tests = compiletest::make_tests(&config);
145144

146-
let res = run_ui_toml_tests(&config, tests);
145+
let res = run_tests(&config, tests);
146+
match res {
147+
Ok(true) => {},
148+
Ok(false) => panic!("Some tests failed"),
149+
Err(e) => {
150+
println!("I/O failure during tests: {:?}", e);
151+
},
152+
}
153+
}
154+
155+
fn run_ui_cargo(config: &mut compiletest::Config) {
156+
fn run_tests(
157+
config: &compiletest::Config,
158+
filter: &Option<String>,
159+
mut tests: Vec<tester::TestDescAndFn>,
160+
) -> Result<bool, io::Error> {
161+
let mut result = true;
162+
let opts = compiletest::test_opts(config);
163+
164+
for dir in fs::read_dir(&config.src_base)? {
165+
let dir = dir?;
166+
if !dir.file_type()?.is_dir() {
167+
continue;
168+
}
169+
170+
// Use the filter if provided
171+
let dir_path = dir.path();
172+
match &filter {
173+
Some(name) if !dir_path.ends_with(name) => continue,
174+
_ => {},
175+
}
176+
177+
for case in &["pass", "fail"] {
178+
let tail: PathBuf = [case, "src"].iter().collect();
179+
let src_path = dir_path.join(tail);
180+
env::set_current_dir(&src_path)?;
181+
182+
for file in fs::read_dir(&src_path)? {
183+
let file = file?;
184+
if file.file_type()?.is_dir() {
185+
continue;
186+
}
187+
188+
// Search for the main file to avoid running a test for each file in the project
189+
let file_path = file.path();
190+
match file_path.file_name().and_then(OsStr::to_str) {
191+
Some("main.rs") => {},
192+
_ => continue,
193+
}
194+
195+
let paths = compiletest::common::TestPaths {
196+
file: file_path,
197+
base: config.src_base.clone(),
198+
relative_dir: src_path.strip_prefix(&config.src_base).unwrap().into(),
199+
};
200+
let test_name = compiletest::make_test_name(&config, &paths);
201+
let index = tests
202+
.iter()
203+
.position(|test| test.desc.name == test_name)
204+
.expect("The test should be in there");
205+
result &= tester::run_tests_console(&opts, vec![tests.swap_remove(index)])?;
206+
}
207+
}
208+
}
209+
Ok(result)
210+
}
211+
212+
config.mode = TestMode::Ui;
213+
config.src_base = Path::new("tests").join("ui-cargo").canonicalize().unwrap();
214+
215+
let tests = compiletest::make_tests(&config);
216+
217+
let current_dir = env::current_dir().unwrap();
218+
let filter = env::var("TESTNAME").ok();
219+
let res = run_tests(&config, &filter, tests);
220+
env::set_current_dir(current_dir).unwrap();
221+
147222
match res {
148223
Ok(true) => {},
149224
Ok(false) => panic!("Some tests failed"),
@@ -165,4 +240,5 @@ fn compile_test() {
165240
let mut config = default_config();
166241
run_mode(&mut config);
167242
run_ui_toml(&mut config);
243+
run_ui_cargo(&mut config);
168244
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
#
3+
# A script to update the references for all tests. The idea is that
4+
# you do a run, which will generate files in the build directory
5+
# containing the (normalized) actual output of the compiler. You then
6+
# run this script, which will copy those files over. If you find
7+
# yourself manually editing a foo.stderr file, you're doing it wrong.
8+
#
9+
# See all `update-references.sh`, if you just want to update a single test.
10+
11+
if [[ "$1" == "--help" || "$1" == "-h" ]]; then
12+
echo "usage: $0"
13+
fi
14+
15+
BUILD_DIR=$PWD/target/debug/test_build_base
16+
MY_DIR=$(dirname "$0")
17+
cd "$MY_DIR" || exit
18+
find . -name '*.rs' -exec ./update-references.sh "$BUILD_DIR" {} +

tests/ui-cargo/update-references.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/bin/bash
2+
3+
# A script to update the references for particular tests. The idea is
4+
# that you do a run, which will generate files in the build directory
5+
# containing the (normalized) actual output of the compiler. This
6+
# script will then copy that output and replace the "expected output"
7+
# files. You can then commit the changes.
8+
#
9+
# If you find yourself manually editing a foo.stderr file, you're
10+
# doing it wrong.
11+
12+
if [[ "$1" == "--help" || "$1" == "-h" || "$1" == "" || "$2" == "" ]]; then
13+
echo "usage: $0 <build-directory> <relative-path-to-rs-files>"
14+
echo ""
15+
echo "For example:"
16+
echo " $0 ../../../build/x86_64-apple-darwin/test/ui *.rs */*.rs"
17+
fi
18+
19+
MYDIR=$(dirname "$0")
20+
21+
BUILD_DIR="$1"
22+
shift
23+
24+
while [[ "$1" != "" ]]; do
25+
STDERR_NAME="${1/%.rs/.stderr}"
26+
STDOUT_NAME="${1/%.rs/.stdout}"
27+
shift
28+
if [[ -f "$BUILD_DIR"/"$STDOUT_NAME" ]] && \
29+
! (cmp -s -- "$BUILD_DIR"/"$STDOUT_NAME" "$MYDIR"/"$STDOUT_NAME"); then
30+
echo updating "$MYDIR"/"$STDOUT_NAME"
31+
cp "$BUILD_DIR"/"$STDOUT_NAME" "$MYDIR"/"$STDOUT_NAME"
32+
fi
33+
if [[ -f "$BUILD_DIR"/"$STDERR_NAME" ]] && \
34+
! (cmp -s -- "$BUILD_DIR"/"$STDERR_NAME" "$MYDIR"/"$STDERR_NAME"); then
35+
echo updating "$MYDIR"/"$STDERR_NAME"
36+
cp "$BUILD_DIR"/"$STDERR_NAME" "$MYDIR"/"$STDERR_NAME"
37+
fi
38+
done

0 commit comments

Comments
 (0)