|
| 1 | +"""Every ``*_runner.py`` module must export a function decorated with |
| 2 | +``@module_runner``. |
| 3 | +
|
| 4 | +The decorator attaches introspection metadata the pipeline needs |
| 5 | +(``version``, ``input_module``, ``file_pattern``, ``file_ext``, |
| 6 | +``depends``, ``executes``, ``numbering_scheme``, ``run_method``). A new |
| 7 | +runner that forgets the decorator loads silently but fails at dispatch |
| 8 | +time. This test catches the oversight at import time. |
| 9 | +
|
| 10 | +We locate the runner by looking for any top-level function that carries |
| 11 | +the metadata, since the function name doesn't always match the file |
| 12 | +name (e.g. ``pastecat_runner.py`` exports ``paste_cat_runner``). |
| 13 | +""" |
| 14 | + |
| 15 | +import importlib |
| 16 | +import inspect |
| 17 | +import pkgutil |
| 18 | + |
| 19 | +import pytest |
| 20 | + |
| 21 | +import shapepipe.modules |
| 22 | + |
| 23 | +REQUIRED_ATTRS = ( |
| 24 | + "version", |
| 25 | + "input_module", |
| 26 | + "file_pattern", |
| 27 | + "file_ext", |
| 28 | + "depends", |
| 29 | + "executes", |
| 30 | + "numbering_scheme", |
| 31 | + "run_method", |
| 32 | +) |
| 33 | + |
| 34 | +# Modules imported here but broken upstream — tracked separately. |
| 35 | +# Mapping: module name → xfail reason. |
| 36 | +KNOWN_XFAIL = { |
| 37 | + # Both reach stile, which imports treecorr.corr2 (removed in treecorr 5.x). |
| 38 | + "shapepipe.modules.mccd_plots_runner": |
| 39 | + "stile v0.1 imports removed treecorr.corr2", |
| 40 | + "shapepipe.modules.random_cat_runner": |
| 41 | + "stile v0.1 imports removed treecorr.corr2", |
| 42 | +} |
| 43 | + |
| 44 | + |
| 45 | +def _runner_modules() -> list[str]: |
| 46 | + return sorted( |
| 47 | + m.name |
| 48 | + for m in pkgutil.iter_modules( |
| 49 | + shapepipe.modules.__path__, prefix="shapepipe.modules." |
| 50 | + ) |
| 51 | + if m.name.endswith("_runner") |
| 52 | + ) |
| 53 | + |
| 54 | + |
| 55 | +def _params(): |
| 56 | + for name in _runner_modules(): |
| 57 | + if name in KNOWN_XFAIL: |
| 58 | + yield pytest.param( |
| 59 | + name, |
| 60 | + marks=pytest.mark.xfail( |
| 61 | + reason=KNOWN_XFAIL[name], strict=True, raises=Exception |
| 62 | + ), |
| 63 | + ) |
| 64 | + else: |
| 65 | + yield name |
| 66 | + |
| 67 | + |
| 68 | +def pytest_generate_tests(metafunc): |
| 69 | + if "runner_module" in metafunc.fixturenames: |
| 70 | + metafunc.parametrize("runner_module", list(_params())) |
| 71 | + |
| 72 | + |
| 73 | +def test_runner_has_metadata(runner_module: str) -> None: |
| 74 | + mod = importlib.import_module(runner_module) |
| 75 | + decorated = [ |
| 76 | + obj |
| 77 | + for _, obj in inspect.getmembers(mod, inspect.isfunction) |
| 78 | + if obj.__module__ == runner_module |
| 79 | + and all(hasattr(obj, a) for a in REQUIRED_ATTRS) |
| 80 | + ] |
| 81 | + assert decorated, ( |
| 82 | + f"{runner_module} exports no function decorated with @module_runner " |
| 83 | + f"(required attrs: {REQUIRED_ATTRS})" |
| 84 | + ) |
0 commit comments