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

Side by Side Diff: tests/presubmit_unittest.py

Issue 119442: A step closer to make presubmit SCM independent. (Closed)
Patch Set: oops 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 | « tests/gcl_unittest.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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
45 presubmit.os.sep = os_sep 45 presubmit.os.sep = os_sep
46 presubmit.os.path.join = os_path_join 46 presubmit.os.path.join = os_path_join
47 presubmit.os.path.dirname = os_path_dirname 47 presubmit.os.path.dirname = os_path_dirname
48 presubmit.os.path.normpath = os_path_normpath 48 presubmit.os.path.normpath = os_path_normpath
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.mox.StubOutWithMock(presubmit.gcl, 'GetRepositoryRoot') 55 self.fake_root_dir = self.RootDir()
56 fake_root_dir = self.RootDir()
57 self.fake_root_dir = fake_root_dir
58 def MockGetRepositoryRoot():
59 return fake_root_dir
60 presubmit.gcl.GetRepositoryRoot = MockGetRepositoryRoot
61 self.mox.StubOutWithMock(presubmit.gclient, 'CaptureSVNInfo') 56 self.mox.StubOutWithMock(presubmit.gclient, 'CaptureSVNInfo')
62 self.mox.StubOutWithMock(presubmit.gcl, 'GetSVNFileProperty') 57 self.mox.StubOutWithMock(presubmit.gcl, 'GetSVNFileProperty')
63 self.mox.StubOutWithMock(presubmit.gcl, 'ReadFile') 58 self.mox.StubOutWithMock(presubmit.gcl, 'ReadFile')
59 self.mox.StubOutWithMock(presubmit.gcl, 'ChangeInfo')
64 60
65 def MakeBasicChange(self, name, description, root=None): 61 def MakeChangeInfo(self, name, issue, patchset, description):
66 ci = presubmit.gcl.ChangeInfo(name, 0, 0, description, None) 62 ci = self.mox.CreateMock(presubmit.gcl.ChangeInfo)
67 if root is None: 63 ci.name = name
68 root = self.fake_root_dir 64 ci.issue = issue
69 return presubmit.GclChange(ci, root) 65 ci.patchset = patchset
66 ci.description = description
67 ci.patch = None
68 ci.local_root = self.fake_root_dir
69 return ci
70 70
71 71
72 class PresubmitUnittest(PresubmitTestsBase): 72 class PresubmitUnittest(PresubmitTestsBase):
73 """General presubmit_support.py tests (excluding InputApi and OutputApi).""" 73 """General presubmit_support.py tests (excluding InputApi and OutputApi)."""
74 def testMembersChanged(self): 74 def testMembersChanged(self):
75 self.mox.ReplayAll() 75 self.mox.ReplayAll()
76 members = [ 76 members = [
77 'AffectedFile', 'DoPresubmitChecks', 'GclChange', 'InputApi', 77 'AffectedFile', 'DoPresubmitChecks', 'GclChange', 'InputApi',
78 'ListRelevantPresubmitFiles', 'Main', 'NotImplementedException', 78 'ListRelevantPresubmitFiles', 'Main', 'NotImplementedException',
79 'OutputApi', 'ParseFiles', 'PresubmitExecuter', 79 'OutputApi', 'ParseFiles', 'PresubmitExecuter', 'ScanSubDirs',
80 'ScanSubDirs', 'SvnAffectedFile', 80 'SvnAffectedFile',
81 'cPickle', 'cStringIO', 'exceptions', 81 'cPickle', 'cStringIO', 'exceptions',
82 'fnmatch', 'gcl', 'gclient', 'glob', 'logging', 'marshal', 'normpath', 82 'fnmatch', 'gcl', 'gclient', 'glob', 'logging', 'marshal', 'normpath',
83 'optparse', 83 'optparse',
84 'os', 'pickle', 'presubmit_canned_checks', 're', 'subprocess', 'sys', 84 'os', 'pickle', 'presubmit_canned_checks', 're', 'subprocess', 'sys',
85 'tempfile', 'traceback', 'types', 'unittest', 'urllib2', 'warnings', 85 'tempfile', 'traceback', 'types', 'unittest', 'urllib2', 'warnings',
86 ] 86 ]
87 # If this test fails, you should add the relevant test. 87 # If this test fails, you should add the relevant test.
88 self.compareMembers(presubmit, members) 88 self.compareMembers(presubmit, members)
89 89
90 def testListRelevantPresubmitFiles(self): 90 def testListRelevantPresubmitFiles(self):
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 'BUG=123', 135 'BUG=123',
136 ' STORY =http://foo/ \t', 136 ' STORY =http://foo/ \t',
137 'and some more regular text \t') 137 'and some more regular text \t')
138 files = [ 138 files = [
139 ['A', 'foo/blat.cc'], 139 ['A', 'foo/blat.cc'],
140 ['M', 'binary.dll'], # a binary file 140 ['M', 'binary.dll'], # a binary file
141 ['A', 'isdir'], # a directory 141 ['A', 'isdir'], # a directory
142 ['?', 'flop/notfound.txt'], # not found in SVN, still exists locally 142 ['?', 'flop/notfound.txt'], # not found in SVN, still exists locally
143 ['D', 'boo/flap.h'], 143 ['D', 'boo/flap.h'],
144 ] 144 ]
145 blat = presubmit.os.path.join('foo', 'blat.cc') 145 blat = presubmit.os.path.join(self.fake_root_dir, 'foo', 'blat.cc')
146 notfound = presubmit.os.path.join('flop', 'notfound.txt') 146 notfound = presubmit.os.path.join(self.fake_root_dir, 'flop', 'notfound.txt' )
147 flap = presubmit.os.path.join('boo', 'flap.h') 147 flap = presubmit.os.path.join(self.fake_root_dir, 'boo', 'flap.h')
148 binary = presubmit.os.path.join(self.fake_root_dir, 'binary.dll')
149 isdir = presubmit.os.path.join(self.fake_root_dir, 'isdir')
148 presubmit.os.path.exists(blat).AndReturn(True) 150 presubmit.os.path.exists(blat).AndReturn(True)
149 presubmit.os.path.isdir(blat).AndReturn(False) 151 presubmit.os.path.isdir(blat).AndReturn(False)
150 presubmit.os.path.exists('binary.dll').AndReturn(True) 152 presubmit.os.path.exists(binary).AndReturn(True)
151 presubmit.os.path.isdir('binary.dll').AndReturn(False) 153 presubmit.os.path.isdir(binary).AndReturn(False)
152 presubmit.os.path.exists('isdir').AndReturn(True) 154 presubmit.os.path.exists(isdir).AndReturn(True)
153 presubmit.os.path.isdir('isdir').AndReturn(True) 155 presubmit.os.path.isdir(isdir).AndReturn(True)
154 presubmit.os.path.exists(notfound).AndReturn(True) 156 presubmit.os.path.exists(notfound).AndReturn(True)
155 presubmit.os.path.isdir(notfound).AndReturn(False) 157 presubmit.os.path.isdir(notfound).AndReturn(False)
156 presubmit.os.path.exists(flap).AndReturn(False) 158 presubmit.os.path.exists(flap).AndReturn(False)
157 presubmit.gclient.CaptureSVNInfo(flap 159 presubmit.gclient.CaptureSVNInfo(flap
158 ).AndReturn({'Node Kind': 'file'}) 160 ).AndReturn({'Node Kind': 'file'})
159 presubmit.gcl.GetSVNFileProperty(blat, 'svn:mime-type').AndReturn(None) 161 presubmit.gcl.GetSVNFileProperty(blat, 'svn:mime-type').AndReturn(None)
160 presubmit.gcl.GetSVNFileProperty( 162 presubmit.gcl.GetSVNFileProperty(
161 'binary.dll', 'svn:mime-type').AndReturn('application/octet-stream') 163 binary, 'svn:mime-type').AndReturn('application/octet-stream')
162 presubmit.gcl.GetSVNFileProperty( 164 presubmit.gcl.GetSVNFileProperty(
163 notfound, 'svn:mime-type').AndReturn('') 165 notfound, 'svn:mime-type').AndReturn('')
164 presubmit.gclient.CaptureSVNInfo(blat).AndReturn( 166 presubmit.gclient.CaptureSVNInfo(blat).AndReturn(
165 {'URL': 'svn:/foo/foo/blat.cc'}) 167 {'URL': 'svn:/foo/foo/blat.cc'})
166 presubmit.gclient.CaptureSVNInfo('binary.dll').AndReturn( 168 presubmit.gclient.CaptureSVNInfo(binary).AndReturn(
167 {'URL': 'svn:/foo/binary.dll'}) 169 {'URL': 'svn:/foo/binary.dll'})
168 presubmit.gclient.CaptureSVNInfo(notfound).AndReturn({}) 170 presubmit.gclient.CaptureSVNInfo(notfound).AndReturn({})
169 presubmit.gclient.CaptureSVNInfo(flap).AndReturn( 171 presubmit.gclient.CaptureSVNInfo(flap).AndReturn(
170 {'URL': 'svn:/foo/boo/flap.h'}) 172 {'URL': 'svn:/foo/boo/flap.h'})
171 presubmit.gcl.ReadFile(blat).AndReturn('boo!\nahh?') 173 presubmit.gcl.ReadFile(blat).AndReturn('boo!\nahh?')
172 presubmit.gcl.ReadFile(notfound).AndReturn('look!\nthere?') 174 presubmit.gcl.ReadFile(notfound).AndReturn('look!\nthere?')
175 ci = self.MakeChangeInfo('mychange', 0, 0, '\n'.join(description_lines))
176 ci.GetLocalRoot().AndReturn(self.fake_root_dir)
177 ci.GetFiles().AndReturn(files)
173 self.mox.ReplayAll() 178 self.mox.ReplayAll()
174 179
175 ci = presubmit.gcl.ChangeInfo(name='mychange', issue=0, patchset=0,
176 description='\n'.join(description_lines),
177 files=files)
178 change = presubmit.GclChange(ci) 180 change = presubmit.GclChange(ci)
179 181
180 self.failUnless(change.Name() == 'mychange') 182 self.failUnless(change.Name() == 'mychange')
181 self.failUnless(change.DescriptionText() == 183 self.failUnless(change.DescriptionText() ==
182 'Hello there\nthis is a change\nand some more regular text') 184 'Hello there\nthis is a change\nand some more regular text')
183 self.failUnless(change.FullDescriptionText() == 185 self.failUnless(change.FullDescriptionText() ==
184 '\n'.join(description_lines)) 186 '\n'.join(description_lines))
185 187
186 self.failUnless(change.BUG == '123') 188 self.failUnless(change.BUG == '123')
187 self.failUnless(change.STORY == 'http://foo/') 189 self.failUnless(change.STORY == 'http://foo/')
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 self.assertEquals(rhs_lines[3][2], 'there?') 233 self.assertEquals(rhs_lines[3][2], 'there?')
232 234
233 def testExecPresubmitScript(self): 235 def testExecPresubmitScript(self):
234 description_lines = ('Hello there', 236 description_lines = ('Hello there',
235 'this is a change', 237 'this is a change',
236 'STORY=http://tracker/123') 238 'STORY=http://tracker/123')
237 files = [ 239 files = [
238 ['A', 'foo\\blat.cc'], 240 ['A', 'foo\\blat.cc'],
239 ] 241 ]
240 fake_presubmit = presubmit.os.path.join(self.fake_root_dir, 'PRESUBMIT.py') 242 fake_presubmit = presubmit.os.path.join(self.fake_root_dir, 'PRESUBMIT.py')
243 ci = self.MakeChangeInfo('mychange', 0, 0, '\n'.join(description_lines))
244 ci.GetLocalRoot().AndReturn(self.fake_root_dir)
245 ci.GetFiles().AndReturn(files)
246 ci.GetLocalRoot().AndReturn(self.fake_root_dir)
247 ci.GetFiles().AndReturn(files)
241 self.mox.ReplayAll() 248 self.mox.ReplayAll()
242 249
243 ci = presubmit.gcl.ChangeInfo(name='mychange', issue=0, patchset=0,
244 description='\n'.join(description_lines),
245 files=files)
246
247 executer = presubmit.PresubmitExecuter(ci, False) 250 executer = presubmit.PresubmitExecuter(ci, False)
248 self.failIf(executer.ExecPresubmitScript('', fake_presubmit)) 251 self.failIf(executer.ExecPresubmitScript('', fake_presubmit))
249 # No error if no on-upload entry point 252 # No error if no on-upload entry point
250 self.failIf(executer.ExecPresubmitScript( 253 self.failIf(executer.ExecPresubmitScript(
251 ('def CheckChangeOnCommit(input_api, output_api):\n' 254 ('def CheckChangeOnCommit(input_api, output_api):\n'
252 ' return (output_api.PresubmitError("!!"))\n'), 255 ' return (output_api.PresubmitError("!!"))\n'),
253 fake_presubmit 256 fake_presubmit
254 )) 257 ))
255 258
256 executer = presubmit.PresubmitExecuter(ci, True) 259 executer = presubmit.PresubmitExecuter(ci, True)
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
300 ['A', join('haspresubmit', 'blat.cc')], 303 ['A', join('haspresubmit', 'blat.cc')],
301 ] 304 ]
302 haspresubmit_path = join(self.fake_root_dir, 'haspresubmit', 'PRESUBMIT.py') 305 haspresubmit_path = join(self.fake_root_dir, 'haspresubmit', 'PRESUBMIT.py')
303 root_path = join(self.fake_root_dir, 'PRESUBMIT.py') 306 root_path = join(self.fake_root_dir, 'PRESUBMIT.py')
304 presubmit.os.path.isfile(root_path).AndReturn(True) 307 presubmit.os.path.isfile(root_path).AndReturn(True)
305 presubmit.os.path.isfile(haspresubmit_path).AndReturn(True) 308 presubmit.os.path.isfile(haspresubmit_path).AndReturn(True)
306 presubmit.gcl.ReadFile(root_path, 309 presubmit.gcl.ReadFile(root_path,
307 'rU').AndReturn(self.presubmit_text) 310 'rU').AndReturn(self.presubmit_text)
308 presubmit.gcl.ReadFile(haspresubmit_path, 311 presubmit.gcl.ReadFile(haspresubmit_path,
309 'rU').AndReturn(self.presubmit_text) 312 'rU').AndReturn(self.presubmit_text)
313 ci = self.MakeChangeInfo('mychange', 0, 0, '\n'.join(description_lines))
314 ci.GetFileNames().AndReturn([item[1] for item in files])
315 ci.GetLocalRoot().AndReturn(self.fake_root_dir)
316 ci.GetFiles().AndReturn(files)
310 self.mox.ReplayAll() 317 self.mox.ReplayAll()
311 318
312 ci = presubmit.gcl.ChangeInfo(name='mychange', issue=0, patchset=0,
313 description='\n'.join(description_lines),
314 files=files)
315
316 output = StringIO.StringIO() 319 output = StringIO.StringIO()
317 input = StringIO.StringIO('y\n') 320 input = StringIO.StringIO('y\n')
318 321
319 self.failIf(presubmit.DoPresubmitChecks(ci, False, True, output, input, 322 self.failIf(presubmit.DoPresubmitChecks(ci, False, True, output, input,
320 None, False)) 323 None, False))
321 self.assertEqual(output.getvalue().count('!!'), 2) 324 self.assertEqual(output.getvalue().count('!!'), 2)
322 325
323 def testDoPresubmitChecksPromptsAfterWarnings(self): 326 def testDoPresubmitChecksPromptsAfterWarnings(self):
324 join = presubmit.os.path.join 327 join = presubmit.os.path.join
325 description_lines = ('Hello there', 328 description_lines = ('Hello there',
326 'this is a change', 329 'this is a change',
327 'NOSUCHKEY=http://tracker/123') 330 'NOSUCHKEY=http://tracker/123')
328 files = [ 331 files = [
329 ['A', join('haspresubmit', 'blat.cc')], 332 ['A', join('haspresubmit', 'blat.cc')],
330 ] 333 ]
331 presubmit_path = join(self.fake_root_dir, 'PRESUBMIT.py') 334 presubmit_path = join(self.fake_root_dir, 'PRESUBMIT.py')
332 haspresubmit_path = join(self.fake_root_dir, 'haspresubmit', 'PRESUBMIT.py') 335 haspresubmit_path = join(self.fake_root_dir, 'haspresubmit', 'PRESUBMIT.py')
336 ci = self.MakeChangeInfo('mychange', 0, 0, '\n'.join(description_lines))
333 for i in range(2): 337 for i in range(2):
334 presubmit.os.path.isfile(presubmit_path).AndReturn(True) 338 presubmit.os.path.isfile(presubmit_path).AndReturn(True)
335 presubmit.os.path.isfile(haspresubmit_path).AndReturn(True) 339 presubmit.os.path.isfile(haspresubmit_path).AndReturn(True)
336 presubmit.gcl.ReadFile(presubmit_path, 'rU' 340 presubmit.gcl.ReadFile(presubmit_path, 'rU'
337 ).AndReturn(self.presubmit_text) 341 ).AndReturn(self.presubmit_text)
338 presubmit.gcl.ReadFile(haspresubmit_path, 'rU' 342 presubmit.gcl.ReadFile(haspresubmit_path, 'rU'
339 ).AndReturn(self.presubmit_text) 343 ).AndReturn(self.presubmit_text)
344 ci.GetFileNames().AndReturn([item[1] for item in files])
345 ci.GetLocalRoot().AndReturn(self.fake_root_dir)
346 ci.GetFiles().AndReturn(files)
340 self.mox.ReplayAll() 347 self.mox.ReplayAll()
341 348
342 ci = presubmit.gcl.ChangeInfo(name='mychange', issue=0, patchset=0,
343 description='\n'.join(description_lines),
344 files=files)
345
346 output = StringIO.StringIO() 349 output = StringIO.StringIO()
347 input = StringIO.StringIO('n\n') # say no to the warning 350 input = StringIO.StringIO('n\n') # say no to the warning
348 self.failIf(presubmit.DoPresubmitChecks(ci, False, True, output, input, 351 self.failIf(presubmit.DoPresubmitChecks(ci, False, True, output, input,
349 None, True)) 352 None, True))
350 self.assertEqual(output.getvalue().count('??'), 2) 353 self.assertEqual(output.getvalue().count('??'), 2)
351 354
352 output = StringIO.StringIO() 355 output = StringIO.StringIO()
353 input = StringIO.StringIO('y\n') # say yes to the warning 356 input = StringIO.StringIO('y\n') # say yes to the warning
354 self.failUnless(presubmit.DoPresubmitChecks(ci, 357 self.failUnless(presubmit.DoPresubmitChecks(ci, False, True, output, input,
355 False, 358 None, True))
356 True,
357 output,
358 input,
359 None,
360 True))
361 self.assertEquals(output.getvalue().count('??'), 2) 359 self.assertEquals(output.getvalue().count('??'), 2)
362 360
363 def testDoPresubmitChecksNoWarningPromptIfErrors(self): 361 def testDoPresubmitChecksNoWarningPromptIfErrors(self):
364 join = presubmit.os.path.join 362 join = presubmit.os.path.join
365 description_lines = ('Hello there', 363 description_lines = ('Hello there',
366 'this is a change', 364 'this is a change',
367 'NOSUCHKEY=http://tracker/123', 365 'NOSUCHKEY=http://tracker/123',
368 'REALLYNOSUCHKEY=http://tracker/123') 366 'REALLYNOSUCHKEY=http://tracker/123')
369 files = [ 367 files = [
370 ['A', join('haspresubmit', 'blat.cc')], 368 ['A', join('haspresubmit', 'blat.cc')],
371 ] 369 ]
372 presubmit_path = join(self.fake_root_dir, 'PRESUBMIT.py') 370 presubmit_path = join(self.fake_root_dir, 'PRESUBMIT.py')
373 haspresubmit_path = join(self.fake_root_dir, 'haspresubmit', 371 haspresubmit_path = join(self.fake_root_dir, 'haspresubmit',
374 'PRESUBMIT.py') 372 'PRESUBMIT.py')
375 presubmit.os.path.isfile(presubmit_path).AndReturn(True) 373 presubmit.os.path.isfile(presubmit_path).AndReturn(True)
376 presubmit.os.path.isfile(haspresubmit_path).AndReturn(True) 374 presubmit.os.path.isfile(haspresubmit_path).AndReturn(True)
377 presubmit.gcl.ReadFile(presubmit_path, 'rU').AndReturn(self.presubmit_text) 375 presubmit.gcl.ReadFile(presubmit_path, 'rU').AndReturn(self.presubmit_text)
378 presubmit.gcl.ReadFile(haspresubmit_path, 'rU' 376 presubmit.gcl.ReadFile(haspresubmit_path, 'rU').AndReturn(
379 ).AndReturn(self.presubmit_text) 377 self.presubmit_text)
378 ci = self.MakeChangeInfo('mychange', 0, 0, '\n'.join(description_lines))
379 ci.GetFileNames().AndReturn([item[1] for item in files])
380 ci.GetLocalRoot().AndReturn(self.fake_root_dir)
381 ci.GetFiles().AndReturn(files)
380 self.mox.ReplayAll() 382 self.mox.ReplayAll()
381 383
382 ci = presubmit.gcl.ChangeInfo(name='mychange', issue=0, patchset=0,
383 description='\n'.join(description_lines),
384 files=files)
385 output = StringIO.StringIO() 384 output = StringIO.StringIO()
386 input = StringIO.StringIO() # should be unused 385 input = StringIO.StringIO() # should be unused
387 self.failIf(presubmit.DoPresubmitChecks(ci, False, True, output, input, 386 self.failIf(presubmit.DoPresubmitChecks(ci, False, True, output, input,
388 None, False)) 387 None, False))
389 self.assertEqual(output.getvalue().count('??'), 2) 388 self.assertEqual(output.getvalue().count('??'), 2)
390 self.assertEqual(output.getvalue().count('XX!!XX'), 2) 389 self.assertEqual(output.getvalue().count('XX!!XX'), 2)
391 self.assertEqual(output.getvalue().count('(y/N)'), 0) 390 self.assertEqual(output.getvalue().count('(y/N)'), 0)
392 391
393 def testDoDefaultPresubmitChecks(self): 392 def testDoDefaultPresubmitChecks(self):
394 join = presubmit.os.path.join 393 join = presubmit.os.path.join
395 description_lines = ('Hello there', 394 description_lines = ('Hello there',
396 'this is a change', 395 'this is a change',
397 'STORY=http://tracker/123') 396 'STORY=http://tracker/123')
398 files = [ 397 files = [
399 ['A', join('haspresubmit', 'blat.cc')], 398 ['A', join('haspresubmit', 'blat.cc')],
400 ] 399 ]
401 DEFAULT_SCRIPT = """ 400 DEFAULT_SCRIPT = """
402 def CheckChangeOnUpload(input_api, output_api): 401 def CheckChangeOnUpload(input_api, output_api):
403 return [output_api.PresubmitError("!!")] 402 return [output_api.PresubmitError("!!")]
404 def CheckChangeOnCommit(input_api, output_api): 403 def CheckChangeOnCommit(input_api, output_api):
405 raise Exception("Test error") 404 raise Exception("Test error")
406 """ 405 """
407 presubmit.os.path.isfile(join(self.fake_root_dir, 'PRESUBMIT.py') 406 presubmit.os.path.isfile(join(self.fake_root_dir, 'PRESUBMIT.py')
408 ).AndReturn(False) 407 ).AndReturn(False)
409 presubmit.os.path.isfile(join(self.fake_root_dir, 408 presubmit.os.path.isfile(join(self.fake_root_dir,
410 'haspresubmit', 409 'haspresubmit',
411 'PRESUBMIT.py')).AndReturn(False) 410 'PRESUBMIT.py')).AndReturn(False)
411 ci = self.MakeChangeInfo('mychange', 0, 0, '\n'.join(description_lines))
412 ci.GetFileNames().AndReturn([item[1] for item in files])
413 ci.GetLocalRoot().AndReturn(self.fake_root_dir)
414 ci.GetFiles().AndReturn(files)
412 self.mox.ReplayAll() 415 self.mox.ReplayAll()
413
414 ci = presubmit.gcl.ChangeInfo(name='mychange', issue=0, patchset=0,
415 description='\n'.join(description_lines),
416 files=files)
417
418 416
419 output = StringIO.StringIO() 417 output = StringIO.StringIO()
420 input = StringIO.StringIO('y\n') 418 input = StringIO.StringIO('y\n')
421 # Always fail. 419 # Always fail.
422 self.failIf(presubmit.DoPresubmitChecks(ci, False, True, output, input, 420 self.failIf(presubmit.DoPresubmitChecks(ci, False, True, output, input,
423 DEFAULT_SCRIPT, False)) 421 DEFAULT_SCRIPT, False))
424 self.assertEquals(output.getvalue().count('!!'), 1) 422 self.assertEquals(output.getvalue().count('!!'), 1)
425 423
426 def testDirectoryHandling(self): 424 def testDirectoryHandling(self):
427 files = [ 425 files = [
428 ['A', 'isdir'], 426 ['A', 'isdir'],
429 ['A', 'isdir\\blat.cc'], 427 ['A', 'isdir\\blat.cc'],
430 ] 428 ]
431 presubmit.os.path.exists('isdir').AndReturn(True) 429 isdir = presubmit.os.path.join(self.fake_root_dir, 'isdir')
432 presubmit.os.path.isdir('isdir').AndReturn(True) 430 blat = presubmit.os.path.join(isdir, 'blat.cc')
433 presubmit.os.path.exists(presubmit.os.path.join('isdir', 'blat.cc') 431 presubmit.os.path.exists(isdir).AndReturn(True)
434 ).AndReturn(True) 432 presubmit.os.path.isdir(isdir).AndReturn(True)
435 presubmit.os.path.isdir(presubmit.os.path.join('isdir', 'blat.cc') 433 presubmit.os.path.exists(blat).AndReturn(True)
436 ).AndReturn(False) 434 presubmit.os.path.isdir(blat).AndReturn(False)
435 ci = self.MakeChangeInfo('mychange', 0, 0, 'foo')
436 ci.GetLocalRoot().AndReturn(self.fake_root_dir)
437 ci.GetFiles().AndReturn(files)
437 self.mox.ReplayAll() 438 self.mox.ReplayAll()
438 ci = presubmit.gcl.ChangeInfo(name='mychange', issue=0, patchset=0, 439
439 description='foo', files=files)
440 change = presubmit.GclChange(ci) 440 change = presubmit.GclChange(ci)
441
442 affected_files = change.AffectedFiles(include_dirs=False) 441 affected_files = change.AffectedFiles(include_dirs=False)
443 self.failUnless(len(affected_files) == 1) 442 self.failUnless(len(affected_files) == 1)
444 self.failUnless(affected_files[0].LocalPath().endswith('blat.cc')) 443 self.failUnless(affected_files[0].LocalPath().endswith('blat.cc'))
445 affected_files_and_dirs = change.AffectedFiles(include_dirs=True) 444 affected_files_and_dirs = change.AffectedFiles(include_dirs=True)
446 self.failUnless(len(affected_files_and_dirs) == 2) 445 self.failUnless(len(affected_files_and_dirs) == 2)
447 446
448 def testTags(self): 447 def testTags(self):
449 DEFAULT_SCRIPT = """ 448 DEFAULT_SCRIPT = """
450 def CheckChangeOnUpload(input_api, output_api): 449 def CheckChangeOnUpload(input_api, output_api):
451 if input_api.change.tags['BUG'] != 'boo': 450 if input_api.change.tags['BUG'] != 'boo':
(...skipping 17 matching lines...) Expand all
469 return [output_api.PresubmitError('Tag parsing failed. 4 ' + 468 return [output_api.PresubmitError('Tag parsing failed. 4 ' +
470 input_api.change.DescriptionText())] 469 input_api.change.DescriptionText())]
471 if (input_api.change.FullDescriptionText() != 470 if (input_api.change.FullDescriptionText() !=
472 'Blah Blah\\n\\nSTORY=http://tracker.com/42\\nBUG=boo\\n'): 471 'Blah Blah\\n\\nSTORY=http://tracker.com/42\\nBUG=boo\\n'):
473 return [output_api.PresubmitError('Tag parsing failed. 5 ' + 472 return [output_api.PresubmitError('Tag parsing failed. 5 ' +
474 input_api.change.FullDescriptionText())] 473 input_api.change.FullDescriptionText())]
475 return [output_api.PresubmitNotifyResult(input_api.change.tags['STORY'])] 474 return [output_api.PresubmitNotifyResult(input_api.change.tags['STORY'])]
476 def CheckChangeOnCommit(input_api, output_api): 475 def CheckChangeOnCommit(input_api, output_api):
477 raise Exception("Test error") 476 raise Exception("Test error")
478 """ 477 """
478 ci = self.MakeChangeInfo(
479 'foo', 0, 0, "Blah Blah\n\nSTORY=http://tracker.com/42\nBUG=boo\n")
480 ci.GetFileNames().AndReturn([])
481 ci.GetLocalRoot().AndReturn(self.fake_root_dir)
482 ci.GetFiles().AndReturn([])
479 self.mox.ReplayAll() 483 self.mox.ReplayAll()
480 484
481 change = presubmit.gcl.ChangeInfo(
482 name='foo',
483 issue=0,
484 patchset=0,
485 description="Blah Blah\n\nSTORY=http://tracker.com/42\nBUG=boo\n",
486 files=None)
487 output = StringIO.StringIO() 485 output = StringIO.StringIO()
488 input = StringIO.StringIO('y\n') 486 input = StringIO.StringIO('y\n')
489 self.failUnless(presubmit.DoPresubmitChecks(change, False, True, output, 487 self.failUnless(presubmit.DoPresubmitChecks(ci, False, True, output,
490 input, DEFAULT_SCRIPT, False)) 488 input, DEFAULT_SCRIPT, False))
491 self.assertEquals(output.getvalue(), 489 self.assertEquals(output.getvalue(),
492 ('Warning, no presubmit.py found.\n' 490 ('Warning, no presubmit.py found.\n'
493 'Running default presubmit script.\n' 491 'Running default presubmit script.\n'
494 '** Presubmit Messages **\n' 492 '** Presubmit Messages **\n'
495 'http://tracker.com/42\n\n')) 493 'http://tracker.com/42\n\n'))
496 494
497 495
498 class InputApiUnittest(PresubmitTestsBase): 496 class InputApiUnittest(PresubmitTestsBase):
499 """Tests presubmit.InputApi.""" 497 """Tests presubmit.InputApi."""
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
546 self.assertEquals(api.PresubmitLocalPath(), 'foo/path') 544 self.assertEquals(api.PresubmitLocalPath(), 'foo/path')
547 self.assertEquals(api.change, 42) 545 self.assertEquals(api.change, 42)
548 546
549 def testInputApiPresubmitScriptFiltering(self): 547 def testInputApiPresubmitScriptFiltering(self):
550 join = presubmit.os.path.join 548 join = presubmit.os.path.join
551 description_lines = ('Hello there', 549 description_lines = ('Hello there',
552 'this is a change', 550 'this is a change',
553 'BUG=123', 551 'BUG=123',
554 ' STORY =http://foo/ \t', 552 ' STORY =http://foo/ \t',
555 'and some more regular text') 553 'and some more regular text')
556 blat = join('foo', 'blat.cc')
557 readme = join('foo', 'blat', 'READ_ME2')
558 binary = join('foo', 'blat', 'binary.dll')
559 weird = join('foo', 'blat', 'weird.xyz')
560 third_party = join('foo', 'third_party', 'third.cc')
561 another = join('foo', 'blat', 'another.h')
562 beingdeleted = join('foo', 'mat', 'beingdeleted.txt')
563 notfound = join('flop', 'notfound.txt')
564 flap = join('boo', 'flap.h')
565 files = [ 554 files = [
566 ['A', blat], 555 ['A', join('foo', 'blat.cc')],
567 ['M', readme], 556 ['M', join('foo', 'blat', 'READ_ME2')],
568 ['M', binary], 557 ['M', join('foo', 'blat', 'binary.dll')],
569 ['M', weird], 558 ['M', join('foo', 'blat', 'weird.xyz')],
570 ['M', another], 559 ['M', join('foo', 'blat', 'another.h')],
571 ['M', third_party], 560 ['M', join('foo', 'third_party', 'third.cc')],
572 ['D', 'foo/mat/beingdeleted.txt'], 561 ['D', 'foo/mat/beingdeleted.txt'],
573 ['M', 'flop/notfound.txt'], 562 ['M', 'flop/notfound.txt'],
574 ['A', 'boo/flap.h'], 563 ['A', 'boo/flap.h'],
575 ] 564 ]
576 565 blat = presubmit.normpath(join(self.fake_root_dir, files[0][1]))
566 readme = presubmit.normpath(join(self.fake_root_dir, files[1][1]))
567 binary = presubmit.normpath(join(self.fake_root_dir, files[2][1]))
568 weird = presubmit.normpath(join(self.fake_root_dir, files[3][1]))
569 another = presubmit.normpath(join(self.fake_root_dir, files[4][1]))
570 third_party = presubmit.normpath(join(self.fake_root_dir, files[5][1]))
571 beingdeleted = presubmit.normpath(join(self.fake_root_dir, files[6][1]))
572 notfound = presubmit.normpath(join(self.fake_root_dir, files[7][1]))
573 flap = presubmit.normpath(join(self.fake_root_dir, files[8][1]))
577 for i in (blat, readme, binary, weird, another, third_party): 574 for i in (blat, readme, binary, weird, another, third_party):
578 presubmit.os.path.exists(i).AndReturn(True) 575 presubmit.os.path.exists(i).AndReturn(True)
579 presubmit.os.path.isdir(i).AndReturn(False) 576 presubmit.os.path.isdir(i).AndReturn(False)
580 presubmit.os.path.exists(beingdeleted).AndReturn(False) 577 presubmit.os.path.exists(beingdeleted).AndReturn(False)
581 presubmit.os.path.exists(notfound).AndReturn(False) 578 presubmit.os.path.exists(notfound).AndReturn(False)
582 presubmit.os.path.exists(flap).AndReturn(True) 579 presubmit.os.path.exists(flap).AndReturn(True)
583 presubmit.os.path.isdir(flap).AndReturn(False) 580 presubmit.os.path.isdir(flap).AndReturn(False)
584 presubmit.gclient.CaptureSVNInfo(beingdeleted).AndReturn({}) 581 presubmit.gclient.CaptureSVNInfo(beingdeleted).AndReturn({})
585 presubmit.gclient.CaptureSVNInfo(notfound).AndReturn({}) 582 presubmit.gclient.CaptureSVNInfo(notfound).AndReturn({})
586 presubmit.gcl.GetSVNFileProperty(blat, 'svn:mime-type').AndReturn(None) 583 presubmit.gcl.GetSVNFileProperty(blat, 'svn:mime-type').AndReturn(None)
587 presubmit.gcl.GetSVNFileProperty(readme, 'svn:mime-type').AndReturn(None) 584 presubmit.gcl.GetSVNFileProperty(readme, 'svn:mime-type').AndReturn(None)
588 presubmit.gcl.GetSVNFileProperty(binary, 'svn:mime-type').AndReturn( 585 presubmit.gcl.GetSVNFileProperty(binary, 'svn:mime-type').AndReturn(
589 'application/octet-stream') 586 'application/octet-stream')
590 presubmit.gcl.GetSVNFileProperty(weird, 'svn:mime-type').AndReturn(None) 587 presubmit.gcl.GetSVNFileProperty(weird, 'svn:mime-type').AndReturn(None)
591 presubmit.gcl.GetSVNFileProperty(another, 'svn:mime-type').AndReturn(None) 588 presubmit.gcl.GetSVNFileProperty(another, 'svn:mime-type').AndReturn(None)
592 presubmit.gcl.GetSVNFileProperty(third_party, 'svn:mime-type' 589 presubmit.gcl.GetSVNFileProperty(third_party, 'svn:mime-type'
593 ).AndReturn(None) 590 ).AndReturn(None)
594 presubmit.gcl.ReadFile(blat).AndReturn('whatever\ncookie') 591 presubmit.gcl.ReadFile(blat).AndReturn('whatever\ncookie')
595 presubmit.gcl.ReadFile(another).AndReturn('whatever\ncookie2') 592 presubmit.gcl.ReadFile(another).AndReturn('whatever\ncookie2')
593 ci = self.MakeChangeInfo('mychange', 0, 0, '\n'.join(description_lines))
594 ci.GetLocalRoot().AndReturn(self.fake_root_dir)
595 ci.GetFiles().AndReturn(files)
596 self.mox.ReplayAll() 596 self.mox.ReplayAll()
597 597
598 ci = presubmit.gcl.ChangeInfo(name='mychange', issue=0, patchset=0,
599 description='\n'.join(description_lines),
600 files=files)
601 change = presubmit.GclChange(ci) 598 change = presubmit.GclChange(ci)
602 input_api = presubmit.InputApi(change, 'foo/PRESUBMIT.py', False) 599 input_api = presubmit.InputApi(change,
600 join(self.fake_root_dir, 'foo',
601 'PRESUBMIT.py'),
602 False)
603 # Doesn't filter much 603 # Doesn't filter much
604 got_files = input_api.AffectedFiles() 604 got_files = input_api.AffectedFiles()
605 self.assertEquals(len(got_files), 7) 605 self.assertEquals(len(got_files), 7)
606 self.assertEquals(got_files[0].LocalPath(), presubmit.normpath(blat)) 606 self.assertEquals(got_files[0].LocalPath(), presubmit.normpath(files[0][1]))
607 self.assertEquals(got_files[1].LocalPath(), presubmit.normpath(readme)) 607 self.assertEquals(got_files[1].LocalPath(), presubmit.normpath(files[1][1]))
608 self.assertEquals(got_files[2].LocalPath(), presubmit.normpath(binary)) 608 self.assertEquals(got_files[2].LocalPath(), presubmit.normpath(files[2][1]))
609 self.assertEquals(got_files[3].LocalPath(), presubmit.normpath(weird)) 609 self.assertEquals(got_files[3].LocalPath(), presubmit.normpath(files[3][1]))
610 self.assertEquals(got_files[4].LocalPath(), presubmit.normpath(another)) 610 self.assertEquals(got_files[4].LocalPath(), presubmit.normpath(files[4][1]))
611 self.assertEquals(got_files[5].LocalPath(), presubmit.normpath(third_party)) 611 self.assertEquals(got_files[5].LocalPath(), presubmit.normpath(files[5][1]))
612 self.assertEquals(got_files[6].LocalPath(), 612 self.assertEquals(got_files[6].LocalPath(), presubmit.normpath(files[6][1]))
613 presubmit.normpath(beingdeleted))
614 # Ignores weird because of whitelist, third_party because of blacklist, 613 # Ignores weird because of whitelist, third_party because of blacklist,
615 # binary isn't a text file and beingdeleted doesn't exist. The rest is 614 # binary isn't a text file and beingdeleted doesn't exist. The rest is
616 # outside foo/. 615 # outside foo/.
617 rhs_lines = [x for x in input_api.RightHandSideLines(None)] 616 rhs_lines = [x for x in input_api.RightHandSideLines(None)]
618 self.assertEquals(len(rhs_lines), 4) 617 self.assertEquals(len(rhs_lines), 4)
619 self.assertEqual(rhs_lines[0][0].LocalPath(), presubmit.normpath(blat)) 618 self.assertEqual(rhs_lines[0][0].LocalPath(),
620 self.assertEqual(rhs_lines[1][0].LocalPath(), presubmit.normpath(blat)) 619 presubmit.normpath(files[0][1]))
621 self.assertEqual(rhs_lines[2][0].LocalPath(), presubmit.normpath(another)) 620 self.assertEqual(rhs_lines[1][0].LocalPath(),
622 self.assertEqual(rhs_lines[3][0].LocalPath(), presubmit.normpath(another)) 621 presubmit.normpath(files[0][1]))
622 self.assertEqual(rhs_lines[2][0].LocalPath(),
623 presubmit.normpath(files[4][1]))
624 self.assertEqual(rhs_lines[3][0].LocalPath(),
625 presubmit.normpath(files[4][1]))
623 626
624 def testDefaultWhiteListBlackListFilters(self): 627 def testDefaultWhiteListBlackListFilters(self):
625 def f(x): 628 def f(x):
626 return presubmit.AffectedFile(x, 'M') 629 return presubmit.AffectedFile(x, 'M')
627 files = [ 630 files = [
628 ( 631 (
629 [ 632 [
630 # To be tested. 633 # To be tested.
631 f('a/experimental/b'), 634 f('a/experimental/b'),
632 f('experimental/b'), 635 f('experimental/b'),
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
690 self.assertEquals(results[i].LocalPath(), 693 self.assertEquals(results[i].LocalPath(),
691 presubmit.normpath(item[1][i])) 694 presubmit.normpath(item[1][i]))
692 # Same number of expected results. 695 # Same number of expected results.
693 self.assertEquals(len(results), len(item[1])) 696 self.assertEquals(len(results), len(item[1]))
694 697
695 def testCustomFilter(self): 698 def testCustomFilter(self):
696 def FilterSourceFile(affected_file): 699 def FilterSourceFile(affected_file):
697 return 'a' in affected_file.LocalPath() 700 return 'a' in affected_file.LocalPath()
698 files = [('A', 'eeaee'), ('M', 'eeabee'), ('M', 'eebcee')] 701 files = [('A', 'eeaee'), ('M', 'eeabee'), ('M', 'eebcee')]
699 for (action, item) in files: 702 for (action, item) in files:
703 item = presubmit.os.path.join(self.fake_root_dir, item)
700 presubmit.os.path.exists(item).AndReturn(True) 704 presubmit.os.path.exists(item).AndReturn(True)
701 presubmit.os.path.isdir(item).AndReturn(False) 705 presubmit.os.path.isdir(item).AndReturn(False)
702 presubmit.gcl.GetSVNFileProperty(item, 'svn:mime-type').AndReturn(None) 706 presubmit.gcl.GetSVNFileProperty(item, 'svn:mime-type').AndReturn(None)
707 ci = self.MakeChangeInfo('mychange', 0, 0, '')
708 ci.GetLocalRoot().AndReturn(self.fake_root_dir)
709 ci.GetFiles().AndReturn(files)
703 self.mox.ReplayAll() 710 self.mox.ReplayAll()
704 711
705 ci = presubmit.gcl.ChangeInfo('mychange', 0, 0, '', files)
706 change = presubmit.GclChange(ci) 712 change = presubmit.GclChange(ci)
707 input_api = presubmit.InputApi(change, './PRESUBMIT.py', False) 713 input_api = presubmit.InputApi(change,
714 presubmit.os.path.join(self.fake_root_dir,
715 'PRESUBMIT.py'),
716 False)
708 got_files = input_api.AffectedSourceFiles(FilterSourceFile) 717 got_files = input_api.AffectedSourceFiles(FilterSourceFile)
709 self.assertEquals(len(got_files), 2) 718 self.assertEquals(len(got_files), 2)
710 self.assertEquals(got_files[0].LocalPath(), 'eeaee') 719 self.assertEquals(got_files[0].LocalPath(), 'eeaee')
711 self.assertEquals(got_files[1].LocalPath(), 'eeabee') 720 self.assertEquals(got_files[1].LocalPath(), 'eeabee')
712 721
713 def testLambdaFilter(self): 722 def testLambdaFilter(self):
714 white_list = presubmit.InputApi.DEFAULT_BLACK_LIST + (r".*?a.*?",) 723 white_list = presubmit.InputApi.DEFAULT_BLACK_LIST + (r".*?a.*?",)
715 black_list = [r".*?b.*?"] 724 black_list = [r".*?b.*?"]
716 files = [('A', 'eeaee'), ('M', 'eeabee'), ('M', 'eebcee'), ('M', 'eecaee')] 725 files = [('A', 'eeaee'), ('M', 'eeabee'), ('M', 'eebcee'), ('M', 'eecaee')]
717 for (action, item) in files: 726 for (action, item) in files:
727 item = presubmit.os.path.join(self.fake_root_dir, item)
718 presubmit.os.path.exists(item).AndReturn(True) 728 presubmit.os.path.exists(item).AndReturn(True)
719 presubmit.os.path.isdir(item).AndReturn(False) 729 presubmit.os.path.isdir(item).AndReturn(False)
720 presubmit.gcl.GetSVNFileProperty(item, 'svn:mime-type').AndReturn(None) 730 presubmit.gcl.GetSVNFileProperty(item, 'svn:mime-type').AndReturn(None)
731 ci = self.MakeChangeInfo('mychange', 0, 0, '')
732 ci.GetLocalRoot().AndReturn(self.fake_root_dir)
733 ci.GetFiles().AndReturn(files)
721 self.mox.ReplayAll() 734 self.mox.ReplayAll()
722 735
723 ci = presubmit.gcl.ChangeInfo('mychange', 0, 0, '', files)
724 change = presubmit.GclChange(ci) 736 change = presubmit.GclChange(ci)
725 input_api = presubmit.InputApi(change, './PRESUBMIT.py', False) 737 input_api = presubmit.InputApi(change, './PRESUBMIT.py', False)
726 # Sample usage of overiding the default white and black lists. 738 # Sample usage of overiding the default white and black lists.
727 got_files = input_api.AffectedSourceFiles( 739 got_files = input_api.AffectedSourceFiles(
728 lambda x: input_api.FilterSourceFile(x, white_list, black_list)) 740 lambda x: input_api.FilterSourceFile(x, white_list, black_list))
729 self.assertEquals(len(got_files), 2) 741 self.assertEquals(len(got_files), 2)
730 self.assertEquals(got_files[0].LocalPath(), 'eeaee') 742 self.assertEquals(got_files[0].LocalPath(), 'eeaee')
731 self.assertEquals(got_files[1].LocalPath(), 'eecaee') 743 self.assertEquals(got_files[1].LocalPath(), 'eecaee')
732 744
733 def testGetAbsoluteLocalPath(self): 745 def testGetAbsoluteLocalPath(self):
734 join = presubmit.os.path.join 746 join = presubmit.os.path.join
735 normpath = presubmit.normpath 747 normpath = presubmit.normpath
736 # Regression test for bug of presubmit stuff that relies on invoking 748 # Regression test for bug of presubmit stuff that relies on invoking
737 # SVN (e.g. to get mime type of file) not working unless gcl invoked 749 # SVN (e.g. to get mime type of file) not working unless gcl invoked
738 # from the client root (e.g. if you were at 'src' and did 'cd base' before 750 # from the client root (e.g. if you were at 'src' and did 'cd base' before
739 # invoking 'gcl upload' it would fail because svn wouldn't find the files 751 # invoking 'gcl upload' it would fail because svn wouldn't find the files
740 # the presubmit script was asking about). 752 # the presubmit script was asking about).
741 files = [ 753 files = [
742 ['A', 'isdir'], 754 ['A', 'isdir'],
743 ['A', join('isdir', 'blat.cc')], 755 ['A', join('isdir', 'blat.cc')],
744 ['M', join('elsewhere', 'ouf.cc')], 756 ['M', join('elsewhere', 'ouf.cc')],
745 ] 757 ]
758 ci = self.MakeChangeInfo('mychange', 0, 0, '')
759 ci.GetLocalRoot().AndReturn(self.fake_root_dir)
760 ci.GetFiles().AndReturn(files)
746 self.mox.ReplayAll() 761 self.mox.ReplayAll()
747 762
748 ci = presubmit.gcl.ChangeInfo(name='mychange', issue=0, patchset=0,
749 description='', files=files)
750 # It doesn't make sense on non-Windows platform. This is somewhat hacky, 763 # It doesn't make sense on non-Windows platform. This is somewhat hacky,
751 # but it is needed since we can't just use os.path.join('c:', 'temp'). 764 # but it is needed since we can't just use os.path.join('c:', 'temp').
752 change = presubmit.GclChange(ci, self.fake_root_dir) 765 change = presubmit.GclChange(ci)
753 affected_files = change.AffectedFiles(include_dirs=True) 766 affected_files = change.AffectedFiles(include_dirs=True)
754 # Local paths should remain the same 767 # Local paths should remain the same
755 self.assertEquals(affected_files[0].LocalPath(), normpath('isdir')) 768 self.assertEquals(affected_files[0].LocalPath(), normpath('isdir'))
756 self.assertEquals(affected_files[1].LocalPath(), normpath('isdir/blat.cc')) 769 self.assertEquals(affected_files[1].LocalPath(), normpath('isdir/blat.cc'))
757 # Absolute paths should be prefixed 770 # Absolute paths should be prefixed
758 self.assertEquals(affected_files[0].AbsoluteLocalPath(), 771 self.assertEquals(affected_files[0].AbsoluteLocalPath(),
759 normpath(join(self.fake_root_dir, 'isdir'))) 772 normpath(join(self.fake_root_dir, 'isdir')))
760 self.assertEquals(affected_files[1].AbsoluteLocalPath(), 773 self.assertEquals(affected_files[1].AbsoluteLocalPath(),
761 normpath(join(self.fake_root_dir, 'isdir/blat.cc'))) 774 normpath(join(self.fake_root_dir, 'isdir/blat.cc')))
762 775
763 # New helper functions need to work 776 # New helper functions need to work
764 paths_from_change = change.AbsoluteLocalPaths(include_dirs=True) 777 paths_from_change = change.AbsoluteLocalPaths(include_dirs=True)
765 self.assertEqual(len(paths_from_change), 3) 778 self.assertEqual(len(paths_from_change), 3)
766 presubmit_path = join(self.fake_root_dir, 'isdir', 'PRESUBMIT.py') 779 presubmit_path = join(self.fake_root_dir, 'isdir', 'PRESUBMIT.py')
767 api = presubmit.InputApi(change=change, 780 api = presubmit.InputApi(change=change,
768 presubmit_path=presubmit_path, 781 presubmit_path=presubmit_path,
769 is_committing=True) 782 is_committing=True)
770 paths_from_api = api.AbsoluteLocalPaths(include_dirs=True) 783 paths_from_api = api.AbsoluteLocalPaths(include_dirs=True)
771 self.assertEqual(len(paths_from_api), 2) 784 self.assertEqual(len(paths_from_api), 2)
772 for absolute_paths in [paths_from_change, paths_from_api]: 785 for absolute_paths in [paths_from_change, paths_from_api]:
773 self.assertEqual(absolute_paths[0], 786 self.assertEqual(absolute_paths[0],
774 normpath(join(self.fake_root_dir, 'isdir'))) 787 normpath(join(self.fake_root_dir, 'isdir')))
775 self.assertEqual(absolute_paths[1], 788 self.assertEqual(absolute_paths[1],
776 normpath(join(self.fake_root_dir, 'isdir', 'blat.cc'))) 789 normpath(join(self.fake_root_dir, 'isdir', 'blat.cc')))
777 790
778 def testDeprecated(self): 791 def testDeprecated(self):
779 presubmit.warnings.warn(mox.IgnoreArg(), category=mox.IgnoreArg(), 792 presubmit.warnings.warn(mox.IgnoreArg(), category=mox.IgnoreArg(),
780 stacklevel=2) 793 stacklevel=2)
794 ci = self.MakeChangeInfo('mychange', 0, 0, 'Bleh\n')
795 ci.GetLocalRoot().AndReturn(self.fake_root_dir)
796 ci.GetFiles().AndReturn([])
781 self.mox.ReplayAll() 797 self.mox.ReplayAll()
782 change = presubmit.GclChange( 798
783 presubmit.gcl.ChangeInfo(name='mychange', issue=0, patchset=0, 799 change = presubmit.GclChange(ci)
784 description='Bleh\n', files=None)) 800 api = presubmit.InputApi(
785 api = presubmit.InputApi(change, 'foo/PRESUBMIT.py', True) 801 change,
802 presubmit.os.path.join(self.fake_root_dir, 'foo', 'PRESUBMIT.py'), True)
786 api.AffectedTextFiles(include_deletes=False) 803 api.AffectedTextFiles(include_deletes=False)
787 804
788 def testReadFileStringDenied(self): 805 def testReadFileStringDenied(self):
806 ci = self.MakeChangeInfo('foo', 0, 0, 'Foo\n')
807 ci.GetLocalRoot().AndReturn(self.fake_root_dir)
808 ci.GetFiles().AndReturn([('M', 'AA')])
789 self.mox.ReplayAll() 809 self.mox.ReplayAll()
790 input_api = presubmit.InputApi(None, './p', False) 810
791 input_api.change = self.MakeBasicChange('foo', 'Foo\n', '/AA') 811 input_api = presubmit.InputApi(
812 None, presubmit.os.path.join(self.fake_root_dir, '/p'), False)
813 input_api.change = presubmit.GclChange(ci)
792 self.assertRaises(IOError, input_api.ReadFile, 'boo', 'x') 814 self.assertRaises(IOError, input_api.ReadFile, 'boo', 'x')
793 815
794 def testReadFileStringAccepted(self): 816 def testReadFileStringAccepted(self):
795 presubmit.gcl.ReadFile('/AA/boo', 'x').AndReturn(None) 817 ci = self.MakeChangeInfo('foo', 0, 0, 'Foo\n')
818 ci.GetLocalRoot().AndReturn(self.fake_root_dir)
819 path = presubmit.os.path.join(self.fake_root_dir, 'AA/boo')
820 ci.GetFiles().AndReturn([('M', 'AA')])
821 presubmit.gcl.ReadFile(path, 'x').AndReturn(None)
796 self.mox.ReplayAll() 822 self.mox.ReplayAll()
797 input_api = presubmit.InputApi(None, './p', False) 823
798 input_api.change = self.MakeBasicChange('foo', 'Foo\n', '/AA') 824 input_api = presubmit.InputApi(
799 input_api.ReadFile('/AA/boo', 'x') 825 None, presubmit.os.path.join(self.fake_root_dir, '/p'), False)
826 input_api.change = presubmit.GclChange(ci)
827 input_api.ReadFile(path, 'x')
800 828
801 def testReadFileAffectedFileDenied(self): 829 def testReadFileAffectedFileDenied(self):
802 file = presubmit.AffectedFile('boo', 'M') 830 ci = self.MakeChangeInfo('foo', 0, 0, 'Foo\n')
831 ci.GetLocalRoot().AndReturn(self.fake_root_dir)
832 ci.GetFiles().AndReturn([('M', 'AA')])
833 file = presubmit.AffectedFile('boo', 'M', 'Unrelated')
803 self.mox.ReplayAll() 834 self.mox.ReplayAll()
804 input_api = presubmit.InputApi(None, './p', False) 835
805 input_api.change = self.MakeBasicChange('foo', 'Foo\n', '/AA') 836 input_api = presubmit.InputApi(
806 self.assertRaises(IOError, input_api.ReadFile, 'boo', 'x') 837 None, presubmit.os.path.join(self.fake_root_dir, '/p'), False)
838 input_api.change = presubmit.GclChange(ci)
839 self.assertRaises(IOError, input_api.ReadFile, file, 'x')
807 840
808 def testReadFileAffectedFileAccepted(self): 841 def testReadFileAffectedFileAccepted(self):
809 file = presubmit.AffectedFile('/AA/boo', 'M') 842 ci = self.MakeChangeInfo('foo', 0, 0, 'Foo\n')
810 presubmit.gcl.ReadFile('/AA/boo', 'x').AndReturn(None) 843 ci.GetLocalRoot().AndReturn(self.fake_root_dir)
844 ci.GetFiles().AndReturn([('M', 'AA')])
845 file = presubmit.AffectedFile('AA/boo', 'M', self.fake_root_dir)
846 presubmit.gcl.ReadFile(file.AbsoluteLocalPath(), 'x').AndReturn(None)
811 self.mox.ReplayAll() 847 self.mox.ReplayAll()
812 input_api = presubmit.InputApi(None, './p', False) 848
813 input_api.change = self.MakeBasicChange('foo', 'Foo\n', '/AA') 849 input_api = presubmit.InputApi(
814 input_api.ReadFile('/AA/boo', 'x') 850 None, presubmit.os.path.join(self.fake_root_dir, '/p'), False)
851 input_api.change = presubmit.GclChange(ci)
852 input_api.ReadFile(file, 'x')
815 853
816 854
817 class OuputApiUnittest(PresubmitTestsBase): 855 class OuputApiUnittest(PresubmitTestsBase):
818 """Tests presubmit.OutputApi.""" 856 """Tests presubmit.OutputApi."""
819 def testMembersChanged(self): 857 def testMembersChanged(self):
820 self.mox.ReplayAll() 858 self.mox.ReplayAll()
821 members = [ 859 members = [
822 'MailTextResult', 'PresubmitError', 'PresubmitNotifyResult', 860 'MailTextResult', 'PresubmitError', 'PresubmitNotifyResult',
823 'PresubmitPromptWarning', 'PresubmitResult', 861 'PresubmitPromptWarning', 'PresubmitResult',
824 ] 862 ]
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
943 ).AndReturn('application/octet-stream') 981 ).AndReturn('application/octet-stream')
944 self.mox.ReplayAll() 982 self.mox.ReplayAll()
945 983
946 output = filter(lambda x: x.IsTextFile(), list) 984 output = filter(lambda x: x.IsTextFile(), list)
947 self.failUnless(len(output) == 1) 985 self.failUnless(len(output) == 1)
948 self.failUnless(list[0] == output[0]) 986 self.failUnless(list[0] == output[0])
949 987
950 988
951 class GclChangeUnittest(PresubmitTestsBase): 989 class GclChangeUnittest(PresubmitTestsBase):
952 def testMembersChanged(self): 990 def testMembersChanged(self):
953 self.mox.ReplayAll()
954 members = [ 991 members = [
955 'AbsoluteLocalPaths', 'AffectedFiles', 'AffectedTextFiles', 992 'AbsoluteLocalPaths', 'AffectedFiles', 'AffectedTextFiles',
956 'DescriptionText', 'FullDescriptionText', 'LocalPaths', 'Name', 993 'DescriptionText', 'FullDescriptionText', 'LocalPaths', 'Name',
957 'RepositoryRoot', 'RightHandSideLines', 'ServerPaths', 994 'RepositoryRoot', 'RightHandSideLines', 'ServerPaths',
958 'issue', 'patchset', 'tags', 995 'issue', 'patchset', 'tags',
959 ] 996 ]
960 # If this test fails, you should add the relevant test. 997 # If this test fails, you should add the relevant test.
961 ci = presubmit.gcl.ChangeInfo('', 0, 0, '', None) 998 ci = self.MakeChangeInfo('', 0, 0, '')
962 self.compareMembers(presubmit.GclChange(ci, self.fake_root_dir), members) 999 ci.GetLocalRoot().AndReturn(self.fake_root_dir)
1000 ci.GetFiles().AndReturn([])
1001 self.mox.ReplayAll()
1002
1003 self.compareMembers(presubmit.GclChange(ci), members)
963 1004
964 1005
965 class CannedChecksUnittest(PresubmitTestsBase): 1006 class CannedChecksUnittest(PresubmitTestsBase):
966 """Tests presubmit_canned_checks.py.""" 1007 """Tests presubmit_canned_checks.py."""
967 1008
968 def setUp(self): 1009 def setUp(self):
969 PresubmitTestsBase.setUp(self) 1010 PresubmitTestsBase.setUp(self)
970 self.mox.StubOutWithMock(presubmit_canned_checks, 1011 self.mox.StubOutWithMock(presubmit_canned_checks,
971 '_RunPythonUnitTests_LoadTests') 1012 '_RunPythonUnitTests_LoadTests')
972 1013
(...skipping 19 matching lines...) Expand all
992 'CheckDoNotSubmitInDescription', 'CheckDoNotSubmitInFiles', 1033 'CheckDoNotSubmitInDescription', 'CheckDoNotSubmitInFiles',
993 'CheckLongLines', 'CheckTreeIsOpen', 'RunPythonUnitTests', 1034 'CheckLongLines', 'CheckTreeIsOpen', 'RunPythonUnitTests',
994 ] 1035 ]
995 # If this test fails, you should add the relevant test. 1036 # If this test fails, you should add the relevant test.
996 self.compareMembers(presubmit_canned_checks, members) 1037 self.compareMembers(presubmit_canned_checks, members)
997 1038
998 def DescriptionTest(self, check, description1, description2, error_type, 1039 def DescriptionTest(self, check, description1, description2, error_type,
999 committing): 1040 committing):
1000 input_api1 = self.MockInputApi() 1041 input_api1 = self.MockInputApi()
1001 input_api1.is_committing = committing 1042 input_api1.is_committing = committing
1002 input_api1.change = self.MakeBasicChange('foo', description1) 1043 ci1 = self.MakeChangeInfo('foo', 0, 0, description1)
1044 ci1.GetLocalRoot().AndReturn(self.fake_root_dir)
1045 ci1.GetFiles().AndReturn([])
1003 input_api2 = self.MockInputApi() 1046 input_api2 = self.MockInputApi()
1004 input_api2.is_committing = committing 1047 input_api2.is_committing = committing
1005 input_api2.change = self.MakeBasicChange('foo', description2) 1048 ci2 = self.MakeChangeInfo('foo', 0, 0, description2)
1049 ci2.GetLocalRoot().AndReturn(self.fake_root_dir)
1050 ci2.GetFiles().AndReturn([])
1006 self.mox.ReplayAll() 1051 self.mox.ReplayAll()
1007 1052
1053 input_api1.change = presubmit.GclChange(ci1)
1054 input_api2.change = presubmit.GclChange(ci2)
1008 results1 = check(input_api1, presubmit.OutputApi) 1055 results1 = check(input_api1, presubmit.OutputApi)
1009 self.assertEquals(results1, []) 1056 self.assertEquals(results1, [])
1010 results2 = check(input_api2, presubmit.OutputApi) 1057 results2 = check(input_api2, presubmit.OutputApi)
1011 self.assertEquals(len(results2), 1) 1058 self.assertEquals(len(results2), 1)
1012 self.assertEquals(results2[0].__class__, error_type) 1059 self.assertEquals(results2[0].__class__, error_type)
1013 1060
1014 def ContentTest(self, check, content1, content2, error_type): 1061 def ContentTest(self, check, content1, content2, error_type):
1015 input_api1 = self.MockInputApi() 1062 input_api1 = self.MockInputApi()
1016 input_api1.change = self.MakeBasicChange('foo', 'Foo\n') 1063 ci1 = self.MakeChangeInfo('foo', 0, 0, 'foo1\n')
1064 ci1.GetLocalRoot().AndReturn(self.fake_root_dir)
1065 ci1.GetFiles().AndReturn([])
1017 affected_file = self.mox.CreateMock(presubmit.SvnAffectedFile) 1066 affected_file = self.mox.CreateMock(presubmit.SvnAffectedFile)
1018 affected_file.LocalPath().AndReturn('foo.cc') 1067 affected_file.LocalPath().AndReturn('foo.cc')
1019 output1 = [ 1068 output1 = [
1020 (affected_file, 42, 'yo, ' + content1), 1069 (affected_file, 42, 'yo, ' + content1),
1021 (affected_file, 43, 'yer'), 1070 (affected_file, 43, 'yer'),
1022 (affected_file, 23, 'ya'), 1071 (affected_file, 23, 'ya'),
1023 ] 1072 ]
1024 input_api1.RightHandSideLines(mox.IgnoreArg()).AndReturn(output1) 1073 input_api1.RightHandSideLines(mox.IgnoreArg()).AndReturn(output1)
1025 input_api2 = self.MockInputApi() 1074 input_api2 = self.MockInputApi()
1026 input_api2.change = self.MakeBasicChange('foo', 'Foo\n') 1075 ci2 = self.MakeChangeInfo('foo2', 0, 0, 'foo2\n')
1076 ci2.GetLocalRoot().AndReturn(self.fake_root_dir)
1077 ci2.GetFiles().AndReturn([])
1027 output2 = [ 1078 output2 = [
1028 (affected_file, 42, 'yo, ' + content2), 1079 (affected_file, 42, 'yo, ' + content2),
1029 (affected_file, 43, 'yer'), 1080 (affected_file, 43, 'yer'),
1030 (affected_file, 23, 'ya'), 1081 (affected_file, 23, 'ya'),
1031 ] 1082 ]
1032 input_api2.RightHandSideLines(mox.IgnoreArg()).AndReturn(output2) 1083 input_api2.RightHandSideLines(mox.IgnoreArg()).AndReturn(output2)
1033 self.mox.ReplayAll() 1084 self.mox.ReplayAll()
1034 1085
1086 input_api1.change = presubmit.GclChange(ci1)
1087 input_api2.change = presubmit.GclChange(ci2)
1035 results1 = check(input_api1, presubmit.OutputApi, None) 1088 results1 = check(input_api1, presubmit.OutputApi, None)
1036 self.assertEquals(results1, []) 1089 self.assertEquals(results1, [])
1037 results2 = check(input_api2, presubmit.OutputApi, None) 1090 results2 = check(input_api2, presubmit.OutputApi, None)
1038 self.assertEquals(len(results2), 1) 1091 self.assertEquals(len(results2), 1)
1039 self.assertEquals(results2[0].__class__, error_type) 1092 self.assertEquals(results2[0].__class__, error_type)
1040 1093
1041 def ReadFileTest(self, check, content1, content2, error_type): 1094 def ReadFileTest(self, check, content1, content2, error_type):
1042 input_api1 = self.MockInputApi() 1095 input_api1 = self.MockInputApi()
1043 self.mox.StubOutWithMock(input_api1, 'ReadFile') 1096 self.mox.StubOutWithMock(input_api1, 'ReadFile')
1044 input_api1.change = self.MakeBasicChange('foo', 'Foo\n') 1097 ci1 = self.MakeChangeInfo('foo', 0, 0, 'foo1\n')
1098 ci1.GetLocalRoot().AndReturn(self.fake_root_dir)
1099 ci1.GetFiles().AndReturn([])
1045 affected_file1 = self.mox.CreateMock(presubmit.SvnAffectedFile) 1100 affected_file1 = self.mox.CreateMock(presubmit.SvnAffectedFile)
1046 input_api1.AffectedSourceFiles(None).AndReturn([affected_file1]) 1101 input_api1.AffectedSourceFiles(None).AndReturn([affected_file1])
1047 input_api1.ReadFile(affected_file1, 'rb').AndReturn(content1) 1102 input_api1.ReadFile(affected_file1, 'rb').AndReturn(content1)
1048 input_api2 = self.MockInputApi() 1103 input_api2 = self.MockInputApi()
1049 self.mox.StubOutWithMock(input_api2, 'ReadFile') 1104 self.mox.StubOutWithMock(input_api2, 'ReadFile')
1050 input_api2.change = self.MakeBasicChange('foo', 'Foo\n') 1105 ci2 = self.MakeChangeInfo('foo2', 0, 0, 'foo2\n')
1106 ci2.GetLocalRoot().AndReturn(self.fake_root_dir)
1107 ci2.GetFiles().AndReturn([])
1051 affected_file2 = self.mox.CreateMock(presubmit.SvnAffectedFile) 1108 affected_file2 = self.mox.CreateMock(presubmit.SvnAffectedFile)
1052 input_api2.AffectedSourceFiles(None).AndReturn([affected_file2]) 1109 input_api2.AffectedSourceFiles(None).AndReturn([affected_file2])
1053 input_api2.ReadFile(affected_file2, 'rb').AndReturn(content2) 1110 input_api2.ReadFile(affected_file2, 'rb').AndReturn(content2)
1054 affected_file2.LocalPath().AndReturn('bar.cc') 1111 affected_file2.LocalPath().AndReturn('bar.cc')
1055 self.mox.ReplayAll() 1112 self.mox.ReplayAll()
1056 1113
1114 input_api1.change = presubmit.GclChange(ci1)
1115 input_api2.change = presubmit.GclChange(ci2)
1057 results = check(input_api1, presubmit.OutputApi) 1116 results = check(input_api1, presubmit.OutputApi)
1058 self.assertEquals(results, []) 1117 self.assertEquals(results, [])
1059 results2 = check(input_api2, presubmit.OutputApi) 1118 results2 = check(input_api2, presubmit.OutputApi)
1060 self.assertEquals(len(results2), 1) 1119 self.assertEquals(len(results2), 1)
1061 self.assertEquals(results2[0].__class__, error_type) 1120 self.assertEquals(results2[0].__class__, error_type)
1062 1121
1063 def SvnPropertyTest(self, check, property, value1, value2, committing, 1122 def SvnPropertyTest(self, check, property, value1, value2, committing,
1064 error_type): 1123 error_type):
1065 input_api1 = self.MockInputApi() 1124 input_api1 = self.MockInputApi()
1066 input_api1.is_committing = committing 1125 input_api1.is_committing = committing
(...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after
1352 test_result.errors = 0 1411 test_result.errors = 0
1353 self.mox.ReplayAll() 1412 self.mox.ReplayAll()
1354 1413
1355 results = presubmit_canned_checks.RunPythonUnitTests( 1414 results = presubmit_canned_checks.RunPythonUnitTests(
1356 input_api, presubmit.OutputApi, ['test_module']) 1415 input_api, presubmit.OutputApi, ['test_module'])
1357 self.assertEquals(len(results), 0) 1416 self.assertEquals(len(results), 0)
1358 1417
1359 1418
1360 if __name__ == '__main__': 1419 if __name__ == '__main__':
1361 unittest.main() 1420 unittest.main()
OLDNEW
« no previous file with comments | « tests/gcl_unittest.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698