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 gitlabracadabra.containers.registries import Registries

24 from gitlabracadabra.gitlab.connections import GitlabConnections

25

26

27 class TestCase(BaseTestCase):

28 """TestCase."""

29

30 def setUp(self) -> None:

31 """Test setup.

32

33 Create temporary directory, and mock user_cache_dir_path().

34 """

35 super().setUp()

36

37 Registries().reset()

38

39 self._temp_dir = Path(mkdtemp())

40 self._user_cache_dir_path_mock = MagicMock()

41 self._user_cache_dir_path_mock.return_value = self._temp_dir / "cache"

42 self._user_cache_dir_path_patch = patch(

43 "gitlabracadabra.disk_cache.user_cache_dir_path",

44 self._user_cache_dir_path_mock,

45 )

46 self._user_cache_dir_path_patch.start()

47

48 def tearDown(self) -> None:

49 """Test teardown.

50

51 Delete temporary directory, and mock user_cache_dir_path().

52 """

53 self._user_cache_dir_path_patch.stop()

54 rmtree(self._temp_dir)

55 super().tearDown()

56

57

58 class TestCaseWithManager(TestCase):

59 """TestCase with GitLab connection."""

60

61 def setUp(self) -> None:

62 """Test setup.

63

64 Inject a GitLab connection.

65 """

66 super().setUp()

67 pygitlab_config = str((Path(__file__).parent / "python-gitlab.cfg").absolute())

68 GitlabConnections().load(default_id="localhost", config_files=[pygitlab_config], debug=False)

69 GitlabConnections().get_connection(None, auth=False)

70

71 def tearDown(self) -> None:

72 """Test teardown.

73

74 Prune the GitLab connections.

75 """

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

77 super().tearDown()