blob: 694fd80278af8459465d35f007b3a1b0a5dbdfe1 [file] [log] [blame]
maruel@chromium.orgddd59412011-11-30 14:20:38 +00001#!/usr/bin/env python
maruel@chromium.orgeb5edbc2012-01-16 17:03:28 +00002# Copyright (c) 2012 The Chromium Authors. All rights reserved.
maruel@chromium.orgddd59412011-11-30 14:20:38 +00003# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Unit tests for git_cl.py."""
7
8import os
maruel@chromium.orga3353652011-11-30 14:26:57 +00009import StringIO
ukai@chromium.org78c4b982012-02-14 02:20:26 +000010import stat
maruel@chromium.orgddd59412011-11-30 14:20:38 +000011import sys
12import unittest
13
14sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
15
16from testing_support.auto_stub import TestCase
17
18import git_cl
iannucci@chromium.org9e849272014-04-04 00:31:55 +000019import git_common
maruel@chromium.orgddd59412011-11-30 14:20:38 +000020import subprocess2
21
22
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000023class PresubmitMock(object):
24 def __init__(self, *args, **kwargs):
25 self.reviewers = []
26 @staticmethod
27 def should_continue():
28 return True
29
30
31class RietveldMock(object):
32 def __init__(self, *args, **kwargs):
33 pass
maruel@chromium.org78936cb2013-04-11 00:17:52 +000034
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000035 @staticmethod
36 def get_description(issue):
37 return 'Issue: %d' % issue
38
maruel@chromium.org78936cb2013-04-11 00:17:52 +000039 @staticmethod
40 def get_issue_properties(_issue, _messages):
41 return {
42 'reviewers': ['joe@chromium.org', 'john@chromium.org'],
43 'messages': [
44 {
45 'approval': True,
46 'sender': 'john@chromium.org',
47 },
48 ],
49 }
50
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000051
52class WatchlistsMock(object):
53 def __init__(self, _):
54 pass
55 @staticmethod
56 def GetWatchersForPaths(_):
57 return ['joe@example.com']
58
59
ukai@chromium.org78c4b982012-02-14 02:20:26 +000060class CodereviewSettingsFileMock(object):
61 def __init__(self):
62 pass
63 # pylint: disable=R0201
64 def read(self):
65 return ("CODE_REVIEW_SERVER: gerrit.chromium.org\n" +
66 "GERRIT_HOST: gerrit.chromium.org\n" +
67 "GERRIT_PORT: 29418\n")
68
69
maruel@chromium.orgddd59412011-11-30 14:20:38 +000070class TestGitCl(TestCase):
71 def setUp(self):
72 super(TestGitCl, self).setUp()
73 self.calls = []
74 self._calls_done = 0
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000075 self.mock(subprocess2, 'call', self._mocked_call)
76 self.mock(subprocess2, 'check_call', self._mocked_call)
77 self.mock(subprocess2, 'check_output', self._mocked_call)
78 self.mock(subprocess2, 'communicate', self._mocked_call)
79 self.mock(subprocess2, 'Popen', self._mocked_call)
iannucci@chromium.org9e849272014-04-04 00:31:55 +000080 self.mock(git_common, 'get_or_create_merge_base',
81 lambda *a: (
82 self._mocked_call(['get_or_create_merge_base']+list(a))))
maruel@chromium.orgddd59412011-11-30 14:20:38 +000083 self.mock(git_cl, 'FindCodereviewSettingsFile', lambda: '')
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000084 self.mock(git_cl, 'ask_for_data', self._mocked_call)
85 self.mock(git_cl.breakpad, 'post', self._mocked_call)
86 self.mock(git_cl.breakpad, 'SendStack', self._mocked_call)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000087 self.mock(git_cl.presubmit_support, 'DoPresubmitChecks', PresubmitMock)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000088 self.mock(git_cl.rietveld, 'Rietveld', RietveldMock)
maruel@chromium.org4bac4b52012-11-27 20:33:52 +000089 self.mock(git_cl.rietveld, 'CachingRietveld', RietveldMock)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000090 self.mock(git_cl.upload, 'RealMain', self.fail)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000091 self.mock(git_cl.watchlists, 'Watchlists', WatchlistsMock)
92 # It's important to reset settings to not have inter-tests interference.
93 git_cl.settings = None
94
95 def tearDown(self):
96 if not self.has_failed():
97 self.assertEquals([], self.calls)
98 super(TestGitCl, self).tearDown()
99
iannucci@chromium.org9e849272014-04-04 00:31:55 +0000100 def _mocked_call(self, *args, **_kwargs):
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000101 self.assertTrue(
102 self.calls,
103 '@%d Expected: <Missing> Actual: %r' % (self._calls_done, args))
104 expected_args, result = self.calls.pop(0)
maruel@chromium.orge52678e2013-04-26 18:34:44 +0000105 # Also logs otherwise it could get caught in a try/finally and be hard to
106 # diagnose.
107 if expected_args != args:
108 msg = '@%d Expected: %r Actual: %r' % (
109 self._calls_done, expected_args, args)
110 git_cl.logging.error(msg)
111 self.fail(msg)
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000112 self._calls_done += 1
113 return result
114
maruel@chromium.orga3353652011-11-30 14:26:57 +0000115 @classmethod
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000116 def _upload_calls(cls, similarity, find_copies, private):
iannucci@chromium.org79540052012-10-19 23:15:26 +0000117 return (cls._git_base_calls(similarity, find_copies) +
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000118 cls._git_upload_calls(private))
maruel@chromium.orga3353652011-11-30 14:26:57 +0000119
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000120 @classmethod
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000121 def _upload_no_rev_calls(cls, similarity, find_copies):
122 return (cls._git_base_calls(similarity, find_copies) +
123 cls._git_upload_no_rev_calls())
124
125 @classmethod
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000126 def _git_base_calls(cls, similarity, find_copies):
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000127 if similarity is None:
128 similarity = '50'
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000129 similarity_call = ((['git', 'config', '--int', '--get',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000130 'branch.master.git-cl-similarity'],), '')
131 else:
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000132 similarity_call = ((['git', 'config', '--int',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000133 'branch.master.git-cl-similarity', similarity],), '')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000134
135 if find_copies is None:
136 find_copies = True
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000137 find_copies_call = ((['git', 'config', '--int', '--get',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000138 'branch.master.git-find-copies'],), '')
139 else:
140 val = str(int(find_copies))
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000141 find_copies_call = ((['git', 'config', '--int',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000142 'branch.master.git-find-copies', val],), '')
143
144 if find_copies:
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000145 stat_call = ((['git', 'diff', '--no-ext-diff', '--stat',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000146 '--find-copies-harder', '-l100000', '-C'+similarity,
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000147 'fake_ancestor_sha', 'HEAD'],), '+dat')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000148 else:
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000149 stat_call = ((['git', 'diff', '--no-ext-diff', '--stat',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000150 '-M'+similarity, 'fake_ancestor_sha', 'HEAD'],), '+dat')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000151
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000152 return [
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000153 ((['git', 'config', 'rietveld.autoupdate'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000154 ((['git', 'config', 'rietveld.server'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000155 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000156 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000157 similarity_call,
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000158 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
iannucci@chromium.org79540052012-10-19 23:15:26 +0000159 find_copies_call,
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000160 ((['git', 'update-index', '--refresh', '-q'],), ''),
161 ((['git', 'diff-index', '--name-status', 'HEAD'],), ''),
162 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
163 ((['git', 'config', 'branch.master.merge'],), 'master'),
164 ((['git', 'config', 'branch.master.remote'],), 'origin'),
iannucci@chromium.org9e849272014-04-04 00:31:55 +0000165 ((['get_or_create_merge_base', 'master', 'master'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000166 'fake_ancestor_sha'),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000167 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000168 ((['git', 'rev-parse', '--show-cdup'],), ''),
169 ((['git', 'rev-parse', 'HEAD'],), '12345'),
170 ((['git', 'diff', '--name-status', '--no-renames', '-r',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000171 'fake_ancestor_sha...', '.'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000172 'M\t.gitignore\n'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000173 ((['git', 'config', 'branch.master.rietveldissue'],), ''),
174 ((['git', 'config', 'branch.master.rietveldpatchset'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000175 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000176 ((['git', 'log', '--pretty=format:%s%n%n%b',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000177 'fake_ancestor_sha...'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000178 'foo'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000179 ((['git', 'config', 'user.email'],), 'me@example.com'),
iannucci@chromium.org79540052012-10-19 23:15:26 +0000180 stat_call,
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000181 ((['git', 'config', 'gerrit.host'],), ''),
182 ((['git', 'log', '--pretty=format:%s\n\n%b',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000183 'fake_ancestor_sha..HEAD'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000184 'desc\n'),
rmistry@google.com90752582014-01-14 21:04:50 +0000185 ((['git', 'config', 'rietveld.bug-prefix'],), ''),
maruel@chromium.orga3353652011-11-30 14:26:57 +0000186 ]
187
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000188 @classmethod
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000189 def _git_upload_no_rev_calls(cls):
190 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000191 ((['git', 'config', 'core.editor'],), ''),
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000192 ]
193
194 @classmethod
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000195 def _git_upload_calls(cls, private):
196 if private:
tyoshino@chromium.org99918ab2013-09-30 06:17:28 +0000197 cc_call = []
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000198 private_call = []
199 else:
tyoshino@chromium.org99918ab2013-09-30 06:17:28 +0000200 cc_call = [((['git', 'config', 'rietveld.cc'],), '')]
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000201 private_call = [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000202 ((['git', 'config', 'rietveld.private'],), '')]
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000203
maruel@chromium.orga3353652011-11-30 14:26:57 +0000204 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000205 ((['git', 'config', 'core.editor'],), ''),
tyoshino@chromium.org99918ab2013-09-30 06:17:28 +0000206 ] + cc_call + private_call + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000207 ((['git', 'config', 'branch.master.base-url'],), ''),
208 ((['git',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000209 'config', '--local', '--get-regexp', '^svn-remote\\.'],),
210 (('', None), 0)),
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000211 ((['git', 'rev-parse', '--show-cdup'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000212 ((['git', 'svn', 'info'],), ''),
213 ((['git',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000214 'config', 'branch.master.rietveldissue', '1'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000215 ((['git', 'config', 'branch.master.rietveldserver',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000216 'https://codereview.example.com'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000217 ((['git',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000218 'config', 'branch.master.rietveldpatchset', '2'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000219 ((['git', 'rev-parse', 'HEAD'],), 'hash'),
220 ((['git', 'symbolic-ref', 'HEAD'],), 'hash'),
221 ((['git',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000222 'config', 'branch.hash.last-upload-hash', 'hash'],), ''),
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000223 ]
224
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000225 @staticmethod
226 def _git_sanity_checks(diff_base, working_branch):
227 fake_ancestor = 'fake_ancestor'
228 fake_cl = 'fake_cl_for_patch'
229 return [
230 # Calls to verify branch point is ancestor
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000231 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000232 'rev-parse', '--verify', diff_base],), fake_ancestor),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000233 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000234 'merge-base', fake_ancestor, 'HEAD'],), fake_ancestor),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000235 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000236 'rev-list', '^' + fake_ancestor, 'HEAD'],), fake_cl),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000237 # Mock a config miss (error code 1)
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000238 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000239 'config', 'gitcl.remotebranch'],), (('', None), 1)),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000240 # Call to GetRemoteBranch()
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000241 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000242 'config', 'branch.%s.merge' % working_branch],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000243 'refs/heads/master'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000244 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000245 'config', 'branch.%s.remote' % working_branch],), 'origin'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000246 ((['git', 'rev-list', '^' + fake_ancestor,
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000247 'refs/remotes/origin/master'],), ''),
248 ]
249
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000250 @classmethod
251 def _dcommit_calls_1(cls):
252 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000253 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000254 'config', '--local', '--get-regexp', '^svn-remote\\.'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000255 ((('svn-remote.svn.url svn://svn.chromium.org/chrome\n'
256 'svn-remote.svn.fetch trunk/src:refs/remotes/origin/master'),
257 None),
258 0)),
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000259 ((['git', 'config', 'rietveld.autoupdate'],),
260 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000261 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000262 'config', 'rietveld.server'],), 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000263 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
264 ((['git', 'config', '--int', '--get',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000265 'branch.working.git-cl-similarity'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000266 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
267 ((['git', 'config', '--int', '--get',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000268 'branch.working.git-find-copies'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000269 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
270 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000271 'config', 'branch.working.merge'],), 'refs/heads/master'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000272 ((['git', 'config', 'branch.working.remote'],), 'origin'),
iannucci@chromium.org5724c962014-04-11 09:32:56 +0000273 ((['git', 'config', 'branch.working.merge'],),
274 'refs/heads/master'),
275 ((['git', 'config', 'branch.working.remote'],), 'origin'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000276 ((['git', 'rev-list', '--merges',
szager@chromium.orge84b7542012-06-15 21:26:58 +0000277 '--grep=^SVN changes up to revision [0-9]*$',
szager@chromium.org9bb85e22012-06-13 20:28:23 +0000278 'refs/remotes/origin/master^!'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000279 ((['git', 'update-index', '--refresh', '-q'],), ''),
280 ((['git', 'diff-index', '--name-status', 'HEAD'],), ''),
281 ((['git', 'rev-list', '^refs/heads/working',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000282 'refs/remotes/origin/master'],),
283 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000284 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000285 'log', '--grep=^git-svn-id:', '-1', '--pretty=format:%H'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000286 '3fc18b62c4966193eb435baabe2d18a3810ec82e'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000287 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000288 'rev-list', '^3fc18b62c4966193eb435baabe2d18a3810ec82e',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000289 'refs/remotes/origin/master'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000290 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000291 'merge-base', 'refs/remotes/origin/master', 'HEAD'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000292 'fake_ancestor_sha'),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000293 ]
294
295 @classmethod
296 def _dcommit_calls_normal(cls):
297 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000298 ((['git', 'rev-parse', '--show-cdup'],), ''),
299 ((['git', 'rev-parse', 'HEAD'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000300 '00ff397798ea57439712ed7e04ab96e13969ef40'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000301 ((['git',
mcgrathr@chromium.org9249f642013-06-03 21:36:18 +0000302 'diff', '--name-status', '--no-renames', '-r', 'fake_ancestor_sha...',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000303 '.'],),
304 'M\tPRESUBMIT.py'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000305 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000306 'config', 'branch.working.rietveldissue'],), '12345'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000307 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000308 'config', 'branch.working.rietveldpatchset'],), '31137'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000309 ((['git', 'config', 'branch.working.rietveldserver'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000310 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000311 ((['git', 'config', 'user.email'],), 'author@example.com'),
312 ((['git', 'config', 'rietveld.tree-status-url'],), ''),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000313 ]
314
315 @classmethod
316 def _dcommit_calls_bypassed(cls):
317 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000318 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000319 'config', 'branch.working.rietveldissue'],), '12345'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000320 ((['git', 'config', 'branch.working.rietveldserver'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000321 'codereview.example.com'),
jochen@chromium.org3ec0d542014-01-14 20:00:03 +0000322 ((['git', 'config', 'rietveld.tree-status-url'],), ''),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000323 (('GitClHooksBypassedCommit',
324 'Issue https://codereview.example.com/12345 bypassed hook when '
jochen@chromium.org3ec0d542014-01-14 20:00:03 +0000325 'committing (tree status was "unset")'), None),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000326 ]
327
328 @classmethod
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000329 def _dcommit_calls_3(cls):
330 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000331 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000332 'diff', '--no-ext-diff', '--stat', '--find-copies-harder',
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000333 '-l100000', '-C50', 'fake_ancestor_sha',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000334 'refs/heads/working'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000335 (' PRESUBMIT.py | 2 +-\n'
336 ' 1 files changed, 1 insertions(+), 1 deletions(-)\n')),
337 (('About to commit; enter to confirm.',), None),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000338 ((['git', 'show-ref', '--quiet', '--verify',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000339 'refs/heads/git-cl-commit'],),
340 (('', None), 0)),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000341 ((['git', 'branch', '-D', 'git-cl-commit'],), ''),
342 ((['git', 'show-ref', '--quiet', '--verify',
szager@chromium.org9bb85e22012-06-13 20:28:23 +0000343 'refs/heads/git-cl-cherry-pick'],), ''),
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000344 ((['git', 'rev-parse', '--show-cdup'],), '\n'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000345 ((['git', 'checkout', '-q', '-b', 'git-cl-commit'],), ''),
346 ((['git', 'reset', '--soft', 'fake_ancestor_sha'],), ''),
347 ((['git', 'commit', '-m',
maruel@chromium.orge52678e2013-04-26 18:34:44 +0000348 'Issue: 12345\n\nR=john@chromium.org\n\n'
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000349 'Review URL: https://codereview.example.com/12345'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000350 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000351 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000352 'svn', 'dcommit', '-C50', '--no-rebase', '--rmdir'],),
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000353 (('', None), 0)),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000354 ((['git', 'checkout', '-q', 'working'],), ''),
355 ((['git', 'branch', '-D', 'git-cl-commit'],), ''),
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000356 ]
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000357
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000358 @staticmethod
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000359 def _cmd_line(description, args, similarity, find_copies, private):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000360 """Returns the upload command line passed to upload.RealMain()."""
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000361 return [
362 'upload', '--assume_yes', '--server',
maruel@chromium.orgeb5edbc2012-01-16 17:03:28 +0000363 'https://codereview.example.com',
maruel@chromium.org71e12a92012-02-14 02:34:15 +0000364 '--message', description
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000365 ] + args + [
366 '--cc', 'joe@example.com',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000367 ] + (['--private'] if private else []) + [
iannucci@chromium.org79540052012-10-19 23:15:26 +0000368 '--git_similarity', similarity or '50'
369 ] + (['--git_no_find_copies'] if find_copies == False else []) + [
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000370 'fake_ancestor_sha', 'HEAD'
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000371 ]
372
373 def _run_reviewer_test(
374 self,
375 upload_args,
376 expected_description,
377 returned_description,
378 final_description,
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000379 reviewers,
380 private=False):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000381 """Generic reviewer test framework."""
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000382 try:
383 similarity = upload_args[upload_args.index('--similarity')+1]
384 except ValueError:
385 similarity = None
iannucci@chromium.org79540052012-10-19 23:15:26 +0000386
387 if '--find-copies' in upload_args:
388 find_copies = True
389 elif '--no-find-copies' in upload_args:
390 find_copies = False
391 else:
392 find_copies = None
393
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000394 private = '--private' in upload_args
395
396 self.calls = self._upload_calls(similarity, find_copies, private)
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000397
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000398 def RunEditor(desc, _, **kwargs):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000399 self.assertEquals(
400 '# Enter a description of the change.\n'
janx@chromium.org104b2db2013-04-18 12:58:40 +0000401 '# This will be displayed on the codereview site.\n'
alancutter@chromium.org63a4d7f2013-05-31 02:22:45 +0000402 '# The first line will also be used as the subject of the review.\n'
alancutter@chromium.orgbd1073e2013-06-01 00:34:38 +0000403 '#--------------------This line is 72 characters long'
404 '--------------------\n' +
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000405 expected_description,
406 desc)
407 return returned_description
408 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor)
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000409
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000410 def check_upload(args):
iannucci@chromium.org79540052012-10-19 23:15:26 +0000411 cmd_line = self._cmd_line(final_description, reviewers, similarity,
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000412 find_copies, private)
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000413 self.assertEquals(cmd_line, args)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000414 return 1, 2
415 self.mock(git_cl.upload, 'RealMain', check_upload)
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000416
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000417 git_cl.main(['upload'] + upload_args)
418
419 def test_no_reviewer(self):
420 self._run_reviewer_test(
421 [],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000422 'desc\n\nBUG=',
423 '# Blah blah comment.\ndesc\n\nBUG=',
424 'desc\n\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000425 [])
426
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000427 def test_keep_similarity(self):
428 self._run_reviewer_test(
429 ['--similarity', '70'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000430 'desc\n\nBUG=',
431 '# Blah blah comment.\ndesc\n\nBUG=',
432 'desc\n\nBUG=',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000433 [])
434
iannucci@chromium.org79540052012-10-19 23:15:26 +0000435 def test_keep_find_copies(self):
436 self._run_reviewer_test(
437 ['--no-find-copies'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000438 'desc\n\nBUG=',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000439 '# Blah blah comment.\ndesc\n\nBUG=\n',
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000440 'desc\n\nBUG=',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000441 [])
442
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000443 def test_private(self):
444 self._run_reviewer_test(
445 ['--private'],
446 'desc\n\nBUG=',
447 '# Blah blah comment.\ndesc\n\nBUG=\n',
448 'desc\n\nBUG=',
449 [])
450
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000451 def test_reviewers_cmd_line(self):
452 # Reviewer is passed as-is
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000453 description = 'desc\n\nR=foo@example.com\nBUG='
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000454 self._run_reviewer_test(
455 ['-r' 'foo@example.com'],
456 description,
457 '\n%s\n' % description,
458 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000459 ['--reviewers=foo@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000460
461 def test_reviewer_tbr_overriden(self):
462 # Reviewer is overriden with TBR
463 # Also verifies the regexp work without a trailing LF
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000464 description = 'Foo Bar\n\nTBR=reviewer@example.com'
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000465 self._run_reviewer_test(
466 ['-r' 'foo@example.com'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000467 'desc\n\nR=foo@example.com\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000468 description.strip('\n'),
469 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000470 ['--reviewers=reviewer@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000471
472 def test_reviewer_multiple(self):
473 # Handles multiple R= or TBR= lines.
474 description = (
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000475 'Foo Bar\nTBR=reviewer@example.com\nBUG=\nR=another@example.com')
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000476 self._run_reviewer_test(
477 [],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000478 'desc\n\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000479 description,
480 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000481 ['--reviewers=another@example.com,reviewer@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000482
maruel@chromium.orga3353652011-11-30 14:26:57 +0000483 def test_reviewer_send_mail(self):
484 # --send-mail can be used without -r if R= is used
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000485 description = 'Foo Bar\nR=reviewer@example.com'
maruel@chromium.orga3353652011-11-30 14:26:57 +0000486 self._run_reviewer_test(
487 ['--send-mail'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000488 'desc\n\nBUG=',
maruel@chromium.orga3353652011-11-30 14:26:57 +0000489 description.strip('\n'),
490 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000491 ['--reviewers=reviewer@example.com', '--send_mail'])
maruel@chromium.orga3353652011-11-30 14:26:57 +0000492
493 def test_reviewer_send_mail_no_rev(self):
494 # Fails without a reviewer.
maruel@chromium.org2e23ce32013-05-07 12:42:28 +0000495 stdout = StringIO.StringIO()
496 stderr = StringIO.StringIO()
maruel@chromium.orga3353652011-11-30 14:26:57 +0000497 try:
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000498 self.calls = self._upload_no_rev_calls(None, None)
499 def RunEditor(desc, _, **kwargs):
maruel@chromium.orga3353652011-11-30 14:26:57 +0000500 return desc
501 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor)
maruel@chromium.org2e23ce32013-05-07 12:42:28 +0000502 self.mock(sys, 'stdout', stdout)
503 self.mock(sys, 'stderr', stderr)
maruel@chromium.orga3353652011-11-30 14:26:57 +0000504 git_cl.main(['upload', '--send-mail'])
505 self.fail()
506 except SystemExit:
maruel@chromium.org2e23ce32013-05-07 12:42:28 +0000507 self.assertEqual(
508 'Using 50% similarity for rename/copy detection. Override with '
509 '--similarity.\n',
510 stdout.getvalue())
511 self.assertEqual(
512 'Must specify reviewers to send email.\n', stderr.getvalue())
maruel@chromium.orga3353652011-11-30 14:26:57 +0000513
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000514 def test_dcommit(self):
515 self.calls = (
516 self._dcommit_calls_1() +
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000517 self._git_sanity_checks('fake_ancestor_sha', 'working') +
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000518 self._dcommit_calls_normal() +
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000519 self._dcommit_calls_3())
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000520 git_cl.main(['dcommit'])
521
522 def test_dcommit_bypass_hooks(self):
523 self.calls = (
524 self._dcommit_calls_1() +
525 self._dcommit_calls_bypassed() +
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000526 self._dcommit_calls_3())
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000527 git_cl.main(['dcommit', '--bypass-hooks'])
528
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000529
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000530 @classmethod
531 def _gerrit_base_calls(cls):
ukai@chromium.orge8077812012-02-03 03:41:46 +0000532 return [
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000533 ((['git', 'config', 'rietveld.autoupdate'],),
534 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000535 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000536 'config', 'rietveld.server'],), 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000537 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
538 ((['git', 'config', '--int', '--get',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000539 'branch.master.git-cl-similarity'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000540 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
541 ((['git', 'config', '--int', '--get',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000542 'branch.master.git-find-copies'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000543 ((['git', 'update-index', '--refresh', '-q'],), ''),
544 ((['git', 'diff-index', '--name-status', 'HEAD'],), ''),
545 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
546 ((['git', 'config', 'branch.master.merge'],), 'master'),
547 ((['git', 'config', 'branch.master.remote'],), 'origin'),
iannucci@chromium.org9e849272014-04-04 00:31:55 +0000548 ((['get_or_create_merge_base', 'master', 'master'],),
549 'fake_ancestor_sha'),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000550 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000551 ((['git', 'rev-parse', '--show-cdup'],), ''),
552 ((['git', 'rev-parse', 'HEAD'],), '12345'),
553 ((['git',
mcgrathr@chromium.org9249f642013-06-03 21:36:18 +0000554 'diff', '--name-status', '--no-renames', '-r',
555 'fake_ancestor_sha...', '.'],),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000556 'M\t.gitignore\n'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000557 ((['git', 'config', 'branch.master.rietveldissue'],), ''),
558 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000559 'config', 'branch.master.rietveldpatchset'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000560 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000561 'log', '--pretty=format:%s%n%n%b', 'fake_ancestor_sha...'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000562 'foo'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000563 ((['git', 'config', 'user.email'],), 'me@example.com'),
564 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000565 'diff', '--no-ext-diff', '--stat', '--find-copies-harder',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000566 '-l100000', '-C50', 'fake_ancestor_sha', 'HEAD'],),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000567 '+dat'),
568 ]
569
570 @staticmethod
571 def _gerrit_upload_calls(description, reviewers):
572 calls = [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000573 ((['git', 'config', 'gerrit.host'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000574 'gerrit.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000575 ((['git', 'log', '--pretty=format:%s\n\n%b',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000576 'fake_ancestor_sha..HEAD'],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000577 description)
578 ]
579 if git_cl.CHANGE_ID not in description:
580 calls += [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000581 ((['git', 'log', '--pretty=format:%s\n\n%b',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000582 'fake_ancestor_sha..HEAD'],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000583 description),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000584 ((['git', 'commit', '--amend', '-m', description],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000585 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000586 ((['git', 'log', '--pretty=format:%s\n\n%b',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000587 'fake_ancestor_sha..HEAD'],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000588 description)
589 ]
590 calls += [
bauerb@chromium.org279c2182014-05-16 09:22:09 +0000591 ((['git', 'rev-list', 'origin/master..'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000592 ((['git', 'config', 'rietveld.cc'],), '')
ukai@chromium.orge8077812012-02-03 03:41:46 +0000593 ]
ukai@chromium.org19bbfa22012-02-03 16:18:11 +0000594 receive_pack = '--receive-pack=git receive-pack '
ukai@chromium.orge8077812012-02-03 03:41:46 +0000595 receive_pack += '--cc=joe@example.com' # from watch list
596 if reviewers:
597 receive_pack += ' '
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000598 receive_pack += ' '.join(
599 '--reviewer=' + email for email in sorted(reviewers))
ukai@chromium.org19bbfa22012-02-03 16:18:11 +0000600 receive_pack += ''
ukai@chromium.orge8077812012-02-03 03:41:46 +0000601 calls += [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000602 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000603 'push', receive_pack, 'origin', 'HEAD:refs/for/master'],),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000604 '')
605 ]
606 return calls
607
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000608 def _run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000609 self,
610 upload_args,
611 description,
612 reviewers):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000613 """Generic gerrit upload test framework."""
ukai@chromium.orge8077812012-02-03 03:41:46 +0000614 self.calls = self._gerrit_base_calls()
615 self.calls += self._gerrit_upload_calls(description, reviewers)
616 git_cl.main(['upload'] + upload_args)
617
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000618 def test_gerrit_upload_without_change_id(self):
619 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000620 [],
jamesr@chromium.org35d1a842012-07-27 00:20:43 +0000621 'desc\n\nBUG=\n',
ukai@chromium.orge8077812012-02-03 03:41:46 +0000622 [])
623
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000624 def test_gerrit_no_reviewer(self):
625 self._run_gerrit_upload_test(
626 [],
627 'desc\n\nBUG=\nChange-Id:123456789\n',
628 [])
629
ukai@chromium.orge8077812012-02-03 03:41:46 +0000630 def test_gerrit_reviewers_cmd_line(self):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000631 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000632 ['-r', 'foo@example.com'],
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000633 'desc\n\nBUG=\nChange-Id:123456789',
ukai@chromium.orge8077812012-02-03 03:41:46 +0000634 ['foo@example.com'])
635
636 def test_gerrit_reviewer_multiple(self):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000637 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000638 [],
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000639 'desc\nTBR=reviewer@example.com\nBUG=\nR=another@example.com\n'
640 'Change-Id:123456789\n',
ukai@chromium.orge8077812012-02-03 03:41:46 +0000641 ['reviewer@example.com', 'another@example.com'])
642
643
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000644 def test_config_gerrit_download_hook(self):
645 self.mock(git_cl, 'FindCodereviewSettingsFile', CodereviewSettingsFileMock)
646 def ParseCodereviewSettingsContent(content):
647 keyvals = {}
648 keyvals['CODE_REVIEW_SERVER'] = 'gerrit.chromium.org'
649 keyvals['GERRIT_HOST'] = 'gerrit.chromium.org'
650 keyvals['GERRIT_PORT'] = '29418'
651 return keyvals
652 self.mock(git_cl.gclient_utils, 'ParseCodereviewSettingsContent',
653 ParseCodereviewSettingsContent)
654 self.mock(git_cl.os, 'access', self._mocked_call)
655 self.mock(git_cl.os, 'chmod', self._mocked_call)
ukai@chromium.org91655502012-05-25 01:46:07 +0000656 src_dir = os.path.join(os.path.sep, 'usr', 'local', 'src')
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000657 def AbsPath(path):
ukai@chromium.org91655502012-05-25 01:46:07 +0000658 if not path.startswith(os.path.sep):
659 return os.path.join(src_dir, path)
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000660 return path
661 self.mock(git_cl.os.path, 'abspath', AbsPath)
ukai@chromium.org91655502012-05-25 01:46:07 +0000662 commit_msg_path = os.path.join(src_dir, '.git', 'hooks', 'commit-msg')
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000663 def Exists(path):
ukai@chromium.org91655502012-05-25 01:46:07 +0000664 if path == commit_msg_path:
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000665 return False
666 # others paths, such as /usr/share/locale/....
667 return True
668 self.mock(git_cl.os.path, 'exists', Exists)
joshua.lock@intel.com426f69b2012-08-02 23:41:49 +0000669 self.mock(git_cl, 'urlretrieve', self._mocked_call)
ukai@chromium.org712d6102013-11-27 00:52:58 +0000670 self.mock(git_cl, 'hasSheBang', self._mocked_call)
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000671 self.calls = [
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000672 ((['git', 'config', 'rietveld.autoupdate'],),
673 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000674 ((['git', 'config', 'rietveld.server',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000675 'gerrit.chromium.org'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000676 ((['git', 'config', '--unset-all', 'rietveld.cc'],), ''),
677 ((['git', 'config', '--unset-all',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000678 'rietveld.private'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000679 ((['git', 'config', '--unset-all',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000680 'rietveld.tree-status-url'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000681 ((['git', 'config', '--unset-all',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000682 'rietveld.viewvc-url'],), ''),
rmistry@google.com90752582014-01-14 21:04:50 +0000683 ((['git', 'config', '--unset-all',
684 'rietveld.bug-prefix'],), ''),
thestig@chromium.org44202a22014-03-11 19:22:18 +0000685 ((['git', 'config', '--unset-all',
686 'rietveld.cpplint-regex'],), ''),
687 ((['git', 'config', '--unset-all',
688 'rietveld.cpplint-ignore-regex'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000689 ((['git', 'config', 'gerrit.host',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000690 'gerrit.chromium.org'],), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000691 # DownloadHooks(False)
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000692 ((['git', 'config', 'gerrit.host'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000693 'gerrit.chromium.org'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000694 ((['git', 'rev-parse', '--show-cdup'],), ''),
ukai@chromium.org91655502012-05-25 01:46:07 +0000695 ((commit_msg_path, os.X_OK,), False),
ukai@chromium.org712d6102013-11-27 00:52:58 +0000696 (('https://gerrit-review.googlesource.com/tools/hooks/commit-msg',
ukai@chromium.org91655502012-05-25 01:46:07 +0000697 commit_msg_path,), ''),
ukai@chromium.org712d6102013-11-27 00:52:58 +0000698 ((commit_msg_path,), True),
ukai@chromium.org91655502012-05-25 01:46:07 +0000699 ((commit_msg_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR,), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000700 # GetCodereviewSettingsInteractively
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000701 ((['git', 'config', 'rietveld.server'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000702 'gerrit.chromium.org'),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000703 (('Rietveld server (host[:port]) [https://gerrit.chromium.org]:',),
704 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000705 ((['git', 'config', 'rietveld.cc'],), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000706 (('CC list:',), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000707 ((['git', 'config', 'rietveld.private'],), ''),
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000708 (('Private flag (rietveld only):',), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000709 ((['git', 'config', 'rietveld.tree-status-url'],), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000710 (('Tree status URL:',), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000711 ((['git', 'config', 'rietveld.viewvc-url'],), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000712 (('ViewVC URL:',), ''),
713 # DownloadHooks(True)
rmistry@google.com90752582014-01-14 21:04:50 +0000714 ((['git', 'config', 'rietveld.bug-prefix'],), ''),
715 (('Bug Prefix:',), ''),
ukai@chromium.org91655502012-05-25 01:46:07 +0000716 ((commit_msg_path, os.X_OK,), True),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000717 ]
718 git_cl.main(['config'])
719
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000720 def test_update_reviewers(self):
721 data = [
722 ('foo', [], 'foo'),
agable@chromium.org42c20792013-09-12 17:34:49 +0000723 ('foo\nR=xx', [], 'foo\nR=xx'),
724 ('foo\nTBR=xx', [], 'foo\nTBR=xx'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000725 ('foo', ['a@c'], 'foo\n\nR=a@c'),
agable@chromium.org42c20792013-09-12 17:34:49 +0000726 ('foo\nR=xx', ['a@c'], 'foo\n\nR=a@c, xx'),
727 ('foo\nTBR=xx', ['a@c'], 'foo\n\nR=a@c\nTBR=xx'),
728 ('foo\nTBR=xx\nR=yy', ['a@c'], 'foo\n\nR=a@c, yy\nTBR=xx'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000729 ('foo\nBUG=', ['a@c'], 'foo\nBUG=\nR=a@c'),
agable@chromium.org42c20792013-09-12 17:34:49 +0000730 ('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 +0000731 ('foo', ['a@c', 'b@c'], 'foo\n\nR=a@c, b@c'),
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +0000732 ('foo\nBar\n\nR=\nBUG=', ['c@c'], 'foo\nBar\n\nR=c@c\nBUG='),
733 ('foo\nBar\n\nR=\nBUG=\nR=', ['c@c'], 'foo\nBar\n\nR=c@c\nBUG='),
734 # Same as the line before, but full of whitespaces.
735 (
736 'foo\nBar\n\n R = \n BUG = \n R = ', ['c@c'],
737 'foo\nBar\n\nR=c@c\n BUG =',
738 ),
739 # Whitespaces aren't interpreted as new lines.
740 ('foo BUG=allo R=joe ', ['c@c'], 'foo BUG=allo R=joe\n\nR=c@c'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000741 ]
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +0000742 expected = [i[2] for i in data]
743 actual = []
744 for orig, reviewers, _expected in data:
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000745 obj = git_cl.ChangeDescription(orig)
746 obj.update_reviewers(reviewers)
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +0000747 actual.append(obj.description)
748 self.assertEqual(expected, actual)
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000749
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000750
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000751if __name__ == '__main__':
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000752 git_cl.logging.basicConfig(
753 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000754 unittest.main()