blob: bb963c76a0a381fb91429d04e45d55df53fc2690 [file] [log] [blame]
maruel@chromium.orgddd59412011-11-30 14:20:38 +00001#!/usr/bin/env python
maruel@chromium.orgeb5edbc2012-01-16 17:03:28 +00002# Copyright (c) 2012 The Chromium Authors. All rights reserved.
maruel@chromium.orgddd59412011-11-30 14:20:38 +00003# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Unit tests for git_cl.py."""
7
8import os
maruel@chromium.orga3353652011-11-30 14:26:57 +00009import StringIO
ukai@chromium.org78c4b982012-02-14 02:20:26 +000010import stat
maruel@chromium.orgddd59412011-11-30 14:20:38 +000011import sys
12import unittest
13
14sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
15
16from testing_support.auto_stub import TestCase
17
18import git_cl
iannucci@chromium.org9e849272014-04-04 00:31:55 +000019import git_common
maruel@chromium.orgddd59412011-11-30 14:20:38 +000020import subprocess2
maruel@chromium.orgddd59412011-11-30 14:20:38 +000021
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000022class PresubmitMock(object):
23 def __init__(self, *args, **kwargs):
24 self.reviewers = []
25 @staticmethod
26 def should_continue():
27 return True
28
29
30class RietveldMock(object):
31 def __init__(self, *args, **kwargs):
32 pass
maruel@chromium.org78936cb2013-04-11 00:17:52 +000033
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000034 @staticmethod
35 def get_description(issue):
36 return 'Issue: %d' % issue
37
maruel@chromium.org78936cb2013-04-11 00:17:52 +000038 @staticmethod
39 def get_issue_properties(_issue, _messages):
40 return {
41 'reviewers': ['joe@chromium.org', 'john@chromium.org'],
42 'messages': [
43 {
44 'approval': True,
45 'sender': 'john@chromium.org',
46 },
47 ],
48 }
49
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000050
51class WatchlistsMock(object):
52 def __init__(self, _):
53 pass
54 @staticmethod
55 def GetWatchersForPaths(_):
56 return ['joe@example.com']
57
58
ukai@chromium.org78c4b982012-02-14 02:20:26 +000059class CodereviewSettingsFileMock(object):
60 def __init__(self):
61 pass
62 # pylint: disable=R0201
63 def read(self):
64 return ("CODE_REVIEW_SERVER: gerrit.chromium.org\n" +
65 "GERRIT_HOST: gerrit.chromium.org\n" +
66 "GERRIT_PORT: 29418\n")
67
68
vadimsh@chromium.orgeed4df32015-04-10 21:30:20 +000069class AuthenticatorMock(object):
70 def __init__(self, *_args):
71 pass
72 def has_cached_credentials(self):
73 return True
74
75
maruel@chromium.orgddd59412011-11-30 14:20:38 +000076class TestGitCl(TestCase):
77 def setUp(self):
78 super(TestGitCl, self).setUp()
79 self.calls = []
80 self._calls_done = 0
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000081 self.mock(subprocess2, 'call', self._mocked_call)
82 self.mock(subprocess2, 'check_call', self._mocked_call)
83 self.mock(subprocess2, 'check_output', self._mocked_call)
84 self.mock(subprocess2, 'communicate', self._mocked_call)
sbc@chromium.org71437c02015-04-09 19:29:40 +000085 self.mock(git_common, 'is_dirty_git_tree', lambda x: False)
iannucci@chromium.org9e849272014-04-04 00:31:55 +000086 self.mock(git_common, 'get_or_create_merge_base',
87 lambda *a: (
88 self._mocked_call(['get_or_create_merge_base']+list(a))))
maruel@chromium.orgddd59412011-11-30 14:20:38 +000089 self.mock(git_cl, 'FindCodereviewSettingsFile', lambda: '')
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000090 self.mock(git_cl, 'ask_for_data', self._mocked_call)
91 self.mock(git_cl.breakpad, 'post', self._mocked_call)
92 self.mock(git_cl.breakpad, 'SendStack', self._mocked_call)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000093 self.mock(git_cl.presubmit_support, 'DoPresubmitChecks', PresubmitMock)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000094 self.mock(git_cl.rietveld, 'Rietveld', RietveldMock)
maruel@chromium.org4bac4b52012-11-27 20:33:52 +000095 self.mock(git_cl.rietveld, 'CachingRietveld', RietveldMock)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000096 self.mock(git_cl.upload, 'RealMain', self.fail)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000097 self.mock(git_cl.watchlists, 'Watchlists', WatchlistsMock)
vadimsh@chromium.orgeed4df32015-04-10 21:30:20 +000098 self.mock(git_cl.auth, 'get_authenticator_for_host', AuthenticatorMock)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000099 # It's important to reset settings to not have inter-tests interference.
100 git_cl.settings = None
101
102 def tearDown(self):
wychen@chromium.org445c8962015-04-28 23:30:05 +0000103 try:
104 if not self.has_failed():
105 self.assertEquals([], self.calls)
106 finally:
107 super(TestGitCl, self).tearDown()
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000108
iannucci@chromium.org9e849272014-04-04 00:31:55 +0000109 def _mocked_call(self, *args, **_kwargs):
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000110 self.assertTrue(
111 self.calls,
112 '@%d Expected: <Missing> Actual: %r' % (self._calls_done, args))
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'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000232 ((['git', 'rev-parse', 'HEAD'],), 'hash'),
233 ((['git', 'symbolic-ref', 'HEAD'],), 'hash'),
234 ((['git',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000235 'config', 'branch.hash.last-upload-hash', 'hash'],), ''),
rmistry@google.com5626a922015-02-26 14:03:30 +0000236 ((['git', 'config', 'rietveld.run-post-upload-hook'],), ''),
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000237 ]
238
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000239 @staticmethod
240 def _git_sanity_checks(diff_base, working_branch):
241 fake_ancestor = 'fake_ancestor'
242 fake_cl = 'fake_cl_for_patch'
243 return [
244 # Calls to verify branch point is ancestor
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000245 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000246 'rev-parse', '--verify', diff_base],), fake_ancestor),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000247 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000248 'merge-base', fake_ancestor, 'HEAD'],), fake_ancestor),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000249 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000250 'rev-list', '^' + fake_ancestor, 'HEAD'],), fake_cl),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000251 # Mock a config miss (error code 1)
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000252 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000253 'config', 'gitcl.remotebranch'],), (('', None), 1)),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000254 # Call to GetRemoteBranch()
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000255 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000256 'config', 'branch.%s.merge' % working_branch],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000257 'refs/heads/master'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000258 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000259 'config', 'branch.%s.remote' % working_branch],), 'origin'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000260 ((['git', 'rev-list', '^' + fake_ancestor,
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000261 'refs/remotes/origin/master'],), ''),
262 ]
263
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000264 @classmethod
265 def _dcommit_calls_1(cls):
266 return [
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +0000267 ((['git', 'config', 'rietveld.autoupdate'],),
268 ''),
269 ((['git', 'config', 'rietveld.pending-ref-prefix'],),
270 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000271 ((['git',
bratell@opera.com05fb9112014-07-07 09:30:23 +0000272 'config', '--local', '--get-regexp', '^svn-remote\\.'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000273 ((('svn-remote.svn.url svn://svn.chromium.org/chrome\n'
274 'svn-remote.svn.fetch trunk/src:refs/remotes/origin/master'),
275 None),
276 0)),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000277 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000278 'config', 'rietveld.server'],), 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000279 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
280 ((['git', 'config', '--int', '--get',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000281 'branch.working.git-cl-similarity'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000282 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
283 ((['git', 'config', '--int', '--get',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000284 'branch.working.git-find-copies'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000285 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
286 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000287 'config', 'branch.working.merge'],), 'refs/heads/master'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000288 ((['git', 'config', 'branch.working.remote'],), 'origin'),
iannucci@chromium.org5724c962014-04-11 09:32:56 +0000289 ((['git', 'config', 'branch.working.merge'],),
290 'refs/heads/master'),
291 ((['git', 'config', 'branch.working.remote'],), 'origin'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000292 ((['git', 'rev-list', '--merges',
szager@chromium.orge84b7542012-06-15 21:26:58 +0000293 '--grep=^SVN changes up to revision [0-9]*$',
szager@chromium.org9bb85e22012-06-13 20:28:23 +0000294 'refs/remotes/origin/master^!'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000295 ((['git', 'rev-list', '^refs/heads/working',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000296 'refs/remotes/origin/master'],),
297 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000298 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000299 'log', '--grep=^git-svn-id:', '-1', '--pretty=format:%H'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000300 '3fc18b62c4966193eb435baabe2d18a3810ec82e'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000301 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000302 'rev-list', '^3fc18b62c4966193eb435baabe2d18a3810ec82e',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000303 'refs/remotes/origin/master'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000304 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000305 'merge-base', 'refs/remotes/origin/master', 'HEAD'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000306 'fake_ancestor_sha'),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000307 ]
308
309 @classmethod
310 def _dcommit_calls_normal(cls):
311 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000312 ((['git', 'rev-parse', '--show-cdup'],), ''),
313 ((['git', 'rev-parse', 'HEAD'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000314 '00ff397798ea57439712ed7e04ab96e13969ef40'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000315 ((['git',
mcgrathr@chromium.org9249f642013-06-03 21:36:18 +0000316 'diff', '--name-status', '--no-renames', '-r', 'fake_ancestor_sha...',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000317 '.'],),
318 'M\tPRESUBMIT.py'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000319 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000320 'config', 'branch.working.rietveldissue'],), '12345'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000321 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000322 'config', 'branch.working.rietveldpatchset'],), '31137'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000323 ((['git', 'config', 'branch.working.rietveldserver'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000324 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000325 ((['git', 'config', 'user.email'],), 'author@example.com'),
326 ((['git', 'config', 'rietveld.tree-status-url'],), ''),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000327 ]
328
329 @classmethod
330 def _dcommit_calls_bypassed(cls):
331 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000332 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000333 'config', 'branch.working.rietveldissue'],), '12345'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000334 ((['git', 'config', 'branch.working.rietveldserver'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000335 'codereview.example.com'),
jochen@chromium.org3ec0d542014-01-14 20:00:03 +0000336 ((['git', 'config', 'rietveld.tree-status-url'],), ''),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000337 (('GitClHooksBypassedCommit',
338 'Issue https://codereview.example.com/12345 bypassed hook when '
jochen@chromium.org3ec0d542014-01-14 20:00:03 +0000339 'committing (tree status was "unset")'), None),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000340 ]
341
342 @classmethod
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000343 def _dcommit_calls_3(cls):
344 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000345 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000346 'diff', '--no-ext-diff', '--stat', '--find-copies-harder',
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000347 '-l100000', '-C50', 'fake_ancestor_sha',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000348 'refs/heads/working'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000349 (' PRESUBMIT.py | 2 +-\n'
350 ' 1 files changed, 1 insertions(+), 1 deletions(-)\n')),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000351 ((['git', 'show-ref', '--quiet', '--verify',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000352 'refs/heads/git-cl-commit'],),
353 (('', None), 0)),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000354 ((['git', 'branch', '-D', 'git-cl-commit'],), ''),
355 ((['git', 'show-ref', '--quiet', '--verify',
szager@chromium.org9bb85e22012-06-13 20:28:23 +0000356 'refs/heads/git-cl-cherry-pick'],), ''),
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000357 ((['git', 'rev-parse', '--show-cdup'],), '\n'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000358 ((['git', 'checkout', '-q', '-b', 'git-cl-commit'],), ''),
359 ((['git', 'reset', '--soft', 'fake_ancestor_sha'],), ''),
360 ((['git', 'commit', '-m',
maruel@chromium.orge52678e2013-04-26 18:34:44 +0000361 'Issue: 12345\n\nR=john@chromium.org\n\n'
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000362 'Review URL: https://codereview.example.com/12345'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000363 ''),
kjellander@chromium.org6abc6522014-12-02 07:34:49 +0000364 ((['git', 'config', 'rietveld.force-https-commit-url'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000365 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000366 'svn', 'dcommit', '-C50', '--no-rebase', '--rmdir'],),
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000367 (('', None), 0)),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000368 ((['git', 'checkout', '-q', 'working'],), ''),
369 ((['git', 'branch', '-D', 'git-cl-commit'],), ''),
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000370 ]
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000371
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000372 @staticmethod
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000373 def _cmd_line(description, args, similarity, find_copies, private):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000374 """Returns the upload command line passed to upload.RealMain()."""
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000375 return [
376 'upload', '--assume_yes', '--server',
maruel@chromium.orgeb5edbc2012-01-16 17:03:28 +0000377 'https://codereview.example.com',
maruel@chromium.org71e12a92012-02-14 02:34:15 +0000378 '--message', description
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000379 ] + args + [
380 '--cc', 'joe@example.com',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000381 ] + (['--private'] if private else []) + [
iannucci@chromium.org79540052012-10-19 23:15:26 +0000382 '--git_similarity', similarity or '50'
383 ] + (['--git_no_find_copies'] if find_copies == False else []) + [
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000384 'fake_ancestor_sha', 'HEAD'
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000385 ]
386
387 def _run_reviewer_test(
388 self,
389 upload_args,
390 expected_description,
391 returned_description,
392 final_description,
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000393 reviewers,
394 private=False):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000395 """Generic reviewer test framework."""
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000396 try:
397 similarity = upload_args[upload_args.index('--similarity')+1]
398 except ValueError:
399 similarity = None
iannucci@chromium.org79540052012-10-19 23:15:26 +0000400
401 if '--find-copies' in upload_args:
402 find_copies = True
403 elif '--no-find-copies' in upload_args:
404 find_copies = False
405 else:
406 find_copies = None
407
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000408 private = '--private' in upload_args
409
410 self.calls = self._upload_calls(similarity, find_copies, private)
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000411
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000412 def RunEditor(desc, _, **kwargs):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000413 self.assertEquals(
414 '# Enter a description of the change.\n'
janx@chromium.org104b2db2013-04-18 12:58:40 +0000415 '# This will be displayed on the codereview site.\n'
alancutter@chromium.org63a4d7f2013-05-31 02:22:45 +0000416 '# The first line will also be used as the subject of the review.\n'
alancutter@chromium.orgbd1073e2013-06-01 00:34:38 +0000417 '#--------------------This line is 72 characters long'
418 '--------------------\n' +
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000419 expected_description,
420 desc)
421 return returned_description
422 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor)
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000423
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000424 def check_upload(args):
iannucci@chromium.org79540052012-10-19 23:15:26 +0000425 cmd_line = self._cmd_line(final_description, reviewers, similarity,
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000426 find_copies, private)
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000427 self.assertEquals(cmd_line, args)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000428 return 1, 2
429 self.mock(git_cl.upload, 'RealMain', check_upload)
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000430
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000431 git_cl.main(['upload'] + upload_args)
432
433 def test_no_reviewer(self):
434 self._run_reviewer_test(
435 [],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000436 'desc\n\nBUG=',
437 '# Blah blah comment.\ndesc\n\nBUG=',
438 'desc\n\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000439 [])
440
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000441 def test_keep_similarity(self):
442 self._run_reviewer_test(
443 ['--similarity', '70'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000444 'desc\n\nBUG=',
445 '# Blah blah comment.\ndesc\n\nBUG=',
446 'desc\n\nBUG=',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000447 [])
448
iannucci@chromium.org79540052012-10-19 23:15:26 +0000449 def test_keep_find_copies(self):
450 self._run_reviewer_test(
451 ['--no-find-copies'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000452 'desc\n\nBUG=',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000453 '# Blah blah comment.\ndesc\n\nBUG=\n',
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000454 'desc\n\nBUG=',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000455 [])
456
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000457 def test_private(self):
458 self._run_reviewer_test(
459 ['--private'],
460 'desc\n\nBUG=',
461 '# Blah blah comment.\ndesc\n\nBUG=\n',
462 'desc\n\nBUG=',
463 [])
464
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000465 def test_reviewers_cmd_line(self):
466 # Reviewer is passed as-is
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000467 description = 'desc\n\nR=foo@example.com\nBUG='
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000468 self._run_reviewer_test(
469 ['-r' 'foo@example.com'],
470 description,
471 '\n%s\n' % description,
472 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000473 ['--reviewers=foo@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000474
475 def test_reviewer_tbr_overriden(self):
476 # Reviewer is overriden with TBR
477 # Also verifies the regexp work without a trailing LF
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000478 description = 'Foo Bar\n\nTBR=reviewer@example.com'
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000479 self._run_reviewer_test(
480 ['-r' 'foo@example.com'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000481 'desc\n\nR=foo@example.com\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000482 description.strip('\n'),
483 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000484 ['--reviewers=reviewer@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000485
486 def test_reviewer_multiple(self):
487 # Handles multiple R= or TBR= lines.
488 description = (
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000489 'Foo Bar\nTBR=reviewer@example.com\nBUG=\nR=another@example.com')
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000490 self._run_reviewer_test(
491 [],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000492 'desc\n\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000493 description,
494 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000495 ['--reviewers=another@example.com,reviewer@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000496
maruel@chromium.orga3353652011-11-30 14:26:57 +0000497 def test_reviewer_send_mail(self):
498 # --send-mail can be used without -r if R= is used
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000499 description = 'Foo Bar\nR=reviewer@example.com'
maruel@chromium.orga3353652011-11-30 14:26:57 +0000500 self._run_reviewer_test(
501 ['--send-mail'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000502 'desc\n\nBUG=',
maruel@chromium.orga3353652011-11-30 14:26:57 +0000503 description.strip('\n'),
504 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000505 ['--reviewers=reviewer@example.com', '--send_mail'])
maruel@chromium.orga3353652011-11-30 14:26:57 +0000506
507 def test_reviewer_send_mail_no_rev(self):
508 # Fails without a reviewer.
maruel@chromium.org2e23ce32013-05-07 12:42:28 +0000509 stdout = StringIO.StringIO()
510 stderr = StringIO.StringIO()
maruel@chromium.orga3353652011-11-30 14:26:57 +0000511 try:
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000512 self.calls = self._upload_no_rev_calls(None, None)
513 def RunEditor(desc, _, **kwargs):
maruel@chromium.orga3353652011-11-30 14:26:57 +0000514 return desc
515 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor)
maruel@chromium.org2e23ce32013-05-07 12:42:28 +0000516 self.mock(sys, 'stdout', stdout)
517 self.mock(sys, 'stderr', stderr)
maruel@chromium.orga3353652011-11-30 14:26:57 +0000518 git_cl.main(['upload', '--send-mail'])
519 self.fail()
520 except SystemExit:
maruel@chromium.org2e23ce32013-05-07 12:42:28 +0000521 self.assertEqual(
522 'Using 50% similarity for rename/copy detection. Override with '
523 '--similarity.\n',
524 stdout.getvalue())
525 self.assertEqual(
526 'Must specify reviewers to send email.\n', stderr.getvalue())
maruel@chromium.orga3353652011-11-30 14:26:57 +0000527
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000528 def test_dcommit(self):
529 self.calls = (
530 self._dcommit_calls_1() +
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000531 self._git_sanity_checks('fake_ancestor_sha', 'working') +
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000532 self._dcommit_calls_normal() +
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000533 self._dcommit_calls_3())
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000534 git_cl.main(['dcommit'])
535
536 def test_dcommit_bypass_hooks(self):
537 self.calls = (
538 self._dcommit_calls_1() +
539 self._dcommit_calls_bypassed() +
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000540 self._dcommit_calls_3())
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000541 git_cl.main(['dcommit', '--bypass-hooks'])
542
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000543
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000544 @classmethod
545 def _gerrit_base_calls(cls):
ukai@chromium.orge8077812012-02-03 03:41:46 +0000546 return [
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000547 ((['git', 'config', 'rietveld.autoupdate'],),
548 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000549 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000550 'config', 'rietveld.server'],), 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000551 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
552 ((['git', 'config', '--int', '--get',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000553 'branch.master.git-cl-similarity'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000554 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
555 ((['git', 'config', '--int', '--get',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000556 'branch.master.git-find-copies'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000557 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
558 ((['git', 'config', 'branch.master.merge'],), 'master'),
559 ((['git', 'config', 'branch.master.remote'],), 'origin'),
iannucci@chromium.org9e849272014-04-04 00:31:55 +0000560 ((['get_or_create_merge_base', 'master', 'master'],),
561 'fake_ancestor_sha'),
vadimsh@chromium.orgeed4df32015-04-10 21:30:20 +0000562 ((['git', 'config', 'gerrit.host'],), 'gerrit.example.com'),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000563 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000564 ((['git', 'rev-parse', '--show-cdup'],), ''),
565 ((['git', 'rev-parse', 'HEAD'],), '12345'),
566 ((['git',
mcgrathr@chromium.org9249f642013-06-03 21:36:18 +0000567 'diff', '--name-status', '--no-renames', '-r',
568 'fake_ancestor_sha...', '.'],),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000569 'M\t.gitignore\n'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000570 ((['git', 'config', 'branch.master.rietveldissue'],), ''),
571 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000572 'config', 'branch.master.rietveldpatchset'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000573 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000574 'log', '--pretty=format:%s%n%n%b', 'fake_ancestor_sha...'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000575 'foo'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000576 ((['git', 'config', 'user.email'],), 'me@example.com'),
577 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000578 'diff', '--no-ext-diff', '--stat', '--find-copies-harder',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000579 '-l100000', '-C50', 'fake_ancestor_sha', 'HEAD'],),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000580 '+dat'),
581 ]
582
583 @staticmethod
luqui@chromium.org609f3952015-05-04 22:47:04 +0000584 def _gerrit_upload_calls(description, reviewers, squash,
585 expected_upstream_ref='origin/refs/heads/master'):
ukai@chromium.orge8077812012-02-03 03:41:46 +0000586 calls = [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000587 ((['git', 'log', '--pretty=format:%s\n\n%b',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000588 'fake_ancestor_sha..HEAD'],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000589 description)
590 ]
591 if git_cl.CHANGE_ID not in description:
592 calls += [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000593 ((['git', 'log', '--pretty=format:%s\n\n%b',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000594 'fake_ancestor_sha..HEAD'],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000595 description),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000596 ((['git', 'commit', '--amend', '-m', description],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000597 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000598 ((['git', 'log', '--pretty=format:%s\n\n%b',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000599 'fake_ancestor_sha..HEAD'],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000600 description)
601 ]
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000602 if squash:
603 ref_to_push = 'abcdef0123456789'
604 calls += [
605 ((['git', 'show', '--format=%s\n\n%b', '-s',
606 'refs/heads/git_cl_uploads/master'],),
607 (description, 0)),
608 ((['git', 'config', 'branch.master.merge'],),
609 'refs/heads/master'),
610 ((['git', 'config', 'branch.master.remote'],),
611 'origin'),
612 ((['get_or_create_merge_base', 'master', 'master'],),
613 'origin/master'),
614 ((['git', 'rev-parse', 'HEAD:'],),
615 '0123456789abcdef'),
616 ((['git', 'commit-tree', '0123456789abcdef', '-p',
617 'origin/master', '-m', 'd'],),
618 ref_to_push),
619 ]
620 else:
621 ref_to_push = 'HEAD'
622
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000623 calls += [
luqui@chromium.org609f3952015-05-04 22:47:04 +0000624 ((['git', 'rev-list',
625 expected_upstream_ref + '..' + ref_to_push],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000626 ((['git', 'config', 'rietveld.cc'],), '')
ukai@chromium.orge8077812012-02-03 03:41:46 +0000627 ]
ukai@chromium.org19bbfa22012-02-03 16:18:11 +0000628 receive_pack = '--receive-pack=git receive-pack '
ukai@chromium.orge8077812012-02-03 03:41:46 +0000629 receive_pack += '--cc=joe@example.com' # from watch list
630 if reviewers:
631 receive_pack += ' '
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000632 receive_pack += ' '.join(
633 '--reviewer=' + email for email in sorted(reviewers))
ukai@chromium.org19bbfa22012-02-03 16:18:11 +0000634 receive_pack += ''
ukai@chromium.orge8077812012-02-03 03:41:46 +0000635 calls += [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000636 ((['git',
luqui@chromium.org609f3952015-05-04 22:47:04 +0000637 'push', receive_pack, 'origin',
638 ref_to_push + ':refs/for/refs/heads/master'],),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000639 '')
640 ]
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000641 if squash:
642 calls += [
643 ((['git', 'rev-parse', 'HEAD'],), 'abcdef0123456789'),
644 ((['git', 'update-ref', '-m', 'Uploaded abcdef0123456789',
645 'refs/heads/git_cl_uploads/master', 'abcdef0123456789'],),
646 '')
647 ]
648
ukai@chromium.orge8077812012-02-03 03:41:46 +0000649 return calls
650
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000651 def _run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000652 self,
653 upload_args,
654 description,
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000655 reviewers,
luqui@chromium.org609f3952015-05-04 22:47:04 +0000656 squash=False,
657 expected_upstream_ref='origin/refs/heads/master'):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000658 """Generic gerrit upload test framework."""
ukai@chromium.orge8077812012-02-03 03:41:46 +0000659 self.calls = self._gerrit_base_calls()
luqui@chromium.org609f3952015-05-04 22:47:04 +0000660 self.calls += self._gerrit_upload_calls(
661 description, reviewers, squash,
662 expected_upstream_ref=expected_upstream_ref)
ukai@chromium.orge8077812012-02-03 03:41:46 +0000663 git_cl.main(['upload'] + upload_args)
664
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000665 def test_gerrit_upload_without_change_id(self):
666 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000667 [],
jamesr@chromium.org35d1a842012-07-27 00:20:43 +0000668 'desc\n\nBUG=\n',
ukai@chromium.orge8077812012-02-03 03:41:46 +0000669 [])
670
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000671 def test_gerrit_no_reviewer(self):
672 self._run_gerrit_upload_test(
673 [],
674 'desc\n\nBUG=\nChange-Id:123456789\n',
675 [])
676
ukai@chromium.orge8077812012-02-03 03:41:46 +0000677 def test_gerrit_reviewers_cmd_line(self):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000678 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000679 ['-r', 'foo@example.com'],
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000680 'desc\n\nBUG=\nChange-Id:123456789',
ukai@chromium.orge8077812012-02-03 03:41:46 +0000681 ['foo@example.com'])
682
683 def test_gerrit_reviewer_multiple(self):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000684 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000685 [],
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000686 'desc\nTBR=reviewer@example.com\nBUG=\nR=another@example.com\n'
687 'Change-Id:123456789\n',
ukai@chromium.orge8077812012-02-03 03:41:46 +0000688 ['reviewer@example.com', 'another@example.com'])
689
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000690 def test_gerrit_upload_squash(self):
691 self._run_gerrit_upload_test(
692 ['--squash'],
693 'desc\n\nBUG=\nChange-Id:123456789\n',
694 [],
luqui@chromium.org609f3952015-05-04 22:47:04 +0000695 squash=True,
696 expected_upstream_ref='origin/master')
ukai@chromium.orge8077812012-02-03 03:41:46 +0000697
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000698 def test_config_gerrit_download_hook(self):
699 self.mock(git_cl, 'FindCodereviewSettingsFile', CodereviewSettingsFileMock)
700 def ParseCodereviewSettingsContent(content):
701 keyvals = {}
702 keyvals['CODE_REVIEW_SERVER'] = 'gerrit.chromium.org'
703 keyvals['GERRIT_HOST'] = 'gerrit.chromium.org'
704 keyvals['GERRIT_PORT'] = '29418'
705 return keyvals
706 self.mock(git_cl.gclient_utils, 'ParseCodereviewSettingsContent',
707 ParseCodereviewSettingsContent)
708 self.mock(git_cl.os, 'access', self._mocked_call)
709 self.mock(git_cl.os, 'chmod', self._mocked_call)
ukai@chromium.org91655502012-05-25 01:46:07 +0000710 src_dir = os.path.join(os.path.sep, 'usr', 'local', 'src')
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000711 def AbsPath(path):
ukai@chromium.org91655502012-05-25 01:46:07 +0000712 if not path.startswith(os.path.sep):
713 return os.path.join(src_dir, path)
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000714 return path
715 self.mock(git_cl.os.path, 'abspath', AbsPath)
ukai@chromium.org91655502012-05-25 01:46:07 +0000716 commit_msg_path = os.path.join(src_dir, '.git', 'hooks', 'commit-msg')
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000717 def Exists(path):
ukai@chromium.org91655502012-05-25 01:46:07 +0000718 if path == commit_msg_path:
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000719 return False
720 # others paths, such as /usr/share/locale/....
721 return True
722 self.mock(git_cl.os.path, 'exists', Exists)
joshua.lock@intel.com426f69b2012-08-02 23:41:49 +0000723 self.mock(git_cl, 'urlretrieve', self._mocked_call)
ukai@chromium.org712d6102013-11-27 00:52:58 +0000724 self.mock(git_cl, 'hasSheBang', self._mocked_call)
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000725 self.calls = [
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000726 ((['git', 'config', 'rietveld.autoupdate'],),
727 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000728 ((['git', 'config', 'rietveld.server',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000729 'gerrit.chromium.org'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000730 ((['git', 'config', '--unset-all', 'rietveld.cc'],), ''),
731 ((['git', 'config', '--unset-all',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000732 'rietveld.private'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000733 ((['git', 'config', '--unset-all',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000734 'rietveld.tree-status-url'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000735 ((['git', 'config', '--unset-all',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000736 'rietveld.viewvc-url'],), ''),
rmistry@google.com90752582014-01-14 21:04:50 +0000737 ((['git', 'config', '--unset-all',
738 'rietveld.bug-prefix'],), ''),
thestig@chromium.org44202a22014-03-11 19:22:18 +0000739 ((['git', 'config', '--unset-all',
740 'rietveld.cpplint-regex'],), ''),
741 ((['git', 'config', '--unset-all',
kjellander@chromium.org6abc6522014-12-02 07:34:49 +0000742 'rietveld.force-https-commit-url'],), ''),
743 ((['git', 'config', '--unset-all',
thestig@chromium.org44202a22014-03-11 19:22:18 +0000744 'rietveld.cpplint-ignore-regex'],), ''),
sheyang@chromium.org152cf832014-06-11 21:37:49 +0000745 ((['git', 'config', '--unset-all',
746 'rietveld.project'],), ''),
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +0000747 ((['git', 'config', '--unset-all',
748 'rietveld.pending-ref-prefix'],), ''),
rmistry@google.com5626a922015-02-26 14:03:30 +0000749 ((['git', 'config', '--unset-all',
750 'rietveld.run-post-upload-hook'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000751 ((['git', 'config', 'gerrit.host',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000752 'gerrit.chromium.org'],), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000753 # DownloadHooks(False)
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000754 ((['git', 'config', 'gerrit.host'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000755 'gerrit.chromium.org'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000756 ((['git', 'rev-parse', '--show-cdup'],), ''),
ukai@chromium.org91655502012-05-25 01:46:07 +0000757 ((commit_msg_path, os.X_OK,), False),
ukai@chromium.org712d6102013-11-27 00:52:58 +0000758 (('https://gerrit-review.googlesource.com/tools/hooks/commit-msg',
ukai@chromium.org91655502012-05-25 01:46:07 +0000759 commit_msg_path,), ''),
ukai@chromium.org712d6102013-11-27 00:52:58 +0000760 ((commit_msg_path,), True),
ukai@chromium.org91655502012-05-25 01:46:07 +0000761 ((commit_msg_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR,), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000762 # GetCodereviewSettingsInteractively
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000763 ((['git', 'config', 'rietveld.server'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000764 'gerrit.chromium.org'),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000765 (('Rietveld server (host[:port]) [https://gerrit.chromium.org]:',),
766 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000767 ((['git', 'config', 'rietveld.cc'],), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000768 (('CC list:',), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000769 ((['git', 'config', 'rietveld.private'],), ''),
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000770 (('Private flag (rietveld only):',), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000771 ((['git', 'config', 'rietveld.tree-status-url'],), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000772 (('Tree status URL:',), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000773 ((['git', 'config', 'rietveld.viewvc-url'],), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000774 (('ViewVC URL:',), ''),
775 # DownloadHooks(True)
rmistry@google.com90752582014-01-14 21:04:50 +0000776 ((['git', 'config', 'rietveld.bug-prefix'],), ''),
777 (('Bug Prefix:',), ''),
rmistry@google.com5626a922015-02-26 14:03:30 +0000778 ((['git', 'config', 'rietveld.run-post-upload-hook'],), ''),
779 (('Run Post Upload Hook:',), ''),
ukai@chromium.org91655502012-05-25 01:46:07 +0000780 ((commit_msg_path, os.X_OK,), True),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000781 ]
782 git_cl.main(['config'])
783
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000784 def test_update_reviewers(self):
785 data = [
786 ('foo', [], 'foo'),
agable@chromium.org42c20792013-09-12 17:34:49 +0000787 ('foo\nR=xx', [], 'foo\nR=xx'),
788 ('foo\nTBR=xx', [], 'foo\nTBR=xx'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000789 ('foo', ['a@c'], 'foo\n\nR=a@c'),
agable@chromium.org42c20792013-09-12 17:34:49 +0000790 ('foo\nR=xx', ['a@c'], 'foo\n\nR=a@c, xx'),
791 ('foo\nTBR=xx', ['a@c'], 'foo\n\nR=a@c\nTBR=xx'),
792 ('foo\nTBR=xx\nR=yy', ['a@c'], 'foo\n\nR=a@c, yy\nTBR=xx'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000793 ('foo\nBUG=', ['a@c'], 'foo\nBUG=\nR=a@c'),
agable@chromium.org42c20792013-09-12 17:34:49 +0000794 ('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 +0000795 ('foo', ['a@c', 'b@c'], 'foo\n\nR=a@c, b@c'),
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +0000796 ('foo\nBar\n\nR=\nBUG=', ['c@c'], 'foo\nBar\n\nR=c@c\nBUG='),
797 ('foo\nBar\n\nR=\nBUG=\nR=', ['c@c'], 'foo\nBar\n\nR=c@c\nBUG='),
798 # Same as the line before, but full of whitespaces.
799 (
800 'foo\nBar\n\n R = \n BUG = \n R = ', ['c@c'],
801 'foo\nBar\n\nR=c@c\n BUG =',
802 ),
803 # Whitespaces aren't interpreted as new lines.
804 ('foo BUG=allo R=joe ', ['c@c'], 'foo BUG=allo R=joe\n\nR=c@c'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000805 ]
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +0000806 expected = [i[2] for i in data]
807 actual = []
808 for orig, reviewers, _expected in data:
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000809 obj = git_cl.ChangeDescription(orig)
810 obj.update_reviewers(reviewers)
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +0000811 actual.append(obj.description)
812 self.assertEqual(expected, actual)
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000813
wittman@chromium.org455dc922015-01-26 20:15:50 +0000814 def test_get_target_ref(self):
815 # Check remote or remote branch not present.
816 self.assertEqual(None, git_cl.GetTargetRef('origin', None, 'master', None))
817 self.assertEqual(None, git_cl.GetTargetRef(None,
818 'refs/remotes/origin/master',
819 'master', None))
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000820
wittman@chromium.org455dc922015-01-26 20:15:50 +0000821 # Check default target refs for branches.
822 self.assertEqual('refs/heads/master',
823 git_cl.GetTargetRef('origin', 'refs/remotes/origin/master',
824 None, None))
825 self.assertEqual('refs/heads/master',
826 git_cl.GetTargetRef('origin', 'refs/remotes/origin/lkgr',
827 None, None))
828 self.assertEqual('refs/heads/master',
829 git_cl.GetTargetRef('origin', 'refs/remotes/origin/lkcr',
830 None, None))
831 self.assertEqual('refs/branch-heads/123',
832 git_cl.GetTargetRef('origin',
833 'refs/remotes/branch-heads/123',
834 None, None))
835 self.assertEqual('refs/diff/test',
836 git_cl.GetTargetRef('origin',
837 'refs/remotes/origin/refs/diff/test',
838 None, None))
rmistry@google.comc68112d2015-03-03 12:48:06 +0000839 self.assertEqual('refs/heads/chrome/m42',
840 git_cl.GetTargetRef('origin',
841 'refs/remotes/origin/chrome/m42',
842 None, None))
wittman@chromium.org455dc922015-01-26 20:15:50 +0000843
844 # Check target refs for user-specified target branch.
845 for branch in ('branch-heads/123', 'remotes/branch-heads/123',
846 'refs/remotes/branch-heads/123'):
847 self.assertEqual('refs/branch-heads/123',
848 git_cl.GetTargetRef('origin',
849 'refs/remotes/origin/master',
850 branch, None))
851 for branch in ('origin/master', 'remotes/origin/master',
852 'refs/remotes/origin/master'):
853 self.assertEqual('refs/heads/master',
854 git_cl.GetTargetRef('origin',
855 'refs/remotes/branch-heads/123',
856 branch, None))
857 for branch in ('master', 'heads/master', 'refs/heads/master'):
858 self.assertEqual('refs/heads/master',
859 git_cl.GetTargetRef('origin',
860 'refs/remotes/branch-heads/123',
861 branch, None))
862
863 # Check target refs for pending prefix.
864 self.assertEqual('prefix/heads/master',
865 git_cl.GetTargetRef('origin', 'refs/remotes/origin/master',
866 None, 'prefix/'))
867
wychen@chromium.orga872e752015-04-28 23:42:18 +0000868 def test_patch_when_dirty(self):
869 # Patch when local tree is dirty
870 self.mock(git_common, 'is_dirty_git_tree', lambda x: True)
871 self.assertNotEqual(git_cl.main(['patch', '123456']), 0)
872
873 def test_diff_when_dirty(self):
874 # Do 'git cl diff' when local tree is dirty
875 self.mock(git_common, 'is_dirty_git_tree', lambda x: True)
876 self.assertNotEqual(git_cl.main(['diff']), 0)
877
878 def _patch_common(self):
879 self.mock(git_cl.Changelist, 'GetMostRecentPatchset', lambda x: '60001')
880 self.mock(git_cl.Changelist, 'GetPatchSetDiff', lambda *args: None)
881 self.mock(git_cl.Changelist, 'SetIssue', lambda *args: None)
882 self.mock(git_cl.Changelist, 'SetPatchset', lambda *args: None)
883 self.mock(git_cl, 'IsGitVersionAtLeast', lambda *args: True)
884
885 self.calls = [
886 ((['git', 'config', 'rietveld.autoupdate'],), ''),
887 ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'),
888 ((['git', 'rev-parse', '--show-cdup'],), ''),
889 ((['sed', '-e', 's|^--- a/|--- |; s|^+++ b/|+++ |'],), ''),
890 ]
891
892 def test_patch_successful(self):
893 self._patch_common()
894 self.calls += [
895 ((['git', 'apply', '--index', '-p0', '--3way'],), ''),
896 ((['git', 'commit', '-m',
897 'patch from issue 123456 at patchset 60001 ' +
898 '(http://crrev.com/123456#ps60001)'],), ''),
899 ]
900 self.assertEqual(git_cl.main(['patch', '123456']), 0)
901
902 def test_patch_conflict(self):
903 self._patch_common()
904 self.calls += [
905 ((['git', 'apply', '--index', '-p0', '--3way'],), '',
906 subprocess2.CalledProcessError(1, '', '', '', '')),
907 ]
908 self.assertNotEqual(git_cl.main(['patch', '123456']), 0)
wittman@chromium.org455dc922015-01-26 20:15:50 +0000909
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000910if __name__ == '__main__':
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000911 git_cl.logging.basicConfig(
912 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000913 unittest.main()