check-whitespace: fix idempotency issue

See the comment, if there was couple whitespaces at the end of the file without newline, it would take running `check-whitespace` twice to finally fix it.
This commit is contained in:
Andrej730
2026-09-14 12:05:15 +05:00
parent f3013f29df
commit 32b408c3d5
2 changed files with 21 additions and 9 deletions
+14 -1
View File
@@ -276,6 +276,19 @@ class TestChecker:
) -> None: ) -> None:
self._assert_check(Checker._check_trailing_whitespaces, content, expected_issues, fixed, check, line_ending) self._assert_check(Checker._check_trailing_whitespaces, content, expected_issues, fixed, check, line_ending)
def test_trailing_whitespace_runs_before_eof_newline(self, tmp_path: Path) -> None:
# 'foo\n\s\s` should result in `foo\n`.
# If we run eof check first before trailing whitespace check,
# it will result in `foo\n\n` instead.
filepath = tmp_path / "test.txt"
filepath.write_bytes(b"foo\n \n\n")
checker = Checker(LF)
checker.check_trailing_whitespaces(filepath, False)
checker.check_eof_newline(filepath, False)
assert filepath.read_bytes() == b"foo\n"
@staticmethod @staticmethod
def run_tests(extra_args: list[str] | None = None) -> None: def run_tests(extra_args: list[str] | None = None) -> None:
pytest.main([__file__, *(extra_args or [])]) pytest.main([__file__, *(extra_args or [])])
@@ -382,8 +395,8 @@ def main() -> int:
checker.set_newline_for_path(filepath) checker.set_newline_for_path(filepath)
checker.check_stray_cr(filepath, args.check) checker.check_stray_cr(filepath, args.check)
checker.check_line_endings_mismatch(filepath, args.check) checker.check_line_endings_mismatch(filepath, args.check)
checker.check_eof_newline(filepath, args.check)
checker.check_trailing_whitespaces(filepath, args.check) checker.check_trailing_whitespaces(filepath, args.check)
checker.check_eof_newline(filepath, args.check)
print(f"{len(filepaths)} file(s) checked.") print(f"{len(filepaths)} file(s) checked.")
if not checker.issues: if not checker.issues:
color = C.GREEN color = C.GREEN
@@ -5,8 +5,8 @@
box-shadow: 0 0 0 3px #28a745; box-shadow: 0 0 0 3px #28a745;
color: #ddd; color: #ddd;
} }
.classificationView { .classificationView {
border-left: 5px solid #28a745; border-left: 5px solid #28a745;
min-height: 200px; min-height: 200px;
@@ -15,20 +15,20 @@
padding: 5px; padding: 5px;
max-height: 400px; max-height: 400px;
} }
.selectedClassificationText { .selectedClassificationText {
color: #28a745; color: #28a745;
} }
.back-button { .back-button {
margin-bottom: 20px; margin-bottom: 20px;
} }
.back-button:disabled { .back-button:disabled {
cursor: not-allowed; cursor: not-allowed;
background-color: #ccc; background-color: #ccc;
} }
.classification-cell { .classification-cell {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
@@ -36,8 +36,7 @@
align-items: center; align-items: center;
height: 100%; height: 100%;
} }
.classification { .classification {
height: 100%; height: 100%;
} }