blob: b5e14dcc8fb6de11f43bae759fbcd3ec77a2e88a [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))))
pgervais@chromium.org8ba38ff2015-06-11 21:41:25 +000089 self.mock(git_cl, 'BranchExists', lambda _: True)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000090 self.mock(git_cl, 'FindCodereviewSettingsFile', lambda: '')
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000091 self.mock(git_cl, 'ask_for_data', self._mocked_call)
92 self.mock(git_cl.breakpad, 'post', self._mocked_call)
93 self.mock(git_cl.breakpad, 'SendStack', self._mocked_call)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000094 self.mock(git_cl.presubmit_support, 'DoPresubmitChecks', PresubmitMock)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000095 self.mock(git_cl.rietveld, 'Rietveld', RietveldMock)
maruel@chromium.org4bac4b52012-11-27 20:33:52 +000096 self.mock(git_cl.rietveld, 'CachingRietveld', RietveldMock)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000097 self.mock(git_cl.upload, 'RealMain', self.fail)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000098 self.mock(git_cl.watchlists, 'Watchlists', WatchlistsMock)
vadimsh@chromium.orgeed4df32015-04-10 21:30:20 +000099 self.mock(git_cl.auth, 'get_authenticator_for_host', AuthenticatorMock)
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):
wychen@chromium.org445c8962015-04-28 23:30:05 +0000104 try:
105 if not self.has_failed():
106 self.assertEquals([], self.calls)
107 finally:
108 super(TestGitCl, self).tearDown()
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000109
iannucci@chromium.org9e849272014-04-04 00:31:55 +0000110 def _mocked_call(self, *args, **_kwargs):
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000111 self.assertTrue(
112 self.calls,
113 '@%d Expected: <Missing> Actual: %r' % (self._calls_done, args))
wychen@chromium.orga872e752015-04-28 23:42:18 +0000114 top = self.calls.pop(0)
115 if len(top) > 2 and top[2]:
116 raise top[2]
117 expected_args, result = top
118
maruel@chromium.orge52678e2013-04-26 18:34:44 +0000119 # Also logs otherwise it could get caught in a try/finally and be hard to
120 # diagnose.
121 if expected_args != args:
122 msg = '@%d Expected: %r Actual: %r' % (
123 self._calls_done, expected_args, args)
124 git_cl.logging.error(msg)
125 self.fail(msg)
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000126 self._calls_done += 1
127 return result
128
maruel@chromium.orga3353652011-11-30 14:26:57 +0000129 @classmethod
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000130 def _upload_calls(cls, similarity, find_copies, private):
iannucci@chromium.org79540052012-10-19 23:15:26 +0000131 return (cls._git_base_calls(similarity, find_copies) +
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000132 cls._git_upload_calls(private))
maruel@chromium.orga3353652011-11-30 14:26:57 +0000133
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000134 @classmethod
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000135 def _upload_no_rev_calls(cls, similarity, find_copies):
136 return (cls._git_base_calls(similarity, find_copies) +
137 cls._git_upload_no_rev_calls())
138
139 @classmethod
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000140 def _git_base_calls(cls, similarity, find_copies):
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000141 if similarity is None:
142 similarity = '50'
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000143 similarity_call = ((['git', 'config', '--int', '--get',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000144 'branch.master.git-cl-similarity'],), '')
145 else:
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000146 similarity_call = ((['git', 'config', '--int',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000147 'branch.master.git-cl-similarity', similarity],), '')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000148
149 if find_copies is None:
150 find_copies = True
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000151 find_copies_call = ((['git', 'config', '--int', '--get',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000152 'branch.master.git-find-copies'],), '')
153 else:
154 val = str(int(find_copies))
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000155 find_copies_call = ((['git', 'config', '--int',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000156 'branch.master.git-find-copies', val],), '')
157
158 if find_copies:
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000159 stat_call = ((['git', 'diff', '--no-ext-diff', '--stat',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000160 '--find-copies-harder', '-l100000', '-C'+similarity,
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000161 'fake_ancestor_sha', 'HEAD'],), '+dat')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000162 else:
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000163 stat_call = ((['git', 'diff', '--no-ext-diff', '--stat',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000164 '-M'+similarity, 'fake_ancestor_sha', 'HEAD'],), '+dat')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000165
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000166 return [
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000167 ((['git', 'config', 'rietveld.autoupdate'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000168 ((['git', 'config', 'rietveld.server'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000169 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000170 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000171 similarity_call,
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000172 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
iannucci@chromium.org79540052012-10-19 23:15:26 +0000173 find_copies_call,
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000174 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
175 ((['git', 'config', 'branch.master.merge'],), 'master'),
176 ((['git', 'config', 'branch.master.remote'],), 'origin'),
iannucci@chromium.org9e849272014-04-04 00:31:55 +0000177 ((['get_or_create_merge_base', 'master', 'master'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000178 'fake_ancestor_sha'),
vadimsh@chromium.orgeed4df32015-04-10 21:30:20 +0000179 ((['git', 'config', 'gerrit.host'],), ''),
vadimsh@chromium.org19f3fe62015-04-20 17:03:10 +0000180 ((['git', 'config', 'branch.master.rietveldissue'],), ''),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000181 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000182 ((['git', 'rev-parse', '--show-cdup'],), ''),
183 ((['git', 'rev-parse', 'HEAD'],), '12345'),
184 ((['git', 'diff', '--name-status', '--no-renames', '-r',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000185 'fake_ancestor_sha...', '.'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000186 'M\t.gitignore\n'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000187 ((['git', 'config', 'branch.master.rietveldpatchset'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000188 ''),
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...'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000191 'foo'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000192 ((['git', 'config', 'user.email'],), 'me@example.com'),
iannucci@chromium.org79540052012-10-19 23:15:26 +0000193 stat_call,
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000194 ((['git', 'log', '--pretty=format:%s\n\n%b',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000195 'fake_ancestor_sha..HEAD'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000196 'desc\n'),
rmistry@google.com90752582014-01-14 21:04:50 +0000197 ((['git', 'config', 'rietveld.bug-prefix'],), ''),
maruel@chromium.orga3353652011-11-30 14:26:57 +0000198 ]
199
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000200 @classmethod
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000201 def _git_upload_no_rev_calls(cls):
202 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000203 ((['git', 'config', 'core.editor'],), ''),
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000204 ]
205
206 @classmethod
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000207 def _git_upload_calls(cls, private):
208 if private:
tyoshino@chromium.org99918ab2013-09-30 06:17:28 +0000209 cc_call = []
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000210 private_call = []
211 else:
tyoshino@chromium.org99918ab2013-09-30 06:17:28 +0000212 cc_call = [((['git', 'config', 'rietveld.cc'],), '')]
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000213 private_call = [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000214 ((['git', 'config', 'rietveld.private'],), '')]
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000215
maruel@chromium.orga3353652011-11-30 14:26:57 +0000216 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000217 ((['git', 'config', 'core.editor'],), ''),
tyoshino@chromium.org99918ab2013-09-30 06:17:28 +0000218 ] + cc_call + private_call + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000219 ((['git', 'config', 'branch.master.base-url'],), ''),
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +0000220 ((['git', 'config', 'rietveld.pending-ref-prefix'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000221 ((['git',
bratell@opera.com05fb9112014-07-07 09:30:23 +0000222 'config', '--local', '--get-regexp', '^svn-remote\\.'],),
223 (('', None), 0)),
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000224 ((['git', 'rev-parse', '--show-cdup'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000225 ((['git', 'svn', 'info'],), ''),
sheyang@chromium.org152cf832014-06-11 21:37:49 +0000226 ((['git', 'config', 'rietveld.project'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000227 ((['git',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000228 'config', 'branch.master.rietveldissue', '1'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000229 ((['git', 'config', 'branch.master.rietveldserver',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000230 'https://codereview.example.com'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000231 ((['git',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000232 'config', 'branch.master.rietveldpatchset', '2'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000233 ((['git', 'rev-parse', 'HEAD'],), 'hash'),
234 ((['git', 'symbolic-ref', 'HEAD'],), 'hash'),
235 ((['git',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000236 'config', 'branch.hash.last-upload-hash', 'hash'],), ''),
rmistry@google.com5626a922015-02-26 14:03:30 +0000237 ((['git', 'config', 'rietveld.run-post-upload-hook'],), ''),
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000238 ]
239
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000240 @staticmethod
241 def _git_sanity_checks(diff_base, working_branch):
242 fake_ancestor = 'fake_ancestor'
243 fake_cl = 'fake_cl_for_patch'
244 return [
245 # Calls to verify branch point is ancestor
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000246 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000247 'rev-parse', '--verify', diff_base],), fake_ancestor),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000248 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000249 'merge-base', fake_ancestor, 'HEAD'],), fake_ancestor),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000250 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000251 'rev-list', '^' + fake_ancestor, 'HEAD'],), fake_cl),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000252 # Mock a config miss (error code 1)
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000253 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000254 'config', 'gitcl.remotebranch'],), (('', None), 1)),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000255 # Call to GetRemoteBranch()
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000256 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000257 'config', 'branch.%s.merge' % working_branch],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000258 'refs/heads/master'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000259 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000260 'config', 'branch.%s.remote' % working_branch],), 'origin'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000261 ((['git', 'rev-list', '^' + fake_ancestor,
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000262 'refs/remotes/origin/master'],), ''),
263 ]
264
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000265 @classmethod
266 def _dcommit_calls_1(cls):
267 return [
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +0000268 ((['git', 'config', 'rietveld.autoupdate'],),
269 ''),
270 ((['git', 'config', 'rietveld.pending-ref-prefix'],),
271 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000272 ((['git',
bratell@opera.com05fb9112014-07-07 09:30:23 +0000273 'config', '--local', '--get-regexp', '^svn-remote\\.'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000274 ((('svn-remote.svn.url svn://svn.chromium.org/chrome\n'
275 'svn-remote.svn.fetch trunk/src:refs/remotes/origin/master'),
276 None),
277 0)),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000278 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000279 'config', 'rietveld.server'],), 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000280 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
281 ((['git', 'config', '--int', '--get',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000282 'branch.working.git-cl-similarity'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000283 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
284 ((['git', 'config', '--int', '--get',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000285 'branch.working.git-find-copies'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000286 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
287 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000288 'config', 'branch.working.merge'],), 'refs/heads/master'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000289 ((['git', 'config', 'branch.working.remote'],), 'origin'),
iannucci@chromium.org5724c962014-04-11 09:32:56 +0000290 ((['git', 'config', 'branch.working.merge'],),
291 'refs/heads/master'),
292 ((['git', 'config', 'branch.working.remote'],), 'origin'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000293 ((['git', 'rev-list', '--merges',
szager@chromium.orge84b7542012-06-15 21:26:58 +0000294 '--grep=^SVN changes up to revision [0-9]*$',
szager@chromium.org9bb85e22012-06-13 20:28:23 +0000295 'refs/remotes/origin/master^!'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000296 ((['git', 'rev-list', '^refs/heads/working',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000297 'refs/remotes/origin/master'],),
298 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000299 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000300 'log', '--grep=^git-svn-id:', '-1', '--pretty=format:%H'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000301 '3fc18b62c4966193eb435baabe2d18a3810ec82e'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000302 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000303 'rev-list', '^3fc18b62c4966193eb435baabe2d18a3810ec82e',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000304 'refs/remotes/origin/master'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000305 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000306 'merge-base', 'refs/remotes/origin/master', 'HEAD'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000307 'fake_ancestor_sha'),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000308 ]
309
310 @classmethod
311 def _dcommit_calls_normal(cls):
312 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000313 ((['git', 'rev-parse', '--show-cdup'],), ''),
314 ((['git', 'rev-parse', 'HEAD'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000315 '00ff397798ea57439712ed7e04ab96e13969ef40'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000316 ((['git',
mcgrathr@chromium.org9249f642013-06-03 21:36:18 +0000317 'diff', '--name-status', '--no-renames', '-r', 'fake_ancestor_sha...',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000318 '.'],),
319 'M\tPRESUBMIT.py'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000320 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000321 'config', 'branch.working.rietveldissue'],), '12345'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000322 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000323 'config', 'branch.working.rietveldpatchset'],), '31137'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000324 ((['git', 'config', 'branch.working.rietveldserver'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000325 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000326 ((['git', 'config', 'user.email'],), 'author@example.com'),
327 ((['git', 'config', 'rietveld.tree-status-url'],), ''),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000328 ]
329
330 @classmethod
331 def _dcommit_calls_bypassed(cls):
332 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000333 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000334 'config', 'branch.working.rietveldissue'],), '12345'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000335 ((['git', 'config', 'branch.working.rietveldserver'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000336 'codereview.example.com'),
jochen@chromium.org3ec0d542014-01-14 20:00:03 +0000337 ((['git', 'config', 'rietveld.tree-status-url'],), ''),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000338 (('GitClHooksBypassedCommit',
339 'Issue https://codereview.example.com/12345 bypassed hook when '
jochen@chromium.org3ec0d542014-01-14 20:00:03 +0000340 'committing (tree status was "unset")'), None),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000341 ]
342
343 @classmethod
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000344 def _dcommit_calls_3(cls):
345 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000346 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000347 'diff', '--no-ext-diff', '--stat', '--find-copies-harder',
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000348 '-l100000', '-C50', 'fake_ancestor_sha',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000349 'refs/heads/working'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000350 (' PRESUBMIT.py | 2 +-\n'
351 ' 1 files changed, 1 insertions(+), 1 deletions(-)\n')),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000352 ((['git', 'show-ref', '--quiet', '--verify',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000353 'refs/heads/git-cl-commit'],),
354 (('', None), 0)),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000355 ((['git', 'branch', '-D', 'git-cl-commit'],), ''),
356 ((['git', 'show-ref', '--quiet', '--verify',
szager@chromium.org9bb85e22012-06-13 20:28:23 +0000357 'refs/heads/git-cl-cherry-pick'],), ''),
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000358 ((['git', 'rev-parse', '--show-cdup'],), '\n'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000359 ((['git', 'checkout', '-q', '-b', 'git-cl-commit'],), ''),
360 ((['git', 'reset', '--soft', 'fake_ancestor_sha'],), ''),
361 ((['git', 'commit', '-m',
maruel@chromium.orge52678e2013-04-26 18:34:44 +0000362 'Issue: 12345\n\nR=john@chromium.org\n\n'
sergiyb@chromium.org4b39c5f2015-07-07 10:33:12 +0000363 'Review URL: https://codereview.example.com/12345 .'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000364 ''),
kjellander@chromium.org6abc6522014-12-02 07:34:49 +0000365 ((['git', 'config', 'rietveld.force-https-commit-url'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000366 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000367 'svn', 'dcommit', '-C50', '--no-rebase', '--rmdir'],),
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000368 (('', None), 0)),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000369 ((['git', 'checkout', '-q', 'working'],), ''),
370 ((['git', 'branch', '-D', 'git-cl-commit'],), ''),
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000371 ]
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000372
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000373 @staticmethod
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000374 def _cmd_line(description, args, similarity, find_copies, private):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000375 """Returns the upload command line passed to upload.RealMain()."""
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000376 return [
377 'upload', '--assume_yes', '--server',
maruel@chromium.orgeb5edbc2012-01-16 17:03:28 +0000378 'https://codereview.example.com',
maruel@chromium.org71e12a92012-02-14 02:34:15 +0000379 '--message', description
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000380 ] + args + [
381 '--cc', 'joe@example.com',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000382 ] + (['--private'] if private else []) + [
iannucci@chromium.org79540052012-10-19 23:15:26 +0000383 '--git_similarity', similarity or '50'
384 ] + (['--git_no_find_copies'] if find_copies == False else []) + [
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000385 'fake_ancestor_sha', 'HEAD'
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000386 ]
387
388 def _run_reviewer_test(
389 self,
390 upload_args,
391 expected_description,
392 returned_description,
393 final_description,
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000394 reviewers,
395 private=False):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000396 """Generic reviewer test framework."""
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000397 try:
398 similarity = upload_args[upload_args.index('--similarity')+1]
399 except ValueError:
400 similarity = None
iannucci@chromium.org79540052012-10-19 23:15:26 +0000401
402 if '--find-copies' in upload_args:
403 find_copies = True
404 elif '--no-find-copies' in upload_args:
405 find_copies = False
406 else:
407 find_copies = None
408
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000409 private = '--private' in upload_args
410
411 self.calls = self._upload_calls(similarity, find_copies, private)
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000412
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000413 def RunEditor(desc, _, **kwargs):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000414 self.assertEquals(
415 '# Enter a description of the change.\n'
janx@chromium.org104b2db2013-04-18 12:58:40 +0000416 '# This will be displayed on the codereview site.\n'
alancutter@chromium.org63a4d7f2013-05-31 02:22:45 +0000417 '# The first line will also be used as the subject of the review.\n'
alancutter@chromium.orgbd1073e2013-06-01 00:34:38 +0000418 '#--------------------This line is 72 characters long'
419 '--------------------\n' +
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000420 expected_description,
421 desc)
422 return returned_description
423 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor)
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000424
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000425 def check_upload(args):
iannucci@chromium.org79540052012-10-19 23:15:26 +0000426 cmd_line = self._cmd_line(final_description, reviewers, similarity,
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000427 find_copies, private)
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000428 self.assertEquals(cmd_line, args)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000429 return 1, 2
430 self.mock(git_cl.upload, 'RealMain', check_upload)
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000431
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000432 git_cl.main(['upload'] + upload_args)
433
434 def test_no_reviewer(self):
435 self._run_reviewer_test(
436 [],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000437 'desc\n\nBUG=',
438 '# Blah blah comment.\ndesc\n\nBUG=',
439 'desc\n\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000440 [])
441
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000442 def test_keep_similarity(self):
443 self._run_reviewer_test(
444 ['--similarity', '70'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000445 'desc\n\nBUG=',
446 '# Blah blah comment.\ndesc\n\nBUG=',
447 'desc\n\nBUG=',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000448 [])
449
iannucci@chromium.org79540052012-10-19 23:15:26 +0000450 def test_keep_find_copies(self):
451 self._run_reviewer_test(
452 ['--no-find-copies'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000453 'desc\n\nBUG=',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000454 '# Blah blah comment.\ndesc\n\nBUG=\n',
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000455 'desc\n\nBUG=',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000456 [])
457
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000458 def test_private(self):
459 self._run_reviewer_test(
460 ['--private'],
461 'desc\n\nBUG=',
462 '# Blah blah comment.\ndesc\n\nBUG=\n',
463 'desc\n\nBUG=',
464 [])
465
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000466 def test_reviewers_cmd_line(self):
467 # Reviewer is passed as-is
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000468 description = 'desc\n\nR=foo@example.com\nBUG='
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000469 self._run_reviewer_test(
470 ['-r' 'foo@example.com'],
471 description,
472 '\n%s\n' % description,
473 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000474 ['--reviewers=foo@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000475
476 def test_reviewer_tbr_overriden(self):
477 # Reviewer is overriden with TBR
478 # Also verifies the regexp work without a trailing LF
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000479 description = 'Foo Bar\n\nTBR=reviewer@example.com'
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000480 self._run_reviewer_test(
481 ['-r' 'foo@example.com'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000482 'desc\n\nR=foo@example.com\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000483 description.strip('\n'),
484 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000485 ['--reviewers=reviewer@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000486
487 def test_reviewer_multiple(self):
488 # Handles multiple R= or TBR= lines.
489 description = (
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000490 'Foo Bar\nTBR=reviewer@example.com\nBUG=\nR=another@example.com')
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000491 self._run_reviewer_test(
492 [],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000493 'desc\n\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000494 description,
495 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000496 ['--reviewers=another@example.com,reviewer@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000497
maruel@chromium.orga3353652011-11-30 14:26:57 +0000498 def test_reviewer_send_mail(self):
499 # --send-mail can be used without -r if R= is used
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000500 description = 'Foo Bar\nR=reviewer@example.com'
maruel@chromium.orga3353652011-11-30 14:26:57 +0000501 self._run_reviewer_test(
502 ['--send-mail'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000503 'desc\n\nBUG=',
maruel@chromium.orga3353652011-11-30 14:26:57 +0000504 description.strip('\n'),
505 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000506 ['--reviewers=reviewer@example.com', '--send_mail'])
maruel@chromium.orga3353652011-11-30 14:26:57 +0000507
508 def test_reviewer_send_mail_no_rev(self):
509 # Fails without a reviewer.
maruel@chromium.org2e23ce32013-05-07 12:42:28 +0000510 stdout = StringIO.StringIO()
511 stderr = StringIO.StringIO()
maruel@chromium.orga3353652011-11-30 14:26:57 +0000512 try:
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000513 self.calls = self._upload_no_rev_calls(None, None)
514 def RunEditor(desc, _, **kwargs):
maruel@chromium.orga3353652011-11-30 14:26:57 +0000515 return desc
516 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor)
maruel@chromium.org2e23ce32013-05-07 12:42:28 +0000517 self.mock(sys, 'stdout', stdout)
518 self.mock(sys, 'stderr', stderr)
maruel@chromium.orga3353652011-11-30 14:26:57 +0000519 git_cl.main(['upload', '--send-mail'])
520 self.fail()
521 except SystemExit:
maruel@chromium.org2e23ce32013-05-07 12:42:28 +0000522 self.assertEqual(
523 'Using 50% similarity for rename/copy detection. Override with '
524 '--similarity.\n',
525 stdout.getvalue())
526 self.assertEqual(
527 'Must specify reviewers to send email.\n', stderr.getvalue())
maruel@chromium.orga3353652011-11-30 14:26:57 +0000528
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000529 def test_dcommit(self):
530 self.calls = (
531 self._dcommit_calls_1() +
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000532 self._git_sanity_checks('fake_ancestor_sha', 'working') +
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000533 self._dcommit_calls_normal() +
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000534 self._dcommit_calls_3())
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000535 git_cl.main(['dcommit'])
536
537 def test_dcommit_bypass_hooks(self):
538 self.calls = (
539 self._dcommit_calls_1() +
540 self._dcommit_calls_bypassed() +
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000541 self._dcommit_calls_3())
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000542 git_cl.main(['dcommit', '--bypass-hooks'])
543
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000544
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000545 @classmethod
546 def _gerrit_base_calls(cls):
ukai@chromium.orge8077812012-02-03 03:41:46 +0000547 return [
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000548 ((['git', 'config', 'rietveld.autoupdate'],),
549 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000550 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000551 'config', 'rietveld.server'],), 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000552 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
553 ((['git', 'config', '--int', '--get',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000554 'branch.master.git-cl-similarity'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000555 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
556 ((['git', 'config', '--int', '--get',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000557 'branch.master.git-find-copies'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000558 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
559 ((['git', 'config', 'branch.master.merge'],), 'master'),
560 ((['git', 'config', 'branch.master.remote'],), 'origin'),
iannucci@chromium.org9e849272014-04-04 00:31:55 +0000561 ((['get_or_create_merge_base', 'master', 'master'],),
562 'fake_ancestor_sha'),
vadimsh@chromium.orgeed4df32015-04-10 21:30:20 +0000563 ((['git', 'config', 'gerrit.host'],), 'gerrit.example.com'),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000564 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000565 ((['git', 'rev-parse', '--show-cdup'],), ''),
566 ((['git', 'rev-parse', 'HEAD'],), '12345'),
567 ((['git',
mcgrathr@chromium.org9249f642013-06-03 21:36:18 +0000568 'diff', '--name-status', '--no-renames', '-r',
569 'fake_ancestor_sha...', '.'],),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000570 'M\t.gitignore\n'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000571 ((['git', 'config', 'branch.master.rietveldissue'],), ''),
572 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000573 'config', 'branch.master.rietveldpatchset'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000574 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000575 'log', '--pretty=format:%s%n%n%b', 'fake_ancestor_sha...'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000576 'foo'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000577 ((['git', 'config', 'user.email'],), 'me@example.com'),
578 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000579 'diff', '--no-ext-diff', '--stat', '--find-copies-harder',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000580 '-l100000', '-C50', 'fake_ancestor_sha', 'HEAD'],),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000581 '+dat'),
582 ]
583
584 @staticmethod
luqui@chromium.org609f3952015-05-04 22:47:04 +0000585 def _gerrit_upload_calls(description, reviewers, squash,
586 expected_upstream_ref='origin/refs/heads/master'):
ukai@chromium.orge8077812012-02-03 03:41:46 +0000587 calls = [
bauerb@chromium.org54b400c2016-01-14 10:08:25 +0000588 ((['git', 'config', '--bool', 'gerrit.squash-uploads'],), 'false'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000589 ((['git', 'log', '--pretty=format:%s\n\n%b',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000590 'fake_ancestor_sha..HEAD'],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000591 description)
592 ]
593 if git_cl.CHANGE_ID not in description:
594 calls += [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000595 ((['git', 'log', '--pretty=format:%s\n\n%b',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000596 'fake_ancestor_sha..HEAD'],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000597 description),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000598 ((['git', 'commit', '--amend', '-m', description],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000599 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000600 ((['git', 'log', '--pretty=format:%s\n\n%b',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000601 'fake_ancestor_sha..HEAD'],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000602 description)
603 ]
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000604 if squash:
605 ref_to_push = 'abcdef0123456789'
606 calls += [
607 ((['git', 'show', '--format=%s\n\n%b', '-s',
608 'refs/heads/git_cl_uploads/master'],),
609 (description, 0)),
610 ((['git', 'config', 'branch.master.merge'],),
611 'refs/heads/master'),
612 ((['git', 'config', 'branch.master.remote'],),
613 'origin'),
614 ((['get_or_create_merge_base', 'master', 'master'],),
615 'origin/master'),
616 ((['git', 'rev-parse', 'HEAD:'],),
617 '0123456789abcdef'),
618 ((['git', 'commit-tree', '0123456789abcdef', '-p',
619 'origin/master', '-m', 'd'],),
620 ref_to_push),
621 ]
622 else:
623 ref_to_push = 'HEAD'
624
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000625 calls += [
luqui@chromium.org609f3952015-05-04 22:47:04 +0000626 ((['git', 'rev-list',
627 expected_upstream_ref + '..' + ref_to_push],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000628 ((['git', 'config', 'rietveld.cc'],), '')
ukai@chromium.orge8077812012-02-03 03:41:46 +0000629 ]
ukai@chromium.org19bbfa22012-02-03 16:18:11 +0000630 receive_pack = '--receive-pack=git receive-pack '
ukai@chromium.orge8077812012-02-03 03:41:46 +0000631 receive_pack += '--cc=joe@example.com' # from watch list
632 if reviewers:
633 receive_pack += ' '
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000634 receive_pack += ' '.join(
635 '--reviewer=' + email for email in sorted(reviewers))
ukai@chromium.org19bbfa22012-02-03 16:18:11 +0000636 receive_pack += ''
ukai@chromium.orge8077812012-02-03 03:41:46 +0000637 calls += [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000638 ((['git',
luqui@chromium.org609f3952015-05-04 22:47:04 +0000639 'push', receive_pack, 'origin',
640 ref_to_push + ':refs/for/refs/heads/master'],),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000641 '')
642 ]
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000643 if squash:
644 calls += [
645 ((['git', 'rev-parse', 'HEAD'],), 'abcdef0123456789'),
646 ((['git', 'update-ref', '-m', 'Uploaded abcdef0123456789',
647 'refs/heads/git_cl_uploads/master', 'abcdef0123456789'],),
648 '')
649 ]
650
ukai@chromium.orge8077812012-02-03 03:41:46 +0000651 return calls
652
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000653 def _run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000654 self,
655 upload_args,
656 description,
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000657 reviewers,
luqui@chromium.org609f3952015-05-04 22:47:04 +0000658 squash=False,
659 expected_upstream_ref='origin/refs/heads/master'):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000660 """Generic gerrit upload test framework."""
ukai@chromium.orge8077812012-02-03 03:41:46 +0000661 self.calls = self._gerrit_base_calls()
luqui@chromium.org609f3952015-05-04 22:47:04 +0000662 self.calls += self._gerrit_upload_calls(
663 description, reviewers, squash,
664 expected_upstream_ref=expected_upstream_ref)
ukai@chromium.orge8077812012-02-03 03:41:46 +0000665 git_cl.main(['upload'] + upload_args)
666
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000667 def test_gerrit_upload_without_change_id(self):
668 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000669 [],
jamesr@chromium.org35d1a842012-07-27 00:20:43 +0000670 'desc\n\nBUG=\n',
ukai@chromium.orge8077812012-02-03 03:41:46 +0000671 [])
672
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000673 def test_gerrit_no_reviewer(self):
674 self._run_gerrit_upload_test(
675 [],
676 'desc\n\nBUG=\nChange-Id:123456789\n',
677 [])
678
ukai@chromium.orge8077812012-02-03 03:41:46 +0000679 def test_gerrit_reviewers_cmd_line(self):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000680 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000681 ['-r', 'foo@example.com'],
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000682 'desc\n\nBUG=\nChange-Id:123456789',
ukai@chromium.orge8077812012-02-03 03:41:46 +0000683 ['foo@example.com'])
684
685 def test_gerrit_reviewer_multiple(self):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000686 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000687 [],
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000688 'desc\nTBR=reviewer@example.com\nBUG=\nR=another@example.com\n'
689 'Change-Id:123456789\n',
ukai@chromium.orge8077812012-02-03 03:41:46 +0000690 ['reviewer@example.com', 'another@example.com'])
691
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000692 def test_gerrit_upload_squash(self):
693 self._run_gerrit_upload_test(
694 ['--squash'],
695 'desc\n\nBUG=\nChange-Id:123456789\n',
696 [],
luqui@chromium.org609f3952015-05-04 22:47:04 +0000697 squash=True,
698 expected_upstream_ref='origin/master')
ukai@chromium.orge8077812012-02-03 03:41:46 +0000699
rmistry@google.com2dd99862015-06-22 12:22:18 +0000700 def test_upload_branch_deps(self):
701 def mock_run_git(*args, **_kwargs):
702 if args[0] == ['for-each-ref',
703 '--format=%(refname:short) %(upstream:short)',
704 'refs/heads']:
705 # Create a local branch dependency tree that looks like this:
706 # test1 -> test2 -> test3 -> test4 -> test5
707 # -> test3.1
708 # test6 -> test0
709 branch_deps = [
710 'test2 test1', # test1 -> test2
711 'test3 test2', # test2 -> test3
712 'test3.1 test2', # test2 -> test3.1
713 'test4 test3', # test3 -> test4
714 'test5 test4', # test4 -> test5
715 'test6 test0', # test0 -> test6
716 'test7', # test7
717 ]
718 return '\n'.join(branch_deps)
719 self.mock(git_cl, 'RunGit', mock_run_git)
720
721 class RecordCalls:
722 times_called = 0
723 record_calls = RecordCalls()
724 def mock_CMDupload(*args, **_kwargs):
725 record_calls.times_called += 1
726 return 0
727 self.mock(git_cl, 'CMDupload', mock_CMDupload)
728
729 self.calls = [
730 (('[Press enter to continue or ctrl-C to quit]',), ''),
731 ]
732
733 class MockChangelist():
734 def __init__(self):
735 pass
736 def GetBranch(self):
737 return 'test1'
738 def GetIssue(self):
739 return '123'
740 def GetPatchset(self):
741 return '1001'
742
743 ret = git_cl.upload_branch_deps(MockChangelist(), [])
744 # CMDupload should have been called 5 times because of 5 dependent branches.
745 self.assertEquals(5, record_calls.times_called)
746 self.assertEquals(0, ret)
747
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000748 def test_config_gerrit_download_hook(self):
749 self.mock(git_cl, 'FindCodereviewSettingsFile', CodereviewSettingsFileMock)
750 def ParseCodereviewSettingsContent(content):
751 keyvals = {}
752 keyvals['CODE_REVIEW_SERVER'] = 'gerrit.chromium.org'
753 keyvals['GERRIT_HOST'] = 'gerrit.chromium.org'
754 keyvals['GERRIT_PORT'] = '29418'
755 return keyvals
756 self.mock(git_cl.gclient_utils, 'ParseCodereviewSettingsContent',
757 ParseCodereviewSettingsContent)
758 self.mock(git_cl.os, 'access', self._mocked_call)
759 self.mock(git_cl.os, 'chmod', self._mocked_call)
ukai@chromium.org91655502012-05-25 01:46:07 +0000760 src_dir = os.path.join(os.path.sep, 'usr', 'local', 'src')
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000761 def AbsPath(path):
ukai@chromium.org91655502012-05-25 01:46:07 +0000762 if not path.startswith(os.path.sep):
763 return os.path.join(src_dir, path)
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000764 return path
765 self.mock(git_cl.os.path, 'abspath', AbsPath)
ukai@chromium.org91655502012-05-25 01:46:07 +0000766 commit_msg_path = os.path.join(src_dir, '.git', 'hooks', 'commit-msg')
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000767 def Exists(path):
ukai@chromium.org91655502012-05-25 01:46:07 +0000768 if path == commit_msg_path:
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000769 return False
770 # others paths, such as /usr/share/locale/....
771 return True
772 self.mock(git_cl.os.path, 'exists', Exists)
joshua.lock@intel.com426f69b2012-08-02 23:41:49 +0000773 self.mock(git_cl, 'urlretrieve', self._mocked_call)
ukai@chromium.org712d6102013-11-27 00:52:58 +0000774 self.mock(git_cl, 'hasSheBang', self._mocked_call)
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000775 self.calls = [
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000776 ((['git', 'config', 'rietveld.autoupdate'],),
777 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000778 ((['git', 'config', 'rietveld.server',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000779 'gerrit.chromium.org'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000780 ((['git', 'config', '--unset-all', 'rietveld.cc'],), ''),
781 ((['git', 'config', '--unset-all',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000782 'rietveld.private'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000783 ((['git', 'config', '--unset-all',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000784 'rietveld.tree-status-url'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000785 ((['git', 'config', '--unset-all',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000786 'rietveld.viewvc-url'],), ''),
rmistry@google.com90752582014-01-14 21:04:50 +0000787 ((['git', 'config', '--unset-all',
788 'rietveld.bug-prefix'],), ''),
thestig@chromium.org44202a22014-03-11 19:22:18 +0000789 ((['git', 'config', '--unset-all',
790 'rietveld.cpplint-regex'],), ''),
791 ((['git', 'config', '--unset-all',
kjellander@chromium.org6abc6522014-12-02 07:34:49 +0000792 'rietveld.force-https-commit-url'],), ''),
793 ((['git', 'config', '--unset-all',
thestig@chromium.org44202a22014-03-11 19:22:18 +0000794 'rietveld.cpplint-ignore-regex'],), ''),
sheyang@chromium.org152cf832014-06-11 21:37:49 +0000795 ((['git', 'config', '--unset-all',
796 'rietveld.project'],), ''),
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +0000797 ((['git', 'config', '--unset-all',
798 'rietveld.pending-ref-prefix'],), ''),
rmistry@google.com5626a922015-02-26 14:03:30 +0000799 ((['git', 'config', '--unset-all',
800 'rietveld.run-post-upload-hook'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000801 ((['git', 'config', 'gerrit.host',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000802 'gerrit.chromium.org'],), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000803 # DownloadHooks(False)
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000804 ((['git', 'config', 'gerrit.host'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000805 'gerrit.chromium.org'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000806 ((['git', 'rev-parse', '--show-cdup'],), ''),
ukai@chromium.org91655502012-05-25 01:46:07 +0000807 ((commit_msg_path, os.X_OK,), False),
ukai@chromium.org712d6102013-11-27 00:52:58 +0000808 (('https://gerrit-review.googlesource.com/tools/hooks/commit-msg',
ukai@chromium.org91655502012-05-25 01:46:07 +0000809 commit_msg_path,), ''),
ukai@chromium.org712d6102013-11-27 00:52:58 +0000810 ((commit_msg_path,), True),
ukai@chromium.org91655502012-05-25 01:46:07 +0000811 ((commit_msg_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR,), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000812 # GetCodereviewSettingsInteractively
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000813 ((['git', 'config', 'rietveld.server'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000814 'gerrit.chromium.org'),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000815 (('Rietveld server (host[:port]) [https://gerrit.chromium.org]:',),
816 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000817 ((['git', 'config', 'rietveld.cc'],), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000818 (('CC list:',), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000819 ((['git', 'config', 'rietveld.private'],), ''),
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000820 (('Private flag (rietveld only):',), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000821 ((['git', 'config', 'rietveld.tree-status-url'],), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000822 (('Tree status URL:',), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000823 ((['git', 'config', 'rietveld.viewvc-url'],), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000824 (('ViewVC URL:',), ''),
825 # DownloadHooks(True)
rmistry@google.com90752582014-01-14 21:04:50 +0000826 ((['git', 'config', 'rietveld.bug-prefix'],), ''),
827 (('Bug Prefix:',), ''),
rmistry@google.com5626a922015-02-26 14:03:30 +0000828 ((['git', 'config', 'rietveld.run-post-upload-hook'],), ''),
829 (('Run Post Upload Hook:',), ''),
ukai@chromium.org91655502012-05-25 01:46:07 +0000830 ((commit_msg_path, os.X_OK,), True),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000831 ]
832 git_cl.main(['config'])
833
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000834 def test_update_reviewers(self):
835 data = [
836 ('foo', [], 'foo'),
agable@chromium.org42c20792013-09-12 17:34:49 +0000837 ('foo\nR=xx', [], 'foo\nR=xx'),
838 ('foo\nTBR=xx', [], 'foo\nTBR=xx'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000839 ('foo', ['a@c'], 'foo\n\nR=a@c'),
agable@chromium.org42c20792013-09-12 17:34:49 +0000840 ('foo\nR=xx', ['a@c'], 'foo\n\nR=a@c, xx'),
841 ('foo\nTBR=xx', ['a@c'], 'foo\n\nR=a@c\nTBR=xx'),
842 ('foo\nTBR=xx\nR=yy', ['a@c'], 'foo\n\nR=a@c, yy\nTBR=xx'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000843 ('foo\nBUG=', ['a@c'], 'foo\nBUG=\nR=a@c'),
agable@chromium.org42c20792013-09-12 17:34:49 +0000844 ('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 +0000845 ('foo', ['a@c', 'b@c'], 'foo\n\nR=a@c, b@c'),
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +0000846 ('foo\nBar\n\nR=\nBUG=', ['c@c'], 'foo\nBar\n\nR=c@c\nBUG='),
847 ('foo\nBar\n\nR=\nBUG=\nR=', ['c@c'], 'foo\nBar\n\nR=c@c\nBUG='),
848 # Same as the line before, but full of whitespaces.
849 (
850 'foo\nBar\n\n R = \n BUG = \n R = ', ['c@c'],
851 'foo\nBar\n\nR=c@c\n BUG =',
852 ),
853 # Whitespaces aren't interpreted as new lines.
854 ('foo BUG=allo R=joe ', ['c@c'], 'foo BUG=allo R=joe\n\nR=c@c'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000855 ]
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +0000856 expected = [i[2] for i in data]
857 actual = []
858 for orig, reviewers, _expected in data:
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000859 obj = git_cl.ChangeDescription(orig)
860 obj.update_reviewers(reviewers)
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +0000861 actual.append(obj.description)
862 self.assertEqual(expected, actual)
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000863
wittman@chromium.org455dc922015-01-26 20:15:50 +0000864 def test_get_target_ref(self):
865 # Check remote or remote branch not present.
866 self.assertEqual(None, git_cl.GetTargetRef('origin', None, 'master', None))
867 self.assertEqual(None, git_cl.GetTargetRef(None,
868 'refs/remotes/origin/master',
869 'master', None))
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000870
wittman@chromium.org455dc922015-01-26 20:15:50 +0000871 # Check default target refs for branches.
872 self.assertEqual('refs/heads/master',
873 git_cl.GetTargetRef('origin', 'refs/remotes/origin/master',
874 None, None))
875 self.assertEqual('refs/heads/master',
876 git_cl.GetTargetRef('origin', 'refs/remotes/origin/lkgr',
877 None, None))
878 self.assertEqual('refs/heads/master',
879 git_cl.GetTargetRef('origin', 'refs/remotes/origin/lkcr',
880 None, None))
881 self.assertEqual('refs/branch-heads/123',
882 git_cl.GetTargetRef('origin',
883 'refs/remotes/branch-heads/123',
884 None, None))
885 self.assertEqual('refs/diff/test',
886 git_cl.GetTargetRef('origin',
887 'refs/remotes/origin/refs/diff/test',
888 None, None))
rmistry@google.comc68112d2015-03-03 12:48:06 +0000889 self.assertEqual('refs/heads/chrome/m42',
890 git_cl.GetTargetRef('origin',
891 'refs/remotes/origin/chrome/m42',
892 None, None))
wittman@chromium.org455dc922015-01-26 20:15:50 +0000893
894 # Check target refs for user-specified target branch.
895 for branch in ('branch-heads/123', 'remotes/branch-heads/123',
896 'refs/remotes/branch-heads/123'):
897 self.assertEqual('refs/branch-heads/123',
898 git_cl.GetTargetRef('origin',
899 'refs/remotes/origin/master',
900 branch, None))
901 for branch in ('origin/master', 'remotes/origin/master',
902 'refs/remotes/origin/master'):
903 self.assertEqual('refs/heads/master',
904 git_cl.GetTargetRef('origin',
905 'refs/remotes/branch-heads/123',
906 branch, None))
907 for branch in ('master', 'heads/master', 'refs/heads/master'):
908 self.assertEqual('refs/heads/master',
909 git_cl.GetTargetRef('origin',
910 'refs/remotes/branch-heads/123',
911 branch, None))
912
913 # Check target refs for pending prefix.
914 self.assertEqual('prefix/heads/master',
915 git_cl.GetTargetRef('origin', 'refs/remotes/origin/master',
916 None, 'prefix/'))
917
wychen@chromium.orga872e752015-04-28 23:42:18 +0000918 def test_patch_when_dirty(self):
919 # Patch when local tree is dirty
920 self.mock(git_common, 'is_dirty_git_tree', lambda x: True)
921 self.assertNotEqual(git_cl.main(['patch', '123456']), 0)
922
923 def test_diff_when_dirty(self):
924 # Do 'git cl diff' when local tree is dirty
925 self.mock(git_common, 'is_dirty_git_tree', lambda x: True)
926 self.assertNotEqual(git_cl.main(['diff']), 0)
927
928 def _patch_common(self):
929 self.mock(git_cl.Changelist, 'GetMostRecentPatchset', lambda x: '60001')
930 self.mock(git_cl.Changelist, 'GetPatchSetDiff', lambda *args: None)
wychen@chromium.org5b3bebb2015-05-28 21:41:43 +0000931 self.mock(git_cl.Changelist, 'GetDescription', lambda *args: 'Description')
wychen@chromium.orga872e752015-04-28 23:42:18 +0000932 self.mock(git_cl.Changelist, 'SetIssue', lambda *args: None)
933 self.mock(git_cl.Changelist, 'SetPatchset', lambda *args: None)
934 self.mock(git_cl, 'IsGitVersionAtLeast', lambda *args: True)
935
936 self.calls = [
937 ((['git', 'config', 'rietveld.autoupdate'],), ''),
938 ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'),
939 ((['git', 'rev-parse', '--show-cdup'],), ''),
940 ((['sed', '-e', 's|^--- a/|--- |; s|^+++ b/|+++ |'],), ''),
941 ]
942
943 def test_patch_successful(self):
944 self._patch_common()
945 self.calls += [
946 ((['git', 'apply', '--index', '-p0', '--3way'],), ''),
947 ((['git', 'commit', '-m',
wychen@chromium.org5b3bebb2015-05-28 21:41:43 +0000948 'Description\n\n' +
wychen@chromium.orga872e752015-04-28 23:42:18 +0000949 'patch from issue 123456 at patchset 60001 ' +
950 '(http://crrev.com/123456#ps60001)'],), ''),
951 ]
952 self.assertEqual(git_cl.main(['patch', '123456']), 0)
953
954 def test_patch_conflict(self):
955 self._patch_common()
956 self.calls += [
957 ((['git', 'apply', '--index', '-p0', '--3way'],), '',
958 subprocess2.CalledProcessError(1, '', '', '', '')),
959 ]
960 self.assertNotEqual(git_cl.main(['patch', '123456']), 0)
wittman@chromium.org455dc922015-01-26 20:15:50 +0000961
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000962if __name__ == '__main__':
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000963 git_cl.logging.basicConfig(
964 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000965 unittest.main()