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 presubmit_support.py and presubmit_canned_checks.py.""" | 6 """Unit tests for presubmit_support.py and presubmit_canned_checks.py.""" |
7 | 7 |
8 # pylint: disable=E1101,E1103 | 8 # pylint: disable=E1101,E1103 |
9 | 9 |
10 import functools | 10 import functools |
| 11 import itertools |
11 import logging | 12 import logging |
12 import os | 13 import os |
13 import StringIO | 14 import StringIO |
14 import sys | 15 import sys |
15 import time | 16 import time |
16 import unittest | 17 import unittest |
17 | 18 |
18 _ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | 19 _ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
19 sys.path.insert(0, _ROOT) | 20 sys.path.insert(0, _ROOT) |
20 | 21 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 """ | 54 """ |
54 | 55 |
55 presubmit_tryslave_project = """ | 56 presubmit_tryslave_project = """ |
56 def GetPreferredTrySlaves(project): | 57 def GetPreferredTrySlaves(project): |
57 if project == %s: | 58 if project == %s: |
58 return %s | 59 return %s |
59 else: | 60 else: |
60 return %s | 61 return %s |
61 """ | 62 """ |
62 | 63 |
| 64 presubmit_trymaster = """ |
| 65 def GetPreferredTryMasters(project, change): |
| 66 return %s |
| 67 """ |
| 68 |
63 presubmit_diffs = """ | 69 presubmit_diffs = """ |
64 --- file1 2011-02-09 10:38:16.517224845 -0800 | 70 --- file1 2011-02-09 10:38:16.517224845 -0800 |
65 +++ file2 2011-02-09 10:38:53.177226516 -0800 | 71 +++ file2 2011-02-09 10:38:53.177226516 -0800 |
66 @@ -1,6 +1,5 @@ | 72 @@ -1,6 +1,5 @@ |
67 this is line number 0 | 73 this is line number 0 |
68 this is line number 1 | 74 this is line number 1 |
69 -this is line number 2 to be deleted | 75 -this is line number 2 to be deleted |
70 this is line number 3 | 76 this is line number 3 |
71 this is line number 4 | 77 this is line number 4 |
72 this is line number 5 | 78 this is line number 5 |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 'GetTrySlavesExecuter', 'GitAffectedFile', 'CallCommand', 'CommandData', | 169 'GetTrySlavesExecuter', 'GitAffectedFile', 'CallCommand', 'CommandData', |
164 'GitChange', 'InputApi', 'ListRelevantPresubmitFiles', 'Main', | 170 'GitChange', 'InputApi', 'ListRelevantPresubmitFiles', 'Main', |
165 'NonexistantCannedCheckFilter', 'OutputApi', 'ParseFiles', | 171 'NonexistantCannedCheckFilter', 'OutputApi', 'ParseFiles', |
166 'PresubmitFailure', 'PresubmitExecuter', 'PresubmitOutput', 'ScanSubDirs', | 172 'PresubmitFailure', 'PresubmitExecuter', 'PresubmitOutput', 'ScanSubDirs', |
167 'SvnAffectedFile', 'SvnChange', 'cPickle', 'cpplint', 'cStringIO', | 173 'SvnAffectedFile', 'SvnChange', 'cPickle', 'cpplint', 'cStringIO', |
168 'contextlib', 'canned_check_filter', 'fix_encoding', 'fnmatch', | 174 'contextlib', 'canned_check_filter', 'fix_encoding', 'fnmatch', |
169 'gclient_utils', 'glob', 'inspect', 'json', 'load_files', 'logging', | 175 'gclient_utils', 'glob', 'inspect', 'json', 'load_files', 'logging', |
170 'marshal', 'normpath', 'optparse', 'os', 'owners', 'pickle', | 176 'marshal', 'normpath', 'optparse', 'os', 'owners', 'pickle', |
171 'presubmit_canned_checks', 'random', 're', 'rietveld', 'scm', | 177 'presubmit_canned_checks', 'random', 're', 'rietveld', 'scm', |
172 'subprocess', 'sys', 'tempfile', 'time', 'traceback', 'types', 'unittest', | 178 'subprocess', 'sys', 'tempfile', 'time', 'traceback', 'types', 'unittest', |
173 'urllib2', 'warn', 'multiprocessing', | 179 'urllib2', 'warn', 'multiprocessing', 'DoGetTryMasters', |
| 180 'GetTryMastersExecuter', 'itertools', |
174 ] | 181 ] |
175 # If this test fails, you should add the relevant test. | 182 # If this test fails, you should add the relevant test. |
176 self.compareMembers(presubmit, members) | 183 self.compareMembers(presubmit, members) |
177 | 184 |
178 def testCannedCheckFilter(self): | 185 def testCannedCheckFilter(self): |
179 canned = presubmit.presubmit_canned_checks | 186 canned = presubmit.presubmit_canned_checks |
180 orig = canned.CheckOwners | 187 orig = canned.CheckOwners |
181 with presubmit.canned_check_filter(['CheckOwners']): | 188 with presubmit.canned_check_filter(['CheckOwners']): |
182 self.assertNotEqual(canned.CheckOwners, orig) | 189 self.assertNotEqual(canned.CheckOwners, orig) |
183 self.assertEqual(canned.CheckOwners(None, None), []) | 190 self.assertEqual(canned.CheckOwners(None, None), []) |
(...skipping 836 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1020 script_text = ( | 1027 script_text = ( |
1021 'def GetPreferredTrySlaves():\n' | 1028 'def GetPreferredTrySlaves():\n' |
1022 ' return ["foo,bar"]\n') | 1029 ' return ["foo,bar"]\n') |
1023 try: | 1030 try: |
1024 presubmit.GetTrySlavesExecuter.ExecPresubmitScript( | 1031 presubmit.GetTrySlavesExecuter.ExecPresubmitScript( |
1025 script_text, 'path', 'project', None) | 1032 script_text, 'path', 'project', None) |
1026 self.fail() | 1033 self.fail() |
1027 except presubmit.PresubmitFailure: | 1034 except presubmit.PresubmitFailure: |
1028 pass | 1035 pass |
1029 | 1036 |
| 1037 def testGetTryMastersExecuter(self): |
| 1038 self.mox.ReplayAll() |
| 1039 change = presubmit.Change( |
| 1040 'foo', |
| 1041 'Blah Blah\n\nSTORY=http://tracker.com/42\nBUG=boo\n', |
| 1042 self.fake_root_dir, |
| 1043 None, |
| 1044 0, |
| 1045 0, |
| 1046 None) |
| 1047 executer = presubmit.GetTryMastersExecuter() |
| 1048 self.assertEqual({}, executer.ExecPresubmitScript('', '', '', change)) |
| 1049 self.assertEqual({}, |
| 1050 executer.ExecPresubmitScript('def foo():\n return\n', '', '', change)) |
| 1051 |
| 1052 # bad results |
| 1053 empty_master_result = "{'': {}}" |
| 1054 starts_with_space_result = "{' m': {}}" |
| 1055 empty_slave_result = "{'m': {'': set()}}" |
| 1056 not_dict_result1 = "['a']" |
| 1057 not_dict_result2 = "{'m': ''}" |
| 1058 not_dict_result3 = "{'m': []}" |
| 1059 not_set_result = "{'m': {'s': []}}" |
| 1060 empty_set_result = "{'m': {'s': set([])}}" |
| 1061 |
| 1062 for result in ( |
| 1063 empty_master_result, starts_with_space_result, empty_slave_result, |
| 1064 not_dict_result1, not_dict_result2, not_dict_result3, not_set_result, |
| 1065 empty_set_result): |
| 1066 self.assertRaises(presubmit.PresubmitFailure, |
| 1067 executer.ExecPresubmitScript, |
| 1068 self.presubmit_trymaster % result, '', '', change) |
| 1069 |
| 1070 # good results |
| 1071 expected_result = {'m1': {'s1': set(['t1', 't2'])}, |
| 1072 'm2': {'s1': set(['defaulttests']), |
| 1073 's2': set(['defaulttests'])}} |
| 1074 empty_result1 = {} |
| 1075 empty_result2 = {'m': {}} |
| 1076 space_in_name_result = {'m r': {'s\tv': set(['t1'])}} |
| 1077 for result in ( |
| 1078 expected_result, empty_result1, empty_result2, space_in_name_result): |
| 1079 self.assertEqual( |
| 1080 result, |
| 1081 executer.ExecPresubmitScript( |
| 1082 self.presubmit_trymaster % result, '', '', change)) |
| 1083 |
| 1084 def testMergeMasters(self): |
| 1085 merge = presubmit._MergeMasters |
| 1086 self.assertEqual({}, merge({}, {})) |
| 1087 self.assertEqual({'m1': {}}, merge({}, {'m1': {}})) |
| 1088 self.assertEqual({'m1': {}}, merge({'m1': {}}, {})) |
| 1089 parts = [ |
| 1090 {'try1.cr': {'win': set(['defaulttests'])}}, |
| 1091 {'try1.cr': {'linux1': set(['test1'])}, |
| 1092 'try2.cr': {'linux2': set(['defaulttests'])}}, |
| 1093 {'try1.cr': {'mac1': set(['defaulttests']), |
| 1094 'mac2': set(['test1', 'test2']), |
| 1095 'linux1': set(['defaulttests'])}}, |
| 1096 ] |
| 1097 expected = { |
| 1098 'try1.cr': {'win': set(['defaulttests']), |
| 1099 'linux1': set(['defaulttests', 'test1']), |
| 1100 'mac1': set(['defaulttests']), |
| 1101 'mac2': set(['test1', 'test2'])}, |
| 1102 'try2.cr': {'linux2': set(['defaulttests'])}, |
| 1103 } |
| 1104 for permutation in itertools.permutations(parts): |
| 1105 self.assertEqual(expected, reduce(merge, permutation, {})) |
| 1106 |
| 1107 def testDoGetTryMasters(self): |
| 1108 root_text = (self.presubmit_trymaster |
| 1109 % '{"t1.cr": {"win": set(["defaulttests"])}}') |
| 1110 linux_text = (self.presubmit_trymaster |
| 1111 % ('{"t1.cr": {"linux1": set(["t1"])},' |
| 1112 ' "t2.cr": {"linux2": set(["defaulttests"])}}')) |
| 1113 |
| 1114 join = presubmit.os.path.join |
| 1115 isfile = presubmit.os.path.isfile |
| 1116 FileRead = presubmit.gclient_utils.FileRead |
| 1117 filename = 'foo.cc' |
| 1118 filename_linux = join('linux_only', 'penguin.cc') |
| 1119 root_presubmit = join(self.fake_root_dir, 'PRESUBMIT.py') |
| 1120 linux_presubmit = join(self.fake_root_dir, 'linux_only', 'PRESUBMIT.py') |
| 1121 inherit_path = join(self.fake_root_dir, self._INHERIT_SETTINGS) |
| 1122 |
| 1123 isfile(inherit_path).AndReturn(False) |
| 1124 isfile(root_presubmit).AndReturn(True) |
| 1125 FileRead(root_presubmit, 'rU').AndReturn(root_text) |
| 1126 |
| 1127 isfile(inherit_path).AndReturn(False) |
| 1128 isfile(root_presubmit).AndReturn(True) |
| 1129 isfile(linux_presubmit).AndReturn(True) |
| 1130 FileRead(root_presubmit, 'rU').AndReturn(root_text) |
| 1131 FileRead(linux_presubmit, 'rU').AndReturn(linux_text) |
| 1132 self.mox.ReplayAll() |
| 1133 |
| 1134 change = presubmit.Change( |
| 1135 'mychange', '', self.fake_root_dir, [], 0, 0, None) |
| 1136 |
| 1137 output = StringIO.StringIO() |
| 1138 self.assertEqual({'t1.cr': {'win': ['defaulttests']}}, |
| 1139 presubmit.DoGetTryMasters(change, [filename], |
| 1140 self.fake_root_dir, |
| 1141 None, None, False, output)) |
| 1142 output = StringIO.StringIO() |
| 1143 expected = { |
| 1144 't1.cr': {'win': ['defaulttests'], 'linux1': ['t1']}, |
| 1145 't2.cr': {'linux2': ['defaulttests']}, |
| 1146 } |
| 1147 self.assertEqual(expected, |
| 1148 presubmit.DoGetTryMasters(change, |
| 1149 [filename, filename_linux], |
| 1150 self.fake_root_dir, None, None, |
| 1151 False, output)) |
| 1152 |
1030 def testMainUnversioned(self): | 1153 def testMainUnversioned(self): |
1031 # OptParser calls presubmit.os.path.exists and is a pain when mocked. | 1154 # OptParser calls presubmit.os.path.exists and is a pain when mocked. |
1032 self.UnMock(presubmit.os.path, 'exists') | 1155 self.UnMock(presubmit.os.path, 'exists') |
1033 self.mox.StubOutWithMock(presubmit, 'DoPresubmitChecks') | 1156 self.mox.StubOutWithMock(presubmit, 'DoPresubmitChecks') |
1034 self.mox.StubOutWithMock(presubmit, 'ParseFiles') | 1157 self.mox.StubOutWithMock(presubmit, 'ParseFiles') |
1035 presubmit.scm.determine_scm(self.fake_root_dir).AndReturn(None) | 1158 presubmit.scm.determine_scm(self.fake_root_dir).AndReturn(None) |
1036 presubmit.ParseFiles(['random_file.txt'], None | 1159 presubmit.ParseFiles(['random_file.txt'], None |
1037 ).AndReturn([('M', 'random_file.txt')]) | 1160 ).AndReturn([('M', 'random_file.txt')]) |
1038 output = self.mox.CreateMock(presubmit.PresubmitOutput) | 1161 output = self.mox.CreateMock(presubmit.PresubmitOutput) |
1039 output.should_continue().AndReturn(False) | 1162 output.should_continue().AndReturn(False) |
(...skipping 1703 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2743 owners_check=False) | 2866 owners_check=False) |
2744 self.assertEqual(1, len(results)) | 2867 self.assertEqual(1, len(results)) |
2745 self.assertEqual( | 2868 self.assertEqual( |
2746 'Found line ending with white spaces in:', results[0]._message) | 2869 'Found line ending with white spaces in:', results[0]._message) |
2747 self.checkstdout('') | 2870 self.checkstdout('') |
2748 | 2871 |
2749 | 2872 |
2750 if __name__ == '__main__': | 2873 if __name__ == '__main__': |
2751 import unittest | 2874 import unittest |
2752 unittest.main() | 2875 unittest.main() |
OLD | NEW |