|
| 1 | +import unittest |
| 2 | + |
| 3 | +from os.path import join |
| 4 | +from unittest import mock |
| 5 | + |
| 6 | +from pythonforandroid.recipes.hostpython3 import ( |
| 7 | + HOSTPYTHON_VERSION_UNSET_MESSAGE, SETUP_DIST_NOT_FIND_MESSAGE, |
| 8 | +) |
| 9 | +from pythonforandroid.util import BuildInterruptingException |
| 10 | +from tests.recipes.recipe_lib_test import RecipeCtx |
| 11 | + |
| 12 | + |
| 13 | +class TestHostPython3Recipe(RecipeCtx, unittest.TestCase): |
| 14 | + """ |
| 15 | + TestCase for recipe :mod:`~pythonforandroid.recipes.hostpython3` |
| 16 | + """ |
| 17 | + recipe_name = "hostpython3" |
| 18 | + |
| 19 | + def test_property__exe_name_no_version(self): |
| 20 | + hostpython_version = self.recipe.version |
| 21 | + self.recipe._version = None |
| 22 | + with self.assertRaises(BuildInterruptingException) as e: |
| 23 | + py_exe = self.recipe._exe_name # noqa: F841 |
| 24 | + self.assertEqual(e.exception.args[0], HOSTPYTHON_VERSION_UNSET_MESSAGE) |
| 25 | + # restore recipe's version or we will get failures with other test, |
| 26 | + # since we share `self.recipe with all the tests of the class |
| 27 | + self.recipe._version = hostpython_version |
| 28 | + |
| 29 | + def test_property__exe_name(self): |
| 30 | + self.assertEqual(self.recipe._exe_name, 'python3') |
| 31 | + |
| 32 | + def test_property_python_exe(self): |
| 33 | + self.assertEqual( |
| 34 | + self.recipe.python_exe, |
| 35 | + join(self.recipe.get_path_to_python(), 'python3') |
| 36 | + ) |
| 37 | + |
| 38 | + @mock.patch("pythonforandroid.recipes.hostpython3.Path.exists") |
| 39 | + def test_should_build(self, mock_exists): |
| 40 | + # test case for existing python exe which shouldn't trigger the build |
| 41 | + mock_exists.return_value = True |
| 42 | + self.assertFalse(self.recipe.should_build(self.arch)) |
| 43 | + # test case for existing python exe which should trigger the build |
| 44 | + mock_exists.return_value = False |
| 45 | + self.assertTrue(self.recipe.should_build(self.arch)) |
| 46 | + |
| 47 | + @mock.patch("pythonforandroid.util.chdir") |
| 48 | + @mock.patch("pythonforandroid.util.makedirs") |
| 49 | + def test_build_arch(self, mock_makedirs, mock_chdir): |
| 50 | + """ |
| 51 | + Test case for |
| 52 | + :meth:`~pythonforandroid.recipes.python3.HostPython3Recipe.build_arch`, |
| 53 | + where we simulate the build for Python 3.8+. |
| 54 | + """ |
| 55 | + |
| 56 | + with mock.patch( |
| 57 | + "pythonforandroid.recipes.hostpython3.Path.exists" |
| 58 | + ) as mock_path_exists, mock.patch( |
| 59 | + "pythonforandroid.recipes.hostpython3.sh.Command" |
| 60 | + ) as mock_sh_command, mock.patch( |
| 61 | + "pythonforandroid.recipes.hostpython3.sh.make" |
| 62 | + ) as mock_make, mock.patch( |
| 63 | + "pythonforandroid.recipes.hostpython3.Path.is_file" |
| 64 | + ) as mock_path_isfile, mock.patch( |
| 65 | + "pythonforandroid.recipes.hostpython3.sh.cp" |
| 66 | + ) as mock_sh_cp: |
| 67 | + # here we simulate the expected behaviour for Python 3.8+ |
| 68 | + mock_path_exists.side_effect = [ |
| 69 | + False, # "config.status" not exists, so we trigger.configure |
| 70 | + False, # "Modules/Setup.dist" shouldn't exist (3.8+ case) |
| 71 | + True, # "Modules/Setup" exists, so we skip raise exception |
| 72 | + ] |
| 73 | + self.recipe.build_arch(self.arch) |
| 74 | + |
| 75 | + # make sure that the mocked methods are actually called |
| 76 | + mock_path_exists.assert_called() |
| 77 | + recipe_src = self.recipe.get_build_dir(self.arch.arch) |
| 78 | + self.assertIn( |
| 79 | + mock.call(f"{recipe_src}/configure"), |
| 80 | + mock_sh_command.mock_calls, |
| 81 | + ) |
| 82 | + mock_make.assert_called() |
| 83 | + |
| 84 | + exe = join(self.recipe.get_path_to_python(), 'python.exe') |
| 85 | + mock_path_isfile.assert_called() |
| 86 | + |
| 87 | + self.assertEqual(mock_sh_cp.call_count, 1) |
| 88 | + mock_call_args, mock_call_kwargs = mock_sh_cp.call_args_list[0] |
| 89 | + self.assertEqual(mock_call_args[0], exe) |
| 90 | + self.assertEqual(mock_call_args[1], self.recipe.python_exe) |
| 91 | + mock_makedirs.assert_called() |
| 92 | + mock_chdir.assert_called() |
| 93 | + |
| 94 | + @mock.patch("pythonforandroid.util.chdir") |
| 95 | + @mock.patch("pythonforandroid.util.makedirs") |
| 96 | + def test_build_arch_python_lower_than_3_8(self, mock_makedirs, mock_chdir): |
| 97 | + """ |
| 98 | + Test case for |
| 99 | + :meth:`~pythonforandroid.recipes.python3.HostPython3Recipe.build_arch`, |
| 100 | + where we simulate a Python 3.7 build. Here we copy an extra file: |
| 101 | + - Modules/Setup.dist -> Modules/Setup. |
| 102 | +
|
| 103 | + .. note:: We omit some checks because we already dit that at |
| 104 | + `test_build_arch`. Also we skip configure command for the |
| 105 | + same reason. |
| 106 | + """ |
| 107 | + with mock.patch( |
| 108 | + "pythonforandroid.recipes.hostpython3.Path.exists" |
| 109 | + ) as mock_path_exists, mock.patch( |
| 110 | + "pythonforandroid.recipes.hostpython3.sh.make" |
| 111 | + ) as mock_make, mock.patch( |
| 112 | + "pythonforandroid.recipes.hostpython3.Path.is_file" |
| 113 | + ) as mock_path_isfile, mock.patch( |
| 114 | + "pythonforandroid.recipes.hostpython3.sh.cp" |
| 115 | + ) as mock_sh_cp: |
| 116 | + mock_path_exists.side_effect = [ |
| 117 | + True, # simulate that "config.status" exists to skip configure |
| 118 | + True, # "Modules/Setup.dist" exist for Python 3.7 |
| 119 | + True, # "Modules/Setup" exists...we skip raise exception |
| 120 | + ] |
| 121 | + self.recipe.build_arch(self.arch) |
| 122 | + |
| 123 | + build_dir = join( |
| 124 | + self.recipe.get_build_dir(self.arch.arch), self.recipe.build_subdir |
| 125 | + ) |
| 126 | + # we expect two calls to copy command, The one described below and the |
| 127 | + # copy of the python binary which already chechet at `test_build_arch`. |
| 128 | + self.assertEqual(mock_sh_cp.call_count, 2) |
| 129 | + mock_call_args, mock_call_kwargs = mock_sh_cp.call_args_list[0] |
| 130 | + self.assertEqual(mock_call_args[0], "Modules/Setup.dist") |
| 131 | + self.assertEqual(mock_call_args[1], join(build_dir, "Modules/Setup")) |
| 132 | + |
| 133 | + mock_path_exists.assert_called() |
| 134 | + mock_make.assert_called() |
| 135 | + mock_path_isfile.assert_called() |
| 136 | + mock_makedirs.assert_called() |
| 137 | + mock_chdir.assert_called() |
| 138 | + |
| 139 | + @mock.patch("pythonforandroid.util.chdir") |
| 140 | + @mock.patch("pythonforandroid.util.makedirs") |
| 141 | + def test_build_arch_setup_dist_exception(self, mock_makedirs, mock_chdir): |
| 142 | + """ |
| 143 | + Test case for |
| 144 | + :meth:`~pythonforandroid.recipes.python3.HostPython3Recipe.build_arch`, |
| 145 | + where we simulate that the sources hasn't Setup.dist file, which should |
| 146 | + raise an exception. |
| 147 | +
|
| 148 | + .. note:: We skip configure command because already tested at |
| 149 | + `test_build_arch`. |
| 150 | + """ |
| 151 | + with mock.patch( |
| 152 | + "pythonforandroid.recipes.hostpython3.Path.exists" |
| 153 | + ) as mock_path_exists: |
| 154 | + mock_path_exists.side_effect = [ |
| 155 | + True, # simulate that "config.status" exists to skip configure |
| 156 | + False, # "Modules/Setup.dist" shouldn't exist (3.8+ case) |
| 157 | + False, # "Modules/Setup" doesn't exists...raise exception |
| 158 | + ] |
| 159 | + |
| 160 | + with self.assertRaises(BuildInterruptingException) as e: |
| 161 | + self.recipe.build_arch(self.arch) |
| 162 | + self.assertEqual(e.exception.args[0], SETUP_DIST_NOT_FIND_MESSAGE) |
| 163 | + mock_makedirs.assert_called() |
| 164 | + mock_chdir.assert_called() |
0 commit comments