示例#1
0
def _test_style(filename, ignore):
    """ Test style for a certain file.
    """
    if isinstance(ignore, (list, tuple)):
        ignore = ','.join(ignore)
    
    orig_dir = os.getcwd()
    orig_argv = sys.argv
    
    os.chdir(ROOT_DIR)
    sys.argv[1:] = [filename]
    sys.argv.append('--ignore=' + ignore)
    nerrors = 1
    try:
        import flake8  # noqa
        from flake8.main.application import Application
        app = Application()
        app.run()
        nerrors = app.result_count
        app.exit()
    except SystemExit as ex:
        return nerrors
    finally:
        os.chdir(orig_dir)
        sys.argv[:] = orig_argv
示例#2
0
文件: test.py 项目: zoofIO/flexx
def test_style(rel_path='.'):
    # Ensure we have flake8
    try:
        import flake8  # noqa
        from flake8.main.application import Application
    except ImportError as err:
        sys.exit('Cannot do style test: ' + str(err))
    # Prepare
    os.chdir(ROOT_DIR)
    if rel_path in ('', '.'):
        sys.argv[1:] = ['flexx', 'flexxamples']
    else:
        sys.argv[1:] = ['flexx/' + rel_path]
    # Do test
    print('Running flake8 tests ...')
    app = Application()
    app.run()
    # Report
    nerrors = app.result_count
    if nerrors:
        print('Arg! Found %i style errors.' % nerrors)
    else:
        print('Hooray, no style errors found!')
    # Exit (will exit(1) if errors)
    app.exit()
示例#3
0
def test_flake8(dir):
    src_dir = os.path.abspath(os.path.join(__file__, '..', '..', dir))
    app = Application()
    app.run([src_dir])
    assert app.result_count == 0
示例#4
0
from flake8.main.application import Application
import pytest

if __name__ == "__main__":
    # Lint code
    flake8 = Application()
    flake8.run(['assignment', '--max-line-length', '100'])
    if flake8.result_count > 0:
        print('-- flake8 found code style issues --')
        flake8.exit()

    # Run tests
    pytest.main(['-v', '--basetemp', 'processing_results'])
示例#5
0
def lint(paths, config, **lintargs):
    from flake8.main.application import Application

    log = lintargs['log']
    root = lintargs['root']
    config_path = os.path.join(root, '.flake8')

    if lintargs.get('fix'):
        fix_cmd = [
            os.path.join(bindir, 'autopep8'),
            '--global-config',
            config_path,
            '--in-place',
            '--recursive',
        ]

        if config.get('exclude'):
            fix_cmd.extend(['--exclude', ','.join(config['exclude'])])

        subprocess.call(fix_cmd + paths)

    # Run flake8.
    app = Application()
    log.debug("flake8 version={}".format(app.version))

    output_file = mozfile.NamedTemporaryFile(mode='r')
    flake8_cmd = [
        '--config',
        config_path,
        '--output-file',
        output_file.name,
        '--format',
        '{"path":"%(path)s","lineno":%(row)s,'
        '"column":%(col)s,"rule":"%(code)s","message":"%(text)s"}',
        '--filename',
        ','.join(['*.{}'.format(e) for e in config['extensions']]),
    ]
    log.debug("Command: {}".format(' '.join(flake8_cmd)))

    orig_make_file_checker_manager = app.make_file_checker_manager

    def wrap_make_file_checker_manager(self):
        """Flake8 is very inefficient when it comes to applying exclusion
        rules, using `expand_exclusions` to turn directories into a list of
        relevant python files is an order of magnitude faster.

        Hooking into flake8 here also gives us a convenient place to merge the
        `exclude` rules specified in the root .flake8 with the ones added by
        tools/lint/mach_commands.py.
        """
        # Ignore exclude rules if `--no-filter` was passed in.
        config.setdefault('exclude', [])
        if lintargs.get('use_filters', True):
            config['exclude'].extend(self.options.exclude)

        # Since we use the root .flake8 file to store exclusions, we haven't
        # properly filtered the paths through mozlint's `filterpaths` function
        # yet. This mimics that though there could be other edge cases that are
        # different. Maybe we should call `filterpaths` directly, though for
        # now that doesn't appear to be necessary.
        filtered = [
            p for p in paths
            if not any(p.startswith(e) for e in config['exclude'])
        ]

        self.args = self.args + list(expand_exclusions(filtered, config, root))

        if not self.args:
            raise NothingToLint
        return orig_make_file_checker_manager()

    app.make_file_checker_manager = wrap_make_file_checker_manager.__get__(
        app, Application)

    # Make sure to run from repository root so exclusions are joined to the
    # repository root and not the current working directory.
    oldcwd = os.getcwd()
    os.chdir(root)
    try:
        app.run(flake8_cmd)
    except NothingToLint:
        pass
    finally:
        os.chdir(oldcwd)

    results = []

    def process_line(line):
        # Escape slashes otherwise JSON conversion will not work
        line = line.replace('\\', '\\\\')
        try:
            res = json.loads(line)
        except ValueError:
            print('Non JSON output from linter, will not be processed: {}'.
                  format(line))
            return

        if res.get('code') in LINE_OFFSETS:
            res['lineoffset'] = LINE_OFFSETS[res['code']]

        results.append(result.from_config(config, **res))

    list(map(process_line, output_file.readlines()))
    return results
示例#6
0
def lint(paths, config, **lintargs):
    from flake8.main.application import Application

    root = lintargs['root']
    config_path = os.path.join(root, '.flake8')

    if lintargs.get('fix'):
        fix_cmd = [
            os.path.join(bindir, 'autopep8'),
            '--global-config',
            config_path,
            '--in-place',
            '--recursive',
        ]

        if config.get('exclude'):
            fix_cmd.extend(['--exclude', ','.join(config['exclude'])])

        subprocess.call(fix_cmd + paths)

    # Run flake8.
    app = Application()

    output_file = mozfile.NamedTemporaryFile(mode='r')
    flake8_cmd = [
        '--config',
        config_path,
        '--output-file',
        output_file.name,
        '--format',
        '{"path":"%(path)s","lineno":%(row)s,'
        '"column":%(col)s,"rule":"%(code)s","message":"%(text)s"}',
        '--filename',
        ','.join(['*.{}'.format(e) for e in config['extensions']]),
    ]

    orig_make_file_checker_manager = app.make_file_checker_manager

    def wrap_make_file_checker_manager(self):
        """Flake8 is very inefficient when it comes to applying exclusion
        rules, using `expand_exclusions` to turn directories into a list of
        relevant python files is an order of magnitude faster.

        Hooking into flake8 here also gives us a convenient place to merge the
        `exclude` rules specified in the root .flake8 with the ones added by
        tools/lint/mach_commands.py.
        """
        config.setdefault('exclude', []).extend(self.options.exclude)
        self.options.exclude = None
        self.args = self.args + list(expand_exclusions(paths, config, root))

        if not self.args:
            raise NothingToLint
        return orig_make_file_checker_manager()

    app.make_file_checker_manager = wrap_make_file_checker_manager.__get__(
        app, Application)

    # Make sure to run from repository root so exclusions are joined to the
    # repository root and not the current working directory.
    oldcwd = os.getcwd()
    os.chdir(root)
    try:
        app.run(flake8_cmd)
    except NothingToLint:
        pass
    finally:
        os.chdir(oldcwd)

    results = []

    def process_line(line):
        # Escape slashes otherwise JSON conversion will not work
        line = line.replace('\\', '\\\\')
        try:
            res = json.loads(line)
        except ValueError:
            print('Non JSON output from linter, will not be processed: {}'.
                  format(line))
            return

        if res.get('code') in LINE_OFFSETS:
            res['lineoffset'] = LINE_OFFSETS[res['code']]

        results.append(result.from_config(config, **res))

    list(map(process_line, output_file.readlines()))
    return results
示例#7
0
from flake8.main.application import Application
import pytest

if __name__ == "__main__":
    # Lint code
    flake8 = Application()
    flake8.run([
        'assignment', '--max-line-length', '100', '--banned-modules',
        'skimage.exposure = The scikit-image exposure module is not allowed for this assignment'
    ])
    if flake8.result_count > 0:
        print('-- flake8 found code style issues --')
        flake8.exit()

    # Run tests
    pytest.main([
        '-v', '--basetemp', 'processing_results', '-W',
        'ignore::DeprecationWarning'
    ])