Coverage for src/gitlabracadabra/tests/case.py: 91%
42 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-03-10 17:02 +0100
« prev ^ index » next coverage.py v7.6.12, created at 2025-03-10 17:02 +0100
1#
2# Copyright (C) 2019-2025 Mathieu Parent <math.parent@gmail.com>
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU Lesser General Public License as published by
6# the Free Software Foundation, either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU Lesser General Public License for more details.
13#
14# You should have received a copy of the GNU Lesser General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.
17from pathlib import Path
18from shutil import rmtree
19from tempfile import mkdtemp
20from unittest import TestCase as BaseTestCase
21from unittest.mock import MagicMock, patch
23from gitlab import __version__ as gitlab_version
24from packaging import version
26from gitlabracadabra.containers.registries import Registries
27from gitlabracadabra.gitlab.connections import GitlabConnections
30class TestCase(BaseTestCase):
31 """TestCase."""
33 def setUp(self) -> None:
34 """Test setup.
36 Create temporary directory, and mock user_cache_dir_path().
37 """
38 super().setUp()
40 Registries().reset()
42 self._temp_dir = Path(mkdtemp())
43 self._user_cache_dir_path_mock = MagicMock()
44 self._user_cache_dir_path_mock.return_value = self._temp_dir / "cache"
45 self._user_cache_dir_path_patch = patch(
46 "gitlabracadabra.disk_cache.user_cache_dir_path",
47 self._user_cache_dir_path_mock,
48 )
49 self._user_cache_dir_path_patch.start()
51 def tearDown(self) -> None:
52 """Test teardown.
54 Delete temporary directory, and mock user_cache_dir_path().
55 """
56 self._user_cache_dir_path_patch.stop()
57 rmtree(self._temp_dir)
58 super().tearDown()
60 @classmethod
61 def gitlab_version(cls, ge: str | None = None, lt: str | None = None) -> bool:
62 """Compare python-gitlab version.
64 Args:
65 ge: Returns true when python-gitlab version is greater or equal to this version.
66 lt: Returns true when python-gitlab version is lower than this version.
68 Returns:
69 A boolean.
70 """
71 gitlab_version_parsed = version.parse(gitlab_version)
72 ret = True
73 if ge: 73 ↛ 74line 73 didn't jump to line 74 because the condition on line 73 was never true
74 ge_parsed = version.parse(ge)
75 ret = ret and gitlab_version_parsed >= ge_parsed
76 if lt: 76 ↛ 79line 76 didn't jump to line 79 because the condition on line 76 was always true
77 lt_parsed = version.parse(lt)
78 ret = ret and gitlab_version_parsed < lt_parsed
79 return ret
82class TestCaseWithManager(TestCase):
83 """TestCase with GitLab connection."""
85 def setUp(self) -> None:
86 """Test setup.
88 Inject a GitLab connection.
89 """
90 super().setUp()
91 pygitlab_config = str((Path(__file__).parent / "python-gitlab.cfg").absolute())
92 GitlabConnections().load(default_id="localhost", config_files=[pygitlab_config], debug=False)
93 GitlabConnections().get_connection(None, auth=False)
95 def tearDown(self) -> None:
96 """Test teardown.
98 Prune the GitLab connections.
99 """
100 GitlabConnections().load(default_id=None, config_files=None, debug=False)
101 super().tearDown()