61 lines
2.9 KiB
Python
61 lines
2.9 KiB
Python
import ast
|
|
import importlib
|
|
import importlib.util
|
|
import pathlib
|
|
import sys
|
|
import unittest
|
|
|
|
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
|
|
|
from repo_checks import ROOT
|
|
|
|
# The trials folders whose scripts import from bin/.
|
|
TRIALS = [ROOT / "judge-field-trials", ROOT / "reviewer-merge-trials"]
|
|
|
|
|
|
def bin_imports(source: str) -> list[tuple[str, str | None]]:
|
|
"""Each module the source imports from bin/, with each name it takes from that module or reads through a name bound to it, or None for the module itself."""
|
|
tree = ast.parse(source)
|
|
found: list[tuple[str, str | None]] = []
|
|
# A name the source binds to a module, such as rm in `from review_replay import main as rm`.
|
|
modules: dict[str, str] = {}
|
|
for node in ast.walk(tree):
|
|
if isinstance(node, ast.ImportFrom) and node.module and node.level == 0:
|
|
found += [(node.module, alias.name) for alias in node.names]
|
|
modules |= {alias.asname or alias.name: f"{node.module}.{alias.name}" for alias in node.names if is_module(f"{node.module}.{alias.name}")}
|
|
elif isinstance(node, ast.Import):
|
|
found += [(alias.name, None) for alias in node.names]
|
|
modules |= {alias.asname: alias.name for alias in node.names if alias.asname}
|
|
found += [(modules[node.value.id], node.attr) for node in ast.walk(tree) if isinstance(node, ast.Attribute) and isinstance(node.value, ast.Name) and node.value.id in modules]
|
|
return [(module, name) for module, name in found if in_bin(module)]
|
|
|
|
|
|
def is_module(dotted: str) -> bool:
|
|
path = ROOT / "bin" / dotted.replace(".", "/")
|
|
return path.is_dir() or path.with_suffix(".py").is_file()
|
|
|
|
|
|
def in_bin(module: str) -> bool:
|
|
return is_module(module.split(".")[0])
|
|
|
|
|
|
class TrialsImports(unittest.TestCase):
|
|
"""docs/decisions.md says when to rerun each trials folder's scripts, so a refactor of bin/ must not break what they import."""
|
|
|
|
def test_each_name_a_trials_script_imports_from_bin_exists(self):
|
|
scripts = sorted(script for folder in TRIALS for script in folder.glob("*.py"))
|
|
self.assertTrue(all(any(folder.glob("*.py")) for folder in TRIALS))
|
|
for script in scripts:
|
|
for module, name in bin_imports(script.read_text(encoding="utf-8")):
|
|
with self.subTest(script=script.name, module=module, name=name):
|
|
imported = importlib.import_module(module)
|
|
if name is not None:
|
|
self.assertTrue(hasattr(imported, name) or importlib.util.find_spec(f"{module}.{name}"))
|
|
|
|
def test_an_import_of_a_bin_module_is_found_and_a_stdlib_one_is_not(self):
|
|
self.assertEqual(bin_imports("import json\nfrom failure import Failure\nimport review_replay.main as rm\n"), [("failure", "Failure"), ("review_replay.main", None)])
|
|
|
|
def test_a_name_read_through_a_module_alias_is_found(self):
|
|
source = "from review_replay import main as rm\nimport review_replay.rounds as rr\nrm.code_of(rr.Round)\n"
|
|
self.assertEqual(bin_imports(source), [("review_replay", "main"), ("review_replay.rounds", None), ("review_replay.main", "code_of"), ("review_replay.rounds", "Round")])
|