Exemplo n.º 1
0
def test_fold_module(source_files) -> None:
    """
    Test result with a set source files and a module to fold.
    """
    # Given
    conf_graph = {"fold_modules": ["amodule"]}
    drawer = Mock()
    use_case = DrawGraphUC(drawer, PARSER, source_files, conf_graph)

    # When
    use_case.run()

    # Then
    drawer.write.assert_called()  # type: ignore
    global_dep = drawer.write.call_args[0][0]

    assert global_dep == {
        "simple_module":
        set((
            Dependency(Module("module")),
            Dependency(Module("module.inside.module")),
            Dependency(Module("amodule")),
        )),
        "amodule":
        set((Dependency(Module("module")),
             Dependency(Module("module.inside.module")))),
    }
Exemplo n.º 2
0
def test_pop_empty_module_from_dependencies(source_files) -> None:
    # Given
    drawer = Mock()
    config = {"hide_modules": ["module"]}
    use_case = DrawGraphUC(drawer, PARSER, source_files, config)

    # When
    use_case.run()

    # Then
    drawer.write.assert_called_with({
        "simple_module":
        set((Dependency(Module("amodule")), )),
        "amodule.local_module":
        set((Dependency(Module("amodule")),
             Dependency(Module("amodule.inside")))),
    })
Exemplo n.º 3
0
    def find_dependencies(self, source_file: SourceFile) -> Dependencies:
        if not source_file.code:
            return set()
        deps_string = goparse.find_dependencies(source_file.code)
        deps_list = deps_string.replace('"', "").split(";")[:-1]

        dependecies: Dependencies = {Dependency(dep) for dep in deps_list}
        return dependecies
Exemplo n.º 4
0
def test_hide_nominal(source_files) -> None:
    # Given
    drawer = Mock()
    config = {"hide_modules": ["amodule"]}
    use_case = DrawGraphUC(drawer, PARSER, source_files, config)

    # When
    use_case.run()

    # Then
    drawer.write.assert_called()  # type: ignore
    global_dep = drawer.write.call_args[0][0]

    assert global_dep == {
        "simple_module":
        set((Dependency(Module("module")),
             Dependency(Module("module.inside.module"))))
    }
Exemplo n.º 5
0
    def visit(self, node: Any) -> None:
        modules: FrozenSet[Dependency] = frozenset()
        if isinstance(node, ast.Import):
            modules = frozenset(
                Dependency(Module(alias.name)) for alias in node.names)

        elif isinstance(node, ast.ImportFrom):
            module = Module(node.module or "")
            if node.level:
                parent_module = ".".join(
                    self.current_module_parts[:-node.level])
                if node.module:
                    module = Module("{}.{}".format(parent_module, node.module))
                else:
                    module = Module(parent_module)
            modules = frozenset((Dependency(module), ))
        self._dependencies |= modules

        super().visit(node)
Exemplo n.º 6
0
    def find_dependencies(self, source_file: SourceFile) -> Dependencies:
        if source_file.code == "":
            return set()
        deps_string = self.lib.FindDependencies(
            GoString(source_file.code.encode(), len(source_file.code)))
        deps_list = deps_string.data.decode("utf-8").replace(
            '"', "").split(";")[:-1]

        dependecies: Dependencies = {Dependency(dep) for dep in deps_list}
        return dependecies
Exemplo n.º 7
0
def test_nominal() -> None:
    source_file = SourceFile(
        Module("string.string"),
        SourceCode("""package main
    import "fmt"
    import (
        "go/parser"
        "go/module"
        "othermodule"
    )
    import "amodule"
    """),
    )

    assert get_dependencies(source_file, PARSER) == {
        Dependency("fmt"),
        Dependency("go/parser"),
        Dependency("go/module"),
        Dependency("othermodule"),
        Dependency("amodule"),
    }
Exemplo n.º 8
0
def test_fold_dep() -> None:
    """
    Test result of _fold_dep function with an empty fold module
    """
    # Given
    global_dep = GLOBAL_DEPENDENCIES
    fold_module = Module("amodule")

    # When
    global_dep = _fold_dep(global_dep, fold_module)

    # Then
    assert global_dep == {
        "simple_module":
        set((
            Dependency(Module("module")),
            Dependency(Module("module.inside.module")),
            Dependency(Module("amodule")),
        )),
        "amodule":
        set((
            Dependency(Module("module")),
            Dependency(Module("module.inside.module")),
            Dependency(Module("amodule")),
        )),
    }
Exemplo n.º 9
0
def test_empty() -> None:
    """
    Test empty rules case.
    """
    # Given
    dependency = Dependency(Module("toto"))
    authorized_modules: List[ModuleWildcard] = []

    # When
    with raises(NotAllowedDependencyException) as error:
        check_dependency(PARSER, dependency, authorized_modules)

    # Then
    assert error
    assert error.value.dependency == dependency.main_import
    assert error.value.authorized_modules == authorized_modules
Exemplo n.º 10
0
def _fold_dep(
    global_dep: GlobalDependencies, fold_module: Module
) -> GlobalDependencies:

    fold_global_dep: GlobalDependencies = defaultdict(set)
    for module, deps in global_dep.items():
        new_deps: Dependencies = set()
        if module.startswith(tuple((f"{fold_module}.", f"{fold_module}/"))):
            module = fold_module
        fold_dep = set(
            Dependency(fold_module) if dep.main_import.startswith(fold_module) else dep
            for dep in deps
        )
        new_deps |= fold_dep
        fold_global_dep[module] |= new_deps

    return fold_global_dep
Exemplo n.º 11
0
def test_not_passing_case() -> None:
    """
    Test a not passing case.
    """
    # Given
    dependency = Dependency(Module("toto.tata"))
    rules: Rules = [
        (ModuleWildcard("toto.*"), ModuleWildcard("toto")),
        (ModuleWildcard("toto.*"), ModuleWildcard("te.*")),
        (ModuleWildcard("toto.*"), ModuleWildcard("titi\\.tata")),
    ]

    # When
    with raises(NotAllowedDependencyException) as error:
        check_dependency(PARSER, dependency, rules)

    # Then
    assert error
    assert error.value.dependency == dependency.main_import
    assert error.value.authorized_modules == [r for _, r in rules]
Exemplo n.º 12
0
def test_passing_case() -> None:
    """
    Test a passing case.
    """
    # Given
    dependency = Dependency(Module("toto"))
    rules: Rules = [
        (ModuleWildcard("toto"), ModuleWildcard("to*")),
        (ModuleWildcard("toto"), ModuleWildcard("titi.tata")),
    ]

    # When
    error = None
    try:
        check_dependency(PARSER, dependency, rules)
    except NotAllowedDependencyException as exception:
        error = exception

    # Then
    assert not error
Exemplo n.º 13
0
    def test_multi_imports_after_from() -> None:
        # Given
        module = Module("module.toto")
        source_file = SourceFile(
            module=module,
            code=SourceCode(
                "from module import amodule, othermodule, moduleagain"),
        )

        # When
        dependencies = get_import_from_dependencies(source_file, PARSER)

        # Then
        assert dependencies == set((Dependency(
            Module("module"),
            frozenset((
                Module("amodule"),
                Module("othermodule"),
                Module("moduleagain"),
            )),
        ), ))
Exemplo n.º 14
0
FILE_WITH_STD_IMPORT = SourceFile(
    module=Module("amodule.std_module"),
    code=SourceCode(
        """
import module
import module.inside.module
import itertools
from abc import ABC
"""
    ),
)

GLOBAL_DEPENDENCIES = {
    "simple_module": set(
        (
            Dependency(Module("module")),
            Dependency(Module("module.inside.module")),
            Dependency(Module("amodule")),
        )
    ),
    "amodule.local_module": set(
        (
            Dependency(Module("module")),
            Dependency(Module("module.inside.module")),
            Dependency(Module("amodule")),
            Dependency(Module("amodule.inside")),
        )
    ),
    "amodule.std_module": set(
        (Dependency(Module("module")), Dependency(Module("module.inside.module")))
    ),
Exemplo n.º 15
0
"""
Tests about get_dependencies function.
"""
import re

from dep_check.dependency_finder import get_dependencies, get_import_from_dependencies
from dep_check.infra.python_parser import PythonParser
from dep_check.models import Dependency, Module, ModuleWildcard, SourceCode, SourceFile

_SIMPLE_CASE = """
import simple
import module.inside.module
from amodule import aclass
"""
_SIMPLE_RESULT = frozenset((
    Dependency(Module("simple")),
    Dependency(Module("amodule")),
    Dependency(Module("module.inside.module")),
))
_SIMPLE_RESULT_IMPORT_FROM = frozenset((
    Dependency(Module("simple")),
    Dependency(Module("amodule"), frozenset((Module("aclass"), ))),
    Dependency(Module("module.inside.module")),
))

_LOCAL_CASE = """
import simple
from . import aclass
from .inside.module import aclass
"""
_LOCAL_RESULT = set((