|
| 1 | +import os |
| 2 | + |
| 3 | +import unittest |
| 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 |
| 11 | +from pythonforandroid.bootstrap import Bootstrap |
| 12 | +from pythonforandroid.distribution import Distribution |
| 13 | + |
| 14 | +from pythonforandroid.build import Context |
| 15 | +from pythonforandroid.archs import ArchARMv7_a |
| 16 | +from pythonforandroid.recipes.icu import ICURecipe |
| 17 | +from pythonforandroid.recipe import Recipe |
| 18 | + |
| 19 | + |
| 20 | +class TestIcuRecipe(unittest.TestCase): |
| 21 | + """ |
| 22 | + An unittest for recipe :mod:`~pythonforandroid.recipes.icu` |
| 23 | + """ |
| 24 | + |
| 25 | + ctx = None |
| 26 | + |
| 27 | + def setUp(self): |
| 28 | + self.ctx = Context() |
| 29 | + self.ctx.ndk_api = 21 |
| 30 | + self.ctx.android_api = 27 |
| 31 | + self.ctx._sdk_dir = "/opt/android/android-sdk" |
| 32 | + self.ctx._ndk_dir = "/opt/android/android-ndk" |
| 33 | + self.ctx.setup_dirs(os.getcwd()) |
| 34 | + self.ctx.bootstrap = Bootstrap().get_bootstrap("sdl2", self.ctx) |
| 35 | + self.ctx.bootstrap.distribution = Distribution.get_distribution( |
| 36 | + self.ctx, name="sdl2", recipes=["python3", "kivy", "icu"] |
| 37 | + ) |
| 38 | + self.ctx.python_recipe = ICURecipe.get_recipe("python3", self.ctx) |
| 39 | + self.ctx.recipe_build_order = [ |
| 40 | + "hostpython3", |
| 41 | + "icu", |
| 42 | + "python3", |
| 43 | + "sdl2", |
| 44 | + "kivy", |
| 45 | + ] |
| 46 | + |
| 47 | + def tearDown(self): |
| 48 | + self.ctx = None |
| 49 | + |
| 50 | + def test_url(self): |
| 51 | + recipe = ICURecipe() |
| 52 | + recipe.ctx = self.ctx |
| 53 | + self.assertTrue(recipe.versioned_url.startswith("http")) |
| 54 | + self.assertIn(recipe.version, recipe.versioned_url) |
| 55 | + |
| 56 | + @mock.patch( |
| 57 | + "pythonforandroid.recipe.Recipe.url", new_callable=mock.PropertyMock |
| 58 | + ) |
| 59 | + def test_url_none(self, mock_url): |
| 60 | + mock_url.return_value = None |
| 61 | + recipe = ICURecipe() |
| 62 | + recipe.ctx = self.ctx |
| 63 | + self.assertIsNone(recipe.versioned_url) |
| 64 | + |
| 65 | + def test_get_recipe_dir(self): |
| 66 | + recipe = ICURecipe() |
| 67 | + recipe.ctx = self.ctx |
| 68 | + expected_dir = os.path.join(self.ctx.root_dir, "recipes", "icu") |
| 69 | + self.assertEqual(recipe.get_recipe_dir(), expected_dir) |
| 70 | + |
| 71 | + @mock.patch("pythonforandroid.util.makedirs") |
| 72 | + @mock.patch("pythonforandroid.util.chdir") |
| 73 | + @mock.patch("pythonforandroid.bootstrap.sh.Command") |
| 74 | + @mock.patch("pythonforandroid.recipes.icu.sh.make") |
| 75 | + @mock.patch("pythonforandroid.build.ensure_dir") |
| 76 | + @mock.patch("pythonforandroid.archs.glob") |
| 77 | + @mock.patch("pythonforandroid.archs.find_executable") |
| 78 | + def test_do_build_libs( |
| 79 | + self, |
| 80 | + mock_find_executable, |
| 81 | + mock_archs_glob, |
| 82 | + mock_ensure_dir, |
| 83 | + mock_sh_make, |
| 84 | + mock_sh_command, |
| 85 | + mock_chdir, |
| 86 | + mock_makedirs, |
| 87 | + ): |
| 88 | + recipe = ICURecipe() |
| 89 | + recipe.ctx = self.ctx |
| 90 | + mock_find_executable.return_value = 'arm-linux-androideabi-gcc' |
| 91 | + mock_archs_glob.return_value = [ |
| 92 | + os.path.join(self.ctx._ndk_dir, "toolchains", "llvm") |
| 93 | + ] |
| 94 | + arch = ArchARMv7_a(self.ctx) |
| 95 | + self.ctx.toolchain_prefix = arch.toolchain_prefix |
| 96 | + self.ctx.toolchain_version = "4.9" |
| 97 | + recipe.do_build_libs(arch) |
| 98 | + |
| 99 | + # We expect to calls to `sh.Command` |
| 100 | + build_root = recipe.get_build_dir(arch.arch) |
| 101 | + mock_sh_command.has_calls( |
| 102 | + [ |
| 103 | + mock.call( |
| 104 | + os.path.join(build_root, "source", "runConfigureICU") |
| 105 | + ), |
| 106 | + mock.call(os.path.join(build_root, "source", "configure")), |
| 107 | + ] |
| 108 | + ) |
| 109 | + mock_ensure_dir.assert_called() |
| 110 | + mock_chdir.assert_called() |
| 111 | + # we expect for calls to sh.make command |
| 112 | + expected_host_cppflags = ( |
| 113 | + "-O3 -fno-short-wchar -DU_USING_ICU_NAMESPACE=1 -fno-short-enums " |
| 114 | + "-DU_HAVE_NL_LANGINFO_CODESET=0 -D__STDC_INT64__ -DU_TIMEZONE=0 " |
| 115 | + "-DUCONFIG_NO_LEGACY_CONVERSION=1 " |
| 116 | + "-DUCONFIG_NO_TRANSLITERATION=0 " |
| 117 | + ) |
| 118 | + for call_number, call in enumerate(mock_sh_make.call_args_list): |
| 119 | + # here we expect to find the compile command `make -j`in first and |
| 120 | + # third calls, the others should be the `make install` commands |
| 121 | + is_host_build = call_number in [0, 1] |
| 122 | + is_compile = call_number in [0, 2] |
| 123 | + call_args, call_kwargs = call |
| 124 | + print(call_args) |
| 125 | + # print(call_kwargs) |
| 126 | + self.assertTrue( |
| 127 | + call_args[0].startswith("-j" if is_compile else "install") |
| 128 | + ) |
| 129 | + self.assertIn("_env", call_kwargs) |
| 130 | + if is_host_build: |
| 131 | + self.assertIn( |
| 132 | + expected_host_cppflags, call_kwargs["_env"]["CPPFLAGS"] |
| 133 | + ) |
| 134 | + else: |
| 135 | + self.assertNotIn( |
| 136 | + expected_host_cppflags, call_kwargs["_env"]["CPPFLAGS"] |
| 137 | + ) |
| 138 | + mock_makedirs.assert_called() |
| 139 | + |
| 140 | + mock_find_executable.assert_called_once() |
| 141 | + self.assertEqual( |
| 142 | + mock_find_executable.call_args[0][0], |
| 143 | + mock_find_executable.return_value, |
| 144 | + ) |
| 145 | + |
| 146 | + @mock.patch("pythonforandroid.recipes.icu.sh.cp") |
| 147 | + @mock.patch("pythonforandroid.util.makedirs") |
| 148 | + def test_do_install_libs(self, mock_makedirs, mock_sh_cp): |
| 149 | + arch = ArchARMv7_a(self.ctx) |
| 150 | + recipe = Recipe.get_recipe("icu", self.ctx) |
| 151 | + recipe.ctx = self.ctx |
| 152 | + recipe.do_install_libs(arch) |
| 153 | + mock_makedirs.assert_called() |
| 154 | + mock_sh_cp.assert_called() |
| 155 | + |
| 156 | + @mock.patch("pythonforandroid.recipes.icu.exists") |
| 157 | + def test_get_recipe_dir_with_local_recipes(self, mock_exists): |
| 158 | + self.ctx.local_recipes = "/home/user/p4a_local_recipes" |
| 159 | + |
| 160 | + recipe = ICURecipe() |
| 161 | + recipe.ctx = self.ctx |
| 162 | + recipe_dir = recipe.get_recipe_dir() |
| 163 | + |
| 164 | + expected_dir = os.path.join(self.ctx.local_recipes, "icu") |
| 165 | + self.assertEqual(recipe_dir, expected_dir) |
| 166 | + mock_exists.assert_called_once_with(expected_dir) |
0 commit comments