blob: a9c1072dd58fc53ab8d53a53398245538545dbc4 [file] [log] [blame]
maruel@chromium.orgddd59412011-11-30 14:20:38 +00001#!/usr/bin/env python
maruel@chromium.orgeb5edbc2012-01-16 17:03:28 +00002# Copyright (c) 2012 The Chromium Authors. All rights reserved.
maruel@chromium.orgddd59412011-11-30 14:20:38 +00003# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Unit tests for git_cl.py."""
7
8import os
maruel@chromium.orga3353652011-11-30 14:26:57 +00009import StringIO
ukai@chromium.org78c4b982012-02-14 02:20:26 +000010import stat
maruel@chromium.orgddd59412011-11-30 14:20:38 +000011import sys
12import unittest
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +000013import urlparse
maruel@chromium.orgddd59412011-11-30 14:20:38 +000014
15sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
16
17from testing_support.auto_stub import TestCase
18
19import git_cl
iannucci@chromium.org9e849272014-04-04 00:31:55 +000020import git_common
tandrii@chromium.org57d86542016-03-04 16:11:32 +000021import git_footers
maruel@chromium.orgddd59412011-11-30 14:20:38 +000022import subprocess2
maruel@chromium.orgddd59412011-11-30 14:20:38 +000023
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000024class PresubmitMock(object):
25 def __init__(self, *args, **kwargs):
26 self.reviewers = []
27 @staticmethod
28 def should_continue():
29 return True
30
31
32class RietveldMock(object):
33 def __init__(self, *args, **kwargs):
34 pass
maruel@chromium.org78936cb2013-04-11 00:17:52 +000035
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000036 @staticmethod
37 def get_description(issue):
38 return 'Issue: %d' % issue
39
maruel@chromium.org78936cb2013-04-11 00:17:52 +000040 @staticmethod
41 def get_issue_properties(_issue, _messages):
42 return {
43 'reviewers': ['joe@chromium.org', 'john@chromium.org'],
44 'messages': [
45 {
46 'approval': True,
47 'sender': 'john@chromium.org',
48 },
49 ],
50 }
51
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000052
53class WatchlistsMock(object):
54 def __init__(self, _):
55 pass
56 @staticmethod
57 def GetWatchersForPaths(_):
58 return ['joe@example.com']
59
60
ukai@chromium.org78c4b982012-02-14 02:20:26 +000061class CodereviewSettingsFileMock(object):
62 def __init__(self):
63 pass
64 # pylint: disable=R0201
65 def read(self):
66 return ("CODE_REVIEW_SERVER: gerrit.chromium.org\n" +
andybons@chromium.org11f46eb2016-02-02 19:26:51 +000067 "GERRIT_HOST: True\n")
ukai@chromium.org78c4b982012-02-14 02:20:26 +000068
69
vadimsh@chromium.orgeed4df32015-04-10 21:30:20 +000070class AuthenticatorMock(object):
71 def __init__(self, *_args):
72 pass
73 def has_cached_credentials(self):
74 return True
75
76
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +000077class TestGitClBasic(unittest.TestCase):
78 def _test_ParseIssueUrl(self, func, url, issue, patchset, hostname, fail):
79 parsed = urlparse.urlparse(url)
80 result = func(parsed)
81 if fail:
82 self.assertIsNone(result)
83 return None
84 self.assertIsNotNone(result)
85 self.assertEqual(result.issue, issue)
86 self.assertEqual(result.patchset, patchset)
87 self.assertEqual(result.hostname, hostname)
88 return result
89
90 def test_ParseIssueURL_rietveld(self):
91 def test(url, issue=None, patchset=None, hostname=None, patch_url=None,
92 fail=None):
93 result = self._test_ParseIssueUrl(
94 git_cl._RietveldChangelistImpl.ParseIssueURL,
95 url, issue, patchset, hostname, fail)
96 if not fail:
97 self.assertEqual(result.patch_url, patch_url)
98
99 test('http://codereview.chromium.org/123',
100 123, None, 'codereview.chromium.org')
101 test('https://codereview.chromium.org/123',
102 123, None, 'codereview.chromium.org')
103 test('https://codereview.chromium.org/123/',
104 123, None, 'codereview.chromium.org')
105 test('https://codereview.chromium.org/123/whatever',
106 123, None, 'codereview.chromium.org')
107 test('http://codereview.chromium.org/download/issue123_4.diff',
108 123, 4, 'codereview.chromium.org',
109 patch_url='https://codereview.chromium.org/download/issue123_4.diff')
110 # This looks like bad Gerrit, but is actually valid Rietveld.
111 test('https://chrome-review.source.com/123/4/',
112 123, None, 'chrome-review.source.com')
113
114 test('https://codereview.chromium.org/deadbeaf', fail=True)
115 test('https://codereview.chromium.org/api/123', fail=True)
116 test('bad://codereview.chromium.org/123', fail=True)
117 test('http://codereview.chromium.org/download/issue123_4.diffff', fail=True)
118
119 def test_ParseIssueURL_gerrit(self):
120 def test(url, issue=None, patchset=None, hostname=None, fail=None):
121 self._test_ParseIssueUrl(
122 git_cl._GerritChangelistImpl.ParseIssueURL,
123 url, issue, patchset, hostname, fail)
124
125 test('http://chrome-review.source.com/c/123',
126 123, None, 'chrome-review.source.com')
127 test('https://chrome-review.source.com/c/123/',
128 123, None, 'chrome-review.source.com')
129 test('https://chrome-review.source.com/c/123/4',
130 123, 4, 'chrome-review.source.com')
131 test('https://chrome-review.source.com/#/c/123/4',
132 123, 4, 'chrome-review.source.com')
133 test('https://chrome-review.source.com/c/123/4',
134 123, 4, 'chrome-review.source.com')
135 test('https://chrome-review.source.com/123',
136 123, None, 'chrome-review.source.com')
137 test('https://chrome-review.source.com/123/4',
138 123, 4, 'chrome-review.source.com')
139
140 test('https://chrome-review.source.com/c/123/1/whatisthis', fail=True)
141 test('https://chrome-review.source.com/c/abc/', fail=True)
142 test('ssh://chrome-review.source.com/c/123/1/', fail=True)
143
144 def test_ParseIssueNumberArgument(self):
145 def test(arg, issue=None, patchset=None, hostname=None, fail=False):
146 result = git_cl.ParseIssueNumberArgument(arg)
147 self.assertIsNotNone(result)
148 if fail:
149 self.assertFalse(result.valid)
150 else:
151 self.assertEqual(result.issue, issue)
152 self.assertEqual(result.patchset, patchset)
153 self.assertEqual(result.hostname, hostname)
154
155 test('123', 123)
156 test('', fail=True)
157 test('abc', fail=True)
158 test('123/1', fail=True)
159 test('123a', fail=True)
160 test('ssh://chrome-review.source.com/#/c/123/4/', fail=True)
161 # Rietveld.
162 test('https://codereview.source.com/123',
163 123, None, 'codereview.source.com')
164 test('https://codereview.source.com/www123', fail=True)
165 # Gerrrit.
166 test('https://chrome-review.source.com/c/123/4',
167 123, 4, 'chrome-review.source.com')
168 test('https://chrome-review.source.com/bad/123/4', fail=True)
169
170
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000171class TestGitCl(TestCase):
172 def setUp(self):
173 super(TestGitCl, self).setUp()
174 self.calls = []
175 self._calls_done = 0
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000176 self.mock(subprocess2, 'call', self._mocked_call)
177 self.mock(subprocess2, 'check_call', self._mocked_call)
178 self.mock(subprocess2, 'check_output', self._mocked_call)
179 self.mock(subprocess2, 'communicate', self._mocked_call)
tandrii@chromium.orga342c922016-03-16 07:08:25 +0000180 self.mock(git_cl.gclient_utils, 'CheckCallAndFilter', self._mocked_call)
sbc@chromium.org71437c02015-04-09 19:29:40 +0000181 self.mock(git_common, 'is_dirty_git_tree', lambda x: False)
iannucci@chromium.org9e849272014-04-04 00:31:55 +0000182 self.mock(git_common, 'get_or_create_merge_base',
183 lambda *a: (
184 self._mocked_call(['get_or_create_merge_base']+list(a))))
pgervais@chromium.org8ba38ff2015-06-11 21:41:25 +0000185 self.mock(git_cl, 'BranchExists', lambda _: True)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000186 self.mock(git_cl, 'FindCodereviewSettingsFile', lambda: '')
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000187 self.mock(git_cl, 'ask_for_data', self._mocked_call)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000188 self.mock(git_cl.presubmit_support, 'DoPresubmitChecks', PresubmitMock)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000189 self.mock(git_cl.rietveld, 'Rietveld', RietveldMock)
maruel@chromium.org4bac4b52012-11-27 20:33:52 +0000190 self.mock(git_cl.rietveld, 'CachingRietveld', RietveldMock)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000191 self.mock(git_cl.upload, 'RealMain', self.fail)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000192 self.mock(git_cl.watchlists, 'Watchlists', WatchlistsMock)
vadimsh@chromium.orgeed4df32015-04-10 21:30:20 +0000193 self.mock(git_cl.auth, 'get_authenticator_for_host', AuthenticatorMock)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000194 # It's important to reset settings to not have inter-tests interference.
195 git_cl.settings = None
196
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +0000197
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000198 def tearDown(self):
wychen@chromium.org445c8962015-04-28 23:30:05 +0000199 try:
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +0000200 # Note: has_failed returns True if at least 1 test ran so far, current
201 # included, has failed. That means current test may have actually ran
202 # fine, and the check for no leftover calls would be skipped.
wychen@chromium.org445c8962015-04-28 23:30:05 +0000203 if not self.has_failed():
204 self.assertEquals([], self.calls)
205 finally:
206 super(TestGitCl, self).tearDown()
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000207
iannucci@chromium.org9e849272014-04-04 00:31:55 +0000208 def _mocked_call(self, *args, **_kwargs):
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000209 self.assertTrue(
210 self.calls,
211 '@%d Expected: <Missing> Actual: %r' % (self._calls_done, args))
wychen@chromium.orga872e752015-04-28 23:42:18 +0000212 top = self.calls.pop(0)
213 if len(top) > 2 and top[2]:
214 raise top[2]
215 expected_args, result = top
216
maruel@chromium.orge52678e2013-04-26 18:34:44 +0000217 # Also logs otherwise it could get caught in a try/finally and be hard to
218 # diagnose.
219 if expected_args != args:
220 msg = '@%d Expected: %r Actual: %r' % (
221 self._calls_done, expected_args, args)
222 git_cl.logging.error(msg)
223 self.fail(msg)
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000224 self._calls_done += 1
225 return result
226
maruel@chromium.orga3353652011-11-30 14:26:57 +0000227 @classmethod
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000228 def _is_gerrit_calls(cls, gerrit=False):
229 return [((['git', 'config', 'rietveld.autoupdate'],), ''),
230 ((['git', 'config', 'gerrit.host'],), 'True' if gerrit else '')]
231
232 @classmethod
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000233 def _upload_calls(cls, similarity, find_copies, private):
iannucci@chromium.org79540052012-10-19 23:15:26 +0000234 return (cls._git_base_calls(similarity, find_copies) +
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000235 cls._git_upload_calls(private))
maruel@chromium.orga3353652011-11-30 14:26:57 +0000236
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000237 @classmethod
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000238 def _upload_no_rev_calls(cls, similarity, find_copies):
239 return (cls._git_base_calls(similarity, find_copies) +
240 cls._git_upload_no_rev_calls())
241
242 @classmethod
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000243 def _git_base_calls(cls, similarity, find_copies):
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000244 if similarity is None:
245 similarity = '50'
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000246 similarity_call = ((['git', 'config', '--int', '--get',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000247 'branch.master.git-cl-similarity'],), '')
248 else:
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000249 similarity_call = ((['git', 'config', '--int',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000250 'branch.master.git-cl-similarity', similarity],), '')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000251
252 if find_copies is None:
253 find_copies = True
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000254 find_copies_call = ((['git', 'config', '--int', '--get',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000255 'branch.master.git-find-copies'],), '')
256 else:
257 val = str(int(find_copies))
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000258 find_copies_call = ((['git', 'config', '--int',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000259 'branch.master.git-find-copies', val],), '')
260
261 if find_copies:
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000262 stat_call = ((['git', 'diff', '--no-ext-diff', '--stat',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000263 '--find-copies-harder', '-l100000', '-C'+similarity,
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000264 'fake_ancestor_sha', 'HEAD'],), '+dat')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000265 else:
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000266 stat_call = ((['git', 'diff', '--no-ext-diff', '--stat',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000267 '-M'+similarity, 'fake_ancestor_sha', 'HEAD'],), '+dat')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000268
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000269 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000270 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
tandrii@chromium.org87985d22016-03-24 17:33:33 +0000271 similarity_call,
272 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
273 find_copies_call,
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000274 ] + cls._is_gerrit_calls() + [
tandrii@chromium.org87985d22016-03-24 17:33:33 +0000275 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +0000276 ((['git', 'config', 'branch.master.rietveldissue'],), ''),
277 ((['git', 'config', 'branch.master.gerritissue'],), ''),
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +0000278 ((['git', 'config', 'rietveld.server'],),
279 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000280 ((['git', 'config', 'branch.master.merge'],), 'master'),
281 ((['git', 'config', 'branch.master.remote'],), 'origin'),
iannucci@chromium.org9e849272014-04-04 00:31:55 +0000282 ((['get_or_create_merge_base', 'master', 'master'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000283 'fake_ancestor_sha'),
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000284 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000285 ((['git', 'rev-parse', '--show-cdup'],), ''),
286 ((['git', 'rev-parse', 'HEAD'],), '12345'),
287 ((['git', 'diff', '--name-status', '--no-renames', '-r',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000288 'fake_ancestor_sha...', '.'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000289 'M\t.gitignore\n'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000290 ((['git', 'config', 'branch.master.rietveldpatchset'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000291 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000292 ((['git', 'log', '--pretty=format:%s%n%n%b',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000293 'fake_ancestor_sha...'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000294 'foo'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000295 ((['git', 'config', 'user.email'],), 'me@example.com'),
iannucci@chromium.org79540052012-10-19 23:15:26 +0000296 stat_call,
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000297 ((['git', 'log', '--pretty=format:%s\n\n%b',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000298 'fake_ancestor_sha..HEAD'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000299 'desc\n'),
rmistry@google.com90752582014-01-14 21:04:50 +0000300 ((['git', 'config', 'rietveld.bug-prefix'],), ''),
maruel@chromium.orga3353652011-11-30 14:26:57 +0000301 ]
302
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000303 @classmethod
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000304 def _git_upload_no_rev_calls(cls):
305 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000306 ((['git', 'config', 'core.editor'],), ''),
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000307 ]
308
309 @classmethod
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000310 def _git_upload_calls(cls, private):
311 if private:
tyoshino@chromium.org99918ab2013-09-30 06:17:28 +0000312 cc_call = []
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000313 private_call = []
314 else:
tyoshino@chromium.org99918ab2013-09-30 06:17:28 +0000315 cc_call = [((['git', 'config', 'rietveld.cc'],), '')]
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000316 private_call = [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000317 ((['git', 'config', 'rietveld.private'],), '')]
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000318
maruel@chromium.orga3353652011-11-30 14:26:57 +0000319 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000320 ((['git', 'config', 'core.editor'],), ''),
tyoshino@chromium.org99918ab2013-09-30 06:17:28 +0000321 ] + cc_call + private_call + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000322 ((['git', 'config', 'branch.master.base-url'],), ''),
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +0000323 ((['git', 'config', 'rietveld.pending-ref-prefix'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000324 ((['git',
bratell@opera.com05fb9112014-07-07 09:30:23 +0000325 'config', '--local', '--get-regexp', '^svn-remote\\.'],),
326 (('', None), 0)),
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000327 ((['git', 'rev-parse', '--show-cdup'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000328 ((['git', 'svn', 'info'],), ''),
sheyang@chromium.org152cf832014-06-11 21:37:49 +0000329 ((['git', 'config', 'rietveld.project'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000330 ((['git',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000331 'config', 'branch.master.rietveldissue', '1'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000332 ((['git', 'config', 'branch.master.rietveldserver',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000333 'https://codereview.example.com'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000334 ((['git',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000335 'config', 'branch.master.rietveldpatchset', '2'],), ''),
tandrii@chromium.org1e67bb72016-02-11 12:15:49 +0000336 ] + cls._git_post_upload_calls()
337
338 @classmethod
339 def _git_post_upload_calls(cls):
340 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000341 ((['git', 'rev-parse', 'HEAD'],), 'hash'),
342 ((['git', 'symbolic-ref', 'HEAD'],), 'hash'),
343 ((['git',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000344 'config', 'branch.hash.last-upload-hash', 'hash'],), ''),
rmistry@google.com5626a922015-02-26 14:03:30 +0000345 ((['git', 'config', 'rietveld.run-post-upload-hook'],), ''),
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000346 ]
347
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000348 @staticmethod
349 def _git_sanity_checks(diff_base, working_branch):
350 fake_ancestor = 'fake_ancestor'
351 fake_cl = 'fake_cl_for_patch'
352 return [
353 # Calls to verify branch point is ancestor
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000354 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000355 'rev-parse', '--verify', diff_base],), fake_ancestor),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000356 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000357 'merge-base', fake_ancestor, 'HEAD'],), fake_ancestor),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000358 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000359 'rev-list', '^' + fake_ancestor, 'HEAD'],), fake_cl),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000360 # Mock a config miss (error code 1)
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000361 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000362 'config', 'gitcl.remotebranch'],), (('', None), 1)),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000363 # Call to GetRemoteBranch()
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000364 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000365 'config', 'branch.%s.merge' % working_branch],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000366 'refs/heads/master'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000367 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000368 'config', 'branch.%s.remote' % working_branch],), 'origin'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000369 ((['git', 'rev-list', '^' + fake_ancestor,
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000370 'refs/remotes/origin/master'],), ''),
371 ]
372
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000373 @classmethod
374 def _dcommit_calls_1(cls):
375 return [
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +0000376 ((['git', 'config', 'rietveld.autoupdate'],),
377 ''),
378 ((['git', 'config', 'rietveld.pending-ref-prefix'],),
379 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000380 ((['git',
bratell@opera.com05fb9112014-07-07 09:30:23 +0000381 'config', '--local', '--get-regexp', '^svn-remote\\.'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000382 ((('svn-remote.svn.url svn://svn.chromium.org/chrome\n'
383 'svn-remote.svn.fetch trunk/src:refs/remotes/origin/master'),
384 None),
385 0)),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000386 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
387 ((['git', 'config', '--int', '--get',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000388 'branch.working.git-cl-similarity'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000389 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
390 ((['git', 'config', '--int', '--get',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000391 'branch.working.git-find-copies'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000392 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
393 ((['git',
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +0000394 'config', 'branch.working.rietveldissue'],), '12345'),
395 ((['git',
396 'config', 'rietveld.server'],), 'codereview.example.com'),
397 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000398 'config', 'branch.working.merge'],), 'refs/heads/master'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000399 ((['git', 'config', 'branch.working.remote'],), 'origin'),
iannucci@chromium.org5724c962014-04-11 09:32:56 +0000400 ((['git', 'config', 'branch.working.merge'],),
401 'refs/heads/master'),
402 ((['git', 'config', 'branch.working.remote'],), 'origin'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000403 ((['git', 'rev-list', '--merges',
szager@chromium.orge84b7542012-06-15 21:26:58 +0000404 '--grep=^SVN changes up to revision [0-9]*$',
szager@chromium.org9bb85e22012-06-13 20:28:23 +0000405 'refs/remotes/origin/master^!'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000406 ((['git', 'rev-list', '^refs/heads/working',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000407 'refs/remotes/origin/master'],),
408 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000409 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000410 'log', '--grep=^git-svn-id:', '-1', '--pretty=format:%H'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000411 '3fc18b62c4966193eb435baabe2d18a3810ec82e'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000412 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000413 'rev-list', '^3fc18b62c4966193eb435baabe2d18a3810ec82e',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000414 'refs/remotes/origin/master'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000415 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000416 'merge-base', 'refs/remotes/origin/master', 'HEAD'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000417 'fake_ancestor_sha'),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000418 ]
419
420 @classmethod
421 def _dcommit_calls_normal(cls):
422 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000423 ((['git', 'rev-parse', '--show-cdup'],), ''),
424 ((['git', 'rev-parse', 'HEAD'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000425 '00ff397798ea57439712ed7e04ab96e13969ef40'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000426 ((['git',
mcgrathr@chromium.org9249f642013-06-03 21:36:18 +0000427 'diff', '--name-status', '--no-renames', '-r', 'fake_ancestor_sha...',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000428 '.'],),
429 'M\tPRESUBMIT.py'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000430 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000431 'config', 'branch.working.rietveldpatchset'],), '31137'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000432 ((['git', 'config', 'branch.working.rietveldserver'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000433 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000434 ((['git', 'config', 'user.email'],), 'author@example.com'),
435 ((['git', 'config', 'rietveld.tree-status-url'],), ''),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000436 ]
437
438 @classmethod
439 def _dcommit_calls_bypassed(cls):
440 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000441 ((['git', 'config', 'branch.working.rietveldserver'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000442 'codereview.example.com'),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000443 ]
444
445 @classmethod
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000446 def _dcommit_calls_3(cls):
447 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000448 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000449 'diff', '--no-ext-diff', '--stat', '--find-copies-harder',
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000450 '-l100000', '-C50', 'fake_ancestor_sha',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000451 'refs/heads/working'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000452 (' PRESUBMIT.py | 2 +-\n'
453 ' 1 files changed, 1 insertions(+), 1 deletions(-)\n')),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000454 ((['git', 'show-ref', '--quiet', '--verify',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000455 'refs/heads/git-cl-commit'],),
456 (('', None), 0)),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000457 ((['git', 'branch', '-D', 'git-cl-commit'],), ''),
458 ((['git', 'show-ref', '--quiet', '--verify',
szager@chromium.org9bb85e22012-06-13 20:28:23 +0000459 'refs/heads/git-cl-cherry-pick'],), ''),
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000460 ((['git', 'rev-parse', '--show-cdup'],), '\n'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000461 ((['git', 'checkout', '-q', '-b', 'git-cl-commit'],), ''),
462 ((['git', 'reset', '--soft', 'fake_ancestor_sha'],), ''),
463 ((['git', 'commit', '-m',
maruel@chromium.orge52678e2013-04-26 18:34:44 +0000464 'Issue: 12345\n\nR=john@chromium.org\n\n'
sergiyb@chromium.org4b39c5f2015-07-07 10:33:12 +0000465 'Review URL: https://codereview.example.com/12345 .'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000466 ''),
kjellander@chromium.org6abc6522014-12-02 07:34:49 +0000467 ((['git', 'config', 'rietveld.force-https-commit-url'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000468 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000469 'svn', 'dcommit', '-C50', '--no-rebase', '--rmdir'],),
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000470 (('', None), 0)),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000471 ((['git', 'checkout', '-q', 'working'],), ''),
472 ((['git', 'branch', '-D', 'git-cl-commit'],), ''),
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000473 ]
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000474
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000475 @staticmethod
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000476 def _cmd_line(description, args, similarity, find_copies, private):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000477 """Returns the upload command line passed to upload.RealMain()."""
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000478 return [
479 'upload', '--assume_yes', '--server',
maruel@chromium.orgeb5edbc2012-01-16 17:03:28 +0000480 'https://codereview.example.com',
maruel@chromium.org71e12a92012-02-14 02:34:15 +0000481 '--message', description
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000482 ] + args + [
483 '--cc', 'joe@example.com',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000484 ] + (['--private'] if private else []) + [
iannucci@chromium.org79540052012-10-19 23:15:26 +0000485 '--git_similarity', similarity or '50'
486 ] + (['--git_no_find_copies'] if find_copies == False else []) + [
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000487 'fake_ancestor_sha', 'HEAD'
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000488 ]
489
490 def _run_reviewer_test(
491 self,
492 upload_args,
493 expected_description,
494 returned_description,
495 final_description,
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000496 reviewers,
497 private=False):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000498 """Generic reviewer test framework."""
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000499 try:
500 similarity = upload_args[upload_args.index('--similarity')+1]
501 except ValueError:
502 similarity = None
iannucci@chromium.org79540052012-10-19 23:15:26 +0000503
504 if '--find-copies' in upload_args:
505 find_copies = True
506 elif '--no-find-copies' in upload_args:
507 find_copies = False
508 else:
509 find_copies = None
510
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000511 private = '--private' in upload_args
512
513 self.calls = self._upload_calls(similarity, find_copies, private)
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000514
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000515 def RunEditor(desc, _, **kwargs):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000516 self.assertEquals(
517 '# Enter a description of the change.\n'
janx@chromium.org104b2db2013-04-18 12:58:40 +0000518 '# This will be displayed on the codereview site.\n'
alancutter@chromium.org63a4d7f2013-05-31 02:22:45 +0000519 '# The first line will also be used as the subject of the review.\n'
alancutter@chromium.orgbd1073e2013-06-01 00:34:38 +0000520 '#--------------------This line is 72 characters long'
521 '--------------------\n' +
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000522 expected_description,
523 desc)
524 return returned_description
525 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor)
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000526
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000527 def check_upload(args):
iannucci@chromium.org79540052012-10-19 23:15:26 +0000528 cmd_line = self._cmd_line(final_description, reviewers, similarity,
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000529 find_copies, private)
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000530 self.assertEquals(cmd_line, args)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000531 return 1, 2
532 self.mock(git_cl.upload, 'RealMain', check_upload)
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000533
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000534 git_cl.main(['upload'] + upload_args)
535
536 def test_no_reviewer(self):
537 self._run_reviewer_test(
538 [],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000539 'desc\n\nBUG=',
540 '# Blah blah comment.\ndesc\n\nBUG=',
541 'desc\n\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000542 [])
543
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000544 def test_keep_similarity(self):
545 self._run_reviewer_test(
546 ['--similarity', '70'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000547 'desc\n\nBUG=',
548 '# Blah blah comment.\ndesc\n\nBUG=',
549 'desc\n\nBUG=',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000550 [])
551
iannucci@chromium.org79540052012-10-19 23:15:26 +0000552 def test_keep_find_copies(self):
553 self._run_reviewer_test(
554 ['--no-find-copies'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000555 'desc\n\nBUG=',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000556 '# Blah blah comment.\ndesc\n\nBUG=\n',
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000557 'desc\n\nBUG=',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000558 [])
559
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000560 def test_private(self):
561 self._run_reviewer_test(
562 ['--private'],
563 'desc\n\nBUG=',
564 '# Blah blah comment.\ndesc\n\nBUG=\n',
565 'desc\n\nBUG=',
566 [])
567
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000568 def test_reviewers_cmd_line(self):
569 # Reviewer is passed as-is
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000570 description = 'desc\n\nR=foo@example.com\nBUG='
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000571 self._run_reviewer_test(
572 ['-r' 'foo@example.com'],
573 description,
574 '\n%s\n' % description,
575 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000576 ['--reviewers=foo@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000577
578 def test_reviewer_tbr_overriden(self):
579 # Reviewer is overriden with TBR
580 # Also verifies the regexp work without a trailing LF
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000581 description = 'Foo Bar\n\nTBR=reviewer@example.com'
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000582 self._run_reviewer_test(
583 ['-r' 'foo@example.com'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000584 'desc\n\nR=foo@example.com\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000585 description.strip('\n'),
586 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000587 ['--reviewers=reviewer@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000588
589 def test_reviewer_multiple(self):
590 # Handles multiple R= or TBR= lines.
591 description = (
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000592 'Foo Bar\nTBR=reviewer@example.com\nBUG=\nR=another@example.com')
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000593 self._run_reviewer_test(
594 [],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000595 'desc\n\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000596 description,
597 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000598 ['--reviewers=another@example.com,reviewer@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000599
maruel@chromium.orga3353652011-11-30 14:26:57 +0000600 def test_reviewer_send_mail(self):
601 # --send-mail can be used without -r if R= is used
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000602 description = 'Foo Bar\nR=reviewer@example.com'
maruel@chromium.orga3353652011-11-30 14:26:57 +0000603 self._run_reviewer_test(
604 ['--send-mail'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000605 'desc\n\nBUG=',
maruel@chromium.orga3353652011-11-30 14:26:57 +0000606 description.strip('\n'),
607 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000608 ['--reviewers=reviewer@example.com', '--send_mail'])
maruel@chromium.orga3353652011-11-30 14:26:57 +0000609
610 def test_reviewer_send_mail_no_rev(self):
611 # Fails without a reviewer.
maruel@chromium.org2e23ce32013-05-07 12:42:28 +0000612 stdout = StringIO.StringIO()
613 stderr = StringIO.StringIO()
maruel@chromium.orga3353652011-11-30 14:26:57 +0000614 try:
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000615 self.calls = self._upload_no_rev_calls(None, None)
616 def RunEditor(desc, _, **kwargs):
maruel@chromium.orga3353652011-11-30 14:26:57 +0000617 return desc
618 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor)
maruel@chromium.org2e23ce32013-05-07 12:42:28 +0000619 self.mock(sys, 'stdout', stdout)
620 self.mock(sys, 'stderr', stderr)
maruel@chromium.orga3353652011-11-30 14:26:57 +0000621 git_cl.main(['upload', '--send-mail'])
622 self.fail()
623 except SystemExit:
maruel@chromium.org2e23ce32013-05-07 12:42:28 +0000624 self.assertEqual(
625 'Using 50% similarity for rename/copy detection. Override with '
626 '--similarity.\n',
627 stdout.getvalue())
628 self.assertEqual(
629 'Must specify reviewers to send email.\n', stderr.getvalue())
maruel@chromium.orga3353652011-11-30 14:26:57 +0000630
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000631 def test_dcommit(self):
632 self.calls = (
633 self._dcommit_calls_1() +
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000634 self._git_sanity_checks('fake_ancestor_sha', 'working') +
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000635 self._dcommit_calls_normal() +
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000636 self._dcommit_calls_3())
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000637 git_cl.main(['dcommit'])
638
639 def test_dcommit_bypass_hooks(self):
640 self.calls = (
641 self._dcommit_calls_1() +
642 self._dcommit_calls_bypassed() +
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000643 self._dcommit_calls_3())
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000644 git_cl.main(['dcommit', '--bypass-hooks'])
645
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000646
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000647 @classmethod
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000648 def _gerrit_base_calls(cls, issue=None):
ukai@chromium.orge8077812012-02-03 03:41:46 +0000649 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000650 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
651 ((['git', 'config', '--int', '--get',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000652 'branch.master.git-cl-similarity'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000653 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
654 ((['git', 'config', '--int', '--get',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000655 'branch.master.git-find-copies'],), ''),
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000656 ] + cls._is_gerrit_calls(True) + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000657 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +0000658 ((['git', 'config', 'branch.master.rietveldissue'],), ''),
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000659 ((['git', 'config', 'branch.master.gerritissue'],),
660 '' if issue is None else str(issue)),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000661 ((['git', 'config', 'branch.master.merge'],), 'master'),
662 ((['git', 'config', 'branch.master.remote'],), 'origin'),
iannucci@chromium.org9e849272014-04-04 00:31:55 +0000663 ((['get_or_create_merge_base', 'master', 'master'],),
664 'fake_ancestor_sha'),
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +0000665 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000666 ((['git', 'rev-parse', '--show-cdup'],), ''),
667 ((['git', 'rev-parse', 'HEAD'],), '12345'),
668 ((['git',
mcgrathr@chromium.org9249f642013-06-03 21:36:18 +0000669 'diff', '--name-status', '--no-renames', '-r',
670 'fake_ancestor_sha...', '.'],),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000671 'M\t.gitignore\n'),
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +0000672 ((['git', 'config', 'branch.master.gerritpatchset'],), ''),
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000673 ] + ([] if issue else [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000674 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000675 'log', '--pretty=format:%s%n%n%b', 'fake_ancestor_sha...'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000676 'foo'),
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000677 ]) + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000678 ((['git', 'config', 'user.email'],), 'me@example.com'),
679 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000680 'diff', '--no-ext-diff', '--stat', '--find-copies-harder',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000681 '-l100000', '-C50', 'fake_ancestor_sha', 'HEAD'],),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000682 '+dat'),
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +0000683 ]
ukai@chromium.orge8077812012-02-03 03:41:46 +0000684
tandrii@chromium.org1e67bb72016-02-11 12:15:49 +0000685 @classmethod
686 def _gerrit_upload_calls(cls, description, reviewers, squash,
tandrii@chromium.org10625002016-03-04 20:03:47 +0000687 expected_upstream_ref='origin/refs/heads/master',
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000688 post_amend_description=None, issue=None):
tandrii@chromium.org10625002016-03-04 20:03:47 +0000689 if post_amend_description is None:
690 post_amend_description = description
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000691
ukai@chromium.orge8077812012-02-03 03:41:46 +0000692 calls = [
bauerb@chromium.org54b400c2016-01-14 10:08:25 +0000693 ((['git', 'config', '--bool', 'gerrit.squash-uploads'],), 'false'),
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000694 ]
695 # If issue is given, then description is fetched from Gerrit instead.
696 if issue is None:
697 if squash:
698 calls += [
699 ((['git', 'show', '--format=%B', '-s',
700 'refs/heads/git_cl_uploads/master'],), '')]
701 calls += [
702 ((['git', 'log', '--pretty=format:%s\n\n%b',
703 'fake_ancestor_sha..HEAD'],),
704 description)]
tandrii@chromium.org57d86542016-03-04 16:11:32 +0000705 if not git_footers.get_footer_change_id(description) and not squash:
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000706 calls += [
tandrii@chromium.org10625002016-03-04 20:03:47 +0000707 # DownloadGerritHook(False)
708 ((False, ),
709 ''),
710 # Amending of commit message to get the Change-Id.
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000711 ((['git', 'log', '--pretty=format:%s\n\n%b',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000712 'fake_ancestor_sha..HEAD'],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000713 description),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000714 ((['git', 'commit', '--amend', '-m', description],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000715 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000716 ((['git', 'log', '--pretty=format:%s\n\n%b',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000717 'fake_ancestor_sha..HEAD'],),
tandrii@chromium.org10625002016-03-04 20:03:47 +0000718 post_amend_description)
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000719 ]
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000720 if squash:
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000721 if not issue:
722 # Prompting to edit description on first upload.
723 calls += [
724 ((['git', 'config', 'core.editor'],), ''),
725 ((['RunEditor'],), description),
726 ]
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000727 ref_to_push = 'abcdef0123456789'
728 calls += [
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000729 ((['git', 'config', 'branch.master.merge'],),
730 'refs/heads/master'),
731 ((['git', 'config', 'branch.master.remote'],),
732 'origin'),
733 ((['get_or_create_merge_base', 'master', 'master'],),
734 'origin/master'),
735 ((['git', 'rev-parse', 'HEAD:'],),
736 '0123456789abcdef'),
737 ((['git', 'commit-tree', '0123456789abcdef', '-p',
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000738 'origin/master', '-m', description],),
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000739 ref_to_push),
740 ]
741 else:
742 ref_to_push = 'HEAD'
743
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000744 calls += [
luqui@chromium.org609f3952015-05-04 22:47:04 +0000745 ((['git', 'rev-list',
746 expected_upstream_ref + '..' + ref_to_push],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000747 ((['git', 'config', 'rietveld.cc'],), '')
ukai@chromium.orge8077812012-02-03 03:41:46 +0000748 ]
ukai@chromium.org19bbfa22012-02-03 16:18:11 +0000749 receive_pack = '--receive-pack=git receive-pack '
ukai@chromium.orge8077812012-02-03 03:41:46 +0000750 receive_pack += '--cc=joe@example.com' # from watch list
751 if reviewers:
752 receive_pack += ' '
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000753 receive_pack += ' '.join(
754 '--reviewer=' + email for email in sorted(reviewers))
ukai@chromium.org19bbfa22012-02-03 16:18:11 +0000755 receive_pack += ''
ukai@chromium.orge8077812012-02-03 03:41:46 +0000756 calls += [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000757 ((['git',
luqui@chromium.org609f3952015-05-04 22:47:04 +0000758 'push', receive_pack, 'origin',
759 ref_to_push + ':refs/for/refs/heads/master'],),
tandrii@chromium.orga342c922016-03-16 07:08:25 +0000760 ('remote:\n'
761 'remote: Processing changes: (\)\n'
762 'remote: Processing changes: (|)\n'
763 'remote: Processing changes: (/)\n'
764 'remote: Processing changes: (-)\n'
765 'remote: Processing changes: new: 1 (/)\n'
766 'remote: Processing changes: new: 1, done\n'
767 'remote:\n'
768 'remote: New Changes:\n'
769 'remote: https://chromium-review.googlesource.com/123456 XXX.\n'
770 'remote:\n'
771 'To https://chromium.googlesource.com/yyy/zzz\n'
772 ' * [new branch] hhhh -> refs/for/refs/heads/master\n')),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000773 ]
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000774 if squash:
775 calls += [
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +0000776 ((['git', 'config', 'branch.master.gerritissue', '123456'],), ''),
777 ((['git', 'config', 'branch.master.gerritserver'],), ''),
778 ((['git', 'config', 'remote.origin.url'],),
779 'https://chromium.googlesource.com/my/repo.git'),
780 ((['git', 'config', 'branch.master.gerritserver',
781 'https://chromium-review.googlesource.com'],), ''),
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000782 ((['git', 'config', 'branch.master.gerritsquashhash',
783 'abcdef0123456789'],), ''),
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000784 ]
tandrii@chromium.org1e67bb72016-02-11 12:15:49 +0000785 calls += cls._git_post_upload_calls()
ukai@chromium.orge8077812012-02-03 03:41:46 +0000786 return calls
787
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000788 def _run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000789 self,
790 upload_args,
791 description,
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000792 reviewers,
luqui@chromium.org609f3952015-05-04 22:47:04 +0000793 squash=False,
tandrii@chromium.org10625002016-03-04 20:03:47 +0000794 expected_upstream_ref='origin/refs/heads/master',
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000795 post_amend_description=None,
796 issue=None):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000797 """Generic gerrit upload test framework."""
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000798 self.calls = self._gerrit_base_calls(issue=issue)
luqui@chromium.org609f3952015-05-04 22:47:04 +0000799 self.calls += self._gerrit_upload_calls(
800 description, reviewers, squash,
tandrii@chromium.org10625002016-03-04 20:03:47 +0000801 expected_upstream_ref=expected_upstream_ref,
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000802 post_amend_description=post_amend_description,
803 issue=issue)
tandrii@chromium.org09d7a6a2016-03-04 15:44:48 +0000804 # Uncomment when debugging.
805 # print '\n'.join(map(lambda x: '%2i: %s' % x, enumerate(self.calls)))
ukai@chromium.orge8077812012-02-03 03:41:46 +0000806 git_cl.main(['upload'] + upload_args)
807
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000808 def test_gerrit_upload_without_change_id(self):
tandrii@chromium.org10625002016-03-04 20:03:47 +0000809 self.mock(git_cl, 'DownloadGerritHook', self._mocked_call)
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000810 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000811 [],
jamesr@chromium.org35d1a842012-07-27 00:20:43 +0000812 'desc\n\nBUG=\n',
tandrii@chromium.org10625002016-03-04 20:03:47 +0000813 [],
814 post_amend_description='desc\n\nBUG=\n\nChange-Id: Ixxx')
ukai@chromium.orge8077812012-02-03 03:41:46 +0000815
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000816 def test_gerrit_no_reviewer(self):
817 self._run_gerrit_upload_test(
818 [],
tandrii@chromium.org09d7a6a2016-03-04 15:44:48 +0000819 'desc\n\nBUG=\n\nChange-Id: I123456789\n',
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000820 [])
821
ukai@chromium.orge8077812012-02-03 03:41:46 +0000822 def test_gerrit_reviewers_cmd_line(self):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000823 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000824 ['-r', 'foo@example.com'],
tandrii@chromium.org09d7a6a2016-03-04 15:44:48 +0000825 'desc\n\nBUG=\n\nChange-Id: I123456789',
ukai@chromium.orge8077812012-02-03 03:41:46 +0000826 ['foo@example.com'])
827
828 def test_gerrit_reviewer_multiple(self):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000829 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000830 [],
tandrii@chromium.org09d7a6a2016-03-04 15:44:48 +0000831 'desc\nTBR=reviewer@example.com\nBUG=\nR=another@example.com\n\n'
832 'Change-Id: 123456789\n',
ukai@chromium.orge8077812012-02-03 03:41:46 +0000833 ['reviewer@example.com', 'another@example.com'])
834
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000835 def test_gerrit_upload_squash_first(self):
836 # Mock Gerrit CL description to indicate the first upload.
837 self.mock(git_cl.Changelist, 'GetDescription',
838 lambda *_: None)
839 self.mock(git_cl.gclient_utils, 'RunEditor',
840 lambda *_, **__: self._mocked_call(['RunEditor']))
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000841 self._run_gerrit_upload_test(
842 ['--squash'],
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000843 'desc\nBUG=\n\nChange-Id: 123456789',
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000844 [],
luqui@chromium.org609f3952015-05-04 22:47:04 +0000845 squash=True,
846 expected_upstream_ref='origin/master')
ukai@chromium.orge8077812012-02-03 03:41:46 +0000847
tandrii@chromium.org512d79c2016-03-31 12:55:28 +0000848 def test_gerrit_upload_squash_reupload(self):
849 description = 'desc\nBUG=\n\nChange-Id: 123456789'
850 # Mock Gerrit CL description to indicate re-upload.
851 self.mock(git_cl.Changelist, 'GetDescription',
852 lambda *args: description)
853 self.mock(git_cl.Changelist, 'GetMostRecentPatchset',
854 lambda *args: 1)
855 self.mock(git_cl._GerritChangelistImpl, '_GetChangeDetail',
856 lambda *args: {'change_id': '123456789'})
857 self._run_gerrit_upload_test(
858 ['--squash'],
859 description,
860 [],
861 squash=True,
862 expected_upstream_ref='origin/master',
863 issue=123456)
864
rmistry@google.com2dd99862015-06-22 12:22:18 +0000865 def test_upload_branch_deps(self):
866 def mock_run_git(*args, **_kwargs):
867 if args[0] == ['for-each-ref',
868 '--format=%(refname:short) %(upstream:short)',
869 'refs/heads']:
870 # Create a local branch dependency tree that looks like this:
871 # test1 -> test2 -> test3 -> test4 -> test5
872 # -> test3.1
873 # test6 -> test0
874 branch_deps = [
875 'test2 test1', # test1 -> test2
876 'test3 test2', # test2 -> test3
877 'test3.1 test2', # test2 -> test3.1
878 'test4 test3', # test3 -> test4
879 'test5 test4', # test4 -> test5
880 'test6 test0', # test0 -> test6
881 'test7', # test7
882 ]
883 return '\n'.join(branch_deps)
884 self.mock(git_cl, 'RunGit', mock_run_git)
885
886 class RecordCalls:
887 times_called = 0
888 record_calls = RecordCalls()
889 def mock_CMDupload(*args, **_kwargs):
890 record_calls.times_called += 1
891 return 0
892 self.mock(git_cl, 'CMDupload', mock_CMDupload)
893
894 self.calls = [
895 (('[Press enter to continue or ctrl-C to quit]',), ''),
896 ]
897
898 class MockChangelist():
899 def __init__(self):
900 pass
901 def GetBranch(self):
902 return 'test1'
903 def GetIssue(self):
904 return '123'
905 def GetPatchset(self):
906 return '1001'
tandrii@chromium.org4c72b082016-03-31 22:26:35 +0000907 def IsGerrit(self):
908 return False
rmistry@google.com2dd99862015-06-22 12:22:18 +0000909
910 ret = git_cl.upload_branch_deps(MockChangelist(), [])
911 # CMDupload should have been called 5 times because of 5 dependent branches.
912 self.assertEquals(5, record_calls.times_called)
913 self.assertEquals(0, ret)
914
tandrii@chromium.org65874e12016-03-04 12:03:02 +0000915 def test_gerrit_change_id(self):
916 self.calls = [
917 ((['git', 'write-tree'], ),
918 'hashtree'),
919 ((['git', 'rev-parse', 'HEAD~0'], ),
920 'branch-parent'),
921 ((['git', 'var', 'GIT_AUTHOR_IDENT'], ),
922 'A B <a@b.org> 1456848326 +0100'),
923 ((['git', 'var', 'GIT_COMMITTER_IDENT'], ),
924 'C D <c@d.org> 1456858326 +0100'),
925 ((['git', 'hash-object', '-t', 'commit', '--stdin'], ),
926 'hashchange'),
927 ]
928 change_id = git_cl.GenerateGerritChangeId('line1\nline2\n')
929 self.assertEqual(change_id, 'Ihashchange')
930
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000931 def test_update_reviewers(self):
932 data = [
933 ('foo', [], 'foo'),
agable@chromium.org42c20792013-09-12 17:34:49 +0000934 ('foo\nR=xx', [], 'foo\nR=xx'),
935 ('foo\nTBR=xx', [], 'foo\nTBR=xx'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000936 ('foo', ['a@c'], 'foo\n\nR=a@c'),
agable@chromium.org42c20792013-09-12 17:34:49 +0000937 ('foo\nR=xx', ['a@c'], 'foo\n\nR=a@c, xx'),
938 ('foo\nTBR=xx', ['a@c'], 'foo\n\nR=a@c\nTBR=xx'),
939 ('foo\nTBR=xx\nR=yy', ['a@c'], 'foo\n\nR=a@c, yy\nTBR=xx'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000940 ('foo\nBUG=', ['a@c'], 'foo\nBUG=\nR=a@c'),
agable@chromium.org42c20792013-09-12 17:34:49 +0000941 ('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 +0000942 ('foo', ['a@c', 'b@c'], 'foo\n\nR=a@c, b@c'),
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +0000943 ('foo\nBar\n\nR=\nBUG=', ['c@c'], 'foo\nBar\n\nR=c@c\nBUG='),
944 ('foo\nBar\n\nR=\nBUG=\nR=', ['c@c'], 'foo\nBar\n\nR=c@c\nBUG='),
945 # Same as the line before, but full of whitespaces.
946 (
947 'foo\nBar\n\n R = \n BUG = \n R = ', ['c@c'],
948 'foo\nBar\n\nR=c@c\n BUG =',
949 ),
950 # Whitespaces aren't interpreted as new lines.
951 ('foo BUG=allo R=joe ', ['c@c'], 'foo BUG=allo R=joe\n\nR=c@c'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000952 ]
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +0000953 expected = [i[2] for i in data]
954 actual = []
955 for orig, reviewers, _expected in data:
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000956 obj = git_cl.ChangeDescription(orig)
957 obj.update_reviewers(reviewers)
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +0000958 actual.append(obj.description)
959 self.assertEqual(expected, actual)
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000960
wittman@chromium.org455dc922015-01-26 20:15:50 +0000961 def test_get_target_ref(self):
962 # Check remote or remote branch not present.
963 self.assertEqual(None, git_cl.GetTargetRef('origin', None, 'master', None))
964 self.assertEqual(None, git_cl.GetTargetRef(None,
965 'refs/remotes/origin/master',
966 'master', None))
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000967
wittman@chromium.org455dc922015-01-26 20:15:50 +0000968 # Check default target refs for branches.
969 self.assertEqual('refs/heads/master',
970 git_cl.GetTargetRef('origin', 'refs/remotes/origin/master',
971 None, None))
972 self.assertEqual('refs/heads/master',
973 git_cl.GetTargetRef('origin', 'refs/remotes/origin/lkgr',
974 None, None))
975 self.assertEqual('refs/heads/master',
976 git_cl.GetTargetRef('origin', 'refs/remotes/origin/lkcr',
977 None, None))
978 self.assertEqual('refs/branch-heads/123',
979 git_cl.GetTargetRef('origin',
980 'refs/remotes/branch-heads/123',
981 None, None))
982 self.assertEqual('refs/diff/test',
983 git_cl.GetTargetRef('origin',
984 'refs/remotes/origin/refs/diff/test',
985 None, None))
rmistry@google.comc68112d2015-03-03 12:48:06 +0000986 self.assertEqual('refs/heads/chrome/m42',
987 git_cl.GetTargetRef('origin',
988 'refs/remotes/origin/chrome/m42',
989 None, None))
wittman@chromium.org455dc922015-01-26 20:15:50 +0000990
991 # Check target refs for user-specified target branch.
992 for branch in ('branch-heads/123', 'remotes/branch-heads/123',
993 'refs/remotes/branch-heads/123'):
994 self.assertEqual('refs/branch-heads/123',
995 git_cl.GetTargetRef('origin',
996 'refs/remotes/origin/master',
997 branch, None))
998 for branch in ('origin/master', 'remotes/origin/master',
999 'refs/remotes/origin/master'):
1000 self.assertEqual('refs/heads/master',
1001 git_cl.GetTargetRef('origin',
1002 'refs/remotes/branch-heads/123',
1003 branch, None))
1004 for branch in ('master', 'heads/master', 'refs/heads/master'):
1005 self.assertEqual('refs/heads/master',
1006 git_cl.GetTargetRef('origin',
1007 'refs/remotes/branch-heads/123',
1008 branch, None))
1009
1010 # Check target refs for pending prefix.
1011 self.assertEqual('prefix/heads/master',
1012 git_cl.GetTargetRef('origin', 'refs/remotes/origin/master',
1013 None, 'prefix/'))
1014
wychen@chromium.orga872e752015-04-28 23:42:18 +00001015 def test_patch_when_dirty(self):
1016 # Patch when local tree is dirty
1017 self.mock(git_common, 'is_dirty_git_tree', lambda x: True)
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +00001018 self.calls = [
1019 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
1020 ((['git', 'config', 'branch.master.rietveldissue'],), ''),
1021 ((['git', 'config', 'branch.master.gerritissue'],), ''),
1022 ((['git', 'config', 'rietveld.autoupdate'],), ''),
1023 ((['git', 'config', 'gerrit.host'],), ''),
1024 ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'),
1025 ]
wychen@chromium.orga872e752015-04-28 23:42:18 +00001026 self.assertNotEqual(git_cl.main(['patch', '123456']), 0)
1027
1028 def test_diff_when_dirty(self):
1029 # Do 'git cl diff' when local tree is dirty
1030 self.mock(git_common, 'is_dirty_git_tree', lambda x: True)
1031 self.assertNotEqual(git_cl.main(['diff']), 0)
1032
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +00001033 def _patch_common(self, is_gerrit=False):
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00001034 self.mock(git_cl._RietveldChangelistImpl, 'GetMostRecentPatchset',
1035 lambda x: '60001')
1036 self.mock(git_cl._RietveldChangelistImpl, 'GetPatchSetDiff',
1037 lambda *args: None)
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +00001038 self.mock(git_cl._GerritChangelistImpl, '_GetChangeDetail',
1039 lambda *args: {
1040 'current_revision': '7777777777',
1041 'revisions': {
1042 '1111111111': {
1043 '_number': 1,
1044 'fetch': {'http': {
1045 'url': 'https://chromium.googlesource.com/my/repo',
1046 'ref': 'refs/changes/56/123456/1',
1047 }},
1048 },
1049 '7777777777': {
1050 '_number': 7,
1051 'fetch': {'http': {
1052 'url': 'https://chromium.googlesource.com/my/repo',
1053 'ref': 'refs/changes/56/123456/7',
1054 }},
1055 },
1056 },
1057 })
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00001058 self.mock(git_cl.Changelist, 'GetDescription',
1059 lambda *args: 'Description')
wychen@chromium.orga872e752015-04-28 23:42:18 +00001060 self.mock(git_cl, 'IsGitVersionAtLeast', lambda *args: True)
1061
1062 self.calls = [
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +00001063 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
1064 ((['git', 'config', 'branch.master.rietveldissue'],), ''),
1065 ((['git', 'config', 'branch.master.gerritissue'],), ''),
wychen@chromium.orga872e752015-04-28 23:42:18 +00001066 ((['git', 'config', 'rietveld.autoupdate'],), ''),
wychen@chromium.orga872e752015-04-28 23:42:18 +00001067 ]
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +00001068 if is_gerrit:
1069 self.calls += [
1070 ((['git', 'config', 'gerrit.host'],), 'true'),
1071 ]
1072 else:
1073 self.calls += [
1074 ((['git', 'config', 'gerrit.host'],), ''),
1075 ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'),
1076 ((['git', 'rev-parse', '--show-cdup'],), ''),
1077 ((['sed', '-e', 's|^--- a/|--- |; s|^+++ b/|+++ |'],), ''),
1078 ]
wychen@chromium.orga872e752015-04-28 23:42:18 +00001079
1080 def test_patch_successful(self):
1081 self._patch_common()
1082 self.calls += [
1083 ((['git', 'apply', '--index', '-p0', '--3way'],), ''),
1084 ((['git', 'commit', '-m',
wychen@chromium.org5b3bebb2015-05-28 21:41:43 +00001085 'Description\n\n' +
wychen@chromium.orga872e752015-04-28 23:42:18 +00001086 'patch from issue 123456 at patchset 60001 ' +
1087 '(http://crrev.com/123456#ps60001)'],), ''),
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +00001088 ((['git', 'config', 'branch.master.rietveldissue', '123456'],), ''),
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00001089 ((['git', 'config', 'branch.master.rietveldserver'],), ''),
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +00001090 ((['git', 'config', 'branch.master.rietveldserver',
1091 'https://codereview.example.com'],), ''),
1092 ((['git', 'config', 'branch.master.rietveldpatchset', '60001'],), ''),
wychen@chromium.orga872e752015-04-28 23:42:18 +00001093 ]
1094 self.assertEqual(git_cl.main(['patch', '123456']), 0)
1095
1096 def test_patch_conflict(self):
1097 self._patch_common()
1098 self.calls += [
1099 ((['git', 'apply', '--index', '-p0', '--3way'],), '',
1100 subprocess2.CalledProcessError(1, '', '', '', '')),
1101 ]
1102 self.assertNotEqual(git_cl.main(['patch', '123456']), 0)
wittman@chromium.org455dc922015-01-26 20:15:50 +00001103
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +00001104 def test_gerrit_patch_successful(self):
1105 self._patch_common(is_gerrit=True)
1106 self.calls += [
1107 ((['git', 'fetch', 'https://chromium.googlesource.com/my/repo',
1108 'refs/changes/56/123456/7'],), ''),
1109 ((['git', 'cherry-pick', 'FETCH_HEAD'],), ''),
1110 ((['git', 'config', 'branch.master.gerritissue', '123456'],), ''),
1111 ((['git', 'config', 'branch.master.gerritserver'],), ''),
1112 ((['git', 'config', 'branch.master.merge'],), 'master'),
1113 ((['git', 'config', 'branch.master.remote'],), 'origin'),
1114 ((['git', 'config', 'remote.origin.url'],),
1115 'https://chromium.googlesource.com/my/repo'),
1116 ((['git', 'config', 'branch.master.gerritserver',
1117 'https://chromium-review.googlesource.com'],), ''),
1118 ((['git', 'config', 'branch.master.gerritpatchset', '7'],), ''),
1119 ]
1120 self.assertEqual(git_cl.main(['patch', '123456']), 0)
1121
1122 def test_gerrit_patch_url_successful(self):
1123 self._patch_common(is_gerrit=True)
1124 self.calls += [
1125 ((['git', 'fetch', 'https://chromium.googlesource.com/my/repo',
1126 'refs/changes/56/123456/1'],), ''),
1127 ((['git', 'cherry-pick', 'FETCH_HEAD'],), ''),
1128 ((['git', 'config', 'branch.master.gerritissue', '123456'],), ''),
1129 ((['git', 'config', 'branch.master.gerritserver',
1130 'https://chromium-review.googlesource.com'],), ''),
1131 ((['git', 'config', 'branch.master.gerritpatchset', '1'],), ''),
1132 ]
1133 self.assertEqual(git_cl.main(
1134 ['patch', 'https://chromium-review.googlesource.com/#/c/123456/1']), 0)
1135
1136 def test_gerrit_patch_conflict(self):
1137 self._patch_common(is_gerrit=True)
1138 self.mock(git_cl, 'DieWithError',
1139 lambda msg: self._mocked_call(['DieWithError', msg]))
1140 class SystemExitMock(Exception):
1141 pass
1142 self.calls += [
1143 ((['git', 'fetch', 'https://chromium.googlesource.com/my/repo',
1144 'refs/changes/56/123456/1'],), ''),
1145 ((['git', 'cherry-pick', 'FETCH_HEAD'],),
1146 '', subprocess2.CalledProcessError(1, '', '', '', '')),
1147 ((['DieWithError', 'git cherry-pick FETCH_HEAD" failed.\n'],),
1148 '', SystemExitMock()),
1149 ]
1150 with self.assertRaises(SystemExitMock):
1151 git_cl.main(['patch',
1152 'https://chromium-review.googlesource.com/#/c/123456/1'])
1153
tandrii@chromium.org5df290f2016-04-11 16:12:29 +00001154 def _checkout_calls(self):
1155 return [
1156 ((['git', 'config', '--local', '--get-regexp',
1157 'branch\\..*\\.rietveldissue'], ),
1158 ('branch.retrying.rietveldissue 1111111111\n'
1159 'branch.some-fix.rietveldissue 2222222222\n')),
1160 ((['git', 'config', '--local', '--get-regexp',
1161 'branch\\..*\\.gerritissue'], ),
1162 ('branch.ger-branch.gerritissue 123456\n'
1163 'branch.gbranch654.gerritissue 654321\n')),
1164 ]
1165
1166 def test_checkout_gerrit(self):
1167 """Tests git cl checkout <issue>."""
1168 self.calls = self._checkout_calls()
1169 self.calls += [((['git', 'checkout', 'ger-branch'], ), '')]
1170 self.assertEqual(0, git_cl.main(['checkout', '123456']))
1171
1172 def test_checkout_rietveld(self):
1173 """Tests git cl checkout <issue>."""
1174 self.calls = self._checkout_calls()
1175 self.calls += [((['git', 'checkout', 'some-fix'], ), '')]
1176 self.assertEqual(0, git_cl.main(['checkout', '2222222222']))
1177
1178 def test_checkout_not_found(self):
1179 """Tests git cl checkout <issue>."""
1180 self.calls = self._checkout_calls()
1181 self.assertEqual(1, git_cl.main(['checkout', '99999']))
1182
tandrii@chromium.org26c8fd22016-04-11 21:33:21 +00001183 def test_checkout_no_branch_issues(self):
1184 """Tests git cl checkout <issue>."""
1185 self.calls = [
1186 ((['git', 'config', '--local', '--get-regexp',
1187 'branch\\..*\\.rietveldissue'], ), '',
1188 subprocess2.CalledProcessError(1, '', '', '', '')),
1189 ((['git', 'config', '--local', '--get-regexp',
1190 'branch\\..*\\.gerritissue'], ), '',
1191 subprocess2.CalledProcessError(1, '', '', '', '')),
1192
1193 ]
1194 self.assertEqual(1, git_cl.main(['checkout', '99999']))
1195
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +00001196
maruel@chromium.orgddd59412011-11-30 14:20:38 +00001197if __name__ == '__main__':
maruel@chromium.org78936cb2013-04-11 00:17:52 +00001198 git_cl.logging.basicConfig(
1199 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR)
maruel@chromium.orgddd59412011-11-30 14:20:38 +00001200 unittest.main()