Skip to content

Commit

Permalink
Merge pull request #944 from RoelAdriaans/file-content-sorted-blank-l…
Browse files Browse the repository at this point in the history
…ine-fix

Fix blank lines in file-contents-sorter plugin
  • Loading branch information
asottile authored Aug 5, 2023
2 parents 1790c6b + fdbaeb8 commit 61b5f7e
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 61b5f7e

Please sign in to comment.