Skip to content

Commit 52aba6b

Browse files
AbdealiLoKosils
authored andcommitted
YapfBear: Ignore file if it is zero-byte file
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
1 parent 047b683 commit 52aba6b

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

bears/python/YapfBear.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ def run(self, filename, file,
9292
:param based_on_style:
9393
The formatting style to be used as reference.
9494
"""
95+
if not file:
96+
# Yapf cannot handle zero-byte files well, and adds a redundent
97+
# newline into the file. To avoid this, we don't parse zero-byte
98+
# files as they cannot have anything to format either.
99+
return
95100

96101
options = """
97102
[style]

tests/python/YapfBearTest.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ def test_valid(self):
2424
["x = { 'a':37,'b':42,\n", "'c':927}\n", '\n',
2525
"y = 'hello ''world'\n"], valid=False)
2626

27+
def test_eof_handling(self):
28+
self.check_validity(self.uut, [], valid=True)
29+
self.check_validity(self.uut, [''], valid=True)
30+
self.check_validity(self.uut, ['a = 2\n'], valid=True)
31+
self.check_validity(self.uut, ['a = 2'], valid=True)
32+
self.check_validity(self.uut, ['\n'], valid=True)
33+
2734
def test_valid_python_2(self):
2835
self.check_validity(self.uut, ['print 1\n'], valid=True)
2936

0 commit comments

Comments
 (0)