From d3f53a0205a6f02f008921cd5309b19b969aecfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dan=20=C4=8Cerm=C3=A1k?= Date: Tue, 8 Oct 2024 13:38:26 +0200 Subject: [PATCH] Add Replacement.to_service helper method --- src/bci_build/package/__init__.py | 16 ++++++++++++ tests/test_service.py | 43 +++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/src/bci_build/package/__init__.py b/src/bci_build/package/__init__.py index 356d75124..f33750fb3 100644 --- a/src/bci_build/package/__init__.py +++ b/src/bci_build/package/__init__.py @@ -28,6 +28,7 @@ from bci_build.registry import ApplicationCollectionRegistry from bci_build.registry import Registry from bci_build.registry import publish_registry +from bci_build.service import Service from bci_build.templates import DOCKERFILE_TEMPLATE from bci_build.templates import INFOHEADER_TEMPLATE from bci_build.templates import KIWI_TEMPLATE @@ -107,6 +108,21 @@ def __post_init__(self) -> None: if self.file_name and "readme" in self.file_name.lower(): raise ValueError(f"Cannot replace variables in {self.file_name}!") + def to_service(self, default_file_name: str) -> Service: + """Convert this replacement into a + :py:class:`~bci__build.service.Service`. + + """ + return Service( + name="replace_using_package_version", + param=[ + ("file", self.file_name or default_file_name), + ("regex", self.regex_in_build_description), + ("package", self.package_name), + ] + + ([("parse-version", self.parse_version)] if self.parse_version else []), + ) + def _build_tag_prefix(os_version: OsVersion) -> str: if os_version == OsVersion.TUMBLEWEED: diff --git a/tests/test_service.py b/tests/test_service.py index 8ff382075..7dd59d2c7 100644 --- a/tests/test_service.py +++ b/tests/test_service.py @@ -21,6 +21,49 @@ def test_service_with_params_as_xml(): ) +@pytest.mark.parametrize( + "replacement, default_file_name, service", + [ + # bare bone example + ( + Replacement(regex := "%%ver%%", pkg := "pkgFoo"), + "Dockerfile", + Service( + name=(name := "replace_using_package_version"), + param=[("file", "Dockerfile"), ("regex", regex), ("package", pkg)], + ), + ), + # the default file name is ignored if the parameter file_name is given + ( + Replacement(regex, pkg, file_name=(fname := "testfile")), + "Dockerfile", + Service( + name=name, + param=[("file", fname), ("regex", regex), ("package", pkg)], + ), + ), + # specify a parse_version + ( + Replacement(regex, pkg, parse_version=ParseVersion.MAJOR), + "Dockerfile", + Service( + name=name, + param=[ + ("file", "Dockerfile"), + ("regex", regex), + ("package", pkg), + ("parse-version", "major"), + ], + ), + ), + ], +) +def test_replacement_to_service( + replacement: Replacement, default_file_name: str, service: Service +): + assert replacement.to_service(default_file_name) == service + + _BASE_KWARGS = { "name": "test", "package_name": "test-image",