forked from kivy/python-for-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_util.py
More file actions
299 lines (255 loc) · 10.5 KB
/
test_util.py
File metadata and controls
299 lines (255 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import os
from pathlib import Path
from tempfile import TemporaryDirectory
import types
import unittest
from unittest import mock
from pythonforandroid import util
class TestUtil(unittest.TestCase):
"""
An inherited class of `unittest.TestCase`to test the module
:mod:`~pythonforandroid.util`.
"""
@mock.patch("pythonforandroid.util.makedirs")
def test_ensure_dir(self, mock_makedirs):
"""
Basic test for method :meth:`~pythonforandroid.util.ensure_dir`. Here
we make sure that the mentioned method is called only once.
"""
util.ensure_dir("fake_directory")
mock_makedirs.assert_called_once_with("fake_directory")
@mock.patch("shutil.rmtree")
@mock.patch("pythonforandroid.util.mkdtemp")
def test_temp_directory(self, mock_mkdtemp, mock_shutil_rmtree):
"""
Basic test for method :meth:`~pythonforandroid.util.temp_directory`. We
perform this test by `mocking` the command `mkdtemp` and
`shutil.rmtree` and we make sure that those functions are called in the
proper place.
"""
mock_mkdtemp.return_value = "/temp/any_directory"
with util.temp_directory():
mock_mkdtemp.assert_called_once()
mock_shutil_rmtree.assert_not_called()
mock_shutil_rmtree.assert_called_once_with("/temp/any_directory")
@mock.patch("pythonforandroid.util.chdir")
def test_current_directory(self, moch_chdir):
"""
Basic test for method :meth:`~pythonforandroid.util.current_directory`.
We `mock` chdir and we check that the command is executed once we are
inside a python's `with` statement. Then we check that `chdir has been
called with the proper arguments inside this `with` statement and also
that, once we leave the `with` statement, is called again with the
current working path.
"""
chdir_dir = "/temp/any_directory"
# test chdir to existing directory
with util.current_directory(chdir_dir):
moch_chdir.assert_called_once_with("/temp/any_directory")
moch_chdir.assert_has_calls(
[mock.call("/temp/any_directory"), mock.call(os.getcwd())]
)
def test_current_directory_exception(self):
"""
Another test for method
:meth:`~pythonforandroid.util.current_directory`, but here we check
that using the method with a non-existing-directory raises an `OSError`
exception.
.. note:: test chdir to non-existing directory, should raise error,
for py3 the exception is FileNotFoundError and IOError for py2, to
avoid introduce conditions, we test with a more generic exception
"""
with self.assertRaises(OSError), util.current_directory(
"/fake/directory"
):
pass
@mock.patch("pythonforandroid.util.walk")
def test_walk_valid_filens(self, mock_walk):
"""
Test method :meth:`~pythonforandroid.util.walk_valid_filens`
In here we simulate the following directory structure:
/fake_dir
|-- README
|-- setup.py
|-- __pycache__
|-- |__
|__Lib
|-- abc.pyc
|-- abc.py
|__ ctypes
|-- util.pyc
|-- util.py
Then we execute the method in order to check that we got the expected
result, which should be:
.. code-block:: python
:emphasize-lines: 2-4
expected_result = {
"/fake_dir/README",
"/fake_dir/Lib/abc.pyc",
"/fake_dir/Lib/ctypes/util.pyc",
}
"""
simulated_walk_result = [
["/fake_dir", ["__pycache__", "Lib"], ["README", "setup.py"]],
["/fake_dir/Lib", ["ctypes"], ["abc.pyc", "abc.py"]],
["/fake_dir/Lib/ctypes", [], ["util.pyc", "util.py"]],
]
mock_walk.return_value = simulated_walk_result
file_ens = util.walk_valid_filens(
"/fake_dir", ["__pycache__"], ["*.py"]
)
self.assertIsInstance(file_ens, types.GeneratorType)
expected_result = {
"/fake_dir/README",
"/fake_dir/Lib/abc.pyc",
"/fake_dir/Lib/ctypes/util.pyc",
}
result = set(file_ens)
self.assertEqual(result, expected_result)
def test_util_exceptions(self):
"""
Test exceptions for a couple of methods:
- method :meth:`~pythonforandroid.util.BuildInterruptingException`
- method :meth:`~pythonforandroid.util.handle_build_exception`
Here we create an exception with method
:meth:`~pythonforandroid.util.BuildInterruptingException` and we run it
inside method :meth:`~pythonforandroid.util.handle_build_exception` to
make sure that it raises an `SystemExit`.
"""
exc = util.BuildInterruptingException(
"missing dependency xxx", instructions="pip install --user xxx"
)
with self.assertRaises(SystemExit):
util.handle_build_exception(exc)
def test_move(self):
with mock.patch(
"pythonforandroid.util.LOGGER"
) as m_logger, TemporaryDirectory() as base_dir:
new_path = Path(base_dir) / "new"
# Set up source
old_path = Path(base_dir) / "old"
with open(old_path, "w") as outfile:
outfile.write("Temporary content")
# Non existent source
with self.assertRaises(FileNotFoundError):
util.move(new_path, new_path)
m_logger.debug.assert_called()
m_logger.error.assert_not_called()
m_logger.reset_mock()
assert old_path.exists()
assert not new_path.exists()
# Successful move
util.move(old_path, new_path)
assert not old_path.exists()
assert new_path.exists()
m_logger.debug.assert_called()
m_logger.error.assert_not_called()
m_logger.reset_mock()
# Move over existing:
existing_path = Path(base_dir) / "existing"
existing_path.touch()
util.move(new_path, existing_path)
with open(existing_path, "r") as infile:
assert infile.read() == "Temporary content"
m_logger.debug.assert_called()
m_logger.error.assert_not_called()
m_logger.reset_mock()
def test_touch(self):
# Just checking the new file case.
# Assume the existing file timestamp case will work if this does.
with TemporaryDirectory() as base_dir:
new_file_path = Path(base_dir) / "new_file"
assert not new_file_path.exists()
util.touch(new_file_path)
assert new_file_path.exists()
def test_build_tools_version_sort_key(self):
build_tools_versions = [
"26.0.1",
"26.0.0",
"26.0.2",
"32.0.0 rc1",
"31.0.0",
"999something",
]
expected_result = [
"999something", # invalid version
"26.0.0",
"26.0.1",
"26.0.2",
"31.0.0",
"32.0.0 rc1",
]
result = sorted(
build_tools_versions, key=util.build_tools_version_sort_key
)
self.assertEqual(result, expected_result)
def test_max_build_tool_version(self):
build_tools_versions = [
"26.0.1",
"26.0.0",
"26.0.2",
"32.0.0 rc1",
"31.0.0",
"999something",
]
expected_result = "32.0.0 rc1"
result = util.max_build_tool_version(build_tools_versions)
self.assertEqual(result, expected_result)
def test_load_source(self):
"""
Test method :meth:`~pythonforandroid.util.load_source`.
We test loading a Python module from a file path using importlib.
"""
with TemporaryDirectory() as temp_dir:
# Create a test module file
test_module_path = Path(temp_dir) / "test_module.py"
with open(test_module_path, "w") as f:
f.write("TEST_VALUE = 42\n")
f.write("def test_function():\n")
f.write(" return 'hello'\n")
# Load the module
loaded_module = util.load_source("test_module", str(test_module_path))
# Verify the module was loaded correctly
self.assertEqual(loaded_module.TEST_VALUE, 42)
self.assertEqual(loaded_module.test_function(), 'hello')
@mock.patch("pythonforandroid.util.exists")
@mock.patch("shutil.rmtree")
def test_rmdir_exists(self, mock_rmtree, mock_exists):
"""
Test method :meth:`~pythonforandroid.util.rmdir` when directory exists.
We mock exists to return True and verify rmtree is called.
"""
mock_exists.return_value = True
util.rmdir("/fake/directory")
mock_rmtree.assert_called_once_with("/fake/directory", False)
@mock.patch("pythonforandroid.util.exists")
@mock.patch("shutil.rmtree")
def test_rmdir_not_exists(self, mock_rmtree, mock_exists):
"""
Test method :meth:`~pythonforandroid.util.rmdir` when directory doesn't exist.
We mock exists to return False and verify rmtree is not called.
"""
mock_exists.return_value = False
util.rmdir("/fake/directory")
mock_rmtree.assert_not_called()
@mock.patch("pythonforandroid.util.exists")
@mock.patch("shutil.rmtree")
def test_rmdir_ignore_errors(self, mock_rmtree, mock_exists):
"""
Test method :meth:`~pythonforandroid.util.rmdir` with ignore_errors flag.
We verify that the ignore_errors parameter is passed to rmtree.
"""
mock_exists.return_value = True
util.rmdir("/fake/directory", ignore_errors=True)
mock_rmtree.assert_called_once_with("/fake/directory", True)
@mock.patch("pythonforandroid.util.mock")
def test_patch_wheel_setuptools_logging(self, mock_mock):
"""
Test method :meth:`~pythonforandroid.util.patch_wheel_setuptools_logging`.
We verify it returns a mock.patch object for the wheel logging module.
"""
mock_patch_obj = mock.Mock()
mock_mock.patch.return_value = mock_patch_obj
result = util.patch_wheel_setuptools_logging()
mock_mock.patch.assert_called_once_with("wheel._setuptools_logging.configure")
self.assertEqual(result, mock_patch_obj)