Beispiel #1
0
def test_error_on_unused(source_files) -> None:
    """
    Test result with a set rules that accept files.
    """
    # Given
    configuration = Configuration(
        dependency_rules={
            SIMPLE_FILE.module: [ModuleWildcard("module%"), ModuleWildcard("amodule")],
            "amodule.*": [
                ModuleWildcard("module"),
                ModuleWildcard("module.inside.*"),
                ModuleWildcard("amodule%"),
                ModuleWildcard("unused%"),
            ],
            "unused_wildcard.*": [ModuleWildcard("unused%")],
        },
        error_on_unused=True,
    )
    report_printer = Mock()
    use_case = CheckDependenciesUC(configuration, report_printer, PARSER, source_files)

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

    # Then
    report_printer.print_report.assert_called_with(
        [],
        {
            (ModuleWildcard("amodule.*"), ModuleWildcard("unused%")),
            (ModuleWildcard("unused_wildcard.*"), ModuleWildcard("unused%")),
        },
        3,
    )
Beispiel #2
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"])),
            ),
        )
    )
Beispiel #3
0
def test_passing_rules(source_files) -> None:
    """
    Test result with a set rules that accept files.
    """
    # Given
    configuration = Configuration(
        dependency_rules={
            SIMPLE_FILE.module:
            [ModuleWildcard("module%"),
             ModuleWildcard("amodule")],
            "amodule.*": [
                ModuleWildcard("module"),
                ModuleWildcard("module.inside.*"),
                ModuleWildcard("amodule%"),
            ],
        })
    report_printer = Mock()
    use_case = CheckDependenciesUC(configuration, report_printer, PARSER,
                                   source_files)

    # When
    use_case.run()

    # Then
    report_printer.print_report.assert_called_with([], set(), 3)
Beispiel #4
0
def test_empty_rules(source_files) -> None:
    """
    Test result with no rule given.
    """
    # Given
    configuration = Configuration()
    report_printer = Mock()
    use_case = CheckDependenciesUC(configuration, report_printer, PARSER,
                                   source_files)

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

    # Then
    assert set(report_printer.print_report.call_args[0][0]) == set(
        (
            DependencyError(SIMPLE_FILE.module, Module("module"), tuple()),
            DependencyError(SIMPLE_FILE.module, Module("module.inside.module"),
                            tuple()),
            DependencyError(SIMPLE_FILE.module, Module("amodule.aclass"),
                            tuple()),
            DependencyError(FILE_WITH_LOCAL_IMPORT.module, Module("module"),
                            tuple()),
            DependencyError(FILE_WITH_LOCAL_IMPORT.module,
                            Module("module.inside.module"), tuple()),
            DependencyError(FILE_WITH_LOCAL_IMPORT.module,
                            Module("amodule.aclass"), tuple()),
            DependencyError(FILE_WITH_LOCAL_IMPORT.module,
                            Module("amodule.inside.aclass"), tuple()),
            DependencyError(FILE_WITH_STD_IMPORT.module, Module("module"),
                            tuple()),
            DependencyError(FILE_WITH_STD_IMPORT.module,
                            Module("module.inside.module"), tuple()),
        ))
Beispiel #5
0
def test_not_passing_rules(source_files) -> None:
    """
    Test result with a set rules that not accept files.
    """
    # Given
    dep_rules = {
        "simple_module": [ModuleWildcard("module.*"), ModuleWildcard("amodule")],
        "amodule.local_module": [
            ModuleWildcard("module"),
            ModuleWildcard("module.inside.*"),
            ModuleWildcard("amod"),
        ],
        "amodule.std_module": [ModuleWildcard("mod")],
    }

    configuration = Configuration(dependency_rules=dep_rules)
    report_printer = Mock()
    use_case = CheckDependenciesUC(configuration, report_printer, PARSER, source_files)

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

    # Then
    simple = SIMPLE_FILE.module
    local = FILE_WITH_LOCAL_IMPORT.module
    std = FILE_WITH_STD_IMPORT.module

    assert set(report_printer.print_report.call_args[0][0]) == set(
        (
            DependencyError(
                simple, Module("module"), tuple(sorted(dep_rules["simple_module"]))
            ),
            DependencyError(
                local,
                Module("amodule.aclass"),
                tuple(sorted(dep_rules["amodule.local_module"])),
            ),
            DependencyError(
                local,
                Module("amodule.inside.aclass"),
                tuple(sorted(dep_rules["amodule.local_module"])),
            ),
            DependencyError(
                std, Module("module"), tuple(sorted(dep_rules["amodule.std_module"]))
            ),
            DependencyError(
                std,
                Module("module.inside.module"),
                tuple(sorted(dep_rules["amodule.std_module"])),
            ),
        )
    )
Beispiel #6
0
def test_empty() -> None:
    """
    Test result with no source files given.
    """
    # Given
    source_files: Iterator[SourceFile] = iter([])
    dependencies_writer = Mock()
    use_case = BuildConfigurationUC(dependencies_writer, PARSER, source_files)

    # When
    use_case.run()

    # Then
    dependencies_writer.write.assert_called_with(Configuration())
Beispiel #7
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] == []
Beispiel #8
0
    def read(self) -> Configuration:
        if self.config_path == "-":
            return Configuration(**yaml.safe_load(stdin))

        with open(self.config_path, encoding="utf-8") as stream:
            return Configuration(**yaml.safe_load(stream))