From d4a9654b1ebe87ed96afa266ebb828b85a25f1a8 Mon Sep 17 00:00:00 2001 From: AbdealiJK Date: Thu, 1 Sep 2016 01:21:13 +0530 Subject: [PATCH] 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 https://github.com/coala-analyzer/coala-bears/issues/739 --- bears/python/YapfBear.py | 5 +++++ tests/python/YapfBearTest.py | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/bears/python/YapfBear.py b/bears/python/YapfBear.py index dae85050e6..bdde5b0c44 100644 --- a/bears/python/YapfBear.py +++ b/bears/python/YapfBear.py @@ -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] diff --git a/tests/python/YapfBearTest.py b/tests/python/YapfBearTest.py index 7cace2abc2..0b967e1f7c 100644 --- a/tests/python/YapfBearTest.py +++ b/tests/python/YapfBearTest.py @@ -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)