| 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 exceptions | 8 import exceptions |
| 9 import os | 9 import os |
| 10 import StringIO | 10 import StringIO |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 if not input_api.change.NOSUCHKEY: | 24 if not input_api.change.NOSUCHKEY: |
| 25 return [output_api.PresubmitError("!!")] | 25 return [output_api.PresubmitError("!!")] |
| 26 elif not input_api.change.REALLYNOSUCHKEY: | 26 elif not input_api.change.REALLYNOSUCHKEY: |
| 27 return [output_api.PresubmitPromptWarning("??")] | 27 return [output_api.PresubmitPromptWarning("??")] |
| 28 elif not input_api.change.REALLYABSOLUTELYNOSUCHKEY: | 28 elif not input_api.change.REALLYABSOLUTELYNOSUCHKEY: |
| 29 return [output_api.PresubmitPromptWarning("??"), | 29 return [output_api.PresubmitPromptWarning("??"), |
| 30 output_api.PresubmitError("XX!!XX")] | 30 output_api.PresubmitError("XX!!XX")] |
| 31 else: | 31 else: |
| 32 return () | 32 return () |
| 33 """ | 33 """ |
| 34 presubmit_tryslave = """ |
| 35 def GetPreferredTrySlaves(): |
| 36 return %s |
| 37 """ |
| 34 | 38 |
| 35 def setUp(self): | 39 def setUp(self): |
| 36 super_mox.SuperMoxTestBase.setUp(self) | 40 super_mox.SuperMoxTestBase.setUp(self) |
| 37 self.mox.StubOutWithMock(presubmit, 'warnings') | 41 self.mox.StubOutWithMock(presubmit, 'warnings') |
| 38 # Stub out 'os' but keep os.path.commonprefix/dirname/join/normpath/splitext | 42 # Stub out 'os' but keep os.path.commonprefix/dirname/join/normpath/splitext |
| 39 # and os.sep. | 43 # and os.sep. |
| 40 os_sep = presubmit.os.sep | 44 os_sep = presubmit.os.sep |
| 41 os_path_commonprefix = presubmit.os.path.commonprefix | 45 os_path_commonprefix = presubmit.os.path.commonprefix |
| 42 os_path_dirname = presubmit.os.path.dirname | 46 os_path_dirname = presubmit.os.path.dirname |
| 43 os_path_join = presubmit.os.path.join | 47 os_path_join = presubmit.os.path.join |
| (...skipping 22 matching lines...) Expand all Loading... |
| 66 self.mox.StubOutWithMock(presubmit.gclient_scm, 'CaptureSVNInfo') | 70 self.mox.StubOutWithMock(presubmit.gclient_scm, 'CaptureSVNInfo') |
| 67 self.mox.StubOutWithMock(presubmit.gcl, 'GetSVNFileProperty') | 71 self.mox.StubOutWithMock(presubmit.gcl, 'GetSVNFileProperty') |
| 68 self.mox.StubOutWithMock(presubmit.gcl, 'ReadFile') | 72 self.mox.StubOutWithMock(presubmit.gcl, 'ReadFile') |
| 69 | 73 |
| 70 | 74 |
| 71 class PresubmitUnittest(PresubmitTestsBase): | 75 class PresubmitUnittest(PresubmitTestsBase): |
| 72 """General presubmit_support.py tests (excluding InputApi and OutputApi).""" | 76 """General presubmit_support.py tests (excluding InputApi and OutputApi).""" |
| 73 def testMembersChanged(self): | 77 def testMembersChanged(self): |
| 74 self.mox.ReplayAll() | 78 self.mox.ReplayAll() |
| 75 members = [ | 79 members = [ |
| 76 'AffectedFile', 'Change', 'DoPresubmitChecks', 'GitChange', | 80 'AffectedFile', 'Change', 'DoGetTrySlaves', 'DoPresubmitChecks', |
| 77 'GitAffectedFile', 'InputApi', | 81 'GetTrySlavesExecuter', 'GitChange', 'GitAffectedFile', 'InputApi', |
| 78 'ListRelevantPresubmitFiles', 'Main', 'NotImplementedException', | 82 'ListRelevantPresubmitFiles', 'Main', 'NotImplementedException', |
| 79 'OutputApi', 'ParseFiles', 'PresubmitExecuter', 'ScanSubDirs', | 83 'OutputApi', 'ParseFiles', 'PresubmitExecuter', 'ScanSubDirs', |
| 80 'SvnAffectedFile', 'SvnChange', | 84 'SvnAffectedFile', 'SvnChange', |
| 81 'cPickle', 'cStringIO', 'exceptions', | 85 'cPickle', 'cStringIO', 'exceptions', |
| 82 'fnmatch', 'gcl', 'gclient_scm', 'glob', 'logging', 'marshal', 'normpath', | 86 'fnmatch', 'gcl', 'gclient_scm', 'glob', 'logging', 'marshal', 'normpath', |
| 83 'optparse', 'os', 'pickle', | 87 'optparse', 'os', 'pickle', |
| 84 'presubmit_canned_checks', 'random', 're', 'subprocess', 'sys', 'time', | 88 'presubmit_canned_checks', 'random', 're', 'subprocess', 'sys', 'time', |
| 85 'tempfile', 'traceback', 'types', 'unittest', 'urllib2', 'warnings', | 89 'tempfile', 'traceback', 'types', 'unittest', 'urllib2', 'warnings', |
| 86 ] | 90 ] |
| 87 # If this test fails, you should add the relevant test. | 91 # If this test fails, you should add the relevant test. |
| (...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 477 'foo', "Blah Blah\n\nSTORY=http://tracker.com/42\nBUG=boo\n", | 481 'foo', "Blah Blah\n\nSTORY=http://tracker.com/42\nBUG=boo\n", |
| 478 self.fake_root_dir, None, 0, 0) | 482 self.fake_root_dir, None, 0, 0) |
| 479 self.failUnless(presubmit.DoPresubmitChecks(change, False, True, output, | 483 self.failUnless(presubmit.DoPresubmitChecks(change, False, True, output, |
| 480 input, DEFAULT_SCRIPT, False)) | 484 input, DEFAULT_SCRIPT, False)) |
| 481 self.assertEquals(output.getvalue(), | 485 self.assertEquals(output.getvalue(), |
| 482 ('Warning, no presubmit.py found.\n' | 486 ('Warning, no presubmit.py found.\n' |
| 483 'Running default presubmit script.\n' | 487 'Running default presubmit script.\n' |
| 484 '** Presubmit Messages **\n' | 488 '** Presubmit Messages **\n' |
| 485 'http://tracker.com/42\n\n')) | 489 'http://tracker.com/42\n\n')) |
| 486 | 490 |
| 491 def testGetTrySlavesExecuter(self): |
| 492 self.mox.ReplayAll() |
| 493 |
| 494 executer = presubmit.GetTrySlavesExecuter() |
| 495 self.assertEqual([], executer.ExecPresubmitScript('')) |
| 496 self.assertEqual([], executer.ExecPresubmitScript('def foo():\n return\n')) |
| 497 |
| 498 # bad results |
| 499 starts_with_space_result = [' starts_with_space'] |
| 500 not_list_result1 = "'foo'" |
| 501 not_list_result2 = "('a', 'tuple')" |
| 502 for result in starts_with_space_result, not_list_result1, not_list_result2: |
| 503 self.assertRaises(exceptions.RuntimeError, |
| 504 executer.ExecPresubmitScript, |
| 505 self.presubmit_tryslave % result) |
| 506 |
| 507 # good results |
| 508 expected_result = ['1', '2', '3'] |
| 509 empty_result = [] |
| 510 space_in_name_result = ['foo bar', '1\t2 3'] |
| 511 for result in expected_result, empty_result, space_in_name_result: |
| 512 self.assertEqual(result, |
| 513 executer.ExecPresubmitScript(self.presubmit_tryslave % |
| 514 str(result))) |
| 515 |
| 516 def testDoGetTrySlaves(self): |
| 517 join = presubmit.os.path.join |
| 518 filename = 'foo.cc' |
| 519 filename_linux = join('linux_only', 'penguin.cc') |
| 520 root_presubmit = join(self.fake_root_dir, 'PRESUBMIT.py') |
| 521 linux_presubmit = join(self.fake_root_dir, 'linux_only', 'PRESUBMIT.py') |
| 522 |
| 523 presubmit.os.path.isfile(root_presubmit).AndReturn(True) |
| 524 presubmit.gcl.ReadFile(root_presubmit, 'rU').AndReturn( |
| 525 self.presubmit_tryslave % '["win"]') |
| 526 |
| 527 presubmit.os.path.isfile(root_presubmit).AndReturn(True) |
| 528 presubmit.os.path.isfile(linux_presubmit).AndReturn(True) |
| 529 presubmit.gcl.ReadFile(root_presubmit, 'rU').AndReturn( |
| 530 self.presubmit_tryslave % '["win"]') |
| 531 presubmit.gcl.ReadFile(linux_presubmit, 'rU').AndReturn( |
| 532 self.presubmit_tryslave % '["linux"]') |
| 533 self.mox.ReplayAll() |
| 534 |
| 535 output = StringIO.StringIO() |
| 536 self.assertEqual(['win'], |
| 537 presubmit.DoGetTrySlaves([filename], self.fake_root_dir, |
| 538 None, False, output)) |
| 539 output = StringIO.StringIO() |
| 540 self.assertEqual(['win', 'linux'], |
| 541 presubmit.DoGetTrySlaves([filename, filename_linux], |
| 542 self.fake_root_dir, None, False, |
| 543 output)) |
| 544 |
| 487 def testMain(self): | 545 def testMain(self): |
| 488 self.mox.StubOutWithMock(presubmit, 'DoPresubmitChecks') | 546 self.mox.StubOutWithMock(presubmit, 'DoPresubmitChecks') |
| 489 self.mox.StubOutWithMock(presubmit, 'ParseFiles') | 547 self.mox.StubOutWithMock(presubmit, 'ParseFiles') |
| 490 presubmit.os.path.isdir(presubmit.os.path.join(self.fake_root_dir, '.git') | 548 presubmit.os.path.isdir(presubmit.os.path.join(self.fake_root_dir, '.git') |
| 491 ).AndReturn(False) | 549 ).AndReturn(False) |
| 492 presubmit.os.path.isdir(presubmit.os.path.join(self.fake_root_dir, '.svn') | 550 presubmit.os.path.isdir(presubmit.os.path.join(self.fake_root_dir, '.svn') |
| 493 ).AndReturn(False) | 551 ).AndReturn(False) |
| 494 #presubmit.ParseFiles([], None).AndReturn([]) | 552 #presubmit.ParseFiles([], None).AndReturn([]) |
| 495 presubmit.DoPresubmitChecks(mox.IgnoreArg(), False, False, | 553 presubmit.DoPresubmitChecks(mox.IgnoreArg(), False, False, |
| 496 mox.IgnoreArg(), | 554 mox.IgnoreArg(), |
| (...skipping 915 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1412 process.communicate().AndReturn(('', '')) | 1470 process.communicate().AndReturn(('', '')) |
| 1413 self.mox.ReplayAll() | 1471 self.mox.ReplayAll() |
| 1414 | 1472 |
| 1415 results = presubmit_canned_checks.RunPythonUnitTests( | 1473 results = presubmit_canned_checks.RunPythonUnitTests( |
| 1416 input_api, presubmit.OutputApi, ['test_module']) | 1474 input_api, presubmit.OutputApi, ['test_module']) |
| 1417 self.assertEquals(len(results), 0) | 1475 self.assertEquals(len(results), 0) |
| 1418 | 1476 |
| 1419 | 1477 |
| 1420 if __name__ == '__main__': | 1478 if __name__ == '__main__': |
| 1421 unittest.main() | 1479 unittest.main() |
| OLD | NEW |