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 logging | 10 import logging |
11 import os | 11 import os |
12 import StringIO | 12 import StringIO |
13 import subprocess | |
14 import sys | 13 import sys |
15 import time | 14 import time |
| 15 import unittest |
16 | 16 |
17 _ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | 17 _ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
18 sys.path.insert(0, _ROOT) | 18 sys.path.insert(0, _ROOT) |
19 | 19 |
20 from testing_support.super_mox import mox, SuperMoxTestBase | 20 from testing_support.super_mox import mox, SuperMoxTestBase |
21 | 21 |
22 import owners | 22 import owners |
| 23 import subprocess2 as subprocess |
23 import presubmit_support as presubmit | 24 import presubmit_support as presubmit |
24 import rietveld | 25 import rietveld |
25 | 26 |
26 # Shortcut. | 27 # Shortcut. |
27 presubmit_canned_checks = presubmit.presubmit_canned_checks | 28 presubmit_canned_checks = presubmit.presubmit_canned_checks |
28 | 29 |
29 | 30 |
30 # Access to a protected member XXX of a client class | 31 # Access to a protected member XXX of a client class |
31 # pylint: disable=W0212 | 32 # pylint: disable=W0212 |
32 | 33 |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 | 151 |
151 class PresubmitUnittest(PresubmitTestsBase): | 152 class PresubmitUnittest(PresubmitTestsBase): |
152 """General presubmit_support.py tests (excluding InputApi and OutputApi).""" | 153 """General presubmit_support.py tests (excluding InputApi and OutputApi).""" |
153 | 154 |
154 _INHERIT_SETTINGS = 'inherit-review-settings-ok' | 155 _INHERIT_SETTINGS = 'inherit-review-settings-ok' |
155 | 156 |
156 def testMembersChanged(self): | 157 def testMembersChanged(self): |
157 self.mox.ReplayAll() | 158 self.mox.ReplayAll() |
158 members = [ | 159 members = [ |
159 'AffectedFile', 'Change', 'DoGetTrySlaves', 'DoPresubmitChecks', | 160 'AffectedFile', 'Change', 'DoGetTrySlaves', 'DoPresubmitChecks', |
160 'GetTrySlavesExecuter', 'GitAffectedFile', | 161 'GetTrySlavesExecuter', 'GitAffectedFile', 'CallCommand', 'CommandData', |
161 'GitChange', 'InputApi', 'ListRelevantPresubmitFiles', 'Main', | 162 'GitChange', 'InputApi', 'ListRelevantPresubmitFiles', 'Main', |
162 'NonexistantCannedCheckFilter', 'OutputApi', 'ParseFiles', | 163 'NonexistantCannedCheckFilter', 'OutputApi', 'ParseFiles', |
163 'PresubmitFailure', 'PresubmitExecuter', 'PresubmitOutput', 'ScanSubDirs', | 164 'PresubmitFailure', 'PresubmitExecuter', 'PresubmitOutput', 'ScanSubDirs', |
164 'SvnAffectedFile', 'SvnChange', 'cPickle', 'cpplint', 'cStringIO', | 165 'SvnAffectedFile', 'SvnChange', 'cPickle', 'cpplint', 'cStringIO', |
165 'contextlib', 'canned_check_filter', 'fix_encoding', 'fnmatch', | 166 'contextlib', 'canned_check_filter', 'fix_encoding', 'fnmatch', |
166 'gclient_utils', 'glob', 'inspect', 'json', 'load_files', 'logging', | 167 'gclient_utils', 'glob', 'inspect', 'json', 'load_files', 'logging', |
167 'marshal', 'normpath', 'optparse', 'os', 'owners', 'pickle', | 168 'marshal', 'normpath', 'optparse', 'os', 'owners', 'pickle', |
168 'presubmit_canned_checks', 'random', 're', 'rietveld', 'scm', | 169 'presubmit_canned_checks', 'random', 're', 'rietveld', 'scm', |
169 'subprocess', 'sys', 'tempfile', 'time', 'traceback', 'types', 'unittest', | 170 'subprocess', 'sys', 'tempfile', 'time', 'traceback', 'types', 'unittest', |
170 'urllib2', 'warn', | 171 'urllib2', 'warn', 'collections', 'multiprocessing', |
171 ] | 172 ] |
172 # If this test fails, you should add the relevant test. | 173 # If this test fails, you should add the relevant test. |
173 self.compareMembers(presubmit, members) | 174 self.compareMembers(presubmit, members) |
174 | 175 |
175 def testCannedCheckFilter(self): | 176 def testCannedCheckFilter(self): |
176 canned = presubmit.presubmit_canned_checks | 177 canned = presubmit.presubmit_canned_checks |
177 orig = canned.CheckOwners | 178 orig = canned.CheckOwners |
178 with presubmit.canned_check_filter(['CheckOwners']): | 179 with presubmit.canned_check_filter(['CheckOwners']): |
179 self.assertNotEqual(canned.CheckOwners, orig) | 180 self.assertNotEqual(canned.CheckOwners, orig) |
180 self.assertEqual(canned.CheckOwners(None, None), []) | 181 self.assertEqual(canned.CheckOwners(None, None), []) |
(...skipping 693 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
874 | 875 |
875 class InputApiUnittest(PresubmitTestsBase): | 876 class InputApiUnittest(PresubmitTestsBase): |
876 """Tests presubmit.InputApi.""" | 877 """Tests presubmit.InputApi.""" |
877 def testMembersChanged(self): | 878 def testMembersChanged(self): |
878 self.mox.ReplayAll() | 879 self.mox.ReplayAll() |
879 members = [ | 880 members = [ |
880 'AbsoluteLocalPaths', 'AffectedFiles', 'AffectedSourceFiles', | 881 'AbsoluteLocalPaths', 'AffectedFiles', 'AffectedSourceFiles', |
881 'AffectedTextFiles', | 882 'AffectedTextFiles', |
882 'DEFAULT_BLACK_LIST', 'DEFAULT_WHITE_LIST', | 883 'DEFAULT_BLACK_LIST', 'DEFAULT_WHITE_LIST', |
883 'DepotToLocalPath', 'FilterSourceFile', 'LocalPaths', | 884 'DepotToLocalPath', 'FilterSourceFile', 'LocalPaths', |
884 'LocalToDepotPath', | 885 'LocalToDepotPath', 'Command', 'RunTests', |
885 'PresubmitLocalPath', 'ReadFile', 'RightHandSideLines', 'ServerPaths', | 886 'PresubmitLocalPath', 'ReadFile', 'RightHandSideLines', 'ServerPaths', |
886 'basename', 'cPickle', 'cpplint', 'cStringIO', 'canned_checks', 'change', | 887 'basename', 'cPickle', 'cpplint', 'cStringIO', 'canned_checks', 'change', |
887 'environ', 'glob', 'host_url', 'is_committing', 'json', 'logging', | 888 'environ', 'glob', 'host_url', 'is_committing', 'json', 'logging', |
888 'marshal', 'os_listdir', 'os_walk', 'os_path', 'owners_db', 'pickle', | 889 'marshal', 'os_listdir', 'os_walk', 'os_path', 'owners_db', 'pickle', |
889 'platform', 'python_executable', 're', 'rietveld', 'subprocess', 'tbr', | 890 'platform', 'python_executable', 're', 'rietveld', 'subprocess', 'tbr', |
890 'tempfile', 'time', 'traceback', 'unittest', 'urllib2', 'version', | 891 'tempfile', 'time', 'traceback', 'unittest', 'urllib2', 'version', |
891 'verbose', | 892 'verbose', |
892 ] | 893 ] |
893 # If this test fails, you should add the relevant test. | 894 # If this test fails, you should add the relevant test. |
894 self.compareMembers( | 895 self.compareMembers( |
(...skipping 583 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1478 self.assertEquals('foo2', change.DescriptionText()) | 1479 self.assertEquals('foo2', change.DescriptionText()) |
1479 self.assertEquals('foo3', change.author_email) | 1480 self.assertEquals('foo3', change.author_email) |
1480 self.assertEquals('ro', change.DRU) | 1481 self.assertEquals('ro', change.DRU) |
1481 self.assertEquals(3, change.issue) | 1482 self.assertEquals(3, change.issue) |
1482 self.assertEquals(5, change.patchset) | 1483 self.assertEquals(5, change.patchset) |
1483 self.assertEquals(self.fake_root_dir, change.RepositoryRoot()) | 1484 self.assertEquals(self.fake_root_dir, change.RepositoryRoot()) |
1484 self.assertEquals(1, len(change.AffectedFiles(include_dirs=True))) | 1485 self.assertEquals(1, len(change.AffectedFiles(include_dirs=True))) |
1485 self.assertEquals('Y', change.AffectedFiles(include_dirs=True)[0].Action()) | 1486 self.assertEquals('Y', change.AffectedFiles(include_dirs=True)[0].Action()) |
1486 | 1487 |
1487 | 1488 |
| 1489 def CommHelper(input_api, cmd, ret=None, **kwargs): |
| 1490 ret = ret or (('', None), 0) |
| 1491 input_api.subprocess.communicate( |
| 1492 cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, **kwargs |
| 1493 ).AndReturn(ret) |
| 1494 |
| 1495 |
1488 class CannedChecksUnittest(PresubmitTestsBase): | 1496 class CannedChecksUnittest(PresubmitTestsBase): |
1489 """Tests presubmit_canned_checks.py.""" | 1497 """Tests presubmit_canned_checks.py.""" |
1490 | 1498 |
1491 def MockInputApi(self, change, committing): | 1499 def MockInputApi(self, change, committing): |
1492 # pylint: disable=R0201 | 1500 # pylint: disable=R0201 |
1493 input_api = self.mox.CreateMock(presubmit.InputApi) | 1501 input_api = self.mox.CreateMock(presubmit.InputApi) |
1494 input_api.cStringIO = presubmit.cStringIO | 1502 input_api.cStringIO = presubmit.cStringIO |
1495 input_api.json = presubmit.json | 1503 input_api.json = presubmit.json |
1496 input_api.logging = logging | 1504 input_api.logging = logging |
1497 input_api.os_listdir = self.mox.CreateMockAnything() | 1505 input_api.os_listdir = self.mox.CreateMockAnything() |
1498 input_api.os_walk = self.mox.CreateMockAnything() | 1506 input_api.os_walk = self.mox.CreateMockAnything() |
1499 input_api.os_path = presubmit.os.path | 1507 input_api.os_path = presubmit.os.path |
1500 input_api.re = presubmit.re | 1508 input_api.re = presubmit.re |
1501 input_api.rietveld = self.mox.CreateMock(rietveld.Rietveld) | 1509 input_api.rietveld = self.mox.CreateMock(rietveld.Rietveld) |
1502 input_api.traceback = presubmit.traceback | 1510 input_api.traceback = presubmit.traceback |
1503 input_api.urllib2 = self.mox.CreateMock(presubmit.urllib2) | 1511 input_api.urllib2 = self.mox.CreateMock(presubmit.urllib2) |
1504 input_api.unittest = unittest | 1512 input_api.unittest = unittest |
1505 input_api.subprocess = self.mox.CreateMock(presubmit.subprocess) | 1513 input_api.subprocess = self.mox.CreateMock(subprocess) |
| 1514 presubmit.subprocess = input_api.subprocess |
1506 class fake_CalledProcessError(Exception): | 1515 class fake_CalledProcessError(Exception): |
1507 def __str__(self): | 1516 def __str__(self): |
1508 return 'foo' | 1517 return 'foo' |
1509 input_api.subprocess.CalledProcessError = fake_CalledProcessError | 1518 input_api.subprocess.CalledProcessError = fake_CalledProcessError |
1510 input_api.verbose = False | 1519 input_api.verbose = False |
1511 | 1520 |
1512 input_api.change = change | 1521 input_api.change = change |
1513 input_api.host_url = 'http://localhost' | 1522 input_api.host_url = 'http://localhost' |
1514 input_api.is_committing = committing | 1523 input_api.is_committing = committing |
1515 input_api.tbr = False | 1524 input_api.tbr = False |
1516 input_api.python_executable = 'pyyyyython' | 1525 input_api.python_executable = 'pyyyyython' |
1517 input_api.platform = sys.platform | 1526 input_api.platform = sys.platform |
1518 input_api.time = time | 1527 input_api.time = time |
1519 input_api.canned_checks = presubmit_canned_checks | 1528 input_api.canned_checks = presubmit_canned_checks |
| 1529 input_api.Command = presubmit.CommandData |
| 1530 input_api.RunTests = presubmit.InputApi.RunTests |
1520 return input_api | 1531 return input_api |
1521 | 1532 |
1522 def testMembersChanged(self): | 1533 def testMembersChanged(self): |
1523 self.mox.ReplayAll() | 1534 self.mox.ReplayAll() |
1524 members = [ | 1535 members = [ |
1525 'CheckBuildbotPendingBuilds', | 1536 'CheckBuildbotPendingBuilds', |
1526 'CheckChangeHasBugField', 'CheckChangeHasDescription', | 1537 'CheckChangeHasBugField', 'CheckChangeHasDescription', |
1527 'CheckChangeHasNoStrayWhitespace', | 1538 'CheckChangeHasNoStrayWhitespace', |
1528 'CheckChangeHasOnlyOneEol', 'CheckChangeHasNoCR', | 1539 'CheckChangeHasOnlyOneEol', 'CheckChangeHasNoCR', |
1529 'CheckChangeHasNoCrAndHasOnlyOneEol', 'CheckChangeHasNoTabs', | 1540 'CheckChangeHasNoCrAndHasOnlyOneEol', 'CheckChangeHasNoTabs', |
1530 'CheckChangeTodoHasOwner', | 1541 'CheckChangeTodoHasOwner', |
1531 'CheckChangeHasQaField', 'CheckChangeHasTestedField', | 1542 'CheckChangeHasQaField', 'CheckChangeHasTestedField', |
1532 'CheckChangeHasTestField', | 1543 'CheckChangeHasTestField', |
1533 'CheckChangeLintsClean', | 1544 'CheckChangeLintsClean', |
1534 'CheckChangeSvnEolStyle', | 1545 'CheckChangeSvnEolStyle', |
1535 'CheckChangeWasUploaded', | 1546 'CheckChangeWasUploaded', |
1536 'CheckDoNotSubmit', | 1547 'CheckDoNotSubmit', |
1537 'CheckDoNotSubmitInDescription', 'CheckDoNotSubmitInFiles', | 1548 'CheckDoNotSubmitInDescription', 'CheckDoNotSubmitInFiles', |
1538 'CheckLongLines', 'CheckTreeIsOpen', 'PanProjectChecks', | 1549 'CheckLongLines', 'CheckTreeIsOpen', 'PanProjectChecks', |
1539 'CheckLicense', | 1550 'CheckLicense', |
1540 'CheckOwners', | 1551 'CheckOwners', |
1541 'CheckRietveldTryJobExecution', | 1552 'CheckRietveldTryJobExecution', |
1542 'CheckSingletonInHeaders', | 1553 'CheckSingletonInHeaders', |
1543 'CheckSvnModifiedDirectories', | 1554 'CheckSvnModifiedDirectories', |
1544 'CheckSvnForCommonMimeTypes', 'CheckSvnProperty', | 1555 'CheckSvnForCommonMimeTypes', 'CheckSvnProperty', |
1545 'RunPythonUnitTests', 'RunPylint', | 1556 'RunPythonUnitTests', 'RunPylint', |
1546 'RunUnitTests', 'RunUnitTestsInDirectory', | 1557 'RunUnitTests', 'RunUnitTestsInDirectory', |
| 1558 'GetPythonUnitTests', 'GetPylint', |
| 1559 'GetUnitTests', 'GetUnitTestsInDirectory', |
1547 ] | 1560 ] |
1548 # If this test fails, you should add the relevant test. | 1561 # If this test fails, you should add the relevant test. |
1549 self.compareMembers(presubmit_canned_checks, members) | 1562 self.compareMembers(presubmit_canned_checks, members) |
1550 | 1563 |
1551 def DescriptionTest(self, check, description1, description2, error_type, | 1564 def DescriptionTest(self, check, description1, description2, error_type, |
1552 committing): | 1565 committing): |
1553 change1 = presubmit.Change( | 1566 change1 = presubmit.Change( |
1554 'foo1', description1, self.fake_root_dir, None, 0, 0, None) | 1567 'foo1', description1, self.fake_root_dir, None, 0, 0, None) |
1555 input_api1 = self.MockInputApi(change1, committing) | 1568 input_api1 = self.MockInputApi(change1, committing) |
1556 change2 = presubmit.Change( | 1569 change2 = presubmit.Change( |
(...skipping 553 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2110 | 2123 |
2111 def testRunPythonUnitTestsNoTest(self): | 2124 def testRunPythonUnitTestsNoTest(self): |
2112 input_api = self.MockInputApi(None, False) | 2125 input_api = self.MockInputApi(None, False) |
2113 self.mox.ReplayAll() | 2126 self.mox.ReplayAll() |
2114 results = presubmit_canned_checks.RunPythonUnitTests( | 2127 results = presubmit_canned_checks.RunPythonUnitTests( |
2115 input_api, presubmit.OutputApi, []) | 2128 input_api, presubmit.OutputApi, []) |
2116 self.assertEquals(results, []) | 2129 self.assertEquals(results, []) |
2117 | 2130 |
2118 def testRunPythonUnitTestsNonExistentUpload(self): | 2131 def testRunPythonUnitTestsNonExistentUpload(self): |
2119 input_api = self.MockInputApi(None, False) | 2132 input_api = self.MockInputApi(None, False) |
2120 input_api.subprocess.check_output( | 2133 CommHelper(input_api, ['pyyyyython', '-m', '_non_existent_module'], |
2121 ['pyyyyython', '-m', '_non_existent_module'], cwd=None, env=None, | 2134 ret=(('foo', None), 1), cwd=None, env=None) |
2122 stderr=input_api.subprocess.STDOUT).AndRaise( | |
2123 input_api.subprocess.CalledProcessError()) | |
2124 self.mox.ReplayAll() | 2135 self.mox.ReplayAll() |
2125 | 2136 |
2126 results = presubmit_canned_checks.RunPythonUnitTests( | 2137 results = presubmit_canned_checks.RunPythonUnitTests( |
2127 input_api, presubmit.OutputApi, ['_non_existent_module']) | 2138 input_api, presubmit.OutputApi, ['_non_existent_module']) |
2128 self.assertEquals(len(results), 1) | 2139 self.assertEquals(len(results), 1) |
2129 self.assertEquals(results[0].__class__, | 2140 self.assertEquals(results[0].__class__, |
2130 presubmit.OutputApi.PresubmitNotifyResult) | 2141 presubmit.OutputApi.PresubmitNotifyResult) |
2131 | 2142 |
2132 def testRunPythonUnitTestsNonExistentCommitting(self): | 2143 def testRunPythonUnitTestsNonExistentCommitting(self): |
2133 input_api = self.MockInputApi(None, True) | 2144 input_api = self.MockInputApi(None, True) |
2134 input_api.subprocess.check_output( | 2145 CommHelper(input_api, ['pyyyyython', '-m', '_non_existent_module'], |
2135 ['pyyyyython', '-m', '_non_existent_module'], cwd=None, env=None, | 2146 ret=(('foo', None), 1), cwd=None, env=None) |
2136 stderr=input_api.subprocess.STDOUT).AndRaise( | |
2137 input_api.subprocess.CalledProcessError()) | |
2138 self.mox.ReplayAll() | 2147 self.mox.ReplayAll() |
2139 | 2148 |
2140 results = presubmit_canned_checks.RunPythonUnitTests( | 2149 results = presubmit_canned_checks.RunPythonUnitTests( |
2141 input_api, presubmit.OutputApi, ['_non_existent_module']) | 2150 input_api, presubmit.OutputApi, ['_non_existent_module']) |
2142 self.assertEquals(len(results), 1) | 2151 self.assertEquals(len(results), 1) |
2143 self.assertEquals(results[0].__class__, presubmit.OutputApi.PresubmitError) | 2152 self.assertEquals(results[0].__class__, presubmit.OutputApi.PresubmitError) |
2144 | 2153 |
2145 def testRunPythonUnitTestsFailureUpload(self): | 2154 def testRunPythonUnitTestsFailureUpload(self): |
2146 input_api = self.MockInputApi(None, False) | 2155 input_api = self.MockInputApi(None, False) |
2147 input_api.unittest = self.mox.CreateMock(unittest) | 2156 input_api.unittest = self.mox.CreateMock(unittest) |
2148 input_api.cStringIO = self.mox.CreateMock(presubmit.cStringIO) | 2157 input_api.cStringIO = self.mox.CreateMock(presubmit.cStringIO) |
2149 input_api.subprocess.check_output( | 2158 CommHelper(input_api, ['pyyyyython', '-m', 'test_module'], |
2150 ['pyyyyython', '-m', 'test_module'], cwd=None, env=None, | 2159 ret=(('foo', None), 1), cwd=None, env=None) |
2151 stderr=input_api.subprocess.STDOUT).AndRaise( | |
2152 input_api.subprocess.CalledProcessError()) | |
2153 self.mox.ReplayAll() | 2160 self.mox.ReplayAll() |
2154 | 2161 |
2155 results = presubmit_canned_checks.RunPythonUnitTests( | 2162 results = presubmit_canned_checks.RunPythonUnitTests( |
2156 input_api, presubmit.OutputApi, ['test_module']) | 2163 input_api, presubmit.OutputApi, ['test_module']) |
2157 self.assertEquals(len(results), 1) | 2164 self.assertEquals(len(results), 1) |
2158 self.assertEquals(results[0].__class__, | 2165 self.assertEquals(results[0].__class__, |
2159 presubmit.OutputApi.PresubmitNotifyResult) | 2166 presubmit.OutputApi.PresubmitNotifyResult) |
2160 self.assertEquals('test_module failed!\nfoo', results[0]._message) | 2167 self.assertEquals('test_module failed\nfoo', results[0]._message) |
2161 | 2168 |
2162 def testRunPythonUnitTestsFailureCommitting(self): | 2169 def testRunPythonUnitTestsFailureCommitting(self): |
2163 input_api = self.MockInputApi(None, True) | 2170 input_api = self.MockInputApi(None, True) |
2164 input_api.subprocess.check_output( | 2171 CommHelper(input_api, ['pyyyyython', '-m', 'test_module'], |
2165 ['pyyyyython', '-m', 'test_module'], cwd=None, env=None, | 2172 ret=(('foo', None), 1), cwd=None, env=None) |
2166 stderr=input_api.subprocess.STDOUT).AndRaise( | |
2167 input_api.subprocess.CalledProcessError()) | |
2168 self.mox.ReplayAll() | 2173 self.mox.ReplayAll() |
2169 | 2174 |
2170 results = presubmit_canned_checks.RunPythonUnitTests( | 2175 results = presubmit_canned_checks.RunPythonUnitTests( |
2171 input_api, presubmit.OutputApi, ['test_module']) | 2176 input_api, presubmit.OutputApi, ['test_module']) |
2172 self.assertEquals(len(results), 1) | 2177 self.assertEquals(len(results), 1) |
2173 self.assertEquals(results[0].__class__, presubmit.OutputApi.PresubmitError) | 2178 self.assertEquals(results[0].__class__, presubmit.OutputApi.PresubmitError) |
2174 self.assertEquals('test_module failed!\nfoo', results[0]._message) | 2179 self.assertEquals('test_module failed\nfoo', results[0]._message) |
2175 | 2180 |
2176 def testRunPythonUnitTestsSuccess(self): | 2181 def testRunPythonUnitTestsSuccess(self): |
2177 input_api = self.MockInputApi(None, False) | 2182 input_api = self.MockInputApi(None, False) |
2178 input_api.cStringIO = self.mox.CreateMock(presubmit.cStringIO) | 2183 input_api.cStringIO = self.mox.CreateMock(presubmit.cStringIO) |
2179 input_api.unittest = self.mox.CreateMock(unittest) | 2184 input_api.unittest = self.mox.CreateMock(unittest) |
2180 input_api.subprocess.check_output( | 2185 CommHelper(input_api, ['pyyyyython', '-m', 'test_module'], |
2181 ['pyyyyython', '-m', 'test_module'], cwd=None, env=None, | 2186 cwd=None, env=None) |
2182 stderr=input_api.subprocess.STDOUT) | |
2183 self.mox.ReplayAll() | 2187 self.mox.ReplayAll() |
2184 | 2188 |
2185 results = presubmit_canned_checks.RunPythonUnitTests( | 2189 results = presubmit_canned_checks.RunPythonUnitTests( |
2186 input_api, presubmit.OutputApi, ['test_module']) | 2190 input_api, presubmit.OutputApi, ['test_module']) |
2187 self.assertEquals(len(results), 0) | 2191 self.assertEquals(len(results), 0) |
2188 | 2192 |
2189 def testCannedRunPylint(self): | 2193 def testCannedRunPylint(self): |
2190 input_api = self.MockInputApi(None, True) | 2194 input_api = self.MockInputApi(None, True) |
2191 input_api.environ = self.mox.CreateMock(os.environ) | 2195 input_api.environ = self.mox.CreateMock(os.environ) |
2192 input_api.environ.copy().AndReturn({}) | 2196 input_api.environ.copy().AndReturn({}) |
2193 input_api.AffectedSourceFiles(mox.IgnoreArg()).AndReturn(True) | 2197 input_api.AffectedSourceFiles(mox.IgnoreArg()).AndReturn(True) |
2194 input_api.PresubmitLocalPath().AndReturn('/foo') | 2198 input_api.PresubmitLocalPath().AndReturn('/foo') |
2195 input_api.PresubmitLocalPath().AndReturn('/foo') | 2199 input_api.PresubmitLocalPath().AndReturn('/foo') |
2196 input_api.os_walk('/foo').AndReturn([('/foo', [], ['file1.py'])]) | 2200 input_api.os_walk('/foo').AndReturn([('/foo', [], ['file1.py'])]) |
2197 pylint = os.path.join(_ROOT, 'third_party', 'pylint.py') | 2201 pylint = os.path.join(_ROOT, 'third_party', 'pylint.py') |
2198 pylintrc = os.path.join(_ROOT, 'pylintrc') | 2202 pylintrc = os.path.join(_ROOT, 'pylintrc') |
2199 | 2203 |
2200 # Create a mock Popen object, and set up its expectations. | 2204 CommHelper(input_api, |
2201 child = self.mox.CreateMock(subprocess.Popen) | 2205 ['pyyyyython', pylint, '--args-on-stdin'], |
2202 child.stdin = self.mox.CreateMock(file) | 2206 env=mox.IgnoreArg(), stdin='file1.py\n--rcfile=%s' % pylintrc) |
2203 child.stdin.write('file1.py\n') | |
2204 child.stdin.write('--rcfile=%s\n' % pylintrc) | |
2205 child.stdin.close() | |
2206 child.communicate() | |
2207 child.returncode = 0 | |
2208 | |
2209 input_api.subprocess.Popen(['pyyyyython', pylint, '--args-on-stdin'], | |
2210 env=mox.IgnoreArg(), stdin=subprocess.PIPE).AndReturn(child) | |
2211 self.mox.ReplayAll() | 2207 self.mox.ReplayAll() |
2212 | 2208 |
2213 results = presubmit_canned_checks.RunPylint( | 2209 results = presubmit_canned_checks.RunPylint( |
2214 input_api, presubmit.OutputApi) | 2210 input_api, presubmit.OutputApi) |
2215 self.assertEquals([], results) | 2211 self.assertEquals([], results) |
2216 self.checkstdout('Running pylint on 1 files.\n') | 2212 self.checkstdout('') |
2217 | 2213 |
2218 def testCheckBuildbotPendingBuildsBad(self): | 2214 def testCheckBuildbotPendingBuildsBad(self): |
2219 input_api = self.MockInputApi(None, True) | 2215 input_api = self.MockInputApi(None, True) |
2220 connection = self.mox.CreateMockAnything() | 2216 connection = self.mox.CreateMockAnything() |
2221 input_api.urllib2.urlopen('uurl').AndReturn(connection) | 2217 input_api.urllib2.urlopen('uurl').AndReturn(connection) |
2222 connection.read().AndReturn('foo') | 2218 connection.read().AndReturn('foo') |
2223 connection.close() | 2219 connection.close() |
2224 self.mox.ReplayAll() | 2220 self.mox.ReplayAll() |
2225 | 2221 |
2226 results = presubmit_canned_checks.CheckBuildbotPendingBuilds( | 2222 results = presubmit_canned_checks.CheckBuildbotPendingBuilds( |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2448 uncovered_files=set()) | 2444 uncovered_files=set()) |
2449 | 2445 |
2450 def testCannedRunUnitTests(self): | 2446 def testCannedRunUnitTests(self): |
2451 change = presubmit.Change( | 2447 change = presubmit.Change( |
2452 'foo1', 'description1', self.fake_root_dir, None, 0, 0, None) | 2448 'foo1', 'description1', self.fake_root_dir, None, 0, 0, None) |
2453 input_api = self.MockInputApi(change, False) | 2449 input_api = self.MockInputApi(change, False) |
2454 input_api.verbose = True | 2450 input_api.verbose = True |
2455 unit_tests = ['allo', 'bar.py'] | 2451 unit_tests = ['allo', 'bar.py'] |
2456 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) | 2452 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) |
2457 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) | 2453 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) |
2458 input_api.subprocess.check_call( | 2454 CommHelper(input_api, ['allo', '--verbose'], cwd=self.fake_root_dir) |
2459 ['allo', '--verbose'], cwd=self.fake_root_dir) | |
2460 cmd = ['bar.py', '--verbose'] | 2455 cmd = ['bar.py', '--verbose'] |
2461 if input_api.platform == 'win32': | 2456 if input_api.platform == 'win32': |
2462 cmd.insert(0, input_api.python_executable) | 2457 cmd.insert(0, input_api.python_executable) |
2463 input_api.subprocess.check_call( | 2458 CommHelper(input_api, cmd, cwd=self.fake_root_dir, ret=(('', None), 1)) |
2464 cmd, cwd=self.fake_root_dir).AndRaise( | |
2465 input_api.subprocess.CalledProcessError()) | |
2466 | 2459 |
2467 self.mox.ReplayAll() | 2460 self.mox.ReplayAll() |
2468 results = presubmit_canned_checks.RunUnitTests( | 2461 results = presubmit_canned_checks.RunUnitTests( |
2469 input_api, | 2462 input_api, |
2470 presubmit.OutputApi, | 2463 presubmit.OutputApi, |
2471 unit_tests) | 2464 unit_tests) |
2472 self.assertEqual(1, len(results)) | 2465 self.assertEqual(1, len(results)) |
2473 self.assertEqual( | 2466 self.assertEqual( |
2474 presubmit.OutputApi.PresubmitPromptWarning, results[0].__class__) | 2467 presubmit.OutputApi.PresubmitPromptWarning, results[0].__class__) |
2475 self.checkstdout('Running allo\nRunning bar.py\n') | 2468 self.checkstdout('Running allo\nRunning bar.py\n') |
2476 | 2469 |
2477 def testCannedRunUnitTestsInDirectory(self): | 2470 def testCannedRunUnitTestsInDirectory(self): |
2478 change = presubmit.Change( | 2471 change = presubmit.Change( |
2479 'foo1', 'description1', self.fake_root_dir, None, 0, 0, None) | 2472 'foo1', 'description1', self.fake_root_dir, None, 0, 0, None) |
2480 input_api = self.MockInputApi(change, False) | 2473 input_api = self.MockInputApi(change, False) |
2481 input_api.verbose = True | 2474 input_api.verbose = True |
2482 input_api.logging = self.mox.CreateMock(logging) | 2475 input_api.logging = self.mox.CreateMock(logging) |
2483 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) | 2476 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) |
2484 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) | 2477 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) |
2485 path = presubmit.os.path.join(self.fake_root_dir, 'random_directory') | 2478 path = presubmit.os.path.join(self.fake_root_dir, 'random_directory') |
2486 input_api.os_listdir(path).AndReturn(['.', '..', 'a', 'b', 'c']) | 2479 input_api.os_listdir(path).AndReturn(['.', '..', 'a', 'b', 'c']) |
2487 input_api.os_path.isfile = lambda x: not x.endswith('.') | 2480 input_api.os_path.isfile = lambda x: not x.endswith('.') |
2488 input_api.subprocess.check_call( | 2481 CommHelper( |
| 2482 input_api, |
2489 [presubmit.os.path.join('random_directory', 'b'), '--verbose'], | 2483 [presubmit.os.path.join('random_directory', 'b'), '--verbose'], |
2490 cwd=self.fake_root_dir) | 2484 cwd=self.fake_root_dir) |
2491 input_api.logging.debug('Found 5 files, running 1') | 2485 input_api.logging.debug('Found 5 files, running 1') |
2492 | 2486 |
2493 self.mox.ReplayAll() | 2487 self.mox.ReplayAll() |
2494 results = presubmit_canned_checks.RunUnitTestsInDirectory( | 2488 results = presubmit_canned_checks.RunUnitTestsInDirectory( |
2495 input_api, | 2489 input_api, |
2496 presubmit.OutputApi, | 2490 presubmit.OutputApi, |
2497 'random_directory', | 2491 'random_directory', |
2498 whitelist=['^a$', '^b$'], | 2492 whitelist=['^a$', '^b$'], |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2537 owners_check=False) | 2531 owners_check=False) |
2538 self.assertEqual(1, len(results)) | 2532 self.assertEqual(1, len(results)) |
2539 self.assertEqual( | 2533 self.assertEqual( |
2540 'Found line ending with white spaces in:', results[0]._message) | 2534 'Found line ending with white spaces in:', results[0]._message) |
2541 self.checkstdout('') | 2535 self.checkstdout('') |
2542 | 2536 |
2543 | 2537 |
2544 if __name__ == '__main__': | 2538 if __name__ == '__main__': |
2545 import unittest | 2539 import unittest |
2546 unittest.main() | 2540 unittest.main() |
OLD | NEW |