Files
claude-config/bin/repo_checks/test_package_cycles.py
T

188 lines
9.5 KiB
Python

import ast
import graphlib
import pathlib
import sys
import tempfile
import unittest
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from repo_checks import ROOT
def imported_by(module: pathlib.Path) -> set[str]:
"""The top-level name of each module that the file module imports. A deferred import inside a function counts: it only hides a cycle."""
found: set[str] = set()
for node in ast.walk(ast.parse(module.read_text(encoding="utf-8"))):
names = [alias.name for alias in node.names] if isinstance(node, ast.Import) else [node.module or ""] if isinstance(node, ast.ImportFrom) and node.level == 0 else []
found.update(name.split(".")[0] for name in names)
return found
def package_imports(bin_folder: pathlib.Path) -> dict[str, set[str]]:
"""Each package under bin_folder, with the other packages its modules import, tests aside."""
packages = {init.parent.name for init in bin_folder.glob("*/__init__.py")}
imported: dict[str, set[str]] = {package: set() for package in packages}
for package in packages:
for module in (bin_folder / package).rglob("*.py"):
if module.name.startswith("test_"):
continue
imported[package].update(top for top in imported_by(module) if top in packages and top != package)
return imported
def imported_names(module: pathlib.Path) -> list[str]:
"""The dotted name of each thing the file module imports, as `a.b.c` for `from a.b import c`. A deferred import inside a function counts."""
found: list[str] = []
for node in ast.walk(ast.parse(module.read_text(encoding="utf-8"))):
found += [alias.name for alias in node.names] if isinstance(node, ast.Import) else [f"{node.module}.{alias.name}" for alias in node.names] if isinstance(node, ast.ImportFrom) and node.level == 0 and node.module else []
return found
def part_imports(package: pathlib.Path) -> dict[str, set[str]]:
"""Each part of the package, with the other parts of it that its modules import, tests aside. A part is a module at the package's top, or a folder there with every module under it: a folder states that its modules belong together, so the top may not lean on a folder that leans on the top."""
imported: dict[str, set[str]] = {}
for module in package.rglob("*.py"):
if module.name.startswith("test_"):
continue
part = module.relative_to(package).parts[0].removesuffix(".py")
inside = [name.split(".")[1] for name in imported_names(module) if name.startswith(f"{package.name}.")]
imported.setdefault(part, set()).update(other for other in inside if other != part)
return imported
def part_cycles(bin_folder: pathlib.Path) -> dict[str, list[str]]:
"""Each package under bin_folder whose parts import in a cycle, with the parts along one."""
found = {}
for package in sorted(init.parent for init in bin_folder.glob("*/__init__.py")):
imported = part_imports(package)
if cycle(imported):
found[package.name] = cycle(imported)
return found
# The entry points that drive chunks. What two of them need lives in bin/chunk_driver/, so no other package imports one, its tests included.
DRIVERS = ("do_epic", "loop_claude")
def driver_imports(bin_folder: pathlib.Path, drivers: tuple[str, ...]) -> list[str]:
"""Each module under bin_folder that imports a driver's package from outside it, tests included, as `<module> imports <driver>`."""
found = []
for module in sorted(bin_folder.rglob("*.py")):
package = module.relative_to(bin_folder).parts[0]
found += [f"{module.relative_to(bin_folder)} imports {top}" for top in imported_by(module) if top in drivers and top != package]
return sorted(found)
# The packages a package may not import, its tests included, since they are the layer above it: chunk_driver drives chunks through agent_cycle.
ABOVE = {"agent_cycle": ("chunk_driver",)}
def upward_imports(bin_folder: pathlib.Path, above: dict[str, tuple[str, ...]]) -> list[str]:
"""Each module under bin_folder that imports a package above names as above its own, tests included, as `<module> imports <package>`."""
found = []
for package, banned in above.items():
for module in sorted((bin_folder / package).rglob("*.py")):
found += [f"{module.relative_to(bin_folder)} imports {top}" for top in imported_by(module) if top in banned]
return sorted(found)
def imports_of_test_modules(bin_folder: pathlib.Path) -> list[str]:
"""Each module under bin_folder that imports a test module of another package, as `<module> imports <test module>`. A module at bin_folder's root, such as test_env.py, belongs to no package."""
found = []
for module in sorted(bin_folder.rglob("*.py")):
package = module.relative_to(bin_folder).parts[0]
for name in imported_names(module):
top, *below = name.split(".")
tests = [part for part in below if part.startswith("test_")]
if tests and top != package:
found.append(f"{module.relative_to(bin_folder)} imports {name[:name.index(tests[0]) + len(tests[0])]}")
return sorted(set(found))
def cycle(imported: dict[str, set[str]]) -> list[str]:
"""One cycle among the packages or parts that imported names, as those along it; empty where there is none."""
try:
graphlib.TopologicalSorter(imported).prepare()
except graphlib.CycleError as found:
return list(found.args[1])
return []
def tree(folder: pathlib.Path, sources: dict[str, str]) -> None:
"""Writes each source to its path under folder."""
for path, source in sources.items():
(folder / path).parent.mkdir(parents=True, exist_ok=True)
(folder / path).write_text(source, encoding="utf-8")
class PackageCycles(unittest.TestCase):
def test_no_package_under_bin_imports_one_that_imports_it_back(self):
self.assertEqual(cycle(package_imports(ROOT / "bin")), [], "move what both packages need into a package that imports neither")
def test_no_package_imports_a_driver_of_chunks(self):
self.assertEqual(driver_imports(ROOT / "bin", DRIVERS), [], "move what the importer needs into bin/chunk_driver/, or a test double into the package whose code it stands in for")
def test_no_package_imports_the_layer_above_it(self):
self.assertEqual(upward_imports(ROOT / "bin", ABOVE), [], "pass the value in from the layer above, or in a test write it as a literal")
def test_an_import_of_the_layer_above_is_found_in_tests_too(self):
with tempfile.TemporaryDirectory() as temp:
folder = pathlib.Path(temp)
tree(folder, {"low/mod.py": "import other\n", "low/test_mod.py": "from high.mod import x\n", "low/run.py": "def f():\n\timport high\n", "high/mod.py": "import low\n"})
self.assertEqual(upward_imports(folder, {"low": ("high",)}), ["low/run.py imports high", "low/test_mod.py imports high"])
def test_a_driver_imported_from_another_package_is_found_in_tests_too(self):
with tempfile.TemporaryDirectory() as temp:
folder = pathlib.Path(temp)
tree(folder, {"a/mod.py": "import a.other\n", "b/test_mod.py": "from a.mod import x\n", "b/mod.py": "def f():\n\timport a\n", "c/mod.py": "import b\n"})
self.assertEqual(driver_imports(folder, ("a",)), ["b/mod.py imports a", "b/test_mod.py imports a"])
def test_no_module_imports_a_test_module_of_another_package(self):
self.assertEqual(imports_of_test_modules(ROOT / "bin"), [], "move what the importer needs into the doubles.py of the package that owns it")
def test_a_test_module_imported_from_another_package_is_found_however_it_is_spelled(self):
with tempfile.TemporaryDirectory() as temp:
folder = pathlib.Path(temp)
tree(folder, {
"a/test_mod.py": "from a.test_other import x\nfrom test_env import y\nfrom a.doubles import z\n",
"b/test_mod.py": "from a.test_mod import x\n",
"b/mod.py": "def f():\n\timport a.deep.test_far\n",
"c/test_mod.py": "from a import test_mod\nfrom a.test_mod import x, y\n",
"test_root.py": "from a.test_mod import x\n",
})
self.assertEqual(imports_of_test_modules(folder), ["b/mod.py imports a.deep.test_far", "b/test_mod.py imports a.test_mod", "c/test_mod.py imports a.test_mod", "test_root.py imports a.test_mod"])
def test_reads_an_import_deferred_into_a_function_and_leaves_out_tests(self):
with tempfile.TemporaryDirectory() as temp:
folder = pathlib.Path(temp)
tree(folder, {
"a/__init__.py": "", "a/mod.py": "def f():\n\tfrom b.mod import x\n",
"b/__init__.py": "", "b/mod.py": "import a.mod\n",
"c/__init__.py": "", "c/mod.py": "", "c/test_mod.py": "import a\n",
})
self.assertEqual(package_imports(folder), {"a": {"b"}, "b": {"a"}, "c": set()})
def test_no_part_of_a_package_imports_one_that_imports_it_back(self):
self.assertEqual(part_cycles(ROOT / "bin"), {}, "move what both parts need into a module at the package's top that imports neither")
def test_reads_a_folder_of_a_package_as_one_part_and_a_module_at_its_top_as_another(self):
with tempfile.TemporaryDirectory() as temp:
folder = pathlib.Path(temp)
tree(folder, {
"a/__init__.py": "", "a/main.py": "from a.kind.plant import x\nfrom a import shared\nimport b.kind\n",
"a/shared.py": "def f():\n\tfrom a.kind import plant\n",
"a/kind/__init__.py": "", "a/kind/plant.py": "import a.shared\nfrom a.kind.deep import other\n", "a/kind/deep/other.py": "import a.kind.plant\n",
"a/test_shared.py": "import a.main\n", "a/kind/test_plant.py": "import a.main\n",
})
self.assertEqual(part_imports(folder / "a"), {"__init__": set(), "main": {"kind", "shared"}, "shared": {"kind"}, "kind": {"shared"}})
self.assertEqual(sorted(cycle(part_imports(folder / "a"))[:2]), ["kind", "shared"])
def test_a_cycle_through_a_deferred_import_is_found(self):
self.assertEqual(sorted(cycle({"a": {"b"}, "b": {"a"}})[:2]), ["a", "b"])
self.assertEqual(cycle({"a": {"b"}, "b": set()}), [])
if __name__ == "__main__":
unittest.main()