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