OLD | NEW |
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 os | 8 import os |
9 import StringIO | 9 import StringIO |
10 import sys | 10 import sys |
(...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
359 description='\n'.join(description_lines), | 359 description='\n'.join(description_lines), |
360 files=files) | 360 files=files) |
361 | 361 |
362 output = StringIO.StringIO() | 362 output = StringIO.StringIO() |
363 input = StringIO.StringIO('y\n') | 363 input = StringIO.StringIO('y\n') |
364 # Always fail. | 364 # Always fail. |
365 DEFAULT_SCRIPT = """ | 365 DEFAULT_SCRIPT = """ |
366 def CheckChangeOnUpload(input_api, output_api): | 366 def CheckChangeOnUpload(input_api, output_api): |
367 print 'This is a test' | 367 print 'This is a test' |
368 return [output_api.PresubmitError("!!")] | 368 return [output_api.PresubmitError("!!")] |
| 369 def CheckChangeOnCommit(input_api, output_api): |
| 370 raise Exception("Test error") |
369 """ | 371 """ |
370 def MockReadFile(dummy): | 372 def MockReadFile(dummy): |
371 return '' | 373 return '' |
372 gcl.ReadFile = MockReadFile | 374 gcl.ReadFile = MockReadFile |
373 def MockIsFile(dummy): | 375 def MockIsFile(dummy): |
374 return False | 376 return False |
375 os.path.isfile = MockIsFile | 377 os.path.isfile = MockIsFile |
376 self.failIf(presubmit.DoPresubmitChecks(ci, False, True, output, input, | 378 self.failIf(presubmit.DoPresubmitChecks(ci, False, True, output, input, |
377 DEFAULT_SCRIPT)) | 379 DEFAULT_SCRIPT)) |
378 self.assertEquals(output.getvalue().count('!!'), 1) | 380 self.assertEquals(output.getvalue().count('!!'), 1) |
379 | 381 |
380 def testDirectoryHandling(self): | 382 def testDirectoryHandling(self): |
381 files = [ | 383 files = [ |
382 ['A', 'isdir'], | 384 ['A', 'isdir'], |
383 ['A', 'isdir\\blat.cc'], | 385 ['A', 'isdir\\blat.cc'], |
384 ] | 386 ] |
385 ci = gcl.ChangeInfo(name='mychange', | 387 ci = gcl.ChangeInfo(name='mychange', |
386 description='foo', | 388 description='foo', |
387 files=files) | 389 files=files) |
388 change = presubmit.GclChange(ci) | 390 change = presubmit.GclChange(ci) |
389 | 391 |
390 affected_files = change.AffectedFiles(include_dirs=False) | 392 affected_files = change.AffectedFiles(include_dirs=False) |
391 self.failUnless(len(affected_files) == 1) | 393 self.failUnless(len(affected_files) == 1) |
392 self.failUnless(affected_files[0].LocalPath().endswith('blat.cc')) | 394 self.failUnless(affected_files[0].LocalPath().endswith('blat.cc')) |
393 | 395 |
394 affected_files_and_dirs = change.AffectedFiles(include_dirs=True) | 396 affected_files_and_dirs = change.AffectedFiles(include_dirs=True) |
395 self.failUnless(len(affected_files_and_dirs) == 2) | 397 self.failUnless(len(affected_files_and_dirs) == 2) |
396 | 398 |
| 399 def testTags(self): |
| 400 DEFAULT_SCRIPT = """ |
| 401 def CheckChangeOnUpload(input_api, output_api): |
| 402 if input_api.change.tags['BUG'] != 'boo': |
| 403 return [output_api.PresubmitError('Tag parsing failed. 1')] |
| 404 if input_api.change.tags['STORY'] != 'http://tracker.com/42': |
| 405 return [output_api.PresubmitError('Tag parsing failed. 2')] |
| 406 if 'TEST' in input_api.change.tags: |
| 407 return [output_api.PresubmitError('Tag parsing failed. 3')] |
| 408 if input_api.change.DescriptionText() != 'Blah Blah': |
| 409 return [output_api.PresubmitError('Tag parsing failed. 4 ' + |
| 410 input_api.change.DescriptionText())] |
| 411 if (input_api.change.FullDescriptionText() != |
| 412 'Blah Blah\\n\\nSTORY=http://tracker.com/42\\nBUG=boo\\n'): |
| 413 return [output_api.PresubmitError('Tag parsing failed. 5 ' + |
| 414 input_api.change.FullDescriptionText())] |
| 415 return [output_api.PresubmitNotifyResult(input_api.change.tags['STORY'])] |
| 416 def CheckChangeOnCommit(input_api, output_api): |
| 417 raise Exception("Test error") |
| 418 """ |
| 419 def MockReadFile(dummy): |
| 420 return '' |
| 421 gcl.ReadFile = MockReadFile |
| 422 def MockIsFile(dummy): |
| 423 return False |
| 424 os.path.isfile = MockIsFile |
| 425 change = gcl.ChangeInfo( |
| 426 name='foo', |
| 427 description="Blah Blah\n\nSTORY=http://tracker.com/42\nBUG=boo\n") |
| 428 output = StringIO.StringIO() |
| 429 input = StringIO.StringIO('y\n') |
| 430 self.failUnless(presubmit.DoPresubmitChecks(change, False, True, output, |
| 431 input, DEFAULT_SCRIPT)) |
| 432 self.assertEquals(output.getvalue(), |
| 433 ('Warning, no presubmit.py found.\n' |
| 434 'Running default presubmit script.\n\n' |
| 435 '** Presubmit Messages **\n\n' |
| 436 'http://tracker.com/42\n\n')) |
| 437 |
397 | 438 |
398 class InputApiUnittest(PresubmitTestsBase): | 439 class InputApiUnittest(PresubmitTestsBase): |
399 """Tests presubmit.InputApi.""" | 440 """Tests presubmit.InputApi.""" |
400 def testMembersChanged(self): | 441 def testMembersChanged(self): |
401 members = [ | 442 members = [ |
402 'AbsoluteLocalPaths', 'AffectedFiles', 'AffectedTextFiles', | 443 'AbsoluteLocalPaths', 'AffectedFiles', 'AffectedTextFiles', |
403 'DepotToLocalPath', 'FilterTextFiles', 'LocalPaths', 'LocalToDepotPath', | 444 'DepotToLocalPath', 'FilterTextFiles', 'LocalPaths', 'LocalToDepotPath', |
404 'PresubmitLocalPath', 'RightHandSideLines', 'ServerPaths', | 445 'PresubmitLocalPath', 'RightHandSideLines', 'ServerPaths', |
405 'basename', 'cPickle', 'cStringIO', 'canned_checks', 'change', | 446 'basename', 'cPickle', 'cStringIO', 'canned_checks', 'change', |
406 'marshal', 'os_path', 'pickle', 'platform', | 447 'marshal', 'os_path', 'pickle', 'platform', |
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
744 # TODO(maruel): Add real tests. | 785 # TODO(maruel): Add real tests. |
745 self.failIf(presubmit_canned_checks.RunPythonUnitTests( | 786 self.failIf(presubmit_canned_checks.RunPythonUnitTests( |
746 self.MockInputApi(), | 787 self.MockInputApi(), |
747 presubmit.OutputApi, [])) | 788 presubmit.OutputApi, [])) |
748 self.failUnless(presubmit_canned_checks.RunPythonUnitTests( | 789 self.failUnless(presubmit_canned_checks.RunPythonUnitTests( |
749 self.MockInputApi(), | 790 self.MockInputApi(), |
750 presubmit.OutputApi, ['non_existent_module'])) | 791 presubmit.OutputApi, ['non_existent_module'])) |
751 | 792 |
752 if __name__ == '__main__': | 793 if __name__ == '__main__': |
753 unittest.main() | 794 unittest.main() |
OLD | NEW |