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