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/>.

16

17 from pathlib import Path

18 from shutil import rmtree

19 from tempfile import mkdtemp

20 from unittest import TestCase as BaseTestCase

21 from unittest.mock import MagicMock, patch

22

23 from gitlab import __version__ as gitlab_version

24 from packaging import version

25

26 from gitlabracadabra.containers.registries import Registries

27 from gitlabracadabra.gitlab.connections import GitlabConnections

28

29

30 class TestCase(BaseTestCase):

31 """TestCase."""

32

33 def setUp(self) -> None:

34 """Test setup.

35

36 Create temporary directory, and mock user_cache_dir_path().

37 """

38 super().setUp()

39

40 Registries().reset()

41

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()

50

51 def tearDown(self) -> None:

52 """Test teardown.

53

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()

59

60 @classmethod

61 def gitlab_version(cls, ge: str | None = None, lt: str | None = None) -> bool:

62 """Compare python-gitlab version.

63

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.

67

68 Returns:

69 A boolean.

70 """

71 gitlab_version_parsed = version.parse(gitlab_version)

72 ret = True

73 if ge: 73 ↛ 74 line 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 ↛ 79 line 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

80

81

82 class TestCaseWithManager(TestCase):

83 """TestCase with GitLab connection."""

84

85 def setUp(self) -> None:

86 """Test setup.

87

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)

94

95 def tearDown(self) -> None:

96 """Test teardown.

97

98 Prune the GitLab connections.

99 """

100 GitlabConnections().load(default_id=None, config_files=None, debug=False)

101 super().tearDown()