File size: 8,740 Bytes
f628309 ba784dc f4952b6 ba784dc f4952b6 ba784dc f628309 ba784dc f4952b6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 | """Import resolution over a package that uses the awkward import forms."""
from __future__ import annotations
from pathlib import Path
from typing import List, Tuple
from lingologos_python.ast_graph import (
build_reverse_index,
discover_python_files,
extract_python_graph,
importers_from_index,
importers_of,
reexported_names,
)
from lingologos_python.test_selector import select_python_tests
def _write(root: Path, rel_posix: str, text: str) -> None:
path = root / Path(*rel_posix.split("/"))
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(text, encoding="utf-8", newline="\n")
def _hard_package(root: Path) -> None:
"""One package with the forms that broke selection before: dots, stars, aliases, no __init__."""
_write(root, "app/__init__.py", "")
_write(root, "app/core/__init__.py", "")
_write(root, "app/core/engine.py", "def run() -> int:\n return 1\n")
_write(root, "app/core/legacy.py", "def old() -> int:\n return 0\n")
_write(root, "app/core/sub/__init__.py", "")
_write(
root,
"app/core/sub/calc.py",
"from ..engine import run\n\n\ndef twice() -> int:\n return run() + run()\n",
)
# app/plugins carries no __init__.py: a namespace package, named by its folder.
_write(root, "app/plugins/extra.py", "def assist() -> str:\n return 'assist'\n")
_write(
root,
"tests/test_plain.py",
"from app.core.engine import run\n\n\ndef test_run():\n assert run() == 1\n",
)
_write(
root,
"tests/test_alias.py",
"from app.core import engine as eng\n\n\ndef test_eng():\n assert eng.run() == 1\n",
)
_write(
root,
"tests/test_star.py",
"from app.core.legacy import *\n\n\ndef test_old():\n assert old() == 0\n",
)
_write(
root,
"tests/test_ns.py",
"from app.plugins.extra import assist\n\n\ndef test_assist():\n assert assist() == 'assist'\n",
)
def _file_edges(root: Path) -> List[Tuple[str, str]]:
_, edges = extract_python_graph(root, discover_python_files(root))
return [(s.split("::", 1)[0], d.split("::", 1)[0]) for s, d in edges]
def test_relative_dots_namespace_and_star_imports_become_file_edges(tmp_path: Path) -> None:
_hard_package(tmp_path)
edges = set(_file_edges(tmp_path))
assert ("app/core/sub/calc.py", "app/core/engine.py") in edges
assert ("tests/test_alias.py", "app/core/engine.py") in edges
assert ("tests/test_star.py", "app/core/legacy.py") in edges
assert ("tests/test_ns.py", "app/plugins/extra.py") in edges
def test_an_aliased_module_import_selects_the_module_it_names(tmp_path: Path) -> None:
_hard_package(tmp_path)
selected = select_python_tests(tmp_path, ["app/core/engine.py"])
assert "tests/test_alias.py" in selected
assert "tests/test_plain.py" in selected
assert "tests/test_star.py" not in selected
assert "tests/test_ns.py" not in selected
def test_a_star_import_selects_the_test_that_stars_the_changed_module(tmp_path: Path) -> None:
_hard_package(tmp_path)
selected = select_python_tests(tmp_path, ["app/core/legacy.py"])
assert "tests/test_star.py" in selected
assert "tests/test_plain.py" not in selected
def test_a_namespace_package_change_selects_its_only_importer(tmp_path: Path) -> None:
_hard_package(tmp_path)
selected = select_python_tests(tmp_path, ["app/plugins/extra.py"])
assert selected == ["tests/test_ns.py"]
def _reexport_package(root: Path) -> None:
"""One package that answers with names its __init__ imports from inside itself."""
_write(root, "app/__init__.py", "from .impl import helper\nfrom . import tools\n")
_write(root, "app/impl.py", "def helper() -> int:\n return 2\n")
_write(root, "app/tools.py", "X = 1\n")
_write(
root,
"tests/test_app.py",
"from app import helper\n\n\ndef test_helper():\n assert helper() == 2\n",
)
_write(
root,
"tests/test_tools.py",
"from app.tools import X\n\n\ndef test_x():\n assert X == 1\n",
)
def test_a_reexport_table_names_every_file_a_package_answer_for(tmp_path: Path) -> None:
_reexport_package(tmp_path)
table = reexported_names(tmp_path, "app/__init__.py")
assert "app.impl" in table["helper"]
assert "app.tools" in table["tools"]
def test_a_change_behind_a_reexport_reaches_the_test_that_never_named_the_file(
tmp_path: Path,
) -> None:
_reexport_package(tmp_path)
files = discover_python_files(tmp_path)
assert "tests/test_app.py" in importers_of(tmp_path, files, "app/impl.py")
edges = set(_file_edges(tmp_path))
assert ("tests/test_app.py", "app/impl.py") in edges
selected = select_python_tests(tmp_path, ["app/impl.py"])
assert "tests/test_app.py" in selected
def _star_reexport_package(root: Path) -> None:
"""A package whose __init__ stars two modules: one lists __all__, one does not."""
_write(root, "app/__init__.py", "from .impl import *\nfrom .tools import *\n")
_write(
root,
"app/impl.py",
"__all__ = ['shown']\n\nhidden = 3\n\n\ndef shown() -> int:\n return 5\n",
)
_write(
root,
"app/tools.py",
"visible = 1\n_hidden = 2\n\n\ndef usable() -> int:\n return visible\n",
)
_write(
root,
"tests/test_star_app.py",
"from app import *\n\n\ndef test_shown():\n assert shown() == 5\n",
)
def test_a_star_reexport_table_expands_to_the_names_the_star_hands_over(
tmp_path: Path,
) -> None:
_star_reexport_package(tmp_path)
table = reexported_names(tmp_path, "app/__init__.py")
assert table.get("shown") == ["app.impl.shown"]
assert "hidden" not in table # __all__ said shown, and __all__ is the contract
assert table.get("visible") == ["app.tools.visible"]
assert table.get("usable") == ["app.tools.usable"]
assert "_hidden" not in table # a star never carries an underscored name
def test_a_change_behind_a_star_reexport_selects_the_test_that_stars_the_package(
tmp_path: Path,
) -> None:
_star_reexport_package(tmp_path)
edges = set(_file_edges(tmp_path))
assert ("tests/test_star_app.py", "app/impl.py") in edges
selected = select_python_tests(tmp_path, ["app/impl.py"])
assert "tests/test_star_app.py" in selected
def test_a_literal_dynamic_import_counts_as_an_import(tmp_path: Path) -> None:
"""import_module and __import__ with a plain string name bind a file like any import."""
root = tmp_path
_write(root, "app/__init__.py", "")
_write(root, "app/impl.py", "def helper() -> int:\n return 2\n")
_write(
root,
"tests/test_module_lib.py",
"import importlib\n\n\ndef test_dyn():\n"
" mod = importlib.import_module('app.impl')\n"
" assert mod.helper() == 2\n",
)
_write(
root,
"tests/test_dunder_import.py",
"impl = __import__('app.impl')\n\n\ndef test_dyn():\n"
" assert impl.helper() == 2\n",
)
_write(
root,
"tests/test_opaque.py",
"import importlib\n\n\ndef test_opaque():\n"
" mod = importlib.import_module(NAME_FROM_CONFIG)\n"
" assert mod is not None\n",
)
files = discover_python_files(tmp_path)
importers = importers_of(tmp_path, files, "app/impl.py")
assert "tests/test_module_lib.py" in importers
assert "tests/test_dunder_import.py" in importers
assert "tests/test_opaque.py" not in importers
edges = set(_file_edges(tmp_path))
assert ("tests/test_module_lib.py", "app/impl.py") in edges
assert ("tests/test_dunder_import.py", "app/impl.py") in edges
selected = select_python_tests(tmp_path, ["app/impl.py"])
assert "tests/test_module_lib.py" in selected
assert "tests/test_dunder_import.py" in selected
assert "tests/test_opaque.py" not in selected
def test_an_index_built_once_answers_every_target_as_a_per_query_walk_does(
tmp_path: Path,
) -> None:
"""A workspace parsed once gives the same importers as parsing it once per question."""
hard = tmp_path / "hard"
_hard_package(hard)
reexported = tmp_path / "reexported"
_reexport_package(reexported)
for workspace in (hard, reexported):
files = discover_python_files(workspace)
index = build_reverse_index(workspace, files)
for target in files:
remaining = [p for p in files if p != target]
assert importers_from_index(index, target) == sorted(
importers_of(workspace, remaining, target)
), "the index and the walk disagree about %s" % target
|