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 StringIO | 10 import StringIO |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 if not input_api.change.NOSUCHKEY: | 42 if not input_api.change.NOSUCHKEY: |
43 return [output_api.PresubmitError("!!")] | 43 return [output_api.PresubmitError("!!")] |
44 elif not input_api.change.REALLYNOSUCHKEY: | 44 elif not input_api.change.REALLYNOSUCHKEY: |
45 return [output_api.PresubmitPromptWarning("??")] | 45 return [output_api.PresubmitPromptWarning("??")] |
46 elif not input_api.change.REALLYABSOLUTELYNOSUCHKEY: | 46 elif not input_api.change.REALLYABSOLUTELYNOSUCHKEY: |
47 return [output_api.PresubmitPromptWarning("??"), | 47 return [output_api.PresubmitPromptWarning("??"), |
48 output_api.PresubmitError("XX!!XX")] | 48 output_api.PresubmitError("XX!!XX")] |
49 else: | 49 else: |
50 return () | 50 return () |
51 """ | 51 """ |
52 presubmit_tryslave = """ | |
53 def GetPreferredTrySlaves(): | |
54 return %s | |
55 """ | |
56 | |
57 presubmit_tryslave_project = """ | |
58 def GetPreferredTrySlaves(project): | |
59 if project == %s: | |
60 return %s | |
61 else: | |
62 return %s | |
63 """ | |
64 | 52 |
65 presubmit_trymaster = """ | 53 presubmit_trymaster = """ |
66 def GetPreferredTryMasters(project, change): | 54 def GetPreferredTryMasters(project, change): |
67 return %s | 55 return %s |
68 """ | 56 """ |
69 | 57 |
70 presubmit_diffs = """ | 58 presubmit_diffs = """ |
71 --- file1 2011-02-09 10:38:16.517224845 -0800 | 59 --- file1 2011-02-09 10:38:16.517224845 -0800 |
72 +++ file2 2011-02-09 10:38:53.177226516 -0800 | 60 +++ file2 2011-02-09 10:38:53.177226516 -0800 |
73 @@ -1,6 +1,5 @@ | 61 @@ -1,6 +1,5 @@ |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 | 151 |
164 | 152 |
165 class PresubmitUnittest(PresubmitTestsBase): | 153 class PresubmitUnittest(PresubmitTestsBase): |
166 """General presubmit_support.py tests (excluding InputApi and OutputApi).""" | 154 """General presubmit_support.py tests (excluding InputApi and OutputApi).""" |
167 | 155 |
168 _INHERIT_SETTINGS = 'inherit-review-settings-ok' | 156 _INHERIT_SETTINGS = 'inherit-review-settings-ok' |
169 | 157 |
170 def testMembersChanged(self): | 158 def testMembersChanged(self): |
171 self.mox.ReplayAll() | 159 self.mox.ReplayAll() |
172 members = [ | 160 members = [ |
173 'AffectedFile', 'Change', 'DoGetTrySlaves', | 161 'AffectedFile', 'Change', |
174 'DoPostUploadExecuter', 'DoPresubmitChecks', 'GetPostUploadExecuter', | 162 'DoPostUploadExecuter', 'DoPresubmitChecks', 'GetPostUploadExecuter', |
175 'GetTrySlavesExecuter', 'GitAffectedFile', 'CallCommand', 'CommandData', | 163 'GitAffectedFile', 'CallCommand', 'CommandData', |
176 'GitChange', 'InputApi', 'ListRelevantPresubmitFiles', 'main', | 164 'GitChange', 'InputApi', 'ListRelevantPresubmitFiles', 'main', |
177 'NonexistantCannedCheckFilter', 'OutputApi', 'ParseFiles', | 165 'NonexistantCannedCheckFilter', 'OutputApi', 'ParseFiles', |
178 'PresubmitFailure', 'PresubmitExecuter', 'PresubmitOutput', 'ScanSubDirs', | 166 'PresubmitFailure', 'PresubmitExecuter', 'PresubmitOutput', 'ScanSubDirs', |
179 'SvnAffectedFile', 'SvnChange', 'auth', 'cPickle', 'cpplint', 'cStringIO', | 167 'SvnAffectedFile', 'SvnChange', 'auth', 'cPickle', 'cpplint', 'cStringIO', |
180 'contextlib', 'canned_check_filter', 'fix_encoding', 'fnmatch', | 168 'contextlib', 'canned_check_filter', 'fix_encoding', 'fnmatch', |
181 'gclient_utils', 'glob', 'inspect', 'json', 'load_files', 'logging', | 169 'gclient_utils', 'glob', 'inspect', 'json', 'load_files', 'logging', |
182 'marshal', 'normpath', 'optparse', 'os', 'owners', 'pickle', | 170 'marshal', 'normpath', 'optparse', 'os', 'owners', 'pickle', |
183 'presubmit_canned_checks', 'random', 're', 'rietveld', 'scm', | 171 'presubmit_canned_checks', 'random', 're', 'rietveld', 'scm', |
184 'subprocess', 'sys', 'tempfile', 'time', 'traceback', 'types', 'unittest', | 172 'subprocess', 'sys', 'tempfile', 'time', 'traceback', 'types', 'unittest', |
185 'urllib2', 'warn', 'multiprocessing', 'DoGetTryMasters', | 173 'urllib2', 'warn', 'multiprocessing', 'DoGetTryMasters', |
(...skipping 770 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
956 self.assertEquals(output.getvalue(), | 944 self.assertEquals(output.getvalue(), |
957 ('Running presubmit upload checks ...\n' | 945 ('Running presubmit upload checks ...\n' |
958 'Warning, no PRESUBMIT.py found.\n' | 946 'Warning, no PRESUBMIT.py found.\n' |
959 'Running default presubmit script.\n' | 947 'Running default presubmit script.\n' |
960 '\n' | 948 '\n' |
961 '** Presubmit Messages **\n' | 949 '** Presubmit Messages **\n' |
962 'http://tracker.com/42\n' | 950 'http://tracker.com/42\n' |
963 '\n' | 951 '\n' |
964 'Presubmit checks passed.\n')) | 952 'Presubmit checks passed.\n')) |
965 | 953 |
966 def testGetTrySlavesExecuter(self): | |
967 self.mox.ReplayAll() | |
968 change = presubmit.Change( | |
969 'foo', | |
970 'Blah Blah\n\nSTORY=http://tracker.com/42\nBUG=boo\n', | |
971 self.fake_root_dir, | |
972 None, | |
973 0, | |
974 0, | |
975 None) | |
976 executer = presubmit.GetTrySlavesExecuter() | |
977 self.assertEqual([], executer.ExecPresubmitScript('', '', '', change)) | |
978 self.assertEqual([], | |
979 executer.ExecPresubmitScript('def foo():\n return\n', '', '', change)) | |
980 | |
981 # bad results | |
982 starts_with_space_result = [' starts_with_space'] | |
983 not_list_result1 = "'foo'" | |
984 not_list_result2 = "('a', 'tuple')" | |
985 mixed_old_and_new = ['bot', ('bot2', set(['test']))] | |
986 not_set = [('bot2', ['test'])] | |
987 for result in ( | |
988 starts_with_space_result, not_list_result1, not_list_result2, | |
989 mixed_old_and_new, not_set): | |
990 self.assertRaises(presubmit.PresubmitFailure, | |
991 executer.ExecPresubmitScript, | |
992 self.presubmit_tryslave % result, '', '', change) | |
993 | |
994 # good results | |
995 expected_result = ['1', '2', '3'] | |
996 empty_result = [] | |
997 space_in_name_result = ['foo bar', '1\t2 3'] | |
998 new_style = [('bot', set(['cool', 'tests']))] | |
999 for result in ( | |
1000 expected_result, empty_result, space_in_name_result, new_style): | |
1001 self.assertEqual( | |
1002 result, | |
1003 executer.ExecPresubmitScript( | |
1004 self.presubmit_tryslave % result, '', '', change)) | |
1005 | |
1006 def testGetTrySlavesExecuterWithProject(self): | |
1007 self.mox.ReplayAll() | |
1008 | |
1009 change = presubmit.Change( | |
1010 'foo', | |
1011 'Blah Blah\n\nSTORY=http://tracker.com/42\nBUG=boo\n', | |
1012 self.fake_root_dir, | |
1013 None, | |
1014 0, | |
1015 0, | |
1016 None) | |
1017 | |
1018 executer = presubmit.GetTrySlavesExecuter() | |
1019 expected_result1 = ['1', '2'] | |
1020 expected_result2 = ['a', 'b', 'c'] | |
1021 script = self.presubmit_tryslave_project % ( | |
1022 repr('foo'), repr(expected_result1), repr(expected_result2)) | |
1023 self.assertEqual( | |
1024 expected_result1, executer.ExecPresubmitScript(script, '', 'foo', | |
1025 change)) | |
1026 self.assertEqual( | |
1027 expected_result2, executer.ExecPresubmitScript(script, '', 'bar', | |
1028 change)) | |
1029 | |
1030 def testDoGetTrySlaves(self): | |
1031 join = presubmit.os.path.join | |
1032 filename = 'foo.cc' | |
1033 filename_linux = join('linux_only', 'penguin.cc') | |
1034 root_presubmit = join(self.fake_root_dir, 'PRESUBMIT.py') | |
1035 linux_presubmit = join(self.fake_root_dir, 'linux_only', 'PRESUBMIT.py') | |
1036 inherit_path = presubmit.os.path.join(self.fake_root_dir, | |
1037 self._INHERIT_SETTINGS) | |
1038 | |
1039 presubmit.os.path.isfile(inherit_path).AndReturn(False) | |
1040 presubmit.os.listdir(self.fake_root_dir).AndReturn(['PRESUBMIT.py']) | |
1041 presubmit.os.path.isfile(root_presubmit).AndReturn(True) | |
1042 presubmit.gclient_utils.FileRead(root_presubmit, 'rU').AndReturn( | |
1043 self.presubmit_tryslave % '["win"]') | |
1044 | |
1045 presubmit.os.path.isfile(inherit_path).AndReturn(False) | |
1046 presubmit.os.listdir(self.fake_root_dir).AndReturn(['PRESUBMIT.py']) | |
1047 presubmit.os.path.isfile(root_presubmit).AndReturn(True) | |
1048 presubmit.os.listdir(join(self.fake_root_dir, 'linux_only')).AndReturn( | |
1049 ['PRESUBMIT.py']) | |
1050 presubmit.os.path.isfile(linux_presubmit).AndReturn(True) | |
1051 presubmit.gclient_utils.FileRead(root_presubmit, 'rU').AndReturn( | |
1052 self.presubmit_tryslave % '["win"]') | |
1053 presubmit.gclient_utils.FileRead(linux_presubmit, 'rU').AndReturn( | |
1054 self.presubmit_tryslave % '["linux"]') | |
1055 self.mox.ReplayAll() | |
1056 | |
1057 change = presubmit.Change( | |
1058 'mychange', '', self.fake_root_dir, [], 0, 0, None) | |
1059 | |
1060 output = StringIO.StringIO() | |
1061 self.assertEqual(['win'], | |
1062 presubmit.DoGetTrySlaves(change, [filename], | |
1063 self.fake_root_dir, | |
1064 None, None, False, output)) | |
1065 output = StringIO.StringIO() | |
1066 self.assertEqual(['win', 'linux'], | |
1067 presubmit.DoGetTrySlaves(change, | |
1068 [filename, filename_linux], | |
1069 self.fake_root_dir, None, None, | |
1070 False, output)) | |
1071 | |
1072 def testGetTrySlavesExecuter_ok(self): | |
1073 script_text = ( | |
1074 'def GetPreferredTrySlaves():\n' | |
1075 ' return ["foo", "bar"]\n') | |
1076 results = presubmit.GetTrySlavesExecuter.ExecPresubmitScript( | |
1077 script_text, 'path', 'project', None) | |
1078 self.assertEquals(['foo', 'bar'], results) | |
1079 | |
1080 def testGetTrySlavesExecuter_comma(self): | |
1081 script_text = ( | |
1082 'def GetPreferredTrySlaves():\n' | |
1083 ' return ["foo,bar"]\n') | |
1084 try: | |
1085 presubmit.GetTrySlavesExecuter.ExecPresubmitScript( | |
1086 script_text, 'path', 'project', None) | |
1087 self.fail() | |
1088 except presubmit.PresubmitFailure: | |
1089 pass | |
1090 | |
1091 def testGetTryMastersExecuter(self): | 954 def testGetTryMastersExecuter(self): |
1092 self.mox.ReplayAll() | 955 self.mox.ReplayAll() |
1093 change = presubmit.Change( | 956 change = presubmit.Change( |
1094 'foo', | 957 'foo', |
1095 'Blah Blah\n\nSTORY=http://tracker.com/42\nBUG=boo\n', | 958 'Blah Blah\n\nSTORY=http://tracker.com/42\nBUG=boo\n', |
1096 self.fake_root_dir, | 959 self.fake_root_dir, |
1097 None, | 960 None, |
1098 0, | 961 0, |
1099 0, | 962 0, |
1100 None) | 963 None) |
(...skipping 1997 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3098 owners_check=False) | 2961 owners_check=False) |
3099 self.assertEqual(2, len(results)) | 2962 self.assertEqual(2, len(results)) |
3100 self.assertEqual( | 2963 self.assertEqual( |
3101 'Found line ending with white spaces in:', results[0]._message) | 2964 'Found line ending with white spaces in:', results[0]._message) |
3102 self.checkstdout('') | 2965 self.checkstdout('') |
3103 | 2966 |
3104 | 2967 |
3105 if __name__ == '__main__': | 2968 if __name__ == '__main__': |
3106 import unittest | 2969 import unittest |
3107 unittest.main() | 2970 unittest.main() |
OLD | NEW |