From c7c92efb1b2217d2dc003d8d8ea6f21abf0e7c51 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Thu, 25 Jan 2024 18:20:22 +0000 Subject: [PATCH] tests: Add functions to make opening files in the test env easier Signed-off-by: Michael Tremer --- tests/python/keys.py | 7 +------ tests/python/package.py | 7 +------ tests/python/tests.py | 21 +++++++++++++++++++++ 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/tests/python/keys.py b/tests/python/keys.py index 0c84ac0ed..9663776e9 100755 --- a/tests/python/keys.py +++ b/tests/python/keys.py @@ -19,7 +19,6 @@ # # ############################################################################### -import os import pakfire import tests @@ -48,11 +47,7 @@ class KeysTests(tests.TestCase): self.assertEqual(key.algorithm, "Ed255919") def _import(self, path): - path = os.path.join( - os.environ.get("TEST_DATA_DIR"), path, - ) - - with open(path, "rb") as f: + with self.open(path) as f: payload = f.read() return self.pakfire.import_key(payload) diff --git a/tests/python/package.py b/tests/python/package.py index 3b51b6744..a7ffc42ed 100755 --- a/tests/python/package.py +++ b/tests/python/package.py @@ -1,7 +1,5 @@ #!/usr/bin/python3 -import os - import tests class PackageTest(tests.TestCase): @@ -12,10 +10,7 @@ class PackageTest(tests.TestCase): self.pakfire = self.setup_pakfire() def test_open(self): - path = os.path.join( - os.environ["TEST_DATA_DIR"], - "beep-1.3-2.ip3.x86_64.pfm", - ) + path = self.path("beep-1.3-2.ip3.x86_64.pfm") # Open the archive a = self.pakfire.open(path) diff --git a/tests/python/tests.py b/tests/python/tests.py index 9fec90460..bc8509479 100644 --- a/tests/python/tests.py +++ b/tests/python/tests.py @@ -22,6 +22,11 @@ import pakfire import os import unittest +# Fetch TEST_DATA_DIR +TEST_DATA_DIR = os.environ.get("TEST_DATA_DIR") +if not TEST_DATA_DIR: + raise RuntimeError("TEST_DATA_DIR is not set") + class TestCase(unittest.TestCase): """ This is a TestCase class based on unittest.TestCase which has @@ -50,6 +55,22 @@ class TestCase(unittest.TestCase): return pakfire.Pakfire(ctx=ctx, path=path, **kwargs) + def path(self, *args, **kwargs): + """ + Creates a path absolute to the test environment + """ + return os.path.join(TEST_DATA_DIR, *args, **kwargs) + + def open(self, path, *args, **kwargs): + """ + Opens a file in the test environment + """ + # Make the path absolute + path = self.path(path) + + # Open the file + return open(path, *args, **kwargs) + # Overlay for now main = unittest.main -- 2.47.2