mirror of
https://chromium.googlesource.com/chromium/tools/depot_tools.git
synced 2026-01-11 10:41:31 +00:00
[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>
This commit is contained in:
@@ -913,10 +913,10 @@ class ConfigWizard(object):
|
||||
def _move_file(self, path: str) -> None:
|
||||
"""Move file to a backup path."""
|
||||
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)
|
||||
self._println_notify(f'Moved {path!r} to {backup!r}')
|
||||
|
||||
|
||||
@@ -362,6 +362,21 @@ class TestConfigWizard(unittest.TestCase):
|
||||
)
|
||||
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):
|
||||
"""Implements UserInterface for testing."""
|
||||
|
||||
Reference in New Issue
Block a user