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