blob: b034480afc1d28e2638a5ad9604f88df0ce45547 [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
tandrii@chromium.org57d86542016-03-04 16:11:32 +000020import git_footers
maruel@chromium.orgddd59412011-11-30 14:20:38 +000021import subprocess2
maruel@chromium.orgddd59412011-11-30 14:20:38 +000022
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000023class PresubmitMock(object):
24 def __init__(self, *args, **kwargs):
25 self.reviewers = []
26 @staticmethod
27 def should_continue():
28 return True
29
30
31class RietveldMock(object):
32 def __init__(self, *args, **kwargs):
33 pass
maruel@chromium.org78936cb2013-04-11 00:17:52 +000034
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000035 @staticmethod
36 def get_description(issue):
37 return 'Issue: %d' % issue
38
maruel@chromium.org78936cb2013-04-11 00:17:52 +000039 @staticmethod
40 def get_issue_properties(_issue, _messages):
41 return {
42 'reviewers': ['joe@chromium.org', 'john@chromium.org'],
43 'messages': [
44 {
45 'approval': True,
46 'sender': 'john@chromium.org',
47 },
48 ],
49 }
50
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000051
52class WatchlistsMock(object):
53 def __init__(self, _):
54 pass
55 @staticmethod
56 def GetWatchersForPaths(_):
57 return ['joe@example.com']
58
59
ukai@chromium.org78c4b982012-02-14 02:20:26 +000060class CodereviewSettingsFileMock(object):
61 def __init__(self):
62 pass
63 # pylint: disable=R0201
64 def read(self):
65 return ("CODE_REVIEW_SERVER: gerrit.chromium.org\n" +
andybons@chromium.org11f46eb2016-02-02 19:26:51 +000066 "GERRIT_HOST: True\n")
ukai@chromium.org78c4b982012-02-14 02:20:26 +000067
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)
tandrii@chromium.orga342c922016-03-16 07:08:25 +000085 self.mock(git_cl.gclient_utils, 'CheckCallAndFilter', self._mocked_call)
sbc@chromium.org71437c02015-04-09 19:29:40 +000086 self.mock(git_common, 'is_dirty_git_tree', lambda x: False)
iannucci@chromium.org9e849272014-04-04 00:31:55 +000087 self.mock(git_common, 'get_or_create_merge_base',
88 lambda *a: (
89 self._mocked_call(['get_or_create_merge_base']+list(a))))
pgervais@chromium.org8ba38ff2015-06-11 21:41:25 +000090 self.mock(git_cl, 'BranchExists', lambda _: True)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000091 self.mock(git_cl, 'FindCodereviewSettingsFile', lambda: '')
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000092 self.mock(git_cl, 'ask_for_data', self._mocked_call)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000093 self.mock(git_cl.presubmit_support, 'DoPresubmitChecks', PresubmitMock)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000094 self.mock(git_cl.rietveld, 'Rietveld', RietveldMock)
maruel@chromium.org4bac4b52012-11-27 20:33:52 +000095 self.mock(git_cl.rietveld, 'CachingRietveld', RietveldMock)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000096 self.mock(git_cl.upload, 'RealMain', self.fail)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000097 self.mock(git_cl.watchlists, 'Watchlists', WatchlistsMock)
vadimsh@chromium.orgeed4df32015-04-10 21:30:20 +000098 self.mock(git_cl.auth, 'get_authenticator_for_host', AuthenticatorMock)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000099 # It's important to reset settings to not have inter-tests interference.
100 git_cl.settings = None
101
102 def tearDown(self):
wychen@chromium.org445c8962015-04-28 23:30:05 +0000103 try:
104 if not self.has_failed():
105 self.assertEquals([], self.calls)
106 finally:
107 super(TestGitCl, self).tearDown()
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000108
iannucci@chromium.org9e849272014-04-04 00:31:55 +0000109 def _mocked_call(self, *args, **_kwargs):
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000110 self.assertTrue(
111 self.calls,
112 '@%d Expected: <Missing> Actual: %r' % (self._calls_done, args))
wychen@chromium.orga872e752015-04-28 23:42:18 +0000113 top = self.calls.pop(0)
114 if len(top) > 2 and top[2]:
115 raise top[2]
116 expected_args, result = top
117
maruel@chromium.orge52678e2013-04-26 18:34:44 +0000118 # Also logs otherwise it could get caught in a try/finally and be hard to
119 # diagnose.
120 if expected_args != args:
121 msg = '@%d Expected: %r Actual: %r' % (
122 self._calls_done, expected_args, args)
123 git_cl.logging.error(msg)
124 self.fail(msg)
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000125 self._calls_done += 1
126 return result
127
maruel@chromium.orga3353652011-11-30 14:26:57 +0000128 @classmethod
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000129 def _upload_calls(cls, similarity, find_copies, private):
iannucci@chromium.org79540052012-10-19 23:15:26 +0000130 return (cls._git_base_calls(similarity, find_copies) +
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000131 cls._git_upload_calls(private))
maruel@chromium.orga3353652011-11-30 14:26:57 +0000132
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000133 @classmethod
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000134 def _upload_no_rev_calls(cls, similarity, find_copies):
135 return (cls._git_base_calls(similarity, find_copies) +
136 cls._git_upload_no_rev_calls())
137
138 @classmethod
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000139 def _git_base_calls(cls, similarity, find_copies):
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000140 if similarity is None:
141 similarity = '50'
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000142 similarity_call = ((['git', 'config', '--int', '--get',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000143 'branch.master.git-cl-similarity'],), '')
144 else:
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000145 similarity_call = ((['git', 'config', '--int',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000146 'branch.master.git-cl-similarity', similarity],), '')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000147
148 if find_copies is None:
149 find_copies = True
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000150 find_copies_call = ((['git', 'config', '--int', '--get',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000151 'branch.master.git-find-copies'],), '')
152 else:
153 val = str(int(find_copies))
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000154 find_copies_call = ((['git', 'config', '--int',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000155 'branch.master.git-find-copies', val],), '')
156
157 if find_copies:
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000158 stat_call = ((['git', 'diff', '--no-ext-diff', '--stat',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000159 '--find-copies-harder', '-l100000', '-C'+similarity,
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000160 'fake_ancestor_sha', 'HEAD'],), '+dat')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000161 else:
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000162 stat_call = ((['git', 'diff', '--no-ext-diff', '--stat',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000163 '-M'+similarity, 'fake_ancestor_sha', 'HEAD'],), '+dat')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000164
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000165 return [
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000166 ((['git', 'config', 'rietveld.autoupdate'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000167 ((['git', 'config', 'rietveld.server'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000168 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000169 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000170 similarity_call,
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000171 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
iannucci@chromium.org79540052012-10-19 23:15:26 +0000172 find_copies_call,
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000173 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
174 ((['git', 'config', 'branch.master.merge'],), 'master'),
175 ((['git', 'config', 'branch.master.remote'],), 'origin'),
iannucci@chromium.org9e849272014-04-04 00:31:55 +0000176 ((['get_or_create_merge_base', 'master', 'master'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000177 'fake_ancestor_sha'),
vadimsh@chromium.orgeed4df32015-04-10 21:30:20 +0000178 ((['git', 'config', 'gerrit.host'],), ''),
vadimsh@chromium.org19f3fe62015-04-20 17:03:10 +0000179 ((['git', 'config', 'branch.master.rietveldissue'],), ''),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000180 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000181 ((['git', 'rev-parse', '--show-cdup'],), ''),
182 ((['git', 'rev-parse', 'HEAD'],), '12345'),
183 ((['git', 'diff', '--name-status', '--no-renames', '-r',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000184 'fake_ancestor_sha...', '.'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000185 'M\t.gitignore\n'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000186 ((['git', 'config', 'branch.master.rietveldpatchset'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000187 ''),
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...'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000190 'foo'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000191 ((['git', 'config', 'user.email'],), 'me@example.com'),
iannucci@chromium.org79540052012-10-19 23:15:26 +0000192 stat_call,
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000193 ((['git', 'log', '--pretty=format:%s\n\n%b',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000194 'fake_ancestor_sha..HEAD'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000195 'desc\n'),
rmistry@google.com90752582014-01-14 21:04:50 +0000196 ((['git', 'config', 'rietveld.bug-prefix'],), ''),
maruel@chromium.orga3353652011-11-30 14:26:57 +0000197 ]
198
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000199 @classmethod
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000200 def _git_upload_no_rev_calls(cls):
201 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000202 ((['git', 'config', 'core.editor'],), ''),
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000203 ]
204
205 @classmethod
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000206 def _git_upload_calls(cls, private):
207 if private:
tyoshino@chromium.org99918ab2013-09-30 06:17:28 +0000208 cc_call = []
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000209 private_call = []
210 else:
tyoshino@chromium.org99918ab2013-09-30 06:17:28 +0000211 cc_call = [((['git', 'config', 'rietveld.cc'],), '')]
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000212 private_call = [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000213 ((['git', 'config', 'rietveld.private'],), '')]
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000214
maruel@chromium.orga3353652011-11-30 14:26:57 +0000215 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000216 ((['git', 'config', 'core.editor'],), ''),
tyoshino@chromium.org99918ab2013-09-30 06:17:28 +0000217 ] + cc_call + private_call + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000218 ((['git', 'config', 'branch.master.base-url'],), ''),
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +0000219 ((['git', 'config', 'rietveld.pending-ref-prefix'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000220 ((['git',
bratell@opera.com05fb9112014-07-07 09:30:23 +0000221 'config', '--local', '--get-regexp', '^svn-remote\\.'],),
222 (('', None), 0)),
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000223 ((['git', 'rev-parse', '--show-cdup'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000224 ((['git', 'svn', 'info'],), ''),
sheyang@chromium.org152cf832014-06-11 21:37:49 +0000225 ((['git', 'config', 'rietveld.project'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000226 ((['git',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000227 'config', 'branch.master.rietveldissue', '1'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000228 ((['git', 'config', 'branch.master.rietveldserver',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000229 'https://codereview.example.com'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000230 ((['git',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000231 'config', 'branch.master.rietveldpatchset', '2'],), ''),
tandrii@chromium.org1e67bb72016-02-11 12:15:49 +0000232 ] + cls._git_post_upload_calls()
233
234 @classmethod
235 def _git_post_upload_calls(cls):
236 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000237 ((['git', 'rev-parse', 'HEAD'],), 'hash'),
238 ((['git', 'symbolic-ref', 'HEAD'],), 'hash'),
239 ((['git',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000240 'config', 'branch.hash.last-upload-hash', 'hash'],), ''),
rmistry@google.com5626a922015-02-26 14:03:30 +0000241 ((['git', 'config', 'rietveld.run-post-upload-hook'],), ''),
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000242 ]
243
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000244 @staticmethod
245 def _git_sanity_checks(diff_base, working_branch):
246 fake_ancestor = 'fake_ancestor'
247 fake_cl = 'fake_cl_for_patch'
248 return [
249 # Calls to verify branch point is ancestor
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000250 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000251 'rev-parse', '--verify', diff_base],), fake_ancestor),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000252 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000253 'merge-base', fake_ancestor, 'HEAD'],), fake_ancestor),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000254 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000255 'rev-list', '^' + fake_ancestor, 'HEAD'],), fake_cl),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000256 # Mock a config miss (error code 1)
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000257 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000258 'config', 'gitcl.remotebranch'],), (('', None), 1)),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000259 # Call to GetRemoteBranch()
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000260 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000261 'config', 'branch.%s.merge' % working_branch],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000262 'refs/heads/master'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000263 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000264 'config', 'branch.%s.remote' % working_branch],), 'origin'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000265 ((['git', 'rev-list', '^' + fake_ancestor,
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000266 'refs/remotes/origin/master'],), ''),
267 ]
268
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000269 @classmethod
270 def _dcommit_calls_1(cls):
271 return [
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +0000272 ((['git', 'config', 'rietveld.autoupdate'],),
273 ''),
274 ((['git', 'config', 'rietveld.pending-ref-prefix'],),
275 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000276 ((['git',
bratell@opera.com05fb9112014-07-07 09:30:23 +0000277 'config', '--local', '--get-regexp', '^svn-remote\\.'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000278 ((('svn-remote.svn.url svn://svn.chromium.org/chrome\n'
279 'svn-remote.svn.fetch trunk/src:refs/remotes/origin/master'),
280 None),
281 0)),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000282 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000283 'config', 'rietveld.server'],), 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000284 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
285 ((['git', 'config', '--int', '--get',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000286 'branch.working.git-cl-similarity'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000287 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
288 ((['git', 'config', '--int', '--get',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000289 'branch.working.git-find-copies'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000290 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
291 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000292 'config', 'branch.working.merge'],), 'refs/heads/master'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000293 ((['git', 'config', 'branch.working.remote'],), 'origin'),
iannucci@chromium.org5724c962014-04-11 09:32:56 +0000294 ((['git', 'config', 'branch.working.merge'],),
295 'refs/heads/master'),
296 ((['git', 'config', 'branch.working.remote'],), 'origin'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000297 ((['git', 'rev-list', '--merges',
szager@chromium.orge84b7542012-06-15 21:26:58 +0000298 '--grep=^SVN changes up to revision [0-9]*$',
szager@chromium.org9bb85e22012-06-13 20:28:23 +0000299 'refs/remotes/origin/master^!'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000300 ((['git', 'rev-list', '^refs/heads/working',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000301 'refs/remotes/origin/master'],),
302 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000303 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000304 'log', '--grep=^git-svn-id:', '-1', '--pretty=format:%H'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000305 '3fc18b62c4966193eb435baabe2d18a3810ec82e'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000306 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000307 'rev-list', '^3fc18b62c4966193eb435baabe2d18a3810ec82e',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000308 'refs/remotes/origin/master'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000309 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000310 'merge-base', 'refs/remotes/origin/master', 'HEAD'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000311 'fake_ancestor_sha'),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000312 ]
313
314 @classmethod
315 def _dcommit_calls_normal(cls):
316 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000317 ((['git', 'rev-parse', '--show-cdup'],), ''),
318 ((['git', 'rev-parse', 'HEAD'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000319 '00ff397798ea57439712ed7e04ab96e13969ef40'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000320 ((['git',
mcgrathr@chromium.org9249f642013-06-03 21:36:18 +0000321 'diff', '--name-status', '--no-renames', '-r', 'fake_ancestor_sha...',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000322 '.'],),
323 'M\tPRESUBMIT.py'),
tandrii@chromium.orga342c922016-03-16 07:08:25 +0000324 ((['git', 'config', 'gerrit.host'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000325 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000326 'config', 'branch.working.rietveldissue'],), '12345'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000327 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000328 'config', 'branch.working.rietveldpatchset'],), '31137'),
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'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000331 ((['git', 'config', 'user.email'],), 'author@example.com'),
332 ((['git', 'config', 'rietveld.tree-status-url'],), ''),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000333 ]
334
335 @classmethod
336 def _dcommit_calls_bypassed(cls):
337 return [
tandrii@chromium.orga342c922016-03-16 07:08:25 +0000338 ((['git', 'config', 'gerrit.host'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000339 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000340 'config', 'branch.working.rietveldissue'],), '12345'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000341 ((['git', 'config', 'branch.working.rietveldserver'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000342 'codereview.example.com'),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000343 ]
344
345 @classmethod
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000346 def _dcommit_calls_3(cls):
347 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000348 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000349 'diff', '--no-ext-diff', '--stat', '--find-copies-harder',
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000350 '-l100000', '-C50', 'fake_ancestor_sha',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000351 'refs/heads/working'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000352 (' PRESUBMIT.py | 2 +-\n'
353 ' 1 files changed, 1 insertions(+), 1 deletions(-)\n')),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000354 ((['git', 'show-ref', '--quiet', '--verify',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000355 'refs/heads/git-cl-commit'],),
356 (('', None), 0)),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000357 ((['git', 'branch', '-D', 'git-cl-commit'],), ''),
358 ((['git', 'show-ref', '--quiet', '--verify',
szager@chromium.org9bb85e22012-06-13 20:28:23 +0000359 'refs/heads/git-cl-cherry-pick'],), ''),
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000360 ((['git', 'rev-parse', '--show-cdup'],), '\n'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000361 ((['git', 'checkout', '-q', '-b', 'git-cl-commit'],), ''),
362 ((['git', 'reset', '--soft', 'fake_ancestor_sha'],), ''),
363 ((['git', 'commit', '-m',
maruel@chromium.orge52678e2013-04-26 18:34:44 +0000364 'Issue: 12345\n\nR=john@chromium.org\n\n'
sergiyb@chromium.org4b39c5f2015-07-07 10:33:12 +0000365 'Review URL: https://codereview.example.com/12345 .'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000366 ''),
kjellander@chromium.org6abc6522014-12-02 07:34:49 +0000367 ((['git', 'config', 'rietveld.force-https-commit-url'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000368 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000369 'svn', 'dcommit', '-C50', '--no-rebase', '--rmdir'],),
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000370 (('', None), 0)),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000371 ((['git', 'checkout', '-q', 'working'],), ''),
372 ((['git', 'branch', '-D', 'git-cl-commit'],), ''),
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000373 ]
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000374
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000375 @staticmethod
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000376 def _cmd_line(description, args, similarity, find_copies, private):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000377 """Returns the upload command line passed to upload.RealMain()."""
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000378 return [
379 'upload', '--assume_yes', '--server',
maruel@chromium.orgeb5edbc2012-01-16 17:03:28 +0000380 'https://codereview.example.com',
maruel@chromium.org71e12a92012-02-14 02:34:15 +0000381 '--message', description
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000382 ] + args + [
383 '--cc', 'joe@example.com',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000384 ] + (['--private'] if private else []) + [
iannucci@chromium.org79540052012-10-19 23:15:26 +0000385 '--git_similarity', similarity or '50'
386 ] + (['--git_no_find_copies'] if find_copies == False else []) + [
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000387 'fake_ancestor_sha', 'HEAD'
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000388 ]
389
390 def _run_reviewer_test(
391 self,
392 upload_args,
393 expected_description,
394 returned_description,
395 final_description,
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000396 reviewers,
397 private=False):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000398 """Generic reviewer test framework."""
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000399 try:
400 similarity = upload_args[upload_args.index('--similarity')+1]
401 except ValueError:
402 similarity = None
iannucci@chromium.org79540052012-10-19 23:15:26 +0000403
404 if '--find-copies' in upload_args:
405 find_copies = True
406 elif '--no-find-copies' in upload_args:
407 find_copies = False
408 else:
409 find_copies = None
410
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000411 private = '--private' in upload_args
412
413 self.calls = self._upload_calls(similarity, find_copies, private)
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000414
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000415 def RunEditor(desc, _, **kwargs):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000416 self.assertEquals(
417 '# Enter a description of the change.\n'
janx@chromium.org104b2db2013-04-18 12:58:40 +0000418 '# This will be displayed on the codereview site.\n'
alancutter@chromium.org63a4d7f2013-05-31 02:22:45 +0000419 '# The first line will also be used as the subject of the review.\n'
alancutter@chromium.orgbd1073e2013-06-01 00:34:38 +0000420 '#--------------------This line is 72 characters long'
421 '--------------------\n' +
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000422 expected_description,
423 desc)
424 return returned_description
425 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor)
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000426
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000427 def check_upload(args):
iannucci@chromium.org79540052012-10-19 23:15:26 +0000428 cmd_line = self._cmd_line(final_description, reviewers, similarity,
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000429 find_copies, private)
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000430 self.assertEquals(cmd_line, args)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000431 return 1, 2
432 self.mock(git_cl.upload, 'RealMain', check_upload)
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000433
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000434 git_cl.main(['upload'] + upload_args)
435
436 def test_no_reviewer(self):
437 self._run_reviewer_test(
438 [],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000439 'desc\n\nBUG=',
440 '# Blah blah comment.\ndesc\n\nBUG=',
441 'desc\n\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000442 [])
443
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000444 def test_keep_similarity(self):
445 self._run_reviewer_test(
446 ['--similarity', '70'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000447 'desc\n\nBUG=',
448 '# Blah blah comment.\ndesc\n\nBUG=',
449 'desc\n\nBUG=',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000450 [])
451
iannucci@chromium.org79540052012-10-19 23:15:26 +0000452 def test_keep_find_copies(self):
453 self._run_reviewer_test(
454 ['--no-find-copies'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000455 'desc\n\nBUG=',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000456 '# Blah blah comment.\ndesc\n\nBUG=\n',
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000457 'desc\n\nBUG=',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000458 [])
459
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000460 def test_private(self):
461 self._run_reviewer_test(
462 ['--private'],
463 'desc\n\nBUG=',
464 '# Blah blah comment.\ndesc\n\nBUG=\n',
465 'desc\n\nBUG=',
466 [])
467
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000468 def test_reviewers_cmd_line(self):
469 # Reviewer is passed as-is
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000470 description = 'desc\n\nR=foo@example.com\nBUG='
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000471 self._run_reviewer_test(
472 ['-r' 'foo@example.com'],
473 description,
474 '\n%s\n' % description,
475 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000476 ['--reviewers=foo@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000477
478 def test_reviewer_tbr_overriden(self):
479 # Reviewer is overriden with TBR
480 # Also verifies the regexp work without a trailing LF
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000481 description = 'Foo Bar\n\nTBR=reviewer@example.com'
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000482 self._run_reviewer_test(
483 ['-r' 'foo@example.com'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000484 'desc\n\nR=foo@example.com\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000485 description.strip('\n'),
486 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000487 ['--reviewers=reviewer@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000488
489 def test_reviewer_multiple(self):
490 # Handles multiple R= or TBR= lines.
491 description = (
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000492 'Foo Bar\nTBR=reviewer@example.com\nBUG=\nR=another@example.com')
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000493 self._run_reviewer_test(
494 [],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000495 'desc\n\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000496 description,
497 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000498 ['--reviewers=another@example.com,reviewer@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000499
maruel@chromium.orga3353652011-11-30 14:26:57 +0000500 def test_reviewer_send_mail(self):
501 # --send-mail can be used without -r if R= is used
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000502 description = 'Foo Bar\nR=reviewer@example.com'
maruel@chromium.orga3353652011-11-30 14:26:57 +0000503 self._run_reviewer_test(
504 ['--send-mail'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000505 'desc\n\nBUG=',
maruel@chromium.orga3353652011-11-30 14:26:57 +0000506 description.strip('\n'),
507 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000508 ['--reviewers=reviewer@example.com', '--send_mail'])
maruel@chromium.orga3353652011-11-30 14:26:57 +0000509
510 def test_reviewer_send_mail_no_rev(self):
511 # Fails without a reviewer.
maruel@chromium.org2e23ce32013-05-07 12:42:28 +0000512 stdout = StringIO.StringIO()
513 stderr = StringIO.StringIO()
maruel@chromium.orga3353652011-11-30 14:26:57 +0000514 try:
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000515 self.calls = self._upload_no_rev_calls(None, None)
516 def RunEditor(desc, _, **kwargs):
maruel@chromium.orga3353652011-11-30 14:26:57 +0000517 return desc
518 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor)
maruel@chromium.org2e23ce32013-05-07 12:42:28 +0000519 self.mock(sys, 'stdout', stdout)
520 self.mock(sys, 'stderr', stderr)
maruel@chromium.orga3353652011-11-30 14:26:57 +0000521 git_cl.main(['upload', '--send-mail'])
522 self.fail()
523 except SystemExit:
maruel@chromium.org2e23ce32013-05-07 12:42:28 +0000524 self.assertEqual(
525 'Using 50% similarity for rename/copy detection. Override with '
526 '--similarity.\n',
527 stdout.getvalue())
528 self.assertEqual(
529 'Must specify reviewers to send email.\n', stderr.getvalue())
maruel@chromium.orga3353652011-11-30 14:26:57 +0000530
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000531 def test_dcommit(self):
532 self.calls = (
533 self._dcommit_calls_1() +
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000534 self._git_sanity_checks('fake_ancestor_sha', 'working') +
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000535 self._dcommit_calls_normal() +
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000536 self._dcommit_calls_3())
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000537 git_cl.main(['dcommit'])
538
539 def test_dcommit_bypass_hooks(self):
540 self.calls = (
541 self._dcommit_calls_1() +
542 self._dcommit_calls_bypassed() +
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000543 self._dcommit_calls_3())
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000544 git_cl.main(['dcommit', '--bypass-hooks'])
545
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000546
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000547 @classmethod
548 def _gerrit_base_calls(cls):
ukai@chromium.orge8077812012-02-03 03:41:46 +0000549 return [
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000550 ((['git', 'config', 'rietveld.autoupdate'],),
551 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000552 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000553 'config', 'rietveld.server'],), 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000554 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
555 ((['git', 'config', '--int', '--get',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000556 'branch.master.git-cl-similarity'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000557 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
558 ((['git', 'config', '--int', '--get',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000559 'branch.master.git-find-copies'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000560 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
561 ((['git', 'config', 'branch.master.merge'],), 'master'),
562 ((['git', 'config', 'branch.master.remote'],), 'origin'),
iannucci@chromium.org9e849272014-04-04 00:31:55 +0000563 ((['get_or_create_merge_base', 'master', 'master'],),
564 'fake_ancestor_sha'),
andybons@chromium.org11f46eb2016-02-02 19:26:51 +0000565 ((['git', 'config', 'gerrit.host'],), 'True'),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000566 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000567 ((['git', 'rev-parse', '--show-cdup'],), ''),
568 ((['git', 'rev-parse', 'HEAD'],), '12345'),
569 ((['git',
mcgrathr@chromium.org9249f642013-06-03 21:36:18 +0000570 'diff', '--name-status', '--no-renames', '-r',
571 'fake_ancestor_sha...', '.'],),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000572 'M\t.gitignore\n'),
tandrii@chromium.orga342c922016-03-16 07:08:25 +0000573 ((['git', 'config', 'branch.master.gerritissue'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000574 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000575 'config', 'branch.master.rietveldpatchset'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000576 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000577 'log', '--pretty=format:%s%n%n%b', 'fake_ancestor_sha...'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000578 'foo'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000579 ((['git', 'config', 'user.email'],), 'me@example.com'),
580 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000581 'diff', '--no-ext-diff', '--stat', '--find-copies-harder',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000582 '-l100000', '-C50', 'fake_ancestor_sha', 'HEAD'],),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000583 '+dat'),
584 ]
585
tandrii@chromium.org1e67bb72016-02-11 12:15:49 +0000586 @classmethod
587 def _gerrit_upload_calls(cls, description, reviewers, squash,
tandrii@chromium.org10625002016-03-04 20:03:47 +0000588 expected_upstream_ref='origin/refs/heads/master',
589 post_amend_description=None):
590 if post_amend_description is None:
591 post_amend_description = description
ukai@chromium.orge8077812012-02-03 03:41:46 +0000592 calls = [
bauerb@chromium.org54b400c2016-01-14 10:08:25 +0000593 ((['git', 'config', '--bool', 'gerrit.squash-uploads'],), 'false'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000594 ((['git', 'log', '--pretty=format:%s\n\n%b',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000595 'fake_ancestor_sha..HEAD'],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000596 description)
597 ]
tandrii@chromium.org57d86542016-03-04 16:11:32 +0000598 if not git_footers.get_footer_change_id(description) and not squash:
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000599 calls += [
tandrii@chromium.org10625002016-03-04 20:03:47 +0000600 # DownloadGerritHook(False)
601 ((False, ),
602 ''),
603 # Amending of commit message to get the Change-Id.
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000604 ((['git', 'log', '--pretty=format:%s\n\n%b',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000605 'fake_ancestor_sha..HEAD'],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000606 description),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000607 ((['git', 'commit', '--amend', '-m', description],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000608 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000609 ((['git', 'log', '--pretty=format:%s\n\n%b',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000610 'fake_ancestor_sha..HEAD'],),
tandrii@chromium.org10625002016-03-04 20:03:47 +0000611 post_amend_description)
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000612 ]
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000613 if squash:
614 ref_to_push = 'abcdef0123456789'
615 calls += [
bauerb@chromium.org13502e02016-02-18 10:18:29 +0000616 ((['git', 'show', '--format=%B', '-s',
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000617 'refs/heads/git_cl_uploads/master'],),
618 (description, 0)),
619 ((['git', 'config', 'branch.master.merge'],),
620 'refs/heads/master'),
621 ((['git', 'config', 'branch.master.remote'],),
622 'origin'),
623 ((['get_or_create_merge_base', 'master', 'master'],),
624 'origin/master'),
625 ((['git', 'rev-parse', 'HEAD:'],),
626 '0123456789abcdef'),
627 ((['git', 'commit-tree', '0123456789abcdef', '-p',
628 'origin/master', '-m', 'd'],),
629 ref_to_push),
630 ]
631 else:
632 ref_to_push = 'HEAD'
633
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000634 calls += [
luqui@chromium.org609f3952015-05-04 22:47:04 +0000635 ((['git', 'rev-list',
636 expected_upstream_ref + '..' + ref_to_push],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000637 ((['git', 'config', 'rietveld.cc'],), '')
ukai@chromium.orge8077812012-02-03 03:41:46 +0000638 ]
ukai@chromium.org19bbfa22012-02-03 16:18:11 +0000639 receive_pack = '--receive-pack=git receive-pack '
ukai@chromium.orge8077812012-02-03 03:41:46 +0000640 receive_pack += '--cc=joe@example.com' # from watch list
641 if reviewers:
642 receive_pack += ' '
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000643 receive_pack += ' '.join(
644 '--reviewer=' + email for email in sorted(reviewers))
ukai@chromium.org19bbfa22012-02-03 16:18:11 +0000645 receive_pack += ''
ukai@chromium.orge8077812012-02-03 03:41:46 +0000646 calls += [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000647 ((['git',
luqui@chromium.org609f3952015-05-04 22:47:04 +0000648 'push', receive_pack, 'origin',
649 ref_to_push + ':refs/for/refs/heads/master'],),
tandrii@chromium.orga342c922016-03-16 07:08:25 +0000650 ('remote:\n'
651 'remote: Processing changes: (\)\n'
652 'remote: Processing changes: (|)\n'
653 'remote: Processing changes: (/)\n'
654 'remote: Processing changes: (-)\n'
655 'remote: Processing changes: new: 1 (/)\n'
656 'remote: Processing changes: new: 1, done\n'
657 'remote:\n'
658 'remote: New Changes:\n'
659 'remote: https://chromium-review.googlesource.com/123456 XXX.\n'
660 'remote:\n'
661 'To https://chromium.googlesource.com/yyy/zzz\n'
662 ' * [new branch] hhhh -> refs/for/refs/heads/master\n')),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000663 ]
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000664 if squash:
665 calls += [
tandrii@chromium.orga342c922016-03-16 07:08:25 +0000666 ((['git', 'config', 'branch.master.gerritissue', '123456'],), ''),
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000667 ((['git', 'rev-parse', 'HEAD'],), 'abcdef0123456789'),
668 ((['git', 'update-ref', '-m', 'Uploaded abcdef0123456789',
669 'refs/heads/git_cl_uploads/master', 'abcdef0123456789'],),
670 '')
671 ]
tandrii@chromium.org1e67bb72016-02-11 12:15:49 +0000672 calls += cls._git_post_upload_calls()
ukai@chromium.orge8077812012-02-03 03:41:46 +0000673 return calls
674
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000675 def _run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000676 self,
677 upload_args,
678 description,
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000679 reviewers,
luqui@chromium.org609f3952015-05-04 22:47:04 +0000680 squash=False,
tandrii@chromium.org10625002016-03-04 20:03:47 +0000681 expected_upstream_ref='origin/refs/heads/master',
682 post_amend_description=None):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000683 """Generic gerrit upload test framework."""
ukai@chromium.orge8077812012-02-03 03:41:46 +0000684 self.calls = self._gerrit_base_calls()
luqui@chromium.org609f3952015-05-04 22:47:04 +0000685 self.calls += self._gerrit_upload_calls(
686 description, reviewers, squash,
tandrii@chromium.org10625002016-03-04 20:03:47 +0000687 expected_upstream_ref=expected_upstream_ref,
688 post_amend_description=post_amend_description)
tandrii@chromium.org09d7a6a2016-03-04 15:44:48 +0000689 # Uncomment when debugging.
690 # print '\n'.join(map(lambda x: '%2i: %s' % x, enumerate(self.calls)))
ukai@chromium.orge8077812012-02-03 03:41:46 +0000691 git_cl.main(['upload'] + upload_args)
692
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000693 def test_gerrit_upload_without_change_id(self):
tandrii@chromium.org10625002016-03-04 20:03:47 +0000694 self.mock(git_cl, 'DownloadGerritHook', self._mocked_call)
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000695 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000696 [],
jamesr@chromium.org35d1a842012-07-27 00:20:43 +0000697 'desc\n\nBUG=\n',
tandrii@chromium.org10625002016-03-04 20:03:47 +0000698 [],
699 post_amend_description='desc\n\nBUG=\n\nChange-Id: Ixxx')
ukai@chromium.orge8077812012-02-03 03:41:46 +0000700
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000701 def test_gerrit_no_reviewer(self):
702 self._run_gerrit_upload_test(
703 [],
tandrii@chromium.org09d7a6a2016-03-04 15:44:48 +0000704 'desc\n\nBUG=\n\nChange-Id: I123456789\n',
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000705 [])
706
ukai@chromium.orge8077812012-02-03 03:41:46 +0000707 def test_gerrit_reviewers_cmd_line(self):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000708 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000709 ['-r', 'foo@example.com'],
tandrii@chromium.org09d7a6a2016-03-04 15:44:48 +0000710 'desc\n\nBUG=\n\nChange-Id: I123456789',
ukai@chromium.orge8077812012-02-03 03:41:46 +0000711 ['foo@example.com'])
712
713 def test_gerrit_reviewer_multiple(self):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000714 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000715 [],
tandrii@chromium.org09d7a6a2016-03-04 15:44:48 +0000716 'desc\nTBR=reviewer@example.com\nBUG=\nR=another@example.com\n\n'
717 'Change-Id: 123456789\n',
ukai@chromium.orge8077812012-02-03 03:41:46 +0000718 ['reviewer@example.com', 'another@example.com'])
719
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000720 def test_gerrit_upload_squash(self):
721 self._run_gerrit_upload_test(
722 ['--squash'],
723 'desc\n\nBUG=\nChange-Id:123456789\n',
724 [],
luqui@chromium.org609f3952015-05-04 22:47:04 +0000725 squash=True,
726 expected_upstream_ref='origin/master')
ukai@chromium.orge8077812012-02-03 03:41:46 +0000727
rmistry@google.com2dd99862015-06-22 12:22:18 +0000728 def test_upload_branch_deps(self):
729 def mock_run_git(*args, **_kwargs):
730 if args[0] == ['for-each-ref',
731 '--format=%(refname:short) %(upstream:short)',
732 'refs/heads']:
733 # Create a local branch dependency tree that looks like this:
734 # test1 -> test2 -> test3 -> test4 -> test5
735 # -> test3.1
736 # test6 -> test0
737 branch_deps = [
738 'test2 test1', # test1 -> test2
739 'test3 test2', # test2 -> test3
740 'test3.1 test2', # test2 -> test3.1
741 'test4 test3', # test3 -> test4
742 'test5 test4', # test4 -> test5
743 'test6 test0', # test0 -> test6
744 'test7', # test7
745 ]
746 return '\n'.join(branch_deps)
747 self.mock(git_cl, 'RunGit', mock_run_git)
748
andybons@chromium.org962f9462016-02-03 20:00:42 +0000749 git_cl.settings = git_cl.Settings()
750 self.mock(git_cl.settings, 'GetIsGerrit', lambda: False)
751
rmistry@google.com2dd99862015-06-22 12:22:18 +0000752 class RecordCalls:
753 times_called = 0
754 record_calls = RecordCalls()
755 def mock_CMDupload(*args, **_kwargs):
756 record_calls.times_called += 1
757 return 0
758 self.mock(git_cl, 'CMDupload', mock_CMDupload)
759
760 self.calls = [
761 (('[Press enter to continue or ctrl-C to quit]',), ''),
762 ]
763
764 class MockChangelist():
765 def __init__(self):
766 pass
767 def GetBranch(self):
768 return 'test1'
769 def GetIssue(self):
770 return '123'
771 def GetPatchset(self):
772 return '1001'
773
774 ret = git_cl.upload_branch_deps(MockChangelist(), [])
775 # CMDupload should have been called 5 times because of 5 dependent branches.
776 self.assertEquals(5, record_calls.times_called)
777 self.assertEquals(0, ret)
778
tandrii@chromium.org65874e12016-03-04 12:03:02 +0000779 def test_gerrit_change_id(self):
780 self.calls = [
781 ((['git', 'write-tree'], ),
782 'hashtree'),
783 ((['git', 'rev-parse', 'HEAD~0'], ),
784 'branch-parent'),
785 ((['git', 'var', 'GIT_AUTHOR_IDENT'], ),
786 'A B <a@b.org> 1456848326 +0100'),
787 ((['git', 'var', 'GIT_COMMITTER_IDENT'], ),
788 'C D <c@d.org> 1456858326 +0100'),
789 ((['git', 'hash-object', '-t', 'commit', '--stdin'], ),
790 'hashchange'),
791 ]
792 change_id = git_cl.GenerateGerritChangeId('line1\nline2\n')
793 self.assertEqual(change_id, 'Ihashchange')
794
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000795 def test_update_reviewers(self):
796 data = [
797 ('foo', [], 'foo'),
agable@chromium.org42c20792013-09-12 17:34:49 +0000798 ('foo\nR=xx', [], 'foo\nR=xx'),
799 ('foo\nTBR=xx', [], 'foo\nTBR=xx'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000800 ('foo', ['a@c'], 'foo\n\nR=a@c'),
agable@chromium.org42c20792013-09-12 17:34:49 +0000801 ('foo\nR=xx', ['a@c'], 'foo\n\nR=a@c, xx'),
802 ('foo\nTBR=xx', ['a@c'], 'foo\n\nR=a@c\nTBR=xx'),
803 ('foo\nTBR=xx\nR=yy', ['a@c'], 'foo\n\nR=a@c, yy\nTBR=xx'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000804 ('foo\nBUG=', ['a@c'], 'foo\nBUG=\nR=a@c'),
agable@chromium.org42c20792013-09-12 17:34:49 +0000805 ('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 +0000806 ('foo', ['a@c', 'b@c'], 'foo\n\nR=a@c, b@c'),
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +0000807 ('foo\nBar\n\nR=\nBUG=', ['c@c'], 'foo\nBar\n\nR=c@c\nBUG='),
808 ('foo\nBar\n\nR=\nBUG=\nR=', ['c@c'], 'foo\nBar\n\nR=c@c\nBUG='),
809 # Same as the line before, but full of whitespaces.
810 (
811 'foo\nBar\n\n R = \n BUG = \n R = ', ['c@c'],
812 'foo\nBar\n\nR=c@c\n BUG =',
813 ),
814 # Whitespaces aren't interpreted as new lines.
815 ('foo BUG=allo R=joe ', ['c@c'], 'foo BUG=allo R=joe\n\nR=c@c'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000816 ]
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +0000817 expected = [i[2] for i in data]
818 actual = []
819 for orig, reviewers, _expected in data:
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000820 obj = git_cl.ChangeDescription(orig)
821 obj.update_reviewers(reviewers)
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +0000822 actual.append(obj.description)
823 self.assertEqual(expected, actual)
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000824
wittman@chromium.org455dc922015-01-26 20:15:50 +0000825 def test_get_target_ref(self):
826 # Check remote or remote branch not present.
827 self.assertEqual(None, git_cl.GetTargetRef('origin', None, 'master', None))
828 self.assertEqual(None, git_cl.GetTargetRef(None,
829 'refs/remotes/origin/master',
830 'master', None))
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000831
wittman@chromium.org455dc922015-01-26 20:15:50 +0000832 # Check default target refs for branches.
833 self.assertEqual('refs/heads/master',
834 git_cl.GetTargetRef('origin', 'refs/remotes/origin/master',
835 None, None))
836 self.assertEqual('refs/heads/master',
837 git_cl.GetTargetRef('origin', 'refs/remotes/origin/lkgr',
838 None, None))
839 self.assertEqual('refs/heads/master',
840 git_cl.GetTargetRef('origin', 'refs/remotes/origin/lkcr',
841 None, None))
842 self.assertEqual('refs/branch-heads/123',
843 git_cl.GetTargetRef('origin',
844 'refs/remotes/branch-heads/123',
845 None, None))
846 self.assertEqual('refs/diff/test',
847 git_cl.GetTargetRef('origin',
848 'refs/remotes/origin/refs/diff/test',
849 None, None))
rmistry@google.comc68112d2015-03-03 12:48:06 +0000850 self.assertEqual('refs/heads/chrome/m42',
851 git_cl.GetTargetRef('origin',
852 'refs/remotes/origin/chrome/m42',
853 None, None))
wittman@chromium.org455dc922015-01-26 20:15:50 +0000854
855 # Check target refs for user-specified target branch.
856 for branch in ('branch-heads/123', 'remotes/branch-heads/123',
857 'refs/remotes/branch-heads/123'):
858 self.assertEqual('refs/branch-heads/123',
859 git_cl.GetTargetRef('origin',
860 'refs/remotes/origin/master',
861 branch, None))
862 for branch in ('origin/master', 'remotes/origin/master',
863 'refs/remotes/origin/master'):
864 self.assertEqual('refs/heads/master',
865 git_cl.GetTargetRef('origin',
866 'refs/remotes/branch-heads/123',
867 branch, None))
868 for branch in ('master', 'heads/master', 'refs/heads/master'):
869 self.assertEqual('refs/heads/master',
870 git_cl.GetTargetRef('origin',
871 'refs/remotes/branch-heads/123',
872 branch, None))
873
874 # Check target refs for pending prefix.
875 self.assertEqual('prefix/heads/master',
876 git_cl.GetTargetRef('origin', 'refs/remotes/origin/master',
877 None, 'prefix/'))
878
wychen@chromium.orga872e752015-04-28 23:42:18 +0000879 def test_patch_when_dirty(self):
880 # Patch when local tree is dirty
881 self.mock(git_common, 'is_dirty_git_tree', lambda x: True)
882 self.assertNotEqual(git_cl.main(['patch', '123456']), 0)
883
884 def test_diff_when_dirty(self):
885 # Do 'git cl diff' when local tree is dirty
886 self.mock(git_common, 'is_dirty_git_tree', lambda x: True)
887 self.assertNotEqual(git_cl.main(['diff']), 0)
888
889 def _patch_common(self):
890 self.mock(git_cl.Changelist, 'GetMostRecentPatchset', lambda x: '60001')
891 self.mock(git_cl.Changelist, 'GetPatchSetDiff', lambda *args: None)
wychen@chromium.org5b3bebb2015-05-28 21:41:43 +0000892 self.mock(git_cl.Changelist, 'GetDescription', lambda *args: 'Description')
wychen@chromium.orga872e752015-04-28 23:42:18 +0000893 self.mock(git_cl.Changelist, 'SetIssue', lambda *args: None)
894 self.mock(git_cl.Changelist, 'SetPatchset', lambda *args: None)
895 self.mock(git_cl, 'IsGitVersionAtLeast', lambda *args: True)
896
897 self.calls = [
898 ((['git', 'config', 'rietveld.autoupdate'],), ''),
899 ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'),
900 ((['git', 'rev-parse', '--show-cdup'],), ''),
901 ((['sed', '-e', 's|^--- a/|--- |; s|^+++ b/|+++ |'],), ''),
902 ]
903
904 def test_patch_successful(self):
905 self._patch_common()
906 self.calls += [
907 ((['git', 'apply', '--index', '-p0', '--3way'],), ''),
908 ((['git', 'commit', '-m',
wychen@chromium.org5b3bebb2015-05-28 21:41:43 +0000909 'Description\n\n' +
wychen@chromium.orga872e752015-04-28 23:42:18 +0000910 'patch from issue 123456 at patchset 60001 ' +
911 '(http://crrev.com/123456#ps60001)'],), ''),
912 ]
913 self.assertEqual(git_cl.main(['patch', '123456']), 0)
914
915 def test_patch_conflict(self):
916 self._patch_common()
917 self.calls += [
918 ((['git', 'apply', '--index', '-p0', '--3way'],), '',
919 subprocess2.CalledProcessError(1, '', '', '', '')),
920 ]
921 self.assertNotEqual(git_cl.main(['patch', '123456']), 0)
wittman@chromium.org455dc922015-01-26 20:15:50 +0000922
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000923if __name__ == '__main__':
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000924 git_cl.logging.basicConfig(
925 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000926 unittest.main()