blob: a890dac4f3edad5a06db9e1f03ad3f946eba0a70 [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)
101 self.assertEquals(
102 expected_args,
103 args,
104 '@%d Expected: %r Actual: %r' % (
105 self._calls_done, expected_args, args))
106 self._calls_done += 1
107 return result
108
maruel@chromium.orga3353652011-11-30 14:26:57 +0000109 @classmethod
iannucci@chromium.org79540052012-10-19 23:15:26 +0000110 def _upload_calls(cls, similarity, find_copies):
111 return (cls._git_base_calls(similarity, find_copies) +
112 cls._git_upload_calls())
maruel@chromium.orga3353652011-11-30 14:26:57 +0000113
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000114 @classmethod
115 def _git_base_calls(cls, similarity, find_copies):
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000116 if similarity is None:
117 similarity = '50'
118 similarity_call = ((['git', 'config', '--int', '--get',
119 'branch.master.git-cl-similarity'],), '')
120 else:
121 similarity_call = ((['git', 'config', '--int',
122 'branch.master.git-cl-similarity', similarity],), '')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000123
124 if find_copies is None:
125 find_copies = True
126 find_copies_call = ((['git', 'config', '--int', '--get',
127 'branch.master.git-find-copies'],), '')
128 else:
129 val = str(int(find_copies))
130 find_copies_call = ((['git', 'config', '--int',
131 'branch.master.git-find-copies', val],), '')
132
133 if find_copies:
134 stat_call = ((['git', 'diff', '--no-ext-diff', '--stat',
135 '--find-copies-harder', '-l100000', '-C'+similarity,
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000136 'fake_ancestor_sha', 'HEAD'],), '+dat')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000137 else:
138 stat_call = ((['git', 'diff', '--no-ext-diff', '--stat',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000139 '-M'+similarity, 'fake_ancestor_sha', 'HEAD'],), '+dat')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000140
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000141 return [
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000142 ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'),
143 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
144 similarity_call,
iannucci@chromium.org79540052012-10-19 23:15:26 +0000145 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
146 find_copies_call,
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000147 ((['git', 'update-index', '--refresh', '-q'],), ''),
ukai@chromium.org259e4682012-10-25 07:36:33 +0000148 ((['git', 'diff-index', '--name-status', 'HEAD'],), ''),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000149 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
150 ((['git', 'config', 'branch.master.merge'],), 'master'),
151 ((['git', 'config', 'branch.master.remote'],), 'origin'),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000152 ((['git', 'merge-base', 'master', 'HEAD'],), 'fake_ancestor_sha'),
153 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000154 ((['git', 'rev-parse', '--show-cdup'],), ''),
155 ((['git', 'rev-parse', 'HEAD'],), '12345'),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000156 ((['git', 'diff', '--name-status', '-r', 'fake_ancestor_sha...', '.'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000157 'M\t.gitignore\n'),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000158 ((['git', 'config', 'branch.master.rietveldissue'],), ''),
159 ((['git', 'config', 'branch.master.rietveldpatchset'],), ''),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000160 ((['git', 'log', '--pretty=format:%s%n%n%b', 'fake_ancestor_sha...'],),
161 'foo'),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000162 ((['git', 'config', 'user.email'],), 'me@example.com'),
iannucci@chromium.org79540052012-10-19 23:15:26 +0000163 stat_call,
ukai@chromium.org8ef7ab22012-11-28 04:24:52 +0000164 ((['git', 'config', 'gerrit.host'],), ''),
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000165 ((['git', 'log', '--pretty=format:%s\n\n%b', 'fake_ancestor_sha..HEAD'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000166 'desc\n'),
maruel@chromium.orga3353652011-11-30 14:26:57 +0000167 ]
168
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000169 @classmethod
170 def _git_upload_calls(cls):
maruel@chromium.orga3353652011-11-30 14:26:57 +0000171 return [
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000172 ((['git', 'config', 'rietveld.cc'],), ''),
kalmard@homejinni.com6b0051e2012-04-03 15:45:08 +0000173 ((['git', 'config', 'branch.master.base-url'],), ''),
zimmerle@gmail.com3cdcf562013-04-12 19:39:38 +0000174 ((['git', 'config', '--local', '--get-regexp', '^svn-remote\\.'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000175 (('', None), 0)),
176 ((['git', 'rev-parse', '--show-cdup'],), ''),
177 ((['git', 'svn', 'info'],), ''),
178 ((['git', 'config', 'branch.master.rietveldissue', '1'],), ''),
179 ((['git', 'config', 'branch.master.rietveldserver',
180 'https://codereview.example.com'],), ''),
181 ((['git', 'config', 'branch.master.rietveldpatchset', '2'],), ''),
rogerta@chromium.orgcaa16552013-03-18 20:45:05 +0000182 ((['git', 'rev-parse', 'HEAD'],), 'hash'),
183 ((['git', 'symbolic-ref', 'HEAD'],), 'hash'),
184 ((['git', 'config', 'branch.hash.last-upload-hash', 'hash'],), ''),
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000185 ]
186
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000187 @staticmethod
188 def _git_sanity_checks(diff_base, working_branch):
189 fake_ancestor = 'fake_ancestor'
190 fake_cl = 'fake_cl_for_patch'
191 return [
192 # Calls to verify branch point is ancestor
193 ((['git', 'rev-parse', '--verify', diff_base],), fake_ancestor),
194 ((['git', 'merge-base', fake_ancestor, 'HEAD'],), fake_ancestor),
195 ((['git', 'rev-list', '^' + fake_ancestor, 'HEAD'],), fake_cl),
196 # Mock a config miss (error code 1)
197 ((['git', 'config', 'gitcl.remotebranch'],), (('', None), 1)),
198 # Call to GetRemoteBranch()
199 ((['git', 'config', 'branch.%s.merge' % working_branch],),
200 'refs/heads/master'),
201 ((['git', 'config', 'branch.%s.remote' % working_branch],), 'origin'),
202 ((['git', 'rev-list', '^' + fake_ancestor,
203 'refs/remotes/origin/master'],), ''),
204 ]
205
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000206 @classmethod
207 def _dcommit_calls_1(cls):
208 return [
zimmerle@gmail.com3cdcf562013-04-12 19:39:38 +0000209 ((['git', 'config', '--local', '--get-regexp', '^svn-remote\\.'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000210 ((('svn-remote.svn.url svn://svn.chromium.org/chrome\n'
211 'svn-remote.svn.fetch trunk/src:refs/remotes/origin/master'),
212 None),
213 0)),
214 ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'),
215 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000216 ((['git', 'config', '--int', '--get',
217 'branch.working.git-cl-similarity'],), ''),
218 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
iannucci@chromium.org79540052012-10-19 23:15:26 +0000219 ((['git', 'config', '--int', '--get',
220 'branch.working.git-find-copies'],), ''),
221 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000222 ((['git', 'config', 'branch.working.merge'],), 'refs/heads/master'),
223 ((['git', 'config', 'branch.working.remote'],), 'origin'),
szager@chromium.org9bb85e22012-06-13 20:28:23 +0000224 ((['git', 'rev-list', '--merges',
szager@chromium.orge84b7542012-06-15 21:26:58 +0000225 '--grep=^SVN changes up to revision [0-9]*$',
szager@chromium.org9bb85e22012-06-13 20:28:23 +0000226 'refs/remotes/origin/master^!'],), ''),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000227 ((['git', 'update-index', '--refresh', '-q'],), ''),
ukai@chromium.org259e4682012-10-25 07:36:33 +0000228 ((['git', 'diff-index', '--name-status', 'HEAD'],), ''),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000229 ((['git', 'rev-list', '^refs/heads/working',
230 'refs/remotes/origin/master'],),
231 ''),
232 ((['git', 'log', '--grep=^git-svn-id:', '-1', '--pretty=format:%H'],),
233 '3fc18b62c4966193eb435baabe2d18a3810ec82e'),
234 ((['git', 'rev-list', '^3fc18b62c4966193eb435baabe2d18a3810ec82e',
235 'refs/remotes/origin/master'],), ''),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000236 ((['git', 'merge-base', 'refs/remotes/origin/master', 'HEAD'],),
237 'fake_ancestor_sha'),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000238 ]
239
240 @classmethod
241 def _dcommit_calls_normal(cls):
242 return [
243 ((['git', 'rev-parse', '--show-cdup'],), ''),
244 ((['git', 'rev-parse', 'HEAD'],),
245 '00ff397798ea57439712ed7e04ab96e13969ef40'),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000246 ((['git', 'diff', '--name-status', '-r', 'fake_ancestor_sha...',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000247 '.'],),
248 'M\tPRESUBMIT.py'),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000249 ((['git', 'config', 'branch.working.rietveldissue'],), '12345'),
evan@chromium.org0af9b702012-02-11 00:42:16 +0000250 ((['git', 'config', 'branch.working.rietveldpatchset'],), '31137'),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000251 ((['git', 'config', 'branch.working.rietveldserver'],),
252 'codereview.example.com'),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000253 ((['git', 'config', 'user.email'],), 'author@example.com'),
254 ((['git', 'config', 'rietveld.tree-status-url'],), ''),
255 ]
256
257 @classmethod
258 def _dcommit_calls_bypassed(cls):
259 return [
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000260 ((['git', 'config', 'branch.working.rietveldissue'],), '12345'),
261 ((['git', 'config', 'branch.working.rietveldserver'],),
262 'codereview.example.com'),
263 (('GitClHooksBypassedCommit',
264 'Issue https://codereview.example.com/12345 bypassed hook when '
265 'committing'), None),
266 ]
267
268 @classmethod
269 def _dcommit_calls_3(cls):
270 return [
maruel@chromium.org49e3d802012-07-18 23:54:45 +0000271 ((['git', 'diff', '--no-ext-diff', '--stat', '--find-copies-harder',
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000272 '-l100000', '-C50', 'fake_ancestor_sha',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000273 'refs/heads/working'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000274 (' PRESUBMIT.py | 2 +-\n'
275 ' 1 files changed, 1 insertions(+), 1 deletions(-)\n')),
276 (('About to commit; enter to confirm.',), None),
277 ((['git', 'show-ref', '--quiet', '--verify',
278 'refs/heads/git-cl-commit'],),
279 (('', None), 0)),
280 ((['git', 'branch', '-D', 'git-cl-commit'],), ''),
szager@chromium.org9bb85e22012-06-13 20:28:23 +0000281 ((['git', 'show-ref', '--quiet', '--verify',
282 'refs/heads/git-cl-cherry-pick'],), ''),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000283 ((['git', 'rev-parse', '--show-cdup'],), '\n'),
284 ((['git', 'checkout', '-q', '-b', 'git-cl-commit'],), ''),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000285 ((['git', 'reset', '--soft', 'fake_ancestor_sha'],), ''),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000286 ((['git', 'commit', '-m',
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000287 'Issue: 12345\n\n'
288 'Review URL: https://codereview.example.com/12345'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000289 ''),
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000290 ((['git', 'svn', 'dcommit', '-C50', '--no-rebase', '--rmdir'],),
291 (('', None), 0)),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000292 ((['git', 'checkout', '-q', 'working'],), ''),
293 ((['git', 'branch', '-D', 'git-cl-commit'],), ''),
294 ]
295
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000296 @staticmethod
iannucci@chromium.org79540052012-10-19 23:15:26 +0000297 def _cmd_line(description, args, similarity, find_copies):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000298 """Returns the upload command line passed to upload.RealMain()."""
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000299 return [
300 'upload', '--assume_yes', '--server',
maruel@chromium.orgeb5edbc2012-01-16 17:03:28 +0000301 'https://codereview.example.com',
maruel@chromium.org71e12a92012-02-14 02:34:15 +0000302 '--message', description
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000303 ] + args + [
304 '--cc', 'joe@example.com',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000305 '--git_similarity', similarity or '50'
306 ] + (['--git_no_find_copies'] if find_copies == False else []) + [
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000307 'fake_ancestor_sha', 'HEAD'
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000308 ]
309
310 def _run_reviewer_test(
311 self,
312 upload_args,
313 expected_description,
314 returned_description,
315 final_description,
316 reviewers):
317 """Generic reviewer test framework."""
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000318 try:
319 similarity = upload_args[upload_args.index('--similarity')+1]
320 except ValueError:
321 similarity = None
iannucci@chromium.org79540052012-10-19 23:15:26 +0000322
323 if '--find-copies' in upload_args:
324 find_copies = True
325 elif '--no-find-copies' in upload_args:
326 find_copies = False
327 else:
328 find_copies = None
329
330 self.calls = self._upload_calls(similarity, find_copies)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000331 def RunEditor(desc, _):
332 self.assertEquals(
333 '# Enter a description of the change.\n'
334 '# This will displayed on the codereview site.\n'
335 '# The first line will also be used as the subject of the review.\n' +
336 expected_description,
337 desc)
338 return returned_description
339 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor)
340 def check_upload(args):
iannucci@chromium.org79540052012-10-19 23:15:26 +0000341 cmd_line = self._cmd_line(final_description, reviewers, similarity,
342 find_copies)
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000343 self.assertEquals(cmd_line, args)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000344 return 1, 2
345 self.mock(git_cl.upload, 'RealMain', check_upload)
346 git_cl.main(['upload'] + upload_args)
347
348 def test_no_reviewer(self):
349 self._run_reviewer_test(
350 [],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000351 'desc\n\nBUG=',
352 '# Blah blah comment.\ndesc\n\nBUG=',
353 'desc\n\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000354 [])
355
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000356 def test_keep_similarity(self):
357 self._run_reviewer_test(
358 ['--similarity', '70'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000359 'desc\n\nBUG=',
360 '# Blah blah comment.\ndesc\n\nBUG=',
361 'desc\n\nBUG=',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000362 [])
363
iannucci@chromium.org79540052012-10-19 23:15:26 +0000364 def test_keep_find_copies(self):
365 self._run_reviewer_test(
366 ['--no-find-copies'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000367 'desc\n\nBUG=',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000368 '# Blah blah comment.\ndesc\n\nBUG=\n',
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000369 'desc\n\nBUG=',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000370 [])
371
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000372 def test_reviewers_cmd_line(self):
373 # Reviewer is passed as-is
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000374 description = 'desc\n\nR=foo@example.com\nBUG='
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000375 self._run_reviewer_test(
376 ['-r' 'foo@example.com'],
377 description,
378 '\n%s\n' % description,
379 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000380 ['--reviewers=foo@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000381
382 def test_reviewer_tbr_overriden(self):
383 # Reviewer is overriden with TBR
384 # Also verifies the regexp work without a trailing LF
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000385 description = 'Foo Bar\n\nTBR=reviewer@example.com'
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000386 self._run_reviewer_test(
387 ['-r' 'foo@example.com'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000388 'desc\n\nR=foo@example.com\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000389 description.strip('\n'),
390 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000391 ['--reviewers=reviewer@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000392
393 def test_reviewer_multiple(self):
394 # Handles multiple R= or TBR= lines.
395 description = (
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000396 'Foo Bar\nTBR=reviewer@example.com\nBUG=\nR=another@example.com')
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000397 self._run_reviewer_test(
398 [],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000399 'desc\n\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000400 description,
401 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000402 ['--reviewers=another@example.com,reviewer@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000403
maruel@chromium.orga3353652011-11-30 14:26:57 +0000404 def test_reviewer_send_mail(self):
405 # --send-mail can be used without -r if R= is used
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000406 description = 'Foo Bar\nR=reviewer@example.com'
maruel@chromium.orga3353652011-11-30 14:26:57 +0000407 self._run_reviewer_test(
408 ['--send-mail'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000409 'desc\n\nBUG=',
maruel@chromium.orga3353652011-11-30 14:26:57 +0000410 description.strip('\n'),
411 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000412 ['--reviewers=reviewer@example.com', '--send_mail'])
maruel@chromium.orga3353652011-11-30 14:26:57 +0000413
414 def test_reviewer_send_mail_no_rev(self):
415 # Fails without a reviewer.
416 class FileMock(object):
417 buf = StringIO.StringIO()
418 def write(self, content):
419 self.buf.write(content)
420
421 mock = FileMock()
422 try:
iannucci@chromium.org79540052012-10-19 23:15:26 +0000423 self.calls = self._git_base_calls(None, None)
maruel@chromium.orga3353652011-11-30 14:26:57 +0000424 def RunEditor(desc, _):
425 return desc
426 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor)
427 self.mock(sys, 'stderr', mock)
428 git_cl.main(['upload', '--send-mail'])
429 self.fail()
430 except SystemExit:
431 self.assertEquals(
432 'Must specify reviewers to send email.\n', mock.buf.getvalue())
433
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000434 def test_dcommit(self):
435 self.calls = (
436 self._dcommit_calls_1() +
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000437 self._git_sanity_checks('fake_ancestor_sha', 'working') +
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000438 self._dcommit_calls_normal() +
439 self._dcommit_calls_3())
440 git_cl.main(['dcommit'])
441
442 def test_dcommit_bypass_hooks(self):
443 self.calls = (
444 self._dcommit_calls_1() +
445 self._dcommit_calls_bypassed() +
446 self._dcommit_calls_3())
447 git_cl.main(['dcommit', '--bypass-hooks'])
448
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000449
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000450 @classmethod
451 def _gerrit_base_calls(cls):
ukai@chromium.orge8077812012-02-03 03:41:46 +0000452 return [
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000453 ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'),
454 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
455 ((['git', 'config', '--int', '--get',
456 'branch.master.git-cl-similarity'],), ''),
iannucci@chromium.org79540052012-10-19 23:15:26 +0000457 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
458 ((['git', 'config', '--int', '--get',
459 'branch.master.git-find-copies'],), ''),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000460 ((['git', 'update-index', '--refresh', '-q'],), ''),
ukai@chromium.org259e4682012-10-25 07:36:33 +0000461 ((['git', 'diff-index', '--name-status', 'HEAD'],), ''),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000462 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
463 ((['git', 'config', 'branch.master.merge'],), 'master'),
464 ((['git', 'config', 'branch.master.remote'],), 'origin'),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000465 ((['git', 'merge-base', 'master', 'HEAD'],), 'fake_ancestor_sha'),
466 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [
ukai@chromium.orge8077812012-02-03 03:41:46 +0000467 ((['git', 'rev-parse', '--show-cdup'],), ''),
468 ((['git', 'rev-parse', 'HEAD'],), '12345'),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000469 ((['git', 'diff', '--name-status', '-r', 'fake_ancestor_sha...', '.'],),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000470 'M\t.gitignore\n'),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000471 ((['git', 'config', 'branch.master.rietveldissue'],), ''),
472 ((['git', 'config', 'branch.master.rietveldpatchset'],), ''),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000473 ((['git', 'log', '--pretty=format:%s%n%n%b', 'fake_ancestor_sha...'],),
474 'foo'),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000475 ((['git', 'config', 'user.email'],), 'me@example.com'),
cmp@chromium.org5cf70222012-06-12 18:20:13 +0000476 ((['git', 'diff', '--no-ext-diff', '--stat', '--find-copies-harder',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000477 '-l100000', '-C50', 'fake_ancestor_sha', 'HEAD'],),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000478 '+dat'),
479 ]
480
481 @staticmethod
482 def _gerrit_upload_calls(description, reviewers):
483 calls = [
ukai@chromium.org8ef7ab22012-11-28 04:24:52 +0000484 ((['git', 'config', 'gerrit.host'],), 'gerrit.example.com'),
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000485 ((['git', 'log', '--pretty=format:%s\n\n%b',
486 'fake_ancestor_sha..HEAD'],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000487 description)
488 ]
489 if git_cl.CHANGE_ID not in description:
490 calls += [
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000491 ((['git', 'log', '--pretty=format:%s\n\n%b',
492 'fake_ancestor_sha..HEAD'],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000493 description),
494 ((['git', 'commit', '--amend', '-m', description],),
495 ''),
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000496 ((['git', 'log', '--pretty=format:%s\n\n%b',
497 'fake_ancestor_sha..HEAD'],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000498 description)
499 ]
500 calls += [
ukai@chromium.orge8077812012-02-03 03:41:46 +0000501 ((['git', 'config', 'rietveld.cc'],), '')
502 ]
ukai@chromium.org19bbfa22012-02-03 16:18:11 +0000503 receive_pack = '--receive-pack=git receive-pack '
ukai@chromium.orge8077812012-02-03 03:41:46 +0000504 receive_pack += '--cc=joe@example.com' # from watch list
505 if reviewers:
506 receive_pack += ' '
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000507 receive_pack += ' '.join(
508 '--reviewer=' + email for email in sorted(reviewers))
ukai@chromium.org19bbfa22012-02-03 16:18:11 +0000509 receive_pack += ''
ukai@chromium.orge8077812012-02-03 03:41:46 +0000510 calls += [
511 ((['git', 'push', receive_pack, 'origin', 'HEAD:refs/for/master'],),
512 '')
513 ]
514 return calls
515
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000516 def _run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000517 self,
518 upload_args,
519 description,
520 reviewers):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000521 """Generic gerrit upload test framework."""
ukai@chromium.orge8077812012-02-03 03:41:46 +0000522 self.calls = self._gerrit_base_calls()
523 self.calls += self._gerrit_upload_calls(description, reviewers)
524 git_cl.main(['upload'] + upload_args)
525
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000526 def test_gerrit_upload_without_change_id(self):
527 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000528 [],
jamesr@chromium.org35d1a842012-07-27 00:20:43 +0000529 'desc\n\nBUG=\n',
ukai@chromium.orge8077812012-02-03 03:41:46 +0000530 [])
531
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000532 def test_gerrit_no_reviewer(self):
533 self._run_gerrit_upload_test(
534 [],
535 'desc\n\nBUG=\nChange-Id:123456789\n',
536 [])
537
ukai@chromium.orge8077812012-02-03 03:41:46 +0000538 def test_gerrit_reviewers_cmd_line(self):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000539 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000540 ['-r', 'foo@example.com'],
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000541 'desc\n\nBUG=\nChange-Id:123456789',
ukai@chromium.orge8077812012-02-03 03:41:46 +0000542 ['foo@example.com'])
543
544 def test_gerrit_reviewer_multiple(self):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000545 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000546 [],
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000547 'desc\nTBR=reviewer@example.com\nBUG=\nR=another@example.com\n'
548 'Change-Id:123456789\n',
ukai@chromium.orge8077812012-02-03 03:41:46 +0000549 ['reviewer@example.com', 'another@example.com'])
550
551
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000552 def test_config_gerrit_download_hook(self):
553 self.mock(git_cl, 'FindCodereviewSettingsFile', CodereviewSettingsFileMock)
554 def ParseCodereviewSettingsContent(content):
555 keyvals = {}
556 keyvals['CODE_REVIEW_SERVER'] = 'gerrit.chromium.org'
557 keyvals['GERRIT_HOST'] = 'gerrit.chromium.org'
558 keyvals['GERRIT_PORT'] = '29418'
559 return keyvals
560 self.mock(git_cl.gclient_utils, 'ParseCodereviewSettingsContent',
561 ParseCodereviewSettingsContent)
562 self.mock(git_cl.os, 'access', self._mocked_call)
563 self.mock(git_cl.os, 'chmod', self._mocked_call)
ukai@chromium.org91655502012-05-25 01:46:07 +0000564 src_dir = os.path.join(os.path.sep, 'usr', 'local', 'src')
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000565 def AbsPath(path):
ukai@chromium.org91655502012-05-25 01:46:07 +0000566 if not path.startswith(os.path.sep):
567 return os.path.join(src_dir, path)
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000568 return path
569 self.mock(git_cl.os.path, 'abspath', AbsPath)
ukai@chromium.org91655502012-05-25 01:46:07 +0000570 commit_msg_path = os.path.join(src_dir, '.git', 'hooks', 'commit-msg')
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000571 def Exists(path):
ukai@chromium.org91655502012-05-25 01:46:07 +0000572 if path == commit_msg_path:
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000573 return False
574 # others paths, such as /usr/share/locale/....
575 return True
576 self.mock(git_cl.os.path, 'exists', Exists)
joshua.lock@intel.com426f69b2012-08-02 23:41:49 +0000577 self.mock(git_cl, 'urlretrieve', self._mocked_call)
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000578 self.calls = [
579 ((['git', 'config', 'rietveld.server', 'gerrit.chromium.org'],), ''),
580 ((['git', 'config', '--unset-all', 'rietveld.cc'],), ''),
581 ((['git', 'config', '--unset-all', 'rietveld.tree-status-url'],), ''),
582 ((['git', 'config', '--unset-all', 'rietveld.viewvc-url'],), ''),
583 ((['git', 'config', 'gerrit.host', 'gerrit.chromium.org'],), ''),
584 ((['git', 'config', 'gerrit.port', '29418'],), ''),
585 # DownloadHooks(False)
586 ((['git', 'config', 'gerrit.host'],), 'gerrit.chromium.org'),
587 ((['git', 'config', 'rietveld.server'],), 'gerrit.chromium.org'),
588 ((['git', 'rev-parse', '--show-cdup'],), ''),
ukai@chromium.org91655502012-05-25 01:46:07 +0000589 ((commit_msg_path, os.X_OK,), False),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000590 (('https://gerrit.chromium.org/tools/hooks/commit-msg',
ukai@chromium.org91655502012-05-25 01:46:07 +0000591 commit_msg_path,), ''),
592 ((commit_msg_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR,), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000593 # GetCodereviewSettingsInteractively
594 ((['git', 'config', 'rietveld.server'],), 'gerrit.chromium.org'),
595 (('Rietveld server (host[:port]) [https://gerrit.chromium.org]:',),
596 ''),
597 ((['git', 'config', 'rietveld.cc'],), ''),
598 (('CC list:',), ''),
599 ((['git', 'config', 'rietveld.tree-status-url'],), ''),
600 (('Tree status URL:',), ''),
601 ((['git', 'config', 'rietveld.viewvc-url'],), ''),
602 (('ViewVC URL:',), ''),
603 # DownloadHooks(True)
ukai@chromium.org91655502012-05-25 01:46:07 +0000604 ((commit_msg_path, os.X_OK,), True),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000605 ]
606 git_cl.main(['config'])
607
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000608 def test_update_reviewers(self):
609 data = [
610 ('foo', [], 'foo'),
611 ('foo', ['a@c'], 'foo\n\nR=a@c'),
612 ('foo\nBUG=', ['a@c'], 'foo\nBUG=\nR=a@c'),
613 ('foo\nR=xx\nTBR=yy\nR=bar', ['a@c'], 'foo\nTBR=a@c'),
614 ('foo', ['a@c', 'b@c'], 'foo\n\nR=a@c, b@c'),
615 ]
616 for orig, reviewers, expected in data:
617 obj = git_cl.ChangeDescription(orig)
618 obj.update_reviewers(reviewers)
619 self.assertEqual(expected, obj.description)
620
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000621
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000622if __name__ == '__main__':
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000623 git_cl.logging.basicConfig(
624 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000625 unittest.main()