Write summary messages to stderr when fixing via stdin (instead of omitting them) (#7838)

Previously we just omitted diagnostic summaries when using `--fix` or
`--diff` with a stdin file. Now, we still write the summaries to stderr
instead of the main writer (which is generally stdout but could be
changed by `--output-file`).
This commit is contained in:
Zanie Blue 2023-10-06 12:11:03 -05:00 committed by GitHub
parent 4f95df1b6d
commit 3c25d261fe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 9 deletions

View file

@ -208,6 +208,7 @@ pub fn check(args: CheckCommand, log_level: LogLevel) -> Result<ExitStatus> {
} }
_ => Box::new(BufWriter::new(io::stdout())), _ => Box::new(BufWriter::new(io::stdout())),
}; };
let stderr_writer = Box::new(BufWriter::new(io::stderr()));
if cli.show_settings { if cli.show_settings {
commands::show_settings::show_settings( commands::show_settings::show_settings(
@ -392,15 +393,18 @@ pub fn check(args: CheckCommand, log_level: LogLevel) -> Result<ExitStatus> {
)? )?
}; };
// Always try to print violations (the printer itself may suppress output), // Always try to print violations (though the printer itself may suppress output)
// unless we're writing fixes via stdin (in which case, the transformed // If we're writing fixes via stdin, the transformed source code goes to the writer
// source code goes to stdout). // so send the summary to stderr instead
if !(is_stdin && matches!(fix_mode, FixMode::Apply | FixMode::Diff)) { let mut summary_writer = if is_stdin && matches!(fix_mode, FixMode::Apply | FixMode::Diff) {
if cli.statistics { stderr_writer
printer.write_statistics(&diagnostics, &mut writer)?;
} else { } else {
printer.write_once(&diagnostics, &mut writer)?; writer
} };
if cli.statistics {
printer.write_statistics(&diagnostics, &mut summary_writer)?;
} else {
printer.write_once(&diagnostics, &mut summary_writer)?;
} }
if !cli.exit_zero { if !cli.exit_zero {

View file

@ -150,6 +150,7 @@ fn stdin_fix_py() {
print(sys.version) print(sys.version)
----- stderr ----- ----- stderr -----
Found 1 error (1 fixed, 0 remaining).
"###); "###);
} }
@ -317,6 +318,7 @@ fn stdin_fix_jupyter() {
"nbformat_minor": 5 "nbformat_minor": 5
} }
----- stderr ----- ----- stderr -----
Found 2 errors (2 fixed, 0 remaining).
"###); "###);
} }
@ -336,6 +338,8 @@ fn stdin_fix_when_not_fixable_should_still_print_contents() {
print(sys.version) print(sys.version)
----- stderr ----- ----- stderr -----
-:3:4: F634 If test is a tuple, which is always `True`
Found 2 errors (1 fixed, 1 remaining).
"###); "###);
} }
@ -961,6 +965,9 @@ fn fix_applies_safe_fixes_by_default() {
print('foo') print('foo')
----- stderr ----- ----- stderr -----
-:1:14: F601 Dictionary key literal `'a'` repeated
Found 2 errors (1 fixed, 1 remaining).
1 hidden fix can be enabled with the `--unsafe-fixes` option.
"###); "###);
} }
@ -988,6 +995,7 @@ fn fix_applies_unsafe_fixes_with_opt_in() {
print('foo') print('foo')
----- stderr ----- ----- stderr -----
Found 2 errors (2 fixed, 0 remaining).
"###); "###);
} }
@ -1014,6 +1022,7 @@ fn fix_only_flag_applies_safe_fixes_by_default() {
print('foo') print('foo')
----- stderr ----- ----- stderr -----
Fixed 1 error.
"###); "###);
} }
@ -1041,6 +1050,7 @@ fn fix_only_flag_applies_unsafe_fixes_with_opt_in() {
print('foo') print('foo')
----- stderr ----- ----- stderr -----
Fixed 2 errors.
"###); "###);
} }
@ -1070,6 +1080,7 @@ fn diff_shows_safe_fixes_by_default() {
----- stderr ----- ----- stderr -----
Would fix 1 error.
"### "###
); );
} }
@ -1102,6 +1113,7 @@ fn diff_shows_unsafe_fixes_with_opt_in() {
----- stderr ----- ----- stderr -----
Would fix 2 errors.
"### "###
); );
} }