Exemple #1
0
def test_correct_except_case(
    assert_errors,
    parse_ast_tree,
    code,
    default_options,
):
    """Violations are not raised when using correct except case."""
    tree = parse_ast_tree(code)

    visitor = WrongTryExceptVisitor(default_options, tree=tree)
    visitor.run()

    assert_errors(visitor, [])
Exemple #2
0
def test_duplicate_exceptions(
    assert_errors,
    parse_ast_tree,
    code,
    default_options,
):
    """Duplicate exception classes should raise a violation."""
    tree = parse_ast_tree(code)

    visitor = WrongTryExceptVisitor(default_options, tree=tree)
    visitor.run()

    assert_errors(visitor, [DuplicateExceptionViolation])
def test_correct_return_path_in_try_except(
    assert_errors,
    parse_ast_tree,
    code,
    default_options,
):
    """Violations are not raised when `return` path is correct."""
    tree = parse_ast_tree(code)

    visitor = WrongTryExceptVisitor(default_options, tree=tree)
    visitor.run()

    assert_errors(visitor, [])
def test_correct_finally(
    assert_errors,
    parse_ast_tree,
    code,
    default_options,
):
    """Violations are not raised when finally with except in try block."""
    tree = parse_ast_tree(code)

    visitor = WrongTryExceptVisitor(default_options, tree=tree)
    visitor.run()

    assert_errors(visitor, [])
Exemple #5
0
def test_useless_except_case(
    assert_errors,
    parse_ast_tree,
    code,
    default_options,
):
    """Violations are raised when using wrong except case."""
    tree = parse_ast_tree(code)

    visitor = WrongTryExceptVisitor(default_options, tree=tree)
    visitor.run()

    assert_errors(visitor, [UselessExceptCaseViolation])
Exemple #6
0
def test_correct_exceptions(
    assert_errors,
    parse_ast_tree,
    code,
    default_options,
):
    """Violations without duplicates."""
    tree = parse_ast_tree(code)

    visitor = WrongTryExceptVisitor(default_options, tree=tree)
    visitor.run()

    assert_errors(visitor, [])
def test_use_exception(
    assert_errors,
    parse_ast_tree,
    code,
    default_options,
):
    """Testing that `except Exception:` and `except:` are allowed."""
    tree = parse_ast_tree(code)

    visitor = WrongTryExceptVisitor(default_options, tree=tree)
    visitor.run()

    assert_errors(visitor, [])
def test_use_base_exception(
    assert_errors,
    parse_ast_tree,
    code,
    default_options,
):
    """Testing that `except BaseException:` is restricted."""
    tree = parse_ast_tree(code)

    visitor = WrongTryExceptVisitor(default_options, tree=tree)
    visitor.run()

    assert_errors(visitor, [BaseExceptionViolation])
def test_wrong_finally(
    assert_errors,
    parse_ast_tree,
    code,
    default_options,
):
    """Violations are raised when finally without except in try block."""
    tree = parse_ast_tree(code)

    visitor = WrongTryExceptVisitor(default_options, tree=tree)
    visitor.run()

    assert_errors(visitor, [RedundantFinallyViolation])
def test_wrong_return_in_else_or_finally(
    assert_errors,
    parse_ast_tree,
    code,
    default_options,
):
    """Violations are raised when there are multiple `return` path."""
    tree = parse_ast_tree(code)

    visitor = WrongTryExceptVisitor(default_options, tree=tree)
    visitor.run()

    assert_errors(visitor, [TryExceptMultipleReturnPathViolation])
def test_wrong_order_exception(
    assert_errors,
    parse_ast_tree,
    code,
    default_options,
    statements,
):
    """Testing incorrect order of exceptions."""
    tree = parse_ast_tree(code.format(*statements))

    visitor = WrongTryExceptVisitor(default_options, tree=tree)
    visitor.run()

    assert_errors(visitor, [IncorrectExceptOrderViolation])
Exemple #12
0
def test_different_nodes_trigger_violation(
    assert_errors,
    parse_ast_tree,
    default_options,
    mode,
    statements,
):
    """Violations are raised when there are multiple return path."""
    tree = parse_ast_tree(mode(all_nodes.format(*statements)))

    visitor = WrongTryExceptVisitor(default_options, tree=tree)
    visitor.run()

    assert_errors(visitor, [TryExceptMultipleReturnPathViolation])
def test_correct_order_exception(
    assert_errors,
    parse_ast_tree,
    code,
    default_options,
    statements,
):
    """Testing restrictions are not raised when use correct oder of `except`."""
    tree = parse_ast_tree(code.format(*statements))

    visitor = WrongTryExceptVisitor(default_options, tree=tree)
    visitor.run()

    assert_errors(visitor, [])
Exemple #14
0
def test_correct_return_path_in_try_except(
    assert_errors,
    parse_ast_tree,
    code,
    statement,
    default_options,
    mode,
):
    """Violations are not raised when return path is correct."""
    tree = parse_ast_tree(mode(code.format(statement)))

    visitor = WrongTryExceptVisitor(default_options, tree=tree)
    visitor.run()

    assert_errors(visitor, [], ignored_types=UselessExceptCaseViolation)
def test_finally_with_continue(
    assert_errors,
    parse_ast_tree,
    code,
    statement,
    default_options,
    mode,
):
    """Testing that `continue` keyword is not allowed in `finally`."""
    tree = parse_ast_tree(mode(code.format(statement)))

    visitor = WrongTryExceptVisitor(default_options, tree=tree)
    visitor.run()

    assert_errors(visitor, [LoopControlFinallyViolation])
def test_right_finally(
    assert_errors,
    parse_ast_tree,
    code,
    statement,
    default_options,
    mode,
):
    """Testing that regular loops and loops with `try` are allowed."""
    tree = parse_ast_tree(mode(code.format(statement)))

    visitor = WrongTryExceptVisitor(default_options, tree=tree)
    visitor.run()

    assert_errors(visitor, [])
Exemple #17
0
def test_wrong_return_in_else_or_finally(
    assert_errors,
    parse_ast_tree,
    code,
    statement,
    default_options,
    mode,
):
    """Violations are raised when there are multiple return path."""
    tree = parse_ast_tree(mode(code.format(statement)))

    visitor = WrongTryExceptVisitor(default_options, tree=tree)
    visitor.run()

    assert_errors(
        visitor,
        [TryExceptMultipleReturnPathViolation],
        ignored_types=UselessExceptCaseViolation,
    )