Commit d55c5077 authored by Edward Lemur's avatar Edward Lemur Committed by LUCI CQ

git-cl: Mock DieWithError and GitSanityTests

Bug: 1051631
Change-Id: I20f68db7dd0a901ff53513998e148cf4ba2c3eaa
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2065133Reviewed-by: 's avatarAnthony Polito <apolito@google.com>
Commit-Queue: Edward Lesmes <ehmaldonado@chromium.org>
parent 56c9bd5a
...@@ -653,9 +653,7 @@ class TestGitCl(unittest.TestCase): ...@@ -653,9 +653,7 @@ class TestGitCl(unittest.TestCase):
mock.patch( mock.patch(
'git_cl.gerrit_util.ValidAccounts', 'git_cl.gerrit_util.ValidAccounts',
lambda *a: self._mocked_call('ValidAccounts', *a)).start() lambda *a: self._mocked_call('ValidAccounts', *a)).start()
mock.patch( mock.patch('sys.exit', side_effect=SystemExitMock).start()
'git_cl.DieWithError',
lambda msg, change=None: self._mocked_call('DieWithError', msg)).start()
mock.patch('git_cl.Settings.GetRoot', return_value='').start() mock.patch('git_cl.Settings.GetRoot', return_value='').start()
self.mockGit = GitMocks() self.mockGit = GitMocks()
mock.patch('scm.GIT.GetBranchRef', self.mockGit.GetBranchRef).start() mock.patch('scm.GIT.GetBranchRef', self.mockGit.GetBranchRef).start()
...@@ -751,32 +749,6 @@ class TestGitCl(unittest.TestCase): ...@@ -751,32 +749,6 @@ class TestGitCl(unittest.TestCase):
] ]
self.assertIsNone(git_cl.LoadCodereviewSettingsFromFile(codereview_file)) self.assertIsNone(git_cl.LoadCodereviewSettingsFromFile(codereview_file))
@staticmethod
def _git_sanity_checks(diff_base, working_branch, get_remote_branch=True):
fake_ancestor = 'fake_ancestor'
fake_cl = 'fake_cl_for_patch'
return [
((['git',
'rev-parse', '--verify', diff_base],), fake_ancestor),
((['git',
'merge-base', fake_ancestor, 'HEAD'],), fake_ancestor),
((['git',
'rev-list', '^' + fake_ancestor, 'HEAD'],), fake_cl),
# Mock a config miss (error code 1)
((['git',
'config', 'gitcl.remotebranch'],), CERR1),
] + ([
# Call to GetRemoteBranch()
((['git',
'config', 'branch.%s.merge' % working_branch],),
'refs/heads/master'),
((['git',
'config', 'branch.%s.remote' % working_branch],), 'origin'),
] if get_remote_branch else []) + [
((['git', 'rev-list', '^' + fake_ancestor,
'refs/remotes/origin/master'],), ''),
]
@classmethod @classmethod
def _gerrit_base_calls(cls, issue=None, fetched_description=None, def _gerrit_base_calls(cls, issue=None, fetched_description=None,
fetched_status=None, other_cl_owner=None, fetched_status=None, other_cl_owner=None,
...@@ -804,19 +776,12 @@ class TestGitCl(unittest.TestCase): ...@@ -804,19 +776,12 @@ class TestGitCl(unittest.TestCase):
'status': fetched_status or 'NEW', 'status': fetched_status or 'NEW',
} }
if fetched_status == 'ABANDONED': if fetched_status == 'ABANDONED':
calls += [
(('DieWithError', 'Change https://%s-review.googlesource.com/'
'123456 has been abandoned, new uploads are not '
'allowed' % short_hostname), SystemExitMock()),
]
return calls return calls
if other_cl_owner: if other_cl_owner:
calls += [ calls += [
(('ask_for_data', 'Press Enter to upload, or Ctrl+C to abort'), ''), (('ask_for_data', 'Press Enter to upload, or Ctrl+C to abort'), ''),
] ]
calls += cls._git_sanity_checks(ancestor_revision, 'master',
get_remote_branch=False)
calls += [ calls += [
((['git', 'rev-parse', 'HEAD'],), '12345'), ((['git', 'rev-parse', 'HEAD'],), '12345'),
] ]
...@@ -1194,6 +1159,7 @@ class TestGitCl(unittest.TestCase): ...@@ -1194,6 +1159,7 @@ class TestGitCl(unittest.TestCase):
list(args))).start() list(args))).start()
mock.patch('os.path.isfile', mock.patch('os.path.isfile',
lambda path: self._mocked_call(['os.path.isfile', path])).start() lambda path: self._mocked_call(['os.path.isfile', path])).start()
mock.patch('git_cl.Changelist.GitSanityChecks', return_value=True).start()
self.mockGit.config['gerrit.host'] = 'true' self.mockGit.config['gerrit.host'] = 'true'
self.mockGit.config['branch.master.gerritissue'] = ( self.mockGit.config['branch.master.gerritissue'] = (
...@@ -1419,6 +1385,7 @@ class TestGitCl(unittest.TestCase): ...@@ -1419,6 +1385,7 @@ class TestGitCl(unittest.TestCase):
change_id='123456789', change_id='123456789',
original_title='User input') original_title='User input')
@mock.patch('sys.stderr', StringIO())
def test_gerrit_upload_squash_reupload_to_abandoned(self): def test_gerrit_upload_squash_reupload_to_abandoned(self):
description = 'desc ✔\nBUG=\n\nChange-Id: 123456789' description = 'desc ✔\nBUG=\n\nChange-Id: 123456789'
with self.assertRaises(SystemExitMock): with self.assertRaises(SystemExitMock):
...@@ -1431,6 +1398,10 @@ class TestGitCl(unittest.TestCase): ...@@ -1431,6 +1398,10 @@ class TestGitCl(unittest.TestCase):
issue=123456, issue=123456,
fetched_status='ABANDONED', fetched_status='ABANDONED',
change_id='123456789') change_id='123456789')
self.assertEqual(
'Change https://chromium-review.googlesource.com/123456 has been '
'abandoned, new uploads are not allowed\n',
sys.stderr.getvalue())
@mock.patch( @mock.patch(
'gerrit_util.GetAccountDetails', 'gerrit_util.GetAccountDetails',
...@@ -1784,31 +1755,33 @@ class TestGitCl(unittest.TestCase): ...@@ -1784,31 +1755,33 @@ class TestGitCl(unittest.TestCase):
0) 0)
self.assertIssueAndPatchset(patchset='1', git_short_host='else') self.assertIssueAndPatchset(patchset='1', git_short_host='else')
@mock.patch('sys.stderr', StringIO())
def test_patch_gerrit_conflict(self): def test_patch_gerrit_conflict(self):
self._patch_common() self._patch_common()
self.calls += [ self.calls += [
((['git', 'fetch', 'https://chromium.googlesource.com/my/repo', ((['git', 'fetch', 'https://chromium.googlesource.com/my/repo',
'refs/changes/56/123456/7'],), ''), 'refs/changes/56/123456/7'],), ''),
((['git', 'cherry-pick', 'FETCH_HEAD'],), CERR1), ((['git', 'cherry-pick', 'FETCH_HEAD'],), CERR1),
(('DieWithError', 'Command "git cherry-pick FETCH_HEAD" failed.\n'),
SystemExitMock()),
] ]
with self.assertRaises(SystemExitMock): with self.assertRaises(SystemExitMock):
git_cl.main(['patch', '123456']) git_cl.main(['patch', '123456'])
self.assertEqual(
'Command "git cherry-pick FETCH_HEAD" failed.\n\n',
sys.stderr.getvalue())
@mock.patch( @mock.patch(
'gerrit_util.GetChangeDetail', 'gerrit_util.GetChangeDetail',
side_effect=gerrit_util.GerritError(404, '')) side_effect=gerrit_util.GerritError(404, ''))
@mock.patch('sys.stderr', StringIO())
def test_patch_gerrit_not_exists(self, *_mocks): def test_patch_gerrit_not_exists(self, *_mocks):
self.mockGit.config['remote.origin.url'] = ( self.mockGit.config['remote.origin.url'] = (
'https://chromium.googlesource.com/my/repo') 'https://chromium.googlesource.com/my/repo')
self.calls = [
(('DieWithError',
'change 123456 at https://chromium-review.googlesource.com does not '
'exist or you have no access to it'), SystemExitMock()),
]
with self.assertRaises(SystemExitMock): with self.assertRaises(SystemExitMock):
self.assertEqual(1, git_cl.main(['patch', '123456'])) self.assertEqual(1, git_cl.main(['patch', '123456']))
self.assertEqual(
'change 123456 at https://chromium-review.googlesource.com does not '
'exist or you have no access to it\n',
sys.stderr.getvalue())
def _checkout_calls(self): def _checkout_calls(self):
return [ return [
...@@ -1847,18 +1820,20 @@ class TestGitCl(unittest.TestCase): ...@@ -1847,18 +1820,20 @@ class TestGitCl(unittest.TestCase):
cl.branchref = 'refs/heads/master' cl.branchref = 'refs/heads/master'
return cl return cl
@mock.patch('sys.stderr', StringIO())
def test_gerrit_ensure_authenticated_missing(self): def test_gerrit_ensure_authenticated_missing(self):
cl = self._test_gerrit_ensure_authenticated_common(auth={ cl = self._test_gerrit_ensure_authenticated_common(auth={
'chromium.googlesource.com': ('git-is.ok', '', 'but gerrit is missing'), 'chromium.googlesource.com': ('git-is.ok', '', 'but gerrit is missing'),
}) })
self.calls.append( with self.assertRaises(SystemExitMock):
(('DieWithError', cl.EnsureAuthenticated(force=False)
'Credentials for the following hosts are required:\n' self.assertEqual(
' chromium-review.googlesource.com\n' 'Credentials for the following hosts are required:\n'
'These are read from ~/.gitcookies (or legacy ~/.netrc)\n' ' chromium-review.googlesource.com\n'
'You can (re)generate your credentials by visiting ' 'These are read from ~/.gitcookies (or legacy ~/.netrc)\n'
'https://chromium-review.googlesource.com/new-password'), ''),) 'You can (re)generate your credentials by visiting '
self.assertIsNone(cl.EnsureAuthenticated(force=False)) 'https://chromium-review.googlesource.com/new-password\n',
sys.stderr.getvalue())
def test_gerrit_ensure_authenticated_conflict(self): def test_gerrit_ensure_authenticated_conflict(self):
cl = self._test_gerrit_ensure_authenticated_common(auth={ cl = self._test_gerrit_ensure_authenticated_common(auth={
...@@ -1975,8 +1950,7 @@ class TestGitCl(unittest.TestCase): ...@@ -1975,8 +1950,7 @@ class TestGitCl(unittest.TestCase):
def test_StatusFieldOverrideIssueMissingArgs(self): def test_StatusFieldOverrideIssueMissingArgs(self):
try: try:
self.assertEqual(git_cl.main(['status', '--issue', '1']), 0) self.assertEqual(git_cl.main(['status', '--issue', '1']), 0)
except SystemExit as ex: except SystemExitMock:
self.assertEqual(ex.code, 2)
self.assertIn( self.assertIn(
'--field must be given when --issue is set.', sys.stderr.getvalue()) '--field must be given when --issue is set.', sys.stderr.getvalue())
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment