| 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 __builtin__ |  | 
| 11 |  | 
| 12 import contextlib |  | 
| 13 import functools | 10 import functools | 
| 14 import itertools | 11 import itertools | 
| 15 import logging | 12 import logging | 
| 16 import os | 13 import os | 
| 17 import StringIO | 14 import StringIO | 
| 18 import sys | 15 import sys | 
| 19 import time | 16 import time | 
| 20 import unittest | 17 import unittest | 
| 21 | 18 | 
| 22 _ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | 19 _ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | 
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 173       'GitChange', 'InputApi', 'ListRelevantPresubmitFiles', 'Main', | 170       'GitChange', 'InputApi', 'ListRelevantPresubmitFiles', 'Main', | 
| 174       'NonexistantCannedCheckFilter', 'OutputApi', 'ParseFiles', | 171       'NonexistantCannedCheckFilter', 'OutputApi', 'ParseFiles', | 
| 175       'PresubmitFailure', 'PresubmitExecuter', 'PresubmitOutput', 'ScanSubDirs', | 172       'PresubmitFailure', 'PresubmitExecuter', 'PresubmitOutput', 'ScanSubDirs', | 
| 176       'SvnAffectedFile', 'SvnChange', 'cPickle', 'cpplint', 'cStringIO', | 173       'SvnAffectedFile', 'SvnChange', 'cPickle', 'cpplint', 'cStringIO', | 
| 177       'contextlib', 'canned_check_filter', 'fix_encoding', 'fnmatch', | 174       'contextlib', 'canned_check_filter', 'fix_encoding', 'fnmatch', | 
| 178       'gclient_utils', 'glob', 'inspect', 'json', 'load_files', 'logging', | 175       'gclient_utils', 'glob', 'inspect', 'json', 'load_files', 'logging', | 
| 179       'marshal', 'normpath', 'optparse', 'os', 'owners', 'pickle', | 176       'marshal', 'normpath', 'optparse', 'os', 'owners', 'pickle', | 
| 180       'presubmit_canned_checks', 'random', 're', 'rietveld', 'scm', | 177       'presubmit_canned_checks', 'random', 're', 'rietveld', 'scm', | 
| 181       'subprocess', 'sys', 'tempfile', 'time', 'traceback', 'types', 'unittest', | 178       'subprocess', 'sys', 'tempfile', 'time', 'traceback', 'types', 'unittest', | 
| 182       'urllib2', 'warn', 'multiprocessing', 'DoGetTryMasters', | 179       'urllib2', 'warn', 'multiprocessing', 'DoGetTryMasters', | 
| 183       'GetTryMastersExecuter', 'itertools', 'CleanOrphanedCompiledPython', | 180       'GetTryMastersExecuter', 'itertools', | 
| 184       'IsCompiledPython', |  | 
| 185     ] | 181     ] | 
| 186     # If this test fails, you should add the relevant test. | 182     # If this test fails, you should add the relevant test. | 
| 187     self.compareMembers(presubmit, members) | 183     self.compareMembers(presubmit, members) | 
| 188 | 184 | 
| 189   def testCannedCheckFilter(self): | 185   def testCannedCheckFilter(self): | 
| 190     canned = presubmit.presubmit_canned_checks | 186     canned = presubmit.presubmit_canned_checks | 
| 191     orig = canned.CheckOwners | 187     orig = canned.CheckOwners | 
| 192     with presubmit.canned_check_filter(['CheckOwners']): | 188     with presubmit.canned_check_filter(['CheckOwners']): | 
| 193       self.assertNotEqual(canned.CheckOwners, orig) | 189       self.assertNotEqual(canned.CheckOwners, orig) | 
| 194       self.assertEqual(canned.CheckOwners(None, None), []) | 190       self.assertEqual(canned.CheckOwners(None, None), []) | 
| (...skipping 484 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 679     inherit_path = presubmit.os.path.join(self.fake_root_dir, | 675     inherit_path = presubmit.os.path.join(self.fake_root_dir, | 
| 680                                           self._INHERIT_SETTINGS) | 676                                           self._INHERIT_SETTINGS) | 
| 681     presubmit.os.path.isfile(inherit_path).AndReturn(False) | 677     presubmit.os.path.isfile(inherit_path).AndReturn(False) | 
| 682     presubmit.os.path.isfile(root_path).AndReturn(True) | 678     presubmit.os.path.isfile(root_path).AndReturn(True) | 
| 683     presubmit.os.path.isfile(haspresubmit_path).AndReturn(True) | 679     presubmit.os.path.isfile(haspresubmit_path).AndReturn(True) | 
| 684     presubmit.gclient_utils.FileRead(root_path, | 680     presubmit.gclient_utils.FileRead(root_path, | 
| 685                                      'rU').AndReturn(self.presubmit_text) | 681                                      'rU').AndReturn(self.presubmit_text) | 
| 686     presubmit.gclient_utils.FileRead(haspresubmit_path, | 682     presubmit.gclient_utils.FileRead(haspresubmit_path, | 
| 687                                      'rU').AndReturn(self.presubmit_text) | 683                                      'rU').AndReturn(self.presubmit_text) | 
| 688     presubmit.random.randint(0, 4).AndReturn(1) | 684     presubmit.random.randint(0, 4).AndReturn(1) | 
| 689     presubmit.os.walk(self.fake_root_dir).AndReturn([]) |  | 
| 690     self.mox.ReplayAll() | 685     self.mox.ReplayAll() | 
| 691 | 686 | 
| 692     input_buf = StringIO.StringIO('y\n') | 687     input_buf = StringIO.StringIO('y\n') | 
| 693     change = presubmit.Change( | 688     change = presubmit.Change( | 
| 694         'mychange', | 689         'mychange', | 
| 695         '\n'.join(description_lines), | 690         '\n'.join(description_lines), | 
| 696         self.fake_root_dir, | 691         self.fake_root_dir, | 
| 697         files, | 692         files, | 
| 698         0, | 693         0, | 
| 699         0, | 694         0, | 
| (...skipping 20 matching lines...) Expand all  Loading... | 
| 720     for _ in range(2): | 715     for _ in range(2): | 
| 721       presubmit.os.path.isfile(inherit_path).AndReturn(False) | 716       presubmit.os.path.isfile(inherit_path).AndReturn(False) | 
| 722       presubmit.os.path.isfile(presubmit_path).AndReturn(True) | 717       presubmit.os.path.isfile(presubmit_path).AndReturn(True) | 
| 723       presubmit.os.path.isfile(haspresubmit_path).AndReturn(True) | 718       presubmit.os.path.isfile(haspresubmit_path).AndReturn(True) | 
| 724       presubmit.gclient_utils.FileRead(presubmit_path, 'rU' | 719       presubmit.gclient_utils.FileRead(presubmit_path, 'rU' | 
| 725           ).AndReturn(self.presubmit_text) | 720           ).AndReturn(self.presubmit_text) | 
| 726       presubmit.gclient_utils.FileRead(haspresubmit_path, 'rU' | 721       presubmit.gclient_utils.FileRead(haspresubmit_path, 'rU' | 
| 727           ).AndReturn(self.presubmit_text) | 722           ).AndReturn(self.presubmit_text) | 
| 728     presubmit.random.randint(0, 4).AndReturn(1) | 723     presubmit.random.randint(0, 4).AndReturn(1) | 
| 729     presubmit.random.randint(0, 4).AndReturn(1) | 724     presubmit.random.randint(0, 4).AndReturn(1) | 
| 730     presubmit.os.walk(self.fake_root_dir).AndReturn([]) |  | 
| 731     presubmit.os.walk(self.fake_root_dir).AndReturn([]) |  | 
| 732     self.mox.ReplayAll() | 725     self.mox.ReplayAll() | 
| 733 | 726 | 
| 734     input_buf = StringIO.StringIO('n\n')  # say no to the warning | 727     input_buf = StringIO.StringIO('n\n')  # say no to the warning | 
| 735     change = presubmit.Change( | 728     change = presubmit.Change( | 
| 736         'mychange', | 729         'mychange', | 
| 737         '\n'.join(description_lines), | 730         '\n'.join(description_lines), | 
| 738         self.fake_root_dir, | 731         self.fake_root_dir, | 
| 739         files, | 732         files, | 
| 740         0, | 733         0, | 
| 741         0, | 734         0, | 
| (...skipping 26 matching lines...) Expand all  Loading... | 
| 768     inherit_path = presubmit.os.path.join(self.fake_root_dir, | 761     inherit_path = presubmit.os.path.join(self.fake_root_dir, | 
| 769                                           self._INHERIT_SETTINGS) | 762                                           self._INHERIT_SETTINGS) | 
| 770     presubmit.os.path.isfile(inherit_path).AndReturn(False) | 763     presubmit.os.path.isfile(inherit_path).AndReturn(False) | 
| 771     presubmit.os.path.isfile(presubmit_path).AndReturn(True) | 764     presubmit.os.path.isfile(presubmit_path).AndReturn(True) | 
| 772     presubmit.os.path.isfile(haspresubmit_path).AndReturn(True) | 765     presubmit.os.path.isfile(haspresubmit_path).AndReturn(True) | 
| 773     presubmit.gclient_utils.FileRead(presubmit_path, 'rU' | 766     presubmit.gclient_utils.FileRead(presubmit_path, 'rU' | 
| 774                                      ).AndReturn(self.presubmit_text) | 767                                      ).AndReturn(self.presubmit_text) | 
| 775     presubmit.gclient_utils.FileRead(haspresubmit_path, 'rU').AndReturn( | 768     presubmit.gclient_utils.FileRead(haspresubmit_path, 'rU').AndReturn( | 
| 776         self.presubmit_text) | 769         self.presubmit_text) | 
| 777     presubmit.random.randint(0, 4).AndReturn(1) | 770     presubmit.random.randint(0, 4).AndReturn(1) | 
| 778     presubmit.os.walk(self.fake_root_dir).AndReturn([]) |  | 
| 779     self.mox.ReplayAll() | 771     self.mox.ReplayAll() | 
| 780 | 772 | 
| 781     change = presubmit.Change( | 773     change = presubmit.Change( | 
| 782         'mychange', | 774         'mychange', | 
| 783         '\n'.join(description_lines), | 775         '\n'.join(description_lines), | 
| 784         self.fake_root_dir, | 776         self.fake_root_dir, | 
| 785         files, | 777         files, | 
| 786         0, | 778         0, | 
| 787         0, | 779         0, | 
| 788         None) | 780         None) | 
| (...skipping 21 matching lines...) Expand all  Loading... | 
| 810 """ | 802 """ | 
| 811     inherit_path = presubmit.os.path.join(self.fake_root_dir, | 803     inherit_path = presubmit.os.path.join(self.fake_root_dir, | 
| 812                                           self._INHERIT_SETTINGS) | 804                                           self._INHERIT_SETTINGS) | 
| 813     presubmit.os.path.isfile(inherit_path).AndReturn(False) | 805     presubmit.os.path.isfile(inherit_path).AndReturn(False) | 
| 814     presubmit.os.path.isfile(join(self.fake_root_dir, 'PRESUBMIT.py') | 806     presubmit.os.path.isfile(join(self.fake_root_dir, 'PRESUBMIT.py') | 
| 815         ).AndReturn(False) | 807         ).AndReturn(False) | 
| 816     presubmit.os.path.isfile(join(self.fake_root_dir, | 808     presubmit.os.path.isfile(join(self.fake_root_dir, | 
| 817                                   'haspresubmit', | 809                                   'haspresubmit', | 
| 818                                   'PRESUBMIT.py')).AndReturn(False) | 810                                   'PRESUBMIT.py')).AndReturn(False) | 
| 819     presubmit.random.randint(0, 4).AndReturn(0) | 811     presubmit.random.randint(0, 4).AndReturn(0) | 
| 820     presubmit.os.walk(self.fake_root_dir).AndReturn([]) |  | 
| 821     self.mox.ReplayAll() | 812     self.mox.ReplayAll() | 
| 822 | 813 | 
| 823     input_buf = StringIO.StringIO('y\n') | 814     input_buf = StringIO.StringIO('y\n') | 
| 824     # Always fail. | 815     # Always fail. | 
| 825     change = presubmit.Change( | 816     change = presubmit.Change( | 
| 826         'mychange', | 817         'mychange', | 
| 827         '\n'.join(description_lines), | 818         '\n'.join(description_lines), | 
| 828         self.fake_root_dir, | 819         self.fake_root_dir, | 
| 829         files, | 820         files, | 
| 830         0, | 821         0, | 
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 893     return [output_api.PresubmitError('Tag parsing failed. 5 ' + | 884     return [output_api.PresubmitError('Tag parsing failed. 5 ' + | 
| 894                                       input_api.change.FullDescriptionText())] | 885                                       input_api.change.FullDescriptionText())] | 
| 895   return [output_api.PresubmitNotifyResult(input_api.change.tags['STORY'])] | 886   return [output_api.PresubmitNotifyResult(input_api.change.tags['STORY'])] | 
| 896 def CheckChangeOnCommit(input_api, output_api): | 887 def CheckChangeOnCommit(input_api, output_api): | 
| 897   raise Exception("Test error") | 888   raise Exception("Test error") | 
| 898 """ | 889 """ | 
| 899     presubmit.random.randint(0, 4).AndReturn(1) | 890     presubmit.random.randint(0, 4).AndReturn(1) | 
| 900     inherit_path = presubmit.os.path.join(self.fake_root_dir, | 891     inherit_path = presubmit.os.path.join(self.fake_root_dir, | 
| 901                                           self._INHERIT_SETTINGS) | 892                                           self._INHERIT_SETTINGS) | 
| 902     presubmit.os.path.isfile(inherit_path).AndReturn(False) | 893     presubmit.os.path.isfile(inherit_path).AndReturn(False) | 
| 903     presubmit.os.walk(self.fake_root_dir).AndReturn([]) |  | 
| 904     self.mox.ReplayAll() | 894     self.mox.ReplayAll() | 
| 905 | 895 | 
| 906     output = StringIO.StringIO() | 896     output = StringIO.StringIO() | 
| 907     input_buf = StringIO.StringIO('y\n') | 897     input_buf = StringIO.StringIO('y\n') | 
| 908     change = presubmit.Change( | 898     change = presubmit.Change( | 
| 909         'foo', | 899         'foo', | 
| 910         'Blah Blah\n\nSTORY=http://tracker.com/42\nBUG=boo\n', | 900         'Blah Blah\n\nSTORY=http://tracker.com/42\nBUG=boo\n', | 
| 911         self.fake_root_dir, | 901         self.fake_root_dir, | 
| 912         None, | 902         None, | 
| 913         0, | 903         0, | 
| (...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 1178         'presubmit_unittest.py: error: For unversioned directory, <files> is ' | 1168         'presubmit_unittest.py: error: For unversioned directory, <files> is ' | 
| 1179         'not optional.\n') | 1169         'not optional.\n') | 
| 1180     self.mox.ReplayAll() | 1170     self.mox.ReplayAll() | 
| 1181 | 1171 | 
| 1182     try: | 1172     try: | 
| 1183       presubmit.Main(['--root', self.fake_root_dir]) | 1173       presubmit.Main(['--root', self.fake_root_dir]) | 
| 1184       self.fail() | 1174       self.fail() | 
| 1185     except SystemExit, e: | 1175     except SystemExit, e: | 
| 1186       self.assertEquals(2, e.code) | 1176       self.assertEquals(2, e.code) | 
| 1187 | 1177 | 
| 1188   def testOrphanedCompiledPythonDoesntRemoveFiles(self): |  | 
| 1189     self.mox.StubOutWithMock(presubmit, 'IsCompiledPython') |  | 
| 1190     py_path = os.path.join(self.fake_root_dir, 'myfile.py') |  | 
| 1191     pyc_path = os.path.join(self.fake_root_dir, 'myfile.pyc') |  | 
| 1192 |  | 
| 1193     presubmit.os.walk(self.fake_root_dir).AndReturn([ |  | 
| 1194         (self.fake_root_dir, [], ['myfile.pyc']), |  | 
| 1195     ]) |  | 
| 1196     presubmit.os.path.isfile(py_path).AndReturn(False) |  | 
| 1197     presubmit.IsCompiledPython(pyc_path).AndReturn(True) |  | 
| 1198     self.mox.ReplayAll() |  | 
| 1199 |  | 
| 1200     output = StringIO.StringIO() |  | 
| 1201     presubmit.CleanOrphanedCompiledPython(output, self.fake_root_dir, False) |  | 
| 1202 |  | 
| 1203   def testCleanOrphanedCompiledPython(self): |  | 
| 1204     self.mox.StubOutWithMock(presubmit, 'IsCompiledPython') |  | 
| 1205     py_path = os.path.join(self.fake_root_dir, 'myfile.py') |  | 
| 1206     pyc_path = os.path.join(self.fake_root_dir, 'myfile.pyc') |  | 
| 1207 |  | 
| 1208     presubmit.os.walk(self.fake_root_dir).AndReturn([ |  | 
| 1209         (self.fake_root_dir, [], ['myfile.pyc']), |  | 
| 1210     ]) |  | 
| 1211     presubmit.os.path.isfile(py_path).AndReturn(False) |  | 
| 1212     presubmit.IsCompiledPython(pyc_path).AndReturn(True) |  | 
| 1213     presubmit.os.remove(pyc_path) |  | 
| 1214     self.mox.ReplayAll() |  | 
| 1215 |  | 
| 1216     output = StringIO.StringIO() |  | 
| 1217     presubmit.CleanOrphanedCompiledPython(output, self.fake_root_dir, True) |  | 
| 1218 |  | 
| 1219   def testCleanNonOrphanedCompiledPython(self): |  | 
| 1220     self.mox.StubOutWithMock(presubmit, 'IsCompiledPython') |  | 
| 1221     py_path = os.path.join(self.fake_root_dir, 'myfile.py') |  | 
| 1222 |  | 
| 1223     presubmit.os.walk(self.fake_root_dir).AndReturn([ |  | 
| 1224         (self.fake_root_dir, [], ['myfile.pyc', 'myfile.py']), |  | 
| 1225     ]) |  | 
| 1226     presubmit.os.path.isfile(py_path).AndReturn(True) |  | 
| 1227     self.mox.ReplayAll() |  | 
| 1228 |  | 
| 1229     output = StringIO.StringIO() |  | 
| 1230     presubmit.CleanOrphanedCompiledPython(output, self.fake_root_dir, True) |  | 
| 1231 |  | 
| 1232   def testCleanOrphanedCompiledPythonAvoidsOtherFiles(self): |  | 
| 1233     self.mox.StubOutWithMock(presubmit, 'IsCompiledPython') |  | 
| 1234 |  | 
| 1235     presubmit.os.walk(self.fake_root_dir).AndReturn([ |  | 
| 1236         (self.fake_root_dir, [], ['otherfile.txt']), |  | 
| 1237     ]) |  | 
| 1238     self.mox.ReplayAll() |  | 
| 1239 |  | 
| 1240     output = StringIO.StringIO() |  | 
| 1241     presubmit.CleanOrphanedCompiledPython(output, self.fake_root_dir, True) |  | 
| 1242 |  | 
| 1243   def testIsCompiledPython(self): |  | 
| 1244     self.mox.StubOutWithMock(__builtin__, 'open') |  | 
| 1245     pyc_path = os.path.join(self.fake_root_dir, 'testfile.pyc') |  | 
| 1246     open(pyc_path, 'rb').AndReturn(contextlib.closing( |  | 
| 1247         StringIO.StringIO('\x00\x00\x0d\x0a\x00\x00\x00\x00'))) |  | 
| 1248     self.mox.ReplayAll() |  | 
| 1249 |  | 
| 1250     self.assertTrue(presubmit.IsCompiledPython(pyc_path)) |  | 
| 1251 |  | 
| 1252   def testIsCompiledPythonFailsForInvalidMagic(self): |  | 
| 1253     self.mox.StubOutWithMock(__builtin__, 'open') |  | 
| 1254     pyc_path = os.path.join(self.fake_root_dir, 'testfile.pyc') |  | 
| 1255     open(pyc_path, 'rb').AndReturn(contextlib.closing( |  | 
| 1256         StringIO.StringIO('\xca\xfe\xba\xbe\x00\x00\x00\x00'))) |  | 
| 1257     self.mox.ReplayAll() |  | 
| 1258 |  | 
| 1259     self.assertFalse(presubmit.IsCompiledPython(pyc_path)) |  | 
| 1260 |  | 
| 1261   def testIsCompiledPythonFailsForInvalidSize(self): |  | 
| 1262     self.mox.StubOutWithMock(__builtin__, 'open') |  | 
| 1263     pyc_path = os.path.join(self.fake_root_dir, 'testfile.pyc') |  | 
| 1264     open(pyc_path, 'rb').AndReturn(contextlib.closing( |  | 
| 1265         StringIO.StringIO('\x00\x00\x0d\x0a\x00\x00\x00'))) |  | 
| 1266     self.mox.ReplayAll() |  | 
| 1267 |  | 
| 1268     self.assertFalse(presubmit.IsCompiledPython(pyc_path)) |  | 
| 1269 |  | 
| 1270 | 1178 | 
| 1271 class InputApiUnittest(PresubmitTestsBase): | 1179 class InputApiUnittest(PresubmitTestsBase): | 
| 1272   """Tests presubmit.InputApi.""" | 1180   """Tests presubmit.InputApi.""" | 
| 1273   def testMembersChanged(self): | 1181   def testMembersChanged(self): | 
| 1274     self.mox.ReplayAll() | 1182     self.mox.ReplayAll() | 
| 1275     members = [ | 1183     members = [ | 
| 1276       'AbsoluteLocalPaths', 'AffectedFiles', 'AffectedSourceFiles', | 1184       'AbsoluteLocalPaths', 'AffectedFiles', 'AffectedSourceFiles', | 
| 1277       'AffectedTextFiles', | 1185       'AffectedTextFiles', | 
| 1278       'DEFAULT_BLACK_LIST', 'DEFAULT_WHITE_LIST', | 1186       'DEFAULT_BLACK_LIST', 'DEFAULT_WHITE_LIST', | 
| 1279       'DepotToLocalPath', 'FilterSourceFile', 'LocalPaths', | 1187       'DepotToLocalPath', 'FilterSourceFile', 'LocalPaths', | 
| (...skipping 1731 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 3011         owners_check=False) | 2919         owners_check=False) | 
| 3012     self.assertEqual(1, len(results)) | 2920     self.assertEqual(1, len(results)) | 
| 3013     self.assertEqual( | 2921     self.assertEqual( | 
| 3014         'Found line ending with white spaces in:', results[0]._message) | 2922         'Found line ending with white spaces in:', results[0]._message) | 
| 3015     self.checkstdout('') | 2923     self.checkstdout('') | 
| 3016 | 2924 | 
| 3017 | 2925 | 
| 3018 if __name__ == '__main__': | 2926 if __name__ == '__main__': | 
| 3019   import unittest | 2927   import unittest | 
| 3020   unittest.main() | 2928   unittest.main() | 
| OLD | NEW | 
|---|