From 7d6ca8df2ff5f178d73511ffcfed68fc2727f303 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Thu, 25 Jan 2024 16:01:09 +0000 Subject: [PATCH] tests: Build a common basis for Python tests This new class has some methods that make it easy for us to set up some simple Pakfire environments. Signed-off-by: Michael Tremer --- Makefile.am | 4 ++++ tests/python/keys.py | 13 ++++------ tests/python/tests.py | 55 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 8 deletions(-) create mode 100644 tests/python/tests.py diff --git a/Makefile.am b/Makefile.am index 92000e27..2310118c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1129,6 +1129,7 @@ TESTSUITE_LDADD = \ libpakfire-internal.la TESTS_ENVIRONMENT = \ + TEST_CONFIG_FILE="$(abs_top_srcdir)/tests/pakfire/pakfire.conf" \ TEST_DATA_DIR="$(abs_top_srcdir)/tests/data" \ TEST_STUB_ROOT="$(TEST_STUB_ROOT)" \ PAKFIRE_LOG=debug \ @@ -1139,6 +1140,9 @@ dist_check_SCRIPTS = \ tests/python/jail.py \ tests/python/package.py +EXTRA_DIST += \ + tests/python/tests.py + TESTS = \ $(check_PROGRAMS) \ $(dist_check_SCRIPTS) diff --git a/tests/python/keys.py b/tests/python/keys.py index 14473ca9..0c84ac0e 100755 --- a/tests/python/keys.py +++ b/tests/python/keys.py @@ -21,18 +21,15 @@ import os import pakfire -import unittest -class KeysTests(unittest.TestCase): +import tests + +class KeysTests(tests.TestCase): """ This tests the keys """ def setUp(self): - path = os.environ.get("TEST_STUB_ROOT") - if not path: - raise RuntimeError("TEST_STUB_ROOT is not defined") - - self.pakfire = pakfire.Pakfire(path) + self.pakfire = self.setup_pakfire() def test_generate(self): """ @@ -91,4 +88,4 @@ class KeysTests(unittest.TestCase): if __name__ == "__main__": - unittest.main() + tests.main() diff --git a/tests/python/tests.py b/tests/python/tests.py new file mode 100644 index 00000000..9fec9046 --- /dev/null +++ b/tests/python/tests.py @@ -0,0 +1,55 @@ +############################################################################### +# # +# Pakfire - The IPFire package management system # +# Copyright (C) 2024 Pakfire development team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +############################################################################### + +import pakfire +import os +import unittest + +class TestCase(unittest.TestCase): + """ + This is a TestCase class based on unittest.TestCase which has + a couple of handy methods that we frequently need. + """ + def setup_ctx(self, path=None, **kwargs): + """ + Sets up a new Pakfire context + """ + if path is None: + path = os.environ.get("TEST_CONFIG_FILE") + + # Return a new context + return pakfire.Ctx(path=path, **kwargs) + + def setup_pakfire(self, ctx=None, path=None, **kwargs): + """ + Sets up a new Pakfire environment + """ + # Create a context if none given + if ctx is None: + ctx = self.setup_ctx() + + if path is None: + path = os.environ.get("TEST_STUB_ROOT") + + return pakfire.Pakfire(ctx=ctx, path=path, **kwargs) + + +# Overlay for now +main = unittest.main -- 2.39.2