Skip to content

Commit 8884b63

Browse files
authored
Merge pull request #415 from barakreif/fix-requirements-fixer-curruption
add new line after reading requirements file
2 parents 277f875 + e4cfaa6 commit 8884b63

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

pre_commit_hooks/requirements_txt_fixer.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,16 @@ def __lt__(self, requirement): # type: (Requirement) -> int
4040

4141
def fix_requirements(f): # type: (IO[bytes]) -> int
4242
requirements = [] # type: List[Requirement]
43-
before = tuple(f)
43+
before = list(f)
4444
after = [] # type: List[bytes]
4545

4646
before_string = b''.join(before)
4747

48+
# adds new line in case one is missing
49+
# AND a change to the requirements file is needed regardless:
50+
if before and not before[-1].endswith(b'\n'):
51+
before[-1] += b'\n'
52+
4853
# If the file is empty (i.e. only whitespace/newlines) exit early
4954
if before_string.strip() == b'':
5055
return PASS

tests/requirements_txt_fixer_test.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
(b'foo\n# comment at end\n', PASS, b'foo\n# comment at end\n'),
1616
(b'foo\nbar\n', FAIL, b'bar\nfoo\n'),
1717
(b'bar\nfoo\n', PASS, b'bar\nfoo\n'),
18+
(b'a\nc\nb\n', FAIL, b'a\nb\nc\n'),
19+
(b'a\nc\nb', FAIL, b'a\nb\nc\n'),
20+
(b'a\nb\nc', FAIL, b'a\nb\nc\n'),
1821
(
1922
b'#comment1\nfoo\n#comment2\nbar\n',
2023
FAIL,

0 commit comments

Comments
 (0)