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