| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 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,R0201,W0212,W0403 | 9 # pylint: disable=E1101,E1103,R0201,W0212,W0403 |
| 10 | 10 |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 | 146 |
| 147 def testMembersChanged(self): | 147 def testMembersChanged(self): |
| 148 self.mox.ReplayAll() | 148 self.mox.ReplayAll() |
| 149 members = [ | 149 members = [ |
| 150 'AffectedFile', 'Change', 'DoGetTrySlaves', 'DoPresubmitChecks', | 150 'AffectedFile', 'Change', 'DoGetTrySlaves', 'DoPresubmitChecks', |
| 151 'GetTrySlavesExecuter', 'GitAffectedFile', | 151 'GetTrySlavesExecuter', 'GitAffectedFile', |
| 152 'GitChange', 'InputApi', 'ListRelevantPresubmitFiles', 'Main', | 152 'GitChange', 'InputApi', 'ListRelevantPresubmitFiles', 'Main', |
| 153 'OutputApi', 'ParseFiles', 'PresubmitFailure', | 153 'OutputApi', 'ParseFiles', 'PresubmitFailure', |
| 154 'PresubmitExecuter', 'PresubmitOutput', 'ScanSubDirs', | 154 'PresubmitExecuter', 'PresubmitOutput', 'ScanSubDirs', |
| 155 'SvnAffectedFile', 'SvnChange', 'cPickle', 'cStringIO', | 155 'SvnAffectedFile', 'SvnChange', 'cPickle', 'cStringIO', |
| 156 'fix_encoding', 'fnmatch', 'gclient_utils', 'glob', 'json', | 156 'fix_encoding', 'fnmatch', 'gclient_utils', 'glob', 'inspect', 'json', |
| 157 'load_files', | 157 'load_files', |
| 158 'logging', 'marshal', 'normpath', 'optparse', 'os', 'owners', 'pickle', | 158 'logging', 'marshal', 'normpath', 'optparse', 'os', 'owners', 'pickle', |
| 159 'presubmit_canned_checks', 'random', 're', 'rietveld', 'scm', | 159 'presubmit_canned_checks', 'random', 're', 'rietveld', 'scm', |
| 160 'subprocess', | 160 'subprocess', |
| 161 'sys', 'tempfile', 'time', 'traceback', 'types', 'unittest', 'urllib2', | 161 'sys', 'tempfile', 'time', 'traceback', 'types', 'unittest', 'urllib2', |
| 162 'warn', | 162 'warn', |
| 163 ] | 163 ] |
| 164 # If this test fails, you should add the relevant test. | 164 # If this test fails, you should add the relevant test. |
| 165 self.compareMembers(presubmit, members) | 165 self.compareMembers(presubmit, members) |
| 166 | 166 |
| (...skipping 495 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 662 'Warning, no PRESUBMIT.py found.\n' | 662 'Warning, no PRESUBMIT.py found.\n' |
| 663 'Running default presubmit script.\n' | 663 'Running default presubmit script.\n' |
| 664 '\n' | 664 '\n' |
| 665 '** Presubmit Messages **\n' | 665 '** Presubmit Messages **\n' |
| 666 'http://tracker.com/42\n' | 666 'http://tracker.com/42\n' |
| 667 '\n' | 667 '\n' |
| 668 'Presubmit checks passed.\n')) | 668 'Presubmit checks passed.\n')) |
| 669 | 669 |
| 670 def testGetTrySlavesExecuter(self): | 670 def testGetTrySlavesExecuter(self): |
| 671 self.mox.ReplayAll() | 671 self.mox.ReplayAll() |
| 672 | 672 change = presubmit.Change( |
| 673 'foo', |
| 674 'Blah Blah\n\nSTORY=http://tracker.com/42\nBUG=boo\n', |
| 675 self.fake_root_dir, |
| 676 None, |
| 677 0, |
| 678 0, |
| 679 None) |
| 673 executer = presubmit.GetTrySlavesExecuter() | 680 executer = presubmit.GetTrySlavesExecuter() |
| 674 self.assertEqual([], executer.ExecPresubmitScript('', '', '')) | 681 self.assertEqual([], executer.ExecPresubmitScript('', '', '', change)) |
| 675 self.assertEqual( | 682 self.assertEqual([], |
| 676 [], executer.ExecPresubmitScript('def foo():\n return\n', '', '')) | 683 executer.ExecPresubmitScript('def foo():\n return\n', '', '', change)) |
| 677 | 684 |
| 678 # bad results | 685 # bad results |
| 679 starts_with_space_result = [' starts_with_space'] | 686 starts_with_space_result = [' starts_with_space'] |
| 680 not_list_result1 = "'foo'" | 687 not_list_result1 = "'foo'" |
| 681 not_list_result2 = "('a', 'tuple')" | 688 not_list_result2 = "('a', 'tuple')" |
| 682 for result in starts_with_space_result, not_list_result1, not_list_result2: | 689 for result in starts_with_space_result, not_list_result1, not_list_result2: |
| 683 self.assertRaises(presubmit.PresubmitFailure, | 690 self.assertRaises(presubmit.PresubmitFailure, |
| 684 executer.ExecPresubmitScript, | 691 executer.ExecPresubmitScript, |
| 685 self.presubmit_tryslave % result, '', '') | 692 self.presubmit_tryslave % result, '', '', change) |
| 686 | 693 |
| 687 # good results | 694 # good results |
| 688 expected_result = ['1', '2', '3'] | 695 expected_result = ['1', '2', '3'] |
| 689 empty_result = [] | 696 empty_result = [] |
| 690 space_in_name_result = ['foo bar', '1\t2 3'] | 697 space_in_name_result = ['foo bar', '1\t2 3'] |
| 691 for result in expected_result, empty_result, space_in_name_result: | 698 for result in expected_result, empty_result, space_in_name_result: |
| 692 self.assertEqual( | 699 self.assertEqual( |
| 693 result, | 700 result, |
| 694 executer.ExecPresubmitScript( | 701 executer.ExecPresubmitScript( |
| 695 self.presubmit_tryslave % result, '', '')) | 702 self.presubmit_tryslave % result, '', '', change)) |
| 696 | 703 |
| 697 def testGetTrySlavesExecuterWithProject(self): | 704 def testGetTrySlavesExecuterWithProject(self): |
| 698 self.mox.ReplayAll() | 705 self.mox.ReplayAll() |
| 699 | 706 |
| 707 change = presubmit.Change( |
| 708 'foo', |
| 709 'Blah Blah\n\nSTORY=http://tracker.com/42\nBUG=boo\n', |
| 710 self.fake_root_dir, |
| 711 None, |
| 712 0, |
| 713 0, |
| 714 None) |
| 715 |
| 700 executer = presubmit.GetTrySlavesExecuter() | 716 executer = presubmit.GetTrySlavesExecuter() |
| 701 expected_result1 = ['1', '2'] | 717 expected_result1 = ['1', '2'] |
| 702 expected_result2 = ['a', 'b', 'c'] | 718 expected_result2 = ['a', 'b', 'c'] |
| 703 script = self.presubmit_tryslave_project % ( | 719 script = self.presubmit_tryslave_project % ( |
| 704 repr('foo'), repr(expected_result1), repr(expected_result2)) | 720 repr('foo'), repr(expected_result1), repr(expected_result2)) |
| 705 self.assertEqual( | 721 self.assertEqual( |
| 706 expected_result1, executer.ExecPresubmitScript(script, '', 'foo')) | 722 expected_result1, executer.ExecPresubmitScript(script, '', 'foo', |
| 723 change)) |
| 707 self.assertEqual( | 724 self.assertEqual( |
| 708 expected_result2, executer.ExecPresubmitScript(script, '', 'bar')) | 725 expected_result2, executer.ExecPresubmitScript(script, '', 'bar', |
| 726 change)) |
| 709 | 727 |
| 710 def testDoGetTrySlaves(self): | 728 def testDoGetTrySlaves(self): |
| 711 join = presubmit.os.path.join | 729 join = presubmit.os.path.join |
| 712 filename = 'foo.cc' | 730 filename = 'foo.cc' |
| 713 filename_linux = join('linux_only', 'penguin.cc') | 731 filename_linux = join('linux_only', 'penguin.cc') |
| 714 root_presubmit = join(self.fake_root_dir, 'PRESUBMIT.py') | 732 root_presubmit = join(self.fake_root_dir, 'PRESUBMIT.py') |
| 715 linux_presubmit = join(self.fake_root_dir, 'linux_only', 'PRESUBMIT.py') | 733 linux_presubmit = join(self.fake_root_dir, 'linux_only', 'PRESUBMIT.py') |
| 716 inherit_path = presubmit.os.path.join(self.fake_root_dir, | 734 inherit_path = presubmit.os.path.join(self.fake_root_dir, |
| 717 self._INHERIT_SETTINGS) | 735 self._INHERIT_SETTINGS) |
| 718 | 736 |
| 719 presubmit.os.path.isfile(inherit_path).AndReturn(False) | 737 presubmit.os.path.isfile(inherit_path).AndReturn(False) |
| 720 presubmit.os.path.isfile(root_presubmit).AndReturn(True) | 738 presubmit.os.path.isfile(root_presubmit).AndReturn(True) |
| 721 presubmit.gclient_utils.FileRead(root_presubmit, 'rU').AndReturn( | 739 presubmit.gclient_utils.FileRead(root_presubmit, 'rU').AndReturn( |
| 722 self.presubmit_tryslave % '["win"]') | 740 self.presubmit_tryslave % '["win"]') |
| 723 | 741 |
| 724 presubmit.os.path.isfile(inherit_path).AndReturn(False) | 742 presubmit.os.path.isfile(inherit_path).AndReturn(False) |
| 725 presubmit.os.path.isfile(root_presubmit).AndReturn(True) | 743 presubmit.os.path.isfile(root_presubmit).AndReturn(True) |
| 726 presubmit.os.path.isfile(linux_presubmit).AndReturn(True) | 744 presubmit.os.path.isfile(linux_presubmit).AndReturn(True) |
| 727 presubmit.gclient_utils.FileRead(root_presubmit, 'rU').AndReturn( | 745 presubmit.gclient_utils.FileRead(root_presubmit, 'rU').AndReturn( |
| 728 self.presubmit_tryslave % '["win"]') | 746 self.presubmit_tryslave % '["win"]') |
| 729 presubmit.gclient_utils.FileRead(linux_presubmit, 'rU').AndReturn( | 747 presubmit.gclient_utils.FileRead(linux_presubmit, 'rU').AndReturn( |
| 730 self.presubmit_tryslave % '["linux"]') | 748 self.presubmit_tryslave % '["linux"]') |
| 731 self.mox.ReplayAll() | 749 self.mox.ReplayAll() |
| 732 | 750 |
| 751 change = presubmit.Change( |
| 752 'mychange', '', self.fake_root_dir, [], 0, 0, None) |
| 753 |
| 733 output = StringIO.StringIO() | 754 output = StringIO.StringIO() |
| 734 self.assertEqual(['win'], | 755 self.assertEqual(['win'], |
| 735 presubmit.DoGetTrySlaves([filename], self.fake_root_dir, | 756 presubmit.DoGetTrySlaves(change, [filename], |
| 757 self.fake_root_dir, |
| 736 None, None, False, output)) | 758 None, None, False, output)) |
| 737 output = StringIO.StringIO() | 759 output = StringIO.StringIO() |
| 738 self.assertEqual(['win', 'linux'], | 760 self.assertEqual(['win', 'linux'], |
| 739 presubmit.DoGetTrySlaves([filename, filename_linux], | 761 presubmit.DoGetTrySlaves(change, |
| 762 [filename, filename_linux], |
| 740 self.fake_root_dir, None, None, | 763 self.fake_root_dir, None, None, |
| 741 False, output)) | 764 False, output)) |
| 742 | 765 |
| 743 def testMainUnversioned(self): | 766 def testMainUnversioned(self): |
| 744 # OptParser calls presubmit.os.path.exists and is a pain when mocked. | 767 # OptParser calls presubmit.os.path.exists and is a pain when mocked. |
| 745 self.UnMock(presubmit.os.path, 'exists') | 768 self.UnMock(presubmit.os.path, 'exists') |
| 746 self.mox.StubOutWithMock(presubmit, 'DoPresubmitChecks') | 769 self.mox.StubOutWithMock(presubmit, 'DoPresubmitChecks') |
| 747 self.mox.StubOutWithMock(presubmit, 'ParseFiles') | 770 self.mox.StubOutWithMock(presubmit, 'ParseFiles') |
| 748 presubmit.scm.determine_scm(self.fake_root_dir).AndReturn(None) | 771 presubmit.scm.determine_scm(self.fake_root_dir).AndReturn(None) |
| 749 presubmit.ParseFiles(['random_file.txt'], None | 772 presubmit.ParseFiles(['random_file.txt'], None |
| (...skipping 1481 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2231 owners_check=True) | 2254 owners_check=True) |
| 2232 self.assertEqual(1, len(results)) | 2255 self.assertEqual(1, len(results)) |
| 2233 self.assertEqual( | 2256 self.assertEqual( |
| 2234 'Found line ending with white spaces in:', results[0]._message) | 2257 'Found line ending with white spaces in:', results[0]._message) |
| 2235 self.checkstdout('') | 2258 self.checkstdout('') |
| 2236 | 2259 |
| 2237 | 2260 |
| 2238 if __name__ == '__main__': | 2261 if __name__ == '__main__': |
| 2239 import unittest | 2262 import unittest |
| 2240 unittest.main() | 2263 unittest.main() |
| OLD | NEW |