blob: 68be49f792cccf455404b6dfe0428cfa4c43e64a [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)
vadimsh@chromium.org24daf9e2015-04-17 02:42:44 +000099 self.mock(git_cl.auth, '_should_use_oauth2', lambda: False)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000100 # It's important to reset settings to not have inter-tests interference.
101 git_cl.settings = None
102
103 def tearDown(self):
104 if not self.has_failed():
105 self.assertEquals([], self.calls)
106 super(TestGitCl, self).tearDown()
107
iannucci@chromium.org9e849272014-04-04 00:31:55 +0000108 def _mocked_call(self, *args, **_kwargs):
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000109 self.assertTrue(
110 self.calls,
111 '@%d Expected: <Missing> Actual: %r' % (self._calls_done, args))
112 expected_args, result = self.calls.pop(0)
maruel@chromium.orge52678e2013-04-26 18:34:44 +0000113 # Also logs otherwise it could get caught in a try/finally and be hard to
114 # diagnose.
115 if expected_args != args:
116 msg = '@%d Expected: %r Actual: %r' % (
117 self._calls_done, expected_args, args)
118 git_cl.logging.error(msg)
119 self.fail(msg)
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000120 self._calls_done += 1
121 return result
122
maruel@chromium.orga3353652011-11-30 14:26:57 +0000123 @classmethod
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000124 def _upload_calls(cls, similarity, find_copies, private):
iannucci@chromium.org79540052012-10-19 23:15:26 +0000125 return (cls._git_base_calls(similarity, find_copies) +
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000126 cls._git_upload_calls(private))
maruel@chromium.orga3353652011-11-30 14:26:57 +0000127
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000128 @classmethod
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000129 def _upload_no_rev_calls(cls, similarity, find_copies):
130 return (cls._git_base_calls(similarity, find_copies) +
131 cls._git_upload_no_rev_calls())
132
133 @classmethod
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000134 def _git_base_calls(cls, similarity, find_copies):
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000135 if similarity is None:
136 similarity = '50'
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000137 similarity_call = ((['git', 'config', '--int', '--get',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000138 'branch.master.git-cl-similarity'],), '')
139 else:
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000140 similarity_call = ((['git', 'config', '--int',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000141 'branch.master.git-cl-similarity', similarity],), '')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000142
143 if find_copies is None:
144 find_copies = True
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000145 find_copies_call = ((['git', 'config', '--int', '--get',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000146 'branch.master.git-find-copies'],), '')
147 else:
148 val = str(int(find_copies))
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000149 find_copies_call = ((['git', 'config', '--int',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000150 'branch.master.git-find-copies', val],), '')
151
152 if find_copies:
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000153 stat_call = ((['git', 'diff', '--no-ext-diff', '--stat',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000154 '--find-copies-harder', '-l100000', '-C'+similarity,
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000155 'fake_ancestor_sha', 'HEAD'],), '+dat')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000156 else:
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000157 stat_call = ((['git', 'diff', '--no-ext-diff', '--stat',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000158 '-M'+similarity, 'fake_ancestor_sha', 'HEAD'],), '+dat')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000159
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000160 return [
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000161 ((['git', 'config', 'rietveld.autoupdate'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000162 ((['git', 'config', 'rietveld.server'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000163 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000164 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000165 similarity_call,
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000166 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
iannucci@chromium.org79540052012-10-19 23:15:26 +0000167 find_copies_call,
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000168 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
169 ((['git', 'config', 'branch.master.merge'],), 'master'),
170 ((['git', 'config', 'branch.master.remote'],), 'origin'),
iannucci@chromium.org9e849272014-04-04 00:31:55 +0000171 ((['get_or_create_merge_base', 'master', 'master'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000172 'fake_ancestor_sha'),
vadimsh@chromium.orgeed4df32015-04-10 21:30:20 +0000173 ((['git', 'config', 'gerrit.host'],), ''),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000174 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000175 ((['git', 'rev-parse', '--show-cdup'],), ''),
176 ((['git', 'rev-parse', 'HEAD'],), '12345'),
177 ((['git', 'diff', '--name-status', '--no-renames', '-r',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000178 'fake_ancestor_sha...', '.'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000179 'M\t.gitignore\n'),
akuegel@chromium.org5df79ca2015-04-13 13:59:24 +0000180 ((['git', 'config', 'branch.master.rietveldissue'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000181 ((['git', 'config', 'branch.master.rietveldpatchset'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000182 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000183 ((['git', 'log', '--pretty=format:%s%n%n%b',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000184 'fake_ancestor_sha...'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000185 'foo'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000186 ((['git', 'config', 'user.email'],), 'me@example.com'),
iannucci@chromium.org79540052012-10-19 23:15:26 +0000187 stat_call,
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000188 ((['git', 'log', '--pretty=format:%s\n\n%b',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000189 'fake_ancestor_sha..HEAD'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000190 'desc\n'),
rmistry@google.com90752582014-01-14 21:04:50 +0000191 ((['git', 'config', 'rietveld.bug-prefix'],), ''),
maruel@chromium.orga3353652011-11-30 14:26:57 +0000192 ]
193
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000194 @classmethod
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000195 def _git_upload_no_rev_calls(cls):
196 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000197 ((['git', 'config', 'core.editor'],), ''),
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000198 ]
199
200 @classmethod
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000201 def _git_upload_calls(cls, private):
202 if private:
tyoshino@chromium.org99918ab2013-09-30 06:17:28 +0000203 cc_call = []
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000204 private_call = []
205 else:
tyoshino@chromium.org99918ab2013-09-30 06:17:28 +0000206 cc_call = [((['git', 'config', 'rietveld.cc'],), '')]
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000207 private_call = [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000208 ((['git', 'config', 'rietveld.private'],), '')]
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000209
maruel@chromium.orga3353652011-11-30 14:26:57 +0000210 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000211 ((['git', 'config', 'core.editor'],), ''),
tyoshino@chromium.org99918ab2013-09-30 06:17:28 +0000212 ] + cc_call + private_call + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000213 ((['git', 'config', 'branch.master.base-url'],), ''),
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +0000214 ((['git', 'config', 'rietveld.pending-ref-prefix'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000215 ((['git',
bratell@opera.com05fb9112014-07-07 09:30:23 +0000216 'config', '--local', '--get-regexp', '^svn-remote\\.'],),
217 (('', None), 0)),
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000218 ((['git', 'rev-parse', '--show-cdup'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000219 ((['git', 'svn', 'info'],), ''),
sheyang@chromium.org152cf832014-06-11 21:37:49 +0000220 ((['git', 'config', 'rietveld.project'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000221 ((['git',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000222 'config', 'branch.master.rietveldissue', '1'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000223 ((['git', 'config', 'branch.master.rietveldserver',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000224 'https://codereview.example.com'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000225 ((['git',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000226 'config', 'branch.master.rietveldpatchset', '2'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000227 ((['git', 'rev-parse', 'HEAD'],), 'hash'),
228 ((['git', 'symbolic-ref', 'HEAD'],), 'hash'),
229 ((['git',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000230 'config', 'branch.hash.last-upload-hash', 'hash'],), ''),
rmistry@google.com5626a922015-02-26 14:03:30 +0000231 ((['git', 'config', 'rietveld.run-post-upload-hook'],), ''),
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000232 ]
233
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000234 @staticmethod
235 def _git_sanity_checks(diff_base, working_branch):
236 fake_ancestor = 'fake_ancestor'
237 fake_cl = 'fake_cl_for_patch'
238 return [
239 # Calls to verify branch point is ancestor
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000240 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000241 'rev-parse', '--verify', diff_base],), fake_ancestor),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000242 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000243 'merge-base', fake_ancestor, 'HEAD'],), fake_ancestor),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000244 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000245 'rev-list', '^' + fake_ancestor, 'HEAD'],), fake_cl),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000246 # Mock a config miss (error code 1)
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000247 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000248 'config', 'gitcl.remotebranch'],), (('', None), 1)),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000249 # Call to GetRemoteBranch()
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000250 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000251 'config', 'branch.%s.merge' % working_branch],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000252 'refs/heads/master'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000253 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000254 'config', 'branch.%s.remote' % working_branch],), 'origin'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000255 ((['git', 'rev-list', '^' + fake_ancestor,
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000256 'refs/remotes/origin/master'],), ''),
257 ]
258
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000259 @classmethod
260 def _dcommit_calls_1(cls):
261 return [
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +0000262 ((['git', 'config', 'rietveld.autoupdate'],),
263 ''),
264 ((['git', 'config', 'rietveld.pending-ref-prefix'],),
265 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000266 ((['git',
bratell@opera.com05fb9112014-07-07 09:30:23 +0000267 'config', '--local', '--get-regexp', '^svn-remote\\.'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000268 ((('svn-remote.svn.url svn://svn.chromium.org/chrome\n'
269 'svn-remote.svn.fetch trunk/src:refs/remotes/origin/master'),
270 None),
271 0)),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000272 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000273 'config', 'rietveld.server'],), 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000274 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
275 ((['git', 'config', '--int', '--get',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000276 'branch.working.git-cl-similarity'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000277 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
278 ((['git', 'config', '--int', '--get',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000279 'branch.working.git-find-copies'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000280 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
281 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000282 'config', 'branch.working.merge'],), 'refs/heads/master'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000283 ((['git', 'config', 'branch.working.remote'],), 'origin'),
iannucci@chromium.org5724c962014-04-11 09:32:56 +0000284 ((['git', 'config', 'branch.working.merge'],),
285 'refs/heads/master'),
286 ((['git', 'config', 'branch.working.remote'],), 'origin'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000287 ((['git', 'rev-list', '--merges',
szager@chromium.orge84b7542012-06-15 21:26:58 +0000288 '--grep=^SVN changes up to revision [0-9]*$',
szager@chromium.org9bb85e22012-06-13 20:28:23 +0000289 'refs/remotes/origin/master^!'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000290 ((['git', 'rev-list', '^refs/heads/working',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000291 'refs/remotes/origin/master'],),
292 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000293 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000294 'log', '--grep=^git-svn-id:', '-1', '--pretty=format:%H'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000295 '3fc18b62c4966193eb435baabe2d18a3810ec82e'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000296 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000297 'rev-list', '^3fc18b62c4966193eb435baabe2d18a3810ec82e',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000298 'refs/remotes/origin/master'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000299 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000300 'merge-base', 'refs/remotes/origin/master', 'HEAD'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000301 'fake_ancestor_sha'),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000302 ]
303
304 @classmethod
305 def _dcommit_calls_normal(cls):
306 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000307 ((['git', 'rev-parse', '--show-cdup'],), ''),
308 ((['git', 'rev-parse', 'HEAD'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000309 '00ff397798ea57439712ed7e04ab96e13969ef40'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000310 ((['git',
mcgrathr@chromium.org9249f642013-06-03 21:36:18 +0000311 'diff', '--name-status', '--no-renames', '-r', 'fake_ancestor_sha...',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000312 '.'],),
313 'M\tPRESUBMIT.py'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000314 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000315 'config', 'branch.working.rietveldissue'],), '12345'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000316 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000317 'config', 'branch.working.rietveldpatchset'],), '31137'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000318 ((['git', 'config', 'branch.working.rietveldserver'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000319 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000320 ((['git', 'config', 'user.email'],), 'author@example.com'),
321 ((['git', 'config', 'rietveld.tree-status-url'],), ''),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000322 ]
323
324 @classmethod
325 def _dcommit_calls_bypassed(cls):
326 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000327 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000328 'config', 'branch.working.rietveldissue'],), '12345'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000329 ((['git', 'config', 'branch.working.rietveldserver'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000330 'codereview.example.com'),
jochen@chromium.org3ec0d542014-01-14 20:00:03 +0000331 ((['git', 'config', 'rietveld.tree-status-url'],), ''),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000332 (('GitClHooksBypassedCommit',
333 'Issue https://codereview.example.com/12345 bypassed hook when '
jochen@chromium.org3ec0d542014-01-14 20:00:03 +0000334 'committing (tree status was "unset")'), None),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000335 ]
336
337 @classmethod
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000338 def _dcommit_calls_3(cls):
339 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000340 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000341 'diff', '--no-ext-diff', '--stat', '--find-copies-harder',
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000342 '-l100000', '-C50', 'fake_ancestor_sha',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000343 'refs/heads/working'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000344 (' PRESUBMIT.py | 2 +-\n'
345 ' 1 files changed, 1 insertions(+), 1 deletions(-)\n')),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000346 ((['git', 'show-ref', '--quiet', '--verify',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000347 'refs/heads/git-cl-commit'],),
348 (('', None), 0)),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000349 ((['git', 'branch', '-D', 'git-cl-commit'],), ''),
350 ((['git', 'show-ref', '--quiet', '--verify',
szager@chromium.org9bb85e22012-06-13 20:28:23 +0000351 'refs/heads/git-cl-cherry-pick'],), ''),
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000352 ((['git', 'rev-parse', '--show-cdup'],), '\n'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000353 ((['git', 'checkout', '-q', '-b', 'git-cl-commit'],), ''),
354 ((['git', 'reset', '--soft', 'fake_ancestor_sha'],), ''),
355 ((['git', 'commit', '-m',
maruel@chromium.orge52678e2013-04-26 18:34:44 +0000356 'Issue: 12345\n\nR=john@chromium.org\n\n'
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000357 'Review URL: https://codereview.example.com/12345'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000358 ''),
kjellander@chromium.org6abc6522014-12-02 07:34:49 +0000359 ((['git', 'config', 'rietveld.force-https-commit-url'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000360 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000361 'svn', 'dcommit', '-C50', '--no-rebase', '--rmdir'],),
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000362 (('', None), 0)),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000363 ((['git', 'checkout', '-q', 'working'],), ''),
364 ((['git', 'branch', '-D', 'git-cl-commit'],), ''),
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000365 ]
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000366
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000367 @staticmethod
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000368 def _cmd_line(description, args, similarity, find_copies, private):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000369 """Returns the upload command line passed to upload.RealMain()."""
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000370 return [
371 'upload', '--assume_yes', '--server',
maruel@chromium.orgeb5edbc2012-01-16 17:03:28 +0000372 'https://codereview.example.com',
maruel@chromium.org71e12a92012-02-14 02:34:15 +0000373 '--message', description
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000374 ] + args + [
375 '--cc', 'joe@example.com',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000376 ] + (['--private'] if private else []) + [
iannucci@chromium.org79540052012-10-19 23:15:26 +0000377 '--git_similarity', similarity or '50'
378 ] + (['--git_no_find_copies'] if find_copies == False else []) + [
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000379 'fake_ancestor_sha', 'HEAD'
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000380 ]
381
382 def _run_reviewer_test(
383 self,
384 upload_args,
385 expected_description,
386 returned_description,
387 final_description,
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000388 reviewers,
389 private=False):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000390 """Generic reviewer test framework."""
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000391 try:
392 similarity = upload_args[upload_args.index('--similarity')+1]
393 except ValueError:
394 similarity = None
iannucci@chromium.org79540052012-10-19 23:15:26 +0000395
396 if '--find-copies' in upload_args:
397 find_copies = True
398 elif '--no-find-copies' in upload_args:
399 find_copies = False
400 else:
401 find_copies = None
402
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000403 private = '--private' in upload_args
404
405 self.calls = self._upload_calls(similarity, find_copies, private)
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000406
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000407 def RunEditor(desc, _, **kwargs):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000408 self.assertEquals(
409 '# Enter a description of the change.\n'
janx@chromium.org104b2db2013-04-18 12:58:40 +0000410 '# This will be displayed on the codereview site.\n'
alancutter@chromium.org63a4d7f2013-05-31 02:22:45 +0000411 '# The first line will also be used as the subject of the review.\n'
alancutter@chromium.orgbd1073e2013-06-01 00:34:38 +0000412 '#--------------------This line is 72 characters long'
413 '--------------------\n' +
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000414 expected_description,
415 desc)
416 return returned_description
417 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor)
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000418
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000419 def check_upload(args):
iannucci@chromium.org79540052012-10-19 23:15:26 +0000420 cmd_line = self._cmd_line(final_description, reviewers, similarity,
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000421 find_copies, private)
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000422 self.assertEquals(cmd_line, args)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000423 return 1, 2
424 self.mock(git_cl.upload, 'RealMain', check_upload)
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000425
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000426 git_cl.main(['upload'] + upload_args)
427
428 def test_no_reviewer(self):
429 self._run_reviewer_test(
430 [],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000431 'desc\n\nBUG=',
432 '# Blah blah comment.\ndesc\n\nBUG=',
433 'desc\n\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000434 [])
435
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000436 def test_keep_similarity(self):
437 self._run_reviewer_test(
438 ['--similarity', '70'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000439 'desc\n\nBUG=',
440 '# Blah blah comment.\ndesc\n\nBUG=',
441 'desc\n\nBUG=',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000442 [])
443
iannucci@chromium.org79540052012-10-19 23:15:26 +0000444 def test_keep_find_copies(self):
445 self._run_reviewer_test(
446 ['--no-find-copies'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000447 'desc\n\nBUG=',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000448 '# Blah blah comment.\ndesc\n\nBUG=\n',
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000449 'desc\n\nBUG=',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000450 [])
451
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000452 def test_private(self):
453 self._run_reviewer_test(
454 ['--private'],
455 'desc\n\nBUG=',
456 '# Blah blah comment.\ndesc\n\nBUG=\n',
457 'desc\n\nBUG=',
458 [])
459
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000460 def test_reviewers_cmd_line(self):
461 # Reviewer is passed as-is
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000462 description = 'desc\n\nR=foo@example.com\nBUG='
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000463 self._run_reviewer_test(
464 ['-r' 'foo@example.com'],
465 description,
466 '\n%s\n' % description,
467 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000468 ['--reviewers=foo@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000469
470 def test_reviewer_tbr_overriden(self):
471 # Reviewer is overriden with TBR
472 # Also verifies the regexp work without a trailing LF
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000473 description = 'Foo Bar\n\nTBR=reviewer@example.com'
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000474 self._run_reviewer_test(
475 ['-r' 'foo@example.com'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000476 'desc\n\nR=foo@example.com\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000477 description.strip('\n'),
478 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000479 ['--reviewers=reviewer@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000480
481 def test_reviewer_multiple(self):
482 # Handles multiple R= or TBR= lines.
483 description = (
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000484 'Foo Bar\nTBR=reviewer@example.com\nBUG=\nR=another@example.com')
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000485 self._run_reviewer_test(
486 [],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000487 'desc\n\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000488 description,
489 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000490 ['--reviewers=another@example.com,reviewer@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000491
maruel@chromium.orga3353652011-11-30 14:26:57 +0000492 def test_reviewer_send_mail(self):
493 # --send-mail can be used without -r if R= is used
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000494 description = 'Foo Bar\nR=reviewer@example.com'
maruel@chromium.orga3353652011-11-30 14:26:57 +0000495 self._run_reviewer_test(
496 ['--send-mail'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000497 'desc\n\nBUG=',
maruel@chromium.orga3353652011-11-30 14:26:57 +0000498 description.strip('\n'),
499 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000500 ['--reviewers=reviewer@example.com', '--send_mail'])
maruel@chromium.orga3353652011-11-30 14:26:57 +0000501
502 def test_reviewer_send_mail_no_rev(self):
503 # Fails without a reviewer.
maruel@chromium.org2e23ce32013-05-07 12:42:28 +0000504 stdout = StringIO.StringIO()
505 stderr = StringIO.StringIO()
maruel@chromium.orga3353652011-11-30 14:26:57 +0000506 try:
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000507 self.calls = self._upload_no_rev_calls(None, None)
508 def RunEditor(desc, _, **kwargs):
maruel@chromium.orga3353652011-11-30 14:26:57 +0000509 return desc
510 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor)
maruel@chromium.org2e23ce32013-05-07 12:42:28 +0000511 self.mock(sys, 'stdout', stdout)
512 self.mock(sys, 'stderr', stderr)
maruel@chromium.orga3353652011-11-30 14:26:57 +0000513 git_cl.main(['upload', '--send-mail'])
514 self.fail()
515 except SystemExit:
maruel@chromium.org2e23ce32013-05-07 12:42:28 +0000516 self.assertEqual(
517 'Using 50% similarity for rename/copy detection. Override with '
518 '--similarity.\n',
519 stdout.getvalue())
520 self.assertEqual(
521 'Must specify reviewers to send email.\n', stderr.getvalue())
maruel@chromium.orga3353652011-11-30 14:26:57 +0000522
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000523 def test_dcommit(self):
524 self.calls = (
525 self._dcommit_calls_1() +
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000526 self._git_sanity_checks('fake_ancestor_sha', 'working') +
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000527 self._dcommit_calls_normal() +
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000528 self._dcommit_calls_3())
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000529 git_cl.main(['dcommit'])
530
531 def test_dcommit_bypass_hooks(self):
532 self.calls = (
533 self._dcommit_calls_1() +
534 self._dcommit_calls_bypassed() +
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000535 self._dcommit_calls_3())
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000536 git_cl.main(['dcommit', '--bypass-hooks'])
537
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000538
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000539 @classmethod
540 def _gerrit_base_calls(cls):
ukai@chromium.orge8077812012-02-03 03:41:46 +0000541 return [
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000542 ((['git', 'config', 'rietveld.autoupdate'],),
543 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000544 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000545 'config', 'rietveld.server'],), 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000546 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
547 ((['git', 'config', '--int', '--get',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000548 'branch.master.git-cl-similarity'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000549 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
550 ((['git', 'config', '--int', '--get',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000551 'branch.master.git-find-copies'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000552 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
553 ((['git', 'config', 'branch.master.merge'],), 'master'),
554 ((['git', 'config', 'branch.master.remote'],), 'origin'),
iannucci@chromium.org9e849272014-04-04 00:31:55 +0000555 ((['get_or_create_merge_base', 'master', 'master'],),
556 'fake_ancestor_sha'),
vadimsh@chromium.orgeed4df32015-04-10 21:30:20 +0000557 ((['git', 'config', 'gerrit.host'],), 'gerrit.example.com'),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000558 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000559 ((['git', 'rev-parse', '--show-cdup'],), ''),
560 ((['git', 'rev-parse', 'HEAD'],), '12345'),
561 ((['git',
mcgrathr@chromium.org9249f642013-06-03 21:36:18 +0000562 'diff', '--name-status', '--no-renames', '-r',
563 'fake_ancestor_sha...', '.'],),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000564 'M\t.gitignore\n'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000565 ((['git', 'config', 'branch.master.rietveldissue'],), ''),
566 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000567 'config', 'branch.master.rietveldpatchset'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000568 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000569 'log', '--pretty=format:%s%n%n%b', 'fake_ancestor_sha...'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000570 'foo'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000571 ((['git', 'config', 'user.email'],), 'me@example.com'),
572 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000573 'diff', '--no-ext-diff', '--stat', '--find-copies-harder',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000574 '-l100000', '-C50', 'fake_ancestor_sha', 'HEAD'],),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000575 '+dat'),
576 ]
577
578 @staticmethod
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000579 def _gerrit_upload_calls(description, reviewers, squash):
ukai@chromium.orge8077812012-02-03 03:41:46 +0000580 calls = [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000581 ((['git', 'log', '--pretty=format:%s\n\n%b',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000582 'fake_ancestor_sha..HEAD'],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000583 description)
584 ]
585 if git_cl.CHANGE_ID not in description:
586 calls += [
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),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000590 ((['git', 'commit', '--amend', '-m', description],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000591 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000592 ((['git', 'log', '--pretty=format:%s\n\n%b',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000593 'fake_ancestor_sha..HEAD'],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000594 description)
595 ]
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000596 if squash:
597 ref_to_push = 'abcdef0123456789'
598 calls += [
599 ((['git', 'show', '--format=%s\n\n%b', '-s',
600 'refs/heads/git_cl_uploads/master'],),
601 (description, 0)),
602 ((['git', 'config', 'branch.master.merge'],),
603 'refs/heads/master'),
604 ((['git', 'config', 'branch.master.remote'],),
605 'origin'),
606 ((['get_or_create_merge_base', 'master', 'master'],),
607 'origin/master'),
608 ((['git', 'rev-parse', 'HEAD:'],),
609 '0123456789abcdef'),
610 ((['git', 'commit-tree', '0123456789abcdef', '-p',
611 'origin/master', '-m', 'd'],),
612 ref_to_push),
613 ]
614 else:
615 ref_to_push = 'HEAD'
616
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000617 calls += [
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000618 ((['git', 'rev-list', 'origin/master..' + ref_to_push],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000619 ((['git', 'config', 'rietveld.cc'],), '')
ukai@chromium.orge8077812012-02-03 03:41:46 +0000620 ]
ukai@chromium.org19bbfa22012-02-03 16:18:11 +0000621 receive_pack = '--receive-pack=git receive-pack '
ukai@chromium.orge8077812012-02-03 03:41:46 +0000622 receive_pack += '--cc=joe@example.com' # from watch list
623 if reviewers:
624 receive_pack += ' '
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000625 receive_pack += ' '.join(
626 '--reviewer=' + email for email in sorted(reviewers))
ukai@chromium.org19bbfa22012-02-03 16:18:11 +0000627 receive_pack += ''
ukai@chromium.orge8077812012-02-03 03:41:46 +0000628 calls += [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000629 ((['git',
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000630 'push', receive_pack, 'origin', ref_to_push + ':refs/for/master'],),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000631 '')
632 ]
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000633 if squash:
634 calls += [
635 ((['git', 'rev-parse', 'HEAD'],), 'abcdef0123456789'),
636 ((['git', 'update-ref', '-m', 'Uploaded abcdef0123456789',
637 'refs/heads/git_cl_uploads/master', 'abcdef0123456789'],),
638 '')
639 ]
640
ukai@chromium.orge8077812012-02-03 03:41:46 +0000641 return calls
642
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000643 def _run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000644 self,
645 upload_args,
646 description,
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000647 reviewers,
648 squash=False):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000649 """Generic gerrit upload test framework."""
ukai@chromium.orge8077812012-02-03 03:41:46 +0000650 self.calls = self._gerrit_base_calls()
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000651 self.calls += self._gerrit_upload_calls(description, reviewers, squash)
ukai@chromium.orge8077812012-02-03 03:41:46 +0000652 git_cl.main(['upload'] + upload_args)
653
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000654 def test_gerrit_upload_without_change_id(self):
655 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000656 [],
jamesr@chromium.org35d1a842012-07-27 00:20:43 +0000657 'desc\n\nBUG=\n',
ukai@chromium.orge8077812012-02-03 03:41:46 +0000658 [])
659
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000660 def test_gerrit_no_reviewer(self):
661 self._run_gerrit_upload_test(
662 [],
663 'desc\n\nBUG=\nChange-Id:123456789\n',
664 [])
665
ukai@chromium.orge8077812012-02-03 03:41:46 +0000666 def test_gerrit_reviewers_cmd_line(self):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000667 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000668 ['-r', 'foo@example.com'],
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000669 'desc\n\nBUG=\nChange-Id:123456789',
ukai@chromium.orge8077812012-02-03 03:41:46 +0000670 ['foo@example.com'])
671
672 def test_gerrit_reviewer_multiple(self):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000673 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000674 [],
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000675 'desc\nTBR=reviewer@example.com\nBUG=\nR=another@example.com\n'
676 'Change-Id:123456789\n',
ukai@chromium.orge8077812012-02-03 03:41:46 +0000677 ['reviewer@example.com', 'another@example.com'])
678
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000679 def test_gerrit_upload_squash(self):
680 self._run_gerrit_upload_test(
681 ['--squash'],
682 'desc\n\nBUG=\nChange-Id:123456789\n',
683 [],
684 squash=True)
ukai@chromium.orge8077812012-02-03 03:41:46 +0000685
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000686 def test_config_gerrit_download_hook(self):
687 self.mock(git_cl, 'FindCodereviewSettingsFile', CodereviewSettingsFileMock)
688 def ParseCodereviewSettingsContent(content):
689 keyvals = {}
690 keyvals['CODE_REVIEW_SERVER'] = 'gerrit.chromium.org'
691 keyvals['GERRIT_HOST'] = 'gerrit.chromium.org'
692 keyvals['GERRIT_PORT'] = '29418'
693 return keyvals
694 self.mock(git_cl.gclient_utils, 'ParseCodereviewSettingsContent',
695 ParseCodereviewSettingsContent)
696 self.mock(git_cl.os, 'access', self._mocked_call)
697 self.mock(git_cl.os, 'chmod', self._mocked_call)
ukai@chromium.org91655502012-05-25 01:46:07 +0000698 src_dir = os.path.join(os.path.sep, 'usr', 'local', 'src')
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000699 def AbsPath(path):
ukai@chromium.org91655502012-05-25 01:46:07 +0000700 if not path.startswith(os.path.sep):
701 return os.path.join(src_dir, path)
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000702 return path
703 self.mock(git_cl.os.path, 'abspath', AbsPath)
ukai@chromium.org91655502012-05-25 01:46:07 +0000704 commit_msg_path = os.path.join(src_dir, '.git', 'hooks', 'commit-msg')
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000705 def Exists(path):
ukai@chromium.org91655502012-05-25 01:46:07 +0000706 if path == commit_msg_path:
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000707 return False
708 # others paths, such as /usr/share/locale/....
709 return True
710 self.mock(git_cl.os.path, 'exists', Exists)
joshua.lock@intel.com426f69b2012-08-02 23:41:49 +0000711 self.mock(git_cl, 'urlretrieve', self._mocked_call)
ukai@chromium.org712d6102013-11-27 00:52:58 +0000712 self.mock(git_cl, 'hasSheBang', self._mocked_call)
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000713 self.calls = [
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000714 ((['git', 'config', 'rietveld.autoupdate'],),
715 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000716 ((['git', 'config', 'rietveld.server',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000717 'gerrit.chromium.org'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000718 ((['git', 'config', '--unset-all', 'rietveld.cc'],), ''),
719 ((['git', 'config', '--unset-all',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000720 'rietveld.private'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000721 ((['git', 'config', '--unset-all',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000722 'rietveld.tree-status-url'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000723 ((['git', 'config', '--unset-all',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000724 'rietveld.viewvc-url'],), ''),
rmistry@google.com90752582014-01-14 21:04:50 +0000725 ((['git', 'config', '--unset-all',
726 'rietveld.bug-prefix'],), ''),
thestig@chromium.org44202a22014-03-11 19:22:18 +0000727 ((['git', 'config', '--unset-all',
728 'rietveld.cpplint-regex'],), ''),
729 ((['git', 'config', '--unset-all',
kjellander@chromium.org6abc6522014-12-02 07:34:49 +0000730 'rietveld.force-https-commit-url'],), ''),
731 ((['git', 'config', '--unset-all',
thestig@chromium.org44202a22014-03-11 19:22:18 +0000732 'rietveld.cpplint-ignore-regex'],), ''),
sheyang@chromium.org152cf832014-06-11 21:37:49 +0000733 ((['git', 'config', '--unset-all',
734 'rietveld.project'],), ''),
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +0000735 ((['git', 'config', '--unset-all',
736 'rietveld.pending-ref-prefix'],), ''),
rmistry@google.com5626a922015-02-26 14:03:30 +0000737 ((['git', 'config', '--unset-all',
738 'rietveld.run-post-upload-hook'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000739 ((['git', 'config', 'gerrit.host',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000740 'gerrit.chromium.org'],), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000741 # DownloadHooks(False)
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000742 ((['git', 'config', 'gerrit.host'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000743 'gerrit.chromium.org'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000744 ((['git', 'rev-parse', '--show-cdup'],), ''),
ukai@chromium.org91655502012-05-25 01:46:07 +0000745 ((commit_msg_path, os.X_OK,), False),
ukai@chromium.org712d6102013-11-27 00:52:58 +0000746 (('https://gerrit-review.googlesource.com/tools/hooks/commit-msg',
ukai@chromium.org91655502012-05-25 01:46:07 +0000747 commit_msg_path,), ''),
ukai@chromium.org712d6102013-11-27 00:52:58 +0000748 ((commit_msg_path,), True),
ukai@chromium.org91655502012-05-25 01:46:07 +0000749 ((commit_msg_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR,), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000750 # GetCodereviewSettingsInteractively
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000751 ((['git', 'config', 'rietveld.server'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000752 'gerrit.chromium.org'),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000753 (('Rietveld server (host[:port]) [https://gerrit.chromium.org]:',),
754 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000755 ((['git', 'config', 'rietveld.cc'],), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000756 (('CC list:',), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000757 ((['git', 'config', 'rietveld.private'],), ''),
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000758 (('Private flag (rietveld only):',), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000759 ((['git', 'config', 'rietveld.tree-status-url'],), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000760 (('Tree status URL:',), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000761 ((['git', 'config', 'rietveld.viewvc-url'],), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000762 (('ViewVC URL:',), ''),
763 # DownloadHooks(True)
rmistry@google.com90752582014-01-14 21:04:50 +0000764 ((['git', 'config', 'rietveld.bug-prefix'],), ''),
765 (('Bug Prefix:',), ''),
rmistry@google.com5626a922015-02-26 14:03:30 +0000766 ((['git', 'config', 'rietveld.run-post-upload-hook'],), ''),
767 (('Run Post Upload Hook:',), ''),
ukai@chromium.org91655502012-05-25 01:46:07 +0000768 ((commit_msg_path, os.X_OK,), True),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000769 ]
770 git_cl.main(['config'])
771
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000772 def test_update_reviewers(self):
773 data = [
774 ('foo', [], 'foo'),
agable@chromium.org42c20792013-09-12 17:34:49 +0000775 ('foo\nR=xx', [], 'foo\nR=xx'),
776 ('foo\nTBR=xx', [], 'foo\nTBR=xx'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000777 ('foo', ['a@c'], 'foo\n\nR=a@c'),
agable@chromium.org42c20792013-09-12 17:34:49 +0000778 ('foo\nR=xx', ['a@c'], 'foo\n\nR=a@c, xx'),
779 ('foo\nTBR=xx', ['a@c'], 'foo\n\nR=a@c\nTBR=xx'),
780 ('foo\nTBR=xx\nR=yy', ['a@c'], 'foo\n\nR=a@c, yy\nTBR=xx'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000781 ('foo\nBUG=', ['a@c'], 'foo\nBUG=\nR=a@c'),
agable@chromium.org42c20792013-09-12 17:34:49 +0000782 ('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 +0000783 ('foo', ['a@c', 'b@c'], 'foo\n\nR=a@c, b@c'),
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +0000784 ('foo\nBar\n\nR=\nBUG=', ['c@c'], 'foo\nBar\n\nR=c@c\nBUG='),
785 ('foo\nBar\n\nR=\nBUG=\nR=', ['c@c'], 'foo\nBar\n\nR=c@c\nBUG='),
786 # Same as the line before, but full of whitespaces.
787 (
788 'foo\nBar\n\n R = \n BUG = \n R = ', ['c@c'],
789 'foo\nBar\n\nR=c@c\n BUG =',
790 ),
791 # Whitespaces aren't interpreted as new lines.
792 ('foo BUG=allo R=joe ', ['c@c'], 'foo BUG=allo R=joe\n\nR=c@c'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000793 ]
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +0000794 expected = [i[2] for i in data]
795 actual = []
796 for orig, reviewers, _expected in data:
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000797 obj = git_cl.ChangeDescription(orig)
798 obj.update_reviewers(reviewers)
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +0000799 actual.append(obj.description)
800 self.assertEqual(expected, actual)
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000801
wittman@chromium.org455dc922015-01-26 20:15:50 +0000802 def test_get_target_ref(self):
803 # Check remote or remote branch not present.
804 self.assertEqual(None, git_cl.GetTargetRef('origin', None, 'master', None))
805 self.assertEqual(None, git_cl.GetTargetRef(None,
806 'refs/remotes/origin/master',
807 'master', None))
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000808
wittman@chromium.org455dc922015-01-26 20:15:50 +0000809 # Check default target refs for branches.
810 self.assertEqual('refs/heads/master',
811 git_cl.GetTargetRef('origin', 'refs/remotes/origin/master',
812 None, None))
813 self.assertEqual('refs/heads/master',
814 git_cl.GetTargetRef('origin', 'refs/remotes/origin/lkgr',
815 None, None))
816 self.assertEqual('refs/heads/master',
817 git_cl.GetTargetRef('origin', 'refs/remotes/origin/lkcr',
818 None, None))
819 self.assertEqual('refs/branch-heads/123',
820 git_cl.GetTargetRef('origin',
821 'refs/remotes/branch-heads/123',
822 None, None))
823 self.assertEqual('refs/diff/test',
824 git_cl.GetTargetRef('origin',
825 'refs/remotes/origin/refs/diff/test',
826 None, None))
rmistry@google.comc68112d2015-03-03 12:48:06 +0000827 self.assertEqual('refs/heads/chrome/m42',
828 git_cl.GetTargetRef('origin',
829 'refs/remotes/origin/chrome/m42',
830 None, None))
wittman@chromium.org455dc922015-01-26 20:15:50 +0000831
832 # Check target refs for user-specified target branch.
833 for branch in ('branch-heads/123', 'remotes/branch-heads/123',
834 'refs/remotes/branch-heads/123'):
835 self.assertEqual('refs/branch-heads/123',
836 git_cl.GetTargetRef('origin',
837 'refs/remotes/origin/master',
838 branch, None))
839 for branch in ('origin/master', 'remotes/origin/master',
840 'refs/remotes/origin/master'):
841 self.assertEqual('refs/heads/master',
842 git_cl.GetTargetRef('origin',
843 'refs/remotes/branch-heads/123',
844 branch, None))
845 for branch in ('master', 'heads/master', 'refs/heads/master'):
846 self.assertEqual('refs/heads/master',
847 git_cl.GetTargetRef('origin',
848 'refs/remotes/branch-heads/123',
849 branch, None))
850
851 # Check target refs for pending prefix.
852 self.assertEqual('prefix/heads/master',
853 git_cl.GetTargetRef('origin', 'refs/remotes/origin/master',
854 None, 'prefix/'))
855
856
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000857if __name__ == '__main__':
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000858 git_cl.logging.basicConfig(
859 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000860 unittest.main()