Skip to content

Commit

Permalink
Fix blank lines
Browse files Browse the repository at this point in the history
As stated in the documentation: `Note that this hook WILL remove blank lines`
Previously this hook would always add blank a blank line.

With this fix, if the file is empty, a newline will not be added, and multpline blanklines in a file will be removed.

Fixes pre-commit#935
  • Loading branch information
RoelAdriaans committed Aug 5, 2023
1 parent 1790c6b commit fdbaeb8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
5 changes: 4 additions & 1 deletion pre_commit_hooks/file_contents_sorter.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ def sort_file_contents(
after = sorted(lines, key=key)

before_string = b''.join(before)
after_string = b'\n'.join(after) + b'\n'
after_string = b'\n'.join(after)

if after_string:
after_string += b'\n'

if before_string == after_string:
return PASS
Expand Down
4 changes: 3 additions & 1 deletion tests/file_contents_sorter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
@pytest.mark.parametrize(
('input_s', 'argv', 'expected_retval', 'output'),
(
(b'', [], FAIL, b'\n'),
(b'', [], PASS, b''),
(b'\n', [], FAIL, b''),
(b'\n\n', [], FAIL, b''),
(b'lonesome\n', [], PASS, b'lonesome\n'),
(b'missing_newline', [], FAIL, b'missing_newline\n'),
(b'newline\nmissing', [], FAIL, b'missing\nnewline\n'),
Expand Down

0 comments on commit fdbaeb8

Please sign in to comment.