mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-03-29 10:16:52 +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,19 +18,18 @@ FAIL = 1
|
|||
|
||||
|
||||
def sort_file_contents(f):
|
||||
before = tuple(f)
|
||||
before = [line.strip(b'\n\r') for line in f if line.strip()]
|
||||
after = sorted(before)
|
||||
|
||||
before_string = b''.join(before)
|
||||
after_string = b''.join(after)
|
||||
|
||||
if before_string == after_string:
|
||||
if before == after:
|
||||
return PASS
|
||||
else:
|
||||
f.seek(0)
|
||||
f.write(after_string)
|
||||
f.truncate()
|
||||
return FAIL
|
||||
|
||||
after_string = b'\n'.join(after) + b'\n'
|
||||
|
||||
f.seek(0)
|
||||
f.write(after_string)
|
||||
f.truncate()
|
||||
return FAIL
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
|
|
|
|||
|
|
@ -11,12 +11,16 @@ from pre_commit_hooks.file_contents_sorter import PASS
|
|||
(b'', PASS, b''),
|
||||
(b'lonesome\n', PASS, b'lonesome\n'),
|
||||
(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'beta\nalpha\n', FAIL, b'alpha\nbeta\n'),
|
||||
(b'C\nc\n', PASS, 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'@\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):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue