Skip to content

Commit e599383

Browse files
authored
Merge pull request #1855 from opacam/unittest-util
Add unittest for `pythonforandroid.util` and...
2 parents f861429 + 32a4e45 commit e599383

File tree

5 files changed

+344
-99
lines changed

5 files changed

+344
-99
lines changed

.travis.yml

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,6 @@ services:
1212
- docker
1313

1414
before_install:
15-
- travis_retry sudo apt update -qq
16-
# to successfully send the coveralls reports we need pyOpenSSL
17-
- travis_retry sudo apt install -qq --no-install-recommends
18-
python2.7 python3 python3-venv python3-virtualenv python3-pip
19-
python3-setuptools python3-openssl
20-
# (venv/virtualenv are both used by tests/test_pythonpackage.py)
21-
- sudo pip install tox>=2.0
22-
- sudo pip3 install coveralls
2315
# https://github.com/travis-ci/travis-ci/issues/6069#issuecomment-266546552
2416
- git remote set-branches --add origin master
2517
- git fetch
@@ -33,6 +25,21 @@ jobs:
3325
include:
3426
- stage: lint
3527
name: "Tox tests and coverage"
28+
language: python
29+
python: 3.7
30+
before_script:
31+
# We need to escape virtualenv for `test_pythonpackage_basic.test_virtualenv`
32+
# See also: https://github.com/travis-ci/travis-ci/issues/8589
33+
- type -t deactivate && deactivate || true
34+
- export PATH=/opt/python/3.7/bin:$PATH
35+
# Install tox & virtualenv
36+
# Note: venv/virtualenv are both used by tests/test_pythonpackage.py
37+
- pip3.7 install -U virtualenv
38+
- pip3.7 install tox>=2.0
39+
# Install coveralls & dependencies
40+
# Note: pyOpenSSL needed to send the coveralls reports
41+
- pip3.7 install pyOpenSSL
42+
- pip3.7 install coveralls
3643
script:
3744
# we want to fail fast on tox errors without having to `docker build` first
3845
- tox -- tests/ --ignore tests/test_pythonpackage.py

pythonforandroid/util.py

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import contextlib
22
from os.path import exists, join
33
from os import getcwd, chdir, makedirs, walk, uname
4-
import io
5-
import json
64
import sh
75
import shutil
86
import sys
@@ -62,79 +60,6 @@ def ensure_dir(filename):
6260
makedirs(filename)
6361

6462

65-
class JsonStore(object):
66-
"""Replacement of shelve using json, needed for support python 2 and 3.
67-
"""
68-
69-
def __init__(self, filename):
70-
super(JsonStore, self).__init__()
71-
self.filename = filename
72-
self.data = {}
73-
if exists(filename):
74-
try:
75-
with io.open(filename, encoding='utf-8') as fd:
76-
self.data = json.load(fd)
77-
except ValueError:
78-
print("Unable to read the state.db, content will be replaced.")
79-
80-
def __getitem__(self, key):
81-
return self.data[key]
82-
83-
def __setitem__(self, key, value):
84-
self.data[key] = value
85-
self.sync()
86-
87-
def __delitem__(self, key):
88-
del self.data[key]
89-
self.sync()
90-
91-
def __contains__(self, item):
92-
return item in self.data
93-
94-
def get(self, item, default=None):
95-
return self.data.get(item, default)
96-
97-
def keys(self):
98-
return self.data.keys()
99-
100-
def remove_all(self, prefix):
101-
for key in self.data.keys()[:]:
102-
if not key.startswith(prefix):
103-
continue
104-
del self.data[key]
105-
self.sync()
106-
107-
def sync(self):
108-
# http://stackoverflow.com/questions/12309269/write-json-data-to-file-in-python/14870531#14870531
109-
if IS_PY3:
110-
with open(self.filename, 'w') as fd:
111-
json.dump(self.data, fd, ensure_ascii=False)
112-
else:
113-
with io.open(self.filename, 'w', encoding='utf-8') as fd:
114-
fd.write(unicode(json.dumps(self.data, ensure_ascii=False))) # noqa F821
115-
116-
117-
def which(program, path_env):
118-
'''Locate an executable in the system.'''
119-
import os
120-
121-
def is_exe(fpath):
122-
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
123-
124-
fpath, fname = os.path.split(program)
125-
if fpath:
126-
if is_exe(program):
127-
return program
128-
else:
129-
for path in path_env.split(os.pathsep):
130-
path = path.strip('"')
131-
exe_file = os.path.join(path, program)
132-
if is_exe(exe_file):
133-
return exe_file
134-
135-
return None
136-
137-
13863
def get_virtualenv_executable():
13964
virtualenv = None
14065
if virtualenv is None:

tests/test_archs.py

Lines changed: 87 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@
4646

4747

4848
class ArchSetUpBaseClass(object):
49+
"""
50+
An class object which is intended to be used as a base class to configure
51+
an inherited class of `unittest.TestCase`. This class will override the
52+
`setUp` method.
53+
"""
54+
4955
ctx = None
5056

5157
def setUp(self):
@@ -63,6 +69,12 @@ def setUp(self):
6369

6470

6571
class TestArch(ArchSetUpBaseClass, unittest.TestCase):
72+
"""
73+
An inherited class of `ArchSetUpBaseClass` and `unittest.TestCase` which
74+
will be used to perform tests for the base class
75+
:class:`~pythonforandroid.archs.Arch`.
76+
"""
77+
6678
def test_arch(self):
6779
arch = Arch(self.ctx)
6880
with self.assertRaises(AttributeError) as e1:
@@ -81,14 +93,28 @@ def test_arch(self):
8193

8294

8395
class TestArchARM(ArchSetUpBaseClass, unittest.TestCase):
84-
# Here we mock two functions:
85-
# - `ensure_dir` because we don't want to create any directory
86-
# - `find_executable` because otherwise we will
87-
# get an error when trying to find the compiler (we are setting some fake
88-
# paths for our android sdk and ndk so probably will not exist)
96+
"""
97+
An inherited class of `ArchSetUpBaseClass` and `unittest.TestCase` which
98+
will be used to perform tests for :class:`~pythonforandroid.archs.ArchARM`.
99+
"""
100+
89101
@mock.patch("pythonforandroid.archs.find_executable")
90102
@mock.patch("pythonforandroid.build.ensure_dir")
91103
def test_arch_arm(self, mock_ensure_dir, mock_find_executable):
104+
"""
105+
Test that class :class:`~pythonforandroid.archs.ArchARM` returns some
106+
expected attributes and environment variables.
107+
108+
.. note::
109+
Here we mock two methods:
110+
111+
- `ensure_dir` because we don't want to create any directory
112+
- `find_executable` because otherwise we will
113+
get an error when trying to find the compiler (we are setting
114+
some fake paths for our android sdk and ndk so probably will
115+
not exist)
116+
117+
"""
92118
mock_find_executable.return_value = "arm-linux-androideabi-gcc"
93119
mock_ensure_dir.return_value = True
94120

@@ -147,16 +173,30 @@ def test_arch_arm(self, mock_ensure_dir, mock_find_executable):
147173

148174

149175
class TestArchARMv7a(ArchSetUpBaseClass, unittest.TestCase):
150-
# Here we mock the same functions than the previous tests plus `glob`,
151-
# so we make sure that the glob result is the expected even if the folder
152-
# doesn't exist, which is probably the case. This has to be done because
153-
# here we tests the `get_env` with clang
176+
"""
177+
An inherited class of `ArchSetUpBaseClass` and `unittest.TestCase` which
178+
will be used to perform tests for
179+
:class:`~pythonforandroid.archs.ArchARMv7_a`.
180+
"""
181+
154182
@mock.patch("pythonforandroid.archs.glob")
155183
@mock.patch("pythonforandroid.archs.find_executable")
156184
@mock.patch("pythonforandroid.build.ensure_dir")
157185
def test_arch_armv7a(
158186
self, mock_ensure_dir, mock_find_executable, mock_glob
159187
):
188+
"""
189+
Test that class :class:`~pythonforandroid.archs.ArchARMv7_a` returns
190+
some expected attributes and environment variables.
191+
192+
.. note::
193+
Here we mock the same functions than
194+
:meth:`TestArchARM.test_arch_arm` plus `glob`, so we make sure that
195+
the glob result is the expected even if the folder doesn't exist,
196+
which is probably the case. This has to be done because here we
197+
tests the `get_env` with clang
198+
199+
"""
160200
mock_find_executable.return_value = "arm-linux-androideabi-gcc"
161201
mock_ensure_dir.return_value = True
162202
mock_glob.return_value = ["llvm"]
@@ -197,9 +237,21 @@ def test_arch_armv7a(
197237

198238

199239
class TestArchX86(ArchSetUpBaseClass, unittest.TestCase):
240+
"""
241+
An inherited class of `ArchSetUpBaseClass` and `unittest.TestCase` which
242+
will be used to perform tests for :class:`~pythonforandroid.archs.Archx86`.
243+
"""
244+
200245
@mock.patch("pythonforandroid.archs.find_executable")
201246
@mock.patch("pythonforandroid.build.ensure_dir")
202247
def test_arch_x86(self, mock_ensure_dir, mock_find_executable):
248+
"""
249+
Test that class :class:`~pythonforandroid.archs.Archx86` returns
250+
some expected attributes and environment variables.
251+
252+
.. note:: Here we mock the same functions than
253+
:meth:`TestArchARM.test_arch_arm`
254+
"""
203255
mock_find_executable.return_value = "arm-linux-androideabi-gcc"
204256
mock_ensure_dir.return_value = True
205257

@@ -220,9 +272,22 @@ def test_arch_x86(self, mock_ensure_dir, mock_find_executable):
220272

221273

222274
class TestArchX86_64(ArchSetUpBaseClass, unittest.TestCase):
275+
"""
276+
An inherited class of `ArchSetUpBaseClass` and `unittest.TestCase` which
277+
will be used to perform tests for
278+
:class:`~pythonforandroid.archs.Archx86_64`.
279+
"""
280+
223281
@mock.patch("pythonforandroid.archs.find_executable")
224282
@mock.patch("pythonforandroid.build.ensure_dir")
225283
def test_arch_x86_64(self, mock_ensure_dir, mock_find_executable):
284+
"""
285+
Test that class :class:`~pythonforandroid.archs.Archx86_64` returns
286+
some expected attributes and environment variables.
287+
288+
.. note:: Here we mock the same functions than
289+
:meth:`TestArchARM.test_arch_arm`
290+
"""
226291
mock_find_executable.return_value = "arm-linux-androideabi-gcc"
227292
mock_ensure_dir.return_value = True
228293

@@ -242,9 +307,22 @@ def test_arch_x86_64(self, mock_ensure_dir, mock_find_executable):
242307

243308

244309
class TestArchAArch64(ArchSetUpBaseClass, unittest.TestCase):
310+
"""
311+
An inherited class of `ArchSetUpBaseClass` and `unittest.TestCase` which
312+
will be used to perform tests for
313+
:class:`~pythonforandroid.archs.ArchAarch_64`.
314+
"""
315+
245316
@mock.patch("pythonforandroid.archs.find_executable")
246317
@mock.patch("pythonforandroid.build.ensure_dir")
247318
def test_arch_aarch_64(self, mock_ensure_dir, mock_find_executable):
319+
"""
320+
Test that class :class:`~pythonforandroid.archs.ArchAarch_64` returns
321+
some expected attributes and environment variables.
322+
323+
.. note:: Here we mock the same functions than
324+
:meth:`TestArchARM.test_arch_arm`
325+
"""
248326
mock_find_executable.return_value = "arm-linux-androideabi-gcc"
249327
mock_ensure_dir.return_value = True
250328

0 commit comments

Comments
 (0)