diff --git a/crates/ruff_cli/src/commands/format.rs b/crates/ruff_cli/src/commands/format.rs index 697c412163cf8..2b9144ad5b979 100644 --- a/crates/ruff_cli/src/commands/format.rs +++ b/crates/ruff_cli/src/commands/format.rs @@ -515,7 +515,7 @@ impl<'a> FormatResults<'a> { if changed > 0 && unchanged > 0 { writeln!( f, - "{} file{} {}, {} file{} left unchanged", + "{} file{} {}, {} file{} {}", changed, if changed == 1 { "" } else { "s" }, match self.mode { @@ -524,6 +524,10 @@ impl<'a> FormatResults<'a> { }, unchanged, if unchanged == 1 { "" } else { "s" }, + match self.mode { + FormatMode::Write => "left unchanged", + FormatMode::Check | FormatMode::Diff => "already formatted", + }, ) } else if changed > 0 { writeln!( @@ -539,9 +543,13 @@ impl<'a> FormatResults<'a> { } else if unchanged > 0 { writeln!( f, - "{} file{} left unchanged", + "{} file{} {}", unchanged, if unchanged == 1 { "" } else { "s" }, + match self.mode { + FormatMode::Write => "left unchanged", + FormatMode::Check | FormatMode::Diff => "already formatted", + }, ) } else { Ok(()) diff --git a/crates/ruff_cli/tests/format.rs b/crates/ruff_cli/tests/format.rs index cd3e14629899e..09d05d12b0c76 100644 --- a/crates/ruff_cli/tests/format.rs +++ b/crates/ruff_cli/tests/format.rs @@ -255,7 +255,7 @@ fn mixed_line_endings() -> Result<()> { ----- stdout ----- ----- stderr ----- - 2 files left unchanged + 2 files already formatted "###); Ok(()) } @@ -328,6 +328,60 @@ OTHER = "OTHER" Ok(()) } +#[test] +fn messages() -> Result<()> { + let tempdir = TempDir::new()?; + + fs::write( + tempdir.path().join("main.py"), + r#" +from test import say_hy + +if __name__ == "__main__": + say_hy("dear Ruff contributor") +"#, + )?; + + assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME)) + .current_dir(tempdir.path()) + .args(["format", "--no-cache", "--isolated", "--check"]) + .arg("main.py"), @r###" + success: false + exit_code: 1 + ----- stdout ----- + Would reformat: main.py + 1 file would be reformatted + + ----- stderr ----- + "###); + + assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME)) + .current_dir(tempdir.path()) + .args(["format", "--no-cache", "--isolated"]) + .arg("main.py"), @r###" + success: true + exit_code: 0 + ----- stdout ----- + 1 file reformatted + + ----- stderr ----- + "###); + + assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME)) + .current_dir(tempdir.path()) + .args(["format", "--no-cache", "--isolated"]) + .arg("main.py"), @r###" + success: true + exit_code: 0 + ----- stdout ----- + 1 file left unchanged + + ----- stderr ----- + "###); + + Ok(()) +} + #[test] fn force_exclude() -> Result<()> { let tempdir = TempDir::new()?; @@ -876,7 +930,7 @@ fn test_diff() { ----- stderr ----- - 2 files would be reformatted, 1 file left unchanged + 2 files would be reformatted, 1 file already formatted "###); }); }