| 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 copy | 
| 10 import functools | 11 import functools | 
|  | 12 import itertools | 
| 11 import logging | 13 import logging | 
| 12 import os | 14 import os | 
| 13 import StringIO | 15 import StringIO | 
| 14 import sys | 16 import sys | 
| 15 import time | 17 import time | 
| 16 import unittest | 18 import unittest | 
| 17 | 19 | 
| 18 _ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | 20 _ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | 
| 19 sys.path.insert(0, _ROOT) | 21 sys.path.insert(0, _ROOT) | 
| 20 | 22 | 
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 53 """ | 55 """ | 
| 54 | 56 | 
| 55   presubmit_tryslave_project = """ | 57   presubmit_tryslave_project = """ | 
| 56 def GetPreferredTrySlaves(project): | 58 def GetPreferredTrySlaves(project): | 
| 57   if project == %s: | 59   if project == %s: | 
| 58     return %s | 60     return %s | 
| 59   else: | 61   else: | 
| 60     return %s | 62     return %s | 
| 61 """ | 63 """ | 
| 62 | 64 | 
|  | 65   presubmit_trymaster = """ | 
|  | 66 def GetPreferredTryMasters(): | 
|  | 67   return %s | 
|  | 68 """ | 
|  | 69 | 
| 63   presubmit_diffs = """ | 70   presubmit_diffs = """ | 
| 64 --- file1       2011-02-09 10:38:16.517224845 -0800 | 71 --- file1       2011-02-09 10:38:16.517224845 -0800 | 
| 65 +++ file2       2011-02-09 10:38:53.177226516 -0800 | 72 +++ file2       2011-02-09 10:38:53.177226516 -0800 | 
| 66 @@ -1,6 +1,5 @@ | 73 @@ -1,6 +1,5 @@ | 
| 67  this is line number 0 | 74  this is line number 0 | 
| 68  this is line number 1 | 75  this is line number 1 | 
| 69 -this is line number 2 to be deleted | 76 -this is line number 2 to be deleted | 
| 70  this is line number 3 | 77  this is line number 3 | 
| 71  this is line number 4 | 78  this is line number 4 | 
| 72  this is line number 5 | 79  this is line number 5 | 
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 163       'GetTrySlavesExecuter', 'GitAffectedFile', 'CallCommand', 'CommandData', | 170       'GetTrySlavesExecuter', 'GitAffectedFile', 'CallCommand', 'CommandData', | 
| 164       'GitChange', 'InputApi', 'ListRelevantPresubmitFiles', 'Main', | 171       'GitChange', 'InputApi', 'ListRelevantPresubmitFiles', 'Main', | 
| 165       'NonexistantCannedCheckFilter', 'OutputApi', 'ParseFiles', | 172       'NonexistantCannedCheckFilter', 'OutputApi', 'ParseFiles', | 
| 166       'PresubmitFailure', 'PresubmitExecuter', 'PresubmitOutput', 'ScanSubDirs', | 173       'PresubmitFailure', 'PresubmitExecuter', 'PresubmitOutput', 'ScanSubDirs', | 
| 167       'SvnAffectedFile', 'SvnChange', 'cPickle', 'cpplint', 'cStringIO', | 174       'SvnAffectedFile', 'SvnChange', 'cPickle', 'cpplint', 'cStringIO', | 
| 168       'contextlib', 'canned_check_filter', 'fix_encoding', 'fnmatch', | 175       'contextlib', 'canned_check_filter', 'fix_encoding', 'fnmatch', | 
| 169       'gclient_utils', 'glob', 'inspect', 'json', 'load_files', 'logging', | 176       'gclient_utils', 'glob', 'inspect', 'json', 'load_files', 'logging', | 
| 170       'marshal', 'normpath', 'optparse', 'os', 'owners', 'pickle', | 177       'marshal', 'normpath', 'optparse', 'os', 'owners', 'pickle', | 
| 171       'presubmit_canned_checks', 'random', 're', 'rietveld', 'scm', | 178       'presubmit_canned_checks', 'random', 're', 'rietveld', 'scm', | 
| 172       'subprocess', 'sys', 'tempfile', 'time', 'traceback', 'types', 'unittest', | 179       'subprocess', 'sys', 'tempfile', 'time', 'traceback', 'types', 'unittest', | 
| 173       'urllib2', 'warn', 'multiprocessing', | 180       'urllib2', 'warn', 'multiprocessing', 'DoGetTryMasters', | 
|  | 181       'GetTryMastersExecuter', | 
| 174     ] | 182     ] | 
| 175     # If this test fails, you should add the relevant test. | 183     # If this test fails, you should add the relevant test. | 
| 176     self.compareMembers(presubmit, members) | 184     self.compareMembers(presubmit, members) | 
| 177 | 185 | 
| 178   def testCannedCheckFilter(self): | 186   def testCannedCheckFilter(self): | 
| 179     canned = presubmit.presubmit_canned_checks | 187     canned = presubmit.presubmit_canned_checks | 
| 180     orig = canned.CheckOwners | 188     orig = canned.CheckOwners | 
| 181     with presubmit.canned_check_filter(['CheckOwners']): | 189     with presubmit.canned_check_filter(['CheckOwners']): | 
| 182       self.assertNotEqual(canned.CheckOwners, orig) | 190       self.assertNotEqual(canned.CheckOwners, orig) | 
| 183       self.assertEqual(canned.CheckOwners(None, None), []) | 191       self.assertEqual(canned.CheckOwners(None, None), []) | 
| (...skipping 836 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 1020     script_text = ( | 1028     script_text = ( | 
| 1021         'def GetPreferredTrySlaves():\n' | 1029         'def GetPreferredTrySlaves():\n' | 
| 1022         '  return ["foo,bar"]\n') | 1030         '  return ["foo,bar"]\n') | 
| 1023     try: | 1031     try: | 
| 1024       presubmit.GetTrySlavesExecuter.ExecPresubmitScript( | 1032       presubmit.GetTrySlavesExecuter.ExecPresubmitScript( | 
| 1025           script_text, 'path', 'project', None) | 1033           script_text, 'path', 'project', None) | 
| 1026       self.fail() | 1034       self.fail() | 
| 1027     except presubmit.PresubmitFailure: | 1035     except presubmit.PresubmitFailure: | 
| 1028       pass | 1036       pass | 
| 1029 | 1037 | 
|  | 1038   def testGetTryMastersExecuter(self): | 
|  | 1039     self.mox.ReplayAll() | 
|  | 1040     change = presubmit.Change( | 
|  | 1041         'foo', | 
|  | 1042         'Blah Blah\n\nSTORY=http://tracker.com/42\nBUG=boo\n', | 
|  | 1043         self.fake_root_dir, | 
|  | 1044         None, | 
|  | 1045         0, | 
|  | 1046         0, | 
|  | 1047         None) | 
|  | 1048     executer = presubmit.GetTryMastersExecuter() | 
|  | 1049     self.assertEqual({}, executer.ExecPresubmitScript('', '', '', change)) | 
|  | 1050     self.assertEqual({}, | 
|  | 1051         executer.ExecPresubmitScript('def foo():\n  return\n', '', '', change)) | 
|  | 1052 | 
|  | 1053     # bad results | 
|  | 1054     empty_master_result = "{'': {}}" | 
|  | 1055     starts_with_space_result = "{' m': {}}" | 
|  | 1056     empty_slave_result = "{'m': {'': set()}}" | 
|  | 1057     not_dict_result1 = "['a']" | 
|  | 1058     not_dict_result2 = "{'m': ''}" | 
|  | 1059     not_set_result = "{'m': {'s': []}}" | 
|  | 1060 | 
|  | 1061     for result in ( | 
|  | 1062         empty_master_result, starts_with_space_result, empty_slave_result, | 
|  | 1063         not_dict_result1, not_dict_result2, not_set_result): | 
|  | 1064       self.assertRaises(presubmit.PresubmitFailure, | 
|  | 1065                         executer.ExecPresubmitScript, | 
|  | 1066                         self.presubmit_trymaster % result, '', '', change) | 
|  | 1067 | 
|  | 1068     # good results | 
|  | 1069     expected_result = {'m1': {'s1': set(['t1', 't2'])}, 'm2': ['s1', 's2']} | 
|  | 1070     empty_result1 = {} | 
|  | 1071     empty_result2 = {'m': {}} | 
|  | 1072     empty_result3 = {'m': []} | 
|  | 1073     space_in_name_result = {'m r': {'s\tv': set(['t1'])}} | 
|  | 1074     for result in ( | 
|  | 1075         expected_result, empty_result1, empty_result2, empty_result3, | 
|  | 1076         space_in_name_result): | 
|  | 1077       self.assertEqual( | 
|  | 1078           result, | 
|  | 1079           executer.ExecPresubmitScript( | 
|  | 1080               self.presubmit_trymaster % result, '', '', change)) | 
|  | 1081 | 
|  | 1082   def testMergeMasters(self): | 
|  | 1083     merge = presubmit._MergeMasters | 
|  | 1084     self.assertEqual({}, merge({}, {})) | 
|  | 1085     self.assertEqual({'m1': {}}, merge({}, {'m1': {}})) | 
|  | 1086     self.assertEqual({'m1': {}}, merge({'m1': {}}, {})) | 
|  | 1087     parts = [ | 
|  | 1088       {'try1.cr': ['win']}, | 
|  | 1089       {'try1.cr': {'linux1': set(['test1'])}, 'try2.cr': ['linux2']}, | 
|  | 1090       {'try1.cr': {'mac1': set([]), 'mac2': set(['test1', 'test2'])}}, | 
|  | 1091     ] | 
|  | 1092     expected = { | 
|  | 1093       'try1.cr': {'win': set([]), | 
|  | 1094                   'linux1': set(['test1']), | 
|  | 1095                   'mac1': set([]), | 
|  | 1096                   'mac2': set(['test1', 'test2'])}, | 
|  | 1097       'try2.cr': {'linux2': set([])}, | 
|  | 1098     } | 
|  | 1099     for permutation in itertools.permutations(parts): | 
|  | 1100       self.assertEqual(expected, | 
|  | 1101                        reduce(merge, map(copy.deepcopy, permutation), {})) | 
|  | 1102 | 
|  | 1103   def testDoGetTryMasters(self): | 
|  | 1104     root_text = self.presubmit_trymaster % '{"try1.cr": ["win"]}' | 
|  | 1105     linux_text = (self.presubmit_trymaster | 
|  | 1106         % '{"try1.cr": {"linux1": set(["test1"])}, "try2.cr": ["linux2"]}') | 
|  | 1107 | 
|  | 1108     join = presubmit.os.path.join | 
|  | 1109     isfile = presubmit.os.path.isfile | 
|  | 1110     FileRead = presubmit.gclient_utils.FileRead | 
|  | 1111     filename = 'foo.cc' | 
|  | 1112     filename_linux = join('linux_only', 'penguin.cc') | 
|  | 1113     root_presubmit = join(self.fake_root_dir, 'PRESUBMIT.py') | 
|  | 1114     linux_presubmit = join(self.fake_root_dir, 'linux_only', 'PRESUBMIT.py') | 
|  | 1115     inherit_path = join(self.fake_root_dir, self._INHERIT_SETTINGS) | 
|  | 1116 | 
|  | 1117     isfile(inherit_path).AndReturn(False) | 
|  | 1118     isfile(root_presubmit).AndReturn(True) | 
|  | 1119     FileRead(root_presubmit, 'rU').AndReturn(root_text) | 
|  | 1120 | 
|  | 1121     isfile(inherit_path).AndReturn(False) | 
|  | 1122     isfile(root_presubmit).AndReturn(True) | 
|  | 1123     isfile(linux_presubmit).AndReturn(True) | 
|  | 1124     FileRead(root_presubmit, 'rU').AndReturn(root_text) | 
|  | 1125     FileRead(linux_presubmit, 'rU').AndReturn(linux_text) | 
|  | 1126     self.mox.ReplayAll() | 
|  | 1127 | 
|  | 1128     change = presubmit.Change( | 
|  | 1129         'mychange', '', self.fake_root_dir, [], 0, 0, None) | 
|  | 1130 | 
|  | 1131     output = StringIO.StringIO() | 
|  | 1132     self.assertEqual({'try1.cr': {'win': ['defaulttests']}}, | 
|  | 1133                      presubmit.DoGetTryMasters(change, [filename], | 
|  | 1134                                                self.fake_root_dir, | 
|  | 1135                                                None, None, False, output)) | 
|  | 1136     output = StringIO.StringIO() | 
|  | 1137     expected = { | 
|  | 1138       'try1.cr': {'win': ['defaulttests'], 'linux1': ['test1']}, | 
|  | 1139       'try2.cr': {'linux2': ['defaulttests']}, | 
|  | 1140     } | 
|  | 1141     self.assertEqual(expected, | 
|  | 1142                      presubmit.DoGetTryMasters(change, | 
|  | 1143                                                [filename, filename_linux], | 
|  | 1144                                                self.fake_root_dir, None, None, | 
|  | 1145                                                False, output)) | 
|  | 1146 | 
| 1030   def testMainUnversioned(self): | 1147   def testMainUnversioned(self): | 
| 1031     # OptParser calls presubmit.os.path.exists and is a pain when mocked. | 1148     # OptParser calls presubmit.os.path.exists and is a pain when mocked. | 
| 1032     self.UnMock(presubmit.os.path, 'exists') | 1149     self.UnMock(presubmit.os.path, 'exists') | 
| 1033     self.mox.StubOutWithMock(presubmit, 'DoPresubmitChecks') | 1150     self.mox.StubOutWithMock(presubmit, 'DoPresubmitChecks') | 
| 1034     self.mox.StubOutWithMock(presubmit, 'ParseFiles') | 1151     self.mox.StubOutWithMock(presubmit, 'ParseFiles') | 
| 1035     presubmit.scm.determine_scm(self.fake_root_dir).AndReturn(None) | 1152     presubmit.scm.determine_scm(self.fake_root_dir).AndReturn(None) | 
| 1036     presubmit.ParseFiles(['random_file.txt'], None | 1153     presubmit.ParseFiles(['random_file.txt'], None | 
| 1037         ).AndReturn([('M', 'random_file.txt')]) | 1154         ).AndReturn([('M', 'random_file.txt')]) | 
| 1038     output = self.mox.CreateMock(presubmit.PresubmitOutput) | 1155     output = self.mox.CreateMock(presubmit.PresubmitOutput) | 
| 1039     output.should_continue().AndReturn(False) | 1156     output.should_continue().AndReturn(False) | 
| (...skipping 1703 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 2743         owners_check=False) | 2860         owners_check=False) | 
| 2744     self.assertEqual(1, len(results)) | 2861     self.assertEqual(1, len(results)) | 
| 2745     self.assertEqual( | 2862     self.assertEqual( | 
| 2746         'Found line ending with white spaces in:', results[0]._message) | 2863         'Found line ending with white spaces in:', results[0]._message) | 
| 2747     self.checkstdout('') | 2864     self.checkstdout('') | 
| 2748 | 2865 | 
| 2749 | 2866 | 
| 2750 if __name__ == '__main__': | 2867 if __name__ == '__main__': | 
| 2751   import unittest | 2868   import unittest | 
| 2752   unittest.main() | 2869   unittest.main() | 
| OLD | NEW | 
|---|