Exemple #1
0
def test_pep8():
    options = parse_options(linters=['pep8'], config=False)
    errors = run('dummy.py', options=options)
    assert len(errors) == 3

    options.linters_params['pep8'] = dict(max_line_length=60)
    errors = run('dummy.py', options=options)
    assert len(errors) == 11
Exemple #2
0
def test_pep8():
    options = parse_options(linters=['pep8'], config=False)
    errors = run('dummy.py', options=options)
    assert len(errors) == 2

    options.linters_params['pep8'] = dict(max_line_length=60)
    errors = run('dummy.py', options=options)
    assert len(errors) == 11
Exemple #3
0
def test_linters_params():
    options = parse_options(linters='mccabe', config=False)
    options.linters_params['mccabe'] = dict(complexity=2)
    errors = run('dummy.py', options=options)
    assert len(errors) == 13

    options.linters_params['mccabe'] = dict(complexity=20)
    errors = run('dummy.py', options=options)
    assert not errors
Exemple #4
0
def test_linters_params():
    options = parse_options(linters='mccabe', config=False)
    options.linters_params['mccabe'] = dict(complexity=1)
    errors = run('dummy.py', options=options)
    assert len(errors) == 1

    options.linters_params['mccabe'] = dict(complexity=20)
    errors = run('dummy.py', options=options)
    assert not errors
Exemple #5
0
def test_pycodestyle():
    options = parse_options(linters=['pycodestyle'], config=False)
    assert len(options.linters) == 1
    errors = run('dummy.py', options=options)
    assert len(errors) == 2

    options.linters_params['pycodestyle'] = dict(max_line_length=60)
    errors = run('dummy.py', options=options)
    assert len(errors) == 11
Exemple #6
0
def test_linters_params(parse_options, run):
    options = parse_options(linters='mccabe', config=False)
    options.linters_params['mccabe'] = {'max-complexity': '1'}
    errors = run('dummy.py', options=options)
    assert len(errors) == 1

    options.linters_params['mccabe'] = {'max-complexity': '20'}
    errors = run('dummy.py', options=options)
    assert not errors
Exemple #7
0
    def test_ignore_select(self):
        errors = run('dummy.py', ignore=['E301', 'C0110'])
        self.assertEqual(len(errors), 2)

        errors = run('dummy.py', ignore=['E3', 'C'])
        self.assertEqual(len(errors), 2)

        errors = run('dummy.py', ignore=['E3', 'C'], select=['E301'])
        self.assertEqual(len(errors), 3)
        self.assertTrue(errors[0]['col'])
Exemple #8
0
    def test_ignore_select(self):
        errors = run('dummy.py', ignore=['E301', 'C0110'])
        self.assertEqual(len(errors), 2)

        errors = run('dummy.py', ignore=['E3', 'C'])
        self.assertEqual(len(errors), 2)

        errors = run(
            'dummy.py', ignore=['E3', 'C'], select=['E301'])
        self.assertEqual(len(errors), 3)
        self.assertTrue(errors[0]['col'])
Exemple #9
0
def test_pycodestyle():
    options = parse_options(linters=['pycodestyle'], config=False)
    assert len(options.linters) == 1
    errors = run('dummy.py', options=options)
    numbers = [error.number for error in errors]
    assert len(errors) == 4
    assert 'E265' in numbers
    assert 'E301' in numbers
    assert 'E501' in numbers

    options.linters_params['pycodestyle'] = dict(max_line_length=60)
    errors = run('dummy.py', options=options)
    assert len(errors) == 13
Exemple #10
0
def test_pycodestyle():
    options = parse_options(linters=['pycodestyle'], config=False)
    assert len(options.linters) == 1
    errors = run('dummy.py', options=options)
    numbers = [error.number for error in errors]
    assert len(errors) == 4
    assert 'E265' in numbers
    assert 'E301' in numbers
    assert 'E501' in numbers

    options.linters_params['pycodestyle'] = dict(max_line_length=60)
    errors = run('dummy.py', options=options)
    assert len(errors) == 13
Exemple #11
0
def test_ignore_select():
    options = parse_options()
    options.ignore = ['E301', 'D102']
    options.linters = ['pep8', 'pep257', 'pyflakes', 'mccabe']
    errors = run('dummy.py', options=options)
    assert len(errors) == 16

    options.ignore = ['E3', 'D']
    errors = run('dummy.py', options=options)
    assert len(errors) == 1

    options.select = ['E301']
    errors = run('dummy.py', options=options)
    assert len(errors) == 2
    assert errors[0]['col']
Exemple #12
0
def test_ignore_select():
    options = parse_options()
    options.ignore = ['E301', 'D102']
    options.linters = ['pep8', 'pep257', 'pyflakes', 'mccabe']
    errors = run('dummy.py', options=options)
    assert len(errors) == 15

    options.ignore = ['E3', 'D']
    errors = run('dummy.py', options=options)
    assert len(errors) == 0

    options.select = ['E301']
    errors = run('dummy.py', options=options)
    assert len(errors) == 1
    assert errors[0]['col']
Exemple #13
0
def test_ignore_select():
    options = parse_options()
    options.ignore = ['E301', 'D102']
    options.linters = ['pycodestyle', 'pydocstyle', 'pyflakes', 'mccabe']
    errors = run('dummy.py', options=options)
    assert len(errors) == 17

    options.ignore = ['E3', 'D']
    errors = run('dummy.py', options=options)
    assert len(errors) == 0

    options.select = ['E301']
    errors = run('dummy.py', options=options)
    assert len(errors) == 1
    assert errors[0]['col']
Exemple #14
0
def test_skip(parse_options, run):
    options = parse_options()
    errors = run('dummy.py',
                 options=options,
                 code=("undefined()\n"
                       "# pylama: skip=1"))
    assert not errors
Exemple #15
0
def test_sort(parse_options):
    from pylama.core import run

    options = parse_options()
    options.sort = {'C': 1, 'D': 2}
    errors = run('dummy.py', options=options)
    assert errors[0].etype == 'C'
Exemple #16
0
def test_pyflakes():
    options = parse_options(linters=['pyflakes'], config=False)
    assert options.linters
    errors = run('dummy.py',
                 code="\n".join(
                     ["import sys", "def test():", "    unused = 1"]),
                 options=options)
    assert len(errors) == 2
Exemple #17
0
def test_pylint():
    from pylama.core import run
    from pylama.config import parse_options

    options = parse_options(linters=['pylint'], config=False)
    options.ignore = set(['R0912', 'C0111', 'I0011', 'F0401'])
    errors = run('dummy.py', options=options)
    assert len(errors) == 4
    assert errors[0].number == 'W0611'

    options.linters_params['pylint'] = dict(disable="W")
    errors = run('dummy.py', options=options)
    assert len(errors) == 1
    assert errors[0].number == 'E0602'

    options.linters_params['pylint']['max-line_length'] = 200
    errors = run('dummy.py', options=options)
    assert len(errors) == 1
Exemple #18
0
def test_pylint():
    from pylama.core import run
    from pylama.config import parse_options

    options = parse_options(linters=['pylint'], config=False)
    options.ignore = set(['R0912', 'C0111', 'I0011', 'F0401'])
    errors = run('dummy.py', options=options)
    assert len(errors) == 3
    assert errors[0].number == 'W0611'

    options.linters_params['pylint'] = dict(disable="W")
    errors = run('dummy.py', options=options)
    assert len(errors) == 1
    assert errors[0].number == 'E0602'

    options.linters_params['pylint']['max-line_length'] = 200
    errors = run('dummy.py', options=options)
    assert len(errors) == 1
Exemple #19
0
def run_checkers(checkers=None, ignore=None, buf=None, select=None,
                 complexity=None, callback=None, config=None):

    pylint_options = '--rcfile={0} -r n'.format(
        interface.get_var('lint_config')).split()

    return run(
        buf.name, ignore=ignore, select=select, linters=checkers,
        pylint=pylint_options, complexity=complexity, config=config)
Exemple #20
0
def test_pyflakes():
    options = parse_options(linters=['pyflakes'], config=False)
    assert options.linters
    errors = run('dummy.py', code="\n".join([
        "import sys",
        "def test():",
        "    unused = 1"
    ]), options=options)
    assert len(errors) == 2
Exemple #21
0
def test_pylint():
    from pylama.core import run
    from pylama.config import parse_options

    options = parse_options(linters=['pylint'], config=False)
    options.ignore = set(['R0912', 'C0111'])
    errors = run('pylama_pylint/pylint/utils.py', options=options)
    assert len(errors) == 29
    assert errors[0].number == 'W0622'

    options.linters_params['pylint'] = dict(disable="W")
    errors = run('pylama_pylint/pylint/utils.py', options=options)
    assert len(errors) == 21
    assert errors[0].number == 'C0301'

    options.linters_params['pylint']['max-line_length'] = 200
    errors = run('pylama_pylint/pylint/utils.py', options=options)
    assert len(errors) == 3
Exemple #22
0
def test_pylint():
    from pylama.core import run
    from pylama.config import parse_options

    options = parse_options(linters=["pylint"], config=False)
    options.ignore = set(["R0912", "C0111", "I0011", "F0401"])
    errors = run("dummy.py", options=options)
    assert len(errors) == 3
    assert errors[0].number == "W0611"

    options.linters_params["pylint"] = dict(disable="W")
    errors = run("dummy.py", options=options)
    assert len(errors) == 1
    assert errors[0].number == "E0602"

    options.linters_params["pylint"]["max-line_length"] = 200
    errors = run("dummy.py", options=options)
    assert len(errors) == 1
Exemple #23
0
def test_pyflakes():
    options = parse_options(linters=['pyflakes'], config=False)
    assert options.linters
    errors = run('dummy.py', code="""
import sys

def test():
    unused = 1
""", options=options)
    assert len(errors) == 2
Exemple #24
0
 def test_pylint(self):
     # test pylint
     if version_info < (3, 0):
         args = {
             'path': 'pylama/checkers/pylint/utils.py',
             'linters': ['pylint']}
         if platform.startswith('win'):
             # trailing whitespace is handled differently on win platforms
             args.update({'ignore': ['C0303']})
         errors = run(**args)
         self.assertEqual(len(errors), 16)
Exemple #25
0
def test_ignore_select():
    options = parse_options()
    options.ignore = ['E301', 'D102']
    options.linters = ['pycodestyle', 'pydocstyle', 'pyflakes', 'mccabe']
    errors = run('dummy.py', options=options)
    assert len(errors) == 32

    numbers = [error.number for error in errors]
    assert 'D100' in numbers
    assert 'E301' not in numbers
    assert 'D102' not in numbers

    options.ignore = ['E3', 'D', 'E2', 'E8']
    errors = run('dummy.py', options=options)
    assert not errors

    options.select = ['E301']
    errors = run('dummy.py', options=options)
    assert len(errors) == 1
    assert errors[0]['col']
Exemple #26
0
def test_ignore_select(parse_options, run):
    options = parse_options()
    options.ignore = {'E301', 'D102'}
    options.linters = ['pycodestyle', 'pydocstyle', 'pyflakes', 'mccabe']
    errors = run('dummy.py', options=options)
    assert errors
    for err in errors:
        assert err.number not in options.ignore

    numbers = [error.number for error in errors]
    assert 'D100' in numbers
    assert 'E301' not in numbers
    assert 'D102' not in numbers

    options.ignore = {'E', 'D', 'W'}
    errors = run('dummy.py', options=options)
    assert not errors

    options.select = {'E301'}
    errors = run('dummy.py', options=options)
    assert len(errors) == 1
    assert errors[0].col
Exemple #27
0
def run_checkers(checkers=None, ignore=None, buf=None, select=None,
                 complexity=None, callback=None, config=None):
    """ Run pylama code.

    :return list: errors

    """
    pylint_options = '--rcfile={0} -r n'.format(
        interface.get_var('lint_config')).split()

    return run(
        buf.name, ignore=ignore, select=select, linters=checkers,
        pylint=pylint_options, complexity=complexity, config=config)
Exemple #28
0
def check_paths(
    paths: Optional[List[str]],
    options: Namespace,
    code: str = None,
    rootdir: Path = None,
) -> List[Error]:
    """Check the given paths.

    :param rootdir: Root directory (for making relative file paths)
    :param options: Parsed pylama options (from pylama.config.parse_options)
    """
    paths = paths or options.paths
    if not paths:
        return []

    if code is None:
        candidates = []
        for path in paths or options.paths:
            if not op.exists(path):
                continue

            if not op.isdir(path):
                candidates.append(op.abspath(path))

            for root, _, files in walk(path):
                candidates += [op.relpath(op.join(root, f), CURDIR) for f in files]
    else:
        candidates = [paths[0]]

    if not candidates:
        return []

    if rootdir is None:
        path = candidates[0]
        rootdir = Path(path if op.isdir(path) else op.dirname(path))

    candidates = [path for path in candidates if path.endswith(".py")]

    if options.concurrent:
        return check_async(candidates, code=code, options=options, rootdir=rootdir)

    errors = []
    for path in candidates:
        errors += run(path=path, code=code, rootdir=rootdir, options=options)

    return errors
Exemple #29
0
 def test_pylint(self):
     # test pylint
     if version_info < (3, 0):
         errors = run('pylama/checkers/pylint/utils.py', linters=['pylint'])
         self.assertEqual(len(errors), 16)
Exemple #30
0
def test_mypy():
    if sys.version_info.major >= 3 and sys.version_info.minor >= 5:
        options = parse_options(linters=['mypy'])
        assert len(options.linters) == 1
        errors = run('dummy.py', options=options)
        assert len(errors) == 1
Exemple #31
0
def test_pydocstyle():
    options = parse_options(linters=['pydocstyle'])
    assert len(options.linters) == 1
    errors = run('dummy.py', options=options)
    assert errors
Exemple #32
0
 def test_lama(self):
     errors = run('dummy.py',
                  ignore=set(['M234', 'C']),
                  config=dict(lint=1))
     self.assertEqual(len(errors), 3)
Exemple #33
0
def code_check():
    """Run pylama and check current file.

    :return bool:

    """
    with silence_stderr():

        from pylama.core import run
        from pylama.main import parse_options

        if not env.curbuf.name:
            return env.stop()

        linters = env.var('g:pymode_lint_checkers')
        env.debug(linters)

        options = parse_options(
            linters=linters, force=1,
            ignore=env.var('g:pymode_lint_ignore'),
            select=env.var('g:pymode_lint_select'),
        )

        for linter in linters:
            opts = env.var('g:pymode_lint_options_%s' % linter, silence=True)
            if opts:
                options.linters_params[linter] = options.linters_params.get(linter, {})
                options.linters_params[linter].update(opts)
        options.linters_params['pylint']['clear_cache'] = True

        env.debug(options)

        path = os.path.relpath(env.curbuf.name, env.curdir)
        env.debug("Start code check: ", path)

        if getattr(options, 'skip', None) and any(p.match(path) for p in options.skip):  # noqa
            env.message('Skip code checking.')
            env.debug("Skipped")
            return env.stop()

        if env.options.get('debug'):
            from pylama.core import LOGGER, logging
            LOGGER.setLevel(logging.DEBUG)

        errors = run(path, code='\n'.join(env.curbuf) + '\n', options=options)

    env.debug("Find errors: ", len(errors))
    sort_rules = env.var('g:pymode_lint_sort')

    def __sort(e):
        try:
            return sort_rules.index(e.get('type'))
        except ValueError:
            return 999

    if sort_rules:
        env.debug("Find sorting: ", sort_rules)
        errors = sorted(errors, key=__sort)

    for e in errors:
        e._info['bufnr'] = env.curbuf.number
        if e._info['col'] is None:
            e._info['col'] = 1

    env.run('g:PymodeLocList.current().extend', [e._info for e in errors])
Exemple #34
0
 def test_pep257(self):
     errors = run('dummy.py', linters=['pep257'])
     self.assertTrue(errors)
Exemple #35
0
def test_sort():
    options = parse_options()
    options.sort = ['C', 'D']
    errors = run('dummy.py', options=options)
    assert errors[0].type == 'C'
Exemple #36
0
def test_sort():
    options = parse_options()
    options.sort = ['C', 'D']
    errors = run('dummy.py', options=options)
    assert errors[0].type == 'C'
Exemple #37
0
 def test_pep257(self):
     errors = run('dummy.py', linters=['pep257'])
     self.assertTrue(errors)
Exemple #38
0
def test_pep257():
    options = parse_options(linters=['pep257'])
    errors = run('dummy.py', options=options)
    assert errors
Exemple #39
0
 def test_gjslint(self):
     args = {'path': 'dummy.js', 'linters': ['gjslint']}
     errors = run(**args)
     self.assertEqual(len(errors), 1231)
Exemple #40
0
def code_check():
    """Run pylama and check current file.

    :return bool:

    """
    with silence_stderr():

        from pylama.core import run
        from pylama.config import parse_options

        if not env.curbuf.name:
            return env.stop()

        linters = env.var('g:pymode_lint_checkers')
        env.debug(linters)

        # Fixed in v0.9.3: these two parameters may be passed as strings.
        # DEPRECATE: v:0.10.0: need to be set as lists.
        if isinstance(env.var('g:pymode_lint_ignore'), str):
            raise ValueError ('g:pymode_lint_ignore should have a list type')
        else:
            ignore = env.var('g:pymode_lint_ignore')
        if isinstance(env.var('g:pymode_lint_select'), str):
            raise ValueError ('g:pymode_lint_select should have a list type')
        else:
            select = env.var('g:pymode_lint_select')
        options = parse_options(
            linters=linters, force=1,
            ignore=ignore,
            select=select,
        )
        env.debug(options)

        for linter in linters:
            opts = env.var('g:pymode_lint_options_%s' % linter, silence=True)
            if opts:
                options.linters_params[linter] = options.linters_params.get(
                    linter, {})
                options.linters_params[linter].update(opts)

        path = os.path.relpath(env.curbuf.name, env.curdir)
        env.debug("Start code check: ", path)

        if getattr(options, 'skip', None) and any(p.match(path) for p in options.skip):  # noqa
            env.message('Skip code checking.')
            env.debug("Skipped")
            return env.stop()

        if env.options.get('debug'):
            from pylama.core import LOGGER, logging
            LOGGER.setLevel(logging.DEBUG)

        errors = run(path, code='\n'.join(env.curbuf) + '\n', options=options)

    env.debug("Find errors: ", len(errors))
    sort_rules = env.var('g:pymode_lint_sort')

    def __sort(e):
        try:
            return sort_rules.index(e.get('type'))
        except ValueError:
            return 999

    if sort_rules:
        env.debug("Find sorting: ", sort_rules)
        errors = sorted(errors, key=__sort)

    for e in errors:
        e._info['bufnr'] = env.curbuf.number
        if e._info['col'] is None:
            e._info['col'] = 1

    env.run('g:PymodeLocList.current().extend', [e._info for e in errors])
Exemple #41
0
 def test_lama(self):
     errors = run(
         'dummy.py', ignore=set(['M234', 'C']), config=dict(lint=1))
     self.assertEqual(len(errors), 3)
Exemple #42
0
def test_mypy():
    if sys.version_info.major >= 3 and sys.version_info.minor >= 5:
        options = parse_options(linters=['mypy'])
        assert len(options.linters) == 1
        errors = run('dummy.py', options=options)
        assert len(errors) == 1
Exemple #43
0
def code_check():  # noqa
    """Run pylama and check current file.

    :return bool:

    """
    with silence_stderr():

        from pylama.core import run
        from pylama.main import parse_options
        from pylama.config import _override_options

        if not env.curbuf.name:
            return env.stop()

        options = parse_options(
            force=1,
            ignore=env.var('g:pymode_lint_ignore'),
            select=env.var('g:pymode_lint_select'),
        )

        linters = env.var('g:pymode_lint_checkers', default=[])
        if linters:
            _override_options(options, linters=",".join(linters))

            for linter in dict(options.linters):
                opts = env.var('g:pymode_lint_options_%s' % linter,
                               silence=True)
                if opts:
                    options.linters_params[
                        linter] = options.linters_params.get(linter, {})
                    options.linters_params[linter].update(opts)

        if 'pylint' in options.linters_params:
            options.linters_params['pylint']['clear_cache'] = True
        env.debug(options)

        path = os.path.relpath(env.curbuf.name, env.curdir)
        env.debug("Start code check: ", path)

        if getattr(options, 'skip', None) and any(
                p.match(path) for p in options.skip):  # noqa
            env.message('Skip code checking.')
            env.debug("Skipped")
            return env.stop()

        if env.options.get('debug'):
            from pylama.core import LOGGER, logging
            LOGGER.setLevel(logging.DEBUG)

        errors = run(path, code='\n'.join(env.curbuf) + '\n', options=options)

    env.debug("Find errors: ", len(errors))
    sort_rules = env.var('g:pymode_lint_sort')

    def __sort(e):
        try:
            return sort_rules.index(e.get('type'))
        except ValueError:
            return 999

    if sort_rules:
        env.debug("Find sorting: ", sort_rules)
        errors = sorted(errors, key=__sort)

    for e in errors:
        e._info['bufnr'] = env.curbuf.number
        if e._info['col'] is None:
            e._info['col'] = 1

    env.run('g:PymodeLocList.current().extend', [e._info for e in errors])
Exemple #44
0
 def test_pyflakes(self):
     errors = run('dummy.py', linters=['pyflakes'])
     self.assertFalse(errors)
Exemple #45
0
def test_pydocstyle():
    options = parse_options(linters=['pydocstyle'])
    assert len(options.linters) == 1
    errors = run('dummy.py', options=options)
    assert errors
Exemple #46
0
 def test_pyflakes(self):
     errors = run('dummy.py', linters=['pyflakes'])
     self.assertFalse(errors)
Exemple #47
0
def code_check():
    """Run pylama and check current file.

    :return bool:

    """
    with silence_stderr():

        from pylama.core import run
        from pylama.config import parse_options

        if not env.curbuf.name:
            return env.stop()

        linters = env.var('g:pymode_lint_checkers')
        env.debug(linters)

        # Fixed in v0.9.3: these two parameters may be passed as strings.
        # DEPRECATE: v:0.10.0: need to be set as lists.
        if isinstance(env.var('g:pymode_lint_ignore'), str):
            raise ValueError ('g:pymode_lint_ignore should have a list type')
        else:
            ignore = env.var('g:pymode_lint_ignore')
        if isinstance(env.var('g:pymode_lint_select'), str):
            raise ValueError ('g:pymode_lint_select should have a list type')
        else:
            select = env.var('g:pymode_lint_select')
        options = parse_options(
            linters=linters, force=1,
            ignore=ignore,
            select=select,
        )
        env.debug(options)

        for linter in linters:
            opts = env.var('g:pymode_lint_options_%s' % linter, silence=True)
            if opts:
                options.linters_params[linter] = options.linters_params.get(
                    linter, {})
                options.linters_params[linter].update(opts)

        path = os.path.relpath(env.curbuf.name, env.curdir)
        env.debug("Start code check: ", path)

        if getattr(options, 'skip', None) and any(p.match(path) for p in options.skip):  # noqa
            env.message('Skip code checking.')
            env.debug("Skipped")
            return env.stop()

        if env.options.get('debug'):
            from pylama.core import LOGGER, logging
            LOGGER.setLevel(logging.DEBUG)

        errors = run(path, code='\n'.join(env.curbuf) + '\n', options=options)

    env.debug("Find errors: ", len(errors))
    sort_rules = env.var('g:pymode_lint_sort')

    def __sort(e):
        try:
            return sort_rules.index(e.get('type'))
        except ValueError:
            return 999

    if sort_rules:
        env.debug("Find sorting: ", sort_rules)
        errors = sorted(errors, key=__sort)

    for e in errors:
        e._info['bufnr'] = env.curbuf.number
        if e._info['col'] is None:
            e._info['col'] = 1

    env.run('g:PymodeLocList.current().extend', [e._info for e in errors])
Exemple #48
0
def test_pep257():
    options = parse_options(linters=['pep257'])
    errors = run('dummy.py', options=options)
    assert errors
Exemple #49
0
def worker(params):
    """Do work."""
    path, code, options, rootdir = params
    return run(path, code=code, rootdir=rootdir, options=options)