OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2011 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: disable=E1101,E1103 |
9 # pylint: disable=E1101,E1103,R0201,W0212,W0403 | |
10 | 9 |
11 import logging | 10 import logging |
| 11 import os |
12 import StringIO | 12 import StringIO |
13 import sys | 13 import sys |
14 import time | 14 import time |
15 | 15 |
16 # Fixes include path. | 16 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 17 |
17 from super_mox import mox, SuperMoxTestBase | 18 from super_mox import mox, SuperMoxTestBase |
18 | 19 |
19 import owners | 20 import owners |
20 import presubmit_support as presubmit | 21 import presubmit_support as presubmit |
21 import rietveld | 22 import rietveld |
| 23 |
22 # Shortcut. | 24 # Shortcut. |
23 presubmit_canned_checks = presubmit.presubmit_canned_checks | 25 presubmit_canned_checks = presubmit.presubmit_canned_checks |
24 | 26 |
25 | 27 |
26 class PresubmitTestsBase(SuperMoxTestBase): | 28 class PresubmitTestsBase(SuperMoxTestBase): |
27 """Setups and tear downs the mocks but doesn't test anything as-is.""" | 29 """Setups and tear downs the mocks but doesn't test anything as-is.""" |
28 presubmit_text = """ | 30 presubmit_text = """ |
29 def CheckChangeOnUpload(input_api, output_api): | 31 def CheckChangeOnUpload(input_api, output_api): |
30 if not input_api.change.NOSUCHKEY: | 32 if not input_api.change.NOSUCHKEY: |
31 return [output_api.PresubmitError("!!")] | 33 return [output_api.PresubmitError("!!")] |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 | 228 |
227 presubmit_files = presubmit.ListRelevantPresubmitFiles(files, root_dir) | 229 presubmit_files = presubmit.ListRelevantPresubmitFiles(files, root_dir) |
228 self.assertEqual(presubmit_files, | 230 self.assertEqual(presubmit_files, |
229 [ | 231 [ |
230 join(sys_root_dir, 'foo', 'PRESUBMIT.py'), | 232 join(sys_root_dir, 'foo', 'PRESUBMIT.py'), |
231 join(sys_root_dir, 'foo', 'bar', 'moo', 'PRESUBMIT.py') | 233 join(sys_root_dir, 'foo', 'bar', 'moo', 'PRESUBMIT.py') |
232 ]) | 234 ]) |
233 | 235 |
234 def testTagLineRe(self): | 236 def testTagLineRe(self): |
235 self.mox.ReplayAll() | 237 self.mox.ReplayAll() |
236 m = presubmit.Change._TAG_LINE_RE.match(' BUG =1223, 1445 \t') | 238 m = presubmit.Change.TAG_LINE_RE.match(' BUG =1223, 1445 \t') |
237 self.failUnless(m) | 239 self.failUnless(m) |
238 self.failUnlessEqual(m.group('key'), 'BUG') | 240 self.failUnlessEqual(m.group('key'), 'BUG') |
239 self.failUnlessEqual(m.group('value'), '1223, 1445') | 241 self.failUnlessEqual(m.group('value'), '1223, 1445') |
240 | 242 |
241 def testGclChange(self): | 243 def testGclChange(self): |
242 description_lines = ('Hello there', | 244 description_lines = ('Hello there', |
243 'this is a change', | 245 'this is a change', |
244 'BUG=123', | 246 'BUG=123', |
245 ' STORY =http://foo/ \t', | 247 ' STORY =http://foo/ \t', |
246 'and some more regular text \t') | 248 'and some more regular text \t') |
(...skipping 1092 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1339 self.assertEquals(1, len(output)) | 1341 self.assertEquals(1, len(output)) |
1340 self.assertEquals(files[0], output[0]) | 1342 self.assertEquals(files[0], output[0]) |
1341 | 1343 |
1342 | 1344 |
1343 class ChangeUnittest(PresubmitTestsBase): | 1345 class ChangeUnittest(PresubmitTestsBase): |
1344 def testMembersChanged(self): | 1346 def testMembersChanged(self): |
1345 members = [ | 1347 members = [ |
1346 'AbsoluteLocalPaths', 'AffectedFiles', 'AffectedTextFiles', | 1348 'AbsoluteLocalPaths', 'AffectedFiles', 'AffectedTextFiles', |
1347 'DescriptionText', 'FullDescriptionText', 'LocalPaths', 'Name', | 1349 'DescriptionText', 'FullDescriptionText', 'LocalPaths', 'Name', |
1348 'RepositoryRoot', 'RightHandSideLines', 'ServerPaths', | 1350 'RepositoryRoot', 'RightHandSideLines', 'ServerPaths', |
| 1351 'TAG_LINE_RE', |
1349 'author_email', 'issue', 'patchset', 'scm', 'tags', | 1352 'author_email', 'issue', 'patchset', 'scm', 'tags', |
1350 ] | 1353 ] |
1351 # If this test fails, you should add the relevant test. | 1354 # If this test fails, you should add the relevant test. |
1352 self.mox.ReplayAll() | 1355 self.mox.ReplayAll() |
1353 | 1356 |
1354 change = presubmit.Change( | 1357 change = presubmit.Change( |
1355 'foo', 'foo', self.fake_root_dir, [('M', 'AA')], 0, 0, 'foo') | 1358 'foo', 'foo', self.fake_root_dir, [('M', 'AA')], 0, 0, 'foo') |
1356 self.compareMembers(change, members) | 1359 self.compareMembers(change, members) |
1357 | 1360 |
1358 def testMembers(self): | 1361 def testMembers(self): |
1359 change = presubmit.Change( | 1362 change = presubmit.Change( |
1360 'foo1', 'foo2\nDRU=ro', self.fake_root_dir, [('Y', 'AA')], 3, 5, 'foo3') | 1363 'foo1', 'foo2\nDRU=ro', self.fake_root_dir, [('Y', 'AA')], 3, 5, 'foo3') |
1361 self.assertEquals('foo1', change.Name()) | 1364 self.assertEquals('foo1', change.Name()) |
1362 self.assertEquals('foo2', change.DescriptionText()) | 1365 self.assertEquals('foo2', change.DescriptionText()) |
1363 self.assertEquals('foo3', change.author_email) | 1366 self.assertEquals('foo3', change.author_email) |
1364 self.assertEquals('ro', change.DRU) | 1367 self.assertEquals('ro', change.DRU) |
1365 self.assertEquals(3, change.issue) | 1368 self.assertEquals(3, change.issue) |
1366 self.assertEquals(5, change.patchset) | 1369 self.assertEquals(5, change.patchset) |
1367 self.assertEquals(self.fake_root_dir, change.RepositoryRoot()) | 1370 self.assertEquals(self.fake_root_dir, change.RepositoryRoot()) |
1368 self.assertEquals(1, len(change.AffectedFiles(include_dirs=True))) | 1371 self.assertEquals(1, len(change.AffectedFiles(include_dirs=True))) |
1369 self.assertEquals('Y', change.AffectedFiles(include_dirs=True)[0].Action()) | 1372 self.assertEquals('Y', change.AffectedFiles(include_dirs=True)[0].Action()) |
1370 | 1373 |
1371 | 1374 |
1372 class CannedChecksUnittest(PresubmitTestsBase): | 1375 class CannedChecksUnittest(PresubmitTestsBase): |
1373 """Tests presubmit_canned_checks.py.""" | 1376 """Tests presubmit_canned_checks.py.""" |
1374 | 1377 |
1375 def setUp(self): | |
1376 PresubmitTestsBase.setUp(self) | |
1377 | |
1378 def MockInputApi(self, change, committing): | 1378 def MockInputApi(self, change, committing): |
| 1379 # pylint: disable=R0201 |
1379 input_api = self.mox.CreateMock(presubmit.InputApi) | 1380 input_api = self.mox.CreateMock(presubmit.InputApi) |
1380 input_api.cStringIO = presubmit.cStringIO | 1381 input_api.cStringIO = presubmit.cStringIO |
1381 input_api.json = presubmit.json | 1382 input_api.json = presubmit.json |
1382 input_api.os_listdir = self.mox.CreateMockAnything() | 1383 input_api.os_listdir = self.mox.CreateMockAnything() |
1383 input_api.os_walk = self.mox.CreateMockAnything() | 1384 input_api.os_walk = self.mox.CreateMockAnything() |
1384 input_api.os_path = presubmit.os.path | 1385 input_api.os_path = presubmit.os.path |
1385 input_api.re = presubmit.re | 1386 input_api.re = presubmit.re |
1386 input_api.rietveld = self.mox.CreateMock(rietveld.Rietveld) | 1387 input_api.rietveld = self.mox.CreateMock(rietveld.Rietveld) |
1387 input_api.traceback = presubmit.traceback | 1388 input_api.traceback = presubmit.traceback |
1388 input_api.urllib2 = self.mox.CreateMock(presubmit.urllib2) | 1389 input_api.urllib2 = self.mox.CreateMock(presubmit.urllib2) |
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1677 # Override the mock of these functions. | 1678 # Override the mock of these functions. |
1678 input_api1.FilterSourceFile = lambda x: x | 1679 input_api1.FilterSourceFile = lambda x: x |
1679 input_api1.AffectedFiles = test | 1680 input_api1.AffectedFiles = test |
1680 self.mox.ReplayAll() | 1681 self.mox.ReplayAll() |
1681 | 1682 |
1682 results1 = presubmit_canned_checks.CheckChangeHasNoTabs(input_api1, | 1683 results1 = presubmit_canned_checks.CheckChangeHasNoTabs(input_api1, |
1683 presubmit.OutputApi, None) | 1684 presubmit.OutputApi, None) |
1684 self.assertEquals(len(results1), 1) | 1685 self.assertEquals(len(results1), 1) |
1685 self.assertEquals(results1[0].__class__, | 1686 self.assertEquals(results1[0].__class__, |
1686 presubmit.OutputApi.PresubmitPromptWarning) | 1687 presubmit.OutputApi.PresubmitPromptWarning) |
| 1688 # pylint: disable=W0212 |
1687 self.assertEquals(results1[0]._long_text, | 1689 self.assertEquals(results1[0]._long_text, |
1688 'makefile.foo, line 46') | 1690 'makefile.foo, line 46') |
1689 | 1691 |
1690 def testCannedCheckLongLines(self): | 1692 def testCannedCheckLongLines(self): |
1691 check = lambda x, y, z: presubmit_canned_checks.CheckLongLines(x, y, 10, z) | 1693 check = lambda x, y, z: presubmit_canned_checks.CheckLongLines(x, y, 10, z) |
1692 self.ContentTest(check, '0123456789', '01234567890', | 1694 self.ContentTest(check, '0123456789', '01234567890', |
1693 presubmit.OutputApi.PresubmitPromptWarning) | 1695 presubmit.OutputApi.PresubmitPromptWarning) |
1694 | 1696 |
1695 def testCannedCheckLongLinesLF(self): | 1697 def testCannedCheckLongLinesLF(self): |
1696 check = lambda x, y, z: presubmit_canned_checks.CheckLongLines(x, y, 10, z) | 1698 check = lambda x, y, z: presubmit_canned_checks.CheckLongLines(x, y, 10, z) |
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1966 ['pyyyyython', '-m', 'test_module'], cwd=None, env=None, | 1968 ['pyyyyython', '-m', 'test_module'], cwd=None, env=None, |
1967 stderr=input_api.subprocess.STDOUT).AndRaise( | 1969 stderr=input_api.subprocess.STDOUT).AndRaise( |
1968 input_api.subprocess.CalledProcessError()) | 1970 input_api.subprocess.CalledProcessError()) |
1969 self.mox.ReplayAll() | 1971 self.mox.ReplayAll() |
1970 | 1972 |
1971 results = presubmit_canned_checks.RunPythonUnitTests( | 1973 results = presubmit_canned_checks.RunPythonUnitTests( |
1972 input_api, presubmit.OutputApi, ['test_module']) | 1974 input_api, presubmit.OutputApi, ['test_module']) |
1973 self.assertEquals(len(results), 1) | 1975 self.assertEquals(len(results), 1) |
1974 self.assertEquals(results[0].__class__, | 1976 self.assertEquals(results[0].__class__, |
1975 presubmit.OutputApi.PresubmitNotifyResult) | 1977 presubmit.OutputApi.PresubmitNotifyResult) |
| 1978 # pylint: disable=W0212 |
1976 self.assertEquals('test_module failed!\nfoo', results[0]._message) | 1979 self.assertEquals('test_module failed!\nfoo', results[0]._message) |
1977 | 1980 |
1978 def testRunPythonUnitTestsFailureCommitting(self): | 1981 def testRunPythonUnitTestsFailureCommitting(self): |
1979 input_api = self.MockInputApi(None, True) | 1982 input_api = self.MockInputApi(None, True) |
1980 input_api.subprocess.check_output( | 1983 input_api.subprocess.check_output( |
1981 ['pyyyyython', '-m', 'test_module'], cwd=None, env=None, | 1984 ['pyyyyython', '-m', 'test_module'], cwd=None, env=None, |
1982 stderr=input_api.subprocess.STDOUT).AndRaise( | 1985 stderr=input_api.subprocess.STDOUT).AndRaise( |
1983 input_api.subprocess.CalledProcessError()) | 1986 input_api.subprocess.CalledProcessError()) |
1984 self.mox.ReplayAll() | 1987 self.mox.ReplayAll() |
1985 | 1988 |
1986 results = presubmit_canned_checks.RunPythonUnitTests( | 1989 results = presubmit_canned_checks.RunPythonUnitTests( |
1987 input_api, presubmit.OutputApi, ['test_module']) | 1990 input_api, presubmit.OutputApi, ['test_module']) |
1988 self.assertEquals(len(results), 1) | 1991 self.assertEquals(len(results), 1) |
1989 self.assertEquals(results[0].__class__, presubmit.OutputApi.PresubmitError) | 1992 self.assertEquals(results[0].__class__, presubmit.OutputApi.PresubmitError) |
| 1993 # pylint: disable=W0212 |
1990 self.assertEquals('test_module failed!\nfoo', results[0]._message) | 1994 self.assertEquals('test_module failed!\nfoo', results[0]._message) |
1991 | 1995 |
1992 def testRunPythonUnitTestsSuccess(self): | 1996 def testRunPythonUnitTestsSuccess(self): |
1993 input_api = self.MockInputApi(None, False) | 1997 input_api = self.MockInputApi(None, False) |
1994 input_api.cStringIO = self.mox.CreateMock(presubmit.cStringIO) | 1998 input_api.cStringIO = self.mox.CreateMock(presubmit.cStringIO) |
1995 input_api.unittest = self.mox.CreateMock(unittest) | 1999 input_api.unittest = self.mox.CreateMock(unittest) |
1996 input_api.subprocess.check_output( | 2000 input_api.subprocess.check_output( |
1997 ['pyyyyython', '-m', 'test_module'], cwd=None, env=None, | 2001 ['pyyyyython', '-m', 'test_module'], cwd=None, env=None, |
1998 stderr=input_api.subprocess.STDOUT) | 2002 stderr=input_api.subprocess.STDOUT) |
1999 self.mox.ReplayAll() | 2003 self.mox.ReplayAll() |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2220 self.mox.ReplayAll() | 2224 self.mox.ReplayAll() |
2221 results = presubmit_canned_checks.PanProjectChecks( | 2225 results = presubmit_canned_checks.PanProjectChecks( |
2222 input_api, | 2226 input_api, |
2223 presubmit.OutputApi, | 2227 presubmit.OutputApi, |
2224 excluded_paths=None, | 2228 excluded_paths=None, |
2225 text_files=None, | 2229 text_files=None, |
2226 license_header=None, | 2230 license_header=None, |
2227 project_name=None, | 2231 project_name=None, |
2228 owners_check=True) | 2232 owners_check=True) |
2229 self.assertEqual(1, len(results)) | 2233 self.assertEqual(1, len(results)) |
| 2234 # pylint: disable=W0212 |
2230 self.assertEqual( | 2235 self.assertEqual( |
2231 'Found line ending with white spaces in:', results[0]._message) | 2236 'Found line ending with white spaces in:', results[0]._message) |
2232 self.checkstdout('') | 2237 self.checkstdout('') |
2233 | 2238 |
2234 | 2239 |
2235 if __name__ == '__main__': | 2240 if __name__ == '__main__': |
2236 import unittest | 2241 import unittest |
2237 unittest.main() | 2242 unittest.main() |
OLD | NEW |