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 |
| 11 import logging |
11 import StringIO | 12 import StringIO |
12 import sys | 13 import sys |
13 import time | 14 import time |
14 | 15 |
15 # Fixes include path. | 16 # Fixes include path. |
16 from super_mox import mox, SuperMoxTestBase | 17 from super_mox import mox, SuperMoxTestBase |
17 | 18 |
18 import owners | 19 import owners |
19 import presubmit_support as presubmit | 20 import presubmit_support as presubmit |
20 import rietveld | 21 import rietveld |
(...skipping 550 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
571 '\n'.join(description_lines), | 572 '\n'.join(description_lines), |
572 self.fake_root_dir, | 573 self.fake_root_dir, |
573 files, | 574 files, |
574 0, | 575 0, |
575 0, | 576 0, |
576 None) | 577 None) |
577 output = presubmit.DoPresubmitChecks( | 578 output = presubmit.DoPresubmitChecks( |
578 change, False, True, None, input_buf, DEFAULT_SCRIPT, False, None) | 579 change, False, True, None, input_buf, DEFAULT_SCRIPT, False, None) |
579 self.failIf(output.should_continue()) | 580 self.failIf(output.should_continue()) |
580 text = ('Running presubmit upload checks ...\n' | 581 text = ('Running presubmit upload checks ...\n' |
581 'Warning, no presubmit.py found.\n' | 582 'Warning, no PRESUBMIT.py found.\n' |
582 'Running default presubmit script.\n' | 583 'Running default presubmit script.\n' |
583 '\n' | 584 '\n' |
584 '** Presubmit ERRORS **\n!!\n\n' | 585 '** Presubmit ERRORS **\n!!\n\n' |
585 'Was the presubmit check useful? Please send feedback & hate mail ' | 586 'Was the presubmit check useful? Please send feedback & hate mail ' |
586 'to maruel@chromium.org!\n') | 587 'to maruel@chromium.org!\n') |
587 self.assertEquals(output.getvalue(), text) | 588 self.assertEquals(output.getvalue(), text) |
588 | 589 |
589 def testDirectoryHandling(self): | 590 def testDirectoryHandling(self): |
590 files = [ | 591 files = [ |
591 ['A', 'isdir'], | 592 ['A', 'isdir'], |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
651 'Blah Blah\n\nSTORY=http://tracker.com/42\nBUG=boo\n', | 652 'Blah Blah\n\nSTORY=http://tracker.com/42\nBUG=boo\n', |
652 self.fake_root_dir, | 653 self.fake_root_dir, |
653 None, | 654 None, |
654 0, | 655 0, |
655 0, | 656 0, |
656 None) | 657 None) |
657 self.failUnless(presubmit.DoPresubmitChecks( | 658 self.failUnless(presubmit.DoPresubmitChecks( |
658 change, False, True, output, input_buf, DEFAULT_SCRIPT, False, None)) | 659 change, False, True, output, input_buf, DEFAULT_SCRIPT, False, None)) |
659 self.assertEquals(output.getvalue(), | 660 self.assertEquals(output.getvalue(), |
660 ('Running presubmit upload checks ...\n' | 661 ('Running presubmit upload checks ...\n' |
661 'Warning, no presubmit.py found.\n' | 662 'Warning, no PRESUBMIT.py found.\n' |
662 'Running default presubmit script.\n' | 663 'Running default presubmit script.\n' |
663 '\n' | 664 '\n' |
664 '** Presubmit Messages **\n' | 665 '** Presubmit Messages **\n' |
665 'http://tracker.com/42\n' | 666 'http://tracker.com/42\n' |
666 '\n' | 667 '\n' |
667 'Presubmit checks passed.\n')) | 668 'Presubmit checks passed.\n')) |
668 | 669 |
669 def testGetTrySlavesExecuter(self): | 670 def testGetTrySlavesExecuter(self): |
670 self.mox.ReplayAll() | 671 self.mox.ReplayAll() |
671 | 672 |
(...skipping 1215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1887 def testRunPythonUnitTestsNoTest(self): | 1888 def testRunPythonUnitTestsNoTest(self): |
1888 input_api = self.MockInputApi(None, False) | 1889 input_api = self.MockInputApi(None, False) |
1889 self.mox.ReplayAll() | 1890 self.mox.ReplayAll() |
1890 results = presubmit_canned_checks.RunPythonUnitTests( | 1891 results = presubmit_canned_checks.RunPythonUnitTests( |
1891 input_api, presubmit.OutputApi, []) | 1892 input_api, presubmit.OutputApi, []) |
1892 self.assertEquals(results, []) | 1893 self.assertEquals(results, []) |
1893 | 1894 |
1894 def testRunPythonUnitTestsNonExistentUpload(self): | 1895 def testRunPythonUnitTestsNonExistentUpload(self): |
1895 input_api = self.MockInputApi(None, False) | 1896 input_api = self.MockInputApi(None, False) |
1896 input_api.subprocess.check_output( | 1897 input_api.subprocess.check_output( |
1897 ['pyyyyython', '-m', '_non_existent_module'], cwd=None, env=None | 1898 ['pyyyyython', '-m', '_non_existent_module'], cwd=None, env=None, |
1898 ).AndRaise( | 1899 stderr=input_api.subprocess.STDOUT).AndRaise( |
1899 input_api.subprocess.CalledProcessError()) | 1900 input_api.subprocess.CalledProcessError()) |
1900 self.mox.ReplayAll() | 1901 self.mox.ReplayAll() |
1901 | 1902 |
1902 results = presubmit_canned_checks.RunPythonUnitTests( | 1903 results = presubmit_canned_checks.RunPythonUnitTests( |
1903 input_api, presubmit.OutputApi, ['_non_existent_module']) | 1904 input_api, presubmit.OutputApi, ['_non_existent_module']) |
1904 self.assertEquals(len(results), 1) | 1905 self.assertEquals(len(results), 1) |
1905 self.assertEquals(results[0].__class__, | 1906 self.assertEquals(results[0].__class__, |
1906 presubmit.OutputApi.PresubmitNotifyResult) | 1907 presubmit.OutputApi.PresubmitNotifyResult) |
1907 | 1908 |
1908 def testRunPythonUnitTestsNonExistentCommitting(self): | 1909 def testRunPythonUnitTestsNonExistentCommitting(self): |
1909 input_api = self.MockInputApi(None, True) | 1910 input_api = self.MockInputApi(None, True) |
1910 input_api.subprocess.check_output( | 1911 input_api.subprocess.check_output( |
1911 ['pyyyyython', '-m', '_non_existent_module'], cwd=None, env=None | 1912 ['pyyyyython', '-m', '_non_existent_module'], cwd=None, env=None, |
1912 ).AndRaise( | 1913 stderr=input_api.subprocess.STDOUT).AndRaise( |
1913 input_api.subprocess.CalledProcessError()) | 1914 input_api.subprocess.CalledProcessError()) |
1914 self.mox.ReplayAll() | 1915 self.mox.ReplayAll() |
1915 | 1916 |
1916 results = presubmit_canned_checks.RunPythonUnitTests( | 1917 results = presubmit_canned_checks.RunPythonUnitTests( |
1917 input_api, presubmit.OutputApi, ['_non_existent_module']) | 1918 input_api, presubmit.OutputApi, ['_non_existent_module']) |
1918 self.assertEquals(len(results), 1) | 1919 self.assertEquals(len(results), 1) |
1919 self.assertEquals(results[0].__class__, presubmit.OutputApi.PresubmitError) | 1920 self.assertEquals(results[0].__class__, presubmit.OutputApi.PresubmitError) |
1920 | 1921 |
1921 def testRunPythonUnitTestsFailureUpload(self): | 1922 def testRunPythonUnitTestsFailureUpload(self): |
1922 input_api = self.MockInputApi(None, False) | 1923 input_api = self.MockInputApi(None, False) |
1923 input_api.unittest = self.mox.CreateMock(unittest) | 1924 input_api.unittest = self.mox.CreateMock(unittest) |
1924 input_api.cStringIO = self.mox.CreateMock(presubmit.cStringIO) | 1925 input_api.cStringIO = self.mox.CreateMock(presubmit.cStringIO) |
1925 input_api.subprocess.check_output( | 1926 input_api.subprocess.check_output( |
1926 ['pyyyyython', '-m', 'test_module'], cwd=None, env=None).AndRaise( | 1927 ['pyyyyython', '-m', 'test_module'], cwd=None, env=None, |
| 1928 stderr=input_api.subprocess.STDOUT).AndRaise( |
1927 input_api.subprocess.CalledProcessError()) | 1929 input_api.subprocess.CalledProcessError()) |
1928 self.mox.ReplayAll() | 1930 self.mox.ReplayAll() |
1929 | 1931 |
1930 results = presubmit_canned_checks.RunPythonUnitTests( | 1932 results = presubmit_canned_checks.RunPythonUnitTests( |
1931 input_api, presubmit.OutputApi, ['test_module']) | 1933 input_api, presubmit.OutputApi, ['test_module']) |
1932 self.assertEquals(len(results), 1) | 1934 self.assertEquals(len(results), 1) |
1933 self.assertEquals(results[0].__class__, | 1935 self.assertEquals(results[0].__class__, |
1934 presubmit.OutputApi.PresubmitNotifyResult) | 1936 presubmit.OutputApi.PresubmitNotifyResult) |
1935 self.assertEquals('test_module failed!\nfoo', results[0]._message) | 1937 self.assertEquals('test_module failed!\nfoo', results[0]._message) |
1936 | 1938 |
1937 def testRunPythonUnitTestsFailureCommitting(self): | 1939 def testRunPythonUnitTestsFailureCommitting(self): |
1938 input_api = self.MockInputApi(None, True) | 1940 input_api = self.MockInputApi(None, True) |
1939 input_api.subprocess.check_output( | 1941 input_api.subprocess.check_output( |
1940 ['pyyyyython', '-m', 'test_module'], cwd=None, env=None).AndRaise( | 1942 ['pyyyyython', '-m', 'test_module'], cwd=None, env=None, |
| 1943 stderr=input_api.subprocess.STDOUT).AndRaise( |
1941 input_api.subprocess.CalledProcessError()) | 1944 input_api.subprocess.CalledProcessError()) |
1942 self.mox.ReplayAll() | 1945 self.mox.ReplayAll() |
1943 | 1946 |
1944 results = presubmit_canned_checks.RunPythonUnitTests( | 1947 results = presubmit_canned_checks.RunPythonUnitTests( |
1945 input_api, presubmit.OutputApi, ['test_module']) | 1948 input_api, presubmit.OutputApi, ['test_module']) |
1946 self.assertEquals(len(results), 1) | 1949 self.assertEquals(len(results), 1) |
1947 self.assertEquals(results[0].__class__, presubmit.OutputApi.PresubmitError) | 1950 self.assertEquals(results[0].__class__, presubmit.OutputApi.PresubmitError) |
1948 self.assertEquals('test_module failed!\nfoo', results[0]._message) | 1951 self.assertEquals('test_module failed!\nfoo', results[0]._message) |
1949 | 1952 |
1950 def testRunPythonUnitTestsSuccess(self): | 1953 def testRunPythonUnitTestsSuccess(self): |
1951 input_api = self.MockInputApi(None, False) | 1954 input_api = self.MockInputApi(None, False) |
1952 input_api.cStringIO = self.mox.CreateMock(presubmit.cStringIO) | 1955 input_api.cStringIO = self.mox.CreateMock(presubmit.cStringIO) |
1953 input_api.unittest = self.mox.CreateMock(unittest) | 1956 input_api.unittest = self.mox.CreateMock(unittest) |
1954 input_api.subprocess.check_output( | 1957 input_api.subprocess.check_output( |
1955 ['pyyyyython', '-m', 'test_module'], cwd=None, env=None) | 1958 ['pyyyyython', '-m', 'test_module'], cwd=None, env=None, |
| 1959 stderr=input_api.subprocess.STDOUT) |
1956 self.mox.ReplayAll() | 1960 self.mox.ReplayAll() |
1957 | 1961 |
1958 results = presubmit_canned_checks.RunPythonUnitTests( | 1962 results = presubmit_canned_checks.RunPythonUnitTests( |
1959 input_api, presubmit.OutputApi, ['test_module']) | 1963 input_api, presubmit.OutputApi, ['test_module']) |
1960 self.assertEquals(len(results), 0) | 1964 self.assertEquals(len(results), 0) |
1961 | 1965 |
1962 def testCheckRietveldTryJobExecutionBad(self): | 1966 def testCheckRietveldTryJobExecutionBad(self): |
1963 change = self.mox.CreateMock(presubmit.SvnChange) | 1967 change = self.mox.CreateMock(presubmit.SvnChange) |
1964 change.scm = 'svn' | 1968 change.scm = 'svn' |
1965 change.issue = 2 | 1969 change.issue = 2 |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2163 self.assertEqual(1, len(results)) | 2167 self.assertEqual(1, len(results)) |
2164 self.assertEqual( | 2168 self.assertEqual( |
2165 presubmit.OutputApi.PresubmitPromptWarning, results[0].__class__) | 2169 presubmit.OutputApi.PresubmitPromptWarning, results[0].__class__) |
2166 self.checkstdout('Running allo\nRunning bar.py\n') | 2170 self.checkstdout('Running allo\nRunning bar.py\n') |
2167 | 2171 |
2168 def testCannedRunUnitTestsInDirectory(self): | 2172 def testCannedRunUnitTestsInDirectory(self): |
2169 change = presubmit.Change( | 2173 change = presubmit.Change( |
2170 'foo1', 'description1', self.fake_root_dir, None, 0, 0, None) | 2174 'foo1', 'description1', self.fake_root_dir, None, 0, 0, None) |
2171 input_api = self.MockInputApi(change, False) | 2175 input_api = self.MockInputApi(change, False) |
2172 input_api.verbose = True | 2176 input_api.verbose = True |
| 2177 input_api.logging = self.mox.CreateMock(logging) |
2173 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) | 2178 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) |
2174 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) | 2179 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) |
2175 path = presubmit.os.path.join(self.fake_root_dir, 'random_directory') | 2180 path = presubmit.os.path.join(self.fake_root_dir, 'random_directory') |
2176 input_api.os_listdir(path).AndReturn(['.', '..', 'a', 'b', 'c']) | 2181 input_api.os_listdir(path).AndReturn(['.', '..', 'a', 'b', 'c']) |
2177 input_api.os_path.isfile = lambda x: not x.endswith('.') | 2182 input_api.os_path.isfile = lambda x: not x.endswith('.') |
2178 input_api.subprocess.check_call( | 2183 input_api.subprocess.check_call( |
2179 [presubmit.os.path.join('random_directory', 'b'), '--verbose'], | 2184 [presubmit.os.path.join('random_directory', 'b'), '--verbose'], |
2180 cwd=self.fake_root_dir) | 2185 cwd=self.fake_root_dir) |
| 2186 input_api.logging.debug('Found 5 files, running 1') |
2181 | 2187 |
2182 self.mox.ReplayAll() | 2188 self.mox.ReplayAll() |
2183 results = presubmit_canned_checks.RunUnitTestsInDirectory( | 2189 results = presubmit_canned_checks.RunUnitTestsInDirectory( |
2184 input_api, | 2190 input_api, |
2185 presubmit.OutputApi, | 2191 presubmit.OutputApi, |
2186 'random_directory', | 2192 'random_directory', |
2187 whitelist=['^a$', '^b$'], | 2193 whitelist=['^a$', '^b$'], |
2188 blacklist=['a']) | 2194 blacklist=['a']) |
2189 self.assertEqual(results, []) | 2195 self.assertEqual(results, []) |
2190 self.checkstdout( | 2196 self.checkstdout( |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2225 owners_check=True) | 2231 owners_check=True) |
2226 self.assertEqual(1, len(results)) | 2232 self.assertEqual(1, len(results)) |
2227 self.assertEqual( | 2233 self.assertEqual( |
2228 'Found line ending with white spaces in:', results[0]._message) | 2234 'Found line ending with white spaces in:', results[0]._message) |
2229 self.checkstdout('') | 2235 self.checkstdout('') |
2230 | 2236 |
2231 | 2237 |
2232 if __name__ == '__main__': | 2238 if __name__ == '__main__': |
2233 import unittest | 2239 import unittest |
2234 unittest.main() | 2240 unittest.main() |
OLD | NEW |