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, [])
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, [])
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_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_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])
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_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_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_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_duplicate_exceptions( assert_errors, assert_error_text, 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])