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 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 self.mox.StubOutWithMock(presubmit.gcl, 'ReadFile') | 87 self.mox.StubOutWithMock(presubmit.gcl, 'ReadFile') |
88 | 88 |
89 def compareMembers(self, object, members): | 89 def compareMembers(self, object, members): |
90 """If you add a member, be sure to add the relevant test!""" | 90 """If you add a member, be sure to add the relevant test!""" |
91 # Skip over members starting with '_' since they are usually not meant to | 91 # Skip over members starting with '_' since they are usually not meant to |
92 # be for public use. | 92 # be for public use. |
93 actual_members = [x for x in sorted(dir(object)) | 93 actual_members = [x for x in sorted(dir(object)) |
94 if not x.startswith('_')] | 94 if not x.startswith('_')] |
95 self.assertEqual(actual_members, sorted(members)) | 95 self.assertEqual(actual_members, sorted(members)) |
96 | 96 |
| 97 def MakeBasicChange(self, name, description, root=None): |
| 98 ci = presubmit.gcl.ChangeInfo(name, 0, 0, description, None) |
| 99 if root is None: |
| 100 root = self.fake_root_dir |
| 101 return presubmit.GclChange(ci, root) |
| 102 |
97 | 103 |
98 class PresubmitUnittest(PresubmitTestsBase): | 104 class PresubmitUnittest(PresubmitTestsBase): |
99 """General presubmit_support.py tests (excluding InputApi and OutputApi).""" | 105 """General presubmit_support.py tests (excluding InputApi and OutputApi).""" |
100 def testMembersChanged(self): | 106 def testMembersChanged(self): |
101 self.mox.ReplayAll() | 107 self.mox.ReplayAll() |
102 members = [ | 108 members = [ |
103 'AffectedFile', 'DoPresubmitChecks', 'GclChange', 'InputApi', | 109 'AffectedFile', 'DoPresubmitChecks', 'GclChange', 'InputApi', |
104 'ListRelevantPresubmitFiles', 'Main', 'NotImplementedException', | 110 'ListRelevantPresubmitFiles', 'Main', 'NotImplementedException', |
105 'OutputApi', 'ParseFiles', 'PresubmitExecuter', | 111 'OutputApi', 'ParseFiles', 'PresubmitExecuter', |
106 'ScanSubDirs', 'SvnAffectedFile', | 112 'ScanSubDirs', 'SvnAffectedFile', |
(...skipping 405 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
512 'http://tracker.com/42\n\n')) | 518 'http://tracker.com/42\n\n')) |
513 | 519 |
514 | 520 |
515 class InputApiUnittest(PresubmitTestsBase): | 521 class InputApiUnittest(PresubmitTestsBase): |
516 """Tests presubmit.InputApi.""" | 522 """Tests presubmit.InputApi.""" |
517 def testMembersChanged(self): | 523 def testMembersChanged(self): |
518 self.mox.ReplayAll() | 524 self.mox.ReplayAll() |
519 members = [ | 525 members = [ |
520 'AbsoluteLocalPaths', 'AffectedFiles', 'AffectedTextFiles', | 526 'AbsoluteLocalPaths', 'AffectedFiles', 'AffectedTextFiles', |
521 'DepotToLocalPath', 'LocalPaths', 'LocalToDepotPath', | 527 'DepotToLocalPath', 'LocalPaths', 'LocalToDepotPath', |
522 'PresubmitLocalPath', 'RightHandSideLines', 'ServerPaths', | 528 'PresubmitLocalPath', 'RightHandSideLines', 'ReadFile', 'ServerPaths', |
523 'basename', 'cPickle', 'cStringIO', 'canned_checks', 'change', | 529 'basename', 'cPickle', 'cStringIO', 'canned_checks', 'change', |
524 'is_committing', 'marshal', 'os_path', 'pickle', 'platform', | 530 'is_committing', 'marshal', 'os_path', 'pickle', 'platform', |
525 're', 'subprocess', 'tempfile', 'traceback', 'unittest', 'urllib2', | 531 're', 'subprocess', 'tempfile', 'traceback', 'unittest', 'urllib2', |
526 'version', | 532 'version', |
527 ] | 533 ] |
528 # If this test fails, you should add the relevant test. | 534 # If this test fails, you should add the relevant test. |
529 self.compareMembers(presubmit.InputApi(None, './.', False), members) | 535 self.compareMembers(presubmit.InputApi(None, './.', False), members) |
530 | 536 |
531 def testDepotToLocalPath(self): | 537 def testDepotToLocalPath(self): |
532 presubmit.gclient.CaptureSVNInfo('svn://foo/smurf').AndReturn( | 538 presubmit.gclient.CaptureSVNInfo('svn://foo/smurf').AndReturn( |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
665 def testDeprecated(self): | 671 def testDeprecated(self): |
666 presubmit.warnings.warn(mox.IgnoreArg(), category=mox.IgnoreArg(), | 672 presubmit.warnings.warn(mox.IgnoreArg(), category=mox.IgnoreArg(), |
667 stacklevel=2) | 673 stacklevel=2) |
668 self.mox.ReplayAll() | 674 self.mox.ReplayAll() |
669 change = presubmit.GclChange( | 675 change = presubmit.GclChange( |
670 presubmit.gcl.ChangeInfo(name='mychange', issue=0, patchset=0, | 676 presubmit.gcl.ChangeInfo(name='mychange', issue=0, patchset=0, |
671 description='Bleh\n', files=None)) | 677 description='Bleh\n', files=None)) |
672 api = presubmit.InputApi(change, 'foo/PRESUBMIT.py', True) | 678 api = presubmit.InputApi(change, 'foo/PRESUBMIT.py', True) |
673 api.AffectedTextFiles(include_deletes=False) | 679 api.AffectedTextFiles(include_deletes=False) |
674 | 680 |
| 681 def testReadFileStringDenied(self): |
| 682 self.mox.ReplayAll() |
| 683 input_api = presubmit.InputApi(None, './p', False) |
| 684 input_api.change = self.MakeBasicChange('foo', 'Foo\n', '/AA') |
| 685 self.assertRaises(IOError, input_api.ReadFile, 'boo', 'x') |
| 686 |
| 687 def testReadFileStringAccepted(self): |
| 688 presubmit.gcl.ReadFile('/AA/boo', 'x').AndReturn(None) |
| 689 self.mox.ReplayAll() |
| 690 input_api = presubmit.InputApi(None, './p', False) |
| 691 input_api.change = self.MakeBasicChange('foo', 'Foo\n', '/AA') |
| 692 input_api.ReadFile('/AA/boo', 'x') |
| 693 |
| 694 def testReadFileAffectedFileDenied(self): |
| 695 file = presubmit.AffectedFile('boo', 'M') |
| 696 self.mox.ReplayAll() |
| 697 input_api = presubmit.InputApi(None, './p', False) |
| 698 input_api.change = self.MakeBasicChange('foo', 'Foo\n', '/AA') |
| 699 self.assertRaises(IOError, input_api.ReadFile, 'boo', 'x') |
| 700 |
| 701 def testReadFileAffectedFileAccepted(self): |
| 702 file = presubmit.AffectedFile('/AA/boo', 'M') |
| 703 presubmit.gcl.ReadFile('/AA/boo', 'x').AndReturn(None) |
| 704 self.mox.ReplayAll() |
| 705 input_api = presubmit.InputApi(None, './p', False) |
| 706 input_api.change = self.MakeBasicChange('foo', 'Foo\n', '/AA') |
| 707 input_api.ReadFile('/AA/boo', 'x') |
| 708 |
675 | 709 |
676 class OuputApiUnittest(PresubmitTestsBase): | 710 class OuputApiUnittest(PresubmitTestsBase): |
677 """Tests presubmit.OutputApi.""" | 711 """Tests presubmit.OutputApi.""" |
678 def testMembersChanged(self): | 712 def testMembersChanged(self): |
679 self.mox.ReplayAll() | 713 self.mox.ReplayAll() |
680 members = [ | 714 members = [ |
681 'MailTextResult', 'PresubmitError', 'PresubmitNotifyResult', | 715 'MailTextResult', 'PresubmitError', 'PresubmitNotifyResult', |
682 'PresubmitPromptWarning', 'PresubmitResult', | 716 'PresubmitPromptWarning', 'PresubmitResult', |
683 ] | 717 ] |
684 # If this test fails, you should add the relevant test. | 718 # If this test fails, you should add the relevant test. |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
829 '_RunPythonUnitTests_LoadTests') | 863 '_RunPythonUnitTests_LoadTests') |
830 | 864 |
831 def MockInputApi(self): | 865 def MockInputApi(self): |
832 input_api = self.mox.CreateMock(presubmit.InputApi) | 866 input_api = self.mox.CreateMock(presubmit.InputApi) |
833 input_api.re = presubmit.re | 867 input_api.re = presubmit.re |
834 input_api.traceback = presubmit.traceback | 868 input_api.traceback = presubmit.traceback |
835 input_api.urllib2 = self.mox.CreateMock(presubmit.urllib2) | 869 input_api.urllib2 = self.mox.CreateMock(presubmit.urllib2) |
836 input_api.unittest = unittest | 870 input_api.unittest = unittest |
837 return input_api | 871 return input_api |
838 | 872 |
839 def MakeBasicChange(self, name, description): | |
840 ci = presubmit.gcl.ChangeInfo(name, 0, 0, description, None) | |
841 return presubmit.GclChange(ci, self.fake_root_dir) | |
842 | |
843 def testMembersChanged(self): | 873 def testMembersChanged(self): |
844 self.mox.ReplayAll() | 874 self.mox.ReplayAll() |
845 members = [ | 875 members = [ |
846 'CheckChangeHasBugField', 'CheckChangeHasNoTabs', | 876 'CheckChangeHasBugField', 'CheckChangeHasNoCR', 'CheckChangeHasNoTabs', |
847 'CheckChangeHasQaField', 'CheckChangeHasTestedField', | 877 'CheckChangeHasQaField', 'CheckChangeHasTestedField', |
848 'CheckChangeHasTestField', 'CheckDoNotSubmit', | 878 'CheckChangeHasTestField', 'CheckDoNotSubmit', |
849 'CheckDoNotSubmitInDescription', 'CheckDoNotSubmitInFiles', | 879 'CheckDoNotSubmitInDescription', 'CheckDoNotSubmitInFiles', |
850 'CheckLongLines', 'CheckTreeIsOpen', 'RunPythonUnitTests', | 880 'CheckLongLines', 'CheckTreeIsOpen', 'RunPythonUnitTests', |
851 ] | 881 ] |
852 # If this test fails, you should add the relevant test. | 882 # If this test fails, you should add the relevant test. |
853 self.compareMembers(presubmit_canned_checks, members) | 883 self.compareMembers(presubmit_canned_checks, members) |
854 | 884 |
855 def TestDescription(self, check, description1, description2, error_type): | 885 def TestDescription(self, check, description1, description2, error_type): |
856 input_api1 = self.MockInputApi() | 886 input_api1 = self.MockInputApi() |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
915 def testCannedCheckDoNotSubmitInDescription(self): | 945 def testCannedCheckDoNotSubmitInDescription(self): |
916 self.TestDescription(presubmit_canned_checks.CheckDoNotSubmitInDescription, | 946 self.TestDescription(presubmit_canned_checks.CheckDoNotSubmitInDescription, |
917 'DO NOTSUBMIT', 'DO NOT ' + 'SUBMIT', | 947 'DO NOTSUBMIT', 'DO NOT ' + 'SUBMIT', |
918 presubmit.OutputApi.PresubmitError) | 948 presubmit.OutputApi.PresubmitError) |
919 | 949 |
920 def testCannedCheckDoNotSubmitInFiles(self): | 950 def testCannedCheckDoNotSubmitInFiles(self): |
921 self.TestContent(presubmit_canned_checks.CheckDoNotSubmitInFiles, | 951 self.TestContent(presubmit_canned_checks.CheckDoNotSubmitInFiles, |
922 'DO NOTSUBMIT', 'DO NOT ' + 'SUBMIT', | 952 'DO NOTSUBMIT', 'DO NOT ' + 'SUBMIT', |
923 presubmit.OutputApi.PresubmitError) | 953 presubmit.OutputApi.PresubmitError) |
924 | 954 |
| 955 def testCheckChangeHasNoCR(self): |
| 956 input_api1 = self.MockInputApi() |
| 957 self.mox.StubOutWithMock(input_api1, 'ReadFile') |
| 958 input_api1.change = self.MakeBasicChange('foo', 'Foo\n') |
| 959 affected_file1 = self.mox.CreateMock(presubmit.SvnAffectedFile) |
| 960 input_api1.AffectedTextFiles().AndReturn([affected_file1]) |
| 961 input_api1.ReadFile(affected_file1, 'rb').AndReturn("Hey!\nHo!\n") |
| 962 input_api2 = self.MockInputApi() |
| 963 self.mox.StubOutWithMock(input_api2, 'ReadFile') |
| 964 input_api2.change = self.MakeBasicChange('foo', 'Foo\n') |
| 965 affected_file2 = self.mox.CreateMock(presubmit.SvnAffectedFile) |
| 966 input_api2.AffectedTextFiles().AndReturn([affected_file2]) |
| 967 input_api2.ReadFile(affected_file2, 'rb').AndReturn("Hey!\r\nHo!\r\n") |
| 968 affected_file2.LocalPath().AndReturn('bar.cc') |
| 969 self.mox.ReplayAll() |
| 970 |
| 971 results = presubmit_canned_checks.CheckChangeHasNoCR( |
| 972 input_api1, presubmit.OutputApi) |
| 973 self.assertEquals(results, []) |
| 974 results2 = presubmit_canned_checks.CheckChangeHasNoCR( |
| 975 input_api2, presubmit.OutputApi) |
| 976 self.assertEquals(len(results2), 1) |
| 977 self.assertEquals(results2[0].__class__, |
| 978 presubmit.OutputApi.PresubmitPromptWarning) |
| 979 |
925 def testCannedCheckChangeHasNoTabs(self): | 980 def testCannedCheckChangeHasNoTabs(self): |
926 self.TestContent(presubmit_canned_checks.CheckChangeHasNoTabs, | 981 self.TestContent(presubmit_canned_checks.CheckChangeHasNoTabs, |
927 'blah blah', 'blah\tblah', | 982 'blah blah', 'blah\tblah', |
928 presubmit.OutputApi.PresubmitPromptWarning) | 983 presubmit.OutputApi.PresubmitPromptWarning) |
929 | 984 |
930 def testCannedCheckLongLines(self): | 985 def testCannedCheckLongLines(self): |
931 check = lambda x,y: presubmit_canned_checks.CheckLongLines(x, y, 10) | 986 check = lambda x,y: presubmit_canned_checks.CheckLongLines(x, y, 10) |
932 self.TestContent(check, '', 'blah blah blah', | 987 self.TestContent(check, '', 'blah blah blah', |
933 presubmit.OutputApi.PresubmitPromptWarning) | 988 presubmit.OutputApi.PresubmitPromptWarning) |
934 | 989 |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1080 test_result.errors = 0 | 1135 test_result.errors = 0 |
1081 self.mox.ReplayAll() | 1136 self.mox.ReplayAll() |
1082 | 1137 |
1083 results = presubmit_canned_checks.RunPythonUnitTests( | 1138 results = presubmit_canned_checks.RunPythonUnitTests( |
1084 input_api, presubmit.OutputApi, ['test_module']) | 1139 input_api, presubmit.OutputApi, ['test_module']) |
1085 self.assertEquals(len(results), 0) | 1140 self.assertEquals(len(results), 0) |
1086 | 1141 |
1087 | 1142 |
1088 if __name__ == '__main__': | 1143 if __name__ == '__main__': |
1089 unittest.main() | 1144 unittest.main() |
OLD | NEW |