blob: c1eebebf7dc0523dfb075785030033ee72f1f1f3 [file] [log] [blame]
maruel@chromium.orgddd59412011-11-30 14:20:38 +00001#!/usr/bin/env python
maruel@chromium.orgeb5edbc2012-01-16 17:03:28 +00002# Copyright (c) 2012 The Chromium Authors. All rights reserved.
maruel@chromium.orgddd59412011-11-30 14:20:38 +00003# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Unit tests for git_cl.py."""
7
8import os
maruel@chromium.orga3353652011-11-30 14:26:57 +00009import StringIO
ukai@chromium.org78c4b982012-02-14 02:20:26 +000010import stat
maruel@chromium.orgddd59412011-11-30 14:20:38 +000011import sys
12import unittest
13
14sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
15
16from testing_support.auto_stub import TestCase
17
18import git_cl
iannucci@chromium.org9e849272014-04-04 00:31:55 +000019import git_common
maruel@chromium.orgddd59412011-11-30 14:20:38 +000020import subprocess2
maruel@chromium.orgddd59412011-11-30 14:20:38 +000021
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000022class PresubmitMock(object):
23 def __init__(self, *args, **kwargs):
24 self.reviewers = []
25 @staticmethod
26 def should_continue():
27 return True
28
29
30class RietveldMock(object):
31 def __init__(self, *args, **kwargs):
32 pass
maruel@chromium.org78936cb2013-04-11 00:17:52 +000033
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000034 @staticmethod
35 def get_description(issue):
36 return 'Issue: %d' % issue
37
maruel@chromium.org78936cb2013-04-11 00:17:52 +000038 @staticmethod
39 def get_issue_properties(_issue, _messages):
40 return {
41 'reviewers': ['joe@chromium.org', 'john@chromium.org'],
42 'messages': [
43 {
44 'approval': True,
45 'sender': 'john@chromium.org',
46 },
47 ],
48 }
49
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000050
51class WatchlistsMock(object):
52 def __init__(self, _):
53 pass
54 @staticmethod
55 def GetWatchersForPaths(_):
56 return ['joe@example.com']
57
58
ukai@chromium.org78c4b982012-02-14 02:20:26 +000059class CodereviewSettingsFileMock(object):
60 def __init__(self):
61 pass
62 # pylint: disable=R0201
63 def read(self):
64 return ("CODE_REVIEW_SERVER: gerrit.chromium.org\n" +
65 "GERRIT_HOST: gerrit.chromium.org\n" +
66 "GERRIT_PORT: 29418\n")
67
68
vadimsh@chromium.orgeed4df32015-04-10 21:30:20 +000069class AuthenticatorMock(object):
70 def __init__(self, *_args):
71 pass
72 def has_cached_credentials(self):
73 return True
74
75
maruel@chromium.orgddd59412011-11-30 14:20:38 +000076class TestGitCl(TestCase):
77 def setUp(self):
78 super(TestGitCl, self).setUp()
79 self.calls = []
80 self._calls_done = 0
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000081 self.mock(subprocess2, 'call', self._mocked_call)
82 self.mock(subprocess2, 'check_call', self._mocked_call)
83 self.mock(subprocess2, 'check_output', self._mocked_call)
84 self.mock(subprocess2, 'communicate', self._mocked_call)
sbc@chromium.org71437c02015-04-09 19:29:40 +000085 self.mock(git_common, 'is_dirty_git_tree', lambda x: False)
iannucci@chromium.org9e849272014-04-04 00:31:55 +000086 self.mock(git_common, 'get_or_create_merge_base',
87 lambda *a: (
88 self._mocked_call(['get_or_create_merge_base']+list(a))))
maruel@chromium.orgddd59412011-11-30 14:20:38 +000089 self.mock(git_cl, 'FindCodereviewSettingsFile', lambda: '')
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000090 self.mock(git_cl, 'ask_for_data', self._mocked_call)
91 self.mock(git_cl.breakpad, 'post', self._mocked_call)
92 self.mock(git_cl.breakpad, 'SendStack', self._mocked_call)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000093 self.mock(git_cl.presubmit_support, 'DoPresubmitChecks', PresubmitMock)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000094 self.mock(git_cl.rietveld, 'Rietveld', RietveldMock)
maruel@chromium.org4bac4b52012-11-27 20:33:52 +000095 self.mock(git_cl.rietveld, 'CachingRietveld', RietveldMock)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000096 self.mock(git_cl.upload, 'RealMain', self.fail)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000097 self.mock(git_cl.watchlists, 'Watchlists', WatchlistsMock)
vadimsh@chromium.orgeed4df32015-04-10 21:30:20 +000098 self.mock(git_cl.auth, 'get_authenticator_for_host', AuthenticatorMock)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000099 # It's important to reset settings to not have inter-tests interference.
100 git_cl.settings = None
101
102 def tearDown(self):
wychen@chromium.org445c8962015-04-28 23:30:05 +0000103 try:
104 if not self.has_failed():
105 self.assertEquals([], self.calls)
106 finally:
107 super(TestGitCl, self).tearDown()
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000108
iannucci@chromium.org9e849272014-04-04 00:31:55 +0000109 def _mocked_call(self, *args, **_kwargs):
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000110 self.assertTrue(
111 self.calls,
112 '@%d Expected: <Missing> Actual: %r' % (self._calls_done, args))
113 expected_args, result = self.calls.pop(0)
maruel@chromium.orge52678e2013-04-26 18:34:44 +0000114 # Also logs otherwise it could get caught in a try/finally and be hard to
115 # diagnose.
116 if expected_args != args:
117 msg = '@%d Expected: %r Actual: %r' % (
118 self._calls_done, expected_args, args)
119 git_cl.logging.error(msg)
120 self.fail(msg)
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000121 self._calls_done += 1
122 return result
123
maruel@chromium.orga3353652011-11-30 14:26:57 +0000124 @classmethod
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000125 def _upload_calls(cls, similarity, find_copies, private):
iannucci@chromium.org79540052012-10-19 23:15:26 +0000126 return (cls._git_base_calls(similarity, find_copies) +
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000127 cls._git_upload_calls(private))
maruel@chromium.orga3353652011-11-30 14:26:57 +0000128
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000129 @classmethod
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000130 def _upload_no_rev_calls(cls, similarity, find_copies):
131 return (cls._git_base_calls(similarity, find_copies) +
132 cls._git_upload_no_rev_calls())
133
134 @classmethod
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000135 def _git_base_calls(cls, similarity, find_copies):
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000136 if similarity is None:
137 similarity = '50'
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000138 similarity_call = ((['git', 'config', '--int', '--get',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000139 'branch.master.git-cl-similarity'],), '')
140 else:
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000141 similarity_call = ((['git', 'config', '--int',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000142 'branch.master.git-cl-similarity', similarity],), '')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000143
144 if find_copies is None:
145 find_copies = True
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000146 find_copies_call = ((['git', 'config', '--int', '--get',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000147 'branch.master.git-find-copies'],), '')
148 else:
149 val = str(int(find_copies))
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000150 find_copies_call = ((['git', 'config', '--int',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000151 'branch.master.git-find-copies', val],), '')
152
153 if find_copies:
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000154 stat_call = ((['git', 'diff', '--no-ext-diff', '--stat',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000155 '--find-copies-harder', '-l100000', '-C'+similarity,
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000156 'fake_ancestor_sha', 'HEAD'],), '+dat')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000157 else:
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000158 stat_call = ((['git', 'diff', '--no-ext-diff', '--stat',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000159 '-M'+similarity, 'fake_ancestor_sha', 'HEAD'],), '+dat')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000160
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000161 return [
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000162 ((['git', 'config', 'rietveld.autoupdate'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000163 ((['git', 'config', 'rietveld.server'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000164 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000165 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000166 similarity_call,
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000167 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
iannucci@chromium.org79540052012-10-19 23:15:26 +0000168 find_copies_call,
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000169 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
170 ((['git', 'config', 'branch.master.merge'],), 'master'),
171 ((['git', 'config', 'branch.master.remote'],), 'origin'),
iannucci@chromium.org9e849272014-04-04 00:31:55 +0000172 ((['get_or_create_merge_base', 'master', 'master'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000173 'fake_ancestor_sha'),
vadimsh@chromium.orgeed4df32015-04-10 21:30:20 +0000174 ((['git', 'config', 'gerrit.host'],), ''),
vadimsh@chromium.org19f3fe62015-04-20 17:03:10 +0000175 ((['git', 'config', 'branch.master.rietveldissue'],), ''),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000176 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000177 ((['git', 'rev-parse', '--show-cdup'],), ''),
178 ((['git', 'rev-parse', 'HEAD'],), '12345'),
179 ((['git', 'diff', '--name-status', '--no-renames', '-r',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000180 'fake_ancestor_sha...', '.'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000181 'M\t.gitignore\n'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000182 ((['git', 'config', 'branch.master.rietveldpatchset'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000183 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000184 ((['git', 'log', '--pretty=format:%s%n%n%b',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000185 'fake_ancestor_sha...'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000186 'foo'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000187 ((['git', 'config', 'user.email'],), 'me@example.com'),
iannucci@chromium.org79540052012-10-19 23:15:26 +0000188 stat_call,
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000189 ((['git', 'log', '--pretty=format:%s\n\n%b',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000190 'fake_ancestor_sha..HEAD'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000191 'desc\n'),
rmistry@google.com90752582014-01-14 21:04:50 +0000192 ((['git', 'config', 'rietveld.bug-prefix'],), ''),
maruel@chromium.orga3353652011-11-30 14:26:57 +0000193 ]
194
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000195 @classmethod
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000196 def _git_upload_no_rev_calls(cls):
197 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000198 ((['git', 'config', 'core.editor'],), ''),
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000199 ]
200
201 @classmethod
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000202 def _git_upload_calls(cls, private):
203 if private:
tyoshino@chromium.org99918ab2013-09-30 06:17:28 +0000204 cc_call = []
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000205 private_call = []
206 else:
tyoshino@chromium.org99918ab2013-09-30 06:17:28 +0000207 cc_call = [((['git', 'config', 'rietveld.cc'],), '')]
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000208 private_call = [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000209 ((['git', 'config', 'rietveld.private'],), '')]
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000210
maruel@chromium.orga3353652011-11-30 14:26:57 +0000211 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000212 ((['git', 'config', 'core.editor'],), ''),
tyoshino@chromium.org99918ab2013-09-30 06:17:28 +0000213 ] + cc_call + private_call + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000214 ((['git', 'config', 'branch.master.base-url'],), ''),
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +0000215 ((['git', 'config', 'rietveld.pending-ref-prefix'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000216 ((['git',
bratell@opera.com05fb9112014-07-07 09:30:23 +0000217 'config', '--local', '--get-regexp', '^svn-remote\\.'],),
218 (('', None), 0)),
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000219 ((['git', 'rev-parse', '--show-cdup'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000220 ((['git', 'svn', 'info'],), ''),
sheyang@chromium.org152cf832014-06-11 21:37:49 +0000221 ((['git', 'config', 'rietveld.project'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000222 ((['git',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000223 'config', 'branch.master.rietveldissue', '1'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000224 ((['git', 'config', 'branch.master.rietveldserver',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000225 'https://codereview.example.com'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000226 ((['git',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000227 'config', 'branch.master.rietveldpatchset', '2'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000228 ((['git', 'rev-parse', 'HEAD'],), 'hash'),
229 ((['git', 'symbolic-ref', 'HEAD'],), 'hash'),
230 ((['git',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000231 'config', 'branch.hash.last-upload-hash', 'hash'],), ''),
rmistry@google.com5626a922015-02-26 14:03:30 +0000232 ((['git', 'config', 'rietveld.run-post-upload-hook'],), ''),
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000233 ]
234
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000235 @staticmethod
236 def _git_sanity_checks(diff_base, working_branch):
237 fake_ancestor = 'fake_ancestor'
238 fake_cl = 'fake_cl_for_patch'
239 return [
240 # Calls to verify branch point is ancestor
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000241 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000242 'rev-parse', '--verify', diff_base],), fake_ancestor),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000243 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000244 'merge-base', fake_ancestor, 'HEAD'],), fake_ancestor),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000245 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000246 'rev-list', '^' + fake_ancestor, 'HEAD'],), fake_cl),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000247 # Mock a config miss (error code 1)
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000248 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000249 'config', 'gitcl.remotebranch'],), (('', None), 1)),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000250 # Call to GetRemoteBranch()
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000251 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000252 'config', 'branch.%s.merge' % working_branch],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000253 'refs/heads/master'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000254 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000255 'config', 'branch.%s.remote' % working_branch],), 'origin'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000256 ((['git', 'rev-list', '^' + fake_ancestor,
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000257 'refs/remotes/origin/master'],), ''),
258 ]
259
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000260 @classmethod
261 def _dcommit_calls_1(cls):
262 return [
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +0000263 ((['git', 'config', 'rietveld.autoupdate'],),
264 ''),
265 ((['git', 'config', 'rietveld.pending-ref-prefix'],),
266 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000267 ((['git',
bratell@opera.com05fb9112014-07-07 09:30:23 +0000268 'config', '--local', '--get-regexp', '^svn-remote\\.'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000269 ((('svn-remote.svn.url svn://svn.chromium.org/chrome\n'
270 'svn-remote.svn.fetch trunk/src:refs/remotes/origin/master'),
271 None),
272 0)),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000273 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000274 'config', 'rietveld.server'],), 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000275 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
276 ((['git', 'config', '--int', '--get',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000277 'branch.working.git-cl-similarity'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000278 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
279 ((['git', 'config', '--int', '--get',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000280 'branch.working.git-find-copies'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000281 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
282 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000283 'config', 'branch.working.merge'],), 'refs/heads/master'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000284 ((['git', 'config', 'branch.working.remote'],), 'origin'),
iannucci@chromium.org5724c962014-04-11 09:32:56 +0000285 ((['git', 'config', 'branch.working.merge'],),
286 'refs/heads/master'),
287 ((['git', 'config', 'branch.working.remote'],), 'origin'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000288 ((['git', 'rev-list', '--merges',
szager@chromium.orge84b7542012-06-15 21:26:58 +0000289 '--grep=^SVN changes up to revision [0-9]*$',
szager@chromium.org9bb85e22012-06-13 20:28:23 +0000290 'refs/remotes/origin/master^!'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000291 ((['git', 'rev-list', '^refs/heads/working',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000292 'refs/remotes/origin/master'],),
293 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000294 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000295 'log', '--grep=^git-svn-id:', '-1', '--pretty=format:%H'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000296 '3fc18b62c4966193eb435baabe2d18a3810ec82e'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000297 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000298 'rev-list', '^3fc18b62c4966193eb435baabe2d18a3810ec82e',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000299 'refs/remotes/origin/master'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000300 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000301 'merge-base', 'refs/remotes/origin/master', 'HEAD'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000302 'fake_ancestor_sha'),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000303 ]
304
305 @classmethod
306 def _dcommit_calls_normal(cls):
307 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000308 ((['git', 'rev-parse', '--show-cdup'],), ''),
309 ((['git', 'rev-parse', 'HEAD'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000310 '00ff397798ea57439712ed7e04ab96e13969ef40'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000311 ((['git',
mcgrathr@chromium.org9249f642013-06-03 21:36:18 +0000312 'diff', '--name-status', '--no-renames', '-r', 'fake_ancestor_sha...',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000313 '.'],),
314 'M\tPRESUBMIT.py'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000315 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000316 'config', 'branch.working.rietveldissue'],), '12345'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000317 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000318 'config', 'branch.working.rietveldpatchset'],), '31137'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000319 ((['git', 'config', 'branch.working.rietveldserver'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000320 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000321 ((['git', 'config', 'user.email'],), 'author@example.com'),
322 ((['git', 'config', 'rietveld.tree-status-url'],), ''),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000323 ]
324
325 @classmethod
326 def _dcommit_calls_bypassed(cls):
327 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000328 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000329 'config', 'branch.working.rietveldissue'],), '12345'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000330 ((['git', 'config', 'branch.working.rietveldserver'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000331 'codereview.example.com'),
jochen@chromium.org3ec0d542014-01-14 20:00:03 +0000332 ((['git', 'config', 'rietveld.tree-status-url'],), ''),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000333 (('GitClHooksBypassedCommit',
334 'Issue https://codereview.example.com/12345 bypassed hook when '
jochen@chromium.org3ec0d542014-01-14 20:00:03 +0000335 'committing (tree status was "unset")'), None),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000336 ]
337
338 @classmethod
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000339 def _dcommit_calls_3(cls):
340 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000341 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000342 'diff', '--no-ext-diff', '--stat', '--find-copies-harder',
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000343 '-l100000', '-C50', 'fake_ancestor_sha',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000344 'refs/heads/working'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000345 (' PRESUBMIT.py | 2 +-\n'
346 ' 1 files changed, 1 insertions(+), 1 deletions(-)\n')),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000347 ((['git', 'show-ref', '--quiet', '--verify',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000348 'refs/heads/git-cl-commit'],),
349 (('', None), 0)),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000350 ((['git', 'branch', '-D', 'git-cl-commit'],), ''),
351 ((['git', 'show-ref', '--quiet', '--verify',
szager@chromium.org9bb85e22012-06-13 20:28:23 +0000352 'refs/heads/git-cl-cherry-pick'],), ''),
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000353 ((['git', 'rev-parse', '--show-cdup'],), '\n'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000354 ((['git', 'checkout', '-q', '-b', 'git-cl-commit'],), ''),
355 ((['git', 'reset', '--soft', 'fake_ancestor_sha'],), ''),
356 ((['git', 'commit', '-m',
maruel@chromium.orge52678e2013-04-26 18:34:44 +0000357 'Issue: 12345\n\nR=john@chromium.org\n\n'
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000358 'Review URL: https://codereview.example.com/12345'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000359 ''),
kjellander@chromium.org6abc6522014-12-02 07:34:49 +0000360 ((['git', 'config', 'rietveld.force-https-commit-url'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000361 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000362 'svn', 'dcommit', '-C50', '--no-rebase', '--rmdir'],),
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000363 (('', None), 0)),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000364 ((['git', 'checkout', '-q', 'working'],), ''),
365 ((['git', 'branch', '-D', 'git-cl-commit'],), ''),
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000366 ]
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000367
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000368 @staticmethod
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000369 def _cmd_line(description, args, similarity, find_copies, private):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000370 """Returns the upload command line passed to upload.RealMain()."""
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000371 return [
372 'upload', '--assume_yes', '--server',
maruel@chromium.orgeb5edbc2012-01-16 17:03:28 +0000373 'https://codereview.example.com',
maruel@chromium.org71e12a92012-02-14 02:34:15 +0000374 '--message', description
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000375 ] + args + [
376 '--cc', 'joe@example.com',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000377 ] + (['--private'] if private else []) + [
iannucci@chromium.org79540052012-10-19 23:15:26 +0000378 '--git_similarity', similarity or '50'
379 ] + (['--git_no_find_copies'] if find_copies == False else []) + [
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000380 'fake_ancestor_sha', 'HEAD'
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000381 ]
382
383 def _run_reviewer_test(
384 self,
385 upload_args,
386 expected_description,
387 returned_description,
388 final_description,
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000389 reviewers,
390 private=False):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000391 """Generic reviewer test framework."""
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000392 try:
393 similarity = upload_args[upload_args.index('--similarity')+1]
394 except ValueError:
395 similarity = None
iannucci@chromium.org79540052012-10-19 23:15:26 +0000396
397 if '--find-copies' in upload_args:
398 find_copies = True
399 elif '--no-find-copies' in upload_args:
400 find_copies = False
401 else:
402 find_copies = None
403
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000404 private = '--private' in upload_args
405
406 self.calls = self._upload_calls(similarity, find_copies, private)
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000407
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000408 def RunEditor(desc, _, **kwargs):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000409 self.assertEquals(
410 '# Enter a description of the change.\n'
janx@chromium.org104b2db2013-04-18 12:58:40 +0000411 '# This will be displayed on the codereview site.\n'
alancutter@chromium.org63a4d7f2013-05-31 02:22:45 +0000412 '# The first line will also be used as the subject of the review.\n'
alancutter@chromium.orgbd1073e2013-06-01 00:34:38 +0000413 '#--------------------This line is 72 characters long'
414 '--------------------\n' +
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000415 expected_description,
416 desc)
417 return returned_description
418 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor)
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000419
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000420 def check_upload(args):
iannucci@chromium.org79540052012-10-19 23:15:26 +0000421 cmd_line = self._cmd_line(final_description, reviewers, similarity,
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000422 find_copies, private)
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000423 self.assertEquals(cmd_line, args)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000424 return 1, 2
425 self.mock(git_cl.upload, 'RealMain', check_upload)
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000426
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000427 git_cl.main(['upload'] + upload_args)
428
429 def test_no_reviewer(self):
430 self._run_reviewer_test(
431 [],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000432 'desc\n\nBUG=',
433 '# Blah blah comment.\ndesc\n\nBUG=',
434 'desc\n\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000435 [])
436
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000437 def test_keep_similarity(self):
438 self._run_reviewer_test(
439 ['--similarity', '70'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000440 'desc\n\nBUG=',
441 '# Blah blah comment.\ndesc\n\nBUG=',
442 'desc\n\nBUG=',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000443 [])
444
iannucci@chromium.org79540052012-10-19 23:15:26 +0000445 def test_keep_find_copies(self):
446 self._run_reviewer_test(
447 ['--no-find-copies'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000448 'desc\n\nBUG=',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000449 '# Blah blah comment.\ndesc\n\nBUG=\n',
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000450 'desc\n\nBUG=',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000451 [])
452
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000453 def test_private(self):
454 self._run_reviewer_test(
455 ['--private'],
456 'desc\n\nBUG=',
457 '# Blah blah comment.\ndesc\n\nBUG=\n',
458 'desc\n\nBUG=',
459 [])
460
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000461 def test_reviewers_cmd_line(self):
462 # Reviewer is passed as-is
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000463 description = 'desc\n\nR=foo@example.com\nBUG='
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000464 self._run_reviewer_test(
465 ['-r' 'foo@example.com'],
466 description,
467 '\n%s\n' % description,
468 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000469 ['--reviewers=foo@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000470
471 def test_reviewer_tbr_overriden(self):
472 # Reviewer is overriden with TBR
473 # Also verifies the regexp work without a trailing LF
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000474 description = 'Foo Bar\n\nTBR=reviewer@example.com'
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000475 self._run_reviewer_test(
476 ['-r' 'foo@example.com'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000477 'desc\n\nR=foo@example.com\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000478 description.strip('\n'),
479 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000480 ['--reviewers=reviewer@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000481
482 def test_reviewer_multiple(self):
483 # Handles multiple R= or TBR= lines.
484 description = (
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000485 'Foo Bar\nTBR=reviewer@example.com\nBUG=\nR=another@example.com')
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000486 self._run_reviewer_test(
487 [],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000488 'desc\n\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000489 description,
490 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000491 ['--reviewers=another@example.com,reviewer@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000492
maruel@chromium.orga3353652011-11-30 14:26:57 +0000493 def test_reviewer_send_mail(self):
494 # --send-mail can be used without -r if R= is used
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000495 description = 'Foo Bar\nR=reviewer@example.com'
maruel@chromium.orga3353652011-11-30 14:26:57 +0000496 self._run_reviewer_test(
497 ['--send-mail'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000498 'desc\n\nBUG=',
maruel@chromium.orga3353652011-11-30 14:26:57 +0000499 description.strip('\n'),
500 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000501 ['--reviewers=reviewer@example.com', '--send_mail'])
maruel@chromium.orga3353652011-11-30 14:26:57 +0000502
503 def test_reviewer_send_mail_no_rev(self):
504 # Fails without a reviewer.
maruel@chromium.org2e23ce32013-05-07 12:42:28 +0000505 stdout = StringIO.StringIO()
506 stderr = StringIO.StringIO()
maruel@chromium.orga3353652011-11-30 14:26:57 +0000507 try:
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000508 self.calls = self._upload_no_rev_calls(None, None)
509 def RunEditor(desc, _, **kwargs):
maruel@chromium.orga3353652011-11-30 14:26:57 +0000510 return desc
511 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor)
maruel@chromium.org2e23ce32013-05-07 12:42:28 +0000512 self.mock(sys, 'stdout', stdout)
513 self.mock(sys, 'stderr', stderr)
maruel@chromium.orga3353652011-11-30 14:26:57 +0000514 git_cl.main(['upload', '--send-mail'])
515 self.fail()
516 except SystemExit:
maruel@chromium.org2e23ce32013-05-07 12:42:28 +0000517 self.assertEqual(
518 'Using 50% similarity for rename/copy detection. Override with '
519 '--similarity.\n',
520 stdout.getvalue())
521 self.assertEqual(
522 'Must specify reviewers to send email.\n', stderr.getvalue())
maruel@chromium.orga3353652011-11-30 14:26:57 +0000523
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000524 def test_dcommit(self):
525 self.calls = (
526 self._dcommit_calls_1() +
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000527 self._git_sanity_checks('fake_ancestor_sha', 'working') +
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000528 self._dcommit_calls_normal() +
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000529 self._dcommit_calls_3())
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000530 git_cl.main(['dcommit'])
531
532 def test_dcommit_bypass_hooks(self):
533 self.calls = (
534 self._dcommit_calls_1() +
535 self._dcommit_calls_bypassed() +
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000536 self._dcommit_calls_3())
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000537 git_cl.main(['dcommit', '--bypass-hooks'])
538
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000539
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000540 @classmethod
541 def _gerrit_base_calls(cls):
ukai@chromium.orge8077812012-02-03 03:41:46 +0000542 return [
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000543 ((['git', 'config', 'rietveld.autoupdate'],),
544 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000545 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000546 'config', 'rietveld.server'],), 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000547 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
548 ((['git', 'config', '--int', '--get',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000549 'branch.master.git-cl-similarity'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000550 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
551 ((['git', 'config', '--int', '--get',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000552 'branch.master.git-find-copies'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000553 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
554 ((['git', 'config', 'branch.master.merge'],), 'master'),
555 ((['git', 'config', 'branch.master.remote'],), 'origin'),
iannucci@chromium.org9e849272014-04-04 00:31:55 +0000556 ((['get_or_create_merge_base', 'master', 'master'],),
557 'fake_ancestor_sha'),
vadimsh@chromium.orgeed4df32015-04-10 21:30:20 +0000558 ((['git', 'config', 'gerrit.host'],), 'gerrit.example.com'),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000559 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000560 ((['git', 'rev-parse', '--show-cdup'],), ''),
561 ((['git', 'rev-parse', 'HEAD'],), '12345'),
562 ((['git',
mcgrathr@chromium.org9249f642013-06-03 21:36:18 +0000563 'diff', '--name-status', '--no-renames', '-r',
564 'fake_ancestor_sha...', '.'],),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000565 'M\t.gitignore\n'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000566 ((['git', 'config', 'branch.master.rietveldissue'],), ''),
567 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000568 'config', 'branch.master.rietveldpatchset'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000569 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000570 'log', '--pretty=format:%s%n%n%b', 'fake_ancestor_sha...'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000571 'foo'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000572 ((['git', 'config', 'user.email'],), 'me@example.com'),
573 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000574 'diff', '--no-ext-diff', '--stat', '--find-copies-harder',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000575 '-l100000', '-C50', 'fake_ancestor_sha', 'HEAD'],),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000576 '+dat'),
577 ]
578
579 @staticmethod
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000580 def _gerrit_upload_calls(description, reviewers, squash):
ukai@chromium.orge8077812012-02-03 03:41:46 +0000581 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)
585 ]
586 if git_cl.CHANGE_ID not in description:
587 calls += [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000588 ((['git', 'log', '--pretty=format:%s\n\n%b',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000589 'fake_ancestor_sha..HEAD'],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000590 description),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000591 ((['git', 'commit', '--amend', '-m', description],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000592 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000593 ((['git', 'log', '--pretty=format:%s\n\n%b',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000594 'fake_ancestor_sha..HEAD'],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000595 description)
596 ]
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000597 if squash:
598 ref_to_push = 'abcdef0123456789'
599 calls += [
600 ((['git', 'show', '--format=%s\n\n%b', '-s',
601 'refs/heads/git_cl_uploads/master'],),
602 (description, 0)),
603 ((['git', 'config', 'branch.master.merge'],),
604 'refs/heads/master'),
605 ((['git', 'config', 'branch.master.remote'],),
606 'origin'),
607 ((['get_or_create_merge_base', 'master', 'master'],),
608 'origin/master'),
609 ((['git', 'rev-parse', 'HEAD:'],),
610 '0123456789abcdef'),
611 ((['git', 'commit-tree', '0123456789abcdef', '-p',
612 'origin/master', '-m', 'd'],),
613 ref_to_push),
614 ]
615 else:
616 ref_to_push = 'HEAD'
617
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000618 calls += [
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000619 ((['git', 'rev-list', 'origin/master..' + ref_to_push],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000620 ((['git', 'config', 'rietveld.cc'],), '')
ukai@chromium.orge8077812012-02-03 03:41:46 +0000621 ]
ukai@chromium.org19bbfa22012-02-03 16:18:11 +0000622 receive_pack = '--receive-pack=git receive-pack '
ukai@chromium.orge8077812012-02-03 03:41:46 +0000623 receive_pack += '--cc=joe@example.com' # from watch list
624 if reviewers:
625 receive_pack += ' '
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000626 receive_pack += ' '.join(
627 '--reviewer=' + email for email in sorted(reviewers))
ukai@chromium.org19bbfa22012-02-03 16:18:11 +0000628 receive_pack += ''
ukai@chromium.orge8077812012-02-03 03:41:46 +0000629 calls += [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000630 ((['git',
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000631 'push', receive_pack, 'origin', ref_to_push + ':refs/for/master'],),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000632 '')
633 ]
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000634 if squash:
635 calls += [
636 ((['git', 'rev-parse', 'HEAD'],), 'abcdef0123456789'),
637 ((['git', 'update-ref', '-m', 'Uploaded abcdef0123456789',
638 'refs/heads/git_cl_uploads/master', 'abcdef0123456789'],),
639 '')
640 ]
641
ukai@chromium.orge8077812012-02-03 03:41:46 +0000642 return calls
643
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000644 def _run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000645 self,
646 upload_args,
647 description,
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000648 reviewers,
649 squash=False):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000650 """Generic gerrit upload test framework."""
ukai@chromium.orge8077812012-02-03 03:41:46 +0000651 self.calls = self._gerrit_base_calls()
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000652 self.calls += self._gerrit_upload_calls(description, reviewers, squash)
ukai@chromium.orge8077812012-02-03 03:41:46 +0000653 git_cl.main(['upload'] + upload_args)
654
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000655 def test_gerrit_upload_without_change_id(self):
656 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000657 [],
jamesr@chromium.org35d1a842012-07-27 00:20:43 +0000658 'desc\n\nBUG=\n',
ukai@chromium.orge8077812012-02-03 03:41:46 +0000659 [])
660
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000661 def test_gerrit_no_reviewer(self):
662 self._run_gerrit_upload_test(
663 [],
664 'desc\n\nBUG=\nChange-Id:123456789\n',
665 [])
666
ukai@chromium.orge8077812012-02-03 03:41:46 +0000667 def test_gerrit_reviewers_cmd_line(self):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000668 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000669 ['-r', 'foo@example.com'],
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000670 'desc\n\nBUG=\nChange-Id:123456789',
ukai@chromium.orge8077812012-02-03 03:41:46 +0000671 ['foo@example.com'])
672
673 def test_gerrit_reviewer_multiple(self):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000674 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000675 [],
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000676 'desc\nTBR=reviewer@example.com\nBUG=\nR=another@example.com\n'
677 'Change-Id:123456789\n',
ukai@chromium.orge8077812012-02-03 03:41:46 +0000678 ['reviewer@example.com', 'another@example.com'])
679
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000680 def test_gerrit_upload_squash(self):
681 self._run_gerrit_upload_test(
682 ['--squash'],
683 'desc\n\nBUG=\nChange-Id:123456789\n',
684 [],
685 squash=True)
ukai@chromium.orge8077812012-02-03 03:41:46 +0000686
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000687 def test_config_gerrit_download_hook(self):
688 self.mock(git_cl, 'FindCodereviewSettingsFile', CodereviewSettingsFileMock)
689 def ParseCodereviewSettingsContent(content):
690 keyvals = {}
691 keyvals['CODE_REVIEW_SERVER'] = 'gerrit.chromium.org'
692 keyvals['GERRIT_HOST'] = 'gerrit.chromium.org'
693 keyvals['GERRIT_PORT'] = '29418'
694 return keyvals
695 self.mock(git_cl.gclient_utils, 'ParseCodereviewSettingsContent',
696 ParseCodereviewSettingsContent)
697 self.mock(git_cl.os, 'access', self._mocked_call)
698 self.mock(git_cl.os, 'chmod', self._mocked_call)
ukai@chromium.org91655502012-05-25 01:46:07 +0000699 src_dir = os.path.join(os.path.sep, 'usr', 'local', 'src')
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000700 def AbsPath(path):
ukai@chromium.org91655502012-05-25 01:46:07 +0000701 if not path.startswith(os.path.sep):
702 return os.path.join(src_dir, path)
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000703 return path
704 self.mock(git_cl.os.path, 'abspath', AbsPath)
ukai@chromium.org91655502012-05-25 01:46:07 +0000705 commit_msg_path = os.path.join(src_dir, '.git', 'hooks', 'commit-msg')
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000706 def Exists(path):
ukai@chromium.org91655502012-05-25 01:46:07 +0000707 if path == commit_msg_path:
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000708 return False
709 # others paths, such as /usr/share/locale/....
710 return True
711 self.mock(git_cl.os.path, 'exists', Exists)
joshua.lock@intel.com426f69b2012-08-02 23:41:49 +0000712 self.mock(git_cl, 'urlretrieve', self._mocked_call)
ukai@chromium.org712d6102013-11-27 00:52:58 +0000713 self.mock(git_cl, 'hasSheBang', self._mocked_call)
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000714 self.calls = [
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000715 ((['git', 'config', 'rietveld.autoupdate'],),
716 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000717 ((['git', 'config', 'rietveld.server',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000718 'gerrit.chromium.org'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000719 ((['git', 'config', '--unset-all', 'rietveld.cc'],), ''),
720 ((['git', 'config', '--unset-all',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000721 'rietveld.private'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000722 ((['git', 'config', '--unset-all',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000723 'rietveld.tree-status-url'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000724 ((['git', 'config', '--unset-all',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000725 'rietveld.viewvc-url'],), ''),
rmistry@google.com90752582014-01-14 21:04:50 +0000726 ((['git', 'config', '--unset-all',
727 'rietveld.bug-prefix'],), ''),
thestig@chromium.org44202a22014-03-11 19:22:18 +0000728 ((['git', 'config', '--unset-all',
729 'rietveld.cpplint-regex'],), ''),
730 ((['git', 'config', '--unset-all',
kjellander@chromium.org6abc6522014-12-02 07:34:49 +0000731 'rietveld.force-https-commit-url'],), ''),
732 ((['git', 'config', '--unset-all',
thestig@chromium.org44202a22014-03-11 19:22:18 +0000733 'rietveld.cpplint-ignore-regex'],), ''),
sheyang@chromium.org152cf832014-06-11 21:37:49 +0000734 ((['git', 'config', '--unset-all',
735 'rietveld.project'],), ''),
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +0000736 ((['git', 'config', '--unset-all',
737 'rietveld.pending-ref-prefix'],), ''),
rmistry@google.com5626a922015-02-26 14:03:30 +0000738 ((['git', 'config', '--unset-all',
739 'rietveld.run-post-upload-hook'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000740 ((['git', 'config', 'gerrit.host',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000741 'gerrit.chromium.org'],), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000742 # DownloadHooks(False)
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000743 ((['git', 'config', 'gerrit.host'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000744 'gerrit.chromium.org'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000745 ((['git', 'rev-parse', '--show-cdup'],), ''),
ukai@chromium.org91655502012-05-25 01:46:07 +0000746 ((commit_msg_path, os.X_OK,), False),
ukai@chromium.org712d6102013-11-27 00:52:58 +0000747 (('https://gerrit-review.googlesource.com/tools/hooks/commit-msg',
ukai@chromium.org91655502012-05-25 01:46:07 +0000748 commit_msg_path,), ''),
ukai@chromium.org712d6102013-11-27 00:52:58 +0000749 ((commit_msg_path,), True),
ukai@chromium.org91655502012-05-25 01:46:07 +0000750 ((commit_msg_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR,), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000751 # GetCodereviewSettingsInteractively
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000752 ((['git', 'config', 'rietveld.server'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000753 'gerrit.chromium.org'),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000754 (('Rietveld server (host[:port]) [https://gerrit.chromium.org]:',),
755 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000756 ((['git', 'config', 'rietveld.cc'],), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000757 (('CC list:',), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000758 ((['git', 'config', 'rietveld.private'],), ''),
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000759 (('Private flag (rietveld only):',), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000760 ((['git', 'config', 'rietveld.tree-status-url'],), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000761 (('Tree status URL:',), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000762 ((['git', 'config', 'rietveld.viewvc-url'],), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000763 (('ViewVC URL:',), ''),
764 # DownloadHooks(True)
rmistry@google.com90752582014-01-14 21:04:50 +0000765 ((['git', 'config', 'rietveld.bug-prefix'],), ''),
766 (('Bug Prefix:',), ''),
rmistry@google.com5626a922015-02-26 14:03:30 +0000767 ((['git', 'config', 'rietveld.run-post-upload-hook'],), ''),
768 (('Run Post Upload Hook:',), ''),
ukai@chromium.org91655502012-05-25 01:46:07 +0000769 ((commit_msg_path, os.X_OK,), True),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000770 ]
771 git_cl.main(['config'])
772
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000773 def test_update_reviewers(self):
774 data = [
775 ('foo', [], 'foo'),
agable@chromium.org42c20792013-09-12 17:34:49 +0000776 ('foo\nR=xx', [], 'foo\nR=xx'),
777 ('foo\nTBR=xx', [], 'foo\nTBR=xx'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000778 ('foo', ['a@c'], 'foo\n\nR=a@c'),
agable@chromium.org42c20792013-09-12 17:34:49 +0000779 ('foo\nR=xx', ['a@c'], 'foo\n\nR=a@c, xx'),
780 ('foo\nTBR=xx', ['a@c'], 'foo\n\nR=a@c\nTBR=xx'),
781 ('foo\nTBR=xx\nR=yy', ['a@c'], 'foo\n\nR=a@c, yy\nTBR=xx'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000782 ('foo\nBUG=', ['a@c'], 'foo\nBUG=\nR=a@c'),
agable@chromium.org42c20792013-09-12 17:34:49 +0000783 ('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 +0000784 ('foo', ['a@c', 'b@c'], 'foo\n\nR=a@c, b@c'),
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +0000785 ('foo\nBar\n\nR=\nBUG=', ['c@c'], 'foo\nBar\n\nR=c@c\nBUG='),
786 ('foo\nBar\n\nR=\nBUG=\nR=', ['c@c'], 'foo\nBar\n\nR=c@c\nBUG='),
787 # Same as the line before, but full of whitespaces.
788 (
789 'foo\nBar\n\n R = \n BUG = \n R = ', ['c@c'],
790 'foo\nBar\n\nR=c@c\n BUG =',
791 ),
792 # Whitespaces aren't interpreted as new lines.
793 ('foo BUG=allo R=joe ', ['c@c'], 'foo BUG=allo R=joe\n\nR=c@c'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000794 ]
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +0000795 expected = [i[2] for i in data]
796 actual = []
797 for orig, reviewers, _expected in data:
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000798 obj = git_cl.ChangeDescription(orig)
799 obj.update_reviewers(reviewers)
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +0000800 actual.append(obj.description)
801 self.assertEqual(expected, actual)
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000802
wittman@chromium.org455dc922015-01-26 20:15:50 +0000803 def test_get_target_ref(self):
804 # Check remote or remote branch not present.
805 self.assertEqual(None, git_cl.GetTargetRef('origin', None, 'master', None))
806 self.assertEqual(None, git_cl.GetTargetRef(None,
807 'refs/remotes/origin/master',
808 'master', None))
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000809
wittman@chromium.org455dc922015-01-26 20:15:50 +0000810 # Check default target refs for branches.
811 self.assertEqual('refs/heads/master',
812 git_cl.GetTargetRef('origin', 'refs/remotes/origin/master',
813 None, None))
814 self.assertEqual('refs/heads/master',
815 git_cl.GetTargetRef('origin', 'refs/remotes/origin/lkgr',
816 None, None))
817 self.assertEqual('refs/heads/master',
818 git_cl.GetTargetRef('origin', 'refs/remotes/origin/lkcr',
819 None, None))
820 self.assertEqual('refs/branch-heads/123',
821 git_cl.GetTargetRef('origin',
822 'refs/remotes/branch-heads/123',
823 None, None))
824 self.assertEqual('refs/diff/test',
825 git_cl.GetTargetRef('origin',
826 'refs/remotes/origin/refs/diff/test',
827 None, None))
rmistry@google.comc68112d2015-03-03 12:48:06 +0000828 self.assertEqual('refs/heads/chrome/m42',
829 git_cl.GetTargetRef('origin',
830 'refs/remotes/origin/chrome/m42',
831 None, None))
wittman@chromium.org455dc922015-01-26 20:15:50 +0000832
833 # Check target refs for user-specified target branch.
834 for branch in ('branch-heads/123', 'remotes/branch-heads/123',
835 'refs/remotes/branch-heads/123'):
836 self.assertEqual('refs/branch-heads/123',
837 git_cl.GetTargetRef('origin',
838 'refs/remotes/origin/master',
839 branch, None))
840 for branch in ('origin/master', 'remotes/origin/master',
841 'refs/remotes/origin/master'):
842 self.assertEqual('refs/heads/master',
843 git_cl.GetTargetRef('origin',
844 'refs/remotes/branch-heads/123',
845 branch, None))
846 for branch in ('master', 'heads/master', 'refs/heads/master'):
847 self.assertEqual('refs/heads/master',
848 git_cl.GetTargetRef('origin',
849 'refs/remotes/branch-heads/123',
850 branch, None))
851
852 # Check target refs for pending prefix.
853 self.assertEqual('prefix/heads/master',
854 git_cl.GetTargetRef('origin', 'refs/remotes/origin/master',
855 None, 'prefix/'))
856
857
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000858if __name__ == '__main__':
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000859 git_cl.logging.basicConfig(
860 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000861 unittest.main()