| OLD | NEW |
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # Copyright (c) 2010 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2010 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 # pylint is too confused. | 8 # pylint is too confused. |
| 9 # pylint: disable=E1101,E1103,W0212,W0403 | 9 # pylint: disable=E1101,E1103,W0212,W0403 |
| 10 | 10 |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 | 131 |
| 132 _INHERIT_SETTINGS = 'inherit-review-settings-ok' | 132 _INHERIT_SETTINGS = 'inherit-review-settings-ok' |
| 133 | 133 |
| 134 def testMembersChanged(self): | 134 def testMembersChanged(self): |
| 135 self.mox.ReplayAll() | 135 self.mox.ReplayAll() |
| 136 members = [ | 136 members = [ |
| 137 'AffectedFile', 'Change', 'DoGetTrySlaves', 'DoPresubmitChecks', | 137 'AffectedFile', 'Change', 'DoGetTrySlaves', 'DoPresubmitChecks', |
| 138 'GetTrySlavesExecuter', 'GitAffectedFile', 'GitChange', | 138 'GetTrySlavesExecuter', 'GitAffectedFile', 'GitChange', |
| 139 'InputApi', 'ListRelevantPresubmitFiles', 'Main', | 139 'InputApi', 'ListRelevantPresubmitFiles', 'Main', |
| 140 'NotImplementedException', 'OutputApi', 'ParseFiles', | 140 'NotImplementedException', 'OutputApi', 'ParseFiles', |
| 141 'PresubmitExecuter', 'PromptYesNo', 'ScanSubDirs', | 141 'PresubmitExecuter', 'PresubmitOutput', 'ScanSubDirs', |
| 142 'SvnAffectedFile', 'SvnChange', 'cPickle', 'cStringIO', | 142 'SvnAffectedFile', 'SvnChange', 'cPickle', 'cStringIO', |
| 143 'exceptions', 'fnmatch', 'gclient_utils', 'glob', 'json', | 143 'exceptions', 'fnmatch', 'gclient_utils', 'glob', 'json', |
| 144 'logging', 'marshal', 'normpath', 'optparse', 'os', 'owners', 'pickle', | 144 'logging', 'marshal', 'normpath', 'optparse', 'os', 'owners', 'pickle', |
| 145 'presubmit_canned_checks', 'random', 're', 'scm', 'subprocess', | 145 'presubmit_canned_checks', 'random', 're', 'scm', 'subprocess', |
| 146 'sys', 'tempfile', 'time', 'traceback', 'types', 'unittest', 'urllib2', | 146 'sys', 'tempfile', 'time', 'traceback', 'types', 'unittest', 'urllib2', |
| 147 'warn', | 147 'warn', |
| 148 ] | 148 ] |
| 149 # If this test fails, you should add the relevant test. | 149 # If this test fails, you should add the relevant test. |
| 150 self.compareMembers(presubmit, members) | 150 self.compareMembers(presubmit, members) |
| 151 | 151 |
| (...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 404 presubmit.os.path.isfile(inherit_path).AndReturn(False) | 404 presubmit.os.path.isfile(inherit_path).AndReturn(False) |
| 405 presubmit.os.path.isfile(root_path).AndReturn(True) | 405 presubmit.os.path.isfile(root_path).AndReturn(True) |
| 406 presubmit.os.path.isfile(haspresubmit_path).AndReturn(True) | 406 presubmit.os.path.isfile(haspresubmit_path).AndReturn(True) |
| 407 presubmit.gclient_utils.FileRead(root_path, | 407 presubmit.gclient_utils.FileRead(root_path, |
| 408 'rU').AndReturn(self.presubmit_text) | 408 'rU').AndReturn(self.presubmit_text) |
| 409 presubmit.gclient_utils.FileRead(haspresubmit_path, | 409 presubmit.gclient_utils.FileRead(haspresubmit_path, |
| 410 'rU').AndReturn(self.presubmit_text) | 410 'rU').AndReturn(self.presubmit_text) |
| 411 presubmit.random.randint(0, 4).AndReturn(1) | 411 presubmit.random.randint(0, 4).AndReturn(1) |
| 412 self.mox.ReplayAll() | 412 self.mox.ReplayAll() |
| 413 | 413 |
| 414 output = StringIO.StringIO() | |
| 415 input_buf = StringIO.StringIO('y\n') | 414 input_buf = StringIO.StringIO('y\n') |
| 416 change = presubmit.Change('mychange', '\n'.join(description_lines), | 415 change = presubmit.Change('mychange', '\n'.join(description_lines), |
| 417 self.fake_root_dir, files, 0, 0) | 416 self.fake_root_dir, files, 0, 0) |
| 418 self.failIf(presubmit.DoPresubmitChecks( | 417 output = presubmit.DoPresubmitChecks( |
| 419 change, False, True, output, input_buf, None, False)) | 418 change, False, True, None, input_buf, None, False) |
| 419 self.failIf(output.should_continue()) |
| 420 self.assertEqual(output.getvalue().count('!!'), 2) | 420 self.assertEqual(output.getvalue().count('!!'), 2) |
| 421 self.checkstdout('Running presubmit hooks...\n') | 421 self.assertEqual(output.getvalue().count('Running presubmit hooks...\n'), 1) |
| 422 | 422 |
| 423 def testDoPresubmitChecksPromptsAfterWarnings(self): | 423 def testDoPresubmitChecksPromptsAfterWarnings(self): |
| 424 join = presubmit.os.path.join | 424 join = presubmit.os.path.join |
| 425 description_lines = ('Hello there', | 425 description_lines = ('Hello there', |
| 426 'this is a change', | 426 'this is a change', |
| 427 'NOSUCHKEY=http://tracker/123') | 427 'NOSUCHKEY=http://tracker/123') |
| 428 files = [ | 428 files = [ |
| 429 ['A', join('haspresubmit', 'blat.cc')], | 429 ['A', join('haspresubmit', 'blat.cc')], |
| 430 ] | 430 ] |
| 431 presubmit_path = join(self.fake_root_dir, 'PRESUBMIT.py') | 431 presubmit_path = join(self.fake_root_dir, 'PRESUBMIT.py') |
| 432 haspresubmit_path = join(self.fake_root_dir, 'haspresubmit', 'PRESUBMIT.py') | 432 haspresubmit_path = join(self.fake_root_dir, 'haspresubmit', 'PRESUBMIT.py') |
| 433 inherit_path = presubmit.os.path.join(self.fake_root_dir, | 433 inherit_path = presubmit.os.path.join(self.fake_root_dir, |
| 434 self._INHERIT_SETTINGS) | 434 self._INHERIT_SETTINGS) |
| 435 for _ in range(2): | 435 for _ in range(2): |
| 436 presubmit.os.path.isfile(inherit_path).AndReturn(False) | 436 presubmit.os.path.isfile(inherit_path).AndReturn(False) |
| 437 presubmit.os.path.isfile(presubmit_path).AndReturn(True) | 437 presubmit.os.path.isfile(presubmit_path).AndReturn(True) |
| 438 presubmit.os.path.isfile(haspresubmit_path).AndReturn(True) | 438 presubmit.os.path.isfile(haspresubmit_path).AndReturn(True) |
| 439 presubmit.gclient_utils.FileRead(presubmit_path, 'rU' | 439 presubmit.gclient_utils.FileRead(presubmit_path, 'rU' |
| 440 ).AndReturn(self.presubmit_text) | 440 ).AndReturn(self.presubmit_text) |
| 441 presubmit.gclient_utils.FileRead(haspresubmit_path, 'rU' | 441 presubmit.gclient_utils.FileRead(haspresubmit_path, 'rU' |
| 442 ).AndReturn(self.presubmit_text) | 442 ).AndReturn(self.presubmit_text) |
| 443 presubmit.random.randint(0, 4).AndReturn(1) | 443 presubmit.random.randint(0, 4).AndReturn(1) |
| 444 presubmit.random.randint(0, 4).AndReturn(1) | 444 presubmit.random.randint(0, 4).AndReturn(1) |
| 445 self.mox.ReplayAll() | 445 self.mox.ReplayAll() |
| 446 | 446 |
| 447 output = StringIO.StringIO() | |
| 448 input_buf = StringIO.StringIO('n\n') # say no to the warning | 447 input_buf = StringIO.StringIO('n\n') # say no to the warning |
| 449 change = presubmit.Change('mychange', '\n'.join(description_lines), | 448 change = presubmit.Change('mychange', '\n'.join(description_lines), |
| 450 self.fake_root_dir, files, 0, 0) | 449 self.fake_root_dir, files, 0, 0) |
| 451 self.failIf(presubmit.DoPresubmitChecks( | 450 output = presubmit.DoPresubmitChecks( |
| 452 change, False, True, output, input_buf, None, True)) | 451 change, False, True, None, input_buf, None, True) |
| 452 self.failIf(output.should_continue()) |
| 453 self.assertEqual(output.getvalue().count('??'), 2) | 453 self.assertEqual(output.getvalue().count('??'), 2) |
| 454 | 454 |
| 455 output = StringIO.StringIO() | |
| 456 input_buf = StringIO.StringIO('y\n') # say yes to the warning | 455 input_buf = StringIO.StringIO('y\n') # say yes to the warning |
| 457 self.failUnless(presubmit.DoPresubmitChecks( | 456 output = presubmit.DoPresubmitChecks( |
| 458 change, False, True, output, input_buf, None, True)) | 457 change, False, True, None, input_buf, None, True) |
| 458 self.failUnless(output.should_continue()) |
| 459 self.assertEquals(output.getvalue().count('??'), 2) | 459 self.assertEquals(output.getvalue().count('??'), 2) |
| 460 self.checkstdout('Running presubmit hooks...\nRunning presubmit hooks...\n') | 460 self.assertEqual(output.getvalue().count('Running presubmit hooks...\n'), 1) |
| 461 | 461 |
| 462 def testDoPresubmitChecksNoWarningPromptIfErrors(self): | 462 def testDoPresubmitChecksNoWarningPromptIfErrors(self): |
| 463 join = presubmit.os.path.join | 463 join = presubmit.os.path.join |
| 464 description_lines = ('Hello there', | 464 description_lines = ('Hello there', |
| 465 'this is a change', | 465 'this is a change', |
| 466 'NOSUCHKEY=http://tracker/123', | 466 'NOSUCHKEY=http://tracker/123', |
| 467 'REALLYNOSUCHKEY=http://tracker/123') | 467 'REALLYNOSUCHKEY=http://tracker/123') |
| 468 files = [ | 468 files = [ |
| 469 ['A', join('haspresubmit', 'blat.cc')], | 469 ['A', join('haspresubmit', 'blat.cc')], |
| 470 ] | 470 ] |
| 471 presubmit_path = join(self.fake_root_dir, 'PRESUBMIT.py') | 471 presubmit_path = join(self.fake_root_dir, 'PRESUBMIT.py') |
| 472 haspresubmit_path = join(self.fake_root_dir, 'haspresubmit', | 472 haspresubmit_path = join(self.fake_root_dir, 'haspresubmit', |
| 473 'PRESUBMIT.py') | 473 'PRESUBMIT.py') |
| 474 inherit_path = presubmit.os.path.join(self.fake_root_dir, | 474 inherit_path = presubmit.os.path.join(self.fake_root_dir, |
| 475 self._INHERIT_SETTINGS) | 475 self._INHERIT_SETTINGS) |
| 476 presubmit.os.path.isfile(inherit_path).AndReturn(False) | 476 presubmit.os.path.isfile(inherit_path).AndReturn(False) |
| 477 presubmit.os.path.isfile(presubmit_path).AndReturn(True) | 477 presubmit.os.path.isfile(presubmit_path).AndReturn(True) |
| 478 presubmit.os.path.isfile(haspresubmit_path).AndReturn(True) | 478 presubmit.os.path.isfile(haspresubmit_path).AndReturn(True) |
| 479 presubmit.gclient_utils.FileRead(presubmit_path, 'rU' | 479 presubmit.gclient_utils.FileRead(presubmit_path, 'rU' |
| 480 ).AndReturn(self.presubmit_text) | 480 ).AndReturn(self.presubmit_text) |
| 481 presubmit.gclient_utils.FileRead(haspresubmit_path, 'rU').AndReturn( | 481 presubmit.gclient_utils.FileRead(haspresubmit_path, 'rU').AndReturn( |
| 482 self.presubmit_text) | 482 self.presubmit_text) |
| 483 presubmit.random.randint(0, 4).AndReturn(1) | 483 presubmit.random.randint(0, 4).AndReturn(1) |
| 484 self.mox.ReplayAll() | 484 self.mox.ReplayAll() |
| 485 | 485 |
| 486 output = StringIO.StringIO() | |
| 487 input_buf = StringIO.StringIO() # should be unused | |
| 488 change = presubmit.Change('mychange', '\n'.join(description_lines), | 486 change = presubmit.Change('mychange', '\n'.join(description_lines), |
| 489 self.fake_root_dir, files, 0, 0) | 487 self.fake_root_dir, files, 0, 0) |
| 490 self.failIf(presubmit.DoPresubmitChecks( | 488 output = presubmit.DoPresubmitChecks(change, False, True, None, None, |
| 491 change, False, True, output, input_buf, None, False)) | 489 None, False) |
| 492 self.assertEqual(output.getvalue().count('??'), 2) | 490 self.assertEqual(output.getvalue().count('??'), 2) |
| 493 self.assertEqual(output.getvalue().count('XX!!XX'), 2) | 491 self.assertEqual(output.getvalue().count('XX!!XX'), 2) |
| 494 self.assertEqual(output.getvalue().count('(y/N)'), 0) | 492 self.assertEqual(output.getvalue().count('(y/N)'), 0) |
| 495 self.checkstdout('Running presubmit hooks...\n') | 493 self.assertEqual(output.getvalue().count('Running presubmit hooks...\n'), 1) |
| 496 | 494 |
| 497 def testDoDefaultPresubmitChecksAndFeedback(self): | 495 def testDoDefaultPresubmitChecksAndFeedback(self): |
| 498 join = presubmit.os.path.join | 496 join = presubmit.os.path.join |
| 499 description_lines = ('Hello there', | 497 description_lines = ('Hello there', |
| 500 'this is a change', | 498 'this is a change', |
| 501 'STORY=http://tracker/123') | 499 'STORY=http://tracker/123') |
| 502 files = [ | 500 files = [ |
| 503 ['A', join('haspresubmit', 'blat.cc')], | 501 ['A', join('haspresubmit', 'blat.cc')], |
| 504 ] | 502 ] |
| 505 DEFAULT_SCRIPT = """ | 503 DEFAULT_SCRIPT = """ |
| 506 def CheckChangeOnUpload(input_api, output_api): | 504 def CheckChangeOnUpload(input_api, output_api): |
| 507 return [output_api.PresubmitError("!!")] | 505 return [output_api.PresubmitError("!!")] |
| 508 def CheckChangeOnCommit(input_api, output_api): | 506 def CheckChangeOnCommit(input_api, output_api): |
| 509 raise Exception("Test error") | 507 raise Exception("Test error") |
| 510 """ | 508 """ |
| 511 inherit_path = presubmit.os.path.join(self.fake_root_dir, | 509 inherit_path = presubmit.os.path.join(self.fake_root_dir, |
| 512 self._INHERIT_SETTINGS) | 510 self._INHERIT_SETTINGS) |
| 513 presubmit.os.path.isfile(inherit_path).AndReturn(False) | 511 presubmit.os.path.isfile(inherit_path).AndReturn(False) |
| 514 presubmit.os.path.isfile(join(self.fake_root_dir, 'PRESUBMIT.py') | 512 presubmit.os.path.isfile(join(self.fake_root_dir, 'PRESUBMIT.py') |
| 515 ).AndReturn(False) | 513 ).AndReturn(False) |
| 516 presubmit.os.path.isfile(join(self.fake_root_dir, | 514 presubmit.os.path.isfile(join(self.fake_root_dir, |
| 517 'haspresubmit', | 515 'haspresubmit', |
| 518 'PRESUBMIT.py')).AndReturn(False) | 516 'PRESUBMIT.py')).AndReturn(False) |
| 519 presubmit.random.randint(0, 4).AndReturn(0) | 517 presubmit.random.randint(0, 4).AndReturn(0) |
| 520 self.mox.ReplayAll() | 518 self.mox.ReplayAll() |
| 521 | 519 |
| 522 output = StringIO.StringIO() | |
| 523 input_buf = StringIO.StringIO('y\n') | 520 input_buf = StringIO.StringIO('y\n') |
| 524 # Always fail. | 521 # Always fail. |
| 525 change = presubmit.Change('mychange', '\n'.join(description_lines), | 522 change = presubmit.Change('mychange', '\n'.join(description_lines), |
| 526 self.fake_root_dir, files, 0, 0) | 523 self.fake_root_dir, files, 0, 0) |
| 527 self.failIf(presubmit.DoPresubmitChecks( | 524 output = presubmit.DoPresubmitChecks( |
| 528 change, False, True, output, input_buf, DEFAULT_SCRIPT, False)) | 525 change, False, True, None, input_buf, DEFAULT_SCRIPT, False) |
| 529 text = ('Warning, no presubmit.py found.\n' | 526 self.failIf(output.should_continue()) |
| 527 text = ('Running presubmit hooks...\n' |
| 528 'Warning, no presubmit.py found.\n' |
| 530 'Running default presubmit script.\n' | 529 'Running default presubmit script.\n' |
| 531 '** Presubmit ERRORS **\n!!\n\n' | 530 '** Presubmit ERRORS **\n!!\n\n' |
| 532 'Was the presubmit check useful? Please send feedback & hate mail ' | 531 'Was the presubmit check useful? Please send feedback & hate mail ' |
| 533 'to maruel@chromium.org!\n') | 532 'to maruel@chromium.org!\n') |
| 534 self.assertEquals(output.getvalue(), text) | 533 self.assertEquals(output.getvalue(), text) |
| 535 self.checkstdout('Running presubmit hooks...\n') | |
| 536 | 534 |
| 537 def testDirectoryHandling(self): | 535 def testDirectoryHandling(self): |
| 538 files = [ | 536 files = [ |
| 539 ['A', 'isdir'], | 537 ['A', 'isdir'], |
| 540 ['A', presubmit.os.path.join('isdir', 'blat.cc')], | 538 ['A', presubmit.os.path.join('isdir', 'blat.cc')], |
| 541 ] | 539 ] |
| 542 isdir = presubmit.os.path.join(self.fake_root_dir, 'isdir') | 540 isdir = presubmit.os.path.join(self.fake_root_dir, 'isdir') |
| 543 blat = presubmit.os.path.join(isdir, 'blat.cc') | 541 blat = presubmit.os.path.join(isdir, 'blat.cc') |
| 544 presubmit.os.path.exists(isdir).AndReturn(True) | 542 presubmit.os.path.exists(isdir).AndReturn(True) |
| 545 presubmit.os.path.isdir(isdir).AndReturn(True) | 543 presubmit.os.path.isdir(isdir).AndReturn(True) |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 593 self.mox.ReplayAll() | 591 self.mox.ReplayAll() |
| 594 | 592 |
| 595 output = StringIO.StringIO() | 593 output = StringIO.StringIO() |
| 596 input_buf = StringIO.StringIO('y\n') | 594 input_buf = StringIO.StringIO('y\n') |
| 597 change = presubmit.Change( | 595 change = presubmit.Change( |
| 598 'foo', "Blah Blah\n\nSTORY=http://tracker.com/42\nBUG=boo\n", | 596 'foo', "Blah Blah\n\nSTORY=http://tracker.com/42\nBUG=boo\n", |
| 599 self.fake_root_dir, None, 0, 0) | 597 self.fake_root_dir, None, 0, 0) |
| 600 self.failUnless(presubmit.DoPresubmitChecks( | 598 self.failUnless(presubmit.DoPresubmitChecks( |
| 601 change, False, True, output, input_buf, DEFAULT_SCRIPT, False)) | 599 change, False, True, output, input_buf, DEFAULT_SCRIPT, False)) |
| 602 self.assertEquals(output.getvalue(), | 600 self.assertEquals(output.getvalue(), |
| 603 ('Warning, no presubmit.py found.\n' | 601 ('Running presubmit hooks...\n' |
| 602 'Warning, no presubmit.py found.\n' |
| 604 'Running default presubmit script.\n' | 603 'Running default presubmit script.\n' |
| 605 '** Presubmit Messages **\n' | 604 '** Presubmit Messages **\n' |
| 606 'http://tracker.com/42\n\n')) | 605 'http://tracker.com/42\n\n')) |
| 607 self.checkstdout('Running presubmit hooks...\n') | |
| 608 | 606 |
| 609 def testGetTrySlavesExecuter(self): | 607 def testGetTrySlavesExecuter(self): |
| 610 self.mox.ReplayAll() | 608 self.mox.ReplayAll() |
| 611 | 609 |
| 612 executer = presubmit.GetTrySlavesExecuter() | 610 executer = presubmit.GetTrySlavesExecuter() |
| 613 self.assertEqual([], executer.ExecPresubmitScript('')) | 611 self.assertEqual([], executer.ExecPresubmitScript('')) |
| 614 self.assertEqual([], executer.ExecPresubmitScript('def foo():\n return\n')) | 612 self.assertEqual([], executer.ExecPresubmitScript('def foo():\n return\n')) |
| 615 | 613 |
| 616 # bad results | 614 # bad results |
| 617 starts_with_space_result = [' starts_with_space'] | 615 starts_with_space_result = [' starts_with_space'] |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 670 self.mox.StubOutWithMock(presubmit, 'DoPresubmitChecks') | 668 self.mox.StubOutWithMock(presubmit, 'DoPresubmitChecks') |
| 671 self.mox.StubOutWithMock(presubmit, 'ParseFiles') | 669 self.mox.StubOutWithMock(presubmit, 'ParseFiles') |
| 672 presubmit.os.path.isdir(presubmit.os.path.join(self.fake_root_dir, '.svn') | 670 presubmit.os.path.isdir(presubmit.os.path.join(self.fake_root_dir, '.svn') |
| 673 ).AndReturn(False) | 671 ).AndReturn(False) |
| 674 presubmit.os.path.isdir(presubmit.os.path.join(self.fake_root_dir, '.git') | 672 presubmit.os.path.isdir(presubmit.os.path.join(self.fake_root_dir, '.git') |
| 675 ).AndReturn(False) | 673 ).AndReturn(False) |
| 676 presubmit.subprocess.call( | 674 presubmit.subprocess.call( |
| 677 ['git', 'rev-parse', '--show-cdup'], | 675 ['git', 'rev-parse', '--show-cdup'], |
| 678 cwd=self.fake_root_dir, | 676 cwd=self.fake_root_dir, |
| 679 stdout=presubmit.subprocess.PIPE).AndReturn(1) | 677 stdout=presubmit.subprocess.PIPE).AndReturn(1) |
| 678 output = self.mox.CreateMock(presubmit.PresubmitOutput) |
| 679 output.should_continue().AndReturn(False) |
| 680 |
| 680 presubmit.DoPresubmitChecks(mox.IgnoreArg(), False, False, | 681 presubmit.DoPresubmitChecks(mox.IgnoreArg(), False, False, |
| 681 mox.IgnoreArg(), | 682 mox.IgnoreArg(), |
| 682 mox.IgnoreArg(), | 683 mox.IgnoreArg(), |
| 683 None, False).AndReturn(False) | 684 None, False).AndReturn(output) |
| 684 self.mox.ReplayAll() | 685 self.mox.ReplayAll() |
| 685 | 686 |
| 686 self.assertEquals(True, | 687 self.assertEquals(True, |
| 687 presubmit.Main(['presubmit', '--root', | 688 presubmit.Main(['presubmit', '--root', |
| 688 self.fake_root_dir])) | 689 self.fake_root_dir])) |
| 689 | 690 |
| 690 | 691 |
| 691 class InputApiUnittest(PresubmitTestsBase): | 692 class InputApiUnittest(PresubmitTestsBase): |
| 692 """Tests presubmit.InputApi.""" | 693 """Tests presubmit.InputApi.""" |
| 693 def testMembersChanged(self): | 694 def testMembersChanged(self): |
| (...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1039 change, presubmit.os.path.join(self.fake_root_dir, '/p'), False, | 1040 change, presubmit.os.path.join(self.fake_root_dir, '/p'), False, |
| 1040 False, None) | 1041 False, None) |
| 1041 input_api.ReadFile(fileobj, 'x') | 1042 input_api.ReadFile(fileobj, 'x') |
| 1042 | 1043 |
| 1043 | 1044 |
| 1044 class OuputApiUnittest(PresubmitTestsBase): | 1045 class OuputApiUnittest(PresubmitTestsBase): |
| 1045 """Tests presubmit.OutputApi.""" | 1046 """Tests presubmit.OutputApi.""" |
| 1046 def testMembersChanged(self): | 1047 def testMembersChanged(self): |
| 1047 self.mox.ReplayAll() | 1048 self.mox.ReplayAll() |
| 1048 members = [ | 1049 members = [ |
| 1049 'MailTextResult', 'PresubmitAddText', 'PresubmitError', | 1050 'MailTextResult', 'PresubmitAddReviewers', 'PresubmitError', |
| 1050 'PresubmitNotifyResult', 'PresubmitPromptWarning', 'PresubmitResult', | 1051 'PresubmitNotifyResult', 'PresubmitPromptWarning', 'PresubmitResult', |
| 1051 ] | 1052 ] |
| 1052 # If this test fails, you should add the relevant test. | 1053 # If this test fails, you should add the relevant test. |
| 1053 self.compareMembers(presubmit.OutputApi(), members) | 1054 self.compareMembers(presubmit.OutputApi(), members) |
| 1054 | 1055 |
| 1055 def testOutputApiBasics(self): | 1056 def testOutputApiBasics(self): |
| 1056 self.mox.ReplayAll() | 1057 self.mox.ReplayAll() |
| 1057 self.failUnless(presubmit.OutputApi.PresubmitError('').IsFatal()) | 1058 self.failUnless(presubmit.OutputApi.PresubmitError('').fatal) |
| 1058 self.failIf(presubmit.OutputApi.PresubmitError('').ShouldPrompt()) | 1059 self.failIf(presubmit.OutputApi.PresubmitError('').should_prompt) |
| 1059 | 1060 |
| 1060 self.failIf(presubmit.OutputApi.PresubmitPromptWarning('').IsFatal()) | 1061 self.failIf(presubmit.OutputApi.PresubmitPromptWarning('').fatal) |
| 1061 self.failUnless( | 1062 self.failUnless( |
| 1062 presubmit.OutputApi.PresubmitPromptWarning('').ShouldPrompt()) | 1063 presubmit.OutputApi.PresubmitPromptWarning('').should_prompt) |
| 1063 | 1064 |
| 1064 self.failIf(presubmit.OutputApi.PresubmitNotifyResult('').IsFatal()) | 1065 self.failIf(presubmit.OutputApi.PresubmitNotifyResult('').fatal) |
| 1065 self.failIf(presubmit.OutputApi.PresubmitNotifyResult('').ShouldPrompt()) | 1066 self.failIf(presubmit.OutputApi.PresubmitNotifyResult('').should_prompt) |
| 1066 | 1067 |
| 1067 self.failIf(presubmit.OutputApi.PresubmitAddText('foo').IsFatal()) | 1068 self.failIf(presubmit.OutputApi.PresubmitAddReviewers( |
| 1068 self.failIf(presubmit.OutputApi.PresubmitAddText('foo').ShouldPrompt()) | 1069 ['foo']).fatal) |
| 1070 self.failIf(presubmit.OutputApi.PresubmitAddReviewers( |
| 1071 ['foo']).should_prompt) |
| 1069 | 1072 |
| 1070 # TODO(joi) Test MailTextResult once implemented. | 1073 # TODO(joi) Test MailTextResult once implemented. |
| 1071 | 1074 |
| 1072 def testOutputApiHandling(self): | 1075 def testOutputApiHandling(self): |
| 1073 self.mox.ReplayAll() | 1076 self.mox.ReplayAll() |
| 1074 | 1077 |
| 1075 output = StringIO.StringIO() | 1078 output = presubmit.PresubmitOutput() |
| 1076 unused_input = StringIO.StringIO() | 1079 presubmit.OutputApi.PresubmitAddReviewers( |
| 1077 added_text = presubmit.OutputApi.PresubmitAddText('R=ben@example.com') | 1080 ['ben@example.com']).handle(output) |
| 1078 self.failUnless(added_text._Handle(output, unused_input)) | 1081 self.failUnless(output.should_continue()) |
| 1079 self.failUnlessEqual(output.getvalue(), 'ADD: R=ben@example.com\n') | 1082 self.failUnlessEqual(output.reviewers, ['ben@example.com']) |
| 1080 | 1083 |
| 1081 output = StringIO.StringIO() | 1084 output = presubmit.PresubmitOutput() |
| 1082 unused_input = StringIO.StringIO() | 1085 presubmit.OutputApi.PresubmitError('!!!').handle(output) |
| 1083 error = presubmit.OutputApi.PresubmitError('!!!') | 1086 self.failIf(output.should_continue()) |
| 1084 self.failIf(error._Handle(output, unused_input)) | |
| 1085 self.failUnless(output.getvalue().count('!!!')) | 1087 self.failUnless(output.getvalue().count('!!!')) |
| 1086 | 1088 |
| 1087 output = StringIO.StringIO() | 1089 output = presubmit.PresubmitOutput() |
| 1088 notify = presubmit.OutputApi.PresubmitNotifyResult('?see?') | 1090 presubmit.OutputApi.PresubmitNotifyResult('?see?').handle(output) |
| 1089 self.failUnless(notify._Handle(output, unused_input)) | 1091 self.failUnless(output.should_continue()) |
| 1090 self.failUnless(output.getvalue().count('?see?')) | 1092 self.failUnless(output.getvalue().count('?see?')) |
| 1091 | 1093 |
| 1092 output = StringIO.StringIO() | 1094 output = presubmit.PresubmitOutput(input_stream=StringIO.StringIO('y')) |
| 1093 input_buf = StringIO.StringIO('y') | 1095 presubmit.OutputApi.PresubmitPromptWarning('???').handle(output) |
| 1094 warning = presubmit.OutputApi.PresubmitPromptWarning('???') | 1096 output.prompt_yes_no('prompt: ') |
| 1095 self.failUnless(warning._Handle(output, input_buf)) | 1097 self.failUnless(output.should_continue()) |
| 1096 self.failUnless(output.getvalue().count('???')) | 1098 self.failUnless(output.getvalue().count('???')) |
| 1097 | 1099 |
| 1098 output = StringIO.StringIO() | 1100 output = presubmit.PresubmitOutput(input_stream=StringIO.StringIO('y')) |
| 1099 input_buf = StringIO.StringIO('n') | 1101 presubmit.OutputApi.PresubmitPromptWarning('???').handle(output) |
| 1100 warning = presubmit.OutputApi.PresubmitPromptWarning('???') | 1102 output.prompt_yes_no('prompt: ') |
| 1101 self.failIf(warning._Handle(output, input_buf)) | 1103 self.failUnless(output.should_continue()) |
| 1102 self.failUnless(output.getvalue().count('???')) | 1104 self.failUnless(output.getvalue().count('???')) |
| 1103 | 1105 |
| 1104 output = StringIO.StringIO() | 1106 output = presubmit.PresubmitOutput(input_stream=StringIO.StringIO('\n')) |
| 1105 input_buf = StringIO.StringIO('\n') | 1107 presubmit.OutputApi.PresubmitPromptWarning('???').handle(output) |
| 1106 warning = presubmit.OutputApi.PresubmitPromptWarning('???') | 1108 output.prompt_yes_no('prompt: ') |
| 1107 self.failIf(warning._Handle(output, input_buf)) | 1109 self.failIf(output.should_continue()) |
| 1108 self.failUnless(output.getvalue().count('???')) | 1110 self.failUnless(output.getvalue().count('???')) |
| 1109 | 1111 |
| 1110 | 1112 |
| 1111 class AffectedFileUnittest(PresubmitTestsBase): | 1113 class AffectedFileUnittest(PresubmitTestsBase): |
| 1112 def testMembersChanged(self): | 1114 def testMembersChanged(self): |
| 1113 self.mox.ReplayAll() | 1115 self.mox.ReplayAll() |
| 1114 members = [ | 1116 members = [ |
| 1115 'AbsoluteLocalPath', 'Action', 'ChangedContents', 'GenerateScmDiff', | 1117 'AbsoluteLocalPath', 'Action', 'ChangedContents', 'GenerateScmDiff', |
| 1116 'IsDirectory', 'IsTextFile', 'LocalPath', 'NewContents', 'OldContents', | 1118 'IsDirectory', 'IsTextFile', 'LocalPath', 'NewContents', 'OldContents', |
| 1117 'OldFileTempPath', 'Property', 'ServerPath', | 1119 'OldFileTempPath', 'Property', 'ServerPath', |
| (...skipping 740 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1858 self.mox.ReplayAll() | 1860 self.mox.ReplayAll() |
| 1859 | 1861 |
| 1860 results = presubmit_canned_checks.CheckBuildbotPendingBuilds( | 1862 results = presubmit_canned_checks.CheckBuildbotPendingBuilds( |
| 1861 input_api, presubmit.OutputApi, 'uurl', 2, ('foo')) | 1863 input_api, presubmit.OutputApi, 'uurl', 2, ('foo')) |
| 1862 self.assertEquals(len(results), 1) | 1864 self.assertEquals(len(results), 1) |
| 1863 self.assertEquals(results[0].__class__, | 1865 self.assertEquals(results[0].__class__, |
| 1864 presubmit.OutputApi.PresubmitNotifyResult) | 1866 presubmit.OutputApi.PresubmitNotifyResult) |
| 1865 | 1867 |
| 1866 def OwnersTest(self, is_committing, tbr=False, change_tags=None, | 1868 def OwnersTest(self, is_committing, tbr=False, change_tags=None, |
| 1867 suggested_reviewers=None, approvers=None, | 1869 suggested_reviewers=None, approvers=None, |
| 1868 uncovered_files=None, expected_results=None): | 1870 uncovered_files=None, expected_reviewers=None, expected_output=''): |
| 1869 affected_file = self.mox.CreateMock(presubmit.SvnAffectedFile) | 1871 affected_file = self.mox.CreateMock(presubmit.SvnAffectedFile) |
| 1870 affected_file.LocalPath().AndReturn('foo.cc') | 1872 affected_file.LocalPath().AndReturn('foo.cc') |
| 1871 change = self.mox.CreateMock(presubmit.Change) | 1873 change = self.mox.CreateMock(presubmit.Change) |
| 1872 change.AffectedFiles(None).AndReturn([affected_file]) | 1874 change.AffectedFiles(None).AndReturn([affected_file]) |
| 1873 | 1875 |
| 1874 input_api = self.MockInputApi(change, False) | 1876 input_api = self.MockInputApi(change, False) |
| 1875 fake_db = self.mox.CreateMock(owners.Database) | 1877 fake_db = self.mox.CreateMock(owners.Database) |
| 1876 fake_db.email_regexp = input_api.re.compile(owners.BASIC_EMAIL_REGEXP) | 1878 fake_db.email_regexp = input_api.re.compile(owners.BASIC_EMAIL_REGEXP) |
| 1877 input_api.owners_db = fake_db | 1879 input_api.owners_db = fake_db |
| 1878 input_api.is_committing = is_committing | 1880 input_api.is_committing = is_committing |
| (...skipping 10 matching lines...) Expand all Loading... |
| 1889 StringIO.StringIO(rietveld_response)) | 1891 StringIO.StringIO(rietveld_response)) |
| 1890 input_api.json = presubmit.json | 1892 input_api.json = presubmit.json |
| 1891 fake_db.files_not_covered_by(set(['foo.cc']), approvers).AndReturn( | 1893 fake_db.files_not_covered_by(set(['foo.cc']), approvers).AndReturn( |
| 1892 uncovered_files) | 1894 uncovered_files) |
| 1893 elif not is_committing: | 1895 elif not is_committing: |
| 1894 change.tags = change_tags | 1896 change.tags = change_tags |
| 1895 if not change_tags.get('R'): | 1897 if not change_tags.get('R'): |
| 1896 fake_db.reviewers_for(set(['foo.cc'])).AndReturn(suggested_reviewers) | 1898 fake_db.reviewers_for(set(['foo.cc'])).AndReturn(suggested_reviewers) |
| 1897 | 1899 |
| 1898 self.mox.ReplayAll() | 1900 self.mox.ReplayAll() |
| 1901 output = presubmit.PresubmitOutput() |
| 1899 results = presubmit_canned_checks.CheckOwners(input_api, | 1902 results = presubmit_canned_checks.CheckOwners(input_api, |
| 1900 presubmit.OutputApi) | 1903 presubmit.OutputApi) |
| 1901 self.assertEquals(len(results), len(expected_results)) | 1904 if results: |
| 1902 if results and expected_results: | 1905 results[0].handle(output) |
| 1903 output = StringIO.StringIO() | 1906 if expected_reviewers is not None: |
| 1904 unused_input = StringIO.StringIO() | 1907 self.assertEquals(output.reviewers, expected_reviewers) |
| 1905 results[0]._Handle(output, unused_input) | 1908 self.assertEquals(output.getvalue(), expected_output) |
| 1906 self.assertEquals(output.getvalue(), expected_results[0]) | |
| 1907 | 1909 |
| 1908 def testCannedCheckOwners_WithReviewer(self): | 1910 def testCannedCheckOwners_WithReviewer(self): |
| 1909 self.OwnersTest(is_committing=False, change_tags={'R': 'ben@example.com'}, | 1911 self.OwnersTest(is_committing=False, change_tags={'R': 'ben@example.com'}) |
| 1910 expected_results=[]) | |
| 1911 self.OwnersTest(is_committing=False, tbr=True, | 1912 self.OwnersTest(is_committing=False, tbr=True, |
| 1912 change_tags={'R': 'ben@example.com'}, expected_results=[]) | 1913 change_tags={'R': 'ben@example.com'}) |
| 1913 | 1914 |
| 1914 def testCannedCheckOwners_NoReviewer(self): | 1915 def testCannedCheckOwners_NoReviewer(self): |
| 1915 self.OwnersTest(is_committing=False, change_tags={}, | 1916 self.OwnersTest(is_committing=False, change_tags={}, |
| 1916 suggested_reviewers=['ben@example.com'], | 1917 suggested_reviewers=['ben@example.com'], |
| 1917 expected_results=['ADD: R=ben@example.com\n']) | 1918 expected_reviewers=['ben@example.com']) |
| 1918 self.OwnersTest(is_committing=False, tbr=True, change_tags={}, | 1919 self.OwnersTest(is_committing=False, tbr=True, change_tags={}, |
| 1919 suggested_reviewers=['ben@example.com'], | 1920 suggested_reviewers=['ben@example.com'], |
| 1920 expected_results=['ADD: R=ben@example.com\n']) | 1921 expected_reviewers=['ben@example.com']) |
| 1921 | 1922 |
| 1922 def testCannedCheckOwners_CommittingWithoutOwnerLGTM(self): | 1923 def testCannedCheckOwners_CommittingWithoutOwnerLGTM(self): |
| 1923 self.OwnersTest(is_committing=True, | 1924 self.OwnersTest(is_committing=True, |
| 1924 approvers=set(), | 1925 approvers=set(), |
| 1925 uncovered_files=set(['foo.cc']), | 1926 uncovered_files=set(['foo.cc']), |
| 1926 expected_results=['Missing LGTM from an OWNER for: foo.cc\n']) | 1927 expected_output='Missing LGTM from an OWNER for: foo.cc\n') |
| 1927 | 1928 |
| 1928 def testCannedCheckOwners_CommittingWithLGTMs(self): | 1929 def testCannedCheckOwners_CommittingWithLGTMs(self): |
| 1929 self.OwnersTest(is_committing=True, | 1930 self.OwnersTest(is_committing=True, |
| 1930 approvers=set(['ben@example.com']), | 1931 approvers=set(['ben@example.com']), |
| 1931 uncovered_files=set(), | 1932 uncovered_files=set()) |
| 1932 expected_results=[]) | |
| 1933 | 1933 |
| 1934 def testCannedCheckOwners_TBR(self): | 1934 def testCannedCheckOwners_TBR(self): |
| 1935 self.OwnersTest(is_committing=True, tbr=True, | 1935 self.OwnersTest(is_committing=True, tbr=True, |
| 1936 approvers=set(), | 1936 approvers=set(), |
| 1937 uncovered_files=set(), | 1937 uncovered_files=set(), |
| 1938 expected_results=['--tbr was specified, skipping OWNERS check\n']) | 1938 expected_output='--tbr was specified, skipping OWNERS check\n') |
| 1939 | 1939 |
| 1940 if __name__ == '__main__': | 1940 if __name__ == '__main__': |
| 1941 import unittest | 1941 import unittest |
| 1942 unittest.main() | 1942 unittest.main() |
| OLD | NEW |