mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-07 12:26:52 +00:00
Merge pull request #205 from dlgallagher/fix_requirements_txt_fixer_on_empty_requirements_files
Fix NoneTypeError when requirements file is empty
This commit is contained in:
commit
d419bef35c
2 changed files with 13 additions and 8 deletions
|
|
@ -30,21 +30,25 @@ class Requirement(object):
|
||||||
|
|
||||||
def fix_requirements(f):
|
def fix_requirements(f):
|
||||||
requirements = []
|
requirements = []
|
||||||
before = []
|
before = list(f)
|
||||||
after = []
|
after = []
|
||||||
|
|
||||||
for line in f:
|
before_string = b''.join(before)
|
||||||
before.append(line)
|
|
||||||
|
|
||||||
# If the most recent requirement object has a value, then it's time to
|
# If the file is empty (i.e. only whitespace/newlines) exit early
|
||||||
# start building the next requirement object.
|
if before_string.strip() == b'':
|
||||||
|
return 0
|
||||||
|
|
||||||
|
for line in before:
|
||||||
|
# If the most recent requirement object has a value, then it's
|
||||||
|
# time to start building the next requirement object.
|
||||||
if not len(requirements) or requirements[-1].value is not None:
|
if not len(requirements) or requirements[-1].value is not None:
|
||||||
requirements.append(Requirement())
|
requirements.append(Requirement())
|
||||||
|
|
||||||
requirement = requirements[-1]
|
requirement = requirements[-1]
|
||||||
|
|
||||||
# If we see a newline before any requirements, then this is a top of
|
# If we see a newline before any requirements, then this is a
|
||||||
# file comment.
|
# top of file comment.
|
||||||
if len(requirements) == 1 and line.strip() == b'':
|
if len(requirements) == 1 and line.strip() == b'':
|
||||||
if len(requirement.comments) and requirement.comments[0].startswith(b'#'):
|
if len(requirement.comments) and requirement.comments[0].startswith(b'#'):
|
||||||
requirement.value = b'\n'
|
requirement.value = b'\n'
|
||||||
|
|
@ -60,7 +64,6 @@ def fix_requirements(f):
|
||||||
after.append(comment)
|
after.append(comment)
|
||||||
after.append(requirement.value)
|
after.append(requirement.value)
|
||||||
|
|
||||||
before_string = b''.join(before)
|
|
||||||
after_string = b''.join(after)
|
after_string = b''.join(after)
|
||||||
|
|
||||||
if before_string == after_string:
|
if before_string == after_string:
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,8 @@ from pre_commit_hooks.requirements_txt_fixer import Requirement
|
||||||
|
|
||||||
# Input, expected return value, expected output
|
# Input, expected return value, expected output
|
||||||
TESTS = (
|
TESTS = (
|
||||||
|
(b'', 0, b''),
|
||||||
|
(b'\n', 0, b'\n'),
|
||||||
(b'foo\nbar\n', 1, b'bar\nfoo\n'),
|
(b'foo\nbar\n', 1, b'bar\nfoo\n'),
|
||||||
(b'bar\nfoo\n', 0, b'bar\nfoo\n'),
|
(b'bar\nfoo\n', 0, b'bar\nfoo\n'),
|
||||||
(b'#comment1\nfoo\n#comment2\nbar\n', 1, b'#comment2\nbar\n#comment1\nfoo\n'),
|
(b'#comment1\nfoo\n#comment2\nbar\n', 1, b'#comment2\nbar\n#comment1\nfoo\n'),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue