blob: d6610653a0a5a3e3eb219ad9d0a89df5d8a8da5c [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
iannucci@chromium.org79540052012-10-19 23:15:26 +0000112 def _upload_calls(cls, similarity, find_copies):
113 return (cls._git_base_calls(similarity, find_copies) +
114 cls._git_upload_calls())
maruel@chromium.orga3353652011-11-30 14:26:57 +0000115
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000116 @classmethod
117 def _git_base_calls(cls, similarity, find_copies):
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000118 if similarity is None:
119 similarity = '50'
120 similarity_call = ((['git', 'config', '--int', '--get',
121 'branch.master.git-cl-similarity'],), '')
122 else:
123 similarity_call = ((['git', 'config', '--int',
124 'branch.master.git-cl-similarity', similarity],), '')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000125
126 if find_copies is None:
127 find_copies = True
128 find_copies_call = ((['git', 'config', '--int', '--get',
129 'branch.master.git-find-copies'],), '')
130 else:
131 val = str(int(find_copies))
132 find_copies_call = ((['git', 'config', '--int',
133 'branch.master.git-find-copies', val],), '')
134
135 if find_copies:
136 stat_call = ((['git', 'diff', '--no-ext-diff', '--stat',
137 '--find-copies-harder', '-l100000', '-C'+similarity,
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000138 'fake_ancestor_sha', 'HEAD'],), '+dat')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000139 else:
140 stat_call = ((['git', 'diff', '--no-ext-diff', '--stat',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000141 '-M'+similarity, 'fake_ancestor_sha', 'HEAD'],), '+dat')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000142
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000143 return [
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000144 ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'),
145 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
146 similarity_call,
iannucci@chromium.org79540052012-10-19 23:15:26 +0000147 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
148 find_copies_call,
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000149 ((['git', 'update-index', '--refresh', '-q'],), ''),
ukai@chromium.org259e4682012-10-25 07:36:33 +0000150 ((['git', 'diff-index', '--name-status', 'HEAD'],), ''),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000151 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
152 ((['git', 'config', 'branch.master.merge'],), 'master'),
153 ((['git', 'config', 'branch.master.remote'],), 'origin'),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000154 ((['git', 'merge-base', 'master', 'HEAD'],), 'fake_ancestor_sha'),
155 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000156 ((['git', 'rev-parse', '--show-cdup'],), ''),
157 ((['git', 'rev-parse', 'HEAD'],), '12345'),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000158 ((['git', 'diff', '--name-status', '-r', 'fake_ancestor_sha...', '.'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000159 'M\t.gitignore\n'),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000160 ((['git', 'config', 'branch.master.rietveldissue'],), ''),
161 ((['git', 'config', 'branch.master.rietveldpatchset'],), ''),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000162 ((['git', 'log', '--pretty=format:%s%n%n%b', 'fake_ancestor_sha...'],),
163 'foo'),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000164 ((['git', 'config', 'user.email'],), 'me@example.com'),
iannucci@chromium.org79540052012-10-19 23:15:26 +0000165 stat_call,
ukai@chromium.org8ef7ab22012-11-28 04:24:52 +0000166 ((['git', 'config', 'gerrit.host'],), ''),
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000167 ((['git', 'log', '--pretty=format:%s\n\n%b', 'fake_ancestor_sha..HEAD'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000168 'desc\n'),
maruel@chromium.orga3353652011-11-30 14:26:57 +0000169 ]
170
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000171 @classmethod
172 def _git_upload_calls(cls):
maruel@chromium.orga3353652011-11-30 14:26:57 +0000173 return [
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000174 ((['git', 'config', 'rietveld.cc'],), ''),
kalmard@homejinni.com6b0051e2012-04-03 15:45:08 +0000175 ((['git', 'config', 'branch.master.base-url'],), ''),
zimmerle@gmail.com3cdcf562013-04-12 19:39:38 +0000176 ((['git', 'config', '--local', '--get-regexp', '^svn-remote\\.'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000177 (('', None), 0)),
178 ((['git', 'rev-parse', '--show-cdup'],), ''),
179 ((['git', 'svn', 'info'],), ''),
180 ((['git', 'config', 'branch.master.rietveldissue', '1'],), ''),
181 ((['git', 'config', 'branch.master.rietveldserver',
182 'https://codereview.example.com'],), ''),
183 ((['git', 'config', 'branch.master.rietveldpatchset', '2'],), ''),
rogerta@chromium.orgcaa16552013-03-18 20:45:05 +0000184 ((['git', 'rev-parse', 'HEAD'],), 'hash'),
185 ((['git', 'symbolic-ref', 'HEAD'],), 'hash'),
186 ((['git', 'config', 'branch.hash.last-upload-hash', 'hash'],), ''),
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000187 ]
188
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000189 @staticmethod
190 def _git_sanity_checks(diff_base, working_branch):
191 fake_ancestor = 'fake_ancestor'
192 fake_cl = 'fake_cl_for_patch'
193 return [
194 # Calls to verify branch point is ancestor
195 ((['git', 'rev-parse', '--verify', diff_base],), fake_ancestor),
196 ((['git', 'merge-base', fake_ancestor, 'HEAD'],), fake_ancestor),
197 ((['git', 'rev-list', '^' + fake_ancestor, 'HEAD'],), fake_cl),
198 # Mock a config miss (error code 1)
199 ((['git', 'config', 'gitcl.remotebranch'],), (('', None), 1)),
200 # Call to GetRemoteBranch()
201 ((['git', 'config', 'branch.%s.merge' % working_branch],),
202 'refs/heads/master'),
203 ((['git', 'config', 'branch.%s.remote' % working_branch],), 'origin'),
204 ((['git', 'rev-list', '^' + fake_ancestor,
205 'refs/remotes/origin/master'],), ''),
206 ]
207
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000208 @classmethod
209 def _dcommit_calls_1(cls):
210 return [
zimmerle@gmail.com3cdcf562013-04-12 19:39:38 +0000211 ((['git', 'config', '--local', '--get-regexp', '^svn-remote\\.'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000212 ((('svn-remote.svn.url svn://svn.chromium.org/chrome\n'
213 'svn-remote.svn.fetch trunk/src:refs/remotes/origin/master'),
214 None),
215 0)),
216 ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'),
217 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000218 ((['git', 'config', '--int', '--get',
219 'branch.working.git-cl-similarity'],), ''),
220 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
iannucci@chromium.org79540052012-10-19 23:15:26 +0000221 ((['git', 'config', '--int', '--get',
222 'branch.working.git-find-copies'],), ''),
223 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000224 ((['git', 'config', 'branch.working.merge'],), 'refs/heads/master'),
225 ((['git', 'config', 'branch.working.remote'],), 'origin'),
szager@chromium.org9bb85e22012-06-13 20:28:23 +0000226 ((['git', 'rev-list', '--merges',
szager@chromium.orge84b7542012-06-15 21:26:58 +0000227 '--grep=^SVN changes up to revision [0-9]*$',
szager@chromium.org9bb85e22012-06-13 20:28:23 +0000228 'refs/remotes/origin/master^!'],), ''),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000229 ((['git', 'update-index', '--refresh', '-q'],), ''),
ukai@chromium.org259e4682012-10-25 07:36:33 +0000230 ((['git', 'diff-index', '--name-status', 'HEAD'],), ''),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000231 ((['git', 'rev-list', '^refs/heads/working',
232 'refs/remotes/origin/master'],),
233 ''),
234 ((['git', 'log', '--grep=^git-svn-id:', '-1', '--pretty=format:%H'],),
235 '3fc18b62c4966193eb435baabe2d18a3810ec82e'),
236 ((['git', 'rev-list', '^3fc18b62c4966193eb435baabe2d18a3810ec82e',
237 'refs/remotes/origin/master'],), ''),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000238 ((['git', 'merge-base', 'refs/remotes/origin/master', 'HEAD'],),
239 'fake_ancestor_sha'),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000240 ]
241
242 @classmethod
243 def _dcommit_calls_normal(cls):
244 return [
245 ((['git', 'rev-parse', '--show-cdup'],), ''),
246 ((['git', 'rev-parse', 'HEAD'],),
247 '00ff397798ea57439712ed7e04ab96e13969ef40'),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000248 ((['git', 'diff', '--name-status', '-r', 'fake_ancestor_sha...',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000249 '.'],),
250 'M\tPRESUBMIT.py'),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000251 ((['git', 'config', 'branch.working.rietveldissue'],), '12345'),
evan@chromium.org0af9b702012-02-11 00:42:16 +0000252 ((['git', 'config', 'branch.working.rietveldpatchset'],), '31137'),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000253 ((['git', 'config', 'branch.working.rietveldserver'],),
254 'codereview.example.com'),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000255 ((['git', 'config', 'user.email'],), 'author@example.com'),
256 ((['git', 'config', 'rietveld.tree-status-url'],), ''),
257 ]
258
259 @classmethod
260 def _dcommit_calls_bypassed(cls):
261 return [
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000262 ((['git', 'config', 'branch.working.rietveldissue'],), '12345'),
263 ((['git', 'config', 'branch.working.rietveldserver'],),
264 'codereview.example.com'),
265 (('GitClHooksBypassedCommit',
266 'Issue https://codereview.example.com/12345 bypassed hook when '
267 'committing'), None),
268 ]
269
270 @classmethod
271 def _dcommit_calls_3(cls):
272 return [
maruel@chromium.org49e3d802012-07-18 23:54:45 +0000273 ((['git', 'diff', '--no-ext-diff', '--stat', '--find-copies-harder',
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000274 '-l100000', '-C50', 'fake_ancestor_sha',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000275 'refs/heads/working'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000276 (' PRESUBMIT.py | 2 +-\n'
277 ' 1 files changed, 1 insertions(+), 1 deletions(-)\n')),
278 (('About to commit; enter to confirm.',), None),
279 ((['git', 'show-ref', '--quiet', '--verify',
280 'refs/heads/git-cl-commit'],),
281 (('', None), 0)),
282 ((['git', 'branch', '-D', 'git-cl-commit'],), ''),
szager@chromium.org9bb85e22012-06-13 20:28:23 +0000283 ((['git', 'show-ref', '--quiet', '--verify',
284 'refs/heads/git-cl-cherry-pick'],), ''),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000285 ((['git', 'rev-parse', '--show-cdup'],), '\n'),
286 ((['git', 'checkout', '-q', '-b', 'git-cl-commit'],), ''),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000287 ((['git', 'reset', '--soft', 'fake_ancestor_sha'],), ''),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000288 ((['git', 'commit', '-m',
maruel@chromium.orge52678e2013-04-26 18:34:44 +0000289 'Issue: 12345\n\nR=john@chromium.org\n\n'
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000290 'Review URL: https://codereview.example.com/12345'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000291 ''),
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000292 ((['git', 'svn', 'dcommit', '-C50', '--no-rebase', '--rmdir'],),
293 (('', None), 0)),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000294 ((['git', 'checkout', '-q', 'working'],), ''),
295 ((['git', 'branch', '-D', 'git-cl-commit'],), ''),
296 ]
297
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000298 @staticmethod
iannucci@chromium.org79540052012-10-19 23:15:26 +0000299 def _cmd_line(description, args, similarity, find_copies):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000300 """Returns the upload command line passed to upload.RealMain()."""
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000301 return [
302 'upload', '--assume_yes', '--server',
maruel@chromium.orgeb5edbc2012-01-16 17:03:28 +0000303 'https://codereview.example.com',
maruel@chromium.org71e12a92012-02-14 02:34:15 +0000304 '--message', description
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000305 ] + args + [
306 '--cc', 'joe@example.com',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000307 '--git_similarity', similarity or '50'
308 ] + (['--git_no_find_copies'] if find_copies == False else []) + [
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000309 'fake_ancestor_sha', 'HEAD'
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000310 ]
311
312 def _run_reviewer_test(
313 self,
314 upload_args,
315 expected_description,
316 returned_description,
317 final_description,
318 reviewers):
319 """Generic reviewer test framework."""
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000320 try:
321 similarity = upload_args[upload_args.index('--similarity')+1]
322 except ValueError:
323 similarity = None
iannucci@chromium.org79540052012-10-19 23:15:26 +0000324
325 if '--find-copies' in upload_args:
326 find_copies = True
327 elif '--no-find-copies' in upload_args:
328 find_copies = False
329 else:
330 find_copies = None
331
332 self.calls = self._upload_calls(similarity, find_copies)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000333 def RunEditor(desc, _):
334 self.assertEquals(
335 '# Enter a description of the change.\n'
janx@chromium.org104b2db2013-04-18 12:58:40 +0000336 '# This will be displayed on the codereview site.\n'
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000337 '# The first line will also be used as the subject of the review.\n' +
338 expected_description,
339 desc)
340 return returned_description
341 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor)
342 def check_upload(args):
iannucci@chromium.org79540052012-10-19 23:15:26 +0000343 cmd_line = self._cmd_line(final_description, reviewers, similarity,
344 find_copies)
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000345 self.assertEquals(cmd_line, args)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000346 return 1, 2
347 self.mock(git_cl.upload, 'RealMain', check_upload)
348 git_cl.main(['upload'] + upload_args)
349
350 def test_no_reviewer(self):
351 self._run_reviewer_test(
352 [],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000353 'desc\n\nBUG=',
354 '# Blah blah comment.\ndesc\n\nBUG=',
355 'desc\n\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000356 [])
357
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000358 def test_keep_similarity(self):
359 self._run_reviewer_test(
360 ['--similarity', '70'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000361 'desc\n\nBUG=',
362 '# Blah blah comment.\ndesc\n\nBUG=',
363 'desc\n\nBUG=',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000364 [])
365
iannucci@chromium.org79540052012-10-19 23:15:26 +0000366 def test_keep_find_copies(self):
367 self._run_reviewer_test(
368 ['--no-find-copies'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000369 'desc\n\nBUG=',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000370 '# Blah blah comment.\ndesc\n\nBUG=\n',
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000371 'desc\n\nBUG=',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000372 [])
373
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000374 def test_reviewers_cmd_line(self):
375 # Reviewer is passed as-is
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000376 description = 'desc\n\nR=foo@example.com\nBUG='
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000377 self._run_reviewer_test(
378 ['-r' 'foo@example.com'],
379 description,
380 '\n%s\n' % description,
381 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000382 ['--reviewers=foo@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000383
384 def test_reviewer_tbr_overriden(self):
385 # Reviewer is overriden with TBR
386 # Also verifies the regexp work without a trailing LF
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000387 description = 'Foo Bar\n\nTBR=reviewer@example.com'
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000388 self._run_reviewer_test(
389 ['-r' 'foo@example.com'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000390 'desc\n\nR=foo@example.com\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000391 description.strip('\n'),
392 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000393 ['--reviewers=reviewer@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000394
395 def test_reviewer_multiple(self):
396 # Handles multiple R= or TBR= lines.
397 description = (
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000398 'Foo Bar\nTBR=reviewer@example.com\nBUG=\nR=another@example.com')
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000399 self._run_reviewer_test(
400 [],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000401 'desc\n\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000402 description,
403 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000404 ['--reviewers=another@example.com,reviewer@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000405
maruel@chromium.orga3353652011-11-30 14:26:57 +0000406 def test_reviewer_send_mail(self):
407 # --send-mail can be used without -r if R= is used
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000408 description = 'Foo Bar\nR=reviewer@example.com'
maruel@chromium.orga3353652011-11-30 14:26:57 +0000409 self._run_reviewer_test(
410 ['--send-mail'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000411 'desc\n\nBUG=',
maruel@chromium.orga3353652011-11-30 14:26:57 +0000412 description.strip('\n'),
413 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000414 ['--reviewers=reviewer@example.com', '--send_mail'])
maruel@chromium.orga3353652011-11-30 14:26:57 +0000415
416 def test_reviewer_send_mail_no_rev(self):
417 # Fails without a reviewer.
418 class FileMock(object):
419 buf = StringIO.StringIO()
420 def write(self, content):
421 self.buf.write(content)
422
423 mock = FileMock()
424 try:
iannucci@chromium.org79540052012-10-19 23:15:26 +0000425 self.calls = self._git_base_calls(None, None)
maruel@chromium.orga3353652011-11-30 14:26:57 +0000426 def RunEditor(desc, _):
427 return desc
428 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor)
429 self.mock(sys, 'stderr', mock)
430 git_cl.main(['upload', '--send-mail'])
431 self.fail()
432 except SystemExit:
433 self.assertEquals(
434 'Must specify reviewers to send email.\n', mock.buf.getvalue())
435
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000436 def test_dcommit(self):
437 self.calls = (
438 self._dcommit_calls_1() +
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000439 self._git_sanity_checks('fake_ancestor_sha', 'working') +
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000440 self._dcommit_calls_normal() +
441 self._dcommit_calls_3())
442 git_cl.main(['dcommit'])
443
444 def test_dcommit_bypass_hooks(self):
445 self.calls = (
446 self._dcommit_calls_1() +
447 self._dcommit_calls_bypassed() +
448 self._dcommit_calls_3())
449 git_cl.main(['dcommit', '--bypass-hooks'])
450
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000451
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000452 @classmethod
453 def _gerrit_base_calls(cls):
ukai@chromium.orge8077812012-02-03 03:41:46 +0000454 return [
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000455 ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'),
456 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
457 ((['git', 'config', '--int', '--get',
458 'branch.master.git-cl-similarity'],), ''),
iannucci@chromium.org79540052012-10-19 23:15:26 +0000459 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
460 ((['git', 'config', '--int', '--get',
461 'branch.master.git-find-copies'],), ''),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000462 ((['git', 'update-index', '--refresh', '-q'],), ''),
ukai@chromium.org259e4682012-10-25 07:36:33 +0000463 ((['git', 'diff-index', '--name-status', 'HEAD'],), ''),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000464 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
465 ((['git', 'config', 'branch.master.merge'],), 'master'),
466 ((['git', 'config', 'branch.master.remote'],), 'origin'),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000467 ((['git', 'merge-base', 'master', 'HEAD'],), 'fake_ancestor_sha'),
468 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [
ukai@chromium.orge8077812012-02-03 03:41:46 +0000469 ((['git', 'rev-parse', '--show-cdup'],), ''),
470 ((['git', 'rev-parse', 'HEAD'],), '12345'),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000471 ((['git', 'diff', '--name-status', '-r', 'fake_ancestor_sha...', '.'],),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000472 'M\t.gitignore\n'),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000473 ((['git', 'config', 'branch.master.rietveldissue'],), ''),
474 ((['git', 'config', 'branch.master.rietveldpatchset'],), ''),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000475 ((['git', 'log', '--pretty=format:%s%n%n%b', 'fake_ancestor_sha...'],),
476 'foo'),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000477 ((['git', 'config', 'user.email'],), 'me@example.com'),
cmp@chromium.org5cf70222012-06-12 18:20:13 +0000478 ((['git', 'diff', '--no-ext-diff', '--stat', '--find-copies-harder',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000479 '-l100000', '-C50', 'fake_ancestor_sha', 'HEAD'],),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000480 '+dat'),
481 ]
482
483 @staticmethod
484 def _gerrit_upload_calls(description, reviewers):
485 calls = [
ukai@chromium.org8ef7ab22012-11-28 04:24:52 +0000486 ((['git', 'config', 'gerrit.host'],), 'gerrit.example.com'),
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000487 ((['git', 'log', '--pretty=format:%s\n\n%b',
488 'fake_ancestor_sha..HEAD'],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000489 description)
490 ]
491 if git_cl.CHANGE_ID not in description:
492 calls += [
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000493 ((['git', 'log', '--pretty=format:%s\n\n%b',
494 'fake_ancestor_sha..HEAD'],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000495 description),
496 ((['git', 'commit', '--amend', '-m', description],),
497 ''),
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000498 ((['git', 'log', '--pretty=format:%s\n\n%b',
499 'fake_ancestor_sha..HEAD'],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000500 description)
501 ]
502 calls += [
ukai@chromium.orge8077812012-02-03 03:41:46 +0000503 ((['git', 'config', 'rietveld.cc'],), '')
504 ]
ukai@chromium.org19bbfa22012-02-03 16:18:11 +0000505 receive_pack = '--receive-pack=git receive-pack '
ukai@chromium.orge8077812012-02-03 03:41:46 +0000506 receive_pack += '--cc=joe@example.com' # from watch list
507 if reviewers:
508 receive_pack += ' '
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000509 receive_pack += ' '.join(
510 '--reviewer=' + email for email in sorted(reviewers))
ukai@chromium.org19bbfa22012-02-03 16:18:11 +0000511 receive_pack += ''
ukai@chromium.orge8077812012-02-03 03:41:46 +0000512 calls += [
513 ((['git', 'push', receive_pack, 'origin', 'HEAD:refs/for/master'],),
514 '')
515 ]
516 return calls
517
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000518 def _run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000519 self,
520 upload_args,
521 description,
522 reviewers):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000523 """Generic gerrit upload test framework."""
ukai@chromium.orge8077812012-02-03 03:41:46 +0000524 self.calls = self._gerrit_base_calls()
525 self.calls += self._gerrit_upload_calls(description, reviewers)
526 git_cl.main(['upload'] + upload_args)
527
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000528 def test_gerrit_upload_without_change_id(self):
529 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000530 [],
jamesr@chromium.org35d1a842012-07-27 00:20:43 +0000531 'desc\n\nBUG=\n',
ukai@chromium.orge8077812012-02-03 03:41:46 +0000532 [])
533
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000534 def test_gerrit_no_reviewer(self):
535 self._run_gerrit_upload_test(
536 [],
537 'desc\n\nBUG=\nChange-Id:123456789\n',
538 [])
539
ukai@chromium.orge8077812012-02-03 03:41:46 +0000540 def test_gerrit_reviewers_cmd_line(self):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000541 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000542 ['-r', 'foo@example.com'],
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000543 'desc\n\nBUG=\nChange-Id:123456789',
ukai@chromium.orge8077812012-02-03 03:41:46 +0000544 ['foo@example.com'])
545
546 def test_gerrit_reviewer_multiple(self):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000547 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000548 [],
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000549 'desc\nTBR=reviewer@example.com\nBUG=\nR=another@example.com\n'
550 'Change-Id:123456789\n',
ukai@chromium.orge8077812012-02-03 03:41:46 +0000551 ['reviewer@example.com', 'another@example.com'])
552
553
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000554 def test_config_gerrit_download_hook(self):
555 self.mock(git_cl, 'FindCodereviewSettingsFile', CodereviewSettingsFileMock)
556 def ParseCodereviewSettingsContent(content):
557 keyvals = {}
558 keyvals['CODE_REVIEW_SERVER'] = 'gerrit.chromium.org'
559 keyvals['GERRIT_HOST'] = 'gerrit.chromium.org'
560 keyvals['GERRIT_PORT'] = '29418'
561 return keyvals
562 self.mock(git_cl.gclient_utils, 'ParseCodereviewSettingsContent',
563 ParseCodereviewSettingsContent)
564 self.mock(git_cl.os, 'access', self._mocked_call)
565 self.mock(git_cl.os, 'chmod', self._mocked_call)
ukai@chromium.org91655502012-05-25 01:46:07 +0000566 src_dir = os.path.join(os.path.sep, 'usr', 'local', 'src')
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000567 def AbsPath(path):
ukai@chromium.org91655502012-05-25 01:46:07 +0000568 if not path.startswith(os.path.sep):
569 return os.path.join(src_dir, path)
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000570 return path
571 self.mock(git_cl.os.path, 'abspath', AbsPath)
ukai@chromium.org91655502012-05-25 01:46:07 +0000572 commit_msg_path = os.path.join(src_dir, '.git', 'hooks', 'commit-msg')
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000573 def Exists(path):
ukai@chromium.org91655502012-05-25 01:46:07 +0000574 if path == commit_msg_path:
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000575 return False
576 # others paths, such as /usr/share/locale/....
577 return True
578 self.mock(git_cl.os.path, 'exists', Exists)
joshua.lock@intel.com426f69b2012-08-02 23:41:49 +0000579 self.mock(git_cl, 'urlretrieve', self._mocked_call)
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000580 self.calls = [
581 ((['git', 'config', 'rietveld.server', 'gerrit.chromium.org'],), ''),
582 ((['git', 'config', '--unset-all', 'rietveld.cc'],), ''),
583 ((['git', 'config', '--unset-all', 'rietveld.tree-status-url'],), ''),
584 ((['git', 'config', '--unset-all', 'rietveld.viewvc-url'],), ''),
585 ((['git', 'config', 'gerrit.host', 'gerrit.chromium.org'],), ''),
586 ((['git', 'config', 'gerrit.port', '29418'],), ''),
587 # DownloadHooks(False)
588 ((['git', 'config', 'gerrit.host'],), 'gerrit.chromium.org'),
589 ((['git', 'config', 'rietveld.server'],), 'gerrit.chromium.org'),
590 ((['git', 'rev-parse', '--show-cdup'],), ''),
ukai@chromium.org91655502012-05-25 01:46:07 +0000591 ((commit_msg_path, os.X_OK,), False),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000592 (('https://gerrit.chromium.org/tools/hooks/commit-msg',
ukai@chromium.org91655502012-05-25 01:46:07 +0000593 commit_msg_path,), ''),
594 ((commit_msg_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR,), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000595 # GetCodereviewSettingsInteractively
596 ((['git', 'config', 'rietveld.server'],), 'gerrit.chromium.org'),
597 (('Rietveld server (host[:port]) [https://gerrit.chromium.org]:',),
598 ''),
599 ((['git', 'config', 'rietveld.cc'],), ''),
600 (('CC list:',), ''),
601 ((['git', 'config', 'rietveld.tree-status-url'],), ''),
602 (('Tree status URL:',), ''),
603 ((['git', 'config', 'rietveld.viewvc-url'],), ''),
604 (('ViewVC URL:',), ''),
605 # DownloadHooks(True)
ukai@chromium.org91655502012-05-25 01:46:07 +0000606 ((commit_msg_path, os.X_OK,), True),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000607 ]
608 git_cl.main(['config'])
609
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000610 def test_update_reviewers(self):
611 data = [
612 ('foo', [], 'foo'),
613 ('foo', ['a@c'], 'foo\n\nR=a@c'),
614 ('foo\nBUG=', ['a@c'], 'foo\nBUG=\nR=a@c'),
615 ('foo\nR=xx\nTBR=yy\nR=bar', ['a@c'], 'foo\nTBR=a@c'),
616 ('foo', ['a@c', 'b@c'], 'foo\n\nR=a@c, b@c'),
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +0000617 ('foo\nBar\n\nR=\nBUG=', ['c@c'], 'foo\nBar\n\nR=c@c\nBUG='),
618 ('foo\nBar\n\nR=\nBUG=\nR=', ['c@c'], 'foo\nBar\n\nR=c@c\nBUG='),
619 # Same as the line before, but full of whitespaces.
620 (
621 'foo\nBar\n\n R = \n BUG = \n R = ', ['c@c'],
622 'foo\nBar\n\nR=c@c\n BUG =',
623 ),
624 # Whitespaces aren't interpreted as new lines.
625 ('foo BUG=allo R=joe ', ['c@c'], 'foo BUG=allo R=joe\n\nR=c@c'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000626 ]
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +0000627 expected = [i[2] for i in data]
628 actual = []
629 for orig, reviewers, _expected in data:
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000630 obj = git_cl.ChangeDescription(orig)
631 obj.update_reviewers(reviewers)
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +0000632 actual.append(obj.description)
633 self.assertEqual(expected, actual)
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000634
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000635
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000636if __name__ == '__main__':
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000637 git_cl.logging.basicConfig(
638 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000639 unittest.main()