Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Side by Side Diff: tests/presubmit_unittest.py

Issue 126068: Remove gcl.ChangeInfo dependency in presubmit_support.py. (Closed)
Patch Set: . Created 11 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « presubmit_support.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/python 1 #!/usr/bin/python
2 # Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. 2 # Copyright (c) 2006-2009 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 import exceptions 8 import exceptions
9 import StringIO 9 import StringIO
10 import unittest 10 import unittest
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
49 presubmit.os.path.splitext = os_path_splitext 49 presubmit.os.path.splitext = os_path_splitext
50 self.mox.StubOutWithMock(presubmit, 'sys') 50 self.mox.StubOutWithMock(presubmit, 'sys')
51 # Special mocks. 51 # Special mocks.
52 def MockAbsPath(f): 52 def MockAbsPath(f):
53 return f 53 return f
54 presubmit.os.path.abspath = MockAbsPath 54 presubmit.os.path.abspath = MockAbsPath
55 self.fake_root_dir = self.RootDir() 55 self.fake_root_dir = self.RootDir()
56 self.mox.StubOutWithMock(presubmit.gclient, 'CaptureSVNInfo') 56 self.mox.StubOutWithMock(presubmit.gclient, 'CaptureSVNInfo')
57 self.mox.StubOutWithMock(presubmit.gcl, 'GetSVNFileProperty') 57 self.mox.StubOutWithMock(presubmit.gcl, 'GetSVNFileProperty')
58 self.mox.StubOutWithMock(presubmit.gcl, 'ReadFile') 58 self.mox.StubOutWithMock(presubmit.gcl, 'ReadFile')
59 # Stub any non-getter function in gcl.ChangeInfo.
60 to_skip = lambda x: not x.startswith('_') and not x.startswith('Get')
61 for member in filter(to_skip, dir(presubmit.gcl.ChangeInfo)):
62 self.mox.StubOutWithMock(presubmit.gcl.ChangeInfo, member)
63
64 def MakeChangeInfo(self, name, issue, patchset, description, files):
65 ci = presubmit.gcl.ChangeInfo(name, issue, patchset, description, files,
66 self.fake_root_dir)
67 return ci
68 59
69 60
70 class PresubmitUnittest(PresubmitTestsBase): 61 class PresubmitUnittest(PresubmitTestsBase):
71 """General presubmit_support.py tests (excluding InputApi and OutputApi).""" 62 """General presubmit_support.py tests (excluding InputApi and OutputApi)."""
72 def testMembersChanged(self): 63 def testMembersChanged(self):
73 self.mox.ReplayAll() 64 self.mox.ReplayAll()
74 members = [ 65 members = [
75 'AffectedFile', 'DoPresubmitChecks', 'GclChange', 'InputApi', 66 'AffectedFile', 'Change', 'DoPresubmitChecks', 'InputApi',
76 'ListRelevantPresubmitFiles', 'Main', 'NotImplementedException', 67 'ListRelevantPresubmitFiles', 'Main', 'NotImplementedException',
77 'OutputApi', 'ParseFiles', 'PresubmitExecuter', 'ScanSubDirs', 68 'OutputApi', 'ParseFiles', 'PresubmitExecuter', 'ScanSubDirs',
78 'SvnAffectedFile', 69 'SvnAffectedFile', 'SvnChange',
79 'cPickle', 'cStringIO', 'exceptions', 70 'cPickle', 'cStringIO', 'exceptions',
80 'fnmatch', 'gcl', 'gclient', 'glob', 'logging', 'marshal', 'normpath', 71 'fnmatch', 'gcl', 'gclient', 'glob', 'logging', 'marshal', 'normpath',
81 'optparse', 72 'optparse',
82 'os', 'pickle', 'presubmit_canned_checks', 're', 'subprocess', 'sys', 73 'os', 'pickle', 'presubmit_canned_checks', 're', 'subprocess', 'sys',
83 'tempfile', 'traceback', 'types', 'unittest', 'urllib2', 'warnings', 74 'tempfile', 'traceback', 'types', 'unittest', 'urllib2', 'warnings',
84 ] 75 ]
85 # If this test fails, you should add the relevant test. 76 # If this test fails, you should add the relevant test.
86 self.compareMembers(presubmit, members) 77 self.compareMembers(presubmit, members)
87 78
88 def testListRelevantPresubmitFiles(self): 79 def testListRelevantPresubmitFiles(self):
(...skipping 11 matching lines...) Expand all
100 presubmit.os.path.isfile(join(self.fake_root_dir, 'foo', 'haspresubmit', 91 presubmit.os.path.isfile(join(self.fake_root_dir, 'foo', 'haspresubmit',
101 'PRESUBMIT.py')).AndReturn(True) 92 'PRESUBMIT.py')).AndReturn(True)
102 presubmit.os.path.isfile(join(self.fake_root_dir, 'foo', 'haspresubmit', 93 presubmit.os.path.isfile(join(self.fake_root_dir, 'foo', 'haspresubmit',
103 'yodle', 'PRESUBMIT.py')).AndReturn(True) 94 'yodle', 'PRESUBMIT.py')).AndReturn(True)
104 presubmit.os.path.isfile(join(self.fake_root_dir, 'moo', 95 presubmit.os.path.isfile(join(self.fake_root_dir, 'moo',
105 'PRESUBMIT.py')).AndReturn(False) 96 'PRESUBMIT.py')).AndReturn(False)
106 presubmit.os.path.isfile(join(self.fake_root_dir, 'moo', 'mat', 97 presubmit.os.path.isfile(join(self.fake_root_dir, 'moo', 'mat',
107 'PRESUBMIT.py')).AndReturn(False) 98 'PRESUBMIT.py')).AndReturn(False)
108 presubmit.os.path.isfile(join(self.fake_root_dir, 'moo', 'mat', 'gat', 99 presubmit.os.path.isfile(join(self.fake_root_dir, 'moo', 'mat', 'gat',
109 'PRESUBMIT.py')).AndReturn(False) 100 'PRESUBMIT.py')).AndReturn(False)
110 #isfile(join('moo', 'PRESUBMIT.py')).AndReturn(False)
111 self.mox.ReplayAll() 101 self.mox.ReplayAll()
112 102
113 presubmit_files = presubmit.ListRelevantPresubmitFiles(files, 103 presubmit_files = presubmit.ListRelevantPresubmitFiles(files,
114 self.fake_root_dir) 104 self.fake_root_dir)
115 self.assertEqual(presubmit_files, 105 self.assertEqual(presubmit_files,
116 [ 106 [
117 join(self.fake_root_dir, 'PRESUBMIT.py'), 107 join(self.fake_root_dir, 'PRESUBMIT.py'),
118 join(self.fake_root_dir, 'foo', 'haspresubmit', 'PRESUBMIT.py'), 108 join(self.fake_root_dir, 'foo', 'haspresubmit', 'PRESUBMIT.py'),
119 join(self.fake_root_dir, 'foo', 'haspresubmit', 'yodle', 109 join(self.fake_root_dir, 'foo', 'haspresubmit', 'yodle',
120 'PRESUBMIT.py') 110 'PRESUBMIT.py')
121 ]) 111 ])
122 112
123 def testTagLineRe(self): 113 def testTagLineRe(self):
124 self.mox.ReplayAll() 114 self.mox.ReplayAll()
125 m = presubmit.GclChange._tag_line_re.match(' BUG =1223, 1445 \t') 115 m = presubmit.Change._TAG_LINE_RE.match(' BUG =1223, 1445 \t')
126 self.failUnless(m) 116 self.failUnless(m)
127 self.failUnlessEqual(m.group('key'), 'BUG') 117 self.failUnlessEqual(m.group('key'), 'BUG')
128 self.failUnlessEqual(m.group('value'), '1223, 1445') 118 self.failUnlessEqual(m.group('value'), '1223, 1445')
129 119
130 def testGclChange(self): 120 def testGclChange(self):
131 description_lines = ('Hello there', 121 description_lines = ('Hello there',
132 'this is a change', 122 'this is a change',
133 'BUG=123', 123 'BUG=123',
134 ' STORY =http://foo/ \t', 124 ' STORY =http://foo/ \t',
135 'and some more regular text \t') 125 'and some more regular text \t')
(...skipping 27 matching lines...) Expand all
163 notfound, 'svn:mime-type').AndReturn('') 153 notfound, 'svn:mime-type').AndReturn('')
164 presubmit.gclient.CaptureSVNInfo(blat).AndReturn( 154 presubmit.gclient.CaptureSVNInfo(blat).AndReturn(
165 {'URL': 'svn:/foo/foo/blat.cc'}) 155 {'URL': 'svn:/foo/foo/blat.cc'})
166 presubmit.gclient.CaptureSVNInfo(binary).AndReturn( 156 presubmit.gclient.CaptureSVNInfo(binary).AndReturn(
167 {'URL': 'svn:/foo/binary.dll'}) 157 {'URL': 'svn:/foo/binary.dll'})
168 presubmit.gclient.CaptureSVNInfo(notfound).AndReturn({}) 158 presubmit.gclient.CaptureSVNInfo(notfound).AndReturn({})
169 presubmit.gclient.CaptureSVNInfo(flap).AndReturn( 159 presubmit.gclient.CaptureSVNInfo(flap).AndReturn(
170 {'URL': 'svn:/foo/boo/flap.h'}) 160 {'URL': 'svn:/foo/boo/flap.h'})
171 presubmit.gcl.ReadFile(blat).AndReturn('boo!\nahh?') 161 presubmit.gcl.ReadFile(blat).AndReturn('boo!\nahh?')
172 presubmit.gcl.ReadFile(notfound).AndReturn('look!\nthere?') 162 presubmit.gcl.ReadFile(notfound).AndReturn('look!\nthere?')
173 ci = self.MakeChangeInfo('mychange', 0, 0, '\n'.join(description_lines),
174 files)
175 self.mox.ReplayAll() 163 self.mox.ReplayAll()
176 164
177 change = presubmit.GclChange(ci) 165 change = presubmit.SvnChange('mychange', '\n'.join(description_lines),
178 166 self.fake_root_dir, files, 0, 0)
179 self.failUnless(change.Name() == 'mychange') 167 self.failUnless(change.Name() == 'mychange')
180 self.failUnless(change.DescriptionText() == 168 self.failUnless(change.DescriptionText() ==
181 'Hello there\nthis is a change\nand some more regular text') 169 'Hello there\nthis is a change\nand some more regular text')
182 self.failUnless(change.FullDescriptionText() == 170 self.failUnless(change.FullDescriptionText() ==
183 '\n'.join(description_lines)) 171 '\n'.join(description_lines))
184 172
185 self.failUnless(change.BUG == '123') 173 self.failUnless(change.BUG == '123')
186 self.failUnless(change.STORY == 'http://foo/') 174 self.failUnless(change.STORY == 'http://foo/')
175 self.failUnless(change.BLEH == None)
187 176
188 self.failUnless(len(change.AffectedFiles()) == 4) 177 self.failUnless(len(change.AffectedFiles()) == 4)
189 self.failUnless(len(change.AffectedFiles(include_dirs=True)) == 5) 178 self.failUnless(len(change.AffectedFiles(include_dirs=True)) == 5)
190 self.failUnless(len(change.AffectedFiles(include_deletes=False)) == 3) 179 self.failUnless(len(change.AffectedFiles(include_deletes=False)) == 3)
191 self.failUnless(len(change.AffectedFiles(include_dirs=True, 180 self.failUnless(len(change.AffectedFiles(include_dirs=True,
192 include_deletes=False)) == 4) 181 include_deletes=False)) == 4)
193 182
194 affected_text_files = change.AffectedTextFiles() 183 affected_text_files = change.AffectedTextFiles()
195 self.failUnless(len(affected_text_files) == 2) 184 self.failUnless(len(affected_text_files) == 2)
196 self.failIf(filter(lambda x: x.LocalPath() == 'binary.dll', 185 self.failIf(filter(lambda x: x.LocalPath() == 'binary.dll',
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
230 self.assertEquals(rhs_lines[3][2], 'there?') 219 self.assertEquals(rhs_lines[3][2], 'there?')
231 220
232 def testExecPresubmitScript(self): 221 def testExecPresubmitScript(self):
233 description_lines = ('Hello there', 222 description_lines = ('Hello there',
234 'this is a change', 223 'this is a change',
235 'STORY=http://tracker/123') 224 'STORY=http://tracker/123')
236 files = [ 225 files = [
237 ['A', 'foo\\blat.cc'], 226 ['A', 'foo\\blat.cc'],
238 ] 227 ]
239 fake_presubmit = presubmit.os.path.join(self.fake_root_dir, 'PRESUBMIT.py') 228 fake_presubmit = presubmit.os.path.join(self.fake_root_dir, 'PRESUBMIT.py')
240 ci = self.MakeChangeInfo('mychange', 0, 0, '\n'.join(description_lines),
241 files)
242 self.mox.ReplayAll() 229 self.mox.ReplayAll()
243 230
244 executer = presubmit.PresubmitExecuter(ci, False) 231 change = presubmit.Change('mychange', '\n'.join(description_lines),
232 self.fake_root_dir, files, 0, 0)
233 executer = presubmit.PresubmitExecuter(change, False)
245 self.failIf(executer.ExecPresubmitScript('', fake_presubmit)) 234 self.failIf(executer.ExecPresubmitScript('', fake_presubmit))
246 # No error if no on-upload entry point 235 # No error if no on-upload entry point
247 self.failIf(executer.ExecPresubmitScript( 236 self.failIf(executer.ExecPresubmitScript(
248 ('def CheckChangeOnCommit(input_api, output_api):\n' 237 ('def CheckChangeOnCommit(input_api, output_api):\n'
249 ' return (output_api.PresubmitError("!!"))\n'), 238 ' return (output_api.PresubmitError("!!"))\n'),
250 fake_presubmit 239 fake_presubmit
251 )) 240 ))
252 241
253 executer = presubmit.PresubmitExecuter(ci, True) 242 executer = presubmit.PresubmitExecuter(change, True)
254 # No error if no on-commit entry point 243 # No error if no on-commit entry point
255 self.failIf(executer.ExecPresubmitScript( 244 self.failIf(executer.ExecPresubmitScript(
256 ('def CheckChangeOnUpload(input_api, output_api):\n' 245 ('def CheckChangeOnUpload(input_api, output_api):\n'
257 ' return (output_api.PresubmitError("!!"))\n'), 246 ' return (output_api.PresubmitError("!!"))\n'),
258 fake_presubmit 247 fake_presubmit
259 )) 248 ))
260 249
261 self.failIf(executer.ExecPresubmitScript( 250 self.failIf(executer.ExecPresubmitScript(
262 ('def CheckChangeOnUpload(input_api, output_api):\n' 251 ('def CheckChangeOnUpload(input_api, output_api):\n'
263 ' if not input_api.change.STORY:\n' 252 ' if not input_api.change.STORY:\n'
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 ['A', join('haspresubmit', 'blat.cc')], 286 ['A', join('haspresubmit', 'blat.cc')],
298 ] 287 ]
299 haspresubmit_path = join(self.fake_root_dir, 'haspresubmit', 'PRESUBMIT.py') 288 haspresubmit_path = join(self.fake_root_dir, 'haspresubmit', 'PRESUBMIT.py')
300 root_path = join(self.fake_root_dir, 'PRESUBMIT.py') 289 root_path = join(self.fake_root_dir, 'PRESUBMIT.py')
301 presubmit.os.path.isfile(root_path).AndReturn(True) 290 presubmit.os.path.isfile(root_path).AndReturn(True)
302 presubmit.os.path.isfile(haspresubmit_path).AndReturn(True) 291 presubmit.os.path.isfile(haspresubmit_path).AndReturn(True)
303 presubmit.gcl.ReadFile(root_path, 292 presubmit.gcl.ReadFile(root_path,
304 'rU').AndReturn(self.presubmit_text) 293 'rU').AndReturn(self.presubmit_text)
305 presubmit.gcl.ReadFile(haspresubmit_path, 294 presubmit.gcl.ReadFile(haspresubmit_path,
306 'rU').AndReturn(self.presubmit_text) 295 'rU').AndReturn(self.presubmit_text)
307 ci = self.MakeChangeInfo('mychange', 0, 0, '\n'.join(description_lines),
308 files)
309 self.mox.ReplayAll() 296 self.mox.ReplayAll()
310 297
311 output = StringIO.StringIO() 298 output = StringIO.StringIO()
312 input = StringIO.StringIO('y\n') 299 input = StringIO.StringIO('y\n')
313 300 change = presubmit.Change('mychange', '\n'.join(description_lines),
314 self.failIf(presubmit.DoPresubmitChecks(ci, False, True, output, input, 301 self.fake_root_dir, files, 0, 0)
302 self.failIf(presubmit.DoPresubmitChecks(change, False, True, output, input,
315 None, False)) 303 None, False))
316 self.assertEqual(output.getvalue().count('!!'), 2) 304 self.assertEqual(output.getvalue().count('!!'), 2)
317 305
318 def testDoPresubmitChecksPromptsAfterWarnings(self): 306 def testDoPresubmitChecksPromptsAfterWarnings(self):
319 join = presubmit.os.path.join 307 join = presubmit.os.path.join
320 description_lines = ('Hello there', 308 description_lines = ('Hello there',
321 'this is a change', 309 'this is a change',
322 'NOSUCHKEY=http://tracker/123') 310 'NOSUCHKEY=http://tracker/123')
323 files = [ 311 files = [
324 ['A', join('haspresubmit', 'blat.cc')], 312 ['A', join('haspresubmit', 'blat.cc')],
325 ] 313 ]
326 presubmit_path = join(self.fake_root_dir, 'PRESUBMIT.py') 314 presubmit_path = join(self.fake_root_dir, 'PRESUBMIT.py')
327 haspresubmit_path = join(self.fake_root_dir, 'haspresubmit', 'PRESUBMIT.py') 315 haspresubmit_path = join(self.fake_root_dir, 'haspresubmit', 'PRESUBMIT.py')
328 ci = self.MakeChangeInfo('mychange', 0, 0, '\n'.join(description_lines),
329 files)
330 for i in range(2): 316 for i in range(2):
331 presubmit.os.path.isfile(presubmit_path).AndReturn(True) 317 presubmit.os.path.isfile(presubmit_path).AndReturn(True)
332 presubmit.os.path.isfile(haspresubmit_path).AndReturn(True) 318 presubmit.os.path.isfile(haspresubmit_path).AndReturn(True)
333 presubmit.gcl.ReadFile(presubmit_path, 'rU' 319 presubmit.gcl.ReadFile(presubmit_path, 'rU'
334 ).AndReturn(self.presubmit_text) 320 ).AndReturn(self.presubmit_text)
335 presubmit.gcl.ReadFile(haspresubmit_path, 'rU' 321 presubmit.gcl.ReadFile(haspresubmit_path, 'rU'
336 ).AndReturn(self.presubmit_text) 322 ).AndReturn(self.presubmit_text)
337 self.mox.ReplayAll() 323 self.mox.ReplayAll()
338 324
339 output = StringIO.StringIO() 325 output = StringIO.StringIO()
340 input = StringIO.StringIO('n\n') # say no to the warning 326 input = StringIO.StringIO('n\n') # say no to the warning
341 self.failIf(presubmit.DoPresubmitChecks(ci, False, True, output, input, 327 change = presubmit.Change('mychange', '\n'.join(description_lines),
328 self.fake_root_dir, files, 0, 0)
329 self.failIf(presubmit.DoPresubmitChecks(change, False, True, output, input,
342 None, True)) 330 None, True))
343 self.assertEqual(output.getvalue().count('??'), 2) 331 self.assertEqual(output.getvalue().count('??'), 2)
344 332
345 output = StringIO.StringIO() 333 output = StringIO.StringIO()
346 input = StringIO.StringIO('y\n') # say yes to the warning 334 input = StringIO.StringIO('y\n') # say yes to the warning
347 self.failUnless(presubmit.DoPresubmitChecks(ci, False, True, output, input, 335 self.failUnless(presubmit.DoPresubmitChecks(change, False, True, output,
348 None, True)) 336 input, None, True))
349 self.assertEquals(output.getvalue().count('??'), 2) 337 self.assertEquals(output.getvalue().count('??'), 2)
350 338
351 def testDoPresubmitChecksNoWarningPromptIfErrors(self): 339 def testDoPresubmitChecksNoWarningPromptIfErrors(self):
352 join = presubmit.os.path.join 340 join = presubmit.os.path.join
353 description_lines = ('Hello there', 341 description_lines = ('Hello there',
354 'this is a change', 342 'this is a change',
355 'NOSUCHKEY=http://tracker/123', 343 'NOSUCHKEY=http://tracker/123',
356 'REALLYNOSUCHKEY=http://tracker/123') 344 'REALLYNOSUCHKEY=http://tracker/123')
357 files = [ 345 files = [
358 ['A', join('haspresubmit', 'blat.cc')], 346 ['A', join('haspresubmit', 'blat.cc')],
359 ] 347 ]
360 presubmit_path = join(self.fake_root_dir, 'PRESUBMIT.py') 348 presubmit_path = join(self.fake_root_dir, 'PRESUBMIT.py')
361 haspresubmit_path = join(self.fake_root_dir, 'haspresubmit', 349 haspresubmit_path = join(self.fake_root_dir, 'haspresubmit',
362 'PRESUBMIT.py') 350 'PRESUBMIT.py')
363 presubmit.os.path.isfile(presubmit_path).AndReturn(True) 351 presubmit.os.path.isfile(presubmit_path).AndReturn(True)
364 presubmit.os.path.isfile(haspresubmit_path).AndReturn(True) 352 presubmit.os.path.isfile(haspresubmit_path).AndReturn(True)
365 presubmit.gcl.ReadFile(presubmit_path, 'rU').AndReturn(self.presubmit_text) 353 presubmit.gcl.ReadFile(presubmit_path, 'rU').AndReturn(self.presubmit_text)
366 presubmit.gcl.ReadFile(haspresubmit_path, 'rU').AndReturn( 354 presubmit.gcl.ReadFile(haspresubmit_path, 'rU').AndReturn(
367 self.presubmit_text) 355 self.presubmit_text)
368 ci = self.MakeChangeInfo('mychange', 0, 0, '\n'.join(description_lines),
369 files)
370 self.mox.ReplayAll() 356 self.mox.ReplayAll()
371 357
372 output = StringIO.StringIO() 358 output = StringIO.StringIO()
373 input = StringIO.StringIO() # should be unused 359 input = StringIO.StringIO() # should be unused
374 self.failIf(presubmit.DoPresubmitChecks(ci, False, True, output, input, 360 change = presubmit.Change('mychange', '\n'.join(description_lines),
361 self.fake_root_dir, files, 0, 0)
362 self.failIf(presubmit.DoPresubmitChecks(change, False, True, output, input,
375 None, False)) 363 None, False))
376 self.assertEqual(output.getvalue().count('??'), 2) 364 self.assertEqual(output.getvalue().count('??'), 2)
377 self.assertEqual(output.getvalue().count('XX!!XX'), 2) 365 self.assertEqual(output.getvalue().count('XX!!XX'), 2)
378 self.assertEqual(output.getvalue().count('(y/N)'), 0) 366 self.assertEqual(output.getvalue().count('(y/N)'), 0)
379 367
380 def testDoDefaultPresubmitChecks(self): 368 def testDoDefaultPresubmitChecks(self):
381 join = presubmit.os.path.join 369 join = presubmit.os.path.join
382 description_lines = ('Hello there', 370 description_lines = ('Hello there',
383 'this is a change', 371 'this is a change',
384 'STORY=http://tracker/123') 372 'STORY=http://tracker/123')
385 files = [ 373 files = [
386 ['A', join('haspresubmit', 'blat.cc')], 374 ['A', join('haspresubmit', 'blat.cc')],
387 ] 375 ]
388 DEFAULT_SCRIPT = """ 376 DEFAULT_SCRIPT = """
389 def CheckChangeOnUpload(input_api, output_api): 377 def CheckChangeOnUpload(input_api, output_api):
390 return [output_api.PresubmitError("!!")] 378 return [output_api.PresubmitError("!!")]
391 def CheckChangeOnCommit(input_api, output_api): 379 def CheckChangeOnCommit(input_api, output_api):
392 raise Exception("Test error") 380 raise Exception("Test error")
393 """ 381 """
394 presubmit.os.path.isfile(join(self.fake_root_dir, 'PRESUBMIT.py') 382 presubmit.os.path.isfile(join(self.fake_root_dir, 'PRESUBMIT.py')
395 ).AndReturn(False) 383 ).AndReturn(False)
396 presubmit.os.path.isfile(join(self.fake_root_dir, 384 presubmit.os.path.isfile(join(self.fake_root_dir,
397 'haspresubmit', 385 'haspresubmit',
398 'PRESUBMIT.py')).AndReturn(False) 386 'PRESUBMIT.py')).AndReturn(False)
399 ci = self.MakeChangeInfo('mychange', 0, 0, '\n'.join(description_lines),
400 files)
401 self.mox.ReplayAll() 387 self.mox.ReplayAll()
402 388
403 output = StringIO.StringIO() 389 output = StringIO.StringIO()
404 input = StringIO.StringIO('y\n') 390 input = StringIO.StringIO('y\n')
405 # Always fail. 391 # Always fail.
406 self.failIf(presubmit.DoPresubmitChecks(ci, False, True, output, input, 392 change = presubmit.Change('mychange', '\n'.join(description_lines),
393 self.fake_root_dir, files, 0, 0)
394 self.failIf(presubmit.DoPresubmitChecks(change, False, True, output, input,
407 DEFAULT_SCRIPT, False)) 395 DEFAULT_SCRIPT, False))
408 self.assertEquals(output.getvalue().count('!!'), 1) 396 self.assertEquals(output.getvalue().count('!!'), 1)
409 397
410 def testDirectoryHandling(self): 398 def testDirectoryHandling(self):
411 files = [ 399 files = [
412 ['A', 'isdir'], 400 ['A', 'isdir'],
413 ['A', 'isdir\\blat.cc'], 401 ['A', 'isdir\\blat.cc'],
414 ] 402 ]
415 isdir = presubmit.os.path.join(self.fake_root_dir, 'isdir') 403 isdir = presubmit.os.path.join(self.fake_root_dir, 'isdir')
416 blat = presubmit.os.path.join(isdir, 'blat.cc') 404 blat = presubmit.os.path.join(isdir, 'blat.cc')
417 presubmit.os.path.exists(isdir).AndReturn(True) 405 presubmit.os.path.exists(isdir).AndReturn(True)
418 presubmit.os.path.isdir(isdir).AndReturn(True) 406 presubmit.os.path.isdir(isdir).AndReturn(True)
419 presubmit.os.path.exists(blat).AndReturn(True) 407 presubmit.os.path.exists(blat).AndReturn(True)
420 presubmit.os.path.isdir(blat).AndReturn(False) 408 presubmit.os.path.isdir(blat).AndReturn(False)
421 ci = self.MakeChangeInfo('mychange', 0, 0, 'foo', files)
422 self.mox.ReplayAll() 409 self.mox.ReplayAll()
423 410
424 change = presubmit.GclChange(ci) 411 change = presubmit.Change('mychange', 'foo', self.fake_root_dir, files,
412 0, 0)
425 affected_files = change.AffectedFiles(include_dirs=False) 413 affected_files = change.AffectedFiles(include_dirs=False)
426 self.failUnless(len(affected_files) == 1) 414 self.failUnless(len(affected_files) == 1)
427 self.failUnless(affected_files[0].LocalPath().endswith('blat.cc')) 415 self.failUnless(affected_files[0].LocalPath().endswith('blat.cc'))
428 affected_files_and_dirs = change.AffectedFiles(include_dirs=True) 416 affected_files_and_dirs = change.AffectedFiles(include_dirs=True)
429 self.failUnless(len(affected_files_and_dirs) == 2) 417 self.failUnless(len(affected_files_and_dirs) == 2)
430 418
431 def testTags(self): 419 def testTags(self):
432 DEFAULT_SCRIPT = """ 420 DEFAULT_SCRIPT = """
433 def CheckChangeOnUpload(input_api, output_api): 421 def CheckChangeOnUpload(input_api, output_api):
434 if input_api.change.tags['BUG'] != 'boo': 422 if input_api.change.tags['BUG'] != 'boo':
(...skipping 17 matching lines...) Expand all
452 return [output_api.PresubmitError('Tag parsing failed. 4 ' + 440 return [output_api.PresubmitError('Tag parsing failed. 4 ' +
453 input_api.change.DescriptionText())] 441 input_api.change.DescriptionText())]
454 if (input_api.change.FullDescriptionText() != 442 if (input_api.change.FullDescriptionText() !=
455 'Blah Blah\\n\\nSTORY=http://tracker.com/42\\nBUG=boo\\n'): 443 'Blah Blah\\n\\nSTORY=http://tracker.com/42\\nBUG=boo\\n'):
456 return [output_api.PresubmitError('Tag parsing failed. 5 ' + 444 return [output_api.PresubmitError('Tag parsing failed. 5 ' +
457 input_api.change.FullDescriptionText())] 445 input_api.change.FullDescriptionText())]
458 return [output_api.PresubmitNotifyResult(input_api.change.tags['STORY'])] 446 return [output_api.PresubmitNotifyResult(input_api.change.tags['STORY'])]
459 def CheckChangeOnCommit(input_api, output_api): 447 def CheckChangeOnCommit(input_api, output_api):
460 raise Exception("Test error") 448 raise Exception("Test error")
461 """ 449 """
462 ci = self.MakeChangeInfo(
463 'foo', 0, 0, "Blah Blah\n\nSTORY=http://tracker.com/42\nBUG=boo\n",
464 None)
465 self.mox.ReplayAll() 450 self.mox.ReplayAll()
466 451
467 output = StringIO.StringIO() 452 output = StringIO.StringIO()
468 input = StringIO.StringIO('y\n') 453 input = StringIO.StringIO('y\n')
469 self.failUnless(presubmit.DoPresubmitChecks(ci, False, True, output, 454 change = presubmit.Change(
455 'foo', "Blah Blah\n\nSTORY=http://tracker.com/42\nBUG=boo\n",
456 self.fake_root_dir, None, 0, 0)
457 self.failUnless(presubmit.DoPresubmitChecks(change, False, True, output,
470 input, DEFAULT_SCRIPT, False)) 458 input, DEFAULT_SCRIPT, False))
471 self.assertEquals(output.getvalue(), 459 self.assertEquals(output.getvalue(),
472 ('Warning, no presubmit.py found.\n' 460 ('Warning, no presubmit.py found.\n'
473 'Running default presubmit script.\n' 461 'Running default presubmit script.\n'
474 '** Presubmit Messages **\n' 462 '** Presubmit Messages **\n'
475 'http://tracker.com/42\n\n')) 463 'http://tracker.com/42\n\n'))
476 464
477 465
478 class InputApiUnittest(PresubmitTestsBase): 466 class InputApiUnittest(PresubmitTestsBase):
479 """Tests presubmit.InputApi.""" 467 """Tests presubmit.InputApi."""
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
565 presubmit.gcl.GetSVNFileProperty(blat, 'svn:mime-type').AndReturn(None) 553 presubmit.gcl.GetSVNFileProperty(blat, 'svn:mime-type').AndReturn(None)
566 presubmit.gcl.GetSVNFileProperty(readme, 'svn:mime-type').AndReturn(None) 554 presubmit.gcl.GetSVNFileProperty(readme, 'svn:mime-type').AndReturn(None)
567 presubmit.gcl.GetSVNFileProperty(binary, 'svn:mime-type').AndReturn( 555 presubmit.gcl.GetSVNFileProperty(binary, 'svn:mime-type').AndReturn(
568 'application/octet-stream') 556 'application/octet-stream')
569 presubmit.gcl.GetSVNFileProperty(weird, 'svn:mime-type').AndReturn(None) 557 presubmit.gcl.GetSVNFileProperty(weird, 'svn:mime-type').AndReturn(None)
570 presubmit.gcl.GetSVNFileProperty(another, 'svn:mime-type').AndReturn(None) 558 presubmit.gcl.GetSVNFileProperty(another, 'svn:mime-type').AndReturn(None)
571 presubmit.gcl.GetSVNFileProperty(third_party, 'svn:mime-type' 559 presubmit.gcl.GetSVNFileProperty(third_party, 'svn:mime-type'
572 ).AndReturn(None) 560 ).AndReturn(None)
573 presubmit.gcl.ReadFile(blat).AndReturn('whatever\ncookie') 561 presubmit.gcl.ReadFile(blat).AndReturn('whatever\ncookie')
574 presubmit.gcl.ReadFile(another).AndReturn('whatever\ncookie2') 562 presubmit.gcl.ReadFile(another).AndReturn('whatever\ncookie2')
575 ci = self.MakeChangeInfo('mychange', 0, 0, '\n'.join(description_lines),
576 files)
577 self.mox.ReplayAll() 563 self.mox.ReplayAll()
578 564
579 change = presubmit.GclChange(ci) 565 change = presubmit.SvnChange('mychange', '\n'.join(description_lines),
566 self.fake_root_dir, files, 0, 0)
580 input_api = presubmit.InputApi(change, 567 input_api = presubmit.InputApi(change,
581 join(self.fake_root_dir, 'foo', 568 join(self.fake_root_dir, 'foo',
582 'PRESUBMIT.py'), 569 'PRESUBMIT.py'),
583 False) 570 False)
584 # Doesn't filter much 571 # Doesn't filter much
585 got_files = input_api.AffectedFiles() 572 got_files = input_api.AffectedFiles()
586 self.assertEquals(len(got_files), 7) 573 self.assertEquals(len(got_files), 7)
587 self.assertEquals(got_files[0].LocalPath(), presubmit.normpath(files[0][1])) 574 self.assertEquals(got_files[0].LocalPath(), presubmit.normpath(files[0][1]))
588 self.assertEquals(got_files[1].LocalPath(), presubmit.normpath(files[1][1])) 575 self.assertEquals(got_files[1].LocalPath(), presubmit.normpath(files[1][1]))
589 self.assertEquals(got_files[2].LocalPath(), presubmit.normpath(files[2][1])) 576 self.assertEquals(got_files[2].LocalPath(), presubmit.normpath(files[2][1]))
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
678 665
679 def testCustomFilter(self): 666 def testCustomFilter(self):
680 def FilterSourceFile(affected_file): 667 def FilterSourceFile(affected_file):
681 return 'a' in affected_file.LocalPath() 668 return 'a' in affected_file.LocalPath()
682 files = [('A', 'eeaee'), ('M', 'eeabee'), ('M', 'eebcee')] 669 files = [('A', 'eeaee'), ('M', 'eeabee'), ('M', 'eebcee')]
683 for (action, item) in files: 670 for (action, item) in files:
684 item = presubmit.os.path.join(self.fake_root_dir, item) 671 item = presubmit.os.path.join(self.fake_root_dir, item)
685 presubmit.os.path.exists(item).AndReturn(True) 672 presubmit.os.path.exists(item).AndReturn(True)
686 presubmit.os.path.isdir(item).AndReturn(False) 673 presubmit.os.path.isdir(item).AndReturn(False)
687 presubmit.gcl.GetSVNFileProperty(item, 'svn:mime-type').AndReturn(None) 674 presubmit.gcl.GetSVNFileProperty(item, 'svn:mime-type').AndReturn(None)
688 ci = self.MakeChangeInfo('mychange', 0, 0, '', files)
689 self.mox.ReplayAll() 675 self.mox.ReplayAll()
690 676
691 change = presubmit.GclChange(ci) 677 change = presubmit.SvnChange('mychange', '', self.fake_root_dir, files, 0,
678 0)
692 input_api = presubmit.InputApi(change, 679 input_api = presubmit.InputApi(change,
693 presubmit.os.path.join(self.fake_root_dir, 680 presubmit.os.path.join(self.fake_root_dir,
694 'PRESUBMIT.py'), 681 'PRESUBMIT.py'),
695 False) 682 False)
696 got_files = input_api.AffectedSourceFiles(FilterSourceFile) 683 got_files = input_api.AffectedSourceFiles(FilterSourceFile)
697 self.assertEquals(len(got_files), 2) 684 self.assertEquals(len(got_files), 2)
698 self.assertEquals(got_files[0].LocalPath(), 'eeaee') 685 self.assertEquals(got_files[0].LocalPath(), 'eeaee')
699 self.assertEquals(got_files[1].LocalPath(), 'eeabee') 686 self.assertEquals(got_files[1].LocalPath(), 'eeabee')
700 687
701 def testLambdaFilter(self): 688 def testLambdaFilter(self):
702 white_list = presubmit.InputApi.DEFAULT_BLACK_LIST + (r".*?a.*?",) 689 white_list = presubmit.InputApi.DEFAULT_BLACK_LIST + (r".*?a.*?",)
703 black_list = [r".*?b.*?"] 690 black_list = [r".*?b.*?"]
704 files = [('A', 'eeaee'), ('M', 'eeabee'), ('M', 'eebcee'), ('M', 'eecaee')] 691 files = [('A', 'eeaee'), ('M', 'eeabee'), ('M', 'eebcee'), ('M', 'eecaee')]
705 for (action, item) in files: 692 for (action, item) in files:
706 item = presubmit.os.path.join(self.fake_root_dir, item) 693 item = presubmit.os.path.join(self.fake_root_dir, item)
707 presubmit.os.path.exists(item).AndReturn(True) 694 presubmit.os.path.exists(item).AndReturn(True)
708 presubmit.os.path.isdir(item).AndReturn(False) 695 presubmit.os.path.isdir(item).AndReturn(False)
709 presubmit.gcl.GetSVNFileProperty(item, 'svn:mime-type').AndReturn(None) 696 presubmit.gcl.GetSVNFileProperty(item, 'svn:mime-type').AndReturn(None)
710 ci = self.MakeChangeInfo('mychange', 0, 0, '', files)
711 self.mox.ReplayAll() 697 self.mox.ReplayAll()
712 698
713 change = presubmit.GclChange(ci) 699 change = presubmit.SvnChange('mychange', '', self.fake_root_dir, files, 0,
700 0)
714 input_api = presubmit.InputApi(change, './PRESUBMIT.py', False) 701 input_api = presubmit.InputApi(change, './PRESUBMIT.py', False)
715 # Sample usage of overiding the default white and black lists. 702 # Sample usage of overiding the default white and black lists.
716 got_files = input_api.AffectedSourceFiles( 703 got_files = input_api.AffectedSourceFiles(
717 lambda x: input_api.FilterSourceFile(x, white_list, black_list)) 704 lambda x: input_api.FilterSourceFile(x, white_list, black_list))
718 self.assertEquals(len(got_files), 2) 705 self.assertEquals(len(got_files), 2)
719 self.assertEquals(got_files[0].LocalPath(), 'eeaee') 706 self.assertEquals(got_files[0].LocalPath(), 'eeaee')
720 self.assertEquals(got_files[1].LocalPath(), 'eecaee') 707 self.assertEquals(got_files[1].LocalPath(), 'eecaee')
721 708
722 def testGetAbsoluteLocalPath(self): 709 def testGetAbsoluteLocalPath(self):
723 join = presubmit.os.path.join 710 join = presubmit.os.path.join
724 normpath = presubmit.normpath 711 normpath = presubmit.normpath
725 # Regression test for bug of presubmit stuff that relies on invoking 712 # Regression test for bug of presubmit stuff that relies on invoking
726 # SVN (e.g. to get mime type of file) not working unless gcl invoked 713 # SVN (e.g. to get mime type of file) not working unless gcl invoked
727 # from the client root (e.g. if you were at 'src' and did 'cd base' before 714 # from the client root (e.g. if you were at 'src' and did 'cd base' before
728 # invoking 'gcl upload' it would fail because svn wouldn't find the files 715 # invoking 'gcl upload' it would fail because svn wouldn't find the files
729 # the presubmit script was asking about). 716 # the presubmit script was asking about).
730 files = [ 717 files = [
731 ['A', 'isdir'], 718 ['A', 'isdir'],
732 ['A', join('isdir', 'blat.cc')], 719 ['A', join('isdir', 'blat.cc')],
733 ['M', join('elsewhere', 'ouf.cc')], 720 ['M', join('elsewhere', 'ouf.cc')],
734 ] 721 ]
735 ci = self.MakeChangeInfo('mychange', 0, 0, '', files)
736 self.mox.ReplayAll() 722 self.mox.ReplayAll()
737 723
738 # It doesn't make sense on non-Windows platform. This is somewhat hacky, 724 change = presubmit.Change('mychange', '', self.fake_root_dir, files, 0, 0)
739 # but it is needed since we can't just use os.path.join('c:', 'temp').
740 change = presubmit.GclChange(ci)
741 affected_files = change.AffectedFiles(include_dirs=True) 725 affected_files = change.AffectedFiles(include_dirs=True)
742 # Local paths should remain the same 726 # Local paths should remain the same
743 self.assertEquals(affected_files[0].LocalPath(), normpath('isdir')) 727 self.assertEquals(affected_files[0].LocalPath(), normpath('isdir'))
744 self.assertEquals(affected_files[1].LocalPath(), normpath('isdir/blat.cc')) 728 self.assertEquals(affected_files[1].LocalPath(), normpath('isdir/blat.cc'))
745 # Absolute paths should be prefixed 729 # Absolute paths should be prefixed
746 self.assertEquals(affected_files[0].AbsoluteLocalPath(), 730 self.assertEquals(affected_files[0].AbsoluteLocalPath(),
747 normpath(join(self.fake_root_dir, 'isdir'))) 731 normpath(join(self.fake_root_dir, 'isdir')))
748 self.assertEquals(affected_files[1].AbsoluteLocalPath(), 732 self.assertEquals(affected_files[1].AbsoluteLocalPath(),
749 normpath(join(self.fake_root_dir, 'isdir/blat.cc'))) 733 normpath(join(self.fake_root_dir, 'isdir/blat.cc')))
750 734
751 # New helper functions need to work 735 # New helper functions need to work
752 paths_from_change = change.AbsoluteLocalPaths(include_dirs=True) 736 paths_from_change = change.AbsoluteLocalPaths(include_dirs=True)
753 self.assertEqual(len(paths_from_change), 3) 737 self.assertEqual(len(paths_from_change), 3)
754 presubmit_path = join(self.fake_root_dir, 'isdir', 'PRESUBMIT.py') 738 presubmit_path = join(self.fake_root_dir, 'isdir', 'PRESUBMIT.py')
755 api = presubmit.InputApi(change=change, 739 api = presubmit.InputApi(change=change,
756 presubmit_path=presubmit_path, 740 presubmit_path=presubmit_path,
757 is_committing=True) 741 is_committing=True)
758 paths_from_api = api.AbsoluteLocalPaths(include_dirs=True) 742 paths_from_api = api.AbsoluteLocalPaths(include_dirs=True)
759 self.assertEqual(len(paths_from_api), 2) 743 self.assertEqual(len(paths_from_api), 2)
760 for absolute_paths in [paths_from_change, paths_from_api]: 744 for absolute_paths in [paths_from_change, paths_from_api]:
761 self.assertEqual(absolute_paths[0], 745 self.assertEqual(absolute_paths[0],
762 normpath(join(self.fake_root_dir, 'isdir'))) 746 normpath(join(self.fake_root_dir, 'isdir')))
763 self.assertEqual(absolute_paths[1], 747 self.assertEqual(absolute_paths[1],
764 normpath(join(self.fake_root_dir, 'isdir', 'blat.cc'))) 748 normpath(join(self.fake_root_dir, 'isdir', 'blat.cc')))
765 749
766 def testDeprecated(self): 750 def testDeprecated(self):
767 presubmit.warnings.warn(mox.IgnoreArg(), category=mox.IgnoreArg(), 751 presubmit.warnings.warn(mox.IgnoreArg(), category=mox.IgnoreArg(),
768 stacklevel=2) 752 stacklevel=2)
769 ci = self.MakeChangeInfo('mychange', 0, 0, 'Bleh\n', [])
770 self.mox.ReplayAll() 753 self.mox.ReplayAll()
771 754
772 change = presubmit.GclChange(ci) 755 change = presubmit.Change('mychange', '', self.fake_root_dir, [], 0, 0)
773 api = presubmit.InputApi( 756 api = presubmit.InputApi(
774 change, 757 change,
775 presubmit.os.path.join(self.fake_root_dir, 'foo', 'PRESUBMIT.py'), True) 758 presubmit.os.path.join(self.fake_root_dir, 'foo', 'PRESUBMIT.py'), True)
776 api.AffectedTextFiles(include_deletes=False) 759 api.AffectedTextFiles(include_deletes=False)
777 760
778 def testReadFileStringDenied(self): 761 def testReadFileStringDenied(self):
779 ci = self.MakeChangeInfo('foo', 0, 0, 'Foo\n', [('M', 'AA')])
780 self.mox.ReplayAll() 762 self.mox.ReplayAll()
781 763
764 change = presubmit.Change('foo', 'foo', self.fake_root_dir, [('M', 'AA')],
765 0, 0)
782 input_api = presubmit.InputApi( 766 input_api = presubmit.InputApi(
783 None, presubmit.os.path.join(self.fake_root_dir, '/p'), False) 767 change, presubmit.os.path.join(self.fake_root_dir, '/p'), False)
784 input_api.change = presubmit.GclChange(ci)
785 self.assertRaises(IOError, input_api.ReadFile, 'boo', 'x') 768 self.assertRaises(IOError, input_api.ReadFile, 'boo', 'x')
786 769
787 def testReadFileStringAccepted(self): 770 def testReadFileStringAccepted(self):
788 ci = self.MakeChangeInfo('foo', 0, 0, 'Foo\n', [('M', 'AA')])
789 path = presubmit.os.path.join(self.fake_root_dir, 'AA/boo') 771 path = presubmit.os.path.join(self.fake_root_dir, 'AA/boo')
790 presubmit.gcl.ReadFile(path, 'x').AndReturn(None) 772 presubmit.gcl.ReadFile(path, 'x').AndReturn(None)
791 self.mox.ReplayAll() 773 self.mox.ReplayAll()
792 774
775 change = presubmit.Change('foo', 'foo', self.fake_root_dir, [('M', 'AA')],
776 0, 0)
793 input_api = presubmit.InputApi( 777 input_api = presubmit.InputApi(
794 None, presubmit.os.path.join(self.fake_root_dir, '/p'), False) 778 change, presubmit.os.path.join(self.fake_root_dir, '/p'), False)
795 input_api.change = presubmit.GclChange(ci)
796 input_api.ReadFile(path, 'x') 779 input_api.ReadFile(path, 'x')
797 780
798 def testReadFileAffectedFileDenied(self): 781 def testReadFileAffectedFileDenied(self):
799 ci = self.MakeChangeInfo('foo', 0, 0, 'Foo\n', [('M', 'AA')])
800 file = presubmit.AffectedFile('boo', 'M', 'Unrelated') 782 file = presubmit.AffectedFile('boo', 'M', 'Unrelated')
801 self.mox.ReplayAll() 783 self.mox.ReplayAll()
802 784
785 change = presubmit.Change('foo', 'foo', self.fake_root_dir, [('M', 'AA')],
786 0, 0)
803 input_api = presubmit.InputApi( 787 input_api = presubmit.InputApi(
804 None, presubmit.os.path.join(self.fake_root_dir, '/p'), False) 788 change, presubmit.os.path.join(self.fake_root_dir, '/p'), False)
805 input_api.change = presubmit.GclChange(ci)
806 self.assertRaises(IOError, input_api.ReadFile, file, 'x') 789 self.assertRaises(IOError, input_api.ReadFile, file, 'x')
807 790
808 def testReadFileAffectedFileAccepted(self): 791 def testReadFileAffectedFileAccepted(self):
809 ci = self.MakeChangeInfo('foo', 0, 0, 'Foo\n', [('M', 'AA')])
810 file = presubmit.AffectedFile('AA/boo', 'M', self.fake_root_dir) 792 file = presubmit.AffectedFile('AA/boo', 'M', self.fake_root_dir)
811 presubmit.gcl.ReadFile(file.AbsoluteLocalPath(), 'x').AndReturn(None) 793 presubmit.gcl.ReadFile(file.AbsoluteLocalPath(), 'x').AndReturn(None)
812 self.mox.ReplayAll() 794 self.mox.ReplayAll()
813 795
796 change = presubmit.Change('foo', 'foo', self.fake_root_dir, [('M', 'AA')],
797 0, 0)
814 input_api = presubmit.InputApi( 798 input_api = presubmit.InputApi(
815 None, presubmit.os.path.join(self.fake_root_dir, '/p'), False) 799 change, presubmit.os.path.join(self.fake_root_dir, '/p'), False)
816 input_api.change = presubmit.GclChange(ci)
817 input_api.ReadFile(file, 'x') 800 input_api.ReadFile(file, 'x')
818 801
819 802
820 class OuputApiUnittest(PresubmitTestsBase): 803 class OuputApiUnittest(PresubmitTestsBase):
821 """Tests presubmit.OutputApi.""" 804 """Tests presubmit.OutputApi."""
822 def testMembersChanged(self): 805 def testMembersChanged(self):
823 self.mox.ReplayAll() 806 self.mox.ReplayAll()
824 members = [ 807 members = [
825 'MailTextResult', 'PresubmitError', 'PresubmitNotifyResult', 808 'MailTextResult', 'PresubmitError', 'PresubmitNotifyResult',
826 'PresubmitPromptWarning', 'PresubmitResult', 809 'PresubmitPromptWarning', 'PresubmitResult',
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
953 936
954 class GclChangeUnittest(PresubmitTestsBase): 937 class GclChangeUnittest(PresubmitTestsBase):
955 def testMembersChanged(self): 938 def testMembersChanged(self):
956 members = [ 939 members = [
957 'AbsoluteLocalPaths', 'AffectedFiles', 'AffectedTextFiles', 940 'AbsoluteLocalPaths', 'AffectedFiles', 'AffectedTextFiles',
958 'DescriptionText', 'FullDescriptionText', 'LocalPaths', 'Name', 941 'DescriptionText', 'FullDescriptionText', 'LocalPaths', 'Name',
959 'RepositoryRoot', 'RightHandSideLines', 'ServerPaths', 942 'RepositoryRoot', 'RightHandSideLines', 'ServerPaths',
960 'issue', 'patchset', 'tags', 943 'issue', 'patchset', 'tags',
961 ] 944 ]
962 # If this test fails, you should add the relevant test. 945 # If this test fails, you should add the relevant test.
963 ci = self.MakeChangeInfo('', 0, 0, '', [])
964 self.mox.ReplayAll() 946 self.mox.ReplayAll()
965 947
966 self.compareMembers(presubmit.GclChange(ci), members) 948 change = presubmit.Change('foo', 'foo', self.fake_root_dir, [('M', 'AA')],
949 0, 0)
950 self.compareMembers(change, members)
967 951
968 952
969 class CannedChecksUnittest(PresubmitTestsBase): 953 class CannedChecksUnittest(PresubmitTestsBase):
970 """Tests presubmit_canned_checks.py.""" 954 """Tests presubmit_canned_checks.py."""
971 955
972 def setUp(self): 956 def setUp(self):
973 PresubmitTestsBase.setUp(self) 957 PresubmitTestsBase.setUp(self)
974 self.mox.StubOutWithMock(presubmit_canned_checks, 958 self.mox.StubOutWithMock(presubmit_canned_checks,
975 '_RunPythonUnitTests_LoadTests') 959 '_RunPythonUnitTests_LoadTests')
976 960
977 def MockInputApi(self): 961 def MockInputApi(self, change, committing):
978 input_api = self.mox.CreateMock(presubmit.InputApi) 962 input_api = self.mox.CreateMock(presubmit.InputApi)
979 input_api.cStringIO = presubmit.cStringIO 963 input_api.cStringIO = presubmit.cStringIO
980 input_api.re = presubmit.re 964 input_api.re = presubmit.re
981 input_api.traceback = presubmit.traceback 965 input_api.traceback = presubmit.traceback
982 input_api.urllib2 = self.mox.CreateMock(presubmit.urllib2) 966 input_api.urllib2 = self.mox.CreateMock(presubmit.urllib2)
983 input_api.unittest = unittest 967 input_api.unittest = unittest
968 input_api.change = change
969 input_api.is_committing = committing
984 return input_api 970 return input_api
985 971
986 def testMembersChanged(self): 972 def testMembersChanged(self):
987 self.mox.ReplayAll() 973 self.mox.ReplayAll()
988 members = [ 974 members = [
989 'CheckChangeHasBugField', 'CheckChangeHasDescription', 975 'CheckChangeHasBugField', 'CheckChangeHasDescription',
990 'CheckChangeHasNoStrayWhitespace', 976 'CheckChangeHasNoStrayWhitespace',
991 'CheckChangeHasOnlyOneEol', 'CheckChangeHasNoCR', 977 'CheckChangeHasOnlyOneEol', 'CheckChangeHasNoCR',
992 'CheckChangeHasNoCrAndHasOnlyOneEol', 'CheckChangeHasNoTabs', 978 'CheckChangeHasNoCrAndHasOnlyOneEol', 'CheckChangeHasNoTabs',
993 'CheckChangeHasQaField', 'CheckChangeHasTestedField', 979 'CheckChangeHasQaField', 'CheckChangeHasTestedField',
994 'CheckChangeHasTestField', 'CheckChangeSvnEolStyle', 980 'CheckChangeHasTestField', 'CheckChangeSvnEolStyle',
995 'CheckDoNotSubmit', 981 'CheckDoNotSubmit',
996 'CheckDoNotSubmitInDescription', 'CheckDoNotSubmitInFiles', 982 'CheckDoNotSubmitInDescription', 'CheckDoNotSubmitInFiles',
997 'CheckLongLines', 'CheckTreeIsOpen', 'RunPythonUnitTests', 983 'CheckLongLines', 'CheckTreeIsOpen', 'RunPythonUnitTests',
998 ] 984 ]
999 # If this test fails, you should add the relevant test. 985 # If this test fails, you should add the relevant test.
1000 self.compareMembers(presubmit_canned_checks, members) 986 self.compareMembers(presubmit_canned_checks, members)
1001 987
1002 def DescriptionTest(self, check, description1, description2, error_type, 988 def DescriptionTest(self, check, description1, description2, error_type,
1003 committing): 989 committing):
1004 input_api1 = self.MockInputApi() 990 change1 = presubmit.Change('foo1', description1, self.fake_root_dir, None,
1005 input_api1.is_committing = committing 991 0, 0)
1006 ci1 = self.MakeChangeInfo('foo', 0, 0, description1, []) 992 input_api1 = self.MockInputApi(change1, committing)
1007 input_api2 = self.MockInputApi() 993 change2 = presubmit.Change('foo2', description2, self.fake_root_dir, None,
1008 input_api2.is_committing = committing 994 0, 0)
1009 ci2 = self.MakeChangeInfo('foo', 0, 0, description2, []) 995 input_api2 = self.MockInputApi(change2, committing)
1010 self.mox.ReplayAll() 996 self.mox.ReplayAll()
1011 997
1012 input_api1.change = presubmit.GclChange(ci1)
1013 input_api2.change = presubmit.GclChange(ci2)
1014 results1 = check(input_api1, presubmit.OutputApi) 998 results1 = check(input_api1, presubmit.OutputApi)
1015 self.assertEquals(results1, []) 999 self.assertEquals(results1, [])
1016 results2 = check(input_api2, presubmit.OutputApi) 1000 results2 = check(input_api2, presubmit.OutputApi)
1017 self.assertEquals(len(results2), 1) 1001 self.assertEquals(len(results2), 1)
1018 self.assertEquals(results2[0].__class__, error_type) 1002 self.assertEquals(results2[0].__class__, error_type)
1019 1003
1020 def ContentTest(self, check, content1, content2, error_type): 1004 def ContentTest(self, check, content1, content2, error_type):
1021 input_api1 = self.MockInputApi() 1005 change1 = presubmit.Change('foo1', 'foo1\n', self.fake_root_dir, None,
1022 ci1 = self.MakeChangeInfo('foo', 0, 0, 'foo1\n', []) 1006 0, 0)
1007 input_api1 = self.MockInputApi(change1, False)
1023 affected_file = self.mox.CreateMock(presubmit.SvnAffectedFile) 1008 affected_file = self.mox.CreateMock(presubmit.SvnAffectedFile)
1024 affected_file.LocalPath().AndReturn('foo.cc') 1009 affected_file.LocalPath().AndReturn('foo.cc')
1025 output1 = [ 1010 output1 = [
1026 (affected_file, 42, 'yo, ' + content1), 1011 (affected_file, 42, 'yo, ' + content1),
1027 (affected_file, 43, 'yer'), 1012 (affected_file, 43, 'yer'),
1028 (affected_file, 23, 'ya'), 1013 (affected_file, 23, 'ya'),
1029 ] 1014 ]
1030 input_api1.RightHandSideLines(mox.IgnoreArg()).AndReturn(output1) 1015 input_api1.RightHandSideLines(mox.IgnoreArg()).AndReturn(output1)
1031 input_api2 = self.MockInputApi() 1016 change2 = presubmit.Change('foo2', 'foo2\n', self.fake_root_dir, None,
1032 ci2 = self.MakeChangeInfo('foo2', 0, 0, 'foo2\n', None) 1017 0, 0)
1018 input_api2 = self.MockInputApi(change2, False)
1033 output2 = [ 1019 output2 = [
1034 (affected_file, 42, 'yo, ' + content2), 1020 (affected_file, 42, 'yo, ' + content2),
1035 (affected_file, 43, 'yer'), 1021 (affected_file, 43, 'yer'),
1036 (affected_file, 23, 'ya'), 1022 (affected_file, 23, 'ya'),
1037 ] 1023 ]
1038 input_api2.RightHandSideLines(mox.IgnoreArg()).AndReturn(output2) 1024 input_api2.RightHandSideLines(mox.IgnoreArg()).AndReturn(output2)
1039 self.mox.ReplayAll() 1025 self.mox.ReplayAll()
1040 1026
1041 input_api1.change = presubmit.GclChange(ci1)
1042 input_api2.change = presubmit.GclChange(ci2)
1043 results1 = check(input_api1, presubmit.OutputApi, None) 1027 results1 = check(input_api1, presubmit.OutputApi, None)
1044 self.assertEquals(results1, []) 1028 self.assertEquals(results1, [])
1045 results2 = check(input_api2, presubmit.OutputApi, None) 1029 results2 = check(input_api2, presubmit.OutputApi, None)
1046 self.assertEquals(len(results2), 1) 1030 self.assertEquals(len(results2), 1)
1047 self.assertEquals(results2[0].__class__, error_type) 1031 self.assertEquals(results2[0].__class__, error_type)
1048 1032
1049 def ReadFileTest(self, check, content1, content2, error_type): 1033 def ReadFileTest(self, check, content1, content2, error_type):
1050 input_api1 = self.MockInputApi() 1034 self.mox.StubOutWithMock(presubmit.InputApi, 'ReadFile')
1051 self.mox.StubOutWithMock(input_api1, 'ReadFile') 1035 change1 = presubmit.Change('foo1', 'foo1\n', self.fake_root_dir, None,
1052 ci1 = self.MakeChangeInfo('foo', 0, 0, 'foo1\n', None) 1036 0, 0)
1037 input_api1 = self.MockInputApi(change1, False)
1053 affected_file1 = self.mox.CreateMock(presubmit.SvnAffectedFile) 1038 affected_file1 = self.mox.CreateMock(presubmit.SvnAffectedFile)
1054 input_api1.AffectedSourceFiles(None).AndReturn([affected_file1]) 1039 input_api1.AffectedSourceFiles(None).AndReturn([affected_file1])
1055 input_api1.ReadFile(affected_file1, 'rb').AndReturn(content1) 1040 input_api1.ReadFile(affected_file1, 'rb').AndReturn(content1)
1056 input_api2 = self.MockInputApi() 1041 change2 = presubmit.Change('foo2', 'foo2\n', self.fake_root_dir, None,
1057 self.mox.StubOutWithMock(input_api2, 'ReadFile') 1042 0, 0)
1058 ci2 = self.MakeChangeInfo('foo2', 0, 0, 'foo2\n', []) 1043 input_api2 = self.MockInputApi(change2, False)
1059 affected_file2 = self.mox.CreateMock(presubmit.SvnAffectedFile) 1044 affected_file2 = self.mox.CreateMock(presubmit.SvnAffectedFile)
1060 input_api2.AffectedSourceFiles(None).AndReturn([affected_file2]) 1045 input_api2.AffectedSourceFiles(None).AndReturn([affected_file2])
1061 input_api2.ReadFile(affected_file2, 'rb').AndReturn(content2) 1046 input_api2.ReadFile(affected_file2, 'rb').AndReturn(content2)
1062 affected_file2.LocalPath().AndReturn('bar.cc') 1047 affected_file2.LocalPath().AndReturn('bar.cc')
1063 self.mox.ReplayAll() 1048 self.mox.ReplayAll()
1064 1049
1065 input_api1.change = presubmit.GclChange(ci1)
1066 input_api2.change = presubmit.GclChange(ci2)
1067 results = check(input_api1, presubmit.OutputApi) 1050 results = check(input_api1, presubmit.OutputApi)
1068 self.assertEquals(results, []) 1051 self.assertEquals(results, [])
1069 results2 = check(input_api2, presubmit.OutputApi) 1052 results2 = check(input_api2, presubmit.OutputApi)
1070 self.assertEquals(len(results2), 1) 1053 self.assertEquals(len(results2), 1)
1071 self.assertEquals(results2[0].__class__, error_type) 1054 self.assertEquals(results2[0].__class__, error_type)
1072 1055
1073 def SvnPropertyTest(self, check, property, value1, value2, committing, 1056 def SvnPropertyTest(self, check, property, value1, value2, committing,
1074 error_type): 1057 error_type):
1075 input_api1 = self.MockInputApi() 1058 input_api1 = self.MockInputApi(None, committing)
1076 input_api1.is_committing = committing
1077 files1 = [ 1059 files1 = [
1078 presubmit.SvnAffectedFile('foo/bar.cc', 'A'), 1060 presubmit.SvnAffectedFile('foo/bar.cc', 'A'),
1079 presubmit.SvnAffectedFile('foo.cc', 'M'), 1061 presubmit.SvnAffectedFile('foo.cc', 'M'),
1080 ] 1062 ]
1081 input_api1.AffectedSourceFiles(None).AndReturn(files1) 1063 input_api1.AffectedSourceFiles(None).AndReturn(files1)
1082 presubmit.gcl.GetSVNFileProperty(presubmit.normpath('foo/bar.cc'), 1064 presubmit.gcl.GetSVNFileProperty(presubmit.normpath('foo/bar.cc'),
1083 property).AndReturn(value1) 1065 property).AndReturn(value1)
1084 presubmit.gcl.GetSVNFileProperty(presubmit.normpath('foo.cc'), 1066 presubmit.gcl.GetSVNFileProperty(presubmit.normpath('foo.cc'),
1085 property).AndReturn(value1) 1067 property).AndReturn(value1)
1086 input_api2 = self.MockInputApi() 1068 input_api2 = self.MockInputApi(None, committing)
1087 input_api2.is_committing = committing
1088 files2 = [ 1069 files2 = [
1089 presubmit.SvnAffectedFile('foo/bar.cc', 'A'), 1070 presubmit.SvnAffectedFile('foo/bar.cc', 'A'),
1090 presubmit.SvnAffectedFile('foo.cc', 'M'), 1071 presubmit.SvnAffectedFile('foo.cc', 'M'),
1091 ] 1072 ]
1092 input_api2.AffectedSourceFiles(None).AndReturn(files2) 1073 input_api2.AffectedSourceFiles(None).AndReturn(files2)
1093 presubmit.gcl.GetSVNFileProperty(presubmit.normpath('foo/bar.cc'), 1074 presubmit.gcl.GetSVNFileProperty(presubmit.normpath('foo/bar.cc'),
1094 property).AndReturn(value2) 1075 property).AndReturn(value2)
1095 presubmit.gcl.GetSVNFileProperty(presubmit.normpath('foo.cc'), 1076 presubmit.gcl.GetSVNFileProperty(presubmit.normpath('foo.cc'),
1096 property).AndReturn(value2) 1077 property).AndReturn(value2)
1097 self.mox.ReplayAll() 1078 self.mox.ReplayAll()
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
1192 self.SvnPropertyTest(presubmit_canned_checks.CheckChangeSvnEolStyle, 1173 self.SvnPropertyTest(presubmit_canned_checks.CheckChangeSvnEolStyle,
1193 'svn:eol-style', 'LF', '', True, 1174 'svn:eol-style', 'LF', '', True,
1194 presubmit.OutputApi.PresubmitError) 1175 presubmit.OutputApi.PresubmitError)
1195 1176
1196 def testCheckChangeSvnEolStyleUpload(self): 1177 def testCheckChangeSvnEolStyleUpload(self):
1197 self.SvnPropertyTest(presubmit_canned_checks.CheckChangeSvnEolStyle, 1178 self.SvnPropertyTest(presubmit_canned_checks.CheckChangeSvnEolStyle,
1198 'svn:eol-style', 'LF', '', False, 1179 'svn:eol-style', 'LF', '', False,
1199 presubmit.OutputApi.PresubmitNotifyResult) 1180 presubmit.OutputApi.PresubmitNotifyResult)
1200 1181
1201 def testCannedCheckTreeIsOpenOpen(self): 1182 def testCannedCheckTreeIsOpenOpen(self):
1202 input_api = self.MockInputApi() 1183 input_api = self.MockInputApi(None, True)
1203 input_api.is_committing = True
1204 connection = self.mox.CreateMockAnything() 1184 connection = self.mox.CreateMockAnything()
1205 input_api.urllib2.urlopen('url_to_open').AndReturn(connection) 1185 input_api.urllib2.urlopen('url_to_open').AndReturn(connection)
1206 connection.read().AndReturn('1') 1186 connection.read().AndReturn('1')
1207 connection.close() 1187 connection.close()
1208 self.mox.ReplayAll() 1188 self.mox.ReplayAll()
1209 results = presubmit_canned_checks.CheckTreeIsOpen( 1189 results = presubmit_canned_checks.CheckTreeIsOpen(
1210 input_api, presubmit.OutputApi, url='url_to_open', closed='0') 1190 input_api, presubmit.OutputApi, url='url_to_open', closed='0')
1211 self.assertEquals(results, []) 1191 self.assertEquals(results, [])
1212 1192
1213 def testCannedCheckTreeIsOpenClosed(self): 1193 def testCannedCheckTreeIsOpenClosed(self):
1214 input_api = self.MockInputApi() 1194 input_api = self.MockInputApi(None, True)
1215 input_api.is_committing = True
1216 connection = self.mox.CreateMockAnything() 1195 connection = self.mox.CreateMockAnything()
1217 input_api.urllib2.urlopen('url_to_closed').AndReturn(connection) 1196 input_api.urllib2.urlopen('url_to_closed').AndReturn(connection)
1218 connection.read().AndReturn('0') 1197 connection.read().AndReturn('0')
1219 connection.close() 1198 connection.close()
1220 self.mox.ReplayAll() 1199 self.mox.ReplayAll()
1221 results = presubmit_canned_checks.CheckTreeIsOpen( 1200 results = presubmit_canned_checks.CheckTreeIsOpen(
1222 input_api, presubmit.OutputApi, url='url_to_closed', closed='0') 1201 input_api, presubmit.OutputApi, url='url_to_closed', closed='0')
1223 self.assertEquals(len(results), 1) 1202 self.assertEquals(len(results), 1)
1224 self.assertEquals(results[0].__class__, 1203 self.assertEquals(results[0].__class__,
1225 presubmit.OutputApi.PresubmitPromptWarning) 1204 presubmit.OutputApi.PresubmitPromptWarning)
1226 1205
1227 def testRunPythonUnitTestsNoTest(self): 1206 def testRunPythonUnitTestsNoTest(self):
1228 input_api = self.MockInputApi() 1207 input_api = self.MockInputApi(None, False)
1229 input_api.is_committing = False
1230 self.mox.ReplayAll() 1208 self.mox.ReplayAll()
1231 results = presubmit_canned_checks.RunPythonUnitTests( 1209 results = presubmit_canned_checks.RunPythonUnitTests(
1232 input_api, presubmit.OutputApi, []) 1210 input_api, presubmit.OutputApi, [])
1233 self.assertEquals(results, []) 1211 self.assertEquals(results, [])
1234 1212
1235 def testRunPythonUnitTestsNonExistentUpload(self): 1213 def testRunPythonUnitTestsNonExistentUpload(self):
1236 input_api = self.MockInputApi() 1214 input_api = self.MockInputApi(None, False)
1237 input_api.is_committing = False
1238 presubmit_canned_checks._RunPythonUnitTests_LoadTests( 1215 presubmit_canned_checks._RunPythonUnitTests_LoadTests(
1239 input_api, '_non_existent_module').AndRaise( 1216 input_api, '_non_existent_module').AndRaise(
1240 exceptions.ImportError('Blehh')) 1217 exceptions.ImportError('Blehh'))
1241 self.mox.ReplayAll() 1218 self.mox.ReplayAll()
1219
1242 results = presubmit_canned_checks.RunPythonUnitTests( 1220 results = presubmit_canned_checks.RunPythonUnitTests(
1243 input_api, presubmit.OutputApi, ['_non_existent_module']) 1221 input_api, presubmit.OutputApi, ['_non_existent_module'])
1244 self.assertEquals(len(results), 1) 1222 self.assertEquals(len(results), 1)
1245 self.assertEquals(results[0].__class__, 1223 self.assertEquals(results[0].__class__,
1246 presubmit.OutputApi.PresubmitNotifyResult) 1224 presubmit.OutputApi.PresubmitNotifyResult)
1247 1225
1248 def testRunPythonUnitTestsNonExistentCommitting(self): 1226 def testRunPythonUnitTestsNonExistentCommitting(self):
1249 input_api = self.MockInputApi() 1227 input_api = self.MockInputApi(None, True)
1250 input_api.is_committing = True
1251 presubmit_canned_checks._RunPythonUnitTests_LoadTests( 1228 presubmit_canned_checks._RunPythonUnitTests_LoadTests(
1252 input_api, '_non_existent_module').AndRaise( 1229 input_api, '_non_existent_module').AndRaise(
1253 exceptions.ImportError('Blehh')) 1230 exceptions.ImportError('Blehh'))
1254 self.mox.ReplayAll() 1231 self.mox.ReplayAll()
1255 results = presubmit_canned_checks.RunPythonUnitTests( 1232 results = presubmit_canned_checks.RunPythonUnitTests(
1256 input_api, presubmit.OutputApi, ['_non_existent_module']) 1233 input_api, presubmit.OutputApi, ['_non_existent_module'])
1257 self.assertEquals(len(results), 1) 1234 self.assertEquals(len(results), 1)
1258 self.assertEquals(results[0].__class__, presubmit.OutputApi.PresubmitError) 1235 self.assertEquals(results[0].__class__, presubmit.OutputApi.PresubmitError)
1259 1236
1260 def testRunPythonUnitTestsEmptyUpload(self): 1237 def testRunPythonUnitTestsEmptyUpload(self):
1261 input_api = self.MockInputApi() 1238 input_api = self.MockInputApi(None, False)
1262 input_api.is_committing = False
1263 test_module = self.mox.CreateMockAnything() 1239 test_module = self.mox.CreateMockAnything()
1264 presubmit_canned_checks._RunPythonUnitTests_LoadTests( 1240 presubmit_canned_checks._RunPythonUnitTests_LoadTests(
1265 input_api, 'test_module').AndReturn([]) 1241 input_api, 'test_module').AndReturn([])
1266 self.mox.ReplayAll() 1242 self.mox.ReplayAll()
1267 1243
1268 results = presubmit_canned_checks.RunPythonUnitTests( 1244 results = presubmit_canned_checks.RunPythonUnitTests(
1269 input_api, presubmit.OutputApi, ['test_module']) 1245 input_api, presubmit.OutputApi, ['test_module'])
1270 self.assertEquals(results, []) 1246 self.assertEquals(results, [])
1271 1247
1272 def testRunPythonUnitTestsEmptyCommitting(self): 1248 def testRunPythonUnitTestsEmptyCommitting(self):
1273 input_api = self.MockInputApi() 1249 input_api = self.MockInputApi(None, True)
1274 input_api.is_committing = True
1275 test_module = self.mox.CreateMockAnything() 1250 test_module = self.mox.CreateMockAnything()
1276 presubmit_canned_checks._RunPythonUnitTests_LoadTests( 1251 presubmit_canned_checks._RunPythonUnitTests_LoadTests(
1277 input_api, 'test_module').AndReturn([]) 1252 input_api, 'test_module').AndReturn([])
1278 self.mox.ReplayAll() 1253 self.mox.ReplayAll()
1279 1254
1280 results = presubmit_canned_checks.RunPythonUnitTests( 1255 results = presubmit_canned_checks.RunPythonUnitTests(
1281 input_api, presubmit.OutputApi, ['test_module']) 1256 input_api, presubmit.OutputApi, ['test_module'])
1282 self.assertEquals(results, []) 1257 self.assertEquals(results, [])
1283 1258
1284 def testRunPythonUnitTestsFailureUpload(self): 1259 def testRunPythonUnitTestsFailureUpload(self):
1285 input_api = self.MockInputApi() 1260 input_api = self.MockInputApi(None, False)
1286 input_api.is_committing = False
1287 input_api.unittest = self.mox.CreateMock(unittest) 1261 input_api.unittest = self.mox.CreateMock(unittest)
1288 input_api.cStringIO = self.mox.CreateMock(presubmit.cStringIO) 1262 input_api.cStringIO = self.mox.CreateMock(presubmit.cStringIO)
1289 test = self.mox.CreateMockAnything() 1263 test = self.mox.CreateMockAnything()
1290 presubmit_canned_checks._RunPythonUnitTests_LoadTests( 1264 presubmit_canned_checks._RunPythonUnitTests_LoadTests(
1291 input_api, 'test_module').AndReturn([test]) 1265 input_api, 'test_module').AndReturn([test])
1292 runner = self.mox.CreateMockAnything() 1266 runner = self.mox.CreateMockAnything()
1293 buffer = self.mox.CreateMockAnything() 1267 buffer = self.mox.CreateMockAnything()
1294 input_api.cStringIO.StringIO().AndReturn(buffer) 1268 input_api.cStringIO.StringIO().AndReturn(buffer)
1295 buffer.getvalue().AndReturn('BOO HOO!') 1269 buffer.getvalue().AndReturn('BOO HOO!')
1296 input_api.unittest.TextTestRunner(stream=buffer, verbosity=0 1270 input_api.unittest.TextTestRunner(stream=buffer, verbosity=0
1297 ).AndReturn(runner) 1271 ).AndReturn(runner)
1298 suite = self.mox.CreateMockAnything() 1272 suite = self.mox.CreateMockAnything()
1299 input_api.unittest.TestSuite([test]).AndReturn(suite) 1273 input_api.unittest.TestSuite([test]).AndReturn(suite)
1300 test_result = self.mox.CreateMockAnything() 1274 test_result = self.mox.CreateMockAnything()
1301 runner.run(suite).AndReturn(test_result) 1275 runner.run(suite).AndReturn(test_result)
1302 test_result.wasSuccessful().AndReturn(False) 1276 test_result.wasSuccessful().AndReturn(False)
1303 test_result.failures = [None, None] 1277 test_result.failures = [None, None]
1304 test_result.errors = [None, None, None] 1278 test_result.errors = [None, None, None]
1305 self.mox.ReplayAll() 1279 self.mox.ReplayAll()
1306 1280
1307 results = presubmit_canned_checks.RunPythonUnitTests( 1281 results = presubmit_canned_checks.RunPythonUnitTests(
1308 input_api, presubmit.OutputApi, ['test_module']) 1282 input_api, presubmit.OutputApi, ['test_module'])
1309 self.assertEquals(len(results), 1) 1283 self.assertEquals(len(results), 1)
1310 self.assertEquals(results[0].__class__, 1284 self.assertEquals(results[0].__class__,
1311 presubmit.OutputApi.PresubmitNotifyResult) 1285 presubmit.OutputApi.PresubmitNotifyResult)
1312 self.assertEquals(results[0]._long_text, 'BOO HOO!') 1286 self.assertEquals(results[0]._long_text, 'BOO HOO!')
1313 1287
1314 def testRunPythonUnitTestsFailureCommitting(self): 1288 def testRunPythonUnitTestsFailureCommitting(self):
1315 input_api = self.MockInputApi() 1289 input_api = self.MockInputApi(None, True)
1316 input_api.is_committing = True
1317 input_api.unittest = self.mox.CreateMock(unittest) 1290 input_api.unittest = self.mox.CreateMock(unittest)
1318 input_api.cStringIO = self.mox.CreateMock(presubmit.cStringIO) 1291 input_api.cStringIO = self.mox.CreateMock(presubmit.cStringIO)
1319 test = self.mox.CreateMockAnything() 1292 test = self.mox.CreateMockAnything()
1320 presubmit_canned_checks._RunPythonUnitTests_LoadTests( 1293 presubmit_canned_checks._RunPythonUnitTests_LoadTests(
1321 input_api, 'test_module').AndReturn([test]) 1294 input_api, 'test_module').AndReturn([test])
1322 runner = self.mox.CreateMockAnything() 1295 runner = self.mox.CreateMockAnything()
1323 buffer = self.mox.CreateMockAnything() 1296 buffer = self.mox.CreateMockAnything()
1324 input_api.cStringIO.StringIO().AndReturn(buffer) 1297 input_api.cStringIO.StringIO().AndReturn(buffer)
1325 buffer.getvalue().AndReturn('BOO HOO!') 1298 buffer.getvalue().AndReturn('BOO HOO!')
1326 input_api.unittest.TextTestRunner(stream=buffer, verbosity=0 1299 input_api.unittest.TextTestRunner(stream=buffer, verbosity=0
1327 ).AndReturn(runner) 1300 ).AndReturn(runner)
1328 suite = self.mox.CreateMockAnything() 1301 suite = self.mox.CreateMockAnything()
1329 input_api.unittest.TestSuite([test]).AndReturn(suite) 1302 input_api.unittest.TestSuite([test]).AndReturn(suite)
1330 test_result = self.mox.CreateMockAnything() 1303 test_result = self.mox.CreateMockAnything()
1331 runner.run(suite).AndReturn(test_result) 1304 runner.run(suite).AndReturn(test_result)
1332 test_result.wasSuccessful().AndReturn(False) 1305 test_result.wasSuccessful().AndReturn(False)
1333 test_result.failures = [None, None] 1306 test_result.failures = [None, None]
1334 test_result.errors = [None, None, None] 1307 test_result.errors = [None, None, None]
1335 self.mox.ReplayAll() 1308 self.mox.ReplayAll()
1336 1309
1337 results = presubmit_canned_checks.RunPythonUnitTests( 1310 results = presubmit_canned_checks.RunPythonUnitTests(
1338 input_api, presubmit.OutputApi, ['test_module']) 1311 input_api, presubmit.OutputApi, ['test_module'])
1339 self.assertEquals(len(results), 1) 1312 self.assertEquals(len(results), 1)
1340 self.assertEquals(results[0].__class__, presubmit.OutputApi.PresubmitError) 1313 self.assertEquals(results[0].__class__, presubmit.OutputApi.PresubmitError)
1341 self.assertEquals(results[0]._long_text, 'BOO HOO!') 1314 self.assertEquals(results[0]._long_text, 'BOO HOO!')
1342 1315
1343 def testRunPythonUnitTestsSuccess(self): 1316 def testRunPythonUnitTestsSuccess(self):
1344 input_api = self.MockInputApi() 1317 input_api = self.MockInputApi(None, False)
1345 input_api.is_committing = False 1318 input_api.cStringIO = self.mox.CreateMock(presubmit.cStringIO)
1346 input_api.unittest = self.mox.CreateMock(unittest) 1319 input_api.unittest = self.mox.CreateMock(unittest)
1347 input_api.cStringIO = self.mox.CreateMock(presubmit.cStringIO)
1348 test = self.mox.CreateMockAnything() 1320 test = self.mox.CreateMockAnything()
1349 presubmit_canned_checks._RunPythonUnitTests_LoadTests( 1321 presubmit_canned_checks._RunPythonUnitTests_LoadTests(
1350 input_api, 'test_module').AndReturn([test]) 1322 input_api, 'test_module').AndReturn([test])
1351 runner = self.mox.CreateMockAnything() 1323 runner = self.mox.CreateMockAnything()
1352 buffer = self.mox.CreateMockAnything() 1324 buffer = self.mox.CreateMockAnything()
1353 input_api.cStringIO.StringIO().AndReturn(buffer) 1325 input_api.cStringIO.StringIO().AndReturn(buffer)
1354 input_api.unittest.TextTestRunner(stream=buffer, verbosity=0 1326 input_api.unittest.TextTestRunner(stream=buffer, verbosity=0
1355 ).AndReturn(runner) 1327 ).AndReturn(runner)
1356 suite = self.mox.CreateMockAnything() 1328 suite = self.mox.CreateMockAnything()
1357 input_api.unittest.TestSuite([test]).AndReturn(suite) 1329 input_api.unittest.TestSuite([test]).AndReturn(suite)
1358 test_result = self.mox.CreateMockAnything() 1330 test_result = self.mox.CreateMockAnything()
1359 runner.run(suite).AndReturn(test_result) 1331 runner.run(suite).AndReturn(test_result)
1360 test_result.wasSuccessful().AndReturn(True) 1332 test_result.wasSuccessful().AndReturn(True)
1361 test_result.failures = 0 1333 test_result.failures = 0
1362 test_result.errors = 0 1334 test_result.errors = 0
1363 self.mox.ReplayAll() 1335 self.mox.ReplayAll()
1364 1336
1365 results = presubmit_canned_checks.RunPythonUnitTests( 1337 results = presubmit_canned_checks.RunPythonUnitTests(
1366 input_api, presubmit.OutputApi, ['test_module']) 1338 input_api, presubmit.OutputApi, ['test_module'])
1367 self.assertEquals(len(results), 0) 1339 self.assertEquals(len(results), 0)
1368 1340
1369 1341
1370 if __name__ == '__main__': 1342 if __name__ == '__main__':
1371 unittest.main() 1343 unittest.main()
OLDNEW
« no previous file with comments | « presubmit_support.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698