Commits

Ronny Pfannschmidt committed 4a02789

complete cleanup

Comments (0)

Files changed (4)

codecheckers/flakes.py

 
 from pyflakes.checker import Binding, Assignment
 
+
 def assignment_monkeypatched_init(self, name, source):
     Binding.__init__(self, name, source)
     if name == '__tracebackhide__':
 
 Assignment.__init__ = assignment_monkeypatched_init
 
-def check_file(path, filename, io):
-    return pyflakes_check(path.read(), filename, io)
 
+def check_file(path, filename):
+    return pyflakes_check(path.read(), filename)

codecheckers/pep.py

-import sys
 import pep8
 
-class PyTestChecker(pep8.Checker):
-    ignored_errors = 0
-    def report_error(self, line_number, offset, text, check):
-        #XXX: pep8 is a retarded module!
-        if pep8.ignore_code(text[:4]):
-            self.ignored_errors += 1
-        pep8.Checker.report_error(self, line_number, offset, text, check)
 
-def check_file(path, filename, io):
-    oldio = sys.stdout, sys.stderr
-    try:
-        sys.stdout = sys.stderr = io
-        pep8.process_options(['pep8',
-            # ignore list taken from moin
-            '--ignore=E202,E221,E222,E241,E301,E302,E401,E501,E701,W391,W601,W602',
-            '--show-source',
-            '--repeat',
-            'dummy file',
-            ])
-        checker = PyTestChecker(filename, path.readlines())
-        #XXX: bails out on death
-        error_count = checker.check_all()
-        ignored = checker.ignored_errors
-        return max(error_count - ignored, 0)
-    finally:
-        sys.stdout , sys.stderr = oldio
+def check_file(path, filename):
+    checker = pep8.Checker(filename, path.readlines(),
+                           show_source=True,
+                           repeat=True)
+    return checker.check_all()

codecheckers/plugin.py

 import pkg_resources
 
 
+class FoundErrors(Exception):
+    def __init__(self, count, out, err):
+        self.count = count
+        self.out = out
+        self.err = err
+
+
 class PyCodeCheckItem(py.test.collect.Item):
     def __init__(self, ep, parent):
         py.test.collect.Item.__init__(self, ep.name, parent)
         self._ep = ep
 
     def runtest(self):
-        mod = self._ep.load()
-        io = py.io.BytesIO()
+        check = self._ep.load().check_file
+        call = py.io.StdCapture.call
 
-        try:
-            found_errors = mod.check_file(self.fspath, self.filename, io)
-            self.out = io.getvalue()
-        except:
-            found_errors = True
-            self.info = py.code.ExceptionInfo()
-        assert not found_errors
+        found_errors, out, err = call(check, self.fspath, self.filename)
+        if found_errors:
+            raise FoundErrors(FoundErrors, out, err)
 
-    def repr_failure(self, exc_info):
-        try:
-            return self.out
-        except AttributeError:
-            #XXX: internal error ?!
-            self.info = py.code.ExceptionInfo()
-            info = getattr(self, 'info', exc_info)
-            return super(PyCodeCheckItem, self).repr_failure(info)
+    def repr_failure(self, excinfo):
+        if excinfo.errisinstance(FoundErrors):
+            return excinfo.value.out
+        return super(PyCodeCheckItem, self).repr_failure(excinfo)
 
     def reportinfo(self):
-        return (self.fspath, -1, "codecheck %s %s" % (
-            self._ep.name, self.filename))
+        return (self.fspath, -1, "codecheck " + self._ep.name)
 
 
 class PyCheckerCollector(py.test.collect.File):
             return []
         checkers = self.config.getini('codechecks')
         entrypoints = pkg_resources.iter_entry_points('codechecker')
-
-        items = [PyCodeCheckItem(ep, self) for ep in entrypoints if ep.name in checkers]
-        return items
+        wanted = (ep for ep in entrypoints if ep.name in checkers)
+        return [PyCodeCheckItem(wep, self) for wep in wanted]
 
 
 def pytest_collect_file(path, parent):
 
 
 def pytest_addoption(parser):
-    parser.addini('codechecks', type='args', help='listings of the codechecks to use')
+    parser.addini('codechecks', type='args',
+                  help='listings of the codechecks to use',
+                  default=['pep8', 'pyflakes'])
     parser.addoption('--no-codechecks', action='store_true')

tests/test_pyflakes.py

     out.stdout.fnmatch_lines([
         '*abc*',
         '*1 failed*',
-        ])
+    ])
 
 
 def test_reportinfo_verbose(testdir):
     out = testdir.runpytest('-v')
     out.stdout.fnmatch_lines([
         '*test_reportinfo_verbose.py:0: codecheck pyflakes PASSED',
-        ])
+    ])