Skip to content

Commit 44741ae

Browse files
committed
[docs] Add documentation for tests.test_util and other reviewers suggestions
Reviewers suggestions are: - Remove forgotten debug code - `style code` change for `test_current_directory_exception` - add test `mock_shutil_rmtree.assert_not_called()` for `test_temp_directory` Also changed some lines that shouldn't be split (because are pep8 friendly without breaking it in multiple lines)
1 parent 6f65aaf commit 44741ae

File tree

1 file changed

+89
-40
lines changed

1 file changed

+89
-40
lines changed

tests/test_util.py

Lines changed: 89 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,46 +12,81 @@
1212

1313

1414
class TestUtil(unittest.TestCase):
15+
"""
16+
An inherited class of `unittest.TestCase`to test the module
17+
:mod:`~pythonforandroid.util`.
18+
"""
19+
1520
@mock.patch("pythonforandroid.util.makedirs")
1621
def test_ensure_dir(self, mock_makedirs):
22+
"""
23+
Basic test for method :meth:`~pythonforandroid.util.ensure_dir`. Here
24+
we make sure that the mentioned method is called only once.
25+
"""
1726
util.ensure_dir("fake_directory")
1827
mock_makedirs.assert_called_once_with("fake_directory")
1928

2029
@mock.patch("shutil.rmtree")
2130
@mock.patch("pythonforandroid.util.mkdtemp")
2231
def test_temp_directory(self, mock_mkdtemp, mock_shutil_rmtree):
32+
"""
33+
Basic test for method :meth:`~pythonforandroid.util.temp_directory`. We
34+
perform this test by `mocking` the command `mkdtemp` and
35+
`shutil.rmtree` and we make sure that those functions are called in the
36+
proper place.
37+
"""
2338
mock_mkdtemp.return_value = "/temp/any_directory"
2439
with util.temp_directory():
2540
mock_mkdtemp.assert_called_once()
41+
mock_shutil_rmtree.assert_not_called()
2642
mock_shutil_rmtree.assert_called_once_with("/temp/any_directory")
2743

2844
@mock.patch("pythonforandroid.util.chdir")
2945
def test_current_directory(self, moch_chdir):
46+
"""
47+
Basic test for method :meth:`~pythonforandroid.util.current_directory`.
48+
We `mock` chdir and we check that the command is executed once we are
49+
inside a python's `with` statement. Then we check that `chdir has been
50+
called with the proper arguments inside this `with` statement and also
51+
that, once we leave the `with` statement, is called again with the
52+
current working path.
53+
"""
3054
chdir_dir = "/temp/any_directory"
3155
# test chdir to existing directory
3256
with util.current_directory(chdir_dir):
3357
moch_chdir.assert_called_once_with("/temp/any_directory")
3458
moch_chdir.assert_has_calls(
35-
[
36-
mock.call("/temp/any_directory"),
37-
mock.call(os.getcwd()),
38-
]
59+
[mock.call("/temp/any_directory"), mock.call(os.getcwd())]
3960
)
4061

4162
def test_current_directory_exception(self):
42-
# test chdir to non-existing directory, should raise error
43-
# for py3 the exception is FileNotFoundError and IOError for py2, to
44-
# avoid introduce conditions, we test with a more generic exception
45-
with self.assertRaises(OSError):
46-
with util.current_directory("/fake/directory"):
47-
# the line below will never be executed
48-
print("")
63+
"""
64+
Another test for method
65+
:meth:`~pythonforandroid.util.current_directory`, but here we check
66+
that using the method with a non-existing-directory raises an `OSError`
67+
exception.
68+
69+
.. note:: test chdir to non-existing directory, should raise error,
70+
for py3 the exception is FileNotFoundError and IOError for py2, to
71+
avoid introduce conditions, we test with a more generic exception
72+
"""
73+
with self.assertRaises(OSError), util.current_directory(
74+
"/fake/directory"
75+
):
76+
pass
4977

5078
@mock.patch("pythonforandroid.util.sh.which")
5179
def test_get_virtualenv_executable(self, mock_sh_which):
52-
# test that all calls to `sh.which` are performed, so we expect the
53-
# first two `sh.which` calls should be None and the last one should
54-
# return the expected virtualenv (the python3 one)
80+
"""
81+
Test method :meth:`~pythonforandroid.util.get_virtualenv_executable`.
82+
In here we test:
83+
84+
- that all calls to `sh.which` are performed, so we expect the
85+
first two `sh.which` calls should be None and the last one should
86+
return the expected virtualenv (the python3 one)
87+
- that we don't have virtualenv installed, so all calls to
88+
`sh.which` should return None
89+
"""
5590
expected_venv = os.path.join(
5691
os.path.expanduser("~"), ".local/bin/virtualenv"
5792
)
@@ -80,53 +115,67 @@ def test_get_virtualenv_executable(self, mock_sh_which):
80115
]
81116
)
82117

83-
def test_walk_valid_filens_sample(self):
84-
file_ens = util.walk_valid_filens(
85-
"/home/opacam/Devel/python-for-android/tests/",
86-
["__pycache__"],
87-
["*.pyc"],
88-
)
89-
for i in os.walk("/home/opacam/Devel/python-for-android/tests/"):
90-
print(i)
91-
for i in file_ens:
92-
print(i)
93-
94118
@mock.patch("pythonforandroid.util.walk")
95119
def test_walk_valid_filens(self, mock_walk):
120+
"""
121+
Test method :meth:`~pythonforandroid.util.walk_valid_filens`
122+
In here we simulate the following directory structure:
123+
124+
/fake_dir
125+
|-- README
126+
|-- setup.py
127+
|-- __pycache__
128+
|-- |__
129+
|__Lib
130+
|-- abc.pyc
131+
|-- abc.py
132+
|__ ctypes
133+
|-- util.pyc
134+
|-- util.py
135+
136+
Then we execute the method in order to check that we got the expected
137+
result, which should be:
138+
139+
.. code-block:: python
140+
:emphasize-lines: 2-4
141+
142+
expected_result = {
143+
"/fake_dir/README",
144+
"/fake_dir/Lib/abc.pyc",
145+
"/fake_dir/Lib/ctypes/util.pyc",
146+
}
147+
"""
96148
simulated_walk_result = [
97149
["/fake_dir", ["__pycache__", "Lib"], ["README", "setup.py"]],
98150
["/fake_dir/Lib", ["ctypes"], ["abc.pyc", "abc.py"]],
99151
["/fake_dir/Lib/ctypes", [], ["util.pyc", "util.py"]],
100152
]
101-
# /fake_dir
102-
# |-- README
103-
# |-- setup.py
104-
# |-- __pycache__
105-
# |-- |__
106-
# |__Lib
107-
# |-- abc.pyc
108-
# |-- abc.py
109-
# |__ ctypes
110-
# |-- util.pyc
111-
# |-- util.py
112153
mock_walk.return_value = simulated_walk_result
113154
file_ens = util.walk_valid_filens(
114155
"/fake_dir", ["__pycache__"], ["*.py"]
115156
)
116157
self.assertIsInstance(file_ens, types.GeneratorType)
117-
# given the simulated structure we expect:
118158
expected_result = {
119159
"/fake_dir/README",
120160
"/fake_dir/Lib/abc.pyc",
121161
"/fake_dir/Lib/ctypes/util.pyc",
122162
}
123-
result = set()
124-
for i in file_ens:
125-
result.add(i)
163+
result = set(file_ens)
126164

127165
self.assertEqual(result, expected_result)
128166

129167
def test_util_exceptions(self):
168+
"""
169+
Test exceptions for a couple of methods:
170+
171+
- method :meth:`~pythonforandroid.util.BuildInterruptingException`
172+
- method :meth:`~pythonforandroid.util.handle_build_exception`
173+
174+
Here we create an exception with method
175+
:meth:`~pythonforandroid.util.BuildInterruptingException` and we run it
176+
inside method :meth:`~pythonforandroid.util.handle_build_exception` to
177+
make sure that it raises an `SystemExit`.
178+
"""
130179
exc = util.BuildInterruptingException(
131180
"missing dependency xxx", instructions="pip install --user xxx"
132181
)

0 commit comments

Comments
 (0)