Skip to content

Commit 86691ed

Browse files
committed
Fix requirements-txt-fixer for comments at end of file
1 parent 5d08e77 commit 86691ed

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

pre_commit_hooks/requirements_txt_fixer.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,16 @@ def fix_requirements(f):
6363
else:
6464
requirement.value = line
6565

66+
# if a file ends in a comment, preserve it at the end
67+
if requirements[-1].value is None:
68+
rest = requirements.pop().comments
69+
else:
70+
rest = []
71+
6672
for requirement in sorted(requirements):
6773
after.extend(requirement.comments)
6874
after.append(requirement.value)
75+
after.extend(rest)
6976

7077
after_string = b''.join(after)
7178

tests/requirements_txt_fixer_test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
(
1212
(b'', PASS, b''),
1313
(b'\n', PASS, b'\n'),
14+
(b'# intentionally empty\n', PASS, b'# intentionally empty\n'),
15+
(b'foo\n# comment at end\n', PASS, b'foo\n# comment at end\n'),
1416
(b'foo\nbar\n', FAIL, b'bar\nfoo\n'),
1517
(b'bar\nfoo\n', PASS, b'bar\nfoo\n'),
1618
(b'#comment1\nfoo\n#comment2\nbar\n', FAIL, b'#comment2\nbar\n#comment1\nfoo\n'),

0 commit comments

Comments
 (0)