Skip to content

Commit a4ea0d1

Browse files
committed
[tests] Add tests for icu recipe
1 parent 3e089e6 commit a4ea0d1

File tree

1 file changed

+167
-0
lines changed

1 file changed

+167
-0
lines changed

tests/recipes/test_icu.py

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
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_build_arch(
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 = os.path.join(
91+
self.ctx._ndk_dir,
92+
"toolchains/llvm/prebuilt/linux-x86_64/bin/clang",
93+
)
94+
mock_archs_glob.return_value = [
95+
os.path.join(self.ctx._ndk_dir, "toolchains", "llvm")
96+
]
97+
arch = ArchARMv7_a(self.ctx)
98+
self.ctx.toolchain_prefix = arch.toolchain_prefix
99+
self.ctx.toolchain_version = "4.9"
100+
recipe.build_arch(arch)
101+
102+
# We expect to calls to `sh.Command`
103+
build_root = recipe.get_build_dir(arch.arch)
104+
mock_sh_command.has_calls(
105+
[
106+
mock.call(
107+
os.path.join(build_root, "source", "runConfigureICU")
108+
),
109+
mock.call(os.path.join(build_root, "source", "configure")),
110+
]
111+
)
112+
mock_ensure_dir.assert_called()
113+
mock_chdir.assert_called()
114+
# we expect for calls to sh.make command
115+
expected_host_cppflags = (
116+
"-O3 -fno-short-wchar -DU_USING_ICU_NAMESPACE=1 -fno-short-enums "
117+
"-DU_HAVE_NL_LANGINFO_CODESET=0 -D__STDC_INT64__ -DU_TIMEZONE=0 "
118+
"-DUCONFIG_NO_LEGACY_CONVERSION=1 "
119+
"-DUCONFIG_NO_TRANSLITERATION=0 "
120+
)
121+
for call_number, call in enumerate(mock_sh_make.call_args_list):
122+
# here we expect to find the compile command `make -j`in first and
123+
# third calls, the others should be the `make install` commands
124+
is_host_build = call_number in [0, 1]
125+
is_compile = call_number in [0, 2]
126+
call_args, call_kwargs = call
127+
self.assertTrue(
128+
call_args[0].startswith("-j" if is_compile else "install")
129+
)
130+
self.assertIn("_env", call_kwargs)
131+
if is_host_build:
132+
self.assertIn(
133+
expected_host_cppflags, call_kwargs["_env"]["CPPFLAGS"]
134+
)
135+
else:
136+
self.assertNotIn(
137+
expected_host_cppflags, call_kwargs["_env"]["CPPFLAGS"]
138+
)
139+
mock_makedirs.assert_called()
140+
141+
mock_find_executable.assert_called_once()
142+
self.assertEqual(
143+
mock_find_executable.call_args[0][0],
144+
mock_find_executable.return_value,
145+
)
146+
147+
@mock.patch("pythonforandroid.recipes.icu.sh.cp")
148+
@mock.patch("pythonforandroid.util.makedirs")
149+
def test_install_libraries(self, mock_makedirs, mock_sh_cp):
150+
arch = ArchARMv7_a(self.ctx)
151+
recipe = Recipe.get_recipe("icu", self.ctx)
152+
recipe.ctx = self.ctx
153+
recipe.install_libraries(arch)
154+
mock_makedirs.assert_called()
155+
mock_sh_cp.assert_called()
156+
157+
@mock.patch("pythonforandroid.recipes.icu.exists")
158+
def test_get_recipe_dir_with_local_recipes(self, mock_exists):
159+
self.ctx.local_recipes = "/home/user/p4a_local_recipes"
160+
161+
recipe = ICURecipe()
162+
recipe.ctx = self.ctx
163+
recipe_dir = recipe.get_recipe_dir()
164+
165+
expected_dir = os.path.join(self.ctx.local_recipes, "icu")
166+
self.assertEqual(recipe_dir, expected_dir)
167+
mock_exists.assert_called_once_with(expected_dir)

0 commit comments

Comments
 (0)