blob: afcaf6d3772f82c0df68762b7dc103cffa21fa15 [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
martiniss@chromium.orgd6648e22016-04-29 19:22:16 +000024class ChangelistMock(object):
25 # A class variable so we can access it when we don't have access to the
26 # instance that's being set.
27 desc = ""
28 def __init__(self, **kwargs):
29 pass
30 def GetIssue(self):
31 return 1
32 def GetDescription(self):
33 return ChangelistMock.desc
34 def UpdateDescription(self, desc):
35 ChangelistMock.desc = desc
36
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000037class PresubmitMock(object):
38 def __init__(self, *args, **kwargs):
39 self.reviewers = []
40 @staticmethod
41 def should_continue():
42 return True
43
44
45class RietveldMock(object):
46 def __init__(self, *args, **kwargs):
47 pass
maruel@chromium.org78936cb2013-04-11 00:17:52 +000048
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000049 @staticmethod
50 def get_description(issue):
51 return 'Issue: %d' % issue
52
maruel@chromium.org78936cb2013-04-11 00:17:52 +000053 @staticmethod
54 def get_issue_properties(_issue, _messages):
55 return {
56 'reviewers': ['joe@chromium.org', 'john@chromium.org'],
57 'messages': [
58 {
59 'approval': True,
60 'sender': 'john@chromium.org',
61 },
62 ],
63 }
64
iannuccie53c9352016-08-17 14:40:40 -070065 @staticmethod
66 def close_issue(_issue):
67 return 'Closed'
68
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000069
70class WatchlistsMock(object):
71 def __init__(self, _):
72 pass
73 @staticmethod
74 def GetWatchersForPaths(_):
75 return ['joe@example.com']
76
77
ukai@chromium.org78c4b982012-02-14 02:20:26 +000078class CodereviewSettingsFileMock(object):
79 def __init__(self):
80 pass
81 # pylint: disable=R0201
82 def read(self):
83 return ("CODE_REVIEW_SERVER: gerrit.chromium.org\n" +
andybons@chromium.org11f46eb2016-02-02 19:26:51 +000084 "GERRIT_HOST: True\n")
ukai@chromium.org78c4b982012-02-14 02:20:26 +000085
86
vadimsh@chromium.orgeed4df32015-04-10 21:30:20 +000087class AuthenticatorMock(object):
88 def __init__(self, *_args):
89 pass
90 def has_cached_credentials(self):
91 return True
92
93
tandrii@chromium.orgfe30f182016-04-13 12:15:04 +000094def CookiesAuthenticatorMockFactory(hosts_with_creds=None, same_cookie=False):
95 """Use to mock Gerrit/Git credentials from ~/.netrc or ~/.gitcookies.
96
97 Usage:
98 >>> self.mock(git_cl.gerrit_util, "CookiesAuthenticator",
99 CookiesAuthenticatorMockFactory({'host1': 'cookie1'}))
100
101 OR
102 >>> self.mock(git_cl.gerrit_util, "CookiesAuthenticator",
103 CookiesAuthenticatorMockFactory(cookie='cookie'))
104 """
105 class CookiesAuthenticatorMock(git_cl.gerrit_util.CookiesAuthenticator):
106 def __init__(self): # pylint: disable=W0231
107 # Intentionally not calling super() because it reads actual cookie files.
108 pass
109 @classmethod
110 def get_gitcookies_path(cls):
111 return '~/.gitcookies'
112 @classmethod
113 def get_netrc_path(cls):
114 return '~/.netrc'
115 def get_auth_header(self, host):
116 if same_cookie:
117 return same_cookie
118 return (hosts_with_creds or {}).get(host)
119 return CookiesAuthenticatorMock
120
121
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +0000122class TestGitClBasic(unittest.TestCase):
123 def _test_ParseIssueUrl(self, func, url, issue, patchset, hostname, fail):
124 parsed = urlparse.urlparse(url)
125 result = func(parsed)
126 if fail:
127 self.assertIsNone(result)
128 return None
129 self.assertIsNotNone(result)
130 self.assertEqual(result.issue, issue)
131 self.assertEqual(result.patchset, patchset)
132 self.assertEqual(result.hostname, hostname)
133 return result
134
135 def test_ParseIssueURL_rietveld(self):
136 def test(url, issue=None, patchset=None, hostname=None, patch_url=None,
137 fail=None):
138 result = self._test_ParseIssueUrl(
139 git_cl._RietveldChangelistImpl.ParseIssueURL,
140 url, issue, patchset, hostname, fail)
141 if not fail:
142 self.assertEqual(result.patch_url, patch_url)
143
144 test('http://codereview.chromium.org/123',
145 123, None, 'codereview.chromium.org')
146 test('https://codereview.chromium.org/123',
147 123, None, 'codereview.chromium.org')
148 test('https://codereview.chromium.org/123/',
149 123, None, 'codereview.chromium.org')
150 test('https://codereview.chromium.org/123/whatever',
151 123, None, 'codereview.chromium.org')
wychen3c1c1722016-08-04 11:46:36 -0700152 test('https://codereview.chromium.org/123/#ps20001',
153 123, 20001, 'codereview.chromium.org')
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +0000154 test('http://codereview.chromium.org/download/issue123_4.diff',
155 123, 4, 'codereview.chromium.org',
156 patch_url='https://codereview.chromium.org/download/issue123_4.diff')
157 # This looks like bad Gerrit, but is actually valid Rietveld.
158 test('https://chrome-review.source.com/123/4/',
159 123, None, 'chrome-review.source.com')
160
161 test('https://codereview.chromium.org/deadbeaf', fail=True)
162 test('https://codereview.chromium.org/api/123', fail=True)
163 test('bad://codereview.chromium.org/123', fail=True)
164 test('http://codereview.chromium.org/download/issue123_4.diffff', fail=True)
165
166 def test_ParseIssueURL_gerrit(self):
167 def test(url, issue=None, patchset=None, hostname=None, fail=None):
168 self._test_ParseIssueUrl(
169 git_cl._GerritChangelistImpl.ParseIssueURL,
170 url, issue, patchset, hostname, fail)
171
172 test('http://chrome-review.source.com/c/123',
173 123, None, 'chrome-review.source.com')
174 test('https://chrome-review.source.com/c/123/',
175 123, None, 'chrome-review.source.com')
176 test('https://chrome-review.source.com/c/123/4',
177 123, 4, 'chrome-review.source.com')
178 test('https://chrome-review.source.com/#/c/123/4',
179 123, 4, 'chrome-review.source.com')
180 test('https://chrome-review.source.com/c/123/4',
181 123, 4, 'chrome-review.source.com')
182 test('https://chrome-review.source.com/123',
183 123, None, 'chrome-review.source.com')
184 test('https://chrome-review.source.com/123/4',
185 123, 4, 'chrome-review.source.com')
186
187 test('https://chrome-review.source.com/c/123/1/whatisthis', fail=True)
188 test('https://chrome-review.source.com/c/abc/', fail=True)
189 test('ssh://chrome-review.source.com/c/123/1/', fail=True)
190
191 def test_ParseIssueNumberArgument(self):
192 def test(arg, issue=None, patchset=None, hostname=None, fail=False):
193 result = git_cl.ParseIssueNumberArgument(arg)
194 self.assertIsNotNone(result)
195 if fail:
196 self.assertFalse(result.valid)
197 else:
198 self.assertEqual(result.issue, issue)
199 self.assertEqual(result.patchset, patchset)
200 self.assertEqual(result.hostname, hostname)
201
202 test('123', 123)
203 test('', fail=True)
204 test('abc', fail=True)
205 test('123/1', fail=True)
206 test('123a', fail=True)
207 test('ssh://chrome-review.source.com/#/c/123/4/', fail=True)
208 # Rietveld.
209 test('https://codereview.source.com/123',
210 123, None, 'codereview.source.com')
211 test('https://codereview.source.com/www123', fail=True)
212 # Gerrrit.
213 test('https://chrome-review.source.com/c/123/4',
214 123, 4, 'chrome-review.source.com')
215 test('https://chrome-review.source.com/bad/123/4', fail=True)
216
tandriif9aefb72016-07-01 09:06:51 -0700217 def test_get_bug_line_values(self):
218 f = lambda p, bugs: list(git_cl._get_bug_line_values(p, bugs))
219 self.assertEqual(f('', ''), [])
220 self.assertEqual(f('', '123,v8:456'), ['123', 'v8:456'])
221 self.assertEqual(f('v8', '456'), ['v8:456'])
222 self.assertEqual(f('v8', 'chromium:123,456'), ['v8:456', 'chromium:123'])
223 # Not nice, but not worth carying.
224 self.assertEqual(f('v8', 'chromium:123,456,v8:123'),
225 ['v8:456', 'chromium:123', 'v8:123'])
226
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +0000227
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000228class TestGitCl(TestCase):
229 def setUp(self):
230 super(TestGitCl, self).setUp()
231 self.calls = []
tandrii9d206752016-06-20 11:32:47 -0700232 self._calls_done = []
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000233 self.mock(subprocess2, 'call', self._mocked_call)
234 self.mock(subprocess2, 'check_call', self._mocked_call)
235 self.mock(subprocess2, 'check_output', self._mocked_call)
236 self.mock(subprocess2, 'communicate', self._mocked_call)
tandrii@chromium.orga342c922016-03-16 07:08:25 +0000237 self.mock(git_cl.gclient_utils, 'CheckCallAndFilter', self._mocked_call)
sbc@chromium.org71437c02015-04-09 19:29:40 +0000238 self.mock(git_common, 'is_dirty_git_tree', lambda x: False)
iannucci@chromium.org9e849272014-04-04 00:31:55 +0000239 self.mock(git_common, 'get_or_create_merge_base',
240 lambda *a: (
241 self._mocked_call(['get_or_create_merge_base']+list(a))))
pgervais@chromium.org8ba38ff2015-06-11 21:41:25 +0000242 self.mock(git_cl, 'BranchExists', lambda _: True)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000243 self.mock(git_cl, 'FindCodereviewSettingsFile', lambda: '')
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000244 self.mock(git_cl, 'ask_for_data', self._mocked_call)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000245 self.mock(git_cl.presubmit_support, 'DoPresubmitChecks', PresubmitMock)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000246 self.mock(git_cl.rietveld, 'Rietveld', RietveldMock)
maruel@chromium.org4bac4b52012-11-27 20:33:52 +0000247 self.mock(git_cl.rietveld, 'CachingRietveld', RietveldMock)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000248 self.mock(git_cl.upload, 'RealMain', self.fail)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000249 self.mock(git_cl.watchlists, 'Watchlists', WatchlistsMock)
vadimsh@chromium.orgeed4df32015-04-10 21:30:20 +0000250 self.mock(git_cl.auth, 'get_authenticator_for_host', AuthenticatorMock)
tandrii@chromium.orgfe30f182016-04-13 12:15:04 +0000251 self.mock(git_cl.gerrit_util.GceAuthenticator, 'is_gce',
252 classmethod(lambda _: False))
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000253 # It's important to reset settings to not have inter-tests interference.
254 git_cl.settings = None
255
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +0000256
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000257 def tearDown(self):
wychen@chromium.org445c8962015-04-28 23:30:05 +0000258 try:
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +0000259 # Note: has_failed returns True if at least 1 test ran so far, current
260 # included, has failed. That means current test may have actually ran
261 # fine, and the check for no leftover calls would be skipped.
wychen@chromium.org445c8962015-04-28 23:30:05 +0000262 if not self.has_failed():
263 self.assertEquals([], self.calls)
264 finally:
265 super(TestGitCl, self).tearDown()
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000266
iannucci@chromium.org9e849272014-04-04 00:31:55 +0000267 def _mocked_call(self, *args, **_kwargs):
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000268 self.assertTrue(
269 self.calls,
tandrii9d206752016-06-20 11:32:47 -0700270 '@%d Expected: <Missing> Actual: %r' % (len(self._calls_done), args))
wychen@chromium.orga872e752015-04-28 23:42:18 +0000271 top = self.calls.pop(0)
272 if len(top) > 2 and top[2]:
273 raise top[2]
274 expected_args, result = top
275
maruel@chromium.orge52678e2013-04-26 18:34:44 +0000276 # Also logs otherwise it could get caught in a try/finally and be hard to
277 # diagnose.
278 if expected_args != args:
tandrii9d206752016-06-20 11:32:47 -0700279 N = 5
280 prior_calls = '\n '.join(
281 '@%d: %r' % (len(self._calls_done) - N + i, c[0])
282 for i, c in enumerate(self._calls_done[-N:]))
283 following_calls = '\n '.join(
284 '@%d: %r' % (len(self._calls_done) + i + 1, c[0])
285 for i, c in enumerate(self.calls[:N]))
286 extended_msg = (
287 'A few prior calls:\n %s\n\n'
288 'This (expected):\n @%d: %r\n'
289 'This (actual):\n @%d: %r\n\n'
290 'A few following expected calls:\n %s' %
291 (prior_calls, len(self._calls_done), expected_args,
292 len(self._calls_done), args, following_calls))
293 git_cl.logging.error(extended_msg)
294
tandrii99a72f22016-08-17 14:33:24 -0700295 self.fail('@%d\n'
296 ' Expected: %r\n'
297 ' Actual: %r' % (
tandrii9d206752016-06-20 11:32:47 -0700298 len(self._calls_done), expected_args, args))
299
300 self._calls_done.append(top)
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000301 return result
302
maruel@chromium.orga3353652011-11-30 14:26:57 +0000303 @classmethod
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000304 def _is_gerrit_calls(cls, gerrit=False):
305 return [((['git', 'config', 'rietveld.autoupdate'],), ''),
306 ((['git', 'config', 'gerrit.host'],), 'True' if gerrit else '')]
307
308 @classmethod
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000309 def _upload_calls(cls, similarity, find_copies, private):
iannucci@chromium.org79540052012-10-19 23:15:26 +0000310 return (cls._git_base_calls(similarity, find_copies) +
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000311 cls._git_upload_calls(private))
maruel@chromium.orga3353652011-11-30 14:26:57 +0000312
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000313 @classmethod
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000314 def _upload_no_rev_calls(cls, similarity, find_copies):
315 return (cls._git_base_calls(similarity, find_copies) +
316 cls._git_upload_no_rev_calls())
317
318 @classmethod
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000319 def _git_base_calls(cls, similarity, find_copies):
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000320 if similarity is None:
321 similarity = '50'
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000322 similarity_call = ((['git', 'config', '--int', '--get',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000323 'branch.master.git-cl-similarity'],), '')
324 else:
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000325 similarity_call = ((['git', 'config', '--int',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000326 'branch.master.git-cl-similarity', similarity],), '')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000327
328 if find_copies is None:
329 find_copies = True
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000330 find_copies_call = ((['git', 'config', '--int', '--get',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000331 'branch.master.git-find-copies'],), '')
332 else:
333 val = str(int(find_copies))
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000334 find_copies_call = ((['git', 'config', '--int',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000335 'branch.master.git-find-copies', val],), '')
336
337 if find_copies:
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000338 stat_call = ((['git', 'diff', '--no-ext-diff', '--stat',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000339 '--find-copies-harder', '-l100000', '-C'+similarity,
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000340 'fake_ancestor_sha', 'HEAD'],), '+dat')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000341 else:
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000342 stat_call = ((['git', 'diff', '--no-ext-diff', '--stat',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000343 '-M'+similarity, 'fake_ancestor_sha', 'HEAD'],), '+dat')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000344
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000345 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000346 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
tandrii@chromium.org87985d22016-03-24 17:33:33 +0000347 similarity_call,
348 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
349 find_copies_call,
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000350 ] + cls._is_gerrit_calls() + [
tandrii@chromium.org87985d22016-03-24 17:33:33 +0000351 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +0000352 ((['git', 'config', 'branch.master.rietveldissue'],), ''),
353 ((['git', 'config', 'branch.master.gerritissue'],), ''),
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +0000354 ((['git', 'config', 'rietveld.server'],),
355 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000356 ((['git', 'config', 'branch.master.merge'],), 'master'),
357 ((['git', 'config', 'branch.master.remote'],), 'origin'),
iannucci@chromium.org9e849272014-04-04 00:31:55 +0000358 ((['get_or_create_merge_base', 'master', 'master'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000359 'fake_ancestor_sha'),
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000360 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000361 ((['git', 'rev-parse', '--show-cdup'],), ''),
362 ((['git', 'rev-parse', 'HEAD'],), '12345'),
363 ((['git', 'diff', '--name-status', '--no-renames', '-r',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000364 'fake_ancestor_sha...', '.'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000365 'M\t.gitignore\n'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000366 ((['git', 'config', 'branch.master.rietveldpatchset'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000367 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000368 ((['git', 'log', '--pretty=format:%s%n%n%b',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000369 'fake_ancestor_sha...'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000370 'foo'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000371 ((['git', 'config', 'user.email'],), 'me@example.com'),
iannucci@chromium.org79540052012-10-19 23:15:26 +0000372 stat_call,
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000373 ((['git', 'log', '--pretty=format:%s\n\n%b',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000374 'fake_ancestor_sha..HEAD'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000375 'desc\n'),
rmistry@google.com90752582014-01-14 21:04:50 +0000376 ((['git', 'config', 'rietveld.bug-prefix'],), ''),
maruel@chromium.orga3353652011-11-30 14:26:57 +0000377 ]
378
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000379 @classmethod
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000380 def _git_upload_no_rev_calls(cls):
381 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000382 ((['git', 'config', 'core.editor'],), ''),
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000383 ]
384
385 @classmethod
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000386 def _git_upload_calls(cls, private):
387 if private:
tyoshino@chromium.org99918ab2013-09-30 06:17:28 +0000388 cc_call = []
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000389 private_call = []
390 else:
tyoshino@chromium.org99918ab2013-09-30 06:17:28 +0000391 cc_call = [((['git', 'config', 'rietveld.cc'],), '')]
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000392 private_call = [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000393 ((['git', 'config', 'rietveld.private'],), '')]
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000394
maruel@chromium.orga3353652011-11-30 14:26:57 +0000395 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000396 ((['git', 'config', 'core.editor'],), ''),
tyoshino@chromium.org99918ab2013-09-30 06:17:28 +0000397 ] + cc_call + private_call + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000398 ((['git', 'config', 'branch.master.base-url'],), ''),
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +0000399 ((['git', 'config', 'rietveld.pending-ref-prefix'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000400 ((['git',
bratell@opera.com05fb9112014-07-07 09:30:23 +0000401 'config', '--local', '--get-regexp', '^svn-remote\\.'],),
402 (('', None), 0)),
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000403 ((['git', 'rev-parse', '--show-cdup'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000404 ((['git', 'svn', 'info'],), ''),
sheyang@chromium.org152cf832014-06-11 21:37:49 +0000405 ((['git', 'config', 'rietveld.project'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000406 ((['git',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000407 'config', 'branch.master.rietveldissue', '1'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000408 ((['git', 'config', 'branch.master.rietveldserver',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000409 'https://codereview.example.com'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000410 ((['git',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000411 'config', 'branch.master.rietveldpatchset', '2'],), ''),
tandrii@chromium.org1e67bb72016-02-11 12:15:49 +0000412 ] + cls._git_post_upload_calls()
413
414 @classmethod
415 def _git_post_upload_calls(cls):
416 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000417 ((['git', 'rev-parse', 'HEAD'],), 'hash'),
418 ((['git', 'symbolic-ref', 'HEAD'],), 'hash'),
419 ((['git',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000420 'config', 'branch.hash.last-upload-hash', 'hash'],), ''),
rmistry@google.com5626a922015-02-26 14:03:30 +0000421 ((['git', 'config', 'rietveld.run-post-upload-hook'],), ''),
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000422 ]
423
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000424 @staticmethod
tandrii@chromium.orgfe30f182016-04-13 12:15:04 +0000425 def _git_sanity_checks(diff_base, working_branch, get_remote_branch=True):
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000426 fake_ancestor = 'fake_ancestor'
427 fake_cl = 'fake_cl_for_patch'
428 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000429 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000430 'rev-parse', '--verify', diff_base],), fake_ancestor),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000431 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000432 'merge-base', fake_ancestor, 'HEAD'],), fake_ancestor),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000433 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000434 'rev-list', '^' + fake_ancestor, 'HEAD'],), fake_cl),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000435 # Mock a config miss (error code 1)
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000436 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000437 'config', 'gitcl.remotebranch'],), (('', None), 1)),
tandrii@chromium.orgfe30f182016-04-13 12:15:04 +0000438 ] + ([
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000439 # Call to GetRemoteBranch()
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000440 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000441 'config', 'branch.%s.merge' % working_branch],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000442 'refs/heads/master'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000443 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000444 'config', 'branch.%s.remote' % working_branch],), 'origin'),
tandrii@chromium.orgfe30f182016-04-13 12:15:04 +0000445 ] if get_remote_branch else []) + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000446 ((['git', 'rev-list', '^' + fake_ancestor,
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000447 'refs/remotes/origin/master'],), ''),
tandrii@chromium.orgfe30f182016-04-13 12:15:04 +0000448 ]
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000449
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000450 @classmethod
451 def _dcommit_calls_1(cls):
452 return [
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +0000453 ((['git', 'config', 'rietveld.autoupdate'],),
454 ''),
455 ((['git', 'config', 'rietveld.pending-ref-prefix'],),
456 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000457 ((['git',
bratell@opera.com05fb9112014-07-07 09:30:23 +0000458 'config', '--local', '--get-regexp', '^svn-remote\\.'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000459 ((('svn-remote.svn.url svn://svn.chromium.org/chrome\n'
460 'svn-remote.svn.fetch trunk/src:refs/remotes/origin/master'),
461 None),
462 0)),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000463 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
464 ((['git', 'config', '--int', '--get',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000465 'branch.working.git-cl-similarity'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000466 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
467 ((['git', 'config', '--int', '--get',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000468 'branch.working.git-find-copies'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000469 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
470 ((['git',
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +0000471 'config', 'branch.working.rietveldissue'],), '12345'),
472 ((['git',
473 'config', 'rietveld.server'],), 'codereview.example.com'),
474 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000475 'config', 'branch.working.merge'],), 'refs/heads/master'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000476 ((['git', 'config', 'branch.working.remote'],), 'origin'),
iannucci@chromium.org5724c962014-04-11 09:32:56 +0000477 ((['git', 'config', 'branch.working.merge'],),
478 'refs/heads/master'),
479 ((['git', 'config', 'branch.working.remote'],), 'origin'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000480 ((['git', 'rev-list', '--merges',
szager@chromium.orge84b7542012-06-15 21:26:58 +0000481 '--grep=^SVN changes up to revision [0-9]*$',
szager@chromium.org9bb85e22012-06-13 20:28:23 +0000482 'refs/remotes/origin/master^!'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000483 ((['git', 'rev-list', '^refs/heads/working',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000484 'refs/remotes/origin/master'],),
485 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000486 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000487 'log', '--grep=^git-svn-id:', '-1', '--pretty=format:%H'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000488 '3fc18b62c4966193eb435baabe2d18a3810ec82e'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000489 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000490 'rev-list', '^3fc18b62c4966193eb435baabe2d18a3810ec82e',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000491 'refs/remotes/origin/master'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000492 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000493 'merge-base', 'refs/remotes/origin/master', 'HEAD'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000494 'fake_ancestor_sha'),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000495 ]
496
497 @classmethod
498 def _dcommit_calls_normal(cls):
499 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000500 ((['git', 'rev-parse', '--show-cdup'],), ''),
501 ((['git', 'rev-parse', 'HEAD'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000502 '00ff397798ea57439712ed7e04ab96e13969ef40'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000503 ((['git',
mcgrathr@chromium.org9249f642013-06-03 21:36:18 +0000504 'diff', '--name-status', '--no-renames', '-r', 'fake_ancestor_sha...',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000505 '.'],),
506 'M\tPRESUBMIT.py'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000507 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000508 'config', 'branch.working.rietveldpatchset'],), '31137'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000509 ((['git', 'config', 'branch.working.rietveldserver'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000510 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000511 ((['git', 'config', 'user.email'],), 'author@example.com'),
512 ((['git', 'config', 'rietveld.tree-status-url'],), ''),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000513 ]
514
515 @classmethod
516 def _dcommit_calls_bypassed(cls):
517 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000518 ((['git', 'config', 'branch.working.rietveldserver'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000519 'codereview.example.com'),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000520 ]
521
522 @classmethod
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000523 def _dcommit_calls_3(cls):
524 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000525 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000526 'diff', '--no-ext-diff', '--stat', '--find-copies-harder',
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000527 '-l100000', '-C50', 'fake_ancestor_sha',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000528 'refs/heads/working'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000529 (' PRESUBMIT.py | 2 +-\n'
530 ' 1 files changed, 1 insertions(+), 1 deletions(-)\n')),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000531 ((['git', 'show-ref', '--quiet', '--verify',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000532 'refs/heads/git-cl-commit'],),
533 (('', None), 0)),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000534 ((['git', 'branch', '-D', 'git-cl-commit'],), ''),
535 ((['git', 'show-ref', '--quiet', '--verify',
szager@chromium.org9bb85e22012-06-13 20:28:23 +0000536 'refs/heads/git-cl-cherry-pick'],), ''),
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000537 ((['git', 'rev-parse', '--show-cdup'],), '\n'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000538 ((['git', 'checkout', '-q', '-b', 'git-cl-commit'],), ''),
539 ((['git', 'reset', '--soft', 'fake_ancestor_sha'],), ''),
540 ((['git', 'commit', '-m',
maruel@chromium.orge52678e2013-04-26 18:34:44 +0000541 'Issue: 12345\n\nR=john@chromium.org\n\n'
sergiyb@chromium.org4b39c5f2015-07-07 10:33:12 +0000542 'Review URL: https://codereview.example.com/12345 .'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000543 ''),
kjellander@chromium.org6abc6522014-12-02 07:34:49 +0000544 ((['git', 'config', 'rietveld.force-https-commit-url'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000545 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000546 'svn', 'dcommit', '-C50', '--no-rebase', '--rmdir'],),
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000547 (('', None), 0)),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000548 ((['git', 'checkout', '-q', 'working'],), ''),
549 ((['git', 'branch', '-D', 'git-cl-commit'],), ''),
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000550 ]
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000551
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000552 @staticmethod
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000553 def _cmd_line(description, args, similarity, find_copies, private):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000554 """Returns the upload command line passed to upload.RealMain()."""
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000555 return [
556 'upload', '--assume_yes', '--server',
maruel@chromium.orgeb5edbc2012-01-16 17:03:28 +0000557 'https://codereview.example.com',
maruel@chromium.org71e12a92012-02-14 02:34:15 +0000558 '--message', description
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000559 ] + args + [
560 '--cc', 'joe@example.com',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000561 ] + (['--private'] if private else []) + [
iannucci@chromium.org79540052012-10-19 23:15:26 +0000562 '--git_similarity', similarity or '50'
563 ] + (['--git_no_find_copies'] if find_copies == False else []) + [
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000564 'fake_ancestor_sha', 'HEAD'
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000565 ]
566
567 def _run_reviewer_test(
568 self,
569 upload_args,
570 expected_description,
571 returned_description,
572 final_description,
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000573 reviewers,
574 private=False):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000575 """Generic reviewer test framework."""
tandrii@chromium.org28253532016-04-14 13:46:56 +0000576 self.mock(git_cl.sys, 'stdout', StringIO.StringIO())
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000577 try:
578 similarity = upload_args[upload_args.index('--similarity')+1]
579 except ValueError:
580 similarity = None
iannucci@chromium.org79540052012-10-19 23:15:26 +0000581
582 if '--find-copies' in upload_args:
583 find_copies = True
584 elif '--no-find-copies' in upload_args:
585 find_copies = False
586 else:
587 find_copies = None
588
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000589 private = '--private' in upload_args
590
591 self.calls = self._upload_calls(similarity, find_copies, private)
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000592
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000593 def RunEditor(desc, _, **kwargs):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000594 self.assertEquals(
595 '# Enter a description of the change.\n'
janx@chromium.org104b2db2013-04-18 12:58:40 +0000596 '# This will be displayed on the codereview site.\n'
alancutter@chromium.org63a4d7f2013-05-31 02:22:45 +0000597 '# The first line will also be used as the subject of the review.\n'
alancutter@chromium.orgbd1073e2013-06-01 00:34:38 +0000598 '#--------------------This line is 72 characters long'
599 '--------------------\n' +
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000600 expected_description,
601 desc)
602 return returned_description
603 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor)
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000604
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000605 def check_upload(args):
iannucci@chromium.org79540052012-10-19 23:15:26 +0000606 cmd_line = self._cmd_line(final_description, reviewers, similarity,
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000607 find_copies, private)
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000608 self.assertEquals(cmd_line, args)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000609 return 1, 2
610 self.mock(git_cl.upload, 'RealMain', check_upload)
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000611
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000612 git_cl.main(['upload'] + upload_args)
613
614 def test_no_reviewer(self):
615 self._run_reviewer_test(
616 [],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000617 'desc\n\nBUG=',
618 '# Blah blah comment.\ndesc\n\nBUG=',
619 'desc\n\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000620 [])
621
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000622 def test_keep_similarity(self):
623 self._run_reviewer_test(
624 ['--similarity', '70'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000625 'desc\n\nBUG=',
626 '# Blah blah comment.\ndesc\n\nBUG=',
627 'desc\n\nBUG=',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000628 [])
629
iannucci@chromium.org79540052012-10-19 23:15:26 +0000630 def test_keep_find_copies(self):
631 self._run_reviewer_test(
632 ['--no-find-copies'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000633 'desc\n\nBUG=',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000634 '# Blah blah comment.\ndesc\n\nBUG=\n',
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000635 'desc\n\nBUG=',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000636 [])
637
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000638 def test_private(self):
639 self._run_reviewer_test(
640 ['--private'],
641 'desc\n\nBUG=',
642 '# Blah blah comment.\ndesc\n\nBUG=\n',
643 'desc\n\nBUG=',
644 [])
645
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000646 def test_reviewers_cmd_line(self):
647 # Reviewer is passed as-is
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000648 description = 'desc\n\nR=foo@example.com\nBUG='
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000649 self._run_reviewer_test(
650 ['-r' 'foo@example.com'],
651 description,
652 '\n%s\n' % description,
653 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000654 ['--reviewers=foo@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000655
656 def test_reviewer_tbr_overriden(self):
657 # Reviewer is overriden with TBR
658 # Also verifies the regexp work without a trailing LF
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000659 description = 'Foo Bar\n\nTBR=reviewer@example.com'
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000660 self._run_reviewer_test(
661 ['-r' 'foo@example.com'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000662 'desc\n\nR=foo@example.com\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000663 description.strip('\n'),
664 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000665 ['--reviewers=reviewer@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000666
667 def test_reviewer_multiple(self):
668 # Handles multiple R= or TBR= lines.
669 description = (
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000670 'Foo Bar\nTBR=reviewer@example.com\nBUG=\nR=another@example.com')
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000671 self._run_reviewer_test(
672 [],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000673 'desc\n\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000674 description,
675 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000676 ['--reviewers=another@example.com,reviewer@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000677
maruel@chromium.orga3353652011-11-30 14:26:57 +0000678 def test_reviewer_send_mail(self):
679 # --send-mail can be used without -r if R= is used
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000680 description = 'Foo Bar\nR=reviewer@example.com'
maruel@chromium.orga3353652011-11-30 14:26:57 +0000681 self._run_reviewer_test(
682 ['--send-mail'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000683 'desc\n\nBUG=',
maruel@chromium.orga3353652011-11-30 14:26:57 +0000684 description.strip('\n'),
685 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000686 ['--reviewers=reviewer@example.com', '--send_mail'])
maruel@chromium.orga3353652011-11-30 14:26:57 +0000687
688 def test_reviewer_send_mail_no_rev(self):
689 # Fails without a reviewer.
maruel@chromium.org2e23ce32013-05-07 12:42:28 +0000690 stdout = StringIO.StringIO()
691 stderr = StringIO.StringIO()
maruel@chromium.orga3353652011-11-30 14:26:57 +0000692 try:
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000693 self.calls = self._upload_no_rev_calls(None, None)
694 def RunEditor(desc, _, **kwargs):
maruel@chromium.orga3353652011-11-30 14:26:57 +0000695 return desc
696 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor)
maruel@chromium.org2e23ce32013-05-07 12:42:28 +0000697 self.mock(sys, 'stdout', stdout)
698 self.mock(sys, 'stderr', stderr)
maruel@chromium.orga3353652011-11-30 14:26:57 +0000699 git_cl.main(['upload', '--send-mail'])
700 self.fail()
701 except SystemExit:
maruel@chromium.org2e23ce32013-05-07 12:42:28 +0000702 self.assertEqual(
703 'Using 50% similarity for rename/copy detection. Override with '
704 '--similarity.\n',
705 stdout.getvalue())
706 self.assertEqual(
707 'Must specify reviewers to send email.\n', stderr.getvalue())
maruel@chromium.orga3353652011-11-30 14:26:57 +0000708
tandriif9aefb72016-07-01 09:06:51 -0700709 def test_bug_on_cmd(self):
710 self._run_reviewer_test(
711 ['--bug=500658,proj:123'],
712 'desc\n\nBUG=500658\nBUG=proj:123',
713 '# Blah blah comment.\ndesc\n\nBUG=500658\nBUG=proj:1234',
714 'desc\n\nBUG=500658\nBUG=proj:1234',
715 [])
716
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000717 def test_dcommit(self):
tandrii@chromium.org28253532016-04-14 13:46:56 +0000718 self.mock(git_cl.sys, 'stdout', StringIO.StringIO())
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000719 self.calls = (
720 self._dcommit_calls_1() +
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000721 self._git_sanity_checks('fake_ancestor_sha', 'working') +
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000722 self._dcommit_calls_normal() +
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000723 self._dcommit_calls_3())
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000724 git_cl.main(['dcommit'])
725
726 def test_dcommit_bypass_hooks(self):
tandrii@chromium.org28253532016-04-14 13:46:56 +0000727 self.mock(git_cl.sys, 'stdout', StringIO.StringIO())
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000728 self.calls = (
729 self._dcommit_calls_1() +
730 self._dcommit_calls_bypassed() +
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000731 self._dcommit_calls_3())
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000732 git_cl.main(['dcommit', '--bypass-hooks'])
733
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000734
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000735 @classmethod
tandrii@chromium.org28253532016-04-14 13:46:56 +0000736 def _gerrit_ensure_auth_calls(cls, issue=None, skip_auth_check=False):
shinyak@chromium.org00dbccd2016-04-15 07:24:43 +0000737 cmd = ['git', 'config', '--bool', 'gerrit.skip-ensure-authenticated']
tandrii@chromium.org28253532016-04-14 13:46:56 +0000738 if skip_auth_check:
739 return [((cmd, ), 'true')]
740
741 calls = [((cmd, ), '', subprocess2.CalledProcessError(1, '', '', '', ''))]
tandrii@chromium.orgfe30f182016-04-13 12:15:04 +0000742 if issue:
743 calls.extend([
744 ((['git', 'config', 'branch.master.gerritserver'],), ''),
745 ])
746 calls.extend([
747 ((['git', 'config', 'branch.master.merge'],), 'refs/heads/master'),
748 ((['git', 'config', 'branch.master.remote'],), 'origin'),
749 ((['git', 'config', 'remote.origin.url'],),
750 'https://chromium.googlesource.com/my/repo'),
751 ((['git', 'config', 'remote.origin.url'],),
752 'https://chromium.googlesource.com/my/repo'),
753 ])
754 return calls
755
756 @classmethod
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000757 def _gerrit_base_calls(cls, issue=None):
ukai@chromium.orge8077812012-02-03 03:41:46 +0000758 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000759 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
760 ((['git', 'config', '--int', '--get',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000761 'branch.master.git-cl-similarity'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000762 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
763 ((['git', 'config', '--int', '--get',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000764 'branch.master.git-find-copies'],), ''),
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000765 ] + cls._is_gerrit_calls(True) + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000766 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +0000767 ((['git', 'config', 'branch.master.rietveldissue'],), ''),
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000768 ((['git', 'config', 'branch.master.gerritissue'],),
769 '' if issue is None else str(issue)),
tandrii@chromium.orgfe30f182016-04-13 12:15:04 +0000770 ((['git', 'config', 'branch.master.merge'],), 'refs/heads/master'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000771 ((['git', 'config', 'branch.master.remote'],), 'origin'),
tandrii@chromium.orgfe30f182016-04-13 12:15:04 +0000772 ((['get_or_create_merge_base', 'master',
773 'refs/remotes/origin/master'],),
iannucci@chromium.org9e849272014-04-04 00:31:55 +0000774 'fake_ancestor_sha'),
tandrii@chromium.orgfe30f182016-04-13 12:15:04 +0000775 # Calls to verify branch point is ancestor
776 ] + (cls._gerrit_ensure_auth_calls(issue=issue) +
777 cls._git_sanity_checks('fake_ancestor_sha', 'master',
778 get_remote_branch=False)) + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000779 ((['git', 'rev-parse', '--show-cdup'],), ''),
780 ((['git', 'rev-parse', 'HEAD'],), '12345'),
tandrii@chromium.orgfe30f182016-04-13 12:15:04 +0000781
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000782 ((['git',
mcgrathr@chromium.org9249f642013-06-03 21:36:18 +0000783 'diff', '--name-status', '--no-renames', '-r',
784 'fake_ancestor_sha...', '.'],),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000785 'M\t.gitignore\n'),
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +0000786 ((['git', 'config', 'branch.master.gerritpatchset'],), ''),
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000787 ] + ([] if issue else [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000788 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000789 'log', '--pretty=format:%s%n%n%b', 'fake_ancestor_sha...'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000790 'foo'),
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000791 ]) + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000792 ((['git', 'config', 'user.email'],), 'me@example.com'),
793 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000794 'diff', '--no-ext-diff', '--stat', '--find-copies-harder',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000795 '-l100000', '-C50', 'fake_ancestor_sha', 'HEAD'],),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000796 '+dat'),
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +0000797 ]
ukai@chromium.orge8077812012-02-03 03:41:46 +0000798
tandrii@chromium.org1e67bb72016-02-11 12:15:49 +0000799 @classmethod
800 def _gerrit_upload_calls(cls, description, reviewers, squash,
tandriia60502f2016-06-20 02:01:53 -0700801 squash_mode='default',
tandrii@chromium.org10625002016-03-04 20:03:47 +0000802 expected_upstream_ref='origin/refs/heads/master',
tandrii@chromium.org8da45402016-05-24 23:11:03 +0000803 ref_suffix='', notify=False,
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000804 post_amend_description=None, issue=None):
tandrii@chromium.org10625002016-03-04 20:03:47 +0000805 if post_amend_description is None:
806 post_amend_description = description
tandriia60502f2016-06-20 02:01:53 -0700807 calls = []
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000808
tandriia60502f2016-06-20 02:01:53 -0700809 if squash_mode == 'default':
810 calls.extend([
811 ((['git', 'config', '--bool', 'gerrit.override-squash-uploads'],), ''),
812 ((['git', 'config', '--bool', 'gerrit.squash-uploads'],), ''),
813 ])
814 elif squash_mode in ('override_squash', 'override_nosquash'):
815 calls.extend([
816 ((['git', 'config', '--bool', 'gerrit.override-squash-uploads'],),
817 'true' if squash_mode == 'override_squash' else 'false'),
818 ])
819 else:
820 assert squash_mode in ('squash', 'nosquash')
821
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000822 # If issue is given, then description is fetched from Gerrit instead.
823 if issue is None:
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000824 calls += [
825 ((['git', 'log', '--pretty=format:%s\n\n%b',
826 'fake_ancestor_sha..HEAD'],),
827 description)]
tandrii@chromium.org57d86542016-03-04 16:11:32 +0000828 if not git_footers.get_footer_change_id(description) and not squash:
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000829 calls += [
tandrii@chromium.org10625002016-03-04 20:03:47 +0000830 # DownloadGerritHook(False)
831 ((False, ),
832 ''),
833 # Amending of commit message to get the Change-Id.
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000834 ((['git', 'log', '--pretty=format:%s\n\n%b',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000835 'fake_ancestor_sha..HEAD'],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000836 description),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000837 ((['git', 'commit', '--amend', '-m', description],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000838 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000839 ((['git', 'log', '--pretty=format:%s\n\n%b',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000840 'fake_ancestor_sha..HEAD'],),
tandrii@chromium.org10625002016-03-04 20:03:47 +0000841 post_amend_description)
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000842 ]
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000843 if squash:
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000844 if not issue:
845 # Prompting to edit description on first upload.
846 calls += [
847 ((['git', 'config', 'core.editor'],), ''),
848 ((['RunEditor'],), description),
849 ]
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000850 ref_to_push = 'abcdef0123456789'
851 calls += [
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000852 ((['git', 'config', 'branch.master.merge'],),
853 'refs/heads/master'),
854 ((['git', 'config', 'branch.master.remote'],),
855 'origin'),
tandrii@chromium.orgfe30f182016-04-13 12:15:04 +0000856 ((['get_or_create_merge_base', 'master',
857 'refs/remotes/origin/master'],),
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000858 'origin/master'),
859 ((['git', 'rev-parse', 'HEAD:'],),
860 '0123456789abcdef'),
861 ((['git', 'commit-tree', '0123456789abcdef', '-p',
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000862 'origin/master', '-m', description],),
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000863 ref_to_push),
864 ]
865 else:
866 ref_to_push = 'HEAD'
867
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000868 calls += [
luqui@chromium.org609f3952015-05-04 22:47:04 +0000869 ((['git', 'rev-list',
870 expected_upstream_ref + '..' + ref_to_push],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000871 ((['git', 'config', 'rietveld.cc'],), '')
ukai@chromium.orge8077812012-02-03 03:41:46 +0000872 ]
tandrii@chromium.org8da45402016-05-24 23:11:03 +0000873
874 notify_suffix = 'notify=%s' % ('ALL' if notify else 'NONE')
875 if ref_suffix:
876 ref_suffix += ',' + notify_suffix
877 else:
878 ref_suffix = '%' + notify_suffix
tandrii@chromium.org074c2af2016-06-03 23:18:40 +0000879
880 # Add cc from watch list.
881 ref_suffix += ',cc=joe@example.com'
882
ukai@chromium.orge8077812012-02-03 03:41:46 +0000883 if reviewers:
tandrii@chromium.org8da45402016-05-24 23:11:03 +0000884 ref_suffix += ',' + ','.join('r=%s' % email
885 for email in sorted(reviewers))
ukai@chromium.orge8077812012-02-03 03:41:46 +0000886 calls += [
tandrii@chromium.org8acd8332016-04-13 12:56:03 +0000887 ((['git', 'push', 'origin',
tandrii@chromium.orgbf766ba2016-04-13 12:51:23 +0000888 ref_to_push + ':refs/for/refs/heads/master' + ref_suffix],),
tandrii@chromium.orga342c922016-03-16 07:08:25 +0000889 ('remote:\n'
890 'remote: Processing changes: (\)\n'
891 'remote: Processing changes: (|)\n'
892 'remote: Processing changes: (/)\n'
893 'remote: Processing changes: (-)\n'
894 'remote: Processing changes: new: 1 (/)\n'
895 'remote: Processing changes: new: 1, done\n'
896 'remote:\n'
897 'remote: New Changes:\n'
898 'remote: https://chromium-review.googlesource.com/123456 XXX.\n'
899 'remote:\n'
900 'To https://chromium.googlesource.com/yyy/zzz\n'
901 ' * [new branch] hhhh -> refs/for/refs/heads/master\n')),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000902 ]
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000903 if squash:
904 calls += [
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +0000905 ((['git', 'config', 'branch.master.gerritissue', '123456'],), ''),
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +0000906 ((['git', 'config', 'branch.master.gerritserver',
907 'https://chromium-review.googlesource.com'],), ''),
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000908 ((['git', 'config', 'branch.master.gerritsquashhash',
909 'abcdef0123456789'],), ''),
tandrii@chromium.orgfe30f182016-04-13 12:15:04 +0000910 ]
tandrii@chromium.org1e67bb72016-02-11 12:15:49 +0000911 calls += cls._git_post_upload_calls()
ukai@chromium.orge8077812012-02-03 03:41:46 +0000912 return calls
913
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000914 def _run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000915 self,
916 upload_args,
917 description,
tandrii@chromium.orgbf766ba2016-04-13 12:51:23 +0000918 reviewers=None,
tandriia60502f2016-06-20 02:01:53 -0700919 squash=True,
920 squash_mode=None,
tandrii@chromium.org10625002016-03-04 20:03:47 +0000921 expected_upstream_ref='origin/refs/heads/master',
tandrii@chromium.orgbf766ba2016-04-13 12:51:23 +0000922 ref_suffix='',
tandrii@chromium.org8da45402016-05-24 23:11:03 +0000923 notify=False,
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000924 post_amend_description=None,
925 issue=None):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000926 """Generic gerrit upload test framework."""
tandriia60502f2016-06-20 02:01:53 -0700927 if squash_mode is None:
928 if '--no-squash' in upload_args:
929 squash_mode = 'nosquash'
930 elif '--squash' in upload_args:
931 squash_mode = 'squash'
932 else:
933 squash_mode = 'default'
934
tandrii@chromium.orgbf766ba2016-04-13 12:51:23 +0000935 reviewers = reviewers or []
tandrii@chromium.org28253532016-04-14 13:46:56 +0000936 self.mock(git_cl.sys, 'stdout', StringIO.StringIO())
tandrii16e0b4e2016-06-07 10:34:28 -0700937 self.mock(git_cl.gerrit_util, 'CookiesAuthenticator',
tandrii@chromium.orgfe30f182016-04-13 12:15:04 +0000938 CookiesAuthenticatorMockFactory(same_cookie='same_cred'))
tandrii16e0b4e2016-06-07 10:34:28 -0700939 self.mock(git_cl._GerritChangelistImpl, '_GerritCommitMsgHookCheck',
940 lambda _, offer_removal: None)
tandriia60502f2016-06-20 02:01:53 -0700941 self.mock(git_cl.gclient_utils, 'RunEditor',
942 lambda *_, **__: self._mocked_call(['RunEditor']))
943 self.mock(git_cl, 'DownloadGerritHook', self._mocked_call)
944
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000945 self.calls = self._gerrit_base_calls(issue=issue)
luqui@chromium.org609f3952015-05-04 22:47:04 +0000946 self.calls += self._gerrit_upload_calls(
947 description, reviewers, squash,
tandriia60502f2016-06-20 02:01:53 -0700948 squash_mode=squash_mode,
tandrii@chromium.org10625002016-03-04 20:03:47 +0000949 expected_upstream_ref=expected_upstream_ref,
tandrii@chromium.org8da45402016-05-24 23:11:03 +0000950 ref_suffix=ref_suffix, notify=notify,
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000951 post_amend_description=post_amend_description,
952 issue=issue)
tandrii@chromium.org09d7a6a2016-03-04 15:44:48 +0000953 # Uncomment when debugging.
954 # print '\n'.join(map(lambda x: '%2i: %s' % x, enumerate(self.calls)))
ukai@chromium.orge8077812012-02-03 03:41:46 +0000955 git_cl.main(['upload'] + upload_args)
956
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000957 def test_gerrit_upload_without_change_id(self):
tandriia60502f2016-06-20 02:01:53 -0700958 self._run_gerrit_upload_test(
959 ['--no-squash'],
960 'desc\n\nBUG=\n',
961 [],
962 squash=False,
963 post_amend_description='desc\n\nBUG=\n\nChange-Id: Ixxx')
964
965 def test_gerrit_upload_without_change_id_override_nosquash(self):
tandrii@chromium.org10625002016-03-04 20:03:47 +0000966 self.mock(git_cl, 'DownloadGerritHook', self._mocked_call)
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000967 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000968 [],
jamesr@chromium.org35d1a842012-07-27 00:20:43 +0000969 'desc\n\nBUG=\n',
tandrii@chromium.org10625002016-03-04 20:03:47 +0000970 [],
tandriia60502f2016-06-20 02:01:53 -0700971 squash=False,
972 squash_mode='override_nosquash',
tandrii@chromium.org10625002016-03-04 20:03:47 +0000973 post_amend_description='desc\n\nBUG=\n\nChange-Id: Ixxx')
ukai@chromium.orge8077812012-02-03 03:41:46 +0000974
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000975 def test_gerrit_no_reviewer(self):
976 self._run_gerrit_upload_test(
977 [],
tandrii@chromium.org09d7a6a2016-03-04 15:44:48 +0000978 'desc\n\nBUG=\n\nChange-Id: I123456789\n',
tandriia60502f2016-06-20 02:01:53 -0700979 [],
980 squash=False,
981 squash_mode='override_nosquash')
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000982
tandriieefe8322016-08-17 10:12:24 -0700983 def test_gerrit_patch_bad_chars(self):
984 self.mock(git_cl.sys, 'stdout', StringIO.StringIO())
tandrii@chromium.orgbf766ba2016-04-13 12:51:23 +0000985 self._run_gerrit_upload_test(
tandriieefe8322016-08-17 10:12:24 -0700986 ['-f', '-t', 'Don\'t put bad cha,.rs'],
tandrii@chromium.orgbf766ba2016-04-13 12:51:23 +0000987 'desc\n\nBUG=\n\nChange-Id: I123456789',
tandriia60502f2016-06-20 02:01:53 -0700988 squash=False,
989 squash_mode='override_nosquash',
tandriieefe8322016-08-17 10:12:24 -0700990 ref_suffix='%m=Dont_put_bad_chars')
991 self.assertIn(
992 'WARNING: Patchset title may only contain alphanumeric chars '
993 'and spaces. Cleaned up title:\nDont put bad chars\n',
994 git_cl.sys.stdout.getvalue())
tandrii@chromium.orgbf766ba2016-04-13 12:51:23 +0000995
ukai@chromium.orge8077812012-02-03 03:41:46 +0000996 def test_gerrit_reviewers_cmd_line(self):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000997 self._run_gerrit_upload_test(
tandrii@chromium.org8da45402016-05-24 23:11:03 +0000998 ['-r', 'foo@example.com', '--send-mail'],
tandrii@chromium.org09d7a6a2016-03-04 15:44:48 +0000999 'desc\n\nBUG=\n\nChange-Id: I123456789',
tandrii@chromium.org8da45402016-05-24 23:11:03 +00001000 ['foo@example.com'],
tandriia60502f2016-06-20 02:01:53 -07001001 squash=False,
1002 squash_mode='override_nosquash',
tandrii@chromium.org8da45402016-05-24 23:11:03 +00001003 notify=True)
ukai@chromium.orge8077812012-02-03 03:41:46 +00001004
1005 def test_gerrit_reviewer_multiple(self):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +00001006 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +00001007 [],
tandrii@chromium.org09d7a6a2016-03-04 15:44:48 +00001008 'desc\nTBR=reviewer@example.com\nBUG=\nR=another@example.com\n\n'
1009 'Change-Id: 123456789\n',
tandriia60502f2016-06-20 02:01:53 -07001010 ['reviewer@example.com', 'another@example.com'],
1011 squash=False,
tandrii99a72f22016-08-17 14:33:24 -07001012 squash_mode='override_nosquash',
1013 ref_suffix='%l=Code-Review+1')
tandriia60502f2016-06-20 02:01:53 -07001014
1015 def test_gerrit_upload_squash_first_is_default(self):
1016 # Mock Gerrit CL description to indicate the first upload.
1017 self.mock(git_cl.Changelist, 'GetDescription',
1018 lambda *_: None)
1019 self._run_gerrit_upload_test(
1020 [],
1021 'desc\nBUG=\n\nChange-Id: 123456789',
1022 [],
1023 expected_upstream_ref='origin/master')
ukai@chromium.orge8077812012-02-03 03:41:46 +00001024
tandrii@chromium.org512d79c2016-03-31 12:55:28 +00001025 def test_gerrit_upload_squash_first(self):
1026 # Mock Gerrit CL description to indicate the first upload.
1027 self.mock(git_cl.Changelist, 'GetDescription',
1028 lambda *_: None)
bauerb@chromium.org27386dd2015-02-16 10:45:39 +00001029 self._run_gerrit_upload_test(
1030 ['--squash'],
tandrii@chromium.org512d79c2016-03-31 12:55:28 +00001031 'desc\nBUG=\n\nChange-Id: 123456789',
bauerb@chromium.org27386dd2015-02-16 10:45:39 +00001032 [],
luqui@chromium.org609f3952015-05-04 22:47:04 +00001033 squash=True,
1034 expected_upstream_ref='origin/master')
ukai@chromium.orge8077812012-02-03 03:41:46 +00001035
tandrii@chromium.org512d79c2016-03-31 12:55:28 +00001036 def test_gerrit_upload_squash_reupload(self):
1037 description = 'desc\nBUG=\n\nChange-Id: 123456789'
1038 # Mock Gerrit CL description to indicate re-upload.
1039 self.mock(git_cl.Changelist, 'GetDescription',
1040 lambda *args: description)
1041 self.mock(git_cl.Changelist, 'GetMostRecentPatchset',
1042 lambda *args: 1)
1043 self.mock(git_cl._GerritChangelistImpl, '_GetChangeDetail',
1044 lambda *args: {'change_id': '123456789'})
1045 self._run_gerrit_upload_test(
1046 ['--squash'],
1047 description,
1048 [],
1049 squash=True,
1050 expected_upstream_ref='origin/master',
1051 issue=123456)
1052
rmistry@google.com2dd99862015-06-22 12:22:18 +00001053 def test_upload_branch_deps(self):
tandrii@chromium.org28253532016-04-14 13:46:56 +00001054 self.mock(git_cl.sys, 'stdout', StringIO.StringIO())
rmistry@google.com2dd99862015-06-22 12:22:18 +00001055 def mock_run_git(*args, **_kwargs):
1056 if args[0] == ['for-each-ref',
1057 '--format=%(refname:short) %(upstream:short)',
1058 'refs/heads']:
1059 # Create a local branch dependency tree that looks like this:
1060 # test1 -> test2 -> test3 -> test4 -> test5
1061 # -> test3.1
1062 # test6 -> test0
1063 branch_deps = [
1064 'test2 test1', # test1 -> test2
1065 'test3 test2', # test2 -> test3
1066 'test3.1 test2', # test2 -> test3.1
1067 'test4 test3', # test3 -> test4
1068 'test5 test4', # test4 -> test5
1069 'test6 test0', # test0 -> test6
1070 'test7', # test7
1071 ]
1072 return '\n'.join(branch_deps)
1073 self.mock(git_cl, 'RunGit', mock_run_git)
1074
1075 class RecordCalls:
1076 times_called = 0
1077 record_calls = RecordCalls()
1078 def mock_CMDupload(*args, **_kwargs):
1079 record_calls.times_called += 1
1080 return 0
1081 self.mock(git_cl, 'CMDupload', mock_CMDupload)
1082
1083 self.calls = [
1084 (('[Press enter to continue or ctrl-C to quit]',), ''),
1085 ]
1086
1087 class MockChangelist():
1088 def __init__(self):
1089 pass
1090 def GetBranch(self):
1091 return 'test1'
1092 def GetIssue(self):
1093 return '123'
1094 def GetPatchset(self):
1095 return '1001'
tandrii@chromium.org4c72b082016-03-31 22:26:35 +00001096 def IsGerrit(self):
1097 return False
rmistry@google.com2dd99862015-06-22 12:22:18 +00001098
1099 ret = git_cl.upload_branch_deps(MockChangelist(), [])
1100 # CMDupload should have been called 5 times because of 5 dependent branches.
1101 self.assertEquals(5, record_calls.times_called)
1102 self.assertEquals(0, ret)
1103
tandrii@chromium.org65874e12016-03-04 12:03:02 +00001104 def test_gerrit_change_id(self):
1105 self.calls = [
1106 ((['git', 'write-tree'], ),
1107 'hashtree'),
1108 ((['git', 'rev-parse', 'HEAD~0'], ),
1109 'branch-parent'),
1110 ((['git', 'var', 'GIT_AUTHOR_IDENT'], ),
1111 'A B <a@b.org> 1456848326 +0100'),
1112 ((['git', 'var', 'GIT_COMMITTER_IDENT'], ),
1113 'C D <c@d.org> 1456858326 +0100'),
1114 ((['git', 'hash-object', '-t', 'commit', '--stdin'], ),
1115 'hashchange'),
1116 ]
1117 change_id = git_cl.GenerateGerritChangeId('line1\nline2\n')
1118 self.assertEqual(change_id, 'Ihashchange')
1119
tandrii@chromium.org601e1d12016-06-03 13:03:54 +00001120 def test_desecription_append_footer(self):
1121 for init_desc, footer_line, expected_desc in [
1122 # Use unique desc first lines for easy test failure identification.
1123 ('foo', 'R=one', 'foo\n\nR=one'),
1124 ('foo\n\nR=one', 'BUG=', 'foo\n\nR=one\nBUG='),
1125 ('foo\n\nR=one', 'Change-Id: Ixx', 'foo\n\nR=one\n\nChange-Id: Ixx'),
1126 ('foo\n\nChange-Id: Ixx', 'R=one', 'foo\n\nR=one\n\nChange-Id: Ixx'),
1127 ('foo\n\nR=one\n\nChange-Id: Ixx', 'TBR=two',
1128 'foo\n\nR=one\nTBR=two\n\nChange-Id: Ixx'),
1129 ('foo\n\nR=one\n\nChange-Id: Ixx', 'Foo-Bar: baz',
1130 'foo\n\nR=one\n\nChange-Id: Ixx\nFoo-Bar: baz'),
1131 ('foo\n\nChange-Id: Ixx', 'Foo-Bak: baz',
1132 'foo\n\nChange-Id: Ixx\nFoo-Bak: baz'),
1133 ('foo', 'Change-Id: Ixx', 'foo\n\nChange-Id: Ixx'),
1134 ]:
1135 desc = git_cl.ChangeDescription(init_desc)
1136 desc.append_footer(footer_line)
1137 self.assertEqual(desc.description, expected_desc)
1138
maruel@chromium.org78936cb2013-04-11 00:17:52 +00001139 def test_update_reviewers(self):
1140 data = [
1141 ('foo', [], 'foo'),
agable@chromium.org42c20792013-09-12 17:34:49 +00001142 ('foo\nR=xx', [], 'foo\nR=xx'),
1143 ('foo\nTBR=xx', [], 'foo\nTBR=xx'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +00001144 ('foo', ['a@c'], 'foo\n\nR=a@c'),
agable@chromium.org42c20792013-09-12 17:34:49 +00001145 ('foo\nR=xx', ['a@c'], 'foo\n\nR=a@c, xx'),
1146 ('foo\nTBR=xx', ['a@c'], 'foo\n\nR=a@c\nTBR=xx'),
1147 ('foo\nTBR=xx\nR=yy', ['a@c'], 'foo\n\nR=a@c, yy\nTBR=xx'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +00001148 ('foo\nBUG=', ['a@c'], 'foo\nBUG=\nR=a@c'),
agable@chromium.org42c20792013-09-12 17:34:49 +00001149 ('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 +00001150 ('foo', ['a@c', 'b@c'], 'foo\n\nR=a@c, b@c'),
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +00001151 ('foo\nBar\n\nR=\nBUG=', ['c@c'], 'foo\nBar\n\nR=c@c\nBUG='),
1152 ('foo\nBar\n\nR=\nBUG=\nR=', ['c@c'], 'foo\nBar\n\nR=c@c\nBUG='),
1153 # Same as the line before, but full of whitespaces.
1154 (
1155 'foo\nBar\n\n R = \n BUG = \n R = ', ['c@c'],
1156 'foo\nBar\n\nR=c@c\n BUG =',
1157 ),
1158 # Whitespaces aren't interpreted as new lines.
1159 ('foo BUG=allo R=joe ', ['c@c'], 'foo BUG=allo R=joe\n\nR=c@c'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +00001160 ]
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +00001161 expected = [i[2] for i in data]
1162 actual = []
1163 for orig, reviewers, _expected in data:
maruel@chromium.org78936cb2013-04-11 00:17:52 +00001164 obj = git_cl.ChangeDescription(orig)
1165 obj.update_reviewers(reviewers)
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +00001166 actual.append(obj.description)
1167 self.assertEqual(expected, actual)
maruel@chromium.org78936cb2013-04-11 00:17:52 +00001168
wittman@chromium.org455dc922015-01-26 20:15:50 +00001169 def test_get_target_ref(self):
1170 # Check remote or remote branch not present.
1171 self.assertEqual(None, git_cl.GetTargetRef('origin', None, 'master', None))
1172 self.assertEqual(None, git_cl.GetTargetRef(None,
1173 'refs/remotes/origin/master',
1174 'master', None))
bauerb@chromium.org27386dd2015-02-16 10:45:39 +00001175
wittman@chromium.org455dc922015-01-26 20:15:50 +00001176 # Check default target refs for branches.
1177 self.assertEqual('refs/heads/master',
1178 git_cl.GetTargetRef('origin', 'refs/remotes/origin/master',
1179 None, None))
1180 self.assertEqual('refs/heads/master',
1181 git_cl.GetTargetRef('origin', 'refs/remotes/origin/lkgr',
1182 None, None))
1183 self.assertEqual('refs/heads/master',
1184 git_cl.GetTargetRef('origin', 'refs/remotes/origin/lkcr',
1185 None, None))
1186 self.assertEqual('refs/branch-heads/123',
1187 git_cl.GetTargetRef('origin',
1188 'refs/remotes/branch-heads/123',
1189 None, None))
1190 self.assertEqual('refs/diff/test',
1191 git_cl.GetTargetRef('origin',
1192 'refs/remotes/origin/refs/diff/test',
1193 None, None))
rmistry@google.comc68112d2015-03-03 12:48:06 +00001194 self.assertEqual('refs/heads/chrome/m42',
1195 git_cl.GetTargetRef('origin',
1196 'refs/remotes/origin/chrome/m42',
1197 None, None))
wittman@chromium.org455dc922015-01-26 20:15:50 +00001198
1199 # Check target refs for user-specified target branch.
1200 for branch in ('branch-heads/123', 'remotes/branch-heads/123',
1201 'refs/remotes/branch-heads/123'):
1202 self.assertEqual('refs/branch-heads/123',
1203 git_cl.GetTargetRef('origin',
1204 'refs/remotes/origin/master',
1205 branch, None))
1206 for branch in ('origin/master', 'remotes/origin/master',
1207 'refs/remotes/origin/master'):
1208 self.assertEqual('refs/heads/master',
1209 git_cl.GetTargetRef('origin',
1210 'refs/remotes/branch-heads/123',
1211 branch, None))
1212 for branch in ('master', 'heads/master', 'refs/heads/master'):
1213 self.assertEqual('refs/heads/master',
1214 git_cl.GetTargetRef('origin',
1215 'refs/remotes/branch-heads/123',
1216 branch, None))
1217
1218 # Check target refs for pending prefix.
1219 self.assertEqual('prefix/heads/master',
1220 git_cl.GetTargetRef('origin', 'refs/remotes/origin/master',
1221 None, 'prefix/'))
1222
wychen@chromium.orga872e752015-04-28 23:42:18 +00001223 def test_patch_when_dirty(self):
1224 # Patch when local tree is dirty
1225 self.mock(git_common, 'is_dirty_git_tree', lambda x: True)
1226 self.assertNotEqual(git_cl.main(['patch', '123456']), 0)
1227
1228 def test_diff_when_dirty(self):
1229 # Do 'git cl diff' when local tree is dirty
1230 self.mock(git_common, 'is_dirty_git_tree', lambda x: True)
1231 self.assertNotEqual(git_cl.main(['diff']), 0)
1232
tandrii@chromium.orgdde64622016-04-13 17:11:21 +00001233 def _patch_common(self, is_gerrit=False, force_codereview=False):
tandrii@chromium.org28253532016-04-14 13:46:56 +00001234 self.mock(git_cl.sys, 'stdout', StringIO.StringIO())
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00001235 self.mock(git_cl._RietveldChangelistImpl, 'GetMostRecentPatchset',
1236 lambda x: '60001')
1237 self.mock(git_cl._RietveldChangelistImpl, 'GetPatchSetDiff',
1238 lambda *args: None)
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +00001239 self.mock(git_cl._GerritChangelistImpl, '_GetChangeDetail',
1240 lambda *args: {
1241 'current_revision': '7777777777',
1242 'revisions': {
1243 '1111111111': {
1244 '_number': 1,
1245 'fetch': {'http': {
1246 'url': 'https://chromium.googlesource.com/my/repo',
1247 'ref': 'refs/changes/56/123456/1',
1248 }},
1249 },
1250 '7777777777': {
1251 '_number': 7,
1252 'fetch': {'http': {
1253 'url': 'https://chromium.googlesource.com/my/repo',
1254 'ref': 'refs/changes/56/123456/7',
1255 }},
1256 },
1257 },
1258 })
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00001259 self.mock(git_cl.Changelist, 'GetDescription',
1260 lambda *args: 'Description')
wychen@chromium.orga872e752015-04-28 23:42:18 +00001261 self.mock(git_cl, 'IsGitVersionAtLeast', lambda *args: True)
1262
tandrii@chromium.orgc2786d92016-05-31 19:53:50 +00001263 self.calls = self.calls or []
tandrii@chromium.orgdde64622016-04-13 17:11:21 +00001264 if not force_codereview:
1265 # These calls detect codereview to use.
tandrii@chromium.orgc2786d92016-05-31 19:53:50 +00001266 self.calls += [
tandrii@chromium.orgdde64622016-04-13 17:11:21 +00001267 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
1268 ((['git', 'config', 'branch.master.rietveldissue'],), ''),
1269 ((['git', 'config', 'branch.master.gerritissue'],), ''),
1270 ((['git', 'config', 'rietveld.autoupdate'],), ''),
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +00001271 ]
tandrii@chromium.orgdde64622016-04-13 17:11:21 +00001272
1273 if is_gerrit:
1274 if not force_codereview:
1275 self.calls += [
1276 ((['git', 'config', 'gerrit.host'],), 'true'),
1277 ]
1278 else:
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +00001279 self.calls += [
1280 ((['git', 'config', 'gerrit.host'],), ''),
1281 ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'),
1282 ((['git', 'rev-parse', '--show-cdup'],), ''),
1283 ((['sed', '-e', 's|^--- a/|--- |; s|^+++ b/|+++ |'],), ''),
1284 ]
wychen@chromium.orga872e752015-04-28 23:42:18 +00001285
tandrii@chromium.orgc2786d92016-05-31 19:53:50 +00001286 def _common_patch_successful(self):
wychen@chromium.orga872e752015-04-28 23:42:18 +00001287 self._patch_common()
1288 self.calls += [
1289 ((['git', 'apply', '--index', '-p0', '--3way'],), ''),
1290 ((['git', 'commit', '-m',
wychen@chromium.org5b3bebb2015-05-28 21:41:43 +00001291 'Description\n\n' +
wychen@chromium.orga872e752015-04-28 23:42:18 +00001292 'patch from issue 123456 at patchset 60001 ' +
1293 '(http://crrev.com/123456#ps60001)'],), ''),
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +00001294 ((['git', 'config', 'branch.master.rietveldissue', '123456'],), ''),
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00001295 ((['git', 'config', 'branch.master.rietveldserver'],), ''),
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +00001296 ((['git', 'config', 'branch.master.rietveldserver',
1297 'https://codereview.example.com'],), ''),
1298 ((['git', 'config', 'branch.master.rietveldpatchset', '60001'],), ''),
wychen@chromium.orga872e752015-04-28 23:42:18 +00001299 ]
tandrii@chromium.orgc2786d92016-05-31 19:53:50 +00001300
1301 def test_patch_successful(self):
1302 self._common_patch_successful()
wychen@chromium.orga872e752015-04-28 23:42:18 +00001303 self.assertEqual(git_cl.main(['patch', '123456']), 0)
1304
tandrii@chromium.orgc2786d92016-05-31 19:53:50 +00001305 def test_patch_successful_new_branch(self):
1306 self.calls = [ ((['git', 'new-branch', 'master'],), ''), ]
1307 self._common_patch_successful()
1308 self.assertEqual(git_cl.main(['patch', '-b', 'master', '123456']), 0)
1309
wychen@chromium.orga872e752015-04-28 23:42:18 +00001310 def test_patch_conflict(self):
1311 self._patch_common()
1312 self.calls += [
1313 ((['git', 'apply', '--index', '-p0', '--3way'],), '',
1314 subprocess2.CalledProcessError(1, '', '', '', '')),
1315 ]
1316 self.assertNotEqual(git_cl.main(['patch', '123456']), 0)
wittman@chromium.org455dc922015-01-26 20:15:50 +00001317
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +00001318 def test_gerrit_patch_successful(self):
1319 self._patch_common(is_gerrit=True)
1320 self.calls += [
1321 ((['git', 'fetch', 'https://chromium.googlesource.com/my/repo',
1322 'refs/changes/56/123456/7'],), ''),
1323 ((['git', 'cherry-pick', 'FETCH_HEAD'],), ''),
1324 ((['git', 'config', 'branch.master.gerritissue', '123456'],), ''),
1325 ((['git', 'config', 'branch.master.gerritserver'],), ''),
1326 ((['git', 'config', 'branch.master.merge'],), 'master'),
1327 ((['git', 'config', 'branch.master.remote'],), 'origin'),
1328 ((['git', 'config', 'remote.origin.url'],),
1329 'https://chromium.googlesource.com/my/repo'),
1330 ((['git', 'config', 'branch.master.gerritserver',
1331 'https://chromium-review.googlesource.com'],), ''),
1332 ((['git', 'config', 'branch.master.gerritpatchset', '7'],), ''),
1333 ]
1334 self.assertEqual(git_cl.main(['patch', '123456']), 0)
1335
tandrii@chromium.orgdde64622016-04-13 17:11:21 +00001336 def test_patch_force_codereview(self):
1337 self._patch_common(is_gerrit=True, force_codereview=True)
1338 self.calls += [
1339 ((['git', 'fetch', 'https://chromium.googlesource.com/my/repo',
1340 'refs/changes/56/123456/7'],), ''),
1341 ((['git', 'cherry-pick', 'FETCH_HEAD'],), ''),
1342 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
1343 ((['git', 'config', 'branch.master.gerritissue', '123456'],), ''),
1344 ((['git', 'config', 'branch.master.gerritserver'],), ''),
1345 ((['git', 'config', 'branch.master.merge'],), 'master'),
1346 ((['git', 'config', 'branch.master.remote'],), 'origin'),
1347 ((['git', 'config', 'remote.origin.url'],),
1348 'https://chromium.googlesource.com/my/repo'),
1349 ((['git', 'config', 'branch.master.gerritserver',
1350 'https://chromium-review.googlesource.com'],), ''),
1351 ((['git', 'config', 'branch.master.gerritpatchset', '7'],), ''),
1352 ]
1353 self.assertEqual(git_cl.main(['patch', '--gerrit', '123456']), 0)
1354
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +00001355 def test_gerrit_patch_url_successful(self):
1356 self._patch_common(is_gerrit=True)
1357 self.calls += [
1358 ((['git', 'fetch', 'https://chromium.googlesource.com/my/repo',
1359 'refs/changes/56/123456/1'],), ''),
1360 ((['git', 'cherry-pick', 'FETCH_HEAD'],), ''),
1361 ((['git', 'config', 'branch.master.gerritissue', '123456'],), ''),
1362 ((['git', 'config', 'branch.master.gerritserver',
1363 'https://chromium-review.googlesource.com'],), ''),
1364 ((['git', 'config', 'branch.master.gerritpatchset', '1'],), ''),
1365 ]
1366 self.assertEqual(git_cl.main(
1367 ['patch', 'https://chromium-review.googlesource.com/#/c/123456/1']), 0)
1368
1369 def test_gerrit_patch_conflict(self):
1370 self._patch_common(is_gerrit=True)
1371 self.mock(git_cl, 'DieWithError',
1372 lambda msg: self._mocked_call(['DieWithError', msg]))
1373 class SystemExitMock(Exception):
1374 pass
1375 self.calls += [
1376 ((['git', 'fetch', 'https://chromium.googlesource.com/my/repo',
1377 'refs/changes/56/123456/1'],), ''),
1378 ((['git', 'cherry-pick', 'FETCH_HEAD'],),
1379 '', subprocess2.CalledProcessError(1, '', '', '', '')),
1380 ((['DieWithError', 'git cherry-pick FETCH_HEAD" failed.\n'],),
1381 '', SystemExitMock()),
1382 ]
1383 with self.assertRaises(SystemExitMock):
1384 git_cl.main(['patch',
1385 'https://chromium-review.googlesource.com/#/c/123456/1'])
1386
tandrii@chromium.org5df290f2016-04-11 16:12:29 +00001387 def _checkout_calls(self):
1388 return [
1389 ((['git', 'config', '--local', '--get-regexp',
1390 'branch\\..*\\.rietveldissue'], ),
1391 ('branch.retrying.rietveldissue 1111111111\n'
1392 'branch.some-fix.rietveldissue 2222222222\n')),
1393 ((['git', 'config', '--local', '--get-regexp',
1394 'branch\\..*\\.gerritissue'], ),
1395 ('branch.ger-branch.gerritissue 123456\n'
1396 'branch.gbranch654.gerritissue 654321\n')),
1397 ]
1398
1399 def test_checkout_gerrit(self):
1400 """Tests git cl checkout <issue>."""
1401 self.calls = self._checkout_calls()
1402 self.calls += [((['git', 'checkout', 'ger-branch'], ), '')]
1403 self.assertEqual(0, git_cl.main(['checkout', '123456']))
1404
1405 def test_checkout_rietveld(self):
1406 """Tests git cl checkout <issue>."""
1407 self.calls = self._checkout_calls()
1408 self.calls += [((['git', 'checkout', 'some-fix'], ), '')]
1409 self.assertEqual(0, git_cl.main(['checkout', '2222222222']))
1410
1411 def test_checkout_not_found(self):
1412 """Tests git cl checkout <issue>."""
tandrii@chromium.org28253532016-04-14 13:46:56 +00001413 self.mock(git_cl.sys, 'stdout', StringIO.StringIO())
tandrii@chromium.org5df290f2016-04-11 16:12:29 +00001414 self.calls = self._checkout_calls()
1415 self.assertEqual(1, git_cl.main(['checkout', '99999']))
1416
tandrii@chromium.org26c8fd22016-04-11 21:33:21 +00001417 def test_checkout_no_branch_issues(self):
1418 """Tests git cl checkout <issue>."""
tandrii@chromium.org28253532016-04-14 13:46:56 +00001419 self.mock(git_cl.sys, 'stdout', StringIO.StringIO())
tandrii@chromium.org26c8fd22016-04-11 21:33:21 +00001420 self.calls = [
1421 ((['git', 'config', '--local', '--get-regexp',
1422 'branch\\..*\\.rietveldissue'], ), '',
1423 subprocess2.CalledProcessError(1, '', '', '', '')),
1424 ((['git', 'config', '--local', '--get-regexp',
1425 'branch\\..*\\.gerritissue'], ), '',
1426 subprocess2.CalledProcessError(1, '', '', '', '')),
1427
1428 ]
1429 self.assertEqual(1, git_cl.main(['checkout', '99999']))
1430
tandrii@chromium.org28253532016-04-14 13:46:56 +00001431 def _test_gerrit_ensure_authenticated_common(self, auth,
1432 skip_auth_check=False):
tandrii@chromium.orgfe30f182016-04-13 12:15:04 +00001433 self.mock(git_cl.gerrit_util, 'CookiesAuthenticator',
1434 CookiesAuthenticatorMockFactory(hosts_with_creds=auth))
1435 self.mock(git_cl, 'DieWithError',
1436 lambda msg: self._mocked_call(['DieWithError', msg]))
1437 self.mock(git_cl, 'ask_for_data',
1438 lambda msg: self._mocked_call(['ask_for_data', msg]))
tandrii@chromium.org28253532016-04-14 13:46:56 +00001439 self.calls = self._gerrit_ensure_auth_calls(skip_auth_check=skip_auth_check)
tandrii@chromium.orgfe30f182016-04-13 12:15:04 +00001440 cl = git_cl.Changelist(codereview='gerrit')
tandrii@chromium.org28253532016-04-14 13:46:56 +00001441 cl.branch = 'master'
1442 cl.branchref = 'refs/heads/master'
tandrii@chromium.orgfe30f182016-04-13 12:15:04 +00001443 cl.lookedup_issue = True
1444 return cl
1445
1446 def test_gerrit_ensure_authenticated_missing(self):
1447 cl = self._test_gerrit_ensure_authenticated_common(auth={
1448 'chromium.googlesource.com': 'git is ok, but gerrit one is missing',
1449 })
1450 self.calls.append(
1451 ((['DieWithError',
1452 'Credentials for the following hosts are required:\n'
1453 ' chromium-review.googlesource.com\n'
1454 'These are read from ~/.gitcookies (or legacy ~/.netrc)\n'
1455 'You can (re)generate your credentails by visiting '
1456 'https://chromium-review.googlesource.com/new-password'],), ''),)
1457 self.assertIsNone(cl.EnsureAuthenticated(force=False))
1458
1459 def test_gerrit_ensure_authenticated_conflict(self):
tandrii@chromium.org28253532016-04-14 13:46:56 +00001460 self.mock(git_cl.sys, 'stdout', StringIO.StringIO())
tandrii@chromium.orgfe30f182016-04-13 12:15:04 +00001461 cl = self._test_gerrit_ensure_authenticated_common(auth={
1462 'chromium.googlesource.com': 'one',
1463 'chromium-review.googlesource.com': 'other',
1464 })
1465 self.calls.append(
1466 ((['ask_for_data', 'If you know what you are doing, '
1467 'press Enter to continue, Ctrl+C to abort.'],), ''))
1468 self.assertIsNone(cl.EnsureAuthenticated(force=False))
1469
1470 def test_gerrit_ensure_authenticated_ok(self):
1471 cl = self._test_gerrit_ensure_authenticated_common(auth={
1472 'chromium.googlesource.com': 'same',
1473 'chromium-review.googlesource.com': 'same',
1474 })
1475 self.assertIsNone(cl.EnsureAuthenticated(force=False))
1476
tandrii@chromium.org28253532016-04-14 13:46:56 +00001477 def test_gerrit_ensure_authenticated_skipped(self):
1478 cl = self._test_gerrit_ensure_authenticated_common(
1479 auth={}, skip_auth_check=True)
1480 self.assertIsNone(cl.EnsureAuthenticated(force=False))
1481
tandrii@chromium.orgfa330e82016-04-13 17:09:52 +00001482 def test_cmd_set_commit_rietveld(self):
tandrii4d843592016-07-27 08:22:56 -07001483 self.mock(git_cl._RietveldChangelistImpl, 'SetFlags',
1484 lambda _, v: self._mocked_call(['SetFlags', v]))
tandrii@chromium.orgfa330e82016-04-13 17:09:52 +00001485 self.calls = [
1486 ((['git', 'symbolic-ref', 'HEAD'],), 'feature'),
1487 ((['git', 'config', 'branch.feature.rietveldissue'],), '123'),
1488 ((['git', 'config', 'rietveld.autoupdate'],), ''),
1489 ((['git', 'config', 'rietveld.server'],), ''),
1490 ((['git', 'config', 'rietveld.server'],), ''),
1491 ((['git', 'config', 'branch.feature.rietveldserver'],),
1492 'https://codereview.chromium.org'),
tandrii4d843592016-07-27 08:22:56 -07001493 ((['SetFlags', {'commit': '1', 'cq_dry_run': '0'}], ), ''),
tandrii@chromium.orgfa330e82016-04-13 17:09:52 +00001494 ]
1495 self.assertEqual(0, git_cl.main(['set-commit']))
1496
tandriid9e5ce52016-07-13 02:32:59 -07001497 def _cmd_set_commit_gerrit_common(self, vote):
tandrii@chromium.orgfa330e82016-04-13 17:09:52 +00001498 self.mock(git_cl.gerrit_util, 'SetReview',
1499 lambda h, i, labels: self._mocked_call(
1500 ['SetReview', h, i, labels]))
1501 self.calls = [
1502 ((['git', 'symbolic-ref', 'HEAD'],), 'feature'),
1503 ((['git', 'config', 'branch.feature.rietveldissue'],), ''),
1504 ((['git', 'config', 'branch.feature.gerritissue'],), '123'),
1505 ((['git', 'config', 'branch.feature.gerritserver'],),
1506 'https://chromium-review.googlesource.com'),
1507 ((['SetReview', 'chromium-review.googlesource.com', 123,
tandriid9e5ce52016-07-13 02:32:59 -07001508 {'Commit-Queue': vote}],), ''),
tandrii@chromium.orgfa330e82016-04-13 17:09:52 +00001509 ]
tandriid9e5ce52016-07-13 02:32:59 -07001510
1511 def test_cmd_set_commit_gerrit_clear(self):
1512 self._cmd_set_commit_gerrit_common(0)
1513 self.assertEqual(0, git_cl.main(['set-commit', '-c']))
1514
1515 def test_cmd_set_commit_gerrit_dry(self):
1516 self._cmd_set_commit_gerrit_common(1)
tandrii@chromium.orgfa330e82016-04-13 17:09:52 +00001517 self.assertEqual(0, git_cl.main(['set-commit', '-d']))
1518
tandriid9e5ce52016-07-13 02:32:59 -07001519 def test_cmd_set_commit_gerrit(self):
1520 self._cmd_set_commit_gerrit_common(2)
1521 self.assertEqual(0, git_cl.main(['set-commit']))
1522
martiniss@chromium.org2b55fe32016-04-26 20:28:54 +00001523 def test_description_display(self):
1524 out = StringIO.StringIO()
1525 self.mock(git_cl.sys, 'stdout', out)
1526
martiniss@chromium.orgd6648e22016-04-29 19:22:16 +00001527 self.mock(git_cl, 'Changelist', ChangelistMock)
1528 ChangelistMock.desc = 'foo\n'
martiniss@chromium.org2b55fe32016-04-26 20:28:54 +00001529
1530 self.assertEqual(0, git_cl.main(['description', '-d']))
1531 self.assertEqual('foo\n', out.getvalue())
1532
1533 def test_description_rietveld(self):
1534 out = StringIO.StringIO()
1535 self.mock(git_cl.sys, 'stdout', out)
martiniss6eda05f2016-06-30 10:18:35 -07001536 self.mock(git_cl.Changelist, 'GetDescription', lambda *args: 'foobar')
martiniss@chromium.org2b55fe32016-04-26 20:28:54 +00001537
martiniss@chromium.org2b55fe32016-04-26 20:28:54 +00001538 self.assertEqual(0, git_cl.main([
1539 'description', 'https://code.review.org/123123', '-d', '--rietveld']))
1540 self.assertEqual('foobar\n', out.getvalue())
1541
iannucci3c972b92016-08-17 13:24:10 -07001542 def test_StatusFieldOverrideIssueMissingArgs(self):
1543 out = StringIO.StringIO()
1544 self.mock(git_cl.sys, 'stderr', out)
1545
1546 try:
1547 self.assertEqual(git_cl.main(['status', '--issue', '1']), 0)
1548 except SystemExit as ex:
1549 self.assertEqual(ex.code, 2)
iannuccie53c9352016-08-17 14:40:40 -07001550 self.assertRegexpMatches(out.getvalue(), r'--issue must be specified')
iannucci3c972b92016-08-17 13:24:10 -07001551
1552 out = StringIO.StringIO()
1553 self.mock(git_cl.sys, 'stderr', out)
1554
1555 try:
1556 self.assertEqual(git_cl.main(['status', '--issue', '1', '--rietveld']), 0)
1557 except SystemExit as ex:
1558 self.assertEqual(ex.code, 2)
iannuccie53c9352016-08-17 14:40:40 -07001559 self.assertRegexpMatches(out.getvalue(), r'--field must be specified')
iannucci3c972b92016-08-17 13:24:10 -07001560
1561 def test_StatusFieldOverrideIssue(self):
1562 out = StringIO.StringIO()
1563 self.mock(git_cl.sys, 'stdout', out)
1564
1565 def assertIssue(cl_self, *_args):
1566 self.assertEquals(cl_self.issue, 1)
1567 return 'foobar'
1568
1569 self.mock(git_cl.Changelist, 'GetDescription', assertIssue)
1570 self.calls = [
1571 ((['git', 'config', 'rietveld.autoupdate'],), ''),
1572 ((['git', 'config', 'rietveld.server'],), ''),
1573 ((['git', 'config', 'rietveld.server'],), ''),
1574 ]
iannuccie53c9352016-08-17 14:40:40 -07001575 self.assertEqual(
1576 git_cl.main(['status', '--issue', '1', '--rietveld', '--field', 'desc']),
1577 0)
iannucci3c972b92016-08-17 13:24:10 -07001578 self.assertEqual(out.getvalue(), 'foobar\n')
1579
iannuccie53c9352016-08-17 14:40:40 -07001580 def test_SetCloseOverrideIssue(self):
1581 def assertIssue(cl_self, *_args):
1582 self.assertEquals(cl_self.issue, 1)
1583 return 'foobar'
1584
1585 self.mock(git_cl.Changelist, 'GetDescription', assertIssue)
1586 self.mock(git_cl.Changelist, 'CloseIssue', lambda *_: None)
1587 self.calls = [
1588 ((['git', 'config', 'rietveld.autoupdate'],), ''),
1589 ((['git', 'config', 'rietveld.server'],), ''),
1590 ((['git', 'config', 'rietveld.server'],), ''),
1591 ]
1592 self.assertEqual(
1593 git_cl.main(['set-close', '--issue', '1', '--rietveld']), 0)
1594
1595 def test_SetCommitOverrideIssue(self):
1596 def assertIssue(cl_self, *_args):
1597 self.assertEquals(cl_self.issue, 1)
1598 return 'foobar'
1599
1600 self.mock(git_cl.Changelist, 'GetDescription', assertIssue)
1601 self.mock(git_cl.Changelist, 'SetCQState', lambda *_: None)
1602 self.calls = [
1603 ((['git', 'config', 'rietveld.autoupdate'],), ''),
1604 ((['git', 'config', 'rietveld.server'],), ''),
1605 ((['git', 'config', 'rietveld.server'],), ''),
1606 ((['git', 'symbolic-ref', 'HEAD'],), ''),
1607 ((['git', 'config', 'rietveld.server'],), ''),
1608 ((['git', 'config', 'rietveld.server'],), ''),
1609 ]
1610 self.assertEqual(
1611 git_cl.main(['set-close', '--issue', '1', '--rietveld']), 0)
1612
martiniss@chromium.org2b55fe32016-04-26 20:28:54 +00001613 def test_description_gerrit(self):
1614 out = StringIO.StringIO()
1615 self.mock(git_cl.sys, 'stdout', out)
martiniss6eda05f2016-06-30 10:18:35 -07001616 self.mock(git_cl.Changelist, 'GetDescription', lambda *args: 'foobar')
martiniss@chromium.org2b55fe32016-04-26 20:28:54 +00001617
1618 self.assertEqual(0, git_cl.main([
1619 'description', 'https://code.review.org/123123', '-d', '--gerrit']))
1620 self.assertEqual('foobar\n', out.getvalue())
1621
martiniss@chromium.orgd6648e22016-04-29 19:22:16 +00001622 def test_description_set_raw(self):
1623 out = StringIO.StringIO()
1624 self.mock(git_cl.sys, 'stdout', out)
1625
1626 self.mock(git_cl, 'Changelist', ChangelistMock)
1627 self.mock(git_cl.sys, 'stdin', StringIO.StringIO('hihi'))
1628
1629 self.assertEqual(0, git_cl.main(['description', '-n', 'hihi']))
1630 self.assertEqual('hihi', ChangelistMock.desc)
1631
tandrii@chromium.orgd605a512016-06-03 09:55:00 +00001632 def test_description_appends_bug_line(self):
tandrii@chromium.org601e1d12016-06-03 13:03:54 +00001633 current_desc = 'Some.\n\nChange-Id: xxx'
tandrii@chromium.orgd605a512016-06-03 09:55:00 +00001634
1635 def RunEditor(desc, _, **kwargs):
1636 self.assertEquals(
1637 '# Enter a description of the change.\n'
1638 '# This will be displayed on the codereview site.\n'
1639 '# The first line will also be used as the subject of the review.\n'
1640 '#--------------------This line is 72 characters long'
tandrii@chromium.org601e1d12016-06-03 13:03:54 +00001641 '--------------------\n'
1642 'Some.\n\nBUG=\n\nChange-Id: xxx',
tandrii@chromium.orgd605a512016-06-03 09:55:00 +00001643 desc)
tandrii@chromium.org601e1d12016-06-03 13:03:54 +00001644 # Simulate user changing something.
1645 return 'Some.\n\nBUG=123\n\nChange-Id: xxx'
tandrii@chromium.orgd605a512016-06-03 09:55:00 +00001646
1647 def UpdateDescriptionRemote(_, desc):
tandrii@chromium.org601e1d12016-06-03 13:03:54 +00001648 self.assertEquals(desc, 'Some.\n\nBUG=123\n\nChange-Id: xxx')
tandrii@chromium.orgd605a512016-06-03 09:55:00 +00001649
1650 self.mock(git_cl.sys, 'stdout', StringIO.StringIO())
1651 self.mock(git_cl.Changelist, 'GetDescription',
1652 lambda *args: current_desc)
1653 self.mock(git_cl._GerritChangelistImpl, 'UpdateDescriptionRemote',
1654 UpdateDescriptionRemote)
1655 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor)
1656
1657 self.calls = [
1658 ((['git', 'symbolic-ref', 'HEAD'],), 'feature'),
1659 ((['git', 'config', 'branch.feature.gerritissue'],), '123'),
1660 ((['git', 'config', 'rietveld.autoupdate'],), ''),
1661 ((['git', 'config', 'rietveld.bug-prefix'],), ''),
1662 ((['git', 'config', 'core.editor'],), 'vi'),
1663 ]
1664 self.assertEqual(0, git_cl.main(['description', '--gerrit']))
1665
martiniss@chromium.orgd6648e22016-04-29 19:22:16 +00001666 def test_description_set_stdin(self):
1667 out = StringIO.StringIO()
1668 self.mock(git_cl.sys, 'stdout', out)
1669
1670 self.mock(git_cl, 'Changelist', ChangelistMock)
1671 self.mock(git_cl.sys, 'stdin', StringIO.StringIO('hi \r\n\t there\n\nman'))
1672
1673 self.assertEqual(0, git_cl.main(['description', '-n', '-']))
1674 self.assertEqual('hi\n\t there\n\nman', ChangelistMock.desc)
1675
kmarshall3bff56b2016-06-06 18:31:47 -07001676 def test_archive(self):
tandrii1c67da62016-06-10 07:35:53 -07001677 self.mock(git_cl.sys, 'stdout', StringIO.StringIO())
1678
kmarshall3bff56b2016-06-06 18:31:47 -07001679 self.calls = \
1680 [((['git', 'for-each-ref', '--format=%(refname)', 'refs/heads'],),
1681 'refs/heads/master\nrefs/heads/foo\nrefs/heads/bar'),
1682 ((['git', 'config', 'branch.master.rietveldissue'],), '1'),
1683 ((['git', 'config', 'rietveld.autoupdate'],), ''),
1684 ((['git', 'config', 'rietveld.server'],), ''),
1685 ((['git', 'config', 'rietveld.server'],), ''),
1686 ((['git', 'config', 'branch.foo.rietveldissue'],), '456'),
1687 ((['git', 'config', 'rietveld.server'],), ''),
1688 ((['git', 'config', 'rietveld.server'],), ''),
1689 ((['git', 'config', 'branch.bar.rietveldissue'],), ''),
1690 ((['git', 'config', 'branch.bar.gerritissue'],), '789'),
1691 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
1692 ((['git', 'tag', 'git-cl-archived-456-foo', 'foo'],), ''),
1693 ((['git', 'branch', '-D', 'foo'],), '')]
1694
1695 class MockChangelist():
1696 def __init__(self, branch, issue):
1697 self.branch = branch
1698 self.issue = issue
1699 def GetBranch(self):
1700 return self.branch
1701 def GetIssue(self):
1702 return self.issue
1703
1704 self.mock(git_cl, 'get_cl_statuses',
1705 lambda branches, fine_grained, max_processes:
1706 [(MockChangelist('master', 1), 'open'),
1707 (MockChangelist('foo', 456), 'closed'),
1708 (MockChangelist('bar', 789), 'open')])
1709
1710 self.assertEqual(0, git_cl.main(['archive', '-f']))
1711
1712 def test_archive_current_branch_fails(self):
tandrii1c67da62016-06-10 07:35:53 -07001713 self.mock(git_cl.sys, 'stdout', StringIO.StringIO())
kmarshall3bff56b2016-06-06 18:31:47 -07001714 self.calls = \
1715 [((['git', 'for-each-ref', '--format=%(refname)', 'refs/heads'],),
1716 'refs/heads/master'),
1717 ((['git', 'config', 'branch.master.rietveldissue'],), '1'),
1718 ((['git', 'config', 'rietveld.autoupdate'],), ''),
1719 ((['git', 'config', 'rietveld.server'],), ''),
1720 ((['git', 'config', 'rietveld.server'],), ''),
1721 ((['git', 'symbolic-ref', 'HEAD'],), 'master')]
1722
1723 class MockChangelist():
1724 def __init__(self, branch, issue):
1725 self.branch = branch
1726 self.issue = issue
1727 def GetBranch(self):
1728 return self.branch
1729 def GetIssue(self):
1730 return self.issue
1731
1732 self.mock(git_cl, 'get_cl_statuses',
1733 lambda branches, fine_grained, max_processes:
1734 [(MockChangelist('master', 1), 'closed')])
1735
1736 self.assertEqual(1, git_cl.main(['archive', '-f']))
1737
tandrii@chromium.org9b7fd712016-06-01 13:45:20 +00001738 def test_cmd_issue_erase_existing(self):
1739 out = StringIO.StringIO()
1740 self.mock(git_cl.sys, 'stdout', out)
1741 self.calls = [
1742 ((['git', 'symbolic-ref', 'HEAD'],), 'feature'),
1743 ((['git', 'config', 'branch.feature.rietveldissue'],), ''),
1744 ((['git', 'config', 'branch.feature.gerritissue'],), '123'),
1745 ((['git', 'config', '--unset', 'branch.feature.gerritissue'],), ''),
1746 ((['git', 'config', '--unset', 'branch.feature.gerritpatchset'],), ''),
1747 # Let this command raise exception (retcode=1) - it should be ignored.
1748 ((['git', 'config', '--unset', 'branch.feature.last-upload-hash'],),
1749 '', subprocess2.CalledProcessError(1, '', '', '', '')),
1750 ((['git', 'config', '--unset', 'branch.feature.gerritserver'],), ''),
1751 ((['git', 'config', '--unset', 'branch.feature.gerritsquashhash'],),
1752 ''),
1753 ]
1754 self.assertEqual(0, git_cl.main(['issue', '0']))
1755
tandrii9de9ec62016-07-13 03:01:59 -07001756 def test_git_cl_try_default(self):
1757 self.mock(git_cl.Changelist, 'GetChange',
1758 lambda _, *a: (
1759 self._mocked_call(['GetChange']+list(a))))
1760 self.mock(git_cl.presubmit_support, 'DoGetTryMasters',
1761 lambda *_, **__: (
1762 self._mocked_call(['DoGetTryMasters'])))
1763 self.mock(git_cl.presubmit_support, 'DoGetTrySlaves',
1764 lambda *_, **__: (
1765 self._mocked_call(['DoGetTrySlaves'])))
1766 self.mock(git_cl._RietveldChangelistImpl, 'SetCQState',
1767 lambda _, s: self._mocked_call(['SetCQState', s]))
1768 self.calls = [
1769 ((['git', 'symbolic-ref', 'HEAD'],), 'feature'),
1770 ((['git', 'config', 'branch.feature.rietveldissue'],), '123'),
1771 ((['git', 'config', 'rietveld.autoupdate'],), ''),
1772 ((['git', 'config', 'rietveld.server'],),
1773 'https://codereview.chromium.org'),
1774 ((['git', 'config', 'branch.feature.rietveldserver'],), ''),
1775 ((['git', 'config', 'branch.feature.merge'],), 'feature'),
1776 ((['git', 'config', 'branch.feature.remote'],), 'origin'),
1777 ((['get_or_create_merge_base', 'feature', 'feature'],),
1778 'fake_ancestor_sha'),
1779 ((['GetChange', 'fake_ancestor_sha', None], ),
1780 git_cl.presubmit_support.GitChange(
1781 '', '', '', '', '', '', '', '')),
1782 ((['git', 'rev-parse', '--show-cdup'],), '../'),
1783 ((['DoGetTryMasters'], ), None),
1784 ((['DoGetTrySlaves'], ), None),
1785 ((['SetCQState', git_cl._CQState.DRY_RUN], ), None),
1786 ]
1787 out = StringIO.StringIO()
1788 self.mock(git_cl.sys, 'stdout', out)
1789 self.assertEqual(0, git_cl.main(['try']))
1790 self.assertEqual(
1791 out.getvalue(),
1792 'scheduled CQ Dry Run on https://codereview.chromium.org/123\n')
1793
tandrii16e0b4e2016-06-07 10:34:28 -07001794 def _common_GerritCommitMsgHookCheck(self):
1795 self.mock(git_cl.sys, 'stdout', StringIO.StringIO())
1796 self.mock(git_cl.os.path, 'abspath',
1797 lambda path: self._mocked_call(['abspath', path]))
1798 self.mock(git_cl.os.path, 'exists',
1799 lambda path: self._mocked_call(['exists', path]))
1800 self.mock(git_cl.gclient_utils, 'FileRead',
1801 lambda path: self._mocked_call(['FileRead', path]))
1802 self.mock(git_cl.gclient_utils, 'rm_file_or_tree',
1803 lambda path: self._mocked_call(['rm_file_or_tree', path]))
1804 self.calls = [
1805 ((['git', 'rev-parse', '--show-cdup'],), '../'),
1806 ((['abspath', '../'],), '/abs/git_repo_root'),
1807 ]
1808 return git_cl.Changelist(codereview='gerrit', issue=123)
1809
1810 def test_GerritCommitMsgHookCheck_custom_hook(self):
1811 cl = self._common_GerritCommitMsgHookCheck()
1812 self.calls += [
1813 ((['exists', '/abs/git_repo_root/.git/hooks/commit-msg'],), True),
1814 ((['FileRead', '/abs/git_repo_root/.git/hooks/commit-msg'],),
1815 '#!/bin/sh\necho "custom hook"')
1816 ]
1817 cl._codereview_impl._GerritCommitMsgHookCheck(offer_removal=True)
1818
1819 def test_GerritCommitMsgHookCheck_not_exists(self):
1820 cl = self._common_GerritCommitMsgHookCheck()
1821 self.calls += [
1822 ((['exists', '/abs/git_repo_root/.git/hooks/commit-msg'],), False),
1823 ]
1824 cl._codereview_impl._GerritCommitMsgHookCheck(offer_removal=True)
1825
1826 def test_GerritCommitMsgHookCheck(self):
1827 cl = self._common_GerritCommitMsgHookCheck()
1828 self.calls += [
1829 ((['exists', '/abs/git_repo_root/.git/hooks/commit-msg'],), True),
1830 ((['FileRead', '/abs/git_repo_root/.git/hooks/commit-msg'],),
1831 '...\n# From Gerrit Code Review\n...\nadd_ChangeId()\n'),
1832 (('Do you want to remove it now? [Yes/No]',), 'Yes'),
1833 ((['rm_file_or_tree', '/abs/git_repo_root/.git/hooks/commit-msg'],),
1834 ''),
1835 ]
1836 cl._codereview_impl._GerritCommitMsgHookCheck(offer_removal=True)
1837
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +00001838
maruel@chromium.orgddd59412011-11-30 14:20:38 +00001839if __name__ == '__main__':
maruel@chromium.org78936cb2013-04-11 00:17:52 +00001840 git_cl.logging.basicConfig(
1841 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR)
maruel@chromium.orgddd59412011-11-30 14:20:38 +00001842 unittest.main()