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 |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 # Gerrrit. | 206 # Gerrrit. |
207 test('https://chrome-review.source.com/c/123/4', | 207 test('https://chrome-review.source.com/c/123/4', |
208 123, 4, 'chrome-review.source.com') | 208 123, 4, 'chrome-review.source.com') |
209 test('https://chrome-review.source.com/bad/123/4', fail=True) | 209 test('https://chrome-review.source.com/bad/123/4', fail=True) |
210 | 210 |
211 | 211 |
212 class TestGitCl(TestCase): | 212 class TestGitCl(TestCase): |
213 def setUp(self): | 213 def setUp(self): |
214 super(TestGitCl, self).setUp() | 214 super(TestGitCl, self).setUp() |
215 self.calls = [] | 215 self.calls = [] |
216 self._calls_done = 0 | 216 self._calls_done = [] |
217 self.mock(subprocess2, 'call', self._mocked_call) | 217 self.mock(subprocess2, 'call', self._mocked_call) |
218 self.mock(subprocess2, 'check_call', self._mocked_call) | 218 self.mock(subprocess2, 'check_call', self._mocked_call) |
219 self.mock(subprocess2, 'check_output', self._mocked_call) | 219 self.mock(subprocess2, 'check_output', self._mocked_call) |
220 self.mock(subprocess2, 'communicate', self._mocked_call) | 220 self.mock(subprocess2, 'communicate', self._mocked_call) |
221 self.mock(git_cl.gclient_utils, 'CheckCallAndFilter', self._mocked_call) | 221 self.mock(git_cl.gclient_utils, 'CheckCallAndFilter', self._mocked_call) |
222 self.mock(git_common, 'is_dirty_git_tree', lambda x: False) | 222 self.mock(git_common, 'is_dirty_git_tree', lambda x: False) |
223 self.mock(git_common, 'get_or_create_merge_base', | 223 self.mock(git_common, 'get_or_create_merge_base', |
224 lambda *a: ( | 224 lambda *a: ( |
225 self._mocked_call(['get_or_create_merge_base']+list(a)))) | 225 self._mocked_call(['get_or_create_merge_base']+list(a)))) |
226 self.mock(git_cl, 'BranchExists', lambda _: True) | 226 self.mock(git_cl, 'BranchExists', lambda _: True) |
(...skipping 17 matching lines...) Expand all Loading... |
244 # included, has failed. That means current test may have actually ran | 244 # included, has failed. That means current test may have actually ran |
245 # fine, and the check for no leftover calls would be skipped. | 245 # fine, and the check for no leftover calls would be skipped. |
246 if not self.has_failed(): | 246 if not self.has_failed(): |
247 self.assertEquals([], self.calls) | 247 self.assertEquals([], self.calls) |
248 finally: | 248 finally: |
249 super(TestGitCl, self).tearDown() | 249 super(TestGitCl, self).tearDown() |
250 | 250 |
251 def _mocked_call(self, *args, **_kwargs): | 251 def _mocked_call(self, *args, **_kwargs): |
252 self.assertTrue( | 252 self.assertTrue( |
253 self.calls, | 253 self.calls, |
254 '@%d Expected: <Missing> Actual: %r' % (self._calls_done, args)) | 254 '@%d Expected: <Missing> Actual: %r' % (len(self._calls_done), args)) |
255 top = self.calls.pop(0) | 255 top = self.calls.pop(0) |
256 if len(top) > 2 and top[2]: | 256 if len(top) > 2 and top[2]: |
257 raise top[2] | 257 raise top[2] |
258 expected_args, result = top | 258 expected_args, result = top |
259 | 259 |
260 # Also logs otherwise it could get caught in a try/finally and be hard to | 260 # Also logs otherwise it could get caught in a try/finally and be hard to |
261 # diagnose. | 261 # diagnose. |
262 if expected_args != args: | 262 if expected_args != args: |
263 msg = '@%d Expected: %r Actual: %r' % ( | 263 N = 5 |
264 self._calls_done, expected_args, args) | 264 prior_calls = '\n '.join( |
265 git_cl.logging.error(msg) | 265 '@%d: %r' % (len(self._calls_done) - N + i, c[0]) |
266 self.fail(msg) | 266 for i, c in enumerate(self._calls_done[-N:])) |
267 self._calls_done += 1 | 267 following_calls = '\n '.join( |
| 268 '@%d: %r' % (len(self._calls_done) + i + 1, c[0]) |
| 269 for i, c in enumerate(self.calls[:N])) |
| 270 extended_msg = ( |
| 271 'A few prior calls:\n %s\n\n' |
| 272 'This (expected):\n @%d: %r\n' |
| 273 'This (actual):\n @%d: %r\n\n' |
| 274 'A few following expected calls:\n %s' % |
| 275 (prior_calls, len(self._calls_done), expected_args, |
| 276 len(self._calls_done), args, following_calls)) |
| 277 git_cl.logging.error(extended_msg) |
| 278 |
| 279 self.fail('@%d Expected: %r Actual: %r' % ( |
| 280 len(self._calls_done), expected_args, args)) |
| 281 |
| 282 self._calls_done.append(top) |
268 return result | 283 return result |
269 | 284 |
270 @classmethod | 285 @classmethod |
271 def _is_gerrit_calls(cls, gerrit=False): | 286 def _is_gerrit_calls(cls, gerrit=False): |
272 return [((['git', 'config', 'rietveld.autoupdate'],), ''), | 287 return [((['git', 'config', 'rietveld.autoupdate'],), ''), |
273 ((['git', 'config', 'gerrit.host'],), 'True' if gerrit else '')] | 288 ((['git', 'config', 'gerrit.host'],), 'True' if gerrit else '')] |
274 | 289 |
275 @classmethod | 290 @classmethod |
276 def _upload_calls(cls, similarity, find_copies, private): | 291 def _upload_calls(cls, similarity, find_copies, private): |
277 return (cls._git_base_calls(similarity, find_copies) + | 292 return (cls._git_base_calls(similarity, find_copies) + |
(...skipping 1350 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1628 ((['rm_file_or_tree', '/abs/git_repo_root/.git/hooks/commit-msg'],), | 1643 ((['rm_file_or_tree', '/abs/git_repo_root/.git/hooks/commit-msg'],), |
1629 ''), | 1644 ''), |
1630 ] | 1645 ] |
1631 cl._codereview_impl._GerritCommitMsgHookCheck(offer_removal=True) | 1646 cl._codereview_impl._GerritCommitMsgHookCheck(offer_removal=True) |
1632 | 1647 |
1633 | 1648 |
1634 if __name__ == '__main__': | 1649 if __name__ == '__main__': |
1635 git_cl.logging.basicConfig( | 1650 git_cl.logging.basicConfig( |
1636 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) | 1651 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) |
1637 unittest.main() | 1652 unittest.main() |
OLD | NEW |