Skip to content

Commit

Permalink
Update format.rs to display correct message for already formatted fil…
Browse files Browse the repository at this point in the history
…es (#9153)

## Summary

<!-- What's the purpose of the change? What does it do, and why? -->

New messages for "format" mode. 
Fixes #9132 

## Test Plan

<!-- How was it tested? -->

I ran the tests specified in `CONTRIBUTING.md`
```bash
cargo run -p ruff_cli -- check /path/to/some_files.py --no-cache
cargo run -p ruff_cli -- format --check /path/to/some_files.py --no-cache

cargo clippy --workspace --all-targets --all-features -- -D warnings
RUFF_UPDATE_SCHEMA=1 cargo test
pre-commit run --all-files --show-diff-on-failure
```

**Note:** In case no files are detected, either correctly formatted,
changed, or unchanged, it does not display a message. Wouldn't it be
better to show some message in this case?
  • Loading branch information
VictorGob authored Dec 18, 2023
1 parent 2643f74 commit 6feea86
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 4 deletions.
12 changes: 10 additions & 2 deletions crates/ruff_cli/src/commands/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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!(
Expand All @@ -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(())
Expand Down
58 changes: 56 additions & 2 deletions crates/ruff_cli/tests/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ fn mixed_line_endings() -> Result<()> {
----- stdout -----
----- stderr -----
2 files left unchanged
2 files already formatted
"###);
Ok(())
}
Expand Down Expand Up @@ -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()?;
Expand Down Expand Up @@ -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
"###);
});
}
Expand Down

0 comments on commit 6feea86

Please sign in to comment.