示例#1
0
def test_no_exceptions(
    source_code,
    default_options,
    parse_ast_tree,
    parse_tokens,
):
    """
    This testcase is a complex example of magic.

    We use property based-test to generate python programs for us.
    And then we ensure that our linter does not crash on arbitary input.
    """
    try:
        tree = parse_ast_tree(str(source_code.encode('utf-8-sig')))
    except (UnicodeEncodeError, SyntaxError):
        reject()
        raise

    lines = io.StringIO(source_code)
    tokens = list(tokenize.generate_tokens(lambda: next(lines)))

    Checker.parse_options(default_options)
    checker = Checker(tree, tokens)

    for violation in checker.run():
        assert isinstance(violation[0], int)
        assert isinstance(violation[1], int)
        assert violation[2].startswith('WPS'), violation[2]
        assert 'WPS0' not in violation[2]
        assert violation[3] == Checker
示例#2
0
def test_module_names(filename, error, default_options):
    """Ensures that checker works with module names."""
    Checker.parse_options(default_options)
    checker = Checker(tree=ast.parse(''), file_tokens=[], filename=filename)
    _line, _col, error_text, _type = next(checker.run())

    assert int(error_text[1:4]) == error.code
示例#3
0
def test_module_names(filename, error, default_options):
    """Ensures that checker works with module names."""
    module = ast.parse('')
    Checker.parse_options(default_options)
    checker = Checker(tree=module, file_tokens=[], filename=filename)
    _, _, error_text, _ = next(checker.run())
    error_code = int(error_text[1:4])

    assert error_code == error.code
示例#4
0
def test_regression112(default_options):
    """
    There was a conflict between ``pyflakes`` and our plugin.

    We were fighting for ``parent`` property.
    Now we use a custom prefix.

    See: https://github.com/wemake-services/wemake-python-styleguide/issues/112
    """
    module = ast.parse(code_that_brakes)
    Checker.parse_options(default_options)
    Checker(tree=module, file_tokens=[], filename='custom.py')

    # It was failing on this line:
    # AttributeError: 'ExceptHandler' object has no attribute 'depth'
    flakes = PyFlakesChecker(module)

    assert flakes.root
示例#5
0
def test_exception_handling(
    default_options,
    capsys,
):
    """Ensures that checker works with module names."""
    Checker.parse_options(default_options)
    checker = Checker(tree=ast.parse(''), file_tokens=[], filename='test.py')
    checker._visitors = [_BrokenVisitor]  # noqa: WPS437

    with suppress(StopIteration):
        next(checker.run())

    captured = capsys.readouterr()
    assert 'ValueError: Message from visitor' in captured.out