| 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 json | 8 import json |
| 9 import os | 9 import os |
| 10 import StringIO | 10 import StringIO |
| (...skipping 581 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 592 ''), | 592 ''), |
| 593 ((['git', 'config', 'rietveld.force-https-commit-url'],), ''), | 593 ((['git', 'config', 'rietveld.force-https-commit-url'],), ''), |
| 594 ((['git', | 594 ((['git', |
| 595 'svn', 'dcommit', '-C50', '--no-rebase', '--rmdir'],), | 595 'svn', 'dcommit', '-C50', '--no-rebase', '--rmdir'],), |
| 596 (('', None), 0)), | 596 (('', None), 0)), |
| 597 ((['git', 'checkout', '-q', 'working'],), ''), | 597 ((['git', 'checkout', '-q', 'working'],), ''), |
| 598 ((['git', 'branch', '-D', 'git-cl-commit'],), ''), | 598 ((['git', 'branch', '-D', 'git-cl-commit'],), ''), |
| 599 ] | 599 ] |
| 600 | 600 |
| 601 @staticmethod | 601 @staticmethod |
| 602 def _cmd_line(description, args, similarity, find_copies, private): | 602 def _cmd_line(description, args, similarity, find_copies, private, cc): |
| 603 """Returns the upload command line passed to upload.RealMain().""" | 603 """Returns the upload command line passed to upload.RealMain().""" |
| 604 return [ | 604 return [ |
| 605 'upload', '--assume_yes', '--server', | 605 'upload', '--assume_yes', '--server', |
| 606 'https://codereview.example.com', | 606 'https://codereview.example.com', |
| 607 '--message', description | 607 '--message', description |
| 608 ] + args + [ | 608 ] + args + [ |
| 609 '--cc', 'joe@example.com', | 609 '--cc', ','.join(['joe@example.com'] + cc), |
| 610 ] + (['--private'] if private else []) + [ | 610 ] + (['--private'] if private else []) + [ |
| 611 '--git_similarity', similarity or '50' | 611 '--git_similarity', similarity or '50' |
| 612 ] + (['--git_no_find_copies'] if find_copies == False else []) + [ | 612 ] + (['--git_no_find_copies'] if find_copies == False else []) + [ |
| 613 'fake_ancestor_sha', 'HEAD' | 613 'fake_ancestor_sha', 'HEAD' |
| 614 ] | 614 ] |
| 615 | 615 |
| 616 def _run_reviewer_test( | 616 def _run_reviewer_test( |
| 617 self, | 617 self, |
| 618 upload_args, | 618 upload_args, |
| 619 expected_description, | 619 expected_description, |
| 620 returned_description, | 620 returned_description, |
| 621 final_description, | 621 final_description, |
| 622 reviewers, | 622 reviewers, |
| 623 private=False): | 623 private=False, |
| 624 cc=None): |
| 624 """Generic reviewer test framework.""" | 625 """Generic reviewer test framework.""" |
| 625 self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) | 626 self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
| 626 try: | 627 try: |
| 627 similarity = upload_args[upload_args.index('--similarity')+1] | 628 similarity = upload_args[upload_args.index('--similarity')+1] |
| 628 except ValueError: | 629 except ValueError: |
| 629 similarity = None | 630 similarity = None |
| 630 | 631 |
| 631 if '--find-copies' in upload_args: | 632 if '--find-copies' in upload_args: |
| 632 find_copies = True | 633 find_copies = True |
| 633 elif '--no-find-copies' in upload_args: | 634 elif '--no-find-copies' in upload_args: |
| 634 find_copies = False | 635 find_copies = False |
| 635 else: | 636 else: |
| 636 find_copies = None | 637 find_copies = None |
| 637 | 638 |
| 638 private = '--private' in upload_args | 639 private = '--private' in upload_args |
| 640 cc = cc or [] |
| 639 | 641 |
| 640 self.calls = self._upload_calls(similarity, find_copies, private) | 642 self.calls = self._upload_calls(similarity, find_copies, private) |
| 641 | 643 |
| 642 def RunEditor(desc, _, **kwargs): | 644 def RunEditor(desc, _, **kwargs): |
| 643 self.assertEquals( | 645 self.assertEquals( |
| 644 '# Enter a description of the change.\n' | 646 '# Enter a description of the change.\n' |
| 645 '# This will be displayed on the codereview site.\n' | 647 '# This will be displayed on the codereview site.\n' |
| 646 '# The first line will also be used as the subject of the review.\n' | 648 '# The first line will also be used as the subject of the review.\n' |
| 647 '#--------------------This line is 72 characters long' | 649 '#--------------------This line is 72 characters long' |
| 648 '--------------------\n' + | 650 '--------------------\n' + |
| 649 expected_description, | 651 expected_description, |
| 650 desc) | 652 desc) |
| 651 return returned_description | 653 return returned_description |
| 652 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor) | 654 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor) |
| 653 | 655 |
| 654 def check_upload(args): | 656 def check_upload(args): |
| 655 cmd_line = self._cmd_line(final_description, reviewers, similarity, | 657 cmd_line = self._cmd_line(final_description, reviewers, similarity, |
| 656 find_copies, private) | 658 find_copies, private, cc) |
| 657 self.assertEquals(cmd_line, args) | 659 self.assertEquals(cmd_line, args) |
| 658 return 1, 2 | 660 return 1, 2 |
| 659 self.mock(git_cl.upload, 'RealMain', check_upload) | 661 self.mock(git_cl.upload, 'RealMain', check_upload) |
| 660 | 662 |
| 661 git_cl.main(['upload'] + upload_args) | 663 git_cl.main(['upload'] + upload_args) |
| 662 | 664 |
| 663 def test_no_reviewer(self): | 665 def test_no_reviewer(self): |
| 664 self._run_reviewer_test( | 666 self._run_reviewer_test( |
| 665 [], | 667 [], |
| 666 'desc\n\nBUG=', | 668 'desc\n\nBUG=', |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 709 self._run_reviewer_test( | 711 self._run_reviewer_test( |
| 710 ['-r' 'foo@example.com'], | 712 ['-r' 'foo@example.com'], |
| 711 'desc\n\nR=foo@example.com\nBUG=', | 713 'desc\n\nR=foo@example.com\nBUG=', |
| 712 description.strip('\n'), | 714 description.strip('\n'), |
| 713 description, | 715 description, |
| 714 ['--reviewers=reviewer@example.com']) | 716 ['--reviewers=reviewer@example.com']) |
| 715 | 717 |
| 716 def test_reviewer_multiple(self): | 718 def test_reviewer_multiple(self): |
| 717 # Handles multiple R= or TBR= lines. | 719 # Handles multiple R= or TBR= lines. |
| 718 description = ( | 720 description = ( |
| 719 'Foo Bar\nTBR=reviewer@example.com\nBUG=\nR=another@example.com') | 721 'Foo Bar\nTBR=reviewer@example.com\nBUG=\nR=another@example.com\n' |
| 722 'CC=more@example.com,people@example.com') |
| 720 self._run_reviewer_test( | 723 self._run_reviewer_test( |
| 721 [], | 724 [], |
| 722 'desc\n\nBUG=', | 725 'desc\n\nBUG=', |
| 723 description, | 726 description, |
| 724 description, | 727 description, |
| 725 ['--reviewers=another@example.com,reviewer@example.com']) | 728 ['--reviewers=another@example.com,reviewer@example.com'], |
| 729 cc=['more@example.com', 'people@example.com']) |
| 726 | 730 |
| 727 def test_reviewer_send_mail(self): | 731 def test_reviewer_send_mail(self): |
| 728 # --send-mail can be used without -r if R= is used | 732 # --send-mail can be used without -r if R= is used |
| 729 description = 'Foo Bar\nR=reviewer@example.com' | 733 description = 'Foo Bar\nR=reviewer@example.com' |
| 730 self._run_reviewer_test( | 734 self._run_reviewer_test( |
| 731 ['--send-mail'], | 735 ['--send-mail'], |
| 732 'desc\n\nBUG=', | 736 'desc\n\nBUG=', |
| 733 description.strip('\n'), | 737 description.strip('\n'), |
| 734 description, | 738 description, |
| 735 ['--reviewers=reviewer@example.com', '--send_mail']) | 739 ['--reviewers=reviewer@example.com', '--send_mail']) |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 841 'diff', '--no-ext-diff', '--stat', '--find-copies-harder', | 845 'diff', '--no-ext-diff', '--stat', '--find-copies-harder', |
| 842 '-l100000', '-C50', 'fake_ancestor_sha', 'HEAD'],), | 846 '-l100000', '-C50', 'fake_ancestor_sha', 'HEAD'],), |
| 843 '+dat'), | 847 '+dat'), |
| 844 ] | 848 ] |
| 845 | 849 |
| 846 @classmethod | 850 @classmethod |
| 847 def _gerrit_upload_calls(cls, description, reviewers, squash, | 851 def _gerrit_upload_calls(cls, description, reviewers, squash, |
| 848 squash_mode='default', | 852 squash_mode='default', |
| 849 expected_upstream_ref='origin/refs/heads/master', | 853 expected_upstream_ref='origin/refs/heads/master', |
| 850 ref_suffix='', notify=False, | 854 ref_suffix='', notify=False, |
| 851 post_amend_description=None, issue=None): | 855 post_amend_description=None, issue=None, cc=None): |
| 852 if post_amend_description is None: | 856 if post_amend_description is None: |
| 853 post_amend_description = description | 857 post_amend_description = description |
| 854 calls = [] | 858 calls = [] |
| 859 cc = cc or [] |
| 855 | 860 |
| 856 if squash_mode == 'default': | 861 if squash_mode == 'default': |
| 857 calls.extend([ | 862 calls.extend([ |
| 858 ((['git', 'config', '--bool', 'gerrit.override-squash-uploads'],), ''), | 863 ((['git', 'config', '--bool', 'gerrit.override-squash-uploads'],), ''), |
| 859 ((['git', 'config', '--bool', 'gerrit.squash-uploads'],), ''), | 864 ((['git', 'config', '--bool', 'gerrit.squash-uploads'],), ''), |
| 860 ]) | 865 ]) |
| 861 elif squash_mode in ('override_squash', 'override_nosquash'): | 866 elif squash_mode in ('override_squash', 'override_nosquash'): |
| 862 calls.extend([ | 867 calls.extend([ |
| 863 ((['git', 'config', '--bool', 'gerrit.override-squash-uploads'],), | 868 ((['git', 'config', '--bool', 'gerrit.override-squash-uploads'],), |
| 864 'true' if squash_mode == 'override_squash' else 'false'), | 869 'true' if squash_mode == 'override_squash' else 'false'), |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 949 ''), | 954 ''), |
| 950 ((['git', 'config', 'branch.master.gerritserver', | 955 ((['git', 'config', 'branch.master.gerritserver', |
| 951 'https://chromium-review.googlesource.com'],), ''), | 956 'https://chromium-review.googlesource.com'],), ''), |
| 952 ((['git', 'config', 'branch.master.gerritsquashhash', | 957 ((['git', 'config', 'branch.master.gerritsquashhash', |
| 953 'abcdef0123456789'],), ''), | 958 'abcdef0123456789'],), ''), |
| 954 ] | 959 ] |
| 955 calls += [ | 960 calls += [ |
| 956 ((['git', 'config', 'rietveld.cc'],), ''), | 961 ((['git', 'config', 'rietveld.cc'],), ''), |
| 957 ((['AddReviewers', 'chromium-review.googlesource.com', | 962 ((['AddReviewers', 'chromium-review.googlesource.com', |
| 958 123456 if squash else None, | 963 123456 if squash else None, |
| 959 ['joe@example.com'], False],), ''), | 964 ['joe@example.com'] + cc, False],), ''), |
| 960 ] | 965 ] |
| 961 calls += cls._git_post_upload_calls() | 966 calls += cls._git_post_upload_calls() |
| 962 return calls | 967 return calls |
| 963 | 968 |
| 964 def _run_gerrit_upload_test( | 969 def _run_gerrit_upload_test( |
| 965 self, | 970 self, |
| 966 upload_args, | 971 upload_args, |
| 967 description, | 972 description, |
| 968 reviewers=None, | 973 reviewers=None, |
| 969 squash=True, | 974 squash=True, |
| 970 squash_mode=None, | 975 squash_mode=None, |
| 971 expected_upstream_ref='origin/refs/heads/master', | 976 expected_upstream_ref='origin/refs/heads/master', |
| 972 ref_suffix='', | 977 ref_suffix='', |
| 973 notify=False, | 978 notify=False, |
| 974 post_amend_description=None, | 979 post_amend_description=None, |
| 975 issue=None): | 980 issue=None, |
| 981 cc=None): |
| 976 """Generic gerrit upload test framework.""" | 982 """Generic gerrit upload test framework.""" |
| 977 if squash_mode is None: | 983 if squash_mode is None: |
| 978 if '--no-squash' in upload_args: | 984 if '--no-squash' in upload_args: |
| 979 squash_mode = 'nosquash' | 985 squash_mode = 'nosquash' |
| 980 elif '--squash' in upload_args: | 986 elif '--squash' in upload_args: |
| 981 squash_mode = 'squash' | 987 squash_mode = 'squash' |
| 982 else: | 988 else: |
| 983 squash_mode = 'default' | 989 squash_mode = 'default' |
| 984 | 990 |
| 985 reviewers = reviewers or [] | 991 reviewers = reviewers or [] |
| 992 cc = cc or [] |
| 986 self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) | 993 self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
| 987 self.mock(git_cl.gerrit_util, 'CookiesAuthenticator', | 994 self.mock(git_cl.gerrit_util, 'CookiesAuthenticator', |
| 988 CookiesAuthenticatorMockFactory(same_cookie='same_cred')) | 995 CookiesAuthenticatorMockFactory(same_cookie='same_cred')) |
| 989 self.mock(git_cl._GerritChangelistImpl, '_GerritCommitMsgHookCheck', | 996 self.mock(git_cl._GerritChangelistImpl, '_GerritCommitMsgHookCheck', |
| 990 lambda _, offer_removal: None) | 997 lambda _, offer_removal: None) |
| 991 self.mock(git_cl.gclient_utils, 'RunEditor', | 998 self.mock(git_cl.gclient_utils, 'RunEditor', |
| 992 lambda *_, **__: self._mocked_call(['RunEditor'])) | 999 lambda *_, **__: self._mocked_call(['RunEditor'])) |
| 993 self.mock(git_cl, 'DownloadGerritHook', self._mocked_call) | 1000 self.mock(git_cl, 'DownloadGerritHook', self._mocked_call) |
| 994 self.mock(git_cl.gerrit_util, 'AddReviewers', | 1001 self.mock(git_cl.gerrit_util, 'AddReviewers', |
| 995 lambda h, i, add, is_reviewer: self._mocked_call( | 1002 lambda h, i, add, is_reviewer: self._mocked_call( |
| 996 ['AddReviewers', h, i, add, is_reviewer])) | 1003 ['AddReviewers', h, i, add, is_reviewer])) |
| 997 | 1004 |
| 998 self.calls = self._gerrit_base_calls(issue=issue) | 1005 self.calls = self._gerrit_base_calls(issue=issue) |
| 999 self.calls += self._gerrit_upload_calls( | 1006 self.calls += self._gerrit_upload_calls( |
| 1000 description, reviewers, squash, | 1007 description, reviewers, squash, |
| 1001 squash_mode=squash_mode, | 1008 squash_mode=squash_mode, |
| 1002 expected_upstream_ref=expected_upstream_ref, | 1009 expected_upstream_ref=expected_upstream_ref, |
| 1003 ref_suffix=ref_suffix, notify=notify, | 1010 ref_suffix=ref_suffix, notify=notify, |
| 1004 post_amend_description=post_amend_description, | 1011 post_amend_description=post_amend_description, |
| 1005 issue=issue) | 1012 issue=issue, cc=cc) |
| 1006 # Uncomment when debugging. | 1013 # Uncomment when debugging. |
| 1007 # print '\n'.join(map(lambda x: '%2i: %s' % x, enumerate(self.calls))) | 1014 # print '\n'.join(map(lambda x: '%2i: %s' % x, enumerate(self.calls))) |
| 1008 git_cl.main(['upload'] + upload_args) | 1015 git_cl.main(['upload'] + upload_args) |
| 1009 | 1016 |
| 1010 def test_gerrit_upload_without_change_id(self): | 1017 def test_gerrit_upload_without_change_id(self): |
| 1011 self._run_gerrit_upload_test( | 1018 self._run_gerrit_upload_test( |
| 1012 ['--no-squash'], | 1019 ['--no-squash'], |
| 1013 'desc\n\nBUG=\n', | 1020 'desc\n\nBUG=\n', |
| 1014 [], | 1021 [], |
| 1015 squash=False, | 1022 squash=False, |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1051 ['-r', 'foo@example.com', '--send-mail'], | 1058 ['-r', 'foo@example.com', '--send-mail'], |
| 1052 'desc\n\nBUG=\n\nChange-Id: I123456789', | 1059 'desc\n\nBUG=\n\nChange-Id: I123456789', |
| 1053 ['foo@example.com'], | 1060 ['foo@example.com'], |
| 1054 squash=False, | 1061 squash=False, |
| 1055 squash_mode='override_nosquash', | 1062 squash_mode='override_nosquash', |
| 1056 notify=True) | 1063 notify=True) |
| 1057 | 1064 |
| 1058 def test_gerrit_reviewer_multiple(self): | 1065 def test_gerrit_reviewer_multiple(self): |
| 1059 self._run_gerrit_upload_test( | 1066 self._run_gerrit_upload_test( |
| 1060 [], | 1067 [], |
| 1061 'desc\nTBR=reviewer@example.com\nBUG=\nR=another@example.com\n\n' | 1068 'desc\nTBR=reviewer@example.com\nBUG=\nR=another@example.com\n' |
| 1069 'CC=more@example.com,people@example.com\n\n' |
| 1062 'Change-Id: 123456789\n', | 1070 'Change-Id: 123456789\n', |
| 1063 ['reviewer@example.com', 'another@example.com'], | 1071 ['reviewer@example.com', 'another@example.com'], |
| 1064 squash=False, | 1072 squash=False, |
| 1065 squash_mode='override_nosquash', | 1073 squash_mode='override_nosquash', |
| 1066 ref_suffix='%l=Code-Review+1') | 1074 ref_suffix='%l=Code-Review+1', |
| 1075 cc=['more@example.com', 'people@example.com']) |
| 1067 | 1076 |
| 1068 def test_gerrit_upload_squash_first_is_default(self): | 1077 def test_gerrit_upload_squash_first_is_default(self): |
| 1069 # Mock Gerrit CL description to indicate the first upload. | 1078 # Mock Gerrit CL description to indicate the first upload. |
| 1070 self.mock(git_cl.Changelist, 'GetDescription', | 1079 self.mock(git_cl.Changelist, 'GetDescription', |
| 1071 lambda *_: None) | 1080 lambda *_: None) |
| 1072 self._run_gerrit_upload_test( | 1081 self._run_gerrit_upload_test( |
| 1073 [], | 1082 [], |
| 1074 'desc\nBUG=\n\nChange-Id: 123456789', | 1083 'desc\nBUG=\n\nChange-Id: 123456789', |
| 1075 [], | 1084 [], |
| 1076 expected_upstream_ref='origin/master') | 1085 expected_upstream_ref='origin/master') |
| (...skipping 1120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2197 self.assertNotRegexpMatches(sys.stdout.getvalue(), 'Warning') | 2206 self.assertNotRegexpMatches(sys.stdout.getvalue(), 'Warning') |
| 2198 self.assertRegexpMatches(sys.stdout.getvalue(), '^Failures:') | 2207 self.assertRegexpMatches(sys.stdout.getvalue(), '^Failures:') |
| 2199 self.assertRegexpMatches(sys.stdout.getvalue(), 'Started:') | 2208 self.assertRegexpMatches(sys.stdout.getvalue(), 'Started:') |
| 2200 self.assertRegexpMatches(sys.stdout.getvalue(), '2 try jobs') | 2209 self.assertRegexpMatches(sys.stdout.getvalue(), '2 try jobs') |
| 2201 | 2210 |
| 2202 | 2211 |
| 2203 if __name__ == '__main__': | 2212 if __name__ == '__main__': |
| 2204 git_cl.logging.basicConfig( | 2213 git_cl.logging.basicConfig( |
| 2205 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) | 2214 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) |
| 2206 unittest.main() | 2215 unittest.main() |
| OLD | NEW |