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 random | 9 import random |
10 import string | 10 import string |
(...skipping 919 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
930 input_api.cStringIO = presubmit.cStringIO | 930 input_api.cStringIO = presubmit.cStringIO |
931 input_api.re = presubmit.re | 931 input_api.re = presubmit.re |
932 input_api.traceback = presubmit.traceback | 932 input_api.traceback = presubmit.traceback |
933 input_api.urllib2 = self.mox.CreateMock(presubmit.urllib2) | 933 input_api.urllib2 = self.mox.CreateMock(presubmit.urllib2) |
934 input_api.unittest = unittest | 934 input_api.unittest = unittest |
935 return input_api | 935 return input_api |
936 | 936 |
937 def testMembersChanged(self): | 937 def testMembersChanged(self): |
938 self.mox.ReplayAll() | 938 self.mox.ReplayAll() |
939 members = [ | 939 members = [ |
940 'CheckChangeHasBugField', 'CheckChangeHasNoCR', 'CheckChangeHasNoTabs', | 940 'CheckChangeHasBugField', 'CheckChangeHasOnlyOneEol', |
| 941 'CheckChangeHasNoCR', 'CheckChangeHasNoCrAndHasOnlyOneEol', |
| 942 'CheckChangeHasNoTabs', |
941 'CheckChangeHasQaField', 'CheckChangeHasTestedField', | 943 'CheckChangeHasQaField', 'CheckChangeHasTestedField', |
942 'CheckChangeHasTestField', 'CheckChangeSvnEolStyle', | 944 'CheckChangeHasTestField', 'CheckChangeSvnEolStyle', |
943 'CheckDoNotSubmit', | 945 'CheckDoNotSubmit', |
944 'CheckDoNotSubmitInDescription', 'CheckDoNotSubmitInFiles', | 946 'CheckDoNotSubmitInDescription', 'CheckDoNotSubmitInFiles', |
945 'CheckLongLines', 'CheckTreeIsOpen', 'RunPythonUnitTests', | 947 'CheckLongLines', 'CheckTreeIsOpen', 'RunPythonUnitTests', |
946 ] | 948 ] |
947 # If this test fails, you should add the relevant test. | 949 # If this test fails, you should add the relevant test. |
948 self.compareMembers(presubmit_canned_checks, members) | 950 self.compareMembers(presubmit_canned_checks, members) |
949 | 951 |
950 def DescriptionTest(self, check, description1, description2, error_type): | 952 def DescriptionTest(self, check, description1, description2, error_type): |
(...skipping 29 matching lines...) Expand all Loading... |
980 ] | 982 ] |
981 input_api2.RightHandSideLines(mox.IgnoreArg()).AndReturn(output2) | 983 input_api2.RightHandSideLines(mox.IgnoreArg()).AndReturn(output2) |
982 self.mox.ReplayAll() | 984 self.mox.ReplayAll() |
983 | 985 |
984 results1 = check(input_api1, presubmit.OutputApi, None) | 986 results1 = check(input_api1, presubmit.OutputApi, None) |
985 self.assertEquals(results1, []) | 987 self.assertEquals(results1, []) |
986 results2 = check(input_api2, presubmit.OutputApi, None) | 988 results2 = check(input_api2, presubmit.OutputApi, None) |
987 self.assertEquals(len(results2), 1) | 989 self.assertEquals(len(results2), 1) |
988 self.assertEquals(results2[0].__class__, error_type) | 990 self.assertEquals(results2[0].__class__, error_type) |
989 | 991 |
| 992 def ReadFileTest(self, check, content1, content2, error_type): |
| 993 input_api1 = self.MockInputApi() |
| 994 self.mox.StubOutWithMock(input_api1, 'ReadFile') |
| 995 input_api1.change = self.MakeBasicChange('foo', 'Foo\n') |
| 996 affected_file1 = self.mox.CreateMock(presubmit.SvnAffectedFile) |
| 997 input_api1.AffectedSourceFiles(None).AndReturn([affected_file1]) |
| 998 input_api1.ReadFile(affected_file1, 'rb').AndReturn(content1) |
| 999 input_api2 = self.MockInputApi() |
| 1000 self.mox.StubOutWithMock(input_api2, 'ReadFile') |
| 1001 input_api2.change = self.MakeBasicChange('foo', 'Foo\n') |
| 1002 affected_file2 = self.mox.CreateMock(presubmit.SvnAffectedFile) |
| 1003 input_api2.AffectedSourceFiles(None).AndReturn([affected_file2]) |
| 1004 input_api2.ReadFile(affected_file2, 'rb').AndReturn(content2) |
| 1005 affected_file2.LocalPath().AndReturn('bar.cc') |
| 1006 self.mox.ReplayAll() |
| 1007 |
| 1008 results = check(input_api1, presubmit.OutputApi) |
| 1009 self.assertEquals(results, []) |
| 1010 results2 = check(input_api2, presubmit.OutputApi) |
| 1011 self.assertEquals(len(results2), 1) |
| 1012 self.assertEquals(results2[0].__class__, error_type) |
| 1013 |
990 def testCannedCheckChangeHasBugField(self): | 1014 def testCannedCheckChangeHasBugField(self): |
991 self.DescriptionTest(presubmit_canned_checks.CheckChangeHasBugField, | 1015 self.DescriptionTest(presubmit_canned_checks.CheckChangeHasBugField, |
992 'BUG=1234', '', | 1016 'BUG=1234', '', |
993 presubmit.OutputApi.PresubmitNotifyResult) | 1017 presubmit.OutputApi.PresubmitNotifyResult) |
994 | 1018 |
995 def testCannedCheckChangeHasTestField(self): | 1019 def testCannedCheckChangeHasTestField(self): |
996 self.DescriptionTest(presubmit_canned_checks.CheckChangeHasTestField, | 1020 self.DescriptionTest(presubmit_canned_checks.CheckChangeHasTestField, |
997 'TEST=did some stuff', '', | 1021 'TEST=did some stuff', '', |
998 presubmit.OutputApi.PresubmitNotifyResult) | 1022 presubmit.OutputApi.PresubmitNotifyResult) |
999 | 1023 |
(...skipping 11 matching lines...) Expand all Loading... |
1011 self.DescriptionTest(presubmit_canned_checks.CheckDoNotSubmitInDescription, | 1035 self.DescriptionTest(presubmit_canned_checks.CheckDoNotSubmitInDescription, |
1012 'DO NOTSUBMIT', 'DO NOT ' + 'SUBMIT', | 1036 'DO NOTSUBMIT', 'DO NOT ' + 'SUBMIT', |
1013 presubmit.OutputApi.PresubmitError) | 1037 presubmit.OutputApi.PresubmitError) |
1014 | 1038 |
1015 def testCannedCheckDoNotSubmitInFiles(self): | 1039 def testCannedCheckDoNotSubmitInFiles(self): |
1016 self.ContentTest( | 1040 self.ContentTest( |
1017 lambda x,y,z: presubmit_canned_checks.CheckDoNotSubmitInFiles(x, y), | 1041 lambda x,y,z: presubmit_canned_checks.CheckDoNotSubmitInFiles(x, y), |
1018 'DO NOTSUBMIT', 'DO NOT ' + 'SUBMIT', | 1042 'DO NOTSUBMIT', 'DO NOT ' + 'SUBMIT', |
1019 presubmit.OutputApi.PresubmitError) | 1043 presubmit.OutputApi.PresubmitError) |
1020 | 1044 |
| 1045 def testCheckChangeHasOnlyOneEol(self): |
| 1046 self.ReadFileTest(presubmit_canned_checks.CheckChangeHasOnlyOneEol, |
| 1047 "Hey!\nHo!\n", "Hey!\nHo!\n\n", |
| 1048 presubmit.OutputApi.PresubmitPromptWarning) |
| 1049 |
1021 def testCheckChangeHasNoCR(self): | 1050 def testCheckChangeHasNoCR(self): |
1022 input_api1 = self.MockInputApi() | 1051 self.ReadFileTest(presubmit_canned_checks.CheckChangeHasNoCR, |
1023 self.mox.StubOutWithMock(input_api1, 'ReadFile') | 1052 "Hey!\nHo!\n", "Hey!\r\nHo!\r\n", |
1024 input_api1.change = self.MakeBasicChange('foo', 'Foo\n') | 1053 presubmit.OutputApi.PresubmitPromptWarning) |
1025 affected_file1 = self.mox.CreateMock(presubmit.SvnAffectedFile) | 1054 |
1026 input_api1.AffectedSourceFiles(None).AndReturn([affected_file1]) | 1055 def testCheckChangeHasNoCrAndHasOnlyOneEol(self): |
1027 input_api1.ReadFile(affected_file1, 'rb').AndReturn("Hey!\nHo!\n") | 1056 self.ReadFileTest( |
1028 input_api2 = self.MockInputApi() | 1057 presubmit_canned_checks.CheckChangeHasNoCrAndHasOnlyOneEol, |
1029 self.mox.StubOutWithMock(input_api2, 'ReadFile') | 1058 "Hey!\nHo!\n", "Hey!\nHo!\n\n", |
1030 input_api2.change = self.MakeBasicChange('foo', 'Foo\n') | 1059 presubmit.OutputApi.PresubmitPromptWarning) |
1031 affected_file2 = self.mox.CreateMock(presubmit.SvnAffectedFile) | 1060 self.mox.VerifyAll() |
1032 input_api2.AffectedSourceFiles(None).AndReturn([affected_file2]) | 1061 self.ReadFileTest( |
1033 input_api2.ReadFile(affected_file2, 'rb').AndReturn("Hey!\r\nHo!\r\n") | 1062 presubmit_canned_checks.CheckChangeHasNoCrAndHasOnlyOneEol, |
1034 affected_file2.LocalPath().AndReturn('bar.cc') | 1063 "Hey!\nHo!\n", "Hey!\r\nHo!\r\n", |
1035 self.mox.ReplayAll() | 1064 presubmit.OutputApi.PresubmitPromptWarning) |
1036 | 1065 |
1037 results = presubmit_canned_checks.CheckChangeHasNoCR( | |
1038 input_api1, presubmit.OutputApi, None) | |
1039 self.assertEquals(results, []) | |
1040 results2 = presubmit_canned_checks.CheckChangeHasNoCR( | |
1041 input_api2, presubmit.OutputApi, None) | |
1042 self.assertEquals(len(results2), 1) | |
1043 self.assertEquals(results2[0].__class__, | |
1044 presubmit.OutputApi.PresubmitPromptWarning) | |
1045 | |
1046 def testCannedCheckChangeHasNoTabs(self): | 1066 def testCannedCheckChangeHasNoTabs(self): |
1047 self.ContentTest(presubmit_canned_checks.CheckChangeHasNoTabs, | 1067 self.ContentTest(presubmit_canned_checks.CheckChangeHasNoTabs, |
1048 'blah blah', 'blah\tblah', | 1068 'blah blah', 'blah\tblah', |
1049 presubmit.OutputApi.PresubmitPromptWarning) | 1069 presubmit.OutputApi.PresubmitPromptWarning) |
1050 | 1070 |
1051 def testCannedCheckLongLines(self): | 1071 def testCannedCheckLongLines(self): |
1052 check = lambda x,y,z: presubmit_canned_checks.CheckLongLines(x, y, 10, z) | 1072 check = lambda x,y,z: presubmit_canned_checks.CheckLongLines(x, y, 10, z) |
1053 self.ContentTest(check, '', 'blah blah blah', | 1073 self.ContentTest(check, '', 'blah blah blah', |
1054 presubmit.OutputApi.PresubmitPromptWarning) | 1074 presubmit.OutputApi.PresubmitPromptWarning) |
1055 | 1075 |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1249 test_result.errors = 0 | 1269 test_result.errors = 0 |
1250 self.mox.ReplayAll() | 1270 self.mox.ReplayAll() |
1251 | 1271 |
1252 results = presubmit_canned_checks.RunPythonUnitTests( | 1272 results = presubmit_canned_checks.RunPythonUnitTests( |
1253 input_api, presubmit.OutputApi, ['test_module']) | 1273 input_api, presubmit.OutputApi, ['test_module']) |
1254 self.assertEquals(len(results), 0) | 1274 self.assertEquals(len(results), 0) |
1255 | 1275 |
1256 | 1276 |
1257 if __name__ == '__main__': | 1277 if __name__ == '__main__': |
1258 unittest.main() | 1278 unittest.main() |
OLD | NEW |