Exemplo n.º 1
0
def test_file(filename, lines, code, options):
    tree = compile(''.join(lines), '', 'exec', PyCF_ONLY_AST)
    checker = pep8ext_naming.NamingChecker(tree, filename)
    parse_options(checker, options)

    found_errors = []
    for lineno, col_offset, msg, instance in checker.run():
        found_errors.append(msg.split()[0])

    if code is None:  # Invalid test case
        return 0
    if not found_errors and code == 'Okay':  # Expected PASS
        return 0
    if code in found_errors:  # Expected FAIL
        return 0
    print("ERROR: %s not in %s" % (code, filename))
    return 1
Exemplo n.º 2
0
def test_file(filename, lines, codes):
    tree = compile(''.join(lines), '', 'exec', PyCF_ONLY_AST)
    checker = pep8ext_naming.NamingChecker(tree, filename)
    found_errors = []
    for lineno, col_offset, msg, instance in checker.run():
        found_errors.append(msg.split()[0])

    if not found_errors and codes == ['Okay']:
        return 0

    errors = 0
    for code in codes:
        if code not in found_errors:
            errors += 1
            print("ERROR: %s not in %s" % (code, filename))

    return errors
Exemplo n.º 3
0
def test_file(filename, lines, code, options):
    if code is None:  # Invalid test case
        return 0
    source = ''.join(lines)
    tree = compile(source, '', 'exec', PyCF_ONLY_AST)
    checker = pep8ext_naming.NamingChecker(tree, filename)
    parse_options(checker, options)
    error_format = ('{0}:{lineno}:{col_offset}'
                    if ':' in code else '{0}').format

    found_errors = set()
    for lineno, col_offset, msg, instance in checker.run():
        found_errors.add(error_format(msg.split()[0], **locals()))

    if not found_errors and code == 'Okay':  # Expected PASS
        return 0
    if code in found_errors:  # Expected FAIL
        return 0
    print("ERROR: %s not in %s. found_errors: %s. Source:\n%s" %
          (code, filename, found_errors, source))
    return 1
Exemplo n.º 4
0
def lint(lines, settings):
    """
    Run flake8 lint with internal interpreter.
    """
    warnings = []

    # lint with pep8
    if settings.get('pep8', True):
        pep8style = pep8.StyleGuide(
            reporter=Pep8Report,
            ignore=['DIRTY-HACK'],  # PEP8 error will never starts like this
            max_line_length=settings.get('pep8_max_line_length'))
        pep8style.input_file(filename=None, lines=lines.splitlines(True))
        warnings.extend(pep8style.options.report.errors)

    if settings.get('pep257', False):
        for error in PEP257Checker().check_source(lines, ''):
            warnings.append((getattr(error, 'line',
                                     0), 0, getattr(error, 'message', '')))

    try:
        tree = compile(lines, '', 'exec', ast.PyCF_ONLY_AST, True)
    except (SyntaxError, TypeError):
        (exc_type, exc) = sys.exc_info()[:2]
        if len(exc.args) > 1:
            offset = exc.args[1]
            if len(offset) > 2:
                offset = offset[1:3]
        else:
            offset = (1, 0)

        warnings.append(
            (offset[0], offset[1]
             or 0, 'E901 %s: %s' % (exc_type.__name__, exc.args[0])))
    else:
        # lint with pyflakes
        if settings.get('pyflakes', True):
            builtins = settings.get('builtins')
            if builtins:  # builtins is extended
                # some magic (ok, ok, monkey-patching) goes here
                pyflakes.checker.Checker.builtIns = (
                    pyflakes.checker.Checker.builtIns.union(builtins))
            w = pyflakes_checker.Checker(tree)
            w.messages.sort(key=lambda m: m.lineno)

            reporter = FlakesReporter()
            for warning in w.messages:
                reporter.flake(warning)
            warnings.extend(reporter.errors)

        # lint with naming
        if settings.get('naming', True):
            checker = pep8ext_naming.NamingChecker(tree, None)
            for error in checker.run():
                warnings.append(error[0:3])

        try:
            complexity = int(settings.get('complexity', -1))
        except (TypeError, ValueError):
            complexity = -1

        # check complexity
        if complexity > -1:
            mccabe.McCabeChecker.max_complexity = complexity
            checker = mccabe.McCabeChecker(tree, None)
            for error in checker.run():
                warnings.append(error[0:3])

    return sorted(warnings, key=lambda e: '{0:09d}{1:09d}'.format(e[0], e[1]))