diff --git a/injection/_core/module.py b/injection/_core/module.py index 33ef09c..ffa688a 100644 --- a/injection/_core/module.py +++ b/injection/_core/module.py @@ -816,11 +816,11 @@ def load_profile(self, *names: str) -> ContextManager[Self]: self.unlock().init_modules(*modules) @contextmanager - def cleaner() -> Iterator[Self]: + def unload() -> Iterator[Self]: yield self self.unlock().init_modules() - return cleaner() + return unload() async def all_ready(self) -> None: for broker in self.__brokers: diff --git a/injection/entrypoint.py b/injection/entrypoint.py index 419c47b..2c7818e 100644 --- a/injection/entrypoint.py +++ b/injection/entrypoint.py @@ -105,6 +105,14 @@ def load_modules( ) -> Self: return self.setup(lambda: loader.load(*packages)) + def load_profile(self, /, *names: str) -> Self: + @contextmanager + def decorator(module: Module) -> Iterator[None]: + with module.load_profile(*names): + yield + + return self.decorate(decorator(self.module)) + def setup(self, function: Callable[..., Any], /) -> Self: @contextmanager def decorator() -> Iterator[Any]: diff --git a/tests/entrypoint/test_entrypoint.py b/tests/entrypoint/test_entrypoint.py index da5acc5..2c3b4d1 100644 --- a/tests/entrypoint/test_entrypoint.py +++ b/tests/entrypoint/test_entrypoint.py @@ -1,7 +1,7 @@ from collections.abc import Iterator from contextlib import contextmanager -from injection import injectable +from injection import injectable, mod from injection.entrypoint import Entrypoint @@ -41,6 +41,18 @@ def function(service: Service) -> bool: entrypoint = Entrypoint(function).inject() assert entrypoint() + def test_load_profile_with_success_return_entrypoint(self): + profile_name = "test" + + @mod(profile_name).injectable + class Service: ... + + def function(service: Service) -> bool: + return isinstance(service, Service) + + entrypoint = Entrypoint(function).inject().load_profile(profile_name) + assert entrypoint() + def test_setup_with_success_return_entrypoint(self): count = 0