]> git.ipfire.org Git - people/ms/pakfire.git/commitdiff
tests: Add functions to make opening files in the test env easier
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 25 Jan 2024 18:20:22 +0000 (18:20 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 25 Jan 2024 18:20:22 +0000 (18:20 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
tests/python/keys.py
tests/python/package.py
tests/python/tests.py

index 0c84ac0edcfcb956fbbce5ffeca402ff0f687823..9663776e91c42c470ff225a93bd4c47103f006f4 100755 (executable)
@@ -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)
index 3b51b6744fe044e164e0085382fe3b7e1fa8a040..a7ffc42ede968af92e1cb2fe32d73ba63993a8e8 100755 (executable)
@@ -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)
index 9fec904601c31a595381302f9a52ef497d7471f7..bc8509479644f1cbbf87fc93ec8913672f3e2e1c 100644 (file)
@@ -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