Skip to content

Commit d4a9654

Browse files
committed
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 cd322dd commit d4a9654

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

bears/python/YapfBear.py

+5
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ def run(self, filename, file,
113113
:param based_on_style:
114114
The formatting style to be used as reference.
115115
"""
116+
if not file:
117+
# Yapf cannot handle zero-byte files well, and adds a redundent
118+
# newline into the file. To avoid this, we don't parse zero-byte
119+
# files as they cannot have anything to format either.
120+
return
116121

117122
options = """
118123
[style]

tests/python/YapfBearTest.py

+7
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)