inputs: add inputs_service fixture
This new fixture avoids some repetitivness when building tests for input services.
This commit is contained in:
parent
5f31ccf9f2
commit
7c0e9cfcf7
2 changed files with 25 additions and 8 deletions
|
|
@ -1,14 +1,16 @@
|
|||
import inspect
|
||||
import os
|
||||
import pathlib
|
||||
from types import ModuleType
|
||||
|
||||
import pytest
|
||||
|
||||
from osbuild import inputs, testutil
|
||||
from osbuild.testutil.imports import import_module_from_path
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def inputs_module(request: pytest.FixtureRequest) -> ModuleType:
|
||||
@pytest.fixture(name="inputs_module")
|
||||
def inputs_module_fixture(request: pytest.FixtureRequest) -> ModuleType:
|
||||
"""inputs_module is a fixture that imports a stage module by its name
|
||||
defined in INPUTS_NAME in the test module.
|
||||
"""
|
||||
|
|
@ -19,3 +21,21 @@ def inputs_module(request: pytest.FixtureRequest) -> ModuleType:
|
|||
caller_dir = pathlib.Path(request.node.fspath).parent
|
||||
module_path = caller_dir.parent / inputs_name
|
||||
return import_module_from_path("inputs", os.fspath(module_path))
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def inputs_service(inputs_module) -> ModuleType:
|
||||
"""inputs_service is a fixture that imports a inputs module by its name
|
||||
defined in INPUTS_NAME in the test module and returns a InputService
|
||||
"""
|
||||
service_cls = None
|
||||
for memb in inspect.getmembers(
|
||||
inputs_module,
|
||||
predicate=lambda obj: inspect.isclass(obj) and issubclass(
|
||||
obj, inputs.InputService)):
|
||||
if service_cls:
|
||||
raise ValueError(f"already have {service_cls}, also found {memb}")
|
||||
service_cls = memb[1]
|
||||
fd = testutil.make_fake_service_fd()
|
||||
srv_obj = service_cls.from_args(["--service-fd", str(fd)])
|
||||
return srv_obj
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue