Skip to content

Commit bcd9d66

Browse files
committed
[tests] Add tests for recipe with STL support
1 parent 621cd10 commit bcd9d66

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed

tests/test_recipe.py

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@
44
import warnings
55
from pythonforandroid.build import Context
66
from pythonforandroid.recipe import Recipe, import_recipe
7+
from pythonforandroid.archs import ArchAarch_64
8+
from pythonforandroid.bootstrap import Bootstrap
9+
from test_bootstrap import BaseClassSetupBootstrap
10+
11+
try:
12+
from unittest import mock
13+
except ImportError:
14+
# `Python 2` or lower than `Python 3.3` does not
15+
# have the `unittest.mock` module built-in
16+
import mock
717

818

919
class TestRecipe(unittest.TestCase):
@@ -58,3 +68,120 @@ def test_import_recipe(self):
5868
module = import_recipe(name, pathname)
5969
assert module is not None
6070
assert recorded_warnings == []
71+
72+
73+
class TesSTLRecipe(BaseClassSetupBootstrap, unittest.TestCase):
74+
def setUp(self):
75+
"""
76+
Initialize a Context with a Bootstrap and a Distribution to properly
77+
test a recipe which depends on android's STL library, to do so we reuse
78+
`BaseClassSetupBootstrap`
79+
"""
80+
super(TesSTLRecipe, self).setUp()
81+
self.ctx.bootstrap = Bootstrap().get_bootstrap('sdl2', self.ctx)
82+
self.setUp_distribution_with_bootstrap(self.ctx.bootstrap)
83+
self.ctx.python_recipe = Recipe.get_recipe('python3', self.ctx)
84+
85+
def test_get_stl_lib_dir(self):
86+
"""
87+
Test that :meth:`~pythonforandroid.recipe.STLRecipe.get_stl_lib_dir`
88+
returns the expected path for the stl library
89+
"""
90+
arch = ArchAarch_64(self.ctx)
91+
recipe = Recipe.get_recipe('icu', self.ctx)
92+
self.assertTrue(recipe.need_stl_shared)
93+
self.assertEqual(
94+
recipe.get_stl_lib_dir(arch),
95+
os.path.join(
96+
self.ctx.ndk_dir,
97+
'sources/cxx-stl/llvm-libc++/libs/{arch}'.format(
98+
arch=arch.arch
99+
),
100+
),
101+
)
102+
103+
@mock.patch('pythonforandroid.archs.find_executable')
104+
@mock.patch('pythonforandroid.build.ensure_dir')
105+
def test_get_recipe_env_with(
106+
self, mock_ensure_dir, mock_find_executable
107+
):
108+
"""
109+
Test that :meth:`~pythonforandroid.recipe.STLRecipe.get_recipe_env`
110+
returns some expected keys and values.
111+
112+
.. note:: We don't check all the env variables, only those one specific
113+
of :class:`~pythonforandroid.recipe.STLRecipe`, the others
114+
should be tested in the proper test.
115+
"""
116+
expected_compiler = 'aarch64-linux-android-gcc'
117+
mock_find_executable.return_value = expected_compiler
118+
mock_ensure_dir.return_value = True
119+
120+
arch = ArchAarch_64(self.ctx)
121+
recipe = Recipe.get_recipe('icu', self.ctx)
122+
assert recipe.need_stl_shared, True
123+
env = recipe.get_recipe_env(arch)
124+
# check `find_executable` calls
125+
mock_find_executable.assert_called_once_with(
126+
expected_compiler, path=os.environ['PATH']
127+
)
128+
self.assertIsInstance(env, dict)
129+
130+
# check `CPPFLAGS`
131+
expected_cppflags = {
132+
'-I{stl_include}'.format(stl_include=recipe.stl_include_dir)
133+
}
134+
self.assertIn('CPPFLAGS', env)
135+
for flags in expected_cppflags:
136+
self.assertIn(flags, env['CPPFLAGS'])
137+
138+
# check `LIBS`
139+
self.assertIn('LDFLAGS', env)
140+
self.assertIn('-L' + recipe.get_stl_lib_dir(arch), env['LDFLAGS'])
141+
self.assertIn('LIBS', env)
142+
self.assertIn('-lc++_shared', env['LIBS'])
143+
144+
# check `CXXFLAGS` and `CXX`
145+
for flag in {'CXXFLAGS', 'CXX'}:
146+
self.assertIn(flag, env)
147+
self.assertIn('-frtti -fexceptions', env[flag])
148+
149+
@mock.patch('pythonforandroid.recipe.Recipe.install_libs')
150+
@mock.patch('pythonforandroid.recipe.isfile')
151+
@mock.patch('pythonforandroid.build.ensure_dir')
152+
def test_install_stl_lib(
153+
self, mock_ensure_dir, mock_isfile, mock_install_lib
154+
):
155+
"""
156+
Test that :meth:`~pythonforandroid.recipe.STLRecipe.install_stl_lib`,
157+
calls the method :meth:`~pythonforandroid.recipe.Recipe.install_libs`
158+
with the proper arguments: a subclass of
159+
:class:`~pythonforandroid.archs.Arch` and our stl lib
160+
(:attr:`~pythonforandroid.recipe.STLRecipe.stl_lib_name`)
161+
"""
162+
mock_ensure_dir.return_value = True
163+
mock_isfile.return_value = False
164+
mock_install_lib.return_value = True
165+
166+
arch = ArchAarch_64(self.ctx)
167+
recipe = Recipe.get_recipe('icu', self.ctx)
168+
recipe.ctx = self.ctx
169+
assert recipe.need_stl_shared, True
170+
recipe.install_stl_lib(arch)
171+
mock_install_lib.assert_called_once_with(
172+
arch,
173+
'{ndk_dir}/sources/cxx-stl/llvm-libc++/'
174+
'libs/{arch}/lib{stl_lib}.so'.format(
175+
ndk_dir=self.ctx.ndk_dir,
176+
arch=arch.arch,
177+
stl_lib=recipe.stl_lib_name,
178+
),
179+
)
180+
181+
@mock.patch('pythonforandroid.recipe.Recipe.install_stl_lib')
182+
def test_postarch_build(self, mock_install_stl_lib):
183+
arch = ArchAarch_64(self.ctx)
184+
recipe = Recipe.get_recipe('icu', self.ctx)
185+
assert recipe.need_stl_shared, True
186+
recipe.postbuild_arch(arch)
187+
mock_install_stl_lib.assert_called_once_with(arch)

0 commit comments

Comments
 (0)