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 739 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
750 ]) + [ | 750 ]) + [ |
751 ((['git', 'config', 'user.email'],), 'me@example.com'), | 751 ((['git', 'config', 'user.email'],), 'me@example.com'), |
752 ((['git', | 752 ((['git', |
753 'diff', '--no-ext-diff', '--stat', '--find-copies-harder', | 753 'diff', '--no-ext-diff', '--stat', '--find-copies-harder', |
754 '-l100000', '-C50', 'fake_ancestor_sha', 'HEAD'],), | 754 '-l100000', '-C50', 'fake_ancestor_sha', 'HEAD'],), |
755 '+dat'), | 755 '+dat'), |
756 ] | 756 ] |
757 | 757 |
758 @classmethod | 758 @classmethod |
759 def _gerrit_upload_calls(cls, description, reviewers, squash, | 759 def _gerrit_upload_calls(cls, description, reviewers, squash, |
| 760 squash_mode='default', |
760 expected_upstream_ref='origin/refs/heads/master', | 761 expected_upstream_ref='origin/refs/heads/master', |
761 ref_suffix='', notify=False, | 762 ref_suffix='', notify=False, |
762 post_amend_description=None, issue=None): | 763 post_amend_description=None, issue=None): |
763 if post_amend_description is None: | 764 if post_amend_description is None: |
764 post_amend_description = description | 765 post_amend_description = description |
| 766 calls = [] |
765 | 767 |
766 calls = [ | 768 if squash_mode == 'default': |
767 ((['git', 'config', '--bool', 'gerrit.squash-uploads'],), 'false'), | 769 calls.extend([ |
768 ] | 770 ((['git', 'config', '--bool', 'gerrit.override-squash-uploads'],), ''), |
| 771 ((['git', 'config', '--bool', 'gerrit.squash-uploads'],), ''), |
| 772 ]) |
| 773 elif squash_mode in ('override_squash', 'override_nosquash'): |
| 774 calls.extend([ |
| 775 ((['git', 'config', '--bool', 'gerrit.override-squash-uploads'],), |
| 776 'true' if squash_mode == 'override_squash' else 'false'), |
| 777 ]) |
| 778 else: |
| 779 assert squash_mode in ('squash', 'nosquash') |
| 780 |
769 # If issue is given, then description is fetched from Gerrit instead. | 781 # If issue is given, then description is fetched from Gerrit instead. |
770 if issue is None: | 782 if issue is None: |
771 if squash: | 783 if squash: |
772 calls += [ | 784 calls += [ |
773 ((['git', 'show', '--format=%B', '-s', | 785 ((['git', 'show', '--format=%B', '-s', |
774 'refs/heads/git_cl_uploads/master'],), '')] | 786 'refs/heads/git_cl_uploads/master'],), '')] |
775 calls += [ | 787 calls += [ |
776 ((['git', 'log', '--pretty=format:%s\n\n%b', | 788 ((['git', 'log', '--pretty=format:%s\n\n%b', |
777 'fake_ancestor_sha..HEAD'],), | 789 'fake_ancestor_sha..HEAD'],), |
778 description)] | 790 description)] |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
860 'abcdef0123456789'],), ''), | 872 'abcdef0123456789'],), ''), |
861 ] | 873 ] |
862 calls += cls._git_post_upload_calls() | 874 calls += cls._git_post_upload_calls() |
863 return calls | 875 return calls |
864 | 876 |
865 def _run_gerrit_upload_test( | 877 def _run_gerrit_upload_test( |
866 self, | 878 self, |
867 upload_args, | 879 upload_args, |
868 description, | 880 description, |
869 reviewers=None, | 881 reviewers=None, |
870 squash=False, | 882 squash=True, |
| 883 squash_mode=None, |
871 expected_upstream_ref='origin/refs/heads/master', | 884 expected_upstream_ref='origin/refs/heads/master', |
872 ref_suffix='', | 885 ref_suffix='', |
873 notify=False, | 886 notify=False, |
874 post_amend_description=None, | 887 post_amend_description=None, |
875 issue=None): | 888 issue=None): |
876 """Generic gerrit upload test framework.""" | 889 """Generic gerrit upload test framework.""" |
| 890 if squash_mode is None: |
| 891 if '--no-squash' in upload_args: |
| 892 squash_mode = 'nosquash' |
| 893 elif '--squash' in upload_args: |
| 894 squash_mode = 'squash' |
| 895 else: |
| 896 squash_mode = 'default' |
| 897 |
877 reviewers = reviewers or [] | 898 reviewers = reviewers or [] |
878 self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) | 899 self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
879 self.mock(git_cl.gerrit_util, 'CookiesAuthenticator', | 900 self.mock(git_cl.gerrit_util, 'CookiesAuthenticator', |
880 CookiesAuthenticatorMockFactory(same_cookie='same_cred')) | 901 CookiesAuthenticatorMockFactory(same_cookie='same_cred')) |
881 self.mock(git_cl._GerritChangelistImpl, '_GerritCommitMsgHookCheck', | 902 self.mock(git_cl._GerritChangelistImpl, '_GerritCommitMsgHookCheck', |
882 lambda _, offer_removal: None) | 903 lambda _, offer_removal: None) |
| 904 self.mock(git_cl.gclient_utils, 'RunEditor', |
| 905 lambda *_, **__: self._mocked_call(['RunEditor'])) |
| 906 self.mock(git_cl, 'DownloadGerritHook', self._mocked_call) |
| 907 |
883 self.calls = self._gerrit_base_calls(issue=issue) | 908 self.calls = self._gerrit_base_calls(issue=issue) |
884 self.calls += self._gerrit_upload_calls( | 909 self.calls += self._gerrit_upload_calls( |
885 description, reviewers, squash, | 910 description, reviewers, squash, |
| 911 squash_mode=squash_mode, |
886 expected_upstream_ref=expected_upstream_ref, | 912 expected_upstream_ref=expected_upstream_ref, |
887 ref_suffix=ref_suffix, notify=notify, | 913 ref_suffix=ref_suffix, notify=notify, |
888 post_amend_description=post_amend_description, | 914 post_amend_description=post_amend_description, |
889 issue=issue) | 915 issue=issue) |
890 # Uncomment when debugging. | 916 # Uncomment when debugging. |
891 # print '\n'.join(map(lambda x: '%2i: %s' % x, enumerate(self.calls))) | 917 # print '\n'.join(map(lambda x: '%2i: %s' % x, enumerate(self.calls))) |
892 git_cl.main(['upload'] + upload_args) | 918 git_cl.main(['upload'] + upload_args) |
893 | 919 |
894 def test_gerrit_upload_without_change_id(self): | 920 def test_gerrit_upload_without_change_id(self): |
| 921 self._run_gerrit_upload_test( |
| 922 ['--no-squash'], |
| 923 'desc\n\nBUG=\n', |
| 924 [], |
| 925 squash=False, |
| 926 post_amend_description='desc\n\nBUG=\n\nChange-Id: Ixxx') |
| 927 |
| 928 def test_gerrit_upload_without_change_id_override_nosquash(self): |
895 self.mock(git_cl, 'DownloadGerritHook', self._mocked_call) | 929 self.mock(git_cl, 'DownloadGerritHook', self._mocked_call) |
896 self._run_gerrit_upload_test( | 930 self._run_gerrit_upload_test( |
897 [], | 931 [], |
898 'desc\n\nBUG=\n', | 932 'desc\n\nBUG=\n', |
899 [], | 933 [], |
| 934 squash=False, |
| 935 squash_mode='override_nosquash', |
900 post_amend_description='desc\n\nBUG=\n\nChange-Id: Ixxx') | 936 post_amend_description='desc\n\nBUG=\n\nChange-Id: Ixxx') |
901 | 937 |
902 def test_gerrit_no_reviewer(self): | 938 def test_gerrit_no_reviewer(self): |
903 self._run_gerrit_upload_test( | 939 self._run_gerrit_upload_test( |
904 [], | 940 [], |
905 'desc\n\nBUG=\n\nChange-Id: I123456789\n', | 941 'desc\n\nBUG=\n\nChange-Id: I123456789\n', |
906 []) | 942 [], |
| 943 squash=False, |
| 944 squash_mode='override_nosquash') |
907 | 945 |
908 def test_gerrit_patch_title(self): | 946 def test_gerrit_patch_title(self): |
909 self._run_gerrit_upload_test( | 947 self._run_gerrit_upload_test( |
910 ['-t', 'Don\'t put under_scores as they become spaces'], | 948 ['-t', 'Don\'t put under_scores as they become spaces'], |
911 'desc\n\nBUG=\n\nChange-Id: I123456789', | 949 'desc\n\nBUG=\n\nChange-Id: I123456789', |
| 950 squash=False, |
| 951 squash_mode='override_nosquash', |
912 ref_suffix='%m=Don\'t_put_under_scores_as_they_become_spaces') | 952 ref_suffix='%m=Don\'t_put_under_scores_as_they_become_spaces') |
913 | 953 |
914 def test_gerrit_reviewers_cmd_line(self): | 954 def test_gerrit_reviewers_cmd_line(self): |
915 self._run_gerrit_upload_test( | 955 self._run_gerrit_upload_test( |
916 ['-r', 'foo@example.com', '--send-mail'], | 956 ['-r', 'foo@example.com', '--send-mail'], |
917 'desc\n\nBUG=\n\nChange-Id: I123456789', | 957 'desc\n\nBUG=\n\nChange-Id: I123456789', |
918 ['foo@example.com'], | 958 ['foo@example.com'], |
| 959 squash=False, |
| 960 squash_mode='override_nosquash', |
919 notify=True) | 961 notify=True) |
920 | 962 |
921 def test_gerrit_reviewer_multiple(self): | 963 def test_gerrit_reviewer_multiple(self): |
922 self._run_gerrit_upload_test( | 964 self._run_gerrit_upload_test( |
923 [], | 965 [], |
924 'desc\nTBR=reviewer@example.com\nBUG=\nR=another@example.com\n\n' | 966 'desc\nTBR=reviewer@example.com\nBUG=\nR=another@example.com\n\n' |
925 'Change-Id: 123456789\n', | 967 'Change-Id: 123456789\n', |
926 ['reviewer@example.com', 'another@example.com']) | 968 ['reviewer@example.com', 'another@example.com'], |
| 969 squash=False, |
| 970 squash_mode='override_nosquash') |
| 971 |
| 972 def test_gerrit_upload_squash_first_is_default(self): |
| 973 # Mock Gerrit CL description to indicate the first upload. |
| 974 self.mock(git_cl.Changelist, 'GetDescription', |
| 975 lambda *_: None) |
| 976 self._run_gerrit_upload_test( |
| 977 [], |
| 978 'desc\nBUG=\n\nChange-Id: 123456789', |
| 979 [], |
| 980 expected_upstream_ref='origin/master') |
927 | 981 |
928 def test_gerrit_upload_squash_first(self): | 982 def test_gerrit_upload_squash_first(self): |
929 # Mock Gerrit CL description to indicate the first upload. | 983 # Mock Gerrit CL description to indicate the first upload. |
930 self.mock(git_cl.Changelist, 'GetDescription', | 984 self.mock(git_cl.Changelist, 'GetDescription', |
931 lambda *_: None) | 985 lambda *_: None) |
932 self.mock(git_cl.gclient_utils, 'RunEditor', | |
933 lambda *_, **__: self._mocked_call(['RunEditor'])) | |
934 self._run_gerrit_upload_test( | 986 self._run_gerrit_upload_test( |
935 ['--squash'], | 987 ['--squash'], |
936 'desc\nBUG=\n\nChange-Id: 123456789', | 988 'desc\nBUG=\n\nChange-Id: 123456789', |
937 [], | 989 [], |
938 squash=True, | 990 squash=True, |
939 expected_upstream_ref='origin/master') | 991 expected_upstream_ref='origin/master') |
940 | 992 |
941 def test_gerrit_upload_squash_reupload(self): | 993 def test_gerrit_upload_squash_reupload(self): |
942 description = 'desc\nBUG=\n\nChange-Id: 123456789' | 994 description = 'desc\nBUG=\n\nChange-Id: 123456789' |
943 # Mock Gerrit CL description to indicate re-upload. | 995 # Mock Gerrit CL description to indicate re-upload. |
(...skipping 684 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1628 ((['rm_file_or_tree', '/abs/git_repo_root/.git/hooks/commit-msg'],), | 1680 ((['rm_file_or_tree', '/abs/git_repo_root/.git/hooks/commit-msg'],), |
1629 ''), | 1681 ''), |
1630 ] | 1682 ] |
1631 cl._codereview_impl._GerritCommitMsgHookCheck(offer_removal=True) | 1683 cl._codereview_impl._GerritCommitMsgHookCheck(offer_removal=True) |
1632 | 1684 |
1633 | 1685 |
1634 if __name__ == '__main__': | 1686 if __name__ == '__main__': |
1635 git_cl.logging.basicConfig( | 1687 git_cl.logging.basicConfig( |
1636 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) | 1688 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) |
1637 unittest.main() | 1689 unittest.main() |
OLD | NEW |