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 334 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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', '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', 'tbr', 'tempfile', 'traceback', 'unittest', 'urllib2', | 705 'subprocess', 'tbr', 'tempfile', 'traceback', 'unittest', 'urllib2', |
706 'version', | 706 'version', |
707 ] | 707 ] |
708 # If this test fails, you should add the relevant test. | 708 # If this test fails, you should add the relevant test. |
709 self.compareMembers(presubmit.InputApi(self.fake_change, './.', False), | 709 self.compareMembers(presubmit.InputApi(self.fake_change, './.', False, |
| 710 False, None), |
710 members) | 711 members) |
711 | 712 |
712 def testDepotToLocalPath(self): | 713 def testDepotToLocalPath(self): |
713 presubmit.scm.SVN.CaptureInfo('svn://foo/smurf').AndReturn( | 714 presubmit.scm.SVN.CaptureInfo('svn://foo/smurf').AndReturn( |
714 {'Path': 'prout'}) | 715 {'Path': 'prout'}) |
715 presubmit.scm.SVN.CaptureInfo('svn:/foo/notfound/burp').AndReturn({}) | 716 presubmit.scm.SVN.CaptureInfo('svn:/foo/notfound/burp').AndReturn({}) |
716 self.mox.ReplayAll() | 717 self.mox.ReplayAll() |
717 | 718 |
718 path = presubmit.InputApi(self.fake_change, './p', False).DepotToLocalPath( | 719 path = presubmit.InputApi(self.fake_change, './p', False, False, |
719 'svn://foo/smurf') | 720 None).DepotToLocalPath('svn://foo/smurf') |
720 self.failUnless(path == 'prout') | 721 self.failUnless(path == 'prout') |
721 path = presubmit.InputApi(self.fake_change, './p', False).DepotToLocalPath( | 722 path = presubmit.InputApi(self.fake_change, './p', False, False, |
722 'svn:/foo/notfound/burp') | 723 None).DepotToLocalPath('svn:/foo/notfound/burp') |
723 self.failUnless(path == None) | 724 self.failUnless(path == None) |
724 | 725 |
725 def testLocalToDepotPath(self): | 726 def testLocalToDepotPath(self): |
726 presubmit.scm.SVN.CaptureInfo('smurf').AndReturn({'URL': 'svn://foo'}) | 727 presubmit.scm.SVN.CaptureInfo('smurf').AndReturn({'URL': 'svn://foo'}) |
727 presubmit.scm.SVN.CaptureInfo('notfound-food').AndReturn({}) | 728 presubmit.scm.SVN.CaptureInfo('notfound-food').AndReturn({}) |
728 self.mox.ReplayAll() | 729 self.mox.ReplayAll() |
729 path = presubmit.InputApi(self.fake_change, './p', False).LocalToDepotPath( | 730 path = presubmit.InputApi(self.fake_change, './p', False, False, |
730 'smurf') | 731 None).LocalToDepotPath('smurf') |
731 self.assertEqual(path, 'svn://foo') | 732 self.assertEqual(path, 'svn://foo') |
732 path = presubmit.InputApi(self.fake_change, './p', False).LocalToDepotPath( | 733 path = presubmit.InputApi(self.fake_change, './p', False, False, |
733 'notfound-food') | 734 None).LocalToDepotPath('notfound-food') |
734 self.failUnless(path == None) | 735 self.failUnless(path == None) |
735 | 736 |
736 def testInputApiConstruction(self): | 737 def testInputApiConstruction(self): |
737 self.mox.ReplayAll() | 738 self.mox.ReplayAll() |
738 api = presubmit.InputApi(self.fake_change, | 739 api = presubmit.InputApi(self.fake_change, |
739 presubmit_path='foo/path/PRESUBMIT.py', | 740 presubmit_path='foo/path/PRESUBMIT.py', |
740 is_committing=False) | 741 is_committing=False, tbr=False, host_url=None) |
741 self.assertEquals(api.PresubmitLocalPath(), 'foo/path') | 742 self.assertEquals(api.PresubmitLocalPath(), 'foo/path') |
742 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') |
743 | 745 |
744 def testInputApiPresubmitScriptFiltering(self): | 746 def testInputApiPresubmitScriptFiltering(self): |
745 join = presubmit.os.path.join | 747 join = presubmit.os.path.join |
746 description_lines = ('Hello there', | 748 description_lines = ('Hello there', |
747 'this is a change', | 749 'this is a change', |
748 'BUG=123', | 750 'BUG=123', |
749 ' STORY =http://foo/ \t', | 751 ' STORY =http://foo/ \t', |
750 'and some more regular text') | 752 'and some more regular text') |
751 files = [ | 753 files = [ |
752 ['A', join('foo', 'blat.cc')], | 754 ['A', join('foo', 'blat.cc')], |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
788 presubmit.scm.SVN.GenerateDiff([blat]).AndReturn(self.presubmit_diffs) | 790 presubmit.scm.SVN.GenerateDiff([blat]).AndReturn(self.presubmit_diffs) |
789 presubmit.scm.SVN.GenerateDiff([another]).AndReturn(self.presubmit_diffs) | 791 presubmit.scm.SVN.GenerateDiff([another]).AndReturn(self.presubmit_diffs) |
790 | 792 |
791 self.mox.ReplayAll() | 793 self.mox.ReplayAll() |
792 | 794 |
793 change = presubmit.SvnChange('mychange', '\n'.join(description_lines), | 795 change = presubmit.SvnChange('mychange', '\n'.join(description_lines), |
794 self.fake_root_dir, files, 0, 0) | 796 self.fake_root_dir, files, 0, 0) |
795 input_api = presubmit.InputApi(change, | 797 input_api = presubmit.InputApi(change, |
796 join(self.fake_root_dir, 'foo', | 798 join(self.fake_root_dir, 'foo', |
797 'PRESUBMIT.py'), | 799 'PRESUBMIT.py'), |
798 False) | 800 False, False, None) |
799 # Doesn't filter much | 801 # Doesn't filter much |
800 got_files = input_api.AffectedFiles() | 802 got_files = input_api.AffectedFiles() |
801 self.assertEquals(len(got_files), 7) | 803 self.assertEquals(len(got_files), 7) |
802 self.assertEquals(got_files[0].LocalPath(), presubmit.normpath(files[0][1])) | 804 self.assertEquals(got_files[0].LocalPath(), presubmit.normpath(files[0][1])) |
803 self.assertEquals(got_files[1].LocalPath(), presubmit.normpath(files[1][1])) | 805 self.assertEquals(got_files[1].LocalPath(), presubmit.normpath(files[1][1])) |
804 self.assertEquals(got_files[2].LocalPath(), presubmit.normpath(files[2][1])) | 806 self.assertEquals(got_files[2].LocalPath(), presubmit.normpath(files[2][1])) |
805 self.assertEquals(got_files[3].LocalPath(), presubmit.normpath(files[3][1])) | 807 self.assertEquals(got_files[3].LocalPath(), presubmit.normpath(files[3][1])) |
806 self.assertEquals(got_files[4].LocalPath(), presubmit.normpath(files[4][1])) | 808 self.assertEquals(got_files[4].LocalPath(), presubmit.normpath(files[4][1])) |
807 self.assertEquals(got_files[5].LocalPath(), presubmit.normpath(files[5][1])) | 809 self.assertEquals(got_files[5].LocalPath(), presubmit.normpath(files[5][1])) |
808 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... |
873 f('a/.git'), | 875 f('a/.git'), |
874 f('b.c/.git'), | 876 f('b.c/.git'), |
875 f('a/.git/bleh.py'), | 877 f('a/.git/bleh.py'), |
876 f('.git/bleh.py'), | 878 f('.git/bleh.py'), |
877 ], | 879 ], |
878 [ | 880 [ |
879 # Expected. | 881 # Expected. |
880 ], | 882 ], |
881 ), | 883 ), |
882 ] | 884 ] |
883 input_api = presubmit.InputApi(self.fake_change, './PRESUBMIT.py', False) | 885 input_api = presubmit.InputApi(self.fake_change, './PRESUBMIT.py', False, |
| 886 False, None) |
884 self.mox.ReplayAll() | 887 self.mox.ReplayAll() |
885 | 888 |
886 self.assertEqual(len(input_api.DEFAULT_WHITE_LIST), 22) | 889 self.assertEqual(len(input_api.DEFAULT_WHITE_LIST), 22) |
887 self.assertEqual(len(input_api.DEFAULT_BLACK_LIST), 9) | 890 self.assertEqual(len(input_api.DEFAULT_BLACK_LIST), 9) |
888 for item in files: | 891 for item in files: |
889 results = filter(input_api.FilterSourceFile, item[0]) | 892 results = filter(input_api.FilterSourceFile, item[0]) |
890 for i in range(len(results)): | 893 for i in range(len(results)): |
891 self.assertEquals(results[i].LocalPath(), | 894 self.assertEquals(results[i].LocalPath(), |
892 presubmit.normpath(item[1][i])) | 895 presubmit.normpath(item[1][i])) |
893 # Same number of expected results. | 896 # Same number of expected results. |
(...skipping 10 matching lines...) Expand all Loading... |
904 presubmit.os.path.exists(item).AndReturn(True) | 907 presubmit.os.path.exists(item).AndReturn(True) |
905 presubmit.os.path.isdir(item).AndReturn(False) | 908 presubmit.os.path.isdir(item).AndReturn(False) |
906 presubmit.scm.SVN.GetFileProperty(item, 'svn:mime-type').AndReturn(None) | 909 presubmit.scm.SVN.GetFileProperty(item, 'svn:mime-type').AndReturn(None) |
907 self.mox.ReplayAll() | 910 self.mox.ReplayAll() |
908 | 911 |
909 change = presubmit.SvnChange('mychange', '', self.fake_root_dir, files, 0, | 912 change = presubmit.SvnChange('mychange', '', self.fake_root_dir, files, 0, |
910 0) | 913 0) |
911 input_api = presubmit.InputApi(change, | 914 input_api = presubmit.InputApi(change, |
912 presubmit.os.path.join(self.fake_root_dir, | 915 presubmit.os.path.join(self.fake_root_dir, |
913 'PRESUBMIT.py'), | 916 'PRESUBMIT.py'), |
914 False) | 917 False, False, None) |
915 got_files = input_api.AffectedSourceFiles(FilterSourceFile) | 918 got_files = input_api.AffectedSourceFiles(FilterSourceFile) |
916 self.assertEquals(len(got_files), 2) | 919 self.assertEquals(len(got_files), 2) |
917 self.assertEquals(got_files[0].LocalPath(), 'eeaee') | 920 self.assertEquals(got_files[0].LocalPath(), 'eeaee') |
918 self.assertEquals(got_files[1].LocalPath(), 'eeabee') | 921 self.assertEquals(got_files[1].LocalPath(), 'eeabee') |
919 | 922 |
920 def testLambdaFilter(self): | 923 def testLambdaFilter(self): |
921 white_list = presubmit.InputApi.DEFAULT_BLACK_LIST + (r".*?a.*?",) | 924 white_list = presubmit.InputApi.DEFAULT_BLACK_LIST + (r".*?a.*?",) |
922 black_list = [r".*?b.*?"] | 925 black_list = [r".*?b.*?"] |
923 files = [('A', 'eeaee'), ('M', 'eeabee'), ('M', 'eebcee'), ('M', 'eecaee')] | 926 files = [('A', 'eeaee'), ('M', 'eeabee'), ('M', 'eebcee'), ('M', 'eecaee')] |
924 for _, item in files: | 927 for _, item in files: |
925 item = presubmit.os.path.join(self.fake_root_dir, item) | 928 item = presubmit.os.path.join(self.fake_root_dir, item) |
926 presubmit.os.path.exists(item).AndReturn(True) | 929 presubmit.os.path.exists(item).AndReturn(True) |
927 presubmit.os.path.isdir(item).AndReturn(False) | 930 presubmit.os.path.isdir(item).AndReturn(False) |
928 presubmit.scm.SVN.GetFileProperty(item, 'svn:mime-type').AndReturn(None) | 931 presubmit.scm.SVN.GetFileProperty(item, 'svn:mime-type').AndReturn(None) |
929 self.mox.ReplayAll() | 932 self.mox.ReplayAll() |
930 | 933 |
931 change = presubmit.SvnChange('mychange', '', self.fake_root_dir, files, 0, | 934 change = presubmit.SvnChange('mychange', '', self.fake_root_dir, files, 0, |
932 0) | 935 0) |
933 input_api = presubmit.InputApi(change, './PRESUBMIT.py', False) | 936 input_api = presubmit.InputApi(change, './PRESUBMIT.py', False, |
| 937 False, None) |
934 # Sample usage of overiding the default white and black lists. | 938 # Sample usage of overiding the default white and black lists. |
935 got_files = input_api.AffectedSourceFiles( | 939 got_files = input_api.AffectedSourceFiles( |
936 lambda x: input_api.FilterSourceFile(x, white_list, black_list)) | 940 lambda x: input_api.FilterSourceFile(x, white_list, black_list)) |
937 self.assertEquals(len(got_files), 2) | 941 self.assertEquals(len(got_files), 2) |
938 self.assertEquals(got_files[0].LocalPath(), 'eeaee') | 942 self.assertEquals(got_files[0].LocalPath(), 'eeaee') |
939 self.assertEquals(got_files[1].LocalPath(), 'eecaee') | 943 self.assertEquals(got_files[1].LocalPath(), 'eecaee') |
940 | 944 |
941 def testGetAbsoluteLocalPath(self): | 945 def testGetAbsoluteLocalPath(self): |
942 join = presubmit.os.path.join | 946 join = presubmit.os.path.join |
943 normpath = presubmit.normpath | 947 normpath = presubmit.normpath |
(...skipping 19 matching lines...) Expand all Loading... |
963 normpath(join(self.fake_root_dir, 'isdir'))) | 967 normpath(join(self.fake_root_dir, 'isdir'))) |
964 self.assertEquals(affected_files[1].AbsoluteLocalPath(), | 968 self.assertEquals(affected_files[1].AbsoluteLocalPath(), |
965 normpath(join(self.fake_root_dir, 'isdir/blat.cc'))) | 969 normpath(join(self.fake_root_dir, 'isdir/blat.cc'))) |
966 | 970 |
967 # New helper functions need to work | 971 # New helper functions need to work |
968 paths_from_change = change.AbsoluteLocalPaths(include_dirs=True) | 972 paths_from_change = change.AbsoluteLocalPaths(include_dirs=True) |
969 self.assertEqual(len(paths_from_change), 3) | 973 self.assertEqual(len(paths_from_change), 3) |
970 presubmit_path = join(self.fake_root_dir, 'isdir', 'PRESUBMIT.py') | 974 presubmit_path = join(self.fake_root_dir, 'isdir', 'PRESUBMIT.py') |
971 api = presubmit.InputApi(change=change, | 975 api = presubmit.InputApi(change=change, |
972 presubmit_path=presubmit_path, | 976 presubmit_path=presubmit_path, |
973 is_committing=True) | 977 is_committing=True, tbr=False, host_url=None) |
974 paths_from_api = api.AbsoluteLocalPaths(include_dirs=True) | 978 paths_from_api = api.AbsoluteLocalPaths(include_dirs=True) |
975 self.assertEqual(len(paths_from_api), 2) | 979 self.assertEqual(len(paths_from_api), 2) |
976 for absolute_paths in [paths_from_change, paths_from_api]: | 980 for absolute_paths in [paths_from_change, paths_from_api]: |
977 self.assertEqual(absolute_paths[0], | 981 self.assertEqual(absolute_paths[0], |
978 normpath(join(self.fake_root_dir, 'isdir'))) | 982 normpath(join(self.fake_root_dir, 'isdir'))) |
979 self.assertEqual(absolute_paths[1], | 983 self.assertEqual(absolute_paths[1], |
980 normpath(join(self.fake_root_dir, 'isdir', 'blat.cc'))) | 984 normpath(join(self.fake_root_dir, 'isdir', 'blat.cc'))) |
981 | 985 |
982 def testDeprecated(self): | 986 def testDeprecated(self): |
983 presubmit.warn(mox.IgnoreArg(), category=mox.IgnoreArg(), stacklevel=2) | 987 presubmit.warn(mox.IgnoreArg(), category=mox.IgnoreArg(), stacklevel=2) |
984 self.mox.ReplayAll() | 988 self.mox.ReplayAll() |
985 | 989 |
986 change = presubmit.Change('mychange', '', self.fake_root_dir, [], 0, 0) | 990 change = presubmit.Change('mychange', '', self.fake_root_dir, [], 0, 0) |
987 api = presubmit.InputApi( | 991 api = presubmit.InputApi( |
988 change, | 992 change, |
989 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) |
990 api.AffectedTextFiles(include_deletes=False) | 995 api.AffectedTextFiles(include_deletes=False) |
991 | 996 |
992 def testReadFileStringDenied(self): | 997 def testReadFileStringDenied(self): |
993 self.mox.ReplayAll() | 998 self.mox.ReplayAll() |
994 | 999 |
995 change = presubmit.Change('foo', 'foo', self.fake_root_dir, [('M', 'AA')], | 1000 change = presubmit.Change('foo', 'foo', self.fake_root_dir, [('M', 'AA')], |
996 0, 0) | 1001 0, 0) |
997 input_api = presubmit.InputApi( | 1002 input_api = presubmit.InputApi( |
998 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) |
999 self.assertRaises(IOError, input_api.ReadFile, 'boo', 'x') | 1005 self.assertRaises(IOError, input_api.ReadFile, 'boo', 'x') |
1000 | 1006 |
1001 def testReadFileStringAccepted(self): | 1007 def testReadFileStringAccepted(self): |
1002 path = presubmit.os.path.join(self.fake_root_dir, 'AA/boo') | 1008 path = presubmit.os.path.join(self.fake_root_dir, 'AA/boo') |
1003 presubmit.gclient_utils.FileRead(path, 'x').AndReturn(None) | 1009 presubmit.gclient_utils.FileRead(path, 'x').AndReturn(None) |
1004 self.mox.ReplayAll() | 1010 self.mox.ReplayAll() |
1005 | 1011 |
1006 change = presubmit.Change('foo', 'foo', self.fake_root_dir, [('M', 'AA')], | 1012 change = presubmit.Change('foo', 'foo', self.fake_root_dir, [('M', 'AA')], |
1007 0, 0) | 1013 0, 0) |
1008 input_api = presubmit.InputApi( | 1014 input_api = presubmit.InputApi( |
1009 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) |
1010 input_api.ReadFile(path, 'x') | 1017 input_api.ReadFile(path, 'x') |
1011 | 1018 |
1012 def testReadFileAffectedFileDenied(self): | 1019 def testReadFileAffectedFileDenied(self): |
1013 fileobj = presubmit.AffectedFile('boo', 'M', 'Unrelated') | 1020 fileobj = presubmit.AffectedFile('boo', 'M', 'Unrelated') |
1014 self.mox.ReplayAll() | 1021 self.mox.ReplayAll() |
1015 | 1022 |
1016 change = presubmit.Change('foo', 'foo', self.fake_root_dir, [('M', 'AA')], | 1023 change = presubmit.Change('foo', 'foo', self.fake_root_dir, [('M', 'AA')], |
1017 0, 0) | 1024 0, 0) |
1018 input_api = presubmit.InputApi( | 1025 input_api = presubmit.InputApi( |
1019 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) |
1020 self.assertRaises(IOError, input_api.ReadFile, fileobj, 'x') | 1028 self.assertRaises(IOError, input_api.ReadFile, fileobj, 'x') |
1021 | 1029 |
1022 def testReadFileAffectedFileAccepted(self): | 1030 def testReadFileAffectedFileAccepted(self): |
1023 fileobj = presubmit.AffectedFile('AA/boo', 'M', self.fake_root_dir) | 1031 fileobj = presubmit.AffectedFile('AA/boo', 'M', self.fake_root_dir) |
1024 presubmit.gclient_utils.FileRead(fileobj.AbsoluteLocalPath(), 'x' | 1032 presubmit.gclient_utils.FileRead(fileobj.AbsoluteLocalPath(), 'x' |
1025 ).AndReturn(None) | 1033 ).AndReturn(None) |
1026 self.mox.ReplayAll() | 1034 self.mox.ReplayAll() |
1027 | 1035 |
1028 change = presubmit.Change('foo', 'foo', self.fake_root_dir, [('M', 'AA')], | 1036 change = presubmit.Change('foo', 'foo', self.fake_root_dir, [('M', 'AA')], |
1029 0, 0) | 1037 0, 0) |
1030 input_api = presubmit.InputApi( | 1038 input_api = presubmit.InputApi( |
1031 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) |
1032 input_api.ReadFile(fileobj, 'x') | 1041 input_api.ReadFile(fileobj, 'x') |
1033 | 1042 |
1034 | 1043 |
1035 class OuputApiUnittest(PresubmitTestsBase): | 1044 class OuputApiUnittest(PresubmitTestsBase): |
1036 """Tests presubmit.OutputApi.""" | 1045 """Tests presubmit.OutputApi.""" |
1037 def testMembersChanged(self): | 1046 def testMembersChanged(self): |
1038 self.mox.ReplayAll() | 1047 self.mox.ReplayAll() |
1039 members = [ | 1048 members = [ |
1040 'MailTextResult', 'PresubmitAddText', 'PresubmitError', | 1049 'MailTextResult', 'PresubmitAddText', 'PresubmitError', |
1041 'PresubmitNotifyResult', 'PresubmitPromptWarning', 'PresubmitResult', | 1050 'PresubmitNotifyResult', 'PresubmitPromptWarning', 'PresubmitResult', |
1042 ] | 1051 ] |
1043 # If this test fails, you should add the relevant test. | 1052 # If this test fails, you should add the relevant test. |
1044 self.compareMembers(presubmit.OutputApi(), members) | 1053 self.compareMembers(presubmit.OutputApi(), members) |
1045 | 1054 |
1046 def testOutputApiBasics(self): | 1055 def testOutputApiBasics(self): |
1047 self.mox.ReplayAll() | 1056 self.mox.ReplayAll() |
1048 self.failUnless(presubmit.OutputApi.PresubmitError('').IsFatal()) | 1057 self.failUnless(presubmit.OutputApi.PresubmitError('').IsFatal()) |
1049 self.failUnless(presubmit.OutputApi.PresubmitError('').IsMessage()) | |
1050 self.failIf(presubmit.OutputApi.PresubmitError('').ShouldPrompt()) | 1058 self.failIf(presubmit.OutputApi.PresubmitError('').ShouldPrompt()) |
1051 | 1059 |
1052 self.failIf(presubmit.OutputApi.PresubmitPromptWarning('').IsFatal()) | 1060 self.failIf(presubmit.OutputApi.PresubmitPromptWarning('').IsFatal()) |
1053 self.failUnless( | 1061 self.failUnless( |
1054 presubmit.OutputApi.PresubmitPromptWarning('').ShouldPrompt()) | 1062 presubmit.OutputApi.PresubmitPromptWarning('').ShouldPrompt()) |
1055 self.failUnless( | |
1056 presubmit.OutputApi.PresubmitPromptWarning('').IsMessage()) | |
1057 | 1063 |
1058 self.failIf(presubmit.OutputApi.PresubmitNotifyResult('').IsFatal()) | 1064 self.failIf(presubmit.OutputApi.PresubmitNotifyResult('').IsFatal()) |
1059 self.failIf(presubmit.OutputApi.PresubmitNotifyResult('').ShouldPrompt()) | 1065 self.failIf(presubmit.OutputApi.PresubmitNotifyResult('').ShouldPrompt()) |
1060 self.failUnless( | |
1061 presubmit.OutputApi.PresubmitNotifyResult('foo').IsMessage()) | |
1062 | 1066 |
1063 self.failIf(presubmit.OutputApi.PresubmitAddText('foo').IsFatal()) | 1067 self.failIf(presubmit.OutputApi.PresubmitAddText('foo').IsFatal()) |
1064 self.failIf(presubmit.OutputApi.PresubmitAddText('foo').ShouldPrompt()) | 1068 self.failIf(presubmit.OutputApi.PresubmitAddText('foo').ShouldPrompt()) |
1065 self.failIf(presubmit.OutputApi.PresubmitAddText('foo').IsMessage()) | |
1066 | 1069 |
1067 # TODO(joi) Test MailTextResult once implemented. | 1070 # TODO(joi) Test MailTextResult once implemented. |
1068 | 1071 |
1069 def testOutputApiHandling(self): | 1072 def testOutputApiHandling(self): |
1070 self.mox.ReplayAll() | 1073 self.mox.ReplayAll() |
1071 | 1074 |
1072 output = StringIO.StringIO() | 1075 output = StringIO.StringIO() |
1073 unused_input = StringIO.StringIO() | 1076 unused_input = StringIO.StringIO() |
1074 added_text = presubmit.OutputApi.PresubmitAddText('R=ben@example.com') | 1077 added_text = presubmit.OutputApi.PresubmitAddText('R=ben@example.com') |
1075 self.failUnless(added_text._Handle(output, unused_input)) | 1078 self.failUnless(added_text._Handle(output, unused_input)) |
(...skipping 797 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1873 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) |
1874 input_api.owners_db = fake_db | 1877 input_api.owners_db = fake_db |
1875 input_api.is_committing = is_committing | 1878 input_api.is_committing = is_committing |
1876 input_api.tbr = tbr | 1879 input_api.tbr = tbr |
1877 | 1880 |
1878 if is_committing and not tbr: | 1881 if is_committing and not tbr: |
1879 change.issue = '1' | 1882 change.issue = '1' |
1880 messages = list('{"sender": "' + a + '","text": "lgtm"}' for | 1883 messages = list('{"sender": "' + a + '","text": "lgtm"}' for |
1881 a in approvers) | 1884 a in approvers) |
1882 rietveld_response = ('{"owner": "john@example.com",' | 1885 rietveld_response = ('{"owner": "john@example.com",' |
1883 '"messages": [' + ','.join(messages) + ']}') | 1886 '"messages": [' + ','.join(messages) + ']}') |
1884 input_api.urllib2.urlopen( | 1887 input_api.urllib2.urlopen( |
1885 input_api.host_url + '/api/1?messages=true').AndReturn( | 1888 input_api.host_url + '/api/1?messages=true').AndReturn( |
1886 StringIO.StringIO(rietveld_response)) | 1889 StringIO.StringIO(rietveld_response)) |
1887 input_api.json = presubmit.json | 1890 input_api.json = presubmit.json |
1888 fake_db.files_not_covered_by(set(['foo.cc']), approvers).AndReturn( | 1891 fake_db.files_not_covered_by(set(['foo.cc']), approvers).AndReturn( |
1889 uncovered_files) | 1892 uncovered_files) |
1890 elif not is_committing: | 1893 elif not is_committing: |
1891 change.tags = change_tags | 1894 change.tags = change_tags |
1892 if not change_tags.get('R'): | 1895 if not change_tags.get('R'): |
1893 fake_db.reviewers_for(set(['foo.cc'])).AndReturn(suggested_reviewers) | 1896 fake_db.reviewers_for(set(['foo.cc'])).AndReturn(suggested_reviewers) |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1930 | 1933 |
1931 def testCannedCheckOwners_TBR(self): | 1934 def testCannedCheckOwners_TBR(self): |
1932 self.OwnersTest(is_committing=True, tbr=True, | 1935 self.OwnersTest(is_committing=True, tbr=True, |
1933 approvers=set(), | 1936 approvers=set(), |
1934 uncovered_files=set(), | 1937 uncovered_files=set(), |
1935 expected_results=['--tbr was specified, skipping OWNERS check\n']) | 1938 expected_results=['--tbr was specified, skipping OWNERS check\n']) |
1936 | 1939 |
1937 if __name__ == '__main__': | 1940 if __name__ == '__main__': |
1938 import unittest | 1941 import unittest |
1939 unittest.main() | 1942 unittest.main() |
OLD | NEW |