Skip to content

Commit

Permalink
Merge pull request #51 from seddonym/less-fakegraph
Browse files Browse the repository at this point in the history
Less fakegraph
  • Loading branch information
seddonym committed Jun 19, 2019
2 parents 0bc86d0 + 89948aa commit 05682f0
Show file tree
Hide file tree
Showing 4 changed files with 374 additions and 359 deletions.
8 changes: 7 additions & 1 deletion tests/adapters/building.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@


class FakeGraphBuilder(GraphBuilder):
"""
Graph builder that allows you to specify the graph ahead of time.
To use, call inject_graph with the graph you wish to inject, ahead
of when the builder would be called.
"""
def build(
self, root_package_name: str, include_external_packages: bool = False
) -> ImportGraph:
return self._graph

def set_graph(self, graph: ImportGraph) -> None:
def inject_graph(self, graph: ImportGraph) -> None:
self._graph = graph
66 changes: 42 additions & 24 deletions tests/unit/application/test_use_cases.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import string
from typing import Any, Dict, List, Optional

from grimp.adaptors.graph import ImportGraph # type: ignore
from importlinter.application.app_config import settings
from importlinter.application.use_cases import FAILURE, SUCCESS, lint_imports
from importlinter.application.user_options import UserOptions

from tests.adapters.building import FakeGraphBuilder
from tests.adapters.graph import FakeGraph
from tests.adapters.printing import FakePrinter
from tests.adapters.user_options import FakeUserOptionReader

Expand Down Expand Up @@ -32,7 +34,7 @@ def test_all_successful(self):
Contracts
---------
Analyzed 23 files, 44 dependencies.
Analyzed 26 files, 10 dependencies.
-----------------------------------
Contract foo KEPT
Expand Down Expand Up @@ -91,7 +93,7 @@ def test_one_failure(self):
Contracts
---------
Analyzed 23 files, 44 dependencies.
Analyzed 26 files, 10 dependencies.
-----------------------------------
Contract foo BROKEN
Expand All @@ -116,22 +118,18 @@ def test_forbidden_import(self):
Tests the ForbiddenImportContract - a simple contract that
looks at the graph.
"""
graph = FakeGraph(
root_package_name="mypackage",
import_details=[
{
"importer": "mypackage.foo",
"imported": "mypackage.bar",
"line_number": 8,
"line_contents": "from mypackage import bar",
},
{
"importer": "mypackage.foo",
"imported": "mypackage.bar",
"line_number": 16,
"line_contents": "from mypackage.bar import something",
},
],
graph = self._build_default_graph()
graph.add_import(
importer="mypackage.foo",
imported="mypackage.bar",
line_number=8,
line_contents="from mypackage import bar",
)
graph.add_import(
importer="mypackage.foo",
imported="mypackage.bar",
line_number=16,
line_contents="from mypackage.bar import something",
)
self._configure(
contracts_options=[
Expand All @@ -156,6 +154,9 @@ def test_forbidden_import(self):

assert result == FAILURE

# Expecting 28 files (default graph has 26 modules, we add 2).
# Expecting 11 dependencies (default graph has 10 imports, we add 2,
# but it counts as 1 as it's between the same modules).
settings.PRINTER.pop_and_assert(
"""
=============
Expand All @@ -166,8 +167,8 @@ def test_forbidden_import(self):
Contracts
---------
Analyzed 99 files, 999 dependencies.
------------------------------------
Analyzed 28 files, 11 dependencies.
-----------------------------------
Contract foo KEPT
Forbidden contract one BROKEN
Expand All @@ -194,7 +195,7 @@ def _configure(
self,
contracts_options: List[Dict[str, Any]],
contract_types: Optional[List[str]] = None,
graph: Optional[FakeGraph] = None,
graph: Optional[ImportGraph] = None,
):
session_options = {"root_package": "mypackage"}
if not contract_types:
Expand All @@ -213,5 +214,22 @@ def _configure(
USER_OPTION_READERS=[reader], GRAPH_BUILDER=FakeGraphBuilder(), PRINTER=FakePrinter()
)
if graph is None:
graph = FakeGraph(root_package_name="mypackage", module_count=23, import_count=44)
settings.GRAPH_BUILDER.set_graph(graph)
graph = self._build_default_graph()

settings.GRAPH_BUILDER.inject_graph(graph)

def _build_default_graph(self):
graph = ImportGraph()

# Add 26 modules.
for letter in string.ascii_lowercase:
graph.add_module(f"mypackage.{letter}")

# Add 10 imports in total.
for imported in ("d", "e", "f"):
for importer in ("a", "b", "c"):
graph.add_import(
importer=f"mypackage.{importer}", imported=f"mypackage.{imported}"
) # 3 * 3 = 9 imports.
graph.add_import(importer="mypackage.d", imported="mypackage.f") # 1 extra import.
return graph

0 comments on commit 05682f0

Please sign in to comment.