Chromium Code Reviews| 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 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 218 self.mock(subprocess2, 'check_call', self._mocked_call) | 218 self.mock(subprocess2, 'check_call', self._mocked_call) |
| 219 self.mock(subprocess2, 'check_output', self._mocked_call) | 219 self.mock(subprocess2, 'check_output', self._mocked_call) |
| 220 self.mock(subprocess2, 'communicate', self._mocked_call) | 220 self.mock(subprocess2, 'communicate', self._mocked_call) |
| 221 self.mock(git_cl.gclient_utils, 'CheckCallAndFilter', self._mocked_call) | 221 self.mock(git_cl.gclient_utils, 'CheckCallAndFilter', self._mocked_call) |
| 222 self.mock(git_common, 'is_dirty_git_tree', lambda x: False) | 222 self.mock(git_common, 'is_dirty_git_tree', lambda x: False) |
| 223 self.mock(git_common, 'get_or_create_merge_base', | 223 self.mock(git_common, 'get_or_create_merge_base', |
| 224 lambda *a: ( | 224 lambda *a: ( |
| 225 self._mocked_call(['get_or_create_merge_base']+list(a)))) | 225 self._mocked_call(['get_or_create_merge_base']+list(a)))) |
| 226 self.mock(git_cl, 'BranchExists', lambda _: True) | 226 self.mock(git_cl, 'BranchExists', lambda _: True) |
| 227 self.mock(git_cl, 'FindCodereviewSettingsFile', lambda: '') | 227 self.mock(git_cl, 'FindCodereviewSettingsFile', lambda: '') |
| 228 self.mock(git_cl, 'ask_for_data', self._mocked_call) | 228 self.mock(git_cl, 'ask_for_data', self._mocked_call) |
|
tandrii(chromium)
2016/06/07 16:40:51
see here - _mocked_call doesn't get the name of "a
Sergiy Byelozyorov
2016/06/07 17:29:11
Acknowledged.
| |
| 229 self.mock(git_cl.presubmit_support, 'DoPresubmitChecks', PresubmitMock) | 229 self.mock(git_cl.presubmit_support, 'DoPresubmitChecks', PresubmitMock) |
| 230 self.mock(git_cl.rietveld, 'Rietveld', RietveldMock) | 230 self.mock(git_cl.rietveld, 'Rietveld', RietveldMock) |
| 231 self.mock(git_cl.rietveld, 'CachingRietveld', RietveldMock) | 231 self.mock(git_cl.rietveld, 'CachingRietveld', RietveldMock) |
| 232 self.mock(git_cl.upload, 'RealMain', self.fail) | 232 self.mock(git_cl.upload, 'RealMain', self.fail) |
| 233 self.mock(git_cl.watchlists, 'Watchlists', WatchlistsMock) | 233 self.mock(git_cl.watchlists, 'Watchlists', WatchlistsMock) |
| 234 self.mock(git_cl.auth, 'get_authenticator_for_host', AuthenticatorMock) | 234 self.mock(git_cl.auth, 'get_authenticator_for_host', AuthenticatorMock) |
| 235 self.mock(git_cl.gerrit_util.GceAuthenticator, 'is_gce', | 235 self.mock(git_cl.gerrit_util.GceAuthenticator, 'is_gce', |
| 236 classmethod(lambda _: False)) | 236 classmethod(lambda _: False)) |
| 237 # It's important to reset settings to not have inter-tests interference. | 237 # It's important to reset settings to not have inter-tests interference. |
| 238 git_cl.settings = None | 238 git_cl.settings = None |
| (...skipping 632 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 871 reviewers=None, | 871 reviewers=None, |
| 872 squash=False, | 872 squash=False, |
| 873 expected_upstream_ref='origin/refs/heads/master', | 873 expected_upstream_ref='origin/refs/heads/master', |
| 874 ref_suffix='', | 874 ref_suffix='', |
| 875 notify=False, | 875 notify=False, |
| 876 post_amend_description=None, | 876 post_amend_description=None, |
| 877 issue=None): | 877 issue=None): |
| 878 """Generic gerrit upload test framework.""" | 878 """Generic gerrit upload test framework.""" |
| 879 reviewers = reviewers or [] | 879 reviewers = reviewers or [] |
| 880 self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) | 880 self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
| 881 self.mock(git_cl.gerrit_util, "CookiesAuthenticator", | 881 self.mock(git_cl.gerrit_util, 'CookiesAuthenticator', |
| 882 CookiesAuthenticatorMockFactory(same_cookie='same_cred')) | 882 CookiesAuthenticatorMockFactory(same_cookie='same_cred')) |
| 883 self.mock(git_cl._GerritChangelistImpl, '_GerritCommitMsgHookCheck', | |
| 884 lambda _, offer_removal: '') | |
|
Sergiy Byelozyorov
2016/06/07 16:33:21
not that it matters, but why return '' and not Non
tandrii(chromium)
2016/06/07 16:40:51
Typo, though '' is shorter than None.
Sergiy Byelozyorov
2016/06/07 17:29:11
Acknowledged.
| |
| 883 self.calls = self._gerrit_base_calls(issue=issue) | 885 self.calls = self._gerrit_base_calls(issue=issue) |
| 884 self.calls += self._gerrit_upload_calls( | 886 self.calls += self._gerrit_upload_calls( |
| 885 description, reviewers, squash, | 887 description, reviewers, squash, |
| 886 expected_upstream_ref=expected_upstream_ref, | 888 expected_upstream_ref=expected_upstream_ref, |
| 887 ref_suffix=ref_suffix, notify=notify, | 889 ref_suffix=ref_suffix, notify=notify, |
| 888 post_amend_description=post_amend_description, | 890 post_amend_description=post_amend_description, |
| 889 issue=issue) | 891 issue=issue) |
| 890 # Uncomment when debugging. | 892 # Uncomment when debugging. |
| 891 # print '\n'.join(map(lambda x: '%2i: %s' % x, enumerate(self.calls))) | 893 # print '\n'.join(map(lambda x: '%2i: %s' % x, enumerate(self.calls))) |
| 892 git_cl.main(['upload'] + upload_args) | 894 git_cl.main(['upload'] + upload_args) |
| (...skipping 624 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1517 ((['git', 'config', '--unset', 'branch.feature.gerritpatchset'],), ''), | 1519 ((['git', 'config', '--unset', 'branch.feature.gerritpatchset'],), ''), |
| 1518 # Let this command raise exception (retcode=1) - it should be ignored. | 1520 # Let this command raise exception (retcode=1) - it should be ignored. |
| 1519 ((['git', 'config', '--unset', 'branch.feature.last-upload-hash'],), | 1521 ((['git', 'config', '--unset', 'branch.feature.last-upload-hash'],), |
| 1520 '', subprocess2.CalledProcessError(1, '', '', '', '')), | 1522 '', subprocess2.CalledProcessError(1, '', '', '', '')), |
| 1521 ((['git', 'config', '--unset', 'branch.feature.gerritserver'],), ''), | 1523 ((['git', 'config', '--unset', 'branch.feature.gerritserver'],), ''), |
| 1522 ((['git', 'config', '--unset', 'branch.feature.gerritsquashhash'],), | 1524 ((['git', 'config', '--unset', 'branch.feature.gerritsquashhash'],), |
| 1523 ''), | 1525 ''), |
| 1524 ] | 1526 ] |
| 1525 self.assertEqual(0, git_cl.main(['issue', '0'])) | 1527 self.assertEqual(0, git_cl.main(['issue', '0'])) |
| 1526 | 1528 |
| 1529 def _common_GerritCommitMsgHookCheck(self): | |
| 1530 self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) | |
| 1531 self.mock(git_cl.os.path, 'abspath', | |
| 1532 lambda path: self._mocked_call(['abspath', path])) | |
|
Sergiy Byelozyorov
2016/06/07 16:33:21
where is path defined? also didn't quite figure ou
tandrii(chromium)
2016/06/07 16:40:51
it basically pops next item from self.calls, ensur
Sergiy Byelozyorov
2016/06/07 17:29:11
Oh. Nevermind about path. It's lambda argument.
| |
| 1533 self.mock(git_cl.os.path, 'exists', | |
| 1534 lambda path: self._mocked_call(['exists', path])) | |
| 1535 self.mock(git_cl.gclient_utils, 'FileRead', | |
| 1536 lambda path: self._mocked_call(['FileRead', path])) | |
| 1537 self.mock(git_cl.gclient_utils, 'rm_file_or_tree', | |
| 1538 lambda path: self._mocked_call(['rm_file_or_tree', path])) | |
| 1539 self.calls = [ | |
| 1540 ((['git', 'rev-parse', '--show-cdup'],), '../'), | |
| 1541 ((['abspath', '../'],), '/abs/git_repo_root'), | |
| 1542 ] | |
| 1543 return git_cl.Changelist(codereview='gerrit', issue=123) | |
| 1544 | |
| 1545 def test_GerritCommitMsgHookCheck_custom_hook(self): | |
| 1546 cl = self._common_GerritCommitMsgHookCheck() | |
| 1547 self.calls += [ | |
| 1548 ((['exists', '/abs/git_repo_root/.git/hooks/commit-msg'],), True), | |
| 1549 ((['FileRead', '/abs/git_repo_root/.git/hooks/commit-msg'],), | |
| 1550 '#!/bin/sh\necho "custom hook"') | |
| 1551 ] | |
| 1552 cl._codereview_impl._GerritCommitMsgHookCheck(offer_removal=True) | |
| 1553 | |
| 1554 def test_GerritCommitMsgHookCheck_not_exists(self): | |
| 1555 cl = self._common_GerritCommitMsgHookCheck() | |
| 1556 self.calls += [ | |
| 1557 ((['exists', '/abs/git_repo_root/.git/hooks/commit-msg'],), False), | |
| 1558 ] | |
| 1559 cl._codereview_impl._GerritCommitMsgHookCheck(offer_removal=True) | |
| 1560 | |
| 1561 def test_GerritCommitMsgHookCheck(self): | |
| 1562 cl = self._common_GerritCommitMsgHookCheck() | |
| 1563 self.calls += [ | |
| 1564 ((['exists', '/abs/git_repo_root/.git/hooks/commit-msg'],), True), | |
| 1565 ((['FileRead', '/abs/git_repo_root/.git/hooks/commit-msg'],), | |
| 1566 '...\n# From Gerrit Code Review\n...\nadd_ChangeId()\n'), | |
| 1567 (('Do you want to remove it now? [Yes/No]',), 'Yes'), | |
|
Sergiy Byelozyorov
2016/06/07 16:33:22
is there a function name missing here? ask_for_dat
tandrii(chromium)
2016/06/07 16:40:51
yeah, that's how ask_for_data is mocked for this w
Sergiy Byelozyorov
2016/06/07 17:29:11
Highly intuitive... :-)
| |
| 1568 ((['rm_file_or_tree', '/abs/git_repo_root/.git/hooks/commit-msg'],), | |
| 1569 ''), | |
| 1570 ] | |
| 1571 cl._codereview_impl._GerritCommitMsgHookCheck(offer_removal=True) | |
| 1572 | |
| 1527 | 1573 |
| 1528 if __name__ == '__main__': | 1574 if __name__ == '__main__': |
| 1529 git_cl.logging.basicConfig( | 1575 git_cl.logging.basicConfig( |
| 1530 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) | 1576 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) |
| 1531 unittest.main() | 1577 unittest.main() |
| OLD | NEW |