blob: 80021904d7cfb9432e4dd462185553e5069ed240 [file] [log] [blame]
[email protected]ddd59412011-11-30 14:20:381#!/usr/bin/env python
[email protected]eb5edbc2012-01-16 17:03:282# Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]ddd59412011-11-30 14:20:383# 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
[email protected]a3353652011-11-30 14:26:579import StringIO
[email protected]78c4b982012-02-14 02:20:2610import stat
[email protected]ddd59412011-11-30 14:20:3811import sys
12import unittest
[email protected]f86c7d32016-04-01 19:27:3013import urlparse
[email protected]ddd59412011-11-30 14:20:3814
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
[email protected]9e849272014-04-04 00:31:5520import git_common
[email protected]57d86542016-03-04 16:11:3221import git_footers
[email protected]ddd59412011-11-30 14:20:3822import subprocess2
[email protected]ddd59412011-11-30 14:20:3823
[email protected]2e72bb12012-01-17 15:18:3524class 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
[email protected]78936cb2013-04-11 00:17:5235
[email protected]2e72bb12012-01-17 15:18:3536 @staticmethod
37 def get_description(issue):
38 return 'Issue: %d' % issue
39
[email protected]78936cb2013-04-11 00:17:5240 @staticmethod
41 def get_issue_properties(_issue, _messages):
42 return {
43 'reviewers': ['[email protected]', '[email protected]'],
44 'messages': [
45 {
46 'approval': True,
47 'sender': '[email protected]',
48 },
49 ],
50 }
51
[email protected]2e72bb12012-01-17 15:18:3552
53class WatchlistsMock(object):
54 def __init__(self, _):
55 pass
56 @staticmethod
57 def GetWatchersForPaths(_):
58 return ['[email protected]']
59
60
[email protected]78c4b982012-02-14 02:20:2661class 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" +
[email protected]11f46eb2016-02-02 19:26:5167 "GERRIT_HOST: True\n")
[email protected]78c4b982012-02-14 02:20:2668
69
[email protected]eed4df32015-04-10 21:30:2070class AuthenticatorMock(object):
71 def __init__(self, *_args):
72 pass
73 def has_cached_credentials(self):
74 return True
75
76
[email protected]fe30f182016-04-13 12:15:0477def 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
[email protected]f86c7d32016-04-01 19:27:30105class 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
[email protected]ddd59412011-11-30 14:20:38199class TestGitCl(TestCase):
200 def setUp(self):
201 super(TestGitCl, self).setUp()
202 self.calls = []
203 self._calls_done = 0
[email protected]2e72bb12012-01-17 15:18:35204 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)
[email protected]a342c922016-03-16 07:08:25208 self.mock(git_cl.gclient_utils, 'CheckCallAndFilter', self._mocked_call)
[email protected]71437c02015-04-09 19:29:40209 self.mock(git_common, 'is_dirty_git_tree', lambda x: False)
[email protected]9e849272014-04-04 00:31:55210 self.mock(git_common, 'get_or_create_merge_base',
211 lambda *a: (
212 self._mocked_call(['get_or_create_merge_base']+list(a))))
[email protected]8ba38ff2015-06-11 21:41:25213 self.mock(git_cl, 'BranchExists', lambda _: True)
[email protected]ddd59412011-11-30 14:20:38214 self.mock(git_cl, 'FindCodereviewSettingsFile', lambda: '')
[email protected]2e72bb12012-01-17 15:18:35215 self.mock(git_cl, 'ask_for_data', self._mocked_call)
[email protected]ddd59412011-11-30 14:20:38216 self.mock(git_cl.presubmit_support, 'DoPresubmitChecks', PresubmitMock)
[email protected]ddd59412011-11-30 14:20:38217 self.mock(git_cl.rietveld, 'Rietveld', RietveldMock)
[email protected]4bac4b52012-11-27 20:33:52218 self.mock(git_cl.rietveld, 'CachingRietveld', RietveldMock)
[email protected]ddd59412011-11-30 14:20:38219 self.mock(git_cl.upload, 'RealMain', self.fail)
[email protected]ddd59412011-11-30 14:20:38220 self.mock(git_cl.watchlists, 'Watchlists', WatchlistsMock)
[email protected]eed4df32015-04-10 21:30:20221 self.mock(git_cl.auth, 'get_authenticator_for_host', AuthenticatorMock)
[email protected]fe30f182016-04-13 12:15:04222 self.mock(git_cl.gerrit_util.GceAuthenticator, 'is_gce',
223 classmethod(lambda _: False))
[email protected]ddd59412011-11-30 14:20:38224 # It's important to reset settings to not have inter-tests interference.
225 git_cl.settings = None
226
[email protected]f86c7d32016-04-01 19:27:30227
[email protected]ddd59412011-11-30 14:20:38228 def tearDown(self):
[email protected]445c8962015-04-28 23:30:05229 try:
[email protected]f86c7d32016-04-01 19:27:30230 # 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.
[email protected]445c8962015-04-28 23:30:05233 if not self.has_failed():
234 self.assertEquals([], self.calls)
235 finally:
236 super(TestGitCl, self).tearDown()
[email protected]ddd59412011-11-30 14:20:38237
[email protected]9e849272014-04-04 00:31:55238 def _mocked_call(self, *args, **_kwargs):
[email protected]2e72bb12012-01-17 15:18:35239 self.assertTrue(
240 self.calls,
241 '@%d Expected: <Missing> Actual: %r' % (self._calls_done, args))
[email protected]a872e752015-04-28 23:42:18242 top = self.calls.pop(0)
243 if len(top) > 2 and top[2]:
244 raise top[2]
245 expected_args, result = top
246
[email protected]e52678e2013-04-26 18:34:44247 # 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)
[email protected]2e72bb12012-01-17 15:18:35254 self._calls_done += 1
255 return result
256
[email protected]a3353652011-11-30 14:26:57257 @classmethod
[email protected]512d79c2016-03-31 12:55:28258 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
[email protected]c1737d02013-05-29 14:17:28263 def _upload_calls(cls, similarity, find_copies, private):
[email protected]79540052012-10-19 23:15:26264 return (cls._git_base_calls(similarity, find_copies) +
[email protected]c1737d02013-05-29 14:17:28265 cls._git_upload_calls(private))
[email protected]a3353652011-11-30 14:26:57266
[email protected]0f58fa82012-11-05 01:45:20267 @classmethod
[email protected]615a2622013-05-03 13:20:14268 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
[email protected]0f58fa82012-11-05 01:45:20273 def _git_base_calls(cls, similarity, find_copies):
[email protected]53937ba2012-10-02 18:20:43274 if similarity is None:
275 similarity = '50'
[email protected]82b91cd2013-07-09 06:33:41276 similarity_call = ((['git', 'config', '--int', '--get',
[email protected]53937ba2012-10-02 18:20:43277 'branch.master.git-cl-similarity'],), '')
278 else:
[email protected]82b91cd2013-07-09 06:33:41279 similarity_call = ((['git', 'config', '--int',
[email protected]53937ba2012-10-02 18:20:43280 'branch.master.git-cl-similarity', similarity],), '')
[email protected]79540052012-10-19 23:15:26281
282 if find_copies is None:
283 find_copies = True
[email protected]82b91cd2013-07-09 06:33:41284 find_copies_call = ((['git', 'config', '--int', '--get',
[email protected]79540052012-10-19 23:15:26285 'branch.master.git-find-copies'],), '')
286 else:
287 val = str(int(find_copies))
[email protected]82b91cd2013-07-09 06:33:41288 find_copies_call = ((['git', 'config', '--int',
[email protected]79540052012-10-19 23:15:26289 'branch.master.git-find-copies', val],), '')
290
291 if find_copies:
[email protected]82b91cd2013-07-09 06:33:41292 stat_call = ((['git', 'diff', '--no-ext-diff', '--stat',
[email protected]79540052012-10-19 23:15:26293 '--find-copies-harder', '-l100000', '-C'+similarity,
[email protected]5e07e062013-02-28 23:55:44294 'fake_ancestor_sha', 'HEAD'],), '+dat')
[email protected]79540052012-10-19 23:15:26295 else:
[email protected]82b91cd2013-07-09 06:33:41296 stat_call = ((['git', 'diff', '--no-ext-diff', '--stat',
[email protected]5e07e062013-02-28 23:55:44297 '-M'+similarity, 'fake_ancestor_sha', 'HEAD'],), '+dat')
[email protected]79540052012-10-19 23:15:26298
[email protected]ddd59412011-11-30 14:20:38299 return [
[email protected]82b91cd2013-07-09 06:33:41300 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
[email protected]87985d22016-03-24 17:33:33301 similarity_call,
302 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
303 find_copies_call,
[email protected]512d79c2016-03-31 12:55:28304 ] + cls._is_gerrit_calls() + [
[email protected]87985d22016-03-24 17:33:33305 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
[email protected]aa5ced12016-03-29 09:41:14306 ((['git', 'config', 'branch.master.rietveldissue'],), ''),
307 ((['git', 'config', 'branch.master.gerritissue'],), ''),
[email protected]aa5ced12016-03-29 09:41:14308 ((['git', 'config', 'rietveld.server'],),
309 'codereview.example.com'),
[email protected]82b91cd2013-07-09 06:33:41310 ((['git', 'config', 'branch.master.merge'],), 'master'),
311 ((['git', 'config', 'branch.master.remote'],), 'origin'),
[email protected]9e849272014-04-04 00:31:55312 ((['get_or_create_merge_base', 'master', 'master'],),
[email protected]f267b0e2013-05-02 09:11:43313 'fake_ancestor_sha'),
[email protected]512d79c2016-03-31 12:55:28314 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [
[email protected]82b91cd2013-07-09 06:33:41315 ((['git', 'rev-parse', '--show-cdup'],), ''),
316 ((['git', 'rev-parse', 'HEAD'],), '12345'),
317 ((['git', 'diff', '--name-status', '--no-renames', '-r',
[email protected]f267b0e2013-05-02 09:11:43318 'fake_ancestor_sha...', '.'],),
[email protected]2e72bb12012-01-17 15:18:35319 'M\t.gitignore\n'),
[email protected]82b91cd2013-07-09 06:33:41320 ((['git', 'config', 'branch.master.rietveldpatchset'],),
[email protected]f267b0e2013-05-02 09:11:43321 ''),
[email protected]82b91cd2013-07-09 06:33:41322 ((['git', 'log', '--pretty=format:%s%n%n%b',
[email protected]f267b0e2013-05-02 09:11:43323 'fake_ancestor_sha...'],),
[email protected]0f58fa82012-11-05 01:45:20324 'foo'),
[email protected]82b91cd2013-07-09 06:33:41325 ((['git', 'config', 'user.email'],), '[email protected]'),
[email protected]79540052012-10-19 23:15:26326 stat_call,
[email protected]82b91cd2013-07-09 06:33:41327 ((['git', 'log', '--pretty=format:%s\n\n%b',
[email protected]f267b0e2013-05-02 09:11:43328 'fake_ancestor_sha..HEAD'],),
[email protected]0f58fa82012-11-05 01:45:20329 'desc\n'),
[email protected]90752582014-01-14 21:04:50330 ((['git', 'config', 'rietveld.bug-prefix'],), ''),
[email protected]a3353652011-11-30 14:26:57331 ]
332
[email protected]0f58fa82012-11-05 01:45:20333 @classmethod
[email protected]615a2622013-05-03 13:20:14334 def _git_upload_no_rev_calls(cls):
335 return [
[email protected]82b91cd2013-07-09 06:33:41336 ((['git', 'config', 'core.editor'],), ''),
[email protected]615a2622013-05-03 13:20:14337 ]
338
339 @classmethod
[email protected]c1737d02013-05-29 14:17:28340 def _git_upload_calls(cls, private):
341 if private:
[email protected]99918ab2013-09-30 06:17:28342 cc_call = []
[email protected]c1737d02013-05-29 14:17:28343 private_call = []
344 else:
[email protected]99918ab2013-09-30 06:17:28345 cc_call = [((['git', 'config', 'rietveld.cc'],), '')]
[email protected]c1737d02013-05-29 14:17:28346 private_call = [
[email protected]82b91cd2013-07-09 06:33:41347 ((['git', 'config', 'rietveld.private'],), '')]
[email protected]c1737d02013-05-29 14:17:28348
[email protected]a3353652011-11-30 14:26:57349 return [
[email protected]82b91cd2013-07-09 06:33:41350 ((['git', 'config', 'core.editor'],), ''),
[email protected]99918ab2013-09-30 06:17:28351 ] + cc_call + private_call + [
[email protected]82b91cd2013-07-09 06:33:41352 ((['git', 'config', 'branch.master.base-url'],), ''),
[email protected]566a02a2014-08-22 01:34:13353 ((['git', 'config', 'rietveld.pending-ref-prefix'],), ''),
[email protected]82b91cd2013-07-09 06:33:41354 ((['git',
[email protected]05fb9112014-07-07 09:30:23355 'config', '--local', '--get-regexp', '^svn-remote\\.'],),
356 (('', None), 0)),
[email protected]7a54e812014-02-11 19:57:22357 ((['git', 'rev-parse', '--show-cdup'],), ''),
[email protected]82b91cd2013-07-09 06:33:41358 ((['git', 'svn', 'info'],), ''),
[email protected]152cf832014-06-11 21:37:49359 ((['git', 'config', 'rietveld.project'],), ''),
[email protected]82b91cd2013-07-09 06:33:41360 ((['git',
[email protected]c1737d02013-05-29 14:17:28361 'config', 'branch.master.rietveldissue', '1'],), ''),
[email protected]82b91cd2013-07-09 06:33:41362 ((['git', 'config', 'branch.master.rietveldserver',
[email protected]c1737d02013-05-29 14:17:28363 'https://codereview.example.com'],), ''),
[email protected]82b91cd2013-07-09 06:33:41364 ((['git',
[email protected]c1737d02013-05-29 14:17:28365 'config', 'branch.master.rietveldpatchset', '2'],), ''),
[email protected]1e67bb72016-02-11 12:15:49366 ] + cls._git_post_upload_calls()
367
368 @classmethod
369 def _git_post_upload_calls(cls):
370 return [
[email protected]82b91cd2013-07-09 06:33:41371 ((['git', 'rev-parse', 'HEAD'],), 'hash'),
372 ((['git', 'symbolic-ref', 'HEAD'],), 'hash'),
373 ((['git',
[email protected]c1737d02013-05-29 14:17:28374 'config', 'branch.hash.last-upload-hash', 'hash'],), ''),
[email protected]5626a922015-02-26 14:03:30375 ((['git', 'config', 'rietveld.run-post-upload-hook'],), ''),
[email protected]ddd59412011-11-30 14:20:38376 ]
377
[email protected]0f58fa82012-11-05 01:45:20378 @staticmethod
[email protected]fe30f182016-04-13 12:15:04379 def _git_sanity_checks(diff_base, working_branch, get_remote_branch=True):
[email protected]0f58fa82012-11-05 01:45:20380 fake_ancestor = 'fake_ancestor'
381 fake_cl = 'fake_cl_for_patch'
382 return [
[email protected]82b91cd2013-07-09 06:33:41383 ((['git',
[email protected]f267b0e2013-05-02 09:11:43384 'rev-parse', '--verify', diff_base],), fake_ancestor),
[email protected]82b91cd2013-07-09 06:33:41385 ((['git',
[email protected]f267b0e2013-05-02 09:11:43386 'merge-base', fake_ancestor, 'HEAD'],), fake_ancestor),
[email protected]82b91cd2013-07-09 06:33:41387 ((['git',
[email protected]f267b0e2013-05-02 09:11:43388 'rev-list', '^' + fake_ancestor, 'HEAD'],), fake_cl),
[email protected]0f58fa82012-11-05 01:45:20389 # Mock a config miss (error code 1)
[email protected]82b91cd2013-07-09 06:33:41390 ((['git',
[email protected]f267b0e2013-05-02 09:11:43391 'config', 'gitcl.remotebranch'],), (('', None), 1)),
[email protected]fe30f182016-04-13 12:15:04392 ] + ([
[email protected]0f58fa82012-11-05 01:45:20393 # Call to GetRemoteBranch()
[email protected]82b91cd2013-07-09 06:33:41394 ((['git',
[email protected]f267b0e2013-05-02 09:11:43395 'config', 'branch.%s.merge' % working_branch],),
[email protected]0f58fa82012-11-05 01:45:20396 'refs/heads/master'),
[email protected]82b91cd2013-07-09 06:33:41397 ((['git',
[email protected]f267b0e2013-05-02 09:11:43398 'config', 'branch.%s.remote' % working_branch],), 'origin'),
[email protected]fe30f182016-04-13 12:15:04399 ] if get_remote_branch else []) + [
[email protected]82b91cd2013-07-09 06:33:41400 ((['git', 'rev-list', '^' + fake_ancestor,
[email protected]0f58fa82012-11-05 01:45:20401 'refs/remotes/origin/master'],), ''),
[email protected]fe30f182016-04-13 12:15:04402 ]
[email protected]0f58fa82012-11-05 01:45:20403
[email protected]2e72bb12012-01-17 15:18:35404 @classmethod
405 def _dcommit_calls_1(cls):
406 return [
[email protected]566a02a2014-08-22 01:34:13407 ((['git', 'config', 'rietveld.autoupdate'],),
408 ''),
409 ((['git', 'config', 'rietveld.pending-ref-prefix'],),
410 ''),
[email protected]82b91cd2013-07-09 06:33:41411 ((['git',
[email protected]05fb9112014-07-07 09:30:23412 'config', '--local', '--get-regexp', '^svn-remote\\.'],),
[email protected]2e72bb12012-01-17 15:18:35413 ((('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)),
[email protected]82b91cd2013-07-09 06:33:41417 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
418 ((['git', 'config', '--int', '--get',
[email protected]53937ba2012-10-02 18:20:43419 'branch.working.git-cl-similarity'],), ''),
[email protected]82b91cd2013-07-09 06:33:41420 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
421 ((['git', 'config', '--int', '--get',
[email protected]79540052012-10-19 23:15:26422 'branch.working.git-find-copies'],), ''),
[email protected]82b91cd2013-07-09 06:33:41423 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
424 ((['git',
[email protected]aa5ced12016-03-29 09:41:14425 'config', 'branch.working.rietveldissue'],), '12345'),
426 ((['git',
427 'config', 'rietveld.server'],), 'codereview.example.com'),
428 ((['git',
[email protected]f267b0e2013-05-02 09:11:43429 'config', 'branch.working.merge'],), 'refs/heads/master'),
[email protected]82b91cd2013-07-09 06:33:41430 ((['git', 'config', 'branch.working.remote'],), 'origin'),
[email protected]5724c962014-04-11 09:32:56431 ((['git', 'config', 'branch.working.merge'],),
432 'refs/heads/master'),
433 ((['git', 'config', 'branch.working.remote'],), 'origin'),
[email protected]82b91cd2013-07-09 06:33:41434 ((['git', 'rev-list', '--merges',
[email protected]e84b7542012-06-15 21:26:58435 '--grep=^SVN changes up to revision [0-9]*$',
[email protected]9bb85e22012-06-13 20:28:23436 'refs/remotes/origin/master^!'],), ''),
[email protected]82b91cd2013-07-09 06:33:41437 ((['git', 'rev-list', '^refs/heads/working',
[email protected]2e72bb12012-01-17 15:18:35438 'refs/remotes/origin/master'],),
439 ''),
[email protected]82b91cd2013-07-09 06:33:41440 ((['git',
[email protected]f267b0e2013-05-02 09:11:43441 'log', '--grep=^git-svn-id:', '-1', '--pretty=format:%H'],),
[email protected]2e72bb12012-01-17 15:18:35442 '3fc18b62c4966193eb435baabe2d18a3810ec82e'),
[email protected]82b91cd2013-07-09 06:33:41443 ((['git',
[email protected]f267b0e2013-05-02 09:11:43444 'rev-list', '^3fc18b62c4966193eb435baabe2d18a3810ec82e',
[email protected]2e72bb12012-01-17 15:18:35445 'refs/remotes/origin/master'],), ''),
[email protected]82b91cd2013-07-09 06:33:41446 ((['git',
[email protected]f267b0e2013-05-02 09:11:43447 'merge-base', 'refs/remotes/origin/master', 'HEAD'],),
[email protected]0f58fa82012-11-05 01:45:20448 'fake_ancestor_sha'),
[email protected]2e72bb12012-01-17 15:18:35449 ]
450
451 @classmethod
452 def _dcommit_calls_normal(cls):
453 return [
[email protected]82b91cd2013-07-09 06:33:41454 ((['git', 'rev-parse', '--show-cdup'],), ''),
455 ((['git', 'rev-parse', 'HEAD'],),
[email protected]2e72bb12012-01-17 15:18:35456 '00ff397798ea57439712ed7e04ab96e13969ef40'),
[email protected]82b91cd2013-07-09 06:33:41457 ((['git',
[email protected]9249f642013-06-03 21:36:18458 'diff', '--name-status', '--no-renames', '-r', 'fake_ancestor_sha...',
[email protected]2e72bb12012-01-17 15:18:35459 '.'],),
460 'M\tPRESUBMIT.py'),
[email protected]82b91cd2013-07-09 06:33:41461 ((['git',
[email protected]f267b0e2013-05-02 09:11:43462 'config', 'branch.working.rietveldpatchset'],), '31137'),
[email protected]82b91cd2013-07-09 06:33:41463 ((['git', 'config', 'branch.working.rietveldserver'],),
[email protected]2e72bb12012-01-17 15:18:35464 'codereview.example.com'),
[email protected]82b91cd2013-07-09 06:33:41465 ((['git', 'config', 'user.email'],), '[email protected]'),
466 ((['git', 'config', 'rietveld.tree-status-url'],), ''),
[email protected]2e72bb12012-01-17 15:18:35467 ]
468
469 @classmethod
470 def _dcommit_calls_bypassed(cls):
471 return [
[email protected]82b91cd2013-07-09 06:33:41472 ((['git', 'config', 'branch.working.rietveldserver'],),
[email protected]2e72bb12012-01-17 15:18:35473 'codereview.example.com'),
[email protected]2e72bb12012-01-17 15:18:35474 ]
475
476 @classmethod
[email protected]7a54e812014-02-11 19:57:22477 def _dcommit_calls_3(cls):
478 return [
[email protected]82b91cd2013-07-09 06:33:41479 ((['git',
[email protected]f267b0e2013-05-02 09:11:43480 'diff', '--no-ext-diff', '--stat', '--find-copies-harder',
[email protected]0f58fa82012-11-05 01:45:20481 '-l100000', '-C50', 'fake_ancestor_sha',
[email protected]53937ba2012-10-02 18:20:43482 'refs/heads/working'],),
[email protected]2e72bb12012-01-17 15:18:35483 (' PRESUBMIT.py | 2 +-\n'
484 ' 1 files changed, 1 insertions(+), 1 deletions(-)\n')),
[email protected]82b91cd2013-07-09 06:33:41485 ((['git', 'show-ref', '--quiet', '--verify',
[email protected]2e72bb12012-01-17 15:18:35486 'refs/heads/git-cl-commit'],),
487 (('', None), 0)),
[email protected]82b91cd2013-07-09 06:33:41488 ((['git', 'branch', '-D', 'git-cl-commit'],), ''),
489 ((['git', 'show-ref', '--quiet', '--verify',
[email protected]9bb85e22012-06-13 20:28:23490 'refs/heads/git-cl-cherry-pick'],), ''),
[email protected]7a54e812014-02-11 19:57:22491 ((['git', 'rev-parse', '--show-cdup'],), '\n'),
[email protected]82b91cd2013-07-09 06:33:41492 ((['git', 'checkout', '-q', '-b', 'git-cl-commit'],), ''),
493 ((['git', 'reset', '--soft', 'fake_ancestor_sha'],), ''),
494 ((['git', 'commit', '-m',
[email protected]e52678e2013-04-26 18:34:44495 'Issue: 12345\n\[email protected]\n\n'
[email protected]4b39c5f2015-07-07 10:33:12496 'Review URL: https://codereview.example.com/12345 .'],),
[email protected]2e72bb12012-01-17 15:18:35497 ''),
[email protected]6abc6522014-12-02 07:34:49498 ((['git', 'config', 'rietveld.force-https-commit-url'],), ''),
[email protected]82b91cd2013-07-09 06:33:41499 ((['git',
[email protected]f267b0e2013-05-02 09:11:43500 'svn', 'dcommit', '-C50', '--no-rebase', '--rmdir'],),
[email protected]53937ba2012-10-02 18:20:43501 (('', None), 0)),
[email protected]82b91cd2013-07-09 06:33:41502 ((['git', 'checkout', '-q', 'working'],), ''),
503 ((['git', 'branch', '-D', 'git-cl-commit'],), ''),
[email protected]7a54e812014-02-11 19:57:22504 ]
[email protected]2e72bb12012-01-17 15:18:35505
[email protected]ddd59412011-11-30 14:20:38506 @staticmethod
[email protected]c1737d02013-05-29 14:17:28507 def _cmd_line(description, args, similarity, find_copies, private):
[email protected]ddd59412011-11-30 14:20:38508 """Returns the upload command line passed to upload.RealMain()."""
[email protected]ddd59412011-11-30 14:20:38509 return [
510 'upload', '--assume_yes', '--server',
[email protected]eb5edbc2012-01-16 17:03:28511 'https://codereview.example.com',
[email protected]71e12a92012-02-14 02:34:15512 '--message', description
[email protected]ddd59412011-11-30 14:20:38513 ] + args + [
514 '--cc', '[email protected]',
[email protected]c1737d02013-05-29 14:17:28515 ] + (['--private'] if private else []) + [
[email protected]79540052012-10-19 23:15:26516 '--git_similarity', similarity or '50'
517 ] + (['--git_no_find_copies'] if find_copies == False else []) + [
[email protected]5e07e062013-02-28 23:55:44518 'fake_ancestor_sha', 'HEAD'
[email protected]ddd59412011-11-30 14:20:38519 ]
520
521 def _run_reviewer_test(
522 self,
523 upload_args,
524 expected_description,
525 returned_description,
526 final_description,
[email protected]c1737d02013-05-29 14:17:28527 reviewers,
528 private=False):
[email protected]ddd59412011-11-30 14:20:38529 """Generic reviewer test framework."""
[email protected]53937ba2012-10-02 18:20:43530 try:
531 similarity = upload_args[upload_args.index('--similarity')+1]
532 except ValueError:
533 similarity = None
[email protected]79540052012-10-19 23:15:26534
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
[email protected]c1737d02013-05-29 14:17:28542 private = '--private' in upload_args
543
544 self.calls = self._upload_calls(similarity, find_copies, private)
[email protected]87884cc2014-01-03 22:23:41545
[email protected]615a2622013-05-03 13:20:14546 def RunEditor(desc, _, **kwargs):
[email protected]ddd59412011-11-30 14:20:38547 self.assertEquals(
548 '# Enter a description of the change.\n'
[email protected]104b2db2013-04-18 12:58:40549 '# This will be displayed on the codereview site.\n'
[email protected]63a4d7f2013-05-31 02:22:45550 '# The first line will also be used as the subject of the review.\n'
[email protected]bd1073e2013-06-01 00:34:38551 '#--------------------This line is 72 characters long'
552 '--------------------\n' +
[email protected]ddd59412011-11-30 14:20:38553 expected_description,
554 desc)
555 return returned_description
556 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor)
[email protected]87884cc2014-01-03 22:23:41557
[email protected]ddd59412011-11-30 14:20:38558 def check_upload(args):
[email protected]79540052012-10-19 23:15:26559 cmd_line = self._cmd_line(final_description, reviewers, similarity,
[email protected]c1737d02013-05-29 14:17:28560 find_copies, private)
[email protected]53937ba2012-10-02 18:20:43561 self.assertEquals(cmd_line, args)
[email protected]ddd59412011-11-30 14:20:38562 return 1, 2
563 self.mock(git_cl.upload, 'RealMain', check_upload)
[email protected]87884cc2014-01-03 22:23:41564
[email protected]ddd59412011-11-30 14:20:38565 git_cl.main(['upload'] + upload_args)
566
567 def test_no_reviewer(self):
568 self._run_reviewer_test(
569 [],
[email protected]78936cb2013-04-11 00:17:52570 'desc\n\nBUG=',
571 '# Blah blah comment.\ndesc\n\nBUG=',
572 'desc\n\nBUG=',
[email protected]ddd59412011-11-30 14:20:38573 [])
574
[email protected]53937ba2012-10-02 18:20:43575 def test_keep_similarity(self):
576 self._run_reviewer_test(
577 ['--similarity', '70'],
[email protected]78936cb2013-04-11 00:17:52578 'desc\n\nBUG=',
579 '# Blah blah comment.\ndesc\n\nBUG=',
580 'desc\n\nBUG=',
[email protected]53937ba2012-10-02 18:20:43581 [])
582
[email protected]79540052012-10-19 23:15:26583 def test_keep_find_copies(self):
584 self._run_reviewer_test(
585 ['--no-find-copies'],
[email protected]78936cb2013-04-11 00:17:52586 'desc\n\nBUG=',
[email protected]79540052012-10-19 23:15:26587 '# Blah blah comment.\ndesc\n\nBUG=\n',
[email protected]78936cb2013-04-11 00:17:52588 'desc\n\nBUG=',
[email protected]79540052012-10-19 23:15:26589 [])
590
[email protected]c1737d02013-05-29 14:17:28591 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
[email protected]ddd59412011-11-30 14:20:38599 def test_reviewers_cmd_line(self):
600 # Reviewer is passed as-is
[email protected]78936cb2013-04-11 00:17:52601 description = 'desc\n\[email protected]\nBUG='
[email protected]ddd59412011-11-30 14:20:38602 self._run_reviewer_test(
603 ['-r' '[email protected]'],
604 description,
605 '\n%s\n' % description,
606 description,
[email protected]78936cb2013-04-11 00:17:52607 ['[email protected]'])
[email protected]ddd59412011-11-30 14:20:38608
609 def test_reviewer_tbr_overriden(self):
610 # Reviewer is overriden with TBR
611 # Also verifies the regexp work without a trailing LF
[email protected]78936cb2013-04-11 00:17:52612 description = 'Foo Bar\n\[email protected]'
[email protected]ddd59412011-11-30 14:20:38613 self._run_reviewer_test(
614 ['-r' '[email protected]'],
[email protected]78936cb2013-04-11 00:17:52615 'desc\n\[email protected]\nBUG=',
[email protected]ddd59412011-11-30 14:20:38616 description.strip('\n'),
617 description,
[email protected]78936cb2013-04-11 00:17:52618 ['[email protected]'])
[email protected]ddd59412011-11-30 14:20:38619
620 def test_reviewer_multiple(self):
621 # Handles multiple R= or TBR= lines.
622 description = (
[email protected]78936cb2013-04-11 00:17:52623 'Foo Bar\[email protected]\nBUG=\[email protected]')
[email protected]ddd59412011-11-30 14:20:38624 self._run_reviewer_test(
625 [],
[email protected]78936cb2013-04-11 00:17:52626 'desc\n\nBUG=',
[email protected]ddd59412011-11-30 14:20:38627 description,
628 description,
[email protected]78936cb2013-04-11 00:17:52629 ['[email protected],[email protected]'])
[email protected]ddd59412011-11-30 14:20:38630
[email protected]a3353652011-11-30 14:26:57631 def test_reviewer_send_mail(self):
632 # --send-mail can be used without -r if R= is used
[email protected]78936cb2013-04-11 00:17:52633 description = 'Foo Bar\[email protected]'
[email protected]a3353652011-11-30 14:26:57634 self._run_reviewer_test(
635 ['--send-mail'],
[email protected]78936cb2013-04-11 00:17:52636 'desc\n\nBUG=',
[email protected]a3353652011-11-30 14:26:57637 description.strip('\n'),
638 description,
[email protected]78936cb2013-04-11 00:17:52639 ['[email protected]', '--send_mail'])
[email protected]a3353652011-11-30 14:26:57640
641 def test_reviewer_send_mail_no_rev(self):
642 # Fails without a reviewer.
[email protected]2e23ce32013-05-07 12:42:28643 stdout = StringIO.StringIO()
644 stderr = StringIO.StringIO()
[email protected]a3353652011-11-30 14:26:57645 try:
[email protected]615a2622013-05-03 13:20:14646 self.calls = self._upload_no_rev_calls(None, None)
647 def RunEditor(desc, _, **kwargs):
[email protected]a3353652011-11-30 14:26:57648 return desc
649 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor)
[email protected]2e23ce32013-05-07 12:42:28650 self.mock(sys, 'stdout', stdout)
651 self.mock(sys, 'stderr', stderr)
[email protected]a3353652011-11-30 14:26:57652 git_cl.main(['upload', '--send-mail'])
653 self.fail()
654 except SystemExit:
[email protected]2e23ce32013-05-07 12:42:28655 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())
[email protected]a3353652011-11-30 14:26:57661
[email protected]2e72bb12012-01-17 15:18:35662 def test_dcommit(self):
663 self.calls = (
664 self._dcommit_calls_1() +
[email protected]0f58fa82012-11-05 01:45:20665 self._git_sanity_checks('fake_ancestor_sha', 'working') +
[email protected]2e72bb12012-01-17 15:18:35666 self._dcommit_calls_normal() +
[email protected]7a54e812014-02-11 19:57:22667 self._dcommit_calls_3())
[email protected]2e72bb12012-01-17 15:18:35668 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() +
[email protected]7a54e812014-02-11 19:57:22674 self._dcommit_calls_3())
[email protected]2e72bb12012-01-17 15:18:35675 git_cl.main(['dcommit', '--bypass-hooks'])
676
[email protected]ddd59412011-11-30 14:20:38677
[email protected]0f58fa82012-11-05 01:45:20678 @classmethod
[email protected]fe30f182016-04-13 12:15:04679 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
[email protected]512d79c2016-03-31 12:55:28696 def _gerrit_base_calls(cls, issue=None):
[email protected]e8077812012-02-03 03:41:46697 return [
[email protected]82b91cd2013-07-09 06:33:41698 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
699 ((['git', 'config', '--int', '--get',
[email protected]53937ba2012-10-02 18:20:43700 'branch.master.git-cl-similarity'],), ''),
[email protected]82b91cd2013-07-09 06:33:41701 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
702 ((['git', 'config', '--int', '--get',
[email protected]79540052012-10-19 23:15:26703 'branch.master.git-find-copies'],), ''),
[email protected]512d79c2016-03-31 12:55:28704 ] + cls._is_gerrit_calls(True) + [
[email protected]82b91cd2013-07-09 06:33:41705 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
[email protected]aa5ced12016-03-29 09:41:14706 ((['git', 'config', 'branch.master.rietveldissue'],), ''),
[email protected]512d79c2016-03-31 12:55:28707 ((['git', 'config', 'branch.master.gerritissue'],),
708 '' if issue is None else str(issue)),
[email protected]fe30f182016-04-13 12:15:04709 ((['git', 'config', 'branch.master.merge'],), 'refs/heads/master'),
[email protected]82b91cd2013-07-09 06:33:41710 ((['git', 'config', 'branch.master.remote'],), 'origin'),
[email protected]fe30f182016-04-13 12:15:04711 ((['get_or_create_merge_base', 'master',
712 'refs/remotes/origin/master'],),
[email protected]9e849272014-04-04 00:31:55713 'fake_ancestor_sha'),
[email protected]fe30f182016-04-13 12:15:04714 # 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)) + [
[email protected]82b91cd2013-07-09 06:33:41718 ((['git', 'rev-parse', '--show-cdup'],), ''),
719 ((['git', 'rev-parse', 'HEAD'],), '12345'),
[email protected]fe30f182016-04-13 12:15:04720
[email protected]82b91cd2013-07-09 06:33:41721 ((['git',
[email protected]9249f642013-06-03 21:36:18722 'diff', '--name-status', '--no-renames', '-r',
723 'fake_ancestor_sha...', '.'],),
[email protected]e8077812012-02-03 03:41:46724 'M\t.gitignore\n'),
[email protected]aa5ced12016-03-29 09:41:14725 ((['git', 'config', 'branch.master.gerritpatchset'],), ''),
[email protected]512d79c2016-03-31 12:55:28726 ] + ([] if issue else [
[email protected]82b91cd2013-07-09 06:33:41727 ((['git',
[email protected]f267b0e2013-05-02 09:11:43728 'log', '--pretty=format:%s%n%n%b', 'fake_ancestor_sha...'],),
[email protected]0f58fa82012-11-05 01:45:20729 'foo'),
[email protected]512d79c2016-03-31 12:55:28730 ]) + [
[email protected]82b91cd2013-07-09 06:33:41731 ((['git', 'config', 'user.email'],), '[email protected]'),
732 ((['git',
[email protected]f267b0e2013-05-02 09:11:43733 'diff', '--no-ext-diff', '--stat', '--find-copies-harder',
[email protected]5e07e062013-02-28 23:55:44734 '-l100000', '-C50', 'fake_ancestor_sha', 'HEAD'],),
[email protected]e8077812012-02-03 03:41:46735 '+dat'),
[email protected]aa5ced12016-03-29 09:41:14736 ]
[email protected]e8077812012-02-03 03:41:46737
[email protected]1e67bb72016-02-11 12:15:49738 @classmethod
739 def _gerrit_upload_calls(cls, description, reviewers, squash,
[email protected]10625002016-03-04 20:03:47740 expected_upstream_ref='origin/refs/heads/master',
[email protected]bf766ba2016-04-13 12:51:23741 ref_suffix='',
[email protected]512d79c2016-03-31 12:55:28742 post_amend_description=None, issue=None):
[email protected]10625002016-03-04 20:03:47743 if post_amend_description is None:
744 post_amend_description = description
[email protected]512d79c2016-03-31 12:55:28745
[email protected]e8077812012-02-03 03:41:46746 calls = [
[email protected]54b400c2016-01-14 10:08:25747 ((['git', 'config', '--bool', 'gerrit.squash-uploads'],), 'false'),
[email protected]512d79c2016-03-31 12:55:28748 ]
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)]
[email protected]57d86542016-03-04 16:11:32759 if not git_footers.get_footer_change_id(description) and not squash:
[email protected]aebe87f2012-10-22 20:34:21760 calls += [
[email protected]10625002016-03-04 20:03:47761 # DownloadGerritHook(False)
762 ((False, ),
763 ''),
764 # Amending of commit message to get the Change-Id.
[email protected]82b91cd2013-07-09 06:33:41765 ((['git', 'log', '--pretty=format:%s\n\n%b',
[email protected]5e07e062013-02-28 23:55:44766 'fake_ancestor_sha..HEAD'],),
[email protected]aebe87f2012-10-22 20:34:21767 description),
[email protected]82b91cd2013-07-09 06:33:41768 ((['git', 'commit', '--amend', '-m', description],),
[email protected]aebe87f2012-10-22 20:34:21769 ''),
[email protected]82b91cd2013-07-09 06:33:41770 ((['git', 'log', '--pretty=format:%s\n\n%b',
[email protected]5e07e062013-02-28 23:55:44771 'fake_ancestor_sha..HEAD'],),
[email protected]10625002016-03-04 20:03:47772 post_amend_description)
[email protected]aebe87f2012-10-22 20:34:21773 ]
[email protected]27386dd2015-02-16 10:45:39774 if squash:
[email protected]512d79c2016-03-31 12:55:28775 if not issue:
776 # Prompting to edit description on first upload.
777 calls += [
778 ((['git', 'config', 'core.editor'],), ''),
779 ((['RunEditor'],), description),
780 ]
[email protected]27386dd2015-02-16 10:45:39781 ref_to_push = 'abcdef0123456789'
782 calls += [
[email protected]27386dd2015-02-16 10:45:39783 ((['git', 'config', 'branch.master.merge'],),
784 'refs/heads/master'),
785 ((['git', 'config', 'branch.master.remote'],),
786 'origin'),
[email protected]fe30f182016-04-13 12:15:04787 ((['get_or_create_merge_base', 'master',
788 'refs/remotes/origin/master'],),
[email protected]27386dd2015-02-16 10:45:39789 'origin/master'),
790 ((['git', 'rev-parse', 'HEAD:'],),
791 '0123456789abcdef'),
792 ((['git', 'commit-tree', '0123456789abcdef', '-p',
[email protected]512d79c2016-03-31 12:55:28793 'origin/master', '-m', description],),
[email protected]27386dd2015-02-16 10:45:39794 ref_to_push),
795 ]
796 else:
797 ref_to_push = 'HEAD'
798
[email protected]aebe87f2012-10-22 20:34:21799 calls += [
[email protected]609f3952015-05-04 22:47:04800 ((['git', 'rev-list',
801 expected_upstream_ref + '..' + ref_to_push],), ''),
[email protected]82b91cd2013-07-09 06:33:41802 ((['git', 'config', 'rietveld.cc'],), '')
[email protected]e8077812012-02-03 03:41:46803 ]
[email protected]8acd8332016-04-13 12:56:03804 # Add cc from watch list.
805 if ref_suffix == '':
806 ref_suffix = '%[email protected]'
807 else:
808 ref_suffix += ',[email protected]'
[email protected]e8077812012-02-03 03:41:46809 if reviewers:
[email protected]8acd8332016-04-13 12:56:03810 ref_suffix += ',' + ','.join('r=%s' % email
811 for email in sorted(reviewers))
[email protected]e8077812012-02-03 03:41:46812 calls += [
[email protected]8acd8332016-04-13 12:56:03813 ((['git', 'push', 'origin',
[email protected]bf766ba2016-04-13 12:51:23814 ref_to_push + ':refs/for/refs/heads/master' + ref_suffix],),
[email protected]a342c922016-03-16 07:08:25815 ('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')),
[email protected]e8077812012-02-03 03:41:46828 ]
[email protected]27386dd2015-02-16 10:45:39829 if squash:
830 calls += [
[email protected]aa5ced12016-03-29 09:41:14831 ((['git', 'config', 'branch.master.gerritissue', '123456'],), ''),
[email protected]aa5ced12016-03-29 09:41:14832 ((['git', 'config', 'branch.master.gerritserver',
833 'https://chromium-review.googlesource.com'],), ''),
[email protected]512d79c2016-03-31 12:55:28834 ((['git', 'config', 'branch.master.gerritsquashhash',
835 'abcdef0123456789'],), ''),
[email protected]fe30f182016-04-13 12:15:04836 ]
[email protected]1e67bb72016-02-11 12:15:49837 calls += cls._git_post_upload_calls()
[email protected]e8077812012-02-03 03:41:46838 return calls
839
[email protected]aebe87f2012-10-22 20:34:21840 def _run_gerrit_upload_test(
[email protected]e8077812012-02-03 03:41:46841 self,
842 upload_args,
843 description,
[email protected]bf766ba2016-04-13 12:51:23844 reviewers=None,
[email protected]609f3952015-05-04 22:47:04845 squash=False,
[email protected]10625002016-03-04 20:03:47846 expected_upstream_ref='origin/refs/heads/master',
[email protected]bf766ba2016-04-13 12:51:23847 ref_suffix='',
[email protected]512d79c2016-03-31 12:55:28848 post_amend_description=None,
849 issue=None):
[email protected]aebe87f2012-10-22 20:34:21850 """Generic gerrit upload test framework."""
[email protected]bf766ba2016-04-13 12:51:23851 reviewers = reviewers or []
[email protected]fe30f182016-04-13 12:15:04852 self.mock(git_cl.gerrit_util, "CookiesAuthenticator",
853 CookiesAuthenticatorMockFactory(same_cookie='same_cred'))
[email protected]512d79c2016-03-31 12:55:28854 self.calls = self._gerrit_base_calls(issue=issue)
[email protected]609f3952015-05-04 22:47:04855 self.calls += self._gerrit_upload_calls(
856 description, reviewers, squash,
[email protected]10625002016-03-04 20:03:47857 expected_upstream_ref=expected_upstream_ref,
[email protected]bf766ba2016-04-13 12:51:23858 ref_suffix=ref_suffix,
[email protected]512d79c2016-03-31 12:55:28859 post_amend_description=post_amend_description,
860 issue=issue)
[email protected]09d7a6a2016-03-04 15:44:48861 # Uncomment when debugging.
862 # print '\n'.join(map(lambda x: '%2i: %s' % x, enumerate(self.calls)))
[email protected]e8077812012-02-03 03:41:46863 git_cl.main(['upload'] + upload_args)
864
[email protected]aebe87f2012-10-22 20:34:21865 def test_gerrit_upload_without_change_id(self):
[email protected]10625002016-03-04 20:03:47866 self.mock(git_cl, 'DownloadGerritHook', self._mocked_call)
[email protected]aebe87f2012-10-22 20:34:21867 self._run_gerrit_upload_test(
[email protected]e8077812012-02-03 03:41:46868 [],
[email protected]35d1a842012-07-27 00:20:43869 'desc\n\nBUG=\n',
[email protected]10625002016-03-04 20:03:47870 [],
871 post_amend_description='desc\n\nBUG=\n\nChange-Id: Ixxx')
[email protected]e8077812012-02-03 03:41:46872
[email protected]aebe87f2012-10-22 20:34:21873 def test_gerrit_no_reviewer(self):
874 self._run_gerrit_upload_test(
875 [],
[email protected]09d7a6a2016-03-04 15:44:48876 'desc\n\nBUG=\n\nChange-Id: I123456789\n',
[email protected]aebe87f2012-10-22 20:34:21877 [])
878
[email protected]bf766ba2016-04-13 12:51:23879 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
[email protected]e8077812012-02-03 03:41:46885 def test_gerrit_reviewers_cmd_line(self):
[email protected]aebe87f2012-10-22 20:34:21886 self._run_gerrit_upload_test(
[email protected]e8077812012-02-03 03:41:46887 ['-r', '[email protected]'],
[email protected]09d7a6a2016-03-04 15:44:48888 'desc\n\nBUG=\n\nChange-Id: I123456789',
[email protected]e8077812012-02-03 03:41:46889 ['[email protected]'])
890
891 def test_gerrit_reviewer_multiple(self):
[email protected]aebe87f2012-10-22 20:34:21892 self._run_gerrit_upload_test(
[email protected]e8077812012-02-03 03:41:46893 [],
[email protected]09d7a6a2016-03-04 15:44:48894 'desc\[email protected]\nBUG=\[email protected]\n\n'
895 'Change-Id: 123456789\n',
[email protected]e8077812012-02-03 03:41:46896 ['[email protected]', '[email protected]'])
897
[email protected]512d79c2016-03-31 12:55:28898 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']))
[email protected]27386dd2015-02-16 10:45:39904 self._run_gerrit_upload_test(
905 ['--squash'],
[email protected]512d79c2016-03-31 12:55:28906 'desc\nBUG=\n\nChange-Id: 123456789',
[email protected]27386dd2015-02-16 10:45:39907 [],
[email protected]609f3952015-05-04 22:47:04908 squash=True,
909 expected_upstream_ref='origin/master')
[email protected]e8077812012-02-03 03:41:46910
[email protected]512d79c2016-03-31 12:55:28911 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
[email protected]2dd99862015-06-22 12:22:18928 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'
[email protected]4c72b082016-03-31 22:26:35970 def IsGerrit(self):
971 return False
[email protected]2dd99862015-06-22 12:22:18972
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
[email protected]65874e12016-03-04 12:03:02978 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 <[email protected]> 1456848326 +0100'),
986 ((['git', 'var', 'GIT_COMMITTER_IDENT'], ),
987 'C D <[email protected]> 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
[email protected]78936cb2013-04-11 00:17:52994 def test_update_reviewers(self):
995 data = [
996 ('foo', [], 'foo'),
[email protected]42c20792013-09-12 17:34:49997 ('foo\nR=xx', [], 'foo\nR=xx'),
998 ('foo\nTBR=xx', [], 'foo\nTBR=xx'),
[email protected]78936cb2013-04-11 00:17:52999 ('foo', ['a@c'], 'foo\n\nR=a@c'),
[email protected]42c20792013-09-12 17:34:491000 ('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'),
[email protected]78936cb2013-04-11 00:17:521003 ('foo\nBUG=', ['a@c'], 'foo\nBUG=\nR=a@c'),
[email protected]42c20792013-09-12 17:34:491004 ('foo\nR=xx\nTBR=yy\nR=bar', ['a@c'], 'foo\n\nR=a@c, xx, bar\nTBR=yy'),
[email protected]78936cb2013-04-11 00:17:521005 ('foo', ['a@c', 'b@c'], 'foo\n\nR=a@c, b@c'),
[email protected]c6f60e82013-04-19 17:01:571006 ('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'),
[email protected]78936cb2013-04-11 00:17:521015 ]
[email protected]c6f60e82013-04-19 17:01:571016 expected = [i[2] for i in data]
1017 actual = []
1018 for orig, reviewers, _expected in data:
[email protected]78936cb2013-04-11 00:17:521019 obj = git_cl.ChangeDescription(orig)
1020 obj.update_reviewers(reviewers)
[email protected]c6f60e82013-04-19 17:01:571021 actual.append(obj.description)
1022 self.assertEqual(expected, actual)
[email protected]78936cb2013-04-11 00:17:521023
[email protected]455dc922015-01-26 20:15:501024 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))
[email protected]27386dd2015-02-16 10:45:391030
[email protected]455dc922015-01-26 20:15:501031 # 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))
[email protected]c68112d2015-03-03 12:48:061049 self.assertEqual('refs/heads/chrome/m42',
1050 git_cl.GetTargetRef('origin',
1051 'refs/remotes/origin/chrome/m42',
1052 None, None))
[email protected]455dc922015-01-26 20:15:501053
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
[email protected]a872e752015-04-28 23:42:181078 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)
[email protected]f86c7d32016-04-01 19:27:301081 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 ]
[email protected]a872e752015-04-28 23:42:181089 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
[email protected]dde64622016-04-13 17:11:211096 def _patch_common(self, is_gerrit=False, force_codereview=False):
[email protected]aa5ced12016-03-29 09:41:141097 self.mock(git_cl._RietveldChangelistImpl, 'GetMostRecentPatchset',
1098 lambda x: '60001')
1099 self.mock(git_cl._RietveldChangelistImpl, 'GetPatchSetDiff',
1100 lambda *args: None)
[email protected]f86c7d32016-04-01 19:27:301101 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 })
[email protected]aa5ced12016-03-29 09:41:141121 self.mock(git_cl.Changelist, 'GetDescription',
1122 lambda *args: 'Description')
[email protected]a872e752015-04-28 23:42:181123 self.mock(git_cl, 'IsGitVersionAtLeast', lambda *args: True)
1124
[email protected]dde64622016-04-13 17:11:211125 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'],), ''),
[email protected]f86c7d32016-04-01 19:27:301132 ]
1133 else:
[email protected]dde64622016-04-13 17:11:211134 self.calls = []
1135
1136 if is_gerrit:
1137 if not force_codereview:
1138 self.calls += [
1139 ((['git', 'config', 'gerrit.host'],), 'true'),
1140 ]
1141 else:
[email protected]f86c7d32016-04-01 19:27:301142 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 ]
[email protected]a872e752015-04-28 23:42:181148
1149 def test_patch_successful(self):
1150 self._patch_common()
1151 self.calls += [
1152 ((['git', 'apply', '--index', '-p0', '--3way'],), ''),
1153 ((['git', 'commit', '-m',
[email protected]5b3bebb2015-05-28 21:41:431154 'Description\n\n' +
[email protected]a872e752015-04-28 23:42:181155 'patch from issue 123456 at patchset 60001 ' +
1156 '(http://crrev.com/123456#ps60001)'],), ''),
[email protected]f86c7d32016-04-01 19:27:301157 ((['git', 'config', 'branch.master.rietveldissue', '123456'],), ''),
[email protected]aa5ced12016-03-29 09:41:141158 ((['git', 'config', 'branch.master.rietveldserver'],), ''),
[email protected]f86c7d32016-04-01 19:27:301159 ((['git', 'config', 'branch.master.rietveldserver',
1160 'https://codereview.example.com'],), ''),
1161 ((['git', 'config', 'branch.master.rietveldpatchset', '60001'],), ''),
[email protected]a872e752015-04-28 23:42:181162 ]
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)
[email protected]455dc922015-01-26 20:15:501172
[email protected]f86c7d32016-04-01 19:27:301173 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
[email protected]dde64622016-04-13 17:11:211191 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
[email protected]f86c7d32016-04-01 19:27:301210 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
[email protected]5df290f2016-04-11 16:12:291242 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
[email protected]26c8fd22016-04-11 21:33:211271 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
[email protected]fe30f182016-04-13 12:15:041284 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
[email protected]fa330e82016-04-13 17:09:521328 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 ]
[email protected]1a8ef442016-04-13 18:41:371356 # TODO(tandrii): consider testing just set-commit and set-commit --clear,
1357 # but without copy-pasting tons of expectations, as modifying them later is
1358 # super tedious.
[email protected]fa330e82016-04-13 17:09:521359 self.assertEqual(0, git_cl.main(['set-commit', '-d']))
1360
[email protected]f86c7d32016-04-01 19:27:301361
[email protected]ddd59412011-11-30 14:20:381362if __name__ == '__main__':
[email protected]78936cb2013-04-11 00:17:521363 git_cl.logging.basicConfig(
1364 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR)
[email protected]ddd59412011-11-30 14:20:381365 unittest.main()