Skip to content

Commit 2e4e145

Browse files
committed
Rework of test_archs to adapt it to the new ndk-r19 build system
Also add some more tests (now we check if some of the functions we mock, `glob` and `find_executable`, are called with the expected arguments)
1 parent 0c31ee2 commit 2e4e145

File tree

1 file changed

+104
-34
lines changed

1 file changed

+104
-34
lines changed

tests/test_archs.py

Lines changed: 104 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import os
2-
32
import unittest
3+
from os import environ
44

55
try:
66
from unittest import mock
77
except ImportError:
88
# `Python 2` or lower than `Python 3.3` does not
99
# have the `unittest.mock` module built-in
1010
import mock
11+
1112
from pythonforandroid.bootstrap import Bootstrap
1213
from pythonforandroid.distribution import Distribution
1314
from pythonforandroid.recipe import Recipe
@@ -53,6 +54,7 @@ class ArchSetUpBaseClass(object):
5354
"""
5455

5556
ctx = None
57+
expected_compiler = ""
5658

5759
def setUp(self):
5860
self.ctx = Context()
@@ -66,6 +68,12 @@ def setUp(self):
6668
self.ctx, name="sdl2", recipes=["python3", "kivy"]
6769
)
6870
self.ctx.python_recipe = Recipe.get_recipe("python3", self.ctx)
71+
# Here we define the expected compiler, which, as per ndk >= r19,
72+
# should be the same for all the tests (no more gcc compiler)
73+
self.expected_compiler = (
74+
"/opt/android/android-ndk/toolchains/"
75+
"llvm/prebuilt/linux-x86_64/bin/clang"
76+
)
6977

7078

7179
class TestArch(ArchSetUpBaseClass, unittest.TestCase):
@@ -82,11 +90,7 @@ def test_arch(self):
8290
self.assertEqual(
8391
e1.exception.args[0], "'Arch' object has no attribute 'arch'"
8492
)
85-
with self.assertRaises(AttributeError) as e2:
86-
getattr(arch, "target")
87-
self.assertEqual(
88-
e2.exception.args[0], "'NoneType' object has no attribute 'split'"
89-
)
93+
self.assertEqual(arch.target, "None21")
9094
self.assertIsNone(arch.toolchain_prefix)
9195
self.assertIsNone(arch.command_prefix)
9296
self.assertIsInstance(arch.include_dirs, list)
@@ -98,9 +102,10 @@ class TestArchARM(ArchSetUpBaseClass, unittest.TestCase):
98102
will be used to perform tests for :class:`~pythonforandroid.archs.ArchARM`.
99103
"""
100104

105+
@mock.patch("pythonforandroid.archs.glob")
101106
@mock.patch("pythonforandroid.archs.find_executable")
102107
@mock.patch("pythonforandroid.build.ensure_dir")
103-
def test_arch_arm(self, mock_ensure_dir, mock_find_executable):
108+
def test_arch_arm(self, mock_ensure_dir, mock_find_executable, mock_glob):
104109
"""
105110
Test that class :class:`~pythonforandroid.archs.ArchARM` returns some
106111
expected attributes and environment variables.
@@ -115,15 +120,16 @@ def test_arch_arm(self, mock_ensure_dir, mock_find_executable):
115120
not exist)
116121
117122
"""
118-
mock_find_executable.return_value = "arm-linux-androideabi-gcc"
123+
mock_find_executable.return_value = self.expected_compiler
119124
mock_ensure_dir.return_value = True
125+
mock_glob.return_value = ["llvm"]
120126

121127
arch = ArchARM(self.ctx)
122128
self.assertEqual(arch.arch, "armeabi")
123129
self.assertEqual(arch.__str__(), "armeabi")
124130
self.assertEqual(arch.toolchain_prefix, "arm-linux-androideabi")
125131
self.assertEqual(arch.command_prefix, "arm-linux-androideabi")
126-
self.assertEqual(arch.target, "armv7a-none-linux-androideabi")
132+
self.assertEqual(arch.target, "armv7a-linux-androideabi21")
127133
self.assertEqual(arch.platform_dir, "arch-arm")
128134
arch = ArchARM(self.ctx)
129135

@@ -134,9 +140,17 @@ def test_arch_arm(self, mock_ensure_dir, mock_find_executable):
134140
expected_env_gcc_keys, set(env.keys()) & expected_env_gcc_keys
135141
)
136142

143+
# check glob and find_executable calls
144+
mock_glob.assert_called_once_with(
145+
"{ndk_dir}/toolchains/llvm*".format(ndk_dir=self.ctx._ndk_dir)
146+
)
147+
mock_find_executable.assert_called_once_with(
148+
self.expected_compiler, path=environ["PATH"]
149+
)
150+
137151
# check gcc compilers
138-
self.assertEqual(env["CC"].split()[0], "arm-linux-androideabi-gcc")
139-
self.assertEqual(env["CXX"].split()[0], "arm-linux-androideabi-g++")
152+
self.assertEqual(env["CC"].split()[0], self.expected_compiler)
153+
self.assertEqual(env["CXX"].split()[0], self.expected_compiler + "++")
140154
# check android binaries
141155
self.assertEqual(env["AR"], "arm-linux-androideabi-ar")
142156
self.assertEqual(env["LD"], "arm-linux-androideabi-ld")
@@ -166,9 +180,9 @@ def test_arch_arm(self, mock_ensure_dir, mock_find_executable):
166180
self.assertEqual(
167181
e.exception.args[0],
168182
"Couldn't find executable for CC. This indicates a problem "
169-
"locating the arm-linux-androideabi-gcc executable in the Android "
183+
"locating the {expected_compiler} executable in the Android "
170184
"NDK, not that you don't have a normal compiler installed. "
171-
"Exiting.",
185+
"Exiting.".format(expected_compiler=self.expected_compiler),
172186
)
173187

174188

@@ -197,7 +211,7 @@ def test_arch_armv7a(
197211
tests the `get_env` with clang
198212
199213
"""
200-
mock_find_executable.return_value = "arm-linux-androideabi-gcc"
214+
mock_find_executable.return_value = self.expected_compiler
201215
mock_ensure_dir.return_value = True
202216
mock_glob.return_value = ["llvm"]
203217

@@ -206,10 +220,18 @@ def test_arch_armv7a(
206220
self.assertEqual(arch.__str__(), "armeabi-v7a")
207221
self.assertEqual(arch.toolchain_prefix, "arm-linux-androideabi")
208222
self.assertEqual(arch.command_prefix, "arm-linux-androideabi")
209-
self.assertEqual(arch.target, "armv7a-none-linux-androideabi")
223+
self.assertEqual(arch.target, "armv7a-linux-androideabi21")
210224
self.assertEqual(arch.platform_dir, "arch-arm")
211225

212-
env = arch.get_env(clang=True)
226+
env = arch.get_env()
227+
# check glob and find_executable calls
228+
mock_glob.assert_called_once_with(
229+
"{ndk_dir}/toolchains/llvm*".format(ndk_dir=self.ctx._ndk_dir)
230+
)
231+
mock_find_executable.assert_called_once_with(
232+
self.expected_compiler, path=environ["PATH"]
233+
)
234+
213235
# check clang
214236
build_platform = "{system}-{machine}".format(
215237
system=os.uname()[0], machine=os.uname()[-1]
@@ -242,29 +264,43 @@ class TestArchX86(ArchSetUpBaseClass, unittest.TestCase):
242264
will be used to perform tests for :class:`~pythonforandroid.archs.Archx86`.
243265
"""
244266

267+
@mock.patch("pythonforandroid.archs.glob")
245268
@mock.patch("pythonforandroid.archs.find_executable")
246269
@mock.patch("pythonforandroid.build.ensure_dir")
247-
def test_arch_x86(self, mock_ensure_dir, mock_find_executable):
270+
def test_arch_x86(self, mock_ensure_dir, mock_find_executable, mock_glob):
248271
"""
249272
Test that class :class:`~pythonforandroid.archs.Archx86` returns
250273
some expected attributes and environment variables.
251274
252-
.. note:: Here we mock the same functions than
253-
:meth:`TestArchARM.test_arch_arm`
275+
.. note::
276+
Here we mock the same functions than
277+
:meth:`TestArchARM.test_arch_arm` plus `glob`, so we make sure that
278+
the glob result is the expected even if the folder doesn't exist,
279+
which is probably the case. This has to be done because here we
280+
tests the `get_env` with clang
254281
"""
255-
mock_find_executable.return_value = "arm-linux-androideabi-gcc"
282+
mock_find_executable.return_value = self.expected_compiler
256283
mock_ensure_dir.return_value = True
284+
mock_glob.return_value = ["llvm"]
257285

258286
arch = Archx86(self.ctx)
259287
self.assertEqual(arch.arch, "x86")
260288
self.assertEqual(arch.__str__(), "x86")
261289
self.assertEqual(arch.toolchain_prefix, "x86")
262290
self.assertEqual(arch.command_prefix, "i686-linux-android")
263-
self.assertEqual(arch.target, "i686-none-linux-android")
291+
self.assertEqual(arch.target, "i686-linux-android21")
264292
self.assertEqual(arch.platform_dir, "arch-x86")
265293

266-
# For x86 we expect some extra cflags in our `environment`
267294
env = arch.get_env()
295+
# check glob and find_executable calls
296+
mock_glob.assert_called_once_with(
297+
"{ndk_dir}/toolchains/llvm*".format(ndk_dir=self.ctx._ndk_dir)
298+
)
299+
mock_find_executable.assert_called_once_with(
300+
self.expected_compiler, path=environ["PATH"]
301+
)
302+
303+
# For x86 we expect some extra cflags in our `environment`
268304
self.assertIn(
269305
" -march=i686 -mtune=intel -mssse3 -mfpmath=sse -m32",
270306
env["CFLAGS"],
@@ -278,29 +314,47 @@ class TestArchX86_64(ArchSetUpBaseClass, unittest.TestCase):
278314
:class:`~pythonforandroid.archs.Archx86_64`.
279315
"""
280316

317+
@mock.patch("pythonforandroid.archs.glob")
281318
@mock.patch("pythonforandroid.archs.find_executable")
282319
@mock.patch("pythonforandroid.build.ensure_dir")
283-
def test_arch_x86_64(self, mock_ensure_dir, mock_find_executable):
320+
def test_arch_x86_64(
321+
self, mock_ensure_dir, mock_find_executable, mock_glob
322+
):
284323
"""
285324
Test that class :class:`~pythonforandroid.archs.Archx86_64` returns
286325
some expected attributes and environment variables.
287326
288-
.. note:: Here we mock the same functions than
289-
:meth:`TestArchARM.test_arch_arm`
327+
.. note::
328+
Here we mock the same functions than
329+
:meth:`TestArchARM.test_arch_arm` plus `glob`, so we make sure that
330+
the glob result is the expected even if the folder doesn't exist,
331+
which is probably the case. This has to be done because here we
332+
tests the `get_env` with clang
290333
"""
291-
mock_find_executable.return_value = "arm-linux-androideabi-gcc"
334+
mock_find_executable.return_value = self.expected_compiler
292335
mock_ensure_dir.return_value = True
336+
mock_glob.return_value = ["llvm"]
293337

294338
arch = Archx86_64(self.ctx)
295339
self.assertEqual(arch.arch, "x86_64")
296340
self.assertEqual(arch.__str__(), "x86_64")
297341
self.assertEqual(arch.toolchain_prefix, "x86_64")
298342
self.assertEqual(arch.command_prefix, "x86_64-linux-android")
299-
self.assertEqual(arch.target, "x86_64-none-linux-android")
343+
self.assertEqual(arch.target, "x86_64-linux-android21")
300344
self.assertEqual(arch.platform_dir, "arch-x86_64")
301345

302-
# For x86_64 we expect some extra cflags in our `environment`
303346
env = arch.get_env()
347+
# check glob and find_executable calls
348+
mock_glob.assert_called_once_with(
349+
"{ndk_dir}/toolchains/llvm*".format(ndk_dir=self.ctx._ndk_dir)
350+
)
351+
mock_find_executable.assert_called_once_with(
352+
self.expected_compiler, path=environ["PATH"]
353+
)
354+
355+
# For x86_64 we expect some extra cflags in our `environment`
356+
mock_glob.assert_called_once()
357+
mock_find_executable.assert_called_once()
304358
self.assertIn(
305359
" -march=x86-64 -msse4.2 -mpopcnt -m64 -mtune=intel", env["CFLAGS"]
306360
)
@@ -313,27 +367,43 @@ class TestArchAArch64(ArchSetUpBaseClass, unittest.TestCase):
313367
:class:`~pythonforandroid.archs.ArchAarch_64`.
314368
"""
315369

370+
@mock.patch("pythonforandroid.archs.glob")
316371
@mock.patch("pythonforandroid.archs.find_executable")
317372
@mock.patch("pythonforandroid.build.ensure_dir")
318-
def test_arch_aarch_64(self, mock_ensure_dir, mock_find_executable):
373+
def test_arch_aarch_64(
374+
self, mock_ensure_dir, mock_find_executable, mock_glob
375+
):
319376
"""
320377
Test that class :class:`~pythonforandroid.archs.ArchAarch_64` returns
321378
some expected attributes and environment variables.
322379
323-
.. note:: Here we mock the same functions than
324-
:meth:`TestArchARM.test_arch_arm`
380+
.. note::
381+
Here we mock the same functions than
382+
:meth:`TestArchARM.test_arch_arm` plus `glob`, so we make sure that
383+
the glob result is the expected even if the folder doesn't exist,
384+
which is probably the case. This has to be done because here we
385+
tests the `get_env` with clang
325386
"""
326-
mock_find_executable.return_value = "arm-linux-androideabi-gcc"
387+
mock_find_executable.return_value = self.expected_compiler
327388
mock_ensure_dir.return_value = True
389+
mock_glob.return_value = ["llvm"]
328390

329391
arch = ArchAarch_64(self.ctx)
330392
self.assertEqual(arch.arch, "arm64-v8a")
331393
self.assertEqual(arch.__str__(), "arm64-v8a")
332394
self.assertEqual(arch.toolchain_prefix, "aarch64-linux-android")
333395
self.assertEqual(arch.command_prefix, "aarch64-linux-android")
334-
self.assertEqual(arch.target, "aarch64-none-linux-android")
396+
self.assertEqual(arch.target, "aarch64-linux-android21")
335397
self.assertEqual(arch.platform_dir, "arch-arm64")
336398

337-
# For x86_64 we expect to find an extra key in`environment`
338399
env = arch.get_env()
400+
# check glob and find_executable calls
401+
mock_glob.assert_called_once_with(
402+
"{ndk_dir}/toolchains/llvm*".format(ndk_dir=self.ctx._ndk_dir)
403+
)
404+
mock_find_executable.assert_called_once_with(
405+
self.expected_compiler, path=environ["PATH"]
406+
)
407+
408+
# For x86_64 we expect to find an extra key in`environment`
339409
self.assertIn("EXTRA_CFLAGS", env.keys())

0 commit comments

Comments
 (0)