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 |
11 import sys | 11 import sys |
12 import unittest | 12 import unittest |
13 import re | |
14 | 13 |
15 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | 14 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
16 | 15 |
17 from testing_support.auto_stub import TestCase | 16 from testing_support.auto_stub import TestCase |
18 | 17 |
19 import git_cl | 18 import git_cl |
20 import git_common | 19 import git_common |
21 import subprocess2 | 20 import subprocess2 |
22 import presubmit_support | |
23 | 21 |
24 class PresubmitMock(object): | 22 class PresubmitMock(object): |
25 def __init__(self, *args, **kwargs): | 23 def __init__(self, *args, **kwargs): |
26 self.reviewers = [] | 24 self.reviewers = [] |
27 @staticmethod | 25 @staticmethod |
28 def should_continue(): | 26 def should_continue(): |
29 return True | 27 return True |
30 | 28 |
31 | 29 |
32 class RietveldMock(object): | 30 class RietveldMock(object): |
(...skipping 715 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
748 ('foo BUG=allo R=joe ', ['c@c'], 'foo BUG=allo R=joe\n\nR=c@c'), | 746 ('foo BUG=allo R=joe ', ['c@c'], 'foo BUG=allo R=joe\n\nR=c@c'), |
749 ] | 747 ] |
750 expected = [i[2] for i in data] | 748 expected = [i[2] for i in data] |
751 actual = [] | 749 actual = [] |
752 for orig, reviewers, _expected in data: | 750 for orig, reviewers, _expected in data: |
753 obj = git_cl.ChangeDescription(orig) | 751 obj = git_cl.ChangeDescription(orig) |
754 obj.update_reviewers(reviewers) | 752 obj.update_reviewers(reviewers) |
755 actual.append(obj.description) | 753 actual.append(obj.description) |
756 self.assertEqual(expected, actual) | 754 self.assertEqual(expected, actual) |
757 | 755 |
758 def test_trybots_from_PRESUBMIT(self): | |
759 TEST_MASTER = 'testMaster' | |
760 TEST_BUILDER = 'testBuilder' | |
761 MASTERS = {TEST_MASTER:{TEST_BUILDER:['a']}} | |
762 self.mock(presubmit_support, 'DoGetTryMasters', | |
763 lambda *args: MASTERS) | |
764 | |
765 change_mock = ChangeMock() | |
766 changelist_mock = ChangelistMock(change_mock) | |
767 self.mock(git_cl, 'is_dirty_git_tree', lambda x: False) | |
768 self.mock(git_cl, 'print_stats', lambda *arg: True) | |
769 self.mock(git_cl, 'Changelist', lambda *args: changelist_mock) | |
770 self.mock(git_cl, 'CreateDescriptionFromLog', lambda arg: 'Commit message') | |
771 self.mock(git_cl.ChangeDescription, 'prompt', lambda self: None) | |
772 | |
773 self.calls = [ | |
774 ((['git', 'config', 'rietveld.autoupdate',],), | |
775 ''), | |
776 ((['git', 'config', 'gerrit.host',],), | |
777 ''), | |
778 ((['git', 'rev-parse', '--show-cdup',],), | |
779 ''), | |
780 ((['git', 'config', 'rietveld.private',],), | |
781 ''), | |
782 ((['git', 'config', 'rietveld.pending-ref-prefix',],), | |
783 ''), | |
784 ((['git', 'config', '--local', '--get-regexp', '^svn-remote\\.'],), | |
785 ''), | |
786 ((['git', 'config', 'rietveld.project',],), | |
787 ''), | |
788 ((['git', 'rev-parse', 'HEAD',],), | |
789 ''), | |
790 ] | |
791 | |
792 stored_description = [] | |
793 def check_upload(args): | |
794 i = 0 | |
795 for arg in args: | |
796 if arg == '--message': | |
797 break | |
798 i += 1 | |
799 | |
800 self.assertTrue(i < len(args)) | |
801 stored_description.append(args[i+1]) | |
802 return 1, 2 | |
803 self.mock(git_cl.upload, 'RealMain', check_upload) | |
804 | |
805 git_cl.main(['upload', '--bypass-hooks', '--auto-bots']) | |
806 found = re.search("CQ_TRYBOTS=(.*?)$", stored_description[0]) | |
807 self.assertTrue(found) | |
808 self.assertEqual(found.group(1), '%s:%s' % (TEST_MASTER, TEST_BUILDER)) | |
809 | |
810 | |
811 class ChangelistMock(object): | |
812 # Disable "Method could be a function" | |
813 # pylint: disable=R0201 | |
814 | |
815 def __init__(self, change_mock): | |
816 self.change_mock = change_mock | |
817 | |
818 def GetChange(self, *args): | |
819 return self.change_mock | |
820 | |
821 def GetIssue(self): | |
822 return None | |
823 | |
824 def GetBranch(self): | |
825 return [] | |
826 | |
827 def GetCommonAncestorWithUpstream(self): | |
828 return [] | |
829 | |
830 def GetCCList(self): | |
831 return [] | |
832 | |
833 def GetGitBaseUrlFromConfig(self): | |
834 return '' | |
835 | |
836 def GetRemoteUrl(self): | |
837 return '' | |
838 | |
839 def GetRietveldServer(self): | |
840 return None | |
841 | |
842 def SetWatchers(self, *args): | |
843 pass | |
844 | |
845 def SetIssue(self, issue): | |
846 pass | |
847 | |
848 def SetPatchset(self, issue): | |
849 pass | |
850 | |
851 | |
852 class ChangeMock(object): | |
853 # Disable "Method could be a function" | |
854 # pylint: disable=R0201 | |
855 | |
856 def __init__(self): | |
857 self.stored_description = None | |
858 | |
859 def SetDescriptionText(self, desc): | |
860 self.stored_description = desc | |
861 | |
862 def FullDescriptionText(self): | |
863 return 'HIHI TEST DESCRIPTION' | |
864 | |
865 def RepositoryRoot(self): | |
866 return [] | |
867 | |
868 def AffectedFiles(self): | |
869 return [] | |
870 | |
871 def LocalPaths(self): | |
872 return None | |
873 | |
874 if __name__ == '__main__': | 756 if __name__ == '__main__': |
875 git_cl.logging.basicConfig( | 757 git_cl.logging.basicConfig( |
876 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) | 758 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) |
877 unittest.main() | 759 unittest.main() |
OLD | NEW |