Exemple #1
0
def test_missing_module():
    graph = ImportGraph()
    for module in ("mypackage", "mypackage.foo"):
        graph.add_module(module)

    contract = IndependenceContract(
        name="Independence contract",
        session_options={"root_packages": ["mypackage"]},
        contract_options={"modules": ["mypackage.foo", "mypackage.bar"]},
    )

    with pytest.raises(ValueError,
                       match=("Module 'mypackage.bar' does not exist.")):
        contract.check(graph=graph)
Exemple #2
0
def test_ignore_imports(ignore_imports, is_kept):
    graph = ImportGraph()
    graph.add_module("mypackage")
    graph.add_import(importer="mypackage.a",
                     imported="mypackage.irrelevant",
                     line_number=1,
                     line_contents="-")
    graph.add_import(importer="mypackage.a",
                     imported="mypackage.indirect",
                     line_number=1,
                     line_contents="-")
    graph.add_import(importer="mypackage.indirect",
                     imported="mypackage.b",
                     line_number=1,
                     line_contents="-")
    contract = IndependenceContract(
        name="Independence contract",
        session_options={"root_packages": ["mypackage"]},
        contract_options={
            "modules": ("mypackage.a", "mypackage.b"),
            "ignore_imports": ignore_imports,
        },
    )

    contract_check = contract.check(graph=graph)

    assert is_kept == contract_check.kept
def test_ignore_imports_tolerates_duplicates():
    graph = ImportGraph()
    graph.add_module("mypackage")
    graph.add_import(importer="mypackage.a",
                     imported="mypackage.b",
                     line_number=1,
                     line_contents="-")
    graph.add_import(importer="mypackage.a",
                     imported="mypackage.c",
                     line_number=2,
                     line_contents="-")
    contract = IndependenceContract(
        name="Independence contract",
        session_options={"root_packages": ["mypackage"]},
        contract_options={
            "modules": ("mypackage.a", "mypackage.b"),
            "ignore_imports": [
                "mypackage.a -> mypackage.b",
                "mypackage.a -> mypackage.c",
                "mypackage.a -> mypackage.b",
            ],
        },
    )

    contract_check = contract.check(graph=graph)

    assert contract_check.kept
Exemple #4
0
 def _check_default_contract(self, graph):
     contract = IndependenceContract(
         name="Independence contract",
         session_options={"root_packages": ["mypackage"]},
         contract_options={
             "modules":
             ("mypackage.blue", "mypackage.green", "mypackage.yellow")
         },
     )
     return contract.check(graph=graph)