| 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 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 """General presubmit_support.py tests (excluding InputApi and OutputApi).""" | 132 """General presubmit_support.py tests (excluding InputApi and OutputApi).""" |
| 133 | 133 |
| 134 _INHERIT_SETTINGS = 'inherit-review-settings-ok' | 134 _INHERIT_SETTINGS = 'inherit-review-settings-ok' |
| 135 | 135 |
| 136 def testMembersChanged(self): | 136 def testMembersChanged(self): |
| 137 self.mox.ReplayAll() | 137 self.mox.ReplayAll() |
| 138 members = [ | 138 members = [ |
| 139 'AffectedFile', 'Change', 'DoGetTrySlaves', 'DoPresubmitChecks', | 139 'AffectedFile', 'Change', 'DoGetTrySlaves', 'DoPresubmitChecks', |
| 140 'GetTrySlavesExecuter', 'GitAffectedFile', | 140 'GetTrySlavesExecuter', 'GitAffectedFile', |
| 141 'GitChange', 'InputApi', 'ListRelevantPresubmitFiles', 'Main', | 141 'GitChange', 'InputApi', 'ListRelevantPresubmitFiles', 'Main', |
| 142 'NotImplementedException', 'OutputApi', 'ParseFiles', | 142 'OutputApi', 'ParseFiles', 'PresubmitFailure', |
| 143 'PresubmitExecuter', 'PresubmitOutput', 'ScanSubDirs', | 143 'PresubmitExecuter', 'PresubmitOutput', 'ScanSubDirs', |
| 144 'SvnAffectedFile', 'SvnChange', 'cPickle', 'cStringIO', | 144 'SvnAffectedFile', 'SvnChange', 'cPickle', 'cStringIO', |
| 145 'exceptions', 'fix_encoding', 'fnmatch', 'gclient_utils', 'glob', 'json', | 145 'fix_encoding', 'fnmatch', 'gclient_utils', 'glob', 'json', |
| 146 'load_files', | 146 'load_files', |
| 147 'logging', 'marshal', 'normpath', 'optparse', 'os', 'owners', 'pickle', | 147 'logging', 'marshal', 'normpath', 'optparse', 'os', 'owners', 'pickle', |
| 148 'presubmit_canned_checks', 'random', 're', 'scm', 'subprocess', | 148 'presubmit_canned_checks', 'random', 're', 'scm', 'subprocess', |
| 149 'sys', 'tempfile', 'time', 'traceback', 'types', 'unittest', 'urllib2', | 149 'sys', 'tempfile', 'time', 'traceback', 'types', 'unittest', 'urllib2', |
| 150 'warn', | 150 'warn', |
| 151 ] | 151 ] |
| 152 # If this test fails, you should add the relevant test. | 152 # If this test fails, you should add the relevant test. |
| 153 self.compareMembers(presubmit, members) | 153 self.compareMembers(presubmit, members) |
| 154 | 154 |
| 155 def testListRelevantPresubmitFiles(self): | 155 def testListRelevantPresubmitFiles(self): |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 338 'this is a change', | 338 'this is a change', |
| 339 'STORY=http://tracker/123') | 339 'STORY=http://tracker/123') |
| 340 files = [ | 340 files = [ |
| 341 ['A', 'foo\\blat.cc'], | 341 ['A', 'foo\\blat.cc'], |
| 342 ] | 342 ] |
| 343 fake_presubmit = presubmit.os.path.join(self.fake_root_dir, 'PRESUBMIT.py') | 343 fake_presubmit = presubmit.os.path.join(self.fake_root_dir, 'PRESUBMIT.py') |
| 344 self.mox.ReplayAll() | 344 self.mox.ReplayAll() |
| 345 | 345 |
| 346 change = presubmit.Change('mychange', '\n'.join(description_lines), | 346 change = presubmit.Change('mychange', '\n'.join(description_lines), |
| 347 self.fake_root_dir, files, 0, 0) | 347 self.fake_root_dir, files, 0, 0) |
| 348 executer = presubmit.PresubmitExecuter(change, False, False, None) | 348 executer = presubmit.PresubmitExecuter(change, False, False, None, False) |
| 349 self.failIf(executer.ExecPresubmitScript('', fake_presubmit)) | 349 self.failIf(executer.ExecPresubmitScript('', fake_presubmit)) |
| 350 # No error if no on-upload entry point | 350 # No error if no on-upload entry point |
| 351 self.failIf(executer.ExecPresubmitScript( | 351 self.failIf(executer.ExecPresubmitScript( |
| 352 ('def CheckChangeOnCommit(input_api, output_api):\n' | 352 ('def CheckChangeOnCommit(input_api, output_api):\n' |
| 353 ' return (output_api.PresubmitError("!!"))\n'), | 353 ' return (output_api.PresubmitError("!!"))\n'), |
| 354 fake_presubmit | 354 fake_presubmit |
| 355 )) | 355 )) |
| 356 | 356 |
| 357 executer = presubmit.PresubmitExecuter(change, True, False, None) | 357 executer = presubmit.PresubmitExecuter(change, True, False, None, False) |
| 358 # No error if no on-commit entry point | 358 # No error if no on-commit entry point |
| 359 self.failIf(executer.ExecPresubmitScript( | 359 self.failIf(executer.ExecPresubmitScript( |
| 360 ('def CheckChangeOnUpload(input_api, output_api):\n' | 360 ('def CheckChangeOnUpload(input_api, output_api):\n' |
| 361 ' return (output_api.PresubmitError("!!"))\n'), | 361 ' return (output_api.PresubmitError("!!"))\n'), |
| 362 fake_presubmit | 362 fake_presubmit |
| 363 )) | 363 )) |
| 364 | 364 |
| 365 self.failIf(executer.ExecPresubmitScript( | 365 self.failIf(executer.ExecPresubmitScript( |
| 366 ('def CheckChangeOnUpload(input_api, output_api):\n' | 366 ('def CheckChangeOnUpload(input_api, output_api):\n' |
| 367 ' if not input_api.change.STORY:\n' | 367 ' if not input_api.change.STORY:\n' |
| 368 ' return (output_api.PresubmitError("!!"))\n' | 368 ' return (output_api.PresubmitError("!!"))\n' |
| 369 ' else:\n' | 369 ' else:\n' |
| 370 ' return ()'), | 370 ' return ()'), |
| 371 fake_presubmit | 371 fake_presubmit |
| 372 )) | 372 )) |
| 373 | 373 |
| 374 self.failUnless(executer.ExecPresubmitScript( | 374 self.failUnless(executer.ExecPresubmitScript( |
| 375 ('def CheckChangeOnCommit(input_api, output_api):\n' | 375 ('def CheckChangeOnCommit(input_api, output_api):\n' |
| 376 ' if not input_api.change.NOSUCHKEY:\n' | 376 ' if not input_api.change.NOSUCHKEY:\n' |
| 377 ' return [output_api.PresubmitError("!!")]\n' | 377 ' return [output_api.PresubmitError("!!")]\n' |
| 378 ' else:\n' | 378 ' else:\n' |
| 379 ' return ()'), | 379 ' return ()'), |
| 380 fake_presubmit | 380 fake_presubmit |
| 381 )) | 381 )) |
| 382 | 382 |
| 383 self.assertRaises(presubmit.exceptions.RuntimeError, | 383 self.assertRaises(presubmit.PresubmitFailure, |
| 384 executer.ExecPresubmitScript, | 384 executer.ExecPresubmitScript, |
| 385 'def CheckChangeOnCommit(input_api, output_api):\n' | 385 'def CheckChangeOnCommit(input_api, output_api):\n' |
| 386 ' return "foo"', | 386 ' return "foo"', |
| 387 fake_presubmit) | 387 fake_presubmit) |
| 388 | 388 |
| 389 self.assertRaises(presubmit.exceptions.RuntimeError, | 389 self.assertRaises(presubmit.PresubmitFailure, |
| 390 executer.ExecPresubmitScript, | 390 executer.ExecPresubmitScript, |
| 391 'def CheckChangeOnCommit(input_api, output_api):\n' | 391 'def CheckChangeOnCommit(input_api, output_api):\n' |
| 392 ' return ["foo"]', | 392 ' return ["foo"]', |
| 393 fake_presubmit) | 393 fake_presubmit) |
| 394 | 394 |
| 395 def testDoPresubmitChecks(self): | 395 def testDoPresubmitChecks(self): |
| 396 join = presubmit.os.path.join | 396 join = presubmit.os.path.join |
| 397 description_lines = ('Hello there', | 397 description_lines = ('Hello there', |
| 398 'this is a change', | 398 'this is a change', |
| 399 'STORY=http://tracker/123') | 399 'STORY=http://tracker/123') |
| (...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 611 '\n' | 611 '\n' |
| 612 '** Presubmit Messages **\n' | 612 '** Presubmit Messages **\n' |
| 613 'http://tracker.com/42\n' | 613 'http://tracker.com/42\n' |
| 614 '\n' | 614 '\n' |
| 615 'Presubmit checks passed.\n')) | 615 'Presubmit checks passed.\n')) |
| 616 | 616 |
| 617 def testGetTrySlavesExecuter(self): | 617 def testGetTrySlavesExecuter(self): |
| 618 self.mox.ReplayAll() | 618 self.mox.ReplayAll() |
| 619 | 619 |
| 620 executer = presubmit.GetTrySlavesExecuter() | 620 executer = presubmit.GetTrySlavesExecuter() |
| 621 self.assertEqual([], executer.ExecPresubmitScript('')) | 621 self.assertEqual([], executer.ExecPresubmitScript('', '')) |
| 622 self.assertEqual([], executer.ExecPresubmitScript('def foo():\n return\n')) | 622 self.assertEqual( |
| 623 [], executer.ExecPresubmitScript('def foo():\n return\n', '')) |
| 623 | 624 |
| 624 # bad results | 625 # bad results |
| 625 starts_with_space_result = [' starts_with_space'] | 626 starts_with_space_result = [' starts_with_space'] |
| 626 not_list_result1 = "'foo'" | 627 not_list_result1 = "'foo'" |
| 627 not_list_result2 = "('a', 'tuple')" | 628 not_list_result2 = "('a', 'tuple')" |
| 628 for result in starts_with_space_result, not_list_result1, not_list_result2: | 629 for result in starts_with_space_result, not_list_result1, not_list_result2: |
| 629 self.assertRaises(presubmit.exceptions.RuntimeError, | 630 self.assertRaises(presubmit.PresubmitFailure, |
| 630 executer.ExecPresubmitScript, | 631 executer.ExecPresubmitScript, |
| 631 self.presubmit_tryslave % result) | 632 self.presubmit_tryslave % result, '') |
| 632 | 633 |
| 633 # good results | 634 # good results |
| 634 expected_result = ['1', '2', '3'] | 635 expected_result = ['1', '2', '3'] |
| 635 empty_result = [] | 636 empty_result = [] |
| 636 space_in_name_result = ['foo bar', '1\t2 3'] | 637 space_in_name_result = ['foo bar', '1\t2 3'] |
| 637 for result in expected_result, empty_result, space_in_name_result: | 638 for result in expected_result, empty_result, space_in_name_result: |
| 638 self.assertEqual(result, | 639 self.assertEqual( |
| 639 executer.ExecPresubmitScript(self.presubmit_tryslave % | 640 result, |
| 640 str(result))) | 641 executer.ExecPresubmitScript(self.presubmit_tryslave % result, '')) |
| 641 | 642 |
| 642 def testDoGetTrySlaves(self): | 643 def testDoGetTrySlaves(self): |
| 643 join = presubmit.os.path.join | 644 join = presubmit.os.path.join |
| 644 filename = 'foo.cc' | 645 filename = 'foo.cc' |
| 645 filename_linux = join('linux_only', 'penguin.cc') | 646 filename_linux = join('linux_only', 'penguin.cc') |
| 646 root_presubmit = join(self.fake_root_dir, 'PRESUBMIT.py') | 647 root_presubmit = join(self.fake_root_dir, 'PRESUBMIT.py') |
| 647 linux_presubmit = join(self.fake_root_dir, 'linux_only', 'PRESUBMIT.py') | 648 linux_presubmit = join(self.fake_root_dir, 'linux_only', 'PRESUBMIT.py') |
| 648 inherit_path = presubmit.os.path.join(self.fake_root_dir, | 649 inherit_path = presubmit.os.path.join(self.fake_root_dir, |
| 649 self._INHERIT_SETTINGS) | 650 self._INHERIT_SETTINGS) |
| 650 | 651 |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 723 'AbsoluteLocalPaths', 'AffectedFiles', 'AffectedSourceFiles', | 724 'AbsoluteLocalPaths', 'AffectedFiles', 'AffectedSourceFiles', |
| 724 'AffectedTextFiles', | 725 'AffectedTextFiles', |
| 725 'DEFAULT_BLACK_LIST', 'DEFAULT_WHITE_LIST', | 726 'DEFAULT_BLACK_LIST', 'DEFAULT_WHITE_LIST', |
| 726 'DepotToLocalPath', 'FilterSourceFile', 'LocalPaths', | 727 'DepotToLocalPath', 'FilterSourceFile', 'LocalPaths', |
| 727 'LocalToDepotPath', | 728 'LocalToDepotPath', |
| 728 'PresubmitLocalPath', 'ReadFile', 'RightHandSideLines', 'ServerPaths', | 729 'PresubmitLocalPath', 'ReadFile', 'RightHandSideLines', 'ServerPaths', |
| 729 'basename', 'cPickle', 'cStringIO', 'canned_checks', 'change', 'environ', | 730 'basename', 'cPickle', 'cStringIO', 'canned_checks', 'change', 'environ', |
| 730 'host_url', 'is_committing', 'json', 'marshal', 'os_listdir', 'os_walk', | 731 'host_url', 'is_committing', 'json', 'marshal', 'os_listdir', 'os_walk', |
| 731 'os_path', 'owners_db', 'pickle', 'platform', 'python_executable', 're', | 732 'os_path', 'owners_db', 'pickle', 'platform', 'python_executable', 're', |
| 732 'subprocess', 'tbr', 'tempfile', 'time', 'traceback', 'unittest', | 733 'subprocess', 'tbr', 'tempfile', 'time', 'traceback', 'unittest', |
| 733 'urllib2', 'version', | 734 'urllib2', 'version', 'verbose', |
| 734 ] | 735 ] |
| 735 # If this test fails, you should add the relevant test. | 736 # If this test fails, you should add the relevant test. |
| 736 self.compareMembers(presubmit.InputApi(self.fake_change, './.', False, | 737 self.compareMembers( |
| 737 False, None), | 738 presubmit.InputApi(self.fake_change, './.', False, False, None, False), |
| 738 members) | 739 members) |
| 739 | 740 |
| 740 def testDepotToLocalPath(self): | 741 def testDepotToLocalPath(self): |
| 741 presubmit.scm.SVN.CaptureInfo('svn://foo/smurf').AndReturn( | 742 presubmit.scm.SVN.CaptureInfo('svn://foo/smurf').AndReturn( |
| 742 {'Path': 'prout'}) | 743 {'Path': 'prout'}) |
| 743 presubmit.scm.SVN.CaptureInfo('svn:/foo/notfound/burp').AndReturn({}) | 744 presubmit.scm.SVN.CaptureInfo('svn:/foo/notfound/burp').AndReturn({}) |
| 744 self.mox.ReplayAll() | 745 self.mox.ReplayAll() |
| 745 | 746 |
| 746 path = presubmit.InputApi(self.fake_change, './p', False, False, | 747 path = presubmit.InputApi( |
| 747 None).DepotToLocalPath('svn://foo/smurf') | 748 self.fake_change, './p', False, False, None, False).DepotToLocalPath( |
| 749 'svn://foo/smurf') |
| 748 self.failUnless(path == 'prout') | 750 self.failUnless(path == 'prout') |
| 749 path = presubmit.InputApi(self.fake_change, './p', False, False, | 751 path = presubmit.InputApi( |
| 750 None).DepotToLocalPath('svn:/foo/notfound/burp') | 752 self.fake_change, './p', False, False, None, False).DepotToLocalPath( |
| 753 'svn:/foo/notfound/burp') |
| 751 self.failUnless(path == None) | 754 self.failUnless(path == None) |
| 752 | 755 |
| 753 def testLocalToDepotPath(self): | 756 def testLocalToDepotPath(self): |
| 754 presubmit.scm.SVN.CaptureInfo('smurf').AndReturn({'URL': 'svn://foo'}) | 757 presubmit.scm.SVN.CaptureInfo('smurf').AndReturn({'URL': 'svn://foo'}) |
| 755 presubmit.scm.SVN.CaptureInfo('notfound-food').AndReturn({}) | 758 presubmit.scm.SVN.CaptureInfo('notfound-food').AndReturn({}) |
| 756 self.mox.ReplayAll() | 759 self.mox.ReplayAll() |
| 757 path = presubmit.InputApi(self.fake_change, './p', False, False, | 760 path = presubmit.InputApi( |
| 758 None).LocalToDepotPath('smurf') | 761 self.fake_change, './p', False, False, None, False).LocalToDepotPath( |
| 762 'smurf') |
| 759 self.assertEqual(path, 'svn://foo') | 763 self.assertEqual(path, 'svn://foo') |
| 760 path = presubmit.InputApi(self.fake_change, './p', False, False, | 764 path = presubmit.InputApi( |
| 761 None).LocalToDepotPath('notfound-food') | 765 self.fake_change, './p', False, False, None, False).LocalToDepotPath( |
| 762 self.failUnless(path == None) | 766 'notfound-food') |
| 767 self.assertEquals(path, None) |
| 763 | 768 |
| 764 def testInputApiConstruction(self): | 769 def testInputApiConstruction(self): |
| 765 self.mox.ReplayAll() | 770 self.mox.ReplayAll() |
| 766 api = presubmit.InputApi(self.fake_change, | 771 api = presubmit.InputApi( |
| 767 presubmit_path='foo/path/PRESUBMIT.py', | 772 self.fake_change, |
| 768 is_committing=False, tbr=False, host_url=None) | 773 presubmit_path='foo/path/PRESUBMIT.py', |
| 774 is_committing=False, tbr=False, host_url=None, verbose=False) |
| 769 self.assertEquals(api.PresubmitLocalPath(), 'foo/path') | 775 self.assertEquals(api.PresubmitLocalPath(), 'foo/path') |
| 770 self.assertEquals(api.change, self.fake_change) | 776 self.assertEquals(api.change, self.fake_change) |
| 771 self.assertEquals(api.host_url, 'http://codereview.chromium.org') | 777 self.assertEquals(api.host_url, 'http://codereview.chromium.org') |
| 772 | 778 |
| 773 def testInputApiPresubmitScriptFiltering(self): | 779 def testInputApiPresubmitScriptFiltering(self): |
| 774 join = presubmit.os.path.join | 780 join = presubmit.os.path.join |
| 775 description_lines = ('Hello there', | 781 description_lines = ('Hello there', |
| 776 'this is a change', | 782 'this is a change', |
| 777 'BUG=123', | 783 'BUG=123', |
| 778 ' STORY =http://foo/ \t', | 784 ' STORY =http://foo/ \t', |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 814 presubmit.scm.SVN.GetFileProperty(another, 'svn:mime-type').AndReturn(None) | 820 presubmit.scm.SVN.GetFileProperty(another, 'svn:mime-type').AndReturn(None) |
| 815 presubmit.scm.SVN.GetFileProperty(third_party, 'svn:mime-type' | 821 presubmit.scm.SVN.GetFileProperty(third_party, 'svn:mime-type' |
| 816 ).AndReturn(None) | 822 ).AndReturn(None) |
| 817 presubmit.scm.SVN.GenerateDiff([blat]).AndReturn(self.presubmit_diffs) | 823 presubmit.scm.SVN.GenerateDiff([blat]).AndReturn(self.presubmit_diffs) |
| 818 presubmit.scm.SVN.GenerateDiff([another]).AndReturn(self.presubmit_diffs) | 824 presubmit.scm.SVN.GenerateDiff([another]).AndReturn(self.presubmit_diffs) |
| 819 | 825 |
| 820 self.mox.ReplayAll() | 826 self.mox.ReplayAll() |
| 821 | 827 |
| 822 change = presubmit.SvnChange('mychange', '\n'.join(description_lines), | 828 change = presubmit.SvnChange('mychange', '\n'.join(description_lines), |
| 823 self.fake_root_dir, files, 0, 0) | 829 self.fake_root_dir, files, 0, 0) |
| 824 input_api = presubmit.InputApi(change, | 830 input_api = presubmit.InputApi( |
| 825 join(self.fake_root_dir, 'foo', | 831 change, |
| 826 'PRESUBMIT.py'), | 832 join(self.fake_root_dir, 'foo', 'PRESUBMIT.py'), |
| 827 False, False, None) | 833 False, False, None, False) |
| 828 # Doesn't filter much | 834 # Doesn't filter much |
| 829 got_files = input_api.AffectedFiles() | 835 got_files = input_api.AffectedFiles() |
| 830 self.assertEquals(len(got_files), 7) | 836 self.assertEquals(len(got_files), 7) |
| 831 self.assertEquals(got_files[0].LocalPath(), presubmit.normpath(files[0][1])) | 837 self.assertEquals(got_files[0].LocalPath(), presubmit.normpath(files[0][1])) |
| 832 self.assertEquals(got_files[1].LocalPath(), presubmit.normpath(files[1][1])) | 838 self.assertEquals(got_files[1].LocalPath(), presubmit.normpath(files[1][1])) |
| 833 self.assertEquals(got_files[2].LocalPath(), presubmit.normpath(files[2][1])) | 839 self.assertEquals(got_files[2].LocalPath(), presubmit.normpath(files[2][1])) |
| 834 self.assertEquals(got_files[3].LocalPath(), presubmit.normpath(files[3][1])) | 840 self.assertEquals(got_files[3].LocalPath(), presubmit.normpath(files[3][1])) |
| 835 self.assertEquals(got_files[4].LocalPath(), presubmit.normpath(files[4][1])) | 841 self.assertEquals(got_files[4].LocalPath(), presubmit.normpath(files[4][1])) |
| 836 self.assertEquals(got_files[5].LocalPath(), presubmit.normpath(files[5][1])) | 842 self.assertEquals(got_files[5].LocalPath(), presubmit.normpath(files[5][1])) |
| 837 self.assertEquals(got_files[6].LocalPath(), presubmit.normpath(files[6][1])) | 843 self.assertEquals(got_files[6].LocalPath(), presubmit.normpath(files[6][1])) |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 902 f('a/.git'), | 908 f('a/.git'), |
| 903 f('b.c/.git'), | 909 f('b.c/.git'), |
| 904 f('a/.git/bleh.py'), | 910 f('a/.git/bleh.py'), |
| 905 f('.git/bleh.py'), | 911 f('.git/bleh.py'), |
| 906 ], | 912 ], |
| 907 [ | 913 [ |
| 908 # Expected. | 914 # Expected. |
| 909 ], | 915 ], |
| 910 ), | 916 ), |
| 911 ] | 917 ] |
| 912 input_api = presubmit.InputApi(self.fake_change, './PRESUBMIT.py', False, | 918 input_api = presubmit.InputApi( |
| 913 False, None) | 919 self.fake_change, './PRESUBMIT.py', False, |
| 920 False, None, False) |
| 914 self.mox.ReplayAll() | 921 self.mox.ReplayAll() |
| 915 | 922 |
| 916 self.assertEqual(len(input_api.DEFAULT_WHITE_LIST), 22) | 923 self.assertEqual(len(input_api.DEFAULT_WHITE_LIST), 22) |
| 917 self.assertEqual(len(input_api.DEFAULT_BLACK_LIST), 9) | 924 self.assertEqual(len(input_api.DEFAULT_BLACK_LIST), 9) |
| 918 for item in files: | 925 for item in files: |
| 919 results = filter(input_api.FilterSourceFile, item[0]) | 926 results = filter(input_api.FilterSourceFile, item[0]) |
| 920 for i in range(len(results)): | 927 for i in range(len(results)): |
| 921 self.assertEquals(results[i].LocalPath(), | 928 self.assertEquals(results[i].LocalPath(), |
| 922 presubmit.normpath(item[1][i])) | 929 presubmit.normpath(item[1][i])) |
| 923 # Same number of expected results. | 930 # Same number of expected results. |
| 924 self.assertEquals(sorted([f.LocalPath().replace(presubmit.os.sep, '/') | 931 self.assertEquals(sorted([f.LocalPath().replace(presubmit.os.sep, '/') |
| 925 for f in results]), | 932 for f in results]), |
| 926 sorted(item[1])) | 933 sorted(item[1])) |
| 927 | 934 |
| 928 def testCustomFilter(self): | 935 def testCustomFilter(self): |
| 929 def FilterSourceFile(affected_file): | 936 def FilterSourceFile(affected_file): |
| 930 return 'a' in affected_file.LocalPath() | 937 return 'a' in affected_file.LocalPath() |
| 931 files = [('A', 'eeaee'), ('M', 'eeabee'), ('M', 'eebcee')] | 938 files = [('A', 'eeaee'), ('M', 'eeabee'), ('M', 'eebcee')] |
| 932 for _, item in files: | 939 for _, item in files: |
| 933 item = presubmit.os.path.join(self.fake_root_dir, item) | 940 item = presubmit.os.path.join(self.fake_root_dir, item) |
| 934 presubmit.os.path.exists(item).AndReturn(True) | 941 presubmit.os.path.exists(item).AndReturn(True) |
| 935 presubmit.os.path.isdir(item).AndReturn(False) | 942 presubmit.os.path.isdir(item).AndReturn(False) |
| 936 presubmit.scm.SVN.GetFileProperty(item, 'svn:mime-type').AndReturn(None) | 943 presubmit.scm.SVN.GetFileProperty(item, 'svn:mime-type').AndReturn(None) |
| 937 self.mox.ReplayAll() | 944 self.mox.ReplayAll() |
| 938 | 945 |
| 939 change = presubmit.SvnChange('mychange', '', self.fake_root_dir, files, 0, | 946 change = presubmit.SvnChange('mychange', '', self.fake_root_dir, files, 0, |
| 940 0) | 947 0) |
| 941 input_api = presubmit.InputApi(change, | 948 input_api = presubmit.InputApi( |
| 942 presubmit.os.path.join(self.fake_root_dir, | 949 change, |
| 943 'PRESUBMIT.py'), | 950 presubmit.os.path.join(self.fake_root_dir, 'PRESUBMIT.py'), |
| 944 False, False, None) | 951 False, False, None, False) |
| 945 got_files = input_api.AffectedSourceFiles(FilterSourceFile) | 952 got_files = input_api.AffectedSourceFiles(FilterSourceFile) |
| 946 self.assertEquals(len(got_files), 2) | 953 self.assertEquals(len(got_files), 2) |
| 947 self.assertEquals(got_files[0].LocalPath(), 'eeaee') | 954 self.assertEquals(got_files[0].LocalPath(), 'eeaee') |
| 948 self.assertEquals(got_files[1].LocalPath(), 'eeabee') | 955 self.assertEquals(got_files[1].LocalPath(), 'eeabee') |
| 949 | 956 |
| 950 def testLambdaFilter(self): | 957 def testLambdaFilter(self): |
| 951 white_list = presubmit.InputApi.DEFAULT_BLACK_LIST + (r".*?a.*?",) | 958 white_list = presubmit.InputApi.DEFAULT_BLACK_LIST + (r".*?a.*?",) |
| 952 black_list = [r".*?b.*?"] | 959 black_list = [r".*?b.*?"] |
| 953 files = [('A', 'eeaee'), ('M', 'eeabee'), ('M', 'eebcee'), ('M', 'eecaee')] | 960 files = [('A', 'eeaee'), ('M', 'eeabee'), ('M', 'eebcee'), ('M', 'eecaee')] |
| 954 for _, item in files: | 961 for _, item in files: |
| 955 item = presubmit.os.path.join(self.fake_root_dir, item) | 962 item = presubmit.os.path.join(self.fake_root_dir, item) |
| 956 presubmit.os.path.exists(item).AndReturn(True) | 963 presubmit.os.path.exists(item).AndReturn(True) |
| 957 presubmit.os.path.isdir(item).AndReturn(False) | 964 presubmit.os.path.isdir(item).AndReturn(False) |
| 958 presubmit.scm.SVN.GetFileProperty(item, 'svn:mime-type').AndReturn(None) | 965 presubmit.scm.SVN.GetFileProperty(item, 'svn:mime-type').AndReturn(None) |
| 959 self.mox.ReplayAll() | 966 self.mox.ReplayAll() |
| 960 | 967 |
| 961 change = presubmit.SvnChange('mychange', '', self.fake_root_dir, files, 0, | 968 change = presubmit.SvnChange('mychange', '', self.fake_root_dir, files, 0, |
| 962 0) | 969 0) |
| 963 input_api = presubmit.InputApi(change, './PRESUBMIT.py', False, | 970 input_api = presubmit.InputApi( |
| 964 False, None) | 971 change, './PRESUBMIT.py', False, False, None, False) |
| 965 # Sample usage of overiding the default white and black lists. | 972 # Sample usage of overiding the default white and black lists. |
| 966 got_files = input_api.AffectedSourceFiles( | 973 got_files = input_api.AffectedSourceFiles( |
| 967 lambda x: input_api.FilterSourceFile(x, white_list, black_list)) | 974 lambda x: input_api.FilterSourceFile(x, white_list, black_list)) |
| 968 self.assertEquals(len(got_files), 2) | 975 self.assertEquals(len(got_files), 2) |
| 969 self.assertEquals(got_files[0].LocalPath(), 'eeaee') | 976 self.assertEquals(got_files[0].LocalPath(), 'eeaee') |
| 970 self.assertEquals(got_files[1].LocalPath(), 'eecaee') | 977 self.assertEquals(got_files[1].LocalPath(), 'eecaee') |
| 971 | 978 |
| 972 def testGetAbsoluteLocalPath(self): | 979 def testGetAbsoluteLocalPath(self): |
| 973 join = presubmit.os.path.join | 980 join = presubmit.os.path.join |
| 974 normpath = presubmit.normpath | 981 normpath = presubmit.normpath |
| (...skipping 17 matching lines...) Expand all Loading... |
| 992 # Absolute paths should be prefixed | 999 # Absolute paths should be prefixed |
| 993 self.assertEquals(affected_files[0].AbsoluteLocalPath(), | 1000 self.assertEquals(affected_files[0].AbsoluteLocalPath(), |
| 994 normpath(join(self.fake_root_dir, 'isdir'))) | 1001 normpath(join(self.fake_root_dir, 'isdir'))) |
| 995 self.assertEquals(affected_files[1].AbsoluteLocalPath(), | 1002 self.assertEquals(affected_files[1].AbsoluteLocalPath(), |
| 996 normpath(join(self.fake_root_dir, 'isdir/blat.cc'))) | 1003 normpath(join(self.fake_root_dir, 'isdir/blat.cc'))) |
| 997 | 1004 |
| 998 # New helper functions need to work | 1005 # New helper functions need to work |
| 999 paths_from_change = change.AbsoluteLocalPaths(include_dirs=True) | 1006 paths_from_change = change.AbsoluteLocalPaths(include_dirs=True) |
| 1000 self.assertEqual(len(paths_from_change), 3) | 1007 self.assertEqual(len(paths_from_change), 3) |
| 1001 presubmit_path = join(self.fake_root_dir, 'isdir', 'PRESUBMIT.py') | 1008 presubmit_path = join(self.fake_root_dir, 'isdir', 'PRESUBMIT.py') |
| 1002 api = presubmit.InputApi(change=change, | 1009 api = presubmit.InputApi( |
| 1003 presubmit_path=presubmit_path, | 1010 change=change, presubmit_path=presubmit_path, |
| 1004 is_committing=True, tbr=False, host_url=None) | 1011 is_committing=True, tbr=False, host_url=None, verbose=False) |
| 1005 paths_from_api = api.AbsoluteLocalPaths(include_dirs=True) | 1012 paths_from_api = api.AbsoluteLocalPaths(include_dirs=True) |
| 1006 self.assertEqual(len(paths_from_api), 2) | 1013 self.assertEqual(len(paths_from_api), 2) |
| 1007 for absolute_paths in [paths_from_change, paths_from_api]: | 1014 for absolute_paths in [paths_from_change, paths_from_api]: |
| 1008 self.assertEqual(absolute_paths[0], | 1015 self.assertEqual(absolute_paths[0], |
| 1009 normpath(join(self.fake_root_dir, 'isdir'))) | 1016 normpath(join(self.fake_root_dir, 'isdir'))) |
| 1010 self.assertEqual(absolute_paths[1], | 1017 self.assertEqual(absolute_paths[1], |
| 1011 normpath(join(self.fake_root_dir, 'isdir', 'blat.cc'))) | 1018 normpath(join(self.fake_root_dir, 'isdir', 'blat.cc'))) |
| 1012 | 1019 |
| 1013 def testDeprecated(self): | 1020 def testDeprecated(self): |
| 1014 presubmit.warn(mox.IgnoreArg(), category=mox.IgnoreArg(), stacklevel=2) | 1021 presubmit.warn(mox.IgnoreArg(), category=mox.IgnoreArg(), stacklevel=2) |
| 1015 self.mox.ReplayAll() | 1022 self.mox.ReplayAll() |
| 1016 | 1023 |
| 1017 change = presubmit.Change('mychange', '', self.fake_root_dir, [], 0, 0) | 1024 change = presubmit.Change('mychange', '', self.fake_root_dir, [], 0, 0) |
| 1018 api = presubmit.InputApi( | 1025 api = presubmit.InputApi( |
| 1019 change, | 1026 change, |
| 1020 presubmit.os.path.join(self.fake_root_dir, 'foo', 'PRESUBMIT.py'), True, | 1027 presubmit.os.path.join(self.fake_root_dir, 'foo', 'PRESUBMIT.py'), True, |
| 1021 False, None) | 1028 False, None, False) |
| 1022 api.AffectedTextFiles(include_deletes=False) | 1029 api.AffectedTextFiles(include_deletes=False) |
| 1023 | 1030 |
| 1024 def testReadFileStringDenied(self): | 1031 def testReadFileStringDenied(self): |
| 1025 self.mox.ReplayAll() | 1032 self.mox.ReplayAll() |
| 1026 | 1033 |
| 1027 change = presubmit.Change('foo', 'foo', self.fake_root_dir, [('M', 'AA')], | 1034 change = presubmit.Change('foo', 'foo', self.fake_root_dir, [('M', 'AA')], |
| 1028 0, 0) | 1035 0, 0) |
| 1029 input_api = presubmit.InputApi( | 1036 input_api = presubmit.InputApi( |
| 1030 change, presubmit.os.path.join(self.fake_root_dir, '/p'), False, | 1037 change, presubmit.os.path.join(self.fake_root_dir, '/p'), False, |
| 1031 False, None) | 1038 False, None, False) |
| 1032 self.assertRaises(IOError, input_api.ReadFile, 'boo', 'x') | 1039 self.assertRaises(IOError, input_api.ReadFile, 'boo', 'x') |
| 1033 | 1040 |
| 1034 def testReadFileStringAccepted(self): | 1041 def testReadFileStringAccepted(self): |
| 1035 path = presubmit.os.path.join(self.fake_root_dir, 'AA/boo') | 1042 path = presubmit.os.path.join(self.fake_root_dir, 'AA/boo') |
| 1036 presubmit.gclient_utils.FileRead(path, 'x').AndReturn(None) | 1043 presubmit.gclient_utils.FileRead(path, 'x').AndReturn(None) |
| 1037 self.mox.ReplayAll() | 1044 self.mox.ReplayAll() |
| 1038 | 1045 |
| 1039 change = presubmit.Change('foo', 'foo', self.fake_root_dir, [('M', 'AA')], | 1046 change = presubmit.Change('foo', 'foo', self.fake_root_dir, [('M', 'AA')], |
| 1040 0, 0) | 1047 0, 0) |
| 1041 input_api = presubmit.InputApi( | 1048 input_api = presubmit.InputApi( |
| 1042 change, presubmit.os.path.join(self.fake_root_dir, '/p'), False, | 1049 change, presubmit.os.path.join(self.fake_root_dir, '/p'), False, |
| 1043 False, None) | 1050 False, None, False) |
| 1044 input_api.ReadFile(path, 'x') | 1051 input_api.ReadFile(path, 'x') |
| 1045 | 1052 |
| 1046 def testReadFileAffectedFileDenied(self): | 1053 def testReadFileAffectedFileDenied(self): |
| 1047 fileobj = presubmit.AffectedFile('boo', 'M', 'Unrelated') | 1054 fileobj = presubmit.AffectedFile('boo', 'M', 'Unrelated') |
| 1048 self.mox.ReplayAll() | 1055 self.mox.ReplayAll() |
| 1049 | 1056 |
| 1050 change = presubmit.Change('foo', 'foo', self.fake_root_dir, [('M', 'AA')], | 1057 change = presubmit.Change('foo', 'foo', self.fake_root_dir, [('M', 'AA')], |
| 1051 0, 0) | 1058 0, 0) |
| 1052 input_api = presubmit.InputApi( | 1059 input_api = presubmit.InputApi( |
| 1053 change, presubmit.os.path.join(self.fake_root_dir, '/p'), False, | 1060 change, presubmit.os.path.join(self.fake_root_dir, '/p'), False, |
| 1054 False, None) | 1061 False, None, False) |
| 1055 self.assertRaises(IOError, input_api.ReadFile, fileobj, 'x') | 1062 self.assertRaises(IOError, input_api.ReadFile, fileobj, 'x') |
| 1056 | 1063 |
| 1057 def testReadFileAffectedFileAccepted(self): | 1064 def testReadFileAffectedFileAccepted(self): |
| 1058 fileobj = presubmit.AffectedFile('AA/boo', 'M', self.fake_root_dir) | 1065 fileobj = presubmit.AffectedFile('AA/boo', 'M', self.fake_root_dir) |
| 1059 presubmit.gclient_utils.FileRead(fileobj.AbsoluteLocalPath(), 'x' | 1066 presubmit.gclient_utils.FileRead(fileobj.AbsoluteLocalPath(), 'x' |
| 1060 ).AndReturn(None) | 1067 ).AndReturn(None) |
| 1061 self.mox.ReplayAll() | 1068 self.mox.ReplayAll() |
| 1062 | 1069 |
| 1063 change = presubmit.Change('foo', 'foo', self.fake_root_dir, [('M', 'AA')], | 1070 change = presubmit.Change('foo', 'foo', self.fake_root_dir, [('M', 'AA')], |
| 1064 0, 0) | 1071 0, 0) |
| 1065 input_api = presubmit.InputApi( | 1072 input_api = presubmit.InputApi( |
| 1066 change, presubmit.os.path.join(self.fake_root_dir, '/p'), False, | 1073 change, presubmit.os.path.join(self.fake_root_dir, '/p'), False, |
| 1067 False, None) | 1074 False, None, False) |
| 1068 input_api.ReadFile(fileobj, 'x') | 1075 input_api.ReadFile(fileobj, 'x') |
| 1069 | 1076 |
| 1070 | 1077 |
| 1071 class OuputApiUnittest(PresubmitTestsBase): | 1078 class OuputApiUnittest(PresubmitTestsBase): |
| 1072 """Tests presubmit.OutputApi.""" | 1079 """Tests presubmit.OutputApi.""" |
| 1073 def testMembersChanged(self): | 1080 def testMembersChanged(self): |
| 1074 self.mox.ReplayAll() | 1081 self.mox.ReplayAll() |
| 1075 members = [ | 1082 members = [ |
| 1076 'MailTextResult', 'PresubmitAddReviewers', 'PresubmitError', | 1083 'MailTextResult', 'PresubmitAddReviewers', 'PresubmitError', |
| 1077 'PresubmitNotifyResult', 'PresubmitPromptWarning', 'PresubmitResult', | 1084 'PresubmitNotifyResult', 'PresubmitPromptWarning', 'PresubmitResult', |
| (...skipping 916 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1994 expected_output='Missing LGTM from an OWNER for: foo.cc\n') | 2001 expected_output='Missing LGTM from an OWNER for: foo.cc\n') |
| 1995 | 2002 |
| 1996 def testCannedCheckOwners_WithLGTMs(self): | 2003 def testCannedCheckOwners_WithLGTMs(self): |
| 1997 self.AssertOwnersWorks(approvers=set(['ben@example.com']), | 2004 self.AssertOwnersWorks(approvers=set(['ben@example.com']), |
| 1998 uncovered_files=set()) | 2005 uncovered_files=set()) |
| 1999 | 2006 |
| 2000 def testCannedRunUnitTests(self): | 2007 def testCannedRunUnitTests(self): |
| 2001 change = presubmit.Change( | 2008 change = presubmit.Change( |
| 2002 'foo1', 'description1', self.fake_root_dir, None, 0, 0) | 2009 'foo1', 'description1', self.fake_root_dir, None, 0, 0) |
| 2003 input_api = self.MockInputApi(change, False) | 2010 input_api = self.MockInputApi(change, False) |
| 2011 input_api.verbose = True |
| 2004 unit_tests = ['allo', 'bar.py'] | 2012 unit_tests = ['allo', 'bar.py'] |
| 2005 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) | 2013 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) |
| 2006 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) | 2014 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) |
| 2007 input_api.subprocess.check_call( | 2015 input_api.subprocess.check_call( |
| 2008 ['allo'], cwd=self.fake_root_dir) | 2016 ['allo'], cwd=self.fake_root_dir) |
| 2009 cmd = ['bar.py'] | 2017 cmd = ['bar.py'] |
| 2010 if input_api.platform == 'win32': | 2018 if input_api.platform == 'win32': |
| 2011 cmd.insert(0, input_api.python_executable) | 2019 cmd.insert(0, input_api.python_executable) |
| 2012 input_api.subprocess.check_call( | 2020 input_api.subprocess.check_call( |
| 2013 cmd, cwd=self.fake_root_dir).AndRaise( | 2021 cmd, cwd=self.fake_root_dir).AndRaise( |
| 2014 input_api.subprocess.CalledProcessError()) | 2022 input_api.subprocess.CalledProcessError()) |
| 2015 | 2023 |
| 2016 self.mox.ReplayAll() | 2024 self.mox.ReplayAll() |
| 2017 results = presubmit_canned_checks.RunUnitTests( | 2025 results = presubmit_canned_checks.RunUnitTests( |
| 2018 input_api, | 2026 input_api, |
| 2019 presubmit.OutputApi, | 2027 presubmit.OutputApi, |
| 2020 unit_tests, | 2028 unit_tests) |
| 2021 verbose=True) | |
| 2022 self.assertEqual(1, len(results)) | 2029 self.assertEqual(1, len(results)) |
| 2023 self.assertEqual( | 2030 self.assertEqual( |
| 2024 presubmit.OutputApi.PresubmitPromptWarning, results[0].__class__) | 2031 presubmit.OutputApi.PresubmitPromptWarning, results[0].__class__) |
| 2025 self.checkstdout('Running allo\nRunning bar.py\n') | 2032 self.checkstdout('Running allo\nRunning bar.py\n') |
| 2026 | 2033 |
| 2027 def testCannedRunUnitTestsInDirectory(self): | 2034 def testCannedRunUnitTestsInDirectory(self): |
| 2028 change = presubmit.Change( | 2035 change = presubmit.Change( |
| 2029 'foo1', 'description1', self.fake_root_dir, None, 0, 0) | 2036 'foo1', 'description1', self.fake_root_dir, None, 0, 0) |
| 2030 input_api = self.MockInputApi(change, False) | 2037 input_api = self.MockInputApi(change, False) |
| 2038 input_api.verbose = True |
| 2031 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) | 2039 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) |
| 2032 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) | 2040 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) |
| 2033 path = presubmit.os.path.join(self.fake_root_dir, 'random_directory') | 2041 path = presubmit.os.path.join(self.fake_root_dir, 'random_directory') |
| 2034 input_api.os_listdir(path).AndReturn(['.', '..', 'a', 'b', 'c']) | 2042 input_api.os_listdir(path).AndReturn(['.', '..', 'a', 'b', 'c']) |
| 2035 input_api.os_path.isfile = lambda x: not x.endswith('.') | 2043 input_api.os_path.isfile = lambda x: not x.endswith('.') |
| 2036 input_api.subprocess.check_call( | 2044 input_api.subprocess.check_call( |
| 2037 [presubmit.os.path.join('random_directory', 'b')], | 2045 [presubmit.os.path.join('random_directory', 'b')], |
| 2038 cwd=self.fake_root_dir) | 2046 cwd=self.fake_root_dir) |
| 2039 | 2047 |
| 2040 self.mox.ReplayAll() | 2048 self.mox.ReplayAll() |
| 2041 results = presubmit_canned_checks.RunUnitTestsInDirectory( | 2049 results = presubmit_canned_checks.RunUnitTestsInDirectory( |
| 2042 input_api, | 2050 input_api, |
| 2043 presubmit.OutputApi, | 2051 presubmit.OutputApi, |
| 2044 'random_directory', | 2052 'random_directory', |
| 2045 whitelist=['^a$', '^b$'], | 2053 whitelist=['^a$', '^b$'], |
| 2046 blacklist=['a'], | 2054 blacklist=['a']) |
| 2047 verbose=True) | |
| 2048 self.assertEqual(results, []) | 2055 self.assertEqual(results, []) |
| 2049 self.checkstdout( | 2056 self.checkstdout( |
| 2050 'Running %s\n' % presubmit.os.path.join('random_directory', 'b')) | 2057 'Running %s\n' % presubmit.os.path.join('random_directory', 'b')) |
| 2051 | 2058 |
| 2052 | 2059 |
| 2053 if __name__ == '__main__': | 2060 if __name__ == '__main__': |
| 2054 import unittest | 2061 import unittest |
| 2055 unittest.main() | 2062 unittest.main() |
| OLD | NEW |