Skip to content

Commit 937de12

Browse files
authored
check-formatter-stability: Remove newlines and add --error-file (#5491)
## Summary This makes the output of `check-formatter-stability` more concise by removing extraneous newlines. It also adds a `--error-file` option to that script that allows creating a file with just the errors (without the status messages) to share with others. ## Test Plan I ran it over CPython and looked at the output. I then added the `--error-file` option and looked at the contents of the file
1 parent 787e2fd commit 937de12

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

crates/ruff_dev/src/check_formatter_stability.rs

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
//! checking entire repositories.
55
66
use std::fmt::{Display, Formatter};
7-
use std::io::stdout;
7+
use std::fs::File;
88
use std::io::Write;
9+
use std::io::{stdout, BufWriter};
910
use std::panic::catch_unwind;
1011
use std::path::{Path, PathBuf};
1112
use std::process::ExitCode;
@@ -49,6 +50,9 @@ pub(crate) struct Args {
4950
/// Checks each project inside a directory
5051
#[arg(long)]
5152
pub(crate) multi_project: bool,
53+
/// Write all errors to this file in addition to stdout
54+
#[arg(long)]
55+
pub(crate) error_file: Option<PathBuf>,
5256
}
5357

5458
/// Generate ourself a `try_parse_from` impl for `CheckArgs`. This is a strange way to use clap but
@@ -69,6 +73,12 @@ pub(crate) fn main(args: &Args) -> anyhow::Result<ExitCode> {
6973
#[allow(clippy::print_stdout)]
7074
{
7175
print!("{}", result.display(args.format));
76+
println!(
77+
"Found {} stability errors in {} files in {:.2}s",
78+
result.diagnostics.len(),
79+
result.file_count,
80+
result.duration.as_secs_f32(),
81+
);
7282
}
7383

7484
result.is_success()
@@ -114,6 +124,7 @@ fn check_multi_project(args: &Args) -> bool {
114124

115125
match check_repo(&Args {
116126
files: vec![path.clone()],
127+
error_file: args.error_file.clone(),
117128
..*args
118129
}) {
119130
Ok(result) => sender.send(Message::Finished { result, path }),
@@ -126,6 +137,9 @@ fn check_multi_project(args: &Args) -> bool {
126137

127138
scope.spawn(|_| {
128139
let mut stdout = stdout().lock();
140+
let mut error_file = args.error_file.as_ref().map(|error_file| {
141+
BufWriter::new(File::create(error_file).expect("Couldn't open error file"))
142+
});
129143

130144
for message in receiver {
131145
match message {
@@ -135,13 +149,19 @@ fn check_multi_project(args: &Args) -> bool {
135149
Message::Finished { path, result } => {
136150
total_errors += result.diagnostics.len();
137151
total_files += result.file_count;
152+
138153
writeln!(
139154
stdout,
140-
"Finished {}\n{}\n",
155+
"Finished {} with {} files in {:.2}s",
141156
path.display(),
142-
result.display(args.format)
157+
result.file_count,
158+
result.duration.as_secs_f32(),
143159
)
144160
.unwrap();
161+
write!(stdout, "{}", result.display(args.format)).unwrap();
162+
if let Some(error_file) = &mut error_file {
163+
write!(error_file, "{}", result.display(args.format)).unwrap();
164+
}
145165
all_success = all_success && result.is_success();
146166
}
147167
Message::Failed { path, error } => {
@@ -157,8 +177,10 @@ fn check_multi_project(args: &Args) -> bool {
157177

158178
#[allow(clippy::print_stdout)]
159179
{
160-
println!("{total_errors} stability errors in {total_files} files");
161-
println!("Finished in {}s", duration.as_secs_f32());
180+
println!(
181+
"{total_errors} stability errors in {total_files} files in {}s",
182+
duration.as_secs_f32()
183+
);
162184
}
163185

164186
all_success
@@ -295,23 +317,11 @@ struct DisplayCheckRepoResult<'a> {
295317
}
296318

297319
impl Display for DisplayCheckRepoResult<'_> {
298-
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
299-
let CheckRepoResult {
300-
duration,
301-
file_count,
302-
diagnostics,
303-
} = self.result;
304-
305-
for diagnostic in diagnostics {
320+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
321+
for diagnostic in &self.result.diagnostics {
306322
write!(f, "{}", diagnostic.display(self.format))?;
307323
}
308-
309-
writeln!(
310-
f,
311-
"Formatting {} files twice took {:.2}s",
312-
file_count,
313-
duration.as_secs_f32()
314-
)
324+
Ok(())
315325
}
316326
}
317327

0 commit comments

Comments
 (0)