Skip to content

Commit

Permalink
Improve helpers test coverage and fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
seddonym committed Sep 3, 2021
1 parent 61193a5 commit 58424fa
Show file tree
Hide file tree
Showing 2 changed files with 309 additions and 38 deletions.
41 changes: 33 additions & 8 deletions src/importlinter/domain/helpers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import itertools
from typing import Dict, Iterable, List, Tuple, Union, Pattern
import re
from typing import Dict, Iterable, List, Pattern, Tuple, Union, Set

from importlinter.domain.imports import ImportExpression, Module, DirectImport
from importlinter.domain.imports import DirectImport, ImportExpression, Module
from importlinter.domain.ports.graph import ImportGraph


Expand All @@ -18,7 +18,7 @@ def pop_imports(
Returns:
The list of import details that were removed, including any additional metadata.
Raises:
MissingImport if the import is not present in the graph.
MissingImport if an import is not present in the graph.
"""
removed_imports: List[Dict[str, Union[str, int]]] = []
for import_to_remove in imports:
Expand All @@ -37,26 +37,50 @@ def pop_imports(
def import_expressions_to_imports(
graph: ImportGraph, expressions: Iterable[ImportExpression]
) -> List[DirectImport]:
imports: List[DirectImport] = []
"""
Returns a list of imports in a graph, given some import expressions.
Raises:
MissingImport if an import is not present in the graph. For a wildcarded import expression,
this is raised if there is not at least one match.
"""
imports: Set[DirectImport] = set()
for expression in expressions:
matched = False
for (importer, imported) in _expression_to_modules(expression, graph):
import_details = graph.get_import_details(
importer=importer.name, imported=imported.name
)
if import_details:
imports.append(DirectImport(importer=importer, imported=imported))
for individual_import_details in import_details:
imports.add(
DirectImport(
importer=Module(individual_import_details["importer"]),
imported=Module(individual_import_details["imported"]),
line_number=individual_import_details["line_number"],
line_contents=individual_import_details["line_contents"],
)
)
matched = True
if not matched:
raise MissingImport(
f"Ignored import expression {expression} didn't match anything in the graph."
)
return imports
return list(imports)


def pop_import_expressions(
graph: ImportGraph, expressions: Iterable[ImportExpression]
) -> List[Dict[str, Union[str, int]]]:
"""
Removes any imports matching the supplied import expressions from the graph.
Returns:
The list of imports that were removed, including any additional metadata.
Raises:
MissingImport if an import is not present in the graph. For a wildcarded import expression,
this is raised if there is not at least one match.
"""
imports = import_expressions_to_imports(graph, expressions)
return pop_imports(graph, imports)

Expand Down Expand Up @@ -94,7 +118,7 @@ def _to_pattern(expression: str) -> Pattern:
pattern_parts.append(part.replace("*", r"[^\.]+"))
else:
pattern_parts.append(part)
return re.compile(r"\.".join(pattern_parts))
return re.compile(r"^" + r"\.".join(pattern_parts) + r"$")


def _expression_to_modules(
Expand All @@ -115,5 +139,6 @@ def _expression_to_modules(
importer.append(Module(module))
if imported_expression.match(module):
imported.append(Module(module))
imported.append(Module(module))

return itertools.product(importer, imported)
return itertools.product(set(importer), set(imported))
Loading

0 comments on commit 58424fa

Please sign in to comment.