Skip to content

Commit c54fa88

Browse files
committed
Cleanup rustfix parse_and_replace test.
This is just some minor code cleanup for the parse_and_replace test, there should not be any functional differences.
1 parent ef94adb commit c54fa88

File tree

1 file changed

+28
-29
lines changed

1 file changed

+28
-29
lines changed

crates/rustfix/tests/parse_and_replace.rs

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
//! Tests that verify rustfix applies the appropriate changes to a file.
2+
//!
3+
//! This test works by reading a series of `*.rs` files in the
4+
//! `tests/everything` directory. For each `.rs` file, it runs `rustc` to
5+
//! collect JSON diagnostics from the file. It feeds that JSON data into
6+
//! rustfix and applies the recommended suggestions to the `.rs` file. It then
7+
//! compares the result with the corresponding `.fixed.rs` file. If they don't
8+
//! match, then the test fails.
9+
//!
10+
//! There are several debugging environment variables for this test that you can set:
11+
//!
12+
//! - `RUST_LOG=parse_and_replace=debug`: Print debug information.
13+
//! - `RUSTFIX_TEST_RECORD_JSON=1`: Records the JSON output to
14+
//! `*.recorded.json` files. You can then move that to `.json` or whatever
15+
//! you need.
16+
//! - `RUSTFIX_TEST_RECORD_FIXED_RUST=1`: Records the fixed result to
17+
//! `*.recorded.rs` files. You can then move that to `.rs` or whatever you
18+
//! need.
19+
120
#![allow(clippy::disallowed_methods, clippy::print_stdout, clippy::print_stderr)]
221

322
use anyhow::{anyhow, ensure, Context, Error};
@@ -79,15 +98,6 @@ fn compiles_without_errors(file: &Path) -> Result<(), Error> {
7998
}
8099
}
81100

82-
fn read_file(path: &Path) -> Result<String, Error> {
83-
use std::io::Read;
84-
85-
let mut buffer = String::new();
86-
let mut file = fs::File::open(path)?;
87-
file.read_to_string(&mut buffer)?;
88-
Ok(buffer)
89-
}
90-
91101
fn diff(expected: &str, actual: &str) -> String {
92102
use similar::{ChangeTag, TextDiff};
93103
use std::fmt::Write;
@@ -104,11 +114,7 @@ fn diff(expected: &str, actual: &str) -> String {
104114
ChangeTag::Delete => "-",
105115
};
106116
if !different {
107-
write!(
108-
&mut res,
109-
"differences found (+ == actual, - == expected):\n"
110-
)
111-
.unwrap();
117+
writeln!(&mut res, "differences found (+ == actual, - == expected):").unwrap();
112118
different = true;
113119
}
114120
write!(&mut res, "{} {}", prefix, change.value()).unwrap();
@@ -133,23 +139,19 @@ fn test_rustfix_with_file<P: AsRef<Path>>(file: P, mode: &str) -> Result<(), Err
133139
};
134140

135141
debug!("next up: {:?}", file);
136-
let code = read_file(file).context(format!("could not read {}", file.display()))?;
142+
let code = fs::read_to_string(file)?;
137143
let errors =
138144
compile_and_get_json_errors(file).context(format!("could compile {}", file.display()))?;
139145
let suggestions =
140146
rustfix::get_suggestions_from_json(&errors, &HashSet::new(), filter_suggestions)
141147
.context("could not load suggestions")?;
142148

143149
if std::env::var(settings::RECORD_JSON).is_ok() {
144-
use std::io::Write;
145-
let mut recorded_json = fs::File::create(&file.with_extension("recorded.json")).context(
146-
format!("could not create recorded.json for {}", file.display()),
147-
)?;
148-
recorded_json.write_all(errors.as_bytes())?;
150+
fs::write(file.with_extension("recorded.json"), &errors)?;
149151
}
150152

151153
if std::env::var(settings::CHECK_JSON).is_ok() {
152-
let expected_json = read_file(&json_file).context(format!(
154+
let expected_json = fs::read_to_string(&json_file).context(format!(
153155
"could not load json fixtures for {}",
154156
file.display()
155157
))?;
@@ -171,13 +173,11 @@ fn test_rustfix_with_file<P: AsRef<Path>>(file: P, mode: &str) -> Result<(), Err
171173
.context(format!("could not apply suggestions to {}", file.display()))?;
172174

173175
if std::env::var(settings::RECORD_FIXED_RUST).is_ok() {
174-
use std::io::Write;
175-
let mut recorded_rust = fs::File::create(&file.with_extension("recorded.rs"))?;
176-
recorded_rust.write_all(fixed.as_bytes())?;
176+
fs::write(file.with_extension("recorded.rs"), &fixed)?;
177177
}
178178

179-
let expected_fixed =
180-
read_file(&fixed_file).context(format!("could read fixed file for {}", file.display()))?;
179+
let expected_fixed = fs::read_to_string(&fixed_file)
180+
.context(format!("could read fixed file for {}", file.display()))?;
181181
ensure!(
182182
fixed.trim() == expected_fixed.trim(),
183183
"file {} doesn't look fixed:\n{}",
@@ -191,8 +191,7 @@ fn test_rustfix_with_file<P: AsRef<Path>>(file: P, mode: &str) -> Result<(), Err
191191
}
192192

193193
fn get_fixture_files(p: &str) -> Result<Vec<PathBuf>, Error> {
194-
Ok(fs::read_dir(&p)?
195-
.into_iter()
194+
Ok(fs::read_dir(p)?
196195
.map(|e| e.unwrap().path())
197196
.filter(|p| p.is_file())
198197
.filter(|p| {
@@ -203,7 +202,7 @@ fn get_fixture_files(p: &str) -> Result<Vec<PathBuf>, Error> {
203202
}
204203

205204
fn assert_fixtures(dir: &str, mode: &str) {
206-
let files = get_fixture_files(&dir)
205+
let files = get_fixture_files(dir)
207206
.context(format!("couldn't load dir `{}`", dir))
208207
.unwrap();
209208
let mut failures = 0;

0 commit comments

Comments
 (0)