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 functools |
10 import logging | 11 import logging |
11 import os | 12 import os |
12 import StringIO | 13 import StringIO |
13 import sys | 14 import sys |
14 import time | 15 import time |
15 import unittest | 16 import unittest |
16 | 17 |
17 _ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | 18 _ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
18 sys.path.insert(0, _ROOT) | 19 sys.path.insert(0, _ROOT) |
19 | 20 |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 'GetTrySlavesExecuter', 'GitAffectedFile', 'CallCommand', 'CommandData', | 163 'GetTrySlavesExecuter', 'GitAffectedFile', 'CallCommand', 'CommandData', |
163 'GitChange', 'InputApi', 'ListRelevantPresubmitFiles', 'Main', | 164 'GitChange', 'InputApi', 'ListRelevantPresubmitFiles', 'Main', |
164 'NonexistantCannedCheckFilter', 'OutputApi', 'ParseFiles', | 165 'NonexistantCannedCheckFilter', 'OutputApi', 'ParseFiles', |
165 'PresubmitFailure', 'PresubmitExecuter', 'PresubmitOutput', 'ScanSubDirs', | 166 'PresubmitFailure', 'PresubmitExecuter', 'PresubmitOutput', 'ScanSubDirs', |
166 'SvnAffectedFile', 'SvnChange', 'cPickle', 'cpplint', 'cStringIO', | 167 'SvnAffectedFile', 'SvnChange', 'cPickle', 'cpplint', 'cStringIO', |
167 'contextlib', 'canned_check_filter', 'fix_encoding', 'fnmatch', | 168 'contextlib', 'canned_check_filter', 'fix_encoding', 'fnmatch', |
168 'gclient_utils', 'glob', 'inspect', 'json', 'load_files', 'logging', | 169 'gclient_utils', 'glob', 'inspect', 'json', 'load_files', 'logging', |
169 'marshal', 'normpath', 'optparse', 'os', 'owners', 'pickle', | 170 'marshal', 'normpath', 'optparse', 'os', 'owners', 'pickle', |
170 'presubmit_canned_checks', 'random', 're', 'rietveld', 'scm', | 171 'presubmit_canned_checks', 'random', 're', 'rietveld', 'scm', |
171 'subprocess', 'sys', 'tempfile', 'time', 'traceback', 'types', 'unittest', | 172 'subprocess', 'sys', 'tempfile', 'time', 'traceback', 'types', 'unittest', |
172 'urllib2', 'warn', 'collections', 'multiprocessing', | 173 'urllib2', 'warn', 'multiprocessing', |
173 ] | 174 ] |
174 # If this test fails, you should add the relevant test. | 175 # If this test fails, you should add the relevant test. |
175 self.compareMembers(presubmit, members) | 176 self.compareMembers(presubmit, members) |
176 | 177 |
177 def testCannedCheckFilter(self): | 178 def testCannedCheckFilter(self): |
178 canned = presubmit.presubmit_canned_checks | 179 canned = presubmit.presubmit_canned_checks |
179 orig = canned.CheckOwners | 180 orig = canned.CheckOwners |
180 with presubmit.canned_check_filter(['CheckOwners']): | 181 with presubmit.canned_check_filter(['CheckOwners']): |
181 self.assertNotEqual(canned.CheckOwners, orig) | 182 self.assertNotEqual(canned.CheckOwners, orig) |
182 self.assertEqual(canned.CheckOwners(None, None), []) | 183 self.assertEqual(canned.CheckOwners(None, None), []) |
(...skipping 1530 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1713 | 1714 |
1714 input_api.change = change | 1715 input_api.change = change |
1715 input_api.host_url = 'http://localhost' | 1716 input_api.host_url = 'http://localhost' |
1716 input_api.is_committing = committing | 1717 input_api.is_committing = committing |
1717 input_api.tbr = False | 1718 input_api.tbr = False |
1718 input_api.python_executable = 'pyyyyython' | 1719 input_api.python_executable = 'pyyyyython' |
1719 input_api.platform = sys.platform | 1720 input_api.platform = sys.platform |
1720 input_api.time = time | 1721 input_api.time = time |
1721 input_api.canned_checks = presubmit_canned_checks | 1722 input_api.canned_checks = presubmit_canned_checks |
1722 input_api.Command = presubmit.CommandData | 1723 input_api.Command = presubmit.CommandData |
1723 input_api.RunTests = presubmit.InputApi.RunTests | 1724 input_api.RunTests = functools.partial( |
| 1725 presubmit.InputApi.RunTests, input_api) |
1724 return input_api | 1726 return input_api |
1725 | 1727 |
1726 def testMembersChanged(self): | 1728 def testMembersChanged(self): |
1727 self.mox.ReplayAll() | 1729 self.mox.ReplayAll() |
1728 members = [ | 1730 members = [ |
1729 'CheckBuildbotPendingBuilds', | 1731 'CheckBuildbotPendingBuilds', |
1730 'CheckChangeHasBugField', 'CheckChangeHasDescription', | 1732 'CheckChangeHasBugField', 'CheckChangeHasDescription', |
1731 'CheckChangeHasNoStrayWhitespace', | 1733 'CheckChangeHasNoStrayWhitespace', |
1732 'CheckChangeHasOnlyOneEol', 'CheckChangeHasNoCR', | 1734 'CheckChangeHasOnlyOneEol', 'CheckChangeHasNoCR', |
1733 'CheckChangeHasNoCrAndHasOnlyOneEol', 'CheckChangeHasNoTabs', | 1735 'CheckChangeHasNoCrAndHasOnlyOneEol', 'CheckChangeHasNoTabs', |
(...skipping 616 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2350 input_api.cStringIO = self.mox.CreateMock(presubmit.cStringIO) | 2352 input_api.cStringIO = self.mox.CreateMock(presubmit.cStringIO) |
2351 CommHelper(input_api, ['pyyyyython', '-m', 'test_module'], | 2353 CommHelper(input_api, ['pyyyyython', '-m', 'test_module'], |
2352 ret=(('foo', None), 1), cwd=None, env=None) | 2354 ret=(('foo', None), 1), cwd=None, env=None) |
2353 self.mox.ReplayAll() | 2355 self.mox.ReplayAll() |
2354 | 2356 |
2355 results = presubmit_canned_checks.RunPythonUnitTests( | 2357 results = presubmit_canned_checks.RunPythonUnitTests( |
2356 input_api, presubmit.OutputApi, ['test_module']) | 2358 input_api, presubmit.OutputApi, ['test_module']) |
2357 self.assertEquals(len(results), 1) | 2359 self.assertEquals(len(results), 1) |
2358 self.assertEquals(results[0].__class__, | 2360 self.assertEquals(results[0].__class__, |
2359 presubmit.OutputApi.PresubmitNotifyResult) | 2361 presubmit.OutputApi.PresubmitNotifyResult) |
2360 self.assertEquals('test_module failed\nfoo', results[0]._message) | 2362 self.assertEquals('test_module (0.00s) failed\nfoo', results[0]._message) |
2361 | 2363 |
2362 def testRunPythonUnitTestsFailureCommitting(self): | 2364 def testRunPythonUnitTestsFailureCommitting(self): |
2363 input_api = self.MockInputApi(None, True) | 2365 input_api = self.MockInputApi(None, True) |
2364 CommHelper(input_api, ['pyyyyython', '-m', 'test_module'], | 2366 CommHelper(input_api, ['pyyyyython', '-m', 'test_module'], |
2365 ret=(('foo', None), 1), cwd=None, env=None) | 2367 ret=(('foo', None), 1), cwd=None, env=None) |
2366 self.mox.ReplayAll() | 2368 self.mox.ReplayAll() |
2367 | 2369 |
2368 results = presubmit_canned_checks.RunPythonUnitTests( | 2370 results = presubmit_canned_checks.RunPythonUnitTests( |
2369 input_api, presubmit.OutputApi, ['test_module']) | 2371 input_api, presubmit.OutputApi, ['test_module']) |
2370 self.assertEquals(len(results), 1) | 2372 self.assertEquals(len(results), 1) |
2371 self.assertEquals(results[0].__class__, presubmit.OutputApi.PresubmitError) | 2373 self.assertEquals(results[0].__class__, presubmit.OutputApi.PresubmitError) |
2372 self.assertEquals('test_module failed\nfoo', results[0]._message) | 2374 self.assertEquals('test_module (0.00s) failed\nfoo', results[0]._message) |
2373 | 2375 |
2374 def testRunPythonUnitTestsSuccess(self): | 2376 def testRunPythonUnitTestsSuccess(self): |
2375 input_api = self.MockInputApi(None, False) | 2377 input_api = self.MockInputApi(None, False) |
2376 input_api.cStringIO = self.mox.CreateMock(presubmit.cStringIO) | 2378 input_api.cStringIO = self.mox.CreateMock(presubmit.cStringIO) |
2377 input_api.unittest = self.mox.CreateMock(unittest) | 2379 input_api.unittest = self.mox.CreateMock(unittest) |
2378 CommHelper(input_api, ['pyyyyython', '-m', 'test_module'], | 2380 CommHelper(input_api, ['pyyyyython', '-m', 'test_module'], |
2379 cwd=None, env=None) | 2381 cwd=None, env=None) |
2380 self.mox.ReplayAll() | 2382 self.mox.ReplayAll() |
2381 | 2383 |
2382 results = presubmit_canned_checks.RunPythonUnitTests( | 2384 results = presubmit_canned_checks.RunPythonUnitTests( |
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2648 cmd = ['bar.py', '--verbose'] | 2650 cmd = ['bar.py', '--verbose'] |
2649 if input_api.platform == 'win32': | 2651 if input_api.platform == 'win32': |
2650 cmd.insert(0, input_api.python_executable) | 2652 cmd.insert(0, input_api.python_executable) |
2651 CommHelper(input_api, cmd, cwd=self.fake_root_dir, ret=(('', None), 1)) | 2653 CommHelper(input_api, cmd, cwd=self.fake_root_dir, ret=(('', None), 1)) |
2652 | 2654 |
2653 self.mox.ReplayAll() | 2655 self.mox.ReplayAll() |
2654 results = presubmit_canned_checks.RunUnitTests( | 2656 results = presubmit_canned_checks.RunUnitTests( |
2655 input_api, | 2657 input_api, |
2656 presubmit.OutputApi, | 2658 presubmit.OutputApi, |
2657 unit_tests) | 2659 unit_tests) |
2658 self.assertEqual(1, len(results)) | 2660 self.assertEqual(2, len(results)) |
2659 self.assertEqual( | 2661 self.assertEqual( |
2660 presubmit.OutputApi.PresubmitPromptWarning, results[0].__class__) | 2662 presubmit.OutputApi.PresubmitNotifyResult, results[0].__class__) |
2661 self.checkstdout('Running allo\nRunning bar.py\n') | 2663 self.assertEqual( |
| 2664 presubmit.OutputApi.PresubmitPromptWarning, results[1].__class__) |
| 2665 self.checkstdout('') |
2662 | 2666 |
2663 def testCannedRunUnitTestsInDirectory(self): | 2667 def testCannedRunUnitTestsInDirectory(self): |
2664 change = presubmit.Change( | 2668 change = presubmit.Change( |
2665 'foo1', 'description1', self.fake_root_dir, None, 0, 0, None) | 2669 'foo1', 'description1', self.fake_root_dir, None, 0, 0, None) |
2666 input_api = self.MockInputApi(change, False) | 2670 input_api = self.MockInputApi(change, False) |
2667 input_api.verbose = True | 2671 input_api.verbose = True |
2668 input_api.logging = self.mox.CreateMock(logging) | 2672 input_api.logging = self.mox.CreateMock(logging) |
2669 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) | 2673 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) |
2670 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) | 2674 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) |
2671 path = presubmit.os.path.join(self.fake_root_dir, 'random_directory') | 2675 path = presubmit.os.path.join(self.fake_root_dir, 'random_directory') |
2672 input_api.os_listdir(path).AndReturn(['.', '..', 'a', 'b', 'c']) | 2676 input_api.os_listdir(path).AndReturn(['.', '..', 'a', 'b', 'c']) |
2673 input_api.os_path.isfile = lambda x: not x.endswith('.') | 2677 input_api.os_path.isfile = lambda x: not x.endswith('.') |
2674 CommHelper( | 2678 CommHelper( |
2675 input_api, | 2679 input_api, |
2676 [presubmit.os.path.join('random_directory', 'b'), '--verbose'], | 2680 [presubmit.os.path.join('random_directory', 'b'), '--verbose'], |
2677 cwd=self.fake_root_dir) | 2681 cwd=self.fake_root_dir) |
2678 input_api.logging.debug('Found 5 files, running 1') | 2682 input_api.logging.debug('Found 5 files, running 1') |
2679 | 2683 |
2680 self.mox.ReplayAll() | 2684 self.mox.ReplayAll() |
2681 results = presubmit_canned_checks.RunUnitTestsInDirectory( | 2685 results = presubmit_canned_checks.RunUnitTestsInDirectory( |
2682 input_api, | 2686 input_api, |
2683 presubmit.OutputApi, | 2687 presubmit.OutputApi, |
2684 'random_directory', | 2688 'random_directory', |
2685 whitelist=['^a$', '^b$'], | 2689 whitelist=['^a$', '^b$'], |
2686 blacklist=['a']) | 2690 blacklist=['a']) |
2687 self.assertEqual(results, []) | 2691 self.assertEqual(1, len(results)) |
2688 self.checkstdout( | 2692 self.assertEqual( |
2689 'Running %s\n' % presubmit.os.path.join('random_directory', 'b')) | 2693 presubmit.OutputApi.PresubmitNotifyResult, results[0].__class__) |
| 2694 self.checkstdout('') |
2690 | 2695 |
2691 def testPanProjectChecks(self): | 2696 def testPanProjectChecks(self): |
2692 # Make sure it accepts both list and tuples. | 2697 # Make sure it accepts both list and tuples. |
2693 change = presubmit.Change( | 2698 change = presubmit.Change( |
2694 'foo1', 'description1', self.fake_root_dir, None, 0, 0, None) | 2699 'foo1', 'description1', self.fake_root_dir, None, 0, 0, None) |
2695 input_api = self.MockInputApi(change, False) | 2700 input_api = self.MockInputApi(change, False) |
2696 affected_file = self.mox.CreateMock(presubmit.SvnAffectedFile) | 2701 affected_file = self.mox.CreateMock(presubmit.SvnAffectedFile) |
2697 for _ in range(3): | 2702 for _ in range(3): |
2698 input_api.AffectedFiles(file_filter=mox.IgnoreArg(), include_deletes=False | 2703 input_api.AffectedFiles(file_filter=mox.IgnoreArg(), include_deletes=False |
2699 ).AndReturn([affected_file]) | 2704 ).AndReturn([affected_file]) |
(...skipping 24 matching lines...) Expand all Loading... |
2724 owners_check=False) | 2729 owners_check=False) |
2725 self.assertEqual(1, len(results)) | 2730 self.assertEqual(1, len(results)) |
2726 self.assertEqual( | 2731 self.assertEqual( |
2727 'Found line ending with white spaces in:', results[0]._message) | 2732 'Found line ending with white spaces in:', results[0]._message) |
2728 self.checkstdout('') | 2733 self.checkstdout('') |
2729 | 2734 |
2730 | 2735 |
2731 if __name__ == '__main__': | 2736 if __name__ == '__main__': |
2732 import unittest | 2737 import unittest |
2733 unittest.main() | 2738 unittest.main() |
OLD | NEW |