Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: tests/git_cl_test.py

Issue 2259043002: git cl: cleanup branch config get/set/unset. (Closed) Base URL: https://chromium.googlesource.com/chromium/tools/depot_tools.git@master
Patch Set: Review. Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « git_cl.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 urlparse 13 import urlparse
14 14
15 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__))))
16 16
17 from testing_support.auto_stub import TestCase 17 from testing_support.auto_stub import TestCase
18 18
19 import git_cl 19 import git_cl
20 import git_common 20 import git_common
21 import git_footers 21 import git_footers
22 import subprocess2 22 import subprocess2
23 23
24 def callError(code=1, cmd='', cwd='', stdout='', stderr=''):
25 return subprocess2.CalledProcessError(code, cmd, cwd, stdout, stderr)
26
27
28 CERR1 = callError(1)
29
30
24 class ChangelistMock(object): 31 class ChangelistMock(object):
25 # A class variable so we can access it when we don't have access to the 32 # A class variable so we can access it when we don't have access to the
26 # instance that's being set. 33 # instance that's being set.
27 desc = "" 34 desc = ""
28 def __init__(self, **kwargs): 35 def __init__(self, **kwargs):
29 pass 36 pass
30 def GetIssue(self): 37 def GetIssue(self):
31 return 1 38 return 1
32 def GetDescription(self): 39 def GetDescription(self):
33 return ChangelistMock.desc 40 return ChangelistMock.desc
34 def UpdateDescription(self, desc): 41 def UpdateDescription(self, desc):
35 ChangelistMock.desc = desc 42 ChangelistMock.desc = desc
36 43
44
37 class PresubmitMock(object): 45 class PresubmitMock(object):
38 def __init__(self, *args, **kwargs): 46 def __init__(self, *args, **kwargs):
39 self.reviewers = [] 47 self.reviewers = []
40 @staticmethod 48 @staticmethod
41 def should_continue(): 49 def should_continue():
42 return True 50 return True
43 51
44 52
45 class RietveldMock(object): 53 class RietveldMock(object):
46 def __init__(self, *args, **kwargs): 54 def __init__(self, *args, **kwargs):
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 234
227 235
228 class TestGitCl(TestCase): 236 class TestGitCl(TestCase):
229 def setUp(self): 237 def setUp(self):
230 super(TestGitCl, self).setUp() 238 super(TestGitCl, self).setUp()
231 self.calls = [] 239 self.calls = []
232 self._calls_done = [] 240 self._calls_done = []
233 self.mock(subprocess2, 'call', self._mocked_call) 241 self.mock(subprocess2, 'call', self._mocked_call)
234 self.mock(subprocess2, 'check_call', self._mocked_call) 242 self.mock(subprocess2, 'check_call', self._mocked_call)
235 self.mock(subprocess2, 'check_output', self._mocked_call) 243 self.mock(subprocess2, 'check_output', self._mocked_call)
236 self.mock(subprocess2, 'communicate', self._mocked_call) 244 self.mock(subprocess2, 'communicate',
245 lambda *a, **kw: ([self._mocked_call(*a, **kw), ''], 0))
237 self.mock(git_cl.gclient_utils, 'CheckCallAndFilter', self._mocked_call) 246 self.mock(git_cl.gclient_utils, 'CheckCallAndFilter', self._mocked_call)
238 self.mock(git_common, 'is_dirty_git_tree', lambda x: False) 247 self.mock(git_common, 'is_dirty_git_tree', lambda x: False)
239 self.mock(git_common, 'get_or_create_merge_base', 248 self.mock(git_common, 'get_or_create_merge_base',
240 lambda *a: ( 249 lambda *a: (
241 self._mocked_call(['get_or_create_merge_base']+list(a)))) 250 self._mocked_call(['get_or_create_merge_base']+list(a))))
242 self.mock(git_cl, 'BranchExists', lambda _: True) 251 self.mock(git_cl, 'BranchExists', lambda _: True)
243 self.mock(git_cl, 'FindCodereviewSettingsFile', lambda: '') 252 self.mock(git_cl, 'FindCodereviewSettingsFile', lambda: '')
244 self.mock(git_cl, 'ask_for_data', self._mocked_call) 253 self.mock(git_cl, 'ask_for_data', self._mocked_call)
245 self.mock(git_cl.presubmit_support, 'DoPresubmitChecks', PresubmitMock) 254 self.mock(git_cl.presubmit_support, 'DoPresubmitChecks', PresubmitMock)
246 self.mock(git_cl.rietveld, 'Rietveld', RietveldMock) 255 self.mock(git_cl.rietveld, 'Rietveld', RietveldMock)
(...skipping 15 matching lines...) Expand all
262 if not self.has_failed(): 271 if not self.has_failed():
263 self.assertEquals([], self.calls) 272 self.assertEquals([], self.calls)
264 finally: 273 finally:
265 super(TestGitCl, self).tearDown() 274 super(TestGitCl, self).tearDown()
266 275
267 def _mocked_call(self, *args, **_kwargs): 276 def _mocked_call(self, *args, **_kwargs):
268 self.assertTrue( 277 self.assertTrue(
269 self.calls, 278 self.calls,
270 '@%d Expected: <Missing> Actual: %r' % (len(self._calls_done), args)) 279 '@%d Expected: <Missing> Actual: %r' % (len(self._calls_done), args))
271 top = self.calls.pop(0) 280 top = self.calls.pop(0)
272 if len(top) > 2 and top[2]:
273 raise top[2]
274 expected_args, result = top 281 expected_args, result = top
275 282
276 # Also logs otherwise it could get caught in a try/finally and be hard to 283 # Also logs otherwise it could get caught in a try/finally and be hard to
277 # diagnose. 284 # diagnose.
278 if expected_args != args: 285 if expected_args != args:
279 N = 5 286 N = 5
280 prior_calls = '\n '.join( 287 prior_calls = '\n '.join(
281 '@%d: %r' % (len(self._calls_done) - N + i, c[0]) 288 '@%d: %r' % (len(self._calls_done) - N + i, c[0])
282 for i, c in enumerate(self._calls_done[-N:])) 289 for i, c in enumerate(self._calls_done[-N:]))
283 following_calls = '\n '.join( 290 following_calls = '\n '.join(
284 '@%d: %r' % (len(self._calls_done) + i + 1, c[0]) 291 '@%d: %r' % (len(self._calls_done) + i + 1, c[0])
285 for i, c in enumerate(self.calls[:N])) 292 for i, c in enumerate(self.calls[:N]))
286 extended_msg = ( 293 extended_msg = (
287 'A few prior calls:\n %s\n\n' 294 'A few prior calls:\n %s\n\n'
288 'This (expected):\n @%d: %r\n' 295 'This (expected):\n @%d: %r\n'
289 'This (actual):\n @%d: %r\n\n' 296 'This (actual):\n @%d: %r\n\n'
290 'A few following expected calls:\n %s' % 297 'A few following expected calls:\n %s' %
291 (prior_calls, len(self._calls_done), expected_args, 298 (prior_calls, len(self._calls_done), expected_args,
292 len(self._calls_done), args, following_calls)) 299 len(self._calls_done), args, following_calls))
293 git_cl.logging.error(extended_msg) 300 git_cl.logging.error(extended_msg)
294 301
295 self.fail('@%d\n' 302 self.fail('@%d\n'
296 ' Expected: %r\n' 303 ' Expected: %r\n'
297 ' Actual: %r' % ( 304 ' Actual: %r' % (
298 len(self._calls_done), expected_args, args)) 305 len(self._calls_done), expected_args, args))
299 306
300 self._calls_done.append(top) 307 self._calls_done.append(top)
308 if isinstance(result, Exception):
309 raise result
301 return result 310 return result
302 311
303 @classmethod 312 @classmethod
304 def _is_gerrit_calls(cls, gerrit=False): 313 def _is_gerrit_calls(cls, gerrit=False):
305 return [((['git', 'config', 'rietveld.autoupdate'],), ''), 314 return [((['git', 'config', 'rietveld.autoupdate'],), ''),
306 ((['git', 'config', 'gerrit.host'],), 'True' if gerrit else '')] 315 ((['git', 'config', 'gerrit.host'],), 'True' if gerrit else '')]
307 316
308 @classmethod 317 @classmethod
309 def _upload_calls(cls, similarity, find_copies, private): 318 def _upload_calls(cls, similarity, find_copies, private):
310 return (cls._git_base_calls(similarity, find_copies) + 319 return (cls._git_base_calls(similarity, find_copies) +
311 cls._git_upload_calls(private)) 320 cls._git_upload_calls(private))
312 321
313 @classmethod 322 @classmethod
314 def _upload_no_rev_calls(cls, similarity, find_copies): 323 def _upload_no_rev_calls(cls, similarity, find_copies):
315 return (cls._git_base_calls(similarity, find_copies) + 324 return (cls._git_base_calls(similarity, find_copies) +
316 cls._git_upload_no_rev_calls()) 325 cls._git_upload_no_rev_calls())
317 326
318 @classmethod 327 @classmethod
319 def _git_base_calls(cls, similarity, find_copies): 328 def _git_base_calls(cls, similarity, find_copies):
320 if similarity is None: 329 if similarity is None:
321 similarity = '50' 330 similarity = '50'
322 similarity_call = ((['git', 'config', '--int', '--get', 331 similarity_call = ((['git', 'config', '--int',
323 'branch.master.git-cl-similarity'],), '') 332 'branch.master.git-cl-similarity'],), CERR1)
324 else: 333 else:
325 similarity_call = ((['git', 'config', '--int', 334 similarity_call = ((['git', 'config', '--int',
326 'branch.master.git-cl-similarity', similarity],), '') 335 'branch.master.git-cl-similarity', similarity],), '')
327 336
328 if find_copies is None: 337 if find_copies is None:
329 find_copies = True 338 find_copies = True
330 find_copies_call = ((['git', 'config', '--int', '--get', 339 find_copies_call = ((['git', 'config', '--bool',
331 'branch.master.git-find-copies'],), '') 340 'branch.master.git-find-copies'],), CERR1)
332 else: 341 else:
333 val = str(int(find_copies)) 342 val = str(find_copies).lower()
334 find_copies_call = ((['git', 'config', '--int', 343 find_copies_call = ((['git', 'config', '--bool',
335 'branch.master.git-find-copies', val],), '') 344 'branch.master.git-find-copies', val],), '')
336 345
337 if find_copies: 346 if find_copies:
338 stat_call = ((['git', 'diff', '--no-ext-diff', '--stat', 347 stat_call = ((['git', 'diff', '--no-ext-diff', '--stat',
339 '--find-copies-harder', '-l100000', '-C'+similarity, 348 '--find-copies-harder', '-l100000', '-C'+similarity,
340 'fake_ancestor_sha', 'HEAD'],), '+dat') 349 'fake_ancestor_sha', 'HEAD'],), '+dat')
341 else: 350 else:
342 stat_call = ((['git', 'diff', '--no-ext-diff', '--stat', 351 stat_call = ((['git', 'diff', '--no-ext-diff', '--stat',
343 '-M'+similarity, 'fake_ancestor_sha', 'HEAD'],), '+dat') 352 '-M'+similarity, 'fake_ancestor_sha', 'HEAD'],), '+dat')
344 353
345 return [ 354 return [
346 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), 355 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
347 similarity_call, 356 similarity_call,
348 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), 357 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
349 find_copies_call, 358 find_copies_call,
350 ] + cls._is_gerrit_calls() + [ 359 ] + cls._is_gerrit_calls() + [
351 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), 360 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
352 ((['git', 'config', 'branch.master.rietveldissue'],), ''), 361 ((['git', 'config', '--int', 'branch.master.rietveldissue'],), CERR1),
353 ((['git', 'config', 'branch.master.gerritissue'],), ''), 362 ((['git', 'config', '--int', 'branch.master.gerritissue'],), CERR1),
354 ((['git', 'config', 'rietveld.server'],), 363 ((['git', 'config', 'rietveld.server'],),
355 'codereview.example.com'), 364 'codereview.example.com'),
356 ((['git', 'config', 'branch.master.merge'],), 'master'), 365 ((['git', 'config', 'branch.master.merge'],), 'master'),
357 ((['git', 'config', 'branch.master.remote'],), 'origin'), 366 ((['git', 'config', 'branch.master.remote'],), 'origin'),
358 ((['get_or_create_merge_base', 'master', 'master'],), 367 ((['get_or_create_merge_base', 'master', 'master'],),
359 'fake_ancestor_sha'), 368 'fake_ancestor_sha'),
360 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [ 369 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [
361 ((['git', 'rev-parse', '--show-cdup'],), ''), 370 ((['git', 'rev-parse', '--show-cdup'],), ''),
362 ((['git', 'rev-parse', 'HEAD'],), '12345'), 371 ((['git', 'rev-parse', 'HEAD'],), '12345'),
363 ((['git', 'diff', '--name-status', '--no-renames', '-r', 372 ((['git', 'diff', '--name-status', '--no-renames', '-r',
364 'fake_ancestor_sha...', '.'],), 373 'fake_ancestor_sha...', '.'],),
365 'M\t.gitignore\n'), 374 'M\t.gitignore\n'),
366 ((['git', 'config', 'branch.master.rietveldpatchset'],), 375 ((['git', 'config', '--int', 'branch.master.rietveldpatchset'],), CERR1),
367 ''),
368 ((['git', 'log', '--pretty=format:%s%n%n%b', 376 ((['git', 'log', '--pretty=format:%s%n%n%b',
369 'fake_ancestor_sha...'],), 377 'fake_ancestor_sha...'],),
370 'foo'), 378 'foo'),
371 ((['git', 'config', 'user.email'],), 'me@example.com'), 379 ((['git', 'config', 'user.email'],), 'me@example.com'),
372 stat_call, 380 stat_call,
373 ((['git', 'log', '--pretty=format:%s\n\n%b', 381 ((['git', 'log', '--pretty=format:%s\n\n%b',
374 'fake_ancestor_sha..HEAD'],), 382 'fake_ancestor_sha..HEAD'],),
375 'desc\n'), 383 'desc\n'),
376 ((['git', 'config', 'rietveld.bug-prefix'],), ''), 384 ((['git', 'config', 'rietveld.bug-prefix'],), ''),
377 ] 385 ]
(...skipping 18 matching lines...) Expand all
396 ((['git', 'config', 'core.editor'],), ''), 404 ((['git', 'config', 'core.editor'],), ''),
397 ] + cc_call + private_call + [ 405 ] + cc_call + private_call + [
398 ((['git', 'config', 'branch.master.base-url'],), ''), 406 ((['git', 'config', 'branch.master.base-url'],), ''),
399 ((['git', 'config', 'rietveld.pending-ref-prefix'],), ''), 407 ((['git', 'config', 'rietveld.pending-ref-prefix'],), ''),
400 ((['git', 408 ((['git',
401 'config', '--local', '--get-regexp', '^svn-remote\\.'],), 409 'config', '--local', '--get-regexp', '^svn-remote\\.'],),
402 (('', None), 0)), 410 (('', None), 0)),
403 ((['git', 'rev-parse', '--show-cdup'],), ''), 411 ((['git', 'rev-parse', '--show-cdup'],), ''),
404 ((['git', 'svn', 'info'],), ''), 412 ((['git', 'svn', 'info'],), ''),
405 ((['git', 'config', 'rietveld.project'],), ''), 413 ((['git', 'config', 'rietveld.project'],), ''),
406 ((['git', 414 ((['git', 'config', '--int', 'branch.master.rietveldissue', '1'],), ''),
407 'config', 'branch.master.rietveldissue', '1'],), ''),
408 ((['git', 'config', 'branch.master.rietveldserver', 415 ((['git', 'config', 'branch.master.rietveldserver',
409 'https://codereview.example.com'],), ''), 416 'https://codereview.example.com'],), ''),
410 ((['git', 417 ((['git',
411 'config', 'branch.master.rietveldpatchset', '2'],), ''), 418 'config', '--int', 'branch.master.rietveldpatchset', '2'],), ''),
412 ] + cls._git_post_upload_calls() 419 ] + cls._git_post_upload_calls()
413 420
414 @classmethod 421 @classmethod
415 def _git_post_upload_calls(cls): 422 def _git_post_upload_calls(cls):
416 return [ 423 return [
417 ((['git', 'rev-parse', 'HEAD'],), 'hash'), 424 ((['git', 'rev-parse', 'HEAD'],), 'hash'),
418 ((['git', 'symbolic-ref', 'HEAD'],), 'hash'), 425 ((['git', 'symbolic-ref', 'HEAD'],), 'hash'),
419 ((['git', 426 ((['git',
420 'config', 'branch.hash.last-upload-hash', 'hash'],), ''), 427 'config', 'branch.hash.last-upload-hash', 'hash'],), ''),
421 ((['git', 'config', 'rietveld.run-post-upload-hook'],), ''), 428 ((['git', 'config', 'rietveld.run-post-upload-hook'],), ''),
422 ] 429 ]
423 430
424 @staticmethod 431 @staticmethod
425 def _git_sanity_checks(diff_base, working_branch, get_remote_branch=True): 432 def _git_sanity_checks(diff_base, working_branch, get_remote_branch=True):
426 fake_ancestor = 'fake_ancestor' 433 fake_ancestor = 'fake_ancestor'
427 fake_cl = 'fake_cl_for_patch' 434 fake_cl = 'fake_cl_for_patch'
428 return [ 435 return [
429 ((['git', 436 ((['git',
430 'rev-parse', '--verify', diff_base],), fake_ancestor), 437 'rev-parse', '--verify', diff_base],), fake_ancestor),
431 ((['git', 438 ((['git',
432 'merge-base', fake_ancestor, 'HEAD'],), fake_ancestor), 439 'merge-base', fake_ancestor, 'HEAD'],), fake_ancestor),
433 ((['git', 440 ((['git',
434 'rev-list', '^' + fake_ancestor, 'HEAD'],), fake_cl), 441 'rev-list', '^' + fake_ancestor, 'HEAD'],), fake_cl),
435 # Mock a config miss (error code 1) 442 # Mock a config miss (error code 1)
436 ((['git', 443 ((['git',
437 'config', 'gitcl.remotebranch'],), (('', None), 1)), 444 'config', 'gitcl.remotebranch'],), CERR1),
438 ] + ([ 445 ] + ([
439 # Call to GetRemoteBranch() 446 # Call to GetRemoteBranch()
440 ((['git', 447 ((['git',
441 'config', 'branch.%s.merge' % working_branch],), 448 'config', 'branch.%s.merge' % working_branch],),
442 'refs/heads/master'), 449 'refs/heads/master'),
443 ((['git', 450 ((['git',
444 'config', 'branch.%s.remote' % working_branch],), 'origin'), 451 'config', 'branch.%s.remote' % working_branch],), 'origin'),
445 ] if get_remote_branch else []) + [ 452 ] if get_remote_branch else []) + [
446 ((['git', 'rev-list', '^' + fake_ancestor, 453 ((['git', 'rev-list', '^' + fake_ancestor,
447 'refs/remotes/origin/master'],), ''), 454 'refs/remotes/origin/master'],), ''),
448 ] 455 ]
449 456
450 @classmethod 457 @classmethod
451 def _dcommit_calls_1(cls): 458 def _dcommit_calls_1(cls):
452 return [ 459 return [
453 ((['git', 'config', 'rietveld.autoupdate'],), 460 ((['git', 'config', 'rietveld.autoupdate'],),
454 ''), 461 ''),
455 ((['git', 'config', 'rietveld.pending-ref-prefix'],), 462 ((['git', 'config', 'rietveld.pending-ref-prefix'],),
456 ''), 463 ''),
457 ((['git', 464 ((['git',
458 'config', '--local', '--get-regexp', '^svn-remote\\.'],), 465 'config', '--local', '--get-regexp', '^svn-remote\\.'],),
459 ((('svn-remote.svn.url svn://svn.chromium.org/chrome\n' 466 ((('svn-remote.svn.url svn://svn.chromium.org/chrome\n'
460 'svn-remote.svn.fetch trunk/src:refs/remotes/origin/master'), 467 'svn-remote.svn.fetch trunk/src:refs/remotes/origin/master'),
461 None), 468 None),
462 0)), 469 0)),
463 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'), 470 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
464 ((['git', 'config', '--int', '--get', 471 ((['git', 'config', '--int',
465 'branch.working.git-cl-similarity'],), ''), 472 'branch.working.git-cl-similarity'],), CERR1),
466 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'), 473 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
467 ((['git', 'config', '--int', '--get', 474 ((['git', 'config', '--bool',
468 'branch.working.git-find-copies'],), ''), 475 'branch.working.git-find-copies'],), CERR1),
469 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'), 476 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
470 ((['git', 477 ((['git',
471 'config', 'branch.working.rietveldissue'],), '12345'), 478 'config', '--int', 'branch.working.rietveldissue'],), '12345'),
472 ((['git', 479 ((['git',
473 'config', 'rietveld.server'],), 'codereview.example.com'), 480 'config', 'rietveld.server'],), 'codereview.example.com'),
474 ((['git', 481 ((['git',
475 'config', 'branch.working.merge'],), 'refs/heads/master'), 482 'config', 'branch.working.merge'],), 'refs/heads/master'),
476 ((['git', 'config', 'branch.working.remote'],), 'origin'), 483 ((['git', 'config', 'branch.working.remote'],), 'origin'),
477 ((['git', 'config', 'branch.working.merge'],), 484 ((['git', 'config', 'branch.working.merge'],),
478 'refs/heads/master'), 485 'refs/heads/master'),
479 ((['git', 'config', 'branch.working.remote'],), 'origin'), 486 ((['git', 'config', 'branch.working.remote'],), 'origin'),
480 ((['git', 'rev-list', '--merges', 487 ((['git', 'rev-list', '--merges',
481 '--grep=^SVN changes up to revision [0-9]*$', 488 '--grep=^SVN changes up to revision [0-9]*$',
(...skipping 16 matching lines...) Expand all
498 def _dcommit_calls_normal(cls): 505 def _dcommit_calls_normal(cls):
499 return [ 506 return [
500 ((['git', 'rev-parse', '--show-cdup'],), ''), 507 ((['git', 'rev-parse', '--show-cdup'],), ''),
501 ((['git', 'rev-parse', 'HEAD'],), 508 ((['git', 'rev-parse', 'HEAD'],),
502 '00ff397798ea57439712ed7e04ab96e13969ef40'), 509 '00ff397798ea57439712ed7e04ab96e13969ef40'),
503 ((['git', 510 ((['git',
504 'diff', '--name-status', '--no-renames', '-r', 'fake_ancestor_sha...', 511 'diff', '--name-status', '--no-renames', '-r', 'fake_ancestor_sha...',
505 '.'],), 512 '.'],),
506 'M\tPRESUBMIT.py'), 513 'M\tPRESUBMIT.py'),
507 ((['git', 514 ((['git',
508 'config', 'branch.working.rietveldpatchset'],), '31137'), 515 'config', '--int', 'branch.working.rietveldpatchset'],), '31137'),
509 ((['git', 'config', 'branch.working.rietveldserver'],), 516 ((['git', 'config', 'branch.working.rietveldserver'],),
510 'codereview.example.com'), 517 'codereview.example.com'),
511 ((['git', 'config', 'user.email'],), 'author@example.com'), 518 ((['git', 'config', 'user.email'],), 'author@example.com'),
512 ((['git', 'config', 'rietveld.tree-status-url'],), ''), 519 ((['git', 'config', 'rietveld.tree-status-url'],), ''),
513 ] 520 ]
514 521
515 @classmethod 522 @classmethod
516 def _dcommit_calls_bypassed(cls): 523 def _dcommit_calls_bypassed(cls):
517 return [ 524 return [
518 ((['git', 'config', 'branch.working.rietveldserver'],), 525 ((['git', 'config', 'branch.working.rietveldserver'],),
519 'codereview.example.com'), 526 'codereview.example.com'),
520 ] 527 ]
521 528
522 @classmethod 529 @classmethod
523 def _dcommit_calls_3(cls): 530 def _dcommit_calls_3(cls):
524 return [ 531 return [
525 ((['git', 532 ((['git',
526 'diff', '--no-ext-diff', '--stat', '--find-copies-harder', 533 'diff', '--no-ext-diff', '--stat', '--find-copies-harder',
527 '-l100000', '-C50', 'fake_ancestor_sha', 534 '-l100000', '-C50', 'fake_ancestor_sha',
528 'refs/heads/working'],), 535 'refs/heads/working'],),
529 (' PRESUBMIT.py | 2 +-\n' 536 (' PRESUBMIT.py | 2 +-\n'
530 ' 1 files changed, 1 insertions(+), 1 deletions(-)\n')), 537 ' 1 files changed, 1 insertions(+), 1 deletions(-)\n')),
531 ((['git', 'show-ref', '--quiet', '--verify', 538 ((['git', 'show-ref', '--quiet', '--verify',
532 'refs/heads/git-cl-commit'],), 539 'refs/heads/git-cl-commit'],), ''),
533 (('', None), 0)),
534 ((['git', 'branch', '-D', 'git-cl-commit'],), ''), 540 ((['git', 'branch', '-D', 'git-cl-commit'],), ''),
535 ((['git', 'show-ref', '--quiet', '--verify', 541 ((['git', 'show-ref', '--quiet', '--verify',
536 'refs/heads/git-cl-cherry-pick'],), ''), 542 'refs/heads/git-cl-cherry-pick'],), CERR1),
537 ((['git', 'rev-parse', '--show-cdup'],), '\n'), 543 ((['git', 'rev-parse', '--show-cdup'],), '\n'),
538 ((['git', 'checkout', '-q', '-b', 'git-cl-commit'],), ''), 544 ((['git', 'checkout', '-q', '-b', 'git-cl-commit'],), ''),
539 ((['git', 'reset', '--soft', 'fake_ancestor_sha'],), ''), 545 ((['git', 'reset', '--soft', 'fake_ancestor_sha'],), ''),
540 ((['git', 'commit', '-m', 546 ((['git', 'commit', '-m',
541 'Issue: 12345\n\nR=john@chromium.org\n\n' 547 'Issue: 12345\n\nR=john@chromium.org\n\n'
542 'Review URL: https://codereview.example.com/12345 .'],), 548 'Review URL: https://codereview.example.com/12345 .'],),
543 ''), 549 ''),
544 ((['git', 'config', 'rietveld.force-https-commit-url'],), ''), 550 ((['git', 'config', 'rietveld.force-https-commit-url'],), ''),
545 ((['git', 551 ((['git',
546 'svn', 'dcommit', '-C50', '--no-rebase', '--rmdir'],), 552 'svn', 'dcommit', '-C50', '--no-rebase', '--rmdir'],),
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
731 self._dcommit_calls_3()) 737 self._dcommit_calls_3())
732 git_cl.main(['dcommit', '--bypass-hooks']) 738 git_cl.main(['dcommit', '--bypass-hooks'])
733 739
734 740
735 @classmethod 741 @classmethod
736 def _gerrit_ensure_auth_calls(cls, issue=None, skip_auth_check=False): 742 def _gerrit_ensure_auth_calls(cls, issue=None, skip_auth_check=False):
737 cmd = ['git', 'config', '--bool', 'gerrit.skip-ensure-authenticated'] 743 cmd = ['git', 'config', '--bool', 'gerrit.skip-ensure-authenticated']
738 if skip_auth_check: 744 if skip_auth_check:
739 return [((cmd, ), 'true')] 745 return [((cmd, ), 'true')]
740 746
741 calls = [((cmd, ), '', subprocess2.CalledProcessError(1, '', '', '', ''))] 747 calls = [((cmd, ), CERR1)]
742 if issue: 748 if issue:
743 calls.extend([ 749 calls.extend([
744 ((['git', 'config', 'branch.master.gerritserver'],), ''), 750 ((['git', 'config', 'branch.master.gerritserver'],), ''),
745 ]) 751 ])
746 calls.extend([ 752 calls.extend([
747 ((['git', 'config', 'branch.master.merge'],), 'refs/heads/master'), 753 ((['git', 'config', 'branch.master.merge'],), 'refs/heads/master'),
748 ((['git', 'config', 'branch.master.remote'],), 'origin'), 754 ((['git', 'config', 'branch.master.remote'],), 'origin'),
749 ((['git', 'config', 'remote.origin.url'],), 755 ((['git', 'config', 'remote.origin.url'],),
750 'https://chromium.googlesource.com/my/repo'), 756 'https://chromium.googlesource.com/my/repo'),
751 ((['git', 'config', 'remote.origin.url'],), 757 ((['git', 'config', 'remote.origin.url'],),
752 'https://chromium.googlesource.com/my/repo'), 758 'https://chromium.googlesource.com/my/repo'),
753 ]) 759 ])
754 return calls 760 return calls
755 761
756 @classmethod 762 @classmethod
757 def _gerrit_base_calls(cls, issue=None): 763 def _gerrit_base_calls(cls, issue=None):
758 return [ 764 return [
759 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), 765 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
760 ((['git', 'config', '--int', '--get', 766 ((['git', 'config', '--int', 'branch.master.git-cl-similarity'],),
761 'branch.master.git-cl-similarity'],), ''), 767 CERR1),
762 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), 768 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
763 ((['git', 'config', '--int', '--get', 769 ((['git', 'config', '--bool', 'branch.master.git-find-copies'],),
764 'branch.master.git-find-copies'],), ''), 770 CERR1),
765 ] + cls._is_gerrit_calls(True) + [ 771 ] + cls._is_gerrit_calls(True) + [
766 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), 772 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
767 ((['git', 'config', 'branch.master.rietveldissue'],), ''), 773 ((['git', 'config', '--int', 'branch.master.rietveldissue'],), CERR1),
768 ((['git', 'config', 'branch.master.gerritissue'],), 774 ((['git', 'config', '--int', 'branch.master.gerritissue'],),
769 '' if issue is None else str(issue)), 775 CERR1 if issue is None else str(issue)),
770 ((['git', 'config', 'branch.master.merge'],), 'refs/heads/master'), 776 ((['git', 'config', 'branch.master.merge'],), 'refs/heads/master'),
771 ((['git', 'config', 'branch.master.remote'],), 'origin'), 777 ((['git', 'config', 'branch.master.remote'],), 'origin'),
772 ((['get_or_create_merge_base', 'master', 778 ((['get_or_create_merge_base', 'master',
773 'refs/remotes/origin/master'],), 779 'refs/remotes/origin/master'],),
774 'fake_ancestor_sha'), 780 'fake_ancestor_sha'),
775 # Calls to verify branch point is ancestor 781 # Calls to verify branch point is ancestor
776 ] + (cls._gerrit_ensure_auth_calls(issue=issue) + 782 ] + (cls._gerrit_ensure_auth_calls(issue=issue) +
777 cls._git_sanity_checks('fake_ancestor_sha', 'master', 783 cls._git_sanity_checks('fake_ancestor_sha', 'master',
778 get_remote_branch=False)) + [ 784 get_remote_branch=False)) + [
779 ((['git', 'rev-parse', '--show-cdup'],), ''), 785 ((['git', 'rev-parse', '--show-cdup'],), ''),
780 ((['git', 'rev-parse', 'HEAD'],), '12345'), 786 ((['git', 'rev-parse', 'HEAD'],), '12345'),
781 787
782 ((['git', 788 ((['git',
783 'diff', '--name-status', '--no-renames', '-r', 789 'diff', '--name-status', '--no-renames', '-r',
784 'fake_ancestor_sha...', '.'],), 790 'fake_ancestor_sha...', '.'],),
785 'M\t.gitignore\n'), 791 'M\t.gitignore\n'),
786 ((['git', 'config', 'branch.master.gerritpatchset'],), ''), 792 ((['git', 'config', '--int', 'branch.master.gerritpatchset'],), CERR1),
787 ] + ([] if issue else [ 793 ] + ([] if issue else [
788 ((['git', 794 ((['git',
789 'log', '--pretty=format:%s%n%n%b', 'fake_ancestor_sha...'],), 795 'log', '--pretty=format:%s%n%n%b', 'fake_ancestor_sha...'],),
790 'foo'), 796 'foo'),
791 ]) + [ 797 ]) + [
792 ((['git', 'config', 'user.email'],), 'me@example.com'), 798 ((['git', 'config', 'user.email'],), 'me@example.com'),
793 ((['git', 799 ((['git',
794 'diff', '--no-ext-diff', '--stat', '--find-copies-harder', 800 'diff', '--no-ext-diff', '--stat', '--find-copies-harder',
795 '-l100000', '-C50', 'fake_ancestor_sha', 'HEAD'],), 801 '-l100000', '-C50', 'fake_ancestor_sha', 'HEAD'],),
796 '+dat'), 802 '+dat'),
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
895 'remote: Processing changes: new: 1, done\n' 901 'remote: Processing changes: new: 1, done\n'
896 'remote:\n' 902 'remote:\n'
897 'remote: New Changes:\n' 903 'remote: New Changes:\n'
898 'remote: https://chromium-review.googlesource.com/123456 XXX.\n' 904 'remote: https://chromium-review.googlesource.com/123456 XXX.\n'
899 'remote:\n' 905 'remote:\n'
900 'To https://chromium.googlesource.com/yyy/zzz\n' 906 'To https://chromium.googlesource.com/yyy/zzz\n'
901 ' * [new branch] hhhh -> refs/for/refs/heads/master\n')), 907 ' * [new branch] hhhh -> refs/for/refs/heads/master\n')),
902 ] 908 ]
903 if squash: 909 if squash:
904 calls += [ 910 calls += [
905 ((['git', 'config', 'branch.master.gerritissue', '123456'],), ''), 911 ((['git', 'config', '--int', 'branch.master.gerritissue', '123456'],),
912 ''),
906 ((['git', 'config', 'branch.master.gerritserver', 913 ((['git', 'config', 'branch.master.gerritserver',
907 'https://chromium-review.googlesource.com'],), ''), 914 'https://chromium-review.googlesource.com'],), ''),
908 ((['git', 'config', 'branch.master.gerritsquashhash', 915 ((['git', 'config', 'branch.master.gerritsquashhash',
909 'abcdef0123456789'],), ''), 916 'abcdef0123456789'],), ''),
910 ] 917 ]
911 calls += cls._git_post_upload_calls() 918 calls += cls._git_post_upload_calls()
912 return calls 919 return calls
913 920
914 def _run_gerrit_upload_test( 921 def _run_gerrit_upload_test(
915 self, 922 self,
916 upload_args, 923 upload_args,
917 description, 924 description,
(...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after
1258 }) 1265 })
1259 self.mock(git_cl.Changelist, 'GetDescription', 1266 self.mock(git_cl.Changelist, 'GetDescription',
1260 lambda *args: 'Description') 1267 lambda *args: 'Description')
1261 self.mock(git_cl, 'IsGitVersionAtLeast', lambda *args: True) 1268 self.mock(git_cl, 'IsGitVersionAtLeast', lambda *args: True)
1262 1269
1263 self.calls = self.calls or [] 1270 self.calls = self.calls or []
1264 if not force_codereview: 1271 if not force_codereview:
1265 # These calls detect codereview to use. 1272 # These calls detect codereview to use.
1266 self.calls += [ 1273 self.calls += [
1267 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), 1274 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
1268 ((['git', 'config', 'branch.master.rietveldissue'],), ''), 1275 ((['git', 'config', '--int', 'branch.master.rietveldissue'],), CERR1),
1269 ((['git', 'config', 'branch.master.gerritissue'],), ''), 1276 ((['git', 'config', '--int', 'branch.master.gerritissue'],), CERR1),
1270 ((['git', 'config', 'rietveld.autoupdate'],), ''), 1277 ((['git', 'config', 'rietveld.autoupdate'],), CERR1),
1271 ] 1278 ]
1272 1279
1273 if is_gerrit: 1280 if is_gerrit:
1274 if not force_codereview: 1281 if not force_codereview:
1275 self.calls += [ 1282 self.calls += [
1276 ((['git', 'config', 'gerrit.host'],), 'true'), 1283 ((['git', 'config', 'gerrit.host'],), 'true'),
1277 ] 1284 ]
1278 else: 1285 else:
1279 self.calls += [ 1286 self.calls += [
1280 ((['git', 'config', 'gerrit.host'],), ''), 1287 ((['git', 'config', 'gerrit.host'],), CERR1),
1281 ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'), 1288 ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'),
1282 ((['git', 'rev-parse', '--show-cdup'],), ''), 1289 ((['git', 'rev-parse', '--show-cdup'],), ''),
1283 ((['sed', '-e', 's|^--- a/|--- |; s|^+++ b/|+++ |'],), ''), 1290 ((['sed', '-e', 's|^--- a/|--- |; s|^+++ b/|+++ |'],), ''),
1284 ] 1291 ]
1285 1292
1286 def _common_patch_successful(self): 1293 def _common_patch_successful(self):
1287 self._patch_common() 1294 self._patch_common()
1288 self.calls += [ 1295 self.calls += [
1289 ((['git', 'apply', '--index', '-p0', '--3way'],), ''), 1296 ((['git', 'apply', '--index', '-p0', '--3way'],), ''),
1290 ((['git', 'commit', '-m', 1297 ((['git', 'commit', '-m',
1291 'Description\n\n' + 1298 'Description\n\n' +
1292 'patch from issue 123456 at patchset 60001 ' + 1299 'patch from issue 123456 at patchset 60001 ' +
1293 '(http://crrev.com/123456#ps60001)'],), ''), 1300 '(http://crrev.com/123456#ps60001)'],), ''),
1294 ((['git', 'config', 'branch.master.rietveldissue', '123456'],), ''), 1301 ((['git', 'config', '--int', 'branch.master.rietveldissue', '123456'],),
1295 ((['git', 'config', 'branch.master.rietveldserver'],), ''), 1302 ''),
1303 ((['git', 'config', 'branch.master.rietveldserver'],), CERR1),
1296 ((['git', 'config', 'branch.master.rietveldserver', 1304 ((['git', 'config', 'branch.master.rietveldserver',
1297 'https://codereview.example.com'],), ''), 1305 'https://codereview.example.com'],), ''),
1298 ((['git', 'config', 'branch.master.rietveldpatchset', '60001'],), ''), 1306 ((['git', 'config', '--int', 'branch.master.rietveldpatchset', '60001'],),
1307 ''),
1299 ] 1308 ]
1300 1309
1301 def test_patch_successful(self): 1310 def test_patch_successful(self):
1302 self._common_patch_successful() 1311 self._common_patch_successful()
1303 self.assertEqual(git_cl.main(['patch', '123456']), 0) 1312 self.assertEqual(git_cl.main(['patch', '123456']), 0)
1304 1313
1305 def test_patch_successful_new_branch(self): 1314 def test_patch_successful_new_branch(self):
1306 self.calls = [ ((['git', 'new-branch', 'master'],), ''), ] 1315 self.calls = [ ((['git', 'new-branch', 'master'],), ''), ]
1307 self._common_patch_successful() 1316 self._common_patch_successful()
1308 self.assertEqual(git_cl.main(['patch', '-b', 'master', '123456']), 0) 1317 self.assertEqual(git_cl.main(['patch', '-b', 'master', '123456']), 0)
1309 1318
1310 def test_patch_conflict(self): 1319 def test_patch_conflict(self):
1311 self._patch_common() 1320 self._patch_common()
1312 self.calls += [ 1321 self.calls += [
1313 ((['git', 'apply', '--index', '-p0', '--3way'],), '', 1322 ((['git', 'apply', '--index', '-p0', '--3way'],), CERR1),
1314 subprocess2.CalledProcessError(1, '', '', '', '')),
1315 ] 1323 ]
1316 self.assertNotEqual(git_cl.main(['patch', '123456']), 0) 1324 self.assertNotEqual(git_cl.main(['patch', '123456']), 0)
1317 1325
1318 def test_gerrit_patch_successful(self): 1326 def test_gerrit_patch_successful(self):
1319 self._patch_common(is_gerrit=True) 1327 self._patch_common(is_gerrit=True)
1320 self.calls += [ 1328 self.calls += [
1321 ((['git', 'fetch', 'https://chromium.googlesource.com/my/repo', 1329 ((['git', 'fetch', 'https://chromium.googlesource.com/my/repo',
1322 'refs/changes/56/123456/7'],), ''), 1330 'refs/changes/56/123456/7'],), ''),
1323 ((['git', 'cherry-pick', 'FETCH_HEAD'],), ''), 1331 ((['git', 'cherry-pick', 'FETCH_HEAD'],), ''),
1324 ((['git', 'config', 'branch.master.gerritissue', '123456'],), ''), 1332 ((['git', 'config', '--int', 'branch.master.gerritissue', '123456'],),
1333 ''),
1325 ((['git', 'config', 'branch.master.gerritserver'],), ''), 1334 ((['git', 'config', 'branch.master.gerritserver'],), ''),
1326 ((['git', 'config', 'branch.master.merge'],), 'master'), 1335 ((['git', 'config', 'branch.master.merge'],), 'master'),
1327 ((['git', 'config', 'branch.master.remote'],), 'origin'), 1336 ((['git', 'config', 'branch.master.remote'],), 'origin'),
1328 ((['git', 'config', 'remote.origin.url'],), 1337 ((['git', 'config', 'remote.origin.url'],),
1329 'https://chromium.googlesource.com/my/repo'), 1338 'https://chromium.googlesource.com/my/repo'),
1330 ((['git', 'config', 'branch.master.gerritserver', 1339 ((['git', 'config', 'branch.master.gerritserver',
1331 'https://chromium-review.googlesource.com'],), ''), 1340 'https://chromium-review.googlesource.com'],), ''),
1332 ((['git', 'config', 'branch.master.gerritpatchset', '7'],), ''), 1341 ((['git', 'config', '--int', 'branch.master.gerritpatchset', '7'],), ''),
1333 ] 1342 ]
1334 self.assertEqual(git_cl.main(['patch', '123456']), 0) 1343 self.assertEqual(git_cl.main(['patch', '123456']), 0)
1335 1344
1336 def test_patch_force_codereview(self): 1345 def test_patch_force_codereview(self):
1337 self._patch_common(is_gerrit=True, force_codereview=True) 1346 self._patch_common(is_gerrit=True, force_codereview=True)
1338 self.calls += [ 1347 self.calls += [
1339 ((['git', 'fetch', 'https://chromium.googlesource.com/my/repo', 1348 ((['git', 'fetch', 'https://chromium.googlesource.com/my/repo',
1340 'refs/changes/56/123456/7'],), ''), 1349 'refs/changes/56/123456/7'],), ''),
1341 ((['git', 'cherry-pick', 'FETCH_HEAD'],), ''), 1350 ((['git', 'cherry-pick', 'FETCH_HEAD'],), ''),
1342 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), 1351 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
1343 ((['git', 'config', 'branch.master.gerritissue', '123456'],), ''), 1352 ((['git', 'config', '--int', 'branch.master.gerritissue', '123456'],),
1353 ''),
1344 ((['git', 'config', 'branch.master.gerritserver'],), ''), 1354 ((['git', 'config', 'branch.master.gerritserver'],), ''),
1345 ((['git', 'config', 'branch.master.merge'],), 'master'), 1355 ((['git', 'config', 'branch.master.merge'],), 'master'),
1346 ((['git', 'config', 'branch.master.remote'],), 'origin'), 1356 ((['git', 'config', 'branch.master.remote'],), 'origin'),
1347 ((['git', 'config', 'remote.origin.url'],), 1357 ((['git', 'config', 'remote.origin.url'],),
1348 'https://chromium.googlesource.com/my/repo'), 1358 'https://chromium.googlesource.com/my/repo'),
1349 ((['git', 'config', 'branch.master.gerritserver', 1359 ((['git', 'config', 'branch.master.gerritserver',
1350 'https://chromium-review.googlesource.com'],), ''), 1360 'https://chromium-review.googlesource.com'],), ''),
1351 ((['git', 'config', 'branch.master.gerritpatchset', '7'],), ''), 1361 ((['git', 'config', '--int', 'branch.master.gerritpatchset', '7'],), ''),
1352 ] 1362 ]
1353 self.assertEqual(git_cl.main(['patch', '--gerrit', '123456']), 0) 1363 self.assertEqual(git_cl.main(['patch', '--gerrit', '123456']), 0)
1354 1364
1355 def test_gerrit_patch_url_successful(self): 1365 def test_gerrit_patch_url_successful(self):
1356 self._patch_common(is_gerrit=True) 1366 self._patch_common(is_gerrit=True)
1357 self.calls += [ 1367 self.calls += [
1358 ((['git', 'fetch', 'https://chromium.googlesource.com/my/repo', 1368 ((['git', 'fetch', 'https://chromium.googlesource.com/my/repo',
1359 'refs/changes/56/123456/1'],), ''), 1369 'refs/changes/56/123456/1'],), ''),
1360 ((['git', 'cherry-pick', 'FETCH_HEAD'],), ''), 1370 ((['git', 'cherry-pick', 'FETCH_HEAD'],), ''),
1361 ((['git', 'config', 'branch.master.gerritissue', '123456'],), ''), 1371 ((['git', 'config', '--int', 'branch.master.gerritissue', '123456'],),
1372 ''),
1362 ((['git', 'config', 'branch.master.gerritserver', 1373 ((['git', 'config', 'branch.master.gerritserver',
1363 'https://chromium-review.googlesource.com'],), ''), 1374 'https://chromium-review.googlesource.com'],), ''),
1364 ((['git', 'config', 'branch.master.gerritpatchset', '1'],), ''), 1375 ((['git', 'config', '--int', 'branch.master.gerritpatchset', '1'],), ''),
1365 ] 1376 ]
1366 self.assertEqual(git_cl.main( 1377 self.assertEqual(git_cl.main(
1367 ['patch', 'https://chromium-review.googlesource.com/#/c/123456/1']), 0) 1378 ['patch', 'https://chromium-review.googlesource.com/#/c/123456/1']), 0)
1368 1379
1369 def test_gerrit_patch_conflict(self): 1380 def test_gerrit_patch_conflict(self):
1370 self._patch_common(is_gerrit=True) 1381 self._patch_common(is_gerrit=True)
1371 self.mock(git_cl, 'DieWithError', 1382 self.mock(git_cl, 'DieWithError',
1372 lambda msg: self._mocked_call(['DieWithError', msg])) 1383 lambda msg: self._mocked_call(['DieWithError', msg]))
1373 class SystemExitMock(Exception): 1384 class SystemExitMock(Exception):
1374 pass 1385 pass
1375 self.calls += [ 1386 self.calls += [
1376 ((['git', 'fetch', 'https://chromium.googlesource.com/my/repo', 1387 ((['git', 'fetch', 'https://chromium.googlesource.com/my/repo',
1377 'refs/changes/56/123456/1'],), ''), 1388 'refs/changes/56/123456/1'],), ''),
1378 ((['git', 'cherry-pick', 'FETCH_HEAD'],), 1389 ((['git', 'cherry-pick', 'FETCH_HEAD'],), CERR1),
1379 '', subprocess2.CalledProcessError(1, '', '', '', '')), 1390 ((['DieWithError', 'Command "git cherry-pick FETCH_HEAD" failed.\n'],),
1380 ((['DieWithError', 'git cherry-pick FETCH_HEAD" failed.\n'],), 1391 SystemExitMock()),
1381 '', SystemExitMock()),
1382 ] 1392 ]
1383 with self.assertRaises(SystemExitMock): 1393 with self.assertRaises(SystemExitMock):
1384 git_cl.main(['patch', 1394 git_cl.main(['patch',
1385 'https://chromium-review.googlesource.com/#/c/123456/1']) 1395 'https://chromium-review.googlesource.com/#/c/123456/1'])
1386 1396
1387 def _checkout_calls(self): 1397 def _checkout_calls(self):
1388 return [ 1398 return [
1389 ((['git', 'config', '--local', '--get-regexp', 1399 ((['git', 'config', '--local', '--get-regexp',
1390 'branch\\..*\\.rietveldissue'], ), 1400 'branch\\..*\\.rietveldissue'], ),
1391 ('branch.retrying.rietveldissue 1111111111\n' 1401 ('branch.retrying.rietveldissue 1111111111\n'
(...skipping 20 matching lines...) Expand all
1412 """Tests git cl checkout <issue>.""" 1422 """Tests git cl checkout <issue>."""
1413 self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) 1423 self.mock(git_cl.sys, 'stdout', StringIO.StringIO())
1414 self.calls = self._checkout_calls() 1424 self.calls = self._checkout_calls()
1415 self.assertEqual(1, git_cl.main(['checkout', '99999'])) 1425 self.assertEqual(1, git_cl.main(['checkout', '99999']))
1416 1426
1417 def test_checkout_no_branch_issues(self): 1427 def test_checkout_no_branch_issues(self):
1418 """Tests git cl checkout <issue>.""" 1428 """Tests git cl checkout <issue>."""
1419 self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) 1429 self.mock(git_cl.sys, 'stdout', StringIO.StringIO())
1420 self.calls = [ 1430 self.calls = [
1421 ((['git', 'config', '--local', '--get-regexp', 1431 ((['git', 'config', '--local', '--get-regexp',
1422 'branch\\..*\\.rietveldissue'], ), '', 1432 'branch\\..*\\.rietveldissue'], ), CERR1),
1423 subprocess2.CalledProcessError(1, '', '', '', '')),
1424 ((['git', 'config', '--local', '--get-regexp', 1433 ((['git', 'config', '--local', '--get-regexp',
1425 'branch\\..*\\.gerritissue'], ), '', 1434 'branch\\..*\\.gerritissue'], ), CERR1),
1426 subprocess2.CalledProcessError(1, '', '', '', '')),
1427
1428 ] 1435 ]
1429 self.assertEqual(1, git_cl.main(['checkout', '99999'])) 1436 self.assertEqual(1, git_cl.main(['checkout', '99999']))
1430 1437
1431 def _test_gerrit_ensure_authenticated_common(self, auth, 1438 def _test_gerrit_ensure_authenticated_common(self, auth,
1432 skip_auth_check=False): 1439 skip_auth_check=False):
1433 self.mock(git_cl.gerrit_util, 'CookiesAuthenticator', 1440 self.mock(git_cl.gerrit_util, 'CookiesAuthenticator',
1434 CookiesAuthenticatorMockFactory(hosts_with_creds=auth)) 1441 CookiesAuthenticatorMockFactory(hosts_with_creds=auth))
1435 self.mock(git_cl, 'DieWithError', 1442 self.mock(git_cl, 'DieWithError',
1436 lambda msg: self._mocked_call(['DieWithError', msg])) 1443 lambda msg: self._mocked_call(['DieWithError', msg]))
1437 self.mock(git_cl, 'ask_for_data', 1444 self.mock(git_cl, 'ask_for_data',
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
1477 def test_gerrit_ensure_authenticated_skipped(self): 1484 def test_gerrit_ensure_authenticated_skipped(self):
1478 cl = self._test_gerrit_ensure_authenticated_common( 1485 cl = self._test_gerrit_ensure_authenticated_common(
1479 auth={}, skip_auth_check=True) 1486 auth={}, skip_auth_check=True)
1480 self.assertIsNone(cl.EnsureAuthenticated(force=False)) 1487 self.assertIsNone(cl.EnsureAuthenticated(force=False))
1481 1488
1482 def test_cmd_set_commit_rietveld(self): 1489 def test_cmd_set_commit_rietveld(self):
1483 self.mock(git_cl._RietveldChangelistImpl, 'SetFlags', 1490 self.mock(git_cl._RietveldChangelistImpl, 'SetFlags',
1484 lambda _, v: self._mocked_call(['SetFlags', v])) 1491 lambda _, v: self._mocked_call(['SetFlags', v]))
1485 self.calls = [ 1492 self.calls = [
1486 ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), 1493 ((['git', 'symbolic-ref', 'HEAD'],), 'feature'),
1487 ((['git', 'config', 'branch.feature.rietveldissue'],), '123'), 1494 ((['git', 'config', '--int', 'branch.feature.rietveldissue'],), '123'),
1488 ((['git', 'config', 'rietveld.autoupdate'],), ''), 1495 ((['git', 'config', 'rietveld.autoupdate'],), ''),
1489 ((['git', 'config', 'rietveld.server'],), ''), 1496 ((['git', 'config', 'rietveld.server'],), ''),
1490 ((['git', 'config', 'rietveld.server'],), ''), 1497 ((['git', 'config', 'rietveld.server'],), ''),
1491 ((['git', 'config', 'branch.feature.rietveldserver'],), 1498 ((['git', 'config', 'branch.feature.rietveldserver'],),
1492 'https://codereview.chromium.org'), 1499 'https://codereview.chromium.org'),
1493 ((['SetFlags', {'commit': '1', 'cq_dry_run': '0'}], ), ''), 1500 ((['SetFlags', {'commit': '1', 'cq_dry_run': '0'}], ), ''),
1494 ] 1501 ]
1495 self.assertEqual(0, git_cl.main(['set-commit'])) 1502 self.assertEqual(0, git_cl.main(['set-commit']))
1496 1503
1497 def _cmd_set_commit_gerrit_common(self, vote): 1504 def _cmd_set_commit_gerrit_common(self, vote):
1498 self.mock(git_cl.gerrit_util, 'SetReview', 1505 self.mock(git_cl.gerrit_util, 'SetReview',
1499 lambda h, i, labels: self._mocked_call( 1506 lambda h, i, labels: self._mocked_call(
1500 ['SetReview', h, i, labels])) 1507 ['SetReview', h, i, labels]))
1501 self.calls = [ 1508 self.calls = [
1502 ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), 1509 ((['git', 'symbolic-ref', 'HEAD'],), 'feature'),
1503 ((['git', 'config', 'branch.feature.rietveldissue'],), ''), 1510 ((['git', 'config', '--int', 'branch.feature.rietveldissue'],), CERR1),
1504 ((['git', 'config', 'branch.feature.gerritissue'],), '123'), 1511 ((['git', 'config', '--int', 'branch.feature.gerritissue'],), '123'),
1505 ((['git', 'config', 'branch.feature.gerritserver'],), 1512 ((['git', 'config', 'branch.feature.gerritserver'],),
1506 'https://chromium-review.googlesource.com'), 1513 'https://chromium-review.googlesource.com'),
1507 ((['SetReview', 'chromium-review.googlesource.com', 123, 1514 ((['SetReview', 'chromium-review.googlesource.com', 123,
1508 {'Commit-Queue': vote}],), ''), 1515 {'Commit-Queue': vote}],), ''),
1509 ] 1516 ]
1510 1517
1511 def test_cmd_set_commit_gerrit_clear(self): 1518 def test_cmd_set_commit_gerrit_clear(self):
1512 self._cmd_set_commit_gerrit_common(0) 1519 self._cmd_set_commit_gerrit_common(0)
1513 self.assertEqual(0, git_cl.main(['set-commit', '-c'])) 1520 self.assertEqual(0, git_cl.main(['set-commit', '-c']))
1514 1521
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
1649 1656
1650 self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) 1657 self.mock(git_cl.sys, 'stdout', StringIO.StringIO())
1651 self.mock(git_cl.Changelist, 'GetDescription', 1658 self.mock(git_cl.Changelist, 'GetDescription',
1652 lambda *args: current_desc) 1659 lambda *args: current_desc)
1653 self.mock(git_cl._GerritChangelistImpl, 'UpdateDescriptionRemote', 1660 self.mock(git_cl._GerritChangelistImpl, 'UpdateDescriptionRemote',
1654 UpdateDescriptionRemote) 1661 UpdateDescriptionRemote)
1655 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor) 1662 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor)
1656 1663
1657 self.calls = [ 1664 self.calls = [
1658 ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), 1665 ((['git', 'symbolic-ref', 'HEAD'],), 'feature'),
1659 ((['git', 'config', 'branch.feature.gerritissue'],), '123'), 1666 ((['git', 'config', '--int', 'branch.feature.gerritissue'],), '123'),
1660 ((['git', 'config', 'rietveld.autoupdate'],), ''), 1667 ((['git', 'config', 'rietveld.autoupdate'],), CERR1),
1661 ((['git', 'config', 'rietveld.bug-prefix'],), ''), 1668 ((['git', 'config', 'rietveld.bug-prefix'],), CERR1),
1662 ((['git', 'config', 'core.editor'],), 'vi'), 1669 ((['git', 'config', 'core.editor'],), 'vi'),
1663 ] 1670 ]
1664 self.assertEqual(0, git_cl.main(['description', '--gerrit'])) 1671 self.assertEqual(0, git_cl.main(['description', '--gerrit']))
1665 1672
1666 def test_description_set_stdin(self): 1673 def test_description_set_stdin(self):
1667 out = StringIO.StringIO() 1674 out = StringIO.StringIO()
1668 self.mock(git_cl.sys, 'stdout', out) 1675 self.mock(git_cl.sys, 'stdout', out)
1669 1676
1670 self.mock(git_cl, 'Changelist', ChangelistMock) 1677 self.mock(git_cl, 'Changelist', ChangelistMock)
1671 self.mock(git_cl.sys, 'stdin', StringIO.StringIO('hi \r\n\t there\n\nman')) 1678 self.mock(git_cl.sys, 'stdin', StringIO.StringIO('hi \r\n\t there\n\nman'))
1672 1679
1673 self.assertEqual(0, git_cl.main(['description', '-n', '-'])) 1680 self.assertEqual(0, git_cl.main(['description', '-n', '-']))
1674 self.assertEqual('hi\n\t there\n\nman', ChangelistMock.desc) 1681 self.assertEqual('hi\n\t there\n\nman', ChangelistMock.desc)
1675 1682
1676 def test_archive(self): 1683 def test_archive(self):
1677 self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) 1684 self.mock(git_cl.sys, 'stdout', StringIO.StringIO())
1678 1685
1679 self.calls = \ 1686 self.calls = \
1680 [((['git', 'for-each-ref', '--format=%(refname)', 'refs/heads'],), 1687 [((['git', 'for-each-ref', '--format=%(refname)', 'refs/heads'],),
1681 'refs/heads/master\nrefs/heads/foo\nrefs/heads/bar'), 1688 'refs/heads/master\nrefs/heads/foo\nrefs/heads/bar'),
1682 ((['git', 'config', 'branch.master.rietveldissue'],), '1'), 1689 ((['git', 'config', '--int', 'branch.master.rietveldissue'],), '1'),
1683 ((['git', 'config', 'rietveld.autoupdate'],), ''), 1690 ((['git', 'config', 'rietveld.autoupdate'],), CERR1),
1684 ((['git', 'config', 'rietveld.server'],), ''), 1691 ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'),
1685 ((['git', 'config', 'rietveld.server'],), ''), 1692 ((['git', 'config', '--int', 'branch.foo.rietveldissue'],), '456'),
1686 ((['git', 'config', 'branch.foo.rietveldissue'],), '456'), 1693 ((['git', 'config', '--int', 'branch.bar.rietveldissue'],), CERR1),
1687 ((['git', 'config', 'rietveld.server'],), ''), 1694 ((['git', 'config', '--int', 'branch.bar.gerritissue'],), '789'),
1688 ((['git', 'config', 'rietveld.server'],), ''),
1689 ((['git', 'config', 'branch.bar.rietveldissue'],), ''),
1690 ((['git', 'config', 'branch.bar.gerritissue'],), '789'),
1691 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), 1695 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
1692 ((['git', 'tag', 'git-cl-archived-456-foo', 'foo'],), ''), 1696 ((['git', 'tag', 'git-cl-archived-456-foo', 'foo'],), ''),
1693 ((['git', 'branch', '-D', 'foo'],), '')] 1697 ((['git', 'branch', '-D', 'foo'],), '')]
1694 1698
1695 class MockChangelist(): 1699 class MockChangelist():
1696 def __init__(self, branch, issue): 1700 def __init__(self, branch, issue):
1697 self.branch = branch 1701 self.branch = branch
1698 self.issue = issue 1702 self.issue = issue
1699 def GetBranch(self): 1703 def GetBranch(self):
1700 return self.branch 1704 return self.branch
1701 def GetIssue(self): 1705 def GetIssue(self):
1702 return self.issue 1706 return self.issue
1703 1707
1704 self.mock(git_cl, 'get_cl_statuses', 1708 self.mock(git_cl, 'get_cl_statuses',
1705 lambda branches, fine_grained, max_processes: 1709 lambda branches, fine_grained, max_processes:
1706 [(MockChangelist('master', 1), 'open'), 1710 [(MockChangelist('master', 1), 'open'),
1707 (MockChangelist('foo', 456), 'closed'), 1711 (MockChangelist('foo', 456), 'closed'),
1708 (MockChangelist('bar', 789), 'open')]) 1712 (MockChangelist('bar', 789), 'open')])
1709 1713
1710 self.assertEqual(0, git_cl.main(['archive', '-f'])) 1714 self.assertEqual(0, git_cl.main(['archive', '-f']))
1711 1715
1712 def test_archive_current_branch_fails(self): 1716 def test_archive_current_branch_fails(self):
1713 self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) 1717 self.mock(git_cl.sys, 'stdout', StringIO.StringIO())
1714 self.calls = \ 1718 self.calls = \
1715 [((['git', 'for-each-ref', '--format=%(refname)', 'refs/heads'],), 1719 [((['git', 'for-each-ref', '--format=%(refname)', 'refs/heads'],),
1716 'refs/heads/master'), 1720 'refs/heads/master'),
1717 ((['git', 'config', 'branch.master.rietveldissue'],), '1'), 1721 ((['git', 'config', '--int', 'branch.master.rietveldissue'],), '1'),
1718 ((['git', 'config', 'rietveld.autoupdate'],), ''), 1722 ((['git', 'config', 'rietveld.autoupdate'],), CERR1),
1719 ((['git', 'config', 'rietveld.server'],), ''), 1723 ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'),
1720 ((['git', 'config', 'rietveld.server'],), ''),
1721 ((['git', 'symbolic-ref', 'HEAD'],), 'master')] 1724 ((['git', 'symbolic-ref', 'HEAD'],), 'master')]
1722 1725
1723 class MockChangelist(): 1726 class MockChangelist():
1724 def __init__(self, branch, issue): 1727 def __init__(self, branch, issue):
1725 self.branch = branch 1728 self.branch = branch
1726 self.issue = issue 1729 self.issue = issue
1727 def GetBranch(self): 1730 def GetBranch(self):
1728 return self.branch 1731 return self.branch
1729 def GetIssue(self): 1732 def GetIssue(self):
1730 return self.issue 1733 return self.issue
1731 1734
1732 self.mock(git_cl, 'get_cl_statuses', 1735 self.mock(git_cl, 'get_cl_statuses',
1733 lambda branches, fine_grained, max_processes: 1736 lambda branches, fine_grained, max_processes:
1734 [(MockChangelist('master', 1), 'closed')]) 1737 [(MockChangelist('master', 1), 'closed')])
1735 1738
1736 self.assertEqual(1, git_cl.main(['archive', '-f'])) 1739 self.assertEqual(1, git_cl.main(['archive', '-f']))
1737 1740
1738 def test_cmd_issue_erase_existing(self): 1741 def test_cmd_issue_erase_existing(self):
1739 out = StringIO.StringIO() 1742 out = StringIO.StringIO()
1740 self.mock(git_cl.sys, 'stdout', out) 1743 self.mock(git_cl.sys, 'stdout', out)
1741 self.calls = [ 1744 self.calls = [
1742 ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), 1745 ((['git', 'symbolic-ref', 'HEAD'],), 'feature'),
1743 ((['git', 'config', 'branch.feature.rietveldissue'],), ''), 1746 ((['git', 'config', '--int', 'branch.feature.rietveldissue'],), CERR1),
1744 ((['git', 'config', 'branch.feature.gerritissue'],), '123'), 1747 ((['git', 'config', '--int', 'branch.feature.gerritissue'],), '123'),
1748 # Let this command raise exception (retcode=1) - it should be ignored.
1749 ((['git', 'config', '--unset', 'branch.feature.last-upload-hash'],),
1750 CERR1),
1745 ((['git', 'config', '--unset', 'branch.feature.gerritissue'],), ''), 1751 ((['git', 'config', '--unset', 'branch.feature.gerritissue'],), ''),
1746 ((['git', 'config', '--unset', 'branch.feature.gerritpatchset'],), ''), 1752 ((['git', 'config', '--unset', 'branch.feature.gerritpatchset'],), ''),
1747 # Let this command raise exception (retcode=1) - it should be ignored.
1748 ((['git', 'config', '--unset', 'branch.feature.last-upload-hash'],),
1749 '', subprocess2.CalledProcessError(1, '', '', '', '')),
1750 ((['git', 'config', '--unset', 'branch.feature.gerritserver'],), ''), 1753 ((['git', 'config', '--unset', 'branch.feature.gerritserver'],), ''),
1751 ((['git', 'config', '--unset', 'branch.feature.gerritsquashhash'],), 1754 ((['git', 'config', '--unset', 'branch.feature.gerritsquashhash'],),
1752 ''), 1755 ''),
1753 ] 1756 ]
1754 self.assertEqual(0, git_cl.main(['issue', '0'])) 1757 self.assertEqual(0, git_cl.main(['issue', '0']))
1755 1758
1756 def test_git_cl_try_default(self): 1759 def test_git_cl_try_default(self):
1757 self.mock(git_cl.Changelist, 'GetChange', 1760 self.mock(git_cl.Changelist, 'GetChange',
1758 lambda _, *a: ( 1761 lambda _, *a: (
1759 self._mocked_call(['GetChange']+list(a)))) 1762 self._mocked_call(['GetChange']+list(a))))
1760 self.mock(git_cl.presubmit_support, 'DoGetTryMasters', 1763 self.mock(git_cl.presubmit_support, 'DoGetTryMasters',
1761 lambda *_, **__: ( 1764 lambda *_, **__: (
1762 self._mocked_call(['DoGetTryMasters']))) 1765 self._mocked_call(['DoGetTryMasters'])))
1763 self.mock(git_cl.presubmit_support, 'DoGetTrySlaves', 1766 self.mock(git_cl.presubmit_support, 'DoGetTrySlaves',
1764 lambda *_, **__: ( 1767 lambda *_, **__: (
1765 self._mocked_call(['DoGetTrySlaves']))) 1768 self._mocked_call(['DoGetTrySlaves'])))
1766 self.mock(git_cl._RietveldChangelistImpl, 'SetCQState', 1769 self.mock(git_cl._RietveldChangelistImpl, 'SetCQState',
1767 lambda _, s: self._mocked_call(['SetCQState', s])) 1770 lambda _, s: self._mocked_call(['SetCQState', s]))
1768 self.calls = [ 1771 self.calls = [
1769 ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), 1772 ((['git', 'symbolic-ref', 'HEAD'],), 'feature'),
1770 ((['git', 'config', 'branch.feature.rietveldissue'],), '123'), 1773 ((['git', 'config', '--int', 'branch.feature.rietveldissue'],), '123'),
1771 ((['git', 'config', 'rietveld.autoupdate'],), ''), 1774 ((['git', 'config', 'rietveld.autoupdate'],), ''),
1772 ((['git', 'config', 'rietveld.server'],), 1775 ((['git', 'config', 'rietveld.server'],),
1773 'https://codereview.chromium.org'), 1776 'https://codereview.chromium.org'),
1774 ((['git', 'config', 'branch.feature.rietveldserver'],), ''), 1777 ((['git', 'config', 'branch.feature.rietveldserver'],), ''),
1775 ((['git', 'config', 'branch.feature.merge'],), 'feature'), 1778 ((['git', 'config', 'branch.feature.merge'],), 'feature'),
1776 ((['git', 'config', 'branch.feature.remote'],), 'origin'), 1779 ((['git', 'config', 'branch.feature.remote'],), 'origin'),
1777 ((['get_or_create_merge_base', 'feature', 'feature'],), 1780 ((['get_or_create_merge_base', 'feature', 'feature'],),
1778 'fake_ancestor_sha'), 1781 'fake_ancestor_sha'),
1779 ((['GetChange', 'fake_ancestor_sha', None], ), 1782 ((['GetChange', 'fake_ancestor_sha', None], ),
1780 git_cl.presubmit_support.GitChange( 1783 git_cl.presubmit_support.GitChange(
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
1833 ((['rm_file_or_tree', '/abs/git_repo_root/.git/hooks/commit-msg'],), 1836 ((['rm_file_or_tree', '/abs/git_repo_root/.git/hooks/commit-msg'],),
1834 ''), 1837 ''),
1835 ] 1838 ]
1836 cl._codereview_impl._GerritCommitMsgHookCheck(offer_removal=True) 1839 cl._codereview_impl._GerritCommitMsgHookCheck(offer_removal=True)
1837 1840
1838 1841
1839 if __name__ == '__main__': 1842 if __name__ == '__main__':
1840 git_cl.logging.basicConfig( 1843 git_cl.logging.basicConfig(
1841 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) 1844 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR)
1842 unittest.main() 1845 unittest.main()
OLDNEW
« no previous file with comments | « git_cl.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698