提交 da2ea3f7 编辑于 作者: Barak Y. Reif's avatar Barak Y. Reif
浏览文件

add new line post reading requirements file, change before from tuple to list, add test cases

显示 10 个添加2 个删除
+10 -2
......@@ -40,9 +40,14 @@ class Requirement(object):
def fix_requirements(f): # type: (IO[bytes]) -> int
requirements = [] # type: List[Requirement]
before = tuple(f)
before = list(f) # type: List[bytes]
after = [] # type: List[bytes]
# adds new line in case one is missing
# AND a change to the requirements file is needed regardless:
if before and not before[-1].endswith(b'\n'):
before[-1] += b'\n'
before_string = b''.join(before)
# If the file is empty (i.e. only whitespace/newlines) exit early
......@@ -94,7 +99,7 @@ def fix_requirements(f): # type: (IO[bytes]) -> int
after_string = b''.join(after)
if before_string == after_string:
if before_string.rstrip() == after_string.rstrip():
return PASS
else:
f.seek(0)
......
......@@ -15,6 +15,9 @@ from pre_commit_hooks.requirements_txt_fixer import Requirement
(b'foo\n# comment at end\n', PASS, b'foo\n# comment at end\n'),
(b'foo\nbar\n', FAIL, b'bar\nfoo\n'),
(b'bar\nfoo\n', PASS, b'bar\nfoo\n'),
(b'a\nc\nb\n', FAIL, b'a\nb\nc\n'),
(b'a\nc\nb', FAIL, b'a\nb\nc\n'),
(b'a\nb\nc', PASS, b'a\nb\nc'),
(
b'#comment1\nfoo\n#comment2\nbar\n',
FAIL,
......
支持 Markdown
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册