| 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.py and presubmit_canned_checks.py.""" | 6 """Unit tests for presubmit.py and presubmit_canned_checks.py.""" |
| 7 | 7 |
| 8 import os | 8 import os |
| 9 import StringIO | 9 import StringIO |
| 10 import unittest | 10 import unittest |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 gcl.GetSVNFileProperty = MockGetSVNFileProperty | 53 gcl.GetSVNFileProperty = MockGetSVNFileProperty |
| 54 | 54 |
| 55 self.original_ReadFile = gcl.ReadFile | 55 self.original_ReadFile = gcl.ReadFile |
| 56 def MockReadFile(path): | 56 def MockReadFile(path): |
| 57 if path.count('nosuchfile'): | 57 if path.count('nosuchfile'): |
| 58 return None | 58 return None |
| 59 elif path.endswith('isdir'): | 59 elif path.endswith('isdir'): |
| 60 self.fail('Should not attempt to read file that is directory.') | 60 self.fail('Should not attempt to read file that is directory.') |
| 61 elif path.endswith('PRESUBMIT.py'): | 61 elif path.endswith('PRESUBMIT.py'): |
| 62 # used in testDoPresubmitChecks | 62 # used in testDoPresubmitChecks |
| 63 return """ | 63 return ('def CheckChangeOnUpload(input_api, output_api):\n' |
| 64 def CheckChangeOnUpload(input_api, output_api): | 64 ' if not input_api.change.NOSUCHKEY:\n' |
| 65 if not input_api.change.NOSUCHKEY: | 65 ' return [output_api.PresubmitError("!!")]\n' |
| 66 return [output_api.PresubmitError("!!")] | 66 ' elif not input_api.change.REALLYNOSUCHKEY:\n' |
| 67 elif not input_api.change.REALLYNOSUCHKEY: | 67 ' return [output_api.PresubmitPromptWarning("??")]\n' |
| 68 return [output_api.PresubmitPromptWarning("??")] | 68 ' elif not input_api.change.REALLYABSOLUTELYNOSUCHKEY:\n' |
| 69 elif not input_api.change.REALLYABSOLUTELYNOSUCHKEY: | 69 ' return [output_api.PresubmitPromptWarning("??"),\n' |
| 70 return [output_api.PresubmitPromptWarning("??"), | 70 ' output_api.PresubmitError("XX!!XX")]\n' |
| 71 output_api.PresubmitError("XX!!XX")] | 71 ' else:\n' |
| 72 else: | 72 ' return ()') |
| 73 return () | |
| 74 """ | |
| 75 else: | 73 else: |
| 76 return 'one:%s\r\ntwo:%s' % (path, path) | 74 return 'one:%s\r\ntwo:%s' % (path, path) |
| 77 gcl.ReadFile = MockReadFile | 75 gcl.ReadFile = MockReadFile |
| 78 | 76 |
| 79 self.original_GetRepositoryRoot = gcl.GetRepositoryRoot | 77 self.original_GetRepositoryRoot = gcl.GetRepositoryRoot |
| 80 def MockGetRepositoryRoot(): | 78 def MockGetRepositoryRoot(): |
| 81 return '' | 79 return '' |
| 82 gcl.GetRepositoryRoot = MockGetRepositoryRoot | 80 gcl.GetRepositoryRoot = MockGetRepositoryRoot |
| 83 | 81 |
| 84 def tearDown(self): | 82 def tearDown(self): |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 files = [ | 291 files = [ |
| 294 ['A', 'haspresubmit\\blat.cc'], | 292 ['A', 'haspresubmit\\blat.cc'], |
| 295 ] | 293 ] |
| 296 ci = gcl.ChangeInfo(name='mychange', | 294 ci = gcl.ChangeInfo(name='mychange', |
| 297 description='\n'.join(description_lines), | 295 description='\n'.join(description_lines), |
| 298 files=files) | 296 files=files) |
| 299 | 297 |
| 300 output = StringIO.StringIO() | 298 output = StringIO.StringIO() |
| 301 input = StringIO.StringIO('y\n') | 299 input = StringIO.StringIO('y\n') |
| 302 | 300 |
| 303 self.failIf(presubmit.DoPresubmitChecks(ci, False, False, output, input, | 301 self.failIf(presubmit.DoPresubmitChecks(ci, False, False, output, input)) |
| 304 None)) | 302 self.failUnless(output.getvalue().count('!!')) |
| 305 self.assertEqual(output.getvalue().count('!!'), 2) | |
| 306 | 303 |
| 307 def testDoPresubmitChecksPromptsAfterWarnings(self): | 304 def testDoPresubmitChecksPromptsAfterWarnings(self): |
| 308 description_lines = ('Hello there', | 305 description_lines = ('Hello there', |
| 309 'this is a change', | 306 'this is a change', |
| 310 'NOSUCHKEY=http://tracker/123') | 307 'NOSUCHKEY=http://tracker/123') |
| 311 files = [ | 308 files = [ |
| 312 ['A', 'haspresubmit\\blat.cc'], | 309 ['A', 'haspresubmit\\blat.cc'], |
| 313 ] | 310 ] |
| 314 ci = gcl.ChangeInfo(name='mychange', | 311 ci = gcl.ChangeInfo(name='mychange', |
| 315 description='\n'.join(description_lines), | 312 description='\n'.join(description_lines), |
| 316 files=files) | 313 files=files) |
| 317 | 314 |
| 318 output = StringIO.StringIO() | 315 output = StringIO.StringIO() |
| 319 input = StringIO.StringIO('n\n') # say no to the warning | 316 input = StringIO.StringIO('n\n') # say no to the warning |
| 320 | 317 |
| 321 self.failIf(presubmit.DoPresubmitChecks(ci, False, False, output, input, | 318 self.failIf(presubmit.DoPresubmitChecks(ci, False, False, output, input)) |
| 322 None)) | 319 self.failUnless(output.getvalue().count('??')) |
| 323 self.assertEqual(output.getvalue().count('??'), 2) | |
| 324 | 320 |
| 325 output = StringIO.StringIO() | 321 output = StringIO.StringIO() |
| 326 input = StringIO.StringIO('y\n') # say yes to the warning | 322 input = StringIO.StringIO('y\n') # say yes to the warning |
| 327 | 323 |
| 328 self.failUnless(presubmit.DoPresubmitChecks(ci, | 324 self.failUnless(presubmit.DoPresubmitChecks(ci, |
| 329 False, | 325 False, |
| 330 False, | 326 False, |
| 331 output, | 327 output, |
| 332 input, | 328 input)) |
| 333 None)) | |
| 334 self.failUnless(output.getvalue().count('??')) | 329 self.failUnless(output.getvalue().count('??')) |
| 335 | 330 |
| 336 def testDoPresubmitChecksNoWarningPromptIfErrors(self): | 331 def testDoPresubmitChecksNoWarningPromptIfErrors(self): |
| 337 description_lines = ('Hello there', | 332 description_lines = ('Hello there', |
| 338 'this is a change', | 333 'this is a change', |
| 339 'NOSUCHKEY=http://tracker/123', | 334 'NOSUCHKEY=http://tracker/123', |
| 340 'REALLYNOSUCHKEY=http://tracker/123') | 335 'REALLYNOSUCHKEY=http://tracker/123') |
| 341 files = [ | 336 files = [ |
| 342 ['A', 'haspresubmit\\blat.cc'], | 337 ['A', 'haspresubmit\\blat.cc'], |
| 343 ] | 338 ] |
| 344 ci = gcl.ChangeInfo(name='mychange', | 339 ci = gcl.ChangeInfo(name='mychange', |
| 345 description='\n'.join(description_lines), | 340 description='\n'.join(description_lines), |
| 346 files=files) | 341 files=files) |
| 347 | 342 |
| 348 output = StringIO.StringIO() | 343 output = StringIO.StringIO() |
| 349 input = StringIO.StringIO() # should be unused | 344 input = StringIO.StringIO() # should be unused |
| 350 | 345 |
| 351 self.failIf(presubmit.DoPresubmitChecks(ci, False, False, output, input, | 346 self.failIf(presubmit.DoPresubmitChecks(ci, False, False, output, input)) |
| 352 None)) | 347 self.failUnless(output.getvalue().count('??')) |
| 353 self.assertEqual(output.getvalue().count('??'), 2) | 348 self.failUnless(output.getvalue().count('XX!!XX')) |
| 354 self.assertEqual(output.getvalue().count('XX!!XX'), 2) | 349 self.failIf(output.getvalue().count('(y/N)')) |
| 355 self.assertEqual(output.getvalue().count('(y/N)'), 0) | |
| 356 | |
| 357 def testDoDefaultPresubmitChecks(self): | |
| 358 description_lines = ('Hello there', | |
| 359 'this is a change', | |
| 360 'STORY=http://tracker/123') | |
| 361 files = [ | |
| 362 ['A', 'haspresubmit\\blat.cc'], | |
| 363 ] | |
| 364 ci = gcl.ChangeInfo(name='mychange', | |
| 365 description='\n'.join(description_lines), | |
| 366 files=files) | |
| 367 | |
| 368 output = StringIO.StringIO() | |
| 369 input = StringIO.StringIO('y\n') | |
| 370 DEFAULT_SCRIPT = """ | |
| 371 def CheckChangeOnUpload(input_api, output_api): | |
| 372 return [output_api.PresubmitError("!!")] | |
| 373 """ | |
| 374 def MockReadFile(dummy): | |
| 375 return '' | |
| 376 gcl.ReadFile = MockReadFile | |
| 377 def MockIsFile(dummy): | |
| 378 return False | |
| 379 os.path.isfile = MockIsFile | |
| 380 self.failUnless(presubmit.DoPresubmitChecks(ci, False, False, output, input, | |
| 381 DEFAULT_SCRIPT)) | |
| 382 self.failIf(output.getvalue().count('!!') == 1) | |
| 383 | 350 |
| 384 def testDirectoryHandling(self): | 351 def testDirectoryHandling(self): |
| 385 files = [ | 352 files = [ |
| 386 ['A', 'isdir'], | 353 ['A', 'isdir'], |
| 387 ['A', 'isdir\\blat.cc'], | 354 ['A', 'isdir\\blat.cc'], |
| 388 ] | 355 ] |
| 389 ci = gcl.ChangeInfo(name='mychange', | 356 ci = gcl.ChangeInfo(name='mychange', |
| 390 description='foo', | 357 description='foo', |
| 391 files=files) | 358 files=files) |
| 392 change = presubmit.GclChange(ci) | 359 change = presubmit.GclChange(ci) |
| (...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 690 self.failIf(presubmit_canned_checks.CheckTreeIsOpen( | 657 self.failIf(presubmit_canned_checks.CheckTreeIsOpen( |
| 691 self.MockInputApi(), presubmit.OutputApi, url='url_to_open', closed='0' | 658 self.MockInputApi(), presubmit.OutputApi, url='url_to_open', closed='0' |
| 692 )) | 659 )) |
| 693 self.failUnless(presubmit_canned_checks.CheckTreeIsOpen( | 660 self.failUnless(presubmit_canned_checks.CheckTreeIsOpen( |
| 694 self.MockInputApi(), presubmit.OutputApi, url='url_to_closed', closed='0' | 661 self.MockInputApi(), presubmit.OutputApi, url='url_to_closed', closed='0' |
| 695 )) | 662 )) |
| 696 | 663 |
| 697 | 664 |
| 698 if __name__ == '__main__': | 665 if __name__ == '__main__': |
| 699 unittest.main() | 666 unittest.main() |
| OLD | NEW |