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 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 # diagnose. | 119 # diagnose. |
120 if expected_args != args: | 120 if expected_args != args: |
121 msg = '@%d Expected: %r Actual: %r' % ( | 121 msg = '@%d Expected: %r Actual: %r' % ( |
122 self._calls_done, expected_args, args) | 122 self._calls_done, expected_args, args) |
123 git_cl.logging.error(msg) | 123 git_cl.logging.error(msg) |
124 self.fail(msg) | 124 self.fail(msg) |
125 self._calls_done += 1 | 125 self._calls_done += 1 |
126 return result | 126 return result |
127 | 127 |
128 @classmethod | 128 @classmethod |
| 129 def _is_gerrit_calls(cls, gerrit=False): |
| 130 return [((['git', 'config', 'rietveld.autoupdate'],), ''), |
| 131 ((['git', 'config', 'gerrit.host'],), 'True' if gerrit else '')] |
| 132 |
| 133 @classmethod |
129 def _upload_calls(cls, similarity, find_copies, private): | 134 def _upload_calls(cls, similarity, find_copies, private): |
130 return (cls._git_base_calls(similarity, find_copies) + | 135 return (cls._git_base_calls(similarity, find_copies) + |
131 cls._git_upload_calls(private)) | 136 cls._git_upload_calls(private)) |
132 | 137 |
133 @classmethod | 138 @classmethod |
134 def _upload_no_rev_calls(cls, similarity, find_copies): | 139 def _upload_no_rev_calls(cls, similarity, find_copies): |
135 return (cls._git_base_calls(similarity, find_copies) + | 140 return (cls._git_base_calls(similarity, find_copies) + |
136 cls._git_upload_no_rev_calls()) | 141 cls._git_upload_no_rev_calls()) |
137 | 142 |
138 @classmethod | 143 @classmethod |
(...skipping 21 matching lines...) Expand all Loading... |
160 'fake_ancestor_sha', 'HEAD'],), '+dat') | 165 'fake_ancestor_sha', 'HEAD'],), '+dat') |
161 else: | 166 else: |
162 stat_call = ((['git', 'diff', '--no-ext-diff', '--stat', | 167 stat_call = ((['git', 'diff', '--no-ext-diff', '--stat', |
163 '-M'+similarity, 'fake_ancestor_sha', 'HEAD'],), '+dat') | 168 '-M'+similarity, 'fake_ancestor_sha', 'HEAD'],), '+dat') |
164 | 169 |
165 return [ | 170 return [ |
166 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), | 171 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
167 similarity_call, | 172 similarity_call, |
168 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), | 173 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
169 find_copies_call, | 174 find_copies_call, |
| 175 ] + cls._is_gerrit_calls() + [ |
170 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), | 176 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
171 ((['git', 'config', 'branch.master.rietveldissue'],), ''), | 177 ((['git', 'config', 'branch.master.rietveldissue'],), ''), |
172 ((['git', 'config', 'branch.master.gerritissue'],), ''), | 178 ((['git', 'config', 'branch.master.gerritissue'],), ''), |
173 ((['git', 'config', 'rietveld.autoupdate'],), ''), | |
174 ((['git', 'config', 'gerrit.host'],), ''), | |
175 ((['git', 'config', 'rietveld.server'],), | 179 ((['git', 'config', 'rietveld.server'],), |
176 'codereview.example.com'), | 180 'codereview.example.com'), |
177 ((['git', 'config', 'branch.master.merge'],), 'master'), | 181 ((['git', 'config', 'branch.master.merge'],), 'master'), |
178 ((['git', 'config', 'branch.master.remote'],), 'origin'), | 182 ((['git', 'config', 'branch.master.remote'],), 'origin'), |
179 ((['get_or_create_merge_base', 'master', 'master'],), | 183 ((['get_or_create_merge_base', 'master', 'master'],), |
180 'fake_ancestor_sha'), | 184 'fake_ancestor_sha'), |
181 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [ | 185 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [ |
182 ((['git', 'rev-parse', '--show-cdup'],), ''), | 186 ((['git', 'rev-parse', '--show-cdup'],), ''), |
183 ((['git', 'rev-parse', 'HEAD'],), '12345'), | 187 ((['git', 'rev-parse', 'HEAD'],), '12345'), |
184 ((['git', 'diff', '--name-status', '--no-renames', '-r', | 188 ((['git', 'diff', '--name-status', '--no-renames', '-r', |
185 'fake_ancestor_sha...', '.'],), | 189 'fake_ancestor_sha...', '.'],), |
186 'M\t.gitignore\n'), | 190 'M\t.gitignore\n'), |
187 ((['git', 'config', 'branch.master.rietveldpatchset'],), | 191 ((['git', 'config', 'branch.master.rietveldpatchset'],), |
188 ''), | 192 ''), |
189 ((['git', 'log', '--pretty=format:%s%n%n%b', | 193 ((['git', 'log', '--pretty=format:%s%n%n%b', |
190 'fake_ancestor_sha...'],), | 194 'fake_ancestor_sha...'],), |
191 'foo'), | 195 'foo'), |
(...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
535 | 539 |
536 def test_dcommit_bypass_hooks(self): | 540 def test_dcommit_bypass_hooks(self): |
537 self.calls = ( | 541 self.calls = ( |
538 self._dcommit_calls_1() + | 542 self._dcommit_calls_1() + |
539 self._dcommit_calls_bypassed() + | 543 self._dcommit_calls_bypassed() + |
540 self._dcommit_calls_3()) | 544 self._dcommit_calls_3()) |
541 git_cl.main(['dcommit', '--bypass-hooks']) | 545 git_cl.main(['dcommit', '--bypass-hooks']) |
542 | 546 |
543 | 547 |
544 @classmethod | 548 @classmethod |
545 def _gerrit_base_calls(cls): | 549 def _gerrit_base_calls(cls, issue=None): |
546 return [ | 550 return [ |
547 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), | 551 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
548 ((['git', 'config', '--int', '--get', | 552 ((['git', 'config', '--int', '--get', |
549 'branch.master.git-cl-similarity'],), ''), | 553 'branch.master.git-cl-similarity'],), ''), |
550 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), | 554 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
551 ((['git', 'config', '--int', '--get', | 555 ((['git', 'config', '--int', '--get', |
552 'branch.master.git-find-copies'],), ''), | 556 'branch.master.git-find-copies'],), ''), |
| 557 ] + cls._is_gerrit_calls(True) + [ |
553 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), | 558 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
554 ((['git', 'config', 'branch.master.rietveldissue'],), ''), | 559 ((['git', 'config', 'branch.master.rietveldissue'],), ''), |
555 ((['git', 'config', 'branch.master.gerritissue'],), ''), | 560 ((['git', 'config', 'branch.master.gerritissue'],), |
556 ((['git', 'config', 'rietveld.autoupdate'],), ''), | 561 '' if issue is None else str(issue)), |
557 ((['git', 'config', 'gerrit.host'],), 'True'), | |
558 ((['git', 'config', 'branch.master.merge'],), 'master'), | 562 ((['git', 'config', 'branch.master.merge'],), 'master'), |
559 ((['git', 'config', 'branch.master.remote'],), 'origin'), | 563 ((['git', 'config', 'branch.master.remote'],), 'origin'), |
560 ((['get_or_create_merge_base', 'master', 'master'],), | 564 ((['get_or_create_merge_base', 'master', 'master'],), |
561 'fake_ancestor_sha'), | 565 'fake_ancestor_sha'), |
562 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [ | 566 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [ |
563 ((['git', 'rev-parse', '--show-cdup'],), ''), | 567 ((['git', 'rev-parse', '--show-cdup'],), ''), |
564 ((['git', 'rev-parse', 'HEAD'],), '12345'), | 568 ((['git', 'rev-parse', 'HEAD'],), '12345'), |
565 ((['git', | 569 ((['git', |
566 'diff', '--name-status', '--no-renames', '-r', | 570 'diff', '--name-status', '--no-renames', '-r', |
567 'fake_ancestor_sha...', '.'],), | 571 'fake_ancestor_sha...', '.'],), |
568 'M\t.gitignore\n'), | 572 'M\t.gitignore\n'), |
569 ((['git', 'config', 'branch.master.gerritpatchset'],), ''), | 573 ((['git', 'config', 'branch.master.gerritpatchset'],), ''), |
| 574 ] + ([] if issue else [ |
570 ((['git', | 575 ((['git', |
571 'log', '--pretty=format:%s%n%n%b', 'fake_ancestor_sha...'],), | 576 'log', '--pretty=format:%s%n%n%b', 'fake_ancestor_sha...'],), |
572 'foo'), | 577 'foo'), |
| 578 ]) + [ |
573 ((['git', 'config', 'user.email'],), 'me@example.com'), | 579 ((['git', 'config', 'user.email'],), 'me@example.com'), |
574 ((['git', | 580 ((['git', |
575 'diff', '--no-ext-diff', '--stat', '--find-copies-harder', | 581 'diff', '--no-ext-diff', '--stat', '--find-copies-harder', |
576 '-l100000', '-C50', 'fake_ancestor_sha', 'HEAD'],), | 582 '-l100000', '-C50', 'fake_ancestor_sha', 'HEAD'],), |
577 '+dat'), | 583 '+dat'), |
578 ] | 584 ] |
579 | 585 |
580 @classmethod | 586 @classmethod |
581 def _gerrit_upload_calls(cls, description, reviewers, squash, | 587 def _gerrit_upload_calls(cls, description, reviewers, squash, |
582 expected_upstream_ref='origin/refs/heads/master', | 588 expected_upstream_ref='origin/refs/heads/master', |
583 post_amend_description=None): | 589 post_amend_description=None, issue=None): |
584 if post_amend_description is None: | 590 if post_amend_description is None: |
585 post_amend_description = description | 591 post_amend_description = description |
| 592 |
586 calls = [ | 593 calls = [ |
587 ((['git', 'config', '--bool', 'gerrit.squash-uploads'],), 'false'), | 594 ((['git', 'config', '--bool', 'gerrit.squash-uploads'],), 'false'), |
588 ((['git', 'log', '--pretty=format:%s\n\n%b', | 595 ] |
589 'fake_ancestor_sha..HEAD'],), | 596 # If issue is given, then description is fetched from Gerrit instead. |
590 description) | 597 if issue is None: |
591 ] | 598 if squash: |
| 599 calls += [ |
| 600 ((['git', 'show', '--format=%B', '-s', |
| 601 'refs/heads/git_cl_uploads/master'],), '')] |
| 602 calls += [ |
| 603 ((['git', 'log', '--pretty=format:%s\n\n%b', |
| 604 'fake_ancestor_sha..HEAD'],), |
| 605 description)] |
592 if not git_footers.get_footer_change_id(description) and not squash: | 606 if not git_footers.get_footer_change_id(description) and not squash: |
593 calls += [ | 607 calls += [ |
594 # DownloadGerritHook(False) | 608 # DownloadGerritHook(False) |
595 ((False, ), | 609 ((False, ), |
596 ''), | 610 ''), |
597 # Amending of commit message to get the Change-Id. | 611 # Amending of commit message to get the Change-Id. |
598 ((['git', 'log', '--pretty=format:%s\n\n%b', | 612 ((['git', 'log', '--pretty=format:%s\n\n%b', |
599 'fake_ancestor_sha..HEAD'],), | 613 'fake_ancestor_sha..HEAD'],), |
600 description), | 614 description), |
601 ((['git', 'commit', '--amend', '-m', description],), | 615 ((['git', 'commit', '--amend', '-m', description],), |
602 ''), | 616 ''), |
603 ((['git', 'log', '--pretty=format:%s\n\n%b', | 617 ((['git', 'log', '--pretty=format:%s\n\n%b', |
604 'fake_ancestor_sha..HEAD'],), | 618 'fake_ancestor_sha..HEAD'],), |
605 post_amend_description) | 619 post_amend_description) |
606 ] | 620 ] |
607 if squash: | 621 if squash: |
| 622 if not issue: |
| 623 # Prompting to edit description on first upload. |
| 624 calls += [ |
| 625 ((['git', 'config', 'core.editor'],), ''), |
| 626 ((['RunEditor'],), description), |
| 627 ] |
608 ref_to_push = 'abcdef0123456789' | 628 ref_to_push = 'abcdef0123456789' |
609 calls += [ | 629 calls += [ |
610 ((['git', 'show', '--format=%B', '-s', | |
611 'refs/heads/git_cl_uploads/master'],), | |
612 (description, 0)), | |
613 ((['git', 'config', 'branch.master.merge'],), | 630 ((['git', 'config', 'branch.master.merge'],), |
614 'refs/heads/master'), | 631 'refs/heads/master'), |
615 ((['git', 'config', 'branch.master.remote'],), | 632 ((['git', 'config', 'branch.master.remote'],), |
616 'origin'), | 633 'origin'), |
617 ((['get_or_create_merge_base', 'master', 'master'],), | 634 ((['get_or_create_merge_base', 'master', 'master'],), |
618 'origin/master'), | 635 'origin/master'), |
619 ((['git', 'rev-parse', 'HEAD:'],), | 636 ((['git', 'rev-parse', 'HEAD:'],), |
620 '0123456789abcdef'), | 637 '0123456789abcdef'), |
621 ((['git', 'commit-tree', '0123456789abcdef', '-p', | 638 ((['git', 'commit-tree', '0123456789abcdef', '-p', |
622 'origin/master', '-m', 'd'],), | 639 'origin/master', '-m', description],), |
623 ref_to_push), | 640 ref_to_push), |
624 ] | 641 ] |
625 else: | 642 else: |
626 ref_to_push = 'HEAD' | 643 ref_to_push = 'HEAD' |
627 | 644 |
628 calls += [ | 645 calls += [ |
629 ((['git', 'rev-list', | 646 ((['git', 'rev-list', |
630 expected_upstream_ref + '..' + ref_to_push],), ''), | 647 expected_upstream_ref + '..' + ref_to_push],), ''), |
631 ((['git', 'config', 'rietveld.cc'],), '') | 648 ((['git', 'config', 'rietveld.cc'],), '') |
632 ] | 649 ] |
(...skipping 23 matching lines...) Expand all Loading... |
656 ' * [new branch] hhhh -> refs/for/refs/heads/master\n')), | 673 ' * [new branch] hhhh -> refs/for/refs/heads/master\n')), |
657 ] | 674 ] |
658 if squash: | 675 if squash: |
659 calls += [ | 676 calls += [ |
660 ((['git', 'config', 'branch.master.gerritissue', '123456'],), ''), | 677 ((['git', 'config', 'branch.master.gerritissue', '123456'],), ''), |
661 ((['git', 'config', 'branch.master.gerritserver'],), ''), | 678 ((['git', 'config', 'branch.master.gerritserver'],), ''), |
662 ((['git', 'config', 'remote.origin.url'],), | 679 ((['git', 'config', 'remote.origin.url'],), |
663 'https://chromium.googlesource.com/my/repo.git'), | 680 'https://chromium.googlesource.com/my/repo.git'), |
664 ((['git', 'config', 'branch.master.gerritserver', | 681 ((['git', 'config', 'branch.master.gerritserver', |
665 'https://chromium-review.googlesource.com'],), ''), | 682 'https://chromium-review.googlesource.com'],), ''), |
666 ((['git', 'rev-parse', 'HEAD'],), 'abcdef0123456789'), | 683 ((['git', 'config', 'branch.master.gerritsquashhash', |
667 ((['git', 'update-ref', '-m', 'Uploaded abcdef0123456789', | 684 'abcdef0123456789'],), ''), |
668 'refs/heads/git_cl_uploads/master', 'abcdef0123456789'],), | |
669 '') | |
670 ] | 685 ] |
671 calls += cls._git_post_upload_calls() | 686 calls += cls._git_post_upload_calls() |
672 return calls | 687 return calls |
673 | 688 |
674 def _run_gerrit_upload_test( | 689 def _run_gerrit_upload_test( |
675 self, | 690 self, |
676 upload_args, | 691 upload_args, |
677 description, | 692 description, |
678 reviewers, | 693 reviewers, |
679 squash=False, | 694 squash=False, |
680 expected_upstream_ref='origin/refs/heads/master', | 695 expected_upstream_ref='origin/refs/heads/master', |
681 post_amend_description=None): | 696 post_amend_description=None, |
| 697 issue=None): |
682 """Generic gerrit upload test framework.""" | 698 """Generic gerrit upload test framework.""" |
683 self.calls = self._gerrit_base_calls() | 699 self.calls = self._gerrit_base_calls(issue=issue) |
684 self.calls += self._gerrit_upload_calls( | 700 self.calls += self._gerrit_upload_calls( |
685 description, reviewers, squash, | 701 description, reviewers, squash, |
686 expected_upstream_ref=expected_upstream_ref, | 702 expected_upstream_ref=expected_upstream_ref, |
687 post_amend_description=post_amend_description) | 703 post_amend_description=post_amend_description, |
| 704 issue=issue) |
688 # Uncomment when debugging. | 705 # Uncomment when debugging. |
689 # print '\n'.join(map(lambda x: '%2i: %s' % x, enumerate(self.calls))) | 706 # print '\n'.join(map(lambda x: '%2i: %s' % x, enumerate(self.calls))) |
690 git_cl.main(['upload'] + upload_args) | 707 git_cl.main(['upload'] + upload_args) |
691 | 708 |
692 def test_gerrit_upload_without_change_id(self): | 709 def test_gerrit_upload_without_change_id(self): |
693 self.mock(git_cl, 'DownloadGerritHook', self._mocked_call) | 710 self.mock(git_cl, 'DownloadGerritHook', self._mocked_call) |
694 self._run_gerrit_upload_test( | 711 self._run_gerrit_upload_test( |
695 [], | 712 [], |
696 'desc\n\nBUG=\n', | 713 'desc\n\nBUG=\n', |
697 [], | 714 [], |
(...skipping 11 matching lines...) Expand all Loading... |
709 'desc\n\nBUG=\n\nChange-Id: I123456789', | 726 'desc\n\nBUG=\n\nChange-Id: I123456789', |
710 ['foo@example.com']) | 727 ['foo@example.com']) |
711 | 728 |
712 def test_gerrit_reviewer_multiple(self): | 729 def test_gerrit_reviewer_multiple(self): |
713 self._run_gerrit_upload_test( | 730 self._run_gerrit_upload_test( |
714 [], | 731 [], |
715 'desc\nTBR=reviewer@example.com\nBUG=\nR=another@example.com\n\n' | 732 'desc\nTBR=reviewer@example.com\nBUG=\nR=another@example.com\n\n' |
716 'Change-Id: 123456789\n', | 733 'Change-Id: 123456789\n', |
717 ['reviewer@example.com', 'another@example.com']) | 734 ['reviewer@example.com', 'another@example.com']) |
718 | 735 |
719 def test_gerrit_upload_squash(self): | 736 def test_gerrit_upload_squash_first(self): |
| 737 # Mock Gerrit CL description to indicate the first upload. |
| 738 self.mock(git_cl.Changelist, 'GetDescription', |
| 739 lambda *_: None) |
| 740 self.mock(git_cl.gclient_utils, 'RunEditor', |
| 741 lambda *_, **__: self._mocked_call(['RunEditor'])) |
720 self._run_gerrit_upload_test( | 742 self._run_gerrit_upload_test( |
721 ['--squash'], | 743 ['--squash'], |
722 'desc\n\nBUG=\nChange-Id:123456789\n', | 744 'desc\nBUG=\n\nChange-Id: 123456789', |
723 [], | 745 [], |
724 squash=True, | 746 squash=True, |
725 expected_upstream_ref='origin/master') | 747 expected_upstream_ref='origin/master') |
726 | 748 |
| 749 def test_gerrit_upload_squash_reupload(self): |
| 750 description = 'desc\nBUG=\n\nChange-Id: 123456789' |
| 751 # Mock Gerrit CL description to indicate re-upload. |
| 752 self.mock(git_cl.Changelist, 'GetDescription', |
| 753 lambda *args: description) |
| 754 self.mock(git_cl.Changelist, 'GetMostRecentPatchset', |
| 755 lambda *args: 1) |
| 756 self.mock(git_cl._GerritChangelistImpl, '_GetChangeDetail', |
| 757 lambda *args: {'change_id': '123456789'}) |
| 758 self._run_gerrit_upload_test( |
| 759 ['--squash'], |
| 760 description, |
| 761 [], |
| 762 squash=True, |
| 763 expected_upstream_ref='origin/master', |
| 764 issue=123456) |
| 765 |
727 def test_upload_branch_deps(self): | 766 def test_upload_branch_deps(self): |
728 def mock_run_git(*args, **_kwargs): | 767 def mock_run_git(*args, **_kwargs): |
729 if args[0] == ['for-each-ref', | 768 if args[0] == ['for-each-ref', |
730 '--format=%(refname:short) %(upstream:short)', | 769 '--format=%(refname:short) %(upstream:short)', |
731 'refs/heads']: | 770 'refs/heads']: |
732 # Create a local branch dependency tree that looks like this: | 771 # Create a local branch dependency tree that looks like this: |
733 # test1 -> test2 -> test3 -> test4 -> test5 | 772 # test1 -> test2 -> test3 -> test4 -> test5 |
734 # -> test3.1 | 773 # -> test3.1 |
735 # test6 -> test0 | 774 # test6 -> test0 |
736 branch_deps = [ | 775 branch_deps = [ |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
921 self.calls += [ | 960 self.calls += [ |
922 ((['git', 'apply', '--index', '-p0', '--3way'],), '', | 961 ((['git', 'apply', '--index', '-p0', '--3way'],), '', |
923 subprocess2.CalledProcessError(1, '', '', '', '')), | 962 subprocess2.CalledProcessError(1, '', '', '', '')), |
924 ] | 963 ] |
925 self.assertNotEqual(git_cl.main(['patch', '123456']), 0) | 964 self.assertNotEqual(git_cl.main(['patch', '123456']), 0) |
926 | 965 |
927 if __name__ == '__main__': | 966 if __name__ == '__main__': |
928 git_cl.logging.basicConfig( | 967 git_cl.logging.basicConfig( |
929 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) | 968 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) |
930 unittest.main() | 969 unittest.main() |
OLD | NEW |