[Python-modules-commits] [pytest-pylint] 02/10: Import pytest-pylint_0.7.1.orig.tar.gz
Ondrej Koblizek
kobla-guest at moszumanska.debian.org
Thu Jul 20 13:30:29 UTC 2017
This is an automated email from the git hooks/post-receive script.
kobla-guest pushed a commit to branch master
in repository pytest-pylint.
commit dfff20c4c1d63e1160a845b2390009c5e2b62fcb
Author: Ondřej Kobližek <koblizeko at gmail.com>
Date: Thu Jul 20 14:40:23 2017 +0200
Import pytest-pylint_0.7.1.orig.tar.gz
---
.gitignore | 3 ++
.travis.yml | 7 ++-
README.rst | 15 ++++++
pytest_pylint.py | 124 ++++++++++++++++++++++++++++++++++----------------
setup.py | 2 +-
test_pytest_pylint.py | 16 ++++++-
tox.ini | 2 +-
7 files changed, 126 insertions(+), 43 deletions(-)
diff --git a/.gitignore b/.gitignore
index ba74660..39efeea 100644
--- a/.gitignore
+++ b/.gitignore
@@ -55,3 +55,6 @@ docs/_build/
# PyBuilder
target/
+
+# PyEnv
+.python-version
diff --git a/.travis.yml b/.travis.yml
index d8d2d3e..c2b44c3 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,9 +1,12 @@
language: python
python:
- "2.7"
+ - "3.3"
+ - "3.4"
+ - "3.5"
+ - "3.6"
install:
- - "pip install tox"
- - "pip install coveralls"
+ - "pip install tox-travis tox coveralls"
- "pip install -e ."
script: tox
after_success:
diff --git a/README.rst b/README.rst
index 08b0eb2..5dcd0fd 100644
--- a/README.rst
+++ b/README.rst
@@ -42,3 +42,18 @@ Acknowledgements
This code is heavily based on
`pytest-flakes <https://github.com/fschulze/pytest-flakes>`_
+
+
+Releases
+========
+
+0.7.1
+~~~~~
+
+- Corrected path issue reported by `Kargathia <https://github.com/Kargathia>`_
+
+0.7.0
+~~~~~
+
+- Linting is performed before tests which enables code duplication
+ checks to work along with a performance boost, thanks to @heoga
diff --git a/pytest_pylint.py b/pytest_pylint.py
index 65b0df0..1ba8de0 100644
--- a/pytest_pylint.py
+++ b/pytest_pylint.py
@@ -1,6 +1,8 @@
"""Pylint plugin for py.test"""
from __future__ import unicode_literals
from __future__ import absolute_import
+from __future__ import print_function
+from os import sep
from os.path import exists, join, dirname
from six.moves.configparser import ( # pylint: disable=import-error
ConfigParser,
@@ -15,6 +17,11 @@ from pylint.reporters import BaseReporter
import pytest
+class PyLintException(Exception):
+ """Exception to raise if a file has a specified pylint error"""
+ pass
+
+
class ProgrammaticReporter(BaseReporter):
"""Reporter that replaces output with storage in list of dictionaries"""
@@ -38,6 +45,18 @@ class ProgrammaticReporter(BaseReporter):
"""launch layouts display"""
+def get_rel_path(path, parent_path):
+ """
+ Give the path to object relative to ``parent_path``.
+ """
+ replaced_path = path.replace(parent_path, '', 1)
+ if replaced_path[0] == sep:
+ rel_path = replaced_path[1:]
+ else:
+ rel_path = replaced_path
+ return rel_path
+
+
def pytest_addoption(parser):
"""Add all our command line options"""
group = parser.getgroup("general")
@@ -59,13 +78,15 @@ def pytest_addoption(parser):
)
-def pytest_collect_file(path, parent):
- """Handle running pylint on files discovered"""
- config = parent.config
- if not config.option.pylint:
- return
- if path.ext != ".py":
- return
+def pytest_sessionstart(session):
+ """Storing pylint settings on the session"""
+ session.pylint_files = set()
+ session.pylint_messages = {}
+ session.pylint_config = None
+ session.pylintrc_file = None
+ session.pylint_ignore = []
+ session.pylint_msg_template = None
+ config = session.config
# Find pylintrc to check ignore list
pylintrc_file = config.option.pylint_rcfile or PYLINTRC
@@ -74,32 +95,66 @@ def pytest_collect_file(path, parent):
# 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):
+ if pylintrc_file and exists(pylintrc_file):
+ session.pylintrc_file = pylintrc_file
+ session.pylint_config = ConfigParser()
+ session.pylint_config.read(pylintrc_file)
+ try:
+ ignore_string = session.pylint_config.get('MASTER', 'ignore')
+ if ignore_string:
+ session.pylint_ignore = ignore_string.split(',')
+ except (NoSectionError, NoOptionError):
+ pass
+ try:
+ session.pylint_msg_template = session.pylint_config.get(
+ 'REPORTS', 'msg-template'
+ )
+ except (NoSectionError, NoOptionError):
+ pass
+
+
+def pytest_collect_file(path, parent):
+ """Collect files on which pylint should run"""
+ config = parent.config
+ if not config.option.pylint:
+ return
+ if path.ext != ".py":
+ return
+ rel_path = get_rel_path(path.strpath, parent.fspath.strpath)
+ if parent.pylint_config is None:
+ parent.pylint_files.add(rel_path)
# No pylintrc, therefore no ignores, so return the item.
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, pylintrc_file)
+ if not any(basename in rel_path for basename in parent.pylint_ignore):
+ parent.pylint_files.add(rel_path)
+ return PyLintItem(
+ path, parent, parent.pylint_msg_template, parent.pylintrc_file
+ )
-class PyLintException(Exception):
- """Exception to raise if a file has a specified pylint error"""
- pass
+def pytest_collection_finish(session):
+ """Lint collected files and store messages on session."""
+ if not session.pylint_files:
+ return
+ reporter = ProgrammaticReporter()
+ # Build argument list for pylint
+ args_list = list(session.pylint_files)
+ if session.pylintrc_file:
+ args_list.append('--rcfile={0}'.format(
+ session.pylintrc_file
+ ))
+ print('-' * 65)
+ print('Linting files')
+ # Run pylint over the collected files.
+ result = lint.Run(args_list, reporter=reporter, exit=False)
+ messages = result.linter.reporter.data
+ # Stores the messages in a dictionary for lookup in tests.
+ for message in messages:
+ if message.path not in session.pylint_messages:
+ session.pylint_messages[message.path] = []
+ session.pylint_messages[message.path].append(message)
+ print('-' * 65)
class PyLintItem(pytest.Item, pytest.File):
@@ -112,6 +167,7 @@ class PyLintItem(pytest.Item, pytest.File):
super(PyLintItem, self).__init__(fspath, parent)
self.add_marker("pylint")
+ self.rel_path = get_rel_path(fspath.strpath, parent.fspath.strpath)
if msg_format is None:
self._msg_format = '{C}:{line:3d},{column:2d}: {msg} ({symbol})'
@@ -121,17 +177,9 @@ class PyLintItem(pytest.Item, pytest.File):
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.pylintrc_file:
- args_list.append('--rcfile={0}'.format(
- self.pylintrc_file
- ))
- lint.Run(args_list, reporter=reporter, exit=False)
+ """Check the pylint messages to see if any errors were reported."""
reported_errors = []
- for error in reporter.data:
+ for error in self.session.pylint_messages.get(self.rel_path, []):
if error.C in self.config.option.pylint_error_types:
reported_errors.append(
error.format(self._msg_format)
diff --git a/setup.py b/setup.py
index 49cf286..7d55568 100644
--- a/setup.py
+++ b/setup.py
@@ -13,7 +13,7 @@ setup(
description='pytest plugin to check source code with pylint',
long_description=open("README.rst").read(),
license="MIT",
- version='0.6.0',
+ version='0.7.1',
author='Carson Gee',
author_email='x at carsongee.com',
url='https://github.com/carsongee/pytest-pylint',
diff --git a/test_pytest_pylint.py b/test_pytest_pylint.py
index 4c8baac..78eb82a 100644
--- a/test_pytest_pylint.py
+++ b/test_pytest_pylint.py
@@ -3,7 +3,7 @@
Unit testing module for pytest-pylti plugin
"""
-pytest_plugins = 'pytester', # pylint: disable=invalid-name
+pytest_plugins = ('pytester',) # pylint: disable=invalid-name
def test_basic(testdir):
@@ -96,3 +96,17 @@ msg-template=start {msg_id} end
'--pylint', '--pylint-rcfile={0}'.format(rcfile.strpath)
)
assert 'start W0611 end' in result.stdout.str()
+
+
+def test_get_rel_path():
+ """
+ Verify our relative path function.
+ """
+ from pytest_pylint import get_rel_path
+ correct_rel_path = 'How/Are/You/blah.py'
+ path = '/Hi/How/Are/You/blah.py'
+ parent_path = '/Hi/'
+ assert get_rel_path(path, parent_path) == correct_rel_path
+
+ parent_path = '/Hi'
+ assert get_rel_path(path, parent_path) == correct_rel_path
diff --git a/tox.ini b/tox.ini
index 562e09a..34f73d9 100644
--- a/tox.ini
+++ b/tox.ini
@@ -1,5 +1,5 @@
[tox]
-envlist = py27,py33,py34
+envlist = py27,py33,py34,py35,py36
skip_missing_interpreters =
true
[testenv]
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/pytest-pylint.git
More information about the Python-modules-commits
mailing list