Debian Codegraph @

test/unit/path_test.py

raw
1 import logging
2 from unittest.mock import (
3 patch, call, Mock
4 )
5 from pytest import (
6 raises, fixture
7 )
8
9 import os
10
11 import pytest
12
13 from kiwi.path import Path
14 from kiwi.exceptions import KiwiFileAccessError
15
16
17 class TestPath:
18 @fixture(autouse=True)
19 def inject_fixtures(self, caplog):
20 self._caplog = caplog
21
22 def test_sort_by_hierarchy(self):
23 ordered = Path.sort_by_hierarchy(
24 ['usr', 'usr/bin', 'etc', 'usr/lib']
25 )
26 assert ordered == ['usr', 'etc', 'usr/bin', 'usr/lib']
27
28 @patch('pathlib.Path')
29 def test_create(self, mock_Path):
30 path = Mock()
31 mock_Path.return_value = path
32 Path.create('foo')
33 path.mkdir.assert_called_once_with(
34 parents=True, exist_ok=True
35 )
36 mock_Path.return_value = Exception
37 with raises(KiwiFileAccessError):
38 Path.create('foo')
39
40 @patch('kiwi.command.os.path.exists')
41 @patch('kiwi.command.Command.run')
42 def test_wipe(self, mock_command, mock_exists):
43 mock_exists.return_value = True
44 Path.wipe('foo')
45 mock_command.assert_called_once_with(
46 ['rm', '-r', '-f', 'foo']
47 )
48 mock_exists.return_value = False
49 mock_command.reset_mock()
50 Path.wipe('foo')
51 assert not mock_command.called
52
53 @patch('kiwi.command.Command.run')
54 def test_remove_hierarchy(self, mock_command):
55 Path.remove_hierarchy('/my_root', '/tmp/foo/bar')
56 with self._caplog.at_level(logging.WARNING):
57 assert mock_command.call_args_list == [
58 call(
59 [
60 'rmdir', '--ignore-fail-on-non-empty',
61 '/my_root/tmp/foo/bar'
62 ]
63 ),
64 call(
65 [
66 'rmdir', '--ignore-fail-on-non-empty',
67 '/my_root/tmp/foo'
68 ]
69 )
70 ]
71 assert 'remove_hierarchy: path /my_root/tmp is protected' in \
72 self._caplog.text
73 mock_command.reset_mock()
74 Path.remove_hierarchy('/home/kiwi/my_root', 'foo')
75 mock_command.assert_called_once_with(
76 ['rmdir', '--ignore-fail-on-non-empty', '/home/kiwi/my_root/foo']
77 )
78
79 def test_move_to_root(self):
80 assert [
81 '/usr/bin', '/sbin'
82 ] == Path.move_to_root('/root_dir', ['/root_dir/usr/bin', '/sbin'])
83
84 def test_rebase_to_root(self):
85 assert [
86 '/root_dir/usr/bin', '/root_dir/sbin'
87 ] == Path.rebase_to_root('/root_dir', ['usr/bin', '/sbin'])
88
89 @patch('os.access')
90 @patch('os.environ.get')
91 @patch('os.path.exists')
92 def test_which(self, mock_exists, mock_env, mock_access):
93 mock_env.return_value = '/usr/local/bin:/usr/bin:/bin'
94 mock_exists.return_value = True
95 assert Path.which('some-file') == '/usr/local/bin/some-file'
96 mock_exists.return_value = False
97 assert Path.which('some-file') is None
98 mock_access.return_value = False
99 mock_env.return_value = '/usr/local/bin:/usr/bin:/bin'
100 assert Path.which('some-file', access_mode=os.X_OK) is None
101
102 def test_which_with_real_data(self, pytestconfig: 'pytest.Config'):
103 rootpath = getattr(
104 pytestconfig, 'rootpath', getattr(pytestconfig, 'rootdir')
105 )
106 assert Path.which(
107 'pyproject.toml',
108 custom_env={'PATH': str(rootpath)},
109 access_mode=os.F_OK
110 ) == str(rootpath / "pyproject.toml")
111
112 assert Path.which(
113 '__init__.py',
114 custom_env={'PATH': "/kiwi/"},
115 access_mode=os.F_OK,
116 root_dir=str(rootpath)
117 ) == str(rootpath / "kiwi" / "__init__.py")
118
119 @patch('os.access')
120 @patch('os.environ.get')
121 @patch('os.path.exists')
122 def test_which_not_found_log(
123 self, mock_exists, mock_env, mock_access
124 ):
125 PATH = '/usr/local/bin:/usr/bin:/bin'
126 mock_env.return_value = PATH
127 mock_exists.return_value = False
128 with self._caplog.at_level(logging.DEBUG):
129 assert Path.which('file') is None
130 assert ('Looking for file in ' + PATH) in self._caplog.text
131
132 def test_access_invalid_mode(self):
133 with raises(ValueError) as issue:
134 Path.access("/some/non-existent-file/", 128)
135 assert '0x80' in format(issue.value)
136
137 def test_access_with_non_existent_file(self):
138 non_existent = "/some/file/that/should/not/exist"
139 with raises(KiwiFileAccessError) as issue:
140 Path.access(non_existent, os.F_OK)
141 assert non_existent in issue.value.message
142
143 @patch('os.stat')
144 @patch('os.access')
145 def test_access_with_args(self, mock_access, mock_stat):
146 mock_access.return_value = True
147
148 fname = "arbitrary"
149 mode = os.R_OK
150 assert Path.access(fname, mode, effective_ids=True)
151
152 mock_stat.assert_called_once_with(fname)
153 mock_access.assert_called_once_with(fname, mode, effective_ids=True)
154
155 def test_first_exists(self):
156 assert Path.first_exists('/') == '/'
157 assert Path.first_exists('/etc/foo/bar') == '/etc'
158 assert Path.first_exists('artificial') == '.'
159 assert Path.first_exists('foo/bar') == '.'
160 assert Path.first_exists('/x/y/z') == '/'