]> git.ipfire.org Git - pakfire.git/blame - tests/python/tests.py
tests: Add functions to make opening files in the test env easier
[pakfire.git] / tests / python / tests.py
CommitLineData
7d6ca8df
MT
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
21import pakfire
22import os
23import unittest
24
c7c92efb
MT
25# Fetch TEST_DATA_DIR
26TEST_DATA_DIR = os.environ.get("TEST_DATA_DIR")
27if not TEST_DATA_DIR:
28 raise RuntimeError("TEST_DATA_DIR is not set")
29
7d6ca8df
MT
30class TestCase(unittest.TestCase):
31 """
32 This is a TestCase class based on unittest.TestCase which has
33 a couple of handy methods that we frequently need.
34 """
35 def setup_ctx(self, path=None, **kwargs):
36 """
37 Sets up a new Pakfire context
38 """
39 if path is None:
40 path = os.environ.get("TEST_CONFIG_FILE")
41
42 # Return a new context
43 return pakfire.Ctx(path=path, **kwargs)
44
45 def setup_pakfire(self, ctx=None, path=None, **kwargs):
46 """
47 Sets up a new Pakfire environment
48 """
49 # Create a context if none given
50 if ctx is None:
51 ctx = self.setup_ctx()
52
53 if path is None:
54 path = os.environ.get("TEST_STUB_ROOT")
55
56 return pakfire.Pakfire(ctx=ctx, path=path, **kwargs)
57
c7c92efb
MT
58 def path(self, *args, **kwargs):
59 """
60 Creates a path absolute to the test environment
61 """
62 return os.path.join(TEST_DATA_DIR, *args, **kwargs)
63
64 def open(self, path, *args, **kwargs):
65 """
66 Opens a file in the test environment
67 """
68 # Make the path absolute
69 path = self.path(path)
70
71 # Open the file
72 return open(path, *args, **kwargs)
73
7d6ca8df
MT
74
75# Overlay for now
76main = unittest.main