pytest-codecheckers / codecheckers /

import py

import pkg_resources

class PyCodeCheckItem(py.test.collect.Item):
    def __init__(self, ep, parent):
        py.test.collect.Item.__init__(self,, parent)
        self._ep = ep

    def runtest(self):
        c =
        mod = self._ep.load()
            found_errors, out, err =, self.fspath)
            self.out, self.err = out, err
            found_errors = True
   = py.code.ExceptionInfo()
        assert not found_errors

    def repr_failure(self, exc_info):
            return self.out
        except AttributeError:
            #XXX: internal error ?!
            return super(PyCodeCheckItem, self).repr_failure(

    def reportinfo(self):
        return (self.fspath, -1, "codecheck %s" %

class PyCheckerCollector(py.test.collect.File):
    def __init__(self, path, parent):
        super(PyCheckerCollector, self).__init__(path, parent) += '[code-check]'

    def collect(self):
        if self.config.option.no_codechecks:
            return []
        checkers = self.config.getini('codechecks')
        entrypoints = pkg_resources.iter_entry_points('codechecker')
        #XXX: list wanted checkers we didnt get
        return [PyCodeCheckItem(ep, self) for ep in entrypoints if in checkers]

def pytest_collect_file(path, parent):
    if path.ext == '.py':
        return PyCheckerCollector(path, parent)

def pytest_addoption(parser):
    parser.addini('codechecks', type='args', help='listings of the codechecks to use')
    parser.addoption('--no-codechecks', action='store_true')