OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Unit tests for git_cl.py.""" | 6 """Unit tests for git_cl.py.""" |
7 | 7 |
8 import os | 8 import os |
9 import StringIO | 9 import StringIO |
10 import stat | 10 import stat |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
130 | 130 |
131 class MockChangelistWithBranchAndIssue(): | 131 class MockChangelistWithBranchAndIssue(): |
132 def __init__(self, branch, issue): | 132 def __init__(self, branch, issue): |
133 self.branch = branch | 133 self.branch = branch |
134 self.issue = issue | 134 self.issue = issue |
135 def GetBranch(self): | 135 def GetBranch(self): |
136 return self.branch | 136 return self.branch |
137 def GetIssue(self): | 137 def GetIssue(self): |
138 return self.issue | 138 return self.issue |
139 | 139 |
140 | |
141 class SystemExitMock(Exception): | |
142 pass | |
143 | |
144 | |
140 class TestGitClBasic(unittest.TestCase): | 145 class TestGitClBasic(unittest.TestCase): |
141 def _test_ParseIssueUrl(self, func, url, issue, patchset, hostname, fail): | 146 def _test_ParseIssueUrl(self, func, url, issue, patchset, hostname, fail): |
142 parsed = urlparse.urlparse(url) | 147 parsed = urlparse.urlparse(url) |
143 result = func(parsed) | 148 result = func(parsed) |
144 if fail: | 149 if fail: |
145 self.assertIsNone(result) | 150 self.assertIsNone(result) |
146 return None | 151 return None |
147 self.assertIsNotNone(result) | 152 self.assertIsNotNone(result) |
148 self.assertEqual(result.issue, issue) | 153 self.assertEqual(result.issue, issue) |
149 self.assertEqual(result.patchset, patchset) | 154 self.assertEqual(result.patchset, patchset) |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
264 self.mock(git_cl, 'write_json', lambda path, contents: | 269 self.mock(git_cl, 'write_json', lambda path, contents: |
265 self._mocked_call('write_json', path, contents)) | 270 self._mocked_call('write_json', path, contents)) |
266 self.mock(git_cl.presubmit_support, 'DoPresubmitChecks', PresubmitMock) | 271 self.mock(git_cl.presubmit_support, 'DoPresubmitChecks', PresubmitMock) |
267 self.mock(git_cl.rietveld, 'Rietveld', RietveldMock) | 272 self.mock(git_cl.rietveld, 'Rietveld', RietveldMock) |
268 self.mock(git_cl.rietveld, 'CachingRietveld', RietveldMock) | 273 self.mock(git_cl.rietveld, 'CachingRietveld', RietveldMock) |
269 self.mock(git_cl.upload, 'RealMain', self.fail) | 274 self.mock(git_cl.upload, 'RealMain', self.fail) |
270 self.mock(git_cl.watchlists, 'Watchlists', WatchlistsMock) | 275 self.mock(git_cl.watchlists, 'Watchlists', WatchlistsMock) |
271 self.mock(git_cl.auth, 'get_authenticator_for_host', AuthenticatorMock) | 276 self.mock(git_cl.auth, 'get_authenticator_for_host', AuthenticatorMock) |
272 self.mock(git_cl.gerrit_util.GceAuthenticator, 'is_gce', | 277 self.mock(git_cl.gerrit_util.GceAuthenticator, 'is_gce', |
273 classmethod(lambda _: False)) | 278 classmethod(lambda _: False)) |
279 self.mock(git_cl, 'DieWithError', | |
280 lambda msg: self._mocked_call(['DieWithError', msg])) | |
274 # It's important to reset settings to not have inter-tests interference. | 281 # It's important to reset settings to not have inter-tests interference. |
275 git_cl.settings = None | 282 git_cl.settings = None |
276 | 283 |
277 | 284 |
278 def tearDown(self): | 285 def tearDown(self): |
279 try: | 286 try: |
280 # Note: has_failed returns True if at least 1 test ran so far, current | 287 # Note: has_failed returns True if at least 1 test ran so far, current |
281 # included, has failed. That means current test may have actually ran | 288 # included, has failed. That means current test may have actually ran |
282 # fine, and the check for no leftover calls would be skipped. | 289 # fine, and the check for no leftover calls would be skipped. |
283 if not self.has_failed(): | 290 if not self.has_failed(): |
(...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
699 self._run_reviewer_test( | 706 self._run_reviewer_test( |
700 ['--send-mail'], | 707 ['--send-mail'], |
701 'desc\n\nBUG=', | 708 'desc\n\nBUG=', |
702 description.strip('\n'), | 709 description.strip('\n'), |
703 description, | 710 description, |
704 ['--reviewers=reviewer@example.com', '--send_mail']) | 711 ['--reviewers=reviewer@example.com', '--send_mail']) |
705 | 712 |
706 def test_reviewer_send_mail_no_rev(self): | 713 def test_reviewer_send_mail_no_rev(self): |
707 # Fails without a reviewer. | 714 # Fails without a reviewer. |
708 stdout = StringIO.StringIO() | 715 stdout = StringIO.StringIO() |
709 stderr = StringIO.StringIO() | 716 self.calls = self._upload_no_rev_calls(None, None) + [ |
710 try: | 717 ((['DieWithError', 'Must specify reviewers to send email.'],), |
711 self.calls = self._upload_no_rev_calls(None, None) | 718 SystemExitMock()) |
712 def RunEditor(desc, _, **kwargs): | 719 ] |
713 return desc | 720 |
714 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor) | 721 def RunEditor(desc, _, **kwargs): |
715 self.mock(sys, 'stdout', stdout) | 722 return desc |
716 self.mock(sys, 'stderr', stderr) | 723 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor) |
724 self.mock(sys, 'stdout', stdout) | |
725 with self.assertRaises(SystemExitMock): | |
717 git_cl.main(['upload', '--send-mail']) | 726 git_cl.main(['upload', '--send-mail']) |
718 self.fail() | 727 self.assertEqual( |
719 except SystemExit: | 728 'Using 50% similarity for rename/copy detection. Override with ' |
Sergiy Byelozyorov
2016/10/10 11:41:12
nice refactor
tandrii(chromium)
2016/10/10 15:10:14
:)
| |
720 self.assertEqual( | 729 '--similarity.\n', |
721 'Using 50% similarity for rename/copy detection. Override with ' | 730 stdout.getvalue()) |
722 '--similarity.\n', | |
723 stdout.getvalue()) | |
724 self.assertEqual( | |
725 'Must specify reviewers to send email.\n', stderr.getvalue()) | |
726 | 731 |
727 def test_bug_on_cmd(self): | 732 def test_bug_on_cmd(self): |
728 self._run_reviewer_test( | 733 self._run_reviewer_test( |
729 ['--bug=500658,proj:123'], | 734 ['--bug=500658,proj:123'], |
730 'desc\n\nBUG=500658\nBUG=proj:123', | 735 'desc\n\nBUG=500658\nBUG=proj:123', |
731 '# Blah blah comment.\ndesc\n\nBUG=500658\nBUG=proj:1234', | 736 '# Blah blah comment.\ndesc\n\nBUG=500658\nBUG=proj:1234', |
732 'desc\n\nBUG=500658\nBUG=proj:1234', | 737 'desc\n\nBUG=500658\nBUG=proj:1234', |
733 []) | 738 []) |
734 | 739 |
735 def test_dcommit(self): | 740 def test_dcommit(self): |
(...skipping 656 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1392 ''), | 1397 ''), |
1393 ((['git', 'config', 'branch.master.gerritserver', | 1398 ((['git', 'config', 'branch.master.gerritserver', |
1394 'https://chromium-review.googlesource.com'],), ''), | 1399 'https://chromium-review.googlesource.com'],), ''), |
1395 ((['git', 'config', 'branch.master.gerritpatchset', '1'],), ''), | 1400 ((['git', 'config', 'branch.master.gerritpatchset', '1'],), ''), |
1396 ] | 1401 ] |
1397 self.assertEqual(git_cl.main( | 1402 self.assertEqual(git_cl.main( |
1398 ['patch', 'https://chromium-review.googlesource.com/#/c/123456/1']), 0) | 1403 ['patch', 'https://chromium-review.googlesource.com/#/c/123456/1']), 0) |
1399 | 1404 |
1400 def test_gerrit_patch_conflict(self): | 1405 def test_gerrit_patch_conflict(self): |
1401 self._patch_common(is_gerrit=True) | 1406 self._patch_common(is_gerrit=True) |
1402 self.mock(git_cl, 'DieWithError', | |
1403 lambda msg: self._mocked_call(['DieWithError', msg])) | |
1404 class SystemExitMock(Exception): | |
1405 pass | |
1406 self.calls += [ | 1407 self.calls += [ |
1407 ((['git', 'fetch', 'https://chromium.googlesource.com/my/repo', | 1408 ((['git', 'fetch', 'https://chromium.googlesource.com/my/repo', |
1408 'refs/changes/56/123456/1'],), ''), | 1409 'refs/changes/56/123456/1'],), ''), |
1409 ((['git', 'cherry-pick', 'FETCH_HEAD'],), CERR1), | 1410 ((['git', 'cherry-pick', 'FETCH_HEAD'],), CERR1), |
1410 ((['DieWithError', 'Command "git cherry-pick FETCH_HEAD" failed.\n'],), | 1411 ((['DieWithError', 'Command "git cherry-pick FETCH_HEAD" failed.\n'],), |
1411 SystemExitMock()), | 1412 SystemExitMock()), |
1412 ] | 1413 ] |
1413 with self.assertRaises(SystemExitMock): | 1414 with self.assertRaises(SystemExitMock): |
1414 git_cl.main(['patch', | 1415 git_cl.main(['patch', |
1415 'https://chromium-review.googlesource.com/#/c/123456/1']) | 1416 'https://chromium-review.googlesource.com/#/c/123456/1']) |
1416 | 1417 |
1418 def test_gerrit_patch_not_exists(self): | |
1419 url = 'https://chromium-review.googlesource.com' | |
1420 self.mock(git_cl.gerrit_util, 'GetChangeDetail', lambda _, __, ___: None) | |
1421 self.calls = [ | |
1422 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), | |
1423 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), | |
1424 ((['git', 'config', 'branch.master.rietveldissue'],), CERR1), | |
1425 ((['git', 'config', 'branch.master.gerritissue'],), CERR1), | |
1426 ((['git', 'config', 'rietveld.autoupdate'],), CERR1), | |
1427 ((['git', 'config', 'gerrit.host'],), 'true'), | |
1428 ((['DieWithError', 'issue 123456 at ' + url + ' does not exist ' | |
1429 'or you have no access to it'],), SystemExitMock()), | |
1430 ] | |
1431 with self.assertRaises(SystemExitMock): | |
1432 self.assertEqual(1, git_cl.main(['patch', url + '/#/c/123456/1'])) | |
1433 | |
1417 def _checkout_calls(self): | 1434 def _checkout_calls(self): |
1418 return [ | 1435 return [ |
1419 ((['git', 'config', '--local', '--get-regexp', | 1436 ((['git', 'config', '--local', '--get-regexp', |
1420 'branch\\..*\\.rietveldissue'], ), | 1437 'branch\\..*\\.rietveldissue'], ), |
1421 ('branch.retrying.rietveldissue 1111111111\n' | 1438 ('branch.retrying.rietveldissue 1111111111\n' |
1422 'branch.some-fix.rietveldissue 2222222222\n')), | 1439 'branch.some-fix.rietveldissue 2222222222\n')), |
1423 ((['git', 'config', '--local', '--get-regexp', | 1440 ((['git', 'config', '--local', '--get-regexp', |
1424 'branch\\..*\\.gerritissue'], ), | 1441 'branch\\..*\\.gerritissue'], ), |
1425 ('branch.ger-branch.gerritissue 123456\n' | 1442 ('branch.ger-branch.gerritissue 123456\n' |
1426 'branch.gbranch654.gerritissue 654321\n')), | 1443 'branch.gbranch654.gerritissue 654321\n')), |
(...skipping 575 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2002 self.assertEqual(0, git_cl.main(['try-results'])) | 2019 self.assertEqual(0, git_cl.main(['try-results'])) |
2003 self.assertRegexpMatches(sys.stdout.getvalue(), 'Failures:') | 2020 self.assertRegexpMatches(sys.stdout.getvalue(), 'Failures:') |
2004 self.assertRegexpMatches(sys.stdout.getvalue(), 'Started:') | 2021 self.assertRegexpMatches(sys.stdout.getvalue(), 'Started:') |
2005 self.assertRegexpMatches(sys.stdout.getvalue(), '2 try jobs') | 2022 self.assertRegexpMatches(sys.stdout.getvalue(), '2 try jobs') |
2006 | 2023 |
2007 | 2024 |
2008 if __name__ == '__main__': | 2025 if __name__ == '__main__': |
2009 git_cl.logging.basicConfig( | 2026 git_cl.logging.basicConfig( |
2010 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) | 2027 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) |
2011 unittest.main() | 2028 unittest.main() |
OLD | NEW |