Exemplo n.º 1
0
def test_not_passing_rules_with_import_from() -> None:
    """
    Test result with a set rules that accept files.
    """
    # Given
    dep_rules = {
        "module": [ModuleWildcard("module%"), ModuleWildcard("amodule.submodule")]
    }
    configuration = Configuration(dep_rules)
    source_file = SourceFile(
        Module("module"), SourceCode("from amodule import othermodule, submodule\n")
    )
    report_printer = Mock()
    use_case = CheckDependenciesUC(
        configuration, report_printer, PARSER, iter([source_file])
    )

    # When
    with pytest.raises(ForbiddenDepencyError):
        use_case.run()

    # Then
    assert set(report_printer.print_report.call_args[0][0]) == set(
        (
            DependencyError(
                Module("module"),
                Module("amodule.othermodule"),
                tuple(sorted(dep_rules["module"])),
            ),
        )
    )
Exemplo n.º 2
0
    def test_local_import_case() -> None:
        """
        Test code with local import case.
        """
        # Given
        module = Module("module.toto")
        source_file = SourceFile(module=module, code=SourceCode(_LOCAL_CASE))

        # When
        dependencies = get_dependencies(source_file, PARSER)

        # Then
        assert dependencies == _LOCAL_RESULT
Exemplo n.º 3
0
    def test_simple_case() -> None:
        """
        Test simple code case.
        """
        # Given
        module = Module("toto_program")
        source_file = SourceFile(module=module, code=SourceCode(_SIMPLE_CASE))

        # When
        dependencies = get_dependencies(source_file, PARSER)

        # Then
        assert dependencies == _SIMPLE_RESULT
Exemplo n.º 4
0
def test_empty() -> None:
    """
    Test empty code case.
    """
    # Given
    module = Module("")
    source_code = SourceCode("")
    source_file = SourceFile(module=module, code=source_code)

    # When
    dependencies = get_dependencies(source_file, PARSER)

    # Then
    assert dependencies == frozenset()
Exemplo n.º 5
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.º 6
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.º 7
0
def source_file_iterator(root_dir: str,
                         file_extension: str) -> Iterator[SourceFile]:
    """
    Iterator of all python source files in a directory.
    """
    if file_extension == "py":
        project_root = _get_python_project_root(root_dir)
        separator = "."
    elif file_extension == "go":
        project_root = ""
        separator = "/"

    with _change_dir(root_dir):
        for file_path in Path(".").rglob(f"*.{file_extension}"):
            with open(str(file_path), "r") as stream:
                content = stream.read()
            yield SourceFile(
                Module(project_root +
                       _get_module_from_file_path(file_path, separator)),
                SourceCode(content),
            )
Exemplo n.º 8
0
def test_passing_rules_with_import_from() -> None:
    """
    Test result with a set rules that accept files.
    """
    # Given
    configuration = Configuration(
        dependency_rules={
            "module": [ModuleWildcard("module%"), ModuleWildcard("amodule.submodule")]
        }
    )
    source_file = SourceFile("module", SourceCode("from amodule import submodule"))
    report_printer = Mock()
    use_case = CheckDependenciesUC(
        configuration, report_printer, PARSER, iter([source_file])
    )

    # When
    use_case.run()

    # Then
    assert report_printer.print_report.call_args[0][0] == []
Exemplo n.º 9
0
from dep_check.models import Dependency, Module, SourceCode, SourceFile

SIMPLE_FILE = SourceFile(
    module=Module("simple_module"),
    code=SourceCode(
        """
import module
import module.inside.module
from amodule import aclass
"""
    ),
)
FILE_WITH_LOCAL_IMPORT = SourceFile(
    module=Module("amodule.local_module"),
    code=SourceCode(
        """
import module
import module.inside.module
from . import aclass
from .inside import aclass
"""
    ),
)
FILE_WITH_STD_IMPORT = SourceFile(
    module=Module("amodule.std_module"),
    code=SourceCode(
        """
import module
import module.inside.module
import itertools
from abc import ABC