1
1
import os
2
-
3
2
import unittest
3
+ from os import environ
4
4
5
5
try :
6
6
from unittest import mock
7
7
except ImportError :
8
8
# `Python 2` or lower than `Python 3.3` does not
9
9
# have the `unittest.mock` module built-in
10
10
import mock
11
+
11
12
from pythonforandroid .bootstrap import Bootstrap
12
13
from pythonforandroid .distribution import Distribution
13
14
from pythonforandroid .recipe import Recipe
@@ -53,6 +54,7 @@ class ArchSetUpBaseClass(object):
53
54
"""
54
55
55
56
ctx = None
57
+ expected_compiler = ""
56
58
57
59
def setUp (self ):
58
60
self .ctx = Context ()
@@ -66,6 +68,12 @@ def setUp(self):
66
68
self .ctx , name = "sdl2" , recipes = ["python3" , "kivy" ]
67
69
)
68
70
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
+ )
69
77
70
78
71
79
class TestArch (ArchSetUpBaseClass , unittest .TestCase ):
@@ -82,11 +90,7 @@ def test_arch(self):
82
90
self .assertEqual (
83
91
e1 .exception .args [0 ], "'Arch' object has no attribute 'arch'"
84
92
)
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" )
90
94
self .assertIsNone (arch .toolchain_prefix )
91
95
self .assertIsNone (arch .command_prefix )
92
96
self .assertIsInstance (arch .include_dirs , list )
@@ -98,9 +102,10 @@ class TestArchARM(ArchSetUpBaseClass, unittest.TestCase):
98
102
will be used to perform tests for :class:`~pythonforandroid.archs.ArchARM`.
99
103
"""
100
104
105
+ @mock .patch ("pythonforandroid.archs.glob" )
101
106
@mock .patch ("pythonforandroid.archs.find_executable" )
102
107
@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 ):
104
109
"""
105
110
Test that class :class:`~pythonforandroid.archs.ArchARM` returns some
106
111
expected attributes and environment variables.
@@ -115,15 +120,16 @@ def test_arch_arm(self, mock_ensure_dir, mock_find_executable):
115
120
not exist)
116
121
117
122
"""
118
- mock_find_executable .return_value = "arm-linux-androideabi-gcc"
123
+ mock_find_executable .return_value = self . expected_compiler
119
124
mock_ensure_dir .return_value = True
125
+ mock_glob .return_value = ["llvm" ]
120
126
121
127
arch = ArchARM (self .ctx )
122
128
self .assertEqual (arch .arch , "armeabi" )
123
129
self .assertEqual (arch .__str__ (), "armeabi" )
124
130
self .assertEqual (arch .toolchain_prefix , "arm-linux-androideabi" )
125
131
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 " )
127
133
self .assertEqual (arch .platform_dir , "arch-arm" )
128
134
arch = ArchARM (self .ctx )
129
135
@@ -134,9 +140,17 @@ def test_arch_arm(self, mock_ensure_dir, mock_find_executable):
134
140
expected_env_gcc_keys , set (env .keys ()) & expected_env_gcc_keys
135
141
)
136
142
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
+
137
151
# 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 + " ++" )
140
154
# check android binaries
141
155
self .assertEqual (env ["AR" ], "arm-linux-androideabi-ar" )
142
156
self .assertEqual (env ["LD" ], "arm-linux-androideabi-ld" )
@@ -166,9 +180,9 @@ def test_arch_arm(self, mock_ensure_dir, mock_find_executable):
166
180
self .assertEqual (
167
181
e .exception .args [0 ],
168
182
"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 "
170
184
"NDK, not that you don't have a normal compiler installed. "
171
- "Exiting." ,
185
+ "Exiting." . format ( expected_compiler = self . expected_compiler ) ,
172
186
)
173
187
174
188
@@ -197,7 +211,7 @@ def test_arch_armv7a(
197
211
tests the `get_env` with clang
198
212
199
213
"""
200
- mock_find_executable .return_value = "arm-linux-androideabi-gcc"
214
+ mock_find_executable .return_value = self . expected_compiler
201
215
mock_ensure_dir .return_value = True
202
216
mock_glob .return_value = ["llvm" ]
203
217
@@ -206,10 +220,18 @@ def test_arch_armv7a(
206
220
self .assertEqual (arch .__str__ (), "armeabi-v7a" )
207
221
self .assertEqual (arch .toolchain_prefix , "arm-linux-androideabi" )
208
222
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 " )
210
224
self .assertEqual (arch .platform_dir , "arch-arm" )
211
225
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
+
213
235
# check clang
214
236
build_platform = "{system}-{machine}" .format (
215
237
system = os .uname ()[0 ], machine = os .uname ()[- 1 ]
@@ -242,29 +264,43 @@ class TestArchX86(ArchSetUpBaseClass, unittest.TestCase):
242
264
will be used to perform tests for :class:`~pythonforandroid.archs.Archx86`.
243
265
"""
244
266
267
+ @mock .patch ("pythonforandroid.archs.glob" )
245
268
@mock .patch ("pythonforandroid.archs.find_executable" )
246
269
@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 ):
248
271
"""
249
272
Test that class :class:`~pythonforandroid.archs.Archx86` returns
250
273
some expected attributes and environment variables.
251
274
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
254
281
"""
255
- mock_find_executable .return_value = "arm-linux-androideabi-gcc"
282
+ mock_find_executable .return_value = self . expected_compiler
256
283
mock_ensure_dir .return_value = True
284
+ mock_glob .return_value = ["llvm" ]
257
285
258
286
arch = Archx86 (self .ctx )
259
287
self .assertEqual (arch .arch , "x86" )
260
288
self .assertEqual (arch .__str__ (), "x86" )
261
289
self .assertEqual (arch .toolchain_prefix , "x86" )
262
290
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 " )
264
292
self .assertEqual (arch .platform_dir , "arch-x86" )
265
293
266
- # For x86 we expect some extra cflags in our `environment`
267
294
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`
268
304
self .assertIn (
269
305
" -march=i686 -mtune=intel -mssse3 -mfpmath=sse -m32" ,
270
306
env ["CFLAGS" ],
@@ -278,29 +314,47 @@ class TestArchX86_64(ArchSetUpBaseClass, unittest.TestCase):
278
314
:class:`~pythonforandroid.archs.Archx86_64`.
279
315
"""
280
316
317
+ @mock .patch ("pythonforandroid.archs.glob" )
281
318
@mock .patch ("pythonforandroid.archs.find_executable" )
282
319
@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
+ ):
284
323
"""
285
324
Test that class :class:`~pythonforandroid.archs.Archx86_64` returns
286
325
some expected attributes and environment variables.
287
326
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
290
333
"""
291
- mock_find_executable .return_value = "arm-linux-androideabi-gcc"
334
+ mock_find_executable .return_value = self . expected_compiler
292
335
mock_ensure_dir .return_value = True
336
+ mock_glob .return_value = ["llvm" ]
293
337
294
338
arch = Archx86_64 (self .ctx )
295
339
self .assertEqual (arch .arch , "x86_64" )
296
340
self .assertEqual (arch .__str__ (), "x86_64" )
297
341
self .assertEqual (arch .toolchain_prefix , "x86_64" )
298
342
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 " )
300
344
self .assertEqual (arch .platform_dir , "arch-x86_64" )
301
345
302
- # For x86_64 we expect some extra cflags in our `environment`
303
346
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 ()
304
358
self .assertIn (
305
359
" -march=x86-64 -msse4.2 -mpopcnt -m64 -mtune=intel" , env ["CFLAGS" ]
306
360
)
@@ -313,27 +367,43 @@ class TestArchAArch64(ArchSetUpBaseClass, unittest.TestCase):
313
367
:class:`~pythonforandroid.archs.ArchAarch_64`.
314
368
"""
315
369
370
+ @mock .patch ("pythonforandroid.archs.glob" )
316
371
@mock .patch ("pythonforandroid.archs.find_executable" )
317
372
@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
+ ):
319
376
"""
320
377
Test that class :class:`~pythonforandroid.archs.ArchAarch_64` returns
321
378
some expected attributes and environment variables.
322
379
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
325
386
"""
326
- mock_find_executable .return_value = "arm-linux-androideabi-gcc"
387
+ mock_find_executable .return_value = self . expected_compiler
327
388
mock_ensure_dir .return_value = True
389
+ mock_glob .return_value = ["llvm" ]
328
390
329
391
arch = ArchAarch_64 (self .ctx )
330
392
self .assertEqual (arch .arch , "arm64-v8a" )
331
393
self .assertEqual (arch .__str__ (), "arm64-v8a" )
332
394
self .assertEqual (arch .toolchain_prefix , "aarch64-linux-android" )
333
395
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 " )
335
397
self .assertEqual (arch .platform_dir , "arch-arm64" )
336
398
337
- # For x86_64 we expect to find an extra key in`environment`
338
399
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`
339
409
self .assertIn ("EXTRA_CFLAGS" , env .keys ())
0 commit comments