-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathtest_recommendations.py
254 lines (231 loc) · 9.38 KB
/
test_recommendations.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
import unittest
from os.path import join
from sys import version as py_version
import packaging.version
from unittest import mock
from pythonforandroid.recommendations import (
check_ndk_api,
check_ndk_version,
check_target_api,
read_ndk_version,
check_python_version,
print_recommendations,
MAX_NDK_VERSION,
RECOMMENDED_NDK_VERSION,
RECOMMENDED_TARGET_API,
MIN_NDK_API,
MIN_NDK_VERSION,
NDK_DOWNLOAD_URL,
ARMEABI_MAX_TARGET_API,
MIN_TARGET_API,
UNKNOWN_NDK_MESSAGE,
PARSE_ERROR_NDK_MESSAGE,
READ_ERROR_NDK_MESSAGE,
ENSURE_RIGHT_NDK_MESSAGE,
NDK_LOWER_THAN_SUPPORTED_MESSAGE,
UNSUPPORTED_NDK_API_FOR_ARMEABI_MESSAGE,
CURRENT_NDK_VERSION_MESSAGE,
RECOMMENDED_NDK_VERSION_MESSAGE,
TARGET_NDK_API_GREATER_THAN_TARGET_API_MESSAGE,
OLD_NDK_API_MESSAGE,
NEW_NDK_MESSAGE,
OLD_API_MESSAGE,
MIN_PYTHON_MAJOR_VERSION,
MIN_PYTHON_MINOR_VERSION,
PY2_ERROR_TEXT,
PY_VERSION_ERROR_TEXT,
)
from pythonforandroid.util import BuildInterruptingException
running_in_py2 = int(py_version[0]) < 3
class TestRecommendations(unittest.TestCase):
"""
An inherited class of `unittest.TestCase`to test the module
:mod:`~pythonforandroid.recommendations`.
"""
def setUp(self):
self.ndk_dir = "/opt/android/android-ndk"
@unittest.skipIf(running_in_py2, "`assertLogs` requires Python 3.4+")
@mock.patch("pythonforandroid.recommendations.read_ndk_version")
def test_check_ndk_version_greater_than_recommended(self, mock_read_ndk):
_version_string = f"{MIN_NDK_VERSION + 1}.0.5232133"
mock_read_ndk.return_value = packaging.version.Version(_version_string)
with self.assertLogs(level="INFO") as cm:
check_ndk_version(self.ndk_dir)
mock_read_ndk.assert_called_once_with(self.ndk_dir)
self.assertEqual(
cm.output,
[
"INFO:p4a:[INFO]: {}".format(
CURRENT_NDK_VERSION_MESSAGE.format(
ndk_version=MAX_NDK_VERSION + 1
)
),
"WARNING:p4a:[WARNING]: {}".format(
RECOMMENDED_NDK_VERSION_MESSAGE.format(
recommended_ndk_version=RECOMMENDED_NDK_VERSION
)
),
"WARNING:p4a:[WARNING]: {}".format(NEW_NDK_MESSAGE),
],
)
@mock.patch("pythonforandroid.recommendations.read_ndk_version")
def test_check_ndk_version_lower_than_recommended(self, mock_read_ndk):
_version_string = f"{MIN_NDK_VERSION - 1}.0.5232133"
mock_read_ndk.return_value = packaging.version.Version(_version_string)
with self.assertRaises(BuildInterruptingException) as e:
check_ndk_version(self.ndk_dir)
self.assertEqual(
e.exception.args[0],
NDK_LOWER_THAN_SUPPORTED_MESSAGE.format(
min_supported=MIN_NDK_VERSION, ndk_url=NDK_DOWNLOAD_URL
),
)
mock_read_ndk.assert_called_once_with(self.ndk_dir)
@unittest.skipIf(running_in_py2, "`assertLogs` requires Python 3.4+")
def test_check_ndk_version_error(self):
"""
Test that a fake ndk dir give us two messages:
- first should be an `INFO` log
- second should be an `WARNING` log
"""
with self.assertLogs(level="INFO") as cm:
check_ndk_version(self.ndk_dir)
self.assertEqual(
cm.output,
[
"INFO:p4a:[INFO]: {}".format(UNKNOWN_NDK_MESSAGE),
"WARNING:p4a:[WARNING]: {}".format(
READ_ERROR_NDK_MESSAGE.format(ndk_dir=self.ndk_dir)
),
"WARNING:p4a:[WARNING]: {}".format(
ENSURE_RIGHT_NDK_MESSAGE.format(
min_supported=MIN_NDK_VERSION,
rec_version=RECOMMENDED_NDK_VERSION,
ndk_url=NDK_DOWNLOAD_URL,
)
),
],
)
@mock.patch("pythonforandroid.recommendations.open")
def test_read_ndk_version(self, mock_open_src_prop):
mock_open_src_prop.side_effect = [
mock.mock_open(
read_data="Pkg.Revision = 17.2.4988734"
).return_value
]
version = read_ndk_version(self.ndk_dir)
mock_open_src_prop.assert_called_once_with(
join(self.ndk_dir, "source.properties")
)
assert version.major == 17
assert version.minor == 2
assert version.micro == 4988734
@unittest.skipIf(running_in_py2, "`assertLogs` requires Python 3.4+")
@mock.patch("pythonforandroid.recommendations.open")
def test_read_ndk_version_error(self, mock_open_src_prop):
mock_open_src_prop.side_effect = [
mock.mock_open(read_data="").return_value
]
with self.assertLogs(level="INFO") as cm:
version = read_ndk_version(self.ndk_dir)
self.assertEqual(
cm.output,
["INFO:p4a:[INFO]: {}".format(PARSE_ERROR_NDK_MESSAGE)],
)
mock_open_src_prop.assert_called_once_with(
join(self.ndk_dir, "source.properties")
)
assert version is None
def test_check_target_api_error_arch_armeabi(self):
with self.assertRaises(BuildInterruptingException) as e:
check_target_api(RECOMMENDED_TARGET_API, "armeabi")
self.assertEqual(
e.exception.args[0],
UNSUPPORTED_NDK_API_FOR_ARMEABI_MESSAGE.format(
req_ndk_api=RECOMMENDED_TARGET_API,
max_ndk_api=ARMEABI_MAX_TARGET_API,
),
)
@unittest.skipIf(running_in_py2, "`assertLogs` requires Python 3.4+")
def test_check_target_api_warning_target_api(self):
with self.assertLogs(level="INFO") as cm:
check_target_api(MIN_TARGET_API - 1, MIN_TARGET_API)
self.assertEqual(
cm.output,
[
"WARNING:p4a:[WARNING]: Target API 29 < 30",
"WARNING:p4a:[WARNING]: {old_api_msg}".format(
old_api_msg=OLD_API_MESSAGE
),
],
)
def test_check_ndk_api_error_android_api(self):
"""
Given an `android api` greater than an `ndk_api`, we should get an
`BuildInterruptingException`.
"""
ndk_api = MIN_NDK_API + 1
android_api = MIN_NDK_API
with self.assertRaises(BuildInterruptingException) as e:
check_ndk_api(ndk_api, android_api)
self.assertEqual(
e.exception.args[0],
TARGET_NDK_API_GREATER_THAN_TARGET_API_MESSAGE.format(
ndk_api=ndk_api, android_api=android_api
),
)
@unittest.skipIf(running_in_py2, "`assertLogs` requires Python 3.4+")
def test_check_ndk_api_warning_old_ndk(self):
"""
Given an `android api` lower than the supported by p4a, we should
get an `BuildInterruptingException`.
"""
ndk_api = MIN_NDK_API - 1
android_api = RECOMMENDED_TARGET_API
with self.assertLogs(level="INFO") as cm:
check_ndk_api(ndk_api, android_api)
self.assertEqual(
cm.output,
[
"WARNING:p4a:[WARNING]: {}".format(
OLD_NDK_API_MESSAGE.format(MIN_NDK_API)
)
],
)
def test_check_python_version(self):
"""With any version info lower than the minimum, we should get a
BuildInterruptingException with an appropriate message.
"""
with mock.patch('sys.version_info') as fake_version_info:
# Major version is Python 2 => exception
fake_version_info.major = MIN_PYTHON_MAJOR_VERSION - 1
fake_version_info.minor = MIN_PYTHON_MINOR_VERSION
with self.assertRaises(BuildInterruptingException) as context:
check_python_version()
assert context.exception.message == PY2_ERROR_TEXT
# Major version too low => exception
# Using a float valued major version just to test the logic and avoid
# clashing with the Python 2 check
fake_version_info.major = MIN_PYTHON_MAJOR_VERSION - 0.1
fake_version_info.minor = MIN_PYTHON_MINOR_VERSION
with self.assertRaises(BuildInterruptingException) as context:
check_python_version()
assert context.exception.message == PY_VERSION_ERROR_TEXT
# Minor version too low => exception
fake_version_info.major = MIN_PYTHON_MAJOR_VERSION
fake_version_info.minor = MIN_PYTHON_MINOR_VERSION - 1
with self.assertRaises(BuildInterruptingException) as context:
check_python_version()
assert context.exception.message == PY_VERSION_ERROR_TEXT
# Version high enough => nothing interesting happens
fake_version_info.major = MIN_PYTHON_MAJOR_VERSION
fake_version_info.minor = MIN_PYTHON_MINOR_VERSION
check_python_version()
def test_print_recommendations(self):
"""
Simple test that the function actually runs.
"""
# The main failure mode is if the function tries to print a variable
# that doesn't actually exist, so simply running to check all the
# prints work is the most important test.
print_recommendations()