From 80ea9c682a92167e9680b7e31df5f54c5a2d379f Mon Sep 17 00:00:00 2001 From: "Commentator2.0" Date: Thu, 29 May 2025 19:19:11 +0200 Subject: [PATCH] tests/functional2/files: allow absolute paths as origin allow to pass absolute paths or similar Path entries to declaration of files instead of just string paths relative to the requesting file Change-Id: I616da6abbb73d1d63ead370e9ae37a401d85f42d --- tests/functional2/testlib/fixtures/file_helper.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/tests/functional2/testlib/fixtures/file_helper.py b/tests/functional2/testlib/fixtures/file_helper.py index a3c4d3709..73eb1bdc0 100644 --- a/tests/functional2/testlib/fixtures/file_helper.py +++ b/tests/functional2/testlib/fixtures/file_helper.py @@ -55,7 +55,7 @@ class File(_ByContentFileish): class CopyFile(Fileish): - def __init__(self, source: str): + def __init__(self, source: str | Path): """ Declares a file as a copy of an existing file :param source: Path to the file to be copied @@ -63,11 +63,12 @@ class CopyFile(Fileish): self.source = source def copy_to(self, path: Path, origin: Path): - shutil.copyfile(origin / self.source, path) + orig = self.source if isinstance(self.source, Path) else origin / self.source + shutil.copyfile(orig, path) class CopyTree(Fileish): - def __init__(self, tree_base: str): + def __init__(self, tree_base: str | Path): """ Declares a folder as a copy of an existing folder :param tree_base: base folder of the tree being copied @@ -75,11 +76,12 @@ class CopyTree(Fileish): self.tree_base = tree_base def copy_to(self, path: Path, origin: Path): - shutil.copytree(origin / self.tree_base, path, dirs_exist_ok=True) + orig = self.tree_base if isinstance(self.tree_base, Path) else origin / self.tree_base + shutil.copytree(orig, path, dirs_exist_ok=True) class CopyTemplate(_ByContentFileish): - def __init__(self, template: str, values: dict[str, Any], mode: int | None = None): + def __init__(self, template: str | Path, values: dict[str, Any], mode: int | None = None): """ Declares a file as an initiated version of the given file template :param template: source template's file name. Parameters formatted as `{key_name}` are replaced by corresponding values @@ -93,7 +95,7 @@ class CopyTemplate(_ByContentFileish): super().__init__(mode) def get_content(self, origin: Path) -> str: - template_path = origin / self.template + template_path = self.template if isinstance(self.template, Path) else origin / self.template template_content = template_path.read_text() self.content = BalancedTemplater(template_content).substitute(**self.values)