| 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 is too confused. |
| 9 # pylint: disable=E1101,E1103,R0201,W0212,W0403 | 9 # pylint: disable=E1101,E1103,R0201,W0212,W0403 |
| 10 | 10 |
| (...skipping 1409 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1420 """ | 1420 """ |
| 1421 change1 = presubmit.Change( | 1421 change1 = presubmit.Change( |
| 1422 'foo1', 'foo1\n', self.fake_root_dir, None, 0, 0, None) | 1422 'foo1', 'foo1\n', self.fake_root_dir, None, 0, 0, None) |
| 1423 input_api1 = self.MockInputApi(change1, False) | 1423 input_api1 = self.MockInputApi(change1, False) |
| 1424 affected_file = self.mox.CreateMock(presubmit.SvnAffectedFile) | 1424 affected_file = self.mox.CreateMock(presubmit.SvnAffectedFile) |
| 1425 input_api1.AffectedFiles( | 1425 input_api1.AffectedFiles( |
| 1426 include_deletes=False, | 1426 include_deletes=False, |
| 1427 file_filter=mox.IgnoreArg()).AndReturn([affected_file]) | 1427 file_filter=mox.IgnoreArg()).AndReturn([affected_file]) |
| 1428 affected_file.NewContents().AndReturn([ | 1428 affected_file.NewContents().AndReturn([ |
| 1429 'ahoy', | 1429 'ahoy', |
| 1430 'yo' + content1, | 1430 content1, |
| 1431 'hay', | 1431 'hay', |
| 1432 'yer', | 1432 'yer', |
| 1433 'ya']) | 1433 'ya']) |
| 1434 | 1434 |
| 1435 change2 = presubmit.Change( | 1435 change2 = presubmit.Change( |
| 1436 'foo2', 'foo2\n', self.fake_root_dir, None, 0, 0, None) | 1436 'foo2', 'foo2\n', self.fake_root_dir, None, 0, 0, None) |
| 1437 input_api2 = self.MockInputApi(change2, False) | 1437 input_api2 = self.MockInputApi(change2, False) |
| 1438 | 1438 |
| 1439 input_api2.AffectedFiles( | 1439 input_api2.AffectedFiles( |
| 1440 include_deletes=False, | 1440 include_deletes=False, |
| 1441 file_filter=mox.IgnoreArg()).AndReturn([affected_file]) | 1441 file_filter=mox.IgnoreArg()).AndReturn([affected_file]) |
| 1442 affected_file.NewContents().AndReturn([ | 1442 affected_file.NewContents().AndReturn([ |
| 1443 'ahoy', | 1443 'ahoy', |
| 1444 'yo' + content2, | 1444 content2, |
| 1445 'hay', | 1445 'hay', |
| 1446 'yer', | 1446 'yer', |
| 1447 'ya']) | 1447 'ya']) |
| 1448 # It falls back to ChangedContents when there is a failure. This is an |
| 1449 # optimization since NewContents() is much faster to execute than |
| 1450 # ChangedContents(). |
| 1448 affected_file.ChangedContents().AndReturn([ | 1451 affected_file.ChangedContents().AndReturn([ |
| 1449 (42, 'yo, ' + content2), | 1452 (42, content2), |
| 1450 (43, 'yer'), | 1453 (43, 'yer'), |
| 1451 (23, 'ya')]) | 1454 (23, 'ya')]) |
| 1452 affected_file.LocalPath().AndReturn('foo.cc') | 1455 affected_file.LocalPath().AndReturn('foo.cc') |
| 1453 | 1456 |
| 1454 self.mox.ReplayAll() | 1457 self.mox.ReplayAll() |
| 1455 | 1458 |
| 1456 results1 = check(input_api1, presubmit.OutputApi, None) | 1459 results1 = check(input_api1, presubmit.OutputApi, None) |
| 1457 self.assertEquals(results1, []) | 1460 self.assertEquals(results1, []) |
| 1458 results2 = check(input_api2, presubmit.OutputApi, None) | 1461 results2 = check(input_api2, presubmit.OutputApi, None) |
| 1459 self.assertEquals(len(results2), 1) | 1462 self.assertEquals(len(results2), 1) |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1639 self.mox.ReplayAll() | 1642 self.mox.ReplayAll() |
| 1640 | 1643 |
| 1641 results1 = presubmit_canned_checks.CheckChangeHasNoTabs(input_api1, | 1644 results1 = presubmit_canned_checks.CheckChangeHasNoTabs(input_api1, |
| 1642 presubmit.OutputApi, None) | 1645 presubmit.OutputApi, None) |
| 1643 self.assertEquals(len(results1), 1) | 1646 self.assertEquals(len(results1), 1) |
| 1644 self.assertEquals(results1[0].__class__, | 1647 self.assertEquals(results1[0].__class__, |
| 1645 presubmit.OutputApi.PresubmitPromptWarning) | 1648 presubmit.OutputApi.PresubmitPromptWarning) |
| 1646 self.assertEquals(results1[0]._long_text, | 1649 self.assertEquals(results1[0]._long_text, |
| 1647 'makefile.foo, line 46') | 1650 'makefile.foo, line 46') |
| 1648 | 1651 |
| 1649 | |
| 1650 def testCannedCheckLongLines(self): | 1652 def testCannedCheckLongLines(self): |
| 1651 check = lambda x, y, z: presubmit_canned_checks.CheckLongLines(x, y, 10, z) | 1653 check = lambda x, y, z: presubmit_canned_checks.CheckLongLines(x, y, 10, z) |
| 1652 self.ContentTest(check, '', 'blah blah blah', | 1654 self.ContentTest(check, '0123456789', '01234567890', |
| 1653 presubmit.OutputApi.PresubmitPromptWarning) | 1655 presubmit.OutputApi.PresubmitPromptWarning) |
| 1654 | 1656 |
| 1657 def testCannedCheckLongLinesLF(self): |
| 1658 check = lambda x, y, z: presubmit_canned_checks.CheckLongLines(x, y, 10, z) |
| 1659 self.ContentTest(check, '012345678\n', '0123456789\n', |
| 1660 presubmit.OutputApi.PresubmitPromptWarning) |
| 1661 |
| 1662 def testCannedCheckLongLinesMacro(self): |
| 1663 check = lambda x, y, z: presubmit_canned_checks.CheckLongLines(x, y, 10, z) |
| 1664 self.ContentTest( |
| 1665 check, |
| 1666 # Put a space in so it doesn't trigger long symbols. Allow 1/3 more. |
| 1667 '#if 56 89 12 45', |
| 1668 '#if 56 89 12 456', |
| 1669 presubmit.OutputApi.PresubmitPromptWarning) |
| 1670 |
| 1671 def testCannedCheckLongLinesHttp(self): |
| 1672 check = lambda x, y, z: presubmit_canned_checks.CheckLongLines(x, y, 10, z) |
| 1673 self.ContentTest( |
| 1674 check, |
| 1675 ' http:// 0 23 5', |
| 1676 ' http:// 0 23 56', |
| 1677 presubmit.OutputApi.PresubmitPromptWarning) |
| 1678 |
| 1679 def testCannedCheckLongLinesLongSymbol(self): |
| 1680 check = lambda x, y, z: presubmit_canned_checks.CheckLongLines(x, y, 10, z) |
| 1681 self.ContentTest( |
| 1682 check, |
| 1683 ' TUP5D_LoNG_SY ', |
| 1684 ' TUP5D_LoNG_SY5 ', |
| 1685 presubmit.OutputApi.PresubmitPromptWarning) |
| 1686 |
| 1655 def testCheckChangeSvnEolStyleCommit(self): | 1687 def testCheckChangeSvnEolStyleCommit(self): |
| 1656 # Test CheckSvnProperty at the same time. | 1688 # Test CheckSvnProperty at the same time. |
| 1657 self.SvnPropertyTest(presubmit_canned_checks.CheckChangeSvnEolStyle, | 1689 self.SvnPropertyTest(presubmit_canned_checks.CheckChangeSvnEolStyle, |
| 1658 'svn:eol-style', 'LF', '', True, | 1690 'svn:eol-style', 'LF', '', True, |
| 1659 presubmit.OutputApi.PresubmitError, True) | 1691 presubmit.OutputApi.PresubmitError, True) |
| 1660 | 1692 |
| 1661 def testCheckChangeSvnEolStyleUpload(self): | 1693 def testCheckChangeSvnEolStyleUpload(self): |
| 1662 self.SvnPropertyTest(presubmit_canned_checks.CheckChangeSvnEolStyle, | 1694 self.SvnPropertyTest(presubmit_canned_checks.CheckChangeSvnEolStyle, |
| 1663 'svn:eol-style', 'LF', '', False, | 1695 'svn:eol-style', 'LF', '', False, |
| 1664 presubmit.OutputApi.PresubmitNotifyResult, True) | 1696 presubmit.OutputApi.PresubmitNotifyResult, True) |
| (...skipping 530 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2195 owners_check=True) | 2227 owners_check=True) |
| 2196 self.assertEqual(1, len(results)) | 2228 self.assertEqual(1, len(results)) |
| 2197 self.assertEqual( | 2229 self.assertEqual( |
| 2198 'Found line ending with white spaces in:', results[0]._message) | 2230 'Found line ending with white spaces in:', results[0]._message) |
| 2199 self.checkstdout('') | 2231 self.checkstdout('') |
| 2200 | 2232 |
| 2201 | 2233 |
| 2202 if __name__ == '__main__': | 2234 if __name__ == '__main__': |
| 2203 import unittest | 2235 import unittest |
| 2204 unittest.main() | 2236 unittest.main() |
| OLD | NEW |