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