|
| 1 | + |
| 2 | +# This test is a special case that we expect to run under Python 2, so |
| 3 | +# include the necessary compatibility imports: |
| 4 | +try: # Python 3 |
| 5 | + from unittest import mock |
| 6 | +except ImportError: # Python 2 |
| 7 | + import mock |
| 8 | + |
| 9 | +from pythonforandroid.recommendations import PY2_ERROR_TEXT |
| 10 | +from pythonforandroid import entrypoints |
| 11 | + |
| 12 | + |
| 13 | +def test_main_python2(): |
| 14 | + """Test that running under Python 2 leads to the build failing, while |
| 15 | + running under a suitable version works fine. |
| 16 | +
|
| 17 | + Note that this test must be run *using* Python 2 to truly test |
| 18 | + that p4a can reach the Python version check before importing some |
| 19 | + Python-3-only syntax and crashing. |
| 20 | + """ |
| 21 | + |
| 22 | + # Under Python 2, we should get a normal control flow exception |
| 23 | + # that is handled properly, not any other type of crash |
| 24 | + with mock.patch('sys.version_info') as fake_version_info, \ |
| 25 | + mock.patch('pythonforandroid.entrypoints.handle_build_exception') as handle_build_exception: |
| 26 | + |
| 27 | + fake_version_info.major = 2 |
| 28 | + fake_version_info.minor = 7 |
| 29 | + |
| 30 | + def check_python2_exception(exc): |
| 31 | + """Check that the exception message is Python 2 specific.""" |
| 32 | + assert exc.message == PY2_ERROR_TEXT |
| 33 | + handle_build_exception.side_effect = check_python2_exception |
| 34 | + |
| 35 | + entrypoints.main() |
| 36 | + |
| 37 | + handle_build_exception.assert_called_once() |
0 commit comments