changeset 4946:020f7bdf3282

add tests for filesys.rename*
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sat, 15 Aug 2009 22:27:04 +0200
parents 37b919adfab4
children 3695ad9d93f2
files MoinMoin/util/_tests/test_filesys.py
diffstat 1 files changed, 56 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/util/_tests/test_filesys.py	Sat Aug 15 22:08:33 2009 +0200
+++ b/MoinMoin/util/_tests/test_filesys.py	Sat Aug 15 22:27:04 2009 +0200
@@ -79,4 +79,60 @@
         assert uid2 != uid1  # should be considered stale if platform has no inode support
 
 
+class TestRename:
+    """ test filesys.rename* """
+
+    def setup_method(self, method):
+        self.test_dir = tempfile.mkdtemp('', 'rename_')
+        self.src = os.path.join(self.test_dir, "rename-src")
+        self.dst = os.path.join(self.test_dir, "rename-dst")
+
+    def teardown_method(self, method):
+        shutil.rmtree(self.test_dir)
+
+    def makefile(self, fname, content):
+        f = open(fname, "w")
+        f.write(content)
+        f.close()
+
+    def test_posix_rename_exists(self):
+        self.makefile(self.src, "src")
+        self.makefile(self.dst, "dst")
+        # posix rename overwrites an existing destination
+        # (on win32, we emulate this behaviour)
+        filesys.rename(self.src, self.dst)
+        dst_contents = open(self.dst).read()
+        assert dst_contents == "src"
+
+    def test_win32_rename_exists(self):
+        self.makefile(self.src, "src")
+        self.makefile(self.dst, "dst")
+        # win32-like rename does not overwrite an existing destination
+        # (on posix, we emulate this behaviour)
+        py.test.raises(OSError, filesys.rename_no_overwrite, self.src, self.dst)
+
+    def test_special_rename_exists(self):
+        self.makefile(self.src, "src")
+        self.makefile(self.dst, "dst")
+        py.test.raises(OSError, filesys.rename_no_overwrite, self.src, self.dst, delete_old=True)
+        assert not os.path.exists(self.src)
+
+    def test_posix_rename_notexists(self):
+        self.makefile(self.src, "src")
+        filesys.rename(self.src, self.dst)
+        dst_contents = open(self.dst).read()
+        assert dst_contents == "src"
+
+    def test_win32_rename_notexists(self):
+        self.makefile(self.src, "src")
+        filesys.rename_no_overwrite(self.src, self.dst)
+        dst_contents = open(self.dst).read()
+        assert dst_contents == "src"
+
+    def test_special_rename_notexists(self):
+        self.makefile(self.src, "src")
+        filesys.rename_no_overwrite(self.src, self.dst, delete_old=True)
+        assert not os.path.exists(self.src)
+
+
 coverage_modules = ['MoinMoin.util.filesys']