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
This commit is contained in:
konsti 2023-07-04 09:54:35 +02:00 committed by GitHub
parent 787e2fd49d
commit 937de121f3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -4,8 +4,9 @@
//! checking entire repositories. //! checking entire repositories.
use std::fmt::{Display, Formatter}; use std::fmt::{Display, Formatter};
use std::io::stdout; use std::fs::File;
use std::io::Write; use std::io::Write;
use std::io::{stdout, BufWriter};
use std::panic::catch_unwind; use std::panic::catch_unwind;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::process::ExitCode; use std::process::ExitCode;
@ -49,6 +50,9 @@ pub(crate) struct Args {
/// Checks each project inside a directory /// Checks each project inside a directory
#[arg(long)] #[arg(long)]
pub(crate) multi_project: bool, pub(crate) multi_project: bool,
/// Write all errors to this file in addition to stdout
#[arg(long)]
pub(crate) error_file: Option<PathBuf>,
} }
/// Generate ourself a `try_parse_from` impl for `CheckArgs`. This is a strange way to use clap but /// 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> {
#[allow(clippy::print_stdout)] #[allow(clippy::print_stdout)]
{ {
print!("{}", result.display(args.format)); print!("{}", result.display(args.format));
println!(
"Found {} stability errors in {} files in {:.2}s",
result.diagnostics.len(),
result.file_count,
result.duration.as_secs_f32(),
);
} }
result.is_success() result.is_success()
@ -114,6 +124,7 @@ fn check_multi_project(args: &Args) -> bool {
match check_repo(&Args { match check_repo(&Args {
files: vec![path.clone()], files: vec![path.clone()],
error_file: args.error_file.clone(),
..*args ..*args
}) { }) {
Ok(result) => sender.send(Message::Finished { result, path }), Ok(result) => sender.send(Message::Finished { result, path }),
@ -126,6 +137,9 @@ fn check_multi_project(args: &Args) -> bool {
scope.spawn(|_| { scope.spawn(|_| {
let mut stdout = stdout().lock(); let mut stdout = stdout().lock();
let mut error_file = args.error_file.as_ref().map(|error_file| {
BufWriter::new(File::create(error_file).expect("Couldn't open error file"))
});
for message in receiver { for message in receiver {
match message { match message {
@ -135,13 +149,19 @@ fn check_multi_project(args: &Args) -> bool {
Message::Finished { path, result } => { Message::Finished { path, result } => {
total_errors += result.diagnostics.len(); total_errors += result.diagnostics.len();
total_files += result.file_count; total_files += result.file_count;
writeln!( writeln!(
stdout, stdout,
"Finished {}\n{}\n", "Finished {} with {} files in {:.2}s",
path.display(), path.display(),
result.display(args.format) result.file_count,
result.duration.as_secs_f32(),
) )
.unwrap(); .unwrap();
write!(stdout, "{}", result.display(args.format)).unwrap();
if let Some(error_file) = &mut error_file {
write!(error_file, "{}", result.display(args.format)).unwrap();
}
all_success = all_success && result.is_success(); all_success = all_success && result.is_success();
} }
Message::Failed { path, error } => { Message::Failed { path, error } => {
@ -157,8 +177,10 @@ fn check_multi_project(args: &Args) -> bool {
#[allow(clippy::print_stdout)] #[allow(clippy::print_stdout)]
{ {
println!("{total_errors} stability errors in {total_files} files"); println!(
println!("Finished in {}s", duration.as_secs_f32()); "{total_errors} stability errors in {total_files} files in {}s",
duration.as_secs_f32()
);
} }
all_success all_success
@ -295,23 +317,11 @@ struct DisplayCheckRepoResult<'a> {
} }
impl Display for DisplayCheckRepoResult<'_> { impl Display for DisplayCheckRepoResult<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let CheckRepoResult { for diagnostic in &self.result.diagnostics {
duration,
file_count,
diagnostics,
} = self.result;
for diagnostic in diagnostics {
write!(f, "{}", diagnostic.display(self.format))?; write!(f, "{}", diagnostic.display(self.format))?;
} }
Ok(())
writeln!(
f,
"Formatting {} files twice took {:.2}s",
file_count,
duration.as_secs_f32()
)
} }
} }