debian-forge/sources/test/conftest.py
Michael Vogt 79360b529a sources: add new sources_service fixture
Similar to the previous commit to include a `inputs_service` fixture
this does the same for `source.SourcesService` imports.

Note that we cannot easily share the helpers so we have to life with
a bit of very similar but duplicated code. To fix this we would have
to have a shared confftest.py that pytest can find. Which would mean
that we need to put the tests under a common dir that is reachable
via __init__.py files (which we currently not have because stages,
inputs etc do not have a __init__.py so python does not considers
them modules).
2024-04-03 11:36:01 +02:00

41 lines
1.5 KiB
Python

import inspect
import os
import pathlib
from types import ModuleType
import pytest
from osbuild import sources, testutil
from osbuild.testutil.imports import import_module_from_path
@pytest.fixture(name="sources_module")
def sources_module_fixture(request: pytest.FixtureRequest) -> ModuleType:
"""sources_module is a fixture that imports a stage module by its name
defined in SOURCES_NAME in the test module.
"""
if not hasattr(request.module, "SOURCES_NAME"):
raise ValueError("sources_module fixture must be used in a test module that defines SOURCES_NAME")
sources_name = request.module.SOURCES_NAME
caller_dir = pathlib.Path(request.node.fspath).parent
module_path = caller_dir.parent / sources_name
return import_module_from_path("sources", os.fspath(module_path))
@pytest.fixture
def sources_service(sources_module) -> ModuleType:
"""sources_service is a fixture that imports a sources module by its name
defined in SOURCES_NAME in the test module and returns a SourcesService
"""
service_cls = None
for memb in inspect.getmembers(
sources_module,
predicate=lambda obj: inspect.isclass(obj) and issubclass(
obj, sources.SourceService)):
if service_cls:
raise ValueError(f"already have {service_cls}, also found {memb}")
service_cls = memb[1]
fd = testutil.make_fake_service_fd()
services_obj = service_cls.from_args(["--service-fd", str(fd)])
return services_obj