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