Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(71)

Side by Side Diff: tests/presubmit_unittest.py

Issue 621533002: Added orphaned compiled Python file checks. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/tools/depot_tools
Patch Set: Updated tests w/ suggestions. Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « presubmit_support.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
170 'GitChange', 'InputApi', 'ListRelevantPresubmitFiles', 'Main', 173 'GitChange', 'InputApi', 'ListRelevantPresubmitFiles', 'Main',
171 'NonexistantCannedCheckFilter', 'OutputApi', 'ParseFiles', 174 'NonexistantCannedCheckFilter', 'OutputApi', 'ParseFiles',
172 'PresubmitFailure', 'PresubmitExecuter', 'PresubmitOutput', 'ScanSubDirs', 175 'PresubmitFailure', 'PresubmitExecuter', 'PresubmitOutput', 'ScanSubDirs',
173 'SvnAffectedFile', 'SvnChange', 'cPickle', 'cpplint', 'cStringIO', 176 'SvnAffectedFile', 'SvnChange', 'cPickle', 'cpplint', 'cStringIO',
174 'contextlib', 'canned_check_filter', 'fix_encoding', 'fnmatch', 177 'contextlib', 'canned_check_filter', 'fix_encoding', 'fnmatch',
175 'gclient_utils', 'glob', 'inspect', 'json', 'load_files', 'logging', 178 'gclient_utils', 'glob', 'inspect', 'json', 'load_files', 'logging',
176 'marshal', 'normpath', 'optparse', 'os', 'owners', 'pickle', 179 'marshal', 'normpath', 'optparse', 'os', 'owners', 'pickle',
177 'presubmit_canned_checks', 'random', 're', 'rietveld', 'scm', 180 'presubmit_canned_checks', 'random', 're', 'rietveld', 'scm',
178 'subprocess', 'sys', 'tempfile', 'time', 'traceback', 'types', 'unittest', 181 'subprocess', 'sys', 'tempfile', 'time', 'traceback', 'types', 'unittest',
179 'urllib2', 'warn', 'multiprocessing', 'DoGetTryMasters', 182 'urllib2', 'warn', 'multiprocessing', 'DoGetTryMasters',
180 'GetTryMastersExecuter', 'itertools', 183 'GetTryMastersExecuter', 'itertools', 'CleanOrphanedCompiledPython',
184 'IsCompiledPython',
181 ] 185 ]
182 # If this test fails, you should add the relevant test. 186 # If this test fails, you should add the relevant test.
183 self.compareMembers(presubmit, members) 187 self.compareMembers(presubmit, members)
184 188
185 def testCannedCheckFilter(self): 189 def testCannedCheckFilter(self):
186 canned = presubmit.presubmit_canned_checks 190 canned = presubmit.presubmit_canned_checks
187 orig = canned.CheckOwners 191 orig = canned.CheckOwners
188 with presubmit.canned_check_filter(['CheckOwners']): 192 with presubmit.canned_check_filter(['CheckOwners']):
189 self.assertNotEqual(canned.CheckOwners, orig) 193 self.assertNotEqual(canned.CheckOwners, orig)
190 self.assertEqual(canned.CheckOwners(None, None), []) 194 self.assertEqual(canned.CheckOwners(None, None), [])
(...skipping 484 matching lines...) Expand 10 before | Expand all | Expand 10 after
675 inherit_path = presubmit.os.path.join(self.fake_root_dir, 679 inherit_path = presubmit.os.path.join(self.fake_root_dir,
676 self._INHERIT_SETTINGS) 680 self._INHERIT_SETTINGS)
677 presubmit.os.path.isfile(inherit_path).AndReturn(False) 681 presubmit.os.path.isfile(inherit_path).AndReturn(False)
678 presubmit.os.path.isfile(root_path).AndReturn(True) 682 presubmit.os.path.isfile(root_path).AndReturn(True)
679 presubmit.os.path.isfile(haspresubmit_path).AndReturn(True) 683 presubmit.os.path.isfile(haspresubmit_path).AndReturn(True)
680 presubmit.gclient_utils.FileRead(root_path, 684 presubmit.gclient_utils.FileRead(root_path,
681 'rU').AndReturn(self.presubmit_text) 685 'rU').AndReturn(self.presubmit_text)
682 presubmit.gclient_utils.FileRead(haspresubmit_path, 686 presubmit.gclient_utils.FileRead(haspresubmit_path,
683 'rU').AndReturn(self.presubmit_text) 687 'rU').AndReturn(self.presubmit_text)
684 presubmit.random.randint(0, 4).AndReturn(1) 688 presubmit.random.randint(0, 4).AndReturn(1)
689 presubmit.os.walk(self.fake_root_dir).AndReturn([])
685 self.mox.ReplayAll() 690 self.mox.ReplayAll()
686 691
687 input_buf = StringIO.StringIO('y\n') 692 input_buf = StringIO.StringIO('y\n')
688 change = presubmit.Change( 693 change = presubmit.Change(
689 'mychange', 694 'mychange',
690 '\n'.join(description_lines), 695 '\n'.join(description_lines),
691 self.fake_root_dir, 696 self.fake_root_dir,
692 files, 697 files,
693 0, 698 0,
694 0, 699 0,
(...skipping 20 matching lines...) Expand all
715 for _ in range(2): 720 for _ in range(2):
716 presubmit.os.path.isfile(inherit_path).AndReturn(False) 721 presubmit.os.path.isfile(inherit_path).AndReturn(False)
717 presubmit.os.path.isfile(presubmit_path).AndReturn(True) 722 presubmit.os.path.isfile(presubmit_path).AndReturn(True)
718 presubmit.os.path.isfile(haspresubmit_path).AndReturn(True) 723 presubmit.os.path.isfile(haspresubmit_path).AndReturn(True)
719 presubmit.gclient_utils.FileRead(presubmit_path, 'rU' 724 presubmit.gclient_utils.FileRead(presubmit_path, 'rU'
720 ).AndReturn(self.presubmit_text) 725 ).AndReturn(self.presubmit_text)
721 presubmit.gclient_utils.FileRead(haspresubmit_path, 'rU' 726 presubmit.gclient_utils.FileRead(haspresubmit_path, 'rU'
722 ).AndReturn(self.presubmit_text) 727 ).AndReturn(self.presubmit_text)
723 presubmit.random.randint(0, 4).AndReturn(1) 728 presubmit.random.randint(0, 4).AndReturn(1)
724 presubmit.random.randint(0, 4).AndReturn(1) 729 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([])
725 self.mox.ReplayAll() 732 self.mox.ReplayAll()
726 733
727 input_buf = StringIO.StringIO('n\n') # say no to the warning 734 input_buf = StringIO.StringIO('n\n') # say no to the warning
728 change = presubmit.Change( 735 change = presubmit.Change(
729 'mychange', 736 'mychange',
730 '\n'.join(description_lines), 737 '\n'.join(description_lines),
731 self.fake_root_dir, 738 self.fake_root_dir,
732 files, 739 files,
733 0, 740 0,
734 0, 741 0,
(...skipping 26 matching lines...) Expand all
761 inherit_path = presubmit.os.path.join(self.fake_root_dir, 768 inherit_path = presubmit.os.path.join(self.fake_root_dir,
762 self._INHERIT_SETTINGS) 769 self._INHERIT_SETTINGS)
763 presubmit.os.path.isfile(inherit_path).AndReturn(False) 770 presubmit.os.path.isfile(inherit_path).AndReturn(False)
764 presubmit.os.path.isfile(presubmit_path).AndReturn(True) 771 presubmit.os.path.isfile(presubmit_path).AndReturn(True)
765 presubmit.os.path.isfile(haspresubmit_path).AndReturn(True) 772 presubmit.os.path.isfile(haspresubmit_path).AndReturn(True)
766 presubmit.gclient_utils.FileRead(presubmit_path, 'rU' 773 presubmit.gclient_utils.FileRead(presubmit_path, 'rU'
767 ).AndReturn(self.presubmit_text) 774 ).AndReturn(self.presubmit_text)
768 presubmit.gclient_utils.FileRead(haspresubmit_path, 'rU').AndReturn( 775 presubmit.gclient_utils.FileRead(haspresubmit_path, 'rU').AndReturn(
769 self.presubmit_text) 776 self.presubmit_text)
770 presubmit.random.randint(0, 4).AndReturn(1) 777 presubmit.random.randint(0, 4).AndReturn(1)
778 presubmit.os.walk(self.fake_root_dir).AndReturn([])
771 self.mox.ReplayAll() 779 self.mox.ReplayAll()
772 780
773 change = presubmit.Change( 781 change = presubmit.Change(
774 'mychange', 782 'mychange',
775 '\n'.join(description_lines), 783 '\n'.join(description_lines),
776 self.fake_root_dir, 784 self.fake_root_dir,
777 files, 785 files,
778 0, 786 0,
779 0, 787 0,
780 None) 788 None)
(...skipping 21 matching lines...) Expand all
802 """ 810 """
803 inherit_path = presubmit.os.path.join(self.fake_root_dir, 811 inherit_path = presubmit.os.path.join(self.fake_root_dir,
804 self._INHERIT_SETTINGS) 812 self._INHERIT_SETTINGS)
805 presubmit.os.path.isfile(inherit_path).AndReturn(False) 813 presubmit.os.path.isfile(inherit_path).AndReturn(False)
806 presubmit.os.path.isfile(join(self.fake_root_dir, 'PRESUBMIT.py') 814 presubmit.os.path.isfile(join(self.fake_root_dir, 'PRESUBMIT.py')
807 ).AndReturn(False) 815 ).AndReturn(False)
808 presubmit.os.path.isfile(join(self.fake_root_dir, 816 presubmit.os.path.isfile(join(self.fake_root_dir,
809 'haspresubmit', 817 'haspresubmit',
810 'PRESUBMIT.py')).AndReturn(False) 818 'PRESUBMIT.py')).AndReturn(False)
811 presubmit.random.randint(0, 4).AndReturn(0) 819 presubmit.random.randint(0, 4).AndReturn(0)
820 presubmit.os.walk(self.fake_root_dir).AndReturn([])
812 self.mox.ReplayAll() 821 self.mox.ReplayAll()
813 822
814 input_buf = StringIO.StringIO('y\n') 823 input_buf = StringIO.StringIO('y\n')
815 # Always fail. 824 # Always fail.
816 change = presubmit.Change( 825 change = presubmit.Change(
817 'mychange', 826 'mychange',
818 '\n'.join(description_lines), 827 '\n'.join(description_lines),
819 self.fake_root_dir, 828 self.fake_root_dir,
820 files, 829 files,
821 0, 830 0,
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
884 return [output_api.PresubmitError('Tag parsing failed. 5 ' + 893 return [output_api.PresubmitError('Tag parsing failed. 5 ' +
885 input_api.change.FullDescriptionText())] 894 input_api.change.FullDescriptionText())]
886 return [output_api.PresubmitNotifyResult(input_api.change.tags['STORY'])] 895 return [output_api.PresubmitNotifyResult(input_api.change.tags['STORY'])]
887 def CheckChangeOnCommit(input_api, output_api): 896 def CheckChangeOnCommit(input_api, output_api):
888 raise Exception("Test error") 897 raise Exception("Test error")
889 """ 898 """
890 presubmit.random.randint(0, 4).AndReturn(1) 899 presubmit.random.randint(0, 4).AndReturn(1)
891 inherit_path = presubmit.os.path.join(self.fake_root_dir, 900 inherit_path = presubmit.os.path.join(self.fake_root_dir,
892 self._INHERIT_SETTINGS) 901 self._INHERIT_SETTINGS)
893 presubmit.os.path.isfile(inherit_path).AndReturn(False) 902 presubmit.os.path.isfile(inherit_path).AndReturn(False)
903 presubmit.os.walk(self.fake_root_dir).AndReturn([])
894 self.mox.ReplayAll() 904 self.mox.ReplayAll()
895 905
896 output = StringIO.StringIO() 906 output = StringIO.StringIO()
897 input_buf = StringIO.StringIO('y\n') 907 input_buf = StringIO.StringIO('y\n')
898 change = presubmit.Change( 908 change = presubmit.Change(
899 'foo', 909 'foo',
900 'Blah Blah\n\nSTORY=http://tracker.com/42\nBUG=boo\n', 910 'Blah Blah\n\nSTORY=http://tracker.com/42\nBUG=boo\n',
901 self.fake_root_dir, 911 self.fake_root_dir,
902 None, 912 None,
903 0, 913 0,
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after
1168 'presubmit_unittest.py: error: For unversioned directory, <files> is ' 1178 'presubmit_unittest.py: error: For unversioned directory, <files> is '
1169 'not optional.\n') 1179 'not optional.\n')
1170 self.mox.ReplayAll() 1180 self.mox.ReplayAll()
1171 1181
1172 try: 1182 try:
1173 presubmit.Main(['--root', self.fake_root_dir]) 1183 presubmit.Main(['--root', self.fake_root_dir])
1174 self.fail() 1184 self.fail()
1175 except SystemExit, e: 1185 except SystemExit, e:
1176 self.assertEquals(2, e.code) 1186 self.assertEquals(2, e.code)
1177 1187
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
1178 1270
1179 class InputApiUnittest(PresubmitTestsBase): 1271 class InputApiUnittest(PresubmitTestsBase):
1180 """Tests presubmit.InputApi.""" 1272 """Tests presubmit.InputApi."""
1181 def testMembersChanged(self): 1273 def testMembersChanged(self):
1182 self.mox.ReplayAll() 1274 self.mox.ReplayAll()
1183 members = [ 1275 members = [
1184 'AbsoluteLocalPaths', 'AffectedFiles', 'AffectedSourceFiles', 1276 'AbsoluteLocalPaths', 'AffectedFiles', 'AffectedSourceFiles',
1185 'AffectedTextFiles', 1277 'AffectedTextFiles',
1186 'DEFAULT_BLACK_LIST', 'DEFAULT_WHITE_LIST', 1278 'DEFAULT_BLACK_LIST', 'DEFAULT_WHITE_LIST',
1187 'DepotToLocalPath', 'FilterSourceFile', 'LocalPaths', 1279 'DepotToLocalPath', 'FilterSourceFile', 'LocalPaths',
(...skipping 1731 matching lines...) Expand 10 before | Expand all | Expand 10 after
2919 owners_check=False) 3011 owners_check=False)
2920 self.assertEqual(1, len(results)) 3012 self.assertEqual(1, len(results))
2921 self.assertEqual( 3013 self.assertEqual(
2922 'Found line ending with white spaces in:', results[0]._message) 3014 'Found line ending with white spaces in:', results[0]._message)
2923 self.checkstdout('') 3015 self.checkstdout('')
2924 3016
2925 3017
2926 if __name__ == '__main__': 3018 if __name__ == '__main__':
2927 import unittest 3019 import unittest
2928 unittest.main() 3020 unittest.main()
OLDNEW
« no previous file with comments | « presubmit_support.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698