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