blob: 36730bcd6234203f99ec7f6e690ff0eac679e602 [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
maruel@chromium.orgddd59412011-11-30 14:20:38 +000021
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000022class PresubmitMock(object):
23 def __init__(self, *args, **kwargs):
24 self.reviewers = []
25 @staticmethod
26 def should_continue():
27 return True
28
29
30class RietveldMock(object):
31 def __init__(self, *args, **kwargs):
32 pass
maruel@chromium.org78936cb2013-04-11 00:17:52 +000033
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000034 @staticmethod
35 def get_description(issue):
36 return 'Issue: %d' % issue
37
maruel@chromium.org78936cb2013-04-11 00:17:52 +000038 @staticmethod
39 def get_issue_properties(_issue, _messages):
40 return {
41 'reviewers': ['joe@chromium.org', 'john@chromium.org'],
42 'messages': [
43 {
44 'approval': True,
45 'sender': 'john@chromium.org',
46 },
47 ],
48 }
49
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000050
51class WatchlistsMock(object):
52 def __init__(self, _):
53 pass
54 @staticmethod
55 def GetWatchersForPaths(_):
56 return ['joe@example.com']
57
58
ukai@chromium.org78c4b982012-02-14 02:20:26 +000059class CodereviewSettingsFileMock(object):
60 def __init__(self):
61 pass
62 # pylint: disable=R0201
63 def read(self):
64 return ("CODE_REVIEW_SERVER: gerrit.chromium.org\n" +
65 "GERRIT_HOST: gerrit.chromium.org\n" +
66 "GERRIT_PORT: 29418\n")
67
68
maruel@chromium.orgddd59412011-11-30 14:20:38 +000069class TestGitCl(TestCase):
70 def setUp(self):
71 super(TestGitCl, self).setUp()
72 self.calls = []
73 self._calls_done = 0
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000074 self.mock(subprocess2, 'call', self._mocked_call)
75 self.mock(subprocess2, 'check_call', self._mocked_call)
76 self.mock(subprocess2, 'check_output', self._mocked_call)
77 self.mock(subprocess2, 'communicate', self._mocked_call)
78 self.mock(subprocess2, 'Popen', self._mocked_call)
iannucci@chromium.org9e849272014-04-04 00:31:55 +000079 self.mock(git_common, 'get_or_create_merge_base',
80 lambda *a: (
81 self._mocked_call(['get_or_create_merge_base']+list(a))))
maruel@chromium.orgddd59412011-11-30 14:20:38 +000082 self.mock(git_cl, 'FindCodereviewSettingsFile', lambda: '')
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000083 self.mock(git_cl, 'ask_for_data', self._mocked_call)
84 self.mock(git_cl.breakpad, 'post', self._mocked_call)
85 self.mock(git_cl.breakpad, 'SendStack', self._mocked_call)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000086 self.mock(git_cl.presubmit_support, 'DoPresubmitChecks', PresubmitMock)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000087 self.mock(git_cl.rietveld, 'Rietveld', RietveldMock)
maruel@chromium.org4bac4b52012-11-27 20:33:52 +000088 self.mock(git_cl.rietveld, 'CachingRietveld', RietveldMock)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000089 self.mock(git_cl.upload, 'RealMain', self.fail)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000090 self.mock(git_cl.watchlists, 'Watchlists', WatchlistsMock)
91 # It's important to reset settings to not have inter-tests interference.
92 git_cl.settings = None
93
94 def tearDown(self):
95 if not self.has_failed():
96 self.assertEquals([], self.calls)
97 super(TestGitCl, self).tearDown()
98
iannucci@chromium.org9e849272014-04-04 00:31:55 +000099 def _mocked_call(self, *args, **_kwargs):
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000100 self.assertTrue(
101 self.calls,
102 '@%d Expected: <Missing> Actual: %r' % (self._calls_done, args))
103 expected_args, result = self.calls.pop(0)
maruel@chromium.orge52678e2013-04-26 18:34:44 +0000104 # Also logs otherwise it could get caught in a try/finally and be hard to
105 # diagnose.
106 if expected_args != args:
107 msg = '@%d Expected: %r Actual: %r' % (
108 self._calls_done, expected_args, args)
109 git_cl.logging.error(msg)
110 self.fail(msg)
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000111 self._calls_done += 1
112 return result
113
maruel@chromium.orga3353652011-11-30 14:26:57 +0000114 @classmethod
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000115 def _upload_calls(cls, similarity, find_copies, private):
iannucci@chromium.org79540052012-10-19 23:15:26 +0000116 return (cls._git_base_calls(similarity, find_copies) +
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000117 cls._git_upload_calls(private))
maruel@chromium.orga3353652011-11-30 14:26:57 +0000118
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000119 @classmethod
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000120 def _upload_no_rev_calls(cls, similarity, find_copies):
121 return (cls._git_base_calls(similarity, find_copies) +
122 cls._git_upload_no_rev_calls())
123
124 @classmethod
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000125 def _git_base_calls(cls, similarity, find_copies):
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000126 if similarity is None:
127 similarity = '50'
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000128 similarity_call = ((['git', 'config', '--int', '--get',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000129 'branch.master.git-cl-similarity'],), '')
130 else:
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000131 similarity_call = ((['git', 'config', '--int',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000132 'branch.master.git-cl-similarity', similarity],), '')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000133
134 if find_copies is None:
135 find_copies = True
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000136 find_copies_call = ((['git', 'config', '--int', '--get',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000137 'branch.master.git-find-copies'],), '')
138 else:
139 val = str(int(find_copies))
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000140 find_copies_call = ((['git', 'config', '--int',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000141 'branch.master.git-find-copies', val],), '')
142
143 if find_copies:
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000144 stat_call = ((['git', 'diff', '--no-ext-diff', '--stat',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000145 '--find-copies-harder', '-l100000', '-C'+similarity,
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000146 'fake_ancestor_sha', 'HEAD'],), '+dat')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000147 else:
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000148 stat_call = ((['git', 'diff', '--no-ext-diff', '--stat',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000149 '-M'+similarity, 'fake_ancestor_sha', 'HEAD'],), '+dat')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000150
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000151 return [
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000152 ((['git', 'config', 'rietveld.autoupdate'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000153 ((['git', 'config', 'rietveld.server'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000154 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000155 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000156 similarity_call,
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000157 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
iannucci@chromium.org79540052012-10-19 23:15:26 +0000158 find_copies_call,
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000159 ((['git', 'update-index', '--refresh', '-q'],), ''),
160 ((['git', 'diff-index', '--name-status', 'HEAD'],), ''),
161 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
162 ((['git', 'config', 'branch.master.merge'],), 'master'),
163 ((['git', 'config', 'branch.master.remote'],), 'origin'),
iannucci@chromium.org9e849272014-04-04 00:31:55 +0000164 ((['get_or_create_merge_base', 'master', 'master'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000165 'fake_ancestor_sha'),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000166 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000167 ((['git', 'rev-parse', '--show-cdup'],), ''),
168 ((['git', 'rev-parse', 'HEAD'],), '12345'),
169 ((['git', 'diff', '--name-status', '--no-renames', '-r',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000170 'fake_ancestor_sha...', '.'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000171 'M\t.gitignore\n'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000172 ((['git', 'config', 'branch.master.rietveldissue'],), ''),
173 ((['git', 'config', 'branch.master.rietveldpatchset'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000174 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000175 ((['git', 'log', '--pretty=format:%s%n%n%b',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000176 'fake_ancestor_sha...'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000177 'foo'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000178 ((['git', 'config', 'user.email'],), 'me@example.com'),
iannucci@chromium.org79540052012-10-19 23:15:26 +0000179 stat_call,
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000180 ((['git', 'config', 'gerrit.host'],), ''),
181 ((['git', 'log', '--pretty=format:%s\n\n%b',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000182 'fake_ancestor_sha..HEAD'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000183 'desc\n'),
rmistry@google.com90752582014-01-14 21:04:50 +0000184 ((['git', 'config', 'rietveld.bug-prefix'],), ''),
maruel@chromium.orga3353652011-11-30 14:26:57 +0000185 ]
186
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000187 @classmethod
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000188 def _git_upload_no_rev_calls(cls):
189 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000190 ((['git', 'config', 'core.editor'],), ''),
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000191 ]
192
193 @classmethod
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000194 def _git_upload_calls(cls, private):
195 if private:
tyoshino@chromium.org99918ab2013-09-30 06:17:28 +0000196 cc_call = []
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000197 private_call = []
198 else:
tyoshino@chromium.org99918ab2013-09-30 06:17:28 +0000199 cc_call = [((['git', 'config', 'rietveld.cc'],), '')]
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000200 private_call = [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000201 ((['git', 'config', 'rietveld.private'],), '')]
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000202
maruel@chromium.orga3353652011-11-30 14:26:57 +0000203 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000204 ((['git', 'config', 'core.editor'],), ''),
tyoshino@chromium.org99918ab2013-09-30 06:17:28 +0000205 ] + cc_call + private_call + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000206 ((['git', 'config', 'branch.master.base-url'],), ''),
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +0000207 ((['git', 'config', 'rietveld.pending-ref-prefix'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000208 ((['git',
bratell@opera.com05fb9112014-07-07 09:30:23 +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'],), ''),
sheyang@chromium.org152cf832014-06-11 21:37:49 +0000213 ((['git', 'config', 'rietveld.project'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000214 ((['git',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000215 'config', 'branch.master.rietveldissue', '1'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000216 ((['git', 'config', 'branch.master.rietveldserver',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000217 'https://codereview.example.com'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000218 ((['git',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000219 'config', 'branch.master.rietveldpatchset', '2'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000220 ((['git', 'rev-parse', 'HEAD'],), 'hash'),
221 ((['git', 'symbolic-ref', 'HEAD'],), 'hash'),
222 ((['git',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000223 'config', 'branch.hash.last-upload-hash', 'hash'],), ''),
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000224 ]
225
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000226 @staticmethod
227 def _git_sanity_checks(diff_base, working_branch):
228 fake_ancestor = 'fake_ancestor'
229 fake_cl = 'fake_cl_for_patch'
230 return [
231 # Calls to verify branch point is ancestor
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000232 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000233 'rev-parse', '--verify', diff_base],), fake_ancestor),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000234 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000235 'merge-base', fake_ancestor, 'HEAD'],), fake_ancestor),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000236 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000237 'rev-list', '^' + fake_ancestor, 'HEAD'],), fake_cl),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000238 # Mock a config miss (error code 1)
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000239 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000240 'config', 'gitcl.remotebranch'],), (('', None), 1)),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000241 # Call to GetRemoteBranch()
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000242 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000243 'config', 'branch.%s.merge' % working_branch],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000244 'refs/heads/master'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000245 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000246 'config', 'branch.%s.remote' % working_branch],), 'origin'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000247 ((['git', 'rev-list', '^' + fake_ancestor,
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000248 'refs/remotes/origin/master'],), ''),
249 ]
250
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000251 @classmethod
252 def _dcommit_calls_1(cls):
253 return [
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +0000254 ((['git', 'config', 'rietveld.autoupdate'],),
255 ''),
256 ((['git', 'config', 'rietveld.pending-ref-prefix'],),
257 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000258 ((['git',
bratell@opera.com05fb9112014-07-07 09:30:23 +0000259 'config', '--local', '--get-regexp', '^svn-remote\\.'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000260 ((('svn-remote.svn.url svn://svn.chromium.org/chrome\n'
261 'svn-remote.svn.fetch trunk/src:refs/remotes/origin/master'),
262 None),
263 0)),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000264 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000265 'config', 'rietveld.server'],), 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000266 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
267 ((['git', 'config', '--int', '--get',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000268 'branch.working.git-cl-similarity'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000269 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
270 ((['git', 'config', '--int', '--get',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000271 'branch.working.git-find-copies'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000272 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
273 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000274 'config', 'branch.working.merge'],), 'refs/heads/master'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000275 ((['git', 'config', 'branch.working.remote'],), 'origin'),
iannucci@chromium.org5724c962014-04-11 09:32:56 +0000276 ((['git', 'config', 'branch.working.merge'],),
277 'refs/heads/master'),
278 ((['git', 'config', 'branch.working.remote'],), 'origin'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000279 ((['git', 'rev-list', '--merges',
szager@chromium.orge84b7542012-06-15 21:26:58 +0000280 '--grep=^SVN changes up to revision [0-9]*$',
szager@chromium.org9bb85e22012-06-13 20:28:23 +0000281 'refs/remotes/origin/master^!'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000282 ((['git', 'update-index', '--refresh', '-q'],), ''),
283 ((['git', 'diff-index', '--name-status', 'HEAD'],), ''),
284 ((['git', 'rev-list', '^refs/heads/working',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000285 'refs/remotes/origin/master'],),
286 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000287 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000288 'log', '--grep=^git-svn-id:', '-1', '--pretty=format:%H'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000289 '3fc18b62c4966193eb435baabe2d18a3810ec82e'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000290 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000291 'rev-list', '^3fc18b62c4966193eb435baabe2d18a3810ec82e',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000292 'refs/remotes/origin/master'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000293 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000294 'merge-base', 'refs/remotes/origin/master', 'HEAD'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000295 'fake_ancestor_sha'),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000296 ]
297
298 @classmethod
299 def _dcommit_calls_normal(cls):
300 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000301 ((['git', 'rev-parse', '--show-cdup'],), ''),
302 ((['git', 'rev-parse', 'HEAD'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000303 '00ff397798ea57439712ed7e04ab96e13969ef40'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000304 ((['git',
mcgrathr@chromium.org9249f642013-06-03 21:36:18 +0000305 'diff', '--name-status', '--no-renames', '-r', 'fake_ancestor_sha...',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000306 '.'],),
307 'M\tPRESUBMIT.py'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000308 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000309 'config', 'branch.working.rietveldissue'],), '12345'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000310 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000311 'config', 'branch.working.rietveldpatchset'],), '31137'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000312 ((['git', 'config', 'branch.working.rietveldserver'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000313 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000314 ((['git', 'config', 'user.email'],), 'author@example.com'),
315 ((['git', 'config', 'rietveld.tree-status-url'],), ''),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000316 ]
317
318 @classmethod
319 def _dcommit_calls_bypassed(cls):
320 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000321 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000322 'config', 'branch.working.rietveldissue'],), '12345'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000323 ((['git', 'config', 'branch.working.rietveldserver'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000324 'codereview.example.com'),
jochen@chromium.org3ec0d542014-01-14 20:00:03 +0000325 ((['git', 'config', 'rietveld.tree-status-url'],), ''),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000326 (('GitClHooksBypassedCommit',
327 'Issue https://codereview.example.com/12345 bypassed hook when '
jochen@chromium.org3ec0d542014-01-14 20:00:03 +0000328 'committing (tree status was "unset")'), None),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000329 ]
330
331 @classmethod
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000332 def _dcommit_calls_3(cls):
333 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000334 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000335 'diff', '--no-ext-diff', '--stat', '--find-copies-harder',
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000336 '-l100000', '-C50', 'fake_ancestor_sha',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000337 'refs/heads/working'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000338 (' PRESUBMIT.py | 2 +-\n'
339 ' 1 files changed, 1 insertions(+), 1 deletions(-)\n')),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000340 ((['git', 'show-ref', '--quiet', '--verify',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000341 'refs/heads/git-cl-commit'],),
342 (('', None), 0)),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000343 ((['git', 'branch', '-D', 'git-cl-commit'],), ''),
344 ((['git', 'show-ref', '--quiet', '--verify',
szager@chromium.org9bb85e22012-06-13 20:28:23 +0000345 'refs/heads/git-cl-cherry-pick'],), ''),
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000346 ((['git', 'rev-parse', '--show-cdup'],), '\n'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000347 ((['git', 'checkout', '-q', '-b', 'git-cl-commit'],), ''),
348 ((['git', 'reset', '--soft', 'fake_ancestor_sha'],), ''),
349 ((['git', 'commit', '-m',
maruel@chromium.orge52678e2013-04-26 18:34:44 +0000350 'Issue: 12345\n\nR=john@chromium.org\n\n'
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000351 'Review URL: https://codereview.example.com/12345'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000352 ''),
kjellander@chromium.org6abc6522014-12-02 07:34:49 +0000353 ((['git', 'config', 'rietveld.force-https-commit-url'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000354 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000355 'svn', 'dcommit', '-C50', '--no-rebase', '--rmdir'],),
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000356 (('', None), 0)),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000357 ((['git', 'checkout', '-q', 'working'],), ''),
358 ((['git', 'branch', '-D', 'git-cl-commit'],), ''),
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000359 ]
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000360
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000361 @staticmethod
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000362 def _cmd_line(description, args, similarity, find_copies, private):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000363 """Returns the upload command line passed to upload.RealMain()."""
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000364 return [
365 'upload', '--assume_yes', '--server',
maruel@chromium.orgeb5edbc2012-01-16 17:03:28 +0000366 'https://codereview.example.com',
maruel@chromium.org71e12a92012-02-14 02:34:15 +0000367 '--message', description
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000368 ] + args + [
369 '--cc', 'joe@example.com',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000370 ] + (['--private'] if private else []) + [
iannucci@chromium.org79540052012-10-19 23:15:26 +0000371 '--git_similarity', similarity or '50'
372 ] + (['--git_no_find_copies'] if find_copies == False else []) + [
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000373 'fake_ancestor_sha', 'HEAD'
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000374 ]
375
376 def _run_reviewer_test(
377 self,
378 upload_args,
379 expected_description,
380 returned_description,
381 final_description,
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000382 reviewers,
383 private=False):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000384 """Generic reviewer test framework."""
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000385 try:
386 similarity = upload_args[upload_args.index('--similarity')+1]
387 except ValueError:
388 similarity = None
iannucci@chromium.org79540052012-10-19 23:15:26 +0000389
390 if '--find-copies' in upload_args:
391 find_copies = True
392 elif '--no-find-copies' in upload_args:
393 find_copies = False
394 else:
395 find_copies = None
396
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000397 private = '--private' in upload_args
398
399 self.calls = self._upload_calls(similarity, find_copies, private)
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000400
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000401 def RunEditor(desc, _, **kwargs):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000402 self.assertEquals(
403 '# Enter a description of the change.\n'
janx@chromium.org104b2db2013-04-18 12:58:40 +0000404 '# This will be displayed on the codereview site.\n'
alancutter@chromium.org63a4d7f2013-05-31 02:22:45 +0000405 '# The first line will also be used as the subject of the review.\n'
alancutter@chromium.orgbd1073e2013-06-01 00:34:38 +0000406 '#--------------------This line is 72 characters long'
407 '--------------------\n' +
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000408 expected_description,
409 desc)
410 return returned_description
411 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor)
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000412
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000413 def check_upload(args):
iannucci@chromium.org79540052012-10-19 23:15:26 +0000414 cmd_line = self._cmd_line(final_description, reviewers, similarity,
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000415 find_copies, private)
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000416 self.assertEquals(cmd_line, args)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000417 return 1, 2
418 self.mock(git_cl.upload, 'RealMain', check_upload)
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000419
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000420 git_cl.main(['upload'] + upload_args)
421
422 def test_no_reviewer(self):
423 self._run_reviewer_test(
424 [],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000425 'desc\n\nBUG=',
426 '# Blah blah comment.\ndesc\n\nBUG=',
427 'desc\n\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000428 [])
429
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000430 def test_keep_similarity(self):
431 self._run_reviewer_test(
432 ['--similarity', '70'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000433 'desc\n\nBUG=',
434 '# Blah blah comment.\ndesc\n\nBUG=',
435 'desc\n\nBUG=',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000436 [])
437
iannucci@chromium.org79540052012-10-19 23:15:26 +0000438 def test_keep_find_copies(self):
439 self._run_reviewer_test(
440 ['--no-find-copies'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000441 'desc\n\nBUG=',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000442 '# Blah blah comment.\ndesc\n\nBUG=\n',
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000443 'desc\n\nBUG=',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000444 [])
445
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000446 def test_private(self):
447 self._run_reviewer_test(
448 ['--private'],
449 'desc\n\nBUG=',
450 '# Blah blah comment.\ndesc\n\nBUG=\n',
451 'desc\n\nBUG=',
452 [])
453
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000454 def test_reviewers_cmd_line(self):
455 # Reviewer is passed as-is
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000456 description = 'desc\n\nR=foo@example.com\nBUG='
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000457 self._run_reviewer_test(
458 ['-r' 'foo@example.com'],
459 description,
460 '\n%s\n' % description,
461 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000462 ['--reviewers=foo@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000463
464 def test_reviewer_tbr_overriden(self):
465 # Reviewer is overriden with TBR
466 # Also verifies the regexp work without a trailing LF
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000467 description = 'Foo Bar\n\nTBR=reviewer@example.com'
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000468 self._run_reviewer_test(
469 ['-r' 'foo@example.com'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000470 'desc\n\nR=foo@example.com\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000471 description.strip('\n'),
472 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000473 ['--reviewers=reviewer@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000474
475 def test_reviewer_multiple(self):
476 # Handles multiple R= or TBR= lines.
477 description = (
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000478 'Foo Bar\nTBR=reviewer@example.com\nBUG=\nR=another@example.com')
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000479 self._run_reviewer_test(
480 [],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000481 'desc\n\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000482 description,
483 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000484 ['--reviewers=another@example.com,reviewer@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000485
maruel@chromium.orga3353652011-11-30 14:26:57 +0000486 def test_reviewer_send_mail(self):
487 # --send-mail can be used without -r if R= is used
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000488 description = 'Foo Bar\nR=reviewer@example.com'
maruel@chromium.orga3353652011-11-30 14:26:57 +0000489 self._run_reviewer_test(
490 ['--send-mail'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000491 'desc\n\nBUG=',
maruel@chromium.orga3353652011-11-30 14:26:57 +0000492 description.strip('\n'),
493 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000494 ['--reviewers=reviewer@example.com', '--send_mail'])
maruel@chromium.orga3353652011-11-30 14:26:57 +0000495
496 def test_reviewer_send_mail_no_rev(self):
497 # Fails without a reviewer.
maruel@chromium.org2e23ce32013-05-07 12:42:28 +0000498 stdout = StringIO.StringIO()
499 stderr = StringIO.StringIO()
maruel@chromium.orga3353652011-11-30 14:26:57 +0000500 try:
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000501 self.calls = self._upload_no_rev_calls(None, None)
502 def RunEditor(desc, _, **kwargs):
maruel@chromium.orga3353652011-11-30 14:26:57 +0000503 return desc
504 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor)
maruel@chromium.org2e23ce32013-05-07 12:42:28 +0000505 self.mock(sys, 'stdout', stdout)
506 self.mock(sys, 'stderr', stderr)
maruel@chromium.orga3353652011-11-30 14:26:57 +0000507 git_cl.main(['upload', '--send-mail'])
508 self.fail()
509 except SystemExit:
maruel@chromium.org2e23ce32013-05-07 12:42:28 +0000510 self.assertEqual(
511 'Using 50% similarity for rename/copy detection. Override with '
512 '--similarity.\n',
513 stdout.getvalue())
514 self.assertEqual(
515 'Must specify reviewers to send email.\n', stderr.getvalue())
maruel@chromium.orga3353652011-11-30 14:26:57 +0000516
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000517 def test_dcommit(self):
518 self.calls = (
519 self._dcommit_calls_1() +
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000520 self._git_sanity_checks('fake_ancestor_sha', 'working') +
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000521 self._dcommit_calls_normal() +
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000522 self._dcommit_calls_3())
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000523 git_cl.main(['dcommit'])
524
525 def test_dcommit_bypass_hooks(self):
526 self.calls = (
527 self._dcommit_calls_1() +
528 self._dcommit_calls_bypassed() +
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000529 self._dcommit_calls_3())
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000530 git_cl.main(['dcommit', '--bypass-hooks'])
531
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000532
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000533 @classmethod
534 def _gerrit_base_calls(cls):
ukai@chromium.orge8077812012-02-03 03:41:46 +0000535 return [
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000536 ((['git', 'config', 'rietveld.autoupdate'],),
537 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000538 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000539 'config', 'rietveld.server'],), 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000540 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
541 ((['git', 'config', '--int', '--get',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000542 'branch.master.git-cl-similarity'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000543 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
544 ((['git', 'config', '--int', '--get',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000545 'branch.master.git-find-copies'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000546 ((['git', 'update-index', '--refresh', '-q'],), ''),
547 ((['git', 'diff-index', '--name-status', 'HEAD'],), ''),
548 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
549 ((['git', 'config', 'branch.master.merge'],), 'master'),
550 ((['git', 'config', 'branch.master.remote'],), 'origin'),
iannucci@chromium.org9e849272014-04-04 00:31:55 +0000551 ((['get_or_create_merge_base', 'master', 'master'],),
552 'fake_ancestor_sha'),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000553 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000554 ((['git', 'rev-parse', '--show-cdup'],), ''),
555 ((['git', 'rev-parse', 'HEAD'],), '12345'),
556 ((['git',
mcgrathr@chromium.org9249f642013-06-03 21:36:18 +0000557 'diff', '--name-status', '--no-renames', '-r',
558 'fake_ancestor_sha...', '.'],),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000559 'M\t.gitignore\n'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000560 ((['git', 'config', 'branch.master.rietveldissue'],), ''),
561 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000562 'config', 'branch.master.rietveldpatchset'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000563 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000564 'log', '--pretty=format:%s%n%n%b', 'fake_ancestor_sha...'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000565 'foo'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000566 ((['git', 'config', 'user.email'],), 'me@example.com'),
567 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000568 'diff', '--no-ext-diff', '--stat', '--find-copies-harder',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000569 '-l100000', '-C50', 'fake_ancestor_sha', 'HEAD'],),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000570 '+dat'),
571 ]
572
573 @staticmethod
574 def _gerrit_upload_calls(description, reviewers):
575 calls = [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000576 ((['git', 'config', 'gerrit.host'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000577 'gerrit.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000578 ((['git', 'log', '--pretty=format:%s\n\n%b',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000579 'fake_ancestor_sha..HEAD'],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000580 description)
581 ]
582 if git_cl.CHANGE_ID not in description:
583 calls += [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000584 ((['git', 'log', '--pretty=format:%s\n\n%b',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000585 'fake_ancestor_sha..HEAD'],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000586 description),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000587 ((['git', 'commit', '--amend', '-m', description],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000588 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000589 ((['git', 'log', '--pretty=format:%s\n\n%b',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000590 'fake_ancestor_sha..HEAD'],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000591 description)
592 ]
593 calls += [
bauerb@chromium.org279c2182014-05-16 09:22:09 +0000594 ((['git', 'rev-list', 'origin/master..'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000595 ((['git', 'config', 'rietveld.cc'],), '')
ukai@chromium.orge8077812012-02-03 03:41:46 +0000596 ]
ukai@chromium.org19bbfa22012-02-03 16:18:11 +0000597 receive_pack = '--receive-pack=git receive-pack '
ukai@chromium.orge8077812012-02-03 03:41:46 +0000598 receive_pack += '--cc=joe@example.com' # from watch list
599 if reviewers:
600 receive_pack += ' '
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000601 receive_pack += ' '.join(
602 '--reviewer=' + email for email in sorted(reviewers))
ukai@chromium.org19bbfa22012-02-03 16:18:11 +0000603 receive_pack += ''
ukai@chromium.orge8077812012-02-03 03:41:46 +0000604 calls += [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000605 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000606 'push', receive_pack, 'origin', 'HEAD:refs/for/master'],),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000607 '')
608 ]
609 return calls
610
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000611 def _run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000612 self,
613 upload_args,
614 description,
615 reviewers):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000616 """Generic gerrit upload test framework."""
ukai@chromium.orge8077812012-02-03 03:41:46 +0000617 self.calls = self._gerrit_base_calls()
618 self.calls += self._gerrit_upload_calls(description, reviewers)
619 git_cl.main(['upload'] + upload_args)
620
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000621 def test_gerrit_upload_without_change_id(self):
622 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000623 [],
jamesr@chromium.org35d1a842012-07-27 00:20:43 +0000624 'desc\n\nBUG=\n',
ukai@chromium.orge8077812012-02-03 03:41:46 +0000625 [])
626
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000627 def test_gerrit_no_reviewer(self):
628 self._run_gerrit_upload_test(
629 [],
630 'desc\n\nBUG=\nChange-Id:123456789\n',
631 [])
632
ukai@chromium.orge8077812012-02-03 03:41:46 +0000633 def test_gerrit_reviewers_cmd_line(self):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000634 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000635 ['-r', 'foo@example.com'],
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000636 'desc\n\nBUG=\nChange-Id:123456789',
ukai@chromium.orge8077812012-02-03 03:41:46 +0000637 ['foo@example.com'])
638
639 def test_gerrit_reviewer_multiple(self):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000640 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000641 [],
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000642 'desc\nTBR=reviewer@example.com\nBUG=\nR=another@example.com\n'
643 'Change-Id:123456789\n',
ukai@chromium.orge8077812012-02-03 03:41:46 +0000644 ['reviewer@example.com', 'another@example.com'])
645
646
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000647 def test_config_gerrit_download_hook(self):
648 self.mock(git_cl, 'FindCodereviewSettingsFile', CodereviewSettingsFileMock)
649 def ParseCodereviewSettingsContent(content):
650 keyvals = {}
651 keyvals['CODE_REVIEW_SERVER'] = 'gerrit.chromium.org'
652 keyvals['GERRIT_HOST'] = 'gerrit.chromium.org'
653 keyvals['GERRIT_PORT'] = '29418'
654 return keyvals
655 self.mock(git_cl.gclient_utils, 'ParseCodereviewSettingsContent',
656 ParseCodereviewSettingsContent)
657 self.mock(git_cl.os, 'access', self._mocked_call)
658 self.mock(git_cl.os, 'chmod', self._mocked_call)
ukai@chromium.org91655502012-05-25 01:46:07 +0000659 src_dir = os.path.join(os.path.sep, 'usr', 'local', 'src')
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000660 def AbsPath(path):
ukai@chromium.org91655502012-05-25 01:46:07 +0000661 if not path.startswith(os.path.sep):
662 return os.path.join(src_dir, path)
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000663 return path
664 self.mock(git_cl.os.path, 'abspath', AbsPath)
ukai@chromium.org91655502012-05-25 01:46:07 +0000665 commit_msg_path = os.path.join(src_dir, '.git', 'hooks', 'commit-msg')
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000666 def Exists(path):
ukai@chromium.org91655502012-05-25 01:46:07 +0000667 if path == commit_msg_path:
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000668 return False
669 # others paths, such as /usr/share/locale/....
670 return True
671 self.mock(git_cl.os.path, 'exists', Exists)
joshua.lock@intel.com426f69b2012-08-02 23:41:49 +0000672 self.mock(git_cl, 'urlretrieve', self._mocked_call)
ukai@chromium.org712d6102013-11-27 00:52:58 +0000673 self.mock(git_cl, 'hasSheBang', self._mocked_call)
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000674 self.calls = [
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000675 ((['git', 'config', 'rietveld.autoupdate'],),
676 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000677 ((['git', 'config', 'rietveld.server',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000678 'gerrit.chromium.org'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000679 ((['git', 'config', '--unset-all', 'rietveld.cc'],), ''),
680 ((['git', 'config', '--unset-all',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000681 'rietveld.private'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000682 ((['git', 'config', '--unset-all',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000683 'rietveld.tree-status-url'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000684 ((['git', 'config', '--unset-all',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000685 'rietveld.viewvc-url'],), ''),
rmistry@google.com90752582014-01-14 21:04:50 +0000686 ((['git', 'config', '--unset-all',
687 'rietveld.bug-prefix'],), ''),
thestig@chromium.org44202a22014-03-11 19:22:18 +0000688 ((['git', 'config', '--unset-all',
689 'rietveld.cpplint-regex'],), ''),
690 ((['git', 'config', '--unset-all',
kjellander@chromium.org6abc6522014-12-02 07:34:49 +0000691 'rietveld.force-https-commit-url'],), ''),
692 ((['git', 'config', '--unset-all',
thestig@chromium.org44202a22014-03-11 19:22:18 +0000693 'rietveld.cpplint-ignore-regex'],), ''),
sheyang@chromium.org152cf832014-06-11 21:37:49 +0000694 ((['git', 'config', '--unset-all',
695 'rietveld.project'],), ''),
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +0000696 ((['git', 'config', '--unset-all',
697 'rietveld.pending-ref-prefix'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000698 ((['git', 'config', 'gerrit.host',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000699 'gerrit.chromium.org'],), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000700 # DownloadHooks(False)
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000701 ((['git', 'config', 'gerrit.host'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000702 'gerrit.chromium.org'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000703 ((['git', 'rev-parse', '--show-cdup'],), ''),
ukai@chromium.org91655502012-05-25 01:46:07 +0000704 ((commit_msg_path, os.X_OK,), False),
ukai@chromium.org712d6102013-11-27 00:52:58 +0000705 (('https://gerrit-review.googlesource.com/tools/hooks/commit-msg',
ukai@chromium.org91655502012-05-25 01:46:07 +0000706 commit_msg_path,), ''),
ukai@chromium.org712d6102013-11-27 00:52:58 +0000707 ((commit_msg_path,), True),
ukai@chromium.org91655502012-05-25 01:46:07 +0000708 ((commit_msg_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR,), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000709 # GetCodereviewSettingsInteractively
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000710 ((['git', 'config', 'rietveld.server'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000711 'gerrit.chromium.org'),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000712 (('Rietveld server (host[:port]) [https://gerrit.chromium.org]:',),
713 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000714 ((['git', 'config', 'rietveld.cc'],), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000715 (('CC list:',), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000716 ((['git', 'config', 'rietveld.private'],), ''),
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000717 (('Private flag (rietveld only):',), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000718 ((['git', 'config', 'rietveld.tree-status-url'],), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000719 (('Tree status URL:',), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000720 ((['git', 'config', 'rietveld.viewvc-url'],), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000721 (('ViewVC URL:',), ''),
722 # DownloadHooks(True)
rmistry@google.com90752582014-01-14 21:04:50 +0000723 ((['git', 'config', 'rietveld.bug-prefix'],), ''),
724 (('Bug Prefix:',), ''),
ukai@chromium.org91655502012-05-25 01:46:07 +0000725 ((commit_msg_path, os.X_OK,), True),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000726 ]
727 git_cl.main(['config'])
728
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000729 def test_update_reviewers(self):
730 data = [
731 ('foo', [], 'foo'),
agable@chromium.org42c20792013-09-12 17:34:49 +0000732 ('foo\nR=xx', [], 'foo\nR=xx'),
733 ('foo\nTBR=xx', [], 'foo\nTBR=xx'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000734 ('foo', ['a@c'], 'foo\n\nR=a@c'),
agable@chromium.org42c20792013-09-12 17:34:49 +0000735 ('foo\nR=xx', ['a@c'], 'foo\n\nR=a@c, xx'),
736 ('foo\nTBR=xx', ['a@c'], 'foo\n\nR=a@c\nTBR=xx'),
737 ('foo\nTBR=xx\nR=yy', ['a@c'], 'foo\n\nR=a@c, yy\nTBR=xx'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000738 ('foo\nBUG=', ['a@c'], 'foo\nBUG=\nR=a@c'),
agable@chromium.org42c20792013-09-12 17:34:49 +0000739 ('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 +0000740 ('foo', ['a@c', 'b@c'], 'foo\n\nR=a@c, b@c'),
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +0000741 ('foo\nBar\n\nR=\nBUG=', ['c@c'], 'foo\nBar\n\nR=c@c\nBUG='),
742 ('foo\nBar\n\nR=\nBUG=\nR=', ['c@c'], 'foo\nBar\n\nR=c@c\nBUG='),
743 # Same as the line before, but full of whitespaces.
744 (
745 'foo\nBar\n\n R = \n BUG = \n R = ', ['c@c'],
746 'foo\nBar\n\nR=c@c\n BUG =',
747 ),
748 # Whitespaces aren't interpreted as new lines.
749 ('foo BUG=allo R=joe ', ['c@c'], 'foo BUG=allo R=joe\n\nR=c@c'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000750 ]
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +0000751 expected = [i[2] for i in data]
752 actual = []
753 for orig, reviewers, _expected in data:
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000754 obj = git_cl.ChangeDescription(orig)
755 obj.update_reviewers(reviewers)
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +0000756 actual.append(obj.description)
757 self.assertEqual(expected, actual)
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000758
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000759if __name__ == '__main__':
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000760 git_cl.logging.basicConfig(
761 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000762 unittest.main()