|
| 1 | +"""Project plugins see the module-level aliases of a function. |
| 2 | +
|
| 3 | +An integration reading a deployment declaration names its handler as the module binds |
| 4 | +it, ``handlers.main``, and the engine applies an entry point named that way to the |
| 5 | +function a module-level alias, ``main = actual``, denotes. ``ModuleFunction.aliases`` |
| 6 | +gives a project plugin the names the module binds to each function that way, in source |
| 7 | +order and transitively, so it can report a declared name that names no function instead |
| 8 | +of trusting the assignment. A name bound to anything but a name is no alias, a method |
| 9 | +or a nested function has none, and the aliases survive the cache. |
| 10 | +""" |
| 11 | + |
| 12 | +from __future__ import annotations |
| 13 | + |
| 14 | +from pathlib import Path |
| 15 | + |
| 16 | +from coretrace_python import engine |
| 17 | +from coretrace_python.cache import ProjectCache |
| 18 | +from coretrace_python.findings import Finding |
| 19 | +from coretrace_python.plugins import ProjectContext, ProjectPlugin |
| 20 | + |
| 21 | +HANDLERS = ( |
| 22 | + "def actual(event, context):\n" |
| 23 | + " pass\n\n" |
| 24 | + "handler = actual\n" |
| 25 | + "main = handler\n\n" |
| 26 | + "class Impl:\n" |
| 27 | + " def run(self, event, context):\n" |
| 28 | + " pass\n\n" |
| 29 | + "Worker = Impl\n\n" |
| 30 | + "def outer():\n" |
| 31 | + " def inner():\n" |
| 32 | + " pass\n" |
| 33 | + " return inner\n\n" |
| 34 | + "made = outer()\n" |
| 35 | +) |
| 36 | + |
| 37 | + |
| 38 | +class Peek(ProjectPlugin): |
| 39 | + name = "peek" |
| 40 | + |
| 41 | + def __init__(self) -> None: |
| 42 | + self.seen: dict[str, tuple[str, ...]] = {} |
| 43 | + |
| 44 | + def analyze_project(self, ctx: ProjectContext) -> tuple[Finding, ...]: |
| 45 | + for module in ctx.modules: |
| 46 | + for function in ctx.functions(module): |
| 47 | + self.seen[f"{module}:{function.name}"] = function.aliases |
| 48 | + return () |
| 49 | + |
| 50 | + |
| 51 | +def project(tmp_path: Path) -> Path: |
| 52 | + root = tmp_path / "project" |
| 53 | + for relative, text in {"app/__init__.py": "", "app/handlers.py": HANDLERS}.items(): |
| 54 | + (root / relative).parent.mkdir(parents=True, exist_ok=True) |
| 55 | + (root / relative).write_text(text, encoding="utf-8") |
| 56 | + return root |
| 57 | + |
| 58 | + |
| 59 | +def peek(root: Path, cache: ProjectCache | None = None) -> tuple[dict[str, tuple[str, ...]], engine.ProjectAnalysis]: |
| 60 | + plugin = Peek() |
| 61 | + analysis = engine.analyze_project(root, [engine.BUNDLED_PLUGINS], plugins=[plugin], cache=cache) |
| 62 | + return plugin.seen, analysis |
| 63 | + |
| 64 | + |
| 65 | +def test_a_function_lists_the_module_level_names_bound_to_it(tmp_path: Path) -> None: |
| 66 | + seen, _ = peek(project(tmp_path)) |
| 67 | + |
| 68 | + assert seen["app.handlers:actual"] == ("handler", "main") |
| 69 | + assert seen["app.handlers:Impl.run"] == () |
| 70 | + assert seen["app.handlers:outer"] == () |
| 71 | + assert seen["app.handlers:outer.inner"] == () |
| 72 | + assert seen["app.handlers:<module>"] == () |
| 73 | + |
| 74 | + |
| 75 | +def test_the_aliases_survive_the_cache(tmp_path: Path) -> None: |
| 76 | + root = project(tmp_path) |
| 77 | + cache = ProjectCache(tmp_path / "cache") |
| 78 | + |
| 79 | + first, _ = peek(root, cache) |
| 80 | + second, analysis = peek(root, cache) |
| 81 | + |
| 82 | + assert analysis.reused == ("app", "app.handlers") |
| 83 | + assert second == first |
| 84 | + assert second["app.handlers:actual"] == ("handler", "main") |
0 commit comments