Jelajahi Sumber

[git_auth] Move gitcookies to free backup name

Bug: 410576093
Change-Id: Ib2d9db97f4e4e1e8cfe1914ba7ffb881f9e16bbf
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/6453918
Reviewed-by: Yiwei Zhang <yiwzhang@google.com>
Commit-Queue: Yiwei Zhang <yiwzhang@google.com>
Reviewed-by: Gavin Mak <gavinmak@google.com>
Allen Li 4 bulan lalu
induk
melakukan
d28539d200
2 mengubah file dengan 19 tambahan dan 4 penghapusan
  1. 4 4
      git_auth.py
  2. 15 0
      tests/git_auth_test.py

+ 4 - 4
git_auth.py

@@ -913,10 +913,10 @@ class ConfigWizard(object):
     def _move_file(self, path: str) -> None:
     def _move_file(self, path: str) -> None:
         """Move file to a backup path."""
         """Move file to a backup path."""
         backup = f'{path}.bak'
         backup = f'{path}.bak'
-        if os.path.exists(backup):
-            raise _ConfigError(
-                f'wanted to move {path} to {backup}, but {backup} already exists'
-            )
+        n = 1
+        while os.path.exists(backup):
+            n += 1
+            backup = f'{path}.bak{n}'
         os.rename(path, backup)
         os.rename(path, backup)
         self._println_notify(f'Moved {path!r} to {backup!r}')
         self._println_notify(f'Moved {path!r} to {backup!r}')
 
 

+ 15 - 0
tests/git_auth_test.py

@@ -362,6 +362,21 @@ class TestConfigWizard(unittest.TestCase):
             )
             )
             self.assertEqual(got, want)
             self.assertEqual(got, want)
 
 
+    def test_move_file(self):
+        with tempfile.TemporaryDirectory() as d:
+            path = os.path.join(d, 'foo')
+            open(path, 'w').close()
+            self.wizard._move_file(path)
+            self.assertEqual(os.listdir(d), ['foo.bak'])
+
+    def test_move_file_backup_exists(self):
+        with tempfile.TemporaryDirectory() as d:
+            path = os.path.join(d, 'foo')
+            open(path, 'w').close()
+            open(os.path.join(d, 'foo.bak'), 'w').close()
+            self.wizard._move_file(path)
+            self.assertEqual(sorted(os.listdir(d)), ['foo.bak', 'foo.bak2'])
+
 
 
 class _FakeUI(object):
 class _FakeUI(object):
     """Implements UserInterface for testing."""
     """Implements UserInterface for testing."""