1
1
import os
2
+ from pathlib import Path
3
+ from tempfile import TemporaryDirectory
2
4
import types
3
5
import unittest
4
6
from unittest import mock
@@ -21,20 +23,21 @@ def test_ensure_dir(self, mock_makedirs):
21
23
util .ensure_dir ("fake_directory" )
22
24
mock_makedirs .assert_called_once_with ("fake_directory" )
23
25
24
- @mock .patch ("pythonforandroid.util .rmtree" )
26
+ @mock .patch ("shutil .rmtree" )
25
27
@mock .patch ("pythonforandroid.util.mkdtemp" )
26
- def test_temp_directory (self , mock_mkdtemp , mock_rmtree ):
28
+ def test_temp_directory (self , mock_mkdtemp , mock_shutil_rmtree ):
29
+
27
30
"""
28
31
Basic test for method :meth:`~pythonforandroid.util.temp_directory`. We
29
32
perform this test by `mocking` the command `mkdtemp` and
30
- `rmdir ` and we make sure that those functions are called in the
33
+ `shutil.rmtree ` and we make sure that those functions are called in the
31
34
proper place.
32
35
"""
33
36
mock_mkdtemp .return_value = "/temp/any_directory"
34
37
with util .temp_directory ():
35
38
mock_mkdtemp .assert_called_once ()
36
- mock_rmtree .assert_not_called ()
37
- mock_rmtree .assert_called_once_with ("/temp/any_directory" )
39
+ mock_shutil_rmtree .assert_not_called ()
40
+ mock_shutil_rmtree .assert_called_once_with ("/temp/any_directory" )
38
41
39
42
@mock .patch ("pythonforandroid.util.chdir" )
40
43
def test_current_directory (self , moch_chdir ):
@@ -136,3 +139,42 @@ def test_util_exceptions(self):
136
139
)
137
140
with self .assertRaises (SystemExit ):
138
141
util .handle_build_exception (exc )
142
+
143
+ def test_move (self ):
144
+ with mock .patch (
145
+ "pythonforandroid.util.LOGGER"
146
+ ) as m_logger , TemporaryDirectory () as base_dir :
147
+ new_path = Path (base_dir ) / "new"
148
+
149
+ # Set up source
150
+ old_path = Path (base_dir ) / "old"
151
+ with open (old_path , "w" ) as outfile :
152
+ outfile .write ("Temporary content" )
153
+
154
+ # Non existent source
155
+ with self .assertRaises (FileNotFoundError ):
156
+ util .move (new_path , new_path )
157
+ m_logger .debug .assert_called ()
158
+ m_logger .error .assert_not_called ()
159
+ m_logger .reset_mock ()
160
+ assert old_path .exists ()
161
+ assert not new_path .exists ()
162
+
163
+ # Successful move
164
+ util .move (old_path , new_path )
165
+ assert not old_path .exists ()
166
+ assert new_path .exists ()
167
+ m_logger .debug .assert_called ()
168
+ m_logger .error .assert_not_called ()
169
+ m_logger .reset_mock ()
170
+
171
+ # Move over existing:
172
+ existing_path = Path (base_dir ) / "existing"
173
+ existing_path .touch ()
174
+
175
+ util .move (new_path , existing_path )
176
+ with open (existing_path , "r" ) as infile :
177
+ assert infile .read () == "Temporary content"
178
+ m_logger .debug .assert_called ()
179
+ m_logger .error .assert_not_called ()
180
+ m_logger .reset_mock ()
0 commit comments