blob: 1be529000fdec59ad5f132c7e747ec680ef3c29c [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
19import subprocess2
20
21
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
maruel@chromium.orgddd59412011-11-30 14:20:38 +000069class TestGitCl(TestCase):
70 def setUp(self):
71 super(TestGitCl, self).setUp()
72 self.calls = []
73 self._calls_done = 0
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000074 self.mock(subprocess2, 'call', self._mocked_call)
75 self.mock(subprocess2, 'check_call', self._mocked_call)
76 self.mock(subprocess2, 'check_output', self._mocked_call)
77 self.mock(subprocess2, 'communicate', self._mocked_call)
78 self.mock(subprocess2, 'Popen', self._mocked_call)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000079 self.mock(git_cl, 'FindCodereviewSettingsFile', lambda: '')
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000080 self.mock(git_cl, 'ask_for_data', self._mocked_call)
81 self.mock(git_cl.breakpad, 'post', self._mocked_call)
82 self.mock(git_cl.breakpad, 'SendStack', self._mocked_call)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000083 self.mock(git_cl.presubmit_support, 'DoPresubmitChecks', PresubmitMock)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000084 self.mock(git_cl.rietveld, 'Rietveld', RietveldMock)
maruel@chromium.org4bac4b52012-11-27 20:33:52 +000085 self.mock(git_cl.rietveld, 'CachingRietveld', RietveldMock)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000086 self.mock(git_cl.upload, 'RealMain', self.fail)
maruel@chromium.orgddd59412011-11-30 14:20:38 +000087 self.mock(git_cl.watchlists, 'Watchlists', WatchlistsMock)
88 # It's important to reset settings to not have inter-tests interference.
89 git_cl.settings = None
90
91 def tearDown(self):
92 if not self.has_failed():
93 self.assertEquals([], self.calls)
94 super(TestGitCl, self).tearDown()
95
maruel@chromium.org2e72bb12012-01-17 15:18:35 +000096 def _mocked_call(self, *args, **kwargs):
97 self.assertTrue(
98 self.calls,
99 '@%d Expected: <Missing> Actual: %r' % (self._calls_done, args))
100 expected_args, result = self.calls.pop(0)
maruel@chromium.orge52678e2013-04-26 18:34:44 +0000101 # Also logs otherwise it could get caught in a try/finally and be hard to
102 # diagnose.
103 if expected_args != args:
104 msg = '@%d Expected: %r Actual: %r' % (
105 self._calls_done, expected_args, args)
106 git_cl.logging.error(msg)
107 self.fail(msg)
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000108 self._calls_done += 1
109 return result
110
maruel@chromium.orga3353652011-11-30 14:26:57 +0000111 @classmethod
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000112 def _upload_calls(cls, similarity, find_copies, private):
iannucci@chromium.org79540052012-10-19 23:15:26 +0000113 return (cls._git_base_calls(similarity, find_copies) +
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000114 cls._git_upload_calls(private))
maruel@chromium.orga3353652011-11-30 14:26:57 +0000115
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000116 @classmethod
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000117 def _upload_no_rev_calls(cls, similarity, find_copies):
118 return (cls._git_base_calls(similarity, find_copies) +
119 cls._git_upload_no_rev_calls())
120
121 @classmethod
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000122 def _git_base_calls(cls, similarity, find_copies):
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000123 if similarity is None:
124 similarity = '50'
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000125 similarity_call = ((['git', 'config', '--int', '--get',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000126 'branch.master.git-cl-similarity'],), '')
127 else:
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000128 similarity_call = ((['git', 'config', '--int',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000129 'branch.master.git-cl-similarity', similarity],), '')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000130
131 if find_copies is None:
132 find_copies = True
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000133 find_copies_call = ((['git', 'config', '--int', '--get',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000134 'branch.master.git-find-copies'],), '')
135 else:
136 val = str(int(find_copies))
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000137 find_copies_call = ((['git', 'config', '--int',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000138 'branch.master.git-find-copies', val],), '')
139
140 if find_copies:
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000141 stat_call = ((['git', 'diff', '--no-ext-diff', '--stat',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000142 '--find-copies-harder', '-l100000', '-C'+similarity,
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000143 'fake_ancestor_sha', 'HEAD'],), '+dat')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000144 else:
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000145 stat_call = ((['git', 'diff', '--no-ext-diff', '--stat',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000146 '-M'+similarity, 'fake_ancestor_sha', 'HEAD'],), '+dat')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000147
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000148 return [
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000149 ((['git', 'config', 'rietveld.autoupdate'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000150 ((['git', 'config', 'rietveld.server'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000151 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000152 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000153 similarity_call,
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000154 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
iannucci@chromium.org79540052012-10-19 23:15:26 +0000155 find_copies_call,
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000156 ((['git', 'update-index', '--refresh', '-q'],), ''),
157 ((['git', 'diff-index', '--name-status', 'HEAD'],), ''),
158 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
159 ((['git', 'config', 'branch.master.merge'],), 'master'),
160 ((['git', 'config', 'branch.master.remote'],), 'origin'),
161 ((['git', 'merge-base', 'master', 'HEAD'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000162 'fake_ancestor_sha'),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000163 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000164 ((['git', 'rev-parse', '--show-cdup'],), ''),
165 ((['git', 'rev-parse', 'HEAD'],), '12345'),
166 ((['git', 'diff', '--name-status', '--no-renames', '-r',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000167 'fake_ancestor_sha...', '.'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000168 'M\t.gitignore\n'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000169 ((['git', 'config', 'branch.master.rietveldissue'],), ''),
170 ((['git', 'config', 'branch.master.rietveldpatchset'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000171 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000172 ((['git', 'log', '--pretty=format:%s%n%n%b',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000173 'fake_ancestor_sha...'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000174 'foo'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000175 ((['git', 'config', 'user.email'],), 'me@example.com'),
iannucci@chromium.org79540052012-10-19 23:15:26 +0000176 stat_call,
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000177 ((['git', 'config', 'gerrit.host'],), ''),
178 ((['git', 'log', '--pretty=format:%s\n\n%b',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000179 'fake_ancestor_sha..HEAD'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000180 'desc\n'),
rmistry@google.com90752582014-01-14 21:04:50 +0000181 ((['git', 'config', 'rietveld.bug-prefix'],), ''),
maruel@chromium.orga3353652011-11-30 14:26:57 +0000182 ]
183
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000184 @classmethod
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000185 def _git_upload_no_rev_calls(cls):
186 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000187 ((['git', 'config', 'core.editor'],), ''),
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000188 ]
189
190 @classmethod
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000191 def _git_upload_calls(cls, private):
192 if private:
tyoshino@chromium.org99918ab2013-09-30 06:17:28 +0000193 cc_call = []
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000194 private_call = []
195 else:
tyoshino@chromium.org99918ab2013-09-30 06:17:28 +0000196 cc_call = [((['git', 'config', 'rietveld.cc'],), '')]
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000197 private_call = [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000198 ((['git', 'config', 'rietveld.private'],), '')]
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000199
maruel@chromium.orga3353652011-11-30 14:26:57 +0000200 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000201 ((['git', 'config', 'core.editor'],), ''),
tyoshino@chromium.org99918ab2013-09-30 06:17:28 +0000202 ] + cc_call + private_call + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000203 ((['git', 'config', 'branch.master.base-url'],), ''),
204 ((['git',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000205 'config', '--local', '--get-regexp', '^svn-remote\\.'],),
206 (('', None), 0)),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000207 ((['git', 'rev-parse', '--show-cdup'],), ''),
208 ((['git', 'svn', 'info'],), ''),
209 ((['git',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000210 'config', 'branch.master.rietveldissue', '1'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000211 ((['git', 'config', 'branch.master.rietveldserver',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000212 'https://codereview.example.com'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000213 ((['git',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000214 'config', 'branch.master.rietveldpatchset', '2'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000215 ((['git', 'rev-parse', 'HEAD'],), 'hash'),
216 ((['git', 'symbolic-ref', 'HEAD'],), 'hash'),
217 ((['git',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000218 'config', 'branch.hash.last-upload-hash', 'hash'],), ''),
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000219 ]
220
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000221 @staticmethod
222 def _git_sanity_checks(diff_base, working_branch):
223 fake_ancestor = 'fake_ancestor'
224 fake_cl = 'fake_cl_for_patch'
225 return [
226 # Calls to verify branch point is ancestor
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000227 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000228 'rev-parse', '--verify', diff_base],), fake_ancestor),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000229 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000230 'merge-base', fake_ancestor, 'HEAD'],), fake_ancestor),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000231 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000232 'rev-list', '^' + fake_ancestor, 'HEAD'],), fake_cl),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000233 # Mock a config miss (error code 1)
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000234 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000235 'config', 'gitcl.remotebranch'],), (('', None), 1)),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000236 # Call to GetRemoteBranch()
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000237 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000238 'config', 'branch.%s.merge' % working_branch],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000239 'refs/heads/master'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000240 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000241 'config', 'branch.%s.remote' % working_branch],), 'origin'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000242 ((['git', 'rev-list', '^' + fake_ancestor,
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000243 'refs/remotes/origin/master'],), ''),
244 ]
245
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000246 @classmethod
247 def _dcommit_calls_1(cls):
248 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000249 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000250 'config', '--local', '--get-regexp', '^svn-remote\\.'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000251 ((('svn-remote.svn.url svn://svn.chromium.org/chrome\n'
252 'svn-remote.svn.fetch trunk/src:refs/remotes/origin/master'),
253 None),
254 0)),
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000255 ((['git', 'config', 'rietveld.autoupdate'],),
256 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000257 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000258 'config', 'rietveld.server'],), 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000259 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
260 ((['git', 'config', '--int', '--get',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000261 'branch.working.git-cl-similarity'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000262 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
263 ((['git', 'config', '--int', '--get',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000264 'branch.working.git-find-copies'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000265 ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'),
266 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000267 'config', 'branch.working.merge'],), 'refs/heads/master'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000268 ((['git', 'config', 'branch.working.remote'],), 'origin'),
269 ((['git', 'rev-list', '--merges',
szager@chromium.orge84b7542012-06-15 21:26:58 +0000270 '--grep=^SVN changes up to revision [0-9]*$',
szager@chromium.org9bb85e22012-06-13 20:28:23 +0000271 'refs/remotes/origin/master^!'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000272 ((['git', 'update-index', '--refresh', '-q'],), ''),
273 ((['git', 'diff-index', '--name-status', 'HEAD'],), ''),
274 ((['git', 'rev-list', '^refs/heads/working',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000275 'refs/remotes/origin/master'],),
276 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000277 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000278 'log', '--grep=^git-svn-id:', '-1', '--pretty=format:%H'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000279 '3fc18b62c4966193eb435baabe2d18a3810ec82e'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000280 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000281 'rev-list', '^3fc18b62c4966193eb435baabe2d18a3810ec82e',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000282 'refs/remotes/origin/master'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000283 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000284 'merge-base', 'refs/remotes/origin/master', 'HEAD'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000285 'fake_ancestor_sha'),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000286 ]
287
288 @classmethod
289 def _dcommit_calls_normal(cls):
290 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000291 ((['git', 'rev-parse', '--show-cdup'],), ''),
292 ((['git', 'rev-parse', 'HEAD'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000293 '00ff397798ea57439712ed7e04ab96e13969ef40'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000294 ((['git',
mcgrathr@chromium.org9249f642013-06-03 21:36:18 +0000295 'diff', '--name-status', '--no-renames', '-r', 'fake_ancestor_sha...',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000296 '.'],),
297 'M\tPRESUBMIT.py'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000298 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000299 'config', 'branch.working.rietveldissue'],), '12345'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000300 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000301 'config', 'branch.working.rietveldpatchset'],), '31137'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000302 ((['git', 'config', 'branch.working.rietveldserver'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000303 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000304 ((['git', 'config', 'user.email'],), 'author@example.com'),
305 ((['git', 'config', 'rietveld.tree-status-url'],), ''),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000306 ]
307
308 @classmethod
309 def _dcommit_calls_bypassed(cls):
310 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000311 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000312 'config', 'branch.working.rietveldissue'],), '12345'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000313 ((['git', 'config', 'branch.working.rietveldserver'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000314 'codereview.example.com'),
jochen@chromium.org3ec0d542014-01-14 20:00:03 +0000315 ((['git', 'config', 'rietveld.tree-status-url'],), ''),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000316 (('GitClHooksBypassedCommit',
317 'Issue https://codereview.example.com/12345 bypassed hook when '
jochen@chromium.org3ec0d542014-01-14 20:00:03 +0000318 'committing (tree status was "unset")'), None),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000319 ]
320
321 @classmethod
322 def _dcommit_calls_3(cls):
323 return [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000324 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000325 'diff', '--no-ext-diff', '--stat', '--find-copies-harder',
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000326 '-l100000', '-C50', 'fake_ancestor_sha',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000327 'refs/heads/working'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000328 (' PRESUBMIT.py | 2 +-\n'
329 ' 1 files changed, 1 insertions(+), 1 deletions(-)\n')),
330 (('About to commit; enter to confirm.',), None),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000331 ((['git', 'show-ref', '--quiet', '--verify',
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000332 'refs/heads/git-cl-commit'],),
333 (('', None), 0)),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000334 ((['git', 'branch', '-D', 'git-cl-commit'],), ''),
335 ((['git', 'show-ref', '--quiet', '--verify',
szager@chromium.org9bb85e22012-06-13 20:28:23 +0000336 'refs/heads/git-cl-cherry-pick'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000337 ((['git', 'rev-parse', '--show-cdup'],), '\n'),
338 ((['git', 'checkout', '-q', '-b', 'git-cl-commit'],), ''),
339 ((['git', 'reset', '--soft', 'fake_ancestor_sha'],), ''),
340 ((['git', 'commit', '-m',
maruel@chromium.orge52678e2013-04-26 18:34:44 +0000341 'Issue: 12345\n\nR=john@chromium.org\n\n'
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000342 'Review URL: https://codereview.example.com/12345'],),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000343 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000344 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000345 'svn', 'dcommit', '-C50', '--no-rebase', '--rmdir'],),
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000346 (('', None), 0)),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000347 ((['git', 'checkout', '-q', 'working'],), ''),
348 ((['git', 'branch', '-D', 'git-cl-commit'],), ''),
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000349 ]
350
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000351 @staticmethod
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000352 def _cmd_line(description, args, similarity, find_copies, private):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000353 """Returns the upload command line passed to upload.RealMain()."""
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000354 return [
355 'upload', '--assume_yes', '--server',
maruel@chromium.orgeb5edbc2012-01-16 17:03:28 +0000356 'https://codereview.example.com',
maruel@chromium.org71e12a92012-02-14 02:34:15 +0000357 '--message', description
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000358 ] + args + [
359 '--cc', 'joe@example.com',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000360 ] + (['--private'] if private else []) + [
iannucci@chromium.org79540052012-10-19 23:15:26 +0000361 '--git_similarity', similarity or '50'
362 ] + (['--git_no_find_copies'] if find_copies == False else []) + [
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000363 'fake_ancestor_sha', 'HEAD'
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000364 ]
365
366 def _run_reviewer_test(
367 self,
368 upload_args,
369 expected_description,
370 returned_description,
371 final_description,
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000372 reviewers,
373 private=False):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000374 """Generic reviewer test framework."""
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000375 try:
376 similarity = upload_args[upload_args.index('--similarity')+1]
377 except ValueError:
378 similarity = None
iannucci@chromium.org79540052012-10-19 23:15:26 +0000379
380 if '--find-copies' in upload_args:
381 find_copies = True
382 elif '--no-find-copies' in upload_args:
383 find_copies = False
384 else:
385 find_copies = None
386
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000387 private = '--private' in upload_args
388
389 self.calls = self._upload_calls(similarity, find_copies, private)
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000390
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000391 def RunEditor(desc, _, **kwargs):
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000392 self.assertEquals(
393 '# Enter a description of the change.\n'
janx@chromium.org104b2db2013-04-18 12:58:40 +0000394 '# This will be displayed on the codereview site.\n'
alancutter@chromium.org63a4d7f2013-05-31 02:22:45 +0000395 '# The first line will also be used as the subject of the review.\n'
alancutter@chromium.orgbd1073e2013-06-01 00:34:38 +0000396 '#--------------------This line is 72 characters long'
397 '--------------------\n' +
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000398 expected_description,
399 desc)
400 return returned_description
401 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor)
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000402
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000403 def check_upload(args):
iannucci@chromium.org79540052012-10-19 23:15:26 +0000404 cmd_line = self._cmd_line(final_description, reviewers, similarity,
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000405 find_copies, private)
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000406 self.assertEquals(cmd_line, args)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000407 return 1, 2
408 self.mock(git_cl.upload, 'RealMain', check_upload)
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000409
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000410 git_cl.main(['upload'] + upload_args)
411
412 def test_no_reviewer(self):
413 self._run_reviewer_test(
414 [],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000415 'desc\n\nBUG=',
416 '# Blah blah comment.\ndesc\n\nBUG=',
417 'desc\n\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000418 [])
419
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000420 def test_keep_similarity(self):
421 self._run_reviewer_test(
422 ['--similarity', '70'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000423 'desc\n\nBUG=',
424 '# Blah blah comment.\ndesc\n\nBUG=',
425 'desc\n\nBUG=',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000426 [])
427
iannucci@chromium.org79540052012-10-19 23:15:26 +0000428 def test_keep_find_copies(self):
429 self._run_reviewer_test(
430 ['--no-find-copies'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000431 'desc\n\nBUG=',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000432 '# Blah blah comment.\ndesc\n\nBUG=\n',
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000433 'desc\n\nBUG=',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000434 [])
435
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000436 def test_private(self):
437 self._run_reviewer_test(
438 ['--private'],
439 'desc\n\nBUG=',
440 '# Blah blah comment.\ndesc\n\nBUG=\n',
441 'desc\n\nBUG=',
442 [])
443
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000444 def test_reviewers_cmd_line(self):
445 # Reviewer is passed as-is
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000446 description = 'desc\n\nR=foo@example.com\nBUG='
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000447 self._run_reviewer_test(
448 ['-r' 'foo@example.com'],
449 description,
450 '\n%s\n' % description,
451 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000452 ['--reviewers=foo@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000453
454 def test_reviewer_tbr_overriden(self):
455 # Reviewer is overriden with TBR
456 # Also verifies the regexp work without a trailing LF
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000457 description = 'Foo Bar\n\nTBR=reviewer@example.com'
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000458 self._run_reviewer_test(
459 ['-r' 'foo@example.com'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000460 'desc\n\nR=foo@example.com\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000461 description.strip('\n'),
462 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000463 ['--reviewers=reviewer@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000464
465 def test_reviewer_multiple(self):
466 # Handles multiple R= or TBR= lines.
467 description = (
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000468 'Foo Bar\nTBR=reviewer@example.com\nBUG=\nR=another@example.com')
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000469 self._run_reviewer_test(
470 [],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000471 'desc\n\nBUG=',
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000472 description,
473 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000474 ['--reviewers=another@example.com,reviewer@example.com'])
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000475
maruel@chromium.orga3353652011-11-30 14:26:57 +0000476 def test_reviewer_send_mail(self):
477 # --send-mail can be used without -r if R= is used
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000478 description = 'Foo Bar\nR=reviewer@example.com'
maruel@chromium.orga3353652011-11-30 14:26:57 +0000479 self._run_reviewer_test(
480 ['--send-mail'],
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000481 'desc\n\nBUG=',
maruel@chromium.orga3353652011-11-30 14:26:57 +0000482 description.strip('\n'),
483 description,
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000484 ['--reviewers=reviewer@example.com', '--send_mail'])
maruel@chromium.orga3353652011-11-30 14:26:57 +0000485
486 def test_reviewer_send_mail_no_rev(self):
487 # Fails without a reviewer.
maruel@chromium.org2e23ce32013-05-07 12:42:28 +0000488 stdout = StringIO.StringIO()
489 stderr = StringIO.StringIO()
maruel@chromium.orga3353652011-11-30 14:26:57 +0000490 try:
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000491 self.calls = self._upload_no_rev_calls(None, None)
492 def RunEditor(desc, _, **kwargs):
maruel@chromium.orga3353652011-11-30 14:26:57 +0000493 return desc
494 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor)
maruel@chromium.org2e23ce32013-05-07 12:42:28 +0000495 self.mock(sys, 'stdout', stdout)
496 self.mock(sys, 'stderr', stderr)
maruel@chromium.orga3353652011-11-30 14:26:57 +0000497 git_cl.main(['upload', '--send-mail'])
498 self.fail()
499 except SystemExit:
maruel@chromium.org2e23ce32013-05-07 12:42:28 +0000500 self.assertEqual(
501 'Using 50% similarity for rename/copy detection. Override with '
502 '--similarity.\n',
503 stdout.getvalue())
504 self.assertEqual(
505 'Must specify reviewers to send email.\n', stderr.getvalue())
maruel@chromium.orga3353652011-11-30 14:26:57 +0000506
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000507 def test_dcommit(self):
508 self.calls = (
509 self._dcommit_calls_1() +
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000510 self._git_sanity_checks('fake_ancestor_sha', 'working') +
maruel@chromium.org2e72bb12012-01-17 15:18:35 +0000511 self._dcommit_calls_normal() +
512 self._dcommit_calls_3())
513 git_cl.main(['dcommit'])
514
515 def test_dcommit_bypass_hooks(self):
516 self.calls = (
517 self._dcommit_calls_1() +
518 self._dcommit_calls_bypassed() +
519 self._dcommit_calls_3())
520 git_cl.main(['dcommit', '--bypass-hooks'])
521
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000522
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000523 @classmethod
524 def _gerrit_base_calls(cls):
ukai@chromium.orge8077812012-02-03 03:41:46 +0000525 return [
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000526 ((['git', 'config', 'rietveld.autoupdate'],),
527 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000528 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000529 'config', 'rietveld.server'],), 'codereview.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000530 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
531 ((['git', 'config', '--int', '--get',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000532 'branch.master.git-cl-similarity'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000533 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
534 ((['git', 'config', '--int', '--get',
iannucci@chromium.org79540052012-10-19 23:15:26 +0000535 'branch.master.git-find-copies'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000536 ((['git', 'update-index', '--refresh', '-q'],), ''),
537 ((['git', 'diff-index', '--name-status', 'HEAD'],), ''),
538 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
539 ((['git', 'config', 'branch.master.merge'],), 'master'),
540 ((['git', 'config', 'branch.master.remote'],), 'origin'),
541 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000542 'merge-base', 'master', 'HEAD'],), 'fake_ancestor_sha'),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000543 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000544 ((['git', 'rev-parse', '--show-cdup'],), ''),
545 ((['git', 'rev-parse', 'HEAD'],), '12345'),
546 ((['git',
mcgrathr@chromium.org9249f642013-06-03 21:36:18 +0000547 'diff', '--name-status', '--no-renames', '-r',
548 'fake_ancestor_sha...', '.'],),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000549 'M\t.gitignore\n'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000550 ((['git', 'config', 'branch.master.rietveldissue'],), ''),
551 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000552 'config', 'branch.master.rietveldpatchset'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000553 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000554 'log', '--pretty=format:%s%n%n%b', 'fake_ancestor_sha...'],),
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +0000555 'foo'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000556 ((['git', 'config', 'user.email'],), 'me@example.com'),
557 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000558 'diff', '--no-ext-diff', '--stat', '--find-copies-harder',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000559 '-l100000', '-C50', 'fake_ancestor_sha', 'HEAD'],),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000560 '+dat'),
561 ]
562
563 @staticmethod
564 def _gerrit_upload_calls(description, reviewers):
565 calls = [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000566 ((['git', 'config', 'gerrit.host'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000567 'gerrit.example.com'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000568 ((['git', 'log', '--pretty=format:%s\n\n%b',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000569 'fake_ancestor_sha..HEAD'],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000570 description)
571 ]
572 if git_cl.CHANGE_ID not in description:
573 calls += [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000574 ((['git', 'log', '--pretty=format:%s\n\n%b',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000575 'fake_ancestor_sha..HEAD'],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000576 description),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000577 ((['git', 'commit', '--amend', '-m', description],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000578 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000579 ((['git', 'log', '--pretty=format:%s\n\n%b',
sbc@chromium.org5e07e062013-02-28 23:55:44 +0000580 'fake_ancestor_sha..HEAD'],),
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000581 description)
582 ]
583 calls += [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000584 ((['git', 'config', 'rietveld.cc'],), '')
ukai@chromium.orge8077812012-02-03 03:41:46 +0000585 ]
ukai@chromium.org19bbfa22012-02-03 16:18:11 +0000586 receive_pack = '--receive-pack=git receive-pack '
ukai@chromium.orge8077812012-02-03 03:41:46 +0000587 receive_pack += '--cc=joe@example.com' # from watch list
588 if reviewers:
589 receive_pack += ' '
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000590 receive_pack += ' '.join(
591 '--reviewer=' + email for email in sorted(reviewers))
ukai@chromium.org19bbfa22012-02-03 16:18:11 +0000592 receive_pack += ''
ukai@chromium.orge8077812012-02-03 03:41:46 +0000593 calls += [
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000594 ((['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000595 'push', receive_pack, 'origin', 'HEAD:refs/for/master'],),
ukai@chromium.orge8077812012-02-03 03:41:46 +0000596 '')
597 ]
598 return calls
599
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000600 def _run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000601 self,
602 upload_args,
603 description,
604 reviewers):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000605 """Generic gerrit upload test framework."""
ukai@chromium.orge8077812012-02-03 03:41:46 +0000606 self.calls = self._gerrit_base_calls()
607 self.calls += self._gerrit_upload_calls(description, reviewers)
608 git_cl.main(['upload'] + upload_args)
609
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000610 def test_gerrit_upload_without_change_id(self):
611 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000612 [],
jamesr@chromium.org35d1a842012-07-27 00:20:43 +0000613 'desc\n\nBUG=\n',
ukai@chromium.orge8077812012-02-03 03:41:46 +0000614 [])
615
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000616 def test_gerrit_no_reviewer(self):
617 self._run_gerrit_upload_test(
618 [],
619 'desc\n\nBUG=\nChange-Id:123456789\n',
620 [])
621
ukai@chromium.orge8077812012-02-03 03:41:46 +0000622 def test_gerrit_reviewers_cmd_line(self):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000623 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000624 ['-r', 'foo@example.com'],
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000625 'desc\n\nBUG=\nChange-Id:123456789',
ukai@chromium.orge8077812012-02-03 03:41:46 +0000626 ['foo@example.com'])
627
628 def test_gerrit_reviewer_multiple(self):
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000629 self._run_gerrit_upload_test(
ukai@chromium.orge8077812012-02-03 03:41:46 +0000630 [],
sivachandra@chromium.orgaebe87f2012-10-22 20:34:21 +0000631 'desc\nTBR=reviewer@example.com\nBUG=\nR=another@example.com\n'
632 'Change-Id:123456789\n',
ukai@chromium.orge8077812012-02-03 03:41:46 +0000633 ['reviewer@example.com', 'another@example.com'])
634
635
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000636 def test_config_gerrit_download_hook(self):
637 self.mock(git_cl, 'FindCodereviewSettingsFile', CodereviewSettingsFileMock)
638 def ParseCodereviewSettingsContent(content):
639 keyvals = {}
640 keyvals['CODE_REVIEW_SERVER'] = 'gerrit.chromium.org'
641 keyvals['GERRIT_HOST'] = 'gerrit.chromium.org'
642 keyvals['GERRIT_PORT'] = '29418'
643 return keyvals
644 self.mock(git_cl.gclient_utils, 'ParseCodereviewSettingsContent',
645 ParseCodereviewSettingsContent)
646 self.mock(git_cl.os, 'access', self._mocked_call)
647 self.mock(git_cl.os, 'chmod', self._mocked_call)
ukai@chromium.org91655502012-05-25 01:46:07 +0000648 src_dir = os.path.join(os.path.sep, 'usr', 'local', 'src')
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000649 def AbsPath(path):
ukai@chromium.org91655502012-05-25 01:46:07 +0000650 if not path.startswith(os.path.sep):
651 return os.path.join(src_dir, path)
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000652 return path
653 self.mock(git_cl.os.path, 'abspath', AbsPath)
ukai@chromium.org91655502012-05-25 01:46:07 +0000654 commit_msg_path = os.path.join(src_dir, '.git', 'hooks', 'commit-msg')
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000655 def Exists(path):
ukai@chromium.org91655502012-05-25 01:46:07 +0000656 if path == commit_msg_path:
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000657 return False
658 # others paths, such as /usr/share/locale/....
659 return True
660 self.mock(git_cl.os.path, 'exists', Exists)
joshua.lock@intel.com426f69b2012-08-02 23:41:49 +0000661 self.mock(git_cl, 'urlretrieve', self._mocked_call)
ukai@chromium.org712d6102013-11-27 00:52:58 +0000662 self.mock(git_cl, 'hasSheBang', self._mocked_call)
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000663 self.calls = [
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000664 ((['git', 'config', 'rietveld.autoupdate'],),
665 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000666 ((['git', 'config', 'rietveld.server',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000667 'gerrit.chromium.org'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000668 ((['git', 'config', '--unset-all', 'rietveld.cc'],), ''),
669 ((['git', 'config', '--unset-all',
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000670 'rietveld.private'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000671 ((['git', 'config', '--unset-all',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000672 'rietveld.tree-status-url'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000673 ((['git', 'config', '--unset-all',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000674 'rietveld.viewvc-url'],), ''),
rmistry@google.com90752582014-01-14 21:04:50 +0000675 ((['git', 'config', '--unset-all',
676 'rietveld.bug-prefix'],), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000677 ((['git', 'config', 'gerrit.host',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000678 'gerrit.chromium.org'],), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000679 # DownloadHooks(False)
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000680 ((['git', 'config', 'gerrit.host'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000681 'gerrit.chromium.org'),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000682 ((['git', 'rev-parse', '--show-cdup'],), ''),
ukai@chromium.org91655502012-05-25 01:46:07 +0000683 ((commit_msg_path, os.X_OK,), False),
ukai@chromium.org712d6102013-11-27 00:52:58 +0000684 (('https://gerrit-review.googlesource.com/tools/hooks/commit-msg',
ukai@chromium.org91655502012-05-25 01:46:07 +0000685 commit_msg_path,), ''),
ukai@chromium.org712d6102013-11-27 00:52:58 +0000686 ((commit_msg_path,), True),
ukai@chromium.org91655502012-05-25 01:46:07 +0000687 ((commit_msg_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR,), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000688 # GetCodereviewSettingsInteractively
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000689 ((['git', 'config', 'rietveld.server'],),
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000690 'gerrit.chromium.org'),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000691 (('Rietveld server (host[:port]) [https://gerrit.chromium.org]:',),
692 ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000693 ((['git', 'config', 'rietveld.cc'],), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000694 (('CC list:',), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000695 ((['git', 'config', 'rietveld.private'],), ''),
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000696 (('Private flag (rietveld only):',), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000697 ((['git', 'config', 'rietveld.tree-status-url'],), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000698 (('Tree status URL:',), ''),
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000699 ((['git', 'config', 'rietveld.viewvc-url'],), ''),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000700 (('ViewVC URL:',), ''),
701 # DownloadHooks(True)
rmistry@google.com90752582014-01-14 21:04:50 +0000702 ((['git', 'config', 'rietveld.bug-prefix'],), ''),
703 (('Bug Prefix:',), ''),
ukai@chromium.org91655502012-05-25 01:46:07 +0000704 ((commit_msg_path, os.X_OK,), True),
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000705 ]
706 git_cl.main(['config'])
707
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000708 def test_update_reviewers(self):
709 data = [
710 ('foo', [], 'foo'),
agable@chromium.org42c20792013-09-12 17:34:49 +0000711 ('foo\nR=xx', [], 'foo\nR=xx'),
712 ('foo\nTBR=xx', [], 'foo\nTBR=xx'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000713 ('foo', ['a@c'], 'foo\n\nR=a@c'),
agable@chromium.org42c20792013-09-12 17:34:49 +0000714 ('foo\nR=xx', ['a@c'], 'foo\n\nR=a@c, xx'),
715 ('foo\nTBR=xx', ['a@c'], 'foo\n\nR=a@c\nTBR=xx'),
716 ('foo\nTBR=xx\nR=yy', ['a@c'], 'foo\n\nR=a@c, yy\nTBR=xx'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000717 ('foo\nBUG=', ['a@c'], 'foo\nBUG=\nR=a@c'),
agable@chromium.org42c20792013-09-12 17:34:49 +0000718 ('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 +0000719 ('foo', ['a@c', 'b@c'], 'foo\n\nR=a@c, b@c'),
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +0000720 ('foo\nBar\n\nR=\nBUG=', ['c@c'], 'foo\nBar\n\nR=c@c\nBUG='),
721 ('foo\nBar\n\nR=\nBUG=\nR=', ['c@c'], 'foo\nBar\n\nR=c@c\nBUG='),
722 # Same as the line before, but full of whitespaces.
723 (
724 'foo\nBar\n\n R = \n BUG = \n R = ', ['c@c'],
725 'foo\nBar\n\nR=c@c\n BUG =',
726 ),
727 # Whitespaces aren't interpreted as new lines.
728 ('foo BUG=allo R=joe ', ['c@c'], 'foo BUG=allo R=joe\n\nR=c@c'),
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000729 ]
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +0000730 expected = [i[2] for i in data]
731 actual = []
732 for orig, reviewers, _expected in data:
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000733 obj = git_cl.ChangeDescription(orig)
734 obj.update_reviewers(reviewers)
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +0000735 actual.append(obj.description)
736 self.assertEqual(expected, actual)
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000737
ukai@chromium.org78c4b982012-02-14 02:20:26 +0000738
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000739if __name__ == '__main__':
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000740 git_cl.logging.basicConfig(
741 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR)
maruel@chromium.orgddd59412011-11-30 14:20:38 +0000742 unittest.main()