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 | 13 |
14 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | 14 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
15 | 15 |
16 from testing_support.auto_stub import TestCase | 16 from testing_support.auto_stub import TestCase |
17 | 17 |
18 import git_cl | 18 import git_cl |
| 19 import git_common |
19 import subprocess2 | 20 import subprocess2 |
20 | 21 |
21 | 22 |
22 class PresubmitMock(object): | 23 class PresubmitMock(object): |
23 def __init__(self, *args, **kwargs): | 24 def __init__(self, *args, **kwargs): |
24 self.reviewers = [] | 25 self.reviewers = [] |
25 @staticmethod | 26 @staticmethod |
26 def should_continue(): | 27 def should_continue(): |
27 return True | 28 return True |
28 | 29 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 class TestGitCl(TestCase): | 70 class TestGitCl(TestCase): |
70 def setUp(self): | 71 def setUp(self): |
71 super(TestGitCl, self).setUp() | 72 super(TestGitCl, self).setUp() |
72 self.calls = [] | 73 self.calls = [] |
73 self._calls_done = 0 | 74 self._calls_done = 0 |
74 self.mock(subprocess2, 'call', self._mocked_call) | 75 self.mock(subprocess2, 'call', self._mocked_call) |
75 self.mock(subprocess2, 'check_call', self._mocked_call) | 76 self.mock(subprocess2, 'check_call', self._mocked_call) |
76 self.mock(subprocess2, 'check_output', self._mocked_call) | 77 self.mock(subprocess2, 'check_output', self._mocked_call) |
77 self.mock(subprocess2, 'communicate', self._mocked_call) | 78 self.mock(subprocess2, 'communicate', self._mocked_call) |
78 self.mock(subprocess2, 'Popen', self._mocked_call) | 79 self.mock(subprocess2, 'Popen', self._mocked_call) |
| 80 self.mock(git_common, 'get_or_create_merge_base', |
| 81 lambda *a: ( |
| 82 self._mocked_call(['get_or_create_merge_base']+list(a)))) |
79 self.mock(git_cl, 'FindCodereviewSettingsFile', lambda: '') | 83 self.mock(git_cl, 'FindCodereviewSettingsFile', lambda: '') |
80 self.mock(git_cl, 'ask_for_data', self._mocked_call) | 84 self.mock(git_cl, 'ask_for_data', self._mocked_call) |
81 self.mock(git_cl.breakpad, 'post', self._mocked_call) | 85 self.mock(git_cl.breakpad, 'post', self._mocked_call) |
82 self.mock(git_cl.breakpad, 'SendStack', self._mocked_call) | 86 self.mock(git_cl.breakpad, 'SendStack', self._mocked_call) |
83 self.mock(git_cl.presubmit_support, 'DoPresubmitChecks', PresubmitMock) | 87 self.mock(git_cl.presubmit_support, 'DoPresubmitChecks', PresubmitMock) |
84 self.mock(git_cl.rietveld, 'Rietveld', RietveldMock) | 88 self.mock(git_cl.rietveld, 'Rietveld', RietveldMock) |
85 self.mock(git_cl.rietveld, 'CachingRietveld', RietveldMock) | 89 self.mock(git_cl.rietveld, 'CachingRietveld', RietveldMock) |
86 self.mock(git_cl.upload, 'RealMain', self.fail) | 90 self.mock(git_cl.upload, 'RealMain', self.fail) |
87 self.mock(git_cl.watchlists, 'Watchlists', WatchlistsMock) | 91 self.mock(git_cl.watchlists, 'Watchlists', WatchlistsMock) |
88 # It's important to reset settings to not have inter-tests interference. | 92 # It's important to reset settings to not have inter-tests interference. |
89 git_cl.settings = None | 93 git_cl.settings = None |
90 | 94 |
91 def tearDown(self): | 95 def tearDown(self): |
92 if not self.has_failed(): | 96 if not self.has_failed(): |
93 self.assertEquals([], self.calls) | 97 self.assertEquals([], self.calls) |
94 super(TestGitCl, self).tearDown() | 98 super(TestGitCl, self).tearDown() |
95 | 99 |
96 def _mocked_call(self, *args, **kwargs): | 100 def _mocked_call(self, *args, **_kwargs): |
97 self.assertTrue( | 101 self.assertTrue( |
98 self.calls, | 102 self.calls, |
99 '@%d Expected: <Missing> Actual: %r' % (self._calls_done, args)) | 103 '@%d Expected: <Missing> Actual: %r' % (self._calls_done, args)) |
100 expected_args, result = self.calls.pop(0) | 104 expected_args, result = self.calls.pop(0) |
101 # Also logs otherwise it could get caught in a try/finally and be hard to | 105 # Also logs otherwise it could get caught in a try/finally and be hard to |
102 # diagnose. | 106 # diagnose. |
103 if expected_args != args: | 107 if expected_args != args: |
104 msg = '@%d Expected: %r Actual: %r' % ( | 108 msg = '@%d Expected: %r Actual: %r' % ( |
105 self._calls_done, expected_args, args) | 109 self._calls_done, expected_args, args) |
106 git_cl.logging.error(msg) | 110 git_cl.logging.error(msg) |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 'codereview.example.com'), | 155 'codereview.example.com'), |
152 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), | 156 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
153 similarity_call, | 157 similarity_call, |
154 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), | 158 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
155 find_copies_call, | 159 find_copies_call, |
156 ((['git', 'update-index', '--refresh', '-q'],), ''), | 160 ((['git', 'update-index', '--refresh', '-q'],), ''), |
157 ((['git', 'diff-index', '--name-status', 'HEAD'],), ''), | 161 ((['git', 'diff-index', '--name-status', 'HEAD'],), ''), |
158 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), | 162 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
159 ((['git', 'config', 'branch.master.merge'],), 'master'), | 163 ((['git', 'config', 'branch.master.merge'],), 'master'), |
160 ((['git', 'config', 'branch.master.remote'],), 'origin'), | 164 ((['git', 'config', 'branch.master.remote'],), 'origin'), |
161 ((['git', 'merge-base', 'master', 'HEAD'],), | 165 ((['get_or_create_merge_base', 'master', 'master'],), |
162 'fake_ancestor_sha'), | 166 'fake_ancestor_sha'), |
163 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [ | 167 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [ |
164 ((['git', 'rev-parse', '--show-cdup'],), ''), | 168 ((['git', 'rev-parse', '--show-cdup'],), ''), |
165 ((['git', 'rev-parse', 'HEAD'],), '12345'), | 169 ((['git', 'rev-parse', 'HEAD'],), '12345'), |
166 ((['git', 'diff', '--name-status', '--no-renames', '-r', | 170 ((['git', 'diff', '--name-status', '--no-renames', '-r', |
167 'fake_ancestor_sha...', '.'],), | 171 'fake_ancestor_sha...', '.'],), |
168 'M\t.gitignore\n'), | 172 'M\t.gitignore\n'), |
169 ((['git', 'config', 'branch.master.rietveldissue'],), ''), | 173 ((['git', 'config', 'branch.master.rietveldissue'],), ''), |
170 ((['git', 'config', 'branch.master.rietveldpatchset'],), | 174 ((['git', 'config', 'branch.master.rietveldpatchset'],), |
171 ''), | 175 ''), |
(...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
531 ((['git', 'config', '--int', '--get', | 535 ((['git', 'config', '--int', '--get', |
532 'branch.master.git-cl-similarity'],), ''), | 536 'branch.master.git-cl-similarity'],), ''), |
533 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), | 537 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
534 ((['git', 'config', '--int', '--get', | 538 ((['git', 'config', '--int', '--get', |
535 'branch.master.git-find-copies'],), ''), | 539 'branch.master.git-find-copies'],), ''), |
536 ((['git', 'update-index', '--refresh', '-q'],), ''), | 540 ((['git', 'update-index', '--refresh', '-q'],), ''), |
537 ((['git', 'diff-index', '--name-status', 'HEAD'],), ''), | 541 ((['git', 'diff-index', '--name-status', 'HEAD'],), ''), |
538 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), | 542 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
539 ((['git', 'config', 'branch.master.merge'],), 'master'), | 543 ((['git', 'config', 'branch.master.merge'],), 'master'), |
540 ((['git', 'config', 'branch.master.remote'],), 'origin'), | 544 ((['git', 'config', 'branch.master.remote'],), 'origin'), |
541 ((['git', | 545 ((['get_or_create_merge_base', 'master', 'master'],), |
542 'merge-base', 'master', 'HEAD'],), 'fake_ancestor_sha'), | 546 'fake_ancestor_sha'), |
543 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [ | 547 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [ |
544 ((['git', 'rev-parse', '--show-cdup'],), ''), | 548 ((['git', 'rev-parse', '--show-cdup'],), ''), |
545 ((['git', 'rev-parse', 'HEAD'],), '12345'), | 549 ((['git', 'rev-parse', 'HEAD'],), '12345'), |
546 ((['git', | 550 ((['git', |
547 'diff', '--name-status', '--no-renames', '-r', | 551 'diff', '--name-status', '--no-renames', '-r', |
548 'fake_ancestor_sha...', '.'],), | 552 'fake_ancestor_sha...', '.'],), |
549 'M\t.gitignore\n'), | 553 'M\t.gitignore\n'), |
550 ((['git', 'config', 'branch.master.rietveldissue'],), ''), | 554 ((['git', 'config', 'branch.master.rietveldissue'],), ''), |
551 ((['git', | 555 ((['git', |
552 'config', 'branch.master.rietveldpatchset'],), ''), | 556 'config', 'branch.master.rietveldpatchset'],), ''), |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
737 obj = git_cl.ChangeDescription(orig) | 741 obj = git_cl.ChangeDescription(orig) |
738 obj.update_reviewers(reviewers) | 742 obj.update_reviewers(reviewers) |
739 actual.append(obj.description) | 743 actual.append(obj.description) |
740 self.assertEqual(expected, actual) | 744 self.assertEqual(expected, actual) |
741 | 745 |
742 | 746 |
743 if __name__ == '__main__': | 747 if __name__ == '__main__': |
744 git_cl.logging.basicConfig( | 748 git_cl.logging.basicConfig( |
745 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) | 749 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) |
746 unittest.main() | 750 unittest.main() |
OLD | NEW |