Chromium Code Reviews| 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 | |
|
Michael Achenbach
2014/03/01 01:30:40
After removing the checks, there are no bad result
| |
| 1052 expected_result = {'m1': {'s1': set(['t1', 't2'])}, | |
| 1053 'm2': {'s1': set(['defaulttests']), | |
| 1054 's2': set(['defaulttests'])}} | |
| 1055 empty_result1 = {} | |
| 1056 empty_result2 = {'m': {}} | |
| 1057 space_in_name_result = {'m r': {'s\tv': set(['t1'])}} | |
| 1058 for result in ( | |
| 1059 expected_result, empty_result1, empty_result2, space_in_name_result): | |
| 1060 self.assertEqual( | |
| 1061 result, | |
| 1062 executer.ExecPresubmitScript( | |
| 1063 self.presubmit_trymaster % result, '', '', change)) | |
| 1064 | |
| 1065 def testMergeMasters(self): | |
| 1066 merge = presubmit._MergeMasters | |
| 1067 self.assertEqual({}, merge({}, {})) | |
| 1068 self.assertEqual({'m1': {}}, merge({}, {'m1': {}})) | |
| 1069 self.assertEqual({'m1': {}}, merge({'m1': {}}, {})) | |
| 1070 parts = [ | |
| 1071 {'try1.cr': {'win': set(['defaulttests'])}}, | |
| 1072 {'try1.cr': {'linux1': set(['test1'])}, | |
| 1073 'try2.cr': {'linux2': set(['defaulttests'])}}, | |
| 1074 {'try1.cr': {'mac1': set(['defaulttests']), | |
| 1075 'mac2': set(['test1', 'test2']), | |
| 1076 'linux1': set(['defaulttests'])}}, | |
| 1077 ] | |
| 1078 expected = { | |
| 1079 'try1.cr': {'win': set(['defaulttests']), | |
| 1080 'linux1': set(['defaulttests', 'test1']), | |
| 1081 'mac1': set(['defaulttests']), | |
| 1082 'mac2': set(['test1', 'test2'])}, | |
| 1083 'try2.cr': {'linux2': set(['defaulttests'])}, | |
| 1084 } | |
| 1085 for permutation in itertools.permutations(parts): | |
| 1086 self.assertEqual(expected, reduce(merge, permutation, {})) | |
| 1087 | |
| 1088 def testDoGetTryMasters(self): | |
| 1089 root_text = (self.presubmit_trymaster | |
| 1090 % '{"t1.cr": {"win": set(["defaulttests"])}}') | |
| 1091 linux_text = (self.presubmit_trymaster | |
| 1092 % ('{"t1.cr": {"linux1": set(["t1"])},' | |
| 1093 ' "t2.cr": {"linux2": set(["defaulttests"])}}')) | |
| 1094 | |
| 1095 join = presubmit.os.path.join | |
| 1096 isfile = presubmit.os.path.isfile | |
| 1097 FileRead = presubmit.gclient_utils.FileRead | |
| 1098 filename = 'foo.cc' | |
| 1099 filename_linux = join('linux_only', 'penguin.cc') | |
| 1100 root_presubmit = join(self.fake_root_dir, 'PRESUBMIT.py') | |
| 1101 linux_presubmit = join(self.fake_root_dir, 'linux_only', 'PRESUBMIT.py') | |
| 1102 inherit_path = join(self.fake_root_dir, self._INHERIT_SETTINGS) | |
| 1103 | |
| 1104 isfile(inherit_path).AndReturn(False) | |
| 1105 isfile(root_presubmit).AndReturn(True) | |
| 1106 FileRead(root_presubmit, 'rU').AndReturn(root_text) | |
| 1107 | |
| 1108 isfile(inherit_path).AndReturn(False) | |
| 1109 isfile(root_presubmit).AndReturn(True) | |
| 1110 isfile(linux_presubmit).AndReturn(True) | |
| 1111 FileRead(root_presubmit, 'rU').AndReturn(root_text) | |
| 1112 FileRead(linux_presubmit, 'rU').AndReturn(linux_text) | |
| 1113 self.mox.ReplayAll() | |
| 1114 | |
| 1115 change = presubmit.Change( | |
| 1116 'mychange', '', self.fake_root_dir, [], 0, 0, None) | |
| 1117 | |
| 1118 output = StringIO.StringIO() | |
| 1119 self.assertEqual({'t1.cr': {'win': ['defaulttests']}}, | |
| 1120 presubmit.DoGetTryMasters(change, [filename], | |
| 1121 self.fake_root_dir, | |
| 1122 None, None, False, output)) | |
| 1123 output = StringIO.StringIO() | |
| 1124 expected = { | |
| 1125 't1.cr': {'win': ['defaulttests'], 'linux1': ['t1']}, | |
| 1126 't2.cr': {'linux2': ['defaulttests']}, | |
| 1127 } | |
| 1128 self.assertEqual(expected, | |
| 1129 presubmit.DoGetTryMasters(change, | |
| 1130 [filename, filename_linux], | |
| 1131 self.fake_root_dir, None, None, | |
| 1132 False, output)) | |
| 1133 | |
| 1030 def testMainUnversioned(self): | 1134 def testMainUnversioned(self): |
| 1031 # OptParser calls presubmit.os.path.exists and is a pain when mocked. | 1135 # OptParser calls presubmit.os.path.exists and is a pain when mocked. |
| 1032 self.UnMock(presubmit.os.path, 'exists') | 1136 self.UnMock(presubmit.os.path, 'exists') |
| 1033 self.mox.StubOutWithMock(presubmit, 'DoPresubmitChecks') | 1137 self.mox.StubOutWithMock(presubmit, 'DoPresubmitChecks') |
| 1034 self.mox.StubOutWithMock(presubmit, 'ParseFiles') | 1138 self.mox.StubOutWithMock(presubmit, 'ParseFiles') |
| 1035 presubmit.scm.determine_scm(self.fake_root_dir).AndReturn(None) | 1139 presubmit.scm.determine_scm(self.fake_root_dir).AndReturn(None) |
| 1036 presubmit.ParseFiles(['random_file.txt'], None | 1140 presubmit.ParseFiles(['random_file.txt'], None |
| 1037 ).AndReturn([('M', 'random_file.txt')]) | 1141 ).AndReturn([('M', 'random_file.txt')]) |
| 1038 output = self.mox.CreateMock(presubmit.PresubmitOutput) | 1142 output = self.mox.CreateMock(presubmit.PresubmitOutput) |
| 1039 output.should_continue().AndReturn(False) | 1143 output.should_continue().AndReturn(False) |
| (...skipping 1703 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2743 owners_check=False) | 2847 owners_check=False) |
| 2744 self.assertEqual(1, len(results)) | 2848 self.assertEqual(1, len(results)) |
| 2745 self.assertEqual( | 2849 self.assertEqual( |
| 2746 'Found line ending with white spaces in:', results[0]._message) | 2850 'Found line ending with white spaces in:', results[0]._message) |
| 2747 self.checkstdout('') | 2851 self.checkstdout('') |
| 2748 | 2852 |
| 2749 | 2853 |
| 2750 if __name__ == '__main__': | 2854 if __name__ == '__main__': |
| 2751 import unittest | 2855 import unittest |
| 2752 unittest.main() | 2856 unittest.main() |
| OLD | NEW |