"""Test helper for importing plugin modules from a hyphenated directory.""" from __future__ import annotations import importlib.util import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] def load_module(filename: str, module_name: str, package: bool = False): path = ROOT / filename if package: pkg_name = "digital_pet_pkg" if pkg_name not in sys.modules: pkg_spec = importlib.util.spec_from_file_location(pkg_name, ROOT / "__init__.py", submodule_search_locations=[str(ROOT)]) pkg = importlib.util.module_from_spec(pkg_spec) sys.modules[pkg_name] = pkg full_name = f"{pkg_name}.{path.stem}" else: full_name = module_name spec = importlib.util.spec_from_file_location(full_name, path) mod = importlib.util.module_from_spec(spec) sys.modules[full_name] = mod assert spec.loader is not None spec.loader.exec_module(mod) return mod