Skip to content

Commit

Permalink
YapfBear: Ignore file if it is zero-byte file
Browse files Browse the repository at this point in the history
Yapf cannot handle zero-byte files correctly, because it adds a newline
into the file when correcting. Hence we ignore zero byte files
completely as they anyway do not have code to format.

Fixes #739
  • Loading branch information
AbdealiLoKo committed Aug 31, 2016
1 parent cd322dd commit d4a9654
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
5 changes: 5 additions & 0 deletions bears/python/YapfBear.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ def run(self, filename, file,
:param based_on_style:
The formatting style to be used as reference.
"""
if not file:
# Yapf cannot handle zero-byte files well, and adds a redundent
# newline into the file. To avoid this, we don't parse zero-byte
# files as they cannot have anything to format either.
return

options = """
[style]
Expand Down
7 changes: 7 additions & 0 deletions tests/python/YapfBearTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ def test_valid(self):
["x = { 'a':37,'b':42,\n", "'c':927}\n", '\n',
"y = 'hello ''world'\n"], valid=False)

def test_eof_handling(self):
self.check_validity(self.uut, [], valid=True)
self.check_validity(self.uut, [''], valid=True)
self.check_validity(self.uut, ['a = 2\n'], valid=True)
self.check_validity(self.uut, ['a = 2'], valid=True)
self.check_validity(self.uut, ['\n'], valid=True)

def test_valid_python_2(self):
self.check_validity(self.uut, ['print 1\n'], valid=True)

Expand Down

0 comments on commit d4a9654

Please sign in to comment.