mirror of
https://github.com/arcan1s/ahriman.git
synced 2026-07-23 19:01:14 +00:00
refactor: implement generic optional import mechanisms
this project is actively using optional imports, which leads to duplicate handling here and there. This commit implements logic in special method to reduce complexity and simplify tests paths
This commit is contained in:
@@ -117,36 +117,12 @@ def get_package_status_extended(package: Package) -> dict[str, Any]:
|
||||
return {"status": BuildStatus().view(), "package": package.view()}
|
||||
|
||||
|
||||
def import_error(package: str, components: list[str], mocker: MockerFixture) -> MagicMock:
|
||||
"""
|
||||
mock import error
|
||||
|
||||
Args:
|
||||
package(str): package name to import
|
||||
components(list[str]): component to import if any (e.g. from ... import ...)
|
||||
mocker(MockerFixture): mocker object
|
||||
|
||||
Returns:
|
||||
MagicMock: mocked object
|
||||
"""
|
||||
import builtins
|
||||
_import = builtins.__import__
|
||||
|
||||
# pylint: disable=redefined-builtin
|
||||
def test_import(name: str, globals: Any, locals: Any, from_list: list[str], level: Any):
|
||||
if name == package and (not components or any(component in from_list for component in components)):
|
||||
raise ImportError
|
||||
return _import(name, globals, locals, from_list, level)
|
||||
|
||||
return mocker.patch.object(builtins, "__import__", test_import)
|
||||
|
||||
|
||||
@pytest.hookimpl(trylast=True)
|
||||
def pytest_configure() -> None:
|
||||
"""
|
||||
register helpers after pytest-helpers-namespace has initialized
|
||||
"""
|
||||
for helper in (anyvar, get_package_status, get_package_status_extended, import_error):
|
||||
for helper in (anyvar, get_package_status, get_package_status_extended):
|
||||
pytest.helpers.register(helper)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user