Use a separate PrinterFlag for including fix diffs (#4615)

This commit is contained in:
Charlie Marsh 2023-05-24 10:22:37 -04:00 committed by GitHub
parent 8961d8eb6f
commit 040fb9cef4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 36 additions and 33 deletions

View file

@ -392,8 +392,9 @@ impl CheckArgs {
add_noqa: self.add_noqa,
config: self.config,
diff: self.diff,
exit_zero: self.exit_zero,
ecosystem_ci: self.ecosystem_ci,
exit_non_zero_on_fix: self.exit_non_zero_on_fix,
exit_zero: self.exit_zero,
files: self.files,
ignore_noqa: self.ignore_noqa,
isolated: self.isolated,
@ -457,8 +458,9 @@ pub struct Arguments {
pub add_noqa: bool,
pub config: Option<PathBuf>,
pub diff: bool,
pub exit_zero: bool,
pub ecosystem_ci: bool,
pub exit_non_zero_on_fix: bool,
pub exit_zero: bool,
pub files: Vec<PathBuf>,
pub ignore_noqa: bool,
pub isolated: bool,

View file

@ -254,7 +254,6 @@ mod test {
LogLevel::Default,
FixMode::None,
Flags::SHOW_VIOLATIONS,
false,
);
let mut writer: Vec<u8> = Vec::new();
// Mute the terminal color codes

View file

@ -155,13 +155,6 @@ fn format(files: &[PathBuf]) -> Result<ExitStatus> {
}
fn check(args: CheckArgs, log_level: LogLevel) -> Result<ExitStatus> {
let ecosystem_ci = args.ecosystem_ci;
if ecosystem_ci {
warn_user_once!(
"The formatting of fixes emitted by this option is a work-in-progress, subject to \
change at any time, and intended for use with the ecosystem ci scripts only."
);
}
let (cli, overrides) = args.partition();
// Construct the "default" settings. These are used when no `pyproject.toml`
@ -219,11 +212,18 @@ fn check(args: CheckArgs, log_level: LogLevel) -> Result<ExitStatus> {
printer_flags |= PrinterFlags::SHOW_VIOLATIONS;
}
if show_fixes {
printer_flags |= PrinterFlags::SHOW_FIXES;
printer_flags |= PrinterFlags::SHOW_FIX_SUMMARY;
}
if show_source {
printer_flags |= PrinterFlags::SHOW_SOURCE;
}
if cli.ecosystem_ci {
warn_user_once!(
"The formatting of fixes emitted by this option is a work-in-progress, subject to \
change at any time, and intended only for internal use."
);
printer_flags |= PrinterFlags::SHOW_FIX_DIFF;
}
#[cfg(debug_assertions)]
if cache {
@ -248,7 +248,7 @@ fn check(args: CheckArgs, log_level: LogLevel) -> Result<ExitStatus> {
return Ok(ExitStatus::Success);
}
let printer = Printer::new(format, log_level, autofix, printer_flags, ecosystem_ci);
let printer = Printer::new(format, log_level, autofix, printer_flags);
if cli.watch {
if format != SerializationFormat::Text {

View file

@ -28,9 +28,14 @@ use crate::diagnostics::Diagnostics;
bitflags! {
#[derive(Default, Debug, Copy, Clone)]
pub(crate) struct Flags: u8 {
/// Whether to show violations when emitting diagnostics.
const SHOW_VIOLATIONS = 0b0000_0001;
const SHOW_FIXES = 0b0000_0010;
const SHOW_SOURCE = 0b000_0100;
/// Whether to show the source code when emitting diagnostics.
const SHOW_SOURCE = 0b000_0010;
/// Whether to show a summary of the fixed violations when emitting diagnostics.
const SHOW_FIX_SUMMARY = 0b0000_0100;
/// Whether to show a diff of each fixed violation when emitting diagnostics.
const SHOW_FIX_DIFF = 0b0000_1000;
}
}
@ -70,8 +75,6 @@ pub(crate) struct Printer {
log_level: LogLevel,
autofix_level: flags::FixMode,
flags: Flags,
/// Dev-only argument to show fixes
ecosystem_ci: bool,
}
impl Printer {
@ -80,14 +83,12 @@ impl Printer {
log_level: LogLevel,
autofix_level: flags::FixMode,
flags: Flags,
ecosystem_ci: bool,
) -> Self {
Self {
format,
log_level,
autofix_level,
flags,
ecosystem_ci,
}
}
@ -165,10 +166,10 @@ impl Printer {
self.format,
SerializationFormat::Text | SerializationFormat::Grouped
) {
if self.flags.contains(Flags::SHOW_FIXES) {
if self.flags.contains(Flags::SHOW_FIX_SUMMARY) {
if !diagnostics.fixed.is_empty() {
writeln!(writer)?;
print_fixed(writer, &diagnostics.fixed)?;
print_fix_summary(writer, &diagnostics.fixed)?;
writeln!(writer)?;
}
}
@ -187,18 +188,16 @@ impl Printer {
JunitEmitter::default().emit(writer, &diagnostics.messages, &context)?;
}
SerializationFormat::Text => {
let show_fixes = self.ecosystem_ci && self.flags.contains(Flags::SHOW_FIXES);
TextEmitter::default()
.with_show_fix_status(show_fix_status(self.autofix_level))
.with_show_fix(show_fixes)
.with_show_fix_diff(self.flags.contains(Flags::SHOW_FIX_DIFF))
.with_show_source(self.flags.contains(Flags::SHOW_SOURCE))
.emit(writer, &diagnostics.messages, &context)?;
if self.flags.contains(Flags::SHOW_FIXES) {
if self.flags.contains(Flags::SHOW_FIX_SUMMARY) {
if !diagnostics.fixed.is_empty() {
writeln!(writer)?;
print_fixed(writer, &diagnostics.fixed)?;
print_fix_summary(writer, &diagnostics.fixed)?;
writeln!(writer)?;
}
}
@ -211,10 +210,10 @@ impl Printer {
.with_show_fix_status(show_fix_status(self.autofix_level))
.emit(writer, &diagnostics.messages, &context)?;
if self.flags.contains(Flags::SHOW_FIXES) {
if self.flags.contains(Flags::SHOW_FIX_SUMMARY) {
if !diagnostics.fixed.is_empty() {
writeln!(writer)?;
print_fixed(writer, &diagnostics.fixed)?;
print_fix_summary(writer, &diagnostics.fixed)?;
writeln!(writer)?;
}
}
@ -392,7 +391,7 @@ const fn show_fix_status(autofix_level: flags::FixMode) -> bool {
!matches!(autofix_level, flags::FixMode::Apply)
}
fn print_fixed<T: Write>(stdout: &mut T, fixed: &FxHashMap<String, FixTable>) -> Result<()> {
fn print_fix_summary<T: Write>(stdout: &mut T, fixed: &FxHashMap<String, FixTable>) -> Result<()> {
let total = fixed
.values()
.map(|table| table.values().sum::<usize>())