Skip to content

Commit ba411d6

Browse files
committed
test(rustfix): Seperated tests to different testcases
1 parent 307cbfd commit ba411d6

File tree

3 files changed

+45
-72
lines changed

3 files changed

+45
-72
lines changed

Cargo.lock

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/rustfix/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rustfix"
3-
version = "0.9.0"
3+
version = "0.9.1"
44
authors = [
55
"Pascal Hertleif <[email protected]>",
66
"Oliver Schneider <[email protected]>",
@@ -29,6 +29,7 @@ proptest.workspace = true
2929
similar.workspace = true
3030
tempfile.workspace = true
3131
tracing-subscriber.workspace = true
32+
snapbox.workspace = true
3233

3334
[lints]
3435
workspace = true

crates/rustfix/tests/parse_and_replace.rs

Lines changed: 41 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@
2222
2323
#![allow(clippy::disallowed_methods, clippy::print_stdout, clippy::print_stderr)]
2424

25-
use anyhow::{anyhow, ensure, Context, Error};
25+
use anyhow::{anyhow, Context, Error};
2626
use rustfix::apply_suggestions;
2727
use std::collections::HashSet;
2828
use std::env;
2929
use std::ffi::OsString;
3030
use std::fs;
31-
use std::path::{Path, PathBuf};
31+
use std::path::Path;
3232
use std::process::{Command, Output};
3333
use tempfile::tempdir;
34-
use tracing::{debug, info, warn};
34+
use tracing::info;
3535

3636
mod fixmode {
3737
pub const EVERYTHING: &str = "yolo";
@@ -151,7 +151,7 @@ fn diff(expected: &str, actual: &str) -> String {
151151
res
152152
}
153153

154-
fn test_rustfix_with_file<P: AsRef<Path>>(file: P, mode: &str) -> Result<(), Error> {
154+
fn test_rustfix_with_file<P: AsRef<Path>>(file: P, mode: &str) {
155155
let file: &Path = file.as_ref();
156156
let json_file = file.with_extension("json");
157157
let fixed_file = file.with_extension("fixed.rs");
@@ -162,26 +162,25 @@ fn test_rustfix_with_file<P: AsRef<Path>>(file: P, mode: &str) -> Result<(), Err
162162
rustfix::Filter::MachineApplicableOnly
163163
};
164164

165-
debug!("next up: {:?}", file);
166-
let code = fs::read_to_string(file)?;
165+
let code = fs::read_to_string(file).unwrap();
167166
let errors = compile_and_get_json_errors(file)
168-
.with_context(|| format!("could not compile {}", file.display()))?;
167+
.with_context(|| format!("could not compile {}", file.display())).unwrap();
169168
let suggestions =
170169
rustfix::get_suggestions_from_json(&errors, &HashSet::new(), filter_suggestions)
171-
.context("could not load suggestions")?;
170+
.context("could not load suggestions").unwrap();
172171

173172
if std::env::var(settings::RECORD_JSON).is_ok() {
174-
fs::write(file.with_extension("recorded.json"), &errors)?;
173+
fs::write(file.with_extension("recorded.json"), &errors).unwrap();
175174
}
176175

177176
if std::env::var(settings::CHECK_JSON).is_ok() {
178177
let expected_json = fs::read_to_string(&json_file)
179-
.with_context(|| format!("could not load json fixtures for {}", file.display()))?;
178+
.with_context(|| format!("could not load json fixtures for {}", file.display())).unwrap();
180179
let expected_suggestions =
181180
rustfix::get_suggestions_from_json(&expected_json, &HashSet::new(), filter_suggestions)
182-
.context("could not load expected suggestions")?;
181+
.context("could not load expected suggestions").unwrap();
183182

184-
ensure!(
183+
assert!(
185184
expected_suggestions == suggestions,
186185
"got unexpected suggestions from clippy:\n{}",
187186
diff(
@@ -192,86 +191,58 @@ fn test_rustfix_with_file<P: AsRef<Path>>(file: P, mode: &str) -> Result<(), Err
192191
}
193192

194193
let fixed = apply_suggestions(&code, &suggestions)
195-
.with_context(|| format!("could not apply suggestions to {}", file.display()))?
194+
.with_context(|| format!("could not apply suggestions to {}", file.display())).unwrap()
196195
.replace('\r', "");
197196

198197
if std::env::var(settings::RECORD_FIXED_RUST).is_ok() {
199-
fs::write(file.with_extension("recorded.rs"), &fixed)?;
198+
fs::write(file.with_extension("recorded.rs"), &fixed).unwrap();
200199
}
201200

202201
if let Some(bless_name) = std::env::var_os(settings::BLESS) {
203202
if bless_name == file.file_name().unwrap() {
204-
std::fs::write(&json_file, &errors)?;
205-
std::fs::write(&fixed_file, &fixed)?;
203+
std::fs::write(&json_file, &errors).unwrap();
204+
std::fs::write(&fixed_file, &fixed).unwrap();
206205
}
207206
}
208207

209208
let expected_fixed = fs::read_to_string(&fixed_file)
210-
.with_context(|| format!("could read fixed file for {}", file.display()))?
209+
.with_context(|| format!("could read fixed file for {}", file.display())).unwrap()
211210
.replace('\r', "");
212-
ensure!(
211+
assert!(
213212
fixed.trim() == expected_fixed.trim(),
214213
"file {} doesn't look fixed:\n{}",
215214
file.display(),
216215
diff(fixed.trim(), expected_fixed.trim())
217216
);
218217

219-
compiles_without_errors(&fixed_file)?;
218+
compiles_without_errors(&fixed_file).unwrap();
220219

221-
Ok(())
222220
}
223221

224-
fn get_fixture_files(p: &str) -> Result<Vec<PathBuf>, Error> {
225-
Ok(fs::read_dir(p)?
226-
.map(|e| e.unwrap().path())
227-
.filter(|p| p.is_file())
228-
.filter(|p| {
229-
let x = p.to_string_lossy();
230-
x.ends_with(".rs") && !x.ends_with(".fixed.rs") && !x.ends_with(".recorded.rs")
231-
})
232-
.collect())
233-
}
234-
235-
fn assert_fixtures(dir: &str, mode: &str) {
236-
let files = get_fixture_files(dir)
237-
.with_context(|| format!("couldn't load dir `{dir}`"))
238-
.unwrap();
239-
let mut failures = 0;
240-
241-
let is_not_nightly = !version().1;
242-
243-
for file in &files {
244-
if file
245-
.file_stem()
246-
.unwrap()
247-
.to_str()
248-
.unwrap()
249-
.ends_with(".nightly")
250-
&& is_not_nightly
251-
{
252-
info!("skipped: {file:?}");
253-
continue;
254-
}
255-
if let Err(err) = test_rustfix_with_file(file, mode) {
256-
println!("failed: {}", file.display());
257-
warn!("{:?}", err);
258-
failures += 1;
222+
macro_rules! run_test {
223+
($name:ident, $file:expr) => {
224+
#[test]
225+
#[allow(non_snake_case)]
226+
fn $name() {
227+
let (_, nightly) = version();
228+
if !$file.ends_with(".nightly.rs") || nightly {
229+
let file = Path::new(concat!("./tests/everything/", $file));
230+
assert!(file.is_file(), "could not load {}", $file);
231+
test_rustfix_with_file(file, fixmode::EVERYTHING);
232+
}
259233
}
260-
info!("passed: {:?}", file);
261-
}
262-
263-
if failures > 0 {
264-
panic!(
265-
"{} out of {} fixture asserts failed\n\
266-
(run with `env RUST_LOG=parse_and_replace=info` to get more details)",
267-
failures,
268-
files.len(),
269-
);
270-
}
234+
};
271235
}
272236

273-
#[test]
274-
fn everything() {
275-
tracing_subscriber::fmt::init();
276-
assert_fixtures("./tests/everything", fixmode::EVERYTHING);
237+
run_test! {
238+
closure_immutable_outer_variable,
239+
"closure-immutable-outer-variable.rs"
277240
}
241+
run_test! {dedup_suggestions, "dedup-suggestions.rs"}
242+
run_test! {E0178, "E0178.rs"}
243+
run_test! {handle_insert_only, "handle-insert-only.rs"}
244+
run_test! {lt_generic_comp, "lt-generic-comp.rs"}
245+
run_test! {multiple_solutions, "multiple-solutions.nightly.rs"}
246+
run_test! {replace_only_one_char, "replace-only-one-char.rs"}
247+
run_test! {str_lit_type_mismatch, "str-lit-type-mismatch.rs"}
248+
run_test! {use_insert, "use-insert.rs"}

0 commit comments

Comments
 (0)