Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 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 git_cl.py.""" | 6 """Unit tests for git_cl.py.""" |
| 7 | 7 |
| 8 import os | 8 import os |
| 9 import StringIO | 9 import StringIO |
| 10 import stat | 10 import stat |
| 11 import sys | 11 import sys |
| 12 import unittest | 12 import unittest |
| 13 import re | |
| 13 | 14 |
| 14 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | 15 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 15 | 16 |
| 16 from testing_support.auto_stub import TestCase | 17 from testing_support.auto_stub import TestCase |
| 17 | 18 |
| 18 import git_cl | 19 import git_cl |
| 19 import git_common | 20 import git_common |
| 20 import subprocess2 | 21 import subprocess2 |
| 21 | 22 import presubmit_support |
| 22 | 23 |
| 23 class PresubmitMock(object): | 24 class PresubmitMock(object): |
| 24 def __init__(self, *args, **kwargs): | 25 def __init__(self, *args, **kwargs): |
| 25 self.reviewers = [] | 26 self.reviewers = [] |
| 26 @staticmethod | 27 @staticmethod |
| 27 def should_continue(): | 28 def should_continue(): |
| 28 return True | 29 return True |
| 29 | 30 |
| 30 | 31 |
| 31 class RietveldMock(object): | 32 class RietveldMock(object): |
| (...skipping 711 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 743 ('foo BUG=allo R=joe ', ['c@c'], 'foo BUG=allo R=joe\n\nR=c@c'), | 744 ('foo BUG=allo R=joe ', ['c@c'], 'foo BUG=allo R=joe\n\nR=c@c'), |
| 744 ] | 745 ] |
| 745 expected = [i[2] for i in data] | 746 expected = [i[2] for i in data] |
| 746 actual = [] | 747 actual = [] |
| 747 for orig, reviewers, _expected in data: | 748 for orig, reviewers, _expected in data: |
| 748 obj = git_cl.ChangeDescription(orig) | 749 obj = git_cl.ChangeDescription(orig) |
| 749 obj.update_reviewers(reviewers) | 750 obj.update_reviewers(reviewers) |
| 750 actual.append(obj.description) | 751 actual.append(obj.description) |
| 751 self.assertEqual(expected, actual) | 752 self.assertEqual(expected, actual) |
| 752 | 753 |
| 754 def test_trybots_from_PRESUBMIT(self): | |
| 755 TEST_MASTER = 'testMaster' | |
| 756 TEST_BUILDER = 'testBuilder' | |
| 757 MASTERS = {TEST_MASTER:{TEST_BUILDER:['a']}} | |
| 758 self.mock(presubmit_support, 'DoGetTryMasters', | |
| 759 lambda *args: MASTERS) | |
| 760 | |
| 761 change_mock = ChangeMock() | |
| 762 changelist_mock = ChangelistMock(change_mock) | |
| 763 self.mock(git_cl, 'is_dirty_git_tree', lambda x: False) | |
| 764 self.mock(git_cl, 'print_stats', lambda *arg: True) | |
| 765 self.mock(git_cl, 'Changelist', lambda *args: changelist_mock) | |
| 766 self.mock(git_cl, 'CreateDescriptionFromLog', lambda arg: 'Commit message') | |
| 767 self.mock(git_cl.ChangeDescription, 'prompt', lambda self: None) | |
| 768 | |
| 769 self.calls = [ | |
| 770 ((['git', 'config', 'rietveld.autoupdate',],), | |
| 771 ''), | |
| 772 ((['git', 'config', 'gerrit.host',],), | |
| 773 ''), | |
| 774 ((['git', 'rev-parse', '--show-cdup',],), | |
| 775 ''), | |
| 776 ((['git', 'config', 'rietveld.private',],), | |
| 777 ''), | |
| 778 ((['git', 'config', '--local', '--get-regexp', '^svn-remote\\.'],), | |
| 779 ''), | |
| 780 ((['git', 'config', 'rietveld.project',],), | |
| 781 ''), | |
| 782 ((['git', 'rev-parse', 'HEAD',],), | |
| 783 ''), | |
| 784 ] | |
| 785 | |
| 786 stored_description = [] | |
| 787 def check_upload(args): | |
| 788 i = 0 | |
| 789 for arg in args: | |
| 790 if arg == '--message': | |
| 791 break | |
| 792 i += 1 | |
| 793 | |
| 794 self.assertTrue(i < len(args)) | |
| 795 stored_description.append(args[i+1]) | |
| 796 return 1, 2 | |
| 797 self.mock(git_cl.upload, 'RealMain', check_upload) | |
| 798 | |
| 799 git_cl.main(['upload', '--bypass-hooks', '--auto-bots']) | |
| 800 found = re.search("CQ_TRYBOTS=(.*?)$", stored_description[0]) | |
| 801 self.assertTrue(found) | |
| 802 self.assertEqual(found.group(1), '%s:%s;' % (TEST_MASTER, TEST_BUILDER)) | |
| 803 | |
| 804 | |
| 805 class ChangelistMock(object): | |
| 806 def __init__(self, change_mock): | |
| 807 self.change_mock = change_mock | |
| 808 | |
| 809 def __getattr__(self, key): | |
| 810 if key == 'GetChange': | |
| 811 return lambda *args: self.change_mock | |
| 812 if key == 'SetWatchers': | |
| 813 return lambda *args: None | |
| 814 if 'Set' in key: | |
| 815 return lambda x: True | |
|
iannucci
2014/06/24 23:23:59
these should be methods :D
martiniss
2014/06/24 23:55:55
Done! Manually added all the mocked methods.
| |
| 816 return lambda: [] | |
| 817 | |
| 818 class ChangeMock(object): | |
| 819 def __init__(self): | |
| 820 self.stored_description = None | |
| 821 | |
| 822 def __getattr__(self, key): | |
| 823 if key == 'SetDescriptionText': | |
| 824 def func(description): | |
| 825 self.stored_description = description | |
| 826 return func | |
| 827 if key == 'FullDescriptionText': | |
| 828 return lambda: "HIHI TEST DESCRIPTION" | |
| 829 if 'Repository' in key: | |
| 830 return lambda: '' | |
| 831 return lambda: [] | |
| 753 | 832 |
| 754 if __name__ == '__main__': | 833 if __name__ == '__main__': |
| 755 git_cl.logging.basicConfig( | 834 git_cl.logging.basicConfig( |
| 756 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) | 835 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) |
| 757 unittest.main() | 836 unittest.main() |
| OLD | NEW |