Skip to content

Commit

Permalink
Merge branch 'rc/0.4.0' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
carsongee committed Sep 28, 2015
2 parents 9b90a13 + 0aed7b9 commit dc1ba06
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 11 deletions.
54 changes: 45 additions & 9 deletions pytest_pylint.py
@@ -1,12 +1,18 @@
"""Pylint plugin for py.test"""
from __future__ import unicode_literals
from __future__ import absolute_import

import pytest
from os.path import exists

from pylint import lint
from pylint.config import PYLINTRC
from pylint.interfaces import IReporter
from pylint.reporters import BaseReporter
import pytest
from six.moves.configparser import ( # pylint: disable=import-error
ConfigParser,
NoSectionError,
NoOptionError
)


class ProgrammaticReporter(BaseReporter):
Expand All @@ -30,7 +36,6 @@ def handle_message(self, msg):

def _display(self, layout):
"""launch layouts display"""
pass


def pytest_addoption(parser):
Expand All @@ -57,9 +62,33 @@ def pytest_addoption(parser):
def pytest_collect_file(path, parent):
"""Handle running pylint on files discovered"""
config = parent.config
if path.ext == ".py":
if config.option.pylint:
return PyLintItem(path, parent)
if not config.option.pylint:
return
if not path.ext == ".py":
return
# Find pylintrc to check ignore list
pylintrc_file = config.option.pylint_rcfile or PYLINTRC
# No pylintrc, therefore no ignores, so return the item.
if not pylintrc_file or not exists(pylintrc_file):
return PyLintItem(path, parent)

pylintrc = ConfigParser()
pylintrc.read(pylintrc_file)
ignore_list = []
try:
ignore_string = pylintrc.get('MASTER', 'ignore')
if len(ignore_string) > 0:
ignore_list = ignore_string.split(',')
except (NoSectionError, NoOptionError):
pass
msg_template = None
try:
msg_template = pylintrc.get('REPORTS', 'msg-template')
except (NoSectionError, NoOptionError):
pass
rel_path = path.strpath.replace(parent.fspath.strpath, '', 1)[1:]
if not any(basename in rel_path for basename in ignore_list):
return PyLintItem(path, parent, msg_template)


class PyLintException(Exception):
Expand All @@ -72,7 +101,15 @@ class PyLintItem(pytest.Item, pytest.File):
# pylint doesn't deal well with dynamic modules and there isn't an
# astng plugin for pylint in pypi yet, so we'll have to disable
# the checks.
# pylint: disable=no-member,no-init,super-on-old-class
# pylint: disable=no-member,super-on-old-class
def __init__(self, fspath, parent, msg_format=None):
super(PyLintItem, self).__init__(fspath, parent)

if msg_format is None:
self._msg_format = '{C}:{line:3d},{column:2d}: {msg} ({symbol})'
else:
self._msg_format = msg_format

def runtest(self):
"""Setup and run pylint for the given test file."""
reporter = ProgrammaticReporter()
Expand All @@ -87,8 +124,7 @@ def runtest(self):
for error in reporter.data:
if error.C in self.config.option.pylint_error_types:
reported_errors.append(
'{msg.C}:{msg.line:3d},{msg.column:2d}: '
'{msg.msg} ({msg.symbol})'.format(msg=error)
error.format(self._msg_format)
)
if reported_errors:
raise PyLintException('\n'.join(reported_errors))
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Expand Up @@ -13,13 +13,13 @@
description='pytest plugin to check source code with pylint',
long_description=open("README.rst").read(),
license="MIT",
version='0.3.0',
version='0.4.0',
author='Carson Gee',
author_email='x@carsongee.com',
url='https://github.com/carsongee/pytest-pylint',
py_modules=['pytest_pylint'],
entry_points={'pytest11': ['pylint = pytest_pylint']},
install_requires=['pytest>=2.4', 'pylint'],
install_requires=['pytest>=2.4', 'pylint', 'six'],
classifiers=[
'Development Status :: 5 - Production/Stable',
'Intended Audience :: Developers',
Expand Down
28 changes: 28 additions & 0 deletions test_pytest_pylint.py
Expand Up @@ -35,3 +35,31 @@ def test_pylintrc_file(testdir):
'--pylint', '--pylint-rcfile={0}'.format(rcfile.strpath)
)
assert 'Line too long (10/3)' in result.stdout.str()


def test_pylintrc_ignore(testdir):
"""Verify that a pylintrc file with ignores will work."""
rcfile = testdir.makefile('rc', """
[MASTER]
ignore = test_pylintrc_ignore.py
""")
testdir.makepyfile("""import sys""")
result = testdir.runpytest(
'--pylint', '--pylint-rcfile={0}'.format(rcfile.strpath)
)
assert 'collected 0 items' in result.stdout.str()


def test_pylintrc_msg_template(testdir):
"""Verify that msg-template from pylintrc file is handled."""
rcfile = testdir.makefile('rc', """
[REPORTS]
msg-template=start {msg_id} end
""")
testdir.makepyfile("""import sys""")
result = testdir.runpytest(
'--pylint', '--pylint-rcfile={0}'.format(rcfile.strpath)
)
assert 'start W0611 end' in result.stdout.str()

0 comments on commit dc1ba06

Please sign in to comment.