Use different symbols for fix messages depending on applicability

Refactors `violation_string` as well
This commit is contained in:
Zanie 2023-10-02 21:58:44 -05:00
parent 96b8ce18c8
commit 9949fa0daf
4 changed files with 47 additions and 37 deletions

View File

@ -478,29 +478,27 @@ impl<'a> FixableStatistics<'a> {
/// Build the displayed fix status message depending on the types of the remaining fixes. /// Build the displayed fix status message depending on the types of the remaining fixes.
fn violation_string(&self) -> String { fn violation_string(&self) -> String {
let prefix = format!("[{}]", "*".cyan()); let automatic_prefix = format!("[{}]", Applicability::Automatic.symbol().cyan());
let mut fix_status = prefix; let suggested_prefix = format!("[{}]", Applicability::Suggested.symbol().cyan());
if self.automatic > 0 { if self.automatic > 0 && self.suggested > 0 {
fix_status = format!( format!(
"{fix_status} {} potentially fixable with the --fix option.", "{automatic_prefix} {} fixable with the --fix option.\n\
self.automatic {suggested_prefix} {} potentially fixable with the --fix-suggested option.",
); self.automatic, self.suggested
)
} else if self.automatic > 0 {
format!(
"{automatic_prefix} {} fixable with the --fix option.",
self.automatic,
)
} else if self.suggested > 0 {
format!(
"{suggested_prefix} {} potentially fixable with the --fix-suggested option.",
self.suggested
)
} else {
String::new()
} }
if self.suggested > 0 {
let (line_break, extra_prefix) = if self.automatic > 0 {
("\n", format!("[{}]", "*".cyan()))
} else {
("", String::new())
};
let total = self.automatic + self.suggested;
fix_status = format!(
"{fix_status}{line_break}{extra_prefix} {total} potentially fixable with the --fix-suggested option."
);
}
fix_status
} }
} }

View File

@ -46,16 +46,16 @@ fn stdin_success() {
fn stdin_error() { fn stdin_error() {
assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME)) assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))
.args(STDIN_BASE_OPTIONS) .args(STDIN_BASE_OPTIONS)
.pass_stdin("import os\n"), @r#" .pass_stdin("import os\n"), @r###"
success: false success: false
exit_code: 1 exit_code: 1
----- stdout ----- ----- stdout -----
-:1:8: F401 [*] `os` imported but unused -:1:8: F401 [*] `os` imported but unused
Found 1 error. Found 1 error.
[*] 1 potentially fixable with the --fix option. [*] 1 fixable with the --fix option.
----- stderr ----- ----- stderr -----
"#); "###);
} }
#[test] #[test]
@ -69,7 +69,7 @@ fn stdin_filename() {
----- stdout ----- ----- stdout -----
F401.py:1:8: F401 [*] `os` imported but unused F401.py:1:8: F401 [*] `os` imported but unused
Found 1 error. Found 1 error.
[*] 1 potentially fixable with the --fix option. [*] 1 fixable with the --fix option.
----- stderr ----- ----- stderr -----
"###); "###);
@ -87,7 +87,7 @@ fn stdin_source_type_py() {
----- stdout ----- ----- stdout -----
TCH.py:1:8: F401 [*] `os` imported but unused TCH.py:1:8: F401 [*] `os` imported but unused
Found 1 error. Found 1 error.
[*] 1 potentially fixable with the --fix option. [*] 1 fixable with the --fix option.
----- stderr ----- ----- stderr -----
"###); "###);
@ -789,7 +789,7 @@ fn check_input_from_argfile() -> Result<()> {
----- stdout ----- ----- stdout -----
/path/to/a.py:1:8: F401 [*] `os` imported but unused /path/to/a.py:1:8: F401 [*] `os` imported but unused
Found 1 error. Found 1 error.
[*] 1 potentially fixable with the --fix option. [*] 1 fixable with the --fix option.
----- stderr ----- ----- stderr -----
"###); "###);
@ -816,11 +816,11 @@ fn displays_fix_applicability_levels() {
success: false success: false
exit_code: 1 exit_code: 1
----- stdout ----- ----- stdout -----
-:1:14: F601 [*] Dictionary key literal `'a'` repeated -:1:14: F601 [**] Dictionary key literal `'a'` repeated
-:2:7: UP034 [*] Avoid extraneous parentheses -:2:7: UP034 [*] Avoid extraneous parentheses
Found 2 errors. Found 2 errors.
[*] 1 potentially fixable with the --fix option. [*] 1 fixable with the --fix option.
[*] 2 potentially fixable with the --fix-suggested option. [**] 1 potentially fixable with the --fix-suggested option.
----- stderr ----- ----- stderr -----
"###); "###);
@ -835,9 +835,9 @@ fn displays_remaining_suggested_fixes() {
success: false success: false
exit_code: 1 exit_code: 1
----- stdout ----- ----- stdout -----
-:1:14: F601 [*] Dictionary key literal `'a'` repeated -:1:14: F601 [**] Dictionary key literal `'a'` repeated
Found 1 error. Found 1 error.
[*] 1 potentially fixable with the --fix-suggested option. [**] 1 potentially fixable with the --fix-suggested option.
----- stderr ----- ----- stderr -----
"###); "###);
@ -889,11 +889,11 @@ fn fix_applies_automatic_and_suggested_fixes_with_fix_suggested() {
success: false success: false
exit_code: 1 exit_code: 1
----- stdout ----- ----- stdout -----
-:1:14: F601 [*] Dictionary key literal `'a'` repeated -:1:14: F601 [**] Dictionary key literal `'a'` repeated
-:2:7: UP034 [*] Avoid extraneous parentheses -:2:7: UP034 [*] Avoid extraneous parentheses
Found 2 errors. Found 2 errors.
[*] 1 potentially fixable with the --fix option. [*] 1 fixable with the --fix option.
[*] 2 potentially fixable with the --fix-suggested option. [**] 1 potentially fixable with the --fix-suggested option.
----- stderr ----- ----- stderr -----
"###); "###);

View File

@ -31,6 +31,16 @@ pub enum Applicability {
Automatic, Automatic,
} }
impl Applicability {
pub fn symbol(&self) -> &'static str {
match self {
Self::Automatic => "*",
Self::Suggested => "**",
_ => "*",
}
}
}
/// Indicates the level of isolation required to apply a fix. /// Indicates the level of isolation required to apply a fix.
#[derive(Default, Copy, Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] #[derive(Default, Copy, Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]

View File

@ -141,11 +141,13 @@ impl Display for RuleCodeAndBody<'_> {
let kind = &self.message.kind; let kind = &self.message.kind;
if self.show_fix_status && self.message.fix.is_some() { if self.show_fix_status && self.message.fix.is_some() {
let indicator = self.message.fix.as_ref().unwrap().applicability().symbol();
write!( write!(
f, f,
"{code} {fix}{body}", "{code} {fix}{body}",
code = kind.rule().noqa_code().to_string().red().bold(), code = kind.rule().noqa_code().to_string().red().bold(),
fix = format_args!("[{}] ", "*".cyan()), fix = format_args!("[{}] ", indicator.cyan()),
body = kind.body, body = kind.body,
) )
} else { } else {