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: disable=E1101,E1103 | 8 # pylint: disable=E1101,E1103 |
9 | 9 |
10 import logging | 10 import logging |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 this is line number 45 | 105 this is line number 45 |
106 + | 106 + |
107 this is line number 46 | 107 this is line number 46 |
108 this is line number 47 | 108 this is line number 47 |
109 -this is line number 48 | 109 -this is line number 48 |
110 +this is line number 48.1 | 110 +this is line number 48.1 |
111 this is line number 49 | 111 this is line number 49 |
112 """ | 112 """ |
113 | 113 |
114 def setUp(self): | 114 def setUp(self): |
| 115 SuperMoxTestBase.setUp(self) |
115 class FakeChange(object): | 116 class FakeChange(object): |
116 root = '/' | 117 def __init__(self, obj): |
| 118 self._root = obj.fake_root_dir |
| 119 def RepositoryRoot(self): |
| 120 return self._root |
117 | 121 |
118 def RepositoryRoot(self): | |
119 return self.root | |
120 | |
121 SuperMoxTestBase.setUp(self) | |
122 self.fake_change = FakeChange() | |
123 self.mox.StubOutWithMock(presubmit, 'random') | 122 self.mox.StubOutWithMock(presubmit, 'random') |
124 self.mox.StubOutWithMock(presubmit, 'warn') | 123 self.mox.StubOutWithMock(presubmit, 'warn') |
125 presubmit._ASKED_FOR_FEEDBACK = False | 124 presubmit._ASKED_FOR_FEEDBACK = False |
126 self.fake_root_dir = self.RootDir() | 125 self.fake_root_dir = self.RootDir() |
| 126 self.fake_change = FakeChange(self) |
| 127 |
127 # Special mocks. | 128 # Special mocks. |
128 def MockAbsPath(f): | 129 def MockAbsPath(f): |
129 return f | 130 return f |
130 def MockChdir(f): | 131 def MockChdir(f): |
131 return None | 132 return None |
132 # SuperMoxTestBase already mock these but simplify our life. | 133 # SuperMoxTestBase already mock these but simplify our life. |
133 presubmit.os.path.abspath = MockAbsPath | 134 presubmit.os.path.abspath = MockAbsPath |
134 presubmit.os.getcwd = self.RootDir | 135 presubmit.os.getcwd = self.RootDir |
135 presubmit.os.chdir = MockChdir | 136 presubmit.os.chdir = MockChdir |
136 self.mox.StubOutWithMock(presubmit.scm, 'determine_scm') | 137 self.mox.StubOutWithMock(presubmit.scm, 'determine_scm') |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 'BUG=123', | 247 'BUG=123', |
247 ' STORY =http://foo/ \t', | 248 ' STORY =http://foo/ \t', |
248 'and some more regular text \t') | 249 'and some more regular text \t') |
249 files = [ | 250 files = [ |
250 ['A', 'foo/blat.cc'], | 251 ['A', 'foo/blat.cc'], |
251 ['M', 'binary.dll'], # a binary file | 252 ['M', 'binary.dll'], # a binary file |
252 ['A', 'isdir'], # a directory | 253 ['A', 'isdir'], # a directory |
253 ['?', 'flop/notfound.txt'], # not found in SVN, still exists locally | 254 ['?', 'flop/notfound.txt'], # not found in SVN, still exists locally |
254 ['D', 'boo/flap.h'], | 255 ['D', 'boo/flap.h'], |
255 ] | 256 ] |
256 blat = presubmit.os.path.join(self.fake_root_dir, 'foo', 'blat.cc') | 257 blat = presubmit.os.path.join('foo', 'blat.cc') |
257 notfound = presubmit.os.path.join( | 258 notfound = presubmit.os.path.join('flop', 'notfound.txt') |
258 self.fake_root_dir, 'flop', 'notfound.txt') | 259 flap = presubmit.os.path.join('boo', 'flap.h') |
259 flap = presubmit.os.path.join(self.fake_root_dir, 'boo', 'flap.h') | 260 binary = 'binary.dll' |
260 binary = presubmit.os.path.join(self.fake_root_dir, 'binary.dll') | 261 isdir = 'isdir' |
261 isdir = presubmit.os.path.join(self.fake_root_dir, 'isdir') | 262 f_blat = presubmit.os.path.join(self.fake_root_dir, blat) |
262 presubmit.os.path.exists(blat).AndReturn(True) | 263 f_notfound = presubmit.os.path.join(self.fake_root_dir, notfound) |
263 presubmit.os.path.isdir(blat).AndReturn(False) | 264 f_flap = presubmit.os.path.join(self.fake_root_dir, flap) |
264 presubmit.os.path.exists(binary).AndReturn(True) | 265 f_binary = presubmit.os.path.join(self.fake_root_dir, binary) |
265 presubmit.os.path.isdir(binary).AndReturn(False) | 266 f_isdir = presubmit.os.path.join(self.fake_root_dir, isdir) |
266 presubmit.os.path.exists(isdir).AndReturn(True) | 267 presubmit.os.path.exists(f_blat).AndReturn(True) |
267 presubmit.os.path.isdir(isdir).AndReturn(True) | 268 presubmit.os.path.isdir(f_blat).AndReturn(False) |
268 presubmit.os.path.exists(notfound).AndReturn(True) | 269 presubmit.os.path.exists(f_binary).AndReturn(True) |
269 presubmit.os.path.isdir(notfound).AndReturn(False) | 270 presubmit.os.path.isdir(f_binary).AndReturn(False) |
270 presubmit.os.path.exists(flap).AndReturn(False) | 271 presubmit.os.path.exists(f_isdir).AndReturn(True) |
271 presubmit.scm.SVN.CaptureInfo(flap | 272 presubmit.os.path.isdir(f_isdir).AndReturn(True) |
| 273 presubmit.os.path.exists(f_notfound).AndReturn(True) |
| 274 presubmit.os.path.isdir(f_notfound).AndReturn(False) |
| 275 presubmit.os.path.exists(f_flap).AndReturn(False) |
| 276 presubmit.scm.SVN.CaptureInfo(flap, self.fake_root_dir |
272 ).AndReturn({'Node Kind': 'file'}) | 277 ).AndReturn({'Node Kind': 'file'}) |
273 presubmit.scm.SVN.GetFileProperty(blat, 'svn:mime-type').AndReturn(None) | |
274 presubmit.scm.SVN.GetFileProperty( | 278 presubmit.scm.SVN.GetFileProperty( |
275 binary, 'svn:mime-type').AndReturn('application/octet-stream') | 279 blat, 'svn:mime-type', self.fake_root_dir).AndReturn(None) |
276 presubmit.scm.SVN.GetFileProperty( | 280 presubmit.scm.SVN.GetFileProperty( |
277 notfound, 'svn:mime-type').AndReturn('') | 281 binary, 'svn:mime-type', self.fake_root_dir |
278 presubmit.scm.SVN.CaptureInfo(blat).AndReturn( | 282 ).AndReturn('application/octet-stream') |
| 283 presubmit.scm.SVN.GetFileProperty( |
| 284 notfound, 'svn:mime-type', self.fake_root_dir).AndReturn('') |
| 285 presubmit.scm.SVN.CaptureInfo(blat, self.fake_root_dir).AndReturn( |
279 {'URL': 'svn:/foo/foo/blat.cc'}) | 286 {'URL': 'svn:/foo/foo/blat.cc'}) |
280 presubmit.scm.SVN.CaptureInfo(binary).AndReturn( | 287 presubmit.scm.SVN.CaptureInfo(binary, self.fake_root_dir).AndReturn( |
281 {'URL': 'svn:/foo/binary.dll'}) | 288 {'URL': 'svn:/foo/binary.dll'}) |
282 presubmit.scm.SVN.CaptureInfo(notfound).AndReturn({}) | 289 presubmit.scm.SVN.CaptureInfo(notfound, self.fake_root_dir).AndReturn({}) |
283 presubmit.scm.SVN.CaptureInfo(flap).AndReturn( | 290 presubmit.scm.SVN.CaptureInfo(flap, self.fake_root_dir).AndReturn( |
284 {'URL': 'svn:/foo/boo/flap.h'}) | 291 {'URL': 'svn:/foo/boo/flap.h'}) |
285 presubmit.scm.SVN.GenerateDiff([blat]).AndReturn(self.presubmit_diffs) | 292 presubmit.scm.SVN.GenerateDiff([blat], self.fake_root_dir, False, None |
286 presubmit.scm.SVN.GenerateDiff([notfound]).AndReturn(self.presubmit_diffs) | 293 ).AndReturn(self.presubmit_diffs) |
| 294 presubmit.scm.SVN.GenerateDiff([notfound], self.fake_root_dir, False, None |
| 295 ).AndReturn(self.presubmit_diffs) |
287 | 296 |
288 self.mox.ReplayAll() | 297 self.mox.ReplayAll() |
289 | 298 |
290 change = presubmit.SvnChange( | 299 change = presubmit.SvnChange( |
291 'mychange', | 300 'mychange', |
292 '\n'.join(description_lines), | 301 '\n'.join(description_lines), |
293 self.fake_root_dir, | 302 self.fake_root_dir, |
294 files, | 303 files, |
295 0, | 304 0, |
296 0, | 305 0, |
(...skipping 542 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
839 'os_path', 'owners_db', 'pickle', 'platform', 'python_executable', 're', | 848 'os_path', 'owners_db', 'pickle', 'platform', 'python_executable', 're', |
840 'rietveld', 'subprocess', 'tbr', 'tempfile', 'time', 'traceback', | 849 'rietveld', 'subprocess', 'tbr', 'tempfile', 'time', 'traceback', |
841 'unittest', 'urllib2', 'version', 'verbose', | 850 'unittest', 'urllib2', 'version', 'verbose', |
842 ] | 851 ] |
843 # If this test fails, you should add the relevant test. | 852 # If this test fails, you should add the relevant test. |
844 self.compareMembers( | 853 self.compareMembers( |
845 presubmit.InputApi(self.fake_change, './.', False, None, False), | 854 presubmit.InputApi(self.fake_change, './.', False, None, False), |
846 members) | 855 members) |
847 | 856 |
848 def testDepotToLocalPath(self): | 857 def testDepotToLocalPath(self): |
849 presubmit.scm.SVN.CaptureInfo('svn://foo/smurf').AndReturn( | 858 presubmit.scm.SVN.CaptureInfo('svn://foo/smurf', self.fake_root_dir |
850 {'Path': 'prout'}) | 859 ).AndReturn({'Path': 'prout'}) |
851 presubmit.scm.SVN.CaptureInfo('svn:/foo/notfound/burp').AndReturn({}) | 860 presubmit.scm.SVN.CaptureInfo('svn:/foo/notfound/burp', self.fake_root_dir |
| 861 ).AndReturn({}) |
852 self.mox.ReplayAll() | 862 self.mox.ReplayAll() |
853 | 863 |
854 path = presubmit.InputApi( | 864 path = presubmit.InputApi( |
855 self.fake_change, './p', False, None, False).DepotToLocalPath( | 865 self.fake_change, './p', False, None, False).DepotToLocalPath( |
856 'svn://foo/smurf') | 866 'svn://foo/smurf') |
857 self.failUnless(path == 'prout') | 867 self.failUnless(path == 'prout') |
858 path = presubmit.InputApi( | 868 path = presubmit.InputApi( |
859 self.fake_change, './p', False, None, False).DepotToLocalPath( | 869 self.fake_change, './p', False, None, False).DepotToLocalPath( |
860 'svn:/foo/notfound/burp') | 870 'svn:/foo/notfound/burp') |
861 self.failUnless(path == None) | 871 self.failUnless(path == None) |
862 | 872 |
863 def testLocalToDepotPath(self): | 873 def testLocalToDepotPath(self): |
864 presubmit.scm.SVN.CaptureInfo('smurf').AndReturn({'URL': 'svn://foo'}) | 874 presubmit.scm.SVN.CaptureInfo('smurf', self.fake_root_dir |
865 presubmit.scm.SVN.CaptureInfo('notfound-food').AndReturn({}) | 875 ).AndReturn({'URL': 'svn://foo'}) |
| 876 presubmit.scm.SVN.CaptureInfo('notfound-food', self.fake_root_dir |
| 877 ).AndReturn({}) |
866 self.mox.ReplayAll() | 878 self.mox.ReplayAll() |
867 path = presubmit.InputApi( | 879 path = presubmit.InputApi( |
868 self.fake_change, './p', False, None, False).LocalToDepotPath( | 880 self.fake_change, './p', False, None, False).LocalToDepotPath( |
869 'smurf') | 881 'smurf') |
870 self.assertEqual(path, 'svn://foo') | 882 self.assertEqual(path, 'svn://foo') |
871 path = presubmit.InputApi( | 883 path = presubmit.InputApi( |
872 self.fake_change, './p', False, None, False).LocalToDepotPath( | 884 self.fake_change, './p', False, None, False).LocalToDepotPath( |
873 'notfound-food') | 885 'notfound-food') |
874 self.assertEquals(path, None) | 886 self.assertEquals(path, None) |
875 | 887 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
910 beingdeleted = presubmit.normpath(join(self.fake_root_dir, files[6][1])) | 922 beingdeleted = presubmit.normpath(join(self.fake_root_dir, files[6][1])) |
911 notfound = presubmit.normpath(join(self.fake_root_dir, files[7][1])) | 923 notfound = presubmit.normpath(join(self.fake_root_dir, files[7][1])) |
912 flap = presubmit.normpath(join(self.fake_root_dir, files[8][1])) | 924 flap = presubmit.normpath(join(self.fake_root_dir, files[8][1])) |
913 for i in (blat, readme, binary, weird, another, third_party): | 925 for i in (blat, readme, binary, weird, another, third_party): |
914 presubmit.os.path.exists(i).AndReturn(True) | 926 presubmit.os.path.exists(i).AndReturn(True) |
915 presubmit.os.path.isdir(i).AndReturn(False) | 927 presubmit.os.path.isdir(i).AndReturn(False) |
916 presubmit.os.path.exists(beingdeleted).AndReturn(False) | 928 presubmit.os.path.exists(beingdeleted).AndReturn(False) |
917 presubmit.os.path.exists(notfound).AndReturn(False) | 929 presubmit.os.path.exists(notfound).AndReturn(False) |
918 presubmit.os.path.exists(flap).AndReturn(True) | 930 presubmit.os.path.exists(flap).AndReturn(True) |
919 presubmit.os.path.isdir(flap).AndReturn(False) | 931 presubmit.os.path.isdir(flap).AndReturn(False) |
920 presubmit.scm.SVN.CaptureInfo(beingdeleted).AndReturn({}) | 932 presubmit.scm.SVN.CaptureInfo(files[6][1], self.fake_root_dir).AndReturn({}) |
921 presubmit.scm.SVN.CaptureInfo(notfound).AndReturn({}) | 933 presubmit.scm.SVN.CaptureInfo(files[7][1], self.fake_root_dir).AndReturn({}) |
922 presubmit.scm.SVN.GetFileProperty(blat, 'svn:mime-type').AndReturn(None) | 934 presubmit.scm.SVN.GetFileProperty( |
923 presubmit.scm.SVN.GetFileProperty(readme, 'svn:mime-type').AndReturn(None) | 935 files[0][1], 'svn:mime-type', self.fake_root_dir |
924 presubmit.scm.SVN.GetFileProperty(binary, 'svn:mime-type').AndReturn( | |
925 'application/octet-stream') | |
926 presubmit.scm.SVN.GetFileProperty(weird, 'svn:mime-type').AndReturn(None) | |
927 presubmit.scm.SVN.GetFileProperty(another, 'svn:mime-type').AndReturn(None) | |
928 presubmit.scm.SVN.GetFileProperty(third_party, 'svn:mime-type' | |
929 ).AndReturn(None) | 936 ).AndReturn(None) |
930 presubmit.scm.SVN.GenerateDiff([blat]).AndReturn(self.presubmit_diffs) | 937 presubmit.scm.SVN.GetFileProperty( |
931 presubmit.scm.SVN.GenerateDiff([another]).AndReturn(self.presubmit_diffs) | 938 files[1][1], 'svn:mime-type', self.fake_root_dir |
| 939 ).AndReturn(None) |
| 940 presubmit.scm.SVN.GetFileProperty( |
| 941 files[2][1], 'svn:mime-type', self.fake_root_dir |
| 942 ).AndReturn('application/octet-stream') |
| 943 presubmit.scm.SVN.GetFileProperty( |
| 944 files[3][1], 'svn:mime-type', self.fake_root_dir |
| 945 ).AndReturn(None) |
| 946 presubmit.scm.SVN.GetFileProperty( |
| 947 files[4][1], 'svn:mime-type', self.fake_root_dir |
| 948 ).AndReturn(None) |
| 949 presubmit.scm.SVN.GetFileProperty( |
| 950 files[5][1], 'svn:mime-type', self.fake_root_dir |
| 951 ).AndReturn(None) |
| 952 presubmit.scm.SVN.GenerateDiff( |
| 953 [files[0][1]], self.fake_root_dir, False, None |
| 954 ).AndReturn(self.presubmit_diffs) |
| 955 presubmit.scm.SVN.GenerateDiff( |
| 956 [files[4][1]], self.fake_root_dir, False, None |
| 957 ).AndReturn(self.presubmit_diffs) |
932 | 958 |
933 self.mox.ReplayAll() | 959 self.mox.ReplayAll() |
934 | 960 |
935 change = presubmit.SvnChange( | 961 change = presubmit.SvnChange( |
936 'mychange', | 962 'mychange', |
937 '\n'.join(description_lines), | 963 '\n'.join(description_lines), |
938 self.fake_root_dir, | 964 self.fake_root_dir, |
939 files, | 965 files, |
940 0, | 966 0, |
941 0, | 967 0, |
(...skipping 21 matching lines...) Expand all Loading... |
963 presubmit.normpath(files[0][1])) | 989 presubmit.normpath(files[0][1])) |
964 self.assertEqual(rhs_lines[3][0].LocalPath(), | 990 self.assertEqual(rhs_lines[3][0].LocalPath(), |
965 presubmit.normpath(files[0][1])) | 991 presubmit.normpath(files[0][1])) |
966 self.assertEqual(rhs_lines[7][0].LocalPath(), | 992 self.assertEqual(rhs_lines[7][0].LocalPath(), |
967 presubmit.normpath(files[4][1])) | 993 presubmit.normpath(files[4][1])) |
968 self.assertEqual(rhs_lines[13][0].LocalPath(), | 994 self.assertEqual(rhs_lines[13][0].LocalPath(), |
969 presubmit.normpath(files[4][1])) | 995 presubmit.normpath(files[4][1])) |
970 | 996 |
971 def testDefaultWhiteListBlackListFilters(self): | 997 def testDefaultWhiteListBlackListFilters(self): |
972 def f(x): | 998 def f(x): |
973 return presubmit.AffectedFile(x, 'M') | 999 return presubmit.AffectedFile(x, 'M', self.fake_root_dir) |
974 files = [ | 1000 files = [ |
975 ( | 1001 ( |
976 [ | 1002 [ |
977 # To be tested. | 1003 # To be tested. |
978 f('a/experimental/b'), | 1004 f('a/experimental/b'), |
979 f('experimental/b'), | 1005 f('experimental/b'), |
980 f('a/experimental'), | 1006 f('a/experimental'), |
981 f('a/experimental.cc'), | 1007 f('a/experimental.cc'), |
982 f('a/experimental.S'), | 1008 f('a/experimental.S'), |
983 ], | 1009 ], |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1044 # Same number of expected results. | 1070 # Same number of expected results. |
1045 self.assertEquals(sorted([f.LocalPath().replace(presubmit.os.sep, '/') | 1071 self.assertEquals(sorted([f.LocalPath().replace(presubmit.os.sep, '/') |
1046 for f in results]), | 1072 for f in results]), |
1047 sorted(item[1])) | 1073 sorted(item[1])) |
1048 | 1074 |
1049 def testCustomFilter(self): | 1075 def testCustomFilter(self): |
1050 def FilterSourceFile(affected_file): | 1076 def FilterSourceFile(affected_file): |
1051 return 'a' in affected_file.LocalPath() | 1077 return 'a' in affected_file.LocalPath() |
1052 files = [('A', 'eeaee'), ('M', 'eeabee'), ('M', 'eebcee')] | 1078 files = [('A', 'eeaee'), ('M', 'eeabee'), ('M', 'eebcee')] |
1053 for _, item in files: | 1079 for _, item in files: |
1054 item = presubmit.os.path.join(self.fake_root_dir, item) | 1080 full_item = presubmit.os.path.join(self.fake_root_dir, item) |
1055 presubmit.os.path.exists(item).AndReturn(True) | 1081 presubmit.os.path.exists(full_item).AndReturn(True) |
1056 presubmit.os.path.isdir(item).AndReturn(False) | 1082 presubmit.os.path.isdir(full_item).AndReturn(False) |
1057 presubmit.scm.SVN.GetFileProperty(item, 'svn:mime-type').AndReturn(None) | 1083 presubmit.scm.SVN.GetFileProperty( |
| 1084 item, 'svn:mime-type', self.fake_root_dir).AndReturn(None) |
1058 self.mox.ReplayAll() | 1085 self.mox.ReplayAll() |
1059 | 1086 |
1060 change = presubmit.SvnChange( | 1087 change = presubmit.SvnChange( |
1061 'mychange', '', self.fake_root_dir, files, 0, 0, None) | 1088 'mychange', '', self.fake_root_dir, files, 0, 0, None) |
1062 input_api = presubmit.InputApi( | 1089 input_api = presubmit.InputApi( |
1063 change, | 1090 change, |
1064 presubmit.os.path.join(self.fake_root_dir, 'PRESUBMIT.py'), | 1091 presubmit.os.path.join(self.fake_root_dir, 'PRESUBMIT.py'), |
1065 False, None, False) | 1092 False, None, False) |
1066 got_files = input_api.AffectedSourceFiles(FilterSourceFile) | 1093 got_files = input_api.AffectedSourceFiles(FilterSourceFile) |
1067 self.assertEquals(len(got_files), 2) | 1094 self.assertEquals(len(got_files), 2) |
1068 self.assertEquals(got_files[0].LocalPath(), 'eeaee') | 1095 self.assertEquals(got_files[0].LocalPath(), 'eeaee') |
1069 self.assertEquals(got_files[1].LocalPath(), 'eeabee') | 1096 self.assertEquals(got_files[1].LocalPath(), 'eeabee') |
1070 | 1097 |
1071 def testLambdaFilter(self): | 1098 def testLambdaFilter(self): |
1072 white_list = presubmit.InputApi.DEFAULT_BLACK_LIST + (r".*?a.*?",) | 1099 white_list = presubmit.InputApi.DEFAULT_BLACK_LIST + (r".*?a.*?",) |
1073 black_list = [r".*?b.*?"] | 1100 black_list = [r".*?b.*?"] |
1074 files = [('A', 'eeaee'), ('M', 'eeabee'), ('M', 'eebcee'), ('M', 'eecaee')] | 1101 files = [('A', 'eeaee'), ('M', 'eeabee'), ('M', 'eebcee'), ('M', 'eecaee')] |
1075 for _, item in files: | 1102 for _, item in files: |
1076 item = presubmit.os.path.join(self.fake_root_dir, item) | 1103 full_item = presubmit.os.path.join(self.fake_root_dir, item) |
1077 presubmit.os.path.exists(item).AndReturn(True) | 1104 presubmit.os.path.exists(full_item).AndReturn(True) |
1078 presubmit.os.path.isdir(item).AndReturn(False) | 1105 presubmit.os.path.isdir(full_item).AndReturn(False) |
1079 presubmit.scm.SVN.GetFileProperty(item, 'svn:mime-type').AndReturn(None) | 1106 presubmit.scm.SVN.GetFileProperty( |
| 1107 item, 'svn:mime-type', self.fake_root_dir).AndReturn(None) |
1080 self.mox.ReplayAll() | 1108 self.mox.ReplayAll() |
1081 | 1109 |
1082 change = presubmit.SvnChange( | 1110 change = presubmit.SvnChange( |
1083 'mychange', '', self.fake_root_dir, files, 0, 0, None) | 1111 'mychange', '', self.fake_root_dir, files, 0, 0, None) |
1084 input_api = presubmit.InputApi( | 1112 input_api = presubmit.InputApi( |
1085 change, './PRESUBMIT.py', False, None, False) | 1113 change, './PRESUBMIT.py', False, None, False) |
1086 # Sample usage of overiding the default white and black lists. | 1114 # Sample usage of overiding the default white and black lists. |
1087 got_files = input_api.AffectedSourceFiles( | 1115 got_files = input_api.AffectedSourceFiles( |
1088 lambda x: input_api.FilterSourceFile(x, white_list, black_list)) | 1116 lambda x: input_api.FilterSourceFile(x, white_list, black_list)) |
1089 self.assertEquals(len(got_files), 2) | 1117 self.assertEquals(len(got_files), 2) |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1261 | 1289 |
1262 class AffectedFileUnittest(PresubmitTestsBase): | 1290 class AffectedFileUnittest(PresubmitTestsBase): |
1263 def testMembersChanged(self): | 1291 def testMembersChanged(self): |
1264 self.mox.ReplayAll() | 1292 self.mox.ReplayAll() |
1265 members = [ | 1293 members = [ |
1266 'AbsoluteLocalPath', 'Action', 'ChangedContents', 'GenerateScmDiff', | 1294 'AbsoluteLocalPath', 'Action', 'ChangedContents', 'GenerateScmDiff', |
1267 'IsDirectory', 'IsTextFile', 'LocalPath', 'NewContents', 'OldContents', | 1295 'IsDirectory', 'IsTextFile', 'LocalPath', 'NewContents', 'OldContents', |
1268 'OldFileTempPath', 'Property', 'ServerPath', | 1296 'OldFileTempPath', 'Property', 'ServerPath', |
1269 ] | 1297 ] |
1270 # If this test fails, you should add the relevant test. | 1298 # If this test fails, you should add the relevant test. |
1271 self.compareMembers(presubmit.AffectedFile('a', 'b'), members) | 1299 self.compareMembers( |
1272 self.compareMembers(presubmit.SvnAffectedFile('a', 'b'), members) | 1300 presubmit.AffectedFile('a', 'b', self.fake_root_dir), members) |
| 1301 self.compareMembers( |
| 1302 presubmit.SvnAffectedFile('a', 'b', self.fake_root_dir), members) |
1273 | 1303 |
1274 def testAffectedFile(self): | 1304 def testAffectedFile(self): |
1275 path = presubmit.os.path.join('foo', 'blat.cc') | 1305 path = presubmit.os.path.join('foo', 'blat.cc') |
1276 presubmit.os.path.exists(path).AndReturn(True) | 1306 f_path = presubmit.os.path.join(self.fake_root_dir, path) |
1277 presubmit.os.path.isdir(path).AndReturn(False) | 1307 presubmit.os.path.exists(f_path).AndReturn(True) |
1278 presubmit.gclient_utils.FileRead(path, 'rU').AndReturn('whatever\ncookie') | 1308 presubmit.os.path.isdir(f_path).AndReturn(False) |
1279 presubmit.scm.SVN.CaptureInfo(path).AndReturn( | 1309 presubmit.gclient_utils.FileRead(f_path, 'rU').AndReturn('whatever\ncookie') |
| 1310 presubmit.scm.SVN.CaptureInfo(path, self.fake_root_dir).AndReturn( |
1280 {'URL': 'svn:/foo/foo/blat.cc'}) | 1311 {'URL': 'svn:/foo/foo/blat.cc'}) |
1281 self.mox.ReplayAll() | 1312 self.mox.ReplayAll() |
1282 af = presubmit.SvnAffectedFile('foo/blat.cc', 'M') | 1313 af = presubmit.SvnAffectedFile('foo/blat.cc', 'M', self.fake_root_dir) |
1283 self.assertEquals('svn:/foo/foo/blat.cc', af.ServerPath()) | 1314 self.assertEquals('svn:/foo/foo/blat.cc', af.ServerPath()) |
1284 self.assertEquals(presubmit.normpath('foo/blat.cc'), af.LocalPath()) | 1315 self.assertEquals(presubmit.normpath('foo/blat.cc'), af.LocalPath()) |
1285 self.assertEquals('M', af.Action()) | 1316 self.assertEquals('M', af.Action()) |
1286 self.assertEquals(['whatever', 'cookie'], af.NewContents()) | 1317 self.assertEquals(['whatever', 'cookie'], af.NewContents()) |
1287 | 1318 |
1288 def testAffectedFileNotExists(self): | 1319 def testAffectedFileNotExists(self): |
1289 presubmit.os.path.exists('notfound.cc').AndReturn(False) | 1320 notfound = 'notfound.cc' |
1290 presubmit.gclient_utils.FileRead('notfound.cc', 'rU').AndRaise(IOError) | 1321 f_notfound = presubmit.os.path.join(self.fake_root_dir, notfound) |
| 1322 presubmit.os.path.exists(f_notfound).AndReturn(False) |
| 1323 presubmit.gclient_utils.FileRead(f_notfound, 'rU').AndRaise(IOError) |
1291 self.mox.ReplayAll() | 1324 self.mox.ReplayAll() |
1292 af = presubmit.AffectedFile('notfound.cc', 'A') | 1325 af = presubmit.AffectedFile(notfound, 'A', self.fake_root_dir) |
1293 self.assertEquals('', af.ServerPath()) | 1326 self.assertEquals('', af.ServerPath()) |
1294 self.assertEquals([], af.NewContents()) | 1327 self.assertEquals([], af.NewContents()) |
1295 | 1328 |
1296 def testProperty(self): | 1329 def testProperty(self): |
1297 presubmit.scm.SVN.GetFileProperty('foo.cc', 'svn:secret-property' | 1330 presubmit.scm.SVN.GetFileProperty( |
| 1331 'foo.cc', 'svn:secret-property', self.fake_root_dir |
1298 ).AndReturn('secret-property-value') | 1332 ).AndReturn('secret-property-value') |
1299 self.mox.ReplayAll() | 1333 self.mox.ReplayAll() |
1300 affected_file = presubmit.SvnAffectedFile('foo.cc', 'A') | 1334 affected_file = presubmit.SvnAffectedFile('foo.cc', 'A', self.fake_root_dir) |
1301 # Verify cache coherency. | 1335 # Verify cache coherency. |
1302 self.assertEquals('secret-property-value', | 1336 self.assertEquals('secret-property-value', |
1303 affected_file.Property('svn:secret-property')) | 1337 affected_file.Property('svn:secret-property')) |
1304 self.assertEquals('secret-property-value', | 1338 self.assertEquals('secret-property-value', |
1305 affected_file.Property('svn:secret-property')) | 1339 affected_file.Property('svn:secret-property')) |
1306 | 1340 |
1307 def testIsDirectoryNotExists(self): | 1341 def testIsDirectoryNotExists(self): |
1308 presubmit.os.path.exists('foo.cc').AndReturn(False) | 1342 filename = 'foo.cc' |
1309 presubmit.scm.SVN.CaptureInfo('foo.cc').AndReturn({}) | 1343 f_filename = presubmit.os.path.join(self.fake_root_dir, filename) |
| 1344 presubmit.os.path.exists(f_filename).AndReturn(False) |
| 1345 presubmit.scm.SVN.CaptureInfo(filename, self.fake_root_dir).AndReturn({}) |
1310 self.mox.ReplayAll() | 1346 self.mox.ReplayAll() |
1311 affected_file = presubmit.SvnAffectedFile('foo.cc', 'A') | 1347 affected_file = presubmit.SvnAffectedFile(filename, 'A', self.fake_root_dir) |
1312 # Verify cache coherency. | 1348 # Verify cache coherency. |
1313 self.failIf(affected_file.IsDirectory()) | 1349 self.failIf(affected_file.IsDirectory()) |
1314 self.failIf(affected_file.IsDirectory()) | 1350 self.failIf(affected_file.IsDirectory()) |
1315 | 1351 |
1316 def testIsDirectory(self): | 1352 def testIsDirectory(self): |
1317 presubmit.os.path.exists('foo.cc').AndReturn(True) | 1353 filename = 'foo.cc' |
1318 presubmit.os.path.isdir('foo.cc').AndReturn(True) | 1354 f_filename = presubmit.os.path.join(self.fake_root_dir, filename) |
| 1355 presubmit.os.path.exists(f_filename).AndReturn(True) |
| 1356 presubmit.os.path.isdir(f_filename).AndReturn(True) |
1319 self.mox.ReplayAll() | 1357 self.mox.ReplayAll() |
1320 affected_file = presubmit.SvnAffectedFile('foo.cc', 'A') | 1358 affected_file = presubmit.SvnAffectedFile(filename, 'A', self.fake_root_dir) |
1321 # Verify cache coherency. | 1359 # Verify cache coherency. |
1322 self.failUnless(affected_file.IsDirectory()) | 1360 self.failUnless(affected_file.IsDirectory()) |
1323 self.failUnless(affected_file.IsDirectory()) | 1361 self.failUnless(affected_file.IsDirectory()) |
1324 | 1362 |
1325 def testIsTextFile(self): | 1363 def testIsTextFile(self): |
1326 files = [presubmit.SvnAffectedFile('foo/blat.txt', 'M'), | 1364 files = [ |
1327 presubmit.SvnAffectedFile('foo/binary.blob', 'M'), | 1365 presubmit.SvnAffectedFile('foo/blat.txt', 'M', self.fake_root_dir), |
1328 presubmit.SvnAffectedFile('blat/flop.txt', 'D')] | 1366 presubmit.SvnAffectedFile('foo/binary.blob', 'M', self.fake_root_dir), |
| 1367 presubmit.SvnAffectedFile('blat/flop.txt', 'D', self.fake_root_dir) |
| 1368 ] |
1329 blat = presubmit.os.path.join('foo', 'blat.txt') | 1369 blat = presubmit.os.path.join('foo', 'blat.txt') |
1330 blob = presubmit.os.path.join('foo', 'binary.blob') | 1370 blob = presubmit.os.path.join('foo', 'binary.blob') |
1331 presubmit.os.path.exists(blat).AndReturn(True) | 1371 f_blat = presubmit.os.path.join(self.fake_root_dir, blat) |
1332 presubmit.os.path.isdir(blat).AndReturn(False) | 1372 f_blob = presubmit.os.path.join(self.fake_root_dir, blob) |
1333 presubmit.os.path.exists(blob).AndReturn(True) | 1373 presubmit.os.path.exists(f_blat).AndReturn(True) |
1334 presubmit.os.path.isdir(blob).AndReturn(False) | 1374 presubmit.os.path.isdir(f_blat).AndReturn(False) |
1335 presubmit.scm.SVN.GetFileProperty(blat, 'svn:mime-type').AndReturn(None) | 1375 presubmit.os.path.exists(f_blob).AndReturn(True) |
1336 presubmit.scm.SVN.GetFileProperty(blob, 'svn:mime-type' | 1376 presubmit.os.path.isdir(f_blob).AndReturn(False) |
| 1377 presubmit.scm.SVN.GetFileProperty(blat, 'svn:mime-type', self.fake_root_dir |
| 1378 ).AndReturn(None) |
| 1379 presubmit.scm.SVN.GetFileProperty(blob, 'svn:mime-type', self.fake_root_dir |
1337 ).AndReturn('application/octet-stream') | 1380 ).AndReturn('application/octet-stream') |
1338 self.mox.ReplayAll() | 1381 self.mox.ReplayAll() |
1339 | 1382 |
1340 output = filter(lambda x: x.IsTextFile(), files) | 1383 output = filter(lambda x: x.IsTextFile(), files) |
1341 self.assertEquals(1, len(output)) | 1384 self.assertEquals(1, len(output)) |
1342 self.assertEquals(files[0], output[0]) | 1385 self.assertEquals(files[0], output[0]) |
1343 | 1386 |
1344 | 1387 |
1345 class ChangeUnittest(PresubmitTestsBase): | 1388 class ChangeUnittest(PresubmitTestsBase): |
1346 def testMembersChanged(self): | 1389 def testMembersChanged(self): |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1523 results2 = check(input_api2, presubmit.OutputApi) | 1566 results2 = check(input_api2, presubmit.OutputApi) |
1524 self.assertEquals(len(results2), 1) | 1567 self.assertEquals(len(results2), 1) |
1525 self.assertEquals(results2[0].__class__, error_type) | 1568 self.assertEquals(results2[0].__class__, error_type) |
1526 | 1569 |
1527 def SvnPropertyTest(self, check, property_name, value1, value2, committing, | 1570 def SvnPropertyTest(self, check, property_name, value1, value2, committing, |
1528 error_type, use_source_file): | 1571 error_type, use_source_file): |
1529 change1 = presubmit.SvnChange( | 1572 change1 = presubmit.SvnChange( |
1530 'mychange', '', self.fake_root_dir, [], 0, 0, None) | 1573 'mychange', '', self.fake_root_dir, [], 0, 0, None) |
1531 input_api1 = self.MockInputApi(change1, committing) | 1574 input_api1 = self.MockInputApi(change1, committing) |
1532 files1 = [ | 1575 files1 = [ |
1533 presubmit.SvnAffectedFile('foo/bar.cc', 'A'), | 1576 presubmit.SvnAffectedFile('foo/bar.cc', 'A', self.fake_root_dir), |
1534 presubmit.SvnAffectedFile('foo.cc', 'M'), | 1577 presubmit.SvnAffectedFile('foo.cc', 'M', self.fake_root_dir), |
1535 ] | 1578 ] |
1536 if use_source_file: | 1579 if use_source_file: |
1537 input_api1.AffectedSourceFiles(None).AndReturn(files1) | 1580 input_api1.AffectedSourceFiles(None).AndReturn(files1) |
1538 else: | 1581 else: |
1539 input_api1.AffectedFiles(include_deleted=False).AndReturn(files1) | 1582 input_api1.AffectedFiles(include_deleted=False).AndReturn(files1) |
1540 presubmit.scm.SVN.GetFileProperty(presubmit.normpath('foo/bar.cc'), | 1583 presubmit.scm.SVN.GetFileProperty( |
1541 property_name).AndReturn(value1) | 1584 presubmit.normpath('foo/bar.cc'), property_name, self.fake_root_dir |
1542 presubmit.scm.SVN.GetFileProperty(presubmit.normpath('foo.cc'), | 1585 ).AndReturn(value1) |
1543 property_name).AndReturn(value1) | 1586 presubmit.scm.SVN.GetFileProperty( |
| 1587 presubmit.normpath('foo.cc'), property_name, self.fake_root_dir |
| 1588 ).AndReturn(value1) |
1544 change2 = presubmit.SvnChange( | 1589 change2 = presubmit.SvnChange( |
1545 'mychange', '', self.fake_root_dir, [], 0, 0, None) | 1590 'mychange', '', self.fake_root_dir, [], 0, 0, None) |
1546 input_api2 = self.MockInputApi(change2, committing) | 1591 input_api2 = self.MockInputApi(change2, committing) |
1547 files2 = [ | 1592 files2 = [ |
1548 presubmit.SvnAffectedFile('foo/bar.cc', 'A'), | 1593 presubmit.SvnAffectedFile('foo/bar.cc', 'A', self.fake_root_dir), |
1549 presubmit.SvnAffectedFile('foo.cc', 'M'), | 1594 presubmit.SvnAffectedFile('foo.cc', 'M', self.fake_root_dir), |
1550 ] | 1595 ] |
1551 if use_source_file: | 1596 if use_source_file: |
1552 input_api2.AffectedSourceFiles(None).AndReturn(files2) | 1597 input_api2.AffectedSourceFiles(None).AndReturn(files2) |
1553 else: | 1598 else: |
1554 input_api2.AffectedFiles(include_deleted=False).AndReturn(files2) | 1599 input_api2.AffectedFiles(include_deleted=False).AndReturn(files2) |
1555 | 1600 |
1556 presubmit.scm.SVN.GetFileProperty(presubmit.normpath('foo/bar.cc'), | 1601 presubmit.scm.SVN.GetFileProperty( |
1557 property_name).AndReturn(value2) | 1602 presubmit.normpath('foo/bar.cc'), property_name, self.fake_root_dir |
1558 presubmit.scm.SVN.GetFileProperty(presubmit.normpath('foo.cc'), | 1603 ).AndReturn(value2) |
1559 property_name).AndReturn(value2) | 1604 presubmit.scm.SVN.GetFileProperty( |
| 1605 presubmit.normpath('foo.cc'), property_name, self.fake_root_dir |
| 1606 ).AndReturn(value2) |
1560 self.mox.ReplayAll() | 1607 self.mox.ReplayAll() |
1561 | 1608 |
1562 results1 = check(input_api1, presubmit.OutputApi, None) | 1609 results1 = check(input_api1, presubmit.OutputApi, None) |
1563 self.assertEquals(results1, []) | 1610 self.assertEquals(results1, []) |
1564 results2 = check(input_api2, presubmit.OutputApi, None) | 1611 results2 = check(input_api2, presubmit.OutputApi, None) |
1565 self.assertEquals(len(results2), 1) | 1612 self.assertEquals(len(results2), 1) |
1566 self.assertEquals(results2[0].__class__, error_type) | 1613 self.assertEquals(results2[0].__class__, error_type) |
1567 | 1614 |
1568 def testCannedCheckChangeHasBugField(self): | 1615 def testCannedCheckChangeHasBugField(self): |
1569 self.DescriptionTest(presubmit_canned_checks.CheckChangeHasBugField, | 1616 self.DescriptionTest(presubmit_canned_checks.CheckChangeHasBugField, |
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1829 check = presubmit_canned_checks.CheckSvnModifiedDirectories | 1876 check = presubmit_canned_checks.CheckSvnModifiedDirectories |
1830 results = check(input_api, presubmit.OutputApi, None) | 1877 results = check(input_api, presubmit.OutputApi, None) |
1831 self.assertEquals(len(results), 1) | 1878 self.assertEquals(len(results), 1) |
1832 self.assertEquals(results[0].__class__, | 1879 self.assertEquals(results[0].__class__, |
1833 presubmit.OutputApi.PresubmitPromptWarning) | 1880 presubmit.OutputApi.PresubmitPromptWarning) |
1834 | 1881 |
1835 def testCheckSvnForCommonMimeTypes(self): | 1882 def testCheckSvnForCommonMimeTypes(self): |
1836 self.mox.StubOutWithMock(presubmit_canned_checks, 'CheckSvnProperty') | 1883 self.mox.StubOutWithMock(presubmit_canned_checks, 'CheckSvnProperty') |
1837 input_api = self.MockInputApi(None, False) | 1884 input_api = self.MockInputApi(None, False) |
1838 output_api = presubmit.OutputApi() | 1885 output_api = presubmit.OutputApi() |
1839 A = lambda x: presubmit.AffectedFile(x, 'M') | 1886 A = lambda x: presubmit.AffectedFile(x, 'M', self.fake_root_dir) |
1840 files = [ | 1887 files = [ |
1841 A('a.pdf'), A('b.bmp'), A('c.gif'), A('d.png'), A('e.jpg'), A('f.jpe'), | 1888 A('a.pdf'), A('b.bmp'), A('c.gif'), A('d.png'), A('e.jpg'), A('f.jpe'), |
1842 A('random'), A('g.jpeg'), A('h.ico'), | 1889 A('random'), A('g.jpeg'), A('h.ico'), |
1843 ] | 1890 ] |
1844 input_api.AffectedFiles(include_deletes=False).AndReturn(files) | 1891 input_api.AffectedFiles(include_deletes=False).AndReturn(files) |
1845 presubmit_canned_checks.CheckSvnProperty( | 1892 presubmit_canned_checks.CheckSvnProperty( |
1846 input_api, output_api, 'svn:mime-type', 'application/pdf', [files[0]] | 1893 input_api, output_api, 'svn:mime-type', 'application/pdf', [files[0]] |
1847 ).AndReturn([1]) | 1894 ).AndReturn([1]) |
1848 presubmit_canned_checks.CheckSvnProperty( | 1895 presubmit_canned_checks.CheckSvnProperty( |
1849 input_api, output_api, 'svn:mime-type', 'image/bmp', [files[1]] | 1896 input_api, output_api, 'svn:mime-type', 'image/bmp', [files[1]] |
(...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2233 self.assertEqual(1, len(results)) | 2280 self.assertEqual(1, len(results)) |
2234 # pylint: disable=W0212 | 2281 # pylint: disable=W0212 |
2235 self.assertEqual( | 2282 self.assertEqual( |
2236 'Found line ending with white spaces in:', results[0]._message) | 2283 'Found line ending with white spaces in:', results[0]._message) |
2237 self.checkstdout('') | 2284 self.checkstdout('') |
2238 | 2285 |
2239 | 2286 |
2240 if __name__ == '__main__': | 2287 if __name__ == '__main__': |
2241 import unittest | 2288 import unittest |
2242 unittest.main() | 2289 unittest.main() |
OLD | NEW |