|
| 1 | +""" py.test plugin for checking files with pylama. """ |
| 2 | +from __future__ import absolute_import |
| 3 | + |
| 4 | +from os import path as op |
| 5 | + |
| 6 | +import py |
| 7 | +import pytest |
| 8 | + |
| 9 | + |
| 10 | +HISTKEY = "pylama/mtimes" |
| 11 | + |
| 12 | + |
| 13 | +def pytest_addoption(parser): |
| 14 | + group = parser.getgroup("general") |
| 15 | + group.addoption( |
| 16 | + '--pylama', action='store_true', |
| 17 | + help="perform some pylama code checks on .py files") |
| 18 | + |
| 19 | + |
| 20 | +def pytest_sessionstart(session): |
| 21 | + config = session.config |
| 22 | + if config.option.pylama and getattr(config, 'cache', None): |
| 23 | + config._pylamamtimes = config.cache.get(HISTKEY, {}) |
| 24 | + |
| 25 | + |
| 26 | +def pytest_sessionfinish(session): |
| 27 | + config = session.config |
| 28 | + if hasattr(config, "_pylamamtimes"): |
| 29 | + config.cache.set(HISTKEY, config._pylamamtimes) |
| 30 | + |
| 31 | + |
| 32 | +def pytest_collect_file(path, parent): |
| 33 | + config = parent.config |
| 34 | + if config.option.pylama and path.ext == '.py': |
| 35 | + return PylamaItem(path, parent) |
| 36 | + |
| 37 | + |
| 38 | +class PylamaError(Exception): |
| 39 | + """ indicates an error during pylama checks. """ |
| 40 | + |
| 41 | + |
| 42 | +class PylamaItem(pytest.Item, pytest.File): |
| 43 | + |
| 44 | + def __init__(self, path, parent): |
| 45 | + super(PylamaItem, self).__init__(path, parent) |
| 46 | + self.add_marker("pep8") |
| 47 | + self.cache = None |
| 48 | + self._pylamamtimes = None |
| 49 | + |
| 50 | + def setup(self): |
| 51 | + if not getattr(self.config, 'cache', None): |
| 52 | + return False |
| 53 | + |
| 54 | + self.cache = True |
| 55 | + self._pylamamtimes = self.fspath.mtime() |
| 56 | + pylamamtimes = self.config._pylamamtimes |
| 57 | + old = pylamamtimes.get(str(self.fspath), 0) |
| 58 | + if old == self._pylamamtimes: |
| 59 | + pytest.skip("file(s) previously passed Pylama checks") |
| 60 | + |
| 61 | + def runtest(self): |
| 62 | + call = py.io.StdCapture.call |
| 63 | + errors, out, err = call(check_file, self.fspath) |
| 64 | + # errors = check_file(self.fspath) |
| 65 | + if errors: |
| 66 | + raise PylamaError(out, err) |
| 67 | + # update mtime only if test passed |
| 68 | + # otherwise failures would not be re-run next time |
| 69 | + if self.cache: |
| 70 | + self.config._pylamamtimes[str(self.fspath)] = self._pylamamtimes |
| 71 | + |
| 72 | + def repr_failure(self, excinfo): |
| 73 | + if excinfo.errisinstance(PylamaError): |
| 74 | + return excinfo.value.args[0] |
| 75 | + return super(PylamaItem, self).repr_failure(excinfo) |
| 76 | + |
| 77 | + |
| 78 | +def check_file(path): |
| 79 | + from pylama.main import parse_options, check_files |
| 80 | + from pylama.config import CURDIR |
| 81 | + |
| 82 | + options = parse_options() |
| 83 | + path = op.relpath(str(path), CURDIR) |
| 84 | + return check_files([path], options, error=False) |
| 85 | + |
| 86 | +# pylama:ignore=D,E1002,W0212 |
0 commit comments