blob: cc5325821d5607721ca6a3e8cd61e8481bba3ccb [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
martiniss@chromium.org090df6a2014-06-26 17:38:38 +000013import re
maruel@chromium.orgddd59412011-11-30 14:20:38 +000014
15sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
16
17from testing_support.auto_stub import TestCase
18
19import git_cl
iannucci@chromium.org9e849272014-04-04 00:31:55 +000020import git_common
maruel@chromium.orgddd59412011-11-30 14:20:38 +000021import subprocess2
martiniss@chromium.org090df6a2014-06-26 17:38:38 +000022import presubmit_support
maruel@chromium.orgddd59412011-11-30 14:20:38 +000023
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000024class PresubmitMock(object):
25 def __init__(self, *args, **kwargs):
26 self.reviewers = []
27 @staticmethod
28 def should_continue():
29 return True
30
31
32class RietveldMock(object):
33 def __init__(self, *args, **kwargs):
34 pass
maruel@chromium.org78936cb2013-04-11 00:17:52 +000035
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000036 @staticmethod
37 def get_description(issue):
38 return 'Issue: %d' % issue
39
maruel@chromium.org78936cb2013-04-11 00:17:52 +000040 @staticmethod
41 def get_issue_properties(_issue, _messages):
42 return {
43 'reviewers': ['joe@chromium.org', 'john@chromium.org'],
44 'messages': [
45 {
46 'approval': True,
47 'sender': 'john@chromium.org',
48 },
49 ],
50 }
51
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000052
53class WatchlistsMock(object):
54 def __init__(self, _):
55 pass
56 @staticmethod
57 def GetWatchersForPaths(_):
58 return ['joe@example.com']
59
60
ukai@chromium.org78c4b982012-02-14 02:20:26 +000061class CodereviewSettingsFileMock(object):
62 def __init__(self):
63 pass
64 # pylint: disable=R0201
65 def read(self):
66 return ("CODE_REVIEW_SERVER: gerrit.chromium.org\n" +
67 "GERRIT_HOST: gerrit.chromium.org\n" +
68 "GERRIT_PORT: 29418\n")
69
70
maruel@chromium.orgddd59412011-11-30 14:20:38 +000071class TestGitCl(TestCase):
72 def setUp(self):
73 super(TestGitCl, self).setUp()
74 self.calls = []
75 self._calls_done = 0
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000076 self.mock(subprocess2, 'call', self._mocked_call)
77 self.mock(subprocess2, 'check_call', self._mocked_call)
78 self.mock(subprocess2, 'check_output', self._mocked_call)
79 self.mock(subprocess2, 'communicate', self._mocked_call)
80 self.mock(subprocess2, 'Popen', self._mocked_call)
iannucci@chromium.org9e849272014-04-04 00:31:55 +000081 self.mock(git_common, 'get_or_create_merge_base',
82 lambda *a: (
83 self._mocked_call(['get_or_create_merge_base']+list(a))))
maruel@chromium.orgddd59412011-11-30 14:20:38 +000084 self.mock(git_cl, 'FindCodereviewSettingsFile', lambda: '')
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000085 self.mock(git_cl, 'ask_for_data', self._mocked_call)
86 self.mock(git_cl.breakpad, 'post', self._mocked_call)
87 self.mock(git_cl.breakpad, 'SendStack', self._mocked_call)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000088 self.mock(git_cl.presubmit_support, 'DoPresubmitChecks', PresubmitMock)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000089 self.mock(git_cl.rietveld, 'Rietveld', RietveldMock)
maruel@chromium.org4bac4b52012-11-27 20:33:52 +000090 self.mock(git_cl.rietveld, 'CachingRietveld', RietveldMock)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000091 self.mock(git_cl.upload, 'RealMain', self.fail)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000092 self.mock(git_cl.watchlists, 'Watchlists', WatchlistsMock)
93 # It's important to reset settings to not have inter-tests interference.
94 git_cl.settings = None
95
96 def tearDown(self):
97 if not self.has_failed():
98 self.assertEquals([], self.calls)
99 super(TestGitCl, self).tearDown()
100
iannucci@chromium.org9e849272014-04-04 00:31:55 +0000101 def _mocked_call(self, *args, **_kwargs):
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000102 self.assertTrue(
103 self.calls,
104 '@%d Expected: <Missing> Actual: %r' % (self._calls_done, args))
105 expected_args, result = self.calls.pop(0)
maruel@chromium.orge52678e2013-04-26 18:34:44 +0000106 # Also logs otherwise it could get caught in a try/finally and be hard to
107 # diagnose.
108 if expected_args != args:
109 msg = '@%d Expected: %r Actual: %r' % (
110 self._calls_done, expected_args, args)
111 git_cl.logging.error(msg)
112 self.fail(msg)
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000113 self._calls_done += 1
114 return result
115
maruel@chromium.orga3353652011-11-30 14:26:57 +0000116 @classmethod
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000117 def _upload_calls(cls, similarity, find_copies, private):
iannucci@chromium.org79540052012-10-19 23:15:26 +0000118 return (cls._git_base_calls(similarity, find_copies) +
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000119 cls._git_upload_calls(private))
maruel@chromium.orga3353652011-11-30 14:26:57 +0000120
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000121 @classmethod
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000122 def _upload_no_rev_calls(cls, similarity, find_copies):
123 return (cls._git_base_calls(similarity, find_copies) +
124 cls._git_upload_no_rev_calls())
125
126 @classmethod
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000127 def _git_base_calls(cls, similarity, find_copies):
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000128 if similarity is None:
129 similarity = '50'
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000130 similarity_call = ((['git', 'config', '--int', '--get',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000131 'branch.master.git-cl-similarity'],), '')
132 else:
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000133 similarity_call = ((['git', 'config', '--int',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000134 'branch.master.git-cl-similarity', similarity],), '')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000135
136 if find_copies is None:
137 find_copies = True
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000138 find_copies_call = ((['git', 'config', '--int', '--get',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000139 'branch.master.git-find-copies'],), '')
140 else:
141 val = str(int(find_copies))
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000142 find_copies_call = ((['git', 'config', '--int',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000143 'branch.master.git-find-copies', val],), '')
144
145 if find_copies:
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000146 stat_call = ((['git', 'diff', '--no-ext-diff', '--stat',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000147 '--find-copies-harder', '-l100000', '-C'+similarity,
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000148 'fake_ancestor_sha', 'HEAD'],), '+dat')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000149 else:
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000150 stat_call = ((['git', 'diff', '--no-ext-diff', '--stat',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000151 '-M'+similarity, 'fake_ancestor_sha', 'HEAD'],), '+dat')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000152
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000153 return [
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000154 ((['git', 'config', 'rietveld.autoupdate'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000155 ((['git', 'config', 'rietveld.server'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000156 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000157 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000158 similarity_call,
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000159 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
iannucci@chromium.org79540052012-10-19 23:15:26 +0000160 find_copies_call,
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000161 ((['git', 'update-index', '--refresh', '-q'],), ''),
162 ((['git', 'diff-index', '--name-status', 'HEAD'],), ''),
163 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
164 ((['git', 'config', 'branch.master.merge'],), 'master'),
165 ((['git', 'config', 'branch.master.remote'],), 'origin'),
iannucci@chromium.org9e849272014-04-04 00:31:55 +0000166 ((['get_or_create_merge_base', 'master', 'master'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000167 'fake_ancestor_sha'),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000168 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000169 ((['git', 'rev-parse', '--show-cdup'],), ''),
170 ((['git', 'rev-parse', 'HEAD'],), '12345'),
171 ((['git', 'diff', '--name-status', '--no-renames', '-r',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000172 'fake_ancestor_sha...', '.'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000173 'M\t.gitignore\n'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000174 ((['git', 'config', 'branch.master.rietveldissue'],), ''),
175 ((['git', 'config', 'branch.master.rietveldpatchset'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000176 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000177 ((['git', 'log', '--pretty=format:%s%n%n%b',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000178 'fake_ancestor_sha...'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000179 'foo'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000180 ((['git', 'config', 'user.email'],), 'me@example.com'),
iannucci@chromium.org79540052012-10-19 23:15:26 +0000181 stat_call,
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000182 ((['git', 'config', 'gerrit.host'],), ''),
183 ((['git', 'log', '--pretty=format:%s\n\n%b',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000184 'fake_ancestor_sha..HEAD'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000185 'desc\n'),
rmistry@google.com90752582014-01-14 21:04:50 +0000186 ((['git', 'config', 'rietveld.bug-prefix'],), ''),
maruel@chromium.orga3353652011-11-30 14:26:57 +0000187 ]
188
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000189 @classmethod
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000190 def _git_upload_no_rev_calls(cls):
191 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000192 ((['git', 'config', 'core.editor'],), ''),
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000193 ]
194
195 @classmethod
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000196 def _git_upload_calls(cls, private):
197 if private:
tyoshino@chromium.org99918ab2013-09-30 06:17:28 +0000198 cc_call = []
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000199 private_call = []
200 else:
tyoshino@chromium.org99918ab2013-09-30 06:17:28 +0000201 cc_call = [((['git', 'config', 'rietveld.cc'],), '')]
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000202 private_call = [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000203 ((['git', 'config', 'rietveld.private'],), '')]
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000204
maruel@chromium.orga3353652011-11-30 14:26:57 +0000205 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000206 ((['git', 'config', 'core.editor'],), ''),
tyoshino@chromium.org99918ab2013-09-30 06:17:28 +0000207 ] + cc_call + private_call + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000208 ((['git', 'config', 'branch.master.base-url'],), ''),
209 ((['git',
bratell@opera.com05fb9112014-07-07 09:30:23 +0000210 'config', '--local', '--get-regexp', '^svn-remote\\.'],),
211 (('', None), 0)),
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000212 ((['git', 'rev-parse', '--show-cdup'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000213 ((['git', 'svn', 'info'],), ''),
sheyang@chromium.org152cf832014-06-11 21:37:49 +0000214 ((['git', 'config', 'rietveld.project'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000215 ((['git',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000216 'config', 'branch.master.rietveldissue', '1'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000217 ((['git', 'config', 'branch.master.rietveldserver',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000218 'https://codereview.example.com'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000219 ((['git',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000220 'config', 'branch.master.rietveldpatchset', '2'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000221 ((['git', 'rev-parse', 'HEAD'],), 'hash'),
222 ((['git', 'symbolic-ref', 'HEAD'],), 'hash'),
223 ((['git',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000224 'config', 'branch.hash.last-upload-hash', 'hash'],), ''),
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000225 ]
226
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000227 @staticmethod
228 def _git_sanity_checks(diff_base, working_branch):
229 fake_ancestor = 'fake_ancestor'
230 fake_cl = 'fake_cl_for_patch'
231 return [
232 # Calls to verify branch point is ancestor
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000233 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000234 'rev-parse', '--verify', diff_base],), fake_ancestor),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000235 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000236 'merge-base', fake_ancestor, 'HEAD'],), fake_ancestor),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000237 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000238 'rev-list', '^' + fake_ancestor, 'HEAD'],), fake_cl),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000239 # Mock a config miss (error code 1)
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000240 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000241 'config', 'gitcl.remotebranch'],), (('', None), 1)),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000242 # Call to GetRemoteBranch()
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000243 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000244 'config', 'branch.%s.merge' % working_branch],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000245 'refs/heads/master'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000246 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000247 'config', 'branch.%s.remote' % working_branch],), 'origin'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000248 ((['git', 'rev-list', '^' + fake_ancestor,
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000249 'refs/remotes/origin/master'],), ''),
250 ]
251
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000252 @classmethod
253 def _dcommit_calls_1(cls):
254 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000255 ((['git',
bratell@opera.com05fb9112014-07-07 09:30:23 +0000256 'config', '--local', '--get-regexp', '^svn-remote\\.'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000257 ((('svn-remote.svn.url svn://svn.chromium.org/chrome\n'
258 'svn-remote.svn.fetch trunk/src:refs/remotes/origin/master'),
259 None),
260 0)),
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000261 ((['git', 'config', 'rietveld.autoupdate'],),
262 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000263 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000264 'config', 'rietveld.server'],), 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000265 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
266 ((['git', 'config', '--int', '--get',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000267 'branch.working.git-cl-similarity'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000268 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
269 ((['git', 'config', '--int', '--get',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000270 'branch.working.git-find-copies'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000271 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
272 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000273 'config', 'branch.working.merge'],), 'refs/heads/master'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000274 ((['git', 'config', 'branch.working.remote'],), 'origin'),
iannucci@chromium.org5724c962014-04-11 09:32:56 +0000275 ((['git', 'config', 'branch.working.merge'],),
276 'refs/heads/master'),
277 ((['git', 'config', 'branch.working.remote'],), 'origin'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000278 ((['git', 'rev-list', '--merges',
szager@chromium.orge84b7542012-06-15 21:26:58 +0000279 '--grep=^SVN changes up to revision [0-9]*$',
szager@chromium.org9bb85e22012-06-13 20:28:23 +0000280 'refs/remotes/origin/master^!'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000281 ((['git', 'update-index', '--refresh', '-q'],), ''),
282 ((['git', 'diff-index', '--name-status', 'HEAD'],), ''),
283 ((['git', 'rev-list', '^refs/heads/working',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000284 'refs/remotes/origin/master'],),
285 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000286 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000287 'log', '--grep=^git-svn-id:', '-1', '--pretty=format:%H'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000288 '3fc18b62c4966193eb435baabe2d18a3810ec82e'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000289 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000290 'rev-list', '^3fc18b62c4966193eb435baabe2d18a3810ec82e',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000291 'refs/remotes/origin/master'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000292 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000293 'merge-base', 'refs/remotes/origin/master', 'HEAD'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000294 'fake_ancestor_sha'),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000295 ]
296
297 @classmethod
298 def _dcommit_calls_normal(cls):
299 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000300 ((['git', 'rev-parse', '--show-cdup'],), ''),
301 ((['git', 'rev-parse', 'HEAD'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000302 '00ff397798ea57439712ed7e04ab96e13969ef40'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000303 ((['git',
mcgrathr@chromium.org9249f642013-06-03 21:36:18 +0000304 'diff', '--name-status', '--no-renames', '-r', 'fake_ancestor_sha...',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000305 '.'],),
306 'M\tPRESUBMIT.py'),
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',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000310 'config', 'branch.working.rietveldpatchset'],), '31137'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000311 ((['git', 'config', 'branch.working.rietveldserver'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000312 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000313 ((['git', 'config', 'user.email'],), 'author@example.com'),
314 ((['git', 'config', 'rietveld.tree-status-url'],), ''),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000315 ]
316
317 @classmethod
318 def _dcommit_calls_bypassed(cls):
319 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000320 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000321 'config', 'branch.working.rietveldissue'],), '12345'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000322 ((['git', 'config', 'branch.working.rietveldserver'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000323 'codereview.example.com'),
jochen@chromium.org3ec0d542014-01-14 20:00:03 +0000324 ((['git', 'config', 'rietveld.tree-status-url'],), ''),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000325 (('GitClHooksBypassedCommit',
326 'Issue https://codereview.example.com/12345 bypassed hook when '
jochen@chromium.org3ec0d542014-01-14 20:00:03 +0000327 'committing (tree status was "unset")'), None),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000328 ]
329
330 @classmethod
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000331 def _dcommit_calls_3(cls):
332 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000333 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000334 'diff', '--no-ext-diff', '--stat', '--find-copies-harder',
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000335 '-l100000', '-C50', 'fake_ancestor_sha',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000336 'refs/heads/working'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000337 (' PRESUBMIT.py | 2 +-\n'
338 ' 1 files changed, 1 insertions(+), 1 deletions(-)\n')),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000339 ((['git', 'show-ref', '--quiet', '--verify',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000340 'refs/heads/git-cl-commit'],),
341 (('', None), 0)),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000342 ((['git', 'branch', '-D', 'git-cl-commit'],), ''),
343 ((['git', 'show-ref', '--quiet', '--verify',
szager@chromium.org9bb85e22012-06-13 20:28:23 +0000344 'refs/heads/git-cl-cherry-pick'],), ''),
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000345 ((['git', 'rev-parse', '--show-cdup'],), '\n'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000346 ((['git', 'checkout', '-q', '-b', 'git-cl-commit'],), ''),
347 ((['git', 'reset', '--soft', 'fake_ancestor_sha'],), ''),
348 ((['git', 'commit', '-m',
maruel@chromium.orge52678e2013-04-26 18:34:44 +0000349 'Issue: 12345\n\nR=john@chromium.org\n\n'
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000350 'Review URL: https://codereview.example.com/12345'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000351 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000352 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000353 'svn', 'dcommit', '-C50', '--no-rebase', '--rmdir'],),
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000354 (('', None), 0)),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000355 ((['git', 'checkout', '-q', 'working'],), ''),
356 ((['git', 'branch', '-D', 'git-cl-commit'],), ''),
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000357 ]
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000358
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000359 @staticmethod
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000360 def _cmd_line(description, args, similarity, find_copies, private):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000361 """Returns the upload command line passed to upload.RealMain()."""
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000362 return [
363 'upload', '--assume_yes', '--server',
maruel@chromium.orgeb5edbc2012-01-16 17:03:28 +0000364 'https://codereview.example.com',
maruel@chromium.org71e12a92012-02-14 02:34:15 +0000365 '--message', description
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000366 ] + args + [
367 '--cc', 'joe@example.com',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000368 ] + (['--private'] if private else []) + [
iannucci@chromium.org79540052012-10-19 23:15:26 +0000369 '--git_similarity', similarity or '50'
370 ] + (['--git_no_find_copies'] if find_copies == False else []) + [
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000371 'fake_ancestor_sha', 'HEAD'
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000372 ]
373
374 def _run_reviewer_test(
375 self,
376 upload_args,
377 expected_description,
378 returned_description,
379 final_description,
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000380 reviewers,
381 private=False):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000382 """Generic reviewer test framework."""
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000383 try:
384 similarity = upload_args[upload_args.index('--similarity')+1]
385 except ValueError:
386 similarity = None
iannucci@chromium.org79540052012-10-19 23:15:26 +0000387
388 if '--find-copies' in upload_args:
389 find_copies = True
390 elif '--no-find-copies' in upload_args:
391 find_copies = False
392 else:
393 find_copies = None
394
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000395 private = '--private' in upload_args
396
397 self.calls = self._upload_calls(similarity, find_copies, private)
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000398
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000399 def RunEditor(desc, _, **kwargs):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000400 self.assertEquals(
401 '# Enter a description of the change.\n'
janx@chromium.org104b2db2013-04-18 12:58:40 +0000402 '# This will be displayed on the codereview site.\n'
alancutter@chromium.org63a4d7f2013-05-31 02:22:45 +0000403 '# The first line will also be used as the subject of the review.\n'
alancutter@chromium.orgbd1073e2013-06-01 00:34:38 +0000404 '#--------------------This line is 72 characters long'
405 '--------------------\n' +
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000406 expected_description,
407 desc)
408 return returned_description
409 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor)
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000410
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000411 def check_upload(args):
iannucci@chromium.org79540052012-10-19 23:15:26 +0000412 cmd_line = self._cmd_line(final_description, reviewers, similarity,
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000413 find_copies, private)
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000414 self.assertEquals(cmd_line, args)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000415 return 1, 2
416 self.mock(git_cl.upload, 'RealMain', check_upload)
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000417
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000418 git_cl.main(['upload'] + upload_args)
419
420 def test_no_reviewer(self):
421 self._run_reviewer_test(
422 [],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000423 'desc\n\nBUG=',
424 '# Blah blah comment.\ndesc\n\nBUG=',
425 'desc\n\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000426 [])
427
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000428 def test_keep_similarity(self):
429 self._run_reviewer_test(
430 ['--similarity', '70'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000431 'desc\n\nBUG=',
432 '# Blah blah comment.\ndesc\n\nBUG=',
433 'desc\n\nBUG=',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000434 [])
435
iannucci@chromium.org79540052012-10-19 23:15:26 +0000436 def test_keep_find_copies(self):
437 self._run_reviewer_test(
438 ['--no-find-copies'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000439 'desc\n\nBUG=',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000440 '# Blah blah comment.\ndesc\n\nBUG=\n',
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000441 'desc\n\nBUG=',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000442 [])
443
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000444 def test_private(self):
445 self._run_reviewer_test(
446 ['--private'],
447 'desc\n\nBUG=',
448 '# Blah blah comment.\ndesc\n\nBUG=\n',
449 'desc\n\nBUG=',
450 [])
451
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000452 def test_reviewers_cmd_line(self):
453 # Reviewer is passed as-is
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000454 description = 'desc\n\nR=foo@example.com\nBUG='
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000455 self._run_reviewer_test(
456 ['-r' 'foo@example.com'],
457 description,
458 '\n%s\n' % description,
459 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000460 ['--reviewers=foo@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000461
462 def test_reviewer_tbr_overriden(self):
463 # Reviewer is overriden with TBR
464 # Also verifies the regexp work without a trailing LF
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000465 description = 'Foo Bar\n\nTBR=reviewer@example.com'
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000466 self._run_reviewer_test(
467 ['-r' 'foo@example.com'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000468 'desc\n\nR=foo@example.com\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000469 description.strip('\n'),
470 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000471 ['--reviewers=reviewer@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000472
473 def test_reviewer_multiple(self):
474 # Handles multiple R= or TBR= lines.
475 description = (
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000476 'Foo Bar\nTBR=reviewer@example.com\nBUG=\nR=another@example.com')
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000477 self._run_reviewer_test(
478 [],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000479 'desc\n\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000480 description,
481 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000482 ['--reviewers=another@example.com,reviewer@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000483
maruel@chromium.orga3353652011-11-30 14:26:57 +0000484 def test_reviewer_send_mail(self):
485 # --send-mail can be used without -r if R= is used
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000486 description = 'Foo Bar\nR=reviewer@example.com'
maruel@chromium.orga3353652011-11-30 14:26:57 +0000487 self._run_reviewer_test(
488 ['--send-mail'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000489 'desc\n\nBUG=',
maruel@chromium.orga3353652011-11-30 14:26:57 +0000490 description.strip('\n'),
491 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000492 ['--reviewers=reviewer@example.com', '--send_mail'])
maruel@chromium.orga3353652011-11-30 14:26:57 +0000493
494 def test_reviewer_send_mail_no_rev(self):
495 # Fails without a reviewer.
maruel@chromium.org2e23ce32013-05-07 12:42:28 +0000496 stdout = StringIO.StringIO()
497 stderr = StringIO.StringIO()
maruel@chromium.orga3353652011-11-30 14:26:57 +0000498 try:
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000499 self.calls = self._upload_no_rev_calls(None, None)
500 def RunEditor(desc, _, **kwargs):
maruel@chromium.orga3353652011-11-30 14:26:57 +0000501 return desc
502 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor)
maruel@chromium.org2e23ce32013-05-07 12:42:28 +0000503 self.mock(sys, 'stdout', stdout)
504 self.mock(sys, 'stderr', stderr)
maruel@chromium.orga3353652011-11-30 14:26:57 +0000505 git_cl.main(['upload', '--send-mail'])
506 self.fail()
507 except SystemExit:
maruel@chromium.org2e23ce32013-05-07 12:42:28 +0000508 self.assertEqual(
509 'Using 50% similarity for rename/copy detection. Override with '
510 '--similarity.\n',
511 stdout.getvalue())
512 self.assertEqual(
513 'Must specify reviewers to send email.\n', stderr.getvalue())
maruel@chromium.orga3353652011-11-30 14:26:57 +0000514
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000515 def test_dcommit(self):
516 self.calls = (
517 self._dcommit_calls_1() +
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000518 self._git_sanity_checks('fake_ancestor_sha', 'working') +
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000519 self._dcommit_calls_normal() +
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000520 self._dcommit_calls_3())
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000521 git_cl.main(['dcommit'])
522
523 def test_dcommit_bypass_hooks(self):
524 self.calls = (
525 self._dcommit_calls_1() +
526 self._dcommit_calls_bypassed() +
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000527 self._dcommit_calls_3())
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000528 git_cl.main(['dcommit', '--bypass-hooks'])
529
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000530
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000531 @classmethod
532 def _gerrit_base_calls(cls):
ukai@chromium.orge8077812012-02-03 03:41:46 +0000533 return [
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000534 ((['git', 'config', 'rietveld.autoupdate'],),
535 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000536 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000537 'config', 'rietveld.server'],), 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000538 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
539 ((['git', 'config', '--int', '--get',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000540 'branch.master.git-cl-similarity'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000541 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
542 ((['git', 'config', '--int', '--get',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000543 'branch.master.git-find-copies'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000544 ((['git', 'update-index', '--refresh', '-q'],), ''),
545 ((['git', 'diff-index', '--name-status', 'HEAD'],), ''),
546 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
547 ((['git', 'config', 'branch.master.merge'],), 'master'),
548 ((['git', 'config', 'branch.master.remote'],), 'origin'),
iannucci@chromium.org9e849272014-04-04 00:31:55 +0000549 ((['get_or_create_merge_base', 'master', 'master'],),
550 'fake_ancestor_sha'),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000551 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000552 ((['git', 'rev-parse', '--show-cdup'],), ''),
553 ((['git', 'rev-parse', 'HEAD'],), '12345'),
554 ((['git',
mcgrathr@chromium.org9249f642013-06-03 21:36:18 +0000555 'diff', '--name-status', '--no-renames', '-r',
556 'fake_ancestor_sha...', '.'],),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000557 'M\t.gitignore\n'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000558 ((['git', 'config', 'branch.master.rietveldissue'],), ''),
559 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000560 'config', 'branch.master.rietveldpatchset'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000561 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000562 'log', '--pretty=format:%s%n%n%b', 'fake_ancestor_sha...'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000563 'foo'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000564 ((['git', 'config', 'user.email'],), 'me@example.com'),
565 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000566 'diff', '--no-ext-diff', '--stat', '--find-copies-harder',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000567 '-l100000', '-C50', 'fake_ancestor_sha', 'HEAD'],),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000568 '+dat'),
569 ]
570
571 @staticmethod
572 def _gerrit_upload_calls(description, reviewers):
573 calls = [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000574 ((['git', 'config', 'gerrit.host'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000575 'gerrit.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000576 ((['git', 'log', '--pretty=format:%s\n\n%b',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000577 'fake_ancestor_sha..HEAD'],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000578 description)
579 ]
580 if git_cl.CHANGE_ID not in description:
581 calls += [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000582 ((['git', 'log', '--pretty=format:%s\n\n%b',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000583 'fake_ancestor_sha..HEAD'],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000584 description),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000585 ((['git', 'commit', '--amend', '-m', description],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000586 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000587 ((['git', 'log', '--pretty=format:%s\n\n%b',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000588 'fake_ancestor_sha..HEAD'],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000589 description)
590 ]
591 calls += [
bauerb@chromium.org279c2182014-05-16 09:22:09 +0000592 ((['git', 'rev-list', 'origin/master..'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000593 ((['git', 'config', 'rietveld.cc'],), '')
ukai@chromium.orge8077812012-02-03 03:41:46 +0000594 ]
ukai@chromium.org19bbfa22012-02-03 16:18:11 +0000595 receive_pack = '--receive-pack=git receive-pack '
ukai@chromium.orge8077812012-02-03 03:41:46 +0000596 receive_pack += '--cc=joe@example.com' # from watch list
597 if reviewers:
598 receive_pack += ' '
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000599 receive_pack += ' '.join(
600 '--reviewer=' + email for email in sorted(reviewers))
ukai@chromium.org19bbfa22012-02-03 16:18:11 +0000601 receive_pack += ''
ukai@chromium.orge8077812012-02-03 03:41:46 +0000602 calls += [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000603 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000604 'push', receive_pack, 'origin', 'HEAD:refs/for/master'],),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000605 '')
606 ]
607 return calls
608
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000609 def _run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000610 self,
611 upload_args,
612 description,
613 reviewers):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000614 """Generic gerrit upload test framework."""
ukai@chromium.orge8077812012-02-03 03:41:46 +0000615 self.calls = self._gerrit_base_calls()
616 self.calls += self._gerrit_upload_calls(description, reviewers)
617 git_cl.main(['upload'] + upload_args)
618
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000619 def test_gerrit_upload_without_change_id(self):
620 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000621 [],
jamesr@chromium.org35d1a842012-07-27 00:20:43 +0000622 'desc\n\nBUG=\n',
ukai@chromium.orge8077812012-02-03 03:41:46 +0000623 [])
624
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000625 def test_gerrit_no_reviewer(self):
626 self._run_gerrit_upload_test(
627 [],
628 'desc\n\nBUG=\nChange-Id:123456789\n',
629 [])
630
ukai@chromium.orge8077812012-02-03 03:41:46 +0000631 def test_gerrit_reviewers_cmd_line(self):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000632 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000633 ['-r', 'foo@example.com'],
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000634 'desc\n\nBUG=\nChange-Id:123456789',
ukai@chromium.orge8077812012-02-03 03:41:46 +0000635 ['foo@example.com'])
636
637 def test_gerrit_reviewer_multiple(self):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000638 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000639 [],
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000640 'desc\nTBR=reviewer@example.com\nBUG=\nR=another@example.com\n'
641 'Change-Id:123456789\n',
ukai@chromium.orge8077812012-02-03 03:41:46 +0000642 ['reviewer@example.com', 'another@example.com'])
643
644
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000645 def test_config_gerrit_download_hook(self):
646 self.mock(git_cl, 'FindCodereviewSettingsFile', CodereviewSettingsFileMock)
647 def ParseCodereviewSettingsContent(content):
648 keyvals = {}
649 keyvals['CODE_REVIEW_SERVER'] = 'gerrit.chromium.org'
650 keyvals['GERRIT_HOST'] = 'gerrit.chromium.org'
651 keyvals['GERRIT_PORT'] = '29418'
652 return keyvals
653 self.mock(git_cl.gclient_utils, 'ParseCodereviewSettingsContent',
654 ParseCodereviewSettingsContent)
655 self.mock(git_cl.os, 'access', self._mocked_call)
656 self.mock(git_cl.os, 'chmod', self._mocked_call)
ukai@chromium.org91655502012-05-25 01:46:07 +0000657 src_dir = os.path.join(os.path.sep, 'usr', 'local', 'src')
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000658 def AbsPath(path):
ukai@chromium.org91655502012-05-25 01:46:07 +0000659 if not path.startswith(os.path.sep):
660 return os.path.join(src_dir, path)
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000661 return path
662 self.mock(git_cl.os.path, 'abspath', AbsPath)
ukai@chromium.org91655502012-05-25 01:46:07 +0000663 commit_msg_path = os.path.join(src_dir, '.git', 'hooks', 'commit-msg')
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000664 def Exists(path):
ukai@chromium.org91655502012-05-25 01:46:07 +0000665 if path == commit_msg_path:
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000666 return False
667 # others paths, such as /usr/share/locale/....
668 return True
669 self.mock(git_cl.os.path, 'exists', Exists)
joshua.lock@intel.com426f69b2012-08-02 23:41:49 +0000670 self.mock(git_cl, 'urlretrieve', self._mocked_call)
ukai@chromium.org712d6102013-11-27 00:52:58 +0000671 self.mock(git_cl, 'hasSheBang', self._mocked_call)
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000672 self.calls = [
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000673 ((['git', 'config', 'rietveld.autoupdate'],),
674 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000675 ((['git', 'config', 'rietveld.server',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000676 'gerrit.chromium.org'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000677 ((['git', 'config', '--unset-all', 'rietveld.cc'],), ''),
678 ((['git', 'config', '--unset-all',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000679 'rietveld.private'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000680 ((['git', 'config', '--unset-all',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000681 'rietveld.tree-status-url'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000682 ((['git', 'config', '--unset-all',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000683 'rietveld.viewvc-url'],), ''),
rmistry@google.com90752582014-01-14 21:04:50 +0000684 ((['git', 'config', '--unset-all',
685 'rietveld.bug-prefix'],), ''),
thestig@chromium.org44202a22014-03-11 19:22:18 +0000686 ((['git', 'config', '--unset-all',
687 'rietveld.cpplint-regex'],), ''),
688 ((['git', 'config', '--unset-all',
689 'rietveld.cpplint-ignore-regex'],), ''),
sheyang@chromium.org152cf832014-06-11 21:37:49 +0000690 ((['git', 'config', '--unset-all',
691 'rietveld.project'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000692 ((['git', 'config', 'gerrit.host',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000693 'gerrit.chromium.org'],), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000694 # DownloadHooks(False)
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000695 ((['git', 'config', 'gerrit.host'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000696 'gerrit.chromium.org'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000697 ((['git', 'rev-parse', '--show-cdup'],), ''),
ukai@chromium.org91655502012-05-25 01:46:07 +0000698 ((commit_msg_path, os.X_OK,), False),
ukai@chromium.org712d6102013-11-27 00:52:58 +0000699 (('https://gerrit-review.googlesource.com/tools/hooks/commit-msg',
ukai@chromium.org91655502012-05-25 01:46:07 +0000700 commit_msg_path,), ''),
ukai@chromium.org712d6102013-11-27 00:52:58 +0000701 ((commit_msg_path,), True),
ukai@chromium.org91655502012-05-25 01:46:07 +0000702 ((commit_msg_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR,), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000703 # GetCodereviewSettingsInteractively
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000704 ((['git', 'config', 'rietveld.server'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000705 'gerrit.chromium.org'),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000706 (('Rietveld server (host[:port]) [https://gerrit.chromium.org]:',),
707 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000708 ((['git', 'config', 'rietveld.cc'],), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000709 (('CC list:',), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000710 ((['git', 'config', 'rietveld.private'],), ''),
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000711 (('Private flag (rietveld only):',), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000712 ((['git', 'config', 'rietveld.tree-status-url'],), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000713 (('Tree status URL:',), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000714 ((['git', 'config', 'rietveld.viewvc-url'],), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000715 (('ViewVC URL:',), ''),
716 # DownloadHooks(True)
rmistry@google.com90752582014-01-14 21:04:50 +0000717 ((['git', 'config', 'rietveld.bug-prefix'],), ''),
718 (('Bug Prefix:',), ''),
ukai@chromium.org91655502012-05-25 01:46:07 +0000719 ((commit_msg_path, os.X_OK,), True),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000720 ]
721 git_cl.main(['config'])
722
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000723 def test_update_reviewers(self):
724 data = [
725 ('foo', [], 'foo'),
agable@chromium.org42c20792013-09-12 17:34:49 +0000726 ('foo\nR=xx', [], 'foo\nR=xx'),
727 ('foo\nTBR=xx', [], 'foo\nTBR=xx'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000728 ('foo', ['a@c'], 'foo\n\nR=a@c'),
agable@chromium.org42c20792013-09-12 17:34:49 +0000729 ('foo\nR=xx', ['a@c'], 'foo\n\nR=a@c, xx'),
730 ('foo\nTBR=xx', ['a@c'], 'foo\n\nR=a@c\nTBR=xx'),
731 ('foo\nTBR=xx\nR=yy', ['a@c'], 'foo\n\nR=a@c, yy\nTBR=xx'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000732 ('foo\nBUG=', ['a@c'], 'foo\nBUG=\nR=a@c'),
agable@chromium.org42c20792013-09-12 17:34:49 +0000733 ('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 +0000734 ('foo', ['a@c', 'b@c'], 'foo\n\nR=a@c, b@c'),
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +0000735 ('foo\nBar\n\nR=\nBUG=', ['c@c'], 'foo\nBar\n\nR=c@c\nBUG='),
736 ('foo\nBar\n\nR=\nBUG=\nR=', ['c@c'], 'foo\nBar\n\nR=c@c\nBUG='),
737 # Same as the line before, but full of whitespaces.
738 (
739 'foo\nBar\n\n R = \n BUG = \n R = ', ['c@c'],
740 'foo\nBar\n\nR=c@c\n BUG =',
741 ),
742 # Whitespaces aren't interpreted as new lines.
743 ('foo BUG=allo R=joe ', ['c@c'], 'foo BUG=allo R=joe\n\nR=c@c'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000744 ]
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +0000745 expected = [i[2] for i in data]
746 actual = []
747 for orig, reviewers, _expected in data:
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000748 obj = git_cl.ChangeDescription(orig)
749 obj.update_reviewers(reviewers)
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +0000750 actual.append(obj.description)
751 self.assertEqual(expected, actual)
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000752
martiniss@chromium.org090df6a2014-06-26 17:38:38 +0000753 def test_trybots_from_PRESUBMIT(self):
754 TEST_MASTER = 'testMaster'
755 TEST_BUILDER = 'testBuilder'
756 MASTERS = {TEST_MASTER:{TEST_BUILDER:['a']}}
757 self.mock(presubmit_support, 'DoGetTryMasters',
758 lambda *args: MASTERS)
759
760 change_mock = ChangeMock()
761 changelist_mock = ChangelistMock(change_mock)
762 self.mock(git_cl, 'is_dirty_git_tree', lambda x: False)
763 self.mock(git_cl, 'print_stats', lambda *arg: True)
764 self.mock(git_cl, 'Changelist', lambda *args: changelist_mock)
765 self.mock(git_cl, 'CreateDescriptionFromLog', lambda arg: 'Commit message')
766 self.mock(git_cl.ChangeDescription, 'prompt', lambda self: None)
767
768 self.calls = [
769 ((['git', 'config', 'rietveld.autoupdate',],),
770 ''),
771 ((['git', 'config', 'gerrit.host',],),
772 ''),
773 ((['git', 'rev-parse', '--show-cdup',],),
774 ''),
775 ((['git', 'config', 'rietveld.private',],),
776 ''),
bratell@opera.com05fb9112014-07-07 09:30:23 +0000777 ((['git', 'config', '--local', '--get-regexp', '^svn-remote\\.'],),
martiniss@chromium.org090df6a2014-06-26 17:38:38 +0000778 ''),
779 ((['git', 'config', 'rietveld.project',],),
780 ''),
781 ((['git', 'rev-parse', 'HEAD',],),
782 ''),
783 ]
784
785 stored_description = []
786 def check_upload(args):
787 i = 0
788 for arg in args:
789 if arg == '--message':
790 break
791 i += 1
792
793 self.assertTrue(i < len(args))
794 stored_description.append(args[i+1])
795 return 1, 2
796 self.mock(git_cl.upload, 'RealMain', check_upload)
797
798 git_cl.main(['upload', '--bypass-hooks', '--auto-bots'])
799 found = re.search("CQ_TRYBOTS=(.*?)$", stored_description[0])
800 self.assertTrue(found)
801 self.assertEqual(found.group(1), '%s:%s' % (TEST_MASTER, TEST_BUILDER))
802
803
804class ChangelistMock(object):
martiniss@chromium.org905312c2014-06-26 18:06:24 +0000805 # Disable "Method could be a function"
806 # pylint: disable=R0201
807
martiniss@chromium.org090df6a2014-06-26 17:38:38 +0000808 def __init__(self, change_mock):
809 self.change_mock = change_mock
810
martiniss@chromium.org090df6a2014-06-26 17:38:38 +0000811 def GetChange(self, *args):
812 return self.change_mock
813
814 def GetIssue(self):
815 return None
816
817 def GetBranch(self):
818 return []
819
820 def GetCommonAncestorWithUpstream(self):
821 return []
822
823 def GetCCList(self):
824 return []
825
826 def GetGitBaseUrlFromConfig(self):
827 return ''
828
829 def GetRemoteUrl(self):
830 return ''
831
832 def GetRietveldServer(self):
833 return None
834
835 def SetWatchers(self, *args):
836 pass
837
838 def SetIssue(self, issue):
839 pass
840
841 def SetPatchset(self, issue):
842 pass
843
844
845class ChangeMock(object):
martiniss@chromium.org905312c2014-06-26 18:06:24 +0000846 # Disable "Method could be a function"
847 # pylint: disable=R0201
848
martiniss@chromium.org090df6a2014-06-26 17:38:38 +0000849 def __init__(self):
850 self.stored_description = None
851
martiniss@chromium.org090df6a2014-06-26 17:38:38 +0000852 def SetDescriptionText(self, desc):
853 self.stored_description = desc
854
855 def FullDescriptionText(self):
856 return 'HIHI TEST DESCRIPTION'
857
858 def RepositoryRoot(self):
859 return []
860
861 def AffectedFiles(self):
862 return []
863
864 def LocalPaths(self):
865 return None
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000866
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000867if __name__ == '__main__':
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000868 git_cl.logging.basicConfig(
869 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000870 unittest.main()