11import os
2+ from pathlib import Path
3+ from tempfile import TemporaryDirectory
24import types
35import unittest
46from unittest import mock
@@ -21,20 +23,21 @@ def test_ensure_dir(self, mock_makedirs):
2123 util .ensure_dir ("fake_directory" )
2224 mock_makedirs .assert_called_once_with ("fake_directory" )
2325
24- @mock .patch ("pythonforandroid.util .rmtree" )
26+ @mock .patch ("shutil .rmtree" )
2527 @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+
2730 """
2831 Basic test for method :meth:`~pythonforandroid.util.temp_directory`. We
2932 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
3134 proper place.
3235 """
3336 mock_mkdtemp .return_value = "/temp/any_directory"
3437 with util .temp_directory ():
3538 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" )
3841
3942 @mock .patch ("pythonforandroid.util.chdir" )
4043 def test_current_directory (self , moch_chdir ):
@@ -136,3 +139,42 @@ def test_util_exceptions(self):
136139 )
137140 with self .assertRaises (SystemExit ):
138141 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