Repository: pre-commit/pre-commit-hooks

Issue:
requirements-txt-fixer broken for empty requirements.txt
A bit of an edge case, shouldn't crash though:


```python
Sorting requirements.txt
Traceback (most recent call last):
  File "/nail/tmp/tmp25dodv0q/venv/bin/requirements-txt-fixer", line 11, in <module>
    sys.exit(fix_requirements_txt())
  File "/nail/tmp/tmp25dodv0q/venv/lib/python3.5/site-packages/pre_commit_hooks/requirements_txt_fixer.py", line 84, in fix_requirements_txt
    ret_for_file = fix_requirements(file_obj)
  File "/nail/tmp/tmp25dodv0q/venv/lib/python3.5/site-packages/pre_commit_hooks/requirements_txt_fixer.py", line 64, in fix_requirements
    after_string = b''.join(after)
TypeError: sequence item 1: expected a bytes-like object, NoneType found
```

Discussion:

Patch:
diff --git a/pre_commit_hooks/requirements_txt_fixer.py b/pre_commit_hooks/requirements_txt_fixer.py
index efa1906..41e1ffc 100644
--- a/pre_commit_hooks/requirements_txt_fixer.py
+++ b/pre_commit_hooks/requirements_txt_fixer.py
@@ -30,21 +30,25 @@ class Requirement(object):
 
 def fix_requirements(f):
     requirements = []
-    before = []
+    before = list(f)
     after = []
 
-    for line in f:
-        before.append(line)
+    before_string = b''.join(before)
+
+    # If the file is empty (i.e. only whitespace/newlines) exit early
+    if before_string.strip() == b'':
+        return 0
 
-        # If the most recent requirement object has a value, then it's time to
-        # start building the next requirement object.
+    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:
             requirements.append(Requirement())
 
         requirement = requirements[-1]
 
-        # If we see a newline before any requirements, then this is a top of
-        # file comment.
+        # If we see a newline before any requirements, then this is a
+        # top of file comment.
         if len(requirements) == 1 and line.strip() == b'':
             if len(requirement.comments) and requirement.comments[0].startswith(b'#'):
                 requirement.value = b'\n'
@@ -60,7 +64,6 @@ def fix_requirements(f):
             after.append(comment)
         after.append(requirement.value)
 
-    before_string = b''.join(before)
     after_string = b''.join(after)
 
     if before_string == after_string:


Test patch:
diff --git a/tests/requirements_txt_fixer_test.py b/tests/requirements_txt_fixer_test.py
index 1c590a5..33f6a47 100644
--- a/tests/requirements_txt_fixer_test.py
+++ b/tests/requirements_txt_fixer_test.py
@@ -5,6 +5,8 @@ from pre_commit_hooks.requirements_txt_fixer import Requirement
 
 # Input, expected return value, expected output
 TESTS = (
+    (b'', 0, b''),
+    (b'\n', 0, b'\n'),
     (b'foo\nbar\n', 1, 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'),