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