Skip to content

Commit

Permalink
Merge pull request #15 from carsongee/rc/0.5.0
Browse files Browse the repository at this point in the history
Rc/0.5.0
  • Loading branch information
carsongee committed Apr 2, 2016
2 parents 9426b89 + 715234f commit 2fc6457
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
20 changes: 14 additions & 6 deletions pytest_pylint.py
@@ -1,7 +1,7 @@
"""Pylint plugin for py.test"""
from __future__ import unicode_literals
from __future__ import absolute_import
from os.path import exists
from os.path import exists, join, dirname
from six.moves.configparser import ( # pylint: disable=import-error
ConfigParser,
NoSectionError,
Expand Down Expand Up @@ -66,10 +66,16 @@ def pytest_collect_file(path, parent):
return
if 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 pylintrc_file and not exists(pylintrc_file):
# The directory of pytest.ini got a chance
pylintrc_file = join(dirname(str(config.inifile)), pylintrc_file)

if not pylintrc_file or not exists(pylintrc_file):
# No pylintrc, therefore no ignores, so return the item.
return PyLintItem(path, parent)

pylintrc = ConfigParser()
Expand All @@ -88,7 +94,7 @@ def pytest_collect_file(path, parent):
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)
return PyLintItem(path, parent, msg_template, pylintrc_file)


class PyLintException(Exception):
Expand All @@ -102,22 +108,24 @@ class PyLintItem(pytest.Item, pytest.File):
# astng plugin for pylint in pypi yet, so we'll have to disable
# the checks.
# pylint: disable=no-member,super-on-old-class
def __init__(self, fspath, parent, msg_format=None):
def __init__(self, fspath, parent, msg_format=None, pylintrc_file=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

self.pylintrc_file = pylintrc_file

def runtest(self):
"""Setup and run pylint for the given test file."""
reporter = ProgrammaticReporter()
# Build argument list for pylint
args_list = [str(self.fspath)]
if self.config.option.pylint_rcfile:
if self.pylintrc_file:
args_list.append('--rcfile={0}'.format(
self.config.option.pylint_rcfile
self.pylintrc_file
))
lint.Run(args_list, reporter=reporter, exit=False)
reported_errors = []
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -13,7 +13,7 @@
description='pytest plugin to check source code with pylint',
long_description=open("README.rst").read(),
license="MIT",
version='0.4.1',
version='0.5.0',
author='Carson Gee',
author_email='x@carsongee.com',
url='https://github.com/carsongee/pytest-pylint',
Expand Down
33 changes: 33 additions & 0 deletions test_pytest_pylint.py
Expand Up @@ -37,6 +37,39 @@ def test_pylintrc_file(testdir):
assert 'Line too long (10/3)' in result.stdout.str()


def test_pylintrc_file_beside_ini(testdir):
"""
Verify that a specified pylint rc file will work what placed into pytest
ini dir.
"""
non_cwd_dir = testdir.mkdir('non_cwd_dir')

rcfile = non_cwd_dir.join('foo.rc')
rcfile.write("""
[FORMAT]
max-line-length=3
""")

inifile = non_cwd_dir.join('foo.ini')
inifile.write("""
[pytest]
addopts = --pylint --pylint-rcfile={0}
""".format(rcfile.basename))

pyfile = testdir.makepyfile("""import sys""")

result = testdir.runpytest(
pyfile.strpath
)
assert 'Line too long (10/3)' not in result.stdout.str()

result = testdir.runpytest(
'-c', inifile.strpath, pyfile.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', """
Expand Down

0 comments on commit 2fc6457

Please sign in to comment.