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 calls += [((['git', 'log', '--pretty=format:%s\n\n%b', |
599 'fake_ancestor_sha..HEAD'],), | |
600 description)] | |
592 if not git_footers.get_footer_change_id(description) and not squash: | 601 if not git_footers.get_footer_change_id(description) and not squash: |
593 calls += [ | 602 calls += [ |
594 # DownloadGerritHook(False) | 603 # DownloadGerritHook(False) |
595 ((False, ), | 604 ((False, ), |
596 ''), | 605 ''), |
597 # Amending of commit message to get the Change-Id. | 606 # Amending of commit message to get the Change-Id. |
598 ((['git', 'log', '--pretty=format:%s\n\n%b', | 607 ((['git', 'log', '--pretty=format:%s\n\n%b', |
599 'fake_ancestor_sha..HEAD'],), | 608 'fake_ancestor_sha..HEAD'],), |
600 description), | 609 description), |
601 ((['git', 'commit', '--amend', '-m', description],), | 610 ((['git', 'commit', '--amend', '-m', description],), |
602 ''), | 611 ''), |
603 ((['git', 'log', '--pretty=format:%s\n\n%b', | 612 ((['git', 'log', '--pretty=format:%s\n\n%b', |
604 'fake_ancestor_sha..HEAD'],), | 613 'fake_ancestor_sha..HEAD'],), |
605 post_amend_description) | 614 post_amend_description) |
606 ] | 615 ] |
607 if squash: | 616 if squash: |
617 if not issue: | |
618 # Prompting to edit description on first upload. | |
619 calls += [ | |
620 ((['git', 'config', 'core.editor'],), ''), | |
621 ((['RunEditor'],), description), | |
622 ] | |
608 ref_to_push = 'abcdef0123456789' | 623 ref_to_push = 'abcdef0123456789' |
609 calls += [ | 624 calls += [ |
610 ((['git', 'show', '--format=%B', '-s', | |
611 'refs/heads/git_cl_uploads/master'],), | |
612 (description, 0)), | |
613 ((['git', 'config', 'branch.master.merge'],), | 625 ((['git', 'config', 'branch.master.merge'],), |
614 'refs/heads/master'), | 626 'refs/heads/master'), |
615 ((['git', 'config', 'branch.master.remote'],), | 627 ((['git', 'config', 'branch.master.remote'],), |
616 'origin'), | 628 'origin'), |
617 ((['get_or_create_merge_base', 'master', 'master'],), | 629 ((['get_or_create_merge_base', 'master', 'master'],), |
618 'origin/master'), | 630 'origin/master'), |
619 ((['git', 'rev-parse', 'HEAD:'],), | 631 ((['git', 'rev-parse', 'HEAD:'],), |
620 '0123456789abcdef'), | 632 '0123456789abcdef'), |
621 ((['git', 'commit-tree', '0123456789abcdef', '-p', | 633 ((['git', 'commit-tree', '0123456789abcdef', '-p', |
622 'origin/master', '-m', 'd'],), | 634 'origin/master', '-m', description],), |
623 ref_to_push), | 635 ref_to_push), |
624 ] | 636 ] |
625 else: | 637 else: |
626 ref_to_push = 'HEAD' | 638 ref_to_push = 'HEAD' |
627 | 639 |
628 calls += [ | 640 calls += [ |
629 ((['git', 'rev-list', | 641 ((['git', 'rev-list', |
630 expected_upstream_ref + '..' + ref_to_push],), ''), | 642 expected_upstream_ref + '..' + ref_to_push],), ''), |
631 ((['git', 'config', 'rietveld.cc'],), '') | 643 ((['git', 'config', 'rietveld.cc'],), '') |
632 ] | 644 ] |
(...skipping 23 matching lines...) Expand all Loading... | |
656 ' * [new branch] hhhh -> refs/for/refs/heads/master\n')), | 668 ' * [new branch] hhhh -> refs/for/refs/heads/master\n')), |
657 ] | 669 ] |
658 if squash: | 670 if squash: |
659 calls += [ | 671 calls += [ |
660 ((['git', 'config', 'branch.master.gerritissue', '123456'],), ''), | 672 ((['git', 'config', 'branch.master.gerritissue', '123456'],), ''), |
661 ((['git', 'config', 'branch.master.gerritserver'],), ''), | 673 ((['git', 'config', 'branch.master.gerritserver'],), ''), |
662 ((['git', 'config', 'remote.origin.url'],), | 674 ((['git', 'config', 'remote.origin.url'],), |
663 'https://chromium.googlesource.com/my/repo.git'), | 675 'https://chromium.googlesource.com/my/repo.git'), |
664 ((['git', 'config', 'branch.master.gerritserver', | 676 ((['git', 'config', 'branch.master.gerritserver', |
665 'https://chromium-review.googlesource.com'],), ''), | 677 'https://chromium-review.googlesource.com'],), ''), |
666 ((['git', 'rev-parse', 'HEAD'],), 'abcdef0123456789'), | 678 ((['git', 'config', 'branch.master.gerritsquashhash', |
667 ((['git', 'update-ref', '-m', 'Uploaded abcdef0123456789', | 679 'abcdef0123456789'],), ''), |
ukai
2016/03/29 02:16:16
ref_to_push ?
| |
668 'refs/heads/git_cl_uploads/master', 'abcdef0123456789'],), | |
669 '') | |
670 ] | 680 ] |
671 calls += cls._git_post_upload_calls() | 681 calls += cls._git_post_upload_calls() |
672 return calls | 682 return calls |
673 | 683 |
674 def _run_gerrit_upload_test( | 684 def _run_gerrit_upload_test( |
675 self, | 685 self, |
676 upload_args, | 686 upload_args, |
677 description, | 687 description, |
678 reviewers, | 688 reviewers, |
679 squash=False, | 689 squash=False, |
680 expected_upstream_ref='origin/refs/heads/master', | 690 expected_upstream_ref='origin/refs/heads/master', |
681 post_amend_description=None): | 691 post_amend_description=None, |
692 issue=None): | |
682 """Generic gerrit upload test framework.""" | 693 """Generic gerrit upload test framework.""" |
683 self.calls = self._gerrit_base_calls() | 694 self.calls = self._gerrit_base_calls(issue=issue) |
684 self.calls += self._gerrit_upload_calls( | 695 self.calls += self._gerrit_upload_calls( |
685 description, reviewers, squash, | 696 description, reviewers, squash, |
686 expected_upstream_ref=expected_upstream_ref, | 697 expected_upstream_ref=expected_upstream_ref, |
687 post_amend_description=post_amend_description) | 698 post_amend_description=post_amend_description, |
699 issue=issue) | |
688 # Uncomment when debugging. | 700 # Uncomment when debugging. |
689 # print '\n'.join(map(lambda x: '%2i: %s' % x, enumerate(self.calls))) | 701 # print '\n'.join(map(lambda x: '%2i: %s' % x, enumerate(self.calls))) |
690 git_cl.main(['upload'] + upload_args) | 702 git_cl.main(['upload'] + upload_args) |
691 | 703 |
692 def test_gerrit_upload_without_change_id(self): | 704 def test_gerrit_upload_without_change_id(self): |
693 self.mock(git_cl, 'DownloadGerritHook', self._mocked_call) | 705 self.mock(git_cl, 'DownloadGerritHook', self._mocked_call) |
694 self._run_gerrit_upload_test( | 706 self._run_gerrit_upload_test( |
695 [], | 707 [], |
696 'desc\n\nBUG=\n', | 708 'desc\n\nBUG=\n', |
697 [], | 709 [], |
(...skipping 11 matching lines...) Expand all Loading... | |
709 'desc\n\nBUG=\n\nChange-Id: I123456789', | 721 'desc\n\nBUG=\n\nChange-Id: I123456789', |
710 ['foo@example.com']) | 722 ['foo@example.com']) |
711 | 723 |
712 def test_gerrit_reviewer_multiple(self): | 724 def test_gerrit_reviewer_multiple(self): |
713 self._run_gerrit_upload_test( | 725 self._run_gerrit_upload_test( |
714 [], | 726 [], |
715 'desc\nTBR=reviewer@example.com\nBUG=\nR=another@example.com\n\n' | 727 'desc\nTBR=reviewer@example.com\nBUG=\nR=another@example.com\n\n' |
716 'Change-Id: 123456789\n', | 728 'Change-Id: 123456789\n', |
717 ['reviewer@example.com', 'another@example.com']) | 729 ['reviewer@example.com', 'another@example.com']) |
718 | 730 |
719 def test_gerrit_upload_squash(self): | 731 def test_gerrit_upload_squash_first(self): |
732 # Mock Gerrit CL description to indicate the first upload. | |
733 self.mock(git_cl.Changelist, 'GetDescription', | |
734 lambda *_: None) | |
735 self.mock(git_cl.gclient_utils, 'RunEditor', | |
736 lambda *_, **__: self._mocked_call(['RunEditor'])) | |
720 self._run_gerrit_upload_test( | 737 self._run_gerrit_upload_test( |
721 ['--squash'], | 738 ['--squash'], |
722 'desc\n\nBUG=\nChange-Id:123456789\n', | 739 'desc\nBUG=\n\nChange-Id: 123456789', |
723 [], | 740 [], |
724 squash=True, | 741 squash=True, |
725 expected_upstream_ref='origin/master') | 742 expected_upstream_ref='origin/master') |
726 | 743 |
744 def test_gerrit_upload_squash_reupload(self): | |
745 description = 'desc\nBUG=\n\nChange-Id: 123456789' | |
746 # Mock Gerrit CL description to indicate re-upload. | |
747 self.mock(git_cl.Changelist, 'GetDescription', | |
748 lambda *args: description) | |
749 self.mock(git_cl.Changelist, 'GetMostRecentPatchset', | |
750 lambda *args: 1) | |
751 self._run_gerrit_upload_test( | |
752 ['--squash'], | |
753 description, | |
754 [], | |
755 squash=True, | |
756 expected_upstream_ref='origin/master', | |
757 issue=123456) | |
758 | |
727 def test_upload_branch_deps(self): | 759 def test_upload_branch_deps(self): |
728 def mock_run_git(*args, **_kwargs): | 760 def mock_run_git(*args, **_kwargs): |
729 if args[0] == ['for-each-ref', | 761 if args[0] == ['for-each-ref', |
730 '--format=%(refname:short) %(upstream:short)', | 762 '--format=%(refname:short) %(upstream:short)', |
731 'refs/heads']: | 763 'refs/heads']: |
732 # Create a local branch dependency tree that looks like this: | 764 # Create a local branch dependency tree that looks like this: |
733 # test1 -> test2 -> test3 -> test4 -> test5 | 765 # test1 -> test2 -> test3 -> test4 -> test5 |
734 # -> test3.1 | 766 # -> test3.1 |
735 # test6 -> test0 | 767 # test6 -> test0 |
736 branch_deps = [ | 768 branch_deps = [ |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
921 self.calls += [ | 953 self.calls += [ |
922 ((['git', 'apply', '--index', '-p0', '--3way'],), '', | 954 ((['git', 'apply', '--index', '-p0', '--3way'],), '', |
923 subprocess2.CalledProcessError(1, '', '', '', '')), | 955 subprocess2.CalledProcessError(1, '', '', '', '')), |
924 ] | 956 ] |
925 self.assertNotEqual(git_cl.main(['patch', '123456']), 0) | 957 self.assertNotEqual(git_cl.main(['patch', '123456']), 0) |
926 | 958 |
927 if __name__ == '__main__': | 959 if __name__ == '__main__': |
928 git_cl.logging.basicConfig( | 960 git_cl.logging.basicConfig( |
929 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) | 961 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) |
930 unittest.main() | 962 unittest.main() |
OLD | NEW |