Fix blank lines
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 #935
diff --git a/pre_commit_hooks/file_contents_sorter.py b/pre_commit_hooks/file_contents_sorter.py
index c5691f0..02bdbcc 100644
--- a/pre_commit_hooks/file_contents_sorter.py
+++ b/pre_commit_hooks/file_contents_sorter.py
@@ -37,7 +37,10 @@
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
diff --git a/tests/file_contents_sorter_test.py b/tests/file_contents_sorter_test.py
index 5e79e40..49b3b79 100644
--- a/tests/file_contents_sorter_test.py
+++ b/tests/file_contents_sorter_test.py
@@ -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'),