|
1 | 1 | import unittest |
2 | 2 | from os.path import join |
3 | | -from sys import version as py_version |
4 | | - |
5 | | -try: |
6 | | - from unittest import mock |
7 | | -except ImportError: |
8 | | - # `Python 2` or lower than `Python 3.3` does not |
9 | | - # have the `unittest.mock` module built-in |
10 | | - import mock |
| 3 | +from sys import version_info, version as py_version |
| 4 | + |
| 5 | +from unittest import mock |
11 | 6 | from pythonforandroid.recommendations import ( |
12 | 7 | check_ndk_api, |
13 | 8 | check_ndk_version, |
14 | 9 | check_target_api, |
15 | 10 | read_ndk_version, |
| 11 | + check_python_version, |
16 | 12 | MAX_NDK_VERSION, |
17 | 13 | RECOMMENDED_NDK_VERSION, |
18 | 14 | RECOMMENDED_TARGET_API, |
|
33 | 29 | OLD_NDK_API_MESSAGE, |
34 | 30 | NEW_NDK_MESSAGE, |
35 | 31 | OLD_API_MESSAGE, |
| 32 | + MIN_PYTHON_MAJOR_VERSION, |
| 33 | + MIN_PYTHON_MINOR_VERSION, |
| 34 | + PY2_ERROR_TEXT, |
| 35 | + PY_VERSION_ERROR_TEXT, |
36 | 36 | ) |
| 37 | +import pythonforandroid.recommendations # for mocking constants only, other imports explicit |
| 38 | + |
37 | 39 | from pythonforandroid.util import BuildInterruptingException |
38 | 40 |
|
39 | 41 | running_in_py2 = int(py_version[0]) < 3 |
@@ -202,3 +204,37 @@ def test_check_ndk_api_warning_old_ndk(self): |
202 | 204 | ) |
203 | 205 | ], |
204 | 206 | ) |
| 207 | + |
| 208 | + def test_check_python_version(self): |
| 209 | + """ |
| 210 | + With any version info lower than the minimum, we should get a |
| 211 | + """ |
| 212 | + with mock.patch('sys.version_info') as fake_version_info: |
| 213 | + |
| 214 | + # Major version is Python 2 => exception |
| 215 | + fake_version_info.major = MIN_PYTHON_MAJOR_VERSION - 1 |
| 216 | + fake_version_info.minor = MIN_PYTHON_MINOR_VERSION |
| 217 | + with self.assertRaises(BuildInterruptingException) as context: |
| 218 | + check_python_version() |
| 219 | + assert context.exception.message == PY2_ERROR_TEXT |
| 220 | + |
| 221 | + # Major version too low => exception |
| 222 | + # Using a float valued major version just to test the logic and avoid |
| 223 | + # clashing with the Python 2 check |
| 224 | + fake_version_info.major = MIN_PYTHON_MAJOR_VERSION - 0.1 |
| 225 | + fake_version_info.minor = MIN_PYTHON_MINOR_VERSION |
| 226 | + with self.assertRaises(BuildInterruptingException) as context: |
| 227 | + check_python_version() |
| 228 | + assert context.exception.message == PY_VERSION_ERROR_TEXT |
| 229 | + |
| 230 | + # Minor version too low => exception |
| 231 | + fake_version_info.major = MIN_PYTHON_MAJOR_VERSION |
| 232 | + fake_version_info.minor = MIN_PYTHON_MINOR_VERSION - 1 |
| 233 | + with self.assertRaises(BuildInterruptingException) as context: |
| 234 | + check_python_version() |
| 235 | + assert context.exception.message == PY_VERSION_ERROR_TEXT |
| 236 | + |
| 237 | + # Version high enough => nothing interesting happens |
| 238 | + fake_version_info.major = MIN_PYTHON_MAJOR_VERSION |
| 239 | + fake_version_info.minor = MIN_PYTHON_MINOR_VERSION |
| 240 | + check_python_version() |
0 commit comments