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,W0212,W0403 | 9 # pylint: disable=E1101,E1103,R0201,W0212,W0403 |
10 | 10 |
11 import StringIO | 11 import StringIO |
12 import sys | 12 import sys |
13 | 13 |
14 # Fixes include path. | 14 # Fixes include path. |
15 from super_mox import mox, SuperMoxTestBase | 15 from super_mox import mox, SuperMoxTestBase |
16 | 16 |
17 import owners | 17 import owners |
18 import presubmit_support as presubmit | 18 import presubmit_support as presubmit |
19 # Shortcut. | 19 # Shortcut. |
(...skipping 1219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1239 input_api = self.mox.CreateMock(presubmit.InputApi) | 1239 input_api = self.mox.CreateMock(presubmit.InputApi) |
1240 input_api.cStringIO = presubmit.cStringIO | 1240 input_api.cStringIO = presubmit.cStringIO |
1241 input_api.os_listdir = self.mox.CreateMockAnything() | 1241 input_api.os_listdir = self.mox.CreateMockAnything() |
1242 input_api.os_walk = self.mox.CreateMockAnything() | 1242 input_api.os_walk = self.mox.CreateMockAnything() |
1243 input_api.os_path = presubmit.os.path | 1243 input_api.os_path = presubmit.os.path |
1244 input_api.re = presubmit.re | 1244 input_api.re = presubmit.re |
1245 input_api.traceback = presubmit.traceback | 1245 input_api.traceback = presubmit.traceback |
1246 input_api.urllib2 = self.mox.CreateMock(presubmit.urllib2) | 1246 input_api.urllib2 = self.mox.CreateMock(presubmit.urllib2) |
1247 input_api.unittest = unittest | 1247 input_api.unittest = unittest |
1248 input_api.subprocess = self.mox.CreateMock(presubmit.subprocess) | 1248 input_api.subprocess = self.mox.CreateMock(presubmit.subprocess) |
| 1249 class fake_CalledProcessError(Exception): |
| 1250 def __str__(self): |
| 1251 return 'foo' |
| 1252 input_api.subprocess.CalledProcessError = fake_CalledProcessError |
1249 | 1253 |
1250 input_api.change = change | 1254 input_api.change = change |
1251 input_api.host_url = 'http://localhost' | 1255 input_api.host_url = 'http://localhost' |
1252 input_api.is_committing = committing | 1256 input_api.is_committing = committing |
1253 input_api.tbr = False | 1257 input_api.tbr = False |
1254 input_api.python_executable = 'pyyyyython' | 1258 input_api.python_executable = 'pyyyyython' |
1255 input_api.platform = sys.platform | 1259 input_api.platform = sys.platform |
1256 return input_api | 1260 return input_api |
1257 | 1261 |
1258 def testMembersChanged(self): | 1262 def testMembersChanged(self): |
(...skipping 465 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1724 | 1728 |
1725 def testRunPythonUnitTestsNoTest(self): | 1729 def testRunPythonUnitTestsNoTest(self): |
1726 input_api = self.MockInputApi(None, False) | 1730 input_api = self.MockInputApi(None, False) |
1727 self.mox.ReplayAll() | 1731 self.mox.ReplayAll() |
1728 results = presubmit_canned_checks.RunPythonUnitTests( | 1732 results = presubmit_canned_checks.RunPythonUnitTests( |
1729 input_api, presubmit.OutputApi, []) | 1733 input_api, presubmit.OutputApi, []) |
1730 self.assertEquals(results, []) | 1734 self.assertEquals(results, []) |
1731 | 1735 |
1732 def testRunPythonUnitTestsNonExistentUpload(self): | 1736 def testRunPythonUnitTestsNonExistentUpload(self): |
1733 input_api = self.MockInputApi(None, False) | 1737 input_api = self.MockInputApi(None, False) |
1734 process = self.mox.CreateMockAnything() | 1738 input_api.subprocess.check_output( |
1735 process.returncode = 2 | 1739 ['pyyyyython', '-m', '_non_existent_module'], cwd=None, env=None |
1736 input_api.subprocess.Popen( | 1740 ).AndRaise( |
1737 ['pyyyyython', '-m', '_non_existent_module'], cwd=None, env=None, | 1741 input_api.subprocess.CalledProcessError()) |
1738 stderr=presubmit.subprocess.PIPE, stdin=presubmit.subprocess.PIPE, | |
1739 stdout=presubmit.subprocess.PIPE).AndReturn(process) | |
1740 process.communicate().AndReturn( | |
1741 ('', 'pyyython: module _non_existent_module not found')) | |
1742 self.mox.ReplayAll() | 1742 self.mox.ReplayAll() |
1743 | 1743 |
1744 results = presubmit_canned_checks.RunPythonUnitTests( | 1744 results = presubmit_canned_checks.RunPythonUnitTests( |
1745 input_api, presubmit.OutputApi, ['_non_existent_module']) | 1745 input_api, presubmit.OutputApi, ['_non_existent_module']) |
1746 self.assertEquals(len(results), 1) | 1746 self.assertEquals(len(results), 1) |
1747 self.assertEquals(results[0].__class__, | 1747 self.assertEquals(results[0].__class__, |
1748 presubmit.OutputApi.PresubmitNotifyResult) | 1748 presubmit.OutputApi.PresubmitNotifyResult) |
1749 | 1749 |
1750 def testRunPythonUnitTestsNonExistentCommitting(self): | 1750 def testRunPythonUnitTestsNonExistentCommitting(self): |
1751 input_api = self.MockInputApi(None, True) | 1751 input_api = self.MockInputApi(None, True) |
1752 process = self.mox.CreateMockAnything() | 1752 input_api.subprocess.check_output( |
1753 process.returncode = 2 | 1753 ['pyyyyython', '-m', '_non_existent_module'], cwd=None, env=None |
1754 input_api.subprocess.Popen( | 1754 ).AndRaise( |
1755 ['pyyyyython', '-m', '_non_existent_module'], cwd=None, env=None, | 1755 input_api.subprocess.CalledProcessError()) |
1756 stderr=presubmit.subprocess.PIPE, stdin=presubmit.subprocess.PIPE, | |
1757 stdout=presubmit.subprocess.PIPE).AndReturn(process) | |
1758 process.communicate().AndReturn( | |
1759 ('', 'pyyython: module _non_existent_module not found')) | |
1760 self.mox.ReplayAll() | 1756 self.mox.ReplayAll() |
| 1757 |
1761 results = presubmit_canned_checks.RunPythonUnitTests( | 1758 results = presubmit_canned_checks.RunPythonUnitTests( |
1762 input_api, presubmit.OutputApi, ['_non_existent_module']) | 1759 input_api, presubmit.OutputApi, ['_non_existent_module']) |
1763 self.assertEquals(len(results), 1) | 1760 self.assertEquals(len(results), 1) |
1764 self.assertEquals(results[0].__class__, presubmit.OutputApi.PresubmitError) | 1761 self.assertEquals(results[0].__class__, presubmit.OutputApi.PresubmitError) |
1765 | 1762 |
1766 def testRunPythonUnitTestsFailureUpload(self): | 1763 def testRunPythonUnitTestsFailureUpload(self): |
1767 input_api = self.MockInputApi(None, False) | 1764 input_api = self.MockInputApi(None, False) |
1768 input_api.unittest = self.mox.CreateMock(unittest) | 1765 input_api.unittest = self.mox.CreateMock(unittest) |
1769 input_api.cStringIO = self.mox.CreateMock(presubmit.cStringIO) | 1766 input_api.cStringIO = self.mox.CreateMock(presubmit.cStringIO) |
1770 process = self.mox.CreateMockAnything() | 1767 input_api.subprocess.check_output( |
1771 process.returncode = -1 | 1768 ['pyyyyython', '-m', 'test_module'], cwd=None, env=None).AndRaise( |
1772 input_api.subprocess.Popen( | 1769 input_api.subprocess.CalledProcessError()) |
1773 ['pyyyyython', '-m', 'test_module'], cwd=None, env=None, | |
1774 stderr=presubmit.subprocess.PIPE, stdin=presubmit.subprocess.PIPE, | |
1775 stdout=presubmit.subprocess.PIPE).AndReturn(process) | |
1776 process.communicate().AndReturn(('BOO HOO!', '')) | |
1777 self.mox.ReplayAll() | 1770 self.mox.ReplayAll() |
1778 | 1771 |
1779 results = presubmit_canned_checks.RunPythonUnitTests( | 1772 results = presubmit_canned_checks.RunPythonUnitTests( |
1780 input_api, presubmit.OutputApi, ['test_module']) | 1773 input_api, presubmit.OutputApi, ['test_module']) |
1781 self.assertEquals(len(results), 1) | 1774 self.assertEquals(len(results), 1) |
1782 self.assertEquals(results[0].__class__, | 1775 self.assertEquals(results[0].__class__, |
1783 presubmit.OutputApi.PresubmitNotifyResult) | 1776 presubmit.OutputApi.PresubmitNotifyResult) |
1784 self.assertEquals(results[0]._long_text, | 1777 self.assertEquals('test_module failed!\nfoo', results[0]._message) |
1785 "Test 'test_module' failed with code -1\nBOO HOO!") | |
1786 | 1778 |
1787 def testRunPythonUnitTestsFailureCommitting(self): | 1779 def testRunPythonUnitTestsFailureCommitting(self): |
1788 input_api = self.MockInputApi(None, True) | 1780 input_api = self.MockInputApi(None, True) |
1789 process = self.mox.CreateMockAnything() | 1781 input_api.subprocess.check_output( |
1790 process.returncode = 1 | 1782 ['pyyyyython', '-m', 'test_module'], cwd=None, env=None).AndRaise( |
1791 input_api.subprocess.Popen( | 1783 input_api.subprocess.CalledProcessError()) |
1792 ['pyyyyython', '-m', 'test_module'], cwd=None, env=None, | |
1793 stderr=presubmit.subprocess.PIPE, stdin=presubmit.subprocess.PIPE, | |
1794 stdout=presubmit.subprocess.PIPE).AndReturn(process) | |
1795 process.communicate().AndReturn(('BOO HOO!', '')) | |
1796 self.mox.ReplayAll() | 1784 self.mox.ReplayAll() |
1797 | 1785 |
1798 results = presubmit_canned_checks.RunPythonUnitTests( | 1786 results = presubmit_canned_checks.RunPythonUnitTests( |
1799 input_api, presubmit.OutputApi, ['test_module']) | 1787 input_api, presubmit.OutputApi, ['test_module']) |
1800 self.assertEquals(len(results), 1) | 1788 self.assertEquals(len(results), 1) |
1801 self.assertEquals(results[0].__class__, presubmit.OutputApi.PresubmitError) | 1789 self.assertEquals(results[0].__class__, presubmit.OutputApi.PresubmitError) |
1802 self.assertEquals(results[0]._long_text, | 1790 self.assertEquals('test_module failed!\nfoo', results[0]._message) |
1803 "Test 'test_module' failed with code 1\nBOO HOO!") | |
1804 | 1791 |
1805 def testRunPythonUnitTestsSuccess(self): | 1792 def testRunPythonUnitTestsSuccess(self): |
1806 input_api = self.MockInputApi(None, False) | 1793 input_api = self.MockInputApi(None, False) |
1807 input_api.cStringIO = self.mox.CreateMock(presubmit.cStringIO) | 1794 input_api.cStringIO = self.mox.CreateMock(presubmit.cStringIO) |
1808 input_api.unittest = self.mox.CreateMock(unittest) | 1795 input_api.unittest = self.mox.CreateMock(unittest) |
1809 process = self.mox.CreateMockAnything() | 1796 input_api.subprocess.check_output( |
1810 process.returncode = 0 | 1797 ['pyyyyython', '-m', 'test_module'], cwd=None, env=None) |
1811 input_api.subprocess.Popen( | |
1812 ['pyyyyython', '-m', 'test_module'], cwd=None, env=None, | |
1813 stderr=presubmit.subprocess.PIPE, stdin=presubmit.subprocess.PIPE, | |
1814 stdout=presubmit.subprocess.PIPE).AndReturn(process) | |
1815 process.communicate().AndReturn(('', '')) | |
1816 self.mox.ReplayAll() | 1798 self.mox.ReplayAll() |
1817 | 1799 |
1818 results = presubmit_canned_checks.RunPythonUnitTests( | 1800 results = presubmit_canned_checks.RunPythonUnitTests( |
1819 input_api, presubmit.OutputApi, ['test_module']) | 1801 input_api, presubmit.OutputApi, ['test_module']) |
1820 self.assertEquals(len(results), 0) | 1802 self.assertEquals(len(results), 0) |
1821 | 1803 |
1822 def testCheckRietveldTryJobExecutionBad(self): | 1804 def testCheckRietveldTryJobExecutionBad(self): |
1823 change = self.mox.CreateMock(presubmit.SvnChange) | 1805 change = self.mox.CreateMock(presubmit.SvnChange) |
1824 change.scm = 'svn' | 1806 change.scm = 'svn' |
1825 change.issue = 2 | 1807 change.issue = 2 |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2015 self.AssertOwnersWorks(approvers=set(['ben@example.com']), | 1997 self.AssertOwnersWorks(approvers=set(['ben@example.com']), |
2016 uncovered_files=set()) | 1998 uncovered_files=set()) |
2017 | 1999 |
2018 def testCannedRunUnitTests(self): | 2000 def testCannedRunUnitTests(self): |
2019 change = presubmit.Change( | 2001 change = presubmit.Change( |
2020 'foo1', 'description1', self.fake_root_dir, None, 0, 0) | 2002 'foo1', 'description1', self.fake_root_dir, None, 0, 0) |
2021 input_api = self.MockInputApi(change, False) | 2003 input_api = self.MockInputApi(change, False) |
2022 unit_tests = ['allo', 'bar.py'] | 2004 unit_tests = ['allo', 'bar.py'] |
2023 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) | 2005 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) |
2024 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) | 2006 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) |
2025 proc1 = self.mox.CreateMockAnything() | 2007 input_api.subprocess.check_call( |
2026 input_api.subprocess.Popen(['allo'], cwd=self.fake_root_dir, | 2008 ['allo'], cwd=self.fake_root_dir) |
2027 stderr=None, stdout=None).AndReturn(proc1) | 2009 input_api.subprocess.check_call( |
2028 proc1.returncode = 0 | 2010 ['bar.py'], cwd=self.fake_root_dir).AndRaise( |
2029 proc1.communicate().AndReturn(['baz', None]) | 2011 input_api.subprocess.CalledProcessError()) |
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 | 2012 |
2036 self.mox.ReplayAll() | 2013 self.mox.ReplayAll() |
2037 results = presubmit_canned_checks.RunUnitTests( | 2014 results = presubmit_canned_checks.RunUnitTests( |
2038 input_api, | 2015 input_api, |
2039 presubmit.OutputApi, | 2016 presubmit.OutputApi, |
2040 unit_tests, | 2017 unit_tests, |
2041 verbose=True) | 2018 verbose=True) |
2042 self.assertEqual(1, len(results)) | 2019 self.assertEqual(1, len(results)) |
2043 self.assertEqual( | 2020 self.assertEqual( |
2044 presubmit.OutputApi.PresubmitPromptWarning, results[0].__class__) | 2021 presubmit.OutputApi.PresubmitPromptWarning, results[0].__class__) |
2045 self.checkstdout('Running allo\nRunning bar.py\n') | 2022 self.checkstdout('Running allo\nRunning bar.py\n') |
2046 | 2023 |
2047 def testCannedRunUnitTestsInDirectory(self): | 2024 def testCannedRunUnitTestsInDirectory(self): |
2048 change = presubmit.Change( | 2025 change = presubmit.Change( |
2049 'foo1', 'description1', self.fake_root_dir, None, 0, 0) | 2026 'foo1', 'description1', self.fake_root_dir, None, 0, 0) |
2050 input_api = self.MockInputApi(change, False) | 2027 input_api = self.MockInputApi(change, False) |
2051 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) | 2028 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) |
2052 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) | 2029 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) |
2053 path = presubmit.os.path.join(self.fake_root_dir, 'random_directory') | 2030 path = presubmit.os.path.join(self.fake_root_dir, 'random_directory') |
2054 input_api.os_listdir(path).AndReturn(['.', '..', 'a', 'b', 'c']) | 2031 input_api.os_listdir(path).AndReturn(['.', '..', 'a', 'b', 'c']) |
2055 input_api.os_path.isfile = lambda x: not x.endswith('.') | 2032 input_api.os_path.isfile = lambda x: not x.endswith('.') |
2056 proc1 = self.mox.CreateMockAnything() | 2033 input_api.subprocess.check_call( |
2057 input_api.subprocess.Popen(['random_directory/b'], cwd=self.fake_root_dir, | 2034 ['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 | 2035 |
2062 self.mox.ReplayAll() | 2036 self.mox.ReplayAll() |
2063 results = presubmit_canned_checks.RunUnitTestsInDirectory( | 2037 results = presubmit_canned_checks.RunUnitTestsInDirectory( |
2064 input_api, | 2038 input_api, |
2065 presubmit.OutputApi, | 2039 presubmit.OutputApi, |
2066 'random_directory', | 2040 'random_directory', |
2067 whitelist=['^a$', '^b$'], | 2041 whitelist=['^a$', '^b$'], |
2068 blacklist=['a'], | 2042 blacklist=['a'], |
2069 verbose=True) | 2043 verbose=True) |
2070 self.assertEqual(results, []) | 2044 self.assertEqual(results, []) |
2071 self.checkstdout('Running random_directory/b\n') | 2045 self.checkstdout('Running random_directory/b\n') |
2072 | 2046 |
2073 | 2047 |
2074 if __name__ == '__main__': | 2048 if __name__ == '__main__': |
2075 import unittest | 2049 import unittest |
2076 unittest.main() | 2050 unittest.main() |
OLD | NEW |