Skip to content

Commit 85c3aea

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

File tree

3 files changed

+53
-89
lines changed

3 files changed

+53
-89
lines changed

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/rustfix/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ proptest.workspace = true
2929
similar.workspace = true
3030
tempfile.workspace = true
3131
tracing-subscriber.workspace = true
32+
cargo-test-support.workspace = true
33+
cargo-test-macro.workspace = true
34+
snapbox.workspace = true
3235

3336
[lints]
3437
workspace = true

crates/rustfix/tests/parse_and_replace.rs

Lines changed: 47 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,17 @@
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};
26+
use cargo_test_macro::cargo_test;
2627
use rustfix::apply_suggestions;
2728
use std::collections::HashSet;
2829
use std::env;
2930
use std::ffi::OsString;
3031
use std::fs;
31-
use std::path::{Path, PathBuf};
32+
use std::path::Path;
3233
use std::process::{Command, Output};
3334
use tempfile::tempdir;
34-
use tracing::{debug, info, warn};
35+
use tracing::info;
3536

3637
mod fixmode {
3738
pub const EVERYTHING: &str = "yolo";
@@ -45,26 +46,6 @@ mod settings {
4546
pub const BLESS: &str = "RUSTFIX_TEST_BLESS";
4647
}
4748

48-
static mut VERSION: (u32, bool) = (0, false);
49-
50-
// Temporarily copy from `cargo_test_macro::version`.
51-
fn version() -> (u32, bool) {
52-
static INIT: std::sync::Once = std::sync::Once::new();
53-
INIT.call_once(|| {
54-
let output = Command::new("rustc")
55-
.arg("-V")
56-
.output()
57-
.expect("cargo should run");
58-
let stdout = std::str::from_utf8(&output.stdout).expect("utf8");
59-
let vers = stdout.split_whitespace().skip(1).next().unwrap();
60-
let is_nightly = option_env!("CARGO_TEST_DISABLE_NIGHTLY").is_none()
61-
&& (vers.contains("-nightly") || vers.contains("-dev"));
62-
let minor = vers.split('.').skip(1).next().unwrap().parse().unwrap();
63-
unsafe { VERSION = (minor, is_nightly) }
64-
});
65-
unsafe { VERSION }
66-
}
67-
6849
fn compile(file: &Path) -> Result<Output, Error> {
6950
let tmp = tempdir()?;
7051

@@ -151,7 +132,7 @@ fn diff(expected: &str, actual: &str) -> String {
151132
res
152133
}
153134

154-
fn test_rustfix_with_file<P: AsRef<Path>>(file: P, mode: &str) -> Result<(), Error> {
135+
fn test_rustfix_with_file<P: AsRef<Path>>(file: P, mode: &str) {
155136
let file: &Path = file.as_ref();
156137
let json_file = file.with_extension("json");
157138
let fixed_file = file.with_extension("fixed.rs");
@@ -162,26 +143,25 @@ fn test_rustfix_with_file<P: AsRef<Path>>(file: P, mode: &str) -> Result<(), Err
162143
rustfix::Filter::MachineApplicableOnly
163144
};
164145

165-
debug!("next up: {:?}", file);
166-
let code = fs::read_to_string(file)?;
146+
let code = fs::read_to_string(file).unwrap();
167147
let errors = compile_and_get_json_errors(file)
168-
.with_context(|| format!("could not compile {}", file.display()))?;
148+
.with_context(|| format!("could not compile {}", file.display())).unwrap();
169149
let suggestions =
170150
rustfix::get_suggestions_from_json(&errors, &HashSet::new(), filter_suggestions)
171-
.context("could not load suggestions")?;
151+
.context("could not load suggestions").unwrap();
172152

173153
if std::env::var(settings::RECORD_JSON).is_ok() {
174-
fs::write(file.with_extension("recorded.json"), &errors)?;
154+
fs::write(file.with_extension("recorded.json"), &errors).unwrap();
175155
}
176156

177157
if std::env::var(settings::CHECK_JSON).is_ok() {
178158
let expected_json = fs::read_to_string(&json_file)
179-
.with_context(|| format!("could not load json fixtures for {}", file.display()))?;
159+
.with_context(|| format!("could not load json fixtures for {}", file.display())).unwrap();
180160
let expected_suggestions =
181161
rustfix::get_suggestions_from_json(&expected_json, &HashSet::new(), filter_suggestions)
182-
.context("could not load expected suggestions")?;
162+
.context("could not load expected suggestions").unwrap();
183163

184-
ensure!(
164+
assert!(
185165
expected_suggestions == suggestions,
186166
"got unexpected suggestions from clippy:\n{}",
187167
diff(
@@ -192,86 +172,64 @@ fn test_rustfix_with_file<P: AsRef<Path>>(file: P, mode: &str) -> Result<(), Err
192172
}
193173

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

198178
if std::env::var(settings::RECORD_FIXED_RUST).is_ok() {
199-
fs::write(file.with_extension("recorded.rs"), &fixed)?;
179+
fs::write(file.with_extension("recorded.rs"), &fixed).unwrap();
200180
}
201181

202182
if let Some(bless_name) = std::env::var_os(settings::BLESS) {
203183
if bless_name == file.file_name().unwrap() {
204-
std::fs::write(&json_file, &errors)?;
205-
std::fs::write(&fixed_file, &fixed)?;
184+
std::fs::write(&json_file, &errors).unwrap();
185+
std::fs::write(&fixed_file, &fixed).unwrap();
206186
}
207187
}
208188

209189
let expected_fixed = fs::read_to_string(&fixed_file)
210-
.with_context(|| format!("could read fixed file for {}", file.display()))?
190+
.with_context(|| format!("could read fixed file for {}", file.display())).unwrap()
211191
.replace('\r', "");
212-
ensure!(
192+
assert!(
213193
fixed.trim() == expected_fixed.trim(),
214194
"file {} doesn't look fixed:\n{}",
215195
file.display(),
216196
diff(fixed.trim(), expected_fixed.trim())
217197
);
218198

219-
compiles_without_errors(&fixed_file)?;
199+
compiles_without_errors(&fixed_file).unwrap();
220200

221-
Ok(())
222201
}
223202

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;
203+
macro_rules! run_test {
204+
($name:ident, $file:expr) => {
205+
#[allow(non_snake_case)]
206+
#[cargo_test]
207+
fn $name() {
208+
let file = Path::new(concat!("./tests/everything/", $file));
209+
assert!(file.is_file(), "could not load {}", $file);
210+
test_rustfix_with_file(file, fixmode::EVERYTHING);
254211
}
255-
if let Err(err) = test_rustfix_with_file(file, mode) {
256-
println!("failed: {}", file.display());
257-
warn!("{:?}", err);
258-
failures += 1;
212+
};
213+
($name:ident, $file:expr, nightly=$reason:expr) => {
214+
#[allow(non_snake_case)]
215+
#[cargo_test(nightly, reason = $reason)]
216+
fn $name() {
217+
let file = Path::new(concat!("./tests/everything/", $file));
218+
assert!(file.is_file(), "could not load {}", $file);
219+
test_rustfix_with_file(file, fixmode::EVERYTHING);
259220
}
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-
}
221+
};
271222
}
272223

273-
#[test]
274-
fn everything() {
275-
tracing_subscriber::fmt::init();
276-
assert_fixtures("./tests/everything", fixmode::EVERYTHING);
224+
run_test! {
225+
closure_immutable_outer_variable,
226+
"closure-immutable-outer-variable.rs"
277227
}
228+
run_test! {dedup_suggestions, "dedup-suggestions.rs"}
229+
run_test! {E0178, "E0178.rs"}
230+
run_test! {handle_insert_only, "handle-insert-only.rs"}
231+
run_test! {lt_generic_comp, "lt-generic-comp.rs"}
232+
run_test! {multiple_solutions, "multiple-solutions.nightly.rs", nightly="requires nightly"}
233+
run_test! {replace_only_one_char, "replace-only-one-char.rs"}
234+
run_test! {str_lit_type_mismatch, "str-lit-type-mismatch.rs"}
235+
run_test! {use_insert, "use-insert.rs"}

0 commit comments

Comments
 (0)