Use public interfaces of pep8 for hacking

Rather than using mock and pep8 internals, we can use
public interfaces of pep8 to register our custom
hacking checks. Further, if pep8 isn't present for testing purposes,
we can simply use pycodestyle. Many distributions are shipping the
latter these days instead of pep8.

Closes-Bug: 1652458

Co-Authored-By: Ian Cordasco <graffatcolmingov@gmail.com>
Change-Id: Ica719703c54a295d10ea800467b25a5d0f65348a
Signed-off-by: Adam Williamson <awilliam@redhat.com>
This commit is contained in:
Steve Martinelli 2017-01-04 23:57:52 -05:00
parent 937a1a3037
commit b63cc5f71d
1 changed files with 11 additions and 7 deletions

View File

@ -12,8 +12,13 @@
import textwrap
import mock
import pep8
try:
import pep8
except ImportError:
# NTOE(stevemar): pycodestyle is not yet in global-requirements, but
# packagers have begun pycodestyle (the renamed version of pep8)
# See: https://github.com/PyCQA/pycodestyle/issues/466
import pycodestyle as pep8
from keystone.tests.hacking import checks
from keystone.tests import unit
@ -35,16 +40,15 @@ class BaseStyleCheck(unit.BaseTestCase):
def get_fixture(self):
return hacking_fixtures.HackingCode()
# We are patching pep8 so that only the check under test is actually
# installed.
@mock.patch('pep8._checks',
{'physical_line': {}, 'logical_line': {}, 'tree': {}})
def run_check(self, code):
pep8.register_check(self.get_checker())
lines = textwrap.dedent(code).strip().splitlines(True)
checker = pep8.Checker(lines=lines)
# Load all keystone hacking checks, they are of the form Kddd,
# where ddd can from range from 000-999
guide = pep8.StyleGuide(select='K')
checker = pep8.Checker(lines=lines, options=guide.options)
checker.check_all()
checker.report._deferred_print.sort()
return checker.report._deferred_print