| 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 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 | 75 |
| 76 class TestGitCl(TestCase): | 76 class TestGitCl(TestCase): |
| 77 def setUp(self): | 77 def setUp(self): |
| 78 super(TestGitCl, self).setUp() | 78 super(TestGitCl, self).setUp() |
| 79 self.calls = [] | 79 self.calls = [] |
| 80 self._calls_done = 0 | 80 self._calls_done = 0 |
| 81 self.mock(subprocess2, 'call', self._mocked_call) | 81 self.mock(subprocess2, 'call', self._mocked_call) |
| 82 self.mock(subprocess2, 'check_call', self._mocked_call) | 82 self.mock(subprocess2, 'check_call', self._mocked_call) |
| 83 self.mock(subprocess2, 'check_output', self._mocked_call) | 83 self.mock(subprocess2, 'check_output', self._mocked_call) |
| 84 self.mock(subprocess2, 'communicate', self._mocked_call) | 84 self.mock(subprocess2, 'communicate', self._mocked_call) |
| 85 self.mock(git_cl.gclient_utils, 'CheckCallAndFilter', self._mocked_call) |
| 85 self.mock(git_common, 'is_dirty_git_tree', lambda x: False) | 86 self.mock(git_common, 'is_dirty_git_tree', lambda x: False) |
| 86 self.mock(git_common, 'get_or_create_merge_base', | 87 self.mock(git_common, 'get_or_create_merge_base', |
| 87 lambda *a: ( | 88 lambda *a: ( |
| 88 self._mocked_call(['get_or_create_merge_base']+list(a)))) | 89 self._mocked_call(['get_or_create_merge_base']+list(a)))) |
| 89 self.mock(git_cl, 'BranchExists', lambda _: True) | 90 self.mock(git_cl, 'BranchExists', lambda _: True) |
| 90 self.mock(git_cl, 'FindCodereviewSettingsFile', lambda: '') | 91 self.mock(git_cl, 'FindCodereviewSettingsFile', lambda: '') |
| 91 self.mock(git_cl, 'ask_for_data', self._mocked_call) | 92 self.mock(git_cl, 'ask_for_data', self._mocked_call) |
| 92 self.mock(git_cl.presubmit_support, 'DoPresubmitChecks', PresubmitMock) | 93 self.mock(git_cl.presubmit_support, 'DoPresubmitChecks', PresubmitMock) |
| 93 self.mock(git_cl.rietveld, 'Rietveld', RietveldMock) | 94 self.mock(git_cl.rietveld, 'Rietveld', RietveldMock) |
| 94 self.mock(git_cl.rietveld, 'CachingRietveld', RietveldMock) | 95 self.mock(git_cl.rietveld, 'CachingRietveld', RietveldMock) |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 313 @classmethod | 314 @classmethod |
| 314 def _dcommit_calls_normal(cls): | 315 def _dcommit_calls_normal(cls): |
| 315 return [ | 316 return [ |
| 316 ((['git', 'rev-parse', '--show-cdup'],), ''), | 317 ((['git', 'rev-parse', '--show-cdup'],), ''), |
| 317 ((['git', 'rev-parse', 'HEAD'],), | 318 ((['git', 'rev-parse', 'HEAD'],), |
| 318 '00ff397798ea57439712ed7e04ab96e13969ef40'), | 319 '00ff397798ea57439712ed7e04ab96e13969ef40'), |
| 319 ((['git', | 320 ((['git', |
| 320 'diff', '--name-status', '--no-renames', '-r', 'fake_ancestor_sha...', | 321 'diff', '--name-status', '--no-renames', '-r', 'fake_ancestor_sha...', |
| 321 '.'],), | 322 '.'],), |
| 322 'M\tPRESUBMIT.py'), | 323 'M\tPRESUBMIT.py'), |
| 324 ((['git', 'config', 'gerrit.host'],), ''), |
| 323 ((['git', | 325 ((['git', |
| 324 'config', 'branch.working.rietveldissue'],), '12345'), | 326 'config', 'branch.working.rietveldissue'],), '12345'), |
| 325 ((['git', | 327 ((['git', |
| 326 'config', 'branch.working.rietveldpatchset'],), '31137'), | 328 'config', 'branch.working.rietveldpatchset'],), '31137'), |
| 327 ((['git', 'config', 'branch.working.rietveldserver'],), | 329 ((['git', 'config', 'branch.working.rietveldserver'],), |
| 328 'codereview.example.com'), | 330 'codereview.example.com'), |
| 329 ((['git', 'config', 'user.email'],), 'author@example.com'), | 331 ((['git', 'config', 'user.email'],), 'author@example.com'), |
| 330 ((['git', 'config', 'rietveld.tree-status-url'],), ''), | 332 ((['git', 'config', 'rietveld.tree-status-url'],), ''), |
| 331 ] | 333 ] |
| 332 | 334 |
| 333 @classmethod | 335 @classmethod |
| 334 def _dcommit_calls_bypassed(cls): | 336 def _dcommit_calls_bypassed(cls): |
| 335 return [ | 337 return [ |
| 338 ((['git', 'config', 'gerrit.host'],), ''), |
| 336 ((['git', | 339 ((['git', |
| 337 'config', 'branch.working.rietveldissue'],), '12345'), | 340 'config', 'branch.working.rietveldissue'],), '12345'), |
| 338 ((['git', 'config', 'branch.working.rietveldserver'],), | 341 ((['git', 'config', 'branch.working.rietveldserver'],), |
| 339 'codereview.example.com'), | 342 'codereview.example.com'), |
| 340 ] | 343 ] |
| 341 | 344 |
| 342 @classmethod | 345 @classmethod |
| 343 def _dcommit_calls_3(cls): | 346 def _dcommit_calls_3(cls): |
| 344 return [ | 347 return [ |
| 345 ((['git', | 348 ((['git', |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 560 ((['get_or_create_merge_base', 'master', 'master'],), | 563 ((['get_or_create_merge_base', 'master', 'master'],), |
| 561 'fake_ancestor_sha'), | 564 'fake_ancestor_sha'), |
| 562 ((['git', 'config', 'gerrit.host'],), 'True'), | 565 ((['git', 'config', 'gerrit.host'],), 'True'), |
| 563 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [ | 566 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [ |
| 564 ((['git', 'rev-parse', '--show-cdup'],), ''), | 567 ((['git', 'rev-parse', '--show-cdup'],), ''), |
| 565 ((['git', 'rev-parse', 'HEAD'],), '12345'), | 568 ((['git', 'rev-parse', 'HEAD'],), '12345'), |
| 566 ((['git', | 569 ((['git', |
| 567 'diff', '--name-status', '--no-renames', '-r', | 570 'diff', '--name-status', '--no-renames', '-r', |
| 568 'fake_ancestor_sha...', '.'],), | 571 'fake_ancestor_sha...', '.'],), |
| 569 'M\t.gitignore\n'), | 572 'M\t.gitignore\n'), |
| 570 ((['git', 'config', 'branch.master.rietveldissue'],), ''), | 573 ((['git', 'config', 'branch.master.gerritissue'],), ''), |
| 571 ((['git', | 574 ((['git', |
| 572 'config', 'branch.master.rietveldpatchset'],), ''), | 575 'config', 'branch.master.rietveldpatchset'],), ''), |
| 573 ((['git', | 576 ((['git', |
| 574 'log', '--pretty=format:%s%n%n%b', 'fake_ancestor_sha...'],), | 577 'log', '--pretty=format:%s%n%n%b', 'fake_ancestor_sha...'],), |
| 575 'foo'), | 578 'foo'), |
| 576 ((['git', 'config', 'user.email'],), 'me@example.com'), | 579 ((['git', 'config', 'user.email'],), 'me@example.com'), |
| 577 ((['git', | 580 ((['git', |
| 578 'diff', '--no-ext-diff', '--stat', '--find-copies-harder', | 581 'diff', '--no-ext-diff', '--stat', '--find-copies-harder', |
| 579 '-l100000', '-C50', 'fake_ancestor_sha', 'HEAD'],), | 582 '-l100000', '-C50', 'fake_ancestor_sha', 'HEAD'],), |
| 580 '+dat'), | 583 '+dat'), |
| 581 ] | 584 ] |
| 582 | 585 |
| 583 @classmethod | 586 @classmethod |
| 584 def _gerrit_upload_calls(cls, description, reviewers, squash, | 587 def _gerrit_upload_calls(cls, description, reviewers, squash, |
| 585 expected_upstream_ref='origin/refs/heads/master', | 588 expected_upstream_ref='origin/refs/heads/master', |
| 586 post_amend_description=None): | 589 post_amend_description=None): |
| 587 if post_amend_description is None: | 590 if post_amend_description is None: |
| 588 post_amend_description = description | 591 post_amend_description = description |
| 589 calls = [ | 592 calls = [ |
| 590 ((['git', 'config', '--bool', 'gerrit.squash-uploads'],), 'false'), | 593 ((['git', 'config', '--bool', 'gerrit.squash-uploads'],), 'false'), |
| 591 ((['git', 'log', '--pretty=format:%s\n\n%b', | 594 ((['git', 'log', '--pretty=format:%s\n\n%b', |
| 592 'fake_ancestor_sha..HEAD'],), | 595 'fake_ancestor_sha..HEAD'],), |
| 593 description) | 596 description) |
| 594 ] | 597 ] |
| 595 if not git_footers.get_footer_change_id(description) and not squash: | 598 if not git_footers.get_footer_change_id(description) and not squash: |
| 596 # TODOOOOO | |
| 597 calls += [ | 599 calls += [ |
| 598 # DownloadGerritHook(False) | 600 # DownloadGerritHook(False) |
| 599 ((False, ), | 601 ((False, ), |
| 600 ''), | 602 ''), |
| 601 # Amending of commit message to get the Change-Id. | 603 # Amending of commit message to get the Change-Id. |
| 602 ((['git', 'log', '--pretty=format:%s\n\n%b', | 604 ((['git', 'log', '--pretty=format:%s\n\n%b', |
| 603 'fake_ancestor_sha..HEAD'],), | 605 'fake_ancestor_sha..HEAD'],), |
| 604 description), | 606 description), |
| 605 ((['git', 'commit', '--amend', '-m', description],), | 607 ((['git', 'commit', '--amend', '-m', description],), |
| 606 ''), | 608 ''), |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 638 receive_pack += '--cc=joe@example.com' # from watch list | 640 receive_pack += '--cc=joe@example.com' # from watch list |
| 639 if reviewers: | 641 if reviewers: |
| 640 receive_pack += ' ' | 642 receive_pack += ' ' |
| 641 receive_pack += ' '.join( | 643 receive_pack += ' '.join( |
| 642 '--reviewer=' + email for email in sorted(reviewers)) | 644 '--reviewer=' + email for email in sorted(reviewers)) |
| 643 receive_pack += '' | 645 receive_pack += '' |
| 644 calls += [ | 646 calls += [ |
| 645 ((['git', | 647 ((['git', |
| 646 'push', receive_pack, 'origin', | 648 'push', receive_pack, 'origin', |
| 647 ref_to_push + ':refs/for/refs/heads/master'],), | 649 ref_to_push + ':refs/for/refs/heads/master'],), |
| 648 '') | 650 ('remote:\n' |
| 651 'remote: Processing changes: (\)\n' |
| 652 'remote: Processing changes: (|)\n' |
| 653 'remote: Processing changes: (/)\n' |
| 654 'remote: Processing changes: (-)\n' |
| 655 'remote: Processing changes: new: 1 (/)\n' |
| 656 'remote: Processing changes: new: 1, done\n' |
| 657 'remote:\n' |
| 658 'remote: New Changes:\n' |
| 659 'remote: https://chromium-review.googlesource.com/123456 XXX.\n' |
| 660 'remote:\n' |
| 661 'To https://chromium.googlesource.com/yyy/zzz\n' |
| 662 ' * [new branch] hhhh -> refs/for/refs/heads/master\n')), |
| 649 ] | 663 ] |
| 650 if squash: | 664 if squash: |
| 651 calls += [ | 665 calls += [ |
| 666 ((['git', 'config', 'branch.master.gerritissue', '123456'],), ''), |
| 652 ((['git', 'rev-parse', 'HEAD'],), 'abcdef0123456789'), | 667 ((['git', 'rev-parse', 'HEAD'],), 'abcdef0123456789'), |
| 653 ((['git', 'update-ref', '-m', 'Uploaded abcdef0123456789', | 668 ((['git', 'update-ref', '-m', 'Uploaded abcdef0123456789', |
| 654 'refs/heads/git_cl_uploads/master', 'abcdef0123456789'],), | 669 'refs/heads/git_cl_uploads/master', 'abcdef0123456789'],), |
| 655 '') | 670 '') |
| 656 ] | 671 ] |
| 657 calls += cls._git_post_upload_calls() | 672 calls += cls._git_post_upload_calls() |
| 658 return calls | 673 return calls |
| 659 | 674 |
| 660 def _run_gerrit_upload_test( | 675 def _run_gerrit_upload_test( |
| 661 self, | 676 self, |
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 902 self.calls += [ | 917 self.calls += [ |
| 903 ((['git', 'apply', '--index', '-p0', '--3way'],), '', | 918 ((['git', 'apply', '--index', '-p0', '--3way'],), '', |
| 904 subprocess2.CalledProcessError(1, '', '', '', '')), | 919 subprocess2.CalledProcessError(1, '', '', '', '')), |
| 905 ] | 920 ] |
| 906 self.assertNotEqual(git_cl.main(['patch', '123456']), 0) | 921 self.assertNotEqual(git_cl.main(['patch', '123456']), 0) |
| 907 | 922 |
| 908 if __name__ == '__main__': | 923 if __name__ == '__main__': |
| 909 git_cl.logging.basicConfig( | 924 git_cl.logging.basicConfig( |
| 910 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) | 925 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) |
| 911 unittest.main() | 926 unittest.main() |
| OLD | NEW |