Rev 4814: 2.1.0b4-win32-fancy-rename-exception was accepted in http://bazaar.launchpad.net/~jameinel/bzr/2.1.0b4-win32-accepted

John Arbash Meinel john at arbash-meinel.com
Mon Nov 16 20:48:10 GMT 2009


At http://bazaar.launchpad.net/~jameinel/bzr/2.1.0b4-win32-accepted

------------------------------------------------------------
revno: 4814 [merge]
revision-id: john at arbash-meinel.com-20091116204805-aziloi9q1vd2t231
parent: john at arbash-meinel.com-20091116204721-mwntcc54s38vaejd
parent: john at arbash-meinel.com-20091110070156-5084nnzo1o1y8xwm
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: 2.1.0b4-win32-accepted
timestamp: Mon 2009-11-16 14:48:05 -0600
message:
  2.1.0b4-win32-fancy-rename-exception was accepted
modified:
  bzrlib/osutils.py              osutils.py-20050309040759-eeaff12fbf77ac86
  bzrlib/tests/test_osutils.py   test_osutils.py-20051201224856-e48ee24c12182989
-------------- next part --------------
=== modified file 'bzrlib/osutils.py'
--- a/bzrlib/osutils.py	2009-10-23 16:31:03 +0000
+++ b/bzrlib/osutils.py	2009-11-10 07:01:56 +0000
@@ -224,6 +224,7 @@
     else:
         file_existed = True
 
+    failure_exc = None
     success = False
     try:
         try:
@@ -235,8 +236,12 @@
             # source and target may be aliases of each other (e.g. on a
             # case-insensitive filesystem), so we may have accidentally renamed
             # source by when we tried to rename target
-            if not (file_existed and e.errno in (None, errno.ENOENT)):
-                raise
+            failure_exc = sys.exc_info()
+            if (file_existed and e.errno in (None, errno.ENOENT)
+                and old.lower() == new.lower()):
+                # source and target are the same file on a case-insensitive
+                # filesystem, so we don't generate an exception
+                failure_exc = None
     finally:
         if file_existed:
             # If the file used to exist, rename it back into place
@@ -245,6 +250,8 @@
                 unlink_func(tmp_name)
             else:
                 rename_func(tmp_name, new)
+    if failure_exc is not None:
+        raise failure_exc[0], failure_exc[1], failure_exc[2]
 
 
 # In Python 2.4.2 and older, os.path.abspath and os.path.realpath

=== modified file 'bzrlib/tests/test_osutils.py'
--- a/bzrlib/tests/test_osutils.py	2009-10-15 04:01:26 +0000
+++ b/bzrlib/tests/test_osutils.py	2009-11-10 07:01:56 +0000
@@ -126,33 +126,51 @@
 
 class TestRename(tests.TestCaseInTempDir):
 
+    def create_file(self, filename, content):
+        f = open(filename, 'wb')
+        try:
+            f.write(content)
+        finally:
+            f.close()
+
+    def _fancy_rename(self, a, b):
+        osutils.fancy_rename(a, b, rename_func=os.rename,
+                             unlink_func=os.unlink)
+
     def test_fancy_rename(self):
         # This should work everywhere
-        def rename(a, b):
-            osutils.fancy_rename(a, b,
-                    rename_func=os.rename,
-                    unlink_func=os.unlink)
-
-        open('a', 'wb').write('something in a\n')
-        rename('a', 'b')
+        self.create_file('a', 'something in a\n')
+        self._fancy_rename('a', 'b')
         self.failIfExists('a')
         self.failUnlessExists('b')
         self.check_file_contents('b', 'something in a\n')
 
-        open('a', 'wb').write('new something in a\n')
-        rename('b', 'a')
+        self.create_file('a', 'new something in a\n')
+        self._fancy_rename('b', 'a')
 
         self.check_file_contents('a', 'something in a\n')
 
+    def test_fancy_rename_fails_source_missing(self):
+        # An exception should be raised, and the target should be left in place
+        self.create_file('target', 'data in target\n')
+        self.assertRaises((IOError, OSError), self._fancy_rename,
+                          'missingsource', 'target')
+        self.failUnlessExists('target')
+        self.check_file_contents('target', 'data in target\n')
+
+    def test_fancy_rename_fails_if_source_and_target_missing(self):
+        self.assertRaises((IOError, OSError), self._fancy_rename,
+                          'missingsource', 'missingtarget')
+
     def test_rename(self):
         # Rename should be semi-atomic on all platforms
-        open('a', 'wb').write('something in a\n')
+        self.create_file('a', 'something in a\n')
         osutils.rename('a', 'b')
         self.failIfExists('a')
         self.failUnlessExists('b')
         self.check_file_contents('b', 'something in a\n')
 
-        open('a', 'wb').write('new something in a\n')
+        self.create_file('a', 'new something in a\n')
         osutils.rename('b', 'a')
 
         self.check_file_contents('a', 'something in a\n')



More information about the bazaar-commits mailing list