OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/env python2.5 |
2 # Copyright (c) 2010 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 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 is too confused. | 8 # pylint is too confused. |
9 # pylint: disable=E1101,E1103,W0212,W0403 | 9 # pylint: disable=E1101,E1103,W0212,W0403 |
10 | 10 |
11 import StringIO | 11 import StringIO |
| 12 import sys |
12 | 13 |
13 # Fixes include path. | 14 # Fixes include path. |
14 from super_mox import mox, SuperMoxTestBase | 15 from super_mox import mox, SuperMoxTestBase |
15 | 16 |
16 import owners | 17 import owners |
17 import presubmit_support as presubmit | 18 import presubmit_support as presubmit |
18 # Shortcut. | 19 # Shortcut. |
19 presubmit_canned_checks = presubmit.presubmit_canned_checks | 20 presubmit_canned_checks = presubmit.presubmit_canned_checks |
20 | 21 |
21 | 22 |
(...skipping 697 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
719 def testMembersChanged(self): | 720 def testMembersChanged(self): |
720 self.mox.ReplayAll() | 721 self.mox.ReplayAll() |
721 members = [ | 722 members = [ |
722 'AbsoluteLocalPaths', 'AffectedFiles', 'AffectedSourceFiles', | 723 'AbsoluteLocalPaths', 'AffectedFiles', 'AffectedSourceFiles', |
723 'AffectedTextFiles', | 724 'AffectedTextFiles', |
724 'DEFAULT_BLACK_LIST', 'DEFAULT_WHITE_LIST', | 725 'DEFAULT_BLACK_LIST', 'DEFAULT_WHITE_LIST', |
725 'DepotToLocalPath', 'FilterSourceFile', 'LocalPaths', | 726 'DepotToLocalPath', 'FilterSourceFile', 'LocalPaths', |
726 'LocalToDepotPath', | 727 'LocalToDepotPath', |
727 'PresubmitLocalPath', 'ReadFile', 'RightHandSideLines', 'ServerPaths', | 728 'PresubmitLocalPath', 'ReadFile', 'RightHandSideLines', 'ServerPaths', |
728 'basename', 'cPickle', 'cStringIO', 'canned_checks', 'change', 'environ', | 729 'basename', 'cPickle', 'cStringIO', 'canned_checks', 'change', 'environ', |
729 'host_url', 'is_committing', 'json', 'marshal', 'os_path', | 730 'host_url', 'is_committing', 'json', 'marshal', 'os_listdir', 'os_walk', |
730 'owners_db', 'pickle', 'platform', 'python_executable', 're', | 731 'os_path', 'owners_db', 'pickle', 'platform', 'python_executable', 're', |
731 'subprocess', 'tbr', 'tempfile', 'time', 'traceback', 'unittest', | 732 'subprocess', 'tbr', 'tempfile', 'time', 'traceback', 'unittest', |
732 'urllib2', 'version', | 733 'urllib2', 'version', |
733 ] | 734 ] |
734 # If this test fails, you should add the relevant test. | 735 # If this test fails, you should add the relevant test. |
735 self.compareMembers(presubmit.InputApi(self.fake_change, './.', False, | 736 self.compareMembers(presubmit.InputApi(self.fake_change, './.', False, |
736 False, None), | 737 False, None), |
737 members) | 738 members) |
738 | 739 |
739 def testDepotToLocalPath(self): | 740 def testDepotToLocalPath(self): |
740 presubmit.scm.SVN.CaptureInfo('svn://foo/smurf').AndReturn( | 741 presubmit.scm.SVN.CaptureInfo('svn://foo/smurf').AndReturn( |
(...skipping 489 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1230 | 1231 |
1231 class CannedChecksUnittest(PresubmitTestsBase): | 1232 class CannedChecksUnittest(PresubmitTestsBase): |
1232 """Tests presubmit_canned_checks.py.""" | 1233 """Tests presubmit_canned_checks.py.""" |
1233 | 1234 |
1234 def setUp(self): | 1235 def setUp(self): |
1235 PresubmitTestsBase.setUp(self) | 1236 PresubmitTestsBase.setUp(self) |
1236 | 1237 |
1237 def MockInputApi(self, change, committing): | 1238 def MockInputApi(self, change, committing): |
1238 input_api = self.mox.CreateMock(presubmit.InputApi) | 1239 input_api = self.mox.CreateMock(presubmit.InputApi) |
1239 input_api.cStringIO = presubmit.cStringIO | 1240 input_api.cStringIO = presubmit.cStringIO |
| 1241 input_api.os_listdir = self.mox.CreateMockAnything() |
| 1242 input_api.os_walk = self.mox.CreateMockAnything() |
1240 input_api.os_path = presubmit.os.path | 1243 input_api.os_path = presubmit.os.path |
1241 input_api.re = presubmit.re | 1244 input_api.re = presubmit.re |
1242 input_api.traceback = presubmit.traceback | 1245 input_api.traceback = presubmit.traceback |
1243 input_api.urllib2 = self.mox.CreateMock(presubmit.urllib2) | 1246 input_api.urllib2 = self.mox.CreateMock(presubmit.urllib2) |
1244 input_api.unittest = unittest | 1247 input_api.unittest = unittest |
1245 input_api.subprocess = self.mox.CreateMock(presubmit.subprocess) | 1248 input_api.subprocess = self.mox.CreateMock(presubmit.subprocess) |
1246 | 1249 |
1247 input_api.change = change | 1250 input_api.change = change |
1248 input_api.host_url = 'http://localhost' | 1251 input_api.host_url = 'http://localhost' |
1249 input_api.is_committing = committing | 1252 input_api.is_committing = committing |
1250 input_api.tbr = False | 1253 input_api.tbr = False |
1251 input_api.python_executable = 'pyyyyython' | 1254 input_api.python_executable = 'pyyyyython' |
| 1255 input_api.platform = sys.platform |
1252 return input_api | 1256 return input_api |
1253 | 1257 |
1254 def testMembersChanged(self): | 1258 def testMembersChanged(self): |
1255 self.mox.ReplayAll() | 1259 self.mox.ReplayAll() |
1256 members = [ | 1260 members = [ |
1257 'CheckBuildbotPendingBuilds', | 1261 'CheckBuildbotPendingBuilds', |
1258 'CheckChangeHasBugField', 'CheckChangeHasDescription', | 1262 'CheckChangeHasBugField', 'CheckChangeHasDescription', |
1259 'CheckChangeHasNoStrayWhitespace', | 1263 'CheckChangeHasNoStrayWhitespace', |
1260 'CheckChangeHasOnlyOneEol', 'CheckChangeHasNoCR', | 1264 'CheckChangeHasOnlyOneEol', 'CheckChangeHasNoCR', |
1261 'CheckChangeHasNoCrAndHasOnlyOneEol', 'CheckChangeHasNoTabs', | 1265 'CheckChangeHasNoCrAndHasOnlyOneEol', 'CheckChangeHasNoTabs', |
1262 'CheckChangeTodoHasOwner', | 1266 'CheckChangeTodoHasOwner', |
1263 'CheckChangeHasQaField', 'CheckChangeHasTestedField', | 1267 'CheckChangeHasQaField', 'CheckChangeHasTestedField', |
1264 'CheckChangeHasTestField', | 1268 'CheckChangeHasTestField', |
1265 'CheckChangeLintsClean', | 1269 'CheckChangeLintsClean', |
1266 'CheckChangeSvnEolStyle', | 1270 'CheckChangeSvnEolStyle', |
1267 'CheckDoNotSubmit', | 1271 'CheckDoNotSubmit', |
1268 'CheckDoNotSubmitInDescription', 'CheckDoNotSubmitInFiles', | 1272 'CheckDoNotSubmitInDescription', 'CheckDoNotSubmitInFiles', |
1269 'CheckLongLines', 'CheckTreeIsOpen', 'PanProjectChecks', | 1273 'CheckLongLines', 'CheckTreeIsOpen', 'PanProjectChecks', |
1270 'CheckLicense', | 1274 'CheckLicense', |
1271 'CheckOwners', | 1275 'CheckOwners', |
1272 'CheckRietveldTryJobExecution', | 1276 'CheckRietveldTryJobExecution', |
1273 'CheckSvnModifiedDirectories', | 1277 'CheckSvnModifiedDirectories', |
1274 'CheckSvnForCommonMimeTypes', 'CheckSvnProperty', | 1278 'CheckSvnForCommonMimeTypes', 'CheckSvnProperty', |
1275 'RunPythonUnitTests', 'RunPylint', | 1279 'RunPythonUnitTests', 'RunPylint', |
| 1280 'RunUnitTests', 'RunUnitTestsInDirectory', |
1276 ] | 1281 ] |
1277 # If this test fails, you should add the relevant test. | 1282 # If this test fails, you should add the relevant test. |
1278 self.compareMembers(presubmit_canned_checks, members) | 1283 self.compareMembers(presubmit_canned_checks, members) |
1279 | 1284 |
1280 def DescriptionTest(self, check, description1, description2, error_type, | 1285 def DescriptionTest(self, check, description1, description2, error_type, |
1281 committing): | 1286 committing): |
1282 change1 = presubmit.Change('foo1', description1, self.fake_root_dir, None, | 1287 change1 = presubmit.Change('foo1', description1, self.fake_root_dir, None, |
1283 0, 0) | 1288 0, 0) |
1284 input_api1 = self.MockInputApi(change1, committing) | 1289 input_api1 = self.MockInputApi(change1, committing) |
1285 change2 = presubmit.Change('foo2', description2, self.fake_root_dir, None, | 1290 change2 = presubmit.Change('foo2', description2, self.fake_root_dir, None, |
(...skipping 717 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2003 self.assertEqual(results, []) | 2008 self.assertEqual(results, []) |
2004 | 2009 |
2005 def testCannedCheckOwners_WithoutOwnerLGTM(self): | 2010 def testCannedCheckOwners_WithoutOwnerLGTM(self): |
2006 self.AssertOwnersWorks(uncovered_files=set(['foo.cc']), | 2011 self.AssertOwnersWorks(uncovered_files=set(['foo.cc']), |
2007 expected_output='Missing LGTM from an OWNER for: foo.cc\n') | 2012 expected_output='Missing LGTM from an OWNER for: foo.cc\n') |
2008 | 2013 |
2009 def testCannedCheckOwners_WithLGTMs(self): | 2014 def testCannedCheckOwners_WithLGTMs(self): |
2010 self.AssertOwnersWorks(approvers=set(['ben@example.com']), | 2015 self.AssertOwnersWorks(approvers=set(['ben@example.com']), |
2011 uncovered_files=set()) | 2016 uncovered_files=set()) |
2012 | 2017 |
| 2018 def testCannedRunUnitTests(self): |
| 2019 change = presubmit.Change( |
| 2020 'foo1', 'description1', self.fake_root_dir, None, 0, 0) |
| 2021 input_api = self.MockInputApi(change, False) |
| 2022 unit_tests = ['allo', 'bar.py'] |
| 2023 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) |
| 2024 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) |
| 2025 proc1 = self.mox.CreateMockAnything() |
| 2026 input_api.subprocess.Popen(['allo'], cwd=self.fake_root_dir, |
| 2027 stderr=None, stdout=None).AndReturn(proc1) |
| 2028 proc1.returncode = 0 |
| 2029 proc1.communicate().AndReturn(['baz', None]) |
| 2030 proc2 = self.mox.CreateMockAnything() |
| 2031 input_api.subprocess.Popen(['bar.py'], cwd=self.fake_root_dir, |
| 2032 stderr=None, stdout=None).AndReturn(proc2) |
| 2033 proc2.returncode = 1 |
| 2034 proc2.communicate().AndReturn(['bouz', None]) |
| 2035 |
| 2036 self.mox.ReplayAll() |
| 2037 results = presubmit_canned_checks.RunUnitTests( |
| 2038 input_api, |
| 2039 presubmit.OutputApi, |
| 2040 unit_tests, |
| 2041 verbose=True) |
| 2042 self.assertEqual(1, len(results)) |
| 2043 self.assertEqual( |
| 2044 presubmit.OutputApi.PresubmitPromptWarning, results[0].__class__) |
| 2045 self.checkstdout('Running allo\nRunning bar.py\n') |
| 2046 |
| 2047 def testCannedRunUnitTestsInDirectory(self): |
| 2048 change = presubmit.Change( |
| 2049 'foo1', 'description1', self.fake_root_dir, None, 0, 0) |
| 2050 input_api = self.MockInputApi(change, False) |
| 2051 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) |
| 2052 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) |
| 2053 path = presubmit.os.path.join(self.fake_root_dir, 'random_directory') |
| 2054 input_api.os_listdir(path).AndReturn(['.', '..', 'a', 'b', 'c']) |
| 2055 input_api.os_path.isfile = lambda x: not x.endswith('.') |
| 2056 proc1 = self.mox.CreateMockAnything() |
| 2057 input_api.subprocess.Popen(['random_directory/b'], cwd=self.fake_root_dir, |
| 2058 stderr=None, stdout=None).AndReturn(proc1) |
| 2059 proc1.returncode = 0 |
| 2060 proc1.communicate().AndReturn(['baz', None]) |
| 2061 |
| 2062 self.mox.ReplayAll() |
| 2063 results = presubmit_canned_checks.RunUnitTestsInDirectory( |
| 2064 input_api, |
| 2065 presubmit.OutputApi, |
| 2066 'random_directory', |
| 2067 whitelist=['^a$', '^b$'], |
| 2068 blacklist=['a'], |
| 2069 verbose=True) |
| 2070 self.assertEqual(results, []) |
| 2071 self.checkstdout('Running random_directory/b\n') |
2013 | 2072 |
2014 | 2073 |
2015 if __name__ == '__main__': | 2074 if __name__ == '__main__': |
2016 import unittest | 2075 import unittest |
2017 unittest.main() | 2076 unittest.main() |
OLD | NEW |