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