mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-07 20:26:54 +00:00
Fix bug with the file-contents-sorter hook when processing file that does not end in a newline
This commit is contained in:
parent
50871f83cc
commit
7cfec24f77
2 changed files with 13 additions and 10 deletions
|
|
@ -18,15 +18,14 @@ FAIL = 1
|
||||||
|
|
||||||
|
|
||||||
def sort_file_contents(f):
|
def sort_file_contents(f):
|
||||||
before = tuple(f)
|
before = [line.strip(b'\n\r') for line in f if line.strip()]
|
||||||
after = sorted(before)
|
after = sorted(before)
|
||||||
|
|
||||||
before_string = b''.join(before)
|
if before == after:
|
||||||
after_string = b''.join(after)
|
|
||||||
|
|
||||||
if before_string == after_string:
|
|
||||||
return PASS
|
return PASS
|
||||||
else:
|
|
||||||
|
after_string = b'\n'.join(after) + b'\n'
|
||||||
|
|
||||||
f.seek(0)
|
f.seek(0)
|
||||||
f.write(after_string)
|
f.write(after_string)
|
||||||
f.truncate()
|
f.truncate()
|
||||||
|
|
|
||||||
|
|
@ -11,12 +11,16 @@ from pre_commit_hooks.file_contents_sorter import PASS
|
||||||
(b'', PASS, b''),
|
(b'', PASS, b''),
|
||||||
(b'lonesome\n', PASS, b'lonesome\n'),
|
(b'lonesome\n', PASS, b'lonesome\n'),
|
||||||
(b'missing_newline', PASS, b'missing_newline'),
|
(b'missing_newline', PASS, b'missing_newline'),
|
||||||
|
(b'newline\nmissing', FAIL, b'missing\nnewline\n'),
|
||||||
|
(b'missing\nnewline', PASS, b'missing\nnewline'),
|
||||||
(b'alpha\nbeta\n', PASS, b'alpha\nbeta\n'),
|
(b'alpha\nbeta\n', PASS, b'alpha\nbeta\n'),
|
||||||
(b'beta\nalpha\n', FAIL, b'alpha\nbeta\n'),
|
(b'beta\nalpha\n', FAIL, b'alpha\nbeta\n'),
|
||||||
(b'C\nc\n', PASS, b'C\nc\n'),
|
(b'C\nc\n', PASS, b'C\nc\n'),
|
||||||
(b'c\nC\n', FAIL, b'C\nc\n'),
|
(b'c\nC\n', FAIL, b'C\nc\n'),
|
||||||
(b'mag ical \n tre vor\n', FAIL, b' tre vor\nmag ical \n'),
|
(b'mag ical \n tre vor\n', FAIL, b' tre vor\nmag ical \n'),
|
||||||
(b'@\n-\n_\n#\n', FAIL, b'#\n-\n@\n_\n'),
|
(b'@\n-\n_\n#\n', FAIL, b'#\n-\n@\n_\n'),
|
||||||
|
(b'extra\n\n\nwhitespace\n', PASS, b'extra\n\n\nwhitespace\n'),
|
||||||
|
(b'whitespace\n\n\nextra\n', FAIL, b'extra\nwhitespace\n'),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
def test_integration(input_s, expected_retval, output, tmpdir):
|
def test_integration(input_s, expected_retval, output, tmpdir):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue