예제 #1
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
예제 #2
0
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
예제 #3
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)
예제 #4
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)
예제 #5
0
def test_render_broken_contract():
    settings.configure(PRINTER=FakePrinter())
    contract = IndependenceContract(
        name="Independence contract",
        session_options={"root_packages": ["mypackage"]},
        contract_options={
            "modules":
            ["mypackage.blue", "mypackage.green", "mypackage.yellow"]
        },
    )
    check = ContractCheck(
        kept=False,
        metadata={
            "invalid_chains": [
                {
                    "upstream_module":
                    "mypackage.yellow",
                    "downstream_module":
                    "mypackage.blue",
                    "chains": [
                        [
                            {
                                "importer": "mypackage.blue.foo",
                                "imported": "mypackage.utils.red",
                                "line_numbers": (16, 102),
                            },
                            {
                                "importer": "mypackage.utils.red",
                                "imported": "mypackage.utils.brown",
                                "line_numbers": (1, ),
                            },
                            {
                                "importer": "mypackage.utils.brown",
                                "imported": "mypackage.yellow.bar",
                                "line_numbers": (3, ),
                            },
                        ],
                        [{
                            "importer": "mypackage.blue.bar",
                            "imported": "mypackage.yellow.baz",
                            "line_numbers": (5, ),
                        }],
                    ],
                },
                {
                    "upstream_module":
                    "mypackage.green",
                    "downstream_module":
                    "mypackage.yellow",
                    "chains": [[{
                        "importer": "mypackage.yellow.foo",
                        "imported": "mypackage.green.bar",
                        "line_numbers": (15, ),
                    }]],
                },
            ]
        },
    )

    contract.render_broken_contract(check)

    settings.PRINTER.pop_and_assert("""
        mypackage.blue is not allowed to import mypackage.yellow:

        -   mypackage.blue.foo -> mypackage.utils.red (l.16, l.102)
            mypackage.utils.red -> mypackage.utils.brown (l.1)
            mypackage.utils.brown -> mypackage.yellow.bar (l.3)

        -   mypackage.blue.bar -> mypackage.yellow.baz (l.5)


        mypackage.yellow is not allowed to import mypackage.green:

        -   mypackage.yellow.foo -> mypackage.green.bar (l.15)


        """)