]> git.ipfire.org Git - pakfire.git/blob - tests/python/tests.py
python: Add tests for the Archive class
[pakfire.git] / tests / python / tests.py
1 ###############################################################################
2 # #
3 # Pakfire - The IPFire package management system #
4 # Copyright (C) 2024 Pakfire development team #
5 # #
6 # This program is free software: you can redistribute it and/or modify #
7 # it under the terms of the GNU General Public License as published by #
8 # the Free Software Foundation, either version 3 of the License, or #
9 # (at your option) any later version. #
10 # #
11 # This program is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
15 # #
16 # You should have received a copy of the GNU General Public License #
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
18 # #
19 ###############################################################################
20
21 import pakfire
22 import os
23 import tempfile
24 import unittest
25
26 # Fetch TEST_DATA_DIR
27 TEST_DATA_DIR = os.environ.get("TEST_DATA_DIR")
28 if not TEST_DATA_DIR:
29 raise RuntimeError("TEST_DATA_DIR is not set")
30
31 class TestCase(unittest.TestCase):
32 """
33 This is a TestCase class based on unittest.TestCase which has
34 a couple of handy methods that we frequently need.
35 """
36 def setup_ctx(self, path=None, **kwargs):
37 """
38 Sets up a new Pakfire context
39 """
40 if path is None:
41 path = os.environ.get("TEST_CONFIG_FILE")
42
43 # Return a new context
44 return pakfire.Ctx(path=path, **kwargs)
45
46 def setup_pakfire(self, ctx=None, path=None, **kwargs):
47 """
48 Sets up a new Pakfire environment
49 """
50 # Create a context if none given
51 if ctx is None:
52 ctx = self.setup_ctx()
53
54 if path is None:
55 path = os.environ.get("TEST_STUB_ROOT")
56
57 return pakfire.Pakfire(ctx=ctx, path=path, **kwargs)
58
59 def path(self, *args, **kwargs):
60 """
61 Creates a path absolute to the test environment
62 """
63 return os.path.join(TEST_DATA_DIR, *args, **kwargs)
64
65 def open(self, path, *args, **kwargs):
66 """
67 Opens a file in the test environment
68 """
69 # Make the path absolute
70 path = self.path(path)
71
72 # Open the file
73 return open(path, *args, **kwargs)
74
75 def tempdir(self):
76 """
77 Creates a temporary directory
78 """
79 return tempfile.TemporaryDirectory(prefix="pakfire-test-")
80
81
82 # Overlay for now
83 main = unittest.main