maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
maruel@chromium.org | eb5edbc | 2012-01-16 17:03:28 +0000 | [diff] [blame] | 2 | # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [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 | |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 8 | import contextlib |
tandrii | de281ae | 2016-10-12 06:02:30 -0700 | [diff] [blame] | 9 | import json |
Andrii Shyshkalov | 18df0cd | 2017-01-25 15:22:20 +0100 | [diff] [blame] | 10 | import logging |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 11 | import os |
maruel@chromium.org | a335365 | 2011-11-30 14:26:57 +0000 | [diff] [blame] | 12 | import StringIO |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 13 | import sys |
| 14 | import unittest |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 15 | import urlparse |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 16 | |
| 17 | sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 18 | |
| 19 | from testing_support.auto_stub import TestCase |
| 20 | |
| 21 | import git_cl |
iannucci@chromium.org | 9e84927 | 2014-04-04 00:31:55 +0000 | [diff] [blame] | 22 | import git_common |
tandrii@chromium.org | 57d8654 | 2016-03-04 16:11:32 +0000 | [diff] [blame] | 23 | import git_footers |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 24 | import subprocess2 |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 25 | |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 26 | def callError(code=1, cmd='', cwd='', stdout='', stderr=''): |
| 27 | return subprocess2.CalledProcessError(code, cmd, cwd, stdout, stderr) |
| 28 | |
| 29 | |
| 30 | CERR1 = callError(1) |
| 31 | |
| 32 | |
martiniss@chromium.org | d6648e2 | 2016-04-29 19:22:16 +0000 | [diff] [blame] | 33 | class ChangelistMock(object): |
| 34 | # A class variable so we can access it when we don't have access to the |
| 35 | # instance that's being set. |
| 36 | desc = "" |
| 37 | def __init__(self, **kwargs): |
| 38 | pass |
| 39 | def GetIssue(self): |
| 40 | return 1 |
Kenneth Russell | 61e2ed4 | 2017-02-15 11:47:13 -0800 | [diff] [blame] | 41 | def GetDescription(self, force=False): |
martiniss@chromium.org | d6648e2 | 2016-04-29 19:22:16 +0000 | [diff] [blame] | 42 | return ChangelistMock.desc |
dsansome | e2d6fd9 | 2016-09-08 00:10:47 -0700 | [diff] [blame] | 43 | def UpdateDescription(self, desc, force=False): |
martiniss@chromium.org | d6648e2 | 2016-04-29 19:22:16 +0000 | [diff] [blame] | 44 | ChangelistMock.desc = desc |
| 45 | |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 46 | |
maruel@chromium.org | 2e72bb1 | 2012-01-17 15:18:35 +0000 | [diff] [blame] | 47 | class PresubmitMock(object): |
| 48 | def __init__(self, *args, **kwargs): |
| 49 | self.reviewers = [] |
| 50 | @staticmethod |
| 51 | def should_continue(): |
| 52 | return True |
| 53 | |
| 54 | |
| 55 | class RietveldMock(object): |
| 56 | def __init__(self, *args, **kwargs): |
| 57 | pass |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 58 | |
maruel@chromium.org | 2e72bb1 | 2012-01-17 15:18:35 +0000 | [diff] [blame] | 59 | @staticmethod |
Kenneth Russell | 61e2ed4 | 2017-02-15 11:47:13 -0800 | [diff] [blame] | 60 | def get_description(issue, force=False): |
maruel@chromium.org | 2e72bb1 | 2012-01-17 15:18:35 +0000 | [diff] [blame] | 61 | return 'Issue: %d' % issue |
| 62 | |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 63 | @staticmethod |
| 64 | def get_issue_properties(_issue, _messages): |
| 65 | return { |
| 66 | 'reviewers': ['joe@chromium.org', 'john@chromium.org'], |
| 67 | 'messages': [ |
| 68 | { |
| 69 | 'approval': True, |
| 70 | 'sender': 'john@chromium.org', |
| 71 | }, |
| 72 | ], |
Andrii Shyshkalov | 06a2502 | 2016-11-24 16:47:00 +0100 | [diff] [blame] | 73 | 'patchsets': [1, 20001], |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 74 | } |
| 75 | |
iannucci | e53c935 | 2016-08-17 14:40:40 -0700 | [diff] [blame] | 76 | @staticmethod |
| 77 | def close_issue(_issue): |
| 78 | return 'Closed' |
| 79 | |
skobes | 6468b90 | 2016-10-24 08:45:10 -0700 | [diff] [blame] | 80 | @staticmethod |
| 81 | def get_patch(issue, patchset): |
| 82 | return 'patch set from issue %s patchset %s' % (issue, patchset) |
| 83 | |
Andrii Shyshkalov | 06a2502 | 2016-11-24 16:47:00 +0100 | [diff] [blame] | 84 | @staticmethod |
| 85 | def update_description(_issue, _description): |
| 86 | return 'Updated' |
| 87 | |
| 88 | @staticmethod |
| 89 | def add_comment(_issue, _comment): |
| 90 | return 'Commented' |
| 91 | |
| 92 | |
| 93 | |
skobes | 6468b90 | 2016-10-24 08:45:10 -0700 | [diff] [blame] | 94 | |
| 95 | class GitCheckoutMock(object): |
| 96 | def __init__(self, *args, **kwargs): |
| 97 | pass |
| 98 | |
| 99 | @staticmethod |
| 100 | def reset(): |
| 101 | GitCheckoutMock.conflict = False |
| 102 | |
| 103 | def apply_patch(self, p): |
| 104 | if GitCheckoutMock.conflict: |
| 105 | raise Exception('failed') |
| 106 | |
maruel@chromium.org | 2e72bb1 | 2012-01-17 15:18:35 +0000 | [diff] [blame] | 107 | |
| 108 | class WatchlistsMock(object): |
| 109 | def __init__(self, _): |
| 110 | pass |
| 111 | @staticmethod |
| 112 | def GetWatchersForPaths(_): |
| 113 | return ['joe@example.com'] |
| 114 | |
| 115 | |
ukai@chromium.org | 78c4b98 | 2012-02-14 02:20:26 +0000 | [diff] [blame] | 116 | class CodereviewSettingsFileMock(object): |
| 117 | def __init__(self): |
| 118 | pass |
Quinten Yearsley | b2cc4a9 | 2016-12-15 13:53:26 -0800 | [diff] [blame] | 119 | # pylint: disable=no-self-use |
ukai@chromium.org | 78c4b98 | 2012-02-14 02:20:26 +0000 | [diff] [blame] | 120 | def read(self): |
| 121 | return ("CODE_REVIEW_SERVER: gerrit.chromium.org\n" + |
andybons@chromium.org | 11f46eb | 2016-02-02 19:26:51 +0000 | [diff] [blame] | 122 | "GERRIT_HOST: True\n") |
ukai@chromium.org | 78c4b98 | 2012-02-14 02:20:26 +0000 | [diff] [blame] | 123 | |
| 124 | |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 125 | class AuthenticatorMock(object): |
| 126 | def __init__(self, *_args): |
| 127 | pass |
| 128 | def has_cached_credentials(self): |
| 129 | return True |
tandrii | 221ab25 | 2016-10-06 08:12:04 -0700 | [diff] [blame] | 130 | def authorize(self, http): |
| 131 | return http |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 132 | |
| 133 | |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 134 | def CookiesAuthenticatorMockFactory(hosts_with_creds=None, same_cookie=False): |
| 135 | """Use to mock Gerrit/Git credentials from ~/.netrc or ~/.gitcookies. |
| 136 | |
| 137 | Usage: |
| 138 | >>> self.mock(git_cl.gerrit_util, "CookiesAuthenticator", |
| 139 | CookiesAuthenticatorMockFactory({'host1': 'cookie1'})) |
| 140 | |
| 141 | OR |
| 142 | >>> self.mock(git_cl.gerrit_util, "CookiesAuthenticator", |
| 143 | CookiesAuthenticatorMockFactory(cookie='cookie')) |
| 144 | """ |
| 145 | class CookiesAuthenticatorMock(git_cl.gerrit_util.CookiesAuthenticator): |
Quinten Yearsley | b2cc4a9 | 2016-12-15 13:53:26 -0800 | [diff] [blame] | 146 | def __init__(self): # pylint: disable=super-init-not-called |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 147 | # Intentionally not calling super() because it reads actual cookie files. |
| 148 | pass |
| 149 | @classmethod |
| 150 | def get_gitcookies_path(cls): |
| 151 | return '~/.gitcookies' |
| 152 | @classmethod |
| 153 | def get_netrc_path(cls): |
| 154 | return '~/.netrc' |
| 155 | def get_auth_header(self, host): |
| 156 | if same_cookie: |
| 157 | return same_cookie |
| 158 | return (hosts_with_creds or {}).get(host) |
| 159 | return CookiesAuthenticatorMock |
| 160 | |
kmarshall | 9249e01 | 2016-08-23 12:02:16 -0700 | [diff] [blame] | 161 | class MockChangelistWithBranchAndIssue(): |
| 162 | def __init__(self, branch, issue): |
| 163 | self.branch = branch |
| 164 | self.issue = issue |
| 165 | def GetBranch(self): |
| 166 | return self.branch |
| 167 | def GetIssue(self): |
| 168 | return self.issue |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 169 | |
tandrii | c2405f5 | 2016-10-10 08:13:15 -0700 | [diff] [blame] | 170 | |
| 171 | class SystemExitMock(Exception): |
| 172 | pass |
| 173 | |
| 174 | |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 175 | class TestGitClBasic(unittest.TestCase): |
Andrii Shyshkalov | 3186301 | 2017-02-08 11:35:12 +0100 | [diff] [blame] | 176 | def test_get_description(self): |
| 177 | cl = git_cl.Changelist(issue=1, codereview='rietveld', |
| 178 | codereview_host='host') |
| 179 | cl.description = 'x' |
| 180 | cl.has_description = True |
Kenneth Russell | 61e2ed4 | 2017-02-15 11:47:13 -0800 | [diff] [blame] | 181 | cl._codereview_impl.FetchDescription = lambda *a, **kw: 'y' |
Andrii Shyshkalov | 3186301 | 2017-02-08 11:35:12 +0100 | [diff] [blame] | 182 | self.assertEquals(cl.GetDescription(), 'x') |
| 183 | self.assertEquals(cl.GetDescription(force=True), 'y') |
| 184 | self.assertEquals(cl.GetDescription(), 'y') |
| 185 | |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 186 | def _test_ParseIssueUrl(self, func, url, issue, patchset, hostname, fail): |
| 187 | parsed = urlparse.urlparse(url) |
| 188 | result = func(parsed) |
| 189 | if fail: |
| 190 | self.assertIsNone(result) |
| 191 | return None |
| 192 | self.assertIsNotNone(result) |
| 193 | self.assertEqual(result.issue, issue) |
| 194 | self.assertEqual(result.patchset, patchset) |
| 195 | self.assertEqual(result.hostname, hostname) |
| 196 | return result |
| 197 | |
| 198 | def test_ParseIssueURL_rietveld(self): |
skobes | 6468b90 | 2016-10-24 08:45:10 -0700 | [diff] [blame] | 199 | def test(url, issue=None, patchset=None, hostname=None, fail=None): |
| 200 | self._test_ParseIssueUrl( |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 201 | git_cl._RietveldChangelistImpl.ParseIssueURL, |
| 202 | url, issue, patchset, hostname, fail) |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 203 | |
| 204 | test('http://codereview.chromium.org/123', |
| 205 | 123, None, 'codereview.chromium.org') |
| 206 | test('https://codereview.chromium.org/123', |
| 207 | 123, None, 'codereview.chromium.org') |
| 208 | test('https://codereview.chromium.org/123/', |
| 209 | 123, None, 'codereview.chromium.org') |
| 210 | test('https://codereview.chromium.org/123/whatever', |
| 211 | 123, None, 'codereview.chromium.org') |
wychen | 3c1c172 | 2016-08-04 11:46:36 -0700 | [diff] [blame] | 212 | test('https://codereview.chromium.org/123/#ps20001', |
| 213 | 123, 20001, 'codereview.chromium.org') |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 214 | test('http://codereview.chromium.org/download/issue123_4.diff', |
skobes | 6468b90 | 2016-10-24 08:45:10 -0700 | [diff] [blame] | 215 | 123, 4, 'codereview.chromium.org') |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 216 | # This looks like bad Gerrit, but is actually valid Rietveld. |
| 217 | test('https://chrome-review.source.com/123/4/', |
| 218 | 123, None, 'chrome-review.source.com') |
| 219 | |
| 220 | test('https://codereview.chromium.org/deadbeaf', fail=True) |
| 221 | test('https://codereview.chromium.org/api/123', fail=True) |
| 222 | test('bad://codereview.chromium.org/123', fail=True) |
| 223 | test('http://codereview.chromium.org/download/issue123_4.diffff', fail=True) |
| 224 | |
| 225 | def test_ParseIssueURL_gerrit(self): |
| 226 | def test(url, issue=None, patchset=None, hostname=None, fail=None): |
| 227 | self._test_ParseIssueUrl( |
| 228 | git_cl._GerritChangelistImpl.ParseIssueURL, |
| 229 | url, issue, patchset, hostname, fail) |
| 230 | |
| 231 | test('http://chrome-review.source.com/c/123', |
| 232 | 123, None, 'chrome-review.source.com') |
| 233 | test('https://chrome-review.source.com/c/123/', |
| 234 | 123, None, 'chrome-review.source.com') |
| 235 | test('https://chrome-review.source.com/c/123/4', |
| 236 | 123, 4, 'chrome-review.source.com') |
| 237 | test('https://chrome-review.source.com/#/c/123/4', |
| 238 | 123, 4, 'chrome-review.source.com') |
| 239 | test('https://chrome-review.source.com/c/123/4', |
| 240 | 123, 4, 'chrome-review.source.com') |
| 241 | test('https://chrome-review.source.com/123', |
| 242 | 123, None, 'chrome-review.source.com') |
| 243 | test('https://chrome-review.source.com/123/4', |
| 244 | 123, 4, 'chrome-review.source.com') |
| 245 | |
| 246 | test('https://chrome-review.source.com/c/123/1/whatisthis', fail=True) |
| 247 | test('https://chrome-review.source.com/c/abc/', fail=True) |
| 248 | test('ssh://chrome-review.source.com/c/123/1/', fail=True) |
| 249 | |
| 250 | def test_ParseIssueNumberArgument(self): |
| 251 | def test(arg, issue=None, patchset=None, hostname=None, fail=False): |
| 252 | result = git_cl.ParseIssueNumberArgument(arg) |
| 253 | self.assertIsNotNone(result) |
| 254 | if fail: |
| 255 | self.assertFalse(result.valid) |
| 256 | else: |
| 257 | self.assertEqual(result.issue, issue) |
| 258 | self.assertEqual(result.patchset, patchset) |
| 259 | self.assertEqual(result.hostname, hostname) |
| 260 | |
| 261 | test('123', 123) |
| 262 | test('', fail=True) |
| 263 | test('abc', fail=True) |
| 264 | test('123/1', fail=True) |
| 265 | test('123a', fail=True) |
| 266 | test('ssh://chrome-review.source.com/#/c/123/4/', fail=True) |
| 267 | # Rietveld. |
| 268 | test('https://codereview.source.com/123', |
| 269 | 123, None, 'codereview.source.com') |
| 270 | test('https://codereview.source.com/www123', fail=True) |
| 271 | # Gerrrit. |
| 272 | test('https://chrome-review.source.com/c/123/4', |
| 273 | 123, 4, 'chrome-review.source.com') |
| 274 | test('https://chrome-review.source.com/bad/123/4', fail=True) |
| 275 | |
tandrii | f9aefb7 | 2016-07-01 09:06:51 -0700 | [diff] [blame] | 276 | def test_get_bug_line_values(self): |
| 277 | f = lambda p, bugs: list(git_cl._get_bug_line_values(p, bugs)) |
| 278 | self.assertEqual(f('', ''), []) |
| 279 | self.assertEqual(f('', '123,v8:456'), ['123', 'v8:456']) |
| 280 | self.assertEqual(f('v8', '456'), ['v8:456']) |
| 281 | self.assertEqual(f('v8', 'chromium:123,456'), ['v8:456', 'chromium:123']) |
| 282 | # Not nice, but not worth carying. |
| 283 | self.assertEqual(f('v8', 'chromium:123,456,v8:123'), |
| 284 | ['v8:456', 'chromium:123', 'v8:123']) |
| 285 | |
Andrii Shyshkalov | 15e50cc | 2016-12-02 14:34:08 +0100 | [diff] [blame] | 286 | def _test_git_number(self, parent_msg, dest_ref, child_msg, |
| 287 | parent_hash='parenthash'): |
| 288 | desc = git_cl.ChangeDescription(child_msg) |
| 289 | desc.update_with_git_number_footers(parent_hash, parent_msg, dest_ref) |
| 290 | return desc.description |
| 291 | |
| 292 | def assertEqualByLine(self, actual, expected): |
| 293 | self.assertEqual(actual.splitlines(), expected.splitlines()) |
| 294 | |
| 295 | def test_git_number_bad_parent(self): |
| 296 | with self.assertRaises(ValueError): |
| 297 | self._test_git_number('Parent', 'refs/heads/master', 'Child') |
| 298 | |
| 299 | def test_git_number_bad_parent_footer(self): |
| 300 | with self.assertRaises(AssertionError): |
| 301 | self._test_git_number( |
| 302 | 'Parent\n' |
| 303 | '\n' |
| 304 | 'Cr-Commit-Position: wrong', |
| 305 | 'refs/heads/master', 'Child') |
| 306 | |
| 307 | def test_git_number_bad_lineage_ignored(self): |
| 308 | actual = self._test_git_number( |
| 309 | 'Parent\n' |
| 310 | '\n' |
| 311 | 'Cr-Commit-Position: refs/heads/master@{#1}\n' |
| 312 | 'Cr-Branched-From: mustBeReal40CharHash-branch@{#pos}', |
| 313 | 'refs/heads/master', 'Child') |
| 314 | self.assertEqualByLine( |
| 315 | actual, |
| 316 | 'Child\n' |
| 317 | '\n' |
| 318 | 'Cr-Commit-Position: refs/heads/master@{#2}\n' |
| 319 | 'Cr-Branched-From: mustBeReal40CharHash-branch@{#pos}') |
| 320 | |
| 321 | def test_git_number_same_branch(self): |
| 322 | actual = self._test_git_number( |
| 323 | 'Parent\n' |
| 324 | '\n' |
| 325 | 'Cr-Commit-Position: refs/heads/master@{#12}', |
| 326 | dest_ref='refs/heads/master', |
| 327 | child_msg='Child') |
| 328 | self.assertEqualByLine( |
| 329 | actual, |
| 330 | 'Child\n' |
| 331 | '\n' |
| 332 | 'Cr-Commit-Position: refs/heads/master@{#13}') |
| 333 | |
| 334 | def test_git_number_same_branch_with_originals(self): |
| 335 | actual = self._test_git_number( |
| 336 | 'Parent\n' |
| 337 | '\n' |
| 338 | 'Cr-Commit-Position: refs/heads/master@{#12}', |
| 339 | dest_ref='refs/heads/master', |
| 340 | child_msg='Child\n' |
| 341 | '\n' |
| 342 | 'Some users are smart and insert their own footers\n' |
| 343 | '\n' |
| 344 | 'Cr-Whatever: value\n' |
| 345 | 'Cr-Commit-Position: refs/copy/paste@{#22}') |
| 346 | self.assertEqualByLine( |
| 347 | actual, |
| 348 | 'Child\n' |
| 349 | '\n' |
| 350 | 'Some users are smart and insert their own footers\n' |
| 351 | '\n' |
| 352 | 'Cr-Original-Whatever: value\n' |
| 353 | 'Cr-Original-Commit-Position: refs/copy/paste@{#22}\n' |
| 354 | 'Cr-Commit-Position: refs/heads/master@{#13}') |
| 355 | |
| 356 | def test_git_number_new_branch(self): |
| 357 | actual = self._test_git_number( |
| 358 | 'Parent\n' |
| 359 | '\n' |
| 360 | 'Cr-Commit-Position: refs/heads/master@{#12}', |
| 361 | dest_ref='refs/heads/branch', |
| 362 | child_msg='Child') |
| 363 | self.assertEqualByLine( |
| 364 | actual, |
| 365 | 'Child\n' |
| 366 | '\n' |
| 367 | 'Cr-Commit-Position: refs/heads/branch@{#1}\n' |
| 368 | 'Cr-Branched-From: parenthash-refs/heads/master@{#12}') |
| 369 | |
| 370 | def test_git_number_lineage(self): |
| 371 | actual = self._test_git_number( |
| 372 | 'Parent\n' |
| 373 | '\n' |
| 374 | 'Cr-Commit-Position: refs/heads/branch@{#1}\n' |
| 375 | 'Cr-Branched-From: somehash-refs/heads/master@{#12}', |
| 376 | dest_ref='refs/heads/branch', |
| 377 | child_msg='Child') |
| 378 | self.assertEqualByLine( |
| 379 | actual, |
| 380 | 'Child\n' |
| 381 | '\n' |
| 382 | 'Cr-Commit-Position: refs/heads/branch@{#2}\n' |
| 383 | 'Cr-Branched-From: somehash-refs/heads/master@{#12}') |
| 384 | |
| 385 | def test_git_number_moooooooore_lineage(self): |
| 386 | actual = self._test_git_number( |
| 387 | 'Parent\n' |
| 388 | '\n' |
| 389 | 'Cr-Commit-Position: refs/heads/branch@{#5}\n' |
| 390 | 'Cr-Branched-From: somehash-refs/heads/master@{#12}', |
| 391 | dest_ref='refs/heads/mooore', |
| 392 | child_msg='Child') |
| 393 | self.assertEqualByLine( |
| 394 | actual, |
| 395 | 'Child\n' |
| 396 | '\n' |
| 397 | 'Cr-Commit-Position: refs/heads/mooore@{#1}\n' |
| 398 | 'Cr-Branched-From: parenthash-refs/heads/branch@{#5}\n' |
| 399 | 'Cr-Branched-From: somehash-refs/heads/master@{#12}') |
| 400 | |
Andrii Shyshkalov | b5effa1 | 2016-12-14 19:35:12 +0100 | [diff] [blame] | 401 | def test_git_number_ever_moooooooore_lineage(self): |
| 402 | self.maxDiff = 10000 |
| 403 | actual = self._test_git_number( |
| 404 | 'CQ commit on fresh new branch + numbering.\n' |
| 405 | '\n' |
| 406 | 'NOTRY=True\n' |
| 407 | 'NOPRESUBMIT=True\n' |
| 408 | 'BUG=\n' |
| 409 | '\n' |
| 410 | 'Review-Url: https://codereview.chromium.org/2577703003\n' |
| 411 | 'Cr-Commit-Position: refs/heads/gnumb-test/br@{#1}\n' |
| 412 | 'Cr-Branched-From: 0749ff9edc-refs/heads/gnumb-test/cq@{#4}\n' |
| 413 | 'Cr-Branched-From: 5c49df2da6-refs/heads/master@{#41618}', |
| 414 | dest_ref='refs/heads/gnumb-test/cl', |
| 415 | child_msg='git cl on fresh new branch + numbering.\n' |
| 416 | '\n' |
| 417 | 'Review-Url: https://codereview.chromium.org/2575043003 .\n') |
| 418 | self.assertEqualByLine( |
| 419 | actual, |
| 420 | 'git cl on fresh new branch + numbering.\n' |
| 421 | '\n' |
| 422 | 'Review-Url: https://codereview.chromium.org/2575043003 .\n' |
| 423 | 'Cr-Commit-Position: refs/heads/gnumb-test/cl@{#1}\n' |
| 424 | 'Cr-Branched-From: parenthash-refs/heads/gnumb-test/br@{#1}\n' |
| 425 | 'Cr-Branched-From: 0749ff9edc-refs/heads/gnumb-test/cq@{#4}\n' |
| 426 | 'Cr-Branched-From: 5c49df2da6-refs/heads/master@{#41618}') |
Andrii Shyshkalov | 15e50cc | 2016-12-02 14:34:08 +0100 | [diff] [blame] | 427 | |
| 428 | def test_git_number_cherry_pick(self): |
| 429 | actual = self._test_git_number( |
| 430 | 'Parent\n' |
| 431 | '\n' |
| 432 | 'Cr-Commit-Position: refs/heads/branch@{#1}\n' |
| 433 | 'Cr-Branched-From: somehash-refs/heads/master@{#12}', |
| 434 | dest_ref='refs/heads/branch', |
| 435 | child_msg='Child, which is cherry-pick from master\n' |
| 436 | '\n' |
| 437 | 'Cr-Commit-Position: refs/heads/master@{#100}\n' |
| 438 | '(cherry picked from commit deadbeef12345678deadbeef12345678deadbeef)') |
| 439 | self.assertEqualByLine( |
| 440 | actual, |
| 441 | 'Child, which is cherry-pick from master\n' |
| 442 | '\n' |
| 443 | '(cherry picked from commit deadbeef12345678deadbeef12345678deadbeef)\n' |
| 444 | '\n' |
| 445 | 'Cr-Original-Commit-Position: refs/heads/master@{#100}\n' |
| 446 | 'Cr-Commit-Position: refs/heads/branch@{#2}\n' |
| 447 | 'Cr-Branched-From: somehash-refs/heads/master@{#12}') |
| 448 | |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 449 | |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 450 | class TestGitCl(TestCase): |
| 451 | def setUp(self): |
| 452 | super(TestGitCl, self).setUp() |
| 453 | self.calls = [] |
tandrii | 9d20675 | 2016-06-20 11:32:47 -0700 | [diff] [blame] | 454 | self._calls_done = [] |
maruel@chromium.org | 2e72bb1 | 2012-01-17 15:18:35 +0000 | [diff] [blame] | 455 | self.mock(subprocess2, 'call', self._mocked_call) |
| 456 | self.mock(subprocess2, 'check_call', self._mocked_call) |
| 457 | self.mock(subprocess2, 'check_output', self._mocked_call) |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 458 | self.mock(subprocess2, 'communicate', |
| 459 | lambda *a, **kw: ([self._mocked_call(*a, **kw), ''], 0)) |
tandrii@chromium.org | a342c92 | 2016-03-16 07:08:25 +0000 | [diff] [blame] | 460 | self.mock(git_cl.gclient_utils, 'CheckCallAndFilter', self._mocked_call) |
sbc@chromium.org | 71437c0 | 2015-04-09 19:29:40 +0000 | [diff] [blame] | 461 | self.mock(git_common, 'is_dirty_git_tree', lambda x: False) |
iannucci@chromium.org | 9e84927 | 2014-04-04 00:31:55 +0000 | [diff] [blame] | 462 | self.mock(git_common, 'get_or_create_merge_base', |
| 463 | lambda *a: ( |
| 464 | self._mocked_call(['get_or_create_merge_base']+list(a)))) |
pgervais@chromium.org | 8ba38ff | 2015-06-11 21:41:25 +0000 | [diff] [blame] | 465 | self.mock(git_cl, 'BranchExists', lambda _: True) |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 466 | self.mock(git_cl, 'FindCodereviewSettingsFile', lambda: '') |
Andrii Shyshkalov | abc26ac | 2017-03-14 14:49:38 +0100 | [diff] [blame] | 467 | self.mock(git_cl, 'ask_for_data', lambda *a, **k: self._mocked_call( |
| 468 | *(['ask_for_data'] + list(a)), **k)) |
phajdan.jr | e328cf9 | 2016-08-22 04:12:17 -0700 | [diff] [blame] | 469 | self.mock(git_cl, 'write_json', lambda path, contents: |
| 470 | self._mocked_call('write_json', path, contents)) |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 471 | self.mock(git_cl.presubmit_support, 'DoPresubmitChecks', PresubmitMock) |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 472 | self.mock(git_cl.rietveld, 'Rietveld', RietveldMock) |
maruel@chromium.org | 4bac4b5 | 2012-11-27 20:33:52 +0000 | [diff] [blame] | 473 | self.mock(git_cl.rietveld, 'CachingRietveld', RietveldMock) |
skobes | 6468b90 | 2016-10-24 08:45:10 -0700 | [diff] [blame] | 474 | self.mock(git_cl.checkout, 'GitCheckout', GitCheckoutMock) |
| 475 | GitCheckoutMock.reset() |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 476 | self.mock(git_cl.upload, 'RealMain', self.fail) |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 477 | self.mock(git_cl.watchlists, 'Watchlists', WatchlistsMock) |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 478 | self.mock(git_cl.auth, 'get_authenticator_for_host', AuthenticatorMock) |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 479 | self.mock(git_cl.gerrit_util, 'GetChangeDetail', |
| 480 | lambda *args, **kwargs: self._mocked_call( |
| 481 | 'GetChangeDetail', *args, **kwargs)) |
| 482 | self.mock(git_cl.gerrit_util, 'AddReviewers', |
| 483 | lambda h, i, add, is_reviewer, notify: self._mocked_call( |
| 484 | 'AddReviewers', h, i, add, is_reviewer, notify)) |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 485 | self.mock(git_cl.gerrit_util.GceAuthenticator, 'is_gce', |
| 486 | classmethod(lambda _: False)) |
tandrii | c2405f5 | 2016-10-10 08:13:15 -0700 | [diff] [blame] | 487 | self.mock(git_cl, 'DieWithError', |
Christopher Lam | f732cd5 | 2017-01-24 12:40:11 +1100 | [diff] [blame] | 488 | lambda msg, change=None: self._mocked_call(['DieWithError', msg])) |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 489 | # It's important to reset settings to not have inter-tests interference. |
| 490 | git_cl.settings = None |
| 491 | |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 492 | |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 493 | def tearDown(self): |
wychen@chromium.org | 445c896 | 2015-04-28 23:30:05 +0000 | [diff] [blame] | 494 | try: |
Andrii Shyshkalov | 18df0cd | 2017-01-25 15:22:20 +0100 | [diff] [blame] | 495 | self.assertEquals([], self.calls) |
| 496 | except AssertionError: |
wychen@chromium.org | 445c896 | 2015-04-28 23:30:05 +0000 | [diff] [blame] | 497 | if not self.has_failed(): |
Andrii Shyshkalov | 18df0cd | 2017-01-25 15:22:20 +0100 | [diff] [blame] | 498 | raise |
| 499 | # Sadly, has_failed() returns True if this OR any other tests before this |
| 500 | # one have failed. |
| 501 | git_cl.logging.exception( |
| 502 | '\nIF YOU SEE THIS, READ BELOW, IT WILL SAVE YOUR TIME!\n' |
| 503 | 'There are un-consumed self.calls after this test has finished.\n' |
| 504 | 'If you don\'t know which test this is, run:\n' |
Mark Mentovai | 57c4721 | 2017-03-09 11:14:09 -0500 | [diff] [blame] | 505 | ' tests/git_cl_test.py -v\n' |
Andrii Shyshkalov | 18df0cd | 2017-01-25 15:22:20 +0100 | [diff] [blame] | 506 | '\n' |
| 507 | 'If you are already running just this single test, then **first** ' |
| 508 | 'fix the problem whose exception is emitted below by unittest ' |
| 509 | 'runner.\n' |
| 510 | '\n' |
| 511 | 'Else, to be sure what\'s going on, run this test **alone** with \n' |
Mark Mentovai | 57c4721 | 2017-03-09 11:14:09 -0500 | [diff] [blame] | 512 | ' tests/git_cl_test.py TestGitCl.<name>\n' |
Andrii Shyshkalov | 18df0cd | 2017-01-25 15:22:20 +0100 | [diff] [blame] | 513 | 'and follow instructions above.\n' + |
| 514 | '=' * 80) |
wychen@chromium.org | 445c896 | 2015-04-28 23:30:05 +0000 | [diff] [blame] | 515 | finally: |
| 516 | super(TestGitCl, self).tearDown() |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 517 | |
iannucci@chromium.org | 9e84927 | 2014-04-04 00:31:55 +0000 | [diff] [blame] | 518 | def _mocked_call(self, *args, **_kwargs): |
maruel@chromium.org | 2e72bb1 | 2012-01-17 15:18:35 +0000 | [diff] [blame] | 519 | self.assertTrue( |
| 520 | self.calls, |
tandrii | 9d20675 | 2016-06-20 11:32:47 -0700 | [diff] [blame] | 521 | '@%d Expected: <Missing> Actual: %r' % (len(self._calls_done), args)) |
wychen@chromium.org | a872e75 | 2015-04-28 23:42:18 +0000 | [diff] [blame] | 522 | top = self.calls.pop(0) |
wychen@chromium.org | a872e75 | 2015-04-28 23:42:18 +0000 | [diff] [blame] | 523 | expected_args, result = top |
| 524 | |
maruel@chromium.org | e52678e | 2013-04-26 18:34:44 +0000 | [diff] [blame] | 525 | # Also logs otherwise it could get caught in a try/finally and be hard to |
| 526 | # diagnose. |
| 527 | if expected_args != args: |
tandrii | 9d20675 | 2016-06-20 11:32:47 -0700 | [diff] [blame] | 528 | N = 5 |
| 529 | prior_calls = '\n '.join( |
| 530 | '@%d: %r' % (len(self._calls_done) - N + i, c[0]) |
| 531 | for i, c in enumerate(self._calls_done[-N:])) |
| 532 | following_calls = '\n '.join( |
| 533 | '@%d: %r' % (len(self._calls_done) + i + 1, c[0]) |
| 534 | for i, c in enumerate(self.calls[:N])) |
| 535 | extended_msg = ( |
| 536 | 'A few prior calls:\n %s\n\n' |
| 537 | 'This (expected):\n @%d: %r\n' |
| 538 | 'This (actual):\n @%d: %r\n\n' |
| 539 | 'A few following expected calls:\n %s' % |
| 540 | (prior_calls, len(self._calls_done), expected_args, |
| 541 | len(self._calls_done), args, following_calls)) |
| 542 | git_cl.logging.error(extended_msg) |
| 543 | |
tandrii | 99a72f2 | 2016-08-17 14:33:24 -0700 | [diff] [blame] | 544 | self.fail('@%d\n' |
| 545 | ' Expected: %r\n' |
| 546 | ' Actual: %r' % ( |
tandrii | 9d20675 | 2016-06-20 11:32:47 -0700 | [diff] [blame] | 547 | len(self._calls_done), expected_args, args)) |
| 548 | |
| 549 | self._calls_done.append(top) |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 550 | if isinstance(result, Exception): |
| 551 | raise result |
maruel@chromium.org | 2e72bb1 | 2012-01-17 15:18:35 +0000 | [diff] [blame] | 552 | return result |
| 553 | |
Andrii Shyshkalov | abc26ac | 2017-03-14 14:49:38 +0100 | [diff] [blame] | 554 | def test_ask_for_explicit_yes_true(self): |
| 555 | self.calls = [ |
| 556 | (('ask_for_data', 'prompt [Yes/No]: '), 'blah'), |
| 557 | (('ask_for_data', 'Please, type yes or no: '), 'ye'), |
| 558 | ] |
| 559 | self.assertTrue(git_cl.ask_for_explicit_yes('prompt')) |
| 560 | |
| 561 | def test_ask_for_explicit_yes_true(self): |
| 562 | self.calls = [ |
| 563 | (('ask_for_data', 'prompt [Yes/No]: '), 'yesish'), |
| 564 | (('ask_for_data', 'Please, type yes or no: '), 'nO'), |
| 565 | ] |
| 566 | self.assertFalse(git_cl.ask_for_explicit_yes('prompt')) |
| 567 | |
tandrii | 48df581 | 2016-10-17 03:55:37 -0700 | [diff] [blame] | 568 | def test_LoadCodereviewSettingsFromFile_gerrit(self): |
| 569 | codereview_file = StringIO.StringIO('GERRIT_HOST: true') |
| 570 | self.calls = [ |
| 571 | ((['git', 'config', '--unset-all', 'rietveld.cc'],), CERR1), |
| 572 | ((['git', 'config', '--unset-all', 'rietveld.private'],), CERR1), |
| 573 | ((['git', 'config', '--unset-all', 'rietveld.tree-status-url'],), CERR1), |
| 574 | ((['git', 'config', '--unset-all', 'rietveld.viewvc-url'],), CERR1), |
Mark Mentovai | 57c4721 | 2017-03-09 11:14:09 -0500 | [diff] [blame] | 575 | ((['git', 'config', '--unset-all', 'rietveld.bug-line-format'],), CERR1), |
tandrii | 48df581 | 2016-10-17 03:55:37 -0700 | [diff] [blame] | 576 | ((['git', 'config', '--unset-all', 'rietveld.bug-prefix'],), CERR1), |
| 577 | ((['git', 'config', '--unset-all', 'rietveld.cpplint-regex'],), CERR1), |
tandrii | 48df581 | 2016-10-17 03:55:37 -0700 | [diff] [blame] | 578 | ((['git', 'config', '--unset-all', 'rietveld.cpplint-ignore-regex'],), |
| 579 | CERR1), |
| 580 | ((['git', 'config', '--unset-all', 'rietveld.project'],), CERR1), |
tandrii | 48df581 | 2016-10-17 03:55:37 -0700 | [diff] [blame] | 581 | ((['git', 'config', '--unset-all', 'rietveld.run-post-upload-hook'],), |
| 582 | CERR1), |
| 583 | ((['git', 'config', 'gerrit.host', 'true'],), ''), |
| 584 | ] |
| 585 | self.assertIsNone(git_cl.LoadCodereviewSettingsFromFile(codereview_file)) |
| 586 | |
maruel@chromium.org | a335365 | 2011-11-30 14:26:57 +0000 | [diff] [blame] | 587 | @classmethod |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 588 | def _is_gerrit_calls(cls, gerrit=False): |
| 589 | return [((['git', 'config', 'rietveld.autoupdate'],), ''), |
| 590 | ((['git', 'config', 'gerrit.host'],), 'True' if gerrit else '')] |
| 591 | |
| 592 | @classmethod |
tyoshino@chromium.org | c1737d0 | 2013-05-29 14:17:28 +0000 | [diff] [blame] | 593 | def _upload_calls(cls, similarity, find_copies, private): |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 594 | return (cls._git_base_calls(similarity, find_copies) + |
tyoshino@chromium.org | c1737d0 | 2013-05-29 14:17:28 +0000 | [diff] [blame] | 595 | cls._git_upload_calls(private)) |
maruel@chromium.org | a335365 | 2011-11-30 14:26:57 +0000 | [diff] [blame] | 596 | |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 597 | @classmethod |
jbroman@chromium.org | 615a262 | 2013-05-03 13:20:14 +0000 | [diff] [blame] | 598 | def _upload_no_rev_calls(cls, similarity, find_copies): |
| 599 | return (cls._git_base_calls(similarity, find_copies) + |
| 600 | cls._git_upload_no_rev_calls()) |
| 601 | |
| 602 | @classmethod |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 603 | def _git_base_calls(cls, similarity, find_copies): |
iannucci@chromium.org | 53937ba | 2012-10-02 18:20:43 +0000 | [diff] [blame] | 604 | if similarity is None: |
| 605 | similarity = '50' |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 606 | similarity_call = ((['git', 'config', |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 607 | 'branch.master.git-cl-similarity'],), CERR1) |
iannucci@chromium.org | 53937ba | 2012-10-02 18:20:43 +0000 | [diff] [blame] | 608 | else: |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 609 | similarity_call = ((['git', 'config', |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 610 | 'branch.master.git-cl-similarity', similarity],), '') |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 611 | |
| 612 | if find_copies is None: |
| 613 | find_copies = True |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 614 | find_copies_call = ((['git', 'config', '--bool', |
| 615 | 'branch.master.git-find-copies'],), CERR1) |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 616 | else: |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 617 | val = str(find_copies).lower() |
| 618 | find_copies_call = ((['git', 'config', '--bool', |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 619 | 'branch.master.git-find-copies', val],), '') |
| 620 | |
| 621 | if find_copies: |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 622 | stat_call = ((['git', 'diff', '--no-ext-diff', '--stat', |
scottmg | b84b5e3 | 2016-11-10 09:25:33 -0800 | [diff] [blame] | 623 | '-l100000', '-C'+similarity, |
| 624 | 'fake_ancestor_sha', 'HEAD'],), '+dat') |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 625 | else: |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 626 | stat_call = ((['git', 'diff', '--no-ext-diff', '--stat', |
sbc@chromium.org | 5e07e06 | 2013-02-28 23:55:44 +0000 | [diff] [blame] | 627 | '-M'+similarity, 'fake_ancestor_sha', 'HEAD'],), '+dat') |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 628 | |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 629 | return [ |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 630 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
tandrii@chromium.org | 87985d2 | 2016-03-24 17:33:33 +0000 | [diff] [blame] | 631 | similarity_call, |
| 632 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
| 633 | find_copies_call, |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 634 | ] + cls._is_gerrit_calls() + [ |
tandrii@chromium.org | 87985d2 | 2016-03-24 17:33:33 +0000 | [diff] [blame] | 635 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 636 | ((['git', 'config', 'branch.master.rietveldissue'],), CERR1), |
| 637 | ((['git', 'config', 'branch.master.gerritissue'],), CERR1), |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 638 | ((['git', 'config', 'rietveld.server'],), |
| 639 | 'codereview.example.com'), |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 640 | ((['git', 'config', 'branch.master.merge'],), 'master'), |
| 641 | ((['git', 'config', 'branch.master.remote'],), 'origin'), |
iannucci@chromium.org | 9e84927 | 2014-04-04 00:31:55 +0000 | [diff] [blame] | 642 | ((['get_or_create_merge_base', 'master', 'master'],), |
bratell@opera.com | f267b0e | 2013-05-02 09:11:43 +0000 | [diff] [blame] | 643 | 'fake_ancestor_sha'), |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 644 | ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [ |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 645 | ((['git', 'rev-parse', '--show-cdup'],), ''), |
| 646 | ((['git', 'rev-parse', 'HEAD'],), '12345'), |
| 647 | ((['git', 'diff', '--name-status', '--no-renames', '-r', |
bratell@opera.com | f267b0e | 2013-05-02 09:11:43 +0000 | [diff] [blame] | 648 | 'fake_ancestor_sha...', '.'],), |
maruel@chromium.org | 2e72bb1 | 2012-01-17 15:18:35 +0000 | [diff] [blame] | 649 | 'M\t.gitignore\n'), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 650 | ((['git', 'config', 'branch.master.rietveldpatchset'],), CERR1), |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 651 | ((['git', 'log', '--pretty=format:%s%n%n%b', |
bratell@opera.com | f267b0e | 2013-05-02 09:11:43 +0000 | [diff] [blame] | 652 | 'fake_ancestor_sha...'],), |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 653 | 'foo'), |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 654 | ((['git', 'config', 'user.email'],), 'me@example.com'), |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 655 | stat_call, |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 656 | ((['git', 'log', '--pretty=format:%s\n\n%b', |
bratell@opera.com | f267b0e | 2013-05-02 09:11:43 +0000 | [diff] [blame] | 657 | 'fake_ancestor_sha..HEAD'],), |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 658 | 'desc\n'), |
Mark Mentovai | 57c4721 | 2017-03-09 11:14:09 -0500 | [diff] [blame] | 659 | ((['git', 'config', 'rietveld.bug-prefix'],), CERR1), |
| 660 | ((['git', 'config', 'rietveld.bug-line-format'],), CERR1), |
maruel@chromium.org | a335365 | 2011-11-30 14:26:57 +0000 | [diff] [blame] | 661 | ] |
| 662 | |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 663 | @classmethod |
jbroman@chromium.org | 615a262 | 2013-05-03 13:20:14 +0000 | [diff] [blame] | 664 | def _git_upload_no_rev_calls(cls): |
| 665 | return [ |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 666 | ((['git', 'config', 'core.editor'],), ''), |
jbroman@chromium.org | 615a262 | 2013-05-03 13:20:14 +0000 | [diff] [blame] | 667 | ] |
| 668 | |
| 669 | @classmethod |
tyoshino@chromium.org | c1737d0 | 2013-05-29 14:17:28 +0000 | [diff] [blame] | 670 | def _git_upload_calls(cls, private): |
| 671 | if private: |
tyoshino@chromium.org | 99918ab | 2013-09-30 06:17:28 +0000 | [diff] [blame] | 672 | cc_call = [] |
tyoshino@chromium.org | c1737d0 | 2013-05-29 14:17:28 +0000 | [diff] [blame] | 673 | private_call = [] |
| 674 | else: |
tyoshino@chromium.org | 99918ab | 2013-09-30 06:17:28 +0000 | [diff] [blame] | 675 | cc_call = [((['git', 'config', 'rietveld.cc'],), '')] |
tyoshino@chromium.org | c1737d0 | 2013-05-29 14:17:28 +0000 | [diff] [blame] | 676 | private_call = [ |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 677 | ((['git', 'config', 'rietveld.private'],), '')] |
tyoshino@chromium.org | c1737d0 | 2013-05-29 14:17:28 +0000 | [diff] [blame] | 678 | |
maruel@chromium.org | a335365 | 2011-11-30 14:26:57 +0000 | [diff] [blame] | 679 | return [ |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 680 | ((['git', 'config', 'core.editor'],), ''), |
tyoshino@chromium.org | 99918ab | 2013-09-30 06:17:28 +0000 | [diff] [blame] | 681 | ] + cc_call + private_call + [ |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 682 | ((['git', 'config', 'branch.master.base-url'],), ''), |
Aaron Gable | 1bc7bfe | 2016-12-19 10:08:14 -0800 | [diff] [blame] | 683 | ((['git', 'config', 'remote.origin.url'],), ''), |
sheyang@chromium.org | 152cf83 | 2014-06-11 21:37:49 +0000 | [diff] [blame] | 684 | ((['git', 'config', 'rietveld.project'],), ''), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 685 | ((['git', 'config', 'branch.master.rietveldissue', '1'],), ''), |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 686 | ((['git', 'config', 'branch.master.rietveldserver', |
tyoshino@chromium.org | c1737d0 | 2013-05-29 14:17:28 +0000 | [diff] [blame] | 687 | 'https://codereview.example.com'],), ''), |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 688 | ((['git', |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 689 | 'config', 'branch.master.rietveldpatchset', '2'],), ''), |
tandrii@chromium.org | 1e67bb7 | 2016-02-11 12:15:49 +0000 | [diff] [blame] | 690 | ] + cls._git_post_upload_calls() |
| 691 | |
| 692 | @classmethod |
| 693 | def _git_post_upload_calls(cls): |
| 694 | return [ |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 695 | ((['git', 'rev-parse', 'HEAD'],), 'hash'), |
| 696 | ((['git', 'symbolic-ref', 'HEAD'],), 'hash'), |
| 697 | ((['git', |
tyoshino@chromium.org | c1737d0 | 2013-05-29 14:17:28 +0000 | [diff] [blame] | 698 | 'config', 'branch.hash.last-upload-hash', 'hash'],), ''), |
rmistry@google.com | 5626a92 | 2015-02-26 14:03:30 +0000 | [diff] [blame] | 699 | ((['git', 'config', 'rietveld.run-post-upload-hook'],), ''), |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 700 | ] |
| 701 | |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 702 | @staticmethod |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 703 | def _git_sanity_checks(diff_base, working_branch, get_remote_branch=True): |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 704 | fake_ancestor = 'fake_ancestor' |
| 705 | fake_cl = 'fake_cl_for_patch' |
| 706 | return [ |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 707 | ((['git', |
bratell@opera.com | f267b0e | 2013-05-02 09:11:43 +0000 | [diff] [blame] | 708 | 'rev-parse', '--verify', diff_base],), fake_ancestor), |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 709 | ((['git', |
bratell@opera.com | f267b0e | 2013-05-02 09:11:43 +0000 | [diff] [blame] | 710 | 'merge-base', fake_ancestor, 'HEAD'],), fake_ancestor), |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 711 | ((['git', |
bratell@opera.com | f267b0e | 2013-05-02 09:11:43 +0000 | [diff] [blame] | 712 | 'rev-list', '^' + fake_ancestor, 'HEAD'],), fake_cl), |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 713 | # Mock a config miss (error code 1) |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 714 | ((['git', |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 715 | 'config', 'gitcl.remotebranch'],), CERR1), |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 716 | ] + ([ |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 717 | # Call to GetRemoteBranch() |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 718 | ((['git', |
bratell@opera.com | f267b0e | 2013-05-02 09:11:43 +0000 | [diff] [blame] | 719 | 'config', 'branch.%s.merge' % working_branch],), |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 720 | 'refs/heads/master'), |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 721 | ((['git', |
bratell@opera.com | f267b0e | 2013-05-02 09:11:43 +0000 | [diff] [blame] | 722 | 'config', 'branch.%s.remote' % working_branch],), 'origin'), |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 723 | ] if get_remote_branch else []) + [ |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 724 | ((['git', 'rev-list', '^' + fake_ancestor, |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 725 | 'refs/remotes/origin/master'],), ''), |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 726 | ] |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 727 | |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 728 | @staticmethod |
bradnelson | d975b30 | 2016-10-23 12:20:23 -0700 | [diff] [blame] | 729 | def _cmd_line(description, args, similarity, find_copies, private, cc): |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 730 | """Returns the upload command line passed to upload.RealMain().""" |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 731 | return [ |
| 732 | 'upload', '--assume_yes', '--server', |
maruel@chromium.org | eb5edbc | 2012-01-16 17:03:28 +0000 | [diff] [blame] | 733 | 'https://codereview.example.com', |
maruel@chromium.org | 71e12a9 | 2012-02-14 02:34:15 +0000 | [diff] [blame] | 734 | '--message', description |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 735 | ] + args + [ |
bradnelson | d975b30 | 2016-10-23 12:20:23 -0700 | [diff] [blame] | 736 | '--cc', ','.join(['joe@example.com'] + cc), |
tyoshino@chromium.org | c1737d0 | 2013-05-29 14:17:28 +0000 | [diff] [blame] | 737 | ] + (['--private'] if private else []) + [ |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 738 | '--git_similarity', similarity or '50' |
Andrii Shyshkalov | 1897532 | 2017-01-25 16:44:13 +0100 | [diff] [blame] | 739 | ] + (['--git_no_find_copies'] if find_copies is False else []) + [ |
sbc@chromium.org | 5e07e06 | 2013-02-28 23:55:44 +0000 | [diff] [blame] | 740 | 'fake_ancestor_sha', 'HEAD' |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 741 | ] |
| 742 | |
| 743 | def _run_reviewer_test( |
| 744 | self, |
| 745 | upload_args, |
| 746 | expected_description, |
| 747 | returned_description, |
| 748 | final_description, |
tyoshino@chromium.org | c1737d0 | 2013-05-29 14:17:28 +0000 | [diff] [blame] | 749 | reviewers, |
bradnelson | d975b30 | 2016-10-23 12:20:23 -0700 | [diff] [blame] | 750 | private=False, |
| 751 | cc=None): |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 752 | """Generic reviewer test framework.""" |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 753 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
iannucci@chromium.org | 53937ba | 2012-10-02 18:20:43 +0000 | [diff] [blame] | 754 | try: |
| 755 | similarity = upload_args[upload_args.index('--similarity')+1] |
| 756 | except ValueError: |
| 757 | similarity = None |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 758 | |
| 759 | if '--find-copies' in upload_args: |
| 760 | find_copies = True |
| 761 | elif '--no-find-copies' in upload_args: |
| 762 | find_copies = False |
| 763 | else: |
| 764 | find_copies = None |
| 765 | |
tyoshino@chromium.org | c1737d0 | 2013-05-29 14:17:28 +0000 | [diff] [blame] | 766 | private = '--private' in upload_args |
bradnelson | d975b30 | 2016-10-23 12:20:23 -0700 | [diff] [blame] | 767 | cc = cc or [] |
tyoshino@chromium.org | c1737d0 | 2013-05-29 14:17:28 +0000 | [diff] [blame] | 768 | |
| 769 | self.calls = self._upload_calls(similarity, find_copies, private) |
pgervais@chromium.org | 87884cc | 2014-01-03 22:23:41 +0000 | [diff] [blame] | 770 | |
jbroman@chromium.org | 615a262 | 2013-05-03 13:20:14 +0000 | [diff] [blame] | 771 | def RunEditor(desc, _, **kwargs): |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 772 | self.assertEquals( |
| 773 | '# Enter a description of the change.\n' |
janx@chromium.org | 104b2db | 2013-04-18 12:58:40 +0000 | [diff] [blame] | 774 | '# This will be displayed on the codereview site.\n' |
alancutter@chromium.org | 63a4d7f | 2013-05-31 02:22:45 +0000 | [diff] [blame] | 775 | '# The first line will also be used as the subject of the review.\n' |
alancutter@chromium.org | bd1073e | 2013-06-01 00:34:38 +0000 | [diff] [blame] | 776 | '#--------------------This line is 72 characters long' |
| 777 | '--------------------\n' + |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 778 | expected_description, |
| 779 | desc) |
| 780 | return returned_description |
| 781 | self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor) |
pgervais@chromium.org | 87884cc | 2014-01-03 22:23:41 +0000 | [diff] [blame] | 782 | |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 783 | def check_upload(args): |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 784 | cmd_line = self._cmd_line(final_description, reviewers, similarity, |
bradnelson | d975b30 | 2016-10-23 12:20:23 -0700 | [diff] [blame] | 785 | find_copies, private, cc) |
iannucci@chromium.org | 53937ba | 2012-10-02 18:20:43 +0000 | [diff] [blame] | 786 | self.assertEquals(cmd_line, args) |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 787 | return 1, 2 |
| 788 | self.mock(git_cl.upload, 'RealMain', check_upload) |
pgervais@chromium.org | 87884cc | 2014-01-03 22:23:41 +0000 | [diff] [blame] | 789 | |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 790 | git_cl.main(['upload'] + upload_args) |
| 791 | |
| 792 | def test_no_reviewer(self): |
| 793 | self._run_reviewer_test( |
| 794 | [], |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 795 | 'desc\n\nBUG=', |
| 796 | '# Blah blah comment.\ndesc\n\nBUG=', |
| 797 | 'desc\n\nBUG=', |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 798 | []) |
| 799 | |
iannucci@chromium.org | 53937ba | 2012-10-02 18:20:43 +0000 | [diff] [blame] | 800 | def test_keep_similarity(self): |
| 801 | self._run_reviewer_test( |
| 802 | ['--similarity', '70'], |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 803 | 'desc\n\nBUG=', |
| 804 | '# Blah blah comment.\ndesc\n\nBUG=', |
| 805 | 'desc\n\nBUG=', |
iannucci@chromium.org | 53937ba | 2012-10-02 18:20:43 +0000 | [diff] [blame] | 806 | []) |
| 807 | |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 808 | def test_keep_find_copies(self): |
| 809 | self._run_reviewer_test( |
| 810 | ['--no-find-copies'], |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 811 | 'desc\n\nBUG=', |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 812 | '# Blah blah comment.\ndesc\n\nBUG=\n', |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 813 | 'desc\n\nBUG=', |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 814 | []) |
| 815 | |
tyoshino@chromium.org | c1737d0 | 2013-05-29 14:17:28 +0000 | [diff] [blame] | 816 | def test_private(self): |
| 817 | self._run_reviewer_test( |
| 818 | ['--private'], |
| 819 | 'desc\n\nBUG=', |
| 820 | '# Blah blah comment.\ndesc\n\nBUG=\n', |
| 821 | 'desc\n\nBUG=', |
| 822 | []) |
| 823 | |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 824 | def test_reviewers_cmd_line(self): |
| 825 | # Reviewer is passed as-is |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 826 | description = 'desc\n\nR=foo@example.com\nBUG=' |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 827 | self._run_reviewer_test( |
| 828 | ['-r' 'foo@example.com'], |
| 829 | description, |
| 830 | '\n%s\n' % description, |
| 831 | description, |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 832 | ['--reviewers=foo@example.com']) |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 833 | |
| 834 | def test_reviewer_tbr_overriden(self): |
| 835 | # Reviewer is overriden with TBR |
| 836 | # Also verifies the regexp work without a trailing LF |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 837 | description = 'Foo Bar\n\nTBR=reviewer@example.com' |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 838 | self._run_reviewer_test( |
| 839 | ['-r' 'foo@example.com'], |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 840 | 'desc\n\nR=foo@example.com\nBUG=', |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 841 | description.strip('\n'), |
| 842 | description, |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 843 | ['--reviewers=reviewer@example.com']) |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 844 | |
| 845 | def test_reviewer_multiple(self): |
| 846 | # Handles multiple R= or TBR= lines. |
| 847 | description = ( |
bradnelson | d975b30 | 2016-10-23 12:20:23 -0700 | [diff] [blame] | 848 | 'Foo Bar\nTBR=reviewer@example.com\nBUG=\nR=another@example.com\n' |
| 849 | 'CC=more@example.com,people@example.com') |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 850 | self._run_reviewer_test( |
| 851 | [], |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 852 | 'desc\n\nBUG=', |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 853 | description, |
| 854 | description, |
bradnelson | d975b30 | 2016-10-23 12:20:23 -0700 | [diff] [blame] | 855 | ['--reviewers=another@example.com,reviewer@example.com'], |
| 856 | cc=['more@example.com', 'people@example.com']) |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 857 | |
maruel@chromium.org | a335365 | 2011-11-30 14:26:57 +0000 | [diff] [blame] | 858 | def test_reviewer_send_mail(self): |
| 859 | # --send-mail can be used without -r if R= is used |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 860 | description = 'Foo Bar\nR=reviewer@example.com' |
maruel@chromium.org | a335365 | 2011-11-30 14:26:57 +0000 | [diff] [blame] | 861 | self._run_reviewer_test( |
| 862 | ['--send-mail'], |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 863 | 'desc\n\nBUG=', |
maruel@chromium.org | a335365 | 2011-11-30 14:26:57 +0000 | [diff] [blame] | 864 | description.strip('\n'), |
| 865 | description, |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 866 | ['--reviewers=reviewer@example.com', '--send_mail']) |
maruel@chromium.org | a335365 | 2011-11-30 14:26:57 +0000 | [diff] [blame] | 867 | |
| 868 | def test_reviewer_send_mail_no_rev(self): |
| 869 | # Fails without a reviewer. |
maruel@chromium.org | 2e23ce3 | 2013-05-07 12:42:28 +0000 | [diff] [blame] | 870 | stdout = StringIO.StringIO() |
tandrii | c2405f5 | 2016-10-10 08:13:15 -0700 | [diff] [blame] | 871 | self.calls = self._upload_no_rev_calls(None, None) + [ |
| 872 | ((['DieWithError', 'Must specify reviewers to send email.'],), |
| 873 | SystemExitMock()) |
| 874 | ] |
| 875 | |
| 876 | def RunEditor(desc, _, **kwargs): |
| 877 | return desc |
| 878 | self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor) |
| 879 | self.mock(sys, 'stdout', stdout) |
| 880 | with self.assertRaises(SystemExitMock): |
maruel@chromium.org | a335365 | 2011-11-30 14:26:57 +0000 | [diff] [blame] | 881 | git_cl.main(['upload', '--send-mail']) |
tandrii | c2405f5 | 2016-10-10 08:13:15 -0700 | [diff] [blame] | 882 | self.assertEqual( |
| 883 | 'Using 50% similarity for rename/copy detection. Override with ' |
| 884 | '--similarity.\n', |
| 885 | stdout.getvalue()) |
maruel@chromium.org | a335365 | 2011-11-30 14:26:57 +0000 | [diff] [blame] | 886 | |
tandrii | f9aefb7 | 2016-07-01 09:06:51 -0700 | [diff] [blame] | 887 | def test_bug_on_cmd(self): |
| 888 | self._run_reviewer_test( |
| 889 | ['--bug=500658,proj:123'], |
| 890 | 'desc\n\nBUG=500658\nBUG=proj:123', |
| 891 | '# Blah blah comment.\ndesc\n\nBUG=500658\nBUG=proj:1234', |
| 892 | 'desc\n\nBUG=500658\nBUG=proj:1234', |
| 893 | []) |
| 894 | |
Andrii Shyshkalov | 768f1d8 | 2016-12-08 15:10:13 +0100 | [diff] [blame] | 895 | def _land_rietveld_common(self, debug=False): |
| 896 | if debug: |
| 897 | # Very useful due to finally clause in git cl land raising exceptions and |
| 898 | # shadowing real cause of failure. |
| 899 | self.mock(git_cl, '_IS_BEING_TESTED', True) |
| 900 | else: |
Andrii Shyshkalov | 06a2502 | 2016-11-24 16:47:00 +0100 | [diff] [blame] | 901 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
Andrii Shyshkalov | 768f1d8 | 2016-12-08 15:10:13 +0100 | [diff] [blame] | 902 | |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 903 | self.mock(git_cl, '_is_git_numberer_enabled', lambda url, ref: |
| 904 | self._mocked_call('_is_git_numberer_enabled', url, ref)) |
Andrii Shyshkalov | 06a2502 | 2016-11-24 16:47:00 +0100 | [diff] [blame] | 905 | self.mock(RietveldMock, 'update_description', staticmethod( |
| 906 | lambda i, d: self._mocked_call(['update_description', i, d]))) |
| 907 | self.mock(RietveldMock, 'add_comment', staticmethod( |
| 908 | lambda i, c: self._mocked_call(['add_comment', i, c]))) |
| 909 | self.calls = [ |
Andrii Shyshkalov | 06a2502 | 2016-11-24 16:47:00 +0100 | [diff] [blame] | 910 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
| 911 | ((['git', 'config', 'branch.feature.git-cl-similarity'],), CERR1), |
| 912 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
| 913 | ((['git', 'config', '--bool', 'branch.feature.git-find-copies'],), |
| 914 | CERR1), |
| 915 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
| 916 | ((['git', 'config', 'branch.feature.rietveldissue'],), '123'), |
Aaron Gable | 1bc7bfe | 2016-12-19 10:08:14 -0800 | [diff] [blame] | 917 | ((['git', 'config', 'rietveld.autoupdate'],), ''), |
Andrii Shyshkalov | 06a2502 | 2016-11-24 16:47:00 +0100 | [diff] [blame] | 918 | ((['git', 'config', 'rietveld.server'],), |
| 919 | 'https://codereview.chromium.org'), |
| 920 | ((['git', 'config', 'branch.feature.merge'],), 'refs/heads/master'), |
| 921 | ((['git', 'config', 'branch.feature.remote'],), 'origin'), |
| 922 | ((['git', 'config', 'branch.feature.merge'],), 'refs/heads/master'), |
| 923 | ((['git', 'config', 'branch.feature.remote'],), 'origin'), |
Andrii Shyshkalov | 06a2502 | 2016-11-24 16:47:00 +0100 | [diff] [blame] | 924 | ((['git', 'rev-list', '^feature', 'refs/remotes/origin/master'],), |
| 925 | ''), # No commits to rebase, according to local view of origin. |
| 926 | ((['git', 'merge-base', 'refs/remotes/origin/master', 'HEAD'],), |
| 927 | 'fake_ancestor_sha'), |
| 928 | ] + self._git_sanity_checks('fake_ancestor_sha', 'feature') + [ |
| 929 | ((['git', 'rev-parse', '--show-cdup'],), ''), |
| 930 | ((['git', 'rev-parse', 'HEAD'],), 'fake_sha'), |
| 931 | ((['git', 'diff', '--name-status', '--no-renames', '-r', |
| 932 | 'fake_ancestor_sha...', '.'],), |
| 933 | 'M\tfile1.cpp'), |
| 934 | ((['git', 'config', 'branch.feature.rietveldpatchset'],), '20001'), |
| 935 | ((['git', 'config', 'branch.feature.rietveldserver'],), |
| 936 | 'https://codereview.chromium.org'), |
| 937 | ((['git', 'config', 'user.email'],), 'user@e.mail'), |
| 938 | ((['git', 'config', 'rietveld.tree-status-url'],), CERR1), |
| 939 | ((['git', 'diff', '--no-ext-diff', '--stat', '-l100000', '-C50', |
| 940 | 'fake_ancestor_sha', 'feature'],), |
| 941 | # This command just prints smth like this: |
| 942 | # file1.cpp | 53 ++++++-- |
| 943 | # 1 file changed, 33 insertions(+), 20 deletions(-)\n |
| 944 | ''), |
| 945 | ((['git', 'show-ref', '--quiet', '--verify', |
| 946 | 'refs/heads/git-cl-commit'],), |
| 947 | ''), # 0 return code means branch exists. |
| 948 | ((['git', 'branch', '-D', 'git-cl-commit'],), ''), |
| 949 | ((['git', 'show-ref', '--quiet', '--verify', |
| 950 | 'refs/heads/git-cl-cherry-pick'],), |
| 951 | CERR1), # This means git-cl-cherry-pick branch does not exist. |
| 952 | ((['git', 'rev-parse', '--show-cdup'],), ''), |
| 953 | ((['git', 'checkout', '-q', '-b', 'git-cl-commit'],), ''), |
| 954 | ((['git', 'reset', '--soft', 'fake_ancestor_sha'],), ''), |
| 955 | ((['git', 'commit', '-m', |
| 956 | 'Issue: 123\n\nR=john@chromium.org\n\n' |
Andrii Shyshkalov | f01be22 | 2016-12-08 19:21:13 +0100 | [diff] [blame] | 957 | 'Review-Url: https://codereview.chromium.org/123 .'],), ''), |
Andrii Shyshkalov | 06a2502 | 2016-11-24 16:47:00 +0100 | [diff] [blame] | 958 | ((['git', 'config', 'branch.feature.merge'],), 'refs/heads/master'), |
| 959 | ((['git', 'config', 'branch.feature.remote'],), 'origin'), |
| 960 | ((['git', 'config', '--get', 'remote.origin.url'],), |
| 961 | 'https://chromium.googlesource.com/infra/infra'), |
| 962 | ] |
| 963 | |
| 964 | def test_land_rietveld(self): |
Andrii Shyshkalov | 768f1d8 | 2016-12-08 15:10:13 +0100 | [diff] [blame] | 965 | self._land_rietveld_common(debug=False) |
Andrii Shyshkalov | 06a2502 | 2016-11-24 16:47:00 +0100 | [diff] [blame] | 966 | self.calls += [ |
Andrii Shyshkalov | 768f1d8 | 2016-12-08 15:10:13 +0100 | [diff] [blame] | 967 | ((['git', 'config', 'remote.origin.url'],), |
| 968 | 'https://chromium.googlesource.com/infra/infra'), |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 969 | (('_is_git_numberer_enabled', |
| 970 | 'https://chromium.googlesource.com/infra/infra', |
| 971 | 'refs/heads/master'), |
| 972 | False), |
Andrii Shyshkalov | 06a2502 | 2016-11-24 16:47:00 +0100 | [diff] [blame] | 973 | ((['git', 'push', '--porcelain', 'origin', 'HEAD:refs/heads/master'],), |
| 974 | ''), |
| 975 | ((['git', 'rev-parse', 'HEAD'],), 'fake_sha_rebased'), |
| 976 | ((['git', 'checkout', '-q', 'feature'],), ''), |
| 977 | ((['git', 'branch', '-D', 'git-cl-commit'],), ''), |
| 978 | ((['git', 'config', 'rietveld.viewvc-url'],), |
| 979 | 'https://chromium.googlesource.com/infra/infra/+/'), |
| 980 | ((['update_description', 123, |
| 981 | 'Issue: 123\n\nR=john@chromium.org\n\nCommitted: ' |
| 982 | 'https://chromium.googlesource.com/infra/infra/+/fake_sha_rebased'],), |
| 983 | ''), |
| 984 | ((['add_comment', 123, 'Committed patchset #2 (id:20001) manually as ' |
| 985 | 'fake_sha_rebased (presubmit successful).'],), ''), |
| 986 | ] |
| 987 | git_cl.main(['land']) |
| 988 | |
Andrii Shyshkalov | 15e50cc | 2016-12-02 14:34:08 +0100 | [diff] [blame] | 989 | def test_land_rietveld_git_numberer(self): |
Andrii Shyshkalov | 768f1d8 | 2016-12-08 15:10:13 +0100 | [diff] [blame] | 990 | self._land_rietveld_common(debug=False) |
Andrii Shyshkalov | a669581 | 2016-12-06 17:47:09 +0100 | [diff] [blame] | 991 | |
| 992 | # Special mocks to check validity of timestamp. |
| 993 | original_git_amend_head = git_cl._git_amend_head |
| 994 | def _git_amend_head_mock(msg, tstamp): |
| 995 | self._mocked_call(['git_amend_head committer timestamp', tstamp]) |
| 996 | return original_git_amend_head(msg, tstamp) |
| 997 | self.mock(git_cl, '_git_amend_head', _git_amend_head_mock) |
| 998 | |
Andrii Shyshkalov | 15e50cc | 2016-12-02 14:34:08 +0100 | [diff] [blame] | 999 | self.calls += [ |
Andrii Shyshkalov | 768f1d8 | 2016-12-08 15:10:13 +0100 | [diff] [blame] | 1000 | ((['git', 'config', 'remote.origin.url'],), |
| 1001 | 'https://chromium.googlesource.com/chromium/src'), |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1002 | (('_is_git_numberer_enabled', |
| 1003 | 'https://chromium.googlesource.com/chromium/src', |
| 1004 | 'refs/heads/master'), |
| 1005 | True), |
Andrii Shyshkalov | 15e50cc | 2016-12-02 14:34:08 +0100 | [diff] [blame] | 1006 | ((['git', 'show', '-s', '--format=%B', 'fake_ancestor_sha'],), |
| 1007 | 'This is parent commit.\n' |
| 1008 | '\n' |
| 1009 | 'Cr-Commit-Position: refs/heads/master@{#543}\n' |
| 1010 | 'Cr-Branched-From: refs/svn/2014@{#2208}'), |
Andrii Shyshkalov | a669581 | 2016-12-06 17:47:09 +0100 | [diff] [blame] | 1011 | ((['git', 'show', '-s', '--format=%ct', 'fake_ancestor_sha'],), |
| 1012 | '1480022355'), # Committer's unix timestamp. |
| 1013 | ((['git', 'show', '-s', '--format=%ct', 'HEAD'],), |
| 1014 | '1480024000'), |
Andrii Shyshkalov | 15e50cc | 2016-12-02 14:34:08 +0100 | [diff] [blame] | 1015 | |
Andrii Shyshkalov | a669581 | 2016-12-06 17:47:09 +0100 | [diff] [blame] | 1016 | ((['git_amend_head committer timestamp', 1480024000],), None), |
Andrii Shyshkalov | 15e50cc | 2016-12-02 14:34:08 +0100 | [diff] [blame] | 1017 | ((['git', 'commit', '--amend', '-m', |
| 1018 | 'Issue: 123\n\nR=john@chromium.org\n' |
| 1019 | '\n' |
Andrii Shyshkalov | f01be22 | 2016-12-08 19:21:13 +0100 | [diff] [blame] | 1020 | 'Review-Url: https://codereview.chromium.org/123 .\n' |
Andrii Shyshkalov | 15e50cc | 2016-12-02 14:34:08 +0100 | [diff] [blame] | 1021 | 'Cr-Commit-Position: refs/heads/master@{#544}\n' |
| 1022 | 'Cr-Branched-From: refs/svn/2014@{#2208}'],), ''), |
| 1023 | |
| 1024 | ((['git', 'push', '--porcelain', 'origin', 'HEAD:refs/heads/master'],), |
| 1025 | ''), |
| 1026 | ((['git', 'rev-parse', 'HEAD'],), 'fake_sha_rebased'), |
| 1027 | ((['git', 'checkout', '-q', 'feature'],), ''), |
| 1028 | ((['git', 'branch', '-D', 'git-cl-commit'],), ''), |
| 1029 | ((['git', 'config', 'rietveld.viewvc-url'],), |
| 1030 | 'https://chromium.googlesource.com/infra/infra/+/'), |
| 1031 | ((['update_description', 123, |
| 1032 | 'Issue: 123\n\nR=john@chromium.org\n' |
| 1033 | '\n' |
Andrii Shyshkalov | f01be22 | 2016-12-08 19:21:13 +0100 | [diff] [blame] | 1034 | 'Review-Url: https://codereview.chromium.org/123 .\n' |
Andrii Shyshkalov | 15e50cc | 2016-12-02 14:34:08 +0100 | [diff] [blame] | 1035 | 'Cr-Commit-Position: refs/heads/master@{#544}\n' |
| 1036 | 'Cr-Branched-From: refs/svn/2014@{#2208}\n' |
| 1037 | 'Committed: ' |
| 1038 | 'https://chromium.googlesource.com/infra/infra/+/fake_sha_rebased'],), |
| 1039 | ''), |
| 1040 | ((['add_comment', 123, 'Committed patchset #2 (id:20001) manually as ' |
| 1041 | 'fake_sha_rebased (presubmit successful).'],), ''), |
| 1042 | ] |
| 1043 | git_cl.main(['land']) |
| 1044 | |
| 1045 | def test_land_rietveld_git_numberer_bad_parent(self): |
Andrii Shyshkalov | 768f1d8 | 2016-12-08 15:10:13 +0100 | [diff] [blame] | 1046 | self._land_rietveld_common(debug=False) |
Andrii Shyshkalov | 15e50cc | 2016-12-02 14:34:08 +0100 | [diff] [blame] | 1047 | self.calls += [ |
Andrii Shyshkalov | 768f1d8 | 2016-12-08 15:10:13 +0100 | [diff] [blame] | 1048 | ((['git', 'config', 'remote.origin.url'],), |
| 1049 | 'https://chromium.googlesource.com/v8/v8'), |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1050 | (('_is_git_numberer_enabled', |
| 1051 | 'https://chromium.googlesource.com/v8/v8', 'refs/heads/master'), |
| 1052 | True), |
Andrii Shyshkalov | 15e50cc | 2016-12-02 14:34:08 +0100 | [diff] [blame] | 1053 | |
| 1054 | ((['git', 'show', '-s', '--format=%B', 'fake_ancestor_sha'],), |
| 1055 | 'This is parent commit with no footer.'), |
| 1056 | |
| 1057 | ((['git', 'checkout', '-q', 'feature'],), ''), |
| 1058 | ((['git', 'branch', '-D', 'git-cl-commit'],), ''), |
| 1059 | ] |
| 1060 | with self.assertRaises(ValueError) as cm: |
| 1061 | git_cl.main(['land']) |
| 1062 | self.assertEqual(cm.exception.message, |
| 1063 | 'Unable to infer commit position from footers') |
| 1064 | |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1065 | def test_git_numberer_not_whitelisted_repo(self): |
| 1066 | self.calls = [] |
| 1067 | res = git_cl._is_git_numberer_enabled( |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1068 | remote_url='https://chromium.googlesource.com/chromium/tools/build', |
| 1069 | remote_ref='refs/whatever') |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1070 | self.assertEqual(res, False) |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1071 | |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1072 | def test_git_numberer_fail_fetch(self): |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1073 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
| 1074 | self.calls = [ |
| 1075 | ((['git', 'fetch', 'https://chromium.googlesource.com/chromium/src', |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1076 | '+refs/meta/config:refs/git_cl/meta/config'],), CERR1), |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1077 | ] |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1078 | res = git_cl._is_git_numberer_enabled( |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1079 | remote_url='https://chromium.googlesource.com/chromium/src', |
| 1080 | remote_ref='refs/whatever') |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1081 | self.assertEqual(res, False) |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1082 | |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1083 | def test_git_numberer_valid_configs(self): |
Andrii Shyshkalov | 351c61d | 2017-01-21 20:40:16 +0000 | [diff] [blame] | 1084 | with git_cl.gclient_utils.temporary_directory() as tempdir: |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1085 | @contextlib.contextmanager |
Andrii Shyshkalov | 351c61d | 2017-01-21 20:40:16 +0000 | [diff] [blame] | 1086 | def fake_temporary_directory(**kwargs): |
| 1087 | yield tempdir |
| 1088 | self.mock(git_cl.gclient_utils, 'temporary_directory', |
| 1089 | fake_temporary_directory) |
| 1090 | self._test_GitNumbererState_valid_configs_inner(tempdir) |
| 1091 | |
| 1092 | def _test_GitNumbererState_valid_configs_inner(self, tempdir): |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1093 | self.calls = [ |
| 1094 | ((['git', 'fetch', 'https://chromium.googlesource.com/chromium/src', |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1095 | '+refs/meta/config:refs/git_cl/meta/config'],), ''), |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1096 | ((['git', 'show', 'refs/git_cl/meta/config:project.config'],), |
| 1097 | ''' |
| 1098 | [plugin "git-numberer"] |
| 1099 | validate-enabled-refglob = refs/else/* |
| 1100 | validate-enabled-refglob = refs/heads/* |
| 1101 | validate-disabled-refglob = refs/heads/disabled |
| 1102 | validate-disabled-refglob = refs/branch-heads/* |
| 1103 | '''), |
Andrii Shyshkalov | 351c61d | 2017-01-21 20:40:16 +0000 | [diff] [blame] | 1104 | ((['git', 'config', '-f', os.path.join(tempdir, 'project.config') , |
| 1105 | '--get-all', 'plugin.git-numberer.validate-enabled-refglob'],), |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1106 | 'refs/else/*\n' |
| 1107 | 'refs/heads/*\n'), |
Andrii Shyshkalov | 351c61d | 2017-01-21 20:40:16 +0000 | [diff] [blame] | 1108 | ((['git', 'config', '-f', os.path.join(tempdir, 'project.config') , |
| 1109 | '--get-all', 'plugin.git-numberer.validate-disabled-refglob'],), |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1110 | 'refs/heads/disabled\n' |
| 1111 | 'refs/branch-heads/*\n'), |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1112 | ] * 3 # 3 tests below have exactly the same IO. |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1113 | |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1114 | res = git_cl._is_git_numberer_enabled( |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1115 | remote_url='https://chromium.googlesource.com/chromium/src', |
| 1116 | remote_ref='refs/heads/test') |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1117 | self.assertEqual(res, True) |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1118 | |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1119 | res = git_cl._is_git_numberer_enabled( |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1120 | remote_url='https://chromium.googlesource.com/chromium/src', |
| 1121 | remote_ref='refs/heads/disabled') |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1122 | self.assertEqual(res, False) |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1123 | |
Andrii Shyshkalov | cbec8ef | 2016-12-09 13:54:51 +0100 | [diff] [blame] | 1124 | # Validator is disabled by default, even if it's not explicitely in disabled |
| 1125 | # refglobs. |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1126 | res = git_cl._is_git_numberer_enabled( |
Andrii Shyshkalov | cbec8ef | 2016-12-09 13:54:51 +0100 | [diff] [blame] | 1127 | remote_url='https://chromium.googlesource.com/chromium/src', |
| 1128 | remote_ref='refs/arbitrary/ref') |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1129 | self.assertEqual(res, False) |
Andrii Shyshkalov | cbec8ef | 2016-12-09 13:54:51 +0100 | [diff] [blame] | 1130 | |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 1131 | @classmethod |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 1132 | def _gerrit_ensure_auth_calls(cls, issue=None, skip_auth_check=False): |
shinyak@chromium.org | 00dbccd | 2016-04-15 07:24:43 +0000 | [diff] [blame] | 1133 | cmd = ['git', 'config', '--bool', 'gerrit.skip-ensure-authenticated'] |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 1134 | if skip_auth_check: |
| 1135 | return [((cmd, ), 'true')] |
| 1136 | |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 1137 | calls = [((cmd, ), CERR1)] |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 1138 | if issue: |
| 1139 | calls.extend([ |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1140 | ((['git', 'config', 'branch.master.gerritserver'],), CERR1), |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 1141 | ]) |
| 1142 | calls.extend([ |
| 1143 | ((['git', 'config', 'branch.master.merge'],), 'refs/heads/master'), |
| 1144 | ((['git', 'config', 'branch.master.remote'],), 'origin'), |
| 1145 | ((['git', 'config', 'remote.origin.url'],), |
| 1146 | 'https://chromium.googlesource.com/my/repo'), |
| 1147 | ((['git', 'config', 'remote.origin.url'],), |
| 1148 | 'https://chromium.googlesource.com/my/repo'), |
| 1149 | ]) |
| 1150 | return calls |
| 1151 | |
| 1152 | @classmethod |
Andrii Shyshkalov | 5c3d0b3 | 2017-02-16 17:47:31 +0100 | [diff] [blame] | 1153 | def _gerrit_base_calls(cls, issue=None, fetched_description=None, |
| 1154 | fetched_status=None): |
Andrii Shyshkalov | 0293956 | 2017-02-16 17:47:17 +0100 | [diff] [blame] | 1155 | calls = [ |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 1156 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 1157 | ((['git', 'config', 'branch.master.git-cl-similarity'],), |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 1158 | CERR1), |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 1159 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 1160 | ((['git', 'config', '--bool', 'branch.master.git-find-copies'],), |
| 1161 | CERR1), |
Andrii Shyshkalov | 0293956 | 2017-02-16 17:47:17 +0100 | [diff] [blame] | 1162 | ] |
| 1163 | calls += cls._is_gerrit_calls(True) |
| 1164 | calls += [ |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 1165 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 1166 | ((['git', 'config', 'branch.master.rietveldissue'],), CERR1), |
| 1167 | ((['git', 'config', 'branch.master.gerritissue'],), |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 1168 | CERR1 if issue is None else str(issue)), |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 1169 | ((['git', 'config', 'branch.master.merge'],), 'refs/heads/master'), |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 1170 | ((['git', 'config', 'branch.master.remote'],), 'origin'), |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 1171 | ((['get_or_create_merge_base', 'master', |
| 1172 | 'refs/remotes/origin/master'],), |
iannucci@chromium.org | 9e84927 | 2014-04-04 00:31:55 +0000 | [diff] [blame] | 1173 | 'fake_ancestor_sha'), |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 1174 | # Calls to verify branch point is ancestor |
Andrii Shyshkalov | 0293956 | 2017-02-16 17:47:17 +0100 | [diff] [blame] | 1175 | ] |
| 1176 | calls += cls._gerrit_ensure_auth_calls(issue=issue) |
| 1177 | |
| 1178 | if issue: |
| 1179 | calls += [ |
Andrii Shyshkalov | 3e63142 | 2017-02-16 17:46:44 +0100 | [diff] [blame] | 1180 | (('GetChangeDetail', 'chromium-review.googlesource.com', |
| 1181 | '123456', ['CURRENT_REVISION', 'CURRENT_COMMIT']), |
| 1182 | { |
| 1183 | 'change_id': '123456789', |
| 1184 | 'current_revision': 'sha1_of_current_revision', |
| 1185 | 'revisions': { 'sha1_of_current_revision': { |
| 1186 | 'commit': {'message': fetched_description}, |
| 1187 | }}, |
Andrii Shyshkalov | 5c3d0b3 | 2017-02-16 17:47:31 +0100 | [diff] [blame] | 1188 | 'status': fetched_status or 'NEW', |
Andrii Shyshkalov | 3e63142 | 2017-02-16 17:46:44 +0100 | [diff] [blame] | 1189 | }), |
Andrii Shyshkalov | 0293956 | 2017-02-16 17:47:17 +0100 | [diff] [blame] | 1190 | ] |
Andrii Shyshkalov | 5c3d0b3 | 2017-02-16 17:47:31 +0100 | [diff] [blame] | 1191 | if fetched_status == 'ABANDONED': |
| 1192 | calls += [ |
| 1193 | (('DieWithError', 'Change https://chromium-review.googlesource.com/' |
| 1194 | '123456 has been abandoned, new uploads are not ' |
| 1195 | 'allowed'), SystemExitMock()), |
| 1196 | ] |
| 1197 | return calls |
Andrii Shyshkalov | 0293956 | 2017-02-16 17:47:17 +0100 | [diff] [blame] | 1198 | |
| 1199 | calls += cls._git_sanity_checks('fake_ancestor_sha', 'master', |
| 1200 | get_remote_branch=False) |
| 1201 | calls += [ |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 1202 | ((['git', 'rev-parse', '--show-cdup'],), ''), |
| 1203 | ((['git', 'rev-parse', 'HEAD'],), '12345'), |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 1204 | |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 1205 | ((['git', |
mcgrathr@chromium.org | 9249f64 | 2013-06-03 21:36:18 +0000 | [diff] [blame] | 1206 | 'diff', '--name-status', '--no-renames', '-r', |
| 1207 | 'fake_ancestor_sha...', '.'],), |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1208 | 'M\t.gitignore\n'), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 1209 | ((['git', 'config', 'branch.master.gerritpatchset'],), CERR1), |
Andrii Shyshkalov | 0293956 | 2017-02-16 17:47:17 +0100 | [diff] [blame] | 1210 | ] |
| 1211 | |
| 1212 | if not issue: |
| 1213 | calls += [ |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 1214 | ((['git', |
bratell@opera.com | f267b0e | 2013-05-02 09:11:43 +0000 | [diff] [blame] | 1215 | 'log', '--pretty=format:%s%n%n%b', 'fake_ancestor_sha...'],), |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 1216 | 'foo'), |
Andrii Shyshkalov | 0293956 | 2017-02-16 17:47:17 +0100 | [diff] [blame] | 1217 | ] |
| 1218 | |
| 1219 | calls += [ |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 1220 | ((['git', 'config', 'user.email'],), 'me@example.com'), |
| 1221 | ((['git', |
scottmg | b84b5e3 | 2016-11-10 09:25:33 -0800 | [diff] [blame] | 1222 | 'diff', '--no-ext-diff', '--stat', '-l100000', '-C50', |
| 1223 | 'fake_ancestor_sha', 'HEAD'],), |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1224 | '+dat'), |
Andrii Shyshkalov | 0293956 | 2017-02-16 17:47:17 +0100 | [diff] [blame] | 1225 | ] |
| 1226 | return calls |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1227 | |
tandrii@chromium.org | 1e67bb7 | 2016-02-11 12:15:49 +0000 | [diff] [blame] | 1228 | @classmethod |
| 1229 | def _gerrit_upload_calls(cls, description, reviewers, squash, |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1230 | squash_mode='default', |
tandrii@chromium.org | 1062500 | 2016-03-04 20:03:47 +0000 | [diff] [blame] | 1231 | expected_upstream_ref='origin/refs/heads/master', |
Aaron Gable | 9b713dd | 2016-12-14 16:04:21 -0800 | [diff] [blame] | 1232 | ref_suffix='', title=None, notify=False, |
Andrii Shyshkalov | e9c78ff | 2017-02-06 15:53:13 +0100 | [diff] [blame] | 1233 | post_amend_description=None, issue=None, cc=None, |
| 1234 | git_mirror=None): |
tandrii@chromium.org | 1062500 | 2016-03-04 20:03:47 +0000 | [diff] [blame] | 1235 | if post_amend_description is None: |
| 1236 | post_amend_description = description |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1237 | calls = [] |
bradnelson | d975b30 | 2016-10-23 12:20:23 -0700 | [diff] [blame] | 1238 | cc = cc or [] |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 1239 | |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1240 | if squash_mode == 'default': |
| 1241 | calls.extend([ |
| 1242 | ((['git', 'config', '--bool', 'gerrit.override-squash-uploads'],), ''), |
| 1243 | ((['git', 'config', '--bool', 'gerrit.squash-uploads'],), ''), |
| 1244 | ]) |
| 1245 | elif squash_mode in ('override_squash', 'override_nosquash'): |
| 1246 | calls.extend([ |
| 1247 | ((['git', 'config', '--bool', 'gerrit.override-squash-uploads'],), |
| 1248 | 'true' if squash_mode == 'override_squash' else 'false'), |
| 1249 | ]) |
| 1250 | else: |
| 1251 | assert squash_mode in ('squash', 'nosquash') |
| 1252 | |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 1253 | # If issue is given, then description is fetched from Gerrit instead. |
| 1254 | if issue is None: |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 1255 | calls += [ |
| 1256 | ((['git', 'log', '--pretty=format:%s\n\n%b', |
| 1257 | 'fake_ancestor_sha..HEAD'],), |
| 1258 | description)] |
Aaron Gable | b56ad33 | 2017-01-06 15:24:31 -0800 | [diff] [blame] | 1259 | if squash: |
| 1260 | title = 'Initial_upload' |
| 1261 | else: |
| 1262 | if not title: |
| 1263 | calls += [ |
| 1264 | ((['git', 'show', '-s', '--format=%s', 'HEAD'],), ''), |
Andrii Shyshkalov | abc26ac | 2017-03-14 14:49:38 +0100 | [diff] [blame] | 1265 | (('ask_for_data', 'Title for patchset []: '), 'User input'), |
Aaron Gable | b56ad33 | 2017-01-06 15:24:31 -0800 | [diff] [blame] | 1266 | ] |
| 1267 | title = 'User_input' |
tandrii@chromium.org | 57d8654 | 2016-03-04 16:11:32 +0000 | [diff] [blame] | 1268 | if not git_footers.get_footer_change_id(description) and not squash: |
sivachandra@chromium.org | aebe87f | 2012-10-22 20:34:21 +0000 | [diff] [blame] | 1269 | calls += [ |
tandrii@chromium.org | 1062500 | 2016-03-04 20:03:47 +0000 | [diff] [blame] | 1270 | # DownloadGerritHook(False) |
| 1271 | ((False, ), |
| 1272 | ''), |
| 1273 | # Amending of commit message to get the Change-Id. |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 1274 | ((['git', 'log', '--pretty=format:%s\n\n%b', |
sbc@chromium.org | 5e07e06 | 2013-02-28 23:55:44 +0000 | [diff] [blame] | 1275 | 'fake_ancestor_sha..HEAD'],), |
sivachandra@chromium.org | aebe87f | 2012-10-22 20:34:21 +0000 | [diff] [blame] | 1276 | description), |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 1277 | ((['git', 'commit', '--amend', '-m', description],), |
sivachandra@chromium.org | aebe87f | 2012-10-22 20:34:21 +0000 | [diff] [blame] | 1278 | ''), |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 1279 | ((['git', 'log', '--pretty=format:%s\n\n%b', |
sbc@chromium.org | 5e07e06 | 2013-02-28 23:55:44 +0000 | [diff] [blame] | 1280 | 'fake_ancestor_sha..HEAD'],), |
tandrii@chromium.org | 1062500 | 2016-03-04 20:03:47 +0000 | [diff] [blame] | 1281 | post_amend_description) |
sivachandra@chromium.org | aebe87f | 2012-10-22 20:34:21 +0000 | [diff] [blame] | 1282 | ] |
bauerb@chromium.org | 27386dd | 2015-02-16 10:45:39 +0000 | [diff] [blame] | 1283 | if squash: |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 1284 | if not issue: |
| 1285 | # Prompting to edit description on first upload. |
| 1286 | calls += [ |
| 1287 | ((['git', 'config', 'core.editor'],), ''), |
| 1288 | ((['RunEditor'],), description), |
| 1289 | ] |
bauerb@chromium.org | 27386dd | 2015-02-16 10:45:39 +0000 | [diff] [blame] | 1290 | ref_to_push = 'abcdef0123456789' |
| 1291 | calls += [ |
bauerb@chromium.org | 27386dd | 2015-02-16 10:45:39 +0000 | [diff] [blame] | 1292 | ((['git', 'config', 'branch.master.merge'],), |
| 1293 | 'refs/heads/master'), |
| 1294 | ((['git', 'config', 'branch.master.remote'],), |
| 1295 | 'origin'), |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 1296 | ((['get_or_create_merge_base', 'master', |
| 1297 | 'refs/remotes/origin/master'],), |
bauerb@chromium.org | 27386dd | 2015-02-16 10:45:39 +0000 | [diff] [blame] | 1298 | 'origin/master'), |
| 1299 | ((['git', 'rev-parse', 'HEAD:'],), |
| 1300 | '0123456789abcdef'), |
| 1301 | ((['git', 'commit-tree', '0123456789abcdef', '-p', |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 1302 | 'origin/master', '-m', description],), |
bauerb@chromium.org | 27386dd | 2015-02-16 10:45:39 +0000 | [diff] [blame] | 1303 | ref_to_push), |
| 1304 | ] |
| 1305 | else: |
| 1306 | ref_to_push = 'HEAD' |
| 1307 | |
sivachandra@chromium.org | aebe87f | 2012-10-22 20:34:21 +0000 | [diff] [blame] | 1308 | calls += [ |
luqui@chromium.org | 609f395 | 2015-05-04 22:47:04 +0000 | [diff] [blame] | 1309 | ((['git', 'rev-list', |
| 1310 | expected_upstream_ref + '..' + ref_to_push],), ''), |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1311 | ] |
tandrii@chromium.org | 8da4540 | 2016-05-24 23:11:03 +0000 | [diff] [blame] | 1312 | |
Aaron Gable | 9b713dd | 2016-12-14 16:04:21 -0800 | [diff] [blame] | 1313 | if title: |
| 1314 | if ref_suffix: |
| 1315 | ref_suffix += ',m=' + title |
| 1316 | else: |
| 1317 | ref_suffix = '%m=' + title |
| 1318 | |
tandrii@chromium.org | 8da4540 | 2016-05-24 23:11:03 +0000 | [diff] [blame] | 1319 | notify_suffix = 'notify=%s' % ('ALL' if notify else 'NONE') |
| 1320 | if ref_suffix: |
| 1321 | ref_suffix += ',' + notify_suffix |
| 1322 | else: |
| 1323 | ref_suffix = '%' + notify_suffix |
tandrii@chromium.org | 074c2af | 2016-06-03 23:18:40 +0000 | [diff] [blame] | 1324 | |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1325 | if reviewers: |
tandrii@chromium.org | 8da4540 | 2016-05-24 23:11:03 +0000 | [diff] [blame] | 1326 | ref_suffix += ',' + ','.join('r=%s' % email |
| 1327 | for email in sorted(reviewers)) |
Andrii Shyshkalov | e9c78ff | 2017-02-06 15:53:13 +0100 | [diff] [blame] | 1328 | |
| 1329 | if git_mirror is None: |
| 1330 | calls += [ |
| 1331 | ((['git', 'config', 'remote.origin.url'],), |
| 1332 | 'https://chromium.googlesource.com/yyy/zzz'), |
| 1333 | ] |
| 1334 | else: |
| 1335 | calls += [ |
| 1336 | ((['git', 'config', 'remote.origin.url'],), |
| 1337 | '/cache/this-dir-exists'), |
| 1338 | (('os.path.isdir', '/cache/this-dir-exists'), |
| 1339 | True), |
| 1340 | # Runs in /cache/this-dir-exists. |
| 1341 | ((['git', 'config', 'remote.origin.url'],), |
| 1342 | 'https://chromium.googlesource.com/yyy/zzz'), |
| 1343 | ] |
| 1344 | |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1345 | calls += [ |
Andrii Shyshkalov | e9c78ff | 2017-02-06 15:53:13 +0100 | [diff] [blame] | 1346 | ((['git', 'push', |
| 1347 | 'https://chromium.googlesource.com/yyy/zzz', |
tandrii@chromium.org | bf766ba | 2016-04-13 12:51:23 +0000 | [diff] [blame] | 1348 | ref_to_push + ':refs/for/refs/heads/master' + ref_suffix],), |
tandrii@chromium.org | a342c92 | 2016-03-16 07:08:25 +0000 | [diff] [blame] | 1349 | ('remote:\n' |
| 1350 | 'remote: Processing changes: (\)\n' |
| 1351 | 'remote: Processing changes: (|)\n' |
| 1352 | 'remote: Processing changes: (/)\n' |
| 1353 | 'remote: Processing changes: (-)\n' |
| 1354 | 'remote: Processing changes: new: 1 (/)\n' |
| 1355 | 'remote: Processing changes: new: 1, done\n' |
| 1356 | 'remote:\n' |
| 1357 | 'remote: New Changes:\n' |
| 1358 | 'remote: https://chromium-review.googlesource.com/123456 XXX.\n' |
| 1359 | 'remote:\n' |
| 1360 | 'To https://chromium.googlesource.com/yyy/zzz\n' |
| 1361 | ' * [new branch] hhhh -> refs/for/refs/heads/master\n')), |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1362 | ] |
bauerb@chromium.org | 27386dd | 2015-02-16 10:45:39 +0000 | [diff] [blame] | 1363 | if squash: |
| 1364 | calls += [ |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 1365 | ((['git', 'config', 'branch.master.gerritissue', '123456'],), |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 1366 | ''), |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 1367 | ((['git', 'config', 'branch.master.gerritserver', |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 1368 | 'https://chromium-review.googlesource.com'],), ''), |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 1369 | ((['git', 'config', 'branch.master.gerritsquashhash', |
| 1370 | 'abcdef0123456789'],), ''), |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 1371 | ] |
tandrii | 8818977 | 2016-09-29 04:29:57 -0700 | [diff] [blame] | 1372 | calls += [ |
| 1373 | ((['git', 'config', 'rietveld.cc'],), ''), |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1374 | (('AddReviewers', 'chromium-review.googlesource.com', |
tandrii | 8818977 | 2016-09-29 04:29:57 -0700 | [diff] [blame] | 1375 | 123456 if squash else None, |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1376 | ['joe@example.com'] + cc, False, notify), ''), |
tandrii | 8818977 | 2016-09-29 04:29:57 -0700 | [diff] [blame] | 1377 | ] |
tandrii@chromium.org | 1e67bb7 | 2016-02-11 12:15:49 +0000 | [diff] [blame] | 1378 | calls += cls._git_post_upload_calls() |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1379 | return calls |
| 1380 | |
sivachandra@chromium.org | aebe87f | 2012-10-22 20:34:21 +0000 | [diff] [blame] | 1381 | def _run_gerrit_upload_test( |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1382 | self, |
| 1383 | upload_args, |
| 1384 | description, |
tandrii@chromium.org | bf766ba | 2016-04-13 12:51:23 +0000 | [diff] [blame] | 1385 | reviewers=None, |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1386 | squash=True, |
| 1387 | squash_mode=None, |
tandrii@chromium.org | 1062500 | 2016-03-04 20:03:47 +0000 | [diff] [blame] | 1388 | expected_upstream_ref='origin/refs/heads/master', |
tandrii@chromium.org | bf766ba | 2016-04-13 12:51:23 +0000 | [diff] [blame] | 1389 | ref_suffix='', |
Aaron Gable | 9b713dd | 2016-12-14 16:04:21 -0800 | [diff] [blame] | 1390 | title=None, |
tandrii@chromium.org | 8da4540 | 2016-05-24 23:11:03 +0000 | [diff] [blame] | 1391 | notify=False, |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 1392 | post_amend_description=None, |
bradnelson | d975b30 | 2016-10-23 12:20:23 -0700 | [diff] [blame] | 1393 | issue=None, |
Andrii Shyshkalov | e9c78ff | 2017-02-06 15:53:13 +0100 | [diff] [blame] | 1394 | cc=None, |
Andrii Shyshkalov | 5c3d0b3 | 2017-02-16 17:47:31 +0100 | [diff] [blame] | 1395 | git_mirror=None, |
| 1396 | fetched_status=None): |
sivachandra@chromium.org | aebe87f | 2012-10-22 20:34:21 +0000 | [diff] [blame] | 1397 | """Generic gerrit upload test framework.""" |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1398 | if squash_mode is None: |
| 1399 | if '--no-squash' in upload_args: |
| 1400 | squash_mode = 'nosquash' |
| 1401 | elif '--squash' in upload_args: |
| 1402 | squash_mode = 'squash' |
| 1403 | else: |
| 1404 | squash_mode = 'default' |
| 1405 | |
tandrii@chromium.org | bf766ba | 2016-04-13 12:51:23 +0000 | [diff] [blame] | 1406 | reviewers = reviewers or [] |
bradnelson | d975b30 | 2016-10-23 12:20:23 -0700 | [diff] [blame] | 1407 | cc = cc or [] |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 1408 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
tandrii | 16e0b4e | 2016-06-07 10:34:28 -0700 | [diff] [blame] | 1409 | self.mock(git_cl.gerrit_util, 'CookiesAuthenticator', |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 1410 | CookiesAuthenticatorMockFactory(same_cookie='same_cred')) |
tandrii | 16e0b4e | 2016-06-07 10:34:28 -0700 | [diff] [blame] | 1411 | self.mock(git_cl._GerritChangelistImpl, '_GerritCommitMsgHookCheck', |
| 1412 | lambda _, offer_removal: None) |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1413 | self.mock(git_cl.gclient_utils, 'RunEditor', |
| 1414 | lambda *_, **__: self._mocked_call(['RunEditor'])) |
| 1415 | self.mock(git_cl, 'DownloadGerritHook', self._mocked_call) |
| 1416 | |
Andrii Shyshkalov | e9c78ff | 2017-02-06 15:53:13 +0100 | [diff] [blame] | 1417 | original_os_path_isdir = os.path.isdir |
| 1418 | def selective_os_path_isdir_mock(path): |
| 1419 | if path == '/cache/this-dir-exists': |
| 1420 | return self._mocked_call('os.path.isdir', path) |
| 1421 | return original_os_path_isdir(path) |
| 1422 | self.mock(os.path, 'isdir', selective_os_path_isdir_mock) |
| 1423 | |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1424 | self.calls = self._gerrit_base_calls( |
| 1425 | issue=issue, |
Andrii Shyshkalov | 5c3d0b3 | 2017-02-16 17:47:31 +0100 | [diff] [blame] | 1426 | fetched_description=description, |
| 1427 | fetched_status=fetched_status) |
| 1428 | if fetched_status != 'ABANDONED': |
| 1429 | self.calls += self._gerrit_upload_calls( |
| 1430 | description, reviewers, squash, |
| 1431 | squash_mode=squash_mode, |
| 1432 | expected_upstream_ref=expected_upstream_ref, |
| 1433 | ref_suffix=ref_suffix, title=title, notify=notify, |
| 1434 | post_amend_description=post_amend_description, |
| 1435 | issue=issue, cc=cc, git_mirror=git_mirror) |
tandrii@chromium.org | 09d7a6a | 2016-03-04 15:44:48 +0000 | [diff] [blame] | 1436 | # Uncomment when debugging. |
| 1437 | # print '\n'.join(map(lambda x: '%2i: %s' % x, enumerate(self.calls))) |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1438 | git_cl.main(['upload'] + upload_args) |
| 1439 | |
sivachandra@chromium.org | aebe87f | 2012-10-22 20:34:21 +0000 | [diff] [blame] | 1440 | def test_gerrit_upload_without_change_id(self): |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1441 | self._run_gerrit_upload_test( |
| 1442 | ['--no-squash'], |
| 1443 | 'desc\n\nBUG=\n', |
| 1444 | [], |
| 1445 | squash=False, |
| 1446 | post_amend_description='desc\n\nBUG=\n\nChange-Id: Ixxx') |
| 1447 | |
| 1448 | def test_gerrit_upload_without_change_id_override_nosquash(self): |
tandrii@chromium.org | 1062500 | 2016-03-04 20:03:47 +0000 | [diff] [blame] | 1449 | self.mock(git_cl, 'DownloadGerritHook', self._mocked_call) |
sivachandra@chromium.org | aebe87f | 2012-10-22 20:34:21 +0000 | [diff] [blame] | 1450 | self._run_gerrit_upload_test( |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1451 | [], |
jamesr@chromium.org | 35d1a84 | 2012-07-27 00:20:43 +0000 | [diff] [blame] | 1452 | 'desc\n\nBUG=\n', |
tandrii@chromium.org | 1062500 | 2016-03-04 20:03:47 +0000 | [diff] [blame] | 1453 | [], |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1454 | squash=False, |
| 1455 | squash_mode='override_nosquash', |
tandrii@chromium.org | 1062500 | 2016-03-04 20:03:47 +0000 | [diff] [blame] | 1456 | post_amend_description='desc\n\nBUG=\n\nChange-Id: Ixxx') |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1457 | |
sivachandra@chromium.org | aebe87f | 2012-10-22 20:34:21 +0000 | [diff] [blame] | 1458 | def test_gerrit_no_reviewer(self): |
| 1459 | self._run_gerrit_upload_test( |
| 1460 | [], |
tandrii@chromium.org | 09d7a6a | 2016-03-04 15:44:48 +0000 | [diff] [blame] | 1461 | 'desc\n\nBUG=\n\nChange-Id: I123456789\n', |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1462 | [], |
| 1463 | squash=False, |
| 1464 | squash_mode='override_nosquash') |
sivachandra@chromium.org | aebe87f | 2012-10-22 20:34:21 +0000 | [diff] [blame] | 1465 | |
tandrii | eefe832 | 2016-08-17 10:12:24 -0700 | [diff] [blame] | 1466 | def test_gerrit_patch_bad_chars(self): |
| 1467 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
tandrii@chromium.org | bf766ba | 2016-04-13 12:51:23 +0000 | [diff] [blame] | 1468 | self._run_gerrit_upload_test( |
tandrii | eefe832 | 2016-08-17 10:12:24 -0700 | [diff] [blame] | 1469 | ['-f', '-t', 'Don\'t put bad cha,.rs'], |
tandrii@chromium.org | bf766ba | 2016-04-13 12:51:23 +0000 | [diff] [blame] | 1470 | 'desc\n\nBUG=\n\nChange-Id: I123456789', |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1471 | squash=False, |
| 1472 | squash_mode='override_nosquash', |
Aaron Gable | 9b713dd | 2016-12-14 16:04:21 -0800 | [diff] [blame] | 1473 | title='Dont_put_bad_chars') |
tandrii | eefe832 | 2016-08-17 10:12:24 -0700 | [diff] [blame] | 1474 | self.assertIn( |
| 1475 | 'WARNING: Patchset title may only contain alphanumeric chars ' |
Aaron Gable | eb3af71 | 2017-02-02 12:42:02 -0800 | [diff] [blame] | 1476 | 'and spaces. You can edit it in the UI. See https://crbug.com/663787.\n' |
| 1477 | 'Cleaned up title: Dont put bad chars\n', |
tandrii | eefe832 | 2016-08-17 10:12:24 -0700 | [diff] [blame] | 1478 | git_cl.sys.stdout.getvalue()) |
tandrii@chromium.org | bf766ba | 2016-04-13 12:51:23 +0000 | [diff] [blame] | 1479 | |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1480 | def test_gerrit_reviewers_cmd_line(self): |
sivachandra@chromium.org | aebe87f | 2012-10-22 20:34:21 +0000 | [diff] [blame] | 1481 | self._run_gerrit_upload_test( |
tandrii@chromium.org | 8da4540 | 2016-05-24 23:11:03 +0000 | [diff] [blame] | 1482 | ['-r', 'foo@example.com', '--send-mail'], |
tandrii@chromium.org | 09d7a6a | 2016-03-04 15:44:48 +0000 | [diff] [blame] | 1483 | 'desc\n\nBUG=\n\nChange-Id: I123456789', |
tandrii@chromium.org | 8da4540 | 2016-05-24 23:11:03 +0000 | [diff] [blame] | 1484 | ['foo@example.com'], |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1485 | squash=False, |
| 1486 | squash_mode='override_nosquash', |
tandrii@chromium.org | 8da4540 | 2016-05-24 23:11:03 +0000 | [diff] [blame] | 1487 | notify=True) |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1488 | |
| 1489 | def test_gerrit_reviewer_multiple(self): |
sivachandra@chromium.org | aebe87f | 2012-10-22 20:34:21 +0000 | [diff] [blame] | 1490 | self._run_gerrit_upload_test( |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1491 | [], |
bradnelson | d975b30 | 2016-10-23 12:20:23 -0700 | [diff] [blame] | 1492 | 'desc\nTBR=reviewer@example.com\nBUG=\nR=another@example.com\n' |
| 1493 | 'CC=more@example.com,people@example.com\n\n' |
tandrii@chromium.org | 09d7a6a | 2016-03-04 15:44:48 +0000 | [diff] [blame] | 1494 | 'Change-Id: 123456789\n', |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1495 | ['reviewer@example.com', 'another@example.com'], |
| 1496 | squash=False, |
tandrii | 99a72f2 | 2016-08-17 14:33:24 -0700 | [diff] [blame] | 1497 | squash_mode='override_nosquash', |
bradnelson | d975b30 | 2016-10-23 12:20:23 -0700 | [diff] [blame] | 1498 | ref_suffix='%l=Code-Review+1', |
| 1499 | cc=['more@example.com', 'people@example.com']) |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1500 | |
| 1501 | def test_gerrit_upload_squash_first_is_default(self): |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1502 | self._run_gerrit_upload_test( |
| 1503 | [], |
| 1504 | 'desc\nBUG=\n\nChange-Id: 123456789', |
| 1505 | [], |
| 1506 | expected_upstream_ref='origin/master') |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1507 | |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 1508 | def test_gerrit_upload_squash_first(self): |
bauerb@chromium.org | 27386dd | 2015-02-16 10:45:39 +0000 | [diff] [blame] | 1509 | self._run_gerrit_upload_test( |
| 1510 | ['--squash'], |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 1511 | 'desc\nBUG=\n\nChange-Id: 123456789', |
bauerb@chromium.org | 27386dd | 2015-02-16 10:45:39 +0000 | [diff] [blame] | 1512 | [], |
luqui@chromium.org | 609f395 | 2015-05-04 22:47:04 +0000 | [diff] [blame] | 1513 | squash=True, |
| 1514 | expected_upstream_ref='origin/master') |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1515 | |
Andrii Shyshkalov | e9c78ff | 2017-02-06 15:53:13 +0100 | [diff] [blame] | 1516 | def test_gerrit_upload_squash_first_with_mirror(self): |
| 1517 | self._run_gerrit_upload_test( |
| 1518 | ['--squash'], |
| 1519 | 'desc\nBUG=\n\nChange-Id: 123456789', |
| 1520 | [], |
| 1521 | squash=True, |
| 1522 | expected_upstream_ref='origin/master', |
| 1523 | git_mirror='https://chromium.googlesource.com/yyy/zzz') |
| 1524 | |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 1525 | def test_gerrit_upload_squash_reupload(self): |
| 1526 | description = 'desc\nBUG=\n\nChange-Id: 123456789' |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 1527 | self._run_gerrit_upload_test( |
| 1528 | ['--squash'], |
| 1529 | description, |
| 1530 | [], |
| 1531 | squash=True, |
| 1532 | expected_upstream_ref='origin/master', |
| 1533 | issue=123456) |
| 1534 | |
Andrii Shyshkalov | 5c3d0b3 | 2017-02-16 17:47:31 +0100 | [diff] [blame] | 1535 | def test_gerrit_upload_squash_reupload_to_abandoned(self): |
| 1536 | self.mock(git_cl, 'DieWithError', |
| 1537 | lambda msg, change=None: self._mocked_call('DieWithError', msg)) |
| 1538 | description = 'desc\nBUG=\n\nChange-Id: 123456789' |
| 1539 | with self.assertRaises(SystemExitMock): |
| 1540 | self._run_gerrit_upload_test( |
| 1541 | ['--squash'], |
| 1542 | description, |
| 1543 | [], |
| 1544 | squash=True, |
| 1545 | expected_upstream_ref='origin/master', |
| 1546 | issue=123456, |
| 1547 | fetched_status='ABANDONED') |
| 1548 | |
rmistry@google.com | 2dd9986 | 2015-06-22 12:22:18 +0000 | [diff] [blame] | 1549 | def test_upload_branch_deps(self): |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 1550 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
rmistry@google.com | 2dd9986 | 2015-06-22 12:22:18 +0000 | [diff] [blame] | 1551 | def mock_run_git(*args, **_kwargs): |
| 1552 | if args[0] == ['for-each-ref', |
| 1553 | '--format=%(refname:short) %(upstream:short)', |
| 1554 | 'refs/heads']: |
| 1555 | # Create a local branch dependency tree that looks like this: |
| 1556 | # test1 -> test2 -> test3 -> test4 -> test5 |
| 1557 | # -> test3.1 |
| 1558 | # test6 -> test0 |
| 1559 | branch_deps = [ |
| 1560 | 'test2 test1', # test1 -> test2 |
| 1561 | 'test3 test2', # test2 -> test3 |
| 1562 | 'test3.1 test2', # test2 -> test3.1 |
| 1563 | 'test4 test3', # test3 -> test4 |
| 1564 | 'test5 test4', # test4 -> test5 |
| 1565 | 'test6 test0', # test0 -> test6 |
| 1566 | 'test7', # test7 |
| 1567 | ] |
| 1568 | return '\n'.join(branch_deps) |
| 1569 | self.mock(git_cl, 'RunGit', mock_run_git) |
| 1570 | |
| 1571 | class RecordCalls: |
| 1572 | times_called = 0 |
| 1573 | record_calls = RecordCalls() |
| 1574 | def mock_CMDupload(*args, **_kwargs): |
| 1575 | record_calls.times_called += 1 |
| 1576 | return 0 |
| 1577 | self.mock(git_cl, 'CMDupload', mock_CMDupload) |
| 1578 | |
| 1579 | self.calls = [ |
Andrii Shyshkalov | abc26ac | 2017-03-14 14:49:38 +0100 | [diff] [blame] | 1580 | (('ask_for_data', 'This command will checkout all dependent branches ' |
| 1581 | 'and run "git cl upload". Press Enter to continue, ' |
| 1582 | 'or Ctrl+C to abort'), ''), |
| 1583 | ] |
rmistry@google.com | 2dd9986 | 2015-06-22 12:22:18 +0000 | [diff] [blame] | 1584 | |
| 1585 | class MockChangelist(): |
| 1586 | def __init__(self): |
| 1587 | pass |
| 1588 | def GetBranch(self): |
| 1589 | return 'test1' |
| 1590 | def GetIssue(self): |
| 1591 | return '123' |
| 1592 | def GetPatchset(self): |
| 1593 | return '1001' |
tandrii@chromium.org | 4c72b08 | 2016-03-31 22:26:35 +0000 | [diff] [blame] | 1594 | def IsGerrit(self): |
| 1595 | return False |
rmistry@google.com | 2dd9986 | 2015-06-22 12:22:18 +0000 | [diff] [blame] | 1596 | |
| 1597 | ret = git_cl.upload_branch_deps(MockChangelist(), []) |
| 1598 | # CMDupload should have been called 5 times because of 5 dependent branches. |
| 1599 | self.assertEquals(5, record_calls.times_called) |
| 1600 | self.assertEquals(0, ret) |
| 1601 | |
tandrii@chromium.org | 65874e1 | 2016-03-04 12:03:02 +0000 | [diff] [blame] | 1602 | def test_gerrit_change_id(self): |
| 1603 | self.calls = [ |
| 1604 | ((['git', 'write-tree'], ), |
| 1605 | 'hashtree'), |
| 1606 | ((['git', 'rev-parse', 'HEAD~0'], ), |
| 1607 | 'branch-parent'), |
| 1608 | ((['git', 'var', 'GIT_AUTHOR_IDENT'], ), |
| 1609 | 'A B <a@b.org> 1456848326 +0100'), |
| 1610 | ((['git', 'var', 'GIT_COMMITTER_IDENT'], ), |
| 1611 | 'C D <c@d.org> 1456858326 +0100'), |
| 1612 | ((['git', 'hash-object', '-t', 'commit', '--stdin'], ), |
| 1613 | 'hashchange'), |
| 1614 | ] |
| 1615 | change_id = git_cl.GenerateGerritChangeId('line1\nline2\n') |
| 1616 | self.assertEqual(change_id, 'Ihashchange') |
| 1617 | |
tandrii@chromium.org | 601e1d1 | 2016-06-03 13:03:54 +0000 | [diff] [blame] | 1618 | def test_desecription_append_footer(self): |
| 1619 | for init_desc, footer_line, expected_desc in [ |
| 1620 | # Use unique desc first lines for easy test failure identification. |
| 1621 | ('foo', 'R=one', 'foo\n\nR=one'), |
| 1622 | ('foo\n\nR=one', 'BUG=', 'foo\n\nR=one\nBUG='), |
| 1623 | ('foo\n\nR=one', 'Change-Id: Ixx', 'foo\n\nR=one\n\nChange-Id: Ixx'), |
| 1624 | ('foo\n\nChange-Id: Ixx', 'R=one', 'foo\n\nR=one\n\nChange-Id: Ixx'), |
| 1625 | ('foo\n\nR=one\n\nChange-Id: Ixx', 'TBR=two', |
| 1626 | 'foo\n\nR=one\nTBR=two\n\nChange-Id: Ixx'), |
| 1627 | ('foo\n\nR=one\n\nChange-Id: Ixx', 'Foo-Bar: baz', |
| 1628 | 'foo\n\nR=one\n\nChange-Id: Ixx\nFoo-Bar: baz'), |
| 1629 | ('foo\n\nChange-Id: Ixx', 'Foo-Bak: baz', |
| 1630 | 'foo\n\nChange-Id: Ixx\nFoo-Bak: baz'), |
| 1631 | ('foo', 'Change-Id: Ixx', 'foo\n\nChange-Id: Ixx'), |
| 1632 | ]: |
| 1633 | desc = git_cl.ChangeDescription(init_desc) |
| 1634 | desc.append_footer(footer_line) |
| 1635 | self.assertEqual(desc.description, expected_desc) |
| 1636 | |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 1637 | def test_update_reviewers(self): |
| 1638 | data = [ |
| 1639 | ('foo', [], 'foo'), |
agable@chromium.org | 42c2079 | 2013-09-12 17:34:49 +0000 | [diff] [blame] | 1640 | ('foo\nR=xx', [], 'foo\nR=xx'), |
| 1641 | ('foo\nTBR=xx', [], 'foo\nTBR=xx'), |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 1642 | ('foo', ['a@c'], 'foo\n\nR=a@c'), |
agable@chromium.org | 42c2079 | 2013-09-12 17:34:49 +0000 | [diff] [blame] | 1643 | ('foo\nR=xx', ['a@c'], 'foo\n\nR=a@c, xx'), |
| 1644 | ('foo\nTBR=xx', ['a@c'], 'foo\n\nR=a@c\nTBR=xx'), |
| 1645 | ('foo\nTBR=xx\nR=yy', ['a@c'], 'foo\n\nR=a@c, yy\nTBR=xx'), |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 1646 | ('foo\nBUG=', ['a@c'], 'foo\nBUG=\nR=a@c'), |
agable@chromium.org | 42c2079 | 2013-09-12 17:34:49 +0000 | [diff] [blame] | 1647 | ('foo\nR=xx\nTBR=yy\nR=bar', ['a@c'], 'foo\n\nR=a@c, xx, bar\nTBR=yy'), |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 1648 | ('foo', ['a@c', 'b@c'], 'foo\n\nR=a@c, b@c'), |
maruel@chromium.org | c6f60e8 | 2013-04-19 17:01:57 +0000 | [diff] [blame] | 1649 | ('foo\nBar\n\nR=\nBUG=', ['c@c'], 'foo\nBar\n\nR=c@c\nBUG='), |
| 1650 | ('foo\nBar\n\nR=\nBUG=\nR=', ['c@c'], 'foo\nBar\n\nR=c@c\nBUG='), |
| 1651 | # Same as the line before, but full of whitespaces. |
| 1652 | ( |
| 1653 | 'foo\nBar\n\n R = \n BUG = \n R = ', ['c@c'], |
| 1654 | 'foo\nBar\n\nR=c@c\n BUG =', |
| 1655 | ), |
| 1656 | # Whitespaces aren't interpreted as new lines. |
| 1657 | ('foo BUG=allo R=joe ', ['c@c'], 'foo BUG=allo R=joe\n\nR=c@c'), |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 1658 | ] |
maruel@chromium.org | c6f60e8 | 2013-04-19 17:01:57 +0000 | [diff] [blame] | 1659 | expected = [i[2] for i in data] |
| 1660 | actual = [] |
| 1661 | for orig, reviewers, _expected in data: |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 1662 | obj = git_cl.ChangeDescription(orig) |
| 1663 | obj.update_reviewers(reviewers) |
maruel@chromium.org | c6f60e8 | 2013-04-19 17:01:57 +0000 | [diff] [blame] | 1664 | actual.append(obj.description) |
| 1665 | self.assertEqual(expected, actual) |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 1666 | |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 1667 | def test_get_target_ref(self): |
| 1668 | # Check remote or remote branch not present. |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1669 | self.assertEqual(None, git_cl.GetTargetRef('origin', None, 'master')) |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 1670 | self.assertEqual(None, git_cl.GetTargetRef(None, |
| 1671 | 'refs/remotes/origin/master', |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1672 | 'master')) |
bauerb@chromium.org | 27386dd | 2015-02-16 10:45:39 +0000 | [diff] [blame] | 1673 | |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 1674 | # Check default target refs for branches. |
| 1675 | self.assertEqual('refs/heads/master', |
| 1676 | git_cl.GetTargetRef('origin', 'refs/remotes/origin/master', |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1677 | None)) |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 1678 | self.assertEqual('refs/heads/master', |
| 1679 | git_cl.GetTargetRef('origin', 'refs/remotes/origin/lkgr', |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1680 | None)) |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 1681 | self.assertEqual('refs/heads/master', |
| 1682 | git_cl.GetTargetRef('origin', 'refs/remotes/origin/lkcr', |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1683 | None)) |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 1684 | self.assertEqual('refs/branch-heads/123', |
| 1685 | git_cl.GetTargetRef('origin', |
| 1686 | 'refs/remotes/branch-heads/123', |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1687 | None)) |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 1688 | self.assertEqual('refs/diff/test', |
| 1689 | git_cl.GetTargetRef('origin', |
| 1690 | 'refs/remotes/origin/refs/diff/test', |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1691 | None)) |
rmistry@google.com | c68112d | 2015-03-03 12:48:06 +0000 | [diff] [blame] | 1692 | self.assertEqual('refs/heads/chrome/m42', |
| 1693 | git_cl.GetTargetRef('origin', |
| 1694 | 'refs/remotes/origin/chrome/m42', |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1695 | None)) |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 1696 | |
| 1697 | # Check target refs for user-specified target branch. |
| 1698 | for branch in ('branch-heads/123', 'remotes/branch-heads/123', |
| 1699 | 'refs/remotes/branch-heads/123'): |
| 1700 | self.assertEqual('refs/branch-heads/123', |
| 1701 | git_cl.GetTargetRef('origin', |
| 1702 | 'refs/remotes/origin/master', |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1703 | branch)) |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 1704 | for branch in ('origin/master', 'remotes/origin/master', |
| 1705 | 'refs/remotes/origin/master'): |
| 1706 | self.assertEqual('refs/heads/master', |
| 1707 | git_cl.GetTargetRef('origin', |
| 1708 | 'refs/remotes/branch-heads/123', |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1709 | branch)) |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 1710 | for branch in ('master', 'heads/master', 'refs/heads/master'): |
| 1711 | self.assertEqual('refs/heads/master', |
| 1712 | git_cl.GetTargetRef('origin', |
| 1713 | 'refs/remotes/branch-heads/123', |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1714 | branch)) |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 1715 | |
wychen@chromium.org | a872e75 | 2015-04-28 23:42:18 +0000 | [diff] [blame] | 1716 | def test_patch_when_dirty(self): |
| 1717 | # Patch when local tree is dirty |
| 1718 | self.mock(git_common, 'is_dirty_git_tree', lambda x: True) |
| 1719 | self.assertNotEqual(git_cl.main(['patch', '123456']), 0) |
| 1720 | |
| 1721 | def test_diff_when_dirty(self): |
| 1722 | # Do 'git cl diff' when local tree is dirty |
| 1723 | self.mock(git_common, 'is_dirty_git_tree', lambda x: True) |
| 1724 | self.assertNotEqual(git_cl.main(['diff']), 0) |
| 1725 | |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1726 | @staticmethod |
| 1727 | def _get_gerrit_codereview_server_calls(branch, value=None, |
| 1728 | git_short_host='host', |
| 1729 | detect_branch=True): |
| 1730 | """Returns calls executed by _GerritChangelistImpl.GetCodereviewServer. |
| 1731 | |
| 1732 | If value is given, branch.<BRANCH>.gerritcodereview is already set. |
| 1733 | """ |
| 1734 | calls = [] |
| 1735 | if detect_branch: |
| 1736 | calls.append(((['git', 'symbolic-ref', 'HEAD'],), branch)) |
| 1737 | calls.append(((['git', 'config', 'branch.' + branch + '.gerritserver'],), |
| 1738 | CERR1 if value is None else value)) |
| 1739 | if value is None: |
| 1740 | calls += [ |
| 1741 | ((['git', 'config', 'branch.' + branch + '.merge'],), |
| 1742 | 'refs/heads' + branch), |
| 1743 | ((['git', 'config', 'branch.' + branch + '.remote'],), |
| 1744 | 'origin'), |
| 1745 | ((['git', 'config', 'remote.origin.url'],), |
| 1746 | 'https://%s.googlesource.com/my/repo' % git_short_host), |
| 1747 | ] |
| 1748 | return calls |
| 1749 | |
tandrii | df09a46 | 2016-08-18 16:23:55 -0700 | [diff] [blame] | 1750 | def _patch_common(self, is_gerrit=False, force_codereview=False, |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1751 | new_branch=False, git_short_host='host', |
| 1752 | detect_gerrit_server=True): |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 1753 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 1754 | self.mock(git_cl._RietveldChangelistImpl, 'GetMostRecentPatchset', |
| 1755 | lambda x: '60001') |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1756 | self.mock(git_cl._RietveldChangelistImpl, 'FetchDescription', |
Kenneth Russell | 61e2ed4 | 2017-02-15 11:47:13 -0800 | [diff] [blame] | 1757 | lambda *a, **kw: 'Description') |
wychen@chromium.org | a872e75 | 2015-04-28 23:42:18 +0000 | [diff] [blame] | 1758 | self.mock(git_cl, 'IsGitVersionAtLeast', lambda *args: True) |
| 1759 | |
tandrii | df09a46 | 2016-08-18 16:23:55 -0700 | [diff] [blame] | 1760 | if new_branch: |
| 1761 | self.calls = [((['git', 'new-branch', 'master'],), ''),] |
| 1762 | else: |
| 1763 | self.calls = [((['git', 'symbolic-ref', 'HEAD'],), 'master')] |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1764 | |
tandrii@chromium.org | dde6462 | 2016-04-13 17:11:21 +0000 | [diff] [blame] | 1765 | if not force_codereview: |
| 1766 | # These calls detect codereview to use. |
tandrii@chromium.org | c2786d9 | 2016-05-31 19:53:50 +0000 | [diff] [blame] | 1767 | self.calls += [ |
tandrii@chromium.org | dde6462 | 2016-04-13 17:11:21 +0000 | [diff] [blame] | 1768 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 1769 | ((['git', 'config', 'branch.master.rietveldissue'],), CERR1), |
| 1770 | ((['git', 'config', 'branch.master.gerritissue'],), CERR1), |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 1771 | ((['git', 'config', 'rietveld.autoupdate'],), CERR1), |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1772 | ] |
tandrii@chromium.org | dde6462 | 2016-04-13 17:11:21 +0000 | [diff] [blame] | 1773 | |
| 1774 | if is_gerrit: |
| 1775 | if not force_codereview: |
| 1776 | self.calls += [ |
| 1777 | ((['git', 'config', 'gerrit.host'],), 'true'), |
| 1778 | ] |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1779 | if detect_gerrit_server: |
| 1780 | self.calls += self._get_gerrit_codereview_server_calls( |
| 1781 | 'master', git_short_host=git_short_host, |
| 1782 | detect_branch=not new_branch and force_codereview) |
| 1783 | |
| 1784 | self.calls += [ |
| 1785 | (('GetChangeDetail', git_short_host + '-review.googlesource.com', |
Andrii Shyshkalov | 8039be7 | 2017-01-26 09:38:18 +0100 | [diff] [blame] | 1786 | '123456', ['ALL_REVISIONS', 'CURRENT_COMMIT']), |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1787 | { |
| 1788 | 'current_revision': '7777777777', |
| 1789 | 'revisions': { |
| 1790 | '1111111111': { |
| 1791 | '_number': 1, |
| 1792 | 'fetch': {'http': { |
| 1793 | 'url': 'https://%s.googlesource.com/my/repo' % git_short_host, |
| 1794 | 'ref': 'refs/changes/56/123456/1', |
| 1795 | }}, |
| 1796 | }, |
| 1797 | '7777777777': { |
| 1798 | '_number': 7, |
| 1799 | 'fetch': {'http': { |
| 1800 | 'url': 'https://%s.googlesource.com/my/repo' % git_short_host, |
| 1801 | 'ref': 'refs/changes/56/123456/7', |
| 1802 | }}, |
| 1803 | }, |
| 1804 | }, |
| 1805 | }), |
| 1806 | ] |
tandrii@chromium.org | dde6462 | 2016-04-13 17:11:21 +0000 | [diff] [blame] | 1807 | else: |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1808 | self.calls += [ |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 1809 | ((['git', 'config', 'gerrit.host'],), CERR1), |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1810 | ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'), |
skobes | 6468b90 | 2016-10-24 08:45:10 -0700 | [diff] [blame] | 1811 | ((['git', 'config', 'branch.master.rietveldserver',],), CERR1), |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1812 | ((['git', 'rev-parse', '--show-cdup'],), ''), |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1813 | ] |
wychen@chromium.org | a872e75 | 2015-04-28 23:42:18 +0000 | [diff] [blame] | 1814 | |
tandrii | df09a46 | 2016-08-18 16:23:55 -0700 | [diff] [blame] | 1815 | def _common_patch_successful(self, new_branch=False): |
| 1816 | self._patch_common(new_branch=new_branch) |
wychen@chromium.org | a872e75 | 2015-04-28 23:42:18 +0000 | [diff] [blame] | 1817 | self.calls += [ |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 1818 | ((['git', 'config', 'branch.master.rietveldissue', '123456'],), |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 1819 | ''), |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1820 | ((['git', 'config', 'branch.master.rietveldserver', |
| 1821 | 'https://codereview.example.com'],), ''), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 1822 | ((['git', 'config', 'branch.master.rietveldpatchset', '60001'],), |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 1823 | ''), |
Aaron Gable | d343c63 | 2017-03-15 11:02:26 -0700 | [diff] [blame] | 1824 | ((['git', 'commit', '-m', |
| 1825 | 'Description\n\n' + |
| 1826 | 'patch from issue 123456 at patchset 60001 ' + |
| 1827 | '(http://crrev.com/123456#ps60001)'],), ''), |
wychen@chromium.org | a872e75 | 2015-04-28 23:42:18 +0000 | [diff] [blame] | 1828 | ] |
tandrii@chromium.org | c2786d9 | 2016-05-31 19:53:50 +0000 | [diff] [blame] | 1829 | |
| 1830 | def test_patch_successful(self): |
| 1831 | self._common_patch_successful() |
wychen@chromium.org | a872e75 | 2015-04-28 23:42:18 +0000 | [diff] [blame] | 1832 | self.assertEqual(git_cl.main(['patch', '123456']), 0) |
| 1833 | |
tandrii@chromium.org | c2786d9 | 2016-05-31 19:53:50 +0000 | [diff] [blame] | 1834 | def test_patch_successful_new_branch(self): |
tandrii | df09a46 | 2016-08-18 16:23:55 -0700 | [diff] [blame] | 1835 | self._common_patch_successful(new_branch=True) |
tandrii@chromium.org | c2786d9 | 2016-05-31 19:53:50 +0000 | [diff] [blame] | 1836 | self.assertEqual(git_cl.main(['patch', '-b', 'master', '123456']), 0) |
| 1837 | |
wychen@chromium.org | a872e75 | 2015-04-28 23:42:18 +0000 | [diff] [blame] | 1838 | def test_patch_conflict(self): |
| 1839 | self._patch_common() |
skobes | 6468b90 | 2016-10-24 08:45:10 -0700 | [diff] [blame] | 1840 | GitCheckoutMock.conflict = True |
wychen@chromium.org | a872e75 | 2015-04-28 23:42:18 +0000 | [diff] [blame] | 1841 | self.assertNotEqual(git_cl.main(['patch', '123456']), 0) |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 1842 | |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1843 | def test_gerrit_patch_successful(self): |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1844 | self._patch_common(is_gerrit=True, git_short_host='chromium', |
| 1845 | detect_gerrit_server=True) |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1846 | self.calls += [ |
| 1847 | ((['git', 'fetch', 'https://chromium.googlesource.com/my/repo', |
| 1848 | 'refs/changes/56/123456/7'],), ''), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 1849 | ((['git', 'config', 'branch.master.gerritissue', '123456'],), |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 1850 | ''), |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1851 | ((['git', 'config', 'branch.master.gerritserver', |
| 1852 | 'https://chromium-review.googlesource.com'],), ''), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 1853 | ((['git', 'config', 'branch.master.gerritpatchset', '7'],), ''), |
Aaron Gable | d343c63 | 2017-03-15 11:02:26 -0700 | [diff] [blame] | 1854 | ((['git', 'cherry-pick', 'FETCH_HEAD'],), ''), |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1855 | ] |
| 1856 | self.assertEqual(git_cl.main(['patch', '123456']), 0) |
| 1857 | |
tandrii@chromium.org | dde6462 | 2016-04-13 17:11:21 +0000 | [diff] [blame] | 1858 | def test_patch_force_codereview(self): |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1859 | self._patch_common(is_gerrit=True, force_codereview=True, |
| 1860 | git_short_host='host', detect_gerrit_server=True) |
tandrii@chromium.org | dde6462 | 2016-04-13 17:11:21 +0000 | [diff] [blame] | 1861 | self.calls += [ |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1862 | ((['git', 'fetch', 'https://host.googlesource.com/my/repo', |
tandrii@chromium.org | dde6462 | 2016-04-13 17:11:21 +0000 | [diff] [blame] | 1863 | 'refs/changes/56/123456/7'],), ''), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 1864 | ((['git', 'config', 'branch.master.gerritissue', '123456'],), |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 1865 | ''), |
tandrii@chromium.org | dde6462 | 2016-04-13 17:11:21 +0000 | [diff] [blame] | 1866 | ((['git', 'config', 'branch.master.gerritserver', |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1867 | 'https://host-review.googlesource.com'],), ''), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 1868 | ((['git', 'config', 'branch.master.gerritpatchset', '7'],), ''), |
Aaron Gable | d343c63 | 2017-03-15 11:02:26 -0700 | [diff] [blame] | 1869 | ((['git', 'cherry-pick', 'FETCH_HEAD'],), ''), |
tandrii@chromium.org | dde6462 | 2016-04-13 17:11:21 +0000 | [diff] [blame] | 1870 | ] |
| 1871 | self.assertEqual(git_cl.main(['patch', '--gerrit', '123456']), 0) |
| 1872 | |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1873 | def test_gerrit_patch_url_successful(self): |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1874 | self._patch_common(is_gerrit=True, git_short_host='else', |
| 1875 | detect_gerrit_server=False) |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1876 | self.calls += [ |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1877 | ((['git', 'fetch', 'https://else.googlesource.com/my/repo', |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1878 | 'refs/changes/56/123456/1'],), ''), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 1879 | ((['git', 'config', 'branch.master.gerritissue', '123456'],), |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 1880 | ''), |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1881 | ((['git', 'config', 'branch.master.gerritserver', |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1882 | 'https://else-review.googlesource.com'],), ''), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 1883 | ((['git', 'config', 'branch.master.gerritpatchset', '1'],), ''), |
Aaron Gable | d343c63 | 2017-03-15 11:02:26 -0700 | [diff] [blame] | 1884 | ((['git', 'cherry-pick', 'FETCH_HEAD'],), ''), |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1885 | ] |
| 1886 | self.assertEqual(git_cl.main( |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1887 | ['patch', 'https://else-review.googlesource.com/#/c/123456/1']), 0) |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1888 | |
| 1889 | def test_gerrit_patch_conflict(self): |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1890 | self._patch_common(is_gerrit=True, detect_gerrit_server=False, |
| 1891 | git_short_host='chromium') |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1892 | self.calls += [ |
| 1893 | ((['git', 'fetch', 'https://chromium.googlesource.com/my/repo', |
| 1894 | 'refs/changes/56/123456/1'],), ''), |
Aaron Gable | d343c63 | 2017-03-15 11:02:26 -0700 | [diff] [blame] | 1895 | ((['git', 'config', 'branch.master.gerritissue', '123456'],), |
| 1896 | ''), |
| 1897 | ((['git', 'config', 'branch.master.gerritserver', |
| 1898 | 'https://chromium-review.googlesource.com'],), ''), |
| 1899 | ((['git', 'config', 'branch.master.gerritpatchset', '1'],), ''), |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 1900 | ((['git', 'cherry-pick', 'FETCH_HEAD'],), CERR1), |
| 1901 | ((['DieWithError', 'Command "git cherry-pick FETCH_HEAD" failed.\n'],), |
| 1902 | SystemExitMock()), |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1903 | ] |
| 1904 | with self.assertRaises(SystemExitMock): |
| 1905 | git_cl.main(['patch', |
| 1906 | 'https://chromium-review.googlesource.com/#/c/123456/1']) |
| 1907 | |
tandrii | c2405f5 | 2016-10-10 08:13:15 -0700 | [diff] [blame] | 1908 | def test_gerrit_patch_not_exists(self): |
Andrii Shyshkalov | c6c8b4c | 2016-11-09 20:51:20 +0100 | [diff] [blame] | 1909 | def notExists(_issue, *_, **kwargs): |
| 1910 | self.assertFalse(kwargs['ignore_404']) |
| 1911 | raise git_cl.gerrit_util.GerritError(404, '') |
| 1912 | self.mock(git_cl.gerrit_util, 'GetChangeDetail', notExists) |
| 1913 | |
tandrii | c2405f5 | 2016-10-10 08:13:15 -0700 | [diff] [blame] | 1914 | url = 'https://chromium-review.googlesource.com' |
tandrii | c2405f5 | 2016-10-10 08:13:15 -0700 | [diff] [blame] | 1915 | self.calls = [ |
| 1916 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
| 1917 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
| 1918 | ((['git', 'config', 'branch.master.rietveldissue'],), CERR1), |
| 1919 | ((['git', 'config', 'branch.master.gerritissue'],), CERR1), |
| 1920 | ((['git', 'config', 'rietveld.autoupdate'],), CERR1), |
| 1921 | ((['git', 'config', 'gerrit.host'],), 'true'), |
Aaron Gable | a45ee11 | 2016-11-22 15:14:38 -0800 | [diff] [blame] | 1922 | ((['DieWithError', 'change 123456 at ' + url + ' does not exist ' |
tandrii | c2405f5 | 2016-10-10 08:13:15 -0700 | [diff] [blame] | 1923 | 'or you have no access to it'],), SystemExitMock()), |
| 1924 | ] |
| 1925 | with self.assertRaises(SystemExitMock): |
| 1926 | self.assertEqual(1, git_cl.main(['patch', url + '/#/c/123456/1'])) |
| 1927 | |
tandrii@chromium.org | 5df290f | 2016-04-11 16:12:29 +0000 | [diff] [blame] | 1928 | def _checkout_calls(self): |
| 1929 | return [ |
| 1930 | ((['git', 'config', '--local', '--get-regexp', |
| 1931 | 'branch\\..*\\.rietveldissue'], ), |
| 1932 | ('branch.retrying.rietveldissue 1111111111\n' |
| 1933 | 'branch.some-fix.rietveldissue 2222222222\n')), |
| 1934 | ((['git', 'config', '--local', '--get-regexp', |
| 1935 | 'branch\\..*\\.gerritissue'], ), |
| 1936 | ('branch.ger-branch.gerritissue 123456\n' |
| 1937 | 'branch.gbranch654.gerritissue 654321\n')), |
| 1938 | ] |
| 1939 | |
| 1940 | def test_checkout_gerrit(self): |
| 1941 | """Tests git cl checkout <issue>.""" |
| 1942 | self.calls = self._checkout_calls() |
| 1943 | self.calls += [((['git', 'checkout', 'ger-branch'], ), '')] |
| 1944 | self.assertEqual(0, git_cl.main(['checkout', '123456'])) |
| 1945 | |
| 1946 | def test_checkout_rietveld(self): |
| 1947 | """Tests git cl checkout <issue>.""" |
| 1948 | self.calls = self._checkout_calls() |
| 1949 | self.calls += [((['git', 'checkout', 'some-fix'], ), '')] |
| 1950 | self.assertEqual(0, git_cl.main(['checkout', '2222222222'])) |
| 1951 | |
| 1952 | def test_checkout_not_found(self): |
| 1953 | """Tests git cl checkout <issue>.""" |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 1954 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
tandrii@chromium.org | 5df290f | 2016-04-11 16:12:29 +0000 | [diff] [blame] | 1955 | self.calls = self._checkout_calls() |
| 1956 | self.assertEqual(1, git_cl.main(['checkout', '99999'])) |
| 1957 | |
tandrii@chromium.org | 26c8fd2 | 2016-04-11 21:33:21 +0000 | [diff] [blame] | 1958 | def test_checkout_no_branch_issues(self): |
| 1959 | """Tests git cl checkout <issue>.""" |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 1960 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
tandrii@chromium.org | 26c8fd2 | 2016-04-11 21:33:21 +0000 | [diff] [blame] | 1961 | self.calls = [ |
| 1962 | ((['git', 'config', '--local', '--get-regexp', |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 1963 | 'branch\\..*\\.rietveldissue'], ), CERR1), |
tandrii@chromium.org | 26c8fd2 | 2016-04-11 21:33:21 +0000 | [diff] [blame] | 1964 | ((['git', 'config', '--local', '--get-regexp', |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 1965 | 'branch\\..*\\.gerritissue'], ), CERR1), |
tandrii@chromium.org | 26c8fd2 | 2016-04-11 21:33:21 +0000 | [diff] [blame] | 1966 | ] |
| 1967 | self.assertEqual(1, git_cl.main(['checkout', '99999'])) |
| 1968 | |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 1969 | def _test_gerrit_ensure_authenticated_common(self, auth, |
| 1970 | skip_auth_check=False): |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 1971 | self.mock(git_cl.gerrit_util, 'CookiesAuthenticator', |
| 1972 | CookiesAuthenticatorMockFactory(hosts_with_creds=auth)) |
| 1973 | self.mock(git_cl, 'DieWithError', |
Christopher Lam | f732cd5 | 2017-01-24 12:40:11 +1100 | [diff] [blame] | 1974 | lambda msg, change=None: self._mocked_call(['DieWithError', msg])) |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 1975 | self.calls = self._gerrit_ensure_auth_calls(skip_auth_check=skip_auth_check) |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 1976 | cl = git_cl.Changelist(codereview='gerrit') |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 1977 | cl.branch = 'master' |
| 1978 | cl.branchref = 'refs/heads/master' |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 1979 | cl.lookedup_issue = True |
| 1980 | return cl |
| 1981 | |
| 1982 | def test_gerrit_ensure_authenticated_missing(self): |
| 1983 | cl = self._test_gerrit_ensure_authenticated_common(auth={ |
| 1984 | 'chromium.googlesource.com': 'git is ok, but gerrit one is missing', |
| 1985 | }) |
| 1986 | self.calls.append( |
| 1987 | ((['DieWithError', |
| 1988 | 'Credentials for the following hosts are required:\n' |
| 1989 | ' chromium-review.googlesource.com\n' |
| 1990 | 'These are read from ~/.gitcookies (or legacy ~/.netrc)\n' |
| 1991 | 'You can (re)generate your credentails by visiting ' |
| 1992 | 'https://chromium-review.googlesource.com/new-password'],), ''),) |
| 1993 | self.assertIsNone(cl.EnsureAuthenticated(force=False)) |
| 1994 | |
| 1995 | def test_gerrit_ensure_authenticated_conflict(self): |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 1996 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 1997 | cl = self._test_gerrit_ensure_authenticated_common(auth={ |
| 1998 | 'chromium.googlesource.com': 'one', |
| 1999 | 'chromium-review.googlesource.com': 'other', |
| 2000 | }) |
| 2001 | self.calls.append( |
Andrii Shyshkalov | abc26ac | 2017-03-14 14:49:38 +0100 | [diff] [blame] | 2002 | (('ask_for_data', 'If you know what you are doing ' |
| 2003 | 'press Enter to continue, or Ctrl+C to abort'), '')) |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 2004 | self.assertIsNone(cl.EnsureAuthenticated(force=False)) |
| 2005 | |
| 2006 | def test_gerrit_ensure_authenticated_ok(self): |
| 2007 | cl = self._test_gerrit_ensure_authenticated_common(auth={ |
| 2008 | 'chromium.googlesource.com': 'same', |
| 2009 | 'chromium-review.googlesource.com': 'same', |
| 2010 | }) |
| 2011 | self.assertIsNone(cl.EnsureAuthenticated(force=False)) |
| 2012 | |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 2013 | def test_gerrit_ensure_authenticated_skipped(self): |
| 2014 | cl = self._test_gerrit_ensure_authenticated_common( |
| 2015 | auth={}, skip_auth_check=True) |
| 2016 | self.assertIsNone(cl.EnsureAuthenticated(force=False)) |
| 2017 | |
tandrii@chromium.org | fa330e8 | 2016-04-13 17:09:52 +0000 | [diff] [blame] | 2018 | def test_cmd_set_commit_rietveld(self): |
tandrii | 4d84359 | 2016-07-27 08:22:56 -0700 | [diff] [blame] | 2019 | self.mock(git_cl._RietveldChangelistImpl, 'SetFlags', |
| 2020 | lambda _, v: self._mocked_call(['SetFlags', v])) |
tandrii@chromium.org | fa330e8 | 2016-04-13 17:09:52 +0000 | [diff] [blame] | 2021 | self.calls = [ |
| 2022 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 2023 | ((['git', 'config', 'branch.feature.rietveldissue'],), '123'), |
tandrii@chromium.org | fa330e8 | 2016-04-13 17:09:52 +0000 | [diff] [blame] | 2024 | ((['git', 'config', 'rietveld.autoupdate'],), ''), |
| 2025 | ((['git', 'config', 'rietveld.server'],), ''), |
| 2026 | ((['git', 'config', 'rietveld.server'],), ''), |
| 2027 | ((['git', 'config', 'branch.feature.rietveldserver'],), |
| 2028 | 'https://codereview.chromium.org'), |
tandrii | 4d84359 | 2016-07-27 08:22:56 -0700 | [diff] [blame] | 2029 | ((['SetFlags', {'commit': '1', 'cq_dry_run': '0'}], ), ''), |
tandrii@chromium.org | fa330e8 | 2016-04-13 17:09:52 +0000 | [diff] [blame] | 2030 | ] |
| 2031 | self.assertEqual(0, git_cl.main(['set-commit'])) |
| 2032 | |
Andrii Shyshkalov | 828701b | 2016-12-09 10:46:47 +0100 | [diff] [blame] | 2033 | def _cmd_set_commit_gerrit_common(self, vote, notify=None): |
tandrii@chromium.org | fa330e8 | 2016-04-13 17:09:52 +0000 | [diff] [blame] | 2034 | self.mock(git_cl.gerrit_util, 'SetReview', |
Andrii Shyshkalov | 828701b | 2016-12-09 10:46:47 +0100 | [diff] [blame] | 2035 | lambda h, i, labels, notify=None: |
| 2036 | self._mocked_call(['SetReview', h, i, labels, notify])) |
| 2037 | |
tandrii@chromium.org | fa330e8 | 2016-04-13 17:09:52 +0000 | [diff] [blame] | 2038 | self.calls = [ |
| 2039 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 2040 | ((['git', 'config', 'branch.feature.rietveldissue'],), CERR1), |
| 2041 | ((['git', 'config', 'branch.feature.gerritissue'],), '123'), |
tandrii@chromium.org | fa330e8 | 2016-04-13 17:09:52 +0000 | [diff] [blame] | 2042 | ((['git', 'config', 'branch.feature.gerritserver'],), |
| 2043 | 'https://chromium-review.googlesource.com'), |
| 2044 | ((['SetReview', 'chromium-review.googlesource.com', 123, |
Andrii Shyshkalov | 828701b | 2016-12-09 10:46:47 +0100 | [diff] [blame] | 2045 | {'Commit-Queue': vote}, notify],), ''), |
tandrii@chromium.org | fa330e8 | 2016-04-13 17:09:52 +0000 | [diff] [blame] | 2046 | ] |
tandrii | d9e5ce5 | 2016-07-13 02:32:59 -0700 | [diff] [blame] | 2047 | |
| 2048 | def test_cmd_set_commit_gerrit_clear(self): |
| 2049 | self._cmd_set_commit_gerrit_common(0) |
| 2050 | self.assertEqual(0, git_cl.main(['set-commit', '-c'])) |
| 2051 | |
| 2052 | def test_cmd_set_commit_gerrit_dry(self): |
Andrii Shyshkalov | 828701b | 2016-12-09 10:46:47 +0100 | [diff] [blame] | 2053 | self._cmd_set_commit_gerrit_common(1, notify='NONE') |
tandrii@chromium.org | fa330e8 | 2016-04-13 17:09:52 +0000 | [diff] [blame] | 2054 | self.assertEqual(0, git_cl.main(['set-commit', '-d'])) |
| 2055 | |
tandrii | d9e5ce5 | 2016-07-13 02:32:59 -0700 | [diff] [blame] | 2056 | def test_cmd_set_commit_gerrit(self): |
| 2057 | self._cmd_set_commit_gerrit_common(2) |
| 2058 | self.assertEqual(0, git_cl.main(['set-commit'])) |
| 2059 | |
martiniss@chromium.org | 2b55fe3 | 2016-04-26 20:28:54 +0000 | [diff] [blame] | 2060 | def test_description_display(self): |
| 2061 | out = StringIO.StringIO() |
| 2062 | self.mock(git_cl.sys, 'stdout', out) |
| 2063 | |
martiniss@chromium.org | d6648e2 | 2016-04-29 19:22:16 +0000 | [diff] [blame] | 2064 | self.mock(git_cl, 'Changelist', ChangelistMock) |
| 2065 | ChangelistMock.desc = 'foo\n' |
martiniss@chromium.org | 2b55fe3 | 2016-04-26 20:28:54 +0000 | [diff] [blame] | 2066 | |
| 2067 | self.assertEqual(0, git_cl.main(['description', '-d'])) |
| 2068 | self.assertEqual('foo\n', out.getvalue()) |
| 2069 | |
| 2070 | def test_description_rietveld(self): |
| 2071 | out = StringIO.StringIO() |
| 2072 | self.mock(git_cl.sys, 'stdout', out) |
martiniss | 6eda05f | 2016-06-30 10:18:35 -0700 | [diff] [blame] | 2073 | self.mock(git_cl.Changelist, 'GetDescription', lambda *args: 'foobar') |
martiniss@chromium.org | 2b55fe3 | 2016-04-26 20:28:54 +0000 | [diff] [blame] | 2074 | |
martiniss@chromium.org | 2b55fe3 | 2016-04-26 20:28:54 +0000 | [diff] [blame] | 2075 | self.assertEqual(0, git_cl.main([ |
| 2076 | 'description', 'https://code.review.org/123123', '-d', '--rietveld'])) |
| 2077 | self.assertEqual('foobar\n', out.getvalue()) |
| 2078 | |
iannucci | 3c972b9 | 2016-08-17 13:24:10 -0700 | [diff] [blame] | 2079 | def test_StatusFieldOverrideIssueMissingArgs(self): |
| 2080 | out = StringIO.StringIO() |
| 2081 | self.mock(git_cl.sys, 'stderr', out) |
| 2082 | |
| 2083 | try: |
| 2084 | self.assertEqual(git_cl.main(['status', '--issue', '1']), 0) |
| 2085 | except SystemExit as ex: |
| 2086 | self.assertEqual(ex.code, 2) |
iannucci | e53c935 | 2016-08-17 14:40:40 -0700 | [diff] [blame] | 2087 | self.assertRegexpMatches(out.getvalue(), r'--issue must be specified') |
iannucci | 3c972b9 | 2016-08-17 13:24:10 -0700 | [diff] [blame] | 2088 | |
| 2089 | out = StringIO.StringIO() |
| 2090 | self.mock(git_cl.sys, 'stderr', out) |
| 2091 | |
| 2092 | try: |
| 2093 | self.assertEqual(git_cl.main(['status', '--issue', '1', '--rietveld']), 0) |
| 2094 | except SystemExit as ex: |
| 2095 | self.assertEqual(ex.code, 2) |
iannucci | e53c935 | 2016-08-17 14:40:40 -0700 | [diff] [blame] | 2096 | self.assertRegexpMatches(out.getvalue(), r'--field must be specified') |
iannucci | 3c972b9 | 2016-08-17 13:24:10 -0700 | [diff] [blame] | 2097 | |
| 2098 | def test_StatusFieldOverrideIssue(self): |
| 2099 | out = StringIO.StringIO() |
| 2100 | self.mock(git_cl.sys, 'stdout', out) |
| 2101 | |
| 2102 | def assertIssue(cl_self, *_args): |
| 2103 | self.assertEquals(cl_self.issue, 1) |
| 2104 | return 'foobar' |
| 2105 | |
| 2106 | self.mock(git_cl.Changelist, 'GetDescription', assertIssue) |
| 2107 | self.calls = [ |
| 2108 | ((['git', 'config', 'rietveld.autoupdate'],), ''), |
| 2109 | ((['git', 'config', 'rietveld.server'],), ''), |
| 2110 | ((['git', 'config', 'rietveld.server'],), ''), |
| 2111 | ] |
iannucci | e53c935 | 2016-08-17 14:40:40 -0700 | [diff] [blame] | 2112 | self.assertEqual( |
| 2113 | git_cl.main(['status', '--issue', '1', '--rietveld', '--field', 'desc']), |
| 2114 | 0) |
iannucci | 3c972b9 | 2016-08-17 13:24:10 -0700 | [diff] [blame] | 2115 | self.assertEqual(out.getvalue(), 'foobar\n') |
| 2116 | |
iannucci | e53c935 | 2016-08-17 14:40:40 -0700 | [diff] [blame] | 2117 | def test_SetCloseOverrideIssue(self): |
| 2118 | def assertIssue(cl_self, *_args): |
| 2119 | self.assertEquals(cl_self.issue, 1) |
| 2120 | return 'foobar' |
| 2121 | |
| 2122 | self.mock(git_cl.Changelist, 'GetDescription', assertIssue) |
| 2123 | self.mock(git_cl.Changelist, 'CloseIssue', lambda *_: None) |
| 2124 | self.calls = [ |
| 2125 | ((['git', 'config', 'rietveld.autoupdate'],), ''), |
| 2126 | ((['git', 'config', 'rietveld.server'],), ''), |
| 2127 | ((['git', 'config', 'rietveld.server'],), ''), |
| 2128 | ] |
| 2129 | self.assertEqual( |
| 2130 | git_cl.main(['set-close', '--issue', '1', '--rietveld']), 0) |
| 2131 | |
| 2132 | def test_SetCommitOverrideIssue(self): |
| 2133 | def assertIssue(cl_self, *_args): |
| 2134 | self.assertEquals(cl_self.issue, 1) |
| 2135 | return 'foobar' |
| 2136 | |
| 2137 | self.mock(git_cl.Changelist, 'GetDescription', assertIssue) |
| 2138 | self.mock(git_cl.Changelist, 'SetCQState', lambda *_: None) |
| 2139 | self.calls = [ |
| 2140 | ((['git', 'config', 'rietveld.autoupdate'],), ''), |
| 2141 | ((['git', 'config', 'rietveld.server'],), ''), |
| 2142 | ((['git', 'config', 'rietveld.server'],), ''), |
| 2143 | ((['git', 'symbolic-ref', 'HEAD'],), ''), |
| 2144 | ((['git', 'config', 'rietveld.server'],), ''), |
| 2145 | ((['git', 'config', 'rietveld.server'],), ''), |
| 2146 | ] |
| 2147 | self.assertEqual( |
| 2148 | git_cl.main(['set-close', '--issue', '1', '--rietveld']), 0) |
| 2149 | |
martiniss@chromium.org | 2b55fe3 | 2016-04-26 20:28:54 +0000 | [diff] [blame] | 2150 | def test_description_gerrit(self): |
| 2151 | out = StringIO.StringIO() |
| 2152 | self.mock(git_cl.sys, 'stdout', out) |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 2153 | self.calls = [ |
Andrii Shyshkalov | 8039be7 | 2017-01-26 09:38:18 +0100 | [diff] [blame] | 2154 | (('GetChangeDetail', 'code.review.org', |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 2155 | '123123', ['CURRENT_REVISION', 'CURRENT_COMMIT']), |
| 2156 | { |
| 2157 | 'current_revision': 'sha1', |
| 2158 | 'revisions': {'sha1': { |
| 2159 | 'commit': {'message': 'foobar'}, |
| 2160 | }}, |
| 2161 | }), |
| 2162 | ] |
martiniss@chromium.org | 2b55fe3 | 2016-04-26 20:28:54 +0000 | [diff] [blame] | 2163 | self.assertEqual(0, git_cl.main([ |
| 2164 | 'description', 'https://code.review.org/123123', '-d', '--gerrit'])) |
| 2165 | self.assertEqual('foobar\n', out.getvalue()) |
| 2166 | |
martiniss@chromium.org | d6648e2 | 2016-04-29 19:22:16 +0000 | [diff] [blame] | 2167 | def test_description_set_raw(self): |
| 2168 | out = StringIO.StringIO() |
| 2169 | self.mock(git_cl.sys, 'stdout', out) |
| 2170 | |
| 2171 | self.mock(git_cl, 'Changelist', ChangelistMock) |
| 2172 | self.mock(git_cl.sys, 'stdin', StringIO.StringIO('hihi')) |
| 2173 | |
| 2174 | self.assertEqual(0, git_cl.main(['description', '-n', 'hihi'])) |
| 2175 | self.assertEqual('hihi', ChangelistMock.desc) |
| 2176 | |
tandrii@chromium.org | d605a51 | 2016-06-03 09:55:00 +0000 | [diff] [blame] | 2177 | def test_description_appends_bug_line(self): |
tandrii@chromium.org | 601e1d1 | 2016-06-03 13:03:54 +0000 | [diff] [blame] | 2178 | current_desc = 'Some.\n\nChange-Id: xxx' |
tandrii@chromium.org | d605a51 | 2016-06-03 09:55:00 +0000 | [diff] [blame] | 2179 | |
| 2180 | def RunEditor(desc, _, **kwargs): |
| 2181 | self.assertEquals( |
| 2182 | '# Enter a description of the change.\n' |
| 2183 | '# This will be displayed on the codereview site.\n' |
| 2184 | '# The first line will also be used as the subject of the review.\n' |
| 2185 | '#--------------------This line is 72 characters long' |
tandrii@chromium.org | 601e1d1 | 2016-06-03 13:03:54 +0000 | [diff] [blame] | 2186 | '--------------------\n' |
| 2187 | 'Some.\n\nBUG=\n\nChange-Id: xxx', |
tandrii@chromium.org | d605a51 | 2016-06-03 09:55:00 +0000 | [diff] [blame] | 2188 | desc) |
tandrii@chromium.org | 601e1d1 | 2016-06-03 13:03:54 +0000 | [diff] [blame] | 2189 | # Simulate user changing something. |
| 2190 | return 'Some.\n\nBUG=123\n\nChange-Id: xxx' |
tandrii@chromium.org | d605a51 | 2016-06-03 09:55:00 +0000 | [diff] [blame] | 2191 | |
dsansome | e2d6fd9 | 2016-09-08 00:10:47 -0700 | [diff] [blame] | 2192 | def UpdateDescriptionRemote(_, desc, force=False): |
tandrii@chromium.org | 601e1d1 | 2016-06-03 13:03:54 +0000 | [diff] [blame] | 2193 | self.assertEquals(desc, 'Some.\n\nBUG=123\n\nChange-Id: xxx') |
tandrii@chromium.org | d605a51 | 2016-06-03 09:55:00 +0000 | [diff] [blame] | 2194 | |
| 2195 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
| 2196 | self.mock(git_cl.Changelist, 'GetDescription', |
| 2197 | lambda *args: current_desc) |
| 2198 | self.mock(git_cl._GerritChangelistImpl, 'UpdateDescriptionRemote', |
| 2199 | UpdateDescriptionRemote) |
| 2200 | self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor) |
| 2201 | |
| 2202 | self.calls = [ |
| 2203 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 2204 | ((['git', 'config', 'branch.feature.gerritissue'],), '123'), |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 2205 | ((['git', 'config', 'rietveld.autoupdate'],), CERR1), |
| 2206 | ((['git', 'config', 'rietveld.bug-prefix'],), CERR1), |
Mark Mentovai | 57c4721 | 2017-03-09 11:14:09 -0500 | [diff] [blame] | 2207 | ((['git', 'config', 'rietveld.bug-line-format'],), CERR1), |
tandrii@chromium.org | d605a51 | 2016-06-03 09:55:00 +0000 | [diff] [blame] | 2208 | ((['git', 'config', 'core.editor'],), 'vi'), |
| 2209 | ] |
| 2210 | self.assertEqual(0, git_cl.main(['description', '--gerrit'])) |
| 2211 | |
martiniss@chromium.org | d6648e2 | 2016-04-29 19:22:16 +0000 | [diff] [blame] | 2212 | def test_description_set_stdin(self): |
| 2213 | out = StringIO.StringIO() |
| 2214 | self.mock(git_cl.sys, 'stdout', out) |
| 2215 | |
| 2216 | self.mock(git_cl, 'Changelist', ChangelistMock) |
| 2217 | self.mock(git_cl.sys, 'stdin', StringIO.StringIO('hi \r\n\t there\n\nman')) |
| 2218 | |
| 2219 | self.assertEqual(0, git_cl.main(['description', '-n', '-'])) |
| 2220 | self.assertEqual('hi\n\t there\n\nman', ChangelistMock.desc) |
| 2221 | |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 2222 | def test_archive(self): |
tandrii | 1c67da6 | 2016-06-10 07:35:53 -0700 | [diff] [blame] | 2223 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
| 2224 | |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 2225 | self.calls = \ |
| 2226 | [((['git', 'for-each-ref', '--format=%(refname)', 'refs/heads'],), |
| 2227 | 'refs/heads/master\nrefs/heads/foo\nrefs/heads/bar'), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 2228 | ((['git', 'config', 'branch.master.rietveldissue'],), '1'), |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 2229 | ((['git', 'config', 'rietveld.autoupdate'],), CERR1), |
| 2230 | ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 2231 | ((['git', 'config', 'branch.foo.rietveldissue'],), '456'), |
| 2232 | ((['git', 'config', 'branch.bar.rietveldissue'],), CERR1), |
| 2233 | ((['git', 'config', 'branch.bar.gerritissue'],), '789'), |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 2234 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
| 2235 | ((['git', 'tag', 'git-cl-archived-456-foo', 'foo'],), ''), |
| 2236 | ((['git', 'branch', '-D', 'foo'],), '')] |
| 2237 | |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 2238 | self.mock(git_cl, 'get_cl_statuses', |
| 2239 | lambda branches, fine_grained, max_processes: |
kmarshall | 9249e01 | 2016-08-23 12:02:16 -0700 | [diff] [blame] | 2240 | [(MockChangelistWithBranchAndIssue('master', 1), 'open'), |
| 2241 | (MockChangelistWithBranchAndIssue('foo', 456), 'closed'), |
| 2242 | (MockChangelistWithBranchAndIssue('bar', 789), 'open')]) |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 2243 | |
| 2244 | self.assertEqual(0, git_cl.main(['archive', '-f'])) |
| 2245 | |
| 2246 | def test_archive_current_branch_fails(self): |
tandrii | 1c67da6 | 2016-06-10 07:35:53 -0700 | [diff] [blame] | 2247 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 2248 | self.calls = \ |
| 2249 | [((['git', 'for-each-ref', '--format=%(refname)', 'refs/heads'],), |
| 2250 | 'refs/heads/master'), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 2251 | ((['git', 'config', 'branch.master.rietveldissue'],), '1'), |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 2252 | ((['git', 'config', 'rietveld.autoupdate'],), CERR1), |
| 2253 | ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'), |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 2254 | ((['git', 'symbolic-ref', 'HEAD'],), 'master')] |
| 2255 | |
kmarshall | 9249e01 | 2016-08-23 12:02:16 -0700 | [diff] [blame] | 2256 | self.mock(git_cl, 'get_cl_statuses', |
| 2257 | lambda branches, fine_grained, max_processes: |
| 2258 | [(MockChangelistWithBranchAndIssue('master', 1), 'closed')]) |
| 2259 | |
| 2260 | self.assertEqual(1, git_cl.main(['archive', '-f'])) |
| 2261 | |
| 2262 | def test_archive_dry_run(self): |
| 2263 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
| 2264 | |
| 2265 | self.calls = \ |
| 2266 | [((['git', 'for-each-ref', '--format=%(refname)', 'refs/heads'],), |
| 2267 | 'refs/heads/master\nrefs/heads/foo\nrefs/heads/bar'), |
| 2268 | ((['git', 'config', 'branch.master.rietveldissue'],), '1'), |
| 2269 | ((['git', 'config', 'rietveld.autoupdate'],), CERR1), |
| 2270 | ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'), |
| 2271 | ((['git', 'config', 'branch.foo.rietveldissue'],), '456'), |
| 2272 | ((['git', 'config', 'branch.bar.rietveldissue'],), CERR1), |
| 2273 | ((['git', 'config', 'branch.bar.gerritissue'],), '789'), |
| 2274 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'),] |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 2275 | |
| 2276 | self.mock(git_cl, 'get_cl_statuses', |
| 2277 | lambda branches, fine_grained, max_processes: |
kmarshall | 9249e01 | 2016-08-23 12:02:16 -0700 | [diff] [blame] | 2278 | [(MockChangelistWithBranchAndIssue('master', 1), 'open'), |
| 2279 | (MockChangelistWithBranchAndIssue('foo', 456), 'closed'), |
| 2280 | (MockChangelistWithBranchAndIssue('bar', 789), 'open')]) |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 2281 | |
kmarshall | 9249e01 | 2016-08-23 12:02:16 -0700 | [diff] [blame] | 2282 | self.assertEqual(0, git_cl.main(['archive', '-f', '--dry-run'])) |
| 2283 | |
| 2284 | def test_archive_no_tags(self): |
| 2285 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
| 2286 | |
| 2287 | self.calls = \ |
| 2288 | [((['git', 'for-each-ref', '--format=%(refname)', 'refs/heads'],), |
| 2289 | 'refs/heads/master\nrefs/heads/foo\nrefs/heads/bar'), |
| 2290 | ((['git', 'config', 'branch.master.rietveldissue'],), '1'), |
| 2291 | ((['git', 'config', 'rietveld.autoupdate'],), CERR1), |
| 2292 | ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'), |
| 2293 | ((['git', 'config', 'branch.foo.rietveldissue'],), '456'), |
| 2294 | ((['git', 'config', 'branch.bar.rietveldissue'],), CERR1), |
| 2295 | ((['git', 'config', 'branch.bar.gerritissue'],), '789'), |
| 2296 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
| 2297 | ((['git', 'branch', '-D', 'foo'],), '')] |
| 2298 | |
| 2299 | self.mock(git_cl, 'get_cl_statuses', |
| 2300 | lambda branches, fine_grained, max_processes: |
| 2301 | [(MockChangelistWithBranchAndIssue('master', 1), 'open'), |
| 2302 | (MockChangelistWithBranchAndIssue('foo', 456), 'closed'), |
| 2303 | (MockChangelistWithBranchAndIssue('bar', 789), 'open')]) |
| 2304 | |
| 2305 | self.assertEqual(0, git_cl.main(['archive', '-f', '--notags'])) |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 2306 | |
tandrii@chromium.org | 9b7fd71 | 2016-06-01 13:45:20 +0000 | [diff] [blame] | 2307 | def test_cmd_issue_erase_existing(self): |
| 2308 | out = StringIO.StringIO() |
| 2309 | self.mock(git_cl.sys, 'stdout', out) |
| 2310 | self.calls = [ |
| 2311 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 2312 | ((['git', 'config', 'branch.feature.rietveldissue'],), CERR1), |
| 2313 | ((['git', 'config', 'branch.feature.gerritissue'],), '123'), |
tandrii@chromium.org | 9b7fd71 | 2016-06-01 13:45:20 +0000 | [diff] [blame] | 2314 | # Let this command raise exception (retcode=1) - it should be ignored. |
| 2315 | ((['git', 'config', '--unset', 'branch.feature.last-upload-hash'],), |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 2316 | CERR1), |
| 2317 | ((['git', 'config', '--unset', 'branch.feature.gerritissue'],), ''), |
| 2318 | ((['git', 'config', '--unset', 'branch.feature.gerritpatchset'],), ''), |
tandrii@chromium.org | 9b7fd71 | 2016-06-01 13:45:20 +0000 | [diff] [blame] | 2319 | ((['git', 'config', '--unset', 'branch.feature.gerritserver'],), ''), |
| 2320 | ((['git', 'config', '--unset', 'branch.feature.gerritsquashhash'],), |
| 2321 | ''), |
| 2322 | ] |
| 2323 | self.assertEqual(0, git_cl.main(['issue', '0'])) |
| 2324 | |
phajdan.jr | e328cf9 | 2016-08-22 04:12:17 -0700 | [diff] [blame] | 2325 | def test_cmd_issue_json(self): |
| 2326 | out = StringIO.StringIO() |
| 2327 | self.mock(git_cl.sys, 'stdout', out) |
| 2328 | self.calls = [ |
| 2329 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 2330 | ((['git', 'config', 'branch.feature.rietveldissue'],), '123'), |
phajdan.jr | e328cf9 | 2016-08-22 04:12:17 -0700 | [diff] [blame] | 2331 | ((['git', 'config', 'rietveld.autoupdate'],), ''), |
| 2332 | ((['git', 'config', 'rietveld.server'],), |
| 2333 | 'https://codereview.chromium.org'), |
| 2334 | ((['git', 'config', 'branch.feature.rietveldserver'],), ''), |
| 2335 | (('write_json', 'output.json', |
| 2336 | {'issue': 123, 'issue_url': 'https://codereview.chromium.org/123'}), |
| 2337 | ''), |
| 2338 | ] |
| 2339 | self.assertEqual(0, git_cl.main(['issue', '--json', 'output.json'])) |
| 2340 | |
tandrii | de281ae | 2016-10-12 06:02:30 -0700 | [diff] [blame] | 2341 | def test_git_cl_try_default_cq_dry_run(self): |
tandrii | 9de9ec6 | 2016-07-13 03:01:59 -0700 | [diff] [blame] | 2342 | self.mock(git_cl.Changelist, 'GetChange', |
| 2343 | lambda _, *a: ( |
| 2344 | self._mocked_call(['GetChange']+list(a)))) |
| 2345 | self.mock(git_cl.presubmit_support, 'DoGetTryMasters', |
| 2346 | lambda *_, **__: ( |
| 2347 | self._mocked_call(['DoGetTryMasters']))) |
tandrii | 9de9ec6 | 2016-07-13 03:01:59 -0700 | [diff] [blame] | 2348 | self.mock(git_cl._RietveldChangelistImpl, 'SetCQState', |
| 2349 | lambda _, s: self._mocked_call(['SetCQState', s])) |
| 2350 | self.calls = [ |
| 2351 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 2352 | ((['git', 'config', 'branch.feature.rietveldissue'],), '123'), |
tandrii | 9de9ec6 | 2016-07-13 03:01:59 -0700 | [diff] [blame] | 2353 | ((['git', 'config', 'rietveld.autoupdate'],), ''), |
| 2354 | ((['git', 'config', 'rietveld.server'],), |
| 2355 | 'https://codereview.chromium.org'), |
| 2356 | ((['git', 'config', 'branch.feature.rietveldserver'],), ''), |
| 2357 | ((['git', 'config', 'branch.feature.merge'],), 'feature'), |
| 2358 | ((['git', 'config', 'branch.feature.remote'],), 'origin'), |
| 2359 | ((['get_or_create_merge_base', 'feature', 'feature'],), |
| 2360 | 'fake_ancestor_sha'), |
| 2361 | ((['GetChange', 'fake_ancestor_sha', None], ), |
| 2362 | git_cl.presubmit_support.GitChange( |
| 2363 | '', '', '', '', '', '', '', '')), |
| 2364 | ((['git', 'rev-parse', '--show-cdup'],), '../'), |
| 2365 | ((['DoGetTryMasters'], ), None), |
tandrii | 9de9ec6 | 2016-07-13 03:01:59 -0700 | [diff] [blame] | 2366 | ((['SetCQState', git_cl._CQState.DRY_RUN], ), None), |
| 2367 | ] |
| 2368 | out = StringIO.StringIO() |
| 2369 | self.mock(git_cl.sys, 'stdout', out) |
| 2370 | self.assertEqual(0, git_cl.main(['try'])) |
| 2371 | self.assertEqual( |
| 2372 | out.getvalue(), |
| 2373 | 'scheduled CQ Dry Run on https://codereview.chromium.org/123\n') |
| 2374 | |
tandrii | 8c5a353 | 2016-11-04 07:52:02 -0700 | [diff] [blame] | 2375 | def test_git_cl_try_default_cq_dry_run_gerrit(self): |
| 2376 | self.mock(git_cl.Changelist, 'GetChange', |
| 2377 | lambda _, *a: ( |
| 2378 | self._mocked_call(['GetChange']+list(a)))) |
| 2379 | self.mock(git_cl.presubmit_support, 'DoGetTryMasters', |
| 2380 | lambda *_, **__: ( |
| 2381 | self._mocked_call(['DoGetTryMasters']))) |
| 2382 | self.mock(git_cl._GerritChangelistImpl, 'SetCQState', |
| 2383 | lambda _, s: self._mocked_call(['SetCQState', s])) |
| 2384 | |
tandrii | 8c5a353 | 2016-11-04 07:52:02 -0700 | [diff] [blame] | 2385 | self.calls = [ |
| 2386 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
| 2387 | ((['git', 'config', 'branch.feature.rietveldissue'],), CERR1), |
| 2388 | ((['git', 'config', 'branch.feature.gerritissue'],), '123456'), |
| 2389 | ((['git', 'config', 'branch.feature.gerritserver'],), |
| 2390 | 'https://chromium-review.googlesource.com'), |
Andrii Shyshkalov | eadad92 | 2017-01-26 09:38:30 +0100 | [diff] [blame] | 2391 | (('GetChangeDetail', 'chromium-review.googlesource.com', '123456', |
| 2392 | ['DETAILED_ACCOUNTS', 'ALL_REVISIONS', 'CURRENT_COMMIT']), { |
| 2393 | 'project': 'depot_tools', |
| 2394 | 'status': 'OPEN', |
| 2395 | 'owner': {'email': 'owner@e.mail'}, |
| 2396 | 'revisions': { |
| 2397 | 'deadbeaf': { |
| 2398 | '_number': 6, |
| 2399 | }, |
| 2400 | 'beeeeeef': { |
| 2401 | '_number': 7, |
| 2402 | 'fetch': {'http': { |
| 2403 | 'url': 'https://chromium.googlesource.com/depot_tools', |
| 2404 | 'ref': 'refs/changes/56/123456/7' |
| 2405 | }}, |
| 2406 | }, |
| 2407 | }, |
| 2408 | }), |
tandrii | 8c5a353 | 2016-11-04 07:52:02 -0700 | [diff] [blame] | 2409 | ((['git', 'config', 'branch.feature.merge'],), 'feature'), |
| 2410 | ((['git', 'config', 'branch.feature.remote'],), 'origin'), |
| 2411 | ((['get_or_create_merge_base', 'feature', 'feature'],), |
| 2412 | 'fake_ancestor_sha'), |
| 2413 | ((['GetChange', 'fake_ancestor_sha', None], ), |
| 2414 | git_cl.presubmit_support.GitChange( |
| 2415 | '', '', '', '', '', '', '', '')), |
| 2416 | ((['git', 'rev-parse', '--show-cdup'],), '../'), |
| 2417 | ((['DoGetTryMasters'], ), None), |
| 2418 | ((['SetCQState', git_cl._CQState.DRY_RUN], ), None), |
| 2419 | ] |
| 2420 | out = StringIO.StringIO() |
| 2421 | self.mock(git_cl.sys, 'stdout', out) |
| 2422 | self.assertEqual(0, git_cl.main(['try'])) |
| 2423 | self.assertEqual( |
| 2424 | out.getvalue(), |
| 2425 | 'scheduled CQ Dry Run on ' |
| 2426 | 'https://chromium-review.googlesource.com/123456\n') |
| 2427 | |
| 2428 | def test_git_cl_try_buildbucket_with_properties_rietveld(self): |
| 2429 | self.mock(git_cl._RietveldChangelistImpl, 'GetIssueProperties', |
| 2430 | lambda _: { |
| 2431 | 'owner_email': 'owner@e.mail', |
| 2432 | 'private': False, |
| 2433 | 'closed': False, |
| 2434 | 'project': 'depot_tools', |
| 2435 | 'patchsets': [20001], |
| 2436 | }) |
tandrii | de281ae | 2016-10-12 06:02:30 -0700 | [diff] [blame] | 2437 | self.mock(git_cl.uuid, 'uuid4', lambda: 'uuid4') |
| 2438 | self.calls = [ |
| 2439 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
| 2440 | ((['git', 'config', 'branch.feature.rietveldissue'],), '123'), |
| 2441 | ((['git', 'config', 'rietveld.autoupdate'],), CERR1), |
| 2442 | ((['git', 'config', 'rietveld.server'],), |
| 2443 | 'https://codereview.chromium.org'), |
tandrii | de281ae | 2016-10-12 06:02:30 -0700 | [diff] [blame] | 2444 | ((['git', 'config', 'branch.feature.rietveldpatchset'],), '20001'), |
tandrii | 8c5a353 | 2016-11-04 07:52:02 -0700 | [diff] [blame] | 2445 | ((['git', 'config', 'branch.feature.rietveldserver'],), CERR1), |
tandrii | de281ae | 2016-10-12 06:02:30 -0700 | [diff] [blame] | 2446 | ] |
| 2447 | |
| 2448 | def _buildbucket_retry(*_, **kw): |
| 2449 | # self.maxDiff = 10000 |
| 2450 | body = json.loads(kw['body']) |
| 2451 | self.assertEqual(len(body['builds']), 1) |
| 2452 | build = body['builds'][0] |
| 2453 | params = json.loads(build.pop('parameters_json')) |
| 2454 | self.assertEqual(params, { |
| 2455 | u'builder_name': u'win', |
| 2456 | u'changes': [{u'author': {u'email': u'owner@e.mail'}, |
| 2457 | u'revision': None}], |
| 2458 | u'properties': { |
| 2459 | u'category': u'git_cl_try', |
| 2460 | u'issue': 123, |
| 2461 | u'key': u'val', |
| 2462 | u'json': [{u'a': 1}, None], |
| 2463 | u'master': u'tryserver.chromium', |
| 2464 | u'patch_project': u'depot_tools', |
| 2465 | u'patch_storage': u'rietveld', |
| 2466 | u'patchset': 20001, |
tandrii | de281ae | 2016-10-12 06:02:30 -0700 | [diff] [blame] | 2467 | u'rietveld': u'https://codereview.chromium.org', |
| 2468 | } |
| 2469 | }) |
| 2470 | self.assertEqual(build, { |
| 2471 | u'bucket': u'master.tryserver.chromium', |
| 2472 | u'client_operation_id': u'uuid4', |
| 2473 | u'tags': [u'builder:win', |
| 2474 | u'buildset:patch/rietveld/codereview.chromium.org/123/20001', |
borenet | 6c0efe6 | 2016-10-19 08:13:29 -0700 | [diff] [blame] | 2475 | u'user_agent:git_cl_try', |
| 2476 | u'master:tryserver.chromium'], |
tandrii | de281ae | 2016-10-12 06:02:30 -0700 | [diff] [blame] | 2477 | }) |
| 2478 | |
| 2479 | self.mock(git_cl, '_buildbucket_retry', _buildbucket_retry) |
| 2480 | |
| 2481 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
| 2482 | self.assertEqual(0, git_cl.main([ |
| 2483 | 'try', '-m', 'tryserver.chromium', '-b', 'win', |
| 2484 | '-p', 'key=val', '-p', 'json=[{"a":1}, null]'])) |
| 2485 | self.assertRegexpMatches( |
| 2486 | git_cl.sys.stdout.getvalue(), |
borenet | 6c0efe6 | 2016-10-19 08:13:29 -0700 | [diff] [blame] | 2487 | 'Tried jobs on:\nBucket: master.tryserver.chromium') |
| 2488 | |
tandrii | 8c5a353 | 2016-11-04 07:52:02 -0700 | [diff] [blame] | 2489 | def test_git_cl_try_buildbucket_with_properties_gerrit(self): |
| 2490 | self.mock(git_cl.Changelist, 'GetMostRecentPatchset', lambda _: 7) |
| 2491 | self.mock(git_cl.uuid, 'uuid4', lambda: 'uuid4') |
| 2492 | |
tandrii | 8c5a353 | 2016-11-04 07:52:02 -0700 | [diff] [blame] | 2493 | self.calls = [ |
| 2494 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
| 2495 | ((['git', 'config', 'branch.feature.rietveldissue'],), CERR1), |
| 2496 | ((['git', 'config', 'branch.feature.gerritissue'],), '123456'), |
| 2497 | ((['git', 'config', 'branch.feature.gerritserver'],), |
| 2498 | 'https://chromium-review.googlesource.com'), |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 2499 | (('GetChangeDetail', 'chromium-review.googlesource.com', '123456', |
Andrii Shyshkalov | eadad92 | 2017-01-26 09:38:30 +0100 | [diff] [blame] | 2500 | ['DETAILED_ACCOUNTS', 'ALL_REVISIONS', 'CURRENT_COMMIT']), { |
tandrii | 8c5a353 | 2016-11-04 07:52:02 -0700 | [diff] [blame] | 2501 | 'project': 'depot_tools', |
Andrii Shyshkalov | eadad92 | 2017-01-26 09:38:30 +0100 | [diff] [blame] | 2502 | 'status': 'OPEN', |
| 2503 | 'owner': {'email': 'owner@e.mail'}, |
tandrii | 8c5a353 | 2016-11-04 07:52:02 -0700 | [diff] [blame] | 2504 | 'revisions': { |
| 2505 | 'deadbeaf': { |
| 2506 | '_number': 6, |
| 2507 | }, |
| 2508 | 'beeeeeef': { |
| 2509 | '_number': 7, |
| 2510 | 'fetch': {'http': { |
| 2511 | 'url': 'https://chromium.googlesource.com/depot_tools', |
| 2512 | 'ref': 'refs/changes/56/123456/7' |
| 2513 | }}, |
| 2514 | }, |
| 2515 | }, |
| 2516 | }), |
| 2517 | ] |
| 2518 | |
| 2519 | def _buildbucket_retry(*_, **kw): |
| 2520 | # self.maxDiff = 10000 |
| 2521 | body = json.loads(kw['body']) |
| 2522 | self.assertEqual(len(body['builds']), 1) |
| 2523 | build = body['builds'][0] |
| 2524 | params = json.loads(build.pop('parameters_json')) |
| 2525 | self.assertEqual(params, { |
| 2526 | u'builder_name': u'win', |
| 2527 | u'changes': [{u'author': {u'email': u'owner@e.mail'}, |
| 2528 | u'revision': None}], |
| 2529 | u'properties': { |
| 2530 | u'category': u'git_cl_try', |
| 2531 | u'key': u'val', |
| 2532 | u'json': [{u'a': 1}, None], |
| 2533 | u'master': u'tryserver.chromium', |
| 2534 | |
| 2535 | u'patch_gerrit_url': |
| 2536 | u'https://chromium-review.googlesource.com', |
| 2537 | u'patch_issue': 123456, |
| 2538 | u'patch_project': u'depot_tools', |
| 2539 | u'patch_ref': u'refs/changes/56/123456/7', |
| 2540 | u'patch_repository_url': |
| 2541 | u'https://chromium.googlesource.com/depot_tools', |
| 2542 | u'patch_set': 7, |
| 2543 | u'patch_storage': u'gerrit', |
| 2544 | } |
| 2545 | }) |
| 2546 | self.assertEqual(build, { |
| 2547 | u'bucket': u'master.tryserver.chromium', |
| 2548 | u'client_operation_id': u'uuid4', |
| 2549 | u'tags': [ |
| 2550 | u'builder:win', |
| 2551 | u'buildset:patch/gerrit/chromium-review.googlesource.com/123456/7', |
| 2552 | u'user_agent:git_cl_try', |
| 2553 | u'master:tryserver.chromium'], |
| 2554 | }) |
| 2555 | |
| 2556 | self.mock(git_cl, '_buildbucket_retry', _buildbucket_retry) |
| 2557 | |
| 2558 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
| 2559 | self.assertEqual(0, git_cl.main([ |
| 2560 | 'try', '-m', 'tryserver.chromium', '-b', 'win', |
| 2561 | '-p', 'key=val', '-p', 'json=[{"a":1}, null]'])) |
| 2562 | self.assertRegexpMatches( |
| 2563 | git_cl.sys.stdout.getvalue(), |
| 2564 | 'Tried jobs on:\nBucket: master.tryserver.chromium') |
| 2565 | |
borenet | 6c0efe6 | 2016-10-19 08:13:29 -0700 | [diff] [blame] | 2566 | def test_git_cl_try_buildbucket_bucket_flag(self): |
tandrii | 8c5a353 | 2016-11-04 07:52:02 -0700 | [diff] [blame] | 2567 | self.mock(git_cl._RietveldChangelistImpl, 'GetIssueProperties', |
| 2568 | lambda _: { |
| 2569 | 'owner_email': 'owner@e.mail', |
| 2570 | 'private': False, |
| 2571 | 'closed': False, |
| 2572 | 'project': 'depot_tools', |
| 2573 | 'patchsets': [20001], |
| 2574 | }) |
borenet | 6c0efe6 | 2016-10-19 08:13:29 -0700 | [diff] [blame] | 2575 | self.mock(git_cl.uuid, 'uuid4', lambda: 'uuid4') |
| 2576 | self.calls = [ |
| 2577 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
| 2578 | ((['git', 'config', 'branch.feature.rietveldissue'],), '123'), |
| 2579 | ((['git', 'config', 'rietveld.autoupdate'],), CERR1), |
| 2580 | ((['git', 'config', 'rietveld.server'],), |
| 2581 | 'https://codereview.chromium.org'), |
borenet | 6c0efe6 | 2016-10-19 08:13:29 -0700 | [diff] [blame] | 2582 | ((['git', 'config', 'branch.feature.rietveldpatchset'],), '20001'), |
tandrii | 8c5a353 | 2016-11-04 07:52:02 -0700 | [diff] [blame] | 2583 | ((['git', 'config', 'branch.feature.rietveldserver'],), CERR1), |
borenet | 6c0efe6 | 2016-10-19 08:13:29 -0700 | [diff] [blame] | 2584 | ] |
| 2585 | |
| 2586 | def _buildbucket_retry(*_, **kw): |
borenet | 6c0efe6 | 2016-10-19 08:13:29 -0700 | [diff] [blame] | 2587 | body = json.loads(kw['body']) |
| 2588 | self.assertEqual(len(body['builds']), 1) |
| 2589 | build = body['builds'][0] |
| 2590 | params = json.loads(build.pop('parameters_json')) |
| 2591 | self.assertEqual(params, { |
| 2592 | u'builder_name': u'win', |
| 2593 | u'changes': [{u'author': {u'email': u'owner@e.mail'}, |
| 2594 | u'revision': None}], |
| 2595 | u'properties': { |
| 2596 | u'category': u'git_cl_try', |
| 2597 | u'issue': 123, |
| 2598 | u'patch_project': u'depot_tools', |
| 2599 | u'patch_storage': u'rietveld', |
| 2600 | u'patchset': 20001, |
borenet | 6c0efe6 | 2016-10-19 08:13:29 -0700 | [diff] [blame] | 2601 | u'rietveld': u'https://codereview.chromium.org', |
| 2602 | } |
| 2603 | }) |
| 2604 | self.assertEqual(build, { |
| 2605 | u'bucket': u'test.bucket', |
| 2606 | u'client_operation_id': u'uuid4', |
| 2607 | u'tags': [u'builder:win', |
| 2608 | u'buildset:patch/rietveld/codereview.chromium.org/123/20001', |
| 2609 | u'user_agent:git_cl_try'], |
| 2610 | }) |
| 2611 | |
| 2612 | self.mock(git_cl, '_buildbucket_retry', _buildbucket_retry) |
| 2613 | |
| 2614 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
| 2615 | self.assertEqual(0, git_cl.main([ |
| 2616 | 'try', '-B', 'test.bucket', '-b', 'win'])) |
| 2617 | self.assertRegexpMatches( |
| 2618 | git_cl.sys.stdout.getvalue(), |
| 2619 | 'Tried jobs on:\nBucket: test.bucket') |
tandrii | de281ae | 2016-10-12 06:02:30 -0700 | [diff] [blame] | 2620 | |
qyearsley | 123a468 | 2016-10-26 09:12:17 -0700 | [diff] [blame] | 2621 | def test_git_cl_try_bots_on_multiple_masters(self): |
| 2622 | self.mock(git_cl.Changelist, 'GetMostRecentPatchset', lambda _: 20001) |
| 2623 | self.calls = [ |
| 2624 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
| 2625 | ((['git', 'config', 'branch.feature.rietveldissue'],), '123'), |
| 2626 | ((['git', 'config', 'rietveld.autoupdate'],), CERR1), |
| 2627 | ((['git', 'config', 'rietveld.server'],), |
tandrii | 8c5a353 | 2016-11-04 07:52:02 -0700 | [diff] [blame] | 2628 | 'https://codereview.chromium.org'), |
qyearsley | 123a468 | 2016-10-26 09:12:17 -0700 | [diff] [blame] | 2629 | ((['git', 'config', 'branch.feature.rietveldserver'],), CERR1), |
| 2630 | ((['git', 'config', 'branch.feature.rietveldpatchset'],), '20001'), |
| 2631 | ] |
| 2632 | |
| 2633 | def _buildbucket_retry(*_, **kw): |
| 2634 | body = json.loads(kw['body']) |
| 2635 | self.assertEqual(len(body['builds']), 2) |
| 2636 | |
| 2637 | first_build_params = json.loads(body['builds'][0]['parameters_json']) |
| 2638 | self.assertEqual(first_build_params['builder_name'], 'builder1') |
| 2639 | self.assertEqual(first_build_params['properties']['master'], 'master1') |
| 2640 | |
| 2641 | first_build_params = json.loads(body['builds'][1]['parameters_json']) |
| 2642 | self.assertEqual(first_build_params['builder_name'], 'builder2') |
| 2643 | self.assertEqual(first_build_params['properties']['master'], 'master2') |
| 2644 | |
| 2645 | self.mock(git_cl, '_buildbucket_retry', _buildbucket_retry) |
| 2646 | |
| 2647 | self.mock(git_cl.urllib2, 'urlopen', lambda _: StringIO.StringIO( |
| 2648 | json.dumps({'builder1': ['master1'], 'builder2': ['master2']}))) |
| 2649 | |
| 2650 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
| 2651 | self.assertEqual( |
| 2652 | 0, git_cl.main(['try', '-b', 'builder1', '-b', 'builder2'])) |
| 2653 | self.assertEqual( |
| 2654 | git_cl.sys.stdout.getvalue(), |
| 2655 | 'Tried jobs on:\n' |
| 2656 | 'Bucket: master.master1\n' |
| 2657 | ' builder1: []\n' |
| 2658 | 'Bucket: master.master2\n' |
| 2659 | ' builder2: []\n' |
| 2660 | 'To see results here, run: git cl try-results\n' |
| 2661 | 'To see results in browser, run: git cl web\n') |
| 2662 | |
tandrii | 16e0b4e | 2016-06-07 10:34:28 -0700 | [diff] [blame] | 2663 | def _common_GerritCommitMsgHookCheck(self): |
| 2664 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
| 2665 | self.mock(git_cl.os.path, 'abspath', |
| 2666 | lambda path: self._mocked_call(['abspath', path])) |
| 2667 | self.mock(git_cl.os.path, 'exists', |
| 2668 | lambda path: self._mocked_call(['exists', path])) |
| 2669 | self.mock(git_cl.gclient_utils, 'FileRead', |
| 2670 | lambda path: self._mocked_call(['FileRead', path])) |
| 2671 | self.mock(git_cl.gclient_utils, 'rm_file_or_tree', |
| 2672 | lambda path: self._mocked_call(['rm_file_or_tree', path])) |
| 2673 | self.calls = [ |
| 2674 | ((['git', 'rev-parse', '--show-cdup'],), '../'), |
| 2675 | ((['abspath', '../'],), '/abs/git_repo_root'), |
| 2676 | ] |
| 2677 | return git_cl.Changelist(codereview='gerrit', issue=123) |
| 2678 | |
| 2679 | def test_GerritCommitMsgHookCheck_custom_hook(self): |
| 2680 | cl = self._common_GerritCommitMsgHookCheck() |
| 2681 | self.calls += [ |
| 2682 | ((['exists', '/abs/git_repo_root/.git/hooks/commit-msg'],), True), |
| 2683 | ((['FileRead', '/abs/git_repo_root/.git/hooks/commit-msg'],), |
| 2684 | '#!/bin/sh\necho "custom hook"') |
| 2685 | ] |
| 2686 | cl._codereview_impl._GerritCommitMsgHookCheck(offer_removal=True) |
| 2687 | |
| 2688 | def test_GerritCommitMsgHookCheck_not_exists(self): |
| 2689 | cl = self._common_GerritCommitMsgHookCheck() |
| 2690 | self.calls += [ |
| 2691 | ((['exists', '/abs/git_repo_root/.git/hooks/commit-msg'],), False), |
| 2692 | ] |
| 2693 | cl._codereview_impl._GerritCommitMsgHookCheck(offer_removal=True) |
| 2694 | |
| 2695 | def test_GerritCommitMsgHookCheck(self): |
| 2696 | cl = self._common_GerritCommitMsgHookCheck() |
| 2697 | self.calls += [ |
| 2698 | ((['exists', '/abs/git_repo_root/.git/hooks/commit-msg'],), True), |
| 2699 | ((['FileRead', '/abs/git_repo_root/.git/hooks/commit-msg'],), |
| 2700 | '...\n# From Gerrit Code Review\n...\nadd_ChangeId()\n'), |
Andrii Shyshkalov | abc26ac | 2017-03-14 14:49:38 +0100 | [diff] [blame] | 2701 | (('ask_for_data', 'Do you want to remove it now? [Yes/No]: '), 'Yes'), |
tandrii | 16e0b4e | 2016-06-07 10:34:28 -0700 | [diff] [blame] | 2702 | ((['rm_file_or_tree', '/abs/git_repo_root/.git/hooks/commit-msg'],), |
| 2703 | ''), |
| 2704 | ] |
| 2705 | cl._codereview_impl._GerritCommitMsgHookCheck(offer_removal=True) |
| 2706 | |
tandrii | c4344b5 | 2016-08-29 06:04:54 -0700 | [diff] [blame] | 2707 | def test_GerritCmdLand(self): |
| 2708 | self.calls += [ |
| 2709 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
| 2710 | ((['git', 'config', 'branch.feature.gerritsquashhash'],), |
| 2711 | 'deadbeaf'), |
| 2712 | ((['git', 'diff', 'deadbeaf'],), ''), # No diff. |
| 2713 | ((['git', 'config', 'branch.feature.gerritserver'],), |
| 2714 | 'chromium-review.googlesource.com'), |
| 2715 | ] |
| 2716 | cl = git_cl.Changelist(issue=123, codereview='gerrit') |
| 2717 | cl._codereview_impl._GetChangeDetail = lambda _: { |
| 2718 | 'labels': {}, |
| 2719 | 'current_revision': 'deadbeaf', |
| 2720 | } |
agable | 32978d9 | 2016-11-01 12:55:02 -0700 | [diff] [blame] | 2721 | cl._codereview_impl._GetChangeCommit = lambda: { |
| 2722 | 'commit': 'deadbeef', |
Aaron Gable | 02cdbb4 | 2016-12-13 16:24:25 -0800 | [diff] [blame] | 2723 | 'web_links': [{'name': 'gitiles', |
agable | 32978d9 | 2016-11-01 12:55:02 -0700 | [diff] [blame] | 2724 | 'url': 'https://git.googlesource.com/test/+/deadbeef'}], |
| 2725 | } |
tandrii | c4344b5 | 2016-08-29 06:04:54 -0700 | [diff] [blame] | 2726 | cl._codereview_impl.SubmitIssue = lambda wait_for_merge: None |
tandrii | 8da412c | 2016-09-07 16:01:07 -0700 | [diff] [blame] | 2727 | out = StringIO.StringIO() |
| 2728 | self.mock(sys, 'stdout', out) |
tandrii | c4344b5 | 2016-08-29 06:04:54 -0700 | [diff] [blame] | 2729 | self.assertEqual(0, cl.CMDLand(force=True, bypass_hooks=True, verbose=True)) |
tandrii | 8da412c | 2016-09-07 16:01:07 -0700 | [diff] [blame] | 2730 | self.assertRegexpMatches(out.getvalue(), 'Issue.*123 has been submitted') |
agable | 32978d9 | 2016-11-01 12:55:02 -0700 | [diff] [blame] | 2731 | self.assertRegexpMatches(out.getvalue(), 'Landed as .*deadbeef') |
tandrii | c4344b5 | 2016-08-29 06:04:54 -0700 | [diff] [blame] | 2732 | |
tandrii | 221ab25 | 2016-10-06 08:12:04 -0700 | [diff] [blame] | 2733 | BUILDBUCKET_BUILDS_MAP = { |
| 2734 | '9000': { |
| 2735 | 'id': '9000', |
| 2736 | 'status': 'STARTED', |
| 2737 | 'url': 'http://build.cr.org/p/x.y/builders/my-builder/builds/2', |
| 2738 | 'result_details_json': '{"properties": {}}', |
| 2739 | 'bucket': 'master.x.y', |
| 2740 | 'created_by': 'user:someone@chromium.org', |
| 2741 | 'created_ts': '147200002222000', |
| 2742 | 'parameters_json': '{"builder_name": "my-builder", "category": ""}', |
| 2743 | }, |
| 2744 | '8000': { |
| 2745 | 'id': '8000', |
| 2746 | 'status': 'COMPLETED', |
| 2747 | 'result': 'FAILURE', |
| 2748 | 'failure_reason': 'BUILD_FAILURE', |
| 2749 | 'url': 'http://build.cr.org/p/x.y/builders/my-builder/builds/1', |
| 2750 | 'result_details_json': '{"properties": {}}', |
| 2751 | 'bucket': 'master.x.y', |
| 2752 | 'created_by': 'user:someone@chromium.org', |
| 2753 | 'created_ts': '147200001111000', |
| 2754 | 'parameters_json': '{"builder_name": "my-builder", "category": ""}', |
| 2755 | }, |
| 2756 | } |
| 2757 | |
| 2758 | def test_write_try_results_json(self): |
| 2759 | expected_output = [ |
| 2760 | { |
| 2761 | 'buildbucket_id': '8000', |
| 2762 | 'bucket': 'master.x.y', |
| 2763 | 'builder_name': 'my-builder', |
| 2764 | 'status': 'COMPLETED', |
| 2765 | 'result': 'FAILURE', |
| 2766 | 'failure_reason': 'BUILD_FAILURE', |
| 2767 | 'url': 'http://build.cr.org/p/x.y/builders/my-builder/builds/1', |
| 2768 | }, |
| 2769 | { |
| 2770 | 'buildbucket_id': '9000', |
| 2771 | 'bucket': 'master.x.y', |
| 2772 | 'builder_name': 'my-builder', |
| 2773 | 'status': 'STARTED', |
| 2774 | 'result': None, |
| 2775 | 'failure_reason': None, |
| 2776 | 'url': 'http://build.cr.org/p/x.y/builders/my-builder/builds/2', |
| 2777 | } |
| 2778 | ] |
| 2779 | self.calls = [(('write_json', 'output.json', expected_output), '')] |
| 2780 | git_cl.write_try_results_json('output.json', self.BUILDBUCKET_BUILDS_MAP) |
| 2781 | |
tandrii | 45b2a58 | 2016-10-11 03:14:16 -0700 | [diff] [blame] | 2782 | def _setup_fetch_try_jobs(self, most_recent_patchset=20001): |
tandrii | 221ab25 | 2016-10-06 08:12:04 -0700 | [diff] [blame] | 2783 | out = StringIO.StringIO() |
| 2784 | self.mock(sys, 'stdout', out) |
tandrii | 45b2a58 | 2016-10-11 03:14:16 -0700 | [diff] [blame] | 2785 | self.mock(git_cl.Changelist, 'GetMostRecentPatchset', |
| 2786 | lambda *args: most_recent_patchset) |
tandrii | 221ab25 | 2016-10-06 08:12:04 -0700 | [diff] [blame] | 2787 | self.mock(git_cl.auth, 'get_authenticator_for_host', lambda host, _cfg: |
| 2788 | self._mocked_call(['get_authenticator_for_host', host])) |
| 2789 | self.mock(git_cl, '_buildbucket_retry', lambda *_, **__: |
| 2790 | self._mocked_call(['_buildbucket_retry'])) |
tandrii | 45b2a58 | 2016-10-11 03:14:16 -0700 | [diff] [blame] | 2791 | |
| 2792 | def _setup_fetch_try_jobs_rietveld(self, *request_results): |
| 2793 | self._setup_fetch_try_jobs(most_recent_patchset=20001) |
tandrii | 221ab25 | 2016-10-06 08:12:04 -0700 | [diff] [blame] | 2794 | self.calls += [ |
| 2795 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
| 2796 | ((['git', 'config', 'branch.feature.rietveldissue'],), '1'), |
| 2797 | ((['git', 'config', 'rietveld.autoupdate'],), CERR1), |
| 2798 | ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'), |
| 2799 | ((['git', 'config', 'branch.feature.rietveldpatchset'],), '20001'), |
| 2800 | ((['git', 'config', 'branch.feature.rietveldserver'],), |
| 2801 | 'codereview.example.com'), |
| 2802 | ((['get_authenticator_for_host', 'codereview.example.com'],), |
| 2803 | AuthenticatorMock()), |
| 2804 | ] + [((['_buildbucket_retry'],), r) for r in request_results] |
| 2805 | |
| 2806 | def test_fetch_try_jobs_none_rietveld(self): |
| 2807 | self._setup_fetch_try_jobs_rietveld({}) |
tandrii | 45b2a58 | 2016-10-11 03:14:16 -0700 | [diff] [blame] | 2808 | # Simulate that user isn't logged in. |
| 2809 | self.mock(AuthenticatorMock, 'has_cached_credentials', lambda _: False) |
tandrii | 221ab25 | 2016-10-06 08:12:04 -0700 | [diff] [blame] | 2810 | self.assertEqual(0, git_cl.main(['try-results'])) |
tandrii | 45b2a58 | 2016-10-11 03:14:16 -0700 | [diff] [blame] | 2811 | self.assertRegexpMatches(sys.stdout.getvalue(), |
| 2812 | 'Warning: Some results might be missing') |
tandrii | 221ab25 | 2016-10-06 08:12:04 -0700 | [diff] [blame] | 2813 | self.assertRegexpMatches(sys.stdout.getvalue(), 'No try jobs') |
| 2814 | |
| 2815 | def test_fetch_try_jobs_some_rietveld(self): |
| 2816 | self._setup_fetch_try_jobs_rietveld({ |
| 2817 | 'builds': self.BUILDBUCKET_BUILDS_MAP.values(), |
| 2818 | }) |
| 2819 | self.assertEqual(0, git_cl.main(['try-results'])) |
tandrii | 45b2a58 | 2016-10-11 03:14:16 -0700 | [diff] [blame] | 2820 | self.assertRegexpMatches(sys.stdout.getvalue(), '^Failures:') |
| 2821 | self.assertRegexpMatches(sys.stdout.getvalue(), 'Started:') |
| 2822 | self.assertRegexpMatches(sys.stdout.getvalue(), '2 try jobs') |
| 2823 | |
| 2824 | def _setup_fetch_try_jobs_gerrit(self, *request_results): |
| 2825 | self._setup_fetch_try_jobs(most_recent_patchset=13) |
| 2826 | self.calls += [ |
| 2827 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
| 2828 | ((['git', 'config', 'branch.feature.rietveldissue'],), CERR1), |
| 2829 | ((['git', 'config', 'branch.feature.gerritissue'],), '1'), |
Ravi Mistry | fda50ca | 2016-11-14 10:19:18 -0500 | [diff] [blame] | 2830 | # TODO(tandrii): Uncomment the below if we decide to support checking |
| 2831 | # patchsets for Gerrit. |
tandrii | 45b2a58 | 2016-10-11 03:14:16 -0700 | [diff] [blame] | 2832 | # Simulate that Gerrit has more patchsets than local. |
Ravi Mistry | fda50ca | 2016-11-14 10:19:18 -0500 | [diff] [blame] | 2833 | # ((['git', 'config', 'branch.feature.gerritpatchset'],), '12'), |
tandrii | 45b2a58 | 2016-10-11 03:14:16 -0700 | [diff] [blame] | 2834 | ((['git', 'config', 'branch.feature.gerritserver'],), |
| 2835 | 'https://x-review.googlesource.com'), |
| 2836 | ((['get_authenticator_for_host', 'x-review.googlesource.com'],), |
| 2837 | AuthenticatorMock()), |
| 2838 | ] + [((['_buildbucket_retry'],), r) for r in request_results] |
| 2839 | |
| 2840 | def test_fetch_try_jobs_none_gerrit(self): |
| 2841 | self._setup_fetch_try_jobs_gerrit({}) |
| 2842 | self.assertEqual(0, git_cl.main(['try-results'])) |
Ravi Mistry | fda50ca | 2016-11-14 10:19:18 -0500 | [diff] [blame] | 2843 | # TODO(tandrii): Uncomment the below if we decide to support checking |
| 2844 | # patchsets for Gerrit. |
| 2845 | # self.assertRegexpMatches( |
| 2846 | # sys.stdout.getvalue(), |
| 2847 | # r'Warning: Codereview server has newer patchsets \(13\)') |
tandrii | 45b2a58 | 2016-10-11 03:14:16 -0700 | [diff] [blame] | 2848 | self.assertRegexpMatches(sys.stdout.getvalue(), 'No try jobs') |
| 2849 | |
| 2850 | def test_fetch_try_jobs_some_gerrit(self): |
| 2851 | self._setup_fetch_try_jobs_gerrit({ |
| 2852 | 'builds': self.BUILDBUCKET_BUILDS_MAP.values(), |
| 2853 | }) |
Ravi Mistry | fda50ca | 2016-11-14 10:19:18 -0500 | [diff] [blame] | 2854 | # TODO(tandrii): Uncomment the below if we decide to support checking |
| 2855 | # patchsets for Gerrit. |
| 2856 | # self.calls.remove( |
| 2857 | # ((['git', 'config', 'branch.feature.gerritpatchset'],), '12')) |
tandrii | 45b2a58 | 2016-10-11 03:14:16 -0700 | [diff] [blame] | 2858 | self.assertEqual(0, git_cl.main(['try-results', '--patchset', '5'])) |
| 2859 | |
| 2860 | # ... and doesn't result in warning. |
| 2861 | self.assertNotRegexpMatches(sys.stdout.getvalue(), 'Warning') |
| 2862 | self.assertRegexpMatches(sys.stdout.getvalue(), '^Failures:') |
tandrii | 221ab25 | 2016-10-06 08:12:04 -0700 | [diff] [blame] | 2863 | self.assertRegexpMatches(sys.stdout.getvalue(), 'Started:') |
| 2864 | self.assertRegexpMatches(sys.stdout.getvalue(), '2 try jobs') |
| 2865 | |
Andrii Shyshkalov | 258e0a6 | 2017-01-24 16:50:57 +0100 | [diff] [blame] | 2866 | def _mock_gerrit_changes_for_detail_cache(self): |
| 2867 | self.mock(git_cl._GerritChangelistImpl, '_GetGerritHost', lambda _: 'host') |
Andrii Shyshkalov | 258e0a6 | 2017-01-24 16:50:57 +0100 | [diff] [blame] | 2868 | |
| 2869 | def test_gerrit_change_detail_cache_normalize(self): |
| 2870 | self._mock_gerrit_changes_for_detail_cache() |
| 2871 | self.calls = [ |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 2872 | (('GetChangeDetail', 'host', '2', ['CASE']), 'b'), |
Andrii Shyshkalov | 258e0a6 | 2017-01-24 16:50:57 +0100 | [diff] [blame] | 2873 | ] |
| 2874 | cl = git_cl.Changelist(codereview='gerrit') |
| 2875 | self.assertEqual(cl._GetChangeDetail(issue=2, options=['CaSe']), 'b') |
| 2876 | self.assertEqual(cl._GetChangeDetail(issue=2, options=['CASE']), 'b') |
| 2877 | self.assertEqual(cl._GetChangeDetail(issue='2'), 'b') |
| 2878 | self.assertEqual(cl._GetChangeDetail(issue=2), 'b') |
| 2879 | |
| 2880 | def test_gerrit_change_detail_cache_simple(self): |
| 2881 | self._mock_gerrit_changes_for_detail_cache() |
| 2882 | self.calls = [ |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 2883 | (('GetChangeDetail', 'host', '1', []), 'a'), |
| 2884 | (('GetChangeDetail', 'host', '2', []), 'b'), |
| 2885 | (('GetChangeDetail', 'host', '2', []), 'b2'), |
Andrii Shyshkalov | 258e0a6 | 2017-01-24 16:50:57 +0100 | [diff] [blame] | 2886 | ] |
| 2887 | cl = git_cl.Changelist(issue=1, codereview='gerrit') |
| 2888 | self.assertEqual(cl._GetChangeDetail(), 'a') # Miss. |
| 2889 | self.assertEqual(cl._GetChangeDetail(), 'a') |
| 2890 | self.assertEqual(cl._GetChangeDetail(issue=2), 'b') # Miss. |
Andrii Shyshkalov | 1897532 | 2017-01-25 16:44:13 +0100 | [diff] [blame] | 2891 | self.assertEqual(cl._GetChangeDetail(issue=2, no_cache=True), 'b2') # Miss. |
Andrii Shyshkalov | 258e0a6 | 2017-01-24 16:50:57 +0100 | [diff] [blame] | 2892 | self.assertEqual(cl._GetChangeDetail(), 'a') |
| 2893 | self.assertEqual(cl._GetChangeDetail(issue=2), 'b2') |
| 2894 | |
| 2895 | def test_gerrit_change_detail_cache_options(self): |
| 2896 | self._mock_gerrit_changes_for_detail_cache() |
| 2897 | self.calls = [ |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 2898 | (('GetChangeDetail', 'host', '1', ['C', 'A', 'B']), 'cab'), |
| 2899 | (('GetChangeDetail', 'host', '1', ['A', 'D']), 'ad'), |
| 2900 | (('GetChangeDetail', 'host', '1', ['A']), 'a'), # no_cache=True |
| 2901 | (('GetChangeDetail', 'host', '1', ['B']), 'b'), # no longer in cache. |
Andrii Shyshkalov | 258e0a6 | 2017-01-24 16:50:57 +0100 | [diff] [blame] | 2902 | ] |
| 2903 | cl = git_cl.Changelist(issue=1, codereview='gerrit') |
| 2904 | self.assertEqual(cl._GetChangeDetail(options=['C', 'A', 'B']), 'cab') |
| 2905 | self.assertEqual(cl._GetChangeDetail(options=['A', 'B', 'C']), 'cab') |
| 2906 | self.assertEqual(cl._GetChangeDetail(options=['B', 'A']), 'cab') |
| 2907 | self.assertEqual(cl._GetChangeDetail(options=['C']), 'cab') |
| 2908 | self.assertEqual(cl._GetChangeDetail(options=['A']), 'cab') |
| 2909 | self.assertEqual(cl._GetChangeDetail(), 'cab') |
| 2910 | |
| 2911 | self.assertEqual(cl._GetChangeDetail(options=['A', 'D']), 'ad') |
| 2912 | self.assertEqual(cl._GetChangeDetail(options=['A']), 'cab') |
| 2913 | self.assertEqual(cl._GetChangeDetail(options=['D']), 'ad') |
| 2914 | self.assertEqual(cl._GetChangeDetail(), 'cab') |
| 2915 | |
| 2916 | # Finally, no_cache should invalidate all caches for given change. |
| 2917 | self.assertEqual(cl._GetChangeDetail(options=['A'], no_cache=True), 'a') |
| 2918 | self.assertEqual(cl._GetChangeDetail(options=['B']), 'b') |
| 2919 | |
Andrii Shyshkalov | 21fb824 | 2017-02-15 21:09:27 +0100 | [diff] [blame] | 2920 | def test_gerrit_description_caching(self): |
| 2921 | def gen_detail(rev, desc): |
| 2922 | return { |
| 2923 | 'current_revision': rev, |
| 2924 | 'revisions': {rev: {'commit': {'message': desc}}} |
| 2925 | } |
| 2926 | self.calls = [ |
| 2927 | (('GetChangeDetail', 'host', '1', |
| 2928 | ['CURRENT_REVISION', 'CURRENT_COMMIT']), |
| 2929 | gen_detail('rev1', 'desc1')), |
| 2930 | (('GetChangeDetail', 'host', '1', |
| 2931 | ['CURRENT_REVISION', 'CURRENT_COMMIT']), |
| 2932 | gen_detail('rev2', 'desc2')), |
| 2933 | ] |
| 2934 | |
| 2935 | self._mock_gerrit_changes_for_detail_cache() |
| 2936 | cl = git_cl.Changelist(issue=1, codereview='gerrit') |
| 2937 | self.assertEqual(cl.GetDescription(), 'desc1') |
| 2938 | self.assertEqual(cl.GetDescription(), 'desc1') # cache hit. |
| 2939 | self.assertEqual(cl.GetDescription(force=True), 'desc2') |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 2940 | |
Andrii Shyshkalov | 34924cd | 2017-03-15 17:08:32 +0100 | [diff] [blame] | 2941 | def test_print_current_creds(self): |
| 2942 | class CookiesAuthenticatorMock(object): |
| 2943 | def __init__(self): |
| 2944 | self.gitcookies = { |
| 2945 | 'host.googlesource.com': ('user', 'pass'), |
| 2946 | 'host-review.googlesource.com': ('user', 'pass'), |
| 2947 | } |
| 2948 | self.netrc = self |
| 2949 | self.netrc.hosts = { |
| 2950 | 'github.com': ('user2', None, 'pass2'), |
| 2951 | 'host2.googlesource.com': ('user3', None, 'pass'), |
| 2952 | } |
| 2953 | self.mock(git_cl.gerrit_util, 'CookiesAuthenticator', |
| 2954 | CookiesAuthenticatorMock) |
| 2955 | self.mock(sys, 'stdout', StringIO.StringIO()) |
| 2956 | git_cl._GitCookiesChecker().print_current_creds(include_netrc=True) |
| 2957 | self.assertEqual(list(sys.stdout.getvalue().splitlines()), [ |
| 2958 | ' Host\t User\t Which file', |
| 2959 | '============================\t=====\t===========', |
| 2960 | 'host-review.googlesource.com\t user\t.gitcookies', |
| 2961 | ' host.googlesource.com\t user\t.gitcookies', |
| 2962 | ' host2.googlesource.com\tuser3\t .netrc', |
| 2963 | ]) |
| 2964 | sys.stdout.buf = '' |
| 2965 | git_cl._GitCookiesChecker().print_current_creds(include_netrc=False) |
| 2966 | self.assertEqual(list(sys.stdout.getvalue().splitlines()), [ |
| 2967 | ' Host\tUser\t Which file', |
| 2968 | '============================\t====\t===========', |
| 2969 | 'host-review.googlesource.com\tuser\t.gitcookies', |
| 2970 | ' host.googlesource.com\tuser\t.gitcookies', |
| 2971 | ]) |
| 2972 | |
Andrii Shyshkalov | 353637c | 2017-03-14 16:52:18 +0100 | [diff] [blame] | 2973 | def _common_creds_check_mocks(self): |
| 2974 | def exists_mock(path): |
| 2975 | dirname = os.path.dirname(path) |
| 2976 | if dirname == os.path.expanduser('~'): |
| 2977 | dirname = '~' |
| 2978 | base = os.path.basename(path) |
| 2979 | if base in ('.netrc', '.gitcookies'): |
| 2980 | return self._mocked_call('os.path.exists', '%s/%s' % (dirname, base)) |
| 2981 | # git cl also checks for existence other files not relevant to this test. |
| 2982 | return None |
| 2983 | self.mock(os.path, 'exists', exists_mock) |
| 2984 | self.mock(sys, 'stdout', StringIO.StringIO()) |
| 2985 | |
| 2986 | def test_creds_check_gitcookies_not_configured(self): |
| 2987 | self._common_creds_check_mocks() |
Andrii Shyshkalov | 34924cd | 2017-03-15 17:08:32 +0100 | [diff] [blame] | 2988 | self.mock(git_cl._GitCookiesChecker, 'get_hosts_with_creds', |
| 2989 | lambda _, include_netrc: []) |
Andrii Shyshkalov | 353637c | 2017-03-14 16:52:18 +0100 | [diff] [blame] | 2990 | self.calls = [ |
| 2991 | ((['git', 'config', '--global', 'http.cookiefile'],), CERR1), |
| 2992 | (('os.path.exists', '~/.netrc'), True), |
| 2993 | (('ask_for_data', 'Press Enter to setup .gitcookies, ' |
| 2994 | 'or Ctrl+C to abort'), ''), |
| 2995 | ((['git', 'config', '--global', 'http.cookiefile', |
| 2996 | os.path.expanduser('~/.gitcookies')], ), ''), |
| 2997 | ] |
| 2998 | self.assertEqual(0, git_cl.main(['creds-check'])) |
| 2999 | self.assertRegexpMatches( |
| 3000 | sys.stdout.getvalue(), |
| 3001 | '^You seem to be using outdated .netrc for git credentials:') |
| 3002 | self.assertRegexpMatches( |
| 3003 | sys.stdout.getvalue(), |
| 3004 | '\nConfigured git to use .gitcookies from') |
| 3005 | |
| 3006 | def test_creds_check_gitcookies_configured_custom_broken(self): |
| 3007 | self._common_creds_check_mocks() |
Andrii Shyshkalov | 34924cd | 2017-03-15 17:08:32 +0100 | [diff] [blame] | 3008 | self.mock(git_cl._GitCookiesChecker, 'get_hosts_with_creds', |
| 3009 | lambda _, include_netrc: []) |
Andrii Shyshkalov | 353637c | 2017-03-14 16:52:18 +0100 | [diff] [blame] | 3010 | self.calls = [ |
| 3011 | ((['git', 'config', '--global', 'http.cookiefile'],), |
| 3012 | '/custom/.gitcookies'), |
| 3013 | (('os.path.exists', '/custom/.gitcookies'), False), |
| 3014 | (('ask_for_data', 'Reconfigure git to use default .gitcookies? ' |
| 3015 | 'Press Enter to reconfigure, or Ctrl+C to abort'), ''), |
| 3016 | ((['git', 'config', '--global', 'http.cookiefile', |
| 3017 | os.path.expanduser('~/.gitcookies')], ), ''), |
| 3018 | ] |
| 3019 | self.assertEqual(0, git_cl.main(['creds-check'])) |
| 3020 | self.assertRegexpMatches( |
| 3021 | sys.stdout.getvalue(), |
| 3022 | 'WARNING: you have configured custom path to .gitcookies: ') |
| 3023 | self.assertRegexpMatches( |
| 3024 | sys.stdout.getvalue(), |
| 3025 | 'However, your configured .gitcookies file is missing.') |
| 3026 | |
Andrii Shyshkalov | 34924cd | 2017-03-15 17:08:32 +0100 | [diff] [blame] | 3027 | |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 3028 | if __name__ == '__main__': |
Andrii Shyshkalov | 18df0cd | 2017-01-25 15:22:20 +0100 | [diff] [blame] | 3029 | logging.basicConfig( |
| 3030 | level=logging.DEBUG if '-v' in sys.argv else logging.ERROR) |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 3031 | unittest.main() |