OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2006-2009 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 import exceptions | 8 import exceptions |
9 import os | 9 import os |
10 import StringIO | 10 import StringIO |
(...skipping 980 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
991 | 991 |
992 def testMembersChanged(self): | 992 def testMembersChanged(self): |
993 self.mox.ReplayAll() | 993 self.mox.ReplayAll() |
994 members = [ | 994 members = [ |
995 'CheckChangeHasBugField', 'CheckChangeHasDescription', | 995 'CheckChangeHasBugField', 'CheckChangeHasDescription', |
996 'CheckChangeHasNoStrayWhitespace', | 996 'CheckChangeHasNoStrayWhitespace', |
997 'CheckChangeHasOnlyOneEol', 'CheckChangeHasNoCR', | 997 'CheckChangeHasOnlyOneEol', 'CheckChangeHasNoCR', |
998 'CheckChangeHasNoCrAndHasOnlyOneEol', 'CheckChangeHasNoTabs', | 998 'CheckChangeHasNoCrAndHasOnlyOneEol', 'CheckChangeHasNoTabs', |
999 'CheckChangeHasQaField', 'CheckChangeHasTestedField', | 999 'CheckChangeHasQaField', 'CheckChangeHasTestedField', |
1000 'CheckChangeHasTestField', 'CheckChangeSvnEolStyle', | 1000 'CheckChangeHasTestField', 'CheckChangeSvnEolStyle', |
| 1001 'CheckSvnForCommonMimeTypes', 'CheckSvnProperty', |
1001 'CheckDoNotSubmit', | 1002 'CheckDoNotSubmit', |
1002 'CheckDoNotSubmitInDescription', 'CheckDoNotSubmitInFiles', | 1003 'CheckDoNotSubmitInDescription', 'CheckDoNotSubmitInFiles', |
1003 'CheckLongLines', 'CheckTreeIsOpen', 'RunPythonUnitTests', | 1004 'CheckLongLines', 'CheckTreeIsOpen', 'RunPythonUnitTests', |
1004 ] | 1005 ] |
1005 # If this test fails, you should add the relevant test. | 1006 # If this test fails, you should add the relevant test. |
1006 self.compareMembers(presubmit_canned_checks, members) | 1007 self.compareMembers(presubmit_canned_checks, members) |
1007 | 1008 |
1008 def DescriptionTest(self, check, description1, description2, error_type, | 1009 def DescriptionTest(self, check, description1, description2, error_type, |
1009 committing): | 1010 committing): |
1010 change1 = presubmit.Change('foo1', description1, self.fake_root_dir, None, | 1011 change1 = presubmit.Change('foo1', description1, self.fake_root_dir, None, |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1067 affected_file2.LocalPath().AndReturn('bar.cc') | 1068 affected_file2.LocalPath().AndReturn('bar.cc') |
1068 self.mox.ReplayAll() | 1069 self.mox.ReplayAll() |
1069 | 1070 |
1070 results = check(input_api1, presubmit.OutputApi) | 1071 results = check(input_api1, presubmit.OutputApi) |
1071 self.assertEquals(results, []) | 1072 self.assertEquals(results, []) |
1072 results2 = check(input_api2, presubmit.OutputApi) | 1073 results2 = check(input_api2, presubmit.OutputApi) |
1073 self.assertEquals(len(results2), 1) | 1074 self.assertEquals(len(results2), 1) |
1074 self.assertEquals(results2[0].__class__, error_type) | 1075 self.assertEquals(results2[0].__class__, error_type) |
1075 | 1076 |
1076 def SvnPropertyTest(self, check, property, value1, value2, committing, | 1077 def SvnPropertyTest(self, check, property, value1, value2, committing, |
1077 error_type): | 1078 error_type, use_source_file): |
1078 input_api1 = self.MockInputApi(None, committing) | 1079 input_api1 = self.MockInputApi(None, committing) |
1079 files1 = [ | 1080 files1 = [ |
1080 presubmit.SvnAffectedFile('foo/bar.cc', 'A'), | 1081 presubmit.SvnAffectedFile('foo/bar.cc', 'A'), |
1081 presubmit.SvnAffectedFile('foo.cc', 'M'), | 1082 presubmit.SvnAffectedFile('foo.cc', 'M'), |
1082 ] | 1083 ] |
1083 input_api1.AffectedSourceFiles(None).AndReturn(files1) | 1084 if use_source_file: |
| 1085 input_api1.AffectedSourceFiles(None).AndReturn(files1) |
| 1086 else: |
| 1087 input_api1.AffectedFiles(include_deleted=False).AndReturn(files1) |
1084 presubmit.gcl.GetSVNFileProperty(presubmit.normpath('foo/bar.cc'), | 1088 presubmit.gcl.GetSVNFileProperty(presubmit.normpath('foo/bar.cc'), |
1085 property).AndReturn(value1) | 1089 property).AndReturn(value1) |
1086 presubmit.gcl.GetSVNFileProperty(presubmit.normpath('foo.cc'), | 1090 presubmit.gcl.GetSVNFileProperty(presubmit.normpath('foo.cc'), |
1087 property).AndReturn(value1) | 1091 property).AndReturn(value1) |
1088 input_api2 = self.MockInputApi(None, committing) | 1092 input_api2 = self.MockInputApi(None, committing) |
1089 files2 = [ | 1093 files2 = [ |
1090 presubmit.SvnAffectedFile('foo/bar.cc', 'A'), | 1094 presubmit.SvnAffectedFile('foo/bar.cc', 'A'), |
1091 presubmit.SvnAffectedFile('foo.cc', 'M'), | 1095 presubmit.SvnAffectedFile('foo.cc', 'M'), |
1092 ] | 1096 ] |
1093 input_api2.AffectedSourceFiles(None).AndReturn(files2) | 1097 if use_source_file: |
| 1098 input_api2.AffectedSourceFiles(None).AndReturn(files2) |
| 1099 else: |
| 1100 input_api2.AffectedFiles(include_deleted=False).AndReturn(files2) |
| 1101 |
1094 presubmit.gcl.GetSVNFileProperty(presubmit.normpath('foo/bar.cc'), | 1102 presubmit.gcl.GetSVNFileProperty(presubmit.normpath('foo/bar.cc'), |
1095 property).AndReturn(value2) | 1103 property).AndReturn(value2) |
1096 presubmit.gcl.GetSVNFileProperty(presubmit.normpath('foo.cc'), | 1104 presubmit.gcl.GetSVNFileProperty(presubmit.normpath('foo.cc'), |
1097 property).AndReturn(value2) | 1105 property).AndReturn(value2) |
1098 self.mox.ReplayAll() | 1106 self.mox.ReplayAll() |
1099 | 1107 |
1100 results1 = check(input_api1, presubmit.OutputApi, None) | 1108 results1 = check(input_api1, presubmit.OutputApi, None) |
1101 self.assertEquals(results1, []) | 1109 self.assertEquals(results1, []) |
1102 results2 = check(input_api2, presubmit.OutputApi, None) | 1110 results2 = check(input_api2, presubmit.OutputApi, None) |
1103 self.assertEquals(len(results2), 1) | 1111 self.assertEquals(len(results2), 1) |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1183 self.ContentTest(presubmit_canned_checks.CheckChangeHasNoTabs, | 1191 self.ContentTest(presubmit_canned_checks.CheckChangeHasNoTabs, |
1184 'blah blah', 'blah\tblah', | 1192 'blah blah', 'blah\tblah', |
1185 presubmit.OutputApi.PresubmitPromptWarning) | 1193 presubmit.OutputApi.PresubmitPromptWarning) |
1186 | 1194 |
1187 def testCannedCheckLongLines(self): | 1195 def testCannedCheckLongLines(self): |
1188 check = lambda x,y,z: presubmit_canned_checks.CheckLongLines(x, y, 10, z) | 1196 check = lambda x,y,z: presubmit_canned_checks.CheckLongLines(x, y, 10, z) |
1189 self.ContentTest(check, '', 'blah blah blah', | 1197 self.ContentTest(check, '', 'blah blah blah', |
1190 presubmit.OutputApi.PresubmitPromptWarning) | 1198 presubmit.OutputApi.PresubmitPromptWarning) |
1191 | 1199 |
1192 def testCheckChangeSvnEolStyleCommit(self): | 1200 def testCheckChangeSvnEolStyleCommit(self): |
| 1201 # Test CheckSvnProperty at the same time. |
1193 self.SvnPropertyTest(presubmit_canned_checks.CheckChangeSvnEolStyle, | 1202 self.SvnPropertyTest(presubmit_canned_checks.CheckChangeSvnEolStyle, |
1194 'svn:eol-style', 'LF', '', True, | 1203 'svn:eol-style', 'LF', '', True, |
1195 presubmit.OutputApi.PresubmitError) | 1204 presubmit.OutputApi.PresubmitError, True) |
1196 | 1205 |
1197 def testCheckChangeSvnEolStyleUpload(self): | 1206 def testCheckChangeSvnEolStyleUpload(self): |
1198 self.SvnPropertyTest(presubmit_canned_checks.CheckChangeSvnEolStyle, | 1207 self.SvnPropertyTest(presubmit_canned_checks.CheckChangeSvnEolStyle, |
1199 'svn:eol-style', 'LF', '', False, | 1208 'svn:eol-style', 'LF', '', False, |
1200 presubmit.OutputApi.PresubmitNotifyResult) | 1209 presubmit.OutputApi.PresubmitNotifyResult, True) |
| 1210 |
| 1211 def testCheckSvnForCommonMimeTypes(self): |
| 1212 self.mox.StubOutWithMock(presubmit_canned_checks, 'CheckSvnProperty') |
| 1213 input_api = self.MockInputApi(None, False) |
| 1214 output_api = presubmit.OutputApi() |
| 1215 input_api.AffectedFiles(include_deletes=False).AndReturn([ |
| 1216 'a.pdf', 'b.bmp', 'c.gif', 'd.png', 'e.jpg', 'f.ico']) |
| 1217 presubmit_canned_checks.CheckSvnProperty( |
| 1218 input_api, output_api, 'svn:mime-type', 'application/pdf', ['a.pdf'] |
| 1219 ).AndReturn([1]) |
| 1220 presubmit_canned_checks.CheckSvnProperty( |
| 1221 input_api, output_api, 'svn:mime-type', 'image/bmp', ['b.bmp'] |
| 1222 ).AndReturn([2]) |
| 1223 presubmit_canned_checks.CheckSvnProperty( |
| 1224 input_api, output_api, 'svn:mime-type', 'image/gif', ['c.gif'] |
| 1225 ).AndReturn([3]) |
| 1226 presubmit_canned_checks.CheckSvnProperty( |
| 1227 input_api, output_api, 'svn:mime-type', 'image/png', ['d.png'] |
| 1228 ).AndReturn([4]) |
| 1229 presubmit_canned_checks.CheckSvnProperty( |
| 1230 input_api, output_api, 'svn:mime-type', 'image/jpeg', ['e.jpg'] |
| 1231 ).AndReturn([5]) |
| 1232 presubmit_canned_checks.CheckSvnProperty( |
| 1233 input_api, output_api, 'svn:mime-type', 'image/vnd.microsoft.icon', |
| 1234 ['f.ico']).AndReturn([6]) |
| 1235 self.mox.ReplayAll() |
| 1236 |
| 1237 results = presubmit_canned_checks.CheckSvnForCommonMimeTypes( |
| 1238 input_api, output_api) |
| 1239 self.assertEquals(results, [1, 2, 3, 4, 5, 6]) |
1201 | 1240 |
1202 def testCannedCheckTreeIsOpenOpen(self): | 1241 def testCannedCheckTreeIsOpenOpen(self): |
1203 input_api = self.MockInputApi(None, True) | 1242 input_api = self.MockInputApi(None, True) |
1204 connection = self.mox.CreateMockAnything() | 1243 connection = self.mox.CreateMockAnything() |
1205 input_api.urllib2.urlopen('url_to_open').AndReturn(connection) | 1244 input_api.urllib2.urlopen('url_to_open').AndReturn(connection) |
1206 connection.read().AndReturn('1') | 1245 connection.read().AndReturn('1') |
1207 connection.close() | 1246 connection.close() |
1208 self.mox.ReplayAll() | 1247 self.mox.ReplayAll() |
1209 results = presubmit_canned_checks.CheckTreeIsOpen( | 1248 results = presubmit_canned_checks.CheckTreeIsOpen( |
1210 input_api, presubmit.OutputApi, url='url_to_open', closed='0') | 1249 input_api, presubmit.OutputApi, url='url_to_open', closed='0') |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1354 test_result.errors = 0 | 1393 test_result.errors = 0 |
1355 self.mox.ReplayAll() | 1394 self.mox.ReplayAll() |
1356 | 1395 |
1357 results = presubmit_canned_checks.RunPythonUnitTests( | 1396 results = presubmit_canned_checks.RunPythonUnitTests( |
1358 input_api, presubmit.OutputApi, ['test_module']) | 1397 input_api, presubmit.OutputApi, ['test_module']) |
1359 self.assertEquals(len(results), 0) | 1398 self.assertEquals(len(results), 0) |
1360 | 1399 |
1361 | 1400 |
1362 if __name__ == '__main__': | 1401 if __name__ == '__main__': |
1363 unittest.main() | 1402 unittest.main() |
OLD | NEW |