| 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 sys | 10 import sys |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 gcl.GetSVNFileProperty = MockGetSVNFileProperty | 55 gcl.GetSVNFileProperty = MockGetSVNFileProperty |
| 56 | 56 |
| 57 self.original_ReadFile = gcl.ReadFile | 57 self.original_ReadFile = gcl.ReadFile |
| 58 def MockReadFile(path, dummy='r'): | 58 def MockReadFile(path, dummy='r'): |
| 59 if path.count('nosuchfile'): | 59 if path.count('nosuchfile'): |
| 60 return None | 60 return None |
| 61 elif path.endswith('isdir'): | 61 elif path.endswith('isdir'): |
| 62 self.fail('Should not attempt to read file that is directory.') | 62 self.fail('Should not attempt to read file that is directory.') |
| 63 elif path.endswith('PRESUBMIT.py'): | 63 elif path.endswith('PRESUBMIT.py'): |
| 64 # used in testDoPresubmitChecks | 64 # used in testDoPresubmitChecks |
| 65 return ('def CheckChangeOnUpload(input_api, output_api):\n' | 65 return """ |
| 66 ' if not input_api.change.NOSUCHKEY:\n' | 66 def CheckChangeOnUpload(input_api, output_api): |
| 67 ' return [output_api.PresubmitError("!!")]\n' | 67 if not input_api.change.NOSUCHKEY: |
| 68 ' elif not input_api.change.REALLYNOSUCHKEY:\n' | 68 return [output_api.PresubmitError("!!")] |
| 69 ' return [output_api.PresubmitPromptWarning("??")]\n' | 69 elif not input_api.change.REALLYNOSUCHKEY: |
| 70 ' elif not input_api.change.REALLYABSOLUTELYNOSUCHKEY:\n' | 70 return [output_api.PresubmitPromptWarning("??")] |
| 71 ' return [output_api.PresubmitPromptWarning("??"),\n' | 71 elif not input_api.change.REALLYABSOLUTELYNOSUCHKEY: |
| 72 ' output_api.PresubmitError("XX!!XX")]\n' | 72 return [output_api.PresubmitPromptWarning("??"), |
| 73 ' else:\n' | 73 output_api.PresubmitError("XX!!XX")] |
| 74 ' return ()') | 74 else: |
| 75 return () |
| 76 """ |
| 75 else: | 77 else: |
| 76 return 'one:%s\r\ntwo:%s' % (path, path) | 78 return 'one:%s\r\ntwo:%s' % (path, path) |
| 77 gcl.ReadFile = MockReadFile | 79 gcl.ReadFile = MockReadFile |
| 78 | 80 |
| 79 self.original_GetRepositoryRoot = gcl.GetRepositoryRoot | 81 self.original_GetRepositoryRoot = gcl.GetRepositoryRoot |
| 80 def MockGetRepositoryRoot(): | 82 def MockGetRepositoryRoot(): |
| 81 return '' | 83 return '' |
| 82 gcl.GetRepositoryRoot = MockGetRepositoryRoot | 84 gcl.GetRepositoryRoot = MockGetRepositoryRoot |
| 83 self._sys_stdout = sys.stdout | 85 self._sys_stdout = sys.stdout |
| 84 sys.stdout = StringIO.StringIO() | 86 sys.stdout = StringIO.StringIO() |
| (...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 files = [ | 298 files = [ |
| 297 ['A', 'haspresubmit\\blat.cc'], | 299 ['A', 'haspresubmit\\blat.cc'], |
| 298 ] | 300 ] |
| 299 ci = gcl.ChangeInfo(name='mychange', | 301 ci = gcl.ChangeInfo(name='mychange', |
| 300 description='\n'.join(description_lines), | 302 description='\n'.join(description_lines), |
| 301 files=files) | 303 files=files) |
| 302 | 304 |
| 303 output = StringIO.StringIO() | 305 output = StringIO.StringIO() |
| 304 input = StringIO.StringIO('y\n') | 306 input = StringIO.StringIO('y\n') |
| 305 | 307 |
| 306 self.failIf(presubmit.DoPresubmitChecks(ci, False, True, output, input)) | 308 self.failIf(presubmit.DoPresubmitChecks(ci, False, True, output, input, |
| 307 self.failUnless(output.getvalue().count('!!')) | 309 None)) |
| 310 self.assertEqual(output.getvalue().count('!!'), 2) |
| 308 | 311 |
| 309 def testDoPresubmitChecksPromptsAfterWarnings(self): | 312 def testDoPresubmitChecksPromptsAfterWarnings(self): |
| 310 description_lines = ('Hello there', | 313 description_lines = ('Hello there', |
| 311 'this is a change', | 314 'this is a change', |
| 312 'NOSUCHKEY=http://tracker/123') | 315 'NOSUCHKEY=http://tracker/123') |
| 313 files = [ | 316 files = [ |
| 314 ['A', 'haspresubmit\\blat.cc'], | 317 ['A', 'haspresubmit\\blat.cc'], |
| 315 ] | 318 ] |
| 316 ci = gcl.ChangeInfo(name='mychange', | 319 ci = gcl.ChangeInfo(name='mychange', |
| 317 description='\n'.join(description_lines), | 320 description='\n'.join(description_lines), |
| 318 files=files) | 321 files=files) |
| 319 | 322 |
| 320 output = StringIO.StringIO() | 323 output = StringIO.StringIO() |
| 321 input = StringIO.StringIO('n\n') # say no to the warning | 324 input = StringIO.StringIO('n\n') # say no to the warning |
| 322 | 325 |
| 323 self.failIf(presubmit.DoPresubmitChecks(ci, False, True, output, input)) | 326 self.failIf(presubmit.DoPresubmitChecks(ci, False, True, output, input, |
| 324 self.failUnless(output.getvalue().count('??')) | 327 None)) |
| 328 self.assertEqual(output.getvalue().count('??'), 2) |
| 325 | 329 |
| 326 output = StringIO.StringIO() | 330 output = StringIO.StringIO() |
| 327 input = StringIO.StringIO('y\n') # say yes to the warning | 331 input = StringIO.StringIO('y\n') # say yes to the warning |
| 328 | 332 |
| 329 self.failUnless(presubmit.DoPresubmitChecks(ci, | 333 self.failUnless(presubmit.DoPresubmitChecks(ci, |
| 330 False, | 334 False, |
| 331 True, | 335 True, |
| 332 output, | 336 output, |
| 333 input)) | 337 input, |
| 338 None)) |
| 334 self.failUnless(output.getvalue().count('??')) | 339 self.failUnless(output.getvalue().count('??')) |
| 335 | 340 |
| 336 def testDoPresubmitChecksNoWarningPromptIfErrors(self): | 341 def testDoPresubmitChecksNoWarningPromptIfErrors(self): |
| 337 description_lines = ('Hello there', | 342 description_lines = ('Hello there', |
| 338 'this is a change', | 343 'this is a change', |
| 339 'NOSUCHKEY=http://tracker/123', | 344 'NOSUCHKEY=http://tracker/123', |
| 340 'REALLYNOSUCHKEY=http://tracker/123') | 345 'REALLYNOSUCHKEY=http://tracker/123') |
| 341 files = [ | 346 files = [ |
| 342 ['A', 'haspresubmit\\blat.cc'], | 347 ['A', 'haspresubmit\\blat.cc'], |
| 343 ] | 348 ] |
| 344 ci = gcl.ChangeInfo(name='mychange', | 349 ci = gcl.ChangeInfo(name='mychange', |
| 345 description='\n'.join(description_lines), | 350 description='\n'.join(description_lines), |
| 346 files=files) | 351 files=files) |
| 347 | 352 |
| 348 output = StringIO.StringIO() | 353 output = StringIO.StringIO() |
| 349 input = StringIO.StringIO() # should be unused | 354 input = StringIO.StringIO() # should be unused |
| 350 | 355 |
| 351 self.failIf(presubmit.DoPresubmitChecks(ci, False, True, output, input)) | 356 self.failIf(presubmit.DoPresubmitChecks(ci, False, True, output, input, |
| 352 self.failUnless(output.getvalue().count('??')) | 357 None)) |
| 353 self.failUnless(output.getvalue().count('XX!!XX')) | 358 self.assertEqual(output.getvalue().count('??'), 2) |
| 354 self.failIf(output.getvalue().count('(y/N)')) | 359 self.assertEqual(output.getvalue().count('XX!!XX'), 2) |
| 360 self.assertEqual(output.getvalue().count('(y/N)'), 0) |
| 361 |
| 362 def testDoDefaultPresubmitChecks(self): |
| 363 description_lines = ('Hello there', |
| 364 'this is a change', |
| 365 'STORY=http://tracker/123') |
| 366 files = [ |
| 367 ['A', 'haspresubmit\\blat.cc'], |
| 368 ] |
| 369 ci = gcl.ChangeInfo(name='mychange', |
| 370 description='\n'.join(description_lines), |
| 371 files=files) |
| 372 |
| 373 output = StringIO.StringIO() |
| 374 input = StringIO.StringIO('y\n') |
| 375 # Always fail. |
| 376 DEFAULT_SCRIPT = """ |
| 377 def CheckChangeOnUpload(input_api, output_api): |
| 378 print 'This is a test' |
| 379 return [output_api.PresubmitError("!!")] |
| 380 """ |
| 381 def MockReadFile(dummy): |
| 382 return '' |
| 383 gcl.ReadFile = MockReadFile |
| 384 def MockIsFile(dummy): |
| 385 return False |
| 386 os.path.isfile = MockIsFile |
| 387 self.failIf(presubmit.DoPresubmitChecks(ci, False, True, output, input, |
| 388 DEFAULT_SCRIPT)) |
| 389 self.assertEquals(output.getvalue().count('!!'), 1) |
| 355 | 390 |
| 356 def testDirectoryHandling(self): | 391 def testDirectoryHandling(self): |
| 357 files = [ | 392 files = [ |
| 358 ['A', 'isdir'], | 393 ['A', 'isdir'], |
| 359 ['A', 'isdir\\blat.cc'], | 394 ['A', 'isdir\\blat.cc'], |
| 360 ] | 395 ] |
| 361 ci = gcl.ChangeInfo(name='mychange', | 396 ci = gcl.ChangeInfo(name='mychange', |
| 362 description='foo', | 397 description='foo', |
| 363 files=files) | 398 files=files) |
| 364 change = presubmit.GclChange(ci) | 399 change = presubmit.GclChange(ci) |
| (...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 697 # TODO(maruel): Add real tests. | 732 # TODO(maruel): Add real tests. |
| 698 self.failIf(presubmit_canned_checks.RunPythonUnitTests( | 733 self.failIf(presubmit_canned_checks.RunPythonUnitTests( |
| 699 self.MockInputApi(), | 734 self.MockInputApi(), |
| 700 presubmit.OutputApi, [])) | 735 presubmit.OutputApi, [])) |
| 701 self.failUnless(presubmit_canned_checks.RunPythonUnitTests( | 736 self.failUnless(presubmit_canned_checks.RunPythonUnitTests( |
| 702 self.MockInputApi(), | 737 self.MockInputApi(), |
| 703 presubmit.OutputApi, ['non_existent_module'])) | 738 presubmit.OutputApi, ['non_existent_module'])) |
| 704 | 739 |
| 705 if __name__ == '__main__': | 740 if __name__ == '__main__': |
| 706 unittest.main() | 741 unittest.main() |
| OLD | NEW |