-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
test_distribution.py
274 lines (251 loc) · 11.1 KB
/
test_distribution.py
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
import os
import json
import unittest
from unittest import mock
from pythonforandroid.bootstrap import Bootstrap
from pythonforandroid.distribution import Distribution
from pythonforandroid.recipe import Recipe
from pythonforandroid.util import BuildInterruptingException
from pythonforandroid.build import Context
dist_info_data = {
"dist_name": "sdl2_dist",
"bootstrap": "sdl2",
"archs": ["armeabi", "armeabi-v7a", "x86", "x86_64", "arm64-v8a"],
"ndk_api": 21,
"use_setup_py": False,
"recipes": ["hostpython3", "python3", "sdl2", "kivy", "requests"],
"hostpython": "/some/fake/hostpython3",
"python_version": "3.7",
}
class TestDistribution(unittest.TestCase):
"""
An inherited class of `unittest.TestCase`to test the module
:mod:`~pythonforandroid.distribution`.
"""
TEST_ARCH = 'armeabi-v7a'
def setUp(self):
"""Configure a :class:`~pythonforandroid.build.Context` so we can
perform our unittests"""
self.ctx = Context()
self.ctx.ndk_api = 21
self.ctx.android_api = 27
self.ctx._sdk_dir = "/opt/android/android-sdk"
self.ctx._ndk_dir = "/opt/android/android-ndk"
self.ctx.setup_dirs(os.getcwd())
self.ctx.recipe_build_order = [
"hostpython3",
"python3",
"sdl2",
"kivy",
]
def setUp_distribution_with_bootstrap(self, bs, **kwargs):
"""Extend the setUp by configuring a distribution, because some test
needs a distribution to be set to be properly tested"""
self.ctx.bootstrap = bs
self.ctx.bootstrap.distribution = Distribution.get_distribution(
self.ctx,
name=kwargs.pop("name", "test_prj"),
recipes=kwargs.pop("recipes", ["python3", "kivy"]),
archs=[self.TEST_ARCH],
**kwargs
)
def tearDown(self):
"""Here we make sure that we reset a possible bootstrap created in
`setUp_distribution_with_bootstrap`"""
self.ctx.bootstrap = None
def test_properties(self):
"""Test that some attributes has the expected result (for now, we check
that `__repr__` and `__str__` return the proper values"""
self.setUp_distribution_with_bootstrap(
Bootstrap().get_bootstrap("sdl2", self.ctx)
)
distribution = self.ctx.bootstrap.distribution
self.assertEqual(self.ctx, distribution.ctx)
expected_repr = (
"<Distribution: name test_prj with recipes (python3, kivy)>"
)
self.assertEqual(distribution.__str__(), expected_repr)
self.assertEqual(distribution.__repr__(), expected_repr)
@mock.patch("pythonforandroid.distribution.exists")
def test_folder_exist(self, mock_exists):
"""Test that method
:meth:`~pythonforandroid.distribution.Distribution.folder_exist` is
called once with the proper arguments."""
mock_exists.return_value = False
self.setUp_distribution_with_bootstrap(
Bootstrap.get_bootstrap("sdl2", self.ctx)
)
self.ctx.bootstrap.distribution.folder_exists()
mock_exists.assert_called_with(
self.ctx.bootstrap.distribution.dist_dir
)
@mock.patch("pythonforandroid.distribution.rmdir")
def test_delete(self, mock_rmdir):
"""Test that method
:meth:`~pythonforandroid.distribution.Distribution.delete` is
called once with the proper arguments."""
self.setUp_distribution_with_bootstrap(
Bootstrap().get_bootstrap("sdl2", self.ctx)
)
self.ctx.bootstrap.distribution.delete()
mock_rmdir.assert_called_once_with(
self.ctx.bootstrap.distribution.dist_dir
)
@mock.patch("pythonforandroid.distribution.exists")
def test_get_distribution_no_name(self, mock_exists):
"""Test that method
:meth:`~pythonforandroid.distribution.Distribution.get_distribution`
returns the proper result which should `unnamed_dist_1`."""
mock_exists.return_value = False
self.ctx.bootstrap = Bootstrap().get_bootstrap("sdl2", self.ctx)
dist = Distribution.get_distribution(self.ctx, archs=[self.TEST_ARCH])
self.assertEqual(dist.name, "unnamed_dist_1")
@mock.patch("pythonforandroid.util.chdir")
@mock.patch("pythonforandroid.distribution.open", create=True)
def test_save_info(self, mock_open_dist_info, mock_chdir):
"""Test that method
:meth:`~pythonforandroid.distribution.Distribution.save_info`
is called once with the proper arguments."""
self.setUp_distribution_with_bootstrap(
Bootstrap().get_bootstrap("sdl2", self.ctx)
)
self.ctx.hostpython = "/some/fake/hostpython3"
self.ctx.python_recipe = Recipe.get_recipe("python3", self.ctx)
self.ctx.python_modules = ["requests"]
mock_open_dist_info.side_effect = [
mock.mock_open(read_data=json.dumps(dist_info_data)).return_value
]
self.ctx.bootstrap.distribution.save_info("/fake_dir")
mock_open_dist_info.assert_called_once_with("dist_info.json", "w")
mock_open_dist_info.reset_mock()
@mock.patch("pythonforandroid.distribution.open", create=True)
@mock.patch("pythonforandroid.distribution.exists")
@mock.patch("pythonforandroid.distribution.glob.glob")
def test_get_distributions(
self, mock_glob, mock_exists, mock_open_dist_info
):
"""Test that method
:meth:`~pythonforandroid.distribution.Distribution.get_distributions`
returns some expected values:
- A list of instances of class
`~pythonforandroid.distribution.Distribution
- That one of the distributions returned in the result has the
proper values (`name`, `ndk_api` and `recipes`)
"""
self.setUp_distribution_with_bootstrap(
Bootstrap().get_bootstrap("sdl2", self.ctx)
)
mock_glob.return_value = ["sdl2-python3"]
mock_open_dist_info.side_effect = [
mock.mock_open(read_data=json.dumps(dist_info_data)).return_value
]
dists = self.ctx.bootstrap.distribution.get_distributions(self.ctx)
self.assertIsInstance(dists, list)
self.assertEqual(len(dists), 1)
self.assertIsInstance(dists[0], Distribution)
self.assertEqual(dists[0].name, "sdl2_dist")
self.assertEqual(dists[0].dist_dir, "sdl2-python3")
self.assertEqual(dists[0].ndk_api, 21)
self.assertEqual(
dists[0].recipes,
["hostpython3", "python3", "sdl2", "kivy", "requests"],
)
mock_open_dist_info.assert_called_with("sdl2-python3/dist_info.json")
mock_open_dist_info.reset_mock()
@mock.patch("pythonforandroid.distribution.open", create=True)
@mock.patch("pythonforandroid.distribution.exists")
@mock.patch("pythonforandroid.distribution.glob.glob")
def test_get_distributions_error_ndk_api(
self, mock_glob, mock_exists, mock_open_dist_info
):
"""Test method
:meth:`~pythonforandroid.distribution.Distribution.get_distributions`
in case that `ndk_api` is not set..which should return a `None`.
"""
dist_info_data_no_ndk_api = dist_info_data.copy()
dist_info_data_no_ndk_api.pop("ndk_api")
self.setUp_distribution_with_bootstrap(
Bootstrap().get_bootstrap("sdl2", self.ctx)
)
mock_glob.return_value = ["sdl2-python3"]
mock_open_dist_info.side_effect = [
mock.mock_open(
read_data=json.dumps(dist_info_data_no_ndk_api)
).return_value
]
dists = self.ctx.bootstrap.distribution.get_distributions(self.ctx)
self.assertEqual(dists[0].ndk_api, None)
mock_open_dist_info.assert_called_with("sdl2-python3/dist_info.json")
mock_open_dist_info.reset_mock()
@mock.patch("pythonforandroid.distribution.Distribution.get_distributions")
@mock.patch("pythonforandroid.distribution.exists")
@mock.patch("pythonforandroid.distribution.glob.glob")
def test_get_distributions_error_ndk_api_mismatch(
self, mock_glob, mock_exists, mock_get_dists
):
"""Test that method
:meth:`~pythonforandroid.distribution.Distribution.get_distribution`
raises an error in case that we have some distribution already build,
with a given `name` and `ndk_api`, and we try to get another
distribution with the same `name` but different `ndk_api`.
"""
expected_dist = Distribution.get_distribution(
self.ctx,
name="test_prj",
recipes=["python3", "kivy"],
archs=[self.TEST_ARCH],
)
mock_get_dists.return_value = [expected_dist]
mock_glob.return_value = ["sdl2-python3"]
with self.assertRaises(BuildInterruptingException) as e:
self.setUp_distribution_with_bootstrap(
Bootstrap().get_bootstrap("sdl2", self.ctx),
allow_replace_dist=False,
ndk_api=22,
)
self.assertEqual(
e.exception.args[0],
"Asked for dist with name test_prj with recipes (python3, kivy)"
" and NDK API 22, but a dist with this name already exists and has"
" either incompatible recipes (python3, kivy) or NDK API 21",
)
def test_get_distributions_error_extra_dist_dirs(self):
"""Test that method
:meth:`~pythonforandroid.distribution.Distribution.get_distributions`
raises an exception of
:class:`~pythonforandroid.util.BuildInterruptingException` in case that
we supply the kwargs `extra_dist_dirs`.
"""
self.setUp_distribution_with_bootstrap(
Bootstrap().get_bootstrap("sdl2", self.ctx)
)
with self.assertRaises(BuildInterruptingException) as e:
self.ctx.bootstrap.distribution.get_distributions(
self.ctx, extra_dist_dirs=["/fake/extra/dist_dirs"]
)
self.assertEqual(
e.exception.args[0],
"extra_dist_dirs argument to get"
"_distributions is not yet implemented",
)
@mock.patch("pythonforandroid.distribution.Distribution.get_distributions")
def test_get_distributions_possible_dists(self, mock_get_dists):
"""Test that method
:meth:`~pythonforandroid.distribution.Distribution.get_distributions`
returns the proper
`:class:`~pythonforandroid.distribution.Distribution` in case that we
already have it build and we request the same
`:class:`~pythonforandroid.distribution.Distribution`.
"""
expected_dist = Distribution.get_distribution(
self.ctx,
name="test_prj",
recipes=["python3", "kivy"],
archs=[self.TEST_ARCH],
)
mock_get_dists.return_value = [expected_dist]
self.setUp_distribution_with_bootstrap(
Bootstrap().get_bootstrap("sdl2", self.ctx), name="test_prj"
)
dists = self.ctx.bootstrap.distribution.get_distributions(self.ctx)
self.assertEqual(dists[0], expected_dist)