blob: df4a794a6c10be42de67d712091b07cb50750049 [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
tandrii@chromium.org57d86542016-03-04 16:11:32 +000020import git_footers
maruel@chromium.orgddd59412011-11-30 14:20:38 +000021import subprocess2
maruel@chromium.orgddd59412011-11-30 14:20:38 +000022
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000023class PresubmitMock(object):
24 def __init__(self, *args, **kwargs):
25 self.reviewers = []
26 @staticmethod
27 def should_continue():
28 return True
29
30
31class RietveldMock(object):
32 def __init__(self, *args, **kwargs):
33 pass
maruel@chromium.org78936cb2013-04-11 00:17:52 +000034
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000035 @staticmethod
36 def get_description(issue):
37 return 'Issue: %d' % issue
38
maruel@chromium.org78936cb2013-04-11 00:17:52 +000039 @staticmethod
40 def get_issue_properties(_issue, _messages):
41 return {
42 'reviewers': ['joe@chromium.org', 'john@chromium.org'],
43 'messages': [
44 {
45 'approval': True,
46 'sender': 'john@chromium.org',
47 },
48 ],
49 }
50
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000051
52class WatchlistsMock(object):
53 def __init__(self, _):
54 pass
55 @staticmethod
56 def GetWatchersForPaths(_):
57 return ['joe@example.com']
58
59
ukai@chromium.org78c4b982012-02-14 02:20:26 +000060class CodereviewSettingsFileMock(object):
61 def __init__(self):
62 pass
63 # pylint: disable=R0201
64 def read(self):
65 return ("CODE_REVIEW_SERVER: gerrit.chromium.org\n" +
andybons@chromium.org11f46eb2016-02-02 19:26:51 +000066 "GERRIT_HOST: True\n")
ukai@chromium.org78c4b982012-02-14 02:20:26 +000067
68
vadimsh@chromium.orgeed4df32015-04-10 21:30:20 +000069class AuthenticatorMock(object):
70 def __init__(self, *_args):
71 pass
72 def has_cached_credentials(self):
73 return True
74
75
maruel@chromium.orgddd59412011-11-30 14:20:38 +000076class TestGitCl(TestCase):
77 def setUp(self):
78 super(TestGitCl, self).setUp()
79 self.calls = []
80 self._calls_done = 0
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000081 self.mock(subprocess2, 'call', self._mocked_call)
82 self.mock(subprocess2, 'check_call', self._mocked_call)
83 self.mock(subprocess2, 'check_output', self._mocked_call)
84 self.mock(subprocess2, 'communicate', self._mocked_call)
tandrii@chromium.orga342c922016-03-16 07:08:25 +000085 self.mock(git_cl.gclient_utils, 'CheckCallAndFilter', self._mocked_call)
sbc@chromium.org71437c02015-04-09 19:29:40 +000086 self.mock(git_common, 'is_dirty_git_tree', lambda x: False)
iannucci@chromium.org9e849272014-04-04 00:31:55 +000087 self.mock(git_common, 'get_or_create_merge_base',
88 lambda *a: (
89 self._mocked_call(['get_or_create_merge_base']+list(a))))
pgervais@chromium.org8ba38ff2015-06-11 21:41:25 +000090 self.mock(git_cl, 'BranchExists', lambda _: True)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000091 self.mock(git_cl, 'FindCodereviewSettingsFile', lambda: '')
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000092 self.mock(git_cl, 'ask_for_data', self._mocked_call)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000093 self.mock(git_cl.presubmit_support, 'DoPresubmitChecks', PresubmitMock)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000094 self.mock(git_cl.rietveld, 'Rietveld', RietveldMock)
maruel@chromium.org4bac4b52012-11-27 20:33:52 +000095 self.mock(git_cl.rietveld, 'CachingRietveld', RietveldMock)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000096 self.mock(git_cl.upload, 'RealMain', self.fail)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000097 self.mock(git_cl.watchlists, 'Watchlists', WatchlistsMock)
vadimsh@chromium.orgeed4df32015-04-10 21:30:20 +000098 self.mock(git_cl.auth, 'get_authenticator_for_host', AuthenticatorMock)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000099 # It's important to reset settings to not have inter-tests interference.
100 git_cl.settings = None
101
102 def tearDown(self):
wychen@chromium.org445c8962015-04-28 23:30:05 +0000103 try:
104 if not self.has_failed():
105 self.assertEquals([], self.calls)
106 finally:
107 super(TestGitCl, self).tearDown()
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000108
iannucci@chromium.org9e849272014-04-04 00:31:55 +0000109 def _mocked_call(self, *args, **_kwargs):
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000110 self.assertTrue(
111 self.calls,
112 '@%d Expected: <Missing> Actual: %r' % (self._calls_done, args))
wychen@chromium.orga872e752015-04-28 23:42:18 +0000113 top = self.calls.pop(0)
114 if len(top) > 2 and top[2]:
115 raise top[2]
116 expected_args, result = top
117
maruel@chromium.orge52678e2013-04-26 18:34:44 +0000118 # Also logs otherwise it could get caught in a try/finally and be hard to
119 # diagnose.
120 if expected_args != args:
121 msg = '@%d Expected: %r Actual: %r' % (
122 self._calls_done, expected_args, args)
123 git_cl.logging.error(msg)
124 self.fail(msg)
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000125 self._calls_done += 1
126 return result
127
maruel@chromium.orga3353652011-11-30 14:26:57 +0000128 @classmethod
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000129 def _is_gerrit_calls(cls, gerrit=False):
130 return [((['git', 'config', 'rietveld.autoupdate'],), ''),
131 ((['git', 'config', 'gerrit.host'],), 'True' if gerrit else '')]
132
133 @classmethod
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000134 def _upload_calls(cls, similarity, find_copies, private):
iannucci@chromium.org79540052012-10-19 23:15:26 +0000135 return (cls._git_base_calls(similarity, find_copies) +
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000136 cls._git_upload_calls(private))
maruel@chromium.orga3353652011-11-30 14:26:57 +0000137
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000138 @classmethod
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000139 def _upload_no_rev_calls(cls, similarity, find_copies):
140 return (cls._git_base_calls(similarity, find_copies) +
141 cls._git_upload_no_rev_calls())
142
143 @classmethod
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000144 def _git_base_calls(cls, similarity, find_copies):
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000145 if similarity is None:
146 similarity = '50'
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000147 similarity_call = ((['git', 'config', '--int', '--get',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000148 'branch.master.git-cl-similarity'],), '')
149 else:
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000150 similarity_call = ((['git', 'config', '--int',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000151 'branch.master.git-cl-similarity', similarity],), '')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000152
153 if find_copies is None:
154 find_copies = True
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000155 find_copies_call = ((['git', 'config', '--int', '--get',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000156 'branch.master.git-find-copies'],), '')
157 else:
158 val = str(int(find_copies))
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000159 find_copies_call = ((['git', 'config', '--int',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000160 'branch.master.git-find-copies', val],), '')
161
162 if find_copies:
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000163 stat_call = ((['git', 'diff', '--no-ext-diff', '--stat',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000164 '--find-copies-harder', '-l100000', '-C'+similarity,
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000165 'fake_ancestor_sha', 'HEAD'],), '+dat')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000166 else:
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000167 stat_call = ((['git', 'diff', '--no-ext-diff', '--stat',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000168 '-M'+similarity, 'fake_ancestor_sha', 'HEAD'],), '+dat')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000169
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000170 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000171 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
tandrii@chromium.org87985d22016-03-24 17:33:33 +0000172 similarity_call,
173 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
174 find_copies_call,
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000175 ] + cls._is_gerrit_calls() + [
tandrii@chromium.org87985d22016-03-24 17:33:33 +0000176 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +0000177 ((['git', 'config', 'branch.master.rietveldissue'],), ''),
178 ((['git', 'config', 'branch.master.gerritissue'],), ''),
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +0000179 ((['git', 'config', 'rietveld.server'],),
180 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000181 ((['git', 'config', 'branch.master.merge'],), 'master'),
182 ((['git', 'config', 'branch.master.remote'],), 'origin'),
iannucci@chromium.org9e849272014-04-04 00:31:55 +0000183 ((['get_or_create_merge_base', 'master', 'master'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000184 'fake_ancestor_sha'),
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000185 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000186 ((['git', 'rev-parse', '--show-cdup'],), ''),
187 ((['git', 'rev-parse', 'HEAD'],), '12345'),
188 ((['git', 'diff', '--name-status', '--no-renames', '-r',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000189 'fake_ancestor_sha...', '.'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000190 'M\t.gitignore\n'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000191 ((['git', 'config', 'branch.master.rietveldpatchset'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000192 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000193 ((['git', 'log', '--pretty=format:%s%n%n%b',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000194 'fake_ancestor_sha...'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000195 'foo'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000196 ((['git', 'config', 'user.email'],), 'me@example.com'),
iannucci@chromium.org79540052012-10-19 23:15:26 +0000197 stat_call,
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000198 ((['git', 'log', '--pretty=format:%s\n\n%b',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000199 'fake_ancestor_sha..HEAD'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000200 'desc\n'),
rmistry@google.com90752582014-01-14 21:04:50 +0000201 ((['git', 'config', 'rietveld.bug-prefix'],), ''),
maruel@chromium.orga3353652011-11-30 14:26:57 +0000202 ]
203
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000204 @classmethod
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000205 def _git_upload_no_rev_calls(cls):
206 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000207 ((['git', 'config', 'core.editor'],), ''),
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000208 ]
209
210 @classmethod
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000211 def _git_upload_calls(cls, private):
212 if private:
tyoshino@chromium.org99918ab2013-09-30 06:17:28 +0000213 cc_call = []
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000214 private_call = []
215 else:
tyoshino@chromium.org99918ab2013-09-30 06:17:28 +0000216 cc_call = [((['git', 'config', 'rietveld.cc'],), '')]
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000217 private_call = [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000218 ((['git', 'config', 'rietveld.private'],), '')]
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000219
maruel@chromium.orga3353652011-11-30 14:26:57 +0000220 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000221 ((['git', 'config', 'core.editor'],), ''),
tyoshino@chromium.org99918ab2013-09-30 06:17:28 +0000222 ] + cc_call + private_call + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000223 ((['git', 'config', 'branch.master.base-url'],), ''),
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +0000224 ((['git', 'config', 'rietveld.pending-ref-prefix'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000225 ((['git',
bratell@opera.com05fb9112014-07-07 09:30:23 +0000226 'config', '--local', '--get-regexp', '^svn-remote\\.'],),
227 (('', None), 0)),
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000228 ((['git', 'rev-parse', '--show-cdup'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000229 ((['git', 'svn', 'info'],), ''),
sheyang@chromium.org152cf832014-06-11 21:37:49 +0000230 ((['git', 'config', 'rietveld.project'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000231 ((['git',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000232 'config', 'branch.master.rietveldissue', '1'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000233 ((['git', 'config', 'branch.master.rietveldserver',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000234 'https://codereview.example.com'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000235 ((['git',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000236 'config', 'branch.master.rietveldpatchset', '2'],), ''),
tandrii@chromium.org1e67bb72016-02-11 12:15:49 +0000237 ] + cls._git_post_upload_calls()
238
239 @classmethod
240 def _git_post_upload_calls(cls):
241 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000242 ((['git', 'rev-parse', 'HEAD'],), 'hash'),
243 ((['git', 'symbolic-ref', 'HEAD'],), 'hash'),
244 ((['git',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000245 'config', 'branch.hash.last-upload-hash', 'hash'],), ''),
rmistry@google.com5626a922015-02-26 14:03:30 +0000246 ((['git', 'config', 'rietveld.run-post-upload-hook'],), ''),
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000247 ]
248
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000249 @staticmethod
250 def _git_sanity_checks(diff_base, working_branch):
251 fake_ancestor = 'fake_ancestor'
252 fake_cl = 'fake_cl_for_patch'
253 return [
254 # Calls to verify branch point is ancestor
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000255 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000256 'rev-parse', '--verify', diff_base],), fake_ancestor),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000257 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000258 'merge-base', fake_ancestor, 'HEAD'],), fake_ancestor),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000259 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000260 'rev-list', '^' + fake_ancestor, 'HEAD'],), fake_cl),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000261 # Mock a config miss (error code 1)
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000262 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000263 'config', 'gitcl.remotebranch'],), (('', None), 1)),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000264 # Call to GetRemoteBranch()
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000265 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000266 'config', 'branch.%s.merge' % working_branch],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000267 'refs/heads/master'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000268 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000269 'config', 'branch.%s.remote' % working_branch],), 'origin'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000270 ((['git', 'rev-list', '^' + fake_ancestor,
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000271 'refs/remotes/origin/master'],), ''),
272 ]
273
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000274 @classmethod
275 def _dcommit_calls_1(cls):
276 return [
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +0000277 ((['git', 'config', 'rietveld.autoupdate'],),
278 ''),
279 ((['git', 'config', 'rietveld.pending-ref-prefix'],),
280 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000281 ((['git',
bratell@opera.com05fb9112014-07-07 09:30:23 +0000282 'config', '--local', '--get-regexp', '^svn-remote\\.'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000283 ((('svn-remote.svn.url svn://svn.chromium.org/chrome\n'
284 'svn-remote.svn.fetch trunk/src:refs/remotes/origin/master'),
285 None),
286 0)),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000287 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
288 ((['git', 'config', '--int', '--get',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000289 'branch.working.git-cl-similarity'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000290 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
291 ((['git', 'config', '--int', '--get',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000292 'branch.working.git-find-copies'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000293 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
294 ((['git',
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +0000295 'config', 'branch.working.rietveldissue'],), '12345'),
296 ((['git',
297 'config', 'rietveld.server'],), 'codereview.example.com'),
298 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000299 'config', 'branch.working.merge'],), 'refs/heads/master'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000300 ((['git', 'config', 'branch.working.remote'],), 'origin'),
iannucci@chromium.org5724c962014-04-11 09:32:56 +0000301 ((['git', 'config', 'branch.working.merge'],),
302 'refs/heads/master'),
303 ((['git', 'config', 'branch.working.remote'],), 'origin'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000304 ((['git', 'rev-list', '--merges',
szager@chromium.orge84b7542012-06-15 21:26:58 +0000305 '--grep=^SVN changes up to revision [0-9]*$',
szager@chromium.org9bb85e22012-06-13 20:28:23 +0000306 'refs/remotes/origin/master^!'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000307 ((['git', 'rev-list', '^refs/heads/working',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000308 'refs/remotes/origin/master'],),
309 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000310 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000311 'log', '--grep=^git-svn-id:', '-1', '--pretty=format:%H'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000312 '3fc18b62c4966193eb435baabe2d18a3810ec82e'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000313 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000314 'rev-list', '^3fc18b62c4966193eb435baabe2d18a3810ec82e',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000315 'refs/remotes/origin/master'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000316 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000317 'merge-base', 'refs/remotes/origin/master', 'HEAD'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000318 'fake_ancestor_sha'),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000319 ]
320
321 @classmethod
322 def _dcommit_calls_normal(cls):
323 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000324 ((['git', 'rev-parse', '--show-cdup'],), ''),
325 ((['git', 'rev-parse', 'HEAD'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000326 '00ff397798ea57439712ed7e04ab96e13969ef40'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000327 ((['git',
mcgrathr@chromium.org9249f642013-06-03 21:36:18 +0000328 'diff', '--name-status', '--no-renames', '-r', 'fake_ancestor_sha...',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000329 '.'],),
330 'M\tPRESUBMIT.py'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000331 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000332 'config', 'branch.working.rietveldpatchset'],), '31137'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000333 ((['git', 'config', 'branch.working.rietveldserver'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000334 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000335 ((['git', 'config', 'user.email'],), 'author@example.com'),
336 ((['git', 'config', 'rietveld.tree-status-url'],), ''),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000337 ]
338
339 @classmethod
340 def _dcommit_calls_bypassed(cls):
341 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000342 ((['git', 'config', 'branch.working.rietveldserver'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000343 'codereview.example.com'),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000344 ]
345
346 @classmethod
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000347 def _dcommit_calls_3(cls):
348 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000349 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000350 'diff', '--no-ext-diff', '--stat', '--find-copies-harder',
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000351 '-l100000', '-C50', 'fake_ancestor_sha',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000352 'refs/heads/working'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000353 (' PRESUBMIT.py | 2 +-\n'
354 ' 1 files changed, 1 insertions(+), 1 deletions(-)\n')),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000355 ((['git', 'show-ref', '--quiet', '--verify',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000356 'refs/heads/git-cl-commit'],),
357 (('', None), 0)),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000358 ((['git', 'branch', '-D', 'git-cl-commit'],), ''),
359 ((['git', 'show-ref', '--quiet', '--verify',
szager@chromium.org9bb85e22012-06-13 20:28:23 +0000360 'refs/heads/git-cl-cherry-pick'],), ''),
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000361 ((['git', 'rev-parse', '--show-cdup'],), '\n'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000362 ((['git', 'checkout', '-q', '-b', 'git-cl-commit'],), ''),
363 ((['git', 'reset', '--soft', 'fake_ancestor_sha'],), ''),
364 ((['git', 'commit', '-m',
maruel@chromium.orge52678e2013-04-26 18:34:44 +0000365 'Issue: 12345\n\nR=john@chromium.org\n\n'
sergiyb@chromium.org4b39c5f2015-07-07 10:33:12 +0000366 'Review URL: https://codereview.example.com/12345 .'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000367 ''),
kjellander@chromium.org6abc6522014-12-02 07:34:49 +0000368 ((['git', 'config', 'rietveld.force-https-commit-url'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000369 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000370 'svn', 'dcommit', '-C50', '--no-rebase', '--rmdir'],),
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000371 (('', None), 0)),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000372 ((['git', 'checkout', '-q', 'working'],), ''),
373 ((['git', 'branch', '-D', 'git-cl-commit'],), ''),
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000374 ]
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000375
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000376 @staticmethod
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000377 def _cmd_line(description, args, similarity, find_copies, private):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000378 """Returns the upload command line passed to upload.RealMain()."""
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000379 return [
380 'upload', '--assume_yes', '--server',
maruel@chromium.orgeb5edbc2012-01-16 17:03:28 +0000381 'https://codereview.example.com',
maruel@chromium.org71e12a92012-02-14 02:34:15 +0000382 '--message', description
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000383 ] + args + [
384 '--cc', 'joe@example.com',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000385 ] + (['--private'] if private else []) + [
iannucci@chromium.org79540052012-10-19 23:15:26 +0000386 '--git_similarity', similarity or '50'
387 ] + (['--git_no_find_copies'] if find_copies == False else []) + [
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000388 'fake_ancestor_sha', 'HEAD'
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000389 ]
390
391 def _run_reviewer_test(
392 self,
393 upload_args,
394 expected_description,
395 returned_description,
396 final_description,
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000397 reviewers,
398 private=False):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000399 """Generic reviewer test framework."""
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000400 try:
401 similarity = upload_args[upload_args.index('--similarity')+1]
402 except ValueError:
403 similarity = None
iannucci@chromium.org79540052012-10-19 23:15:26 +0000404
405 if '--find-copies' in upload_args:
406 find_copies = True
407 elif '--no-find-copies' in upload_args:
408 find_copies = False
409 else:
410 find_copies = None
411
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000412 private = '--private' in upload_args
413
414 self.calls = self._upload_calls(similarity, find_copies, private)
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000415
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000416 def RunEditor(desc, _, **kwargs):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000417 self.assertEquals(
418 '# Enter a description of the change.\n'
janx@chromium.org104b2db2013-04-18 12:58:40 +0000419 '# This will be displayed on the codereview site.\n'
alancutter@chromium.org63a4d7f2013-05-31 02:22:45 +0000420 '# The first line will also be used as the subject of the review.\n'
alancutter@chromium.orgbd1073e2013-06-01 00:34:38 +0000421 '#--------------------This line is 72 characters long'
422 '--------------------\n' +
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000423 expected_description,
424 desc)
425 return returned_description
426 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor)
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000427
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000428 def check_upload(args):
iannucci@chromium.org79540052012-10-19 23:15:26 +0000429 cmd_line = self._cmd_line(final_description, reviewers, similarity,
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000430 find_copies, private)
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000431 self.assertEquals(cmd_line, args)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000432 return 1, 2
433 self.mock(git_cl.upload, 'RealMain', check_upload)
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000434
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000435 git_cl.main(['upload'] + upload_args)
436
437 def test_no_reviewer(self):
438 self._run_reviewer_test(
439 [],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000440 'desc\n\nBUG=',
441 '# Blah blah comment.\ndesc\n\nBUG=',
442 'desc\n\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000443 [])
444
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000445 def test_keep_similarity(self):
446 self._run_reviewer_test(
447 ['--similarity', '70'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000448 'desc\n\nBUG=',
449 '# Blah blah comment.\ndesc\n\nBUG=',
450 'desc\n\nBUG=',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000451 [])
452
iannucci@chromium.org79540052012-10-19 23:15:26 +0000453 def test_keep_find_copies(self):
454 self._run_reviewer_test(
455 ['--no-find-copies'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000456 'desc\n\nBUG=',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000457 '# Blah blah comment.\ndesc\n\nBUG=\n',
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000458 'desc\n\nBUG=',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000459 [])
460
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000461 def test_private(self):
462 self._run_reviewer_test(
463 ['--private'],
464 'desc\n\nBUG=',
465 '# Blah blah comment.\ndesc\n\nBUG=\n',
466 'desc\n\nBUG=',
467 [])
468
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000469 def test_reviewers_cmd_line(self):
470 # Reviewer is passed as-is
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000471 description = 'desc\n\nR=foo@example.com\nBUG='
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000472 self._run_reviewer_test(
473 ['-r' 'foo@example.com'],
474 description,
475 '\n%s\n' % description,
476 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000477 ['--reviewers=foo@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000478
479 def test_reviewer_tbr_overriden(self):
480 # Reviewer is overriden with TBR
481 # Also verifies the regexp work without a trailing LF
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000482 description = 'Foo Bar\n\nTBR=reviewer@example.com'
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000483 self._run_reviewer_test(
484 ['-r' 'foo@example.com'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000485 'desc\n\nR=foo@example.com\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000486 description.strip('\n'),
487 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000488 ['--reviewers=reviewer@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000489
490 def test_reviewer_multiple(self):
491 # Handles multiple R= or TBR= lines.
492 description = (
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000493 'Foo Bar\nTBR=reviewer@example.com\nBUG=\nR=another@example.com')
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000494 self._run_reviewer_test(
495 [],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000496 'desc\n\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000497 description,
498 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000499 ['--reviewers=another@example.com,reviewer@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000500
maruel@chromium.orga3353652011-11-30 14:26:57 +0000501 def test_reviewer_send_mail(self):
502 # --send-mail can be used without -r if R= is used
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000503 description = 'Foo Bar\nR=reviewer@example.com'
maruel@chromium.orga3353652011-11-30 14:26:57 +0000504 self._run_reviewer_test(
505 ['--send-mail'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000506 'desc\n\nBUG=',
maruel@chromium.orga3353652011-11-30 14:26:57 +0000507 description.strip('\n'),
508 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000509 ['--reviewers=reviewer@example.com', '--send_mail'])
maruel@chromium.orga3353652011-11-30 14:26:57 +0000510
511 def test_reviewer_send_mail_no_rev(self):
512 # Fails without a reviewer.
maruel@chromium.org2e23ce32013-05-07 12:42:28 +0000513 stdout = StringIO.StringIO()
514 stderr = StringIO.StringIO()
maruel@chromium.orga3353652011-11-30 14:26:57 +0000515 try:
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000516 self.calls = self._upload_no_rev_calls(None, None)
517 def RunEditor(desc, _, **kwargs):
maruel@chromium.orga3353652011-11-30 14:26:57 +0000518 return desc
519 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor)
maruel@chromium.org2e23ce32013-05-07 12:42:28 +0000520 self.mock(sys, 'stdout', stdout)
521 self.mock(sys, 'stderr', stderr)
maruel@chromium.orga3353652011-11-30 14:26:57 +0000522 git_cl.main(['upload', '--send-mail'])
523 self.fail()
524 except SystemExit:
maruel@chromium.org2e23ce32013-05-07 12:42:28 +0000525 self.assertEqual(
526 'Using 50% similarity for rename/copy detection. Override with '
527 '--similarity.\n',
528 stdout.getvalue())
529 self.assertEqual(
530 'Must specify reviewers to send email.\n', stderr.getvalue())
maruel@chromium.orga3353652011-11-30 14:26:57 +0000531
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000532 def test_dcommit(self):
533 self.calls = (
534 self._dcommit_calls_1() +
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000535 self._git_sanity_checks('fake_ancestor_sha', 'working') +
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000536 self._dcommit_calls_normal() +
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000537 self._dcommit_calls_3())
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000538 git_cl.main(['dcommit'])
539
540 def test_dcommit_bypass_hooks(self):
541 self.calls = (
542 self._dcommit_calls_1() +
543 self._dcommit_calls_bypassed() +
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000544 self._dcommit_calls_3())
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000545 git_cl.main(['dcommit', '--bypass-hooks'])
546
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000547
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000548 @classmethod
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000549 def _gerrit_base_calls(cls, issue=None):
ukai@chromium.orge8077812012-02-03 03:41:46 +0000550 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000551 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
552 ((['git', 'config', '--int', '--get',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000553 'branch.master.git-cl-similarity'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000554 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
555 ((['git', 'config', '--int', '--get',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000556 'branch.master.git-find-copies'],), ''),
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000557 ] + cls._is_gerrit_calls(True) + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000558 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +0000559 ((['git', 'config', 'branch.master.rietveldissue'],), ''),
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000560 ((['git', 'config', 'branch.master.gerritissue'],),
561 '' if issue is None else str(issue)),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000562 ((['git', 'config', 'branch.master.merge'],), 'master'),
563 ((['git', 'config', 'branch.master.remote'],), 'origin'),
iannucci@chromium.org9e849272014-04-04 00:31:55 +0000564 ((['get_or_create_merge_base', 'master', 'master'],),
565 'fake_ancestor_sha'),
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +0000566 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000567 ((['git', 'rev-parse', '--show-cdup'],), ''),
568 ((['git', 'rev-parse', 'HEAD'],), '12345'),
569 ((['git',
mcgrathr@chromium.org9249f642013-06-03 21:36:18 +0000570 'diff', '--name-status', '--no-renames', '-r',
571 'fake_ancestor_sha...', '.'],),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000572 'M\t.gitignore\n'),
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +0000573 ((['git', 'config', 'branch.master.gerritpatchset'],), ''),
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000574 ] + ([] if issue else [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000575 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000576 'log', '--pretty=format:%s%n%n%b', 'fake_ancestor_sha...'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000577 'foo'),
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000578 ]) + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000579 ((['git', 'config', 'user.email'],), 'me@example.com'),
580 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000581 'diff', '--no-ext-diff', '--stat', '--find-copies-harder',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000582 '-l100000', '-C50', 'fake_ancestor_sha', 'HEAD'],),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000583 '+dat'),
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +0000584 ]
ukai@chromium.orge8077812012-02-03 03:41:46 +0000585
tandrii@chromium.org1e67bb72016-02-11 12:15:49 +0000586 @classmethod
587 def _gerrit_upload_calls(cls, description, reviewers, squash,
tandrii@chromium.org10625002016-03-04 20:03:47 +0000588 expected_upstream_ref='origin/refs/heads/master',
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000589 post_amend_description=None, issue=None):
tandrii@chromium.org10625002016-03-04 20:03:47 +0000590 if post_amend_description is None:
591 post_amend_description = description
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000592
ukai@chromium.orge8077812012-02-03 03:41:46 +0000593 calls = [
bauerb@chromium.org54b400c2016-01-14 10:08:25 +0000594 ((['git', 'config', '--bool', 'gerrit.squash-uploads'],), 'false'),
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000595 ]
596 # If issue is given, then description is fetched from Gerrit instead.
597 if issue is None:
598 if squash:
599 calls += [
600 ((['git', 'show', '--format=%B', '-s',
601 'refs/heads/git_cl_uploads/master'],), '')]
602 calls += [
603 ((['git', 'log', '--pretty=format:%s\n\n%b',
604 'fake_ancestor_sha..HEAD'],),
605 description)]
tandrii@chromium.org57d86542016-03-04 16:11:32 +0000606 if not git_footers.get_footer_change_id(description) and not squash:
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000607 calls += [
tandrii@chromium.org10625002016-03-04 20:03:47 +0000608 # DownloadGerritHook(False)
609 ((False, ),
610 ''),
611 # Amending of commit message to get the Change-Id.
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000612 ((['git', 'log', '--pretty=format:%s\n\n%b',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000613 'fake_ancestor_sha..HEAD'],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000614 description),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000615 ((['git', 'commit', '--amend', '-m', description],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000616 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000617 ((['git', 'log', '--pretty=format:%s\n\n%b',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000618 'fake_ancestor_sha..HEAD'],),
tandrii@chromium.org10625002016-03-04 20:03:47 +0000619 post_amend_description)
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000620 ]
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000621 if squash:
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000622 if not issue:
623 # Prompting to edit description on first upload.
624 calls += [
625 ((['git', 'config', 'core.editor'],), ''),
626 ((['RunEditor'],), description),
627 ]
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000628 ref_to_push = 'abcdef0123456789'
629 calls += [
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000630 ((['git', 'config', 'branch.master.merge'],),
631 'refs/heads/master'),
632 ((['git', 'config', 'branch.master.remote'],),
633 'origin'),
634 ((['get_or_create_merge_base', 'master', 'master'],),
635 'origin/master'),
636 ((['git', 'rev-parse', 'HEAD:'],),
637 '0123456789abcdef'),
638 ((['git', 'commit-tree', '0123456789abcdef', '-p',
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000639 'origin/master', '-m', description],),
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000640 ref_to_push),
641 ]
642 else:
643 ref_to_push = 'HEAD'
644
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000645 calls += [
luqui@chromium.org609f3952015-05-04 22:47:04 +0000646 ((['git', 'rev-list',
647 expected_upstream_ref + '..' + ref_to_push],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000648 ((['git', 'config', 'rietveld.cc'],), '')
ukai@chromium.orge8077812012-02-03 03:41:46 +0000649 ]
ukai@chromium.org19bbfa22012-02-03 16:18:11 +0000650 receive_pack = '--receive-pack=git receive-pack '
ukai@chromium.orge8077812012-02-03 03:41:46 +0000651 receive_pack += '--cc=joe@example.com' # from watch list
652 if reviewers:
653 receive_pack += ' '
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000654 receive_pack += ' '.join(
655 '--reviewer=' + email for email in sorted(reviewers))
ukai@chromium.org19bbfa22012-02-03 16:18:11 +0000656 receive_pack += ''
ukai@chromium.orge8077812012-02-03 03:41:46 +0000657 calls += [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000658 ((['git',
luqui@chromium.org609f3952015-05-04 22:47:04 +0000659 'push', receive_pack, 'origin',
660 ref_to_push + ':refs/for/refs/heads/master'],),
tandrii@chromium.orga342c922016-03-16 07:08:25 +0000661 ('remote:\n'
662 'remote: Processing changes: (\)\n'
663 'remote: Processing changes: (|)\n'
664 'remote: Processing changes: (/)\n'
665 'remote: Processing changes: (-)\n'
666 'remote: Processing changes: new: 1 (/)\n'
667 'remote: Processing changes: new: 1, done\n'
668 'remote:\n'
669 'remote: New Changes:\n'
670 'remote: https://chromium-review.googlesource.com/123456 XXX.\n'
671 'remote:\n'
672 'To https://chromium.googlesource.com/yyy/zzz\n'
673 ' * [new branch] hhhh -> refs/for/refs/heads/master\n')),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000674 ]
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000675 if squash:
676 calls += [
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +0000677 ((['git', 'config', 'branch.master.gerritissue', '123456'],), ''),
678 ((['git', 'config', 'branch.master.gerritserver'],), ''),
679 ((['git', 'config', 'remote.origin.url'],),
680 'https://chromium.googlesource.com/my/repo.git'),
681 ((['git', 'config', 'branch.master.gerritserver',
682 'https://chromium-review.googlesource.com'],), ''),
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000683 ((['git', 'config', 'branch.master.gerritsquashhash',
684 'abcdef0123456789'],), ''),
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000685 ]
tandrii@chromium.org1e67bb72016-02-11 12:15:49 +0000686 calls += cls._git_post_upload_calls()
ukai@chromium.orge8077812012-02-03 03:41:46 +0000687 return calls
688
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000689 def _run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000690 self,
691 upload_args,
692 description,
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000693 reviewers,
luqui@chromium.org609f3952015-05-04 22:47:04 +0000694 squash=False,
tandrii@chromium.org10625002016-03-04 20:03:47 +0000695 expected_upstream_ref='origin/refs/heads/master',
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000696 post_amend_description=None,
697 issue=None):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000698 """Generic gerrit upload test framework."""
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000699 self.calls = self._gerrit_base_calls(issue=issue)
luqui@chromium.org609f3952015-05-04 22:47:04 +0000700 self.calls += self._gerrit_upload_calls(
701 description, reviewers, squash,
tandrii@chromium.org10625002016-03-04 20:03:47 +0000702 expected_upstream_ref=expected_upstream_ref,
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000703 post_amend_description=post_amend_description,
704 issue=issue)
tandrii@chromium.org09d7a6a2016-03-04 15:44:48 +0000705 # Uncomment when debugging.
706 # print '\n'.join(map(lambda x: '%2i: %s' % x, enumerate(self.calls)))
ukai@chromium.orge8077812012-02-03 03:41:46 +0000707 git_cl.main(['upload'] + upload_args)
708
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000709 def test_gerrit_upload_without_change_id(self):
tandrii@chromium.org10625002016-03-04 20:03:47 +0000710 self.mock(git_cl, 'DownloadGerritHook', self._mocked_call)
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000711 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000712 [],
jamesr@chromium.org35d1a842012-07-27 00:20:43 +0000713 'desc\n\nBUG=\n',
tandrii@chromium.org10625002016-03-04 20:03:47 +0000714 [],
715 post_amend_description='desc\n\nBUG=\n\nChange-Id: Ixxx')
ukai@chromium.orge8077812012-02-03 03:41:46 +0000716
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000717 def test_gerrit_no_reviewer(self):
718 self._run_gerrit_upload_test(
719 [],
tandrii@chromium.org09d7a6a2016-03-04 15:44:48 +0000720 'desc\n\nBUG=\n\nChange-Id: I123456789\n',
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000721 [])
722
ukai@chromium.orge8077812012-02-03 03:41:46 +0000723 def test_gerrit_reviewers_cmd_line(self):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000724 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000725 ['-r', 'foo@example.com'],
tandrii@chromium.org09d7a6a2016-03-04 15:44:48 +0000726 'desc\n\nBUG=\n\nChange-Id: I123456789',
ukai@chromium.orge8077812012-02-03 03:41:46 +0000727 ['foo@example.com'])
728
729 def test_gerrit_reviewer_multiple(self):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000730 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000731 [],
tandrii@chromium.org09d7a6a2016-03-04 15:44:48 +0000732 'desc\nTBR=reviewer@example.com\nBUG=\nR=another@example.com\n\n'
733 'Change-Id: 123456789\n',
ukai@chromium.orge8077812012-02-03 03:41:46 +0000734 ['reviewer@example.com', 'another@example.com'])
735
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000736 def test_gerrit_upload_squash_first(self):
737 # Mock Gerrit CL description to indicate the first upload.
738 self.mock(git_cl.Changelist, 'GetDescription',
739 lambda *_: None)
740 self.mock(git_cl.gclient_utils, 'RunEditor',
741 lambda *_, **__: self._mocked_call(['RunEditor']))
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000742 self._run_gerrit_upload_test(
743 ['--squash'],
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000744 'desc\nBUG=\n\nChange-Id: 123456789',
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000745 [],
luqui@chromium.org609f3952015-05-04 22:47:04 +0000746 squash=True,
747 expected_upstream_ref='origin/master')
ukai@chromium.orge8077812012-02-03 03:41:46 +0000748
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000749 def test_gerrit_upload_squash_reupload(self):
750 description = 'desc\nBUG=\n\nChange-Id: 123456789'
751 # Mock Gerrit CL description to indicate re-upload.
752 self.mock(git_cl.Changelist, 'GetDescription',
753 lambda *args: description)
754 self.mock(git_cl.Changelist, 'GetMostRecentPatchset',
755 lambda *args: 1)
756 self.mock(git_cl._GerritChangelistImpl, '_GetChangeDetail',
757 lambda *args: {'change_id': '123456789'})
758 self._run_gerrit_upload_test(
759 ['--squash'],
760 description,
761 [],
762 squash=True,
763 expected_upstream_ref='origin/master',
764 issue=123456)
765
rmistry@google.com2dd99862015-06-22 12:22:18 +0000766 def test_upload_branch_deps(self):
767 def mock_run_git(*args, **_kwargs):
768 if args[0] == ['for-each-ref',
769 '--format=%(refname:short) %(upstream:short)',
770 'refs/heads']:
771 # Create a local branch dependency tree that looks like this:
772 # test1 -> test2 -> test3 -> test4 -> test5
773 # -> test3.1
774 # test6 -> test0
775 branch_deps = [
776 'test2 test1', # test1 -> test2
777 'test3 test2', # test2 -> test3
778 'test3.1 test2', # test2 -> test3.1
779 'test4 test3', # test3 -> test4
780 'test5 test4', # test4 -> test5
781 'test6 test0', # test0 -> test6
782 'test7', # test7
783 ]
784 return '\n'.join(branch_deps)
785 self.mock(git_cl, 'RunGit', mock_run_git)
786
andybons@chromium.org962f9462016-02-03 20:00:42 +0000787 git_cl.settings = git_cl.Settings()
788 self.mock(git_cl.settings, 'GetIsGerrit', lambda: False)
789
rmistry@google.com2dd99862015-06-22 12:22:18 +0000790 class RecordCalls:
791 times_called = 0
792 record_calls = RecordCalls()
793 def mock_CMDupload(*args, **_kwargs):
794 record_calls.times_called += 1
795 return 0
796 self.mock(git_cl, 'CMDupload', mock_CMDupload)
797
798 self.calls = [
799 (('[Press enter to continue or ctrl-C to quit]',), ''),
800 ]
801
802 class MockChangelist():
803 def __init__(self):
804 pass
805 def GetBranch(self):
806 return 'test1'
807 def GetIssue(self):
808 return '123'
809 def GetPatchset(self):
810 return '1001'
811
812 ret = git_cl.upload_branch_deps(MockChangelist(), [])
813 # CMDupload should have been called 5 times because of 5 dependent branches.
814 self.assertEquals(5, record_calls.times_called)
815 self.assertEquals(0, ret)
816
tandrii@chromium.org65874e12016-03-04 12:03:02 +0000817 def test_gerrit_change_id(self):
818 self.calls = [
819 ((['git', 'write-tree'], ),
820 'hashtree'),
821 ((['git', 'rev-parse', 'HEAD~0'], ),
822 'branch-parent'),
823 ((['git', 'var', 'GIT_AUTHOR_IDENT'], ),
824 'A B <a@b.org> 1456848326 +0100'),
825 ((['git', 'var', 'GIT_COMMITTER_IDENT'], ),
826 'C D <c@d.org> 1456858326 +0100'),
827 ((['git', 'hash-object', '-t', 'commit', '--stdin'], ),
828 'hashchange'),
829 ]
830 change_id = git_cl.GenerateGerritChangeId('line1\nline2\n')
831 self.assertEqual(change_id, 'Ihashchange')
832
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000833 def test_update_reviewers(self):
834 data = [
835 ('foo', [], 'foo'),
agable@chromium.org42c20792013-09-12 17:34:49 +0000836 ('foo\nR=xx', [], 'foo\nR=xx'),
837 ('foo\nTBR=xx', [], 'foo\nTBR=xx'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000838 ('foo', ['a@c'], 'foo\n\nR=a@c'),
agable@chromium.org42c20792013-09-12 17:34:49 +0000839 ('foo\nR=xx', ['a@c'], 'foo\n\nR=a@c, xx'),
840 ('foo\nTBR=xx', ['a@c'], 'foo\n\nR=a@c\nTBR=xx'),
841 ('foo\nTBR=xx\nR=yy', ['a@c'], 'foo\n\nR=a@c, yy\nTBR=xx'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000842 ('foo\nBUG=', ['a@c'], 'foo\nBUG=\nR=a@c'),
agable@chromium.org42c20792013-09-12 17:34:49 +0000843 ('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 +0000844 ('foo', ['a@c', 'b@c'], 'foo\n\nR=a@c, b@c'),
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +0000845 ('foo\nBar\n\nR=\nBUG=', ['c@c'], 'foo\nBar\n\nR=c@c\nBUG='),
846 ('foo\nBar\n\nR=\nBUG=\nR=', ['c@c'], 'foo\nBar\n\nR=c@c\nBUG='),
847 # Same as the line before, but full of whitespaces.
848 (
849 'foo\nBar\n\n R = \n BUG = \n R = ', ['c@c'],
850 'foo\nBar\n\nR=c@c\n BUG =',
851 ),
852 # Whitespaces aren't interpreted as new lines.
853 ('foo BUG=allo R=joe ', ['c@c'], 'foo BUG=allo R=joe\n\nR=c@c'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000854 ]
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +0000855 expected = [i[2] for i in data]
856 actual = []
857 for orig, reviewers, _expected in data:
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000858 obj = git_cl.ChangeDescription(orig)
859 obj.update_reviewers(reviewers)
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +0000860 actual.append(obj.description)
861 self.assertEqual(expected, actual)
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000862
wittman@chromium.org455dc922015-01-26 20:15:50 +0000863 def test_get_target_ref(self):
864 # Check remote or remote branch not present.
865 self.assertEqual(None, git_cl.GetTargetRef('origin', None, 'master', None))
866 self.assertEqual(None, git_cl.GetTargetRef(None,
867 'refs/remotes/origin/master',
868 'master', None))
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000869
wittman@chromium.org455dc922015-01-26 20:15:50 +0000870 # Check default target refs for branches.
871 self.assertEqual('refs/heads/master',
872 git_cl.GetTargetRef('origin', 'refs/remotes/origin/master',
873 None, None))
874 self.assertEqual('refs/heads/master',
875 git_cl.GetTargetRef('origin', 'refs/remotes/origin/lkgr',
876 None, None))
877 self.assertEqual('refs/heads/master',
878 git_cl.GetTargetRef('origin', 'refs/remotes/origin/lkcr',
879 None, None))
880 self.assertEqual('refs/branch-heads/123',
881 git_cl.GetTargetRef('origin',
882 'refs/remotes/branch-heads/123',
883 None, None))
884 self.assertEqual('refs/diff/test',
885 git_cl.GetTargetRef('origin',
886 'refs/remotes/origin/refs/diff/test',
887 None, None))
rmistry@google.comc68112d2015-03-03 12:48:06 +0000888 self.assertEqual('refs/heads/chrome/m42',
889 git_cl.GetTargetRef('origin',
890 'refs/remotes/origin/chrome/m42',
891 None, None))
wittman@chromium.org455dc922015-01-26 20:15:50 +0000892
893 # Check target refs for user-specified target branch.
894 for branch in ('branch-heads/123', 'remotes/branch-heads/123',
895 'refs/remotes/branch-heads/123'):
896 self.assertEqual('refs/branch-heads/123',
897 git_cl.GetTargetRef('origin',
898 'refs/remotes/origin/master',
899 branch, None))
900 for branch in ('origin/master', 'remotes/origin/master',
901 'refs/remotes/origin/master'):
902 self.assertEqual('refs/heads/master',
903 git_cl.GetTargetRef('origin',
904 'refs/remotes/branch-heads/123',
905 branch, None))
906 for branch in ('master', 'heads/master', 'refs/heads/master'):
907 self.assertEqual('refs/heads/master',
908 git_cl.GetTargetRef('origin',
909 'refs/remotes/branch-heads/123',
910 branch, None))
911
912 # Check target refs for pending prefix.
913 self.assertEqual('prefix/heads/master',
914 git_cl.GetTargetRef('origin', 'refs/remotes/origin/master',
915 None, 'prefix/'))
916
wychen@chromium.orga872e752015-04-28 23:42:18 +0000917 def test_patch_when_dirty(self):
918 # Patch when local tree is dirty
919 self.mock(git_common, 'is_dirty_git_tree', lambda x: True)
920 self.assertNotEqual(git_cl.main(['patch', '123456']), 0)
921
922 def test_diff_when_dirty(self):
923 # Do 'git cl diff' when local tree is dirty
924 self.mock(git_common, 'is_dirty_git_tree', lambda x: True)
925 self.assertNotEqual(git_cl.main(['diff']), 0)
926
927 def _patch_common(self):
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +0000928 self.mock(git_cl._RietveldChangelistImpl, 'GetMostRecentPatchset',
929 lambda x: '60001')
930 self.mock(git_cl._RietveldChangelistImpl, 'GetPatchSetDiff',
931 lambda *args: None)
932 self.mock(git_cl.Changelist, 'GetDescription',
933 lambda *args: 'Description')
wychen@chromium.orga872e752015-04-28 23:42:18 +0000934 self.mock(git_cl.Changelist, 'SetIssue', lambda *args: None)
935 self.mock(git_cl.Changelist, 'SetPatchset', lambda *args: None)
936 self.mock(git_cl, 'IsGitVersionAtLeast', lambda *args: True)
937
938 self.calls = [
939 ((['git', 'config', 'rietveld.autoupdate'],), ''),
940 ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'),
941 ((['git', 'rev-parse', '--show-cdup'],), ''),
942 ((['sed', '-e', 's|^--- a/|--- |; s|^+++ b/|+++ |'],), ''),
943 ]
944
945 def test_patch_successful(self):
946 self._patch_common()
947 self.calls += [
948 ((['git', 'apply', '--index', '-p0', '--3way'],), ''),
949 ((['git', 'commit', '-m',
wychen@chromium.org5b3bebb2015-05-28 21:41:43 +0000950 'Description\n\n' +
wychen@chromium.orga872e752015-04-28 23:42:18 +0000951 'patch from issue 123456 at patchset 60001 ' +
952 '(http://crrev.com/123456#ps60001)'],), ''),
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +0000953 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
954 ((['git', 'config', 'branch.master.rietveldserver'],), ''),
wychen@chromium.orga872e752015-04-28 23:42:18 +0000955 ]
956 self.assertEqual(git_cl.main(['patch', '123456']), 0)
957
958 def test_patch_conflict(self):
959 self._patch_common()
960 self.calls += [
961 ((['git', 'apply', '--index', '-p0', '--3way'],), '',
962 subprocess2.CalledProcessError(1, '', '', '', '')),
963 ]
964 self.assertNotEqual(git_cl.main(['patch', '123456']), 0)
wittman@chromium.org455dc922015-01-26 20:15:50 +0000965
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000966if __name__ == '__main__':
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000967 git_cl.logging.basicConfig(
968 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000969 unittest.main()