|
| 1 | +from grimp.application.graph import ImportGraph |
| 2 | +import pytest |
| 3 | + |
| 4 | + |
| 5 | +class TestFindCycleBreakers: |
| 6 | + def test_empty_graph(self): |
| 7 | + graph = ImportGraph() |
| 8 | + graph.add_module("pkg") |
| 9 | + |
| 10 | + result = graph.nominate_cycle_breakers("pkg") |
| 11 | + |
| 12 | + assert result == set() |
| 13 | + |
| 14 | + @pytest.mark.parametrize( |
| 15 | + "module", |
| 16 | + ( |
| 17 | + "pkg", |
| 18 | + "pkg.foo", |
| 19 | + "pkg.foo.blue", |
| 20 | + ), |
| 21 | + ) |
| 22 | + def test_graph_with_no_imports(self, module: str): |
| 23 | + graph = self._build_graph_with_no_imports() |
| 24 | + |
| 25 | + result = graph.nominate_cycle_breakers(module) |
| 26 | + |
| 27 | + assert result == set() |
| 28 | + |
| 29 | + @pytest.mark.parametrize( |
| 30 | + "module", |
| 31 | + ( |
| 32 | + "pkg", |
| 33 | + "pkg.bar", |
| 34 | + "pkg.foo.blue", |
| 35 | + "pkg.foo.green", # Leaf package. |
| 36 | + ), |
| 37 | + ) |
| 38 | + def test_acyclic_graph(self, module: str): |
| 39 | + graph = self._build_acyclic_graph() |
| 40 | + |
| 41 | + result = graph.nominate_cycle_breakers(module) |
| 42 | + |
| 43 | + assert result == set() |
| 44 | + |
| 45 | + def test_one_breaker(self): |
| 46 | + graph = self._build_acyclic_graph() |
| 47 | + importer, imported = "pkg.bar.red.four", "pkg.foo.blue.two" |
| 48 | + graph.add_import(importer=importer, imported=imported) |
| 49 | + result = graph.nominate_cycle_breakers("pkg") |
| 50 | + |
| 51 | + assert result == {(importer, imported)} |
| 52 | + |
| 53 | + def test_three_breakers(self): |
| 54 | + graph = self._build_acyclic_graph() |
| 55 | + imports = { |
| 56 | + ("pkg.bar.red.four", "pkg.foo.blue.two"), |
| 57 | + ("pkg.bar.yellow", "pkg.foo.blue.three"), |
| 58 | + ("pkg.bar", "pkg.foo.blue.three"), |
| 59 | + } |
| 60 | + for importer, imported in imports: |
| 61 | + graph.add_import(importer=importer, imported=imported) |
| 62 | + |
| 63 | + result = graph.nominate_cycle_breakers("pkg") |
| 64 | + |
| 65 | + assert result == imports |
| 66 | + |
| 67 | + def test_nominated_based_on_dependencies_rather_than_imports(self): |
| 68 | + graph = self._build_acyclic_graph() |
| 69 | + # Add lots of imports from a single module - this will be treated as |
| 70 | + # a single dependency. |
| 71 | + importer, imported = "pkg.bar.red.four", "pkg.foo.blue.two" |
| 72 | + for i in range(1, 30): |
| 73 | + graph.add_import( |
| 74 | + importer=importer, imported=imported, line_number=i, line_contents="-" |
| 75 | + ) |
| 76 | + |
| 77 | + graph.add_import(importer=importer, imported=imported) |
| 78 | + |
| 79 | + result = graph.nominate_cycle_breakers("pkg") |
| 80 | + |
| 81 | + assert result == {(importer, imported)} |
| 82 | + |
| 83 | + def test_imports_between_passed_package_and_children_are_disregarded(self): |
| 84 | + graph = self._build_acyclic_graph() |
| 85 | + parent, child = "pkg.foo.blue", "pkg.foo" |
| 86 | + graph.add_import(importer=parent, imported=child) |
| 87 | + graph.add_import(importer=child, imported=parent) |
| 88 | + |
| 89 | + result = graph.nominate_cycle_breakers(parent) |
| 90 | + |
| 91 | + assert result == set() |
| 92 | + |
| 93 | + def test_on_child_of_root(self): |
| 94 | + graph = self._build_acyclic_graph() |
| 95 | + imports = { |
| 96 | + ("pkg.bar.red.five", "pkg.bar.yellow.eight"), |
| 97 | + ("pkg.bar.red", "pkg.bar.yellow"), |
| 98 | + } |
| 99 | + for importer, imported in imports: |
| 100 | + graph.add_import(importer=importer, imported=imported) |
| 101 | + |
| 102 | + result = graph.nominate_cycle_breakers("pkg.bar") |
| 103 | + |
| 104 | + assert result == imports |
| 105 | + |
| 106 | + def test_on_grandchild_of_root(self): |
| 107 | + graph = self._build_acyclic_graph() |
| 108 | + imports = { |
| 109 | + ("pkg.bar.orange.ten.gamma", "pkg.bar.orange.nine.alpha"), |
| 110 | + ("pkg.bar.orange.ten", "pkg.bar.orange.nine.alpha"), |
| 111 | + } |
| 112 | + for importer, imported in imports: |
| 113 | + graph.add_import(importer=importer, imported=imported) |
| 114 | + |
| 115 | + result = graph.nominate_cycle_breakers("pkg.bar.orange") |
| 116 | + |
| 117 | + assert result == imports |
| 118 | + |
| 119 | + def test_on_package_with_one_child(self): |
| 120 | + graph = self._build_acyclic_graph() |
| 121 | + graph.add_module("pkg.bar.orange.ten.gamma.onechild") |
| 122 | + |
| 123 | + result = graph.nominate_cycle_breakers("pkg.bar.orange.ten.gamma") |
| 124 | + |
| 125 | + assert result == set() |
| 126 | + |
| 127 | + def _build_graph_with_no_imports(self) -> ImportGraph: |
| 128 | + graph = ImportGraph() |
| 129 | + for module in ( |
| 130 | + "pkg", |
| 131 | + "pkg.foo", |
| 132 | + "pkg.foo.blue", |
| 133 | + "pkg.foo.blue.one", |
| 134 | + "pkg.foo.blue.two", |
| 135 | + "pkg.foo.green", |
| 136 | + "pkg.bar", |
| 137 | + "pkg.bar.red", |
| 138 | + "pkg.bar.red.three", |
| 139 | + "pkg.bar.red.four", |
| 140 | + "pkg.bar.red.five", |
| 141 | + "pkg.bar.red.six", |
| 142 | + "pkg.bar.red.seven", |
| 143 | + "pkg.bar.yellow", |
| 144 | + "pkg.bar.yellow.eight", |
| 145 | + "pkg.bar.orange", |
| 146 | + "pkg.bar.orange.nine", |
| 147 | + "pkg.bar.orange.nine.alpha", |
| 148 | + "pkg.bar.orange.nine.beta", |
| 149 | + "pkg.bar.orange.ten", |
| 150 | + "pkg.bar.orange.ten.gamma", |
| 151 | + "pkg.bar.orange.ten.delta", |
| 152 | + ): |
| 153 | + graph.add_module(module) |
| 154 | + return graph |
| 155 | + |
| 156 | + def _build_acyclic_graph(self) -> ImportGraph: |
| 157 | + graph = self._build_graph_with_no_imports() |
| 158 | + # Add imports that make: |
| 159 | + # pkg.foo -> pkg.bar |
| 160 | + # pkg.bar.yellow -> pkg.foo.red |
| 161 | + # pkg.bar.orange.nine -> pkg.bar.orange.ten |
| 162 | + for importer, imported in ( |
| 163 | + ("pkg.foo", "pkg.bar.red"), |
| 164 | + ("pkg.foo.green", "pkg.bar.yellow"), |
| 165 | + ("pkg.foo.blue.two", "pkg.bar.red.three"), |
| 166 | + ("pkg.foo.blue.two", "pkg.bar.red.four"), |
| 167 | + ("pkg.foo.blue.two", "pkg.bar.red.five"), |
| 168 | + ("pkg.foo.blue.two", "pkg.bar.red.six"), |
| 169 | + ("pkg.foo.blue.two", "pkg.bar.red.seven"), |
| 170 | + ("pkg.bar.yellow", "pkg.bar.red"), |
| 171 | + ("pkg.bar.yellow.eight", "pkg.bar.red.three"), |
| 172 | + ("pkg.bar.yellow.eight", "pkg.bar.red.four"), |
| 173 | + ("pkg.bar.yellow.eight", "pkg.bar.red.five"), |
| 174 | + ("pkg.bar.orange.nine", "pkg.bar.orange.ten.gamma"), |
| 175 | + ("pkg.bar.orange.nine.alpha", "pkg.bar.orange.ten.gamma"), |
| 176 | + ("pkg.bar.orange.nine.beta", "pkg.bar.orange.ten.delta"), |
| 177 | + ): |
| 178 | + graph.add_import(importer=importer, imported=imported) |
| 179 | + return graph |
0 commit comments