From 4eb2b0d1fa233d29777807f91b07a98416b332dd Mon Sep 17 00:00:00 2001 From: Stefaan Lippens Date: Fri, 24 Mar 2023 15:56:32 +0100 Subject: [PATCH] minor test utility tweaks --- openeo_driver/testing.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/openeo_driver/testing.py b/openeo_driver/testing.py index 8cd4d6a2..9214ccfc 100644 --- a/openeo_driver/testing.py +++ b/openeo_driver/testing.py @@ -39,13 +39,14 @@ def read_file(path: Union[Path, str], mode='r') -> str: """Get contents of given file as text string.""" + # TODO deprecated, just use Path(path).read_text(encoding="utf-8") with Path(path).open(mode) as f: return f.read() def load_json(path: Union[Path, str], preprocess: Callable[[str], str] = None) -> dict: """Parse data from JSON file""" - data = read_file(path) + data = Path(path).read_text(encoding="utf-8") if preprocess: data = preprocess(data) return json.loads(data) @@ -224,9 +225,18 @@ def data_path(self, filename: str) -> Path: """Get absolute pat to a test data file""" return self.data_root / filename - def read_file(self, filename, mode='r') -> str: + def read_bytes(self, filename) -> bytes: + return self.data_path(filename).read_bytes() + + def read_text(self, filename) -> str: + return self.data_path(filename).read_text(encoding="utf-8") + + def read_file(self, filename, mode="r") -> Union[str, bytes]: """Get contents of test file, given by relative path.""" - return read_file(self.data_path(filename), mode=mode) + if mode in {"rb", "br"}: + return self.read_bytes(filename) + else: + return self.read_text(filename) def load_json(self, filename, preprocess: Callable = None) -> dict: """Load test process graph from json file"""