| OLD | NEW |
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # Copyright (c) 2010 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2010 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,W0212,W0403 |
| 10 | 10 |
| (...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 335 'this is a change', | 335 'this is a change', |
| 336 'STORY=http://tracker/123') | 336 'STORY=http://tracker/123') |
| 337 files = [ | 337 files = [ |
| 338 ['A', 'foo\\blat.cc'], | 338 ['A', 'foo\\blat.cc'], |
| 339 ] | 339 ] |
| 340 fake_presubmit = presubmit.os.path.join(self.fake_root_dir, 'PRESUBMIT.py') | 340 fake_presubmit = presubmit.os.path.join(self.fake_root_dir, 'PRESUBMIT.py') |
| 341 self.mox.ReplayAll() | 341 self.mox.ReplayAll() |
| 342 | 342 |
| 343 change = presubmit.Change('mychange', '\n'.join(description_lines), | 343 change = presubmit.Change('mychange', '\n'.join(description_lines), |
| 344 self.fake_root_dir, files, 0, 0) | 344 self.fake_root_dir, files, 0, 0) |
| 345 executer = presubmit.PresubmitExecuter(change, False) | 345 executer = presubmit.PresubmitExecuter(change, False, False, None) |
| 346 self.failIf(executer.ExecPresubmitScript('', fake_presubmit)) | 346 self.failIf(executer.ExecPresubmitScript('', fake_presubmit)) |
| 347 # No error if no on-upload entry point | 347 # No error if no on-upload entry point |
| 348 self.failIf(executer.ExecPresubmitScript( | 348 self.failIf(executer.ExecPresubmitScript( |
| 349 ('def CheckChangeOnCommit(input_api, output_api):\n' | 349 ('def CheckChangeOnCommit(input_api, output_api):\n' |
| 350 ' return (output_api.PresubmitError("!!"))\n'), | 350 ' return (output_api.PresubmitError("!!"))\n'), |
| 351 fake_presubmit | 351 fake_presubmit |
| 352 )) | 352 )) |
| 353 | 353 |
| 354 executer = presubmit.PresubmitExecuter(change, True) | 354 executer = presubmit.PresubmitExecuter(change, True, False, None) |
| 355 # No error if no on-commit entry point | 355 # No error if no on-commit entry point |
| 356 self.failIf(executer.ExecPresubmitScript( | 356 self.failIf(executer.ExecPresubmitScript( |
| 357 ('def CheckChangeOnUpload(input_api, output_api):\n' | 357 ('def CheckChangeOnUpload(input_api, output_api):\n' |
| 358 ' return (output_api.PresubmitError("!!"))\n'), | 358 ' return (output_api.PresubmitError("!!"))\n'), |
| 359 fake_presubmit | 359 fake_presubmit |
| 360 )) | 360 )) |
| 361 | 361 |
| 362 self.failIf(executer.ExecPresubmitScript( | 362 self.failIf(executer.ExecPresubmitScript( |
| 363 ('def CheckChangeOnUpload(input_api, output_api):\n' | 363 ('def CheckChangeOnUpload(input_api, output_api):\n' |
| 364 ' if not input_api.change.STORY:\n' | 364 ' if not input_api.change.STORY:\n' |
| (...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 693 def testMembersChanged(self): | 693 def testMembersChanged(self): |
| 694 self.mox.ReplayAll() | 694 self.mox.ReplayAll() |
| 695 members = [ | 695 members = [ |
| 696 'AbsoluteLocalPaths', 'AffectedFiles', 'AffectedSourceFiles', | 696 'AbsoluteLocalPaths', 'AffectedFiles', 'AffectedSourceFiles', |
| 697 'AffectedTextFiles', | 697 'AffectedTextFiles', |
| 698 'DEFAULT_BLACK_LIST', 'DEFAULT_WHITE_LIST', | 698 'DEFAULT_BLACK_LIST', 'DEFAULT_WHITE_LIST', |
| 699 'DepotToLocalPath', 'FilterSourceFile', 'LocalPaths', | 699 'DepotToLocalPath', 'FilterSourceFile', 'LocalPaths', |
| 700 'LocalToDepotPath', | 700 'LocalToDepotPath', |
| 701 'PresubmitLocalPath', 'ReadFile', 'RightHandSideLines', 'ServerPaths', | 701 'PresubmitLocalPath', 'ReadFile', 'RightHandSideLines', 'ServerPaths', |
| 702 'basename', 'cPickle', 'cStringIO', 'canned_checks', 'change', 'environ', | 702 'basename', 'cPickle', 'cStringIO', 'canned_checks', 'change', 'environ', |
| 703 'host_url', 'is_committing', 'is_tbr', 'json', 'marshal', 'os_path', | 703 'host_url', 'is_committing', 'json', 'marshal', 'os_path', |
| 704 'owners_db', 'pickle', 'platform', 'python_executable', 're', | 704 'owners_db', 'pickle', 'platform', 'python_executable', 're', |
| 705 'subprocess', 'tempfile', 'traceback', 'unittest', 'urllib2', 'version', | 705 'subprocess', 'tbr', 'tempfile', 'traceback', 'unittest', 'urllib2', |
| 706 'version', |
| 706 ] | 707 ] |
| 707 # If this test fails, you should add the relevant test. | 708 # If this test fails, you should add the relevant test. |
| 708 self.compareMembers(presubmit.InputApi(self.fake_change, './.', False), | 709 self.compareMembers(presubmit.InputApi(self.fake_change, './.', False, |
| 710 False, None), |
| 709 members) | 711 members) |
| 710 | 712 |
| 711 def testDepotToLocalPath(self): | 713 def testDepotToLocalPath(self): |
| 712 presubmit.scm.SVN.CaptureInfo('svn://foo/smurf').AndReturn( | 714 presubmit.scm.SVN.CaptureInfo('svn://foo/smurf').AndReturn( |
| 713 {'Path': 'prout'}) | 715 {'Path': 'prout'}) |
| 714 presubmit.scm.SVN.CaptureInfo('svn:/foo/notfound/burp').AndReturn({}) | 716 presubmit.scm.SVN.CaptureInfo('svn:/foo/notfound/burp').AndReturn({}) |
| 715 self.mox.ReplayAll() | 717 self.mox.ReplayAll() |
| 716 | 718 |
| 717 path = presubmit.InputApi(self.fake_change, './p', False).DepotToLocalPath( | 719 path = presubmit.InputApi(self.fake_change, './p', False, False, |
| 718 'svn://foo/smurf') | 720 None).DepotToLocalPath('svn://foo/smurf') |
| 719 self.failUnless(path == 'prout') | 721 self.failUnless(path == 'prout') |
| 720 path = presubmit.InputApi(self.fake_change, './p', False).DepotToLocalPath( | 722 path = presubmit.InputApi(self.fake_change, './p', False, False, |
| 721 'svn:/foo/notfound/burp') | 723 None).DepotToLocalPath('svn:/foo/notfound/burp') |
| 722 self.failUnless(path == None) | 724 self.failUnless(path == None) |
| 723 | 725 |
| 724 def testLocalToDepotPath(self): | 726 def testLocalToDepotPath(self): |
| 725 presubmit.scm.SVN.CaptureInfo('smurf').AndReturn({'URL': 'svn://foo'}) | 727 presubmit.scm.SVN.CaptureInfo('smurf').AndReturn({'URL': 'svn://foo'}) |
| 726 presubmit.scm.SVN.CaptureInfo('notfound-food').AndReturn({}) | 728 presubmit.scm.SVN.CaptureInfo('notfound-food').AndReturn({}) |
| 727 self.mox.ReplayAll() | 729 self.mox.ReplayAll() |
| 728 path = presubmit.InputApi(self.fake_change, './p', False).LocalToDepotPath( | 730 path = presubmit.InputApi(self.fake_change, './p', False, False, |
| 729 'smurf') | 731 None).LocalToDepotPath('smurf') |
| 730 self.assertEqual(path, 'svn://foo') | 732 self.assertEqual(path, 'svn://foo') |
| 731 path = presubmit.InputApi(self.fake_change, './p', False).LocalToDepotPath( | 733 path = presubmit.InputApi(self.fake_change, './p', False, False, |
| 732 'notfound-food') | 734 None).LocalToDepotPath('notfound-food') |
| 733 self.failUnless(path == None) | 735 self.failUnless(path == None) |
| 734 | 736 |
| 735 def testInputApiConstruction(self): | 737 def testInputApiConstruction(self): |
| 736 self.mox.ReplayAll() | 738 self.mox.ReplayAll() |
| 737 api = presubmit.InputApi(self.fake_change, | 739 api = presubmit.InputApi(self.fake_change, |
| 738 presubmit_path='foo/path/PRESUBMIT.py', | 740 presubmit_path='foo/path/PRESUBMIT.py', |
| 739 is_committing=False) | 741 is_committing=False, tbr=False, host_url=None) |
| 740 self.assertEquals(api.PresubmitLocalPath(), 'foo/path') | 742 self.assertEquals(api.PresubmitLocalPath(), 'foo/path') |
| 741 self.assertEquals(api.change, self.fake_change) | 743 self.assertEquals(api.change, self.fake_change) |
| 744 self.assertEquals(api.host_url, 'http://codereview.chromium.org') |
| 742 | 745 |
| 743 def testInputApiPresubmitScriptFiltering(self): | 746 def testInputApiPresubmitScriptFiltering(self): |
| 744 join = presubmit.os.path.join | 747 join = presubmit.os.path.join |
| 745 description_lines = ('Hello there', | 748 description_lines = ('Hello there', |
| 746 'this is a change', | 749 'this is a change', |
| 747 'BUG=123', | 750 'BUG=123', |
| 748 ' STORY =http://foo/ \t', | 751 ' STORY =http://foo/ \t', |
| 749 'and some more regular text') | 752 'and some more regular text') |
| 750 files = [ | 753 files = [ |
| 751 ['A', join('foo', 'blat.cc')], | 754 ['A', join('foo', 'blat.cc')], |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 787 presubmit.scm.SVN.GenerateDiff([blat]).AndReturn(self.presubmit_diffs) | 790 presubmit.scm.SVN.GenerateDiff([blat]).AndReturn(self.presubmit_diffs) |
| 788 presubmit.scm.SVN.GenerateDiff([another]).AndReturn(self.presubmit_diffs) | 791 presubmit.scm.SVN.GenerateDiff([another]).AndReturn(self.presubmit_diffs) |
| 789 | 792 |
| 790 self.mox.ReplayAll() | 793 self.mox.ReplayAll() |
| 791 | 794 |
| 792 change = presubmit.SvnChange('mychange', '\n'.join(description_lines), | 795 change = presubmit.SvnChange('mychange', '\n'.join(description_lines), |
| 793 self.fake_root_dir, files, 0, 0) | 796 self.fake_root_dir, files, 0, 0) |
| 794 input_api = presubmit.InputApi(change, | 797 input_api = presubmit.InputApi(change, |
| 795 join(self.fake_root_dir, 'foo', | 798 join(self.fake_root_dir, 'foo', |
| 796 'PRESUBMIT.py'), | 799 'PRESUBMIT.py'), |
| 797 False) | 800 False, False, None) |
| 798 # Doesn't filter much | 801 # Doesn't filter much |
| 799 got_files = input_api.AffectedFiles() | 802 got_files = input_api.AffectedFiles() |
| 800 self.assertEquals(len(got_files), 7) | 803 self.assertEquals(len(got_files), 7) |
| 801 self.assertEquals(got_files[0].LocalPath(), presubmit.normpath(files[0][1])) | 804 self.assertEquals(got_files[0].LocalPath(), presubmit.normpath(files[0][1])) |
| 802 self.assertEquals(got_files[1].LocalPath(), presubmit.normpath(files[1][1])) | 805 self.assertEquals(got_files[1].LocalPath(), presubmit.normpath(files[1][1])) |
| 803 self.assertEquals(got_files[2].LocalPath(), presubmit.normpath(files[2][1])) | 806 self.assertEquals(got_files[2].LocalPath(), presubmit.normpath(files[2][1])) |
| 804 self.assertEquals(got_files[3].LocalPath(), presubmit.normpath(files[3][1])) | 807 self.assertEquals(got_files[3].LocalPath(), presubmit.normpath(files[3][1])) |
| 805 self.assertEquals(got_files[4].LocalPath(), presubmit.normpath(files[4][1])) | 808 self.assertEquals(got_files[4].LocalPath(), presubmit.normpath(files[4][1])) |
| 806 self.assertEquals(got_files[5].LocalPath(), presubmit.normpath(files[5][1])) | 809 self.assertEquals(got_files[5].LocalPath(), presubmit.normpath(files[5][1])) |
| 807 self.assertEquals(got_files[6].LocalPath(), presubmit.normpath(files[6][1])) | 810 self.assertEquals(got_files[6].LocalPath(), presubmit.normpath(files[6][1])) |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 872 f('a/.git'), | 875 f('a/.git'), |
| 873 f('b.c/.git'), | 876 f('b.c/.git'), |
| 874 f('a/.git/bleh.py'), | 877 f('a/.git/bleh.py'), |
| 875 f('.git/bleh.py'), | 878 f('.git/bleh.py'), |
| 876 ], | 879 ], |
| 877 [ | 880 [ |
| 878 # Expected. | 881 # Expected. |
| 879 ], | 882 ], |
| 880 ), | 883 ), |
| 881 ] | 884 ] |
| 882 input_api = presubmit.InputApi(self.fake_change, './PRESUBMIT.py', False) | 885 input_api = presubmit.InputApi(self.fake_change, './PRESUBMIT.py', False, |
| 886 False, None) |
| 883 self.mox.ReplayAll() | 887 self.mox.ReplayAll() |
| 884 | 888 |
| 885 self.assertEqual(len(input_api.DEFAULT_WHITE_LIST), 22) | 889 self.assertEqual(len(input_api.DEFAULT_WHITE_LIST), 22) |
| 886 self.assertEqual(len(input_api.DEFAULT_BLACK_LIST), 9) | 890 self.assertEqual(len(input_api.DEFAULT_BLACK_LIST), 9) |
| 887 for item in files: | 891 for item in files: |
| 888 results = filter(input_api.FilterSourceFile, item[0]) | 892 results = filter(input_api.FilterSourceFile, item[0]) |
| 889 for i in range(len(results)): | 893 for i in range(len(results)): |
| 890 self.assertEquals(results[i].LocalPath(), | 894 self.assertEquals(results[i].LocalPath(), |
| 891 presubmit.normpath(item[1][i])) | 895 presubmit.normpath(item[1][i])) |
| 892 # Same number of expected results. | 896 # Same number of expected results. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 903 presubmit.os.path.exists(item).AndReturn(True) | 907 presubmit.os.path.exists(item).AndReturn(True) |
| 904 presubmit.os.path.isdir(item).AndReturn(False) | 908 presubmit.os.path.isdir(item).AndReturn(False) |
| 905 presubmit.scm.SVN.GetFileProperty(item, 'svn:mime-type').AndReturn(None) | 909 presubmit.scm.SVN.GetFileProperty(item, 'svn:mime-type').AndReturn(None) |
| 906 self.mox.ReplayAll() | 910 self.mox.ReplayAll() |
| 907 | 911 |
| 908 change = presubmit.SvnChange('mychange', '', self.fake_root_dir, files, 0, | 912 change = presubmit.SvnChange('mychange', '', self.fake_root_dir, files, 0, |
| 909 0) | 913 0) |
| 910 input_api = presubmit.InputApi(change, | 914 input_api = presubmit.InputApi(change, |
| 911 presubmit.os.path.join(self.fake_root_dir, | 915 presubmit.os.path.join(self.fake_root_dir, |
| 912 'PRESUBMIT.py'), | 916 'PRESUBMIT.py'), |
| 913 False) | 917 False, False, None) |
| 914 got_files = input_api.AffectedSourceFiles(FilterSourceFile) | 918 got_files = input_api.AffectedSourceFiles(FilterSourceFile) |
| 915 self.assertEquals(len(got_files), 2) | 919 self.assertEquals(len(got_files), 2) |
| 916 self.assertEquals(got_files[0].LocalPath(), 'eeaee') | 920 self.assertEquals(got_files[0].LocalPath(), 'eeaee') |
| 917 self.assertEquals(got_files[1].LocalPath(), 'eeabee') | 921 self.assertEquals(got_files[1].LocalPath(), 'eeabee') |
| 918 | 922 |
| 919 def testLambdaFilter(self): | 923 def testLambdaFilter(self): |
| 920 white_list = presubmit.InputApi.DEFAULT_BLACK_LIST + (r".*?a.*?",) | 924 white_list = presubmit.InputApi.DEFAULT_BLACK_LIST + (r".*?a.*?",) |
| 921 black_list = [r".*?b.*?"] | 925 black_list = [r".*?b.*?"] |
| 922 files = [('A', 'eeaee'), ('M', 'eeabee'), ('M', 'eebcee'), ('M', 'eecaee')] | 926 files = [('A', 'eeaee'), ('M', 'eeabee'), ('M', 'eebcee'), ('M', 'eecaee')] |
| 923 for _, item in files: | 927 for _, item in files: |
| 924 item = presubmit.os.path.join(self.fake_root_dir, item) | 928 item = presubmit.os.path.join(self.fake_root_dir, item) |
| 925 presubmit.os.path.exists(item).AndReturn(True) | 929 presubmit.os.path.exists(item).AndReturn(True) |
| 926 presubmit.os.path.isdir(item).AndReturn(False) | 930 presubmit.os.path.isdir(item).AndReturn(False) |
| 927 presubmit.scm.SVN.GetFileProperty(item, 'svn:mime-type').AndReturn(None) | 931 presubmit.scm.SVN.GetFileProperty(item, 'svn:mime-type').AndReturn(None) |
| 928 self.mox.ReplayAll() | 932 self.mox.ReplayAll() |
| 929 | 933 |
| 930 change = presubmit.SvnChange('mychange', '', self.fake_root_dir, files, 0, | 934 change = presubmit.SvnChange('mychange', '', self.fake_root_dir, files, 0, |
| 931 0) | 935 0) |
| 932 input_api = presubmit.InputApi(change, './PRESUBMIT.py', False) | 936 input_api = presubmit.InputApi(change, './PRESUBMIT.py', False, |
| 937 False, None) |
| 933 # Sample usage of overiding the default white and black lists. | 938 # Sample usage of overiding the default white and black lists. |
| 934 got_files = input_api.AffectedSourceFiles( | 939 got_files = input_api.AffectedSourceFiles( |
| 935 lambda x: input_api.FilterSourceFile(x, white_list, black_list)) | 940 lambda x: input_api.FilterSourceFile(x, white_list, black_list)) |
| 936 self.assertEquals(len(got_files), 2) | 941 self.assertEquals(len(got_files), 2) |
| 937 self.assertEquals(got_files[0].LocalPath(), 'eeaee') | 942 self.assertEquals(got_files[0].LocalPath(), 'eeaee') |
| 938 self.assertEquals(got_files[1].LocalPath(), 'eecaee') | 943 self.assertEquals(got_files[1].LocalPath(), 'eecaee') |
| 939 | 944 |
| 940 def testGetAbsoluteLocalPath(self): | 945 def testGetAbsoluteLocalPath(self): |
| 941 join = presubmit.os.path.join | 946 join = presubmit.os.path.join |
| 942 normpath = presubmit.normpath | 947 normpath = presubmit.normpath |
| (...skipping 19 matching lines...) Expand all Loading... |
| 962 normpath(join(self.fake_root_dir, 'isdir'))) | 967 normpath(join(self.fake_root_dir, 'isdir'))) |
| 963 self.assertEquals(affected_files[1].AbsoluteLocalPath(), | 968 self.assertEquals(affected_files[1].AbsoluteLocalPath(), |
| 964 normpath(join(self.fake_root_dir, 'isdir/blat.cc'))) | 969 normpath(join(self.fake_root_dir, 'isdir/blat.cc'))) |
| 965 | 970 |
| 966 # New helper functions need to work | 971 # New helper functions need to work |
| 967 paths_from_change = change.AbsoluteLocalPaths(include_dirs=True) | 972 paths_from_change = change.AbsoluteLocalPaths(include_dirs=True) |
| 968 self.assertEqual(len(paths_from_change), 3) | 973 self.assertEqual(len(paths_from_change), 3) |
| 969 presubmit_path = join(self.fake_root_dir, 'isdir', 'PRESUBMIT.py') | 974 presubmit_path = join(self.fake_root_dir, 'isdir', 'PRESUBMIT.py') |
| 970 api = presubmit.InputApi(change=change, | 975 api = presubmit.InputApi(change=change, |
| 971 presubmit_path=presubmit_path, | 976 presubmit_path=presubmit_path, |
| 972 is_committing=True) | 977 is_committing=True, tbr=False, host_url=None) |
| 973 paths_from_api = api.AbsoluteLocalPaths(include_dirs=True) | 978 paths_from_api = api.AbsoluteLocalPaths(include_dirs=True) |
| 974 self.assertEqual(len(paths_from_api), 2) | 979 self.assertEqual(len(paths_from_api), 2) |
| 975 for absolute_paths in [paths_from_change, paths_from_api]: | 980 for absolute_paths in [paths_from_change, paths_from_api]: |
| 976 self.assertEqual(absolute_paths[0], | 981 self.assertEqual(absolute_paths[0], |
| 977 normpath(join(self.fake_root_dir, 'isdir'))) | 982 normpath(join(self.fake_root_dir, 'isdir'))) |
| 978 self.assertEqual(absolute_paths[1], | 983 self.assertEqual(absolute_paths[1], |
| 979 normpath(join(self.fake_root_dir, 'isdir', 'blat.cc'))) | 984 normpath(join(self.fake_root_dir, 'isdir', 'blat.cc'))) |
| 980 | 985 |
| 981 def testDeprecated(self): | 986 def testDeprecated(self): |
| 982 presubmit.warn(mox.IgnoreArg(), category=mox.IgnoreArg(), stacklevel=2) | 987 presubmit.warn(mox.IgnoreArg(), category=mox.IgnoreArg(), stacklevel=2) |
| 983 self.mox.ReplayAll() | 988 self.mox.ReplayAll() |
| 984 | 989 |
| 985 change = presubmit.Change('mychange', '', self.fake_root_dir, [], 0, 0) | 990 change = presubmit.Change('mychange', '', self.fake_root_dir, [], 0, 0) |
| 986 api = presubmit.InputApi( | 991 api = presubmit.InputApi( |
| 987 change, | 992 change, |
| 988 presubmit.os.path.join(self.fake_root_dir, 'foo', 'PRESUBMIT.py'), True) | 993 presubmit.os.path.join(self.fake_root_dir, 'foo', 'PRESUBMIT.py'), True, |
| 994 False, None) |
| 989 api.AffectedTextFiles(include_deletes=False) | 995 api.AffectedTextFiles(include_deletes=False) |
| 990 | 996 |
| 991 def testReadFileStringDenied(self): | 997 def testReadFileStringDenied(self): |
| 992 self.mox.ReplayAll() | 998 self.mox.ReplayAll() |
| 993 | 999 |
| 994 change = presubmit.Change('foo', 'foo', self.fake_root_dir, [('M', 'AA')], | 1000 change = presubmit.Change('foo', 'foo', self.fake_root_dir, [('M', 'AA')], |
| 995 0, 0) | 1001 0, 0) |
| 996 input_api = presubmit.InputApi( | 1002 input_api = presubmit.InputApi( |
| 997 change, presubmit.os.path.join(self.fake_root_dir, '/p'), False) | 1003 change, presubmit.os.path.join(self.fake_root_dir, '/p'), False, |
| 1004 False, None) |
| 998 self.assertRaises(IOError, input_api.ReadFile, 'boo', 'x') | 1005 self.assertRaises(IOError, input_api.ReadFile, 'boo', 'x') |
| 999 | 1006 |
| 1000 def testReadFileStringAccepted(self): | 1007 def testReadFileStringAccepted(self): |
| 1001 path = presubmit.os.path.join(self.fake_root_dir, 'AA/boo') | 1008 path = presubmit.os.path.join(self.fake_root_dir, 'AA/boo') |
| 1002 presubmit.gclient_utils.FileRead(path, 'x').AndReturn(None) | 1009 presubmit.gclient_utils.FileRead(path, 'x').AndReturn(None) |
| 1003 self.mox.ReplayAll() | 1010 self.mox.ReplayAll() |
| 1004 | 1011 |
| 1005 change = presubmit.Change('foo', 'foo', self.fake_root_dir, [('M', 'AA')], | 1012 change = presubmit.Change('foo', 'foo', self.fake_root_dir, [('M', 'AA')], |
| 1006 0, 0) | 1013 0, 0) |
| 1007 input_api = presubmit.InputApi( | 1014 input_api = presubmit.InputApi( |
| 1008 change, presubmit.os.path.join(self.fake_root_dir, '/p'), False) | 1015 change, presubmit.os.path.join(self.fake_root_dir, '/p'), False, |
| 1016 False, None) |
| 1009 input_api.ReadFile(path, 'x') | 1017 input_api.ReadFile(path, 'x') |
| 1010 | 1018 |
| 1011 def testReadFileAffectedFileDenied(self): | 1019 def testReadFileAffectedFileDenied(self): |
| 1012 fileobj = presubmit.AffectedFile('boo', 'M', 'Unrelated') | 1020 fileobj = presubmit.AffectedFile('boo', 'M', 'Unrelated') |
| 1013 self.mox.ReplayAll() | 1021 self.mox.ReplayAll() |
| 1014 | 1022 |
| 1015 change = presubmit.Change('foo', 'foo', self.fake_root_dir, [('M', 'AA')], | 1023 change = presubmit.Change('foo', 'foo', self.fake_root_dir, [('M', 'AA')], |
| 1016 0, 0) | 1024 0, 0) |
| 1017 input_api = presubmit.InputApi( | 1025 input_api = presubmit.InputApi( |
| 1018 change, presubmit.os.path.join(self.fake_root_dir, '/p'), False) | 1026 change, presubmit.os.path.join(self.fake_root_dir, '/p'), False, |
| 1027 False, None) |
| 1019 self.assertRaises(IOError, input_api.ReadFile, fileobj, 'x') | 1028 self.assertRaises(IOError, input_api.ReadFile, fileobj, 'x') |
| 1020 | 1029 |
| 1021 def testReadFileAffectedFileAccepted(self): | 1030 def testReadFileAffectedFileAccepted(self): |
| 1022 fileobj = presubmit.AffectedFile('AA/boo', 'M', self.fake_root_dir) | 1031 fileobj = presubmit.AffectedFile('AA/boo', 'M', self.fake_root_dir) |
| 1023 presubmit.gclient_utils.FileRead(fileobj.AbsoluteLocalPath(), 'x' | 1032 presubmit.gclient_utils.FileRead(fileobj.AbsoluteLocalPath(), 'x' |
| 1024 ).AndReturn(None) | 1033 ).AndReturn(None) |
| 1025 self.mox.ReplayAll() | 1034 self.mox.ReplayAll() |
| 1026 | 1035 |
| 1027 change = presubmit.Change('foo', 'foo', self.fake_root_dir, [('M', 'AA')], | 1036 change = presubmit.Change('foo', 'foo', self.fake_root_dir, [('M', 'AA')], |
| 1028 0, 0) | 1037 0, 0) |
| 1029 input_api = presubmit.InputApi( | 1038 input_api = presubmit.InputApi( |
| 1030 change, presubmit.os.path.join(self.fake_root_dir, '/p'), False) | 1039 change, presubmit.os.path.join(self.fake_root_dir, '/p'), False, |
| 1040 False, None) |
| 1031 input_api.ReadFile(fileobj, 'x') | 1041 input_api.ReadFile(fileobj, 'x') |
| 1032 | 1042 |
| 1033 | 1043 |
| 1034 class OuputApiUnittest(PresubmitTestsBase): | 1044 class OuputApiUnittest(PresubmitTestsBase): |
| 1035 """Tests presubmit.OutputApi.""" | 1045 """Tests presubmit.OutputApi.""" |
| 1036 def testMembersChanged(self): | 1046 def testMembersChanged(self): |
| 1037 self.mox.ReplayAll() | 1047 self.mox.ReplayAll() |
| 1038 members = [ | 1048 members = [ |
| 1039 'MailTextResult', 'PresubmitAddText', 'PresubmitError', | 1049 'MailTextResult', 'PresubmitAddText', 'PresubmitError', |
| 1040 'PresubmitNotifyResult', 'PresubmitPromptWarning', 'PresubmitResult', | 1050 'PresubmitNotifyResult', 'PresubmitPromptWarning', 'PresubmitResult', |
| 1041 ] | 1051 ] |
| 1042 # If this test fails, you should add the relevant test. | 1052 # If this test fails, you should add the relevant test. |
| 1043 self.compareMembers(presubmit.OutputApi(), members) | 1053 self.compareMembers(presubmit.OutputApi(), members) |
| 1044 | 1054 |
| 1045 def testOutputApiBasics(self): | 1055 def testOutputApiBasics(self): |
| 1046 self.mox.ReplayAll() | 1056 self.mox.ReplayAll() |
| 1047 self.failUnless(presubmit.OutputApi.PresubmitError('').IsFatal()) | 1057 self.failUnless(presubmit.OutputApi.PresubmitError('').IsFatal()) |
| 1048 self.failUnless(presubmit.OutputApi.PresubmitError('').IsMessage()) | |
| 1049 self.failIf(presubmit.OutputApi.PresubmitError('').ShouldPrompt()) | 1058 self.failIf(presubmit.OutputApi.PresubmitError('').ShouldPrompt()) |
| 1050 | 1059 |
| 1051 self.failIf(presubmit.OutputApi.PresubmitPromptWarning('').IsFatal()) | 1060 self.failIf(presubmit.OutputApi.PresubmitPromptWarning('').IsFatal()) |
| 1052 self.failUnless( | 1061 self.failUnless( |
| 1053 presubmit.OutputApi.PresubmitPromptWarning('').ShouldPrompt()) | 1062 presubmit.OutputApi.PresubmitPromptWarning('').ShouldPrompt()) |
| 1054 self.failUnless( | |
| 1055 presubmit.OutputApi.PresubmitPromptWarning('').IsMessage()) | |
| 1056 | 1063 |
| 1057 self.failIf(presubmit.OutputApi.PresubmitNotifyResult('').IsFatal()) | 1064 self.failIf(presubmit.OutputApi.PresubmitNotifyResult('').IsFatal()) |
| 1058 self.failIf(presubmit.OutputApi.PresubmitNotifyResult('').ShouldPrompt()) | 1065 self.failIf(presubmit.OutputApi.PresubmitNotifyResult('').ShouldPrompt()) |
| 1059 self.failUnless( | |
| 1060 presubmit.OutputApi.PresubmitNotifyResult('foo').IsMessage()) | |
| 1061 | 1066 |
| 1062 self.failIf(presubmit.OutputApi.PresubmitAddText('foo').IsFatal()) | 1067 self.failIf(presubmit.OutputApi.PresubmitAddText('foo').IsFatal()) |
| 1063 self.failIf(presubmit.OutputApi.PresubmitAddText('foo').ShouldPrompt()) | 1068 self.failIf(presubmit.OutputApi.PresubmitAddText('foo').ShouldPrompt()) |
| 1064 self.failIf(presubmit.OutputApi.PresubmitAddText('foo').IsMessage()) | |
| 1065 | 1069 |
| 1066 # TODO(joi) Test MailTextResult once implemented. | 1070 # TODO(joi) Test MailTextResult once implemented. |
| 1067 | 1071 |
| 1068 def testOutputApiHandling(self): | 1072 def testOutputApiHandling(self): |
| 1069 self.mox.ReplayAll() | 1073 self.mox.ReplayAll() |
| 1070 | 1074 |
| 1071 output = StringIO.StringIO() | 1075 output = StringIO.StringIO() |
| 1072 unused_input = StringIO.StringIO() | 1076 unused_input = StringIO.StringIO() |
| 1073 added_text = presubmit.OutputApi.PresubmitAddText('R=ben@example.com') | 1077 added_text = presubmit.OutputApi.PresubmitAddText('R=ben@example.com') |
| 1074 self.failUnless(added_text._Handle(output, unused_input)) | 1078 self.failUnless(added_text._Handle(output, unused_input)) |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1209 input_api.os_path = presubmit.os.path | 1213 input_api.os_path = presubmit.os.path |
| 1210 input_api.re = presubmit.re | 1214 input_api.re = presubmit.re |
| 1211 input_api.traceback = presubmit.traceback | 1215 input_api.traceback = presubmit.traceback |
| 1212 input_api.urllib2 = self.mox.CreateMock(presubmit.urllib2) | 1216 input_api.urllib2 = self.mox.CreateMock(presubmit.urllib2) |
| 1213 input_api.unittest = unittest | 1217 input_api.unittest = unittest |
| 1214 input_api.subprocess = self.mox.CreateMock(presubmit.subprocess) | 1218 input_api.subprocess = self.mox.CreateMock(presubmit.subprocess) |
| 1215 | 1219 |
| 1216 input_api.change = change | 1220 input_api.change = change |
| 1217 input_api.host_url = 'http://localhost' | 1221 input_api.host_url = 'http://localhost' |
| 1218 input_api.is_committing = committing | 1222 input_api.is_committing = committing |
| 1219 input_api.ts_tbr = False | 1223 input_api.tbr = False |
| 1220 input_api.python_executable = 'pyyyyython' | 1224 input_api.python_executable = 'pyyyyython' |
| 1221 return input_api | 1225 return input_api |
| 1222 | 1226 |
| 1223 def testMembersChanged(self): | 1227 def testMembersChanged(self): |
| 1224 self.mox.ReplayAll() | 1228 self.mox.ReplayAll() |
| 1225 members = [ | 1229 members = [ |
| 1226 'CheckChangeHasBugField', 'CheckChangeHasDescription', | 1230 'CheckChangeHasBugField', 'CheckChangeHasDescription', |
| 1227 'CheckChangeHasNoStrayWhitespace', | 1231 'CheckChangeHasNoStrayWhitespace', |
| 1228 'CheckChangeHasOnlyOneEol', 'CheckChangeHasNoCR', | 1232 'CheckChangeHasOnlyOneEol', 'CheckChangeHasNoCR', |
| 1229 'CheckChangeHasNoCrAndHasOnlyOneEol', 'CheckChangeHasNoTabs', | 1233 'CheckChangeHasNoCrAndHasOnlyOneEol', 'CheckChangeHasNoTabs', |
| (...skipping 622 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1852 }""") | 1856 }""") |
| 1853 connection.close() | 1857 connection.close() |
| 1854 self.mox.ReplayAll() | 1858 self.mox.ReplayAll() |
| 1855 | 1859 |
| 1856 results = presubmit_canned_checks.CheckBuildbotPendingBuilds( | 1860 results = presubmit_canned_checks.CheckBuildbotPendingBuilds( |
| 1857 input_api, presubmit.OutputApi, 'uurl', 2, ('foo')) | 1861 input_api, presubmit.OutputApi, 'uurl', 2, ('foo')) |
| 1858 self.assertEquals(len(results), 1) | 1862 self.assertEquals(len(results), 1) |
| 1859 self.assertEquals(results[0].__class__, | 1863 self.assertEquals(results[0].__class__, |
| 1860 presubmit.OutputApi.PresubmitNotifyResult) | 1864 presubmit.OutputApi.PresubmitNotifyResult) |
| 1861 | 1865 |
| 1862 def OwnersTest(self, is_committing, is_tbr=False, change_tags=None, | 1866 def OwnersTest(self, is_committing, tbr=False, change_tags=None, |
| 1863 suggested_reviewers=None, approvers=None, | 1867 suggested_reviewers=None, approvers=None, |
| 1864 uncovered_files=None, expected_results=None): | 1868 uncovered_files=None, expected_results=None): |
| 1865 affected_file = self.mox.CreateMock(presubmit.SvnAffectedFile) | 1869 affected_file = self.mox.CreateMock(presubmit.SvnAffectedFile) |
| 1866 affected_file.LocalPath().AndReturn('foo.cc') | 1870 affected_file.LocalPath().AndReturn('foo.cc') |
| 1867 change = self.mox.CreateMock(presubmit.Change) | 1871 change = self.mox.CreateMock(presubmit.Change) |
| 1868 change.AffectedFiles(None).AndReturn([affected_file]) | 1872 change.AffectedFiles(None).AndReturn([affected_file]) |
| 1869 | 1873 |
| 1870 input_api = self.MockInputApi(change, False) | 1874 input_api = self.MockInputApi(change, False) |
| 1871 fake_db = self.mox.CreateMock(owners.Database) | 1875 fake_db = self.mox.CreateMock(owners.Database) |
| 1872 fake_db.email_regexp = input_api.re.compile(owners.BASIC_EMAIL_REGEXP) | 1876 fake_db.email_regexp = input_api.re.compile(owners.BASIC_EMAIL_REGEXP) |
| 1873 input_api.owners_db = fake_db | 1877 input_api.owners_db = fake_db |
| 1874 input_api.is_committing = is_committing | 1878 input_api.is_committing = is_committing |
| 1875 input_api.is_tbr = is_tbr | 1879 input_api.tbr = tbr |
| 1876 | 1880 |
| 1877 if is_committing and not is_tbr: | 1881 if is_committing and not tbr: |
| 1878 change.issue = '1' | 1882 change.issue = '1' |
| 1879 messages = list('{"sender": "' + a + '","text": "lgtm"}' for | 1883 messages = list('{"sender": "' + a + '","text": "lgtm"}' for |
| 1880 a in approvers) | 1884 a in approvers) |
| 1881 rietveld_response = ('{"owner": "john@example.com",' | 1885 rietveld_response = ('{"owner": "john@example.com",' |
| 1882 '"messages": [' + ','.join(messages) + ']}') | 1886 '"messages": [' + ','.join(messages) + ']}') |
| 1883 input_api.urllib2.urlopen( | 1887 input_api.urllib2.urlopen( |
| 1884 input_api.host_url + '/api/1?messages=true').AndReturn( | 1888 input_api.host_url + '/api/1?messages=true').AndReturn( |
| 1885 StringIO.StringIO(rietveld_response)) | 1889 StringIO.StringIO(rietveld_response)) |
| 1886 input_api.json = presubmit.json | 1890 input_api.json = presubmit.json |
| 1887 fake_db.files_not_covered_by(set(['foo.cc']), approvers).AndReturn( | 1891 fake_db.files_not_covered_by(set(['foo.cc']), approvers).AndReturn( |
| 1888 uncovered_files) | 1892 uncovered_files) |
| 1889 elif not is_committing: | 1893 elif not is_committing: |
| 1890 change.tags = change_tags | 1894 change.tags = change_tags |
| 1891 if not change_tags.get('R'): | 1895 if not change_tags.get('R'): |
| 1892 fake_db.reviewers_for(set(['foo.cc'])).AndReturn(suggested_reviewers) | 1896 fake_db.reviewers_for(set(['foo.cc'])).AndReturn(suggested_reviewers) |
| 1893 | 1897 |
| 1894 self.mox.ReplayAll() | 1898 self.mox.ReplayAll() |
| 1895 results = presubmit_canned_checks.CheckOwners(input_api, | 1899 results = presubmit_canned_checks.CheckOwners(input_api, |
| 1896 presubmit.OutputApi) | 1900 presubmit.OutputApi) |
| 1897 self.assertEquals(len(results), len(expected_results)) | 1901 self.assertEquals(len(results), len(expected_results)) |
| 1898 if results and expected_results: | 1902 if results and expected_results: |
| 1899 output = StringIO.StringIO() | 1903 output = StringIO.StringIO() |
| 1900 unused_input = StringIO.StringIO() | 1904 unused_input = StringIO.StringIO() |
| 1901 results[0]._Handle(output, unused_input) | 1905 results[0]._Handle(output, unused_input) |
| 1902 self.assertEquals(output.getvalue(), expected_results[0]) | 1906 self.assertEquals(output.getvalue(), expected_results[0]) |
| 1903 | 1907 |
| 1904 def testCannedCheckOwners_WithReviewer(self): | 1908 def testCannedCheckOwners_WithReviewer(self): |
| 1905 self.OwnersTest(is_committing=False, change_tags={'R': 'ben@example.com'}, | 1909 self.OwnersTest(is_committing=False, change_tags={'R': 'ben@example.com'}, |
| 1906 expected_results=[]) | 1910 expected_results=[]) |
| 1907 self.OwnersTest(is_committing=False, is_tbr=True, | 1911 self.OwnersTest(is_committing=False, tbr=True, |
| 1908 change_tags={'R': 'ben@example.com'}, expected_results=[]) | 1912 change_tags={'R': 'ben@example.com'}, expected_results=[]) |
| 1909 | 1913 |
| 1910 def testCannedCheckOwners_NoReviewer(self): | 1914 def testCannedCheckOwners_NoReviewer(self): |
| 1911 self.OwnersTest(is_committing=False, change_tags={}, | 1915 self.OwnersTest(is_committing=False, change_tags={}, |
| 1912 suggested_reviewers=['ben@example.com'], | 1916 suggested_reviewers=['ben@example.com'], |
| 1913 expected_results=['ADD: R=ben@example.com\n']) | 1917 expected_results=['ADD: R=ben@example.com\n']) |
| 1914 self.OwnersTest(is_committing=False, is_tbr=True, change_tags={}, | 1918 self.OwnersTest(is_committing=False, tbr=True, change_tags={}, |
| 1915 suggested_reviewers=['ben@example.com'], | 1919 suggested_reviewers=['ben@example.com'], |
| 1916 expected_results=['ADD: R=ben@example.com\n']) | 1920 expected_results=['ADD: R=ben@example.com\n']) |
| 1917 | 1921 |
| 1918 def testCannedCheckOwners_CommittingWithoutOwnerLGTM(self): | 1922 def testCannedCheckOwners_CommittingWithoutOwnerLGTM(self): |
| 1919 self.OwnersTest(is_committing=True, | 1923 self.OwnersTest(is_committing=True, |
| 1920 approvers=set(), | 1924 approvers=set(), |
| 1921 uncovered_files=set(['foo.cc']), | 1925 uncovered_files=set(['foo.cc']), |
| 1922 expected_results=['Missing LGTM from an OWNER for: foo.cc\n']) | 1926 expected_results=['Missing LGTM from an OWNER for: foo.cc\n']) |
| 1923 | 1927 |
| 1924 def testCannedCheckOwners_CommittingWithLGTMs(self): | 1928 def testCannedCheckOwners_CommittingWithLGTMs(self): |
| 1925 self.OwnersTest(is_committing=True, | 1929 self.OwnersTest(is_committing=True, |
| 1926 approvers=set(['ben@example.com']), | 1930 approvers=set(['ben@example.com']), |
| 1927 uncovered_files=set(), | 1931 uncovered_files=set(), |
| 1928 expected_results=[]) | 1932 expected_results=[]) |
| 1929 | 1933 |
| 1930 def testCannedCheckOwners_TBR(self): | 1934 def testCannedCheckOwners_TBR(self): |
| 1931 self.OwnersTest(is_committing=True, is_tbr=True, | 1935 self.OwnersTest(is_committing=True, tbr=True, |
| 1932 approvers=set(), | 1936 approvers=set(), |
| 1933 uncovered_files=set(), | 1937 uncovered_files=set(), |
| 1934 expected_results=['--tbr was specified, skipping OWNERS check\n']) | 1938 expected_results=['--tbr was specified, skipping OWNERS check\n']) |
| 1935 | 1939 |
| 1936 if __name__ == '__main__': | 1940 if __name__ == '__main__': |
| 1937 import unittest | 1941 import unittest |
| 1938 unittest.main() | 1942 unittest.main() |
| OLD | NEW |