Print filenames that doesn't comply PEP8

The old output was missing filenames, e.g.

    AssertionError: Found PEP8 errors that may break your code in Python 3:
    2       E201 whitespace after '['
    3       E202 whitespace before ']'
    2       E203 whitespace before ':'
    1       E221 multiple spaces before operator
    1       E302 expected 2 blank lines, found 1
    2       E303 too many blank lines (2)

That's now usefull at all. Where the heck should I search for these errors. The
new output is a little bit messy but contains more usefull information, e.g.

    AssertionError: Found PEP8 errors that may break your code in Python 3:
    /tito/test/unit/../../src/tito/release/LocalYumRepo.py:9:1: E302 expected 2 blank lines, found 1
    /tito/test/unit/../../src/tito/release/LocalYumRepo.py:17:66: E202 whitespace before ']'
    /tito/test/unit/../../src/tito/release/LocalYumRepo.py:21:15: E203 whitespace before ':'
    /tito/test/unit/../../src/tito/release/LocalYumRepo.py:22:16: E203 whitespace before ':'
    /tito/test/unit/../../src/tito/release/LocalYumRepo.py:26:18: E201 whitespace after '['
    /tito/test/unit/../../src/tito/release/LocalYumRepo.py:26:32: E202 whitespace before ']'
    /tito/test/unit/../../src/tito/release/LocalYumRepo.py:29:15: E201 whitespace after '['
    /tito/test/unit/../../src/tito/release/LocalYumRepo.py:29:32: E202 whitespace before ']'
    /tito/test/unit/../../src/tito/release/LocalYumRepo.py:35:17: E221 multiple spaces before operator
    /tito/test/unit/../../src/tito/release/LocalYumRepo.py:76:5: E303 too many blank lines (2)
This commit is contained in:
Jakub Kadlcik 2021-01-11 21:17:49 +01:00 committed by Jakub Kadlčík
parent 998f7fb067
commit d41626d181
2 changed files with 23 additions and 3 deletions

View file

@ -18,6 +18,7 @@ Compatibility library for Python 2.4 up through Python 3.
"""
import os
import sys
import contextlib
ENCODING = sys.getdefaultencoding()
PY2 = sys.version_info[0] == 2
if PY2:
@ -35,6 +36,7 @@ else:
from configparser import RawConfigParser
from io import StringIO
from urllib.parse import urlparse
from contextlib import redirect_stdout
import xmlrpc.client as xmlrpclib
text_type = str
binary_type = bytes
@ -98,3 +100,14 @@ def write(fd, str):
os.write(fd, str)
else:
os.write(fd, bytes(str, ENCODING))
if PY2:
@contextlib.contextmanager
def redirect_stdout(target):
original = sys.stdout
try:
sys.stdout = target
yield
finally:
sys.stdout = original

View file

@ -31,6 +31,7 @@ except ImportError:
from tito.compat import * # NOQA
from tito.compat import StringIO, redirect_stdout
from unit.fixture import TitoUnitTestFixture, REPO_DIR
@ -66,9 +67,15 @@ class TestPep8(TitoUnitTestFixture):
try:
checker = pep8.StyleGuide(select=tests, paths=[REPO_DIR], reporter=pep8.StandardReport)
report = checker.check_files()
result = report.total_errors
output = "\n".join(report.get_statistics())
# Unfortunatelly, IMHO the most interesting information
# `checker.check_files()` prints to the STDOUT and doesn't provide
# API to get it - the list of _bad_ files. Let's just read it from
# the output
with StringIO() as buf, redirect_stdout(buf):
report = checker.check_files()
result = report.total_errors
output = buf.getvalue()
except AttributeError:
# We don't have pep8.StyleGuide, so we must be
# using pep8 older than git tag 1.1-72-gf20d656.