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