|
12 | 12 |
|
13 | 13 |
|
14 | 14 | class TestUtil(unittest.TestCase): |
| 15 | + """ |
| 16 | + An inherited class of `unittest.TestCase`to test the module |
| 17 | + :mod:`~pythonforandroid.util`. |
| 18 | + """ |
| 19 | + |
15 | 20 | @mock.patch("pythonforandroid.util.makedirs") |
16 | 21 | 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 | + """ |
17 | 26 | util.ensure_dir("fake_directory") |
18 | 27 | mock_makedirs.assert_called_once_with("fake_directory") |
19 | 28 |
|
20 | 29 | @mock.patch("shutil.rmtree") |
21 | 30 | @mock.patch("pythonforandroid.util.mkdtemp") |
22 | 31 | 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 | + """ |
23 | 38 | mock_mkdtemp.return_value = "/temp/any_directory" |
24 | 39 | with util.temp_directory(): |
25 | 40 | mock_mkdtemp.assert_called_once() |
| 41 | + mock_shutil_rmtree.assert_not_called() |
26 | 42 | mock_shutil_rmtree.assert_called_once_with("/temp/any_directory") |
27 | 43 |
|
28 | 44 | @mock.patch("pythonforandroid.util.chdir") |
29 | 45 | 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 | + """ |
30 | 54 | chdir_dir = "/temp/any_directory" |
31 | 55 | # test chdir to existing directory |
32 | 56 | with util.current_directory(chdir_dir): |
33 | 57 | moch_chdir.assert_called_once_with("/temp/any_directory") |
34 | 58 | 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())] |
39 | 60 | ) |
40 | 61 |
|
41 | 62 | 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 |
49 | 77 |
|
50 | 78 | @mock.patch("pythonforandroid.util.sh.which") |
51 | 79 | 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 | + """ |
55 | 90 | expected_venv = os.path.join( |
56 | 91 | os.path.expanduser("~"), ".local/bin/virtualenv" |
57 | 92 | ) |
@@ -80,53 +115,67 @@ def test_get_virtualenv_executable(self, mock_sh_which): |
80 | 115 | ] |
81 | 116 | ) |
82 | 117 |
|
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 | | - |
94 | 118 | @mock.patch("pythonforandroid.util.walk") |
95 | 119 | 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 | + """ |
96 | 148 | simulated_walk_result = [ |
97 | 149 | ["/fake_dir", ["__pycache__", "Lib"], ["README", "setup.py"]], |
98 | 150 | ["/fake_dir/Lib", ["ctypes"], ["abc.pyc", "abc.py"]], |
99 | 151 | ["/fake_dir/Lib/ctypes", [], ["util.pyc", "util.py"]], |
100 | 152 | ] |
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 |
112 | 153 | mock_walk.return_value = simulated_walk_result |
113 | 154 | file_ens = util.walk_valid_filens( |
114 | 155 | "/fake_dir", ["__pycache__"], ["*.py"] |
115 | 156 | ) |
116 | 157 | self.assertIsInstance(file_ens, types.GeneratorType) |
117 | | - # given the simulated structure we expect: |
118 | 158 | expected_result = { |
119 | 159 | "/fake_dir/README", |
120 | 160 | "/fake_dir/Lib/abc.pyc", |
121 | 161 | "/fake_dir/Lib/ctypes/util.pyc", |
122 | 162 | } |
123 | | - result = set() |
124 | | - for i in file_ens: |
125 | | - result.add(i) |
| 163 | + result = set(file_ens) |
126 | 164 |
|
127 | 165 | self.assertEqual(result, expected_result) |
128 | 166 |
|
129 | 167 | 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 | + """ |
130 | 179 | exc = util.BuildInterruptingException( |
131 | 180 | "missing dependency xxx", instructions="pip install --user xxx" |
132 | 181 | ) |
|
0 commit comments