blob: 295d1a0fb62f7f073a6099d8e4c9a8818e884759 [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
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +000013import urlparse
maruel@chromium.orgddd59412011-11-30 14:20:38 +000014
15sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
16
17from testing_support.auto_stub import TestCase
18
19import git_cl
iannucci@chromium.org9e849272014-04-04 00:31:55 +000020import git_common
tandrii@chromium.org57d86542016-03-04 16:11:32 +000021import git_footers
maruel@chromium.orgddd59412011-11-30 14:20:38 +000022import subprocess2
maruel@chromium.orgddd59412011-11-30 14:20:38 +000023
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000024class PresubmitMock(object):
25 def __init__(self, *args, **kwargs):
26 self.reviewers = []
27 @staticmethod
28 def should_continue():
29 return True
30
31
32class RietveldMock(object):
33 def __init__(self, *args, **kwargs):
34 pass
maruel@chromium.org78936cb2013-04-11 00:17:52 +000035
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000036 @staticmethod
37 def get_description(issue):
38 return 'Issue: %d' % issue
39
maruel@chromium.org78936cb2013-04-11 00:17:52 +000040 @staticmethod
41 def get_issue_properties(_issue, _messages):
42 return {
43 'reviewers': ['joe@chromium.org', 'john@chromium.org'],
44 'messages': [
45 {
46 'approval': True,
47 'sender': 'john@chromium.org',
48 },
49 ],
50 }
51
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000052
53class WatchlistsMock(object):
54 def __init__(self, _):
55 pass
56 @staticmethod
57 def GetWatchersForPaths(_):
58 return ['joe@example.com']
59
60
ukai@chromium.org78c4b982012-02-14 02:20:26 +000061class CodereviewSettingsFileMock(object):
62 def __init__(self):
63 pass
64 # pylint: disable=R0201
65 def read(self):
66 return ("CODE_REVIEW_SERVER: gerrit.chromium.org\n" +
andybons@chromium.org11f46eb2016-02-02 19:26:51 +000067 "GERRIT_HOST: True\n")
ukai@chromium.org78c4b982012-02-14 02:20:26 +000068
69
vadimsh@chromium.orgeed4df32015-04-10 21:30:20 +000070class AuthenticatorMock(object):
71 def __init__(self, *_args):
72 pass
73 def has_cached_credentials(self):
74 return True
75
76
tandrii@chromium.orgfe30f182016-04-13 12:15:04 +000077def CookiesAuthenticatorMockFactory(hosts_with_creds=None, same_cookie=False):
78 """Use to mock Gerrit/Git credentials from ~/.netrc or ~/.gitcookies.
79
80 Usage:
81 >>> self.mock(git_cl.gerrit_util, "CookiesAuthenticator",
82 CookiesAuthenticatorMockFactory({'host1': 'cookie1'}))
83
84 OR
85 >>> self.mock(git_cl.gerrit_util, "CookiesAuthenticator",
86 CookiesAuthenticatorMockFactory(cookie='cookie'))
87 """
88 class CookiesAuthenticatorMock(git_cl.gerrit_util.CookiesAuthenticator):
89 def __init__(self): # pylint: disable=W0231
90 # Intentionally not calling super() because it reads actual cookie files.
91 pass
92 @classmethod
93 def get_gitcookies_path(cls):
94 return '~/.gitcookies'
95 @classmethod
96 def get_netrc_path(cls):
97 return '~/.netrc'
98 def get_auth_header(self, host):
99 if same_cookie:
100 return same_cookie
101 return (hosts_with_creds or {}).get(host)
102 return CookiesAuthenticatorMock
103
104
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +0000105class TestGitClBasic(unittest.TestCase):
106 def _test_ParseIssueUrl(self, func, url, issue, patchset, hostname, fail):
107 parsed = urlparse.urlparse(url)
108 result = func(parsed)
109 if fail:
110 self.assertIsNone(result)
111 return None
112 self.assertIsNotNone(result)
113 self.assertEqual(result.issue, issue)
114 self.assertEqual(result.patchset, patchset)
115 self.assertEqual(result.hostname, hostname)
116 return result
117
118 def test_ParseIssueURL_rietveld(self):
119 def test(url, issue=None, patchset=None, hostname=None, patch_url=None,
120 fail=None):
121 result = self._test_ParseIssueUrl(
122 git_cl._RietveldChangelistImpl.ParseIssueURL,
123 url, issue, patchset, hostname, fail)
124 if not fail:
125 self.assertEqual(result.patch_url, patch_url)
126
127 test('http://codereview.chromium.org/123',
128 123, None, 'codereview.chromium.org')
129 test('https://codereview.chromium.org/123',
130 123, None, 'codereview.chromium.org')
131 test('https://codereview.chromium.org/123/',
132 123, None, 'codereview.chromium.org')
133 test('https://codereview.chromium.org/123/whatever',
134 123, None, 'codereview.chromium.org')
135 test('http://codereview.chromium.org/download/issue123_4.diff',
136 123, 4, 'codereview.chromium.org',
137 patch_url='https://codereview.chromium.org/download/issue123_4.diff')
138 # This looks like bad Gerrit, but is actually valid Rietveld.
139 test('https://chrome-review.source.com/123/4/',
140 123, None, 'chrome-review.source.com')
141
142 test('https://codereview.chromium.org/deadbeaf', fail=True)
143 test('https://codereview.chromium.org/api/123', fail=True)
144 test('bad://codereview.chromium.org/123', fail=True)
145 test('http://codereview.chromium.org/download/issue123_4.diffff', fail=True)
146
147 def test_ParseIssueURL_gerrit(self):
148 def test(url, issue=None, patchset=None, hostname=None, fail=None):
149 self._test_ParseIssueUrl(
150 git_cl._GerritChangelistImpl.ParseIssueURL,
151 url, issue, patchset, hostname, fail)
152
153 test('http://chrome-review.source.com/c/123',
154 123, None, 'chrome-review.source.com')
155 test('https://chrome-review.source.com/c/123/',
156 123, None, 'chrome-review.source.com')
157 test('https://chrome-review.source.com/c/123/4',
158 123, 4, 'chrome-review.source.com')
159 test('https://chrome-review.source.com/#/c/123/4',
160 123, 4, 'chrome-review.source.com')
161 test('https://chrome-review.source.com/c/123/4',
162 123, 4, 'chrome-review.source.com')
163 test('https://chrome-review.source.com/123',
164 123, None, 'chrome-review.source.com')
165 test('https://chrome-review.source.com/123/4',
166 123, 4, 'chrome-review.source.com')
167
168 test('https://chrome-review.source.com/c/123/1/whatisthis', fail=True)
169 test('https://chrome-review.source.com/c/abc/', fail=True)
170 test('ssh://chrome-review.source.com/c/123/1/', fail=True)
171
172 def test_ParseIssueNumberArgument(self):
173 def test(arg, issue=None, patchset=None, hostname=None, fail=False):
174 result = git_cl.ParseIssueNumberArgument(arg)
175 self.assertIsNotNone(result)
176 if fail:
177 self.assertFalse(result.valid)
178 else:
179 self.assertEqual(result.issue, issue)
180 self.assertEqual(result.patchset, patchset)
181 self.assertEqual(result.hostname, hostname)
182
183 test('123', 123)
184 test('', fail=True)
185 test('abc', fail=True)
186 test('123/1', fail=True)
187 test('123a', fail=True)
188 test('ssh://chrome-review.source.com/#/c/123/4/', fail=True)
189 # Rietveld.
190 test('https://codereview.source.com/123',
191 123, None, 'codereview.source.com')
192 test('https://codereview.source.com/www123', fail=True)
193 # Gerrrit.
194 test('https://chrome-review.source.com/c/123/4',
195 123, 4, 'chrome-review.source.com')
196 test('https://chrome-review.source.com/bad/123/4', fail=True)
197
198
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000199class TestGitCl(TestCase):
200 def setUp(self):
201 super(TestGitCl, self).setUp()
202 self.calls = []
203 self._calls_done = 0
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000204 self.mock(subprocess2, 'call', self._mocked_call)
205 self.mock(subprocess2, 'check_call', self._mocked_call)
206 self.mock(subprocess2, 'check_output', self._mocked_call)
207 self.mock(subprocess2, 'communicate', self._mocked_call)
tandrii@chromium.orga342c922016-03-16 07:08:25 +0000208 self.mock(git_cl.gclient_utils, 'CheckCallAndFilter', self._mocked_call)
sbc@chromium.org71437c02015-04-09 19:29:40 +0000209 self.mock(git_common, 'is_dirty_git_tree', lambda x: False)
iannucci@chromium.org9e849272014-04-04 00:31:55 +0000210 self.mock(git_common, 'get_or_create_merge_base',
211 lambda *a: (
212 self._mocked_call(['get_or_create_merge_base']+list(a))))
pgervais@chromium.org8ba38ff2015-06-11 21:41:25 +0000213 self.mock(git_cl, 'BranchExists', lambda _: True)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000214 self.mock(git_cl, 'FindCodereviewSettingsFile', lambda: '')
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000215 self.mock(git_cl, 'ask_for_data', self._mocked_call)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000216 self.mock(git_cl.presubmit_support, 'DoPresubmitChecks', PresubmitMock)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000217 self.mock(git_cl.rietveld, 'Rietveld', RietveldMock)
maruel@chromium.org4bac4b52012-11-27 20:33:52 +0000218 self.mock(git_cl.rietveld, 'CachingRietveld', RietveldMock)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000219 self.mock(git_cl.upload, 'RealMain', self.fail)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000220 self.mock(git_cl.watchlists, 'Watchlists', WatchlistsMock)
vadimsh@chromium.orgeed4df32015-04-10 21:30:20 +0000221 self.mock(git_cl.auth, 'get_authenticator_for_host', AuthenticatorMock)
tandrii@chromium.orgfe30f182016-04-13 12:15:04 +0000222 self.mock(git_cl.gerrit_util.GceAuthenticator, 'is_gce',
223 classmethod(lambda _: False))
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000224 # It's important to reset settings to not have inter-tests interference.
225 git_cl.settings = None
226
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +0000227
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000228 def tearDown(self):
wychen@chromium.org445c8962015-04-28 23:30:05 +0000229 try:
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +0000230 # Note: has_failed returns True if at least 1 test ran so far, current
231 # included, has failed. That means current test may have actually ran
232 # fine, and the check for no leftover calls would be skipped.
wychen@chromium.org445c8962015-04-28 23:30:05 +0000233 if not self.has_failed():
234 self.assertEquals([], self.calls)
235 finally:
236 super(TestGitCl, self).tearDown()
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000237
iannucci@chromium.org9e849272014-04-04 00:31:55 +0000238 def _mocked_call(self, *args, **_kwargs):
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000239 self.assertTrue(
240 self.calls,
241 '@%d Expected: <Missing> Actual: %r' % (self._calls_done, args))
wychen@chromium.orga872e752015-04-28 23:42:18 +0000242 top = self.calls.pop(0)
243 if len(top) > 2 and top[2]:
244 raise top[2]
245 expected_args, result = top
246
maruel@chromium.orge52678e2013-04-26 18:34:44 +0000247 # Also logs otherwise it could get caught in a try/finally and be hard to
248 # diagnose.
249 if expected_args != args:
250 msg = '@%d Expected: %r Actual: %r' % (
251 self._calls_done, expected_args, args)
252 git_cl.logging.error(msg)
253 self.fail(msg)
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000254 self._calls_done += 1
255 return result
256
maruel@chromium.orga3353652011-11-30 14:26:57 +0000257 @classmethod
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000258 def _is_gerrit_calls(cls, gerrit=False):
259 return [((['git', 'config', 'rietveld.autoupdate'],), ''),
260 ((['git', 'config', 'gerrit.host'],), 'True' if gerrit else '')]
261
262 @classmethod
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000263 def _upload_calls(cls, similarity, find_copies, private):
iannucci@chromium.org79540052012-10-19 23:15:26 +0000264 return (cls._git_base_calls(similarity, find_copies) +
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000265 cls._git_upload_calls(private))
maruel@chromium.orga3353652011-11-30 14:26:57 +0000266
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000267 @classmethod
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000268 def _upload_no_rev_calls(cls, similarity, find_copies):
269 return (cls._git_base_calls(similarity, find_copies) +
270 cls._git_upload_no_rev_calls())
271
272 @classmethod
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000273 def _git_base_calls(cls, similarity, find_copies):
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000274 if similarity is None:
275 similarity = '50'
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000276 similarity_call = ((['git', 'config', '--int', '--get',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000277 'branch.master.git-cl-similarity'],), '')
278 else:
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000279 similarity_call = ((['git', 'config', '--int',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000280 'branch.master.git-cl-similarity', similarity],), '')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000281
282 if find_copies is None:
283 find_copies = True
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000284 find_copies_call = ((['git', 'config', '--int', '--get',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000285 'branch.master.git-find-copies'],), '')
286 else:
287 val = str(int(find_copies))
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000288 find_copies_call = ((['git', 'config', '--int',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000289 'branch.master.git-find-copies', val],), '')
290
291 if find_copies:
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000292 stat_call = ((['git', 'diff', '--no-ext-diff', '--stat',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000293 '--find-copies-harder', '-l100000', '-C'+similarity,
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000294 'fake_ancestor_sha', 'HEAD'],), '+dat')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000295 else:
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000296 stat_call = ((['git', 'diff', '--no-ext-diff', '--stat',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000297 '-M'+similarity, 'fake_ancestor_sha', 'HEAD'],), '+dat')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000298
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000299 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000300 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
tandrii@chromium.org87985d22016-03-24 17:33:33 +0000301 similarity_call,
302 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
303 find_copies_call,
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000304 ] + cls._is_gerrit_calls() + [
tandrii@chromium.org87985d22016-03-24 17:33:33 +0000305 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +0000306 ((['git', 'config', 'branch.master.rietveldissue'],), ''),
307 ((['git', 'config', 'branch.master.gerritissue'],), ''),
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +0000308 ((['git', 'config', 'rietveld.server'],),
309 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000310 ((['git', 'config', 'branch.master.merge'],), 'master'),
311 ((['git', 'config', 'branch.master.remote'],), 'origin'),
iannucci@chromium.org9e849272014-04-04 00:31:55 +0000312 ((['get_or_create_merge_base', 'master', 'master'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000313 'fake_ancestor_sha'),
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000314 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000315 ((['git', 'rev-parse', '--show-cdup'],), ''),
316 ((['git', 'rev-parse', 'HEAD'],), '12345'),
317 ((['git', 'diff', '--name-status', '--no-renames', '-r',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000318 'fake_ancestor_sha...', '.'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000319 'M\t.gitignore\n'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000320 ((['git', 'config', 'branch.master.rietveldpatchset'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000321 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000322 ((['git', 'log', '--pretty=format:%s%n%n%b',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000323 'fake_ancestor_sha...'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000324 'foo'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000325 ((['git', 'config', 'user.email'],), 'me@example.com'),
iannucci@chromium.org79540052012-10-19 23:15:26 +0000326 stat_call,
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000327 ((['git', 'log', '--pretty=format:%s\n\n%b',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000328 'fake_ancestor_sha..HEAD'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000329 'desc\n'),
rmistry@google.com90752582014-01-14 21:04:50 +0000330 ((['git', 'config', 'rietveld.bug-prefix'],), ''),
maruel@chromium.orga3353652011-11-30 14:26:57 +0000331 ]
332
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000333 @classmethod
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000334 def _git_upload_no_rev_calls(cls):
335 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000336 ((['git', 'config', 'core.editor'],), ''),
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000337 ]
338
339 @classmethod
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000340 def _git_upload_calls(cls, private):
341 if private:
tyoshino@chromium.org99918ab2013-09-30 06:17:28 +0000342 cc_call = []
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000343 private_call = []
344 else:
tyoshino@chromium.org99918ab2013-09-30 06:17:28 +0000345 cc_call = [((['git', 'config', 'rietveld.cc'],), '')]
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000346 private_call = [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000347 ((['git', 'config', 'rietveld.private'],), '')]
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000348
maruel@chromium.orga3353652011-11-30 14:26:57 +0000349 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000350 ((['git', 'config', 'core.editor'],), ''),
tyoshino@chromium.org99918ab2013-09-30 06:17:28 +0000351 ] + cc_call + private_call + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000352 ((['git', 'config', 'branch.master.base-url'],), ''),
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +0000353 ((['git', 'config', 'rietveld.pending-ref-prefix'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000354 ((['git',
bratell@opera.com05fb9112014-07-07 09:30:23 +0000355 'config', '--local', '--get-regexp', '^svn-remote\\.'],),
356 (('', None), 0)),
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000357 ((['git', 'rev-parse', '--show-cdup'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000358 ((['git', 'svn', 'info'],), ''),
sheyang@chromium.org152cf832014-06-11 21:37:49 +0000359 ((['git', 'config', 'rietveld.project'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000360 ((['git',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000361 'config', 'branch.master.rietveldissue', '1'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000362 ((['git', 'config', 'branch.master.rietveldserver',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000363 'https://codereview.example.com'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000364 ((['git',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000365 'config', 'branch.master.rietveldpatchset', '2'],), ''),
tandrii@chromium.org1e67bb72016-02-11 12:15:49 +0000366 ] + cls._git_post_upload_calls()
367
368 @classmethod
369 def _git_post_upload_calls(cls):
370 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000371 ((['git', 'rev-parse', 'HEAD'],), 'hash'),
372 ((['git', 'symbolic-ref', 'HEAD'],), 'hash'),
373 ((['git',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000374 'config', 'branch.hash.last-upload-hash', 'hash'],), ''),
rmistry@google.com5626a922015-02-26 14:03:30 +0000375 ((['git', 'config', 'rietveld.run-post-upload-hook'],), ''),
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000376 ]
377
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000378 @staticmethod
tandrii@chromium.orgfe30f182016-04-13 12:15:04 +0000379 def _git_sanity_checks(diff_base, working_branch, get_remote_branch=True):
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000380 fake_ancestor = 'fake_ancestor'
381 fake_cl = 'fake_cl_for_patch'
382 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000383 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000384 'rev-parse', '--verify', diff_base],), fake_ancestor),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000385 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000386 'merge-base', fake_ancestor, 'HEAD'],), fake_ancestor),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000387 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000388 'rev-list', '^' + fake_ancestor, 'HEAD'],), fake_cl),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000389 # Mock a config miss (error code 1)
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000390 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000391 'config', 'gitcl.remotebranch'],), (('', None), 1)),
tandrii@chromium.orgfe30f182016-04-13 12:15:04 +0000392 ] + ([
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000393 # Call to GetRemoteBranch()
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000394 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000395 'config', 'branch.%s.merge' % working_branch],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000396 'refs/heads/master'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000397 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000398 'config', 'branch.%s.remote' % working_branch],), 'origin'),
tandrii@chromium.orgfe30f182016-04-13 12:15:04 +0000399 ] if get_remote_branch else []) + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000400 ((['git', 'rev-list', '^' + fake_ancestor,
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000401 'refs/remotes/origin/master'],), ''),
tandrii@chromium.orgfe30f182016-04-13 12:15:04 +0000402 ]
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000403
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000404 @classmethod
405 def _dcommit_calls_1(cls):
406 return [
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +0000407 ((['git', 'config', 'rietveld.autoupdate'],),
408 ''),
409 ((['git', 'config', 'rietveld.pending-ref-prefix'],),
410 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000411 ((['git',
bratell@opera.com05fb9112014-07-07 09:30:23 +0000412 'config', '--local', '--get-regexp', '^svn-remote\\.'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000413 ((('svn-remote.svn.url svn://svn.chromium.org/chrome\n'
414 'svn-remote.svn.fetch trunk/src:refs/remotes/origin/master'),
415 None),
416 0)),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000417 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
418 ((['git', 'config', '--int', '--get',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000419 'branch.working.git-cl-similarity'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000420 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
421 ((['git', 'config', '--int', '--get',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000422 'branch.working.git-find-copies'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000423 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
424 ((['git',
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +0000425 'config', 'branch.working.rietveldissue'],), '12345'),
426 ((['git',
427 'config', 'rietveld.server'],), 'codereview.example.com'),
428 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000429 'config', 'branch.working.merge'],), 'refs/heads/master'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000430 ((['git', 'config', 'branch.working.remote'],), 'origin'),
iannucci@chromium.org5724c962014-04-11 09:32:56 +0000431 ((['git', 'config', 'branch.working.merge'],),
432 'refs/heads/master'),
433 ((['git', 'config', 'branch.working.remote'],), 'origin'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000434 ((['git', 'rev-list', '--merges',
szager@chromium.orge84b7542012-06-15 21:26:58 +0000435 '--grep=^SVN changes up to revision [0-9]*$',
szager@chromium.org9bb85e22012-06-13 20:28:23 +0000436 'refs/remotes/origin/master^!'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000437 ((['git', 'rev-list', '^refs/heads/working',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000438 'refs/remotes/origin/master'],),
439 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000440 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000441 'log', '--grep=^git-svn-id:', '-1', '--pretty=format:%H'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000442 '3fc18b62c4966193eb435baabe2d18a3810ec82e'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000443 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000444 'rev-list', '^3fc18b62c4966193eb435baabe2d18a3810ec82e',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000445 'refs/remotes/origin/master'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000446 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000447 'merge-base', 'refs/remotes/origin/master', 'HEAD'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000448 'fake_ancestor_sha'),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000449 ]
450
451 @classmethod
452 def _dcommit_calls_normal(cls):
453 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000454 ((['git', 'rev-parse', '--show-cdup'],), ''),
455 ((['git', 'rev-parse', 'HEAD'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000456 '00ff397798ea57439712ed7e04ab96e13969ef40'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000457 ((['git',
mcgrathr@chromium.org9249f642013-06-03 21:36:18 +0000458 'diff', '--name-status', '--no-renames', '-r', 'fake_ancestor_sha...',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000459 '.'],),
460 'M\tPRESUBMIT.py'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000461 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000462 'config', 'branch.working.rietveldpatchset'],), '31137'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000463 ((['git', 'config', 'branch.working.rietveldserver'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000464 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000465 ((['git', 'config', 'user.email'],), 'author@example.com'),
466 ((['git', 'config', 'rietveld.tree-status-url'],), ''),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000467 ]
468
469 @classmethod
470 def _dcommit_calls_bypassed(cls):
471 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000472 ((['git', 'config', 'branch.working.rietveldserver'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000473 'codereview.example.com'),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000474 ]
475
476 @classmethod
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000477 def _dcommit_calls_3(cls):
478 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000479 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000480 'diff', '--no-ext-diff', '--stat', '--find-copies-harder',
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000481 '-l100000', '-C50', 'fake_ancestor_sha',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000482 'refs/heads/working'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000483 (' PRESUBMIT.py | 2 +-\n'
484 ' 1 files changed, 1 insertions(+), 1 deletions(-)\n')),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000485 ((['git', 'show-ref', '--quiet', '--verify',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000486 'refs/heads/git-cl-commit'],),
487 (('', None), 0)),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000488 ((['git', 'branch', '-D', 'git-cl-commit'],), ''),
489 ((['git', 'show-ref', '--quiet', '--verify',
szager@chromium.org9bb85e22012-06-13 20:28:23 +0000490 'refs/heads/git-cl-cherry-pick'],), ''),
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000491 ((['git', 'rev-parse', '--show-cdup'],), '\n'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000492 ((['git', 'checkout', '-q', '-b', 'git-cl-commit'],), ''),
493 ((['git', 'reset', '--soft', 'fake_ancestor_sha'],), ''),
494 ((['git', 'commit', '-m',
maruel@chromium.orge52678e2013-04-26 18:34:44 +0000495 'Issue: 12345\n\nR=john@chromium.org\n\n'
sergiyb@chromium.org4b39c5f2015-07-07 10:33:12 +0000496 'Review URL: https://codereview.example.com/12345 .'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000497 ''),
kjellander@chromium.org6abc6522014-12-02 07:34:49 +0000498 ((['git', 'config', 'rietveld.force-https-commit-url'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000499 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000500 'svn', 'dcommit', '-C50', '--no-rebase', '--rmdir'],),
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000501 (('', None), 0)),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000502 ((['git', 'checkout', '-q', 'working'],), ''),
503 ((['git', 'branch', '-D', 'git-cl-commit'],), ''),
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000504 ]
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000505
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000506 @staticmethod
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000507 def _cmd_line(description, args, similarity, find_copies, private):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000508 """Returns the upload command line passed to upload.RealMain()."""
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000509 return [
510 'upload', '--assume_yes', '--server',
maruel@chromium.orgeb5edbc2012-01-16 17:03:28 +0000511 'https://codereview.example.com',
maruel@chromium.org71e12a92012-02-14 02:34:15 +0000512 '--message', description
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000513 ] + args + [
514 '--cc', 'joe@example.com',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000515 ] + (['--private'] if private else []) + [
iannucci@chromium.org79540052012-10-19 23:15:26 +0000516 '--git_similarity', similarity or '50'
517 ] + (['--git_no_find_copies'] if find_copies == False else []) + [
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000518 'fake_ancestor_sha', 'HEAD'
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000519 ]
520
521 def _run_reviewer_test(
522 self,
523 upload_args,
524 expected_description,
525 returned_description,
526 final_description,
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000527 reviewers,
528 private=False):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000529 """Generic reviewer test framework."""
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000530 try:
531 similarity = upload_args[upload_args.index('--similarity')+1]
532 except ValueError:
533 similarity = None
iannucci@chromium.org79540052012-10-19 23:15:26 +0000534
535 if '--find-copies' in upload_args:
536 find_copies = True
537 elif '--no-find-copies' in upload_args:
538 find_copies = False
539 else:
540 find_copies = None
541
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000542 private = '--private' in upload_args
543
544 self.calls = self._upload_calls(similarity, find_copies, private)
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000545
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000546 def RunEditor(desc, _, **kwargs):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000547 self.assertEquals(
548 '# Enter a description of the change.\n'
janx@chromium.org104b2db2013-04-18 12:58:40 +0000549 '# This will be displayed on the codereview site.\n'
alancutter@chromium.org63a4d7f2013-05-31 02:22:45 +0000550 '# The first line will also be used as the subject of the review.\n'
alancutter@chromium.orgbd1073e2013-06-01 00:34:38 +0000551 '#--------------------This line is 72 characters long'
552 '--------------------\n' +
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000553 expected_description,
554 desc)
555 return returned_description
556 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor)
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000557
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000558 def check_upload(args):
iannucci@chromium.org79540052012-10-19 23:15:26 +0000559 cmd_line = self._cmd_line(final_description, reviewers, similarity,
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000560 find_copies, private)
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000561 self.assertEquals(cmd_line, args)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000562 return 1, 2
563 self.mock(git_cl.upload, 'RealMain', check_upload)
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000564
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000565 git_cl.main(['upload'] + upload_args)
566
567 def test_no_reviewer(self):
568 self._run_reviewer_test(
569 [],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000570 'desc\n\nBUG=',
571 '# Blah blah comment.\ndesc\n\nBUG=',
572 'desc\n\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000573 [])
574
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000575 def test_keep_similarity(self):
576 self._run_reviewer_test(
577 ['--similarity', '70'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000578 'desc\n\nBUG=',
579 '# Blah blah comment.\ndesc\n\nBUG=',
580 'desc\n\nBUG=',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000581 [])
582
iannucci@chromium.org79540052012-10-19 23:15:26 +0000583 def test_keep_find_copies(self):
584 self._run_reviewer_test(
585 ['--no-find-copies'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000586 'desc\n\nBUG=',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000587 '# Blah blah comment.\ndesc\n\nBUG=\n',
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000588 'desc\n\nBUG=',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000589 [])
590
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000591 def test_private(self):
592 self._run_reviewer_test(
593 ['--private'],
594 'desc\n\nBUG=',
595 '# Blah blah comment.\ndesc\n\nBUG=\n',
596 'desc\n\nBUG=',
597 [])
598
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000599 def test_reviewers_cmd_line(self):
600 # Reviewer is passed as-is
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000601 description = 'desc\n\nR=foo@example.com\nBUG='
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000602 self._run_reviewer_test(
603 ['-r' 'foo@example.com'],
604 description,
605 '\n%s\n' % description,
606 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000607 ['--reviewers=foo@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000608
609 def test_reviewer_tbr_overriden(self):
610 # Reviewer is overriden with TBR
611 # Also verifies the regexp work without a trailing LF
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000612 description = 'Foo Bar\n\nTBR=reviewer@example.com'
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000613 self._run_reviewer_test(
614 ['-r' 'foo@example.com'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000615 'desc\n\nR=foo@example.com\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000616 description.strip('\n'),
617 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000618 ['--reviewers=reviewer@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000619
620 def test_reviewer_multiple(self):
621 # Handles multiple R= or TBR= lines.
622 description = (
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000623 'Foo Bar\nTBR=reviewer@example.com\nBUG=\nR=another@example.com')
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000624 self._run_reviewer_test(
625 [],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000626 'desc\n\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000627 description,
628 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000629 ['--reviewers=another@example.com,reviewer@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000630
maruel@chromium.orga3353652011-11-30 14:26:57 +0000631 def test_reviewer_send_mail(self):
632 # --send-mail can be used without -r if R= is used
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000633 description = 'Foo Bar\nR=reviewer@example.com'
maruel@chromium.orga3353652011-11-30 14:26:57 +0000634 self._run_reviewer_test(
635 ['--send-mail'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000636 'desc\n\nBUG=',
maruel@chromium.orga3353652011-11-30 14:26:57 +0000637 description.strip('\n'),
638 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000639 ['--reviewers=reviewer@example.com', '--send_mail'])
maruel@chromium.orga3353652011-11-30 14:26:57 +0000640
641 def test_reviewer_send_mail_no_rev(self):
642 # Fails without a reviewer.
maruel@chromium.org2e23ce32013-05-07 12:42:28 +0000643 stdout = StringIO.StringIO()
644 stderr = StringIO.StringIO()
maruel@chromium.orga3353652011-11-30 14:26:57 +0000645 try:
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000646 self.calls = self._upload_no_rev_calls(None, None)
647 def RunEditor(desc, _, **kwargs):
maruel@chromium.orga3353652011-11-30 14:26:57 +0000648 return desc
649 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor)
maruel@chromium.org2e23ce32013-05-07 12:42:28 +0000650 self.mock(sys, 'stdout', stdout)
651 self.mock(sys, 'stderr', stderr)
maruel@chromium.orga3353652011-11-30 14:26:57 +0000652 git_cl.main(['upload', '--send-mail'])
653 self.fail()
654 except SystemExit:
maruel@chromium.org2e23ce32013-05-07 12:42:28 +0000655 self.assertEqual(
656 'Using 50% similarity for rename/copy detection. Override with '
657 '--similarity.\n',
658 stdout.getvalue())
659 self.assertEqual(
660 'Must specify reviewers to send email.\n', stderr.getvalue())
maruel@chromium.orga3353652011-11-30 14:26:57 +0000661
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000662 def test_dcommit(self):
663 self.calls = (
664 self._dcommit_calls_1() +
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000665 self._git_sanity_checks('fake_ancestor_sha', 'working') +
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000666 self._dcommit_calls_normal() +
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000667 self._dcommit_calls_3())
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000668 git_cl.main(['dcommit'])
669
670 def test_dcommit_bypass_hooks(self):
671 self.calls = (
672 self._dcommit_calls_1() +
673 self._dcommit_calls_bypassed() +
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000674 self._dcommit_calls_3())
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000675 git_cl.main(['dcommit', '--bypass-hooks'])
676
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000677
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000678 @classmethod
tandrii@chromium.orgfe30f182016-04-13 12:15:04 +0000679 def _gerrit_ensure_auth_calls(cls, issue=None):
680 calls = []
681 if issue:
682 calls.extend([
683 ((['git', 'config', 'branch.master.gerritserver'],), ''),
684 ])
685 calls.extend([
686 ((['git', 'config', 'branch.master.merge'],), 'refs/heads/master'),
687 ((['git', 'config', 'branch.master.remote'],), 'origin'),
688 ((['git', 'config', 'remote.origin.url'],),
689 'https://chromium.googlesource.com/my/repo'),
690 ((['git', 'config', 'remote.origin.url'],),
691 'https://chromium.googlesource.com/my/repo'),
692 ])
693 return calls
694
695 @classmethod
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000696 def _gerrit_base_calls(cls, issue=None):
ukai@chromium.orge8077812012-02-03 03:41:46 +0000697 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000698 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
699 ((['git', 'config', '--int', '--get',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000700 'branch.master.git-cl-similarity'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000701 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
702 ((['git', 'config', '--int', '--get',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000703 'branch.master.git-find-copies'],), ''),
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000704 ] + cls._is_gerrit_calls(True) + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000705 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +0000706 ((['git', 'config', 'branch.master.rietveldissue'],), ''),
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000707 ((['git', 'config', 'branch.master.gerritissue'],),
708 '' if issue is None else str(issue)),
tandrii@chromium.orgfe30f182016-04-13 12:15:04 +0000709 ((['git', 'config', 'branch.master.merge'],), 'refs/heads/master'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000710 ((['git', 'config', 'branch.master.remote'],), 'origin'),
tandrii@chromium.orgfe30f182016-04-13 12:15:04 +0000711 ((['get_or_create_merge_base', 'master',
712 'refs/remotes/origin/master'],),
iannucci@chromium.org9e849272014-04-04 00:31:55 +0000713 'fake_ancestor_sha'),
tandrii@chromium.orgfe30f182016-04-13 12:15:04 +0000714 # Calls to verify branch point is ancestor
715 ] + (cls._gerrit_ensure_auth_calls(issue=issue) +
716 cls._git_sanity_checks('fake_ancestor_sha', 'master',
717 get_remote_branch=False)) + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000718 ((['git', 'rev-parse', '--show-cdup'],), ''),
719 ((['git', 'rev-parse', 'HEAD'],), '12345'),
tandrii@chromium.orgfe30f182016-04-13 12:15:04 +0000720
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000721 ((['git',
mcgrathr@chromium.org9249f642013-06-03 21:36:18 +0000722 'diff', '--name-status', '--no-renames', '-r',
723 'fake_ancestor_sha...', '.'],),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000724 'M\t.gitignore\n'),
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +0000725 ((['git', 'config', 'branch.master.gerritpatchset'],), ''),
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000726 ] + ([] if issue else [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000727 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000728 'log', '--pretty=format:%s%n%n%b', 'fake_ancestor_sha...'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000729 'foo'),
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000730 ]) + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000731 ((['git', 'config', 'user.email'],), 'me@example.com'),
732 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000733 'diff', '--no-ext-diff', '--stat', '--find-copies-harder',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000734 '-l100000', '-C50', 'fake_ancestor_sha', 'HEAD'],),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000735 '+dat'),
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +0000736 ]
ukai@chromium.orge8077812012-02-03 03:41:46 +0000737
tandrii@chromium.org1e67bb72016-02-11 12:15:49 +0000738 @classmethod
739 def _gerrit_upload_calls(cls, description, reviewers, squash,
tandrii@chromium.org10625002016-03-04 20:03:47 +0000740 expected_upstream_ref='origin/refs/heads/master',
tandrii@chromium.orgbf766ba2016-04-13 12:51:23 +0000741 ref_suffix='',
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000742 post_amend_description=None, issue=None):
tandrii@chromium.org10625002016-03-04 20:03:47 +0000743 if post_amend_description is None:
744 post_amend_description = description
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000745
ukai@chromium.orge8077812012-02-03 03:41:46 +0000746 calls = [
bauerb@chromium.org54b400c2016-01-14 10:08:25 +0000747 ((['git', 'config', '--bool', 'gerrit.squash-uploads'],), 'false'),
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000748 ]
749 # If issue is given, then description is fetched from Gerrit instead.
750 if issue is None:
751 if squash:
752 calls += [
753 ((['git', 'show', '--format=%B', '-s',
754 'refs/heads/git_cl_uploads/master'],), '')]
755 calls += [
756 ((['git', 'log', '--pretty=format:%s\n\n%b',
757 'fake_ancestor_sha..HEAD'],),
758 description)]
tandrii@chromium.org57d86542016-03-04 16:11:32 +0000759 if not git_footers.get_footer_change_id(description) and not squash:
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000760 calls += [
tandrii@chromium.org10625002016-03-04 20:03:47 +0000761 # DownloadGerritHook(False)
762 ((False, ),
763 ''),
764 # Amending of commit message to get the Change-Id.
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000765 ((['git', 'log', '--pretty=format:%s\n\n%b',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000766 'fake_ancestor_sha..HEAD'],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000767 description),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000768 ((['git', 'commit', '--amend', '-m', description],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000769 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000770 ((['git', 'log', '--pretty=format:%s\n\n%b',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000771 'fake_ancestor_sha..HEAD'],),
tandrii@chromium.org10625002016-03-04 20:03:47 +0000772 post_amend_description)
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000773 ]
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000774 if squash:
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000775 if not issue:
776 # Prompting to edit description on first upload.
777 calls += [
778 ((['git', 'config', 'core.editor'],), ''),
779 ((['RunEditor'],), description),
780 ]
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000781 ref_to_push = 'abcdef0123456789'
782 calls += [
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000783 ((['git', 'config', 'branch.master.merge'],),
784 'refs/heads/master'),
785 ((['git', 'config', 'branch.master.remote'],),
786 'origin'),
tandrii@chromium.orgfe30f182016-04-13 12:15:04 +0000787 ((['get_or_create_merge_base', 'master',
788 'refs/remotes/origin/master'],),
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000789 'origin/master'),
790 ((['git', 'rev-parse', 'HEAD:'],),
791 '0123456789abcdef'),
792 ((['git', 'commit-tree', '0123456789abcdef', '-p',
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000793 'origin/master', '-m', description],),
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000794 ref_to_push),
795 ]
796 else:
797 ref_to_push = 'HEAD'
798
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000799 calls += [
luqui@chromium.org609f3952015-05-04 22:47:04 +0000800 ((['git', 'rev-list',
801 expected_upstream_ref + '..' + ref_to_push],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000802 ((['git', 'config', 'rietveld.cc'],), '')
ukai@chromium.orge8077812012-02-03 03:41:46 +0000803 ]
tandrii@chromium.org8acd8332016-04-13 12:56:03 +0000804 # Add cc from watch list.
805 if ref_suffix == '':
806 ref_suffix = '%cc=joe@example.com'
807 else:
808 ref_suffix += ',cc=joe@example.com'
ukai@chromium.orge8077812012-02-03 03:41:46 +0000809 if reviewers:
tandrii@chromium.org8acd8332016-04-13 12:56:03 +0000810 ref_suffix += ',' + ','.join('r=%s' % email
811 for email in sorted(reviewers))
ukai@chromium.orge8077812012-02-03 03:41:46 +0000812 calls += [
tandrii@chromium.org8acd8332016-04-13 12:56:03 +0000813 ((['git', 'push', 'origin',
tandrii@chromium.orgbf766ba2016-04-13 12:51:23 +0000814 ref_to_push + ':refs/for/refs/heads/master' + ref_suffix],),
tandrii@chromium.orga342c922016-03-16 07:08:25 +0000815 ('remote:\n'
816 'remote: Processing changes: (\)\n'
817 'remote: Processing changes: (|)\n'
818 'remote: Processing changes: (/)\n'
819 'remote: Processing changes: (-)\n'
820 'remote: Processing changes: new: 1 (/)\n'
821 'remote: Processing changes: new: 1, done\n'
822 'remote:\n'
823 'remote: New Changes:\n'
824 'remote: https://chromium-review.googlesource.com/123456 XXX.\n'
825 'remote:\n'
826 'To https://chromium.googlesource.com/yyy/zzz\n'
827 ' * [new branch] hhhh -> refs/for/refs/heads/master\n')),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000828 ]
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000829 if squash:
830 calls += [
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +0000831 ((['git', 'config', 'branch.master.gerritissue', '123456'],), ''),
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +0000832 ((['git', 'config', 'branch.master.gerritserver',
833 'https://chromium-review.googlesource.com'],), ''),
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000834 ((['git', 'config', 'branch.master.gerritsquashhash',
835 'abcdef0123456789'],), ''),
tandrii@chromium.orgfe30f182016-04-13 12:15:04 +0000836 ]
tandrii@chromium.org1e67bb72016-02-11 12:15:49 +0000837 calls += cls._git_post_upload_calls()
ukai@chromium.orge8077812012-02-03 03:41:46 +0000838 return calls
839
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000840 def _run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000841 self,
842 upload_args,
843 description,
tandrii@chromium.orgbf766ba2016-04-13 12:51:23 +0000844 reviewers=None,
luqui@chromium.org609f3952015-05-04 22:47:04 +0000845 squash=False,
tandrii@chromium.org10625002016-03-04 20:03:47 +0000846 expected_upstream_ref='origin/refs/heads/master',
tandrii@chromium.orgbf766ba2016-04-13 12:51:23 +0000847 ref_suffix='',
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000848 post_amend_description=None,
849 issue=None):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000850 """Generic gerrit upload test framework."""
tandrii@chromium.orgbf766ba2016-04-13 12:51:23 +0000851 reviewers = reviewers or []
tandrii@chromium.orgfe30f182016-04-13 12:15:04 +0000852 self.mock(git_cl.gerrit_util, "CookiesAuthenticator",
853 CookiesAuthenticatorMockFactory(same_cookie='same_cred'))
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000854 self.calls = self._gerrit_base_calls(issue=issue)
luqui@chromium.org609f3952015-05-04 22:47:04 +0000855 self.calls += self._gerrit_upload_calls(
856 description, reviewers, squash,
tandrii@chromium.org10625002016-03-04 20:03:47 +0000857 expected_upstream_ref=expected_upstream_ref,
tandrii@chromium.orgbf766ba2016-04-13 12:51:23 +0000858 ref_suffix=ref_suffix,
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000859 post_amend_description=post_amend_description,
860 issue=issue)
tandrii@chromium.org09d7a6a2016-03-04 15:44:48 +0000861 # Uncomment when debugging.
862 # print '\n'.join(map(lambda x: '%2i: %s' % x, enumerate(self.calls)))
ukai@chromium.orge8077812012-02-03 03:41:46 +0000863 git_cl.main(['upload'] + upload_args)
864
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000865 def test_gerrit_upload_without_change_id(self):
tandrii@chromium.org10625002016-03-04 20:03:47 +0000866 self.mock(git_cl, 'DownloadGerritHook', self._mocked_call)
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000867 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000868 [],
jamesr@chromium.org35d1a842012-07-27 00:20:43 +0000869 'desc\n\nBUG=\n',
tandrii@chromium.org10625002016-03-04 20:03:47 +0000870 [],
871 post_amend_description='desc\n\nBUG=\n\nChange-Id: Ixxx')
ukai@chromium.orge8077812012-02-03 03:41:46 +0000872
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000873 def test_gerrit_no_reviewer(self):
874 self._run_gerrit_upload_test(
875 [],
tandrii@chromium.org09d7a6a2016-03-04 15:44:48 +0000876 'desc\n\nBUG=\n\nChange-Id: I123456789\n',
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000877 [])
878
tandrii@chromium.orgbf766ba2016-04-13 12:51:23 +0000879 def test_gerrit_patch_title(self):
880 self._run_gerrit_upload_test(
881 ['-t', 'Don\'t put under_scores as they become spaces'],
882 'desc\n\nBUG=\n\nChange-Id: I123456789',
883 ref_suffix='%m=Don\'t_put_under_scores_as_they_become_spaces')
884
ukai@chromium.orge8077812012-02-03 03:41:46 +0000885 def test_gerrit_reviewers_cmd_line(self):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000886 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000887 ['-r', 'foo@example.com'],
tandrii@chromium.org09d7a6a2016-03-04 15:44:48 +0000888 'desc\n\nBUG=\n\nChange-Id: I123456789',
ukai@chromium.orge8077812012-02-03 03:41:46 +0000889 ['foo@example.com'])
890
891 def test_gerrit_reviewer_multiple(self):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000892 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000893 [],
tandrii@chromium.org09d7a6a2016-03-04 15:44:48 +0000894 'desc\nTBR=reviewer@example.com\nBUG=\nR=another@example.com\n\n'
895 'Change-Id: 123456789\n',
ukai@chromium.orge8077812012-02-03 03:41:46 +0000896 ['reviewer@example.com', 'another@example.com'])
897
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000898 def test_gerrit_upload_squash_first(self):
899 # Mock Gerrit CL description to indicate the first upload.
900 self.mock(git_cl.Changelist, 'GetDescription',
901 lambda *_: None)
902 self.mock(git_cl.gclient_utils, 'RunEditor',
903 lambda *_, **__: self._mocked_call(['RunEditor']))
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000904 self._run_gerrit_upload_test(
905 ['--squash'],
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000906 'desc\nBUG=\n\nChange-Id: 123456789',
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000907 [],
luqui@chromium.org609f3952015-05-04 22:47:04 +0000908 squash=True,
909 expected_upstream_ref='origin/master')
ukai@chromium.orge8077812012-02-03 03:41:46 +0000910
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000911 def test_gerrit_upload_squash_reupload(self):
912 description = 'desc\nBUG=\n\nChange-Id: 123456789'
913 # Mock Gerrit CL description to indicate re-upload.
914 self.mock(git_cl.Changelist, 'GetDescription',
915 lambda *args: description)
916 self.mock(git_cl.Changelist, 'GetMostRecentPatchset',
917 lambda *args: 1)
918 self.mock(git_cl._GerritChangelistImpl, '_GetChangeDetail',
919 lambda *args: {'change_id': '123456789'})
920 self._run_gerrit_upload_test(
921 ['--squash'],
922 description,
923 [],
924 squash=True,
925 expected_upstream_ref='origin/master',
926 issue=123456)
927
rmistry@google.com2dd99862015-06-22 12:22:18 +0000928 def test_upload_branch_deps(self):
929 def mock_run_git(*args, **_kwargs):
930 if args[0] == ['for-each-ref',
931 '--format=%(refname:short) %(upstream:short)',
932 'refs/heads']:
933 # Create a local branch dependency tree that looks like this:
934 # test1 -> test2 -> test3 -> test4 -> test5
935 # -> test3.1
936 # test6 -> test0
937 branch_deps = [
938 'test2 test1', # test1 -> test2
939 'test3 test2', # test2 -> test3
940 'test3.1 test2', # test2 -> test3.1
941 'test4 test3', # test3 -> test4
942 'test5 test4', # test4 -> test5
943 'test6 test0', # test0 -> test6
944 'test7', # test7
945 ]
946 return '\n'.join(branch_deps)
947 self.mock(git_cl, 'RunGit', mock_run_git)
948
949 class RecordCalls:
950 times_called = 0
951 record_calls = RecordCalls()
952 def mock_CMDupload(*args, **_kwargs):
953 record_calls.times_called += 1
954 return 0
955 self.mock(git_cl, 'CMDupload', mock_CMDupload)
956
957 self.calls = [
958 (('[Press enter to continue or ctrl-C to quit]',), ''),
959 ]
960
961 class MockChangelist():
962 def __init__(self):
963 pass
964 def GetBranch(self):
965 return 'test1'
966 def GetIssue(self):
967 return '123'
968 def GetPatchset(self):
969 return '1001'
tandrii@chromium.org4c72b082016-03-31 22:26:35 +0000970 def IsGerrit(self):
971 return False
rmistry@google.com2dd99862015-06-22 12:22:18 +0000972
973 ret = git_cl.upload_branch_deps(MockChangelist(), [])
974 # CMDupload should have been called 5 times because of 5 dependent branches.
975 self.assertEquals(5, record_calls.times_called)
976 self.assertEquals(0, ret)
977
tandrii@chromium.org65874e12016-03-04 12:03:02 +0000978 def test_gerrit_change_id(self):
979 self.calls = [
980 ((['git', 'write-tree'], ),
981 'hashtree'),
982 ((['git', 'rev-parse', 'HEAD~0'], ),
983 'branch-parent'),
984 ((['git', 'var', 'GIT_AUTHOR_IDENT'], ),
985 'A B <a@b.org> 1456848326 +0100'),
986 ((['git', 'var', 'GIT_COMMITTER_IDENT'], ),
987 'C D <c@d.org> 1456858326 +0100'),
988 ((['git', 'hash-object', '-t', 'commit', '--stdin'], ),
989 'hashchange'),
990 ]
991 change_id = git_cl.GenerateGerritChangeId('line1\nline2\n')
992 self.assertEqual(change_id, 'Ihashchange')
993
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000994 def test_update_reviewers(self):
995 data = [
996 ('foo', [], 'foo'),
agable@chromium.org42c20792013-09-12 17:34:49 +0000997 ('foo\nR=xx', [], 'foo\nR=xx'),
998 ('foo\nTBR=xx', [], 'foo\nTBR=xx'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000999 ('foo', ['a@c'], 'foo\n\nR=a@c'),
agable@chromium.org42c20792013-09-12 17:34:49 +00001000 ('foo\nR=xx', ['a@c'], 'foo\n\nR=a@c, xx'),
1001 ('foo\nTBR=xx', ['a@c'], 'foo\n\nR=a@c\nTBR=xx'),
1002 ('foo\nTBR=xx\nR=yy', ['a@c'], 'foo\n\nR=a@c, yy\nTBR=xx'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +00001003 ('foo\nBUG=', ['a@c'], 'foo\nBUG=\nR=a@c'),
agable@chromium.org42c20792013-09-12 17:34:49 +00001004 ('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 +00001005 ('foo', ['a@c', 'b@c'], 'foo\n\nR=a@c, b@c'),
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +00001006 ('foo\nBar\n\nR=\nBUG=', ['c@c'], 'foo\nBar\n\nR=c@c\nBUG='),
1007 ('foo\nBar\n\nR=\nBUG=\nR=', ['c@c'], 'foo\nBar\n\nR=c@c\nBUG='),
1008 # Same as the line before, but full of whitespaces.
1009 (
1010 'foo\nBar\n\n R = \n BUG = \n R = ', ['c@c'],
1011 'foo\nBar\n\nR=c@c\n BUG =',
1012 ),
1013 # Whitespaces aren't interpreted as new lines.
1014 ('foo BUG=allo R=joe ', ['c@c'], 'foo BUG=allo R=joe\n\nR=c@c'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +00001015 ]
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +00001016 expected = [i[2] for i in data]
1017 actual = []
1018 for orig, reviewers, _expected in data:
maruel@chromium.org78936cb2013-04-11 00:17:52 +00001019 obj = git_cl.ChangeDescription(orig)
1020 obj.update_reviewers(reviewers)
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +00001021 actual.append(obj.description)
1022 self.assertEqual(expected, actual)
maruel@chromium.org78936cb2013-04-11 00:17:52 +00001023
wittman@chromium.org455dc922015-01-26 20:15:50 +00001024 def test_get_target_ref(self):
1025 # Check remote or remote branch not present.
1026 self.assertEqual(None, git_cl.GetTargetRef('origin', None, 'master', None))
1027 self.assertEqual(None, git_cl.GetTargetRef(None,
1028 'refs/remotes/origin/master',
1029 'master', None))
bauerb@chromium.org27386dd2015-02-16 10:45:39 +00001030
wittman@chromium.org455dc922015-01-26 20:15:50 +00001031 # Check default target refs for branches.
1032 self.assertEqual('refs/heads/master',
1033 git_cl.GetTargetRef('origin', 'refs/remotes/origin/master',
1034 None, None))
1035 self.assertEqual('refs/heads/master',
1036 git_cl.GetTargetRef('origin', 'refs/remotes/origin/lkgr',
1037 None, None))
1038 self.assertEqual('refs/heads/master',
1039 git_cl.GetTargetRef('origin', 'refs/remotes/origin/lkcr',
1040 None, None))
1041 self.assertEqual('refs/branch-heads/123',
1042 git_cl.GetTargetRef('origin',
1043 'refs/remotes/branch-heads/123',
1044 None, None))
1045 self.assertEqual('refs/diff/test',
1046 git_cl.GetTargetRef('origin',
1047 'refs/remotes/origin/refs/diff/test',
1048 None, None))
rmistry@google.comc68112d2015-03-03 12:48:06 +00001049 self.assertEqual('refs/heads/chrome/m42',
1050 git_cl.GetTargetRef('origin',
1051 'refs/remotes/origin/chrome/m42',
1052 None, None))
wittman@chromium.org455dc922015-01-26 20:15:50 +00001053
1054 # Check target refs for user-specified target branch.
1055 for branch in ('branch-heads/123', 'remotes/branch-heads/123',
1056 'refs/remotes/branch-heads/123'):
1057 self.assertEqual('refs/branch-heads/123',
1058 git_cl.GetTargetRef('origin',
1059 'refs/remotes/origin/master',
1060 branch, None))
1061 for branch in ('origin/master', 'remotes/origin/master',
1062 'refs/remotes/origin/master'):
1063 self.assertEqual('refs/heads/master',
1064 git_cl.GetTargetRef('origin',
1065 'refs/remotes/branch-heads/123',
1066 branch, None))
1067 for branch in ('master', 'heads/master', 'refs/heads/master'):
1068 self.assertEqual('refs/heads/master',
1069 git_cl.GetTargetRef('origin',
1070 'refs/remotes/branch-heads/123',
1071 branch, None))
1072
1073 # Check target refs for pending prefix.
1074 self.assertEqual('prefix/heads/master',
1075 git_cl.GetTargetRef('origin', 'refs/remotes/origin/master',
1076 None, 'prefix/'))
1077
wychen@chromium.orga872e752015-04-28 23:42:18 +00001078 def test_patch_when_dirty(self):
1079 # Patch when local tree is dirty
1080 self.mock(git_common, 'is_dirty_git_tree', lambda x: True)
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +00001081 self.calls = [
1082 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
1083 ((['git', 'config', 'branch.master.rietveldissue'],), ''),
1084 ((['git', 'config', 'branch.master.gerritissue'],), ''),
1085 ((['git', 'config', 'rietveld.autoupdate'],), ''),
1086 ((['git', 'config', 'gerrit.host'],), ''),
1087 ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'),
1088 ]
wychen@chromium.orga872e752015-04-28 23:42:18 +00001089 self.assertNotEqual(git_cl.main(['patch', '123456']), 0)
1090
1091 def test_diff_when_dirty(self):
1092 # Do 'git cl diff' when local tree is dirty
1093 self.mock(git_common, 'is_dirty_git_tree', lambda x: True)
1094 self.assertNotEqual(git_cl.main(['diff']), 0)
1095
tandrii@chromium.orgdde64622016-04-13 17:11:21 +00001096 def _patch_common(self, is_gerrit=False, force_codereview=False):
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00001097 self.mock(git_cl._RietveldChangelistImpl, 'GetMostRecentPatchset',
1098 lambda x: '60001')
1099 self.mock(git_cl._RietveldChangelistImpl, 'GetPatchSetDiff',
1100 lambda *args: None)
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +00001101 self.mock(git_cl._GerritChangelistImpl, '_GetChangeDetail',
1102 lambda *args: {
1103 'current_revision': '7777777777',
1104 'revisions': {
1105 '1111111111': {
1106 '_number': 1,
1107 'fetch': {'http': {
1108 'url': 'https://chromium.googlesource.com/my/repo',
1109 'ref': 'refs/changes/56/123456/1',
1110 }},
1111 },
1112 '7777777777': {
1113 '_number': 7,
1114 'fetch': {'http': {
1115 'url': 'https://chromium.googlesource.com/my/repo',
1116 'ref': 'refs/changes/56/123456/7',
1117 }},
1118 },
1119 },
1120 })
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00001121 self.mock(git_cl.Changelist, 'GetDescription',
1122 lambda *args: 'Description')
wychen@chromium.orga872e752015-04-28 23:42:18 +00001123 self.mock(git_cl, 'IsGitVersionAtLeast', lambda *args: True)
1124
tandrii@chromium.orgdde64622016-04-13 17:11:21 +00001125 if not force_codereview:
1126 # These calls detect codereview to use.
1127 self.calls = [
1128 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
1129 ((['git', 'config', 'branch.master.rietveldissue'],), ''),
1130 ((['git', 'config', 'branch.master.gerritissue'],), ''),
1131 ((['git', 'config', 'rietveld.autoupdate'],), ''),
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +00001132 ]
1133 else:
tandrii@chromium.orgdde64622016-04-13 17:11:21 +00001134 self.calls = []
1135
1136 if is_gerrit:
1137 if not force_codereview:
1138 self.calls += [
1139 ((['git', 'config', 'gerrit.host'],), 'true'),
1140 ]
1141 else:
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +00001142 self.calls += [
1143 ((['git', 'config', 'gerrit.host'],), ''),
1144 ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'),
1145 ((['git', 'rev-parse', '--show-cdup'],), ''),
1146 ((['sed', '-e', 's|^--- a/|--- |; s|^+++ b/|+++ |'],), ''),
1147 ]
wychen@chromium.orga872e752015-04-28 23:42:18 +00001148
1149 def test_patch_successful(self):
1150 self._patch_common()
1151 self.calls += [
1152 ((['git', 'apply', '--index', '-p0', '--3way'],), ''),
1153 ((['git', 'commit', '-m',
wychen@chromium.org5b3bebb2015-05-28 21:41:43 +00001154 'Description\n\n' +
wychen@chromium.orga872e752015-04-28 23:42:18 +00001155 'patch from issue 123456 at patchset 60001 ' +
1156 '(http://crrev.com/123456#ps60001)'],), ''),
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +00001157 ((['git', 'config', 'branch.master.rietveldissue', '123456'],), ''),
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00001158 ((['git', 'config', 'branch.master.rietveldserver'],), ''),
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +00001159 ((['git', 'config', 'branch.master.rietveldserver',
1160 'https://codereview.example.com'],), ''),
1161 ((['git', 'config', 'branch.master.rietveldpatchset', '60001'],), ''),
wychen@chromium.orga872e752015-04-28 23:42:18 +00001162 ]
1163 self.assertEqual(git_cl.main(['patch', '123456']), 0)
1164
1165 def test_patch_conflict(self):
1166 self._patch_common()
1167 self.calls += [
1168 ((['git', 'apply', '--index', '-p0', '--3way'],), '',
1169 subprocess2.CalledProcessError(1, '', '', '', '')),
1170 ]
1171 self.assertNotEqual(git_cl.main(['patch', '123456']), 0)
wittman@chromium.org455dc922015-01-26 20:15:50 +00001172
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +00001173 def test_gerrit_patch_successful(self):
1174 self._patch_common(is_gerrit=True)
1175 self.calls += [
1176 ((['git', 'fetch', 'https://chromium.googlesource.com/my/repo',
1177 'refs/changes/56/123456/7'],), ''),
1178 ((['git', 'cherry-pick', 'FETCH_HEAD'],), ''),
1179 ((['git', 'config', 'branch.master.gerritissue', '123456'],), ''),
1180 ((['git', 'config', 'branch.master.gerritserver'],), ''),
1181 ((['git', 'config', 'branch.master.merge'],), 'master'),
1182 ((['git', 'config', 'branch.master.remote'],), 'origin'),
1183 ((['git', 'config', 'remote.origin.url'],),
1184 'https://chromium.googlesource.com/my/repo'),
1185 ((['git', 'config', 'branch.master.gerritserver',
1186 'https://chromium-review.googlesource.com'],), ''),
1187 ((['git', 'config', 'branch.master.gerritpatchset', '7'],), ''),
1188 ]
1189 self.assertEqual(git_cl.main(['patch', '123456']), 0)
1190
tandrii@chromium.orgdde64622016-04-13 17:11:21 +00001191 def test_patch_force_codereview(self):
1192 self._patch_common(is_gerrit=True, force_codereview=True)
1193 self.calls += [
1194 ((['git', 'fetch', 'https://chromium.googlesource.com/my/repo',
1195 'refs/changes/56/123456/7'],), ''),
1196 ((['git', 'cherry-pick', 'FETCH_HEAD'],), ''),
1197 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
1198 ((['git', 'config', 'branch.master.gerritissue', '123456'],), ''),
1199 ((['git', 'config', 'branch.master.gerritserver'],), ''),
1200 ((['git', 'config', 'branch.master.merge'],), 'master'),
1201 ((['git', 'config', 'branch.master.remote'],), 'origin'),
1202 ((['git', 'config', 'remote.origin.url'],),
1203 'https://chromium.googlesource.com/my/repo'),
1204 ((['git', 'config', 'branch.master.gerritserver',
1205 'https://chromium-review.googlesource.com'],), ''),
1206 ((['git', 'config', 'branch.master.gerritpatchset', '7'],), ''),
1207 ]
1208 self.assertEqual(git_cl.main(['patch', '--gerrit', '123456']), 0)
1209
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +00001210 def test_gerrit_patch_url_successful(self):
1211 self._patch_common(is_gerrit=True)
1212 self.calls += [
1213 ((['git', 'fetch', 'https://chromium.googlesource.com/my/repo',
1214 'refs/changes/56/123456/1'],), ''),
1215 ((['git', 'cherry-pick', 'FETCH_HEAD'],), ''),
1216 ((['git', 'config', 'branch.master.gerritissue', '123456'],), ''),
1217 ((['git', 'config', 'branch.master.gerritserver',
1218 'https://chromium-review.googlesource.com'],), ''),
1219 ((['git', 'config', 'branch.master.gerritpatchset', '1'],), ''),
1220 ]
1221 self.assertEqual(git_cl.main(
1222 ['patch', 'https://chromium-review.googlesource.com/#/c/123456/1']), 0)
1223
1224 def test_gerrit_patch_conflict(self):
1225 self._patch_common(is_gerrit=True)
1226 self.mock(git_cl, 'DieWithError',
1227 lambda msg: self._mocked_call(['DieWithError', msg]))
1228 class SystemExitMock(Exception):
1229 pass
1230 self.calls += [
1231 ((['git', 'fetch', 'https://chromium.googlesource.com/my/repo',
1232 'refs/changes/56/123456/1'],), ''),
1233 ((['git', 'cherry-pick', 'FETCH_HEAD'],),
1234 '', subprocess2.CalledProcessError(1, '', '', '', '')),
1235 ((['DieWithError', 'git cherry-pick FETCH_HEAD" failed.\n'],),
1236 '', SystemExitMock()),
1237 ]
1238 with self.assertRaises(SystemExitMock):
1239 git_cl.main(['patch',
1240 'https://chromium-review.googlesource.com/#/c/123456/1'])
1241
tandrii@chromium.org5df290f2016-04-11 16:12:29 +00001242 def _checkout_calls(self):
1243 return [
1244 ((['git', 'config', '--local', '--get-regexp',
1245 'branch\\..*\\.rietveldissue'], ),
1246 ('branch.retrying.rietveldissue 1111111111\n'
1247 'branch.some-fix.rietveldissue 2222222222\n')),
1248 ((['git', 'config', '--local', '--get-regexp',
1249 'branch\\..*\\.gerritissue'], ),
1250 ('branch.ger-branch.gerritissue 123456\n'
1251 'branch.gbranch654.gerritissue 654321\n')),
1252 ]
1253
1254 def test_checkout_gerrit(self):
1255 """Tests git cl checkout <issue>."""
1256 self.calls = self._checkout_calls()
1257 self.calls += [((['git', 'checkout', 'ger-branch'], ), '')]
1258 self.assertEqual(0, git_cl.main(['checkout', '123456']))
1259
1260 def test_checkout_rietveld(self):
1261 """Tests git cl checkout <issue>."""
1262 self.calls = self._checkout_calls()
1263 self.calls += [((['git', 'checkout', 'some-fix'], ), '')]
1264 self.assertEqual(0, git_cl.main(['checkout', '2222222222']))
1265
1266 def test_checkout_not_found(self):
1267 """Tests git cl checkout <issue>."""
1268 self.calls = self._checkout_calls()
1269 self.assertEqual(1, git_cl.main(['checkout', '99999']))
1270
tandrii@chromium.org26c8fd22016-04-11 21:33:21 +00001271 def test_checkout_no_branch_issues(self):
1272 """Tests git cl checkout <issue>."""
1273 self.calls = [
1274 ((['git', 'config', '--local', '--get-regexp',
1275 'branch\\..*\\.rietveldissue'], ), '',
1276 subprocess2.CalledProcessError(1, '', '', '', '')),
1277 ((['git', 'config', '--local', '--get-regexp',
1278 'branch\\..*\\.gerritissue'], ), '',
1279 subprocess2.CalledProcessError(1, '', '', '', '')),
1280
1281 ]
1282 self.assertEqual(1, git_cl.main(['checkout', '99999']))
1283
tandrii@chromium.orgfe30f182016-04-13 12:15:04 +00001284 def _test_gerrit_ensure_authenticated_common(self, auth):
1285 self.mock(git_cl.gerrit_util, 'CookiesAuthenticator',
1286 CookiesAuthenticatorMockFactory(hosts_with_creds=auth))
1287 self.mock(git_cl, 'DieWithError',
1288 lambda msg: self._mocked_call(['DieWithError', msg]))
1289 self.mock(git_cl, 'ask_for_data',
1290 lambda msg: self._mocked_call(['ask_for_data', msg]))
1291 self.calls = [
1292 ((['git', 'symbolic-ref', 'HEAD'],), 'master')
1293 ] + self._gerrit_ensure_auth_calls()
1294 cl = git_cl.Changelist(codereview='gerrit')
1295 cl.lookedup_issue = True
1296 return cl
1297
1298 def test_gerrit_ensure_authenticated_missing(self):
1299 cl = self._test_gerrit_ensure_authenticated_common(auth={
1300 'chromium.googlesource.com': 'git is ok, but gerrit one is missing',
1301 })
1302 self.calls.append(
1303 ((['DieWithError',
1304 'Credentials for the following hosts are required:\n'
1305 ' chromium-review.googlesource.com\n'
1306 'These are read from ~/.gitcookies (or legacy ~/.netrc)\n'
1307 'You can (re)generate your credentails by visiting '
1308 'https://chromium-review.googlesource.com/new-password'],), ''),)
1309 self.assertIsNone(cl.EnsureAuthenticated(force=False))
1310
1311 def test_gerrit_ensure_authenticated_conflict(self):
1312 cl = self._test_gerrit_ensure_authenticated_common(auth={
1313 'chromium.googlesource.com': 'one',
1314 'chromium-review.googlesource.com': 'other',
1315 })
1316 self.calls.append(
1317 ((['ask_for_data', 'If you know what you are doing, '
1318 'press Enter to continue, Ctrl+C to abort.'],), ''))
1319 self.assertIsNone(cl.EnsureAuthenticated(force=False))
1320
1321 def test_gerrit_ensure_authenticated_ok(self):
1322 cl = self._test_gerrit_ensure_authenticated_common(auth={
1323 'chromium.googlesource.com': 'same',
1324 'chromium-review.googlesource.com': 'same',
1325 })
1326 self.assertIsNone(cl.EnsureAuthenticated(force=False))
1327
tandrii@chromium.orgfa330e82016-04-13 17:09:52 +00001328 def test_cmd_set_commit_rietveld(self):
1329 self.mock(git_cl._RietveldChangelistImpl, 'SetFlag',
1330 lambda _, f, v: self._mocked_call(['SetFlag', f, v]))
1331 self.calls = [
1332 ((['git', 'symbolic-ref', 'HEAD'],), 'feature'),
1333 ((['git', 'config', 'branch.feature.rietveldissue'],), '123'),
1334 ((['git', 'config', 'rietveld.autoupdate'],), ''),
1335 ((['git', 'config', 'rietveld.server'],), ''),
1336 ((['git', 'config', 'rietveld.server'],), ''),
1337 ((['git', 'config', 'branch.feature.rietveldserver'],),
1338 'https://codereview.chromium.org'),
1339 ((['SetFlag', 'commit', '1'], ), ''),
1340 ]
1341 self.assertEqual(0, git_cl.main(['set-commit']))
1342
1343 def test_cmd_set_commit_gerrit(self):
1344 self.mock(git_cl.gerrit_util, 'SetReview',
1345 lambda h, i, labels: self._mocked_call(
1346 ['SetReview', h, i, labels]))
1347 self.calls = [
1348 ((['git', 'symbolic-ref', 'HEAD'],), 'feature'),
1349 ((['git', 'config', 'branch.feature.rietveldissue'],), ''),
1350 ((['git', 'config', 'branch.feature.gerritissue'],), '123'),
1351 ((['git', 'config', 'branch.feature.gerritserver'],),
1352 'https://chromium-review.googlesource.com'),
1353 ((['SetReview', 'chromium-review.googlesource.com', 123,
1354 {'Commit-Queue': 1}],), ''),
1355 ]
1356 self.assertEqual(0, git_cl.main(['set-commit', '-d']))
1357
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +00001358
maruel@chromium.orgddd59412011-11-30 14:20:38 +00001359if __name__ == '__main__':
maruel@chromium.org78936cb2013-04-11 00:17:52 +00001360 git_cl.logging.basicConfig(
1361 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR)
maruel@chromium.orgddd59412011-11-30 14:20:38 +00001362 unittest.main()