[email protected] | ddd5941 | 2011-11-30 14:20:38 | [diff] [blame] | 1 | #!/usr/bin/env python |
[email protected] | eb5edbc | 2012-01-16 17:03:28 | [diff] [blame] | 2 | # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | ddd5941 | 2011-11-30 14:20:38 | [diff] [blame] | 3 | # 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 | |
| 8 | import os |
[email protected] | a335365 | 2011-11-30 14:26:57 | [diff] [blame] | 9 | import StringIO |
[email protected] | 78c4b98 | 2012-02-14 02:20:26 | [diff] [blame] | 10 | import stat |
[email protected] | ddd5941 | 2011-11-30 14:20:38 | [diff] [blame] | 11 | import sys |
| 12 | import unittest |
[email protected] | f86c7d3 | 2016-04-01 19:27:30 | [diff] [blame] | 13 | import urlparse |
[email protected] | ddd5941 | 2011-11-30 14:20:38 | [diff] [blame] | 14 | |
| 15 | sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 16 | |
| 17 | from testing_support.auto_stub import TestCase |
| 18 | |
| 19 | import git_cl |
[email protected] | 9e84927 | 2014-04-04 00:31:55 | [diff] [blame] | 20 | import git_common |
[email protected] | 57d8654 | 2016-03-04 16:11:32 | [diff] [blame] | 21 | import git_footers |
[email protected] | ddd5941 | 2011-11-30 14:20:38 | [diff] [blame] | 22 | import subprocess2 |
[email protected] | ddd5941 | 2011-11-30 14:20:38 | [diff] [blame] | 23 | |
[email protected] | d6648e2 | 2016-04-29 19:22:16 | [diff] [blame] | 24 | class ChangelistMock(object): |
| 25 | # A class variable so we can access it when we don't have access to the |
| 26 | # instance that's being set. |
| 27 | desc = "" |
| 28 | def __init__(self, **kwargs): |
| 29 | pass |
| 30 | def GetIssue(self): |
| 31 | return 1 |
| 32 | def GetDescription(self): |
| 33 | return ChangelistMock.desc |
| 34 | def UpdateDescription(self, desc): |
| 35 | ChangelistMock.desc = desc |
| 36 | |
[email protected] | 2e72bb1 | 2012-01-17 15:18:35 | [diff] [blame] | 37 | class PresubmitMock(object): |
| 38 | def __init__(self, *args, **kwargs): |
| 39 | self.reviewers = [] |
| 40 | @staticmethod |
| 41 | def should_continue(): |
| 42 | return True |
| 43 | |
| 44 | |
| 45 | class RietveldMock(object): |
| 46 | def __init__(self, *args, **kwargs): |
| 47 | pass |
[email protected] | 78936cb | 2013-04-11 00:17:52 | [diff] [blame] | 48 | |
[email protected] | 2e72bb1 | 2012-01-17 15:18:35 | [diff] [blame] | 49 | @staticmethod |
| 50 | def get_description(issue): |
| 51 | return 'Issue: %d' % issue |
| 52 | |
[email protected] | 78936cb | 2013-04-11 00:17:52 | [diff] [blame] | 53 | @staticmethod |
| 54 | def get_issue_properties(_issue, _messages): |
| 55 | return { |
| 56 | 'reviewers': ['[email protected]', '[email protected]'], |
| 57 | 'messages': [ |
| 58 | { |
| 59 | 'approval': True, |
| 60 | 'sender': '[email protected]', |
| 61 | }, |
| 62 | ], |
| 63 | } |
| 64 | |
[email protected] | 2e72bb1 | 2012-01-17 15:18:35 | [diff] [blame] | 65 | |
| 66 | class WatchlistsMock(object): |
| 67 | def __init__(self, _): |
| 68 | pass |
| 69 | @staticmethod |
| 70 | def GetWatchersForPaths(_): |
| 71 | return ['[email protected]'] |
| 72 | |
| 73 | |
[email protected] | 78c4b98 | 2012-02-14 02:20:26 | [diff] [blame] | 74 | class CodereviewSettingsFileMock(object): |
| 75 | def __init__(self): |
| 76 | pass |
| 77 | # pylint: disable=R0201 |
| 78 | def read(self): |
| 79 | return ("CODE_REVIEW_SERVER: gerrit.chromium.org\n" + |
[email protected] | 11f46eb | 2016-02-02 19:26:51 | [diff] [blame] | 80 | "GERRIT_HOST: True\n") |
[email protected] | 78c4b98 | 2012-02-14 02:20:26 | [diff] [blame] | 81 | |
| 82 | |
[email protected] | eed4df3 | 2015-04-10 21:30:20 | [diff] [blame] | 83 | class AuthenticatorMock(object): |
| 84 | def __init__(self, *_args): |
| 85 | pass |
| 86 | def has_cached_credentials(self): |
| 87 | return True |
| 88 | |
| 89 | |
[email protected] | fe30f18 | 2016-04-13 12:15:04 | [diff] [blame] | 90 | def CookiesAuthenticatorMockFactory(hosts_with_creds=None, same_cookie=False): |
| 91 | """Use to mock Gerrit/Git credentials from ~/.netrc or ~/.gitcookies. |
| 92 | |
| 93 | Usage: |
| 94 | >>> self.mock(git_cl.gerrit_util, "CookiesAuthenticator", |
| 95 | CookiesAuthenticatorMockFactory({'host1': 'cookie1'})) |
| 96 | |
| 97 | OR |
| 98 | >>> self.mock(git_cl.gerrit_util, "CookiesAuthenticator", |
| 99 | CookiesAuthenticatorMockFactory(cookie='cookie')) |
| 100 | """ |
| 101 | class CookiesAuthenticatorMock(git_cl.gerrit_util.CookiesAuthenticator): |
| 102 | def __init__(self): # pylint: disable=W0231 |
| 103 | # Intentionally not calling super() because it reads actual cookie files. |
| 104 | pass |
| 105 | @classmethod |
| 106 | def get_gitcookies_path(cls): |
| 107 | return '~/.gitcookies' |
| 108 | @classmethod |
| 109 | def get_netrc_path(cls): |
| 110 | return '~/.netrc' |
| 111 | def get_auth_header(self, host): |
| 112 | if same_cookie: |
| 113 | return same_cookie |
| 114 | return (hosts_with_creds or {}).get(host) |
| 115 | return CookiesAuthenticatorMock |
| 116 | |
| 117 | |
[email protected] | f86c7d3 | 2016-04-01 19:27:30 | [diff] [blame] | 118 | class TestGitClBasic(unittest.TestCase): |
| 119 | def _test_ParseIssueUrl(self, func, url, issue, patchset, hostname, fail): |
| 120 | parsed = urlparse.urlparse(url) |
| 121 | result = func(parsed) |
| 122 | if fail: |
| 123 | self.assertIsNone(result) |
| 124 | return None |
| 125 | self.assertIsNotNone(result) |
| 126 | self.assertEqual(result.issue, issue) |
| 127 | self.assertEqual(result.patchset, patchset) |
| 128 | self.assertEqual(result.hostname, hostname) |
| 129 | return result |
| 130 | |
| 131 | def test_ParseIssueURL_rietveld(self): |
| 132 | def test(url, issue=None, patchset=None, hostname=None, patch_url=None, |
| 133 | fail=None): |
| 134 | result = self._test_ParseIssueUrl( |
| 135 | git_cl._RietveldChangelistImpl.ParseIssueURL, |
| 136 | url, issue, patchset, hostname, fail) |
| 137 | if not fail: |
| 138 | self.assertEqual(result.patch_url, patch_url) |
| 139 | |
| 140 | test('http://codereview.chromium.org/123', |
| 141 | 123, None, 'codereview.chromium.org') |
| 142 | test('https://codereview.chromium.org/123', |
| 143 | 123, None, 'codereview.chromium.org') |
| 144 | test('https://codereview.chromium.org/123/', |
| 145 | 123, None, 'codereview.chromium.org') |
| 146 | test('https://codereview.chromium.org/123/whatever', |
| 147 | 123, None, 'codereview.chromium.org') |
| 148 | test('http://codereview.chromium.org/download/issue123_4.diff', |
| 149 | 123, 4, 'codereview.chromium.org', |
| 150 | patch_url='https://codereview.chromium.org/download/issue123_4.diff') |
| 151 | # This looks like bad Gerrit, but is actually valid Rietveld. |
| 152 | test('https://chrome-review.source.com/123/4/', |
| 153 | 123, None, 'chrome-review.source.com') |
| 154 | |
| 155 | test('https://codereview.chromium.org/deadbeaf', fail=True) |
| 156 | test('https://codereview.chromium.org/api/123', fail=True) |
| 157 | test('bad://codereview.chromium.org/123', fail=True) |
| 158 | test('http://codereview.chromium.org/download/issue123_4.diffff', fail=True) |
| 159 | |
| 160 | def test_ParseIssueURL_gerrit(self): |
| 161 | def test(url, issue=None, patchset=None, hostname=None, fail=None): |
| 162 | self._test_ParseIssueUrl( |
| 163 | git_cl._GerritChangelistImpl.ParseIssueURL, |
| 164 | url, issue, patchset, hostname, fail) |
| 165 | |
| 166 | test('http://chrome-review.source.com/c/123', |
| 167 | 123, None, 'chrome-review.source.com') |
| 168 | test('https://chrome-review.source.com/c/123/', |
| 169 | 123, None, 'chrome-review.source.com') |
| 170 | test('https://chrome-review.source.com/c/123/4', |
| 171 | 123, 4, 'chrome-review.source.com') |
| 172 | test('https://chrome-review.source.com/#/c/123/4', |
| 173 | 123, 4, 'chrome-review.source.com') |
| 174 | test('https://chrome-review.source.com/c/123/4', |
| 175 | 123, 4, 'chrome-review.source.com') |
| 176 | test('https://chrome-review.source.com/123', |
| 177 | 123, None, 'chrome-review.source.com') |
| 178 | test('https://chrome-review.source.com/123/4', |
| 179 | 123, 4, 'chrome-review.source.com') |
| 180 | |
| 181 | test('https://chrome-review.source.com/c/123/1/whatisthis', fail=True) |
| 182 | test('https://chrome-review.source.com/c/abc/', fail=True) |
| 183 | test('ssh://chrome-review.source.com/c/123/1/', fail=True) |
| 184 | |
| 185 | def test_ParseIssueNumberArgument(self): |
| 186 | def test(arg, issue=None, patchset=None, hostname=None, fail=False): |
| 187 | result = git_cl.ParseIssueNumberArgument(arg) |
| 188 | self.assertIsNotNone(result) |
| 189 | if fail: |
| 190 | self.assertFalse(result.valid) |
| 191 | else: |
| 192 | self.assertEqual(result.issue, issue) |
| 193 | self.assertEqual(result.patchset, patchset) |
| 194 | self.assertEqual(result.hostname, hostname) |
| 195 | |
| 196 | test('123', 123) |
| 197 | test('', fail=True) |
| 198 | test('abc', fail=True) |
| 199 | test('123/1', fail=True) |
| 200 | test('123a', fail=True) |
| 201 | test('ssh://chrome-review.source.com/#/c/123/4/', fail=True) |
| 202 | # Rietveld. |
| 203 | test('https://codereview.source.com/123', |
| 204 | 123, None, 'codereview.source.com') |
| 205 | test('https://codereview.source.com/www123', fail=True) |
| 206 | # Gerrrit. |
| 207 | test('https://chrome-review.source.com/c/123/4', |
| 208 | 123, 4, 'chrome-review.source.com') |
| 209 | test('https://chrome-review.source.com/bad/123/4', fail=True) |
| 210 | |
| 211 | |
[email protected] | ddd5941 | 2011-11-30 14:20:38 | [diff] [blame] | 212 | class TestGitCl(TestCase): |
| 213 | def setUp(self): |
| 214 | super(TestGitCl, self).setUp() |
| 215 | self.calls = [] |
| 216 | self._calls_done = 0 |
[email protected] | 2e72bb1 | 2012-01-17 15:18:35 | [diff] [blame] | 217 | self.mock(subprocess2, 'call', self._mocked_call) |
| 218 | self.mock(subprocess2, 'check_call', self._mocked_call) |
| 219 | self.mock(subprocess2, 'check_output', self._mocked_call) |
| 220 | self.mock(subprocess2, 'communicate', self._mocked_call) |
[email protected] | a342c92 | 2016-03-16 07:08:25 | [diff] [blame] | 221 | self.mock(git_cl.gclient_utils, 'CheckCallAndFilter', self._mocked_call) |
[email protected] | 71437c0 | 2015-04-09 19:29:40 | [diff] [blame] | 222 | self.mock(git_common, 'is_dirty_git_tree', lambda x: False) |
[email protected] | 9e84927 | 2014-04-04 00:31:55 | [diff] [blame] | 223 | self.mock(git_common, 'get_or_create_merge_base', |
| 224 | lambda *a: ( |
| 225 | self._mocked_call(['get_or_create_merge_base']+list(a)))) |
[email protected] | 8ba38ff | 2015-06-11 21:41:25 | [diff] [blame] | 226 | self.mock(git_cl, 'BranchExists', lambda _: True) |
[email protected] | ddd5941 | 2011-11-30 14:20:38 | [diff] [blame] | 227 | self.mock(git_cl, 'FindCodereviewSettingsFile', lambda: '') |
[email protected] | 2e72bb1 | 2012-01-17 15:18:35 | [diff] [blame] | 228 | self.mock(git_cl, 'ask_for_data', self._mocked_call) |
[email protected] | ddd5941 | 2011-11-30 14:20:38 | [diff] [blame] | 229 | self.mock(git_cl.presubmit_support, 'DoPresubmitChecks', PresubmitMock) |
[email protected] | ddd5941 | 2011-11-30 14:20:38 | [diff] [blame] | 230 | self.mock(git_cl.rietveld, 'Rietveld', RietveldMock) |
[email protected] | 4bac4b5 | 2012-11-27 20:33:52 | [diff] [blame] | 231 | self.mock(git_cl.rietveld, 'CachingRietveld', RietveldMock) |
[email protected] | ddd5941 | 2011-11-30 14:20:38 | [diff] [blame] | 232 | self.mock(git_cl.upload, 'RealMain', self.fail) |
[email protected] | ddd5941 | 2011-11-30 14:20:38 | [diff] [blame] | 233 | self.mock(git_cl.watchlists, 'Watchlists', WatchlistsMock) |
[email protected] | eed4df3 | 2015-04-10 21:30:20 | [diff] [blame] | 234 | self.mock(git_cl.auth, 'get_authenticator_for_host', AuthenticatorMock) |
[email protected] | fe30f18 | 2016-04-13 12:15:04 | [diff] [blame] | 235 | self.mock(git_cl.gerrit_util.GceAuthenticator, 'is_gce', |
| 236 | classmethod(lambda _: False)) |
[email protected] | ddd5941 | 2011-11-30 14:20:38 | [diff] [blame] | 237 | # It's important to reset settings to not have inter-tests interference. |
| 238 | git_cl.settings = None |
| 239 | |
[email protected] | f86c7d3 | 2016-04-01 19:27:30 | [diff] [blame] | 240 | |
[email protected] | ddd5941 | 2011-11-30 14:20:38 | [diff] [blame] | 241 | def tearDown(self): |
[email protected] | 445c896 | 2015-04-28 23:30:05 | [diff] [blame] | 242 | try: |
[email protected] | f86c7d3 | 2016-04-01 19:27:30 | [diff] [blame] | 243 | # Note: has_failed returns True if at least 1 test ran so far, current |
| 244 | # included, has failed. That means current test may have actually ran |
| 245 | # fine, and the check for no leftover calls would be skipped. |
[email protected] | 445c896 | 2015-04-28 23:30:05 | [diff] [blame] | 246 | if not self.has_failed(): |
| 247 | self.assertEquals([], self.calls) |
| 248 | finally: |
| 249 | super(TestGitCl, self).tearDown() |
[email protected] | ddd5941 | 2011-11-30 14:20:38 | [diff] [blame] | 250 | |
[email protected] | 9e84927 | 2014-04-04 00:31:55 | [diff] [blame] | 251 | def _mocked_call(self, *args, **_kwargs): |
[email protected] | 2e72bb1 | 2012-01-17 15:18:35 | [diff] [blame] | 252 | self.assertTrue( |
| 253 | self.calls, |
| 254 | '@%d Expected: <Missing> Actual: %r' % (self._calls_done, args)) |
[email protected] | a872e75 | 2015-04-28 23:42:18 | [diff] [blame] | 255 | top = self.calls.pop(0) |
| 256 | if len(top) > 2 and top[2]: |
| 257 | raise top[2] |
| 258 | expected_args, result = top |
| 259 | |
[email protected] | e52678e | 2013-04-26 18:34:44 | [diff] [blame] | 260 | # Also logs otherwise it could get caught in a try/finally and be hard to |
| 261 | # diagnose. |
| 262 | if expected_args != args: |
| 263 | msg = '@%d Expected: %r Actual: %r' % ( |
| 264 | self._calls_done, expected_args, args) |
| 265 | git_cl.logging.error(msg) |
| 266 | self.fail(msg) |
[email protected] | 2e72bb1 | 2012-01-17 15:18:35 | [diff] [blame] | 267 | self._calls_done += 1 |
| 268 | return result |
| 269 | |
[email protected] | a335365 | 2011-11-30 14:26:57 | [diff] [blame] | 270 | @classmethod |
[email protected] | 512d79c | 2016-03-31 12:55:28 | [diff] [blame] | 271 | def _is_gerrit_calls(cls, gerrit=False): |
| 272 | return [((['git', 'config', 'rietveld.autoupdate'],), ''), |
| 273 | ((['git', 'config', 'gerrit.host'],), 'True' if gerrit else '')] |
| 274 | |
| 275 | @classmethod |
[email protected] | c1737d0 | 2013-05-29 14:17:28 | [diff] [blame] | 276 | def _upload_calls(cls, similarity, find_copies, private): |
[email protected] | 7954005 | 2012-10-19 23:15:26 | [diff] [blame] | 277 | return (cls._git_base_calls(similarity, find_copies) + |
[email protected] | c1737d0 | 2013-05-29 14:17:28 | [diff] [blame] | 278 | cls._git_upload_calls(private)) |
[email protected] | a335365 | 2011-11-30 14:26:57 | [diff] [blame] | 279 | |
[email protected] | 0f58fa8 | 2012-11-05 01:45:20 | [diff] [blame] | 280 | @classmethod |
[email protected] | 615a262 | 2013-05-03 13:20:14 | [diff] [blame] | 281 | def _upload_no_rev_calls(cls, similarity, find_copies): |
| 282 | return (cls._git_base_calls(similarity, find_copies) + |
| 283 | cls._git_upload_no_rev_calls()) |
| 284 | |
| 285 | @classmethod |
[email protected] | 0f58fa8 | 2012-11-05 01:45:20 | [diff] [blame] | 286 | def _git_base_calls(cls, similarity, find_copies): |
[email protected] | 53937ba | 2012-10-02 18:20:43 | [diff] [blame] | 287 | if similarity is None: |
| 288 | similarity = '50' |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 289 | similarity_call = ((['git', 'config', '--int', '--get', |
[email protected] | 53937ba | 2012-10-02 18:20:43 | [diff] [blame] | 290 | 'branch.master.git-cl-similarity'],), '') |
| 291 | else: |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 292 | similarity_call = ((['git', 'config', '--int', |
[email protected] | 53937ba | 2012-10-02 18:20:43 | [diff] [blame] | 293 | 'branch.master.git-cl-similarity', similarity],), '') |
[email protected] | 7954005 | 2012-10-19 23:15:26 | [diff] [blame] | 294 | |
| 295 | if find_copies is None: |
| 296 | find_copies = True |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 297 | find_copies_call = ((['git', 'config', '--int', '--get', |
[email protected] | 7954005 | 2012-10-19 23:15:26 | [diff] [blame] | 298 | 'branch.master.git-find-copies'],), '') |
| 299 | else: |
| 300 | val = str(int(find_copies)) |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 301 | find_copies_call = ((['git', 'config', '--int', |
[email protected] | 7954005 | 2012-10-19 23:15:26 | [diff] [blame] | 302 | 'branch.master.git-find-copies', val],), '') |
| 303 | |
| 304 | if find_copies: |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 305 | stat_call = ((['git', 'diff', '--no-ext-diff', '--stat', |
[email protected] | 7954005 | 2012-10-19 23:15:26 | [diff] [blame] | 306 | '--find-copies-harder', '-l100000', '-C'+similarity, |
[email protected] | 5e07e06 | 2013-02-28 23:55:44 | [diff] [blame] | 307 | 'fake_ancestor_sha', 'HEAD'],), '+dat') |
[email protected] | 7954005 | 2012-10-19 23:15:26 | [diff] [blame] | 308 | else: |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 309 | stat_call = ((['git', 'diff', '--no-ext-diff', '--stat', |
[email protected] | 5e07e06 | 2013-02-28 23:55:44 | [diff] [blame] | 310 | '-M'+similarity, 'fake_ancestor_sha', 'HEAD'],), '+dat') |
[email protected] | 7954005 | 2012-10-19 23:15:26 | [diff] [blame] | 311 | |
[email protected] | ddd5941 | 2011-11-30 14:20:38 | [diff] [blame] | 312 | return [ |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 313 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
[email protected] | 87985d2 | 2016-03-24 17:33:33 | [diff] [blame] | 314 | similarity_call, |
| 315 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
| 316 | find_copies_call, |
[email protected] | 512d79c | 2016-03-31 12:55:28 | [diff] [blame] | 317 | ] + cls._is_gerrit_calls() + [ |
[email protected] | 87985d2 | 2016-03-24 17:33:33 | [diff] [blame] | 318 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
[email protected] | aa5ced1 | 2016-03-29 09:41:14 | [diff] [blame] | 319 | ((['git', 'config', 'branch.master.rietveldissue'],), ''), |
| 320 | ((['git', 'config', 'branch.master.gerritissue'],), ''), |
[email protected] | aa5ced1 | 2016-03-29 09:41:14 | [diff] [blame] | 321 | ((['git', 'config', 'rietveld.server'],), |
| 322 | 'codereview.example.com'), |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 323 | ((['git', 'config', 'branch.master.merge'],), 'master'), |
| 324 | ((['git', 'config', 'branch.master.remote'],), 'origin'), |
[email protected] | 9e84927 | 2014-04-04 00:31:55 | [diff] [blame] | 325 | ((['get_or_create_merge_base', 'master', 'master'],), |
[email protected] | f267b0e | 2013-05-02 09:11:43 | [diff] [blame] | 326 | 'fake_ancestor_sha'), |
[email protected] | 512d79c | 2016-03-31 12:55:28 | [diff] [blame] | 327 | ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [ |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 328 | ((['git', 'rev-parse', '--show-cdup'],), ''), |
| 329 | ((['git', 'rev-parse', 'HEAD'],), '12345'), |
| 330 | ((['git', 'diff', '--name-status', '--no-renames', '-r', |
[email protected] | f267b0e | 2013-05-02 09:11:43 | [diff] [blame] | 331 | 'fake_ancestor_sha...', '.'],), |
[email protected] | 2e72bb1 | 2012-01-17 15:18:35 | [diff] [blame] | 332 | 'M\t.gitignore\n'), |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 333 | ((['git', 'config', 'branch.master.rietveldpatchset'],), |
[email protected] | f267b0e | 2013-05-02 09:11:43 | [diff] [blame] | 334 | ''), |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 335 | ((['git', 'log', '--pretty=format:%s%n%n%b', |
[email protected] | f267b0e | 2013-05-02 09:11:43 | [diff] [blame] | 336 | 'fake_ancestor_sha...'],), |
[email protected] | 0f58fa8 | 2012-11-05 01:45:20 | [diff] [blame] | 337 | 'foo'), |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 338 | ((['git', 'config', 'user.email'],), '[email protected]'), |
[email protected] | 7954005 | 2012-10-19 23:15:26 | [diff] [blame] | 339 | stat_call, |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 340 | ((['git', 'log', '--pretty=format:%s\n\n%b', |
[email protected] | f267b0e | 2013-05-02 09:11:43 | [diff] [blame] | 341 | 'fake_ancestor_sha..HEAD'],), |
[email protected] | 0f58fa8 | 2012-11-05 01:45:20 | [diff] [blame] | 342 | 'desc\n'), |
[email protected] | 9075258 | 2014-01-14 21:04:50 | [diff] [blame] | 343 | ((['git', 'config', 'rietveld.bug-prefix'],), ''), |
[email protected] | a335365 | 2011-11-30 14:26:57 | [diff] [blame] | 344 | ] |
| 345 | |
[email protected] | 0f58fa8 | 2012-11-05 01:45:20 | [diff] [blame] | 346 | @classmethod |
[email protected] | 615a262 | 2013-05-03 13:20:14 | [diff] [blame] | 347 | def _git_upload_no_rev_calls(cls): |
| 348 | return [ |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 349 | ((['git', 'config', 'core.editor'],), ''), |
[email protected] | 615a262 | 2013-05-03 13:20:14 | [diff] [blame] | 350 | ] |
| 351 | |
| 352 | @classmethod |
[email protected] | c1737d0 | 2013-05-29 14:17:28 | [diff] [blame] | 353 | def _git_upload_calls(cls, private): |
| 354 | if private: |
[email protected] | 99918ab | 2013-09-30 06:17:28 | [diff] [blame] | 355 | cc_call = [] |
[email protected] | c1737d0 | 2013-05-29 14:17:28 | [diff] [blame] | 356 | private_call = [] |
| 357 | else: |
[email protected] | 99918ab | 2013-09-30 06:17:28 | [diff] [blame] | 358 | cc_call = [((['git', 'config', 'rietveld.cc'],), '')] |
[email protected] | c1737d0 | 2013-05-29 14:17:28 | [diff] [blame] | 359 | private_call = [ |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 360 | ((['git', 'config', 'rietveld.private'],), '')] |
[email protected] | c1737d0 | 2013-05-29 14:17:28 | [diff] [blame] | 361 | |
[email protected] | a335365 | 2011-11-30 14:26:57 | [diff] [blame] | 362 | return [ |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 363 | ((['git', 'config', 'core.editor'],), ''), |
[email protected] | 99918ab | 2013-09-30 06:17:28 | [diff] [blame] | 364 | ] + cc_call + private_call + [ |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 365 | ((['git', 'config', 'branch.master.base-url'],), ''), |
[email protected] | 566a02a | 2014-08-22 01:34:13 | [diff] [blame] | 366 | ((['git', 'config', 'rietveld.pending-ref-prefix'],), ''), |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 367 | ((['git', |
[email protected] | 05fb911 | 2014-07-07 09:30:23 | [diff] [blame] | 368 | 'config', '--local', '--get-regexp', '^svn-remote\\.'],), |
| 369 | (('', None), 0)), |
[email protected] | 7a54e81 | 2014-02-11 19:57:22 | [diff] [blame] | 370 | ((['git', 'rev-parse', '--show-cdup'],), ''), |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 371 | ((['git', 'svn', 'info'],), ''), |
[email protected] | 152cf83 | 2014-06-11 21:37:49 | [diff] [blame] | 372 | ((['git', 'config', 'rietveld.project'],), ''), |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 373 | ((['git', |
[email protected] | c1737d0 | 2013-05-29 14:17:28 | [diff] [blame] | 374 | 'config', 'branch.master.rietveldissue', '1'],), ''), |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 375 | ((['git', 'config', 'branch.master.rietveldserver', |
[email protected] | c1737d0 | 2013-05-29 14:17:28 | [diff] [blame] | 376 | 'https://codereview.example.com'],), ''), |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 377 | ((['git', |
[email protected] | c1737d0 | 2013-05-29 14:17:28 | [diff] [blame] | 378 | 'config', 'branch.master.rietveldpatchset', '2'],), ''), |
[email protected] | 1e67bb7 | 2016-02-11 12:15:49 | [diff] [blame] | 379 | ] + cls._git_post_upload_calls() |
| 380 | |
| 381 | @classmethod |
| 382 | def _git_post_upload_calls(cls): |
| 383 | return [ |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 384 | ((['git', 'rev-parse', 'HEAD'],), 'hash'), |
| 385 | ((['git', 'symbolic-ref', 'HEAD'],), 'hash'), |
| 386 | ((['git', |
[email protected] | c1737d0 | 2013-05-29 14:17:28 | [diff] [blame] | 387 | 'config', 'branch.hash.last-upload-hash', 'hash'],), ''), |
[email protected] | 5626a92 | 2015-02-26 14:03:30 | [diff] [blame] | 388 | ((['git', 'config', 'rietveld.run-post-upload-hook'],), ''), |
[email protected] | ddd5941 | 2011-11-30 14:20:38 | [diff] [blame] | 389 | ] |
| 390 | |
[email protected] | 0f58fa8 | 2012-11-05 01:45:20 | [diff] [blame] | 391 | @staticmethod |
[email protected] | fe30f18 | 2016-04-13 12:15:04 | [diff] [blame] | 392 | def _git_sanity_checks(diff_base, working_branch, get_remote_branch=True): |
[email protected] | 0f58fa8 | 2012-11-05 01:45:20 | [diff] [blame] | 393 | fake_ancestor = 'fake_ancestor' |
| 394 | fake_cl = 'fake_cl_for_patch' |
| 395 | return [ |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 396 | ((['git', |
[email protected] | f267b0e | 2013-05-02 09:11:43 | [diff] [blame] | 397 | 'rev-parse', '--verify', diff_base],), fake_ancestor), |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 398 | ((['git', |
[email protected] | f267b0e | 2013-05-02 09:11:43 | [diff] [blame] | 399 | 'merge-base', fake_ancestor, 'HEAD'],), fake_ancestor), |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 400 | ((['git', |
[email protected] | f267b0e | 2013-05-02 09:11:43 | [diff] [blame] | 401 | 'rev-list', '^' + fake_ancestor, 'HEAD'],), fake_cl), |
[email protected] | 0f58fa8 | 2012-11-05 01:45:20 | [diff] [blame] | 402 | # Mock a config miss (error code 1) |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 403 | ((['git', |
[email protected] | f267b0e | 2013-05-02 09:11:43 | [diff] [blame] | 404 | 'config', 'gitcl.remotebranch'],), (('', None), 1)), |
[email protected] | fe30f18 | 2016-04-13 12:15:04 | [diff] [blame] | 405 | ] + ([ |
[email protected] | 0f58fa8 | 2012-11-05 01:45:20 | [diff] [blame] | 406 | # Call to GetRemoteBranch() |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 407 | ((['git', |
[email protected] | f267b0e | 2013-05-02 09:11:43 | [diff] [blame] | 408 | 'config', 'branch.%s.merge' % working_branch],), |
[email protected] | 0f58fa8 | 2012-11-05 01:45:20 | [diff] [blame] | 409 | 'refs/heads/master'), |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 410 | ((['git', |
[email protected] | f267b0e | 2013-05-02 09:11:43 | [diff] [blame] | 411 | 'config', 'branch.%s.remote' % working_branch],), 'origin'), |
[email protected] | fe30f18 | 2016-04-13 12:15:04 | [diff] [blame] | 412 | ] if get_remote_branch else []) + [ |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 413 | ((['git', 'rev-list', '^' + fake_ancestor, |
[email protected] | 0f58fa8 | 2012-11-05 01:45:20 | [diff] [blame] | 414 | 'refs/remotes/origin/master'],), ''), |
[email protected] | fe30f18 | 2016-04-13 12:15:04 | [diff] [blame] | 415 | ] |
[email protected] | 0f58fa8 | 2012-11-05 01:45:20 | [diff] [blame] | 416 | |
[email protected] | 2e72bb1 | 2012-01-17 15:18:35 | [diff] [blame] | 417 | @classmethod |
| 418 | def _dcommit_calls_1(cls): |
| 419 | return [ |
[email protected] | 566a02a | 2014-08-22 01:34:13 | [diff] [blame] | 420 | ((['git', 'config', 'rietveld.autoupdate'],), |
| 421 | ''), |
| 422 | ((['git', 'config', 'rietveld.pending-ref-prefix'],), |
| 423 | ''), |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 424 | ((['git', |
[email protected] | 05fb911 | 2014-07-07 09:30:23 | [diff] [blame] | 425 | 'config', '--local', '--get-regexp', '^svn-remote\\.'],), |
[email protected] | 2e72bb1 | 2012-01-17 15:18:35 | [diff] [blame] | 426 | ((('svn-remote.svn.url svn://svn.chromium.org/chrome\n' |
| 427 | 'svn-remote.svn.fetch trunk/src:refs/remotes/origin/master'), |
| 428 | None), |
| 429 | 0)), |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 430 | ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'), |
| 431 | ((['git', 'config', '--int', '--get', |
[email protected] | 53937ba | 2012-10-02 18:20:43 | [diff] [blame] | 432 | 'branch.working.git-cl-similarity'],), ''), |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 433 | ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'), |
| 434 | ((['git', 'config', '--int', '--get', |
[email protected] | 7954005 | 2012-10-19 23:15:26 | [diff] [blame] | 435 | 'branch.working.git-find-copies'],), ''), |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 436 | ((['git', 'symbolic-ref', 'HEAD'],), 'refs/heads/working'), |
| 437 | ((['git', |
[email protected] | aa5ced1 | 2016-03-29 09:41:14 | [diff] [blame] | 438 | 'config', 'branch.working.rietveldissue'],), '12345'), |
| 439 | ((['git', |
| 440 | 'config', 'rietveld.server'],), 'codereview.example.com'), |
| 441 | ((['git', |
[email protected] | f267b0e | 2013-05-02 09:11:43 | [diff] [blame] | 442 | 'config', 'branch.working.merge'],), 'refs/heads/master'), |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 443 | ((['git', 'config', 'branch.working.remote'],), 'origin'), |
[email protected] | 5724c96 | 2014-04-11 09:32:56 | [diff] [blame] | 444 | ((['git', 'config', 'branch.working.merge'],), |
| 445 | 'refs/heads/master'), |
| 446 | ((['git', 'config', 'branch.working.remote'],), 'origin'), |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 447 | ((['git', 'rev-list', '--merges', |
[email protected] | e84b754 | 2012-06-15 21:26:58 | [diff] [blame] | 448 | '--grep=^SVN changes up to revision [0-9]*$', |
[email protected] | 9bb85e2 | 2012-06-13 20:28:23 | [diff] [blame] | 449 | 'refs/remotes/origin/master^!'],), ''), |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 450 | ((['git', 'rev-list', '^refs/heads/working', |
[email protected] | 2e72bb1 | 2012-01-17 15:18:35 | [diff] [blame] | 451 | 'refs/remotes/origin/master'],), |
| 452 | ''), |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 453 | ((['git', |
[email protected] | f267b0e | 2013-05-02 09:11:43 | [diff] [blame] | 454 | 'log', '--grep=^git-svn-id:', '-1', '--pretty=format:%H'],), |
[email protected] | 2e72bb1 | 2012-01-17 15:18:35 | [diff] [blame] | 455 | '3fc18b62c4966193eb435baabe2d18a3810ec82e'), |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 456 | ((['git', |
[email protected] | f267b0e | 2013-05-02 09:11:43 | [diff] [blame] | 457 | 'rev-list', '^3fc18b62c4966193eb435baabe2d18a3810ec82e', |
[email protected] | 2e72bb1 | 2012-01-17 15:18:35 | [diff] [blame] | 458 | 'refs/remotes/origin/master'],), ''), |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 459 | ((['git', |
[email protected] | f267b0e | 2013-05-02 09:11:43 | [diff] [blame] | 460 | 'merge-base', 'refs/remotes/origin/master', 'HEAD'],), |
[email protected] | 0f58fa8 | 2012-11-05 01:45:20 | [diff] [blame] | 461 | 'fake_ancestor_sha'), |
[email protected] | 2e72bb1 | 2012-01-17 15:18:35 | [diff] [blame] | 462 | ] |
| 463 | |
| 464 | @classmethod |
| 465 | def _dcommit_calls_normal(cls): |
| 466 | return [ |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 467 | ((['git', 'rev-parse', '--show-cdup'],), ''), |
| 468 | ((['git', 'rev-parse', 'HEAD'],), |
[email protected] | 2e72bb1 | 2012-01-17 15:18:35 | [diff] [blame] | 469 | '00ff397798ea57439712ed7e04ab96e13969ef40'), |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 470 | ((['git', |
[email protected] | 9249f64 | 2013-06-03 21:36:18 | [diff] [blame] | 471 | 'diff', '--name-status', '--no-renames', '-r', 'fake_ancestor_sha...', |
[email protected] | 2e72bb1 | 2012-01-17 15:18:35 | [diff] [blame] | 472 | '.'],), |
| 473 | 'M\tPRESUBMIT.py'), |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 474 | ((['git', |
[email protected] | f267b0e | 2013-05-02 09:11:43 | [diff] [blame] | 475 | 'config', 'branch.working.rietveldpatchset'],), '31137'), |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 476 | ((['git', 'config', 'branch.working.rietveldserver'],), |
[email protected] | 2e72bb1 | 2012-01-17 15:18:35 | [diff] [blame] | 477 | 'codereview.example.com'), |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 478 | ((['git', 'config', 'user.email'],), '[email protected]'), |
| 479 | ((['git', 'config', 'rietveld.tree-status-url'],), ''), |
[email protected] | 2e72bb1 | 2012-01-17 15:18:35 | [diff] [blame] | 480 | ] |
| 481 | |
| 482 | @classmethod |
| 483 | def _dcommit_calls_bypassed(cls): |
| 484 | return [ |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 485 | ((['git', 'config', 'branch.working.rietveldserver'],), |
[email protected] | 2e72bb1 | 2012-01-17 15:18:35 | [diff] [blame] | 486 | 'codereview.example.com'), |
[email protected] | 2e72bb1 | 2012-01-17 15:18:35 | [diff] [blame] | 487 | ] |
| 488 | |
| 489 | @classmethod |
[email protected] | 7a54e81 | 2014-02-11 19:57:22 | [diff] [blame] | 490 | def _dcommit_calls_3(cls): |
| 491 | return [ |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 492 | ((['git', |
[email protected] | f267b0e | 2013-05-02 09:11:43 | [diff] [blame] | 493 | 'diff', '--no-ext-diff', '--stat', '--find-copies-harder', |
[email protected] | 0f58fa8 | 2012-11-05 01:45:20 | [diff] [blame] | 494 | '-l100000', '-C50', 'fake_ancestor_sha', |
[email protected] | 53937ba | 2012-10-02 18:20:43 | [diff] [blame] | 495 | 'refs/heads/working'],), |
[email protected] | 2e72bb1 | 2012-01-17 15:18:35 | [diff] [blame] | 496 | (' PRESUBMIT.py | 2 +-\n' |
| 497 | ' 1 files changed, 1 insertions(+), 1 deletions(-)\n')), |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 498 | ((['git', 'show-ref', '--quiet', '--verify', |
[email protected] | 2e72bb1 | 2012-01-17 15:18:35 | [diff] [blame] | 499 | 'refs/heads/git-cl-commit'],), |
| 500 | (('', None), 0)), |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 501 | ((['git', 'branch', '-D', 'git-cl-commit'],), ''), |
| 502 | ((['git', 'show-ref', '--quiet', '--verify', |
[email protected] | 9bb85e2 | 2012-06-13 20:28:23 | [diff] [blame] | 503 | 'refs/heads/git-cl-cherry-pick'],), ''), |
[email protected] | 7a54e81 | 2014-02-11 19:57:22 | [diff] [blame] | 504 | ((['git', 'rev-parse', '--show-cdup'],), '\n'), |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 505 | ((['git', 'checkout', '-q', '-b', 'git-cl-commit'],), ''), |
| 506 | ((['git', 'reset', '--soft', 'fake_ancestor_sha'],), ''), |
| 507 | ((['git', 'commit', '-m', |
[email protected] | e52678e | 2013-04-26 18:34:44 | [diff] [blame] | 508 | 'Issue: 12345\n\[email protected]\n\n' |
[email protected] | 4b39c5f | 2015-07-07 10:33:12 | [diff] [blame] | 509 | 'Review URL: https://codereview.example.com/12345 .'],), |
[email protected] | 2e72bb1 | 2012-01-17 15:18:35 | [diff] [blame] | 510 | ''), |
[email protected] | 6abc652 | 2014-12-02 07:34:49 | [diff] [blame] | 511 | ((['git', 'config', 'rietveld.force-https-commit-url'],), ''), |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 512 | ((['git', |
[email protected] | f267b0e | 2013-05-02 09:11:43 | [diff] [blame] | 513 | 'svn', 'dcommit', '-C50', '--no-rebase', '--rmdir'],), |
[email protected] | 53937ba | 2012-10-02 18:20:43 | [diff] [blame] | 514 | (('', None), 0)), |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 515 | ((['git', 'checkout', '-q', 'working'],), ''), |
| 516 | ((['git', 'branch', '-D', 'git-cl-commit'],), ''), |
[email protected] | 7a54e81 | 2014-02-11 19:57:22 | [diff] [blame] | 517 | ] |
[email protected] | 2e72bb1 | 2012-01-17 15:18:35 | [diff] [blame] | 518 | |
[email protected] | ddd5941 | 2011-11-30 14:20:38 | [diff] [blame] | 519 | @staticmethod |
[email protected] | c1737d0 | 2013-05-29 14:17:28 | [diff] [blame] | 520 | def _cmd_line(description, args, similarity, find_copies, private): |
[email protected] | ddd5941 | 2011-11-30 14:20:38 | [diff] [blame] | 521 | """Returns the upload command line passed to upload.RealMain().""" |
[email protected] | ddd5941 | 2011-11-30 14:20:38 | [diff] [blame] | 522 | return [ |
| 523 | 'upload', '--assume_yes', '--server', |
[email protected] | eb5edbc | 2012-01-16 17:03:28 | [diff] [blame] | 524 | 'https://codereview.example.com', |
[email protected] | 71e12a9 | 2012-02-14 02:34:15 | [diff] [blame] | 525 | '--message', description |
[email protected] | ddd5941 | 2011-11-30 14:20:38 | [diff] [blame] | 526 | ] + args + [ |
| 527 | '--cc', '[email protected]', |
[email protected] | c1737d0 | 2013-05-29 14:17:28 | [diff] [blame] | 528 | ] + (['--private'] if private else []) + [ |
[email protected] | 7954005 | 2012-10-19 23:15:26 | [diff] [blame] | 529 | '--git_similarity', similarity or '50' |
| 530 | ] + (['--git_no_find_copies'] if find_copies == False else []) + [ |
[email protected] | 5e07e06 | 2013-02-28 23:55:44 | [diff] [blame] | 531 | 'fake_ancestor_sha', 'HEAD' |
[email protected] | ddd5941 | 2011-11-30 14:20:38 | [diff] [blame] | 532 | ] |
| 533 | |
| 534 | def _run_reviewer_test( |
| 535 | self, |
| 536 | upload_args, |
| 537 | expected_description, |
| 538 | returned_description, |
| 539 | final_description, |
[email protected] | c1737d0 | 2013-05-29 14:17:28 | [diff] [blame] | 540 | reviewers, |
| 541 | private=False): |
[email protected] | ddd5941 | 2011-11-30 14:20:38 | [diff] [blame] | 542 | """Generic reviewer test framework.""" |
[email protected] | 2825353 | 2016-04-14 13:46:56 | [diff] [blame] | 543 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
[email protected] | 53937ba | 2012-10-02 18:20:43 | [diff] [blame] | 544 | try: |
| 545 | similarity = upload_args[upload_args.index('--similarity')+1] |
| 546 | except ValueError: |
| 547 | similarity = None |
[email protected] | 7954005 | 2012-10-19 23:15:26 | [diff] [blame] | 548 | |
| 549 | if '--find-copies' in upload_args: |
| 550 | find_copies = True |
| 551 | elif '--no-find-copies' in upload_args: |
| 552 | find_copies = False |
| 553 | else: |
| 554 | find_copies = None |
| 555 | |
[email protected] | c1737d0 | 2013-05-29 14:17:28 | [diff] [blame] | 556 | private = '--private' in upload_args |
| 557 | |
| 558 | self.calls = self._upload_calls(similarity, find_copies, private) |
[email protected] | 87884cc | 2014-01-03 22:23:41 | [diff] [blame] | 559 | |
[email protected] | 615a262 | 2013-05-03 13:20:14 | [diff] [blame] | 560 | def RunEditor(desc, _, **kwargs): |
[email protected] | ddd5941 | 2011-11-30 14:20:38 | [diff] [blame] | 561 | self.assertEquals( |
| 562 | '# Enter a description of the change.\n' |
[email protected] | 104b2db | 2013-04-18 12:58:40 | [diff] [blame] | 563 | '# This will be displayed on the codereview site.\n' |
[email protected] | 63a4d7f | 2013-05-31 02:22:45 | [diff] [blame] | 564 | '# The first line will also be used as the subject of the review.\n' |
[email protected] | bd1073e | 2013-06-01 00:34:38 | [diff] [blame] | 565 | '#--------------------This line is 72 characters long' |
| 566 | '--------------------\n' + |
[email protected] | ddd5941 | 2011-11-30 14:20:38 | [diff] [blame] | 567 | expected_description, |
| 568 | desc) |
| 569 | return returned_description |
| 570 | self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor) |
[email protected] | 87884cc | 2014-01-03 22:23:41 | [diff] [blame] | 571 | |
[email protected] | ddd5941 | 2011-11-30 14:20:38 | [diff] [blame] | 572 | def check_upload(args): |
[email protected] | 7954005 | 2012-10-19 23:15:26 | [diff] [blame] | 573 | cmd_line = self._cmd_line(final_description, reviewers, similarity, |
[email protected] | c1737d0 | 2013-05-29 14:17:28 | [diff] [blame] | 574 | find_copies, private) |
[email protected] | 53937ba | 2012-10-02 18:20:43 | [diff] [blame] | 575 | self.assertEquals(cmd_line, args) |
[email protected] | ddd5941 | 2011-11-30 14:20:38 | [diff] [blame] | 576 | return 1, 2 |
| 577 | self.mock(git_cl.upload, 'RealMain', check_upload) |
[email protected] | 87884cc | 2014-01-03 22:23:41 | [diff] [blame] | 578 | |
[email protected] | ddd5941 | 2011-11-30 14:20:38 | [diff] [blame] | 579 | git_cl.main(['upload'] + upload_args) |
| 580 | |
| 581 | def test_no_reviewer(self): |
| 582 | self._run_reviewer_test( |
| 583 | [], |
[email protected] | 78936cb | 2013-04-11 00:17:52 | [diff] [blame] | 584 | 'desc\n\nBUG=', |
| 585 | '# Blah blah comment.\ndesc\n\nBUG=', |
| 586 | 'desc\n\nBUG=', |
[email protected] | ddd5941 | 2011-11-30 14:20:38 | [diff] [blame] | 587 | []) |
| 588 | |
[email protected] | 53937ba | 2012-10-02 18:20:43 | [diff] [blame] | 589 | def test_keep_similarity(self): |
| 590 | self._run_reviewer_test( |
| 591 | ['--similarity', '70'], |
[email protected] | 78936cb | 2013-04-11 00:17:52 | [diff] [blame] | 592 | 'desc\n\nBUG=', |
| 593 | '# Blah blah comment.\ndesc\n\nBUG=', |
| 594 | 'desc\n\nBUG=', |
[email protected] | 53937ba | 2012-10-02 18:20:43 | [diff] [blame] | 595 | []) |
| 596 | |
[email protected] | 7954005 | 2012-10-19 23:15:26 | [diff] [blame] | 597 | def test_keep_find_copies(self): |
| 598 | self._run_reviewer_test( |
| 599 | ['--no-find-copies'], |
[email protected] | 78936cb | 2013-04-11 00:17:52 | [diff] [blame] | 600 | 'desc\n\nBUG=', |
[email protected] | 7954005 | 2012-10-19 23:15:26 | [diff] [blame] | 601 | '# Blah blah comment.\ndesc\n\nBUG=\n', |
[email protected] | 78936cb | 2013-04-11 00:17:52 | [diff] [blame] | 602 | 'desc\n\nBUG=', |
[email protected] | 7954005 | 2012-10-19 23:15:26 | [diff] [blame] | 603 | []) |
| 604 | |
[email protected] | c1737d0 | 2013-05-29 14:17:28 | [diff] [blame] | 605 | def test_private(self): |
| 606 | self._run_reviewer_test( |
| 607 | ['--private'], |
| 608 | 'desc\n\nBUG=', |
| 609 | '# Blah blah comment.\ndesc\n\nBUG=\n', |
| 610 | 'desc\n\nBUG=', |
| 611 | []) |
| 612 | |
[email protected] | ddd5941 | 2011-11-30 14:20:38 | [diff] [blame] | 613 | def test_reviewers_cmd_line(self): |
| 614 | # Reviewer is passed as-is |
[email protected] | 78936cb | 2013-04-11 00:17:52 | [diff] [blame] | 615 | description = 'desc\n\[email protected]\nBUG=' |
[email protected] | ddd5941 | 2011-11-30 14:20:38 | [diff] [blame] | 616 | self._run_reviewer_test( |
| 617 | ['-r' '[email protected]'], |
| 618 | description, |
| 619 | '\n%s\n' % description, |
| 620 | description, |
[email protected] | 78936cb | 2013-04-11 00:17:52 | [diff] [blame] | 621 | ['[email protected]']) |
[email protected] | ddd5941 | 2011-11-30 14:20:38 | [diff] [blame] | 622 | |
| 623 | def test_reviewer_tbr_overriden(self): |
| 624 | # Reviewer is overriden with TBR |
| 625 | # Also verifies the regexp work without a trailing LF |
[email protected] | 78936cb | 2013-04-11 00:17:52 | [diff] [blame] | 626 | description = 'Foo Bar\n\[email protected]' |
[email protected] | ddd5941 | 2011-11-30 14:20:38 | [diff] [blame] | 627 | self._run_reviewer_test( |
| 628 | ['-r' '[email protected]'], |
[email protected] | 78936cb | 2013-04-11 00:17:52 | [diff] [blame] | 629 | 'desc\n\[email protected]\nBUG=', |
[email protected] | ddd5941 | 2011-11-30 14:20:38 | [diff] [blame] | 630 | description.strip('\n'), |
| 631 | description, |
[email protected] | 78936cb | 2013-04-11 00:17:52 | [diff] [blame] | 632 | ['[email protected]']) |
[email protected] | ddd5941 | 2011-11-30 14:20:38 | [diff] [blame] | 633 | |
| 634 | def test_reviewer_multiple(self): |
| 635 | # Handles multiple R= or TBR= lines. |
| 636 | description = ( |
[email protected] | 78936cb | 2013-04-11 00:17:52 | [diff] [blame] | 637 | 'Foo Bar\[email protected]\nBUG=\[email protected]') |
[email protected] | ddd5941 | 2011-11-30 14:20:38 | [diff] [blame] | 638 | self._run_reviewer_test( |
| 639 | [], |
[email protected] | 78936cb | 2013-04-11 00:17:52 | [diff] [blame] | 640 | 'desc\n\nBUG=', |
[email protected] | ddd5941 | 2011-11-30 14:20:38 | [diff] [blame] | 641 | description, |
| 642 | description, |
[email protected] | 78936cb | 2013-04-11 00:17:52 | [diff] [blame] | 643 | ['[email protected],[email protected]']) |
[email protected] | ddd5941 | 2011-11-30 14:20:38 | [diff] [blame] | 644 | |
[email protected] | a335365 | 2011-11-30 14:26:57 | [diff] [blame] | 645 | def test_reviewer_send_mail(self): |
| 646 | # --send-mail can be used without -r if R= is used |
[email protected] | 78936cb | 2013-04-11 00:17:52 | [diff] [blame] | 647 | description = 'Foo Bar\[email protected]' |
[email protected] | a335365 | 2011-11-30 14:26:57 | [diff] [blame] | 648 | self._run_reviewer_test( |
| 649 | ['--send-mail'], |
[email protected] | 78936cb | 2013-04-11 00:17:52 | [diff] [blame] | 650 | 'desc\n\nBUG=', |
[email protected] | a335365 | 2011-11-30 14:26:57 | [diff] [blame] | 651 | description.strip('\n'), |
| 652 | description, |
[email protected] | 78936cb | 2013-04-11 00:17:52 | [diff] [blame] | 653 | ['[email protected]', '--send_mail']) |
[email protected] | a335365 | 2011-11-30 14:26:57 | [diff] [blame] | 654 | |
| 655 | def test_reviewer_send_mail_no_rev(self): |
| 656 | # Fails without a reviewer. |
[email protected] | 2e23ce3 | 2013-05-07 12:42:28 | [diff] [blame] | 657 | stdout = StringIO.StringIO() |
| 658 | stderr = StringIO.StringIO() |
[email protected] | a335365 | 2011-11-30 14:26:57 | [diff] [blame] | 659 | try: |
[email protected] | 615a262 | 2013-05-03 13:20:14 | [diff] [blame] | 660 | self.calls = self._upload_no_rev_calls(None, None) |
| 661 | def RunEditor(desc, _, **kwargs): |
[email protected] | a335365 | 2011-11-30 14:26:57 | [diff] [blame] | 662 | return desc |
| 663 | self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor) |
[email protected] | 2e23ce3 | 2013-05-07 12:42:28 | [diff] [blame] | 664 | self.mock(sys, 'stdout', stdout) |
| 665 | self.mock(sys, 'stderr', stderr) |
[email protected] | a335365 | 2011-11-30 14:26:57 | [diff] [blame] | 666 | git_cl.main(['upload', '--send-mail']) |
| 667 | self.fail() |
| 668 | except SystemExit: |
[email protected] | 2e23ce3 | 2013-05-07 12:42:28 | [diff] [blame] | 669 | self.assertEqual( |
| 670 | 'Using 50% similarity for rename/copy detection. Override with ' |
| 671 | '--similarity.\n', |
| 672 | stdout.getvalue()) |
| 673 | self.assertEqual( |
| 674 | 'Must specify reviewers to send email.\n', stderr.getvalue()) |
[email protected] | a335365 | 2011-11-30 14:26:57 | [diff] [blame] | 675 | |
[email protected] | 2e72bb1 | 2012-01-17 15:18:35 | [diff] [blame] | 676 | def test_dcommit(self): |
[email protected] | 2825353 | 2016-04-14 13:46:56 | [diff] [blame] | 677 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
[email protected] | 2e72bb1 | 2012-01-17 15:18:35 | [diff] [blame] | 678 | self.calls = ( |
| 679 | self._dcommit_calls_1() + |
[email protected] | 0f58fa8 | 2012-11-05 01:45:20 | [diff] [blame] | 680 | self._git_sanity_checks('fake_ancestor_sha', 'working') + |
[email protected] | 2e72bb1 | 2012-01-17 15:18:35 | [diff] [blame] | 681 | self._dcommit_calls_normal() + |
[email protected] | 7a54e81 | 2014-02-11 19:57:22 | [diff] [blame] | 682 | self._dcommit_calls_3()) |
[email protected] | 2e72bb1 | 2012-01-17 15:18:35 | [diff] [blame] | 683 | git_cl.main(['dcommit']) |
| 684 | |
| 685 | def test_dcommit_bypass_hooks(self): |
[email protected] | 2825353 | 2016-04-14 13:46:56 | [diff] [blame] | 686 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
[email protected] | 2e72bb1 | 2012-01-17 15:18:35 | [diff] [blame] | 687 | self.calls = ( |
| 688 | self._dcommit_calls_1() + |
| 689 | self._dcommit_calls_bypassed() + |
[email protected] | 7a54e81 | 2014-02-11 19:57:22 | [diff] [blame] | 690 | self._dcommit_calls_3()) |
[email protected] | 2e72bb1 | 2012-01-17 15:18:35 | [diff] [blame] | 691 | git_cl.main(['dcommit', '--bypass-hooks']) |
| 692 | |
[email protected] | ddd5941 | 2011-11-30 14:20:38 | [diff] [blame] | 693 | |
[email protected] | 0f58fa8 | 2012-11-05 01:45:20 | [diff] [blame] | 694 | @classmethod |
[email protected] | 2825353 | 2016-04-14 13:46:56 | [diff] [blame] | 695 | def _gerrit_ensure_auth_calls(cls, issue=None, skip_auth_check=False): |
[email protected] | 00dbccd | 2016-04-15 07:24:43 | [diff] [blame] | 696 | cmd = ['git', 'config', '--bool', 'gerrit.skip-ensure-authenticated'] |
[email protected] | 2825353 | 2016-04-14 13:46:56 | [diff] [blame] | 697 | if skip_auth_check: |
| 698 | return [((cmd, ), 'true')] |
| 699 | |
| 700 | calls = [((cmd, ), '', subprocess2.CalledProcessError(1, '', '', '', ''))] |
[email protected] | fe30f18 | 2016-04-13 12:15:04 | [diff] [blame] | 701 | if issue: |
| 702 | calls.extend([ |
| 703 | ((['git', 'config', 'branch.master.gerritserver'],), ''), |
| 704 | ]) |
| 705 | calls.extend([ |
| 706 | ((['git', 'config', 'branch.master.merge'],), 'refs/heads/master'), |
| 707 | ((['git', 'config', 'branch.master.remote'],), 'origin'), |
| 708 | ((['git', 'config', 'remote.origin.url'],), |
| 709 | 'https://chromium.googlesource.com/my/repo'), |
| 710 | ((['git', 'config', 'remote.origin.url'],), |
| 711 | 'https://chromium.googlesource.com/my/repo'), |
| 712 | ]) |
| 713 | return calls |
| 714 | |
| 715 | @classmethod |
[email protected] | 512d79c | 2016-03-31 12:55:28 | [diff] [blame] | 716 | def _gerrit_base_calls(cls, issue=None): |
[email protected] | e807781 | 2012-02-03 03:41:46 | [diff] [blame] | 717 | return [ |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 718 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
| 719 | ((['git', 'config', '--int', '--get', |
[email protected] | 53937ba | 2012-10-02 18:20:43 | [diff] [blame] | 720 | 'branch.master.git-cl-similarity'],), ''), |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 721 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
| 722 | ((['git', 'config', '--int', '--get', |
[email protected] | 7954005 | 2012-10-19 23:15:26 | [diff] [blame] | 723 | 'branch.master.git-find-copies'],), ''), |
[email protected] | 512d79c | 2016-03-31 12:55:28 | [diff] [blame] | 724 | ] + cls._is_gerrit_calls(True) + [ |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 725 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
[email protected] | aa5ced1 | 2016-03-29 09:41:14 | [diff] [blame] | 726 | ((['git', 'config', 'branch.master.rietveldissue'],), ''), |
[email protected] | 512d79c | 2016-03-31 12:55:28 | [diff] [blame] | 727 | ((['git', 'config', 'branch.master.gerritissue'],), |
| 728 | '' if issue is None else str(issue)), |
[email protected] | fe30f18 | 2016-04-13 12:15:04 | [diff] [blame] | 729 | ((['git', 'config', 'branch.master.merge'],), 'refs/heads/master'), |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 730 | ((['git', 'config', 'branch.master.remote'],), 'origin'), |
[email protected] | fe30f18 | 2016-04-13 12:15:04 | [diff] [blame] | 731 | ((['get_or_create_merge_base', 'master', |
| 732 | 'refs/remotes/origin/master'],), |
[email protected] | 9e84927 | 2014-04-04 00:31:55 | [diff] [blame] | 733 | 'fake_ancestor_sha'), |
[email protected] | fe30f18 | 2016-04-13 12:15:04 | [diff] [blame] | 734 | # Calls to verify branch point is ancestor |
| 735 | ] + (cls._gerrit_ensure_auth_calls(issue=issue) + |
| 736 | cls._git_sanity_checks('fake_ancestor_sha', 'master', |
| 737 | get_remote_branch=False)) + [ |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 738 | ((['git', 'rev-parse', '--show-cdup'],), ''), |
| 739 | ((['git', 'rev-parse', 'HEAD'],), '12345'), |
[email protected] | fe30f18 | 2016-04-13 12:15:04 | [diff] [blame] | 740 | |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 741 | ((['git', |
[email protected] | 9249f64 | 2013-06-03 21:36:18 | [diff] [blame] | 742 | 'diff', '--name-status', '--no-renames', '-r', |
| 743 | 'fake_ancestor_sha...', '.'],), |
[email protected] | e807781 | 2012-02-03 03:41:46 | [diff] [blame] | 744 | 'M\t.gitignore\n'), |
[email protected] | aa5ced1 | 2016-03-29 09:41:14 | [diff] [blame] | 745 | ((['git', 'config', 'branch.master.gerritpatchset'],), ''), |
[email protected] | 512d79c | 2016-03-31 12:55:28 | [diff] [blame] | 746 | ] + ([] if issue else [ |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 747 | ((['git', |
[email protected] | f267b0e | 2013-05-02 09:11:43 | [diff] [blame] | 748 | 'log', '--pretty=format:%s%n%n%b', 'fake_ancestor_sha...'],), |
[email protected] | 0f58fa8 | 2012-11-05 01:45:20 | [diff] [blame] | 749 | 'foo'), |
[email protected] | 512d79c | 2016-03-31 12:55:28 | [diff] [blame] | 750 | ]) + [ |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 751 | ((['git', 'config', 'user.email'],), '[email protected]'), |
| 752 | ((['git', |
[email protected] | f267b0e | 2013-05-02 09:11:43 | [diff] [blame] | 753 | 'diff', '--no-ext-diff', '--stat', '--find-copies-harder', |
[email protected] | 5e07e06 | 2013-02-28 23:55:44 | [diff] [blame] | 754 | '-l100000', '-C50', 'fake_ancestor_sha', 'HEAD'],), |
[email protected] | e807781 | 2012-02-03 03:41:46 | [diff] [blame] | 755 | '+dat'), |
[email protected] | aa5ced1 | 2016-03-29 09:41:14 | [diff] [blame] | 756 | ] |
[email protected] | e807781 | 2012-02-03 03:41:46 | [diff] [blame] | 757 | |
[email protected] | 1e67bb7 | 2016-02-11 12:15:49 | [diff] [blame] | 758 | @classmethod |
| 759 | def _gerrit_upload_calls(cls, description, reviewers, squash, |
[email protected] | 1062500 | 2016-03-04 20:03:47 | [diff] [blame] | 760 | expected_upstream_ref='origin/refs/heads/master', |
[email protected] | 8da4540 | 2016-05-24 23:11:03 | [diff] [blame] | 761 | ref_suffix='', notify=False, |
[email protected] | 512d79c | 2016-03-31 12:55:28 | [diff] [blame] | 762 | post_amend_description=None, issue=None): |
[email protected] | 1062500 | 2016-03-04 20:03:47 | [diff] [blame] | 763 | if post_amend_description is None: |
| 764 | post_amend_description = description |
[email protected] | 512d79c | 2016-03-31 12:55:28 | [diff] [blame] | 765 | |
[email protected] | e807781 | 2012-02-03 03:41:46 | [diff] [blame] | 766 | calls = [ |
[email protected] | 54b400c | 2016-01-14 10:08:25 | [diff] [blame] | 767 | ((['git', 'config', '--bool', 'gerrit.squash-uploads'],), 'false'), |
[email protected] | 512d79c | 2016-03-31 12:55:28 | [diff] [blame] | 768 | ] |
| 769 | # If issue is given, then description is fetched from Gerrit instead. |
| 770 | if issue is None: |
| 771 | if squash: |
| 772 | calls += [ |
| 773 | ((['git', 'show', '--format=%B', '-s', |
| 774 | 'refs/heads/git_cl_uploads/master'],), '')] |
| 775 | calls += [ |
| 776 | ((['git', 'log', '--pretty=format:%s\n\n%b', |
| 777 | 'fake_ancestor_sha..HEAD'],), |
| 778 | description)] |
[email protected] | 57d8654 | 2016-03-04 16:11:32 | [diff] [blame] | 779 | if not git_footers.get_footer_change_id(description) and not squash: |
[email protected] | aebe87f | 2012-10-22 20:34:21 | [diff] [blame] | 780 | calls += [ |
[email protected] | 1062500 | 2016-03-04 20:03:47 | [diff] [blame] | 781 | # DownloadGerritHook(False) |
| 782 | ((False, ), |
| 783 | ''), |
| 784 | # Amending of commit message to get the Change-Id. |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 785 | ((['git', 'log', '--pretty=format:%s\n\n%b', |
[email protected] | 5e07e06 | 2013-02-28 23:55:44 | [diff] [blame] | 786 | 'fake_ancestor_sha..HEAD'],), |
[email protected] | aebe87f | 2012-10-22 20:34:21 | [diff] [blame] | 787 | description), |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 788 | ((['git', 'commit', '--amend', '-m', description],), |
[email protected] | aebe87f | 2012-10-22 20:34:21 | [diff] [blame] | 789 | ''), |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 790 | ((['git', 'log', '--pretty=format:%s\n\n%b', |
[email protected] | 5e07e06 | 2013-02-28 23:55:44 | [diff] [blame] | 791 | 'fake_ancestor_sha..HEAD'],), |
[email protected] | 1062500 | 2016-03-04 20:03:47 | [diff] [blame] | 792 | post_amend_description) |
[email protected] | aebe87f | 2012-10-22 20:34:21 | [diff] [blame] | 793 | ] |
[email protected] | 27386dd | 2015-02-16 10:45:39 | [diff] [blame] | 794 | if squash: |
[email protected] | 512d79c | 2016-03-31 12:55:28 | [diff] [blame] | 795 | if not issue: |
| 796 | # Prompting to edit description on first upload. |
| 797 | calls += [ |
| 798 | ((['git', 'config', 'core.editor'],), ''), |
| 799 | ((['RunEditor'],), description), |
| 800 | ] |
[email protected] | 27386dd | 2015-02-16 10:45:39 | [diff] [blame] | 801 | ref_to_push = 'abcdef0123456789' |
| 802 | calls += [ |
[email protected] | 27386dd | 2015-02-16 10:45:39 | [diff] [blame] | 803 | ((['git', 'config', 'branch.master.merge'],), |
| 804 | 'refs/heads/master'), |
| 805 | ((['git', 'config', 'branch.master.remote'],), |
| 806 | 'origin'), |
[email protected] | fe30f18 | 2016-04-13 12:15:04 | [diff] [blame] | 807 | ((['get_or_create_merge_base', 'master', |
| 808 | 'refs/remotes/origin/master'],), |
[email protected] | 27386dd | 2015-02-16 10:45:39 | [diff] [blame] | 809 | 'origin/master'), |
| 810 | ((['git', 'rev-parse', 'HEAD:'],), |
| 811 | '0123456789abcdef'), |
| 812 | ((['git', 'commit-tree', '0123456789abcdef', '-p', |
[email protected] | 512d79c | 2016-03-31 12:55:28 | [diff] [blame] | 813 | 'origin/master', '-m', description],), |
[email protected] | 27386dd | 2015-02-16 10:45:39 | [diff] [blame] | 814 | ref_to_push), |
| 815 | ] |
| 816 | else: |
| 817 | ref_to_push = 'HEAD' |
| 818 | |
[email protected] | aebe87f | 2012-10-22 20:34:21 | [diff] [blame] | 819 | calls += [ |
[email protected] | 609f395 | 2015-05-04 22:47:04 | [diff] [blame] | 820 | ((['git', 'rev-list', |
| 821 | expected_upstream_ref + '..' + ref_to_push],), ''), |
[email protected] | 82b91cd | 2013-07-09 06:33:41 | [diff] [blame] | 822 | ((['git', 'config', 'rietveld.cc'],), '') |
[email protected] | e807781 | 2012-02-03 03:41:46 | [diff] [blame] | 823 | ] |
[email protected] | 8acd833 | 2016-04-13 12:56:03 | [diff] [blame] | 824 | # Add cc from watch list. |
[email protected] | 0b2d707 | 2016-04-18 16:19:03 | [diff] [blame] | 825 | # TODO(tandrii): bring this back after http://crbug.com/604377. |
| 826 | # if ref_suffix == '': |
| 827 | # ref_suffix = '%[email protected]' |
| 828 | # else: |
| 829 | # ref_suffix += ',[email protected]' |
[email protected] | 8da4540 | 2016-05-24 23:11:03 | [diff] [blame] | 830 | |
| 831 | notify_suffix = 'notify=%s' % ('ALL' if notify else 'NONE') |
| 832 | if ref_suffix: |
| 833 | ref_suffix += ',' + notify_suffix |
| 834 | else: |
| 835 | ref_suffix = '%' + notify_suffix |
[email protected] | e807781 | 2012-02-03 03:41:46 | [diff] [blame] | 836 | if reviewers: |
[email protected] | 8da4540 | 2016-05-24 23:11:03 | [diff] [blame] | 837 | ref_suffix += ',' + ','.join('r=%s' % email |
| 838 | for email in sorted(reviewers)) |
[email protected] | e807781 | 2012-02-03 03:41:46 | [diff] [blame] | 839 | calls += [ |
[email protected] | 8acd833 | 2016-04-13 12:56:03 | [diff] [blame] | 840 | ((['git', 'push', 'origin', |
[email protected] | bf766ba | 2016-04-13 12:51:23 | [diff] [blame] | 841 | ref_to_push + ':refs/for/refs/heads/master' + ref_suffix],), |
[email protected] | a342c92 | 2016-03-16 07:08:25 | [diff] [blame] | 842 | ('remote:\n' |
| 843 | 'remote: Processing changes: (\)\n' |
| 844 | 'remote: Processing changes: (|)\n' |
| 845 | 'remote: Processing changes: (/)\n' |
| 846 | 'remote: Processing changes: (-)\n' |
| 847 | 'remote: Processing changes: new: 1 (/)\n' |
| 848 | 'remote: Processing changes: new: 1, done\n' |
| 849 | 'remote:\n' |
| 850 | 'remote: New Changes:\n' |
| 851 | 'remote: https://chromium-review.googlesource.com/123456 XXX.\n' |
| 852 | 'remote:\n' |
| 853 | 'To https://chromium.googlesource.com/yyy/zzz\n' |
| 854 | ' * [new branch] hhhh -> refs/for/refs/heads/master\n')), |
[email protected] | e807781 | 2012-02-03 03:41:46 | [diff] [blame] | 855 | ] |
[email protected] | 27386dd | 2015-02-16 10:45:39 | [diff] [blame] | 856 | if squash: |
| 857 | calls += [ |
[email protected] | aa5ced1 | 2016-03-29 09:41:14 | [diff] [blame] | 858 | ((['git', 'config', 'branch.master.gerritissue', '123456'],), ''), |
[email protected] | aa5ced1 | 2016-03-29 09:41:14 | [diff] [blame] | 859 | ((['git', 'config', 'branch.master.gerritserver', |
| 860 | 'https://chromium-review.googlesource.com'],), ''), |
[email protected] | 512d79c | 2016-03-31 12:55:28 | [diff] [blame] | 861 | ((['git', 'config', 'branch.master.gerritsquashhash', |
| 862 | 'abcdef0123456789'],), ''), |
[email protected] | fe30f18 | 2016-04-13 12:15:04 | [diff] [blame] | 863 | ] |
[email protected] | 1e67bb7 | 2016-02-11 12:15:49 | [diff] [blame] | 864 | calls += cls._git_post_upload_calls() |
[email protected] | e807781 | 2012-02-03 03:41:46 | [diff] [blame] | 865 | return calls |
| 866 | |
[email protected] | aebe87f | 2012-10-22 20:34:21 | [diff] [blame] | 867 | def _run_gerrit_upload_test( |
[email protected] | e807781 | 2012-02-03 03:41:46 | [diff] [blame] | 868 | self, |
| 869 | upload_args, |
| 870 | description, |
[email protected] | bf766ba | 2016-04-13 12:51:23 | [diff] [blame] | 871 | reviewers=None, |
[email protected] | 609f395 | 2015-05-04 22:47:04 | [diff] [blame] | 872 | squash=False, |
[email protected] | 1062500 | 2016-03-04 20:03:47 | [diff] [blame] | 873 | expected_upstream_ref='origin/refs/heads/master', |
[email protected] | bf766ba | 2016-04-13 12:51:23 | [diff] [blame] | 874 | ref_suffix='', |
[email protected] | 8da4540 | 2016-05-24 23:11:03 | [diff] [blame] | 875 | notify=False, |
[email protected] | 512d79c | 2016-03-31 12:55:28 | [diff] [blame] | 876 | post_amend_description=None, |
| 877 | issue=None): |
[email protected] | aebe87f | 2012-10-22 20:34:21 | [diff] [blame] | 878 | """Generic gerrit upload test framework.""" |
[email protected] | bf766ba | 2016-04-13 12:51:23 | [diff] [blame] | 879 | reviewers = reviewers or [] |
[email protected] | 2825353 | 2016-04-14 13:46:56 | [diff] [blame] | 880 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
[email protected] | fe30f18 | 2016-04-13 12:15:04 | [diff] [blame] | 881 | self.mock(git_cl.gerrit_util, "CookiesAuthenticator", |
| 882 | CookiesAuthenticatorMockFactory(same_cookie='same_cred')) |
[email protected] | 512d79c | 2016-03-31 12:55:28 | [diff] [blame] | 883 | self.calls = self._gerrit_base_calls(issue=issue) |
[email protected] | 609f395 | 2015-05-04 22:47:04 | [diff] [blame] | 884 | self.calls += self._gerrit_upload_calls( |
| 885 | description, reviewers, squash, |
[email protected] | 1062500 | 2016-03-04 20:03:47 | [diff] [blame] | 886 | expected_upstream_ref=expected_upstream_ref, |
[email protected] | 8da4540 | 2016-05-24 23:11:03 | [diff] [blame] | 887 | ref_suffix=ref_suffix, notify=notify, |
[email protected] | 512d79c | 2016-03-31 12:55:28 | [diff] [blame] | 888 | post_amend_description=post_amend_description, |
| 889 | issue=issue) |
[email protected] | 09d7a6a | 2016-03-04 15:44:48 | [diff] [blame] | 890 | # Uncomment when debugging. |
| 891 | # print '\n'.join(map(lambda x: '%2i: %s' % x, enumerate(self.calls))) |
[email protected] | e807781 | 2012-02-03 03:41:46 | [diff] [blame] | 892 | git_cl.main(['upload'] + upload_args) |
| 893 | |
[email protected] | aebe87f | 2012-10-22 20:34:21 | [diff] [blame] | 894 | def test_gerrit_upload_without_change_id(self): |
[email protected] | 1062500 | 2016-03-04 20:03:47 | [diff] [blame] | 895 | self.mock(git_cl, 'DownloadGerritHook', self._mocked_call) |
[email protected] | aebe87f | 2012-10-22 20:34:21 | [diff] [blame] | 896 | self._run_gerrit_upload_test( |
[email protected] | e807781 | 2012-02-03 03:41:46 | [diff] [blame] | 897 | [], |
[email protected] | 35d1a84 | 2012-07-27 00:20:43 | [diff] [blame] | 898 | 'desc\n\nBUG=\n', |
[email protected] | 1062500 | 2016-03-04 20:03:47 | [diff] [blame] | 899 | [], |
| 900 | post_amend_description='desc\n\nBUG=\n\nChange-Id: Ixxx') |
[email protected] | e807781 | 2012-02-03 03:41:46 | [diff] [blame] | 901 | |
[email protected] | aebe87f | 2012-10-22 20:34:21 | [diff] [blame] | 902 | def test_gerrit_no_reviewer(self): |
| 903 | self._run_gerrit_upload_test( |
| 904 | [], |
[email protected] | 09d7a6a | 2016-03-04 15:44:48 | [diff] [blame] | 905 | 'desc\n\nBUG=\n\nChange-Id: I123456789\n', |
[email protected] | aebe87f | 2012-10-22 20:34:21 | [diff] [blame] | 906 | []) |
| 907 | |
[email protected] | bf766ba | 2016-04-13 12:51:23 | [diff] [blame] | 908 | def test_gerrit_patch_title(self): |
| 909 | self._run_gerrit_upload_test( |
| 910 | ['-t', 'Don\'t put under_scores as they become spaces'], |
| 911 | 'desc\n\nBUG=\n\nChange-Id: I123456789', |
| 912 | ref_suffix='%m=Don\'t_put_under_scores_as_they_become_spaces') |
| 913 | |
[email protected] | e807781 | 2012-02-03 03:41:46 | [diff] [blame] | 914 | def test_gerrit_reviewers_cmd_line(self): |
[email protected] | aebe87f | 2012-10-22 20:34:21 | [diff] [blame] | 915 | self._run_gerrit_upload_test( |
[email protected] | 8da4540 | 2016-05-24 23:11:03 | [diff] [blame] | 916 | ['-r', '[email protected]', '--send-mail'], |
[email protected] | 09d7a6a | 2016-03-04 15:44:48 | [diff] [blame] | 917 | 'desc\n\nBUG=\n\nChange-Id: I123456789', |
[email protected] | 8da4540 | 2016-05-24 23:11:03 | [diff] [blame] | 918 | ['[email protected]'], |
| 919 | notify=True) |
[email protected] | e807781 | 2012-02-03 03:41:46 | [diff] [blame] | 920 | |
| 921 | def test_gerrit_reviewer_multiple(self): |
[email protected] | aebe87f | 2012-10-22 20:34:21 | [diff] [blame] | 922 | self._run_gerrit_upload_test( |
[email protected] | e807781 | 2012-02-03 03:41:46 | [diff] [blame] | 923 | [], |
[email protected] | 09d7a6a | 2016-03-04 15:44:48 | [diff] [blame] | 924 | 'desc\[email protected]\nBUG=\[email protected]\n\n' |
| 925 | 'Change-Id: 123456789\n', |
[email protected] | e807781 | 2012-02-03 03:41:46 | [diff] [blame] | 926 | ['[email protected]', '[email protected]']) |
| 927 | |
[email protected] | 512d79c | 2016-03-31 12:55:28 | [diff] [blame] | 928 | def test_gerrit_upload_squash_first(self): |
| 929 | # Mock Gerrit CL description to indicate the first upload. |
| 930 | self.mock(git_cl.Changelist, 'GetDescription', |
| 931 | lambda *_: None) |
| 932 | self.mock(git_cl.gclient_utils, 'RunEditor', |
| 933 | lambda *_, **__: self._mocked_call(['RunEditor'])) |
[email protected] | 27386dd | 2015-02-16 10:45:39 | [diff] [blame] | 934 | self._run_gerrit_upload_test( |
| 935 | ['--squash'], |
[email protected] | 512d79c | 2016-03-31 12:55:28 | [diff] [blame] | 936 | 'desc\nBUG=\n\nChange-Id: 123456789', |
[email protected] | 27386dd | 2015-02-16 10:45:39 | [diff] [blame] | 937 | [], |
[email protected] | 609f395 | 2015-05-04 22:47:04 | [diff] [blame] | 938 | squash=True, |
| 939 | expected_upstream_ref='origin/master') |
[email protected] | e807781 | 2012-02-03 03:41:46 | [diff] [blame] | 940 | |
[email protected] | 512d79c | 2016-03-31 12:55:28 | [diff] [blame] | 941 | def test_gerrit_upload_squash_reupload(self): |
| 942 | description = 'desc\nBUG=\n\nChange-Id: 123456789' |
| 943 | # Mock Gerrit CL description to indicate re-upload. |
| 944 | self.mock(git_cl.Changelist, 'GetDescription', |
| 945 | lambda *args: description) |
| 946 | self.mock(git_cl.Changelist, 'GetMostRecentPatchset', |
| 947 | lambda *args: 1) |
| 948 | self.mock(git_cl._GerritChangelistImpl, '_GetChangeDetail', |
| 949 | lambda *args: {'change_id': '123456789'}) |
| 950 | self._run_gerrit_upload_test( |
| 951 | ['--squash'], |
| 952 | description, |
| 953 | [], |
| 954 | squash=True, |
| 955 | expected_upstream_ref='origin/master', |
| 956 | issue=123456) |
| 957 | |
[email protected] | 2dd9986 | 2015-06-22 12:22:18 | [diff] [blame] | 958 | def test_upload_branch_deps(self): |
[email protected] | 2825353 | 2016-04-14 13:46:56 | [diff] [blame] | 959 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
[email protected] | 2dd9986 | 2015-06-22 12:22:18 | [diff] [blame] | 960 | def mock_run_git(*args, **_kwargs): |
| 961 | if args[0] == ['for-each-ref', |
| 962 | '--format=%(refname:short) %(upstream:short)', |
| 963 | 'refs/heads']: |
| 964 | # Create a local branch dependency tree that looks like this: |
| 965 | # test1 -> test2 -> test3 -> test4 -> test5 |
| 966 | # -> test3.1 |
| 967 | # test6 -> test0 |
| 968 | branch_deps = [ |
| 969 | 'test2 test1', # test1 -> test2 |
| 970 | 'test3 test2', # test2 -> test3 |
| 971 | 'test3.1 test2', # test2 -> test3.1 |
| 972 | 'test4 test3', # test3 -> test4 |
| 973 | 'test5 test4', # test4 -> test5 |
| 974 | 'test6 test0', # test0 -> test6 |
| 975 | 'test7', # test7 |
| 976 | ] |
| 977 | return '\n'.join(branch_deps) |
| 978 | self.mock(git_cl, 'RunGit', mock_run_git) |
| 979 | |
| 980 | class RecordCalls: |
| 981 | times_called = 0 |
| 982 | record_calls = RecordCalls() |
| 983 | def mock_CMDupload(*args, **_kwargs): |
| 984 | record_calls.times_called += 1 |
| 985 | return 0 |
| 986 | self.mock(git_cl, 'CMDupload', mock_CMDupload) |
| 987 | |
| 988 | self.calls = [ |
| 989 | (('[Press enter to continue or ctrl-C to quit]',), ''), |
| 990 | ] |
| 991 | |
| 992 | class MockChangelist(): |
| 993 | def __init__(self): |
| 994 | pass |
| 995 | def GetBranch(self): |
| 996 | return 'test1' |
| 997 | def GetIssue(self): |
| 998 | return '123' |
| 999 | def GetPatchset(self): |
| 1000 | return '1001' |
[email protected] | 4c72b08 | 2016-03-31 22:26:35 | [diff] [blame] | 1001 | def IsGerrit(self): |
| 1002 | return False |
[email protected] | 2dd9986 | 2015-06-22 12:22:18 | [diff] [blame] | 1003 | |
| 1004 | ret = git_cl.upload_branch_deps(MockChangelist(), []) |
| 1005 | # CMDupload should have been called 5 times because of 5 dependent branches. |
| 1006 | self.assertEquals(5, record_calls.times_called) |
| 1007 | self.assertEquals(0, ret) |
| 1008 | |
[email protected] | 65874e1 | 2016-03-04 12:03:02 | [diff] [blame] | 1009 | def test_gerrit_change_id(self): |
| 1010 | self.calls = [ |
| 1011 | ((['git', 'write-tree'], ), |
| 1012 | 'hashtree'), |
| 1013 | ((['git', 'rev-parse', 'HEAD~0'], ), |
| 1014 | 'branch-parent'), |
| 1015 | ((['git', 'var', 'GIT_AUTHOR_IDENT'], ), |
| 1016 | 'A B <[email protected]> 1456848326 +0100'), |
| 1017 | ((['git', 'var', 'GIT_COMMITTER_IDENT'], ), |
| 1018 | 'C D <[email protected]> 1456858326 +0100'), |
| 1019 | ((['git', 'hash-object', '-t', 'commit', '--stdin'], ), |
| 1020 | 'hashchange'), |
| 1021 | ] |
| 1022 | change_id = git_cl.GenerateGerritChangeId('line1\nline2\n') |
| 1023 | self.assertEqual(change_id, 'Ihashchange') |
| 1024 | |
[email protected] | 78936cb | 2013-04-11 00:17:52 | [diff] [blame] | 1025 | def test_update_reviewers(self): |
| 1026 | data = [ |
| 1027 | ('foo', [], 'foo'), |
[email protected] | 42c2079 | 2013-09-12 17:34:49 | [diff] [blame] | 1028 | ('foo\nR=xx', [], 'foo\nR=xx'), |
| 1029 | ('foo\nTBR=xx', [], 'foo\nTBR=xx'), |
[email protected] | 78936cb | 2013-04-11 00:17:52 | [diff] [blame] | 1030 | ('foo', ['a@c'], 'foo\n\nR=a@c'), |
[email protected] | 42c2079 | 2013-09-12 17:34:49 | [diff] [blame] | 1031 | ('foo\nR=xx', ['a@c'], 'foo\n\nR=a@c, xx'), |
| 1032 | ('foo\nTBR=xx', ['a@c'], 'foo\n\nR=a@c\nTBR=xx'), |
| 1033 | ('foo\nTBR=xx\nR=yy', ['a@c'], 'foo\n\nR=a@c, yy\nTBR=xx'), |
[email protected] | 78936cb | 2013-04-11 00:17:52 | [diff] [blame] | 1034 | ('foo\nBUG=', ['a@c'], 'foo\nBUG=\nR=a@c'), |
[email protected] | 42c2079 | 2013-09-12 17:34:49 | [diff] [blame] | 1035 | ('foo\nR=xx\nTBR=yy\nR=bar', ['a@c'], 'foo\n\nR=a@c, xx, bar\nTBR=yy'), |
[email protected] | 78936cb | 2013-04-11 00:17:52 | [diff] [blame] | 1036 | ('foo', ['a@c', 'b@c'], 'foo\n\nR=a@c, b@c'), |
[email protected] | c6f60e8 | 2013-04-19 17:01:57 | [diff] [blame] | 1037 | ('foo\nBar\n\nR=\nBUG=', ['c@c'], 'foo\nBar\n\nR=c@c\nBUG='), |
| 1038 | ('foo\nBar\n\nR=\nBUG=\nR=', ['c@c'], 'foo\nBar\n\nR=c@c\nBUG='), |
| 1039 | # Same as the line before, but full of whitespaces. |
| 1040 | ( |
| 1041 | 'foo\nBar\n\n R = \n BUG = \n R = ', ['c@c'], |
| 1042 | 'foo\nBar\n\nR=c@c\n BUG =', |
| 1043 | ), |
| 1044 | # Whitespaces aren't interpreted as new lines. |
| 1045 | ('foo BUG=allo R=joe ', ['c@c'], 'foo BUG=allo R=joe\n\nR=c@c'), |
[email protected] | 78936cb | 2013-04-11 00:17:52 | [diff] [blame] | 1046 | ] |
[email protected] | c6f60e8 | 2013-04-19 17:01:57 | [diff] [blame] | 1047 | expected = [i[2] for i in data] |
| 1048 | actual = [] |
| 1049 | for orig, reviewers, _expected in data: |
[email protected] | 78936cb | 2013-04-11 00:17:52 | [diff] [blame] | 1050 | obj = git_cl.ChangeDescription(orig) |
| 1051 | obj.update_reviewers(reviewers) |
[email protected] | c6f60e8 | 2013-04-19 17:01:57 | [diff] [blame] | 1052 | actual.append(obj.description) |
| 1053 | self.assertEqual(expected, actual) |
[email protected] | 78936cb | 2013-04-11 00:17:52 | [diff] [blame] | 1054 | |
[email protected] | 455dc92 | 2015-01-26 20:15:50 | [diff] [blame] | 1055 | def test_get_target_ref(self): |
| 1056 | # Check remote or remote branch not present. |
| 1057 | self.assertEqual(None, git_cl.GetTargetRef('origin', None, 'master', None)) |
| 1058 | self.assertEqual(None, git_cl.GetTargetRef(None, |
| 1059 | 'refs/remotes/origin/master', |
| 1060 | 'master', None)) |
[email protected] | 27386dd | 2015-02-16 10:45:39 | [diff] [blame] | 1061 | |
[email protected] | 455dc92 | 2015-01-26 20:15:50 | [diff] [blame] | 1062 | # Check default target refs for branches. |
| 1063 | self.assertEqual('refs/heads/master', |
| 1064 | git_cl.GetTargetRef('origin', 'refs/remotes/origin/master', |
| 1065 | None, None)) |
| 1066 | self.assertEqual('refs/heads/master', |
| 1067 | git_cl.GetTargetRef('origin', 'refs/remotes/origin/lkgr', |
| 1068 | None, None)) |
| 1069 | self.assertEqual('refs/heads/master', |
| 1070 | git_cl.GetTargetRef('origin', 'refs/remotes/origin/lkcr', |
| 1071 | None, None)) |
| 1072 | self.assertEqual('refs/branch-heads/123', |
| 1073 | git_cl.GetTargetRef('origin', |
| 1074 | 'refs/remotes/branch-heads/123', |
| 1075 | None, None)) |
| 1076 | self.assertEqual('refs/diff/test', |
| 1077 | git_cl.GetTargetRef('origin', |
| 1078 | 'refs/remotes/origin/refs/diff/test', |
| 1079 | None, None)) |
[email protected] | c68112d | 2015-03-03 12:48:06 | [diff] [blame] | 1080 | self.assertEqual('refs/heads/chrome/m42', |
| 1081 | git_cl.GetTargetRef('origin', |
| 1082 | 'refs/remotes/origin/chrome/m42', |
| 1083 | None, None)) |
[email protected] | 455dc92 | 2015-01-26 20:15:50 | [diff] [blame] | 1084 | |
| 1085 | # Check target refs for user-specified target branch. |
| 1086 | for branch in ('branch-heads/123', 'remotes/branch-heads/123', |
| 1087 | 'refs/remotes/branch-heads/123'): |
| 1088 | self.assertEqual('refs/branch-heads/123', |
| 1089 | git_cl.GetTargetRef('origin', |
| 1090 | 'refs/remotes/origin/master', |
| 1091 | branch, None)) |
| 1092 | for branch in ('origin/master', 'remotes/origin/master', |
| 1093 | 'refs/remotes/origin/master'): |
| 1094 | self.assertEqual('refs/heads/master', |
| 1095 | git_cl.GetTargetRef('origin', |
| 1096 | 'refs/remotes/branch-heads/123', |
| 1097 | branch, None)) |
| 1098 | for branch in ('master', 'heads/master', 'refs/heads/master'): |
| 1099 | self.assertEqual('refs/heads/master', |
| 1100 | git_cl.GetTargetRef('origin', |
| 1101 | 'refs/remotes/branch-heads/123', |
| 1102 | branch, None)) |
| 1103 | |
| 1104 | # Check target refs for pending prefix. |
| 1105 | self.assertEqual('prefix/heads/master', |
| 1106 | git_cl.GetTargetRef('origin', 'refs/remotes/origin/master', |
| 1107 | None, 'prefix/')) |
| 1108 | |
[email protected] | a872e75 | 2015-04-28 23:42:18 | [diff] [blame] | 1109 | def test_patch_when_dirty(self): |
| 1110 | # Patch when local tree is dirty |
| 1111 | self.mock(git_common, 'is_dirty_git_tree', lambda x: True) |
[email protected] | a872e75 | 2015-04-28 23:42:18 | [diff] [blame] | 1112 | self.assertNotEqual(git_cl.main(['patch', '123456']), 0) |
| 1113 | |
| 1114 | def test_diff_when_dirty(self): |
| 1115 | # Do 'git cl diff' when local tree is dirty |
| 1116 | self.mock(git_common, 'is_dirty_git_tree', lambda x: True) |
| 1117 | self.assertNotEqual(git_cl.main(['diff']), 0) |
| 1118 | |
[email protected] | dde6462 | 2016-04-13 17:11:21 | [diff] [blame] | 1119 | def _patch_common(self, is_gerrit=False, force_codereview=False): |
[email protected] | 2825353 | 2016-04-14 13:46:56 | [diff] [blame] | 1120 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
[email protected] | aa5ced1 | 2016-03-29 09:41:14 | [diff] [blame] | 1121 | self.mock(git_cl._RietveldChangelistImpl, 'GetMostRecentPatchset', |
| 1122 | lambda x: '60001') |
| 1123 | self.mock(git_cl._RietveldChangelistImpl, 'GetPatchSetDiff', |
| 1124 | lambda *args: None) |
[email protected] | f86c7d3 | 2016-04-01 19:27:30 | [diff] [blame] | 1125 | self.mock(git_cl._GerritChangelistImpl, '_GetChangeDetail', |
| 1126 | lambda *args: { |
| 1127 | 'current_revision': '7777777777', |
| 1128 | 'revisions': { |
| 1129 | '1111111111': { |
| 1130 | '_number': 1, |
| 1131 | 'fetch': {'http': { |
| 1132 | 'url': 'https://chromium.googlesource.com/my/repo', |
| 1133 | 'ref': 'refs/changes/56/123456/1', |
| 1134 | }}, |
| 1135 | }, |
| 1136 | '7777777777': { |
| 1137 | '_number': 7, |
| 1138 | 'fetch': {'http': { |
| 1139 | 'url': 'https://chromium.googlesource.com/my/repo', |
| 1140 | 'ref': 'refs/changes/56/123456/7', |
| 1141 | }}, |
| 1142 | }, |
| 1143 | }, |
| 1144 | }) |
[email protected] | aa5ced1 | 2016-03-29 09:41:14 | [diff] [blame] | 1145 | self.mock(git_cl.Changelist, 'GetDescription', |
| 1146 | lambda *args: 'Description') |
[email protected] | a872e75 | 2015-04-28 23:42:18 | [diff] [blame] | 1147 | self.mock(git_cl, 'IsGitVersionAtLeast', lambda *args: True) |
| 1148 | |
[email protected] | c2786d9 | 2016-05-31 19:53:50 | [diff] [blame] | 1149 | self.calls = self.calls or [] |
[email protected] | dde6462 | 2016-04-13 17:11:21 | [diff] [blame] | 1150 | if not force_codereview: |
| 1151 | # These calls detect codereview to use. |
[email protected] | c2786d9 | 2016-05-31 19:53:50 | [diff] [blame] | 1152 | self.calls += [ |
[email protected] | dde6462 | 2016-04-13 17:11:21 | [diff] [blame] | 1153 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
| 1154 | ((['git', 'config', 'branch.master.rietveldissue'],), ''), |
| 1155 | ((['git', 'config', 'branch.master.gerritissue'],), ''), |
| 1156 | ((['git', 'config', 'rietveld.autoupdate'],), ''), |
[email protected] | f86c7d3 | 2016-04-01 19:27:30 | [diff] [blame] | 1157 | ] |
[email protected] | dde6462 | 2016-04-13 17:11:21 | [diff] [blame] | 1158 | |
| 1159 | if is_gerrit: |
| 1160 | if not force_codereview: |
| 1161 | self.calls += [ |
| 1162 | ((['git', 'config', 'gerrit.host'],), 'true'), |
| 1163 | ] |
| 1164 | else: |
[email protected] | f86c7d3 | 2016-04-01 19:27:30 | [diff] [blame] | 1165 | self.calls += [ |
| 1166 | ((['git', 'config', 'gerrit.host'],), ''), |
| 1167 | ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'), |
| 1168 | ((['git', 'rev-parse', '--show-cdup'],), ''), |
| 1169 | ((['sed', '-e', 's|^--- a/|--- |; s|^+++ b/|+++ |'],), ''), |
| 1170 | ] |
[email protected] | a872e75 | 2015-04-28 23:42:18 | [diff] [blame] | 1171 | |
[email protected] | c2786d9 | 2016-05-31 19:53:50 | [diff] [blame] | 1172 | def _common_patch_successful(self): |
[email protected] | a872e75 | 2015-04-28 23:42:18 | [diff] [blame] | 1173 | self._patch_common() |
| 1174 | self.calls += [ |
| 1175 | ((['git', 'apply', '--index', '-p0', '--3way'],), ''), |
| 1176 | ((['git', 'commit', '-m', |
[email protected] | 5b3bebb | 2015-05-28 21:41:43 | [diff] [blame] | 1177 | 'Description\n\n' + |
[email protected] | a872e75 | 2015-04-28 23:42:18 | [diff] [blame] | 1178 | 'patch from issue 123456 at patchset 60001 ' + |
| 1179 | '(http://crrev.com/123456#ps60001)'],), ''), |
[email protected] | f86c7d3 | 2016-04-01 19:27:30 | [diff] [blame] | 1180 | ((['git', 'config', 'branch.master.rietveldissue', '123456'],), ''), |
[email protected] | aa5ced1 | 2016-03-29 09:41:14 | [diff] [blame] | 1181 | ((['git', 'config', 'branch.master.rietveldserver'],), ''), |
[email protected] | f86c7d3 | 2016-04-01 19:27:30 | [diff] [blame] | 1182 | ((['git', 'config', 'branch.master.rietveldserver', |
| 1183 | 'https://codereview.example.com'],), ''), |
| 1184 | ((['git', 'config', 'branch.master.rietveldpatchset', '60001'],), ''), |
[email protected] | a872e75 | 2015-04-28 23:42:18 | [diff] [blame] | 1185 | ] |
[email protected] | c2786d9 | 2016-05-31 19:53:50 | [diff] [blame] | 1186 | |
| 1187 | def test_patch_successful(self): |
| 1188 | self._common_patch_successful() |
[email protected] | a872e75 | 2015-04-28 23:42:18 | [diff] [blame] | 1189 | self.assertEqual(git_cl.main(['patch', '123456']), 0) |
| 1190 | |
[email protected] | c2786d9 | 2016-05-31 19:53:50 | [diff] [blame] | 1191 | def test_patch_successful_new_branch(self): |
| 1192 | self.calls = [ ((['git', 'new-branch', 'master'],), ''), ] |
| 1193 | self._common_patch_successful() |
| 1194 | self.assertEqual(git_cl.main(['patch', '-b', 'master', '123456']), 0) |
| 1195 | |
[email protected] | a872e75 | 2015-04-28 23:42:18 | [diff] [blame] | 1196 | def test_patch_conflict(self): |
| 1197 | self._patch_common() |
| 1198 | self.calls += [ |
| 1199 | ((['git', 'apply', '--index', '-p0', '--3way'],), '', |
| 1200 | subprocess2.CalledProcessError(1, '', '', '', '')), |
| 1201 | ] |
| 1202 | self.assertNotEqual(git_cl.main(['patch', '123456']), 0) |
[email protected] | 455dc92 | 2015-01-26 20:15:50 | [diff] [blame] | 1203 | |
[email protected] | f86c7d3 | 2016-04-01 19:27:30 | [diff] [blame] | 1204 | def test_gerrit_patch_successful(self): |
| 1205 | self._patch_common(is_gerrit=True) |
| 1206 | self.calls += [ |
| 1207 | ((['git', 'fetch', 'https://chromium.googlesource.com/my/repo', |
| 1208 | 'refs/changes/56/123456/7'],), ''), |
| 1209 | ((['git', 'cherry-pick', 'FETCH_HEAD'],), ''), |
| 1210 | ((['git', 'config', 'branch.master.gerritissue', '123456'],), ''), |
| 1211 | ((['git', 'config', 'branch.master.gerritserver'],), ''), |
| 1212 | ((['git', 'config', 'branch.master.merge'],), 'master'), |
| 1213 | ((['git', 'config', 'branch.master.remote'],), 'origin'), |
| 1214 | ((['git', 'config', 'remote.origin.url'],), |
| 1215 | 'https://chromium.googlesource.com/my/repo'), |
| 1216 | ((['git', 'config', 'branch.master.gerritserver', |
| 1217 | 'https://chromium-review.googlesource.com'],), ''), |
| 1218 | ((['git', 'config', 'branch.master.gerritpatchset', '7'],), ''), |
| 1219 | ] |
| 1220 | self.assertEqual(git_cl.main(['patch', '123456']), 0) |
| 1221 | |
[email protected] | dde6462 | 2016-04-13 17:11:21 | [diff] [blame] | 1222 | def test_patch_force_codereview(self): |
| 1223 | self._patch_common(is_gerrit=True, force_codereview=True) |
| 1224 | self.calls += [ |
| 1225 | ((['git', 'fetch', 'https://chromium.googlesource.com/my/repo', |
| 1226 | 'refs/changes/56/123456/7'],), ''), |
| 1227 | ((['git', 'cherry-pick', 'FETCH_HEAD'],), ''), |
| 1228 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
| 1229 | ((['git', 'config', 'branch.master.gerritissue', '123456'],), ''), |
| 1230 | ((['git', 'config', 'branch.master.gerritserver'],), ''), |
| 1231 | ((['git', 'config', 'branch.master.merge'],), 'master'), |
| 1232 | ((['git', 'config', 'branch.master.remote'],), 'origin'), |
| 1233 | ((['git', 'config', 'remote.origin.url'],), |
| 1234 | 'https://chromium.googlesource.com/my/repo'), |
| 1235 | ((['git', 'config', 'branch.master.gerritserver', |
| 1236 | 'https://chromium-review.googlesource.com'],), ''), |
| 1237 | ((['git', 'config', 'branch.master.gerritpatchset', '7'],), ''), |
| 1238 | ] |
| 1239 | self.assertEqual(git_cl.main(['patch', '--gerrit', '123456']), 0) |
| 1240 | |
[email protected] | f86c7d3 | 2016-04-01 19:27:30 | [diff] [blame] | 1241 | def test_gerrit_patch_url_successful(self): |
| 1242 | self._patch_common(is_gerrit=True) |
| 1243 | self.calls += [ |
| 1244 | ((['git', 'fetch', 'https://chromium.googlesource.com/my/repo', |
| 1245 | 'refs/changes/56/123456/1'],), ''), |
| 1246 | ((['git', 'cherry-pick', 'FETCH_HEAD'],), ''), |
| 1247 | ((['git', 'config', 'branch.master.gerritissue', '123456'],), ''), |
| 1248 | ((['git', 'config', 'branch.master.gerritserver', |
| 1249 | 'https://chromium-review.googlesource.com'],), ''), |
| 1250 | ((['git', 'config', 'branch.master.gerritpatchset', '1'],), ''), |
| 1251 | ] |
| 1252 | self.assertEqual(git_cl.main( |
| 1253 | ['patch', 'https://chromium-review.googlesource.com/#/c/123456/1']), 0) |
| 1254 | |
| 1255 | def test_gerrit_patch_conflict(self): |
| 1256 | self._patch_common(is_gerrit=True) |
| 1257 | self.mock(git_cl, 'DieWithError', |
| 1258 | lambda msg: self._mocked_call(['DieWithError', msg])) |
| 1259 | class SystemExitMock(Exception): |
| 1260 | pass |
| 1261 | self.calls += [ |
| 1262 | ((['git', 'fetch', 'https://chromium.googlesource.com/my/repo', |
| 1263 | 'refs/changes/56/123456/1'],), ''), |
| 1264 | ((['git', 'cherry-pick', 'FETCH_HEAD'],), |
| 1265 | '', subprocess2.CalledProcessError(1, '', '', '', '')), |
| 1266 | ((['DieWithError', 'git cherry-pick FETCH_HEAD" failed.\n'],), |
| 1267 | '', SystemExitMock()), |
| 1268 | ] |
| 1269 | with self.assertRaises(SystemExitMock): |
| 1270 | git_cl.main(['patch', |
| 1271 | 'https://chromium-review.googlesource.com/#/c/123456/1']) |
| 1272 | |
[email protected] | 5df290f | 2016-04-11 16:12:29 | [diff] [blame] | 1273 | def _checkout_calls(self): |
| 1274 | return [ |
| 1275 | ((['git', 'config', '--local', '--get-regexp', |
| 1276 | 'branch\\..*\\.rietveldissue'], ), |
| 1277 | ('branch.retrying.rietveldissue 1111111111\n' |
| 1278 | 'branch.some-fix.rietveldissue 2222222222\n')), |
| 1279 | ((['git', 'config', '--local', '--get-regexp', |
| 1280 | 'branch\\..*\\.gerritissue'], ), |
| 1281 | ('branch.ger-branch.gerritissue 123456\n' |
| 1282 | 'branch.gbranch654.gerritissue 654321\n')), |
| 1283 | ] |
| 1284 | |
| 1285 | def test_checkout_gerrit(self): |
| 1286 | """Tests git cl checkout <issue>.""" |
| 1287 | self.calls = self._checkout_calls() |
| 1288 | self.calls += [((['git', 'checkout', 'ger-branch'], ), '')] |
| 1289 | self.assertEqual(0, git_cl.main(['checkout', '123456'])) |
| 1290 | |
| 1291 | def test_checkout_rietveld(self): |
| 1292 | """Tests git cl checkout <issue>.""" |
| 1293 | self.calls = self._checkout_calls() |
| 1294 | self.calls += [((['git', 'checkout', 'some-fix'], ), '')] |
| 1295 | self.assertEqual(0, git_cl.main(['checkout', '2222222222'])) |
| 1296 | |
| 1297 | def test_checkout_not_found(self): |
| 1298 | """Tests git cl checkout <issue>.""" |
[email protected] | 2825353 | 2016-04-14 13:46:56 | [diff] [blame] | 1299 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
[email protected] | 5df290f | 2016-04-11 16:12:29 | [diff] [blame] | 1300 | self.calls = self._checkout_calls() |
| 1301 | self.assertEqual(1, git_cl.main(['checkout', '99999'])) |
| 1302 | |
[email protected] | 26c8fd2 | 2016-04-11 21:33:21 | [diff] [blame] | 1303 | def test_checkout_no_branch_issues(self): |
| 1304 | """Tests git cl checkout <issue>.""" |
[email protected] | 2825353 | 2016-04-14 13:46:56 | [diff] [blame] | 1305 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
[email protected] | 26c8fd2 | 2016-04-11 21:33:21 | [diff] [blame] | 1306 | self.calls = [ |
| 1307 | ((['git', 'config', '--local', '--get-regexp', |
| 1308 | 'branch\\..*\\.rietveldissue'], ), '', |
| 1309 | subprocess2.CalledProcessError(1, '', '', '', '')), |
| 1310 | ((['git', 'config', '--local', '--get-regexp', |
| 1311 | 'branch\\..*\\.gerritissue'], ), '', |
| 1312 | subprocess2.CalledProcessError(1, '', '', '', '')), |
| 1313 | |
| 1314 | ] |
| 1315 | self.assertEqual(1, git_cl.main(['checkout', '99999'])) |
| 1316 | |
[email protected] | 2825353 | 2016-04-14 13:46:56 | [diff] [blame] | 1317 | def _test_gerrit_ensure_authenticated_common(self, auth, |
| 1318 | skip_auth_check=False): |
[email protected] | fe30f18 | 2016-04-13 12:15:04 | [diff] [blame] | 1319 | self.mock(git_cl.gerrit_util, 'CookiesAuthenticator', |
| 1320 | CookiesAuthenticatorMockFactory(hosts_with_creds=auth)) |
| 1321 | self.mock(git_cl, 'DieWithError', |
| 1322 | lambda msg: self._mocked_call(['DieWithError', msg])) |
| 1323 | self.mock(git_cl, 'ask_for_data', |
| 1324 | lambda msg: self._mocked_call(['ask_for_data', msg])) |
[email protected] | 2825353 | 2016-04-14 13:46:56 | [diff] [blame] | 1325 | self.calls = self._gerrit_ensure_auth_calls(skip_auth_check=skip_auth_check) |
[email protected] | fe30f18 | 2016-04-13 12:15:04 | [diff] [blame] | 1326 | cl = git_cl.Changelist(codereview='gerrit') |
[email protected] | 2825353 | 2016-04-14 13:46:56 | [diff] [blame] | 1327 | cl.branch = 'master' |
| 1328 | cl.branchref = 'refs/heads/master' |
[email protected] | fe30f18 | 2016-04-13 12:15:04 | [diff] [blame] | 1329 | cl.lookedup_issue = True |
| 1330 | return cl |
| 1331 | |
| 1332 | def test_gerrit_ensure_authenticated_missing(self): |
| 1333 | cl = self._test_gerrit_ensure_authenticated_common(auth={ |
| 1334 | 'chromium.googlesource.com': 'git is ok, but gerrit one is missing', |
| 1335 | }) |
| 1336 | self.calls.append( |
| 1337 | ((['DieWithError', |
| 1338 | 'Credentials for the following hosts are required:\n' |
| 1339 | ' chromium-review.googlesource.com\n' |
| 1340 | 'These are read from ~/.gitcookies (or legacy ~/.netrc)\n' |
| 1341 | 'You can (re)generate your credentails by visiting ' |
| 1342 | 'https://chromium-review.googlesource.com/new-password'],), ''),) |
| 1343 | self.assertIsNone(cl.EnsureAuthenticated(force=False)) |
| 1344 | |
| 1345 | def test_gerrit_ensure_authenticated_conflict(self): |
[email protected] | 2825353 | 2016-04-14 13:46:56 | [diff] [blame] | 1346 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
[email protected] | fe30f18 | 2016-04-13 12:15:04 | [diff] [blame] | 1347 | cl = self._test_gerrit_ensure_authenticated_common(auth={ |
| 1348 | 'chromium.googlesource.com': 'one', |
| 1349 | 'chromium-review.googlesource.com': 'other', |
| 1350 | }) |
| 1351 | self.calls.append( |
| 1352 | ((['ask_for_data', 'If you know what you are doing, ' |
| 1353 | 'press Enter to continue, Ctrl+C to abort.'],), '')) |
| 1354 | self.assertIsNone(cl.EnsureAuthenticated(force=False)) |
| 1355 | |
| 1356 | def test_gerrit_ensure_authenticated_ok(self): |
| 1357 | cl = self._test_gerrit_ensure_authenticated_common(auth={ |
| 1358 | 'chromium.googlesource.com': 'same', |
| 1359 | 'chromium-review.googlesource.com': 'same', |
| 1360 | }) |
| 1361 | self.assertIsNone(cl.EnsureAuthenticated(force=False)) |
| 1362 | |
[email protected] | 2825353 | 2016-04-14 13:46:56 | [diff] [blame] | 1363 | def test_gerrit_ensure_authenticated_skipped(self): |
| 1364 | cl = self._test_gerrit_ensure_authenticated_common( |
| 1365 | auth={}, skip_auth_check=True) |
| 1366 | self.assertIsNone(cl.EnsureAuthenticated(force=False)) |
| 1367 | |
[email protected] | fa330e8 | 2016-04-13 17:09:52 | [diff] [blame] | 1368 | def test_cmd_set_commit_rietveld(self): |
| 1369 | self.mock(git_cl._RietveldChangelistImpl, 'SetFlag', |
| 1370 | lambda _, f, v: self._mocked_call(['SetFlag', f, v])) |
| 1371 | self.calls = [ |
| 1372 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
| 1373 | ((['git', 'config', 'branch.feature.rietveldissue'],), '123'), |
| 1374 | ((['git', 'config', 'rietveld.autoupdate'],), ''), |
| 1375 | ((['git', 'config', 'rietveld.server'],), ''), |
| 1376 | ((['git', 'config', 'rietveld.server'],), ''), |
| 1377 | ((['git', 'config', 'branch.feature.rietveldserver'],), |
| 1378 | 'https://codereview.chromium.org'), |
| 1379 | ((['SetFlag', 'commit', '1'], ), ''), |
| 1380 | ] |
| 1381 | self.assertEqual(0, git_cl.main(['set-commit'])) |
| 1382 | |
| 1383 | def test_cmd_set_commit_gerrit(self): |
| 1384 | self.mock(git_cl.gerrit_util, 'SetReview', |
| 1385 | lambda h, i, labels: self._mocked_call( |
| 1386 | ['SetReview', h, i, labels])) |
| 1387 | self.calls = [ |
| 1388 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
| 1389 | ((['git', 'config', 'branch.feature.rietveldissue'],), ''), |
| 1390 | ((['git', 'config', 'branch.feature.gerritissue'],), '123'), |
| 1391 | ((['git', 'config', 'branch.feature.gerritserver'],), |
| 1392 | 'https://chromium-review.googlesource.com'), |
| 1393 | ((['SetReview', 'chromium-review.googlesource.com', 123, |
| 1394 | {'Commit-Queue': 1}],), ''), |
| 1395 | ] |
[email protected] | 1a8ef44 | 2016-04-13 18:41:37 | [diff] [blame] | 1396 | # TODO(tandrii): consider testing just set-commit and set-commit --clear, |
| 1397 | # but without copy-pasting tons of expectations, as modifying them later is |
| 1398 | # super tedious. |
[email protected] | fa330e8 | 2016-04-13 17:09:52 | [diff] [blame] | 1399 | self.assertEqual(0, git_cl.main(['set-commit', '-d'])) |
| 1400 | |
[email protected] | 2b55fe3 | 2016-04-26 20:28:54 | [diff] [blame] | 1401 | def test_description_display(self): |
| 1402 | out = StringIO.StringIO() |
| 1403 | self.mock(git_cl.sys, 'stdout', out) |
| 1404 | |
[email protected] | d6648e2 | 2016-04-29 19:22:16 | [diff] [blame] | 1405 | self.mock(git_cl, 'Changelist', ChangelistMock) |
| 1406 | ChangelistMock.desc = 'foo\n' |
[email protected] | 2b55fe3 | 2016-04-26 20:28:54 | [diff] [blame] | 1407 | |
| 1408 | self.assertEqual(0, git_cl.main(['description', '-d'])) |
| 1409 | self.assertEqual('foo\n', out.getvalue()) |
| 1410 | |
| 1411 | def test_description_rietveld(self): |
| 1412 | out = StringIO.StringIO() |
| 1413 | self.mock(git_cl.sys, 'stdout', out) |
| 1414 | self.mock(git_cl.Changelist, 'GetDescription', |
| 1415 | lambda *args: 'foobar') |
| 1416 | |
| 1417 | self.calls = [ |
| 1418 | ((['git', 'config', 'rietveld.autoupdate'],), ''), |
| 1419 | ((['git', 'config', 'rietveld.server'],), ''), |
| 1420 | ((['git', 'config', 'rietveld.server'],), ''), |
| 1421 | ] |
| 1422 | self.assertEqual(0, git_cl.main([ |
| 1423 | 'description', 'https://code.review.org/123123', '-d', '--rietveld'])) |
| 1424 | self.assertEqual('foobar\n', out.getvalue()) |
| 1425 | |
| 1426 | def test_description_gerrit(self): |
| 1427 | out = StringIO.StringIO() |
| 1428 | self.mock(git_cl.sys, 'stdout', out) |
| 1429 | self.mock(git_cl.Changelist, 'GetDescription', |
| 1430 | lambda *args: 'foobar') |
| 1431 | |
| 1432 | self.assertEqual(0, git_cl.main([ |
| 1433 | 'description', 'https://code.review.org/123123', '-d', '--gerrit'])) |
| 1434 | self.assertEqual('foobar\n', out.getvalue()) |
| 1435 | |
[email protected] | d6648e2 | 2016-04-29 19:22:16 | [diff] [blame] | 1436 | def test_description_set_raw(self): |
| 1437 | out = StringIO.StringIO() |
| 1438 | self.mock(git_cl.sys, 'stdout', out) |
| 1439 | |
| 1440 | self.mock(git_cl, 'Changelist', ChangelistMock) |
| 1441 | self.mock(git_cl.sys, 'stdin', StringIO.StringIO('hihi')) |
| 1442 | |
| 1443 | self.assertEqual(0, git_cl.main(['description', '-n', 'hihi'])) |
| 1444 | self.assertEqual('hihi', ChangelistMock.desc) |
| 1445 | |
[email protected] | d605a51 | 2016-06-03 09:55:00 | [diff] [blame^] | 1446 | def test_description_appends_bug_line(self): |
| 1447 | current_desc = 'Some\n\nChange-Id: xxx' |
| 1448 | |
| 1449 | def RunEditor(desc, _, **kwargs): |
| 1450 | self.assertEquals( |
| 1451 | '# Enter a description of the change.\n' |
| 1452 | '# This will be displayed on the codereview site.\n' |
| 1453 | '# The first line will also be used as the subject of the review.\n' |
| 1454 | '#--------------------This line is 72 characters long' |
| 1455 | '--------------------\n' + |
| 1456 | # TODO(tandrii): fix this http://crbug.com/614587. |
| 1457 | current_desc + '\n\nBUG=', |
| 1458 | desc) |
| 1459 | return current_desc + '\n\nBUG=' |
| 1460 | |
| 1461 | def UpdateDescriptionRemote(_, desc): |
| 1462 | # TODO(tandrii): fix this http://crbug.com/614587. |
| 1463 | self.assertEquals(desc, current_desc + '\n\nBUG=') |
| 1464 | |
| 1465 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
| 1466 | self.mock(git_cl.Changelist, 'GetDescription', |
| 1467 | lambda *args: current_desc) |
| 1468 | self.mock(git_cl._GerritChangelistImpl, 'UpdateDescriptionRemote', |
| 1469 | UpdateDescriptionRemote) |
| 1470 | self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor) |
| 1471 | |
| 1472 | self.calls = [ |
| 1473 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
| 1474 | ((['git', 'config', 'branch.feature.gerritissue'],), '123'), |
| 1475 | ((['git', 'config', 'rietveld.autoupdate'],), ''), |
| 1476 | ((['git', 'config', 'rietveld.bug-prefix'],), ''), |
| 1477 | ((['git', 'config', 'core.editor'],), 'vi'), |
| 1478 | ] |
| 1479 | self.assertEqual(0, git_cl.main(['description', '--gerrit'])) |
| 1480 | |
[email protected] | d6648e2 | 2016-04-29 19:22:16 | [diff] [blame] | 1481 | def test_description_set_stdin(self): |
| 1482 | out = StringIO.StringIO() |
| 1483 | self.mock(git_cl.sys, 'stdout', out) |
| 1484 | |
| 1485 | self.mock(git_cl, 'Changelist', ChangelistMock) |
| 1486 | self.mock(git_cl.sys, 'stdin', StringIO.StringIO('hi \r\n\t there\n\nman')) |
| 1487 | |
| 1488 | self.assertEqual(0, git_cl.main(['description', '-n', '-'])) |
| 1489 | self.assertEqual('hi\n\t there\n\nman', ChangelistMock.desc) |
| 1490 | |
[email protected] | 9b7fd71 | 2016-06-01 13:45:20 | [diff] [blame] | 1491 | def test_cmd_issue_erase_existing(self): |
| 1492 | out = StringIO.StringIO() |
| 1493 | self.mock(git_cl.sys, 'stdout', out) |
| 1494 | self.calls = [ |
| 1495 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
| 1496 | ((['git', 'config', 'branch.feature.rietveldissue'],), ''), |
| 1497 | ((['git', 'config', 'branch.feature.gerritissue'],), '123'), |
| 1498 | ((['git', 'config', '--unset', 'branch.feature.gerritissue'],), ''), |
| 1499 | ((['git', 'config', '--unset', 'branch.feature.gerritpatchset'],), ''), |
| 1500 | # Let this command raise exception (retcode=1) - it should be ignored. |
| 1501 | ((['git', 'config', '--unset', 'branch.feature.last-upload-hash'],), |
| 1502 | '', subprocess2.CalledProcessError(1, '', '', '', '')), |
| 1503 | ((['git', 'config', '--unset', 'branch.feature.gerritserver'],), ''), |
| 1504 | ((['git', 'config', '--unset', 'branch.feature.gerritsquashhash'],), |
| 1505 | ''), |
| 1506 | ] |
| 1507 | self.assertEqual(0, git_cl.main(['issue', '0'])) |
| 1508 | |
[email protected] | f86c7d3 | 2016-04-01 19:27:30 | [diff] [blame] | 1509 | |
[email protected] | ddd5941 | 2011-11-30 14:20:38 | [diff] [blame] | 1510 | if __name__ == '__main__': |
[email protected] | 78936cb | 2013-04-11 00:17:52 | [diff] [blame] | 1511 | git_cl.logging.basicConfig( |
| 1512 | level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) |
[email protected] | ddd5941 | 2011-11-30 14:20:38 | [diff] [blame] | 1513 | unittest.main() |