def test_incorrect_import(assert_errors, parse_ast_tree, code,
                          default_options):
    """Testing that imports from protected modules are restricted."""
    tree = parse_ast_tree(code)
    visitor = WrongImportVisitor(default_options, tree=tree)
    visitor.run()
    assert_errors(visitor, [ProtectedModuleViolation])
def test_nested_import(assert_errors, parse_ast_tree, code, default_options):
    """Testing that nested imports are restricted."""
    tree = parse_ast_tree(code)

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

    assert_errors(visitor, [NestedImportViolation])
def test_regular_imports(assert_errors, parse_ast_tree, code, default_options):
    """Testing that regular imports are allowed."""
    tree = parse_ast_tree(code)

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

    assert_errors(visitor, [])
def test_correct_future_import(
    assert_errors, parse_ast_tree, code, to_import, default_options,
):
    """Testing that some future imports are not restricted."""
    tree = parse_ast_tree(code.format(to_import), do_compile=False)

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

    assert_errors(visitor, [])
def test_wrong_multiple_future_import(
    assert_errors,
    parse_ast_tree,
    default_options,
):
    """Testing that multiple future imports are restricted."""
    tree = parse_ast_tree(
        'from __future__ import print_function, unicode_literals', )

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

    assert_errors(visitor, [FutureImportViolation, FutureImportViolation])
def test_correct_imports(
    assert_errors,
    parse_ast_tree,
    code,
    default_options,
):
    """Testing that no colliding imports are allowed."""
    tree = parse_ast_tree(code)

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

    assert_errors(visitor, [], ignored_types=LocalFolderImportViolation)
Esempio n. 7
0
def test_incorrect_module_members_import(
    assert_errors,
    parse_ast_tree,
    code,
    default_options,
):
    """Testing that importing of protected objects is restricted."""
    tree = parse_ast_tree(code)

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

    assert_errors(visitor, [ProtectedModuleMemberViolation])
def test_local_folder_import(
    assert_errors,
    parse_ast_tree,
    code,
    default_options,
):
    """Testing that relative to local folder imports are restricted."""
    tree = parse_ast_tree(code)

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

    assert_errors(visitor, [LocalFolderImportViolation])
Esempio n. 9
0
def test_vague_alias_import(
    assert_errors,
    parse_ast_tree,
    alias,
    default_options,
):
    """Testing that imports with the same aliases are restricted."""
    tree = parse_ast_tree(alias_import_template.format(alias))

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

    assert_errors(visitor, [VagueImportViolation])
def test_regular_imports(assert_errors, parse_ast_tree, code, default_options):
    """
    Testing imports that are allowed.

    Regular imports are allowed.
    Imports nested inside the TYPE_CHECKING check are allowed.
    """
    tree = parse_ast_tree(code)

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

    assert_errors(visitor, [])
Esempio n. 11
0
def test_same_alias_import(
    assert_errors,
    parse_ast_tree,
    code,
    default_options,
):
    """Testing that imports with the same aliases are restricted."""
    tree = parse_ast_tree(code.format('os'))

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

    assert_errors(visitor, [SameAliasImportViolation])
def test_regular_from_import(
    assert_errors,
    parse_ast_tree,
    code,
    to_import,
    default_options,
):
    """Testing that dotted `from` imports are allowed."""
    tree = parse_ast_tree(code.format(to_import))

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

    assert_errors(visitor, [])
def test_correct_flat_import(
    assert_errors,
    parse_ast_tree,
    code,
    to_import,
    default_options,
):
    """Testing that flat raw imports are allowed."""
    tree = parse_ast_tree(code.format(to_import))

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

    assert_errors(visitor, [])
def test_regular_import(
    assert_errors,
    parse_ast_tree,
    code,
    import_name,
    default_options,
):
    """Testing that regular imports are ok."""
    tree = parse_ast_tree(code.format(import_name))

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

    assert_errors(visitor, [])
Esempio n. 15
0
def test_wrong_future_import(
    assert_errors,
    parse_ast_tree,
    code,
    to_import,
    default_options,
):
    """Testing that future imports are restricted."""
    tree = parse_ast_tree(code.format(to_import))

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

    assert_errors(visitor, [FutureImportViolation])
def test_vague_method_name_import(
    assert_errors,
    parse_ast_tree,
    code,
    import_name,
    default_options,
):
    """Testing that vague imports are restricted."""
    tree = parse_ast_tree(code.format(import_name))

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

    assert_errors(visitor, [VagueImportViolation])
def test_fixed_vague_method_name_import(
    assert_errors,
    parse_ast_tree,
    code,
    import_name,
    default_options,
):
    """Testing that some imports are not reported as vague."""
    tree = parse_ast_tree(code.format(import_name))

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

    assert_errors(visitor, [])
def test_other_alias_name(
    assert_errors,
    parse_ast_tree,
    code,
    to_import,
    default_options,
):
    """Testing that imports with other aliases are allowed."""
    tree = parse_ast_tree(code.format(to_import))

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

    assert_errors(visitor, [])
def test_same_alias_import_without_control(
    assert_errors,
    parse_ast_tree,
    code,
    options,
):
    """Testing that imports with the same aliases are restricted."""
    same_alias = 'os'
    tree = parse_ast_tree(code.format(same_alias))
    custom_options = options(i_control_code=False)

    visitor = WrongImportVisitor(custom_options, tree=tree)
    visitor.run()

    assert_errors(visitor, [])
def test_same_alias_import(
    assert_errors,
    assert_error_text,
    parse_ast_tree,
    code,
    default_options,
):
    """Testing that imports with the same aliases are allowed."""
    same_alias = 'os'
    tree = parse_ast_tree(code.format(same_alias))

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

    assert_errors(visitor, [])
def test_wrong_dotted_import(
    assert_errors,
    assert_error_text,
    parse_ast_tree,
    code,
    to_import,
    default_options,
):
    """Testing that dotted raw imports are restricted."""
    tree = parse_ast_tree(code.format(to_import))

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

    assert_errors(visitor, [DottedRawImportViolation])
    assert_error_text(visitor, to_import)
def test_imports_collision(
    assert_errors,
    parse_ast_tree,
    code,
    default_options,
):
    """Testing that colliding imports are restricted."""
    tree = parse_ast_tree(code)

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

    assert_errors(
        visitor,
        [ImportCollisionViolation],
        ignored_types=(DottedRawImportViolation, LocalFolderImportViolation),
    )