blob: c24a4d8fec553d7abe979606e352516c141e8852 [file] [log] [blame]
maruel@chromium.orgddd59412011-11-30 14:20:38 +00001#!/usr/bin/env python
maruel@chromium.orgeb5edbc2012-01-16 17:03:28 +00002# Copyright (c) 2012 The Chromium Authors. All rights reserved.
maruel@chromium.orgddd59412011-11-30 14:20:38 +00003# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Unit tests for git_cl.py."""
7
8import os
maruel@chromium.orga3353652011-11-30 14:26:57 +00009import StringIO
ukai@chromium.org78c4b982012-02-14 02:20:26 +000010import stat
maruel@chromium.orgddd59412011-11-30 14:20:38 +000011import sys
12import unittest
13
14sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
15
16from testing_support.auto_stub import TestCase
17
18import git_cl
iannucci@chromium.org9e849272014-04-04 00:31:55 +000019import git_common
maruel@chromium.orgddd59412011-11-30 14:20:38 +000020import subprocess2
maruel@chromium.orgddd59412011-11-30 14:20:38 +000021
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000022class PresubmitMock(object):
23 def __init__(self, *args, **kwargs):
24 self.reviewers = []
25 @staticmethod
26 def should_continue():
27 return True
28
29
30class RietveldMock(object):
31 def __init__(self, *args, **kwargs):
32 pass
maruel@chromium.org78936cb2013-04-11 00:17:52 +000033
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000034 @staticmethod
35 def get_description(issue):
36 return 'Issue: %d' % issue
37
maruel@chromium.org78936cb2013-04-11 00:17:52 +000038 @staticmethod
39 def get_issue_properties(_issue, _messages):
40 return {
41 'reviewers': ['joe@chromium.org', 'john@chromium.org'],
42 'messages': [
43 {
44 'approval': True,
45 'sender': 'john@chromium.org',
46 },
47 ],
48 }
49
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000050
51class WatchlistsMock(object):
52 def __init__(self, _):
53 pass
54 @staticmethod
55 def GetWatchersForPaths(_):
56 return ['joe@example.com']
57
58
ukai@chromium.org78c4b982012-02-14 02:20:26 +000059class CodereviewSettingsFileMock(object):
60 def __init__(self):
61 pass
62 # pylint: disable=R0201
63 def read(self):
64 return ("CODE_REVIEW_SERVER: gerrit.chromium.org\n" +
andybons@chromium.org11f46eb2016-02-02 19:26:51 +000065 "GERRIT_HOST: True\n")
ukai@chromium.org78c4b982012-02-14 02:20:26 +000066
67
vadimsh@chromium.orgeed4df32015-04-10 21:30:20 +000068class AuthenticatorMock(object):
69 def __init__(self, *_args):
70 pass
71 def has_cached_credentials(self):
72 return True
73
74
maruel@chromium.orgddd59412011-11-30 14:20:38 +000075class TestGitCl(TestCase):
76 def setUp(self):
77 super(TestGitCl, self).setUp()
78 self.calls = []
79 self._calls_done = 0
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000080 self.mock(subprocess2, 'call', self._mocked_call)
81 self.mock(subprocess2, 'check_call', self._mocked_call)
82 self.mock(subprocess2, 'check_output', self._mocked_call)
83 self.mock(subprocess2, 'communicate', self._mocked_call)
sbc@chromium.org71437c02015-04-09 19:29:40 +000084 self.mock(git_common, 'is_dirty_git_tree', lambda x: False)
iannucci@chromium.org9e849272014-04-04 00:31:55 +000085 self.mock(git_common, 'get_or_create_merge_base',
86 lambda *a: (
87 self._mocked_call(['get_or_create_merge_base']+list(a))))
pgervais@chromium.org8ba38ff2015-06-11 21:41:25 +000088 self.mock(git_cl, 'BranchExists', lambda _: True)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000089 self.mock(git_cl, 'FindCodereviewSettingsFile', lambda: '')
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000090 self.mock(git_cl, 'ask_for_data', self._mocked_call)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000091 self.mock(git_cl.presubmit_support, 'DoPresubmitChecks', PresubmitMock)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000092 self.mock(git_cl.rietveld, 'Rietveld', RietveldMock)
maruel@chromium.org4bac4b52012-11-27 20:33:52 +000093 self.mock(git_cl.rietveld, 'CachingRietveld', RietveldMock)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000094 self.mock(git_cl.upload, 'RealMain', self.fail)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000095 self.mock(git_cl.watchlists, 'Watchlists', WatchlistsMock)
vadimsh@chromium.orgeed4df32015-04-10 21:30:20 +000096 self.mock(git_cl.auth, 'get_authenticator_for_host', AuthenticatorMock)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000097 # It's important to reset settings to not have inter-tests interference.
98 git_cl.settings = None
99
100 def tearDown(self):
wychen@chromium.org445c8962015-04-28 23:30:05 +0000101 try:
102 if not self.has_failed():
103 self.assertEquals([], self.calls)
104 finally:
105 super(TestGitCl, self).tearDown()
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000106
iannucci@chromium.org9e849272014-04-04 00:31:55 +0000107 def _mocked_call(self, *args, **_kwargs):
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000108 self.assertTrue(
109 self.calls,
110 '@%d Expected: <Missing> Actual: %r' % (self._calls_done, args))
wychen@chromium.orga872e752015-04-28 23:42:18 +0000111 top = self.calls.pop(0)
112 if len(top) > 2 and top[2]:
113 raise top[2]
114 expected_args, result = top
115
maruel@chromium.orge52678e2013-04-26 18:34:44 +0000116 # Also logs otherwise it could get caught in a try/finally and be hard to
117 # diagnose.
118 if expected_args != args:
119 msg = '@%d Expected: %r Actual: %r' % (
120 self._calls_done, expected_args, args)
121 git_cl.logging.error(msg)
122 self.fail(msg)
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000123 self._calls_done += 1
124 return result
125
maruel@chromium.orga3353652011-11-30 14:26:57 +0000126 @classmethod
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000127 def _upload_calls(cls, similarity, find_copies, private):
iannucci@chromium.org79540052012-10-19 23:15:26 +0000128 return (cls._git_base_calls(similarity, find_copies) +
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000129 cls._git_upload_calls(private))
maruel@chromium.orga3353652011-11-30 14:26:57 +0000130
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000131 @classmethod
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000132 def _upload_no_rev_calls(cls, similarity, find_copies):
133 return (cls._git_base_calls(similarity, find_copies) +
134 cls._git_upload_no_rev_calls())
135
136 @classmethod
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000137 def _git_base_calls(cls, similarity, find_copies):
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000138 if similarity is None:
139 similarity = '50'
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000140 similarity_call = ((['git', 'config', '--int', '--get',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000141 'branch.master.git-cl-similarity'],), '')
142 else:
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000143 similarity_call = ((['git', 'config', '--int',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000144 'branch.master.git-cl-similarity', similarity],), '')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000145
146 if find_copies is None:
147 find_copies = True
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000148 find_copies_call = ((['git', 'config', '--int', '--get',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000149 'branch.master.git-find-copies'],), '')
150 else:
151 val = str(int(find_copies))
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000152 find_copies_call = ((['git', 'config', '--int',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000153 'branch.master.git-find-copies', val],), '')
154
155 if find_copies:
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000156 stat_call = ((['git', 'diff', '--no-ext-diff', '--stat',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000157 '--find-copies-harder', '-l100000', '-C'+similarity,
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000158 'fake_ancestor_sha', 'HEAD'],), '+dat')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000159 else:
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000160 stat_call = ((['git', 'diff', '--no-ext-diff', '--stat',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000161 '-M'+similarity, 'fake_ancestor_sha', 'HEAD'],), '+dat')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000162
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000163 return [
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000164 ((['git', 'config', 'rietveld.autoupdate'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000165 ((['git', 'config', 'rietveld.server'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000166 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000167 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000168 similarity_call,
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000169 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
iannucci@chromium.org79540052012-10-19 23:15:26 +0000170 find_copies_call,
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000171 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
172 ((['git', 'config', 'branch.master.merge'],), 'master'),
173 ((['git', 'config', 'branch.master.remote'],), 'origin'),
iannucci@chromium.org9e849272014-04-04 00:31:55 +0000174 ((['get_or_create_merge_base', 'master', 'master'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000175 'fake_ancestor_sha'),
vadimsh@chromium.orgeed4df32015-04-10 21:30:20 +0000176 ((['git', 'config', 'gerrit.host'],), ''),
vadimsh@chromium.org19f3fe62015-04-20 17:03:10 +0000177 ((['git', 'config', 'branch.master.rietveldissue'],), ''),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000178 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000179 ((['git', 'rev-parse', '--show-cdup'],), ''),
180 ((['git', 'rev-parse', 'HEAD'],), '12345'),
181 ((['git', 'diff', '--name-status', '--no-renames', '-r',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000182 'fake_ancestor_sha...', '.'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000183 'M\t.gitignore\n'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000184 ((['git', 'config', 'branch.master.rietveldpatchset'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000185 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000186 ((['git', 'log', '--pretty=format:%s%n%n%b',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000187 'fake_ancestor_sha...'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000188 'foo'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000189 ((['git', 'config', 'user.email'],), 'me@example.com'),
iannucci@chromium.org79540052012-10-19 23:15:26 +0000190 stat_call,
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000191 ((['git', 'log', '--pretty=format:%s\n\n%b',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000192 'fake_ancestor_sha..HEAD'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000193 'desc\n'),
rmistry@google.com90752582014-01-14 21:04:50 +0000194 ((['git', 'config', 'rietveld.bug-prefix'],), ''),
maruel@chromium.orga3353652011-11-30 14:26:57 +0000195 ]
196
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000197 @classmethod
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000198 def _git_upload_no_rev_calls(cls):
199 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000200 ((['git', 'config', 'core.editor'],), ''),
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000201 ]
202
203 @classmethod
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000204 def _git_upload_calls(cls, private):
205 if private:
tyoshino@chromium.org99918ab2013-09-30 06:17:28 +0000206 cc_call = []
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000207 private_call = []
208 else:
tyoshino@chromium.org99918ab2013-09-30 06:17:28 +0000209 cc_call = [((['git', 'config', 'rietveld.cc'],), '')]
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000210 private_call = [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000211 ((['git', 'config', 'rietveld.private'],), '')]
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000212
maruel@chromium.orga3353652011-11-30 14:26:57 +0000213 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000214 ((['git', 'config', 'core.editor'],), ''),
tyoshino@chromium.org99918ab2013-09-30 06:17:28 +0000215 ] + cc_call + private_call + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000216 ((['git', 'config', 'branch.master.base-url'],), ''),
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +0000217 ((['git', 'config', 'rietveld.pending-ref-prefix'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000218 ((['git',
bratell@opera.com05fb9112014-07-07 09:30:23 +0000219 'config', '--local', '--get-regexp', '^svn-remote\\.'],),
220 (('', None), 0)),
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000221 ((['git', 'rev-parse', '--show-cdup'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000222 ((['git', 'svn', 'info'],), ''),
sheyang@chromium.org152cf832014-06-11 21:37:49 +0000223 ((['git', 'config', 'rietveld.project'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000224 ((['git',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000225 'config', 'branch.master.rietveldissue', '1'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000226 ((['git', 'config', 'branch.master.rietveldserver',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000227 'https://codereview.example.com'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000228 ((['git',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000229 'config', 'branch.master.rietveldpatchset', '2'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000230 ((['git', 'rev-parse', 'HEAD'],), 'hash'),
231 ((['git', 'symbolic-ref', 'HEAD'],), 'hash'),
232 ((['git',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000233 'config', 'branch.hash.last-upload-hash', 'hash'],), ''),
rmistry@google.com5626a922015-02-26 14:03:30 +0000234 ((['git', 'config', 'rietveld.run-post-upload-hook'],), ''),
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000235 ]
236
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000237 @staticmethod
238 def _git_sanity_checks(diff_base, working_branch):
239 fake_ancestor = 'fake_ancestor'
240 fake_cl = 'fake_cl_for_patch'
241 return [
242 # Calls to verify branch point is ancestor
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000243 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000244 'rev-parse', '--verify', diff_base],), fake_ancestor),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000245 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000246 'merge-base', fake_ancestor, 'HEAD'],), fake_ancestor),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000247 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000248 'rev-list', '^' + fake_ancestor, 'HEAD'],), fake_cl),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000249 # Mock a config miss (error code 1)
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000250 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000251 'config', 'gitcl.remotebranch'],), (('', None), 1)),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000252 # Call to GetRemoteBranch()
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000253 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000254 'config', 'branch.%s.merge' % working_branch],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000255 'refs/heads/master'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000256 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000257 'config', 'branch.%s.remote' % working_branch],), 'origin'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000258 ((['git', 'rev-list', '^' + fake_ancestor,
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000259 'refs/remotes/origin/master'],), ''),
260 ]
261
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000262 @classmethod
263 def _dcommit_calls_1(cls):
264 return [
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +0000265 ((['git', 'config', 'rietveld.autoupdate'],),
266 ''),
267 ((['git', 'config', 'rietveld.pending-ref-prefix'],),
268 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000269 ((['git',
bratell@opera.com05fb9112014-07-07 09:30:23 +0000270 'config', '--local', '--get-regexp', '^svn-remote\\.'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000271 ((('svn-remote.svn.url svn://svn.chromium.org/chrome\n'
272 'svn-remote.svn.fetch trunk/src:refs/remotes/origin/master'),
273 None),
274 0)),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000275 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000276 'config', 'rietveld.server'],), 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000277 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
278 ((['git', 'config', '--int', '--get',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000279 'branch.working.git-cl-similarity'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000280 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
281 ((['git', 'config', '--int', '--get',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000282 'branch.working.git-find-copies'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000283 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
284 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000285 'config', 'branch.working.merge'],), 'refs/heads/master'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000286 ((['git', 'config', 'branch.working.remote'],), 'origin'),
iannucci@chromium.org5724c962014-04-11 09:32:56 +0000287 ((['git', 'config', 'branch.working.merge'],),
288 'refs/heads/master'),
289 ((['git', 'config', 'branch.working.remote'],), 'origin'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000290 ((['git', 'rev-list', '--merges',
szager@chromium.orge84b7542012-06-15 21:26:58 +0000291 '--grep=^SVN changes up to revision [0-9]*$',
szager@chromium.org9bb85e22012-06-13 20:28:23 +0000292 'refs/remotes/origin/master^!'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000293 ((['git', 'rev-list', '^refs/heads/working',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000294 'refs/remotes/origin/master'],),
295 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000296 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000297 'log', '--grep=^git-svn-id:', '-1', '--pretty=format:%H'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000298 '3fc18b62c4966193eb435baabe2d18a3810ec82e'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000299 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000300 'rev-list', '^3fc18b62c4966193eb435baabe2d18a3810ec82e',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000301 'refs/remotes/origin/master'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000302 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000303 'merge-base', 'refs/remotes/origin/master', 'HEAD'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000304 'fake_ancestor_sha'),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000305 ]
306
307 @classmethod
308 def _dcommit_calls_normal(cls):
309 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000310 ((['git', 'rev-parse', '--show-cdup'],), ''),
311 ((['git', 'rev-parse', 'HEAD'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000312 '00ff397798ea57439712ed7e04ab96e13969ef40'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000313 ((['git',
mcgrathr@chromium.org9249f642013-06-03 21:36:18 +0000314 'diff', '--name-status', '--no-renames', '-r', 'fake_ancestor_sha...',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000315 '.'],),
316 'M\tPRESUBMIT.py'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000317 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000318 'config', 'branch.working.rietveldissue'],), '12345'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000319 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000320 'config', 'branch.working.rietveldpatchset'],), '31137'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000321 ((['git', 'config', 'branch.working.rietveldserver'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000322 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000323 ((['git', 'config', 'user.email'],), 'author@example.com'),
324 ((['git', 'config', 'rietveld.tree-status-url'],), ''),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000325 ]
326
327 @classmethod
328 def _dcommit_calls_bypassed(cls):
329 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000330 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000331 'config', 'branch.working.rietveldissue'],), '12345'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000332 ((['git', 'config', 'branch.working.rietveldserver'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000333 'codereview.example.com'),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000334 ]
335
336 @classmethod
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000337 def _dcommit_calls_3(cls):
338 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000339 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000340 'diff', '--no-ext-diff', '--stat', '--find-copies-harder',
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000341 '-l100000', '-C50', 'fake_ancestor_sha',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000342 'refs/heads/working'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000343 (' PRESUBMIT.py | 2 +-\n'
344 ' 1 files changed, 1 insertions(+), 1 deletions(-)\n')),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000345 ((['git', 'show-ref', '--quiet', '--verify',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000346 'refs/heads/git-cl-commit'],),
347 (('', None), 0)),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000348 ((['git', 'branch', '-D', 'git-cl-commit'],), ''),
349 ((['git', 'show-ref', '--quiet', '--verify',
szager@chromium.org9bb85e22012-06-13 20:28:23 +0000350 'refs/heads/git-cl-cherry-pick'],), ''),
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000351 ((['git', 'rev-parse', '--show-cdup'],), '\n'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000352 ((['git', 'checkout', '-q', '-b', 'git-cl-commit'],), ''),
353 ((['git', 'reset', '--soft', 'fake_ancestor_sha'],), ''),
354 ((['git', 'commit', '-m',
maruel@chromium.orge52678e2013-04-26 18:34:44 +0000355 'Issue: 12345\n\nR=john@chromium.org\n\n'
sergiyb@chromium.org4b39c5f2015-07-07 10:33:12 +0000356 'Review URL: https://codereview.example.com/12345 .'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000357 ''),
kjellander@chromium.org6abc6522014-12-02 07:34:49 +0000358 ((['git', 'config', 'rietveld.force-https-commit-url'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000359 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000360 'svn', 'dcommit', '-C50', '--no-rebase', '--rmdir'],),
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000361 (('', None), 0)),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000362 ((['git', 'checkout', '-q', 'working'],), ''),
363 ((['git', 'branch', '-D', 'git-cl-commit'],), ''),
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000364 ]
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000365
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000366 @staticmethod
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000367 def _cmd_line(description, args, similarity, find_copies, private):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000368 """Returns the upload command line passed to upload.RealMain()."""
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000369 return [
370 'upload', '--assume_yes', '--server',
maruel@chromium.orgeb5edbc2012-01-16 17:03:28 +0000371 'https://codereview.example.com',
maruel@chromium.org71e12a92012-02-14 02:34:15 +0000372 '--message', description
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000373 ] + args + [
374 '--cc', 'joe@example.com',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000375 ] + (['--private'] if private else []) + [
iannucci@chromium.org79540052012-10-19 23:15:26 +0000376 '--git_similarity', similarity or '50'
377 ] + (['--git_no_find_copies'] if find_copies == False else []) + [
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000378 'fake_ancestor_sha', 'HEAD'
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000379 ]
380
381 def _run_reviewer_test(
382 self,
383 upload_args,
384 expected_description,
385 returned_description,
386 final_description,
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000387 reviewers,
388 private=False):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000389 """Generic reviewer test framework."""
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000390 try:
391 similarity = upload_args[upload_args.index('--similarity')+1]
392 except ValueError:
393 similarity = None
iannucci@chromium.org79540052012-10-19 23:15:26 +0000394
395 if '--find-copies' in upload_args:
396 find_copies = True
397 elif '--no-find-copies' in upload_args:
398 find_copies = False
399 else:
400 find_copies = None
401
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000402 private = '--private' in upload_args
403
404 self.calls = self._upload_calls(similarity, find_copies, private)
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000405
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000406 def RunEditor(desc, _, **kwargs):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000407 self.assertEquals(
408 '# Enter a description of the change.\n'
janx@chromium.org104b2db2013-04-18 12:58:40 +0000409 '# This will be displayed on the codereview site.\n'
alancutter@chromium.org63a4d7f2013-05-31 02:22:45 +0000410 '# The first line will also be used as the subject of the review.\n'
alancutter@chromium.orgbd1073e2013-06-01 00:34:38 +0000411 '#--------------------This line is 72 characters long'
412 '--------------------\n' +
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000413 expected_description,
414 desc)
415 return returned_description
416 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor)
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000417
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000418 def check_upload(args):
iannucci@chromium.org79540052012-10-19 23:15:26 +0000419 cmd_line = self._cmd_line(final_description, reviewers, similarity,
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000420 find_copies, private)
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000421 self.assertEquals(cmd_line, args)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000422 return 1, 2
423 self.mock(git_cl.upload, 'RealMain', check_upload)
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000424
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000425 git_cl.main(['upload'] + upload_args)
426
427 def test_no_reviewer(self):
428 self._run_reviewer_test(
429 [],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000430 'desc\n\nBUG=',
431 '# Blah blah comment.\ndesc\n\nBUG=',
432 'desc\n\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000433 [])
434
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000435 def test_keep_similarity(self):
436 self._run_reviewer_test(
437 ['--similarity', '70'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000438 'desc\n\nBUG=',
439 '# Blah blah comment.\ndesc\n\nBUG=',
440 'desc\n\nBUG=',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000441 [])
442
iannucci@chromium.org79540052012-10-19 23:15:26 +0000443 def test_keep_find_copies(self):
444 self._run_reviewer_test(
445 ['--no-find-copies'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000446 'desc\n\nBUG=',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000447 '# Blah blah comment.\ndesc\n\nBUG=\n',
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000448 'desc\n\nBUG=',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000449 [])
450
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000451 def test_private(self):
452 self._run_reviewer_test(
453 ['--private'],
454 'desc\n\nBUG=',
455 '# Blah blah comment.\ndesc\n\nBUG=\n',
456 'desc\n\nBUG=',
457 [])
458
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000459 def test_reviewers_cmd_line(self):
460 # Reviewer is passed as-is
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000461 description = 'desc\n\nR=foo@example.com\nBUG='
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000462 self._run_reviewer_test(
463 ['-r' 'foo@example.com'],
464 description,
465 '\n%s\n' % description,
466 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000467 ['--reviewers=foo@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000468
469 def test_reviewer_tbr_overriden(self):
470 # Reviewer is overriden with TBR
471 # Also verifies the regexp work without a trailing LF
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000472 description = 'Foo Bar\n\nTBR=reviewer@example.com'
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000473 self._run_reviewer_test(
474 ['-r' 'foo@example.com'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000475 'desc\n\nR=foo@example.com\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000476 description.strip('\n'),
477 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000478 ['--reviewers=reviewer@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000479
480 def test_reviewer_multiple(self):
481 # Handles multiple R= or TBR= lines.
482 description = (
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000483 'Foo Bar\nTBR=reviewer@example.com\nBUG=\nR=another@example.com')
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000484 self._run_reviewer_test(
485 [],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000486 'desc\n\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000487 description,
488 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000489 ['--reviewers=another@example.com,reviewer@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000490
maruel@chromium.orga3353652011-11-30 14:26:57 +0000491 def test_reviewer_send_mail(self):
492 # --send-mail can be used without -r if R= is used
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000493 description = 'Foo Bar\nR=reviewer@example.com'
maruel@chromium.orga3353652011-11-30 14:26:57 +0000494 self._run_reviewer_test(
495 ['--send-mail'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000496 'desc\n\nBUG=',
maruel@chromium.orga3353652011-11-30 14:26:57 +0000497 description.strip('\n'),
498 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000499 ['--reviewers=reviewer@example.com', '--send_mail'])
maruel@chromium.orga3353652011-11-30 14:26:57 +0000500
501 def test_reviewer_send_mail_no_rev(self):
502 # Fails without a reviewer.
maruel@chromium.org2e23ce32013-05-07 12:42:28 +0000503 stdout = StringIO.StringIO()
504 stderr = StringIO.StringIO()
maruel@chromium.orga3353652011-11-30 14:26:57 +0000505 try:
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000506 self.calls = self._upload_no_rev_calls(None, None)
507 def RunEditor(desc, _, **kwargs):
maruel@chromium.orga3353652011-11-30 14:26:57 +0000508 return desc
509 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor)
maruel@chromium.org2e23ce32013-05-07 12:42:28 +0000510 self.mock(sys, 'stdout', stdout)
511 self.mock(sys, 'stderr', stderr)
maruel@chromium.orga3353652011-11-30 14:26:57 +0000512 git_cl.main(['upload', '--send-mail'])
513 self.fail()
514 except SystemExit:
maruel@chromium.org2e23ce32013-05-07 12:42:28 +0000515 self.assertEqual(
516 'Using 50% similarity for rename/copy detection. Override with '
517 '--similarity.\n',
518 stdout.getvalue())
519 self.assertEqual(
520 'Must specify reviewers to send email.\n', stderr.getvalue())
maruel@chromium.orga3353652011-11-30 14:26:57 +0000521
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000522 def test_dcommit(self):
523 self.calls = (
524 self._dcommit_calls_1() +
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000525 self._git_sanity_checks('fake_ancestor_sha', 'working') +
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000526 self._dcommit_calls_normal() +
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000527 self._dcommit_calls_3())
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000528 git_cl.main(['dcommit'])
529
530 def test_dcommit_bypass_hooks(self):
531 self.calls = (
532 self._dcommit_calls_1() +
533 self._dcommit_calls_bypassed() +
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000534 self._dcommit_calls_3())
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000535 git_cl.main(['dcommit', '--bypass-hooks'])
536
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000537
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000538 @classmethod
539 def _gerrit_base_calls(cls):
ukai@chromium.orge8077812012-02-03 03:41:46 +0000540 return [
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000541 ((['git', 'config', 'rietveld.autoupdate'],),
542 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000543 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000544 'config', 'rietveld.server'],), 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000545 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
546 ((['git', 'config', '--int', '--get',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000547 'branch.master.git-cl-similarity'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000548 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
549 ((['git', 'config', '--int', '--get',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000550 'branch.master.git-find-copies'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000551 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
552 ((['git', 'config', 'branch.master.merge'],), 'master'),
553 ((['git', 'config', 'branch.master.remote'],), 'origin'),
iannucci@chromium.org9e849272014-04-04 00:31:55 +0000554 ((['get_or_create_merge_base', 'master', 'master'],),
555 'fake_ancestor_sha'),
andybons@chromium.org11f46eb2016-02-02 19:26:51 +0000556 ((['git', 'config', 'gerrit.host'],), 'True'),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000557 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000558 ((['git', 'rev-parse', '--show-cdup'],), ''),
559 ((['git', 'rev-parse', 'HEAD'],), '12345'),
560 ((['git',
mcgrathr@chromium.org9249f642013-06-03 21:36:18 +0000561 'diff', '--name-status', '--no-renames', '-r',
562 'fake_ancestor_sha...', '.'],),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000563 'M\t.gitignore\n'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000564 ((['git', 'config', 'branch.master.rietveldissue'],), ''),
565 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000566 'config', 'branch.master.rietveldpatchset'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000567 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000568 'log', '--pretty=format:%s%n%n%b', 'fake_ancestor_sha...'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000569 'foo'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000570 ((['git', 'config', 'user.email'],), 'me@example.com'),
571 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000572 'diff', '--no-ext-diff', '--stat', '--find-copies-harder',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000573 '-l100000', '-C50', 'fake_ancestor_sha', 'HEAD'],),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000574 '+dat'),
575 ]
576
577 @staticmethod
luqui@chromium.org609f3952015-05-04 22:47:04 +0000578 def _gerrit_upload_calls(description, reviewers, squash,
579 expected_upstream_ref='origin/refs/heads/master'):
ukai@chromium.orge8077812012-02-03 03:41:46 +0000580 calls = [
bauerb@chromium.org54b400c2016-01-14 10:08:25 +0000581 ((['git', 'config', '--bool', 'gerrit.squash-uploads'],), 'false'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000582 ((['git', 'log', '--pretty=format:%s\n\n%b',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000583 'fake_ancestor_sha..HEAD'],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000584 description)
585 ]
586 if git_cl.CHANGE_ID not in description:
587 calls += [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000588 ((['git', 'log', '--pretty=format:%s\n\n%b',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000589 'fake_ancestor_sha..HEAD'],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000590 description),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000591 ((['git', 'commit', '--amend', '-m', description],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000592 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000593 ((['git', 'log', '--pretty=format:%s\n\n%b',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000594 'fake_ancestor_sha..HEAD'],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000595 description)
596 ]
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000597 if squash:
598 ref_to_push = 'abcdef0123456789'
599 calls += [
600 ((['git', 'show', '--format=%s\n\n%b', '-s',
601 'refs/heads/git_cl_uploads/master'],),
602 (description, 0)),
603 ((['git', 'config', 'branch.master.merge'],),
604 'refs/heads/master'),
605 ((['git', 'config', 'branch.master.remote'],),
606 'origin'),
607 ((['get_or_create_merge_base', 'master', 'master'],),
608 'origin/master'),
609 ((['git', 'rev-parse', 'HEAD:'],),
610 '0123456789abcdef'),
611 ((['git', 'commit-tree', '0123456789abcdef', '-p',
612 'origin/master', '-m', 'd'],),
613 ref_to_push),
614 ]
615 else:
616 ref_to_push = 'HEAD'
617
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000618 calls += [
luqui@chromium.org609f3952015-05-04 22:47:04 +0000619 ((['git', 'rev-list',
620 expected_upstream_ref + '..' + ref_to_push],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000621 ((['git', 'config', 'rietveld.cc'],), '')
ukai@chromium.orge8077812012-02-03 03:41:46 +0000622 ]
ukai@chromium.org19bbfa22012-02-03 16:18:11 +0000623 receive_pack = '--receive-pack=git receive-pack '
ukai@chromium.orge8077812012-02-03 03:41:46 +0000624 receive_pack += '--cc=joe@example.com' # from watch list
625 if reviewers:
626 receive_pack += ' '
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000627 receive_pack += ' '.join(
628 '--reviewer=' + email for email in sorted(reviewers))
ukai@chromium.org19bbfa22012-02-03 16:18:11 +0000629 receive_pack += ''
ukai@chromium.orge8077812012-02-03 03:41:46 +0000630 calls += [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000631 ((['git',
luqui@chromium.org609f3952015-05-04 22:47:04 +0000632 'push', receive_pack, 'origin',
633 ref_to_push + ':refs/for/refs/heads/master'],),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000634 '')
635 ]
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000636 if squash:
637 calls += [
638 ((['git', 'rev-parse', 'HEAD'],), 'abcdef0123456789'),
639 ((['git', 'update-ref', '-m', 'Uploaded abcdef0123456789',
640 'refs/heads/git_cl_uploads/master', 'abcdef0123456789'],),
641 '')
642 ]
643
ukai@chromium.orge8077812012-02-03 03:41:46 +0000644 return calls
645
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000646 def _run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000647 self,
648 upload_args,
649 description,
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000650 reviewers,
luqui@chromium.org609f3952015-05-04 22:47:04 +0000651 squash=False,
652 expected_upstream_ref='origin/refs/heads/master'):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000653 """Generic gerrit upload test framework."""
ukai@chromium.orge8077812012-02-03 03:41:46 +0000654 self.calls = self._gerrit_base_calls()
luqui@chromium.org609f3952015-05-04 22:47:04 +0000655 self.calls += self._gerrit_upload_calls(
656 description, reviewers, squash,
657 expected_upstream_ref=expected_upstream_ref)
ukai@chromium.orge8077812012-02-03 03:41:46 +0000658 git_cl.main(['upload'] + upload_args)
659
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000660 def test_gerrit_upload_without_change_id(self):
661 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000662 [],
jamesr@chromium.org35d1a842012-07-27 00:20:43 +0000663 'desc\n\nBUG=\n',
ukai@chromium.orge8077812012-02-03 03:41:46 +0000664 [])
665
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000666 def test_gerrit_no_reviewer(self):
667 self._run_gerrit_upload_test(
668 [],
669 'desc\n\nBUG=\nChange-Id:123456789\n',
670 [])
671
ukai@chromium.orge8077812012-02-03 03:41:46 +0000672 def test_gerrit_reviewers_cmd_line(self):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000673 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000674 ['-r', 'foo@example.com'],
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000675 'desc\n\nBUG=\nChange-Id:123456789',
ukai@chromium.orge8077812012-02-03 03:41:46 +0000676 ['foo@example.com'])
677
678 def test_gerrit_reviewer_multiple(self):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000679 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000680 [],
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000681 'desc\nTBR=reviewer@example.com\nBUG=\nR=another@example.com\n'
682 'Change-Id:123456789\n',
ukai@chromium.orge8077812012-02-03 03:41:46 +0000683 ['reviewer@example.com', 'another@example.com'])
684
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000685 def test_gerrit_upload_squash(self):
686 self._run_gerrit_upload_test(
687 ['--squash'],
688 'desc\n\nBUG=\nChange-Id:123456789\n',
689 [],
luqui@chromium.org609f3952015-05-04 22:47:04 +0000690 squash=True,
691 expected_upstream_ref='origin/master')
ukai@chromium.orge8077812012-02-03 03:41:46 +0000692
rmistry@google.com2dd99862015-06-22 12:22:18 +0000693 def test_upload_branch_deps(self):
694 def mock_run_git(*args, **_kwargs):
695 if args[0] == ['for-each-ref',
696 '--format=%(refname:short) %(upstream:short)',
697 'refs/heads']:
698 # Create a local branch dependency tree that looks like this:
699 # test1 -> test2 -> test3 -> test4 -> test5
700 # -> test3.1
701 # test6 -> test0
702 branch_deps = [
703 'test2 test1', # test1 -> test2
704 'test3 test2', # test2 -> test3
705 'test3.1 test2', # test2 -> test3.1
706 'test4 test3', # test3 -> test4
707 'test5 test4', # test4 -> test5
708 'test6 test0', # test0 -> test6
709 'test7', # test7
710 ]
711 return '\n'.join(branch_deps)
712 self.mock(git_cl, 'RunGit', mock_run_git)
713
andybons@chromium.org962f9462016-02-03 20:00:42 +0000714 git_cl.settings = git_cl.Settings()
715 self.mock(git_cl.settings, 'GetIsGerrit', lambda: False)
716
rmistry@google.com2dd99862015-06-22 12:22:18 +0000717 class RecordCalls:
718 times_called = 0
719 record_calls = RecordCalls()
720 def mock_CMDupload(*args, **_kwargs):
721 record_calls.times_called += 1
722 return 0
723 self.mock(git_cl, 'CMDupload', mock_CMDupload)
724
725 self.calls = [
726 (('[Press enter to continue or ctrl-C to quit]',), ''),
727 ]
728
729 class MockChangelist():
730 def __init__(self):
731 pass
732 def GetBranch(self):
733 return 'test1'
734 def GetIssue(self):
735 return '123'
736 def GetPatchset(self):
737 return '1001'
738
739 ret = git_cl.upload_branch_deps(MockChangelist(), [])
740 # CMDupload should have been called 5 times because of 5 dependent branches.
741 self.assertEquals(5, record_calls.times_called)
742 self.assertEquals(0, ret)
743
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000744 def test_config_gerrit_download_hook(self):
745 self.mock(git_cl, 'FindCodereviewSettingsFile', CodereviewSettingsFileMock)
746 def ParseCodereviewSettingsContent(content):
747 keyvals = {}
748 keyvals['CODE_REVIEW_SERVER'] = 'gerrit.chromium.org'
andybons@chromium.org11f46eb2016-02-02 19:26:51 +0000749 keyvals['GERRIT_HOST'] = 'True'
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000750 return keyvals
751 self.mock(git_cl.gclient_utils, 'ParseCodereviewSettingsContent',
752 ParseCodereviewSettingsContent)
753 self.mock(git_cl.os, 'access', self._mocked_call)
754 self.mock(git_cl.os, 'chmod', self._mocked_call)
ukai@chromium.org91655502012-05-25 01:46:07 +0000755 src_dir = os.path.join(os.path.sep, 'usr', 'local', 'src')
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000756 def AbsPath(path):
ukai@chromium.org91655502012-05-25 01:46:07 +0000757 if not path.startswith(os.path.sep):
758 return os.path.join(src_dir, path)
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000759 return path
760 self.mock(git_cl.os.path, 'abspath', AbsPath)
ukai@chromium.org91655502012-05-25 01:46:07 +0000761 commit_msg_path = os.path.join(src_dir, '.git', 'hooks', 'commit-msg')
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000762 def Exists(path):
ukai@chromium.org91655502012-05-25 01:46:07 +0000763 if path == commit_msg_path:
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000764 return False
765 # others paths, such as /usr/share/locale/....
766 return True
767 self.mock(git_cl.os.path, 'exists', Exists)
joshua.lock@intel.com426f69b2012-08-02 23:41:49 +0000768 self.mock(git_cl, 'urlretrieve', self._mocked_call)
ukai@chromium.org712d6102013-11-27 00:52:58 +0000769 self.mock(git_cl, 'hasSheBang', self._mocked_call)
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000770 self.calls = [
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000771 ((['git', 'config', 'rietveld.autoupdate'],),
772 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000773 ((['git', 'config', 'rietveld.server',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000774 'gerrit.chromium.org'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000775 ((['git', 'config', '--unset-all', 'rietveld.cc'],), ''),
776 ((['git', 'config', '--unset-all',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000777 'rietveld.private'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000778 ((['git', 'config', '--unset-all',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000779 'rietveld.tree-status-url'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000780 ((['git', 'config', '--unset-all',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000781 'rietveld.viewvc-url'],), ''),
rmistry@google.com90752582014-01-14 21:04:50 +0000782 ((['git', 'config', '--unset-all',
783 'rietveld.bug-prefix'],), ''),
thestig@chromium.org44202a22014-03-11 19:22:18 +0000784 ((['git', 'config', '--unset-all',
785 'rietveld.cpplint-regex'],), ''),
786 ((['git', 'config', '--unset-all',
kjellander@chromium.org6abc6522014-12-02 07:34:49 +0000787 'rietveld.force-https-commit-url'],), ''),
788 ((['git', 'config', '--unset-all',
thestig@chromium.org44202a22014-03-11 19:22:18 +0000789 'rietveld.cpplint-ignore-regex'],), ''),
sheyang@chromium.org152cf832014-06-11 21:37:49 +0000790 ((['git', 'config', '--unset-all',
791 'rietveld.project'],), ''),
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +0000792 ((['git', 'config', '--unset-all',
793 'rietveld.pending-ref-prefix'],), ''),
rmistry@google.com5626a922015-02-26 14:03:30 +0000794 ((['git', 'config', '--unset-all',
795 'rietveld.run-post-upload-hook'],), ''),
andybons@chromium.org11f46eb2016-02-02 19:26:51 +0000796 ((['git', 'config', 'gerrit.host', 'True'],), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000797 # DownloadHooks(False)
andybons@chromium.org11f46eb2016-02-02 19:26:51 +0000798 ((['git', 'config', 'gerrit.host'],), 'True'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000799 ((['git', 'rev-parse', '--show-cdup'],), ''),
ukai@chromium.org91655502012-05-25 01:46:07 +0000800 ((commit_msg_path, os.X_OK,), False),
ukai@chromium.org712d6102013-11-27 00:52:58 +0000801 (('https://gerrit-review.googlesource.com/tools/hooks/commit-msg',
ukai@chromium.org91655502012-05-25 01:46:07 +0000802 commit_msg_path,), ''),
ukai@chromium.org712d6102013-11-27 00:52:58 +0000803 ((commit_msg_path,), True),
ukai@chromium.org91655502012-05-25 01:46:07 +0000804 ((commit_msg_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR,), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000805 # GetCodereviewSettingsInteractively
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000806 ((['git', 'config', 'rietveld.server'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000807 'gerrit.chromium.org'),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000808 (('Rietveld server (host[:port]) [https://gerrit.chromium.org]:',),
809 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000810 ((['git', 'config', 'rietveld.cc'],), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000811 (('CC list:',), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000812 ((['git', 'config', 'rietveld.private'],), ''),
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000813 (('Private flag (rietveld only):',), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000814 ((['git', 'config', 'rietveld.tree-status-url'],), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000815 (('Tree status URL:',), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000816 ((['git', 'config', 'rietveld.viewvc-url'],), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000817 (('ViewVC URL:',), ''),
818 # DownloadHooks(True)
rmistry@google.com90752582014-01-14 21:04:50 +0000819 ((['git', 'config', 'rietveld.bug-prefix'],), ''),
820 (('Bug Prefix:',), ''),
rmistry@google.com5626a922015-02-26 14:03:30 +0000821 ((['git', 'config', 'rietveld.run-post-upload-hook'],), ''),
822 (('Run Post Upload Hook:',), ''),
ukai@chromium.org91655502012-05-25 01:46:07 +0000823 ((commit_msg_path, os.X_OK,), True),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000824 ]
825 git_cl.main(['config'])
826
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000827 def test_update_reviewers(self):
828 data = [
829 ('foo', [], 'foo'),
agable@chromium.org42c20792013-09-12 17:34:49 +0000830 ('foo\nR=xx', [], 'foo\nR=xx'),
831 ('foo\nTBR=xx', [], 'foo\nTBR=xx'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000832 ('foo', ['a@c'], 'foo\n\nR=a@c'),
agable@chromium.org42c20792013-09-12 17:34:49 +0000833 ('foo\nR=xx', ['a@c'], 'foo\n\nR=a@c, xx'),
834 ('foo\nTBR=xx', ['a@c'], 'foo\n\nR=a@c\nTBR=xx'),
835 ('foo\nTBR=xx\nR=yy', ['a@c'], 'foo\n\nR=a@c, yy\nTBR=xx'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000836 ('foo\nBUG=', ['a@c'], 'foo\nBUG=\nR=a@c'),
agable@chromium.org42c20792013-09-12 17:34:49 +0000837 ('foo\nR=xx\nTBR=yy\nR=bar', ['a@c'], 'foo\n\nR=a@c, xx, bar\nTBR=yy'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000838 ('foo', ['a@c', 'b@c'], 'foo\n\nR=a@c, b@c'),
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +0000839 ('foo\nBar\n\nR=\nBUG=', ['c@c'], 'foo\nBar\n\nR=c@c\nBUG='),
840 ('foo\nBar\n\nR=\nBUG=\nR=', ['c@c'], 'foo\nBar\n\nR=c@c\nBUG='),
841 # Same as the line before, but full of whitespaces.
842 (
843 'foo\nBar\n\n R = \n BUG = \n R = ', ['c@c'],
844 'foo\nBar\n\nR=c@c\n BUG =',
845 ),
846 # Whitespaces aren't interpreted as new lines.
847 ('foo BUG=allo R=joe ', ['c@c'], 'foo BUG=allo R=joe\n\nR=c@c'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000848 ]
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +0000849 expected = [i[2] for i in data]
850 actual = []
851 for orig, reviewers, _expected in data:
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000852 obj = git_cl.ChangeDescription(orig)
853 obj.update_reviewers(reviewers)
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +0000854 actual.append(obj.description)
855 self.assertEqual(expected, actual)
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000856
wittman@chromium.org455dc922015-01-26 20:15:50 +0000857 def test_get_target_ref(self):
858 # Check remote or remote branch not present.
859 self.assertEqual(None, git_cl.GetTargetRef('origin', None, 'master', None))
860 self.assertEqual(None, git_cl.GetTargetRef(None,
861 'refs/remotes/origin/master',
862 'master', None))
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000863
wittman@chromium.org455dc922015-01-26 20:15:50 +0000864 # Check default target refs for branches.
865 self.assertEqual('refs/heads/master',
866 git_cl.GetTargetRef('origin', 'refs/remotes/origin/master',
867 None, None))
868 self.assertEqual('refs/heads/master',
869 git_cl.GetTargetRef('origin', 'refs/remotes/origin/lkgr',
870 None, None))
871 self.assertEqual('refs/heads/master',
872 git_cl.GetTargetRef('origin', 'refs/remotes/origin/lkcr',
873 None, None))
874 self.assertEqual('refs/branch-heads/123',
875 git_cl.GetTargetRef('origin',
876 'refs/remotes/branch-heads/123',
877 None, None))
878 self.assertEqual('refs/diff/test',
879 git_cl.GetTargetRef('origin',
880 'refs/remotes/origin/refs/diff/test',
881 None, None))
rmistry@google.comc68112d2015-03-03 12:48:06 +0000882 self.assertEqual('refs/heads/chrome/m42',
883 git_cl.GetTargetRef('origin',
884 'refs/remotes/origin/chrome/m42',
885 None, None))
wittman@chromium.org455dc922015-01-26 20:15:50 +0000886
887 # Check target refs for user-specified target branch.
888 for branch in ('branch-heads/123', 'remotes/branch-heads/123',
889 'refs/remotes/branch-heads/123'):
890 self.assertEqual('refs/branch-heads/123',
891 git_cl.GetTargetRef('origin',
892 'refs/remotes/origin/master',
893 branch, None))
894 for branch in ('origin/master', 'remotes/origin/master',
895 'refs/remotes/origin/master'):
896 self.assertEqual('refs/heads/master',
897 git_cl.GetTargetRef('origin',
898 'refs/remotes/branch-heads/123',
899 branch, None))
900 for branch in ('master', 'heads/master', 'refs/heads/master'):
901 self.assertEqual('refs/heads/master',
902 git_cl.GetTargetRef('origin',
903 'refs/remotes/branch-heads/123',
904 branch, None))
905
906 # Check target refs for pending prefix.
907 self.assertEqual('prefix/heads/master',
908 git_cl.GetTargetRef('origin', 'refs/remotes/origin/master',
909 None, 'prefix/'))
910
wychen@chromium.orga872e752015-04-28 23:42:18 +0000911 def test_patch_when_dirty(self):
912 # Patch when local tree is dirty
913 self.mock(git_common, 'is_dirty_git_tree', lambda x: True)
914 self.assertNotEqual(git_cl.main(['patch', '123456']), 0)
915
916 def test_diff_when_dirty(self):
917 # Do 'git cl diff' when local tree is dirty
918 self.mock(git_common, 'is_dirty_git_tree', lambda x: True)
919 self.assertNotEqual(git_cl.main(['diff']), 0)
920
921 def _patch_common(self):
922 self.mock(git_cl.Changelist, 'GetMostRecentPatchset', lambda x: '60001')
923 self.mock(git_cl.Changelist, 'GetPatchSetDiff', lambda *args: None)
wychen@chromium.org5b3bebb2015-05-28 21:41:43 +0000924 self.mock(git_cl.Changelist, 'GetDescription', lambda *args: 'Description')
wychen@chromium.orga872e752015-04-28 23:42:18 +0000925 self.mock(git_cl.Changelist, 'SetIssue', lambda *args: None)
926 self.mock(git_cl.Changelist, 'SetPatchset', lambda *args: None)
927 self.mock(git_cl, 'IsGitVersionAtLeast', lambda *args: True)
928
929 self.calls = [
930 ((['git', 'config', 'rietveld.autoupdate'],), ''),
931 ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'),
932 ((['git', 'rev-parse', '--show-cdup'],), ''),
933 ((['sed', '-e', 's|^--- a/|--- |; s|^+++ b/|+++ |'],), ''),
934 ]
935
936 def test_patch_successful(self):
937 self._patch_common()
938 self.calls += [
939 ((['git', 'apply', '--index', '-p0', '--3way'],), ''),
940 ((['git', 'commit', '-m',
wychen@chromium.org5b3bebb2015-05-28 21:41:43 +0000941 'Description\n\n' +
wychen@chromium.orga872e752015-04-28 23:42:18 +0000942 'patch from issue 123456 at patchset 60001 ' +
943 '(http://crrev.com/123456#ps60001)'],), ''),
944 ]
945 self.assertEqual(git_cl.main(['patch', '123456']), 0)
946
947 def test_patch_conflict(self):
948 self._patch_common()
949 self.calls += [
950 ((['git', 'apply', '--index', '-p0', '--3way'],), '',
951 subprocess2.CalledProcessError(1, '', '', '', '')),
952 ]
953 self.assertNotEqual(git_cl.main(['patch', '123456']), 0)
wittman@chromium.org455dc922015-01-26 20:15:50 +0000954
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000955if __name__ == '__main__':
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000956 git_cl.logging.basicConfig(
957 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000958 unittest.main()