summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_shutil.py
diff options
context:
space:
mode:
authorSean Reifscheider <jafo@tummy.com>2008-03-18 17:24:12 (GMT)
committerSean Reifscheider <jafo@tummy.com>2008-03-18 17:24:12 (GMT)
commit493894c3e0abb9ec32f51f9c0630ddeee8a06719 (patch)
tree154ffc47b5eb50d322362af1a43e5e8936356af3 /Lib/test/test_shutil.py
parentc81d3dc8535a4a4a608e3b0a9da5079bfe80cf54 (diff)
downloadcpython-493894c3e0abb9ec32f51f9c0630ddeee8a06719.zip
cpython-493894c3e0abb9ec32f51f9c0630ddeee8a06719.tar.gz
cpython-493894c3e0abb9ec32f51f9c0630ddeee8a06719.tar.bz2
Issue 1577: shutil.move() where destination is a directory was doing a
copy, now it is doing a os.rename() if it's on the same file-system.
Diffstat (limited to 'Lib/test/test_shutil.py')
-rw-r--r--Lib/test/test_shutil.py127
1 files changed, 115 insertions, 12 deletions
diff --git a/Lib/test/test_shutil.py b/Lib/test/test_shutil.py
index b63031e..dfdec0d 100644
--- a/Lib/test/test_shutil.py
+++ b/Lib/test/test_shutil.py
@@ -63,17 +63,6 @@ class TestShutil(unittest.TestCase):
self.assertRaises(OSError, shutil.rmtree, path)
os.remove(path)
- def test_dont_move_dir_in_itself(self):
- src_dir = tempfile.mkdtemp()
- try:
- dst = os.path.join(src_dir, 'foo')
- self.assertRaises(shutil.Error, shutil.move, src_dir, dst)
- finally:
- try:
- os.rmdir(src_dir)
- except:
- pass
-
def test_copytree_simple(self):
def write_data(path, data):
f = open(path, "w")
@@ -162,9 +151,123 @@ class TestShutil(unittest.TestCase):
shutil.rmtree(TESTFN, ignore_errors=True)
+class TestMove(unittest.TestCase):
+
+ def setUp(self):
+ filename = "foo"
+ self.src_dir = tempfile.mkdtemp()
+ self.dst_dir = tempfile.mkdtemp()
+ self.src_file = os.path.join(self.src_dir, filename)
+ self.dst_file = os.path.join(self.dst_dir, filename)
+ # Try to create a dir in the current directory, hoping that it is
+ # not located on the same filesystem as the system tmp dir.
+ try:
+ self.dir_other_fs = tempfile.mkdtemp(
+ dir=os.path.dirname(__file__))
+ self.file_other_fs = os.path.join(self.dir_other_fs,
+ filename)
+ except OSError:
+ self.dir_other_fs = None
+ with open(self.src_file, "wb") as f:
+ f.write("spam")
+
+ def tearDown(self):
+ for d in (self.src_dir, self.dst_dir, self.dir_other_fs):
+ try:
+ if d:
+ shutil.rmtree(d)
+ except:
+ pass
+
+ def _check_move_file(self, src, dst, real_dst):
+ contents = open(src, "rb").read()
+ shutil.move(src, dst)
+ self.assertEqual(contents, open(real_dst, "rb").read())
+ self.assertFalse(os.path.exists(src))
+
+ def _check_move_dir(self, src, dst, real_dst):
+ contents = sorted(os.listdir(src))
+ shutil.move(src, dst)
+ self.assertEqual(contents, sorted(os.listdir(real_dst)))
+ self.assertFalse(os.path.exists(src))
+
+ def test_move_file(self):
+ # Move a file to another location on the same filesystem.
+ self._check_move_file(self.src_file, self.dst_file, self.dst_file)
+
+ def test_move_file_to_dir(self):
+ # Move a file inside an existing dir on the same filesystem.
+ self._check_move_file(self.src_file, self.dst_dir, self.dst_file)
+
+ def test_move_file_other_fs(self):
+ # Move a file to an existing dir on another filesystem.
+ if not self.dir_other_fs:
+ # skip
+ return
+ self._check_move_file(self.src_file, self.file_other_fs,
+ self.file_other_fs)
+
+ def test_move_file_to_dir_other_fs(self):
+ # Move a file to another location on another filesystem.
+ if not self.dir_other_fs:
+ # skip
+ return
+ self._check_move_file(self.src_file, self.dir_other_fs,
+ self.file_other_fs)
+
+ def test_move_dir(self):
+ # Move a dir to another location on the same filesystem.
+ dst_dir = tempfile.mktemp()
+ try:
+ self._check_move_dir(self.src_dir, dst_dir, dst_dir)
+ finally:
+ try:
+ shutil.rmtree(dst_dir)
+ except:
+ pass
+
+ def test_move_dir_other_fs(self):
+ # Move a dir to another location on another filesystem.
+ if not self.dir_other_fs:
+ # skip
+ return
+ dst_dir = tempfile.mktemp(dir=self.dir_other_fs)
+ try:
+ self._check_move_dir(self.src_dir, dst_dir, dst_dir)
+ finally:
+ try:
+ shutil.rmtree(dst_dir)
+ except:
+ pass
+
+ def test_move_dir_to_dir(self):
+ # Move a dir inside an existing dir on the same filesystem.
+ self._check_move_dir(self.src_dir, self.dst_dir,
+ os.path.join(self.dst_dir, os.path.basename(self.src_dir)))
+
+ def test_move_dir_to_dir_other_fs(self):
+ # Move a dir inside an existing dir on another filesystem.
+ if not self.dir_other_fs:
+ # skip
+ return
+ self._check_move_dir(self.src_dir, self.dir_other_fs,
+ os.path.join(self.dir_other_fs, os.path.basename(self.src_dir)))
+
+ def test_existing_file_inside_dest_dir(self):
+ # A file with the same name inside the destination dir already exists.
+ with open(self.dst_file, "wb"):
+ pass
+ self.assertRaises(shutil.Error, shutil.move, self.src_file, self.dst_dir)
+
+ def test_dont_move_dir_in_itself(self):
+ # Moving a dir inside itself raises an Error.
+ dst = os.path.join(self.src_dir, "bar")
+ self.assertRaises(shutil.Error, shutil.move, self.src_dir, dst)
+
+
def test_main():
- test_support.run_unittest(TestShutil)
+ test_support.run_unittest(TestShutil, TestMove)
if __name__ == '__main__':
test_main()