blob: dce632ea9f526310ec495e3af690f7181320fb82 [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
19import subprocess2
20
21
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)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000079 self.mock(git_cl, 'FindCodereviewSettingsFile', lambda: '')
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000080 self.mock(git_cl, 'ask_for_data', self._mocked_call)
81 self.mock(git_cl.breakpad, 'post', self._mocked_call)
82 self.mock(git_cl.breakpad, 'SendStack', self._mocked_call)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000083 self.mock(git_cl.presubmit_support, 'DoPresubmitChecks', PresubmitMock)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000084 self.mock(git_cl.rietveld, 'Rietveld', RietveldMock)
maruel@chromium.org4bac4b52012-11-27 20:33:52 +000085 self.mock(git_cl.rietveld, 'CachingRietveld', RietveldMock)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000086 self.mock(git_cl.upload, 'RealMain', self.fail)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000087 self.mock(git_cl.watchlists, 'Watchlists', WatchlistsMock)
88 # It's important to reset settings to not have inter-tests interference.
89 git_cl.settings = None
90
91 def tearDown(self):
92 if not self.has_failed():
93 self.assertEquals([], self.calls)
94 super(TestGitCl, self).tearDown()
95
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000096 def _mocked_call(self, *args, **kwargs):
97 self.assertTrue(
98 self.calls,
99 '@%d Expected: <Missing> Actual: %r' % (self._calls_done, args))
100 expected_args, result = self.calls.pop(0)
maruel@chromium.orge52678e2013-04-26 18:34:44 +0000101 # Also logs otherwise it could get caught in a try/finally and be hard to
102 # diagnose.
103 if expected_args != args:
104 msg = '@%d Expected: %r Actual: %r' % (
105 self._calls_done, expected_args, args)
106 git_cl.logging.error(msg)
107 self.fail(msg)
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000108 self._calls_done += 1
109 return result
110
maruel@chromium.orga3353652011-11-30 14:26:57 +0000111 @classmethod
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000112 def _upload_calls(cls, similarity, find_copies, private):
iannucci@chromium.org79540052012-10-19 23:15:26 +0000113 return (cls._git_base_calls(similarity, find_copies) +
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000114 cls._git_upload_calls(private))
maruel@chromium.orga3353652011-11-30 14:26:57 +0000115
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000116 @classmethod
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000117 def _upload_no_rev_calls(cls, similarity, find_copies):
118 return (cls._git_base_calls(similarity, find_copies) +
119 cls._git_upload_no_rev_calls())
120
121 @classmethod
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000122 def _git_base_calls(cls, similarity, find_copies):
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000123 if similarity is None:
124 similarity = '50'
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000125 similarity_call = ((['git', 'config', '--int', '--get',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000126 'branch.master.git-cl-similarity'],), '')
127 else:
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000128 similarity_call = ((['git', 'config', '--int',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000129 'branch.master.git-cl-similarity', similarity],), '')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000130
131 if find_copies is None:
132 find_copies = True
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000133 find_copies_call = ((['git', 'config', '--int', '--get',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000134 'branch.master.git-find-copies'],), '')
135 else:
136 val = str(int(find_copies))
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000137 find_copies_call = ((['git', 'config', '--int',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000138 'branch.master.git-find-copies', val],), '')
139
140 if find_copies:
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000141 stat_call = ((['git', 'diff', '--no-ext-diff', '--stat',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000142 '--find-copies-harder', '-l100000', '-C'+similarity,
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000143 'fake_ancestor_sha', 'HEAD'],), '+dat')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000144 else:
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000145 stat_call = ((['git', 'diff', '--no-ext-diff', '--stat',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000146 '-M'+similarity, 'fake_ancestor_sha', 'HEAD'],), '+dat')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000147
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000148 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000149 ((['git', 'config', 'rietveld.server'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000150 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000151 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000152 similarity_call,
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000153 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
iannucci@chromium.org79540052012-10-19 23:15:26 +0000154 find_copies_call,
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000155 ((['git', 'update-index', '--refresh', '-q'],), ''),
156 ((['git', 'diff-index', '--name-status', 'HEAD'],), ''),
157 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
158 ((['git', 'config', 'branch.master.merge'],), 'master'),
159 ((['git', 'config', 'branch.master.remote'],), 'origin'),
160 ((['git', 'merge-base', 'master', 'HEAD'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000161 'fake_ancestor_sha'),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000162 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000163 ((['git', 'rev-parse', '--show-cdup'],), ''),
164 ((['git', 'rev-parse', 'HEAD'],), '12345'),
165 ((['git', 'diff', '--name-status', '--no-renames', '-r',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000166 'fake_ancestor_sha...', '.'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000167 'M\t.gitignore\n'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000168 ((['git', 'config', 'branch.master.rietveldissue'],), ''),
169 ((['git', 'config', 'branch.master.rietveldpatchset'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000170 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000171 ((['git', 'log', '--pretty=format:%s%n%n%b',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000172 'fake_ancestor_sha...'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000173 'foo'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000174 ((['git', 'config', 'user.email'],), 'me@example.com'),
iannucci@chromium.org79540052012-10-19 23:15:26 +0000175 stat_call,
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000176 ((['git', 'config', 'gerrit.host'],), ''),
177 ((['git', 'log', '--pretty=format:%s\n\n%b',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000178 'fake_ancestor_sha..HEAD'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000179 'desc\n'),
maruel@chromium.orga3353652011-11-30 14:26:57 +0000180 ]
181
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000182 @classmethod
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000183 def _git_upload_no_rev_calls(cls):
184 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000185 ((['git', 'config', 'core.editor'],), ''),
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000186 ]
187
188 @classmethod
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000189 def _git_upload_calls(cls, private):
190 if private:
tyoshino@chromium.org99918ab2013-09-30 06:17:28 +0000191 cc_call = []
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000192 private_call = []
193 else:
tyoshino@chromium.org99918ab2013-09-30 06:17:28 +0000194 cc_call = [((['git', 'config', 'rietveld.cc'],), '')]
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000195 private_call = [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000196 ((['git', 'config', 'rietveld.private'],), '')]
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000197
maruel@chromium.orga3353652011-11-30 14:26:57 +0000198 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000199 ((['git', 'config', 'core.editor'],), ''),
tyoshino@chromium.org99918ab2013-09-30 06:17:28 +0000200 ] + cc_call + private_call + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000201 ((['git', 'config', 'branch.master.base-url'],), ''),
202 ((['git',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000203 'config', '--local', '--get-regexp', '^svn-remote\\.'],),
204 (('', None), 0)),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000205 ((['git', 'rev-parse', '--show-cdup'],), ''),
206 ((['git', 'svn', 'info'],), ''),
207 ((['git',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000208 'config', 'branch.master.rietveldissue', '1'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000209 ((['git', 'config', 'branch.master.rietveldserver',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000210 'https://codereview.example.com'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000211 ((['git',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000212 'config', 'branch.master.rietveldpatchset', '2'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000213 ((['git', 'rev-parse', 'HEAD'],), 'hash'),
214 ((['git', 'symbolic-ref', 'HEAD'],), 'hash'),
215 ((['git',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000216 'config', 'branch.hash.last-upload-hash', 'hash'],), ''),
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000217 ]
218
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000219 @staticmethod
220 def _git_sanity_checks(diff_base, working_branch):
221 fake_ancestor = 'fake_ancestor'
222 fake_cl = 'fake_cl_for_patch'
223 return [
224 # Calls to verify branch point is ancestor
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000225 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000226 'rev-parse', '--verify', diff_base],), fake_ancestor),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000227 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000228 'merge-base', fake_ancestor, 'HEAD'],), fake_ancestor),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000229 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000230 'rev-list', '^' + fake_ancestor, 'HEAD'],), fake_cl),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000231 # Mock a config miss (error code 1)
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000232 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000233 'config', 'gitcl.remotebranch'],), (('', None), 1)),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000234 # Call to GetRemoteBranch()
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000235 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000236 'config', 'branch.%s.merge' % working_branch],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000237 'refs/heads/master'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000238 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000239 'config', 'branch.%s.remote' % working_branch],), 'origin'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000240 ((['git', 'rev-list', '^' + fake_ancestor,
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000241 'refs/remotes/origin/master'],), ''),
242 ]
243
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000244 @classmethod
245 def _dcommit_calls_1(cls):
246 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000247 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000248 'config', '--local', '--get-regexp', '^svn-remote\\.'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000249 ((('svn-remote.svn.url svn://svn.chromium.org/chrome\n'
250 'svn-remote.svn.fetch trunk/src:refs/remotes/origin/master'),
251 None),
252 0)),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000253 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000254 'config', 'rietveld.server'],), 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000255 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
256 ((['git', 'config', '--int', '--get',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000257 'branch.working.git-cl-similarity'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000258 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
259 ((['git', 'config', '--int', '--get',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000260 'branch.working.git-find-copies'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000261 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
262 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000263 'config', 'branch.working.merge'],), 'refs/heads/master'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000264 ((['git', 'config', 'branch.working.remote'],), 'origin'),
265 ((['git', 'rev-list', '--merges',
szager@chromium.orge84b7542012-06-15 21:26:58 +0000266 '--grep=^SVN changes up to revision [0-9]*$',
szager@chromium.org9bb85e22012-06-13 20:28:23 +0000267 'refs/remotes/origin/master^!'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000268 ((['git', 'update-index', '--refresh', '-q'],), ''),
269 ((['git', 'diff-index', '--name-status', 'HEAD'],), ''),
270 ((['git', 'rev-list', '^refs/heads/working',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000271 'refs/remotes/origin/master'],),
272 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000273 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000274 'log', '--grep=^git-svn-id:', '-1', '--pretty=format:%H'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000275 '3fc18b62c4966193eb435baabe2d18a3810ec82e'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000276 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000277 'rev-list', '^3fc18b62c4966193eb435baabe2d18a3810ec82e',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000278 'refs/remotes/origin/master'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000279 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000280 'merge-base', 'refs/remotes/origin/master', 'HEAD'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000281 'fake_ancestor_sha'),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000282 ]
283
284 @classmethod
285 def _dcommit_calls_normal(cls):
286 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000287 ((['git', 'rev-parse', '--show-cdup'],), ''),
288 ((['git', 'rev-parse', 'HEAD'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000289 '00ff397798ea57439712ed7e04ab96e13969ef40'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000290 ((['git',
mcgrathr@chromium.org9249f642013-06-03 21:36:18 +0000291 'diff', '--name-status', '--no-renames', '-r', 'fake_ancestor_sha...',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000292 '.'],),
293 'M\tPRESUBMIT.py'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000294 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000295 'config', 'branch.working.rietveldissue'],), '12345'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000296 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000297 'config', 'branch.working.rietveldpatchset'],), '31137'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000298 ((['git', 'config', 'branch.working.rietveldserver'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000299 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000300 ((['git', 'config', 'user.email'],), 'author@example.com'),
301 ((['git', 'config', 'rietveld.tree-status-url'],), ''),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000302 ]
303
304 @classmethod
305 def _dcommit_calls_bypassed(cls):
306 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000307 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000308 'config', 'branch.working.rietveldissue'],), '12345'),
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'),
311 (('GitClHooksBypassedCommit',
312 'Issue https://codereview.example.com/12345 bypassed hook when '
313 'committing'), None),
314 ]
315
316 @classmethod
317 def _dcommit_calls_3(cls):
318 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000319 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000320 'diff', '--no-ext-diff', '--stat', '--find-copies-harder',
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000321 '-l100000', '-C50', 'fake_ancestor_sha',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000322 'refs/heads/working'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000323 (' PRESUBMIT.py | 2 +-\n'
324 ' 1 files changed, 1 insertions(+), 1 deletions(-)\n')),
325 (('About to commit; enter to confirm.',), None),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000326 ((['git', 'show-ref', '--quiet', '--verify',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000327 'refs/heads/git-cl-commit'],),
328 (('', None), 0)),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000329 ((['git', 'branch', '-D', 'git-cl-commit'],), ''),
330 ((['git', 'show-ref', '--quiet', '--verify',
szager@chromium.org9bb85e22012-06-13 20:28:23 +0000331 'refs/heads/git-cl-cherry-pick'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000332 ((['git', 'rev-parse', '--show-cdup'],), '\n'),
333 ((['git', 'checkout', '-q', '-b', 'git-cl-commit'],), ''),
334 ((['git', 'reset', '--soft', 'fake_ancestor_sha'],), ''),
335 ((['git', 'commit', '-m',
maruel@chromium.orge52678e2013-04-26 18:34:44 +0000336 'Issue: 12345\n\nR=john@chromium.org\n\n'
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000337 'Review URL: https://codereview.example.com/12345'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000338 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000339 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000340 'svn', 'dcommit', '-C50', '--no-rebase', '--rmdir'],),
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000341 (('', None), 0)),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000342 ((['git', 'checkout', '-q', 'working'],), ''),
343 ((['git', 'branch', '-D', 'git-cl-commit'],), ''),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000344 ]
345
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000346 @staticmethod
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000347 def _cmd_line(description, args, similarity, find_copies, private):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000348 """Returns the upload command line passed to upload.RealMain()."""
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000349 return [
350 'upload', '--assume_yes', '--server',
maruel@chromium.orgeb5edbc2012-01-16 17:03:28 +0000351 'https://codereview.example.com',
maruel@chromium.org71e12a92012-02-14 02:34:15 +0000352 '--message', description
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000353 ] + args + [
354 '--cc', 'joe@example.com',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000355 ] + (['--private'] if private else []) + [
iannucci@chromium.org79540052012-10-19 23:15:26 +0000356 '--git_similarity', similarity or '50'
357 ] + (['--git_no_find_copies'] if find_copies == False else []) + [
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000358 'fake_ancestor_sha', 'HEAD'
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000359 ]
360
361 def _run_reviewer_test(
362 self,
363 upload_args,
364 expected_description,
365 returned_description,
366 final_description,
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000367 reviewers,
368 private=False):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000369 """Generic reviewer test framework."""
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000370 try:
371 similarity = upload_args[upload_args.index('--similarity')+1]
372 except ValueError:
373 similarity = None
iannucci@chromium.org79540052012-10-19 23:15:26 +0000374
375 if '--find-copies' in upload_args:
376 find_copies = True
377 elif '--no-find-copies' in upload_args:
378 find_copies = False
379 else:
380 find_copies = None
381
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000382 private = '--private' in upload_args
383
384 self.calls = self._upload_calls(similarity, find_copies, private)
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000385 def RunEditor(desc, _, **kwargs):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000386 self.assertEquals(
387 '# Enter a description of the change.\n'
janx@chromium.org104b2db2013-04-18 12:58:40 +0000388 '# This will be displayed on the codereview site.\n'
alancutter@chromium.org63a4d7f2013-05-31 02:22:45 +0000389 '# The first line will also be used as the subject of the review.\n'
alancutter@chromium.orgbd1073e2013-06-01 00:34:38 +0000390 '#--------------------This line is 72 characters long'
391 '--------------------\n' +
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000392 expected_description,
393 desc)
394 return returned_description
395 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor)
396 def check_upload(args):
iannucci@chromium.org79540052012-10-19 23:15:26 +0000397 cmd_line = self._cmd_line(final_description, reviewers, similarity,
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000398 find_copies, private)
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000399 self.assertEquals(cmd_line, args)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000400 return 1, 2
401 self.mock(git_cl.upload, 'RealMain', check_upload)
402 git_cl.main(['upload'] + upload_args)
403
404 def test_no_reviewer(self):
405 self._run_reviewer_test(
406 [],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000407 'desc\n\nBUG=',
408 '# Blah blah comment.\ndesc\n\nBUG=',
409 'desc\n\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000410 [])
411
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000412 def test_keep_similarity(self):
413 self._run_reviewer_test(
414 ['--similarity', '70'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000415 'desc\n\nBUG=',
416 '# Blah blah comment.\ndesc\n\nBUG=',
417 'desc\n\nBUG=',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000418 [])
419
iannucci@chromium.org79540052012-10-19 23:15:26 +0000420 def test_keep_find_copies(self):
421 self._run_reviewer_test(
422 ['--no-find-copies'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000423 'desc\n\nBUG=',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000424 '# Blah blah comment.\ndesc\n\nBUG=\n',
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000425 'desc\n\nBUG=',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000426 [])
427
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000428 def test_private(self):
429 self._run_reviewer_test(
430 ['--private'],
431 'desc\n\nBUG=',
432 '# Blah blah comment.\ndesc\n\nBUG=\n',
433 'desc\n\nBUG=',
434 [])
435
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000436 def test_reviewers_cmd_line(self):
437 # Reviewer is passed as-is
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000438 description = 'desc\n\nR=foo@example.com\nBUG='
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000439 self._run_reviewer_test(
440 ['-r' 'foo@example.com'],
441 description,
442 '\n%s\n' % description,
443 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000444 ['--reviewers=foo@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000445
446 def test_reviewer_tbr_overriden(self):
447 # Reviewer is overriden with TBR
448 # Also verifies the regexp work without a trailing LF
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000449 description = 'Foo Bar\n\nTBR=reviewer@example.com'
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000450 self._run_reviewer_test(
451 ['-r' 'foo@example.com'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000452 'desc\n\nR=foo@example.com\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000453 description.strip('\n'),
454 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000455 ['--reviewers=reviewer@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000456
457 def test_reviewer_multiple(self):
458 # Handles multiple R= or TBR= lines.
459 description = (
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000460 'Foo Bar\nTBR=reviewer@example.com\nBUG=\nR=another@example.com')
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000461 self._run_reviewer_test(
462 [],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000463 'desc\n\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000464 description,
465 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000466 ['--reviewers=another@example.com,reviewer@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000467
maruel@chromium.orga3353652011-11-30 14:26:57 +0000468 def test_reviewer_send_mail(self):
469 # --send-mail can be used without -r if R= is used
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000470 description = 'Foo Bar\nR=reviewer@example.com'
maruel@chromium.orga3353652011-11-30 14:26:57 +0000471 self._run_reviewer_test(
472 ['--send-mail'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000473 'desc\n\nBUG=',
maruel@chromium.orga3353652011-11-30 14:26:57 +0000474 description.strip('\n'),
475 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000476 ['--reviewers=reviewer@example.com', '--send_mail'])
maruel@chromium.orga3353652011-11-30 14:26:57 +0000477
478 def test_reviewer_send_mail_no_rev(self):
479 # Fails without a reviewer.
maruel@chromium.org2e23ce32013-05-07 12:42:28 +0000480 stdout = StringIO.StringIO()
481 stderr = StringIO.StringIO()
maruel@chromium.orga3353652011-11-30 14:26:57 +0000482 try:
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000483 self.calls = self._upload_no_rev_calls(None, None)
484 def RunEditor(desc, _, **kwargs):
maruel@chromium.orga3353652011-11-30 14:26:57 +0000485 return desc
486 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor)
maruel@chromium.org2e23ce32013-05-07 12:42:28 +0000487 self.mock(sys, 'stdout', stdout)
488 self.mock(sys, 'stderr', stderr)
maruel@chromium.orga3353652011-11-30 14:26:57 +0000489 git_cl.main(['upload', '--send-mail'])
490 self.fail()
491 except SystemExit:
maruel@chromium.org2e23ce32013-05-07 12:42:28 +0000492 self.assertEqual(
493 'Using 50% similarity for rename/copy detection. Override with '
494 '--similarity.\n',
495 stdout.getvalue())
496 self.assertEqual(
497 'Must specify reviewers to send email.\n', stderr.getvalue())
maruel@chromium.orga3353652011-11-30 14:26:57 +0000498
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000499 def test_dcommit(self):
500 self.calls = (
501 self._dcommit_calls_1() +
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000502 self._git_sanity_checks('fake_ancestor_sha', 'working') +
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000503 self._dcommit_calls_normal() +
504 self._dcommit_calls_3())
505 git_cl.main(['dcommit'])
506
507 def test_dcommit_bypass_hooks(self):
508 self.calls = (
509 self._dcommit_calls_1() +
510 self._dcommit_calls_bypassed() +
511 self._dcommit_calls_3())
512 git_cl.main(['dcommit', '--bypass-hooks'])
513
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000514
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000515 @classmethod
516 def _gerrit_base_calls(cls):
ukai@chromium.orge8077812012-02-03 03:41:46 +0000517 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000518 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000519 'config', 'rietveld.server'],), 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000520 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
521 ((['git', 'config', '--int', '--get',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000522 'branch.master.git-cl-similarity'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000523 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
524 ((['git', 'config', '--int', '--get',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000525 'branch.master.git-find-copies'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000526 ((['git', 'update-index', '--refresh', '-q'],), ''),
527 ((['git', 'diff-index', '--name-status', 'HEAD'],), ''),
528 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
529 ((['git', 'config', 'branch.master.merge'],), 'master'),
530 ((['git', 'config', 'branch.master.remote'],), 'origin'),
531 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000532 'merge-base', 'master', 'HEAD'],), 'fake_ancestor_sha'),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000533 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000534 ((['git', 'rev-parse', '--show-cdup'],), ''),
535 ((['git', 'rev-parse', 'HEAD'],), '12345'),
536 ((['git',
mcgrathr@chromium.org9249f642013-06-03 21:36:18 +0000537 'diff', '--name-status', '--no-renames', '-r',
538 'fake_ancestor_sha...', '.'],),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000539 'M\t.gitignore\n'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000540 ((['git', 'config', 'branch.master.rietveldissue'],), ''),
541 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000542 'config', 'branch.master.rietveldpatchset'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000543 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000544 'log', '--pretty=format:%s%n%n%b', 'fake_ancestor_sha...'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000545 'foo'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000546 ((['git', 'config', 'user.email'],), 'me@example.com'),
547 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000548 'diff', '--no-ext-diff', '--stat', '--find-copies-harder',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000549 '-l100000', '-C50', 'fake_ancestor_sha', 'HEAD'],),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000550 '+dat'),
551 ]
552
553 @staticmethod
554 def _gerrit_upload_calls(description, reviewers):
555 calls = [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000556 ((['git', 'config', 'gerrit.host'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000557 'gerrit.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000558 ((['git', 'log', '--pretty=format:%s\n\n%b',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000559 'fake_ancestor_sha..HEAD'],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000560 description)
561 ]
562 if git_cl.CHANGE_ID not in description:
563 calls += [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000564 ((['git', 'log', '--pretty=format:%s\n\n%b',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000565 'fake_ancestor_sha..HEAD'],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000566 description),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000567 ((['git', 'commit', '--amend', '-m', description],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000568 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000569 ((['git', 'log', '--pretty=format:%s\n\n%b',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000570 'fake_ancestor_sha..HEAD'],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000571 description)
572 ]
573 calls += [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000574 ((['git', 'config', 'rietveld.cc'],), '')
ukai@chromium.orge8077812012-02-03 03:41:46 +0000575 ]
ukai@chromium.org19bbfa22012-02-03 16:18:11 +0000576 receive_pack = '--receive-pack=git receive-pack '
ukai@chromium.orge8077812012-02-03 03:41:46 +0000577 receive_pack += '--cc=joe@example.com' # from watch list
578 if reviewers:
579 receive_pack += ' '
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000580 receive_pack += ' '.join(
581 '--reviewer=' + email for email in sorted(reviewers))
ukai@chromium.org19bbfa22012-02-03 16:18:11 +0000582 receive_pack += ''
ukai@chromium.orge8077812012-02-03 03:41:46 +0000583 calls += [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000584 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000585 'push', receive_pack, 'origin', 'HEAD:refs/for/master'],),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000586 '')
587 ]
588 return calls
589
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000590 def _run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000591 self,
592 upload_args,
593 description,
594 reviewers):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000595 """Generic gerrit upload test framework."""
ukai@chromium.orge8077812012-02-03 03:41:46 +0000596 self.calls = self._gerrit_base_calls()
597 self.calls += self._gerrit_upload_calls(description, reviewers)
598 git_cl.main(['upload'] + upload_args)
599
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000600 def test_gerrit_upload_without_change_id(self):
601 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000602 [],
jamesr@chromium.org35d1a842012-07-27 00:20:43 +0000603 'desc\n\nBUG=\n',
ukai@chromium.orge8077812012-02-03 03:41:46 +0000604 [])
605
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000606 def test_gerrit_no_reviewer(self):
607 self._run_gerrit_upload_test(
608 [],
609 'desc\n\nBUG=\nChange-Id:123456789\n',
610 [])
611
ukai@chromium.orge8077812012-02-03 03:41:46 +0000612 def test_gerrit_reviewers_cmd_line(self):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000613 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000614 ['-r', 'foo@example.com'],
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000615 'desc\n\nBUG=\nChange-Id:123456789',
ukai@chromium.orge8077812012-02-03 03:41:46 +0000616 ['foo@example.com'])
617
618 def test_gerrit_reviewer_multiple(self):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000619 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000620 [],
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000621 'desc\nTBR=reviewer@example.com\nBUG=\nR=another@example.com\n'
622 'Change-Id:123456789\n',
ukai@chromium.orge8077812012-02-03 03:41:46 +0000623 ['reviewer@example.com', 'another@example.com'])
624
625
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000626 def test_config_gerrit_download_hook(self):
627 self.mock(git_cl, 'FindCodereviewSettingsFile', CodereviewSettingsFileMock)
628 def ParseCodereviewSettingsContent(content):
629 keyvals = {}
630 keyvals['CODE_REVIEW_SERVER'] = 'gerrit.chromium.org'
631 keyvals['GERRIT_HOST'] = 'gerrit.chromium.org'
632 keyvals['GERRIT_PORT'] = '29418'
633 return keyvals
634 self.mock(git_cl.gclient_utils, 'ParseCodereviewSettingsContent',
635 ParseCodereviewSettingsContent)
636 self.mock(git_cl.os, 'access', self._mocked_call)
637 self.mock(git_cl.os, 'chmod', self._mocked_call)
ukai@chromium.org91655502012-05-25 01:46:07 +0000638 src_dir = os.path.join(os.path.sep, 'usr', 'local', 'src')
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000639 def AbsPath(path):
ukai@chromium.org91655502012-05-25 01:46:07 +0000640 if not path.startswith(os.path.sep):
641 return os.path.join(src_dir, path)
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000642 return path
643 self.mock(git_cl.os.path, 'abspath', AbsPath)
ukai@chromium.org91655502012-05-25 01:46:07 +0000644 commit_msg_path = os.path.join(src_dir, '.git', 'hooks', 'commit-msg')
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000645 def Exists(path):
ukai@chromium.org91655502012-05-25 01:46:07 +0000646 if path == commit_msg_path:
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000647 return False
648 # others paths, such as /usr/share/locale/....
649 return True
650 self.mock(git_cl.os.path, 'exists', Exists)
joshua.lock@intel.com426f69b2012-08-02 23:41:49 +0000651 self.mock(git_cl, 'urlretrieve', self._mocked_call)
ukai@chromium.org712d6102013-11-27 00:52:58 +0000652 self.mock(git_cl, 'hasSheBang', self._mocked_call)
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000653 self.calls = [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000654 ((['git', 'config', 'rietveld.server',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000655 'gerrit.chromium.org'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000656 ((['git', 'config', '--unset-all', 'rietveld.cc'],), ''),
657 ((['git', 'config', '--unset-all',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000658 'rietveld.private'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000659 ((['git', 'config', '--unset-all',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000660 'rietveld.tree-status-url'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000661 ((['git', 'config', '--unset-all',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000662 'rietveld.viewvc-url'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000663 ((['git', 'config', 'gerrit.host',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000664 'gerrit.chromium.org'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000665 ((['git', 'config', 'gerrit.port', '29418'],), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000666 # DownloadHooks(False)
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000667 ((['git', 'config', 'gerrit.host'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000668 'gerrit.chromium.org'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000669 ((['git', 'rev-parse', '--show-cdup'],), ''),
ukai@chromium.org91655502012-05-25 01:46:07 +0000670 ((commit_msg_path, os.X_OK,), False),
ukai@chromium.org712d6102013-11-27 00:52:58 +0000671 (('https://gerrit-review.googlesource.com/tools/hooks/commit-msg',
ukai@chromium.org91655502012-05-25 01:46:07 +0000672 commit_msg_path,), ''),
ukai@chromium.org712d6102013-11-27 00:52:58 +0000673 ((commit_msg_path,), True),
ukai@chromium.org91655502012-05-25 01:46:07 +0000674 ((commit_msg_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR,), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000675 # GetCodereviewSettingsInteractively
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000676 ((['git', 'config', 'rietveld.server'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000677 'gerrit.chromium.org'),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000678 (('Rietveld server (host[:port]) [https://gerrit.chromium.org]:',),
679 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000680 ((['git', 'config', 'rietveld.cc'],), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000681 (('CC list:',), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000682 ((['git', 'config', 'rietveld.private'],), ''),
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000683 (('Private flag (rietveld only):',), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000684 ((['git', 'config', 'rietveld.tree-status-url'],), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000685 (('Tree status URL:',), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000686 ((['git', 'config', 'rietveld.viewvc-url'],), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000687 (('ViewVC URL:',), ''),
688 # DownloadHooks(True)
ukai@chromium.org91655502012-05-25 01:46:07 +0000689 ((commit_msg_path, os.X_OK,), True),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000690 ]
691 git_cl.main(['config'])
692
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000693 def test_update_reviewers(self):
694 data = [
695 ('foo', [], 'foo'),
agable@chromium.org42c20792013-09-12 17:34:49 +0000696 ('foo\nR=xx', [], 'foo\nR=xx'),
697 ('foo\nTBR=xx', [], 'foo\nTBR=xx'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000698 ('foo', ['a@c'], 'foo\n\nR=a@c'),
agable@chromium.org42c20792013-09-12 17:34:49 +0000699 ('foo\nR=xx', ['a@c'], 'foo\n\nR=a@c, xx'),
700 ('foo\nTBR=xx', ['a@c'], 'foo\n\nR=a@c\nTBR=xx'),
701 ('foo\nTBR=xx\nR=yy', ['a@c'], 'foo\n\nR=a@c, yy\nTBR=xx'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000702 ('foo\nBUG=', ['a@c'], 'foo\nBUG=\nR=a@c'),
agable@chromium.org42c20792013-09-12 17:34:49 +0000703 ('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 +0000704 ('foo', ['a@c', 'b@c'], 'foo\n\nR=a@c, b@c'),
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +0000705 ('foo\nBar\n\nR=\nBUG=', ['c@c'], 'foo\nBar\n\nR=c@c\nBUG='),
706 ('foo\nBar\n\nR=\nBUG=\nR=', ['c@c'], 'foo\nBar\n\nR=c@c\nBUG='),
707 # Same as the line before, but full of whitespaces.
708 (
709 'foo\nBar\n\n R = \n BUG = \n R = ', ['c@c'],
710 'foo\nBar\n\nR=c@c\n BUG =',
711 ),
712 # Whitespaces aren't interpreted as new lines.
713 ('foo BUG=allo R=joe ', ['c@c'], 'foo BUG=allo R=joe\n\nR=c@c'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000714 ]
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +0000715 expected = [i[2] for i in data]
716 actual = []
717 for orig, reviewers, _expected in data:
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000718 obj = git_cl.ChangeDescription(orig)
719 obj.update_reviewers(reviewers)
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +0000720 actual.append(obj.description)
721 self.assertEqual(expected, actual)
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000722
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000723
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000724if __name__ == '__main__':
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000725 git_cl.logging.basicConfig(
726 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000727 unittest.main()