Skip to content

Commit 8637a20

Browse files
committed
Added tests for Python version checking
1 parent 00d12b5 commit 8637a20

File tree

4 files changed

+64
-17
lines changed

4 files changed

+64
-17
lines changed

pythonforandroid/recommendations.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,12 @@ def check_ndk_api(ndk_api, android_api):
192192
minor=MIN_PYTHON_MINOR_VERSION))
193193
PY2_ERROR_TEXT = (
194194
'python-for-android no longer supports running under Python 2. Either upgrade to '
195-
'Python {min_version} (recommended), or revert to python-for-android 2019.07.08. Note that '
196-
'you *can* still target Python 2 on Android by including python2 in your requirements.').format(
195+
'Python {min_version} or higher (recommended), or revert to python-for-android 2019.07.08. '
196+
'Note that you *can* still target Python 2 on Android by including python2 in your '
197+
'requirements.').format(
197198
min_version=MIN_PYTHON_VERSION)
198199

199-
PY_MINOR_VERSION_ERROR_TEXT = (
200+
PY_VERSION_ERROR_TEXT = (
200201
'Your Python version {user_major}.{user_minor} is not supported by python-for-android, '
201202
'please upgrade to {min_version} or higher.'
202203
).format(
@@ -206,8 +207,12 @@ def check_ndk_api(ndk_api, android_api):
206207

207208

208209
def check_python_version():
209-
if sys.version_info.major < MIN_PYTHON_MAJOR_VERSION:
210+
# Python 2 special cased because it's a major transition. In the
211+
# future the major or minor versions can increment more quietly.
212+
if sys.version_info.major == 2:
210213
raise BuildInterruptingException(PY2_ERROR_TEXT)
211214

212-
if sys.version_info.minor < MIN_PYTHON_MINOR_VERSION:
213-
raise BuildInterruptingException(PY_MINOR_VERSION_ERROR_TEXT)
215+
if (sys.version_info.major < MIN_PYTHON_MAJOR_VERSION or
216+
sys.version_info.minor < MIN_PYTHON_MINOR_VERSION):
217+
218+
raise BuildInterruptingException(PY_VERSION_ERROR_TEXT)

tests/test_androidmodule_ctypes_finder.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11

2-
import mock
3-
from mock import MagicMock
2+
# This test is still expected to support Python 2, as it tests
3+
# on-Android functionality that we still maintain
4+
try: # Python 3+
5+
from unittest import mock
6+
from unittest.mock import MagicMock
7+
except: # Python 2
8+
import mock
9+
from mock import MagicMock
410
import os
511
import shutil
612
import sys

tests/test_recommendations.py

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
import unittest
22
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
116
from pythonforandroid.recommendations import (
127
check_ndk_api,
138
check_ndk_version,
149
check_target_api,
1510
read_ndk_version,
11+
check_python_version,
1612
MAX_NDK_VERSION,
1713
RECOMMENDED_NDK_VERSION,
1814
RECOMMENDED_TARGET_API,
@@ -33,7 +29,13 @@
3329
OLD_NDK_API_MESSAGE,
3430
NEW_NDK_MESSAGE,
3531
OLD_API_MESSAGE,
32+
MIN_PYTHON_MAJOR_VERSION,
33+
MIN_PYTHON_MINOR_VERSION,
34+
PY2_ERROR_TEXT,
35+
PY_VERSION_ERROR_TEXT,
3636
)
37+
import pythonforandroid.recommendations # for mocking constants only, other imports explicit
38+
3739
from pythonforandroid.util import BuildInterruptingException
3840

3941
running_in_py2 = int(py_version[0]) < 3
@@ -202,3 +204,37 @@ def test_check_ndk_api_warning_old_ndk(self):
202204
)
203205
],
204206
)
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()

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ setenv =
1717
PYTHONPATH={toxinidir}
1818

1919
[testenv:py27]
20-
commands = pytest {posargs:tests/test_androidmodule_ctypes_finder.py}
20+
commands = pytest {posargs:tests/test_androidmodule_ctypes_finder.py tests/test_entrypoints_python2.py}
2121

2222
[testenv:py3]
2323
# for py3 env we will get code coverage

0 commit comments

Comments
 (0)