Skip to content
This repository was archived by the owner on Nov 24, 2023. It is now read-only.

Commit 5f1c320

Browse files
committed
tests: use the cached json errors instead of calling rustc
1 parent 4a577a6 commit 5f1c320

File tree

1 file changed

+4
-46
lines changed

1 file changed

+4
-46
lines changed

tests/parse_and_replace.rs

Lines changed: 4 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ mod fixmode {
2929

3030
mod settings {
3131
// can be set as env var to debug
32-
pub const CHECK_JSON: &str = "RUSTFIX_TEST_CHECK_JSON";
33-
pub const RECORD_JSON: &str = "RUSTFIX_TEST_RECORD_JSON";
3432
pub const RECORD_FIXED_RUST: &str = "RUSTFIX_TEST_RECORD_FIXED_RUST";
3533
}
3634

@@ -62,20 +60,6 @@ fn compile(file: &Path, mode: &str) -> Result<Output, Error> {
6260
Ok(res)
6361
}
6462

65-
fn compile_and_get_json_errors(file: &Path, mode: &str) -> Result<String, Error> {
66-
let res = compile(file, mode)?;
67-
let stderr = String::from_utf8(res.stderr)?;
68-
69-
match res.status.code() {
70-
Some(0) | Some(1) | Some(101) => Ok(stderr),
71-
_ => Err(format_err!(
72-
"failed with status {:?}: {}",
73-
res.status.code(),
74-
stderr
75-
)),
76-
}
77-
}
78-
7963
fn compiles_without_errors(file: &Path, mode: &str) -> Result<(), Error> {
8064
let res = compile(file, mode)?;
8165

@@ -122,7 +106,8 @@ fn diff(expected: &str, actual: &str) -> String {
122106
write!(
123107
&mut res,
124108
"differences found (+ == actual, - == expected):\n"
125-
).unwrap();
109+
)
110+
.unwrap();
126111
different = true;
127112
}
128113
for diff in diff.lines() {
@@ -149,39 +134,12 @@ fn test_rustfix_with_file<P: AsRef<Path>>(file: P, mode: &str) -> Result<(), Err
149134

150135
debug!("next up: {:?}", file);
151136
let code = read_file(file).context(format!("could not read {}", file.display()))?;
152-
let errors = compile_and_get_json_errors(file, mode)
153-
.context(format!("could compile {}", file.display()))?;
137+
let errors = read_file(&json_file)
138+
.with_context(|_| format!("could not load json suggestions for {}", file.display()))?;
154139
let suggestions =
155140
rustfix::get_suggestions_from_json(&errors, &HashSet::new(), filter_suggestions)
156141
.context("could not load suggestions")?;
157142

158-
if std::env::var(settings::RECORD_JSON).is_ok() {
159-
use std::io::Write;
160-
let mut recorded_json = fs::File::create(&file.with_extension("recorded.json")).context(
161-
format!("could not create recorded.json for {}", file.display()),
162-
)?;
163-
recorded_json.write_all(errors.as_bytes())?;
164-
}
165-
166-
if std::env::var(settings::CHECK_JSON).is_ok() {
167-
let expected_json = read_file(&json_file).context(format!(
168-
"could not load json fixtures for {}",
169-
file.display()
170-
))?;
171-
let expected_suggestions =
172-
rustfix::get_suggestions_from_json(&expected_json, &HashSet::new(), filter_suggestions)
173-
.context("could not load expected suggesitons")?;
174-
175-
ensure!(
176-
expected_suggestions == suggestions,
177-
"got unexpected suggestions from clippy:\n{}",
178-
diff(
179-
&format!("{:?}", expected_suggestions),
180-
&format!("{:?}", suggestions)
181-
)
182-
);
183-
}
184-
185143
let fixed = apply_suggestions(&code, &suggestions)
186144
.context(format!("could not apply suggestions to {}", file.display()))?;
187145

0 commit comments

Comments
 (0)