Skip to content

Commit

Permalink
devices: add pytest conftest infra for devices
Browse files Browse the repository at this point in the history
This is similar to the other `inputs`, `sources` etc conftest.py
modules and allows us to write unit test for the devices code.
  • Loading branch information
mvo5 committed Aug 21, 2024
1 parent 658502a commit 31176aa
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
33 changes: 33 additions & 0 deletions devices/test/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import os
import pathlib
from types import ModuleType

import pytest

from osbuild import devices, testutil
from osbuild.testutil.imports import import_module_from_path


@pytest.fixture(name="devices_module")
def devices_module_fixture(request: pytest.FixtureRequest) -> ModuleType:
"""devices_module is a fixture that imports a stage module by its name
defined in DEVICES_NAME in the test module.
"""
if not hasattr(request.module, "DEVICES_NAME"):
raise ValueError("devices_module fixture must be used in a test module that defines DEVICES_NAME")

devices_name = request.module.DEVICES_NAME
caller_dir = pathlib.Path(request.node.fspath).parent
module_path = caller_dir.parent / devices_name
return import_module_from_path("devices", os.fspath(module_path))


@pytest.fixture
def devices_service(devices_module) -> ModuleType:
"""devices_service is a fixture that imports a devices module by its name
defined in DEVICES_NAME in the test module and returns a Deviceservice
"""
service_cls = testutil.find_one_subclass_in_module(devices_module, devices.DeviceService)
fd = testutil.make_fake_service_fd()
srv_obj = service_cls.from_args(["--service-fd", str(fd)])
return srv_obj
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ deps =
requests

setenv =
LINTABLES = osbuild/ assemblers/* devices/* inputs/*.* mounts/*.* mounts/test/*.py runners/* sources/*.* stages/*.* inputs/test/*.py stages/test/*.py sources/test/*.py test/ tools/
LINTABLES = osbuild/ assemblers/* devices/*.* devices/test/*.py inputs/*.* mounts/*.* mounts/test/*.py runners/* sources/*.* stages/*.* inputs/test/*.py stages/test/*.py sources/test/*.py test/ tools/
LINTABLES_EXCLUDES = "*.json,*.sh"
LINTABLES_EXCLUDES_RE = ".*\.json$,.*\.sh"
TYPEABLES = osbuild
Expand Down

0 comments on commit 31176aa

Please sign in to comment.