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 | 8515328 | 2020-02-14 22:06:29 +0000 | [diff] [blame] | 9 | from __future__ import print_function |
Edward Lemur | 0db01f0 | 2019-11-12 22:01:51 +0000 | [diff] [blame] | 10 | from __future__ import unicode_literals |
| 11 | |
Andrii Shyshkalov | d8aa49f | 2017-03-17 16:05:49 +0100 | [diff] [blame] | 12 | import datetime |
tandrii | de281ae | 2016-10-12 06:02:30 -0700 | [diff] [blame] | 13 | import json |
Andrii Shyshkalov | 18df0cd | 2017-01-25 15:22:20 +0100 | [diff] [blame] | 14 | import logging |
Edward Lemur | 61bf417 | 2020-02-24 23:22:37 +0000 | [diff] [blame] | 15 | import multiprocessing |
Quinten Yearsley | ee8be8a | 2020-03-05 21:48:32 +0000 | [diff] [blame] | 16 | import optparse |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 17 | import os |
Edward Lemur | 8515328 | 2020-02-14 22:06:29 +0000 | [diff] [blame] | 18 | import pprint |
Brian Sheedy | 59b06a8 | 2019-10-14 17:03:29 +0000 | [diff] [blame] | 19 | import shutil |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 20 | import sys |
Aaron Gable | 9a03ae0 | 2017-11-03 11:31:07 -0700 | [diff] [blame] | 21 | import tempfile |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 22 | import unittest |
| 23 | |
Edward Lemur | a814502 | 2020-01-06 18:47:54 +0000 | [diff] [blame] | 24 | if sys.version_info.major == 2: |
| 25 | from StringIO import StringIO |
| 26 | import mock |
| 27 | else: |
| 28 | from io import StringIO |
| 29 | from unittest import mock |
| 30 | |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 31 | sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 32 | |
Edward Lemur | 5ba1e9c | 2018-07-23 18:19:02 +0000 | [diff] [blame] | 33 | import metrics |
| 34 | # We have to disable monitoring before importing git_cl. |
| 35 | metrics.DISABLE_METRICS_COLLECTION = True |
| 36 | |
Jamie Madill | 5e96ad1 | 2020-01-13 16:08:35 +0000 | [diff] [blame] | 37 | import clang_format |
Edward Lemur | 227d510 | 2020-02-25 23:45:35 +0000 | [diff] [blame] | 38 | import contextlib |
Edward Lemur | 1773f37 | 2020-02-22 00:27:14 +0000 | [diff] [blame] | 39 | import gclient_utils |
Eric Boren | 2fb6310 | 2018-10-05 13:05:03 +0000 | [diff] [blame] | 40 | import gerrit_util |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 41 | import git_cl |
iannucci@chromium.org | 9e84927 | 2014-04-04 00:31:55 +0000 | [diff] [blame] | 42 | import git_common |
tandrii@chromium.org | 57d8654 | 2016-03-04 16:11:32 +0000 | [diff] [blame] | 43 | import git_footers |
Edward Lemur | 8515328 | 2020-02-14 22:06:29 +0000 | [diff] [blame] | 44 | import git_new_branch |
Edward Lesmes | 0e4e5ae | 2021-01-08 18:28:46 +0000 | [diff] [blame] | 45 | import owners_client |
Edward Lemur | 8515328 | 2020-02-14 22:06:29 +0000 | [diff] [blame] | 46 | import scm |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 47 | import subprocess2 |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 48 | |
Josip Sokcevic | 464e9ff | 2020-03-18 23:48:55 +0000 | [diff] [blame] | 49 | NETRC_FILENAME = '_netrc' if sys.platform == 'win32' else '.netrc' |
| 50 | |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 51 | |
Edward Lemur | 0db01f0 | 2019-11-12 22:01:51 +0000 | [diff] [blame] | 52 | def callError(code=1, cmd='', cwd='', stdout=b'', stderr=b''): |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 53 | return subprocess2.CalledProcessError(code, cmd, cwd, stdout, stderr) |
| 54 | |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 55 | CERR1 = callError(1) |
| 56 | |
| 57 | |
Edward Lemur | 1773f37 | 2020-02-22 00:27:14 +0000 | [diff] [blame] | 58 | class TemporaryFileMock(object): |
| 59 | def __init__(self): |
| 60 | self.suffix = 0 |
Aaron Gable | 9a03ae0 | 2017-11-03 11:31:07 -0700 | [diff] [blame] | 61 | |
Edward Lemur | 1773f37 | 2020-02-22 00:27:14 +0000 | [diff] [blame] | 62 | @contextlib.contextmanager |
| 63 | def __call__(self): |
| 64 | self.suffix += 1 |
| 65 | yield '/tmp/fake-temp' + str(self.suffix) |
Aaron Gable | 9a03ae0 | 2017-11-03 11:31:07 -0700 | [diff] [blame] | 66 | |
| 67 | |
martiniss@chromium.org | d6648e2 | 2016-04-29 19:22:16 +0000 | [diff] [blame] | 68 | class ChangelistMock(object): |
| 69 | # A class variable so we can access it when we don't have access to the |
| 70 | # instance that's being set. |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 71 | desc = '' |
Quinten Yearsley | ee8be8a | 2020-03-05 21:48:32 +0000 | [diff] [blame] | 72 | |
| 73 | def __init__(self, gerrit_change=None, **kwargs): |
| 74 | self._gerrit_change = gerrit_change |
| 75 | |
martiniss@chromium.org | d6648e2 | 2016-04-29 19:22:16 +0000 | [diff] [blame] | 76 | def GetIssue(self): |
| 77 | return 1 |
Quinten Yearsley | ee8be8a | 2020-03-05 21:48:32 +0000 | [diff] [blame] | 78 | |
Edward Lemur | 6c6827c | 2020-02-06 21:15:18 +0000 | [diff] [blame] | 79 | def FetchDescription(self): |
martiniss@chromium.org | d6648e2 | 2016-04-29 19:22:16 +0000 | [diff] [blame] | 80 | return ChangelistMock.desc |
Quinten Yearsley | ee8be8a | 2020-03-05 21:48:32 +0000 | [diff] [blame] | 81 | |
dsansome | e2d6fd9 | 2016-09-08 00:10:47 -0700 | [diff] [blame] | 82 | def UpdateDescription(self, desc, force=False): |
martiniss@chromium.org | d6648e2 | 2016-04-29 19:22:16 +0000 | [diff] [blame] | 83 | ChangelistMock.desc = desc |
| 84 | |
Quinten Yearsley | ee8be8a | 2020-03-05 21:48:32 +0000 | [diff] [blame] | 85 | def GetGerritChange(self, patchset=None, **kwargs): |
| 86 | del patchset |
| 87 | return self._gerrit_change |
| 88 | |
Josip Sokcevic | 9011a5b | 2021-02-12 18:59:44 +0000 | [diff] [blame] | 89 | def GetRemoteBranch(self): |
| 90 | return ('origin', 'refs/remotes/origin/main') |
| 91 | |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 92 | |
Edward Lemur | 8515328 | 2020-02-14 22:06:29 +0000 | [diff] [blame] | 93 | class GitMocks(object): |
| 94 | def __init__(self, config=None, branchref=None): |
| 95 | self.branchref = branchref or 'refs/heads/master' |
| 96 | self.config = config or {} |
| 97 | |
| 98 | def GetBranchRef(self, _root): |
| 99 | return self.branchref |
| 100 | |
| 101 | def NewBranch(self, branchref): |
| 102 | self.branchref = branchref |
| 103 | |
Edward Lemur | 2696407 | 2020-02-19 19:18:51 +0000 | [diff] [blame] | 104 | def GetConfig(self, root, key, default=None): |
| 105 | if root != '': |
| 106 | key = '%s:%s' % (root, key) |
Edward Lemur | 8515328 | 2020-02-14 22:06:29 +0000 | [diff] [blame] | 107 | return self.config.get(key, default) |
| 108 | |
Edward Lemur | 2696407 | 2020-02-19 19:18:51 +0000 | [diff] [blame] | 109 | def SetConfig(self, root, key, value=None): |
| 110 | if root != '': |
| 111 | key = '%s:%s' % (root, key) |
Edward Lemur | 8515328 | 2020-02-14 22:06:29 +0000 | [diff] [blame] | 112 | if value: |
| 113 | self.config[key] = value |
| 114 | return |
| 115 | if key not in self.config: |
| 116 | raise CERR1 |
| 117 | del self.config[key] |
| 118 | |
| 119 | |
maruel@chromium.org | 2e72bb1 | 2012-01-17 15:18:35 +0000 | [diff] [blame] | 120 | class WatchlistsMock(object): |
| 121 | def __init__(self, _): |
| 122 | pass |
| 123 | @staticmethod |
| 124 | def GetWatchersForPaths(_): |
| 125 | return ['joe@example.com'] |
| 126 | |
| 127 | |
Edward Lemur | 4c707a2 | 2019-09-24 21:13:43 +0000 | [diff] [blame] | 128 | class CodereviewSettingsFileMock(object): |
| 129 | def __init__(self): |
| 130 | pass |
| 131 | # pylint: disable=no-self-use |
| 132 | def read(self): |
| 133 | return ('CODE_REVIEW_SERVER: gerrit.chromium.org\n' + |
| 134 | 'GERRIT_HOST: True\n') |
| 135 | |
| 136 | |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 137 | class AuthenticatorMock(object): |
| 138 | def __init__(self, *_args): |
| 139 | pass |
| 140 | def has_cached_credentials(self): |
| 141 | return True |
tandrii | 221ab25 | 2016-10-06 08:12:04 -0700 | [diff] [blame] | 142 | def authorize(self, http): |
| 143 | return http |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 144 | |
| 145 | |
Andrii Shyshkalov | bb86fbb | 2017-03-24 14:59:28 +0100 | [diff] [blame] | 146 | def CookiesAuthenticatorMockFactory(hosts_with_creds=None, same_auth=False): |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 147 | """Use to mock Gerrit/Git credentials from ~/.netrc or ~/.gitcookies. |
| 148 | |
| 149 | Usage: |
| 150 | >>> self.mock(git_cl.gerrit_util, "CookiesAuthenticator", |
Andrii Shyshkalov | bb86fbb | 2017-03-24 14:59:28 +0100 | [diff] [blame] | 151 | CookiesAuthenticatorMockFactory({'host': ('user', _, 'pass')}) |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 152 | |
| 153 | OR |
| 154 | >>> self.mock(git_cl.gerrit_util, "CookiesAuthenticator", |
Andrii Shyshkalov | bb86fbb | 2017-03-24 14:59:28 +0100 | [diff] [blame] | 155 | CookiesAuthenticatorMockFactory( |
| 156 | same_auth=('user', '', 'pass')) |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 157 | """ |
| 158 | class CookiesAuthenticatorMock(git_cl.gerrit_util.CookiesAuthenticator): |
Quinten Yearsley | b2cc4a9 | 2016-12-15 13:53:26 -0800 | [diff] [blame] | 159 | def __init__(self): # pylint: disable=super-init-not-called |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 160 | # Intentionally not calling super() because it reads actual cookie files. |
| 161 | pass |
| 162 | @classmethod |
| 163 | def get_gitcookies_path(cls): |
Josip Sokcevic | 464e9ff | 2020-03-18 23:48:55 +0000 | [diff] [blame] | 164 | return os.path.join('~', '.gitcookies') |
| 165 | |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 166 | @classmethod |
| 167 | def get_netrc_path(cls): |
Josip Sokcevic | 464e9ff | 2020-03-18 23:48:55 +0000 | [diff] [blame] | 168 | return os.path.join('~', NETRC_FILENAME) |
| 169 | |
Andrii Shyshkalov | bb86fbb | 2017-03-24 14:59:28 +0100 | [diff] [blame] | 170 | def _get_auth_for_host(self, host): |
| 171 | if same_auth: |
| 172 | return same_auth |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 173 | return (hosts_with_creds or {}).get(host) |
| 174 | return CookiesAuthenticatorMock |
| 175 | |
Aaron Gable | 9a03ae0 | 2017-11-03 11:31:07 -0700 | [diff] [blame] | 176 | |
kmarshall | 9249e01 | 2016-08-23 12:02:16 -0700 | [diff] [blame] | 177 | class MockChangelistWithBranchAndIssue(): |
| 178 | def __init__(self, branch, issue): |
| 179 | self.branch = branch |
| 180 | self.issue = issue |
| 181 | def GetBranch(self): |
| 182 | return self.branch |
| 183 | def GetIssue(self): |
| 184 | return self.issue |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 185 | |
tandrii | c2405f5 | 2016-10-10 08:13:15 -0700 | [diff] [blame] | 186 | |
| 187 | class SystemExitMock(Exception): |
| 188 | pass |
| 189 | |
| 190 | |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 191 | class TestGitClBasic(unittest.TestCase): |
Josip Sokcevic | 953278a | 2020-02-28 19:46:36 +0000 | [diff] [blame] | 192 | def setUp(self): |
| 193 | mock.patch('sys.exit', side_effect=SystemExitMock).start() |
| 194 | mock.patch('sys.stdout', StringIO()).start() |
| 195 | mock.patch('sys.stderr', StringIO()).start() |
| 196 | self.addCleanup(mock.patch.stopall) |
| 197 | |
| 198 | def test_die_with_error(self): |
| 199 | with self.assertRaises(SystemExitMock): |
| 200 | git_cl.DieWithError('foo', git_cl.ChangeDescription('lorem ipsum')) |
| 201 | self.assertEqual(sys.stderr.getvalue(), 'foo\n') |
| 202 | self.assertTrue('saving CL description' in sys.stdout.getvalue()) |
| 203 | self.assertTrue('Content of CL description' in sys.stdout.getvalue()) |
| 204 | self.assertTrue('lorem ipsum' in sys.stdout.getvalue()) |
| 205 | sys.exit.assert_called_once_with(1) |
| 206 | |
| 207 | def test_die_with_error_no_desc(self): |
| 208 | with self.assertRaises(SystemExitMock): |
| 209 | git_cl.DieWithError('foo') |
| 210 | self.assertEqual(sys.stderr.getvalue(), 'foo\n') |
| 211 | self.assertEqual(sys.stdout.getvalue(), '') |
| 212 | sys.exit.assert_called_once_with(1) |
| 213 | |
Edward Lemur | 6c6827c | 2020-02-06 21:15:18 +0000 | [diff] [blame] | 214 | def test_fetch_description(self): |
Edward Lemur | f38bc17 | 2019-09-03 21:02:13 +0000 | [diff] [blame] | 215 | cl = git_cl.Changelist(issue=1, codereview_host='host') |
Andrii Shyshkalov | 3186301 | 2017-02-08 11:35:12 +0100 | [diff] [blame] | 216 | cl.description = 'x' |
Edward Lemur | 6c6827c | 2020-02-06 21:15:18 +0000 | [diff] [blame] | 217 | self.assertEqual(cl.FetchDescription(), 'x') |
Robert Iannucci | 09f1f3d | 2017-03-28 16:54:32 -0700 | [diff] [blame] | 218 | |
Edward Lemur | 61bf417 | 2020-02-24 23:22:37 +0000 | [diff] [blame] | 219 | @mock.patch('git_cl.Changelist.EnsureAuthenticated') |
| 220 | @mock.patch('git_cl.Changelist.GetStatus', lambda cl: cl.status) |
| 221 | def test_get_cl_statuses(self, *_mocks): |
| 222 | statuses = [ |
| 223 | 'closed', 'commit', 'dry-run', 'lgtm', 'reply', 'unsent', 'waiting'] |
| 224 | changes = [] |
| 225 | for status in statuses: |
| 226 | cl = git_cl.Changelist() |
| 227 | cl.status = status |
| 228 | changes.append(cl) |
| 229 | |
| 230 | actual = set(git_cl.get_cl_statuses(changes, True)) |
| 231 | self.assertEqual(set(zip(changes, statuses)), actual) |
| 232 | |
Josip Sokcevic | f736cab | 2020-10-20 23:41:38 +0000 | [diff] [blame] | 233 | def test_upload_to_non_default_branch_no_retry(self): |
| 234 | m = mock.patch('git_cl.Changelist._CMDUploadChange', |
| 235 | side_effect=[git_cl.GitPushError(), None]).start() |
| 236 | mock.patch('git_cl.Changelist.GetRemoteBranch', |
| 237 | return_value=('foo', 'bar')).start() |
Edward Lesmes | eeca9c6 | 2020-11-20 00:00:17 +0000 | [diff] [blame] | 238 | mock.patch('git_cl.Changelist.GetGerritProject', |
Josip Sokcevic | f736cab | 2020-10-20 23:41:38 +0000 | [diff] [blame] | 239 | return_value='foo').start() |
| 240 | mock.patch('git_cl.gerrit_util.GetProjectHead', |
| 241 | return_value='refs/heads/main').start() |
| 242 | |
| 243 | cl = git_cl.Changelist() |
| 244 | options = optparse.Values() |
Josip Sokcevic | b631a88 | 2021-01-06 18:18:10 +0000 | [diff] [blame] | 245 | options.target_branch = 'refs/heads/bar' |
Josip Sokcevic | f736cab | 2020-10-20 23:41:38 +0000 | [diff] [blame] | 246 | with self.assertRaises(SystemExitMock): |
| 247 | cl.CMDUploadChange(options, [], 'foo', git_cl.ChangeDescription('bar')) |
| 248 | |
| 249 | # ensure upload is called once |
| 250 | self.assertEqual(len(m.mock_calls), 1) |
| 251 | sys.exit.assert_called_once_with(1) |
| 252 | # option not set as retry didn't happen |
| 253 | self.assertFalse(hasattr(options, 'force')) |
| 254 | self.assertFalse(hasattr(options, 'edit_description')) |
| 255 | |
| 256 | def test_upload_to_old_default_still_active(self): |
| 257 | m = mock.patch('git_cl.Changelist._CMDUploadChange', |
| 258 | side_effect=[git_cl.GitPushError(), None]).start() |
| 259 | mock.patch('git_cl.Changelist.GetRemoteBranch', |
| 260 | return_value=('foo', git_cl.DEFAULT_OLD_BRANCH)).start() |
Edward Lesmes | eeca9c6 | 2020-11-20 00:00:17 +0000 | [diff] [blame] | 261 | mock.patch('git_cl.Changelist.GetGerritProject', |
Josip Sokcevic | f736cab | 2020-10-20 23:41:38 +0000 | [diff] [blame] | 262 | return_value='foo').start() |
| 263 | mock.patch('git_cl.gerrit_util.GetProjectHead', |
Josip Sokcevic | b631a88 | 2021-01-06 18:18:10 +0000 | [diff] [blame] | 264 | return_value='refs/heads/master').start() |
Josip Sokcevic | f736cab | 2020-10-20 23:41:38 +0000 | [diff] [blame] | 265 | |
| 266 | cl = git_cl.Changelist() |
| 267 | options = optparse.Values() |
Josip Sokcevic | b631a88 | 2021-01-06 18:18:10 +0000 | [diff] [blame] | 268 | options.target_branch = 'refs/heads/master' |
Josip Sokcevic | f736cab | 2020-10-20 23:41:38 +0000 | [diff] [blame] | 269 | with self.assertRaises(SystemExitMock): |
| 270 | cl.CMDUploadChange(options, [], 'foo', git_cl.ChangeDescription('bar')) |
| 271 | |
| 272 | # ensure upload is called once |
| 273 | self.assertEqual(len(m.mock_calls), 1) |
| 274 | sys.exit.assert_called_once_with(1) |
| 275 | # option not set as retry didn't happen |
| 276 | self.assertFalse(hasattr(options, 'force')) |
| 277 | self.assertFalse(hasattr(options, 'edit_description')) |
| 278 | |
| 279 | def test_upload_to_old_default_retry_on_failure(self): |
| 280 | m = mock.patch('git_cl.Changelist._CMDUploadChange', |
| 281 | side_effect=[git_cl.GitPushError(), None]).start() |
| 282 | mock.patch('git_cl.Changelist.GetRemoteBranch', |
| 283 | return_value=('foo', git_cl.DEFAULT_OLD_BRANCH)).start() |
Edward Lesmes | eeca9c6 | 2020-11-20 00:00:17 +0000 | [diff] [blame] | 284 | mock.patch('git_cl.Changelist.GetGerritProject', |
Josip Sokcevic | f736cab | 2020-10-20 23:41:38 +0000 | [diff] [blame] | 285 | return_value='foo').start() |
| 286 | mock.patch('git_cl.gerrit_util.GetProjectHead', |
| 287 | return_value='refs/heads/main').start() |
| 288 | mock.patch('git_cl.RunGit').start() |
| 289 | |
| 290 | cl = git_cl.Changelist() |
| 291 | options = optparse.Values() |
Josip Sokcevic | b631a88 | 2021-01-06 18:18:10 +0000 | [diff] [blame] | 292 | options.target_branch = 'refs/heads/master' |
| 293 | cl.CMDUploadChange(options, [], 'foo', git_cl.ChangeDescription('bar')) |
| 294 | # ensure upload is called twice |
| 295 | self.assertEqual(len(m.mock_calls), 2) |
| 296 | # option overrides on retry |
| 297 | self.assertEqual(options.force, True) |
| 298 | self.assertEqual(options.edit_description, False) |
| 299 | |
Gavin Mak | 68e6cf3 | 2021-01-25 18:24:08 +0000 | [diff] [blame] | 300 | def test_upload_with_message_file_no_editor(self): |
| 301 | m = mock.patch('git_cl.ChangeDescription.prompt', |
| 302 | return_value=None).start() |
| 303 | mock.patch('git_cl.Changelist.GetRemoteBranch', |
| 304 | return_value=('foo', git_cl.DEFAULT_NEW_BRANCH)).start() |
| 305 | mock.patch('git_cl.GetTargetRef', |
| 306 | return_value='refs/heads/main').start() |
| 307 | mock.patch('git_cl.Changelist._GerritCommitMsgHookCheck', |
| 308 | lambda _, offer_removal: None).start() |
| 309 | mock.patch('git_cl.Changelist.GetIssue', return_value=None).start() |
| 310 | mock.patch('git_cl.Changelist.GetBranch', |
| 311 | side_effect=SystemExitMock).start() |
| 312 | mock.patch('git_cl.GenerateGerritChangeId', return_value=None).start() |
| 313 | mock.patch('git_cl.RunGit').start() |
| 314 | |
| 315 | cl = git_cl.Changelist() |
| 316 | options = optparse.Values() |
| 317 | options.target_branch = 'refs/heads/master' |
| 318 | options.squash = True |
| 319 | options.edit_description = False |
| 320 | options.force = False |
| 321 | options.preserve_tryjobs = False |
| 322 | options.message_file = "message.txt" |
| 323 | |
| 324 | with self.assertRaises(SystemExitMock): |
| 325 | cl.CMDUploadChange(options, [], 'foo', git_cl.ChangeDescription('bar')) |
| 326 | self.assertEqual(len(m.mock_calls), 0) |
| 327 | |
| 328 | options.message_file = None |
| 329 | with self.assertRaises(SystemExitMock): |
| 330 | cl.CMDUploadChange(options, [], 'foo', git_cl.ChangeDescription('bar')) |
| 331 | self.assertEqual(len(m.mock_calls), 1) |
| 332 | |
Josip Sokcevic | b631a88 | 2021-01-06 18:18:10 +0000 | [diff] [blame] | 333 | def test_upload_to_old_default_retry_on_rollback(self): |
| 334 | """Test when default branch migration had to be rolled back to old name""" |
| 335 | m = mock.patch('git_cl.Changelist._CMDUploadChange', |
| 336 | side_effect=[git_cl.GitPushError(), None]).start() |
| 337 | mock.patch('git_cl.Changelist.GetRemoteBranch', |
| 338 | return_value=('foo', git_cl.DEFAULT_NEW_BRANCH)).start() |
| 339 | mock.patch('git_cl.Changelist.GetGerritProject', |
| 340 | return_value='foo').start() |
| 341 | mock.patch('git_cl.gerrit_util.GetProjectHead', |
| 342 | return_value='refs/heads/master').start() |
| 343 | # GetTargetRef returns new default branch since it has stale remote |
| 344 | # information. |
| 345 | mock.patch('git_cl.GetTargetRef', |
| 346 | return_value='refs/heads/main').start() |
| 347 | mock.patch('git_cl.RunGit').start() |
| 348 | |
| 349 | cl = git_cl.Changelist() |
| 350 | options = optparse.Values() |
| 351 | options.target_branch = 'refs/heads/master' |
Josip Sokcevic | f736cab | 2020-10-20 23:41:38 +0000 | [diff] [blame] | 352 | cl.CMDUploadChange(options, [], 'foo', git_cl.ChangeDescription('bar')) |
| 353 | # ensure upload is called twice |
| 354 | self.assertEqual(len(m.mock_calls), 2) |
| 355 | # option overrides on retry |
| 356 | self.assertEqual(options.force, True) |
| 357 | self.assertEqual(options.edit_description, False) |
| 358 | |
Edward Lemur | 61bf417 | 2020-02-24 23:22:37 +0000 | [diff] [blame] | 359 | def test_get_cl_statuses_no_changes(self): |
| 360 | self.assertEqual([], list(git_cl.get_cl_statuses([], True))) |
| 361 | |
| 362 | @mock.patch('git_cl.Changelist.EnsureAuthenticated') |
| 363 | @mock.patch('multiprocessing.pool.ThreadPool') |
| 364 | def test_get_cl_statuses_timeout(self, *_mocks): |
| 365 | changes = [git_cl.Changelist() for _ in range(2)] |
| 366 | pool = multiprocessing.pool.ThreadPool() |
| 367 | it = pool.imap_unordered.return_value.__iter__ = mock.Mock() |
| 368 | it.return_value.next.side_effect = [ |
| 369 | (changes[0], 'lgtm'), |
| 370 | multiprocessing.TimeoutError, |
| 371 | ] |
| 372 | |
| 373 | actual = list(git_cl.get_cl_statuses(changes, True)) |
| 374 | self.assertEqual([(changes[0], 'lgtm'), (changes[1], 'error')], actual) |
| 375 | |
| 376 | @mock.patch('git_cl.Changelist.GetIssueURL') |
| 377 | def test_get_cl_statuses_not_finegrained(self, _mock): |
| 378 | changes = [git_cl.Changelist() for _ in range(2)] |
| 379 | urls = ['some-url', None] |
| 380 | git_cl.Changelist.GetIssueURL.side_effect = urls |
| 381 | |
| 382 | actual = set(git_cl.get_cl_statuses(changes, False)) |
| 383 | self.assertEqual( |
| 384 | set([(changes[0], 'waiting'), (changes[1], 'error')]), actual) |
| 385 | |
Andrii Shyshkalov | 1ee78cd | 2020-03-12 01:31:53 +0000 | [diff] [blame] | 386 | def test_get_issue_url(self): |
| 387 | cl = git_cl.Changelist(issue=123) |
| 388 | cl._gerrit_server = 'https://example.com' |
| 389 | self.assertEqual(cl.GetIssueURL(), 'https://example.com/123') |
| 390 | self.assertEqual(cl.GetIssueURL(short=True), 'https://example.com/123') |
| 391 | |
| 392 | cl = git_cl.Changelist(issue=123) |
| 393 | cl._gerrit_server = 'https://chromium-review.googlesource.com' |
| 394 | self.assertEqual(cl.GetIssueURL(), |
| 395 | 'https://chromium-review.googlesource.com/123') |
| 396 | self.assertEqual(cl.GetIssueURL(short=True), 'https://crrev.com/c/123') |
| 397 | |
Andrii Shyshkalov | 71f0da3 | 2019-07-15 22:45:18 +0000 | [diff] [blame] | 398 | def test_set_preserve_tryjobs(self): |
| 399 | d = git_cl.ChangeDescription('Simple.') |
| 400 | d.set_preserve_tryjobs() |
| 401 | self.assertEqual(d.description.splitlines(), [ |
| 402 | 'Simple.', |
| 403 | '', |
| 404 | 'Cq-Do-Not-Cancel-Tryjobs: true', |
| 405 | ]) |
| 406 | before = d.description |
| 407 | d.set_preserve_tryjobs() |
| 408 | self.assertEqual(before, d.description) |
| 409 | |
| 410 | d = git_cl.ChangeDescription('\n'.join([ |
| 411 | 'One is enough', |
| 412 | '', |
| 413 | 'Cq-Do-Not-Cancel-Tryjobs: dups not encouraged, but don\'t hurt', |
| 414 | 'Change-Id: Ideadbeef', |
| 415 | ])) |
| 416 | d.set_preserve_tryjobs() |
| 417 | self.assertEqual(d.description.splitlines(), [ |
| 418 | 'One is enough', |
| 419 | '', |
| 420 | 'Cq-Do-Not-Cancel-Tryjobs: dups not encouraged, but don\'t hurt', |
| 421 | 'Change-Id: Ideadbeef', |
| 422 | 'Cq-Do-Not-Cancel-Tryjobs: true', |
| 423 | ]) |
| 424 | |
tandrii | f9aefb7 | 2016-07-01 09:06:51 -0700 | [diff] [blame] | 425 | def test_get_bug_line_values(self): |
| 426 | f = lambda p, bugs: list(git_cl._get_bug_line_values(p, bugs)) |
| 427 | self.assertEqual(f('', ''), []) |
| 428 | self.assertEqual(f('', '123,v8:456'), ['123', 'v8:456']) |
Lei Zhang | 8a0efc1 | 2020-08-05 19:58:45 +0000 | [diff] [blame] | 429 | # Prefix that ends with colon. |
| 430 | self.assertEqual(f('v8:', '456'), ['v8:456']) |
| 431 | self.assertEqual(f('v8:', 'chromium:123,456'), ['v8:456', 'chromium:123']) |
| 432 | # Prefix that ends without colon. |
tandrii | f9aefb7 | 2016-07-01 09:06:51 -0700 | [diff] [blame] | 433 | self.assertEqual(f('v8', '456'), ['v8:456']) |
| 434 | self.assertEqual(f('v8', 'chromium:123,456'), ['v8:456', 'chromium:123']) |
| 435 | # Not nice, but not worth carying. |
Lei Zhang | 8a0efc1 | 2020-08-05 19:58:45 +0000 | [diff] [blame] | 436 | self.assertEqual(f('v8:', 'chromium:123,456,v8:123'), |
| 437 | ['v8:456', 'chromium:123', 'v8:123']) |
tandrii | f9aefb7 | 2016-07-01 09:06:51 -0700 | [diff] [blame] | 438 | self.assertEqual(f('v8', 'chromium:123,456,v8:123'), |
| 439 | ['v8:456', 'chromium:123', 'v8:123']) |
| 440 | |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 441 | @mock.patch('gerrit_util.GetAccountDetails') |
| 442 | def test_valid_accounts(self, mockGetAccountDetails): |
Andrii Shyshkalov | ba7b0a4 | 2018-10-15 03:20:35 +0000 | [diff] [blame] | 443 | mock_per_account = { |
| 444 | 'u1': None, # 404, doesn't exist. |
| 445 | 'u2': { |
| 446 | '_account_id': 123124, |
| 447 | 'avatars': [], |
| 448 | 'email': 'u2@example.com', |
| 449 | 'name': 'User Number 2', |
| 450 | 'status': 'OOO', |
| 451 | }, |
| 452 | 'u3': git_cl.gerrit_util.GerritError(500, 'retries didn\'t help :('), |
| 453 | } |
| 454 | def GetAccountDetailsMock(_, account): |
| 455 | # Poor-man's mock library's side_effect. |
| 456 | v = mock_per_account.pop(account) |
| 457 | if isinstance(v, Exception): |
| 458 | raise v |
| 459 | return v |
| 460 | |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 461 | mockGetAccountDetails.side_effect = GetAccountDetailsMock |
| 462 | actual = git_cl.gerrit_util.ValidAccounts( |
| 463 | 'host', ['u1', 'u2', 'u3'], max_threads=1) |
Andrii Shyshkalov | ba7b0a4 | 2018-10-15 03:20:35 +0000 | [diff] [blame] | 464 | self.assertEqual(actual, { |
| 465 | 'u2': { |
| 466 | '_account_id': 123124, |
| 467 | 'avatars': [], |
| 468 | 'email': 'u2@example.com', |
| 469 | 'name': 'User Number 2', |
| 470 | 'status': 'OOO', |
| 471 | }, |
| 472 | }) |
| 473 | |
| 474 | |
Andrii Shyshkalov | 90f3192 | 2017-04-10 16:10:21 +0200 | [diff] [blame] | 475 | class TestParseIssueURL(unittest.TestCase): |
Andrii Shyshkalov | 8aebb60 | 2020-04-16 22:10:27 +0000 | [diff] [blame] | 476 | def _test(self, arg, issue=None, patchset=None, hostname=None, fail=False): |
| 477 | parsed = git_cl.ParseIssueNumberArgument(arg) |
Andrii Shyshkalov | 90f3192 | 2017-04-10 16:10:21 +0200 | [diff] [blame] | 478 | self.assertIsNotNone(parsed) |
| 479 | if fail: |
| 480 | self.assertFalse(parsed.valid) |
| 481 | return |
| 482 | self.assertTrue(parsed.valid) |
| 483 | self.assertEqual(parsed.issue, issue) |
| 484 | self.assertEqual(parsed.patchset, patchset) |
| 485 | self.assertEqual(parsed.hostname, hostname) |
Andrii Shyshkalov | 90f3192 | 2017-04-10 16:10:21 +0200 | [diff] [blame] | 486 | |
Andrii Shyshkalov | 8aebb60 | 2020-04-16 22:10:27 +0000 | [diff] [blame] | 487 | def test_basic(self): |
| 488 | self._test('123', 123) |
| 489 | self._test('', fail=True) |
| 490 | self._test('abc', fail=True) |
| 491 | self._test('123/1', fail=True) |
| 492 | self._test('123a', fail=True) |
| 493 | self._test('ssh://chrome-review.source.com/#/c/123/4/', fail=True) |
| 494 | self._test('ssh://chrome-review.source.com/c/123/1/', fail=True) |
Andrii Shyshkalov | 90f3192 | 2017-04-10 16:10:21 +0200 | [diff] [blame] | 495 | |
Andrii Shyshkalov | 8aebb60 | 2020-04-16 22:10:27 +0000 | [diff] [blame] | 496 | def test_gerrit_url(self): |
| 497 | self._test('https://codereview.source.com/123', 123, None, |
| 498 | 'codereview.source.com') |
| 499 | self._test('http://chrome-review.source.com/c/123', 123, None, |
| 500 | 'chrome-review.source.com') |
| 501 | self._test('https://chrome-review.source.com/c/123/', 123, None, |
| 502 | 'chrome-review.source.com') |
| 503 | self._test('https://chrome-review.source.com/c/123/4', 123, 4, |
| 504 | 'chrome-review.source.com') |
| 505 | self._test('https://chrome-review.source.com/#/c/123/4', 123, 4, |
| 506 | 'chrome-review.source.com') |
| 507 | self._test('https://chrome-review.source.com/c/123/4', 123, 4, |
| 508 | 'chrome-review.source.com') |
| 509 | self._test('https://chrome-review.source.com/123', 123, None, |
| 510 | 'chrome-review.source.com') |
| 511 | self._test('https://chrome-review.source.com/123/4', 123, 4, |
| 512 | 'chrome-review.source.com') |
Andrii Shyshkalov | 90f3192 | 2017-04-10 16:10:21 +0200 | [diff] [blame] | 513 | |
Andrii Shyshkalov | 8aebb60 | 2020-04-16 22:10:27 +0000 | [diff] [blame] | 514 | self._test('https://chrome-review.source.com/bad/123/4', fail=True) |
| 515 | self._test('https://chrome-review.source.com/c/123/1/whatisthis', fail=True) |
| 516 | self._test('https://chrome-review.source.com/c/abc/', fail=True) |
Andrii Shyshkalov | 90f3192 | 2017-04-10 16:10:21 +0200 | [diff] [blame] | 517 | |
Andrii Shyshkalov | 8aebb60 | 2020-04-16 22:10:27 +0000 | [diff] [blame] | 518 | def test_short_urls(self): |
| 519 | self._test('https://crrev.com/c/2151934', 2151934, None, |
| 520 | 'chromium-review.googlesource.com') |
Andrii Shyshkalov | 90f3192 | 2017-04-10 16:10:21 +0200 | [diff] [blame] | 521 | |
| 522 | |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 523 | class GitCookiesCheckerTest(unittest.TestCase): |
Andrii Shyshkalov | 9780050 | 2017-03-16 16:04:32 +0100 | [diff] [blame] | 524 | def setUp(self): |
| 525 | super(GitCookiesCheckerTest, self).setUp() |
| 526 | self.c = git_cl._GitCookiesChecker() |
Andrii Shyshkalov | 0a0b067 | 2017-03-16 16:27:48 +0100 | [diff] [blame] | 527 | self.c._all_hosts = [] |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 528 | mock.patch('sys.stdout', StringIO()).start() |
| 529 | self.addCleanup(mock.patch.stopall) |
Andrii Shyshkalov | 9780050 | 2017-03-16 16:04:32 +0100 | [diff] [blame] | 530 | |
| 531 | def mock_hosts_creds(self, subhost_identity_pairs): |
| 532 | def ensure_googlesource(h): |
| 533 | if not h.endswith(self.c._GOOGLESOURCE): |
| 534 | assert not h.endswith('.') |
| 535 | return h + '.' + self.c._GOOGLESOURCE |
| 536 | return h |
| 537 | self.c._all_hosts = [(ensure_googlesource(h), i, '.gitcookies') |
| 538 | for h, i in subhost_identity_pairs] |
| 539 | |
Andrii Shyshkalov | 0d2dea0 | 2017-07-17 15:17:55 +0200 | [diff] [blame] | 540 | def test_identity_parsing(self): |
| 541 | self.assertEqual(self.c._parse_identity('ldap.google.com'), |
| 542 | ('ldap', 'google.com')) |
| 543 | self.assertEqual(self.c._parse_identity('git-ldap.example.com'), |
| 544 | ('ldap', 'example.com')) |
| 545 | # Specical case because we know there are no subdomains in chromium.org. |
| 546 | self.assertEqual(self.c._parse_identity('git-note.period.chromium.org'), |
| 547 | ('note.period', 'chromium.org')) |
Lei Zhang | d3f769a | 2017-12-15 15:16:14 -0800 | [diff] [blame] | 548 | # Pathological: ".period." can be either username OR domain, more likely |
| 549 | # domain. |
Andrii Shyshkalov | 0d2dea0 | 2017-07-17 15:17:55 +0200 | [diff] [blame] | 550 | self.assertEqual(self.c._parse_identity('git-note.period.example.com'), |
| 551 | ('note', 'period.example.com')) |
| 552 | |
Andrii Shyshkalov | 9780050 | 2017-03-16 16:04:32 +0100 | [diff] [blame] | 553 | def test_analysis_nothing(self): |
| 554 | self.c._all_hosts = [] |
| 555 | self.assertFalse(self.c.has_generic_host()) |
| 556 | self.assertEqual(set(), self.c.get_conflicting_hosts()) |
| 557 | self.assertEqual(set(), self.c.get_duplicated_hosts()) |
| 558 | self.assertEqual(set(), self.c.get_partially_configured_hosts()) |
Andrii Shyshkalov | 9780050 | 2017-03-16 16:04:32 +0100 | [diff] [blame] | 559 | |
| 560 | def test_analysis(self): |
| 561 | self.mock_hosts_creds([ |
| 562 | ('.googlesource.com', 'git-example.chromium.org'), |
| 563 | |
| 564 | ('chromium', 'git-example.google.com'), |
| 565 | ('chromium-review', 'git-example.google.com'), |
| 566 | ('chrome-internal', 'git-example.chromium.org'), |
| 567 | ('chrome-internal-review', 'git-example.chromium.org'), |
| 568 | ('conflict', 'git-example.google.com'), |
| 569 | ('conflict-review', 'git-example.chromium.org'), |
| 570 | ('dup', 'git-example.google.com'), |
| 571 | ('dup', 'git-example.google.com'), |
| 572 | ('dup-review', 'git-example.google.com'), |
| 573 | ('partial', 'git-example.google.com'), |
Andrii Shyshkalov | c817382 | 2017-07-10 12:10:53 +0200 | [diff] [blame] | 574 | ('gpartial-review', 'git-example.google.com'), |
Andrii Shyshkalov | 9780050 | 2017-03-16 16:04:32 +0100 | [diff] [blame] | 575 | ]) |
| 576 | self.assertTrue(self.c.has_generic_host()) |
| 577 | self.assertEqual(set(['conflict.googlesource.com']), |
| 578 | self.c.get_conflicting_hosts()) |
| 579 | self.assertEqual(set(['dup.googlesource.com']), |
| 580 | self.c.get_duplicated_hosts()) |
Andrii Shyshkalov | c817382 | 2017-07-10 12:10:53 +0200 | [diff] [blame] | 581 | self.assertEqual(set(['partial.googlesource.com', |
| 582 | 'gpartial-review.googlesource.com']), |
Andrii Shyshkalov | 9780050 | 2017-03-16 16:04:32 +0100 | [diff] [blame] | 583 | self.c.get_partially_configured_hosts()) |
Andrii Shyshkalov | 9780050 | 2017-03-16 16:04:32 +0100 | [diff] [blame] | 584 | |
Andrii Shyshkalov | 0a0b067 | 2017-03-16 16:27:48 +0100 | [diff] [blame] | 585 | def test_report_no_problems(self): |
| 586 | self.test_analysis_nothing() |
Andrii Shyshkalov | 0a0b067 | 2017-03-16 16:27:48 +0100 | [diff] [blame] | 587 | self.assertFalse(self.c.find_and_report_problems()) |
| 588 | self.assertEqual(sys.stdout.getvalue(), '') |
| 589 | |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 590 | @mock.patch( |
| 591 | 'git_cl.gerrit_util.CookiesAuthenticator.get_gitcookies_path', |
Josip Sokcevic | 464e9ff | 2020-03-18 23:48:55 +0000 | [diff] [blame] | 592 | return_value=os.path.join('~', '.gitcookies')) |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 593 | def test_report(self, *_mocks): |
Andrii Shyshkalov | 0a0b067 | 2017-03-16 16:27:48 +0100 | [diff] [blame] | 594 | self.test_analysis() |
Andrii Shyshkalov | 0a0b067 | 2017-03-16 16:27:48 +0100 | [diff] [blame] | 595 | self.assertTrue(self.c.find_and_report_problems()) |
| 596 | with open(os.path.join(os.path.dirname(__file__), |
| 597 | 'git_cl_creds_check_report.txt')) as f: |
Josip Sokcevic | 464e9ff | 2020-03-18 23:48:55 +0000 | [diff] [blame] | 598 | expected = f.read() % { |
| 599 | 'sep': os.sep, |
| 600 | } |
| 601 | |
Andrii Shyshkalov | 0a0b067 | 2017-03-16 16:27:48 +0100 | [diff] [blame] | 602 | def by_line(text): |
| 603 | return [l.rstrip() for l in text.rstrip().splitlines()] |
Robert Iannucci | 456b0d6 | 2018-03-13 19:15:50 -0700 | [diff] [blame] | 604 | self.maxDiff = 10000 # pylint: disable=attribute-defined-outside-init |
Andrii Shyshkalov | 4812e61 | 2017-03-27 17:22:57 +0200 | [diff] [blame] | 605 | self.assertEqual(by_line(sys.stdout.getvalue().strip()), by_line(expected)) |
Andrii Shyshkalov | 9780050 | 2017-03-16 16:04:32 +0100 | [diff] [blame] | 606 | |
Nodir Turakulov | d0e2cd2 | 2017-11-15 10:22:06 -0800 | [diff] [blame] | 607 | |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 608 | class TestGitCl(unittest.TestCase): |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 609 | def setUp(self): |
| 610 | super(TestGitCl, self).setUp() |
| 611 | self.calls = [] |
tandrii | 9d20675 | 2016-06-20 11:32:47 -0700 | [diff] [blame] | 612 | self._calls_done = [] |
Edward Lesmes | 0dd5482 | 2020-03-26 18:24:25 +0000 | [diff] [blame] | 613 | self.failed = False |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 614 | mock.patch('sys.stdout', StringIO()).start() |
| 615 | mock.patch( |
| 616 | 'git_cl.time_time', |
| 617 | lambda: self._mocked_call('time.time')).start() |
| 618 | mock.patch( |
| 619 | 'git_cl.metrics.collector.add_repeated', |
| 620 | lambda *a: self._mocked_call('add_repeated', *a)).start() |
| 621 | mock.patch('subprocess2.call', self._mocked_call).start() |
| 622 | mock.patch('subprocess2.check_call', self._mocked_call).start() |
| 623 | mock.patch('subprocess2.check_output', self._mocked_call).start() |
| 624 | mock.patch( |
| 625 | 'subprocess2.communicate', |
| 626 | lambda *a, **_k: ([self._mocked_call(*a), ''], 0)).start() |
| 627 | mock.patch( |
| 628 | 'git_cl.gclient_utils.CheckCallAndFilter', |
| 629 | self._mocked_call).start() |
| 630 | mock.patch('git_common.is_dirty_git_tree', lambda x: False).start() |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 631 | mock.patch('git_cl.FindCodereviewSettingsFile', return_value='').start() |
| 632 | mock.patch( |
| 633 | 'git_cl.SaveDescriptionBackup', |
| 634 | lambda _: self._mocked_call('SaveDescriptionBackup')).start() |
| 635 | mock.patch( |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 636 | 'git_cl.write_json', |
| 637 | lambda *a: self._mocked_call('write_json', *a)).start() |
| 638 | mock.patch( |
Edward Lemur | 227d510 | 2020-02-25 23:45:35 +0000 | [diff] [blame] | 639 | 'git_cl.Changelist.RunHook', |
| 640 | return_value={'more_cc': ['test-more-cc@chromium.org']}).start() |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 641 | mock.patch('git_cl.watchlists.Watchlists', WatchlistsMock).start() |
| 642 | mock.patch('git_cl.auth.Authenticator', AuthenticatorMock).start() |
Edward Lesmes | 7677e5c | 2020-02-19 20:39:03 +0000 | [diff] [blame] | 643 | mock.patch('gerrit_util.GetChangeDetail').start() |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 644 | mock.patch( |
| 645 | 'git_cl.gerrit_util.GetChangeComments', |
| 646 | lambda *a: self._mocked_call('GetChangeComments', *a)).start() |
| 647 | mock.patch( |
| 648 | 'git_cl.gerrit_util.GetChangeRobotComments', |
| 649 | lambda *a: self._mocked_call('GetChangeRobotComments', *a)).start() |
| 650 | mock.patch( |
| 651 | 'git_cl.gerrit_util.AddReviewers', |
| 652 | lambda *a: self._mocked_call('AddReviewers', *a)).start() |
| 653 | mock.patch( |
| 654 | 'git_cl.gerrit_util.SetReview', |
| 655 | lambda h, i, msg=None, labels=None, notify=None, ready=None: ( |
| 656 | self._mocked_call( |
| 657 | 'SetReview', h, i, msg, labels, notify, ready))).start() |
| 658 | mock.patch( |
| 659 | 'git_cl.gerrit_util.LuciContextAuthenticator.is_luci', |
| 660 | return_value=False).start() |
| 661 | mock.patch( |
| 662 | 'git_cl.gerrit_util.GceAuthenticator.is_gce', |
| 663 | return_value=False).start() |
| 664 | mock.patch( |
| 665 | 'git_cl.gerrit_util.ValidAccounts', |
| 666 | lambda *a: self._mocked_call('ValidAccounts', *a)).start() |
Edward Lemur | d55c507 | 2020-02-20 01:09:07 +0000 | [diff] [blame] | 667 | mock.patch('sys.exit', side_effect=SystemExitMock).start() |
Edward Lemur | 15a9b8c | 2020-02-13 00:52:30 +0000 | [diff] [blame] | 668 | mock.patch('git_cl.Settings.GetRoot', return_value='').start() |
Edward Lemur | 8515328 | 2020-02-14 22:06:29 +0000 | [diff] [blame] | 669 | self.mockGit = GitMocks() |
| 670 | mock.patch('scm.GIT.GetBranchRef', self.mockGit.GetBranchRef).start() |
| 671 | mock.patch('scm.GIT.GetConfig', self.mockGit.GetConfig).start() |
Edward Lesmes | 50da770 | 2020-03-30 19:23:43 +0000 | [diff] [blame] | 672 | mock.patch('scm.GIT.ResolveCommit', return_value='hash').start() |
| 673 | mock.patch('scm.GIT.IsValidRevision', return_value=True).start() |
Edward Lemur | 8515328 | 2020-02-14 22:06:29 +0000 | [diff] [blame] | 674 | mock.patch('scm.GIT.SetConfig', self.mockGit.SetConfig).start() |
Edward Lemur | 8410164 | 2020-02-21 21:40:34 +0000 | [diff] [blame] | 675 | mock.patch( |
| 676 | 'git_new_branch.create_new_branch', self.mockGit.NewBranch).start() |
Edward Lemur | 15a9b8c | 2020-02-13 00:52:30 +0000 | [diff] [blame] | 677 | mock.patch( |
Edward Lemur | 8515328 | 2020-02-14 22:06:29 +0000 | [diff] [blame] | 678 | 'scm.GIT.FetchUpstreamTuple', |
Edward Lemur | 15a9b8c | 2020-02-13 00:52:30 +0000 | [diff] [blame] | 679 | return_value=('origin', 'refs/heads/master')).start() |
Edward Lemur | 8515328 | 2020-02-14 22:06:29 +0000 | [diff] [blame] | 680 | mock.patch( |
| 681 | 'scm.GIT.CaptureStatus', return_value=[('M', 'foo.txt')]).start() |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 682 | # It's important to reset settings to not have inter-tests interference. |
| 683 | git_cl.settings = None |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 684 | self.addCleanup(mock.patch.stopall) |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 685 | |
| 686 | def tearDown(self): |
wychen@chromium.org | 445c896 | 2015-04-28 23:30:05 +0000 | [diff] [blame] | 687 | try: |
Edward Lesmes | 0dd5482 | 2020-03-26 18:24:25 +0000 | [diff] [blame] | 688 | if not self.failed: |
| 689 | self.assertEqual([], self.calls) |
Andrii Shyshkalov | 18df0cd | 2017-01-25 15:22:20 +0100 | [diff] [blame] | 690 | except AssertionError: |
Edward Lemur | 8515328 | 2020-02-14 22:06:29 +0000 | [diff] [blame] | 691 | calls = ''.join(' %s\n' % str(call) for call in self.calls[:5]) |
| 692 | if len(self.calls) > 5: |
| 693 | calls += ' ...\n' |
| 694 | self.fail( |
| 695 | '\n' |
| 696 | 'There are un-consumed calls after this test has finished:\n' + |
| 697 | calls) |
wychen@chromium.org | 445c896 | 2015-04-28 23:30:05 +0000 | [diff] [blame] | 698 | finally: |
| 699 | super(TestGitCl, self).tearDown() |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 700 | |
iannucci@chromium.org | 9e84927 | 2014-04-04 00:31:55 +0000 | [diff] [blame] | 701 | def _mocked_call(self, *args, **_kwargs): |
maruel@chromium.org | 2e72bb1 | 2012-01-17 15:18:35 +0000 | [diff] [blame] | 702 | self.assertTrue( |
| 703 | self.calls, |
tandrii | 9d20675 | 2016-06-20 11:32:47 -0700 | [diff] [blame] | 704 | '@%d Expected: <Missing> Actual: %r' % (len(self._calls_done), args)) |
wychen@chromium.org | a872e75 | 2015-04-28 23:42:18 +0000 | [diff] [blame] | 705 | top = self.calls.pop(0) |
wychen@chromium.org | a872e75 | 2015-04-28 23:42:18 +0000 | [diff] [blame] | 706 | expected_args, result = top |
| 707 | |
maruel@chromium.org | e52678e | 2013-04-26 18:34:44 +0000 | [diff] [blame] | 708 | # Also logs otherwise it could get caught in a try/finally and be hard to |
| 709 | # diagnose. |
| 710 | if expected_args != args: |
tandrii | 9d20675 | 2016-06-20 11:32:47 -0700 | [diff] [blame] | 711 | N = 5 |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 712 | prior_calls = '\n '.join( |
tandrii | 9d20675 | 2016-06-20 11:32:47 -0700 | [diff] [blame] | 713 | '@%d: %r' % (len(self._calls_done) - N + i, c[0]) |
| 714 | for i, c in enumerate(self._calls_done[-N:])) |
| 715 | following_calls = '\n '.join( |
| 716 | '@%d: %r' % (len(self._calls_done) + i + 1, c[0]) |
| 717 | for i, c in enumerate(self.calls[:N])) |
| 718 | extended_msg = ( |
| 719 | 'A few prior calls:\n %s\n\n' |
| 720 | 'This (expected):\n @%d: %r\n' |
| 721 | 'This (actual):\n @%d: %r\n\n' |
| 722 | 'A few following expected calls:\n %s' % |
| 723 | (prior_calls, len(self._calls_done), expected_args, |
| 724 | len(self._calls_done), args, following_calls)) |
tandrii | 9d20675 | 2016-06-20 11:32:47 -0700 | [diff] [blame] | 725 | |
Edward Lesmes | 0dd5482 | 2020-03-26 18:24:25 +0000 | [diff] [blame] | 726 | self.failed = True |
tandrii | 99a72f2 | 2016-08-17 14:33:24 -0700 | [diff] [blame] | 727 | self.fail('@%d\n' |
| 728 | ' Expected: %r\n' |
Edward Lemur | 2696407 | 2020-02-19 19:18:51 +0000 | [diff] [blame] | 729 | ' Actual: %r\n' |
| 730 | '\n' |
| 731 | '%s' % ( |
| 732 | len(self._calls_done), expected_args, args, extended_msg)) |
tandrii | 9d20675 | 2016-06-20 11:32:47 -0700 | [diff] [blame] | 733 | |
| 734 | self._calls_done.append(top) |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 735 | if isinstance(result, Exception): |
| 736 | raise result |
Edward Lemur | 0db01f0 | 2019-11-12 22:01:51 +0000 | [diff] [blame] | 737 | # stdout from git commands is supposed to be a bytestream. Convert it here |
| 738 | # instead of converting all test output in this file to bytes. |
| 739 | if args[0][0] == 'git' and not isinstance(result, bytes): |
| 740 | result = result.encode('utf-8') |
maruel@chromium.org | 2e72bb1 | 2012-01-17 15:18:35 +0000 | [diff] [blame] | 741 | return result |
| 742 | |
Edward Lemur | 1a83da1 | 2020-03-04 21:18:36 +0000 | [diff] [blame] | 743 | @mock.patch('sys.stdin', StringIO('blah\nye\n')) |
| 744 | @mock.patch('sys.stdout', StringIO()) |
Andrii Shyshkalov | abc26ac | 2017-03-14 14:49:38 +0100 | [diff] [blame] | 745 | def test_ask_for_explicit_yes_true(self): |
Andrii Shyshkalov | abc26ac | 2017-03-14 14:49:38 +0100 | [diff] [blame] | 746 | self.assertTrue(git_cl.ask_for_explicit_yes('prompt')) |
Edward Lemur | 1a83da1 | 2020-03-04 21:18:36 +0000 | [diff] [blame] | 747 | self.assertEqual( |
| 748 | 'prompt [Yes/No]: Please, type yes or no: ', |
| 749 | sys.stdout.getvalue()) |
Andrii Shyshkalov | abc26ac | 2017-03-14 14:49:38 +0100 | [diff] [blame] | 750 | |
tandrii | 48df581 | 2016-10-17 03:55:37 -0700 | [diff] [blame] | 751 | def test_LoadCodereviewSettingsFromFile_gerrit(self): |
Edward Lemur | 79d4f99 | 2019-11-11 23:49:02 +0000 | [diff] [blame] | 752 | codereview_file = StringIO('GERRIT_HOST: true') |
tandrii | 48df581 | 2016-10-17 03:55:37 -0700 | [diff] [blame] | 753 | self.calls = [ |
| 754 | ((['git', 'config', '--unset-all', 'rietveld.cc'],), CERR1), |
tandrii | 48df581 | 2016-10-17 03:55:37 -0700 | [diff] [blame] | 755 | ((['git', 'config', '--unset-all', 'rietveld.tree-status-url'],), CERR1), |
| 756 | ((['git', 'config', '--unset-all', 'rietveld.viewvc-url'],), CERR1), |
| 757 | ((['git', 'config', '--unset-all', 'rietveld.bug-prefix'],), CERR1), |
| 758 | ((['git', 'config', '--unset-all', 'rietveld.cpplint-regex'],), CERR1), |
tandrii | 48df581 | 2016-10-17 03:55:37 -0700 | [diff] [blame] | 759 | ((['git', 'config', '--unset-all', 'rietveld.cpplint-ignore-regex'],), |
| 760 | CERR1), |
tandrii | 48df581 | 2016-10-17 03:55:37 -0700 | [diff] [blame] | 761 | ((['git', 'config', '--unset-all', 'rietveld.run-post-upload-hook'],), |
| 762 | CERR1), |
Jamie Madill | dc4d19e | 2019-10-24 21:50:02 +0000 | [diff] [blame] | 763 | ((['git', 'config', '--unset-all', 'rietveld.format-full-by-default'],), |
| 764 | CERR1), |
tandrii | 48df581 | 2016-10-17 03:55:37 -0700 | [diff] [blame] | 765 | ((['git', 'config', 'gerrit.host', 'true'],), ''), |
| 766 | ] |
| 767 | self.assertIsNone(git_cl.LoadCodereviewSettingsFromFile(codereview_file)) |
| 768 | |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 769 | @classmethod |
Andrii Shyshkalov | 5c3d0b3 | 2017-02-16 17:47:31 +0100 | [diff] [blame] | 770 | def _gerrit_base_calls(cls, issue=None, fetched_description=None, |
Andrii Shyshkalov | 550e924 | 2017-04-12 17:14:49 +0200 | [diff] [blame] | 771 | fetched_status=None, other_cl_owner=None, |
Anthony Polito | 8b95534 | 2019-09-24 19:01:36 +0000 | [diff] [blame] | 772 | custom_cl_base=None, short_hostname='chromium', |
Edward Lesmes | 8c43c3f | 2021-01-20 00:20:26 +0000 | [diff] [blame] | 773 | change_id=None, default_branch='master'): |
Edward Lemur | 2696407 | 2020-02-19 19:18:51 +0000 | [diff] [blame] | 774 | calls = [] |
Andrii Shyshkalov | 550e924 | 2017-04-12 17:14:49 +0200 | [diff] [blame] | 775 | if custom_cl_base: |
| 776 | ancestor_revision = custom_cl_base |
| 777 | else: |
| 778 | # Determine ancestor_revision to be merge base. |
Edward Lesmes | 8c43c3f | 2021-01-20 00:20:26 +0000 | [diff] [blame] | 779 | ancestor_revision = 'origin/' + default_branch |
Andrii Shyshkalov | 550e924 | 2017-04-12 17:14:49 +0200 | [diff] [blame] | 780 | |
Andrii Shyshkalov | 0293956 | 2017-02-16 17:47:17 +0100 | [diff] [blame] | 781 | if issue: |
Edward Lesmes | 7677e5c | 2020-02-19 20:39:03 +0000 | [diff] [blame] | 782 | gerrit_util.GetChangeDetail.return_value = { |
| 783 | 'owner': {'email': (other_cl_owner or 'owner@example.com')}, |
| 784 | 'change_id': (change_id or '123456789'), |
| 785 | 'current_revision': 'sha1_of_current_revision', |
| 786 | 'revisions': {'sha1_of_current_revision': { |
| 787 | 'commit': {'message': fetched_description}, |
| 788 | }}, |
| 789 | 'status': fetched_status or 'NEW', |
| 790 | } |
Andrii Shyshkalov | 5c3d0b3 | 2017-02-16 17:47:31 +0100 | [diff] [blame] | 791 | if fetched_status == 'ABANDONED': |
Andrii Shyshkalov | 5c3d0b3 | 2017-02-16 17:47:31 +0100 | [diff] [blame] | 792 | return calls |
Andrii Shyshkalov | bb86fbb | 2017-03-24 14:59:28 +0100 | [diff] [blame] | 793 | if other_cl_owner: |
| 794 | calls += [ |
| 795 | (('ask_for_data', 'Press Enter to upload, or Ctrl+C to abort'), ''), |
| 796 | ] |
Andrii Shyshkalov | 0293956 | 2017-02-16 17:47:17 +0100 | [diff] [blame] | 797 | |
Andrii Shyshkalov | 0293956 | 2017-02-16 17:47:17 +0100 | [diff] [blame] | 798 | calls += [ |
Andrii Shyshkalov | 550e924 | 2017-04-12 17:14:49 +0200 | [diff] [blame] | 799 | ((['git', 'diff', '--no-ext-diff', '--stat', '-l100000', '-C50'] + |
| 800 | ([custom_cl_base] if custom_cl_base else |
| 801 | [ancestor_revision, 'HEAD']),), |
| 802 | '+dat'), |
Andrii Shyshkalov | 0293956 | 2017-02-16 17:47:17 +0100 | [diff] [blame] | 803 | ] |
Josip Sokcevic | c39ab99 | 2020-09-24 20:09:15 +0000 | [diff] [blame] | 804 | calls += [ |
| 805 | ((['git', 'show-branch', 'refs/remotes/origin/main'], ), |
Edward Lesmes | 8c43c3f | 2021-01-20 00:20:26 +0000 | [diff] [blame] | 806 | '1' if default_branch == 'main' else callError(1)), |
Josip Sokcevic | c39ab99 | 2020-09-24 20:09:15 +0000 | [diff] [blame] | 807 | ] |
Edward Lemur | 2c62b33 | 2020-03-12 22:12:33 +0000 | [diff] [blame] | 808 | |
Andrii Shyshkalov | 0293956 | 2017-02-16 17:47:17 +0100 | [diff] [blame] | 809 | return calls |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 810 | |
Josip Sokcevic | f2cfd3d | 2021-03-30 18:39:18 +0000 | [diff] [blame] | 811 | def _gerrit_upload_calls(self, |
| 812 | description, |
| 813 | reviewers, |
| 814 | squash, |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 815 | squash_mode='default', |
Josip Sokcevic | f2cfd3d | 2021-03-30 18:39:18 +0000 | [diff] [blame] | 816 | title=None, |
| 817 | notify=False, |
| 818 | post_amend_description=None, |
| 819 | issue=None, |
| 820 | cc=None, |
| 821 | custom_cl_base=None, |
| 822 | tbr=None, |
Andrii Shyshkalov | e7a7fc4 | 2018-10-30 17:35:09 +0000 | [diff] [blame] | 823 | short_hostname='chromium', |
Josip Sokcevic | f2cfd3d | 2021-03-30 18:39:18 +0000 | [diff] [blame] | 824 | labels=None, |
| 825 | change_id=None, |
| 826 | final_description=None, |
| 827 | gitcookies_exists=True, |
| 828 | force=False, |
| 829 | edit_description=None, |
| 830 | default_branch='master', |
| 831 | push_opts=None): |
tandrii@chromium.org | 1062500 | 2016-03-04 20:03:47 +0000 | [diff] [blame] | 832 | if post_amend_description is None: |
| 833 | post_amend_description = description |
bradnelson | d975b30 | 2016-10-23 12:20:23 -0700 | [diff] [blame] | 834 | cc = cc or [] |
Andrii Shyshkalov | 550e924 | 2017-04-12 17:14:49 +0200 | [diff] [blame] | 835 | |
| 836 | calls = [] |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 837 | |
Edward Lesmes | 4de5413 | 2020-05-05 19:41:33 +0000 | [diff] [blame] | 838 | if squash_mode in ('override_squash', 'override_nosquash'): |
| 839 | self.mockGit.config['gerrit.override-squash-uploads'] = ( |
| 840 | 'true' if squash_mode == 'override_squash' else 'false') |
| 841 | |
tandrii@chromium.org | 57d8654 | 2016-03-04 16:11:32 +0000 | [diff] [blame] | 842 | 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] | 843 | calls += [ |
Andrii Shyshkalov | 550e924 | 2017-04-12 17:14:49 +0200 | [diff] [blame] | 844 | (('DownloadGerritHook', False), ''), |
Andrii Shyshkalov | 550e924 | 2017-04-12 17:14:49 +0200 | [diff] [blame] | 845 | ] |
bauerb@chromium.org | 27386dd | 2015-02-16 10:45:39 +0000 | [diff] [blame] | 846 | if squash: |
Edward Lemur | 5a644f8 | 2020-03-18 16:44:57 +0000 | [diff] [blame] | 847 | if not issue and not force: |
Edward Lemur | 5fb2224 | 2020-03-12 22:05:13 +0000 | [diff] [blame] | 848 | calls += [ |
| 849 | ((['RunEditor'],), description), |
| 850 | ] |
Josip | e827b0f | 2020-01-30 00:07:20 +0000 | [diff] [blame] | 851 | # user wants to edit description |
| 852 | if edit_description: |
| 853 | calls += [ |
Josip | e827b0f | 2020-01-30 00:07:20 +0000 | [diff] [blame] | 854 | ((['RunEditor'],), edit_description), |
| 855 | ] |
bauerb@chromium.org | 27386dd | 2015-02-16 10:45:39 +0000 | [diff] [blame] | 856 | ref_to_push = 'abcdef0123456789' |
Andrii Shyshkalov | 550e924 | 2017-04-12 17:14:49 +0200 | [diff] [blame] | 857 | |
| 858 | if custom_cl_base is None: |
Josip Sokcevic | c39ab99 | 2020-09-24 20:09:15 +0000 | [diff] [blame] | 859 | parent = 'origin/' + default_branch |
Edward Lesmes | 8c43c3f | 2021-01-20 00:20:26 +0000 | [diff] [blame] | 860 | git_common.get_or_create_merge_base.return_value = parent |
Andrii Shyshkalov | 550e924 | 2017-04-12 17:14:49 +0200 | [diff] [blame] | 861 | else: |
| 862 | calls += [ |
| 863 | ((['git', 'merge-base', '--is-ancestor', custom_cl_base, |
Josip Sokcevic | c39ab99 | 2020-09-24 20:09:15 +0000 | [diff] [blame] | 864 | 'refs/remotes/origin/' + default_branch],), |
Andrii Shyshkalov | 550e924 | 2017-04-12 17:14:49 +0200 | [diff] [blame] | 865 | callError(1)), # Means not ancenstor. |
| 866 | (('ask_for_data', |
| 867 | 'Do you take responsibility for cleaning up potential mess ' |
| 868 | 'resulting from proceeding with upload? Press Enter to upload, ' |
| 869 | 'or Ctrl+C to abort'), ''), |
| 870 | ] |
| 871 | parent = custom_cl_base |
| 872 | |
| 873 | calls += [ |
| 874 | ((['git', 'rev-parse', 'HEAD:'],), # `HEAD:` means HEAD's tree hash. |
| 875 | '0123456789abcdef'), |
Edward Lemur | 1773f37 | 2020-02-22 00:27:14 +0000 | [diff] [blame] | 876 | ((['FileWrite', '/tmp/fake-temp1', description],), None), |
Andrii Shyshkalov | 550e924 | 2017-04-12 17:14:49 +0200 | [diff] [blame] | 877 | ((['git', 'commit-tree', '0123456789abcdef', '-p', parent, |
Edward Lemur | 1773f37 | 2020-02-22 00:27:14 +0000 | [diff] [blame] | 878 | '-F', '/tmp/fake-temp1'],), |
Andrii Shyshkalov | 550e924 | 2017-04-12 17:14:49 +0200 | [diff] [blame] | 879 | ref_to_push), |
| 880 | ] |
bauerb@chromium.org | 27386dd | 2015-02-16 10:45:39 +0000 | [diff] [blame] | 881 | else: |
| 882 | ref_to_push = 'HEAD' |
Josip Sokcevic | c39ab99 | 2020-09-24 20:09:15 +0000 | [diff] [blame] | 883 | parent = 'origin/refs/heads/' + default_branch |
bauerb@chromium.org | 27386dd | 2015-02-16 10:45:39 +0000 | [diff] [blame] | 884 | |
sivachandra@chromium.org | aebe87f | 2012-10-22 20:34:21 +0000 | [diff] [blame] | 885 | calls += [ |
Andrii Shyshkalov | d9fdc1f | 2018-09-27 02:13:09 +0000 | [diff] [blame] | 886 | (('SaveDescriptionBackup',), None), |
Edward Lemur | 5a644f8 | 2020-03-18 16:44:57 +0000 | [diff] [blame] | 887 | ((['git', 'rev-list', parent + '..' + ref_to_push],),'1hashPerLine\n'), |
Andrii Shyshkalov | 550e924 | 2017-04-12 17:14:49 +0200 | [diff] [blame] | 888 | ] |
tandrii@chromium.org | 8da4540 | 2016-05-24 23:11:03 +0000 | [diff] [blame] | 889 | |
Josip Sokcevic | d0ba91f | 2021-03-29 20:12:09 +0000 | [diff] [blame] | 890 | metrics_arguments = [] |
Edward Lemur | 01f4a4f | 2018-11-03 00:40:38 +0000 | [diff] [blame] | 891 | |
Aaron Gable | afd5277 | 2017-06-27 16:40:10 -0700 | [diff] [blame] | 892 | if notify: |
Josip Sokcevic | d0ba91f | 2021-03-29 20:12:09 +0000 | [diff] [blame] | 893 | ref_suffix = '%ready,notify=ALL' |
Edward Lemur | 01f4a4f | 2018-11-03 00:40:38 +0000 | [diff] [blame] | 894 | metrics_arguments += ['ready', 'notify=ALL'] |
Aaron Gable | 844cf29 | 2017-06-28 11:32:59 -0700 | [diff] [blame] | 895 | else: |
Jamie Madill | 276da0b | 2018-04-27 14:41:20 -0400 | [diff] [blame] | 896 | if not issue and squash: |
Josip Sokcevic | d0ba91f | 2021-03-29 20:12:09 +0000 | [diff] [blame] | 897 | ref_suffix = '%wip' |
Edward Lemur | 01f4a4f | 2018-11-03 00:40:38 +0000 | [diff] [blame] | 898 | metrics_arguments.append('wip') |
Aaron Gable | 844cf29 | 2017-06-28 11:32:59 -0700 | [diff] [blame] | 899 | else: |
Josip Sokcevic | d0ba91f | 2021-03-29 20:12:09 +0000 | [diff] [blame] | 900 | ref_suffix = '%notify=NONE' |
Edward Lemur | 01f4a4f | 2018-11-03 00:40:38 +0000 | [diff] [blame] | 901 | metrics_arguments.append('notify=NONE') |
Aaron Gable | 9b713dd | 2016-12-14 16:04:21 -0800 | [diff] [blame] | 902 | |
Edward Lemur | 5a644f8 | 2020-03-18 16:44:57 +0000 | [diff] [blame] | 903 | # If issue is given, then description is fetched from Gerrit instead. |
| 904 | if issue is None: |
| 905 | if squash: |
| 906 | title = 'Initial upload' |
| 907 | else: |
| 908 | if not title: |
| 909 | calls += [ |
| 910 | ((['git', 'show', '-s', '--format=%s', 'HEAD'],), ''), |
| 911 | (('ask_for_data', 'Title for patchset []: '), 'User input'), |
| 912 | ] |
| 913 | title = 'User input' |
Aaron Gable | 70f4e24 | 2017-06-26 10:45:59 -0700 | [diff] [blame] | 914 | if title: |
Josip Sokcevic | d0ba91f | 2021-03-29 20:12:09 +0000 | [diff] [blame] | 915 | ref_suffix += ',m=' + gerrit_util.PercentEncodeForGitRef(title) |
Edward Lemur | 01f4a4f | 2018-11-03 00:40:38 +0000 | [diff] [blame] | 916 | metrics_arguments.append('m') |
Andrii Shyshkalov | febbae9 | 2017-04-05 15:05:20 +0000 | [diff] [blame] | 917 | |
Andrii Shyshkalov | 0da5e8f | 2018-10-30 17:29:18 +0000 | [diff] [blame] | 918 | if short_hostname == 'chromium': |
Quinten Yearsley | 925cedb | 2020-04-13 17:49:39 +0000 | [diff] [blame] | 919 | # All reviewers and ccs get into ref_suffix. |
Andrii Shyshkalov | 0da5e8f | 2018-10-30 17:29:18 +0000 | [diff] [blame] | 920 | for r in sorted(reviewers): |
Josip Sokcevic | d0ba91f | 2021-03-29 20:12:09 +0000 | [diff] [blame] | 921 | ref_suffix += ',r=%s' % r |
Edward Lemur | 01f4a4f | 2018-11-03 00:40:38 +0000 | [diff] [blame] | 922 | metrics_arguments.append('r') |
Edward Lemur | 4508b42 | 2019-10-03 21:56:35 +0000 | [diff] [blame] | 923 | if issue is None: |
Edward Lemur | 227d510 | 2020-02-25 23:45:35 +0000 | [diff] [blame] | 924 | cc += ['test-more-cc@chromium.org', 'joe@example.com'] |
Edward Lemur | 4508b42 | 2019-10-03 21:56:35 +0000 | [diff] [blame] | 925 | for c in sorted(cc): |
Josip Sokcevic | d0ba91f | 2021-03-29 20:12:09 +0000 | [diff] [blame] | 926 | ref_suffix += ',cc=%s' % c |
Edward Lemur | 01f4a4f | 2018-11-03 00:40:38 +0000 | [diff] [blame] | 927 | metrics_arguments.append('cc') |
Andrii Shyshkalov | 0da5e8f | 2018-10-30 17:29:18 +0000 | [diff] [blame] | 928 | reviewers, cc = [], [] |
| 929 | else: |
| 930 | # TODO(crbug/877717): remove this case. |
| 931 | calls += [ |
| 932 | (('ValidAccounts', '%s-review.googlesource.com' % short_hostname, |
| 933 | sorted(reviewers) + ['joe@example.com', |
Edward Lemur | 227d510 | 2020-02-25 23:45:35 +0000 | [diff] [blame] | 934 | 'test-more-cc@chromium.org'] + cc), |
Andrii Shyshkalov | 0da5e8f | 2018-10-30 17:29:18 +0000 | [diff] [blame] | 935 | { |
| 936 | e: {'email': e} |
| 937 | for e in (reviewers + ['joe@example.com'] + cc) |
| 938 | }) |
| 939 | ] |
| 940 | for r in sorted(reviewers): |
| 941 | if r != 'bad-account-or-email': |
Josip Sokcevic | d0ba91f | 2021-03-29 20:12:09 +0000 | [diff] [blame] | 942 | ref_suffix += ',r=%s' % r |
Edward Lemur | 01f4a4f | 2018-11-03 00:40:38 +0000 | [diff] [blame] | 943 | metrics_arguments.append('r') |
Andrii Shyshkalov | 0da5e8f | 2018-10-30 17:29:18 +0000 | [diff] [blame] | 944 | reviewers.remove(r) |
Edward Lemur | 4508b42 | 2019-10-03 21:56:35 +0000 | [diff] [blame] | 945 | if issue is None: |
| 946 | cc += ['joe@example.com'] |
| 947 | for c in sorted(cc): |
Josip Sokcevic | d0ba91f | 2021-03-29 20:12:09 +0000 | [diff] [blame] | 948 | ref_suffix += ',cc=%s' % c |
Edward Lemur | 01f4a4f | 2018-11-03 00:40:38 +0000 | [diff] [blame] | 949 | metrics_arguments.append('cc') |
Andrii Shyshkalov | 0da5e8f | 2018-10-30 17:29:18 +0000 | [diff] [blame] | 950 | if c in cc: |
| 951 | cc.remove(c) |
Andrii Shyshkalov | 76988a8 | 2018-10-15 03:12:25 +0000 | [diff] [blame] | 952 | |
Edward Lemur | 687ca90 | 2018-12-05 02:30:30 +0000 | [diff] [blame] | 953 | for k, v in sorted((labels or {}).items()): |
Josip Sokcevic | d0ba91f | 2021-03-29 20:12:09 +0000 | [diff] [blame] | 954 | ref_suffix += ',l=%s+%d' % (k, v) |
Edward Lemur | 687ca90 | 2018-12-05 02:30:30 +0000 | [diff] [blame] | 955 | metrics_arguments.append('l=%s+%d' % (k, v)) |
| 956 | |
| 957 | if tbr: |
| 958 | calls += [ |
| 959 | (('GetCodeReviewTbrScore', |
| 960 | '%s-review.googlesource.com' % short_hostname, |
| 961 | 'my/repo'), |
| 962 | 2,), |
| 963 | ] |
Andrii Shyshkalov | e7a7fc4 | 2018-10-30 17:35:09 +0000 | [diff] [blame] | 964 | |
Edward Lemur | 01f4a4f | 2018-11-03 00:40:38 +0000 | [diff] [blame] | 965 | calls += [ |
Josip Sokcevic | f2cfd3d | 2021-03-30 18:39:18 +0000 | [diff] [blame] | 966 | ( |
| 967 | ('time.time', ), |
| 968 | 1000, |
| 969 | ), |
| 970 | ( |
| 971 | ([ |
| 972 | 'git', 'push', |
| 973 | 'https://%s.googlesource.com/my/repo' % short_hostname, |
| 974 | ref_to_push + ':refs/for/refs/heads/' + default_branch + |
| 975 | ref_suffix |
| 976 | ] + (push_opts if push_opts else []), ), |
| 977 | (('remote:\n' |
| 978 | 'remote: Processing changes: (\)\n' |
| 979 | 'remote: Processing changes: (|)\n' |
| 980 | 'remote: Processing changes: (/)\n' |
| 981 | 'remote: Processing changes: (-)\n' |
| 982 | 'remote: Processing changes: new: 1 (/)\n' |
| 983 | 'remote: Processing changes: new: 1, done\n' |
| 984 | 'remote:\n' |
| 985 | 'remote: New Changes:\n' |
| 986 | 'remote: ' |
| 987 | 'https://%s-review.googlesource.com/#/c/my/repo/+/123456' |
| 988 | ' XXX\n' |
| 989 | 'remote:\n' |
| 990 | 'To https://%s.googlesource.com/my/repo\n' |
| 991 | ' * [new branch] hhhh -> refs/for/refs/heads/%s\n') % |
| 992 | (short_hostname, short_hostname, default_branch)), |
| 993 | ), |
| 994 | ( |
| 995 | ('time.time', ), |
| 996 | 2000, |
| 997 | ), |
| 998 | ( |
| 999 | ('add_repeated', 'sub_commands', { |
| 1000 | 'execution_time': 1000, |
| 1001 | 'command': 'git push', |
| 1002 | 'exit_code': 0, |
| 1003 | 'arguments': sorted(metrics_arguments), |
| 1004 | }), |
| 1005 | None, |
| 1006 | ), |
Edward Lemur | 01f4a4f | 2018-11-03 00:40:38 +0000 | [diff] [blame] | 1007 | ] |
| 1008 | |
Edward Lemur | 1b52d87 | 2019-05-09 21:12:12 +0000 | [diff] [blame] | 1009 | final_description = final_description or post_amend_description.strip() |
Josip Sokcevic | 464e9ff | 2020-03-18 23:48:55 +0000 | [diff] [blame] | 1010 | |
Josip Sokcevic | 464e9ff | 2020-03-18 23:48:55 +0000 | [diff] [blame] | 1011 | trace_name = os.path.join('TRACES_DIR', '20170316T200041.000000') |
| 1012 | |
Edward Lemur | 1b52d87 | 2019-05-09 21:12:12 +0000 | [diff] [blame] | 1013 | # Trace-related calls |
| 1014 | calls += [ |
| 1015 | # Write a description with context for the current trace. |
Josip Sokcevic | 464e9ff | 2020-03-18 23:48:55 +0000 | [diff] [blame] | 1016 | ( |
| 1017 | ([ |
| 1018 | 'FileWrite', trace_name + '-README', |
| 1019 | '%(date)s\n' |
| 1020 | '%(short_hostname)s-review.googlesource.com\n' |
| 1021 | '%(change_id)s\n' |
| 1022 | '%(title)s\n' |
| 1023 | '%(description)s\n' |
| 1024 | '1000\n' |
| 1025 | '0\n' |
| 1026 | '%(trace_name)s' % { |
Josip Sokcevic | 5e18b60 | 2020-04-23 21:47:00 +0000 | [diff] [blame] | 1027 | 'date': '2017-03-16T20:00:41.000000', |
Josip Sokcevic | 464e9ff | 2020-03-18 23:48:55 +0000 | [diff] [blame] | 1028 | 'short_hostname': short_hostname, |
| 1029 | 'change_id': change_id, |
| 1030 | 'description': final_description, |
| 1031 | 'title': title or '<untitled>', |
| 1032 | 'trace_name': trace_name, |
| 1033 | } |
| 1034 | ], ), |
| 1035 | None, |
Edward Lemur | 1b52d87 | 2019-05-09 21:12:12 +0000 | [diff] [blame] | 1036 | ), |
| 1037 | # Read traces and shorten git hashes. |
Josip Sokcevic | 464e9ff | 2020-03-18 23:48:55 +0000 | [diff] [blame] | 1038 | ( |
| 1039 | (['os.path.isfile', |
| 1040 | os.path.join('TEMP_DIR', 'trace-packet')], ), |
| 1041 | True, |
Edward Lemur | 1b52d87 | 2019-05-09 21:12:12 +0000 | [diff] [blame] | 1042 | ), |
Josip Sokcevic | 464e9ff | 2020-03-18 23:48:55 +0000 | [diff] [blame] | 1043 | ( |
| 1044 | (['FileRead', os.path.join('TEMP_DIR', 'trace-packet')], ), |
| 1045 | ('git-hash: 0123456789012345678901234567890123456789\n' |
| 1046 | 'git-hash: abcdeabcdeabcdeabcdeabcdeabcdeabcdeabcde\n'), |
Edward Lemur | 1b52d87 | 2019-05-09 21:12:12 +0000 | [diff] [blame] | 1047 | ), |
Josip Sokcevic | 464e9ff | 2020-03-18 23:48:55 +0000 | [diff] [blame] | 1048 | ( |
| 1049 | ([ |
| 1050 | 'FileWrite', |
| 1051 | os.path.join('TEMP_DIR', 'trace-packet'), 'git-hash: 012345\n' |
| 1052 | 'git-hash: abcdea\n' |
| 1053 | ], ), |
| 1054 | None, |
Edward Lemur | 1b52d87 | 2019-05-09 21:12:12 +0000 | [diff] [blame] | 1055 | ), |
| 1056 | # Make zip file for the git traces. |
Josip Sokcevic | 464e9ff | 2020-03-18 23:48:55 +0000 | [diff] [blame] | 1057 | ( |
| 1058 | (['make_archive', trace_name + '-traces', 'zip', 'TEMP_DIR'], ), |
| 1059 | None, |
Edward Lemur | 1b52d87 | 2019-05-09 21:12:12 +0000 | [diff] [blame] | 1060 | ), |
| 1061 | # Collect git config and gitcookies. |
Josip Sokcevic | 464e9ff | 2020-03-18 23:48:55 +0000 | [diff] [blame] | 1062 | ( |
| 1063 | (['git', 'config', '-l'], ), |
| 1064 | 'git-config-output', |
Edward Lemur | 1b52d87 | 2019-05-09 21:12:12 +0000 | [diff] [blame] | 1065 | ), |
Josip Sokcevic | 464e9ff | 2020-03-18 23:48:55 +0000 | [diff] [blame] | 1066 | ( |
| 1067 | ([ |
| 1068 | 'FileWrite', |
| 1069 | os.path.join('TEMP_DIR', 'git-config'), 'git-config-output' |
| 1070 | ], ), |
| 1071 | None, |
Edward Lemur | 1b52d87 | 2019-05-09 21:12:12 +0000 | [diff] [blame] | 1072 | ), |
Josip Sokcevic | 464e9ff | 2020-03-18 23:48:55 +0000 | [diff] [blame] | 1073 | ( |
| 1074 | (['os.path.isfile', |
| 1075 | os.path.join('~', '.gitcookies')], ), |
| 1076 | gitcookies_exists, |
Edward Lemur | 1b52d87 | 2019-05-09 21:12:12 +0000 | [diff] [blame] | 1077 | ), |
| 1078 | ] |
| 1079 | if gitcookies_exists: |
| 1080 | calls += [ |
Josip Sokcevic | 464e9ff | 2020-03-18 23:48:55 +0000 | [diff] [blame] | 1081 | ( |
| 1082 | (['FileRead', os.path.join('~', '.gitcookies')], ), |
| 1083 | 'gitcookies 1/SECRET', |
Edward Lemur | 1b52d87 | 2019-05-09 21:12:12 +0000 | [diff] [blame] | 1084 | ), |
Josip Sokcevic | 464e9ff | 2020-03-18 23:48:55 +0000 | [diff] [blame] | 1085 | ( |
| 1086 | ([ |
| 1087 | 'FileWrite', |
| 1088 | os.path.join('TEMP_DIR', 'gitcookies'), 'gitcookies REDACTED' |
| 1089 | ], ), |
| 1090 | None, |
Edward Lemur | 1b52d87 | 2019-05-09 21:12:12 +0000 | [diff] [blame] | 1091 | ), |
| 1092 | ] |
| 1093 | calls += [ |
| 1094 | # Make zip file for the git config and gitcookies. |
Josip Sokcevic | 464e9ff | 2020-03-18 23:48:55 +0000 | [diff] [blame] | 1095 | ( |
| 1096 | (['make_archive', trace_name + '-git-info', 'zip', 'TEMP_DIR'], ), |
| 1097 | None, |
Edward Lemur | 1b52d87 | 2019-05-09 21:12:12 +0000 | [diff] [blame] | 1098 | ), |
| 1099 | ] |
| 1100 | |
Andrii Shyshkalov | 0da5e8f | 2018-10-30 17:29:18 +0000 | [diff] [blame] | 1101 | # TODO(crbug/877717): this should never be used. |
| 1102 | if squash and short_hostname != 'chromium': |
Andrii Shyshkalov | 0ec9d15 | 2018-08-23 00:22:58 +0000 | [diff] [blame] | 1103 | calls += [ |
| 1104 | (('AddReviewers', |
Andrii Shyshkalov | 1e82867 | 2018-08-23 22:34:37 +0000 | [diff] [blame] | 1105 | 'chromium-review.googlesource.com', 'my%2Frepo~123456', |
Andrii Shyshkalov | 0ec9d15 | 2018-08-23 00:22:58 +0000 | [diff] [blame] | 1106 | sorted(reviewers), |
Edward Lemur | 227d510 | 2020-02-25 23:45:35 +0000 | [diff] [blame] | 1107 | cc + ['test-more-cc@chromium.org'], |
Andrii Shyshkalov | 2f72791 | 2018-10-15 17:02:33 +0000 | [diff] [blame] | 1108 | notify), |
| 1109 | ''), |
Andrii Shyshkalov | 0ec9d15 | 2018-08-23 00:22:58 +0000 | [diff] [blame] | 1110 | ] |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1111 | return calls |
| 1112 | |
Josip Sokcevic | f2cfd3d | 2021-03-30 18:39:18 +0000 | [diff] [blame] | 1113 | def _run_gerrit_upload_test(self, |
| 1114 | upload_args, |
| 1115 | description, |
| 1116 | reviewers=None, |
| 1117 | squash=True, |
| 1118 | squash_mode=None, |
| 1119 | title=None, |
| 1120 | notify=False, |
| 1121 | post_amend_description=None, |
| 1122 | issue=None, |
| 1123 | cc=None, |
| 1124 | fetched_status=None, |
| 1125 | other_cl_owner=None, |
| 1126 | custom_cl_base=None, |
| 1127 | tbr=None, |
| 1128 | short_hostname='chromium', |
| 1129 | labels=None, |
| 1130 | change_id=None, |
| 1131 | final_description=None, |
| 1132 | gitcookies_exists=True, |
| 1133 | force=False, |
| 1134 | log_description=None, |
| 1135 | edit_description=None, |
| 1136 | fetched_description=None, |
| 1137 | default_branch='master', |
| 1138 | push_opts=None): |
sivachandra@chromium.org | aebe87f | 2012-10-22 20:34:21 +0000 | [diff] [blame] | 1139 | """Generic gerrit upload test framework.""" |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1140 | if squash_mode is None: |
| 1141 | if '--no-squash' in upload_args: |
| 1142 | squash_mode = 'nosquash' |
| 1143 | elif '--squash' in upload_args: |
| 1144 | squash_mode = 'squash' |
| 1145 | else: |
| 1146 | squash_mode = 'default' |
| 1147 | |
tandrii@chromium.org | bf766ba | 2016-04-13 12:51:23 +0000 | [diff] [blame] | 1148 | reviewers = reviewers or [] |
bradnelson | d975b30 | 2016-10-23 12:20:23 -0700 | [diff] [blame] | 1149 | cc = cc or [] |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 1150 | mock.patch('git_cl.gerrit_util.CookiesAuthenticator', |
Andrii Shyshkalov | bb86fbb | 2017-03-24 14:59:28 +0100 | [diff] [blame] | 1151 | CookiesAuthenticatorMockFactory( |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 1152 | same_auth=('git-owner.example.com', '', 'pass'))).start() |
| 1153 | mock.patch('git_cl.Changelist._GerritCommitMsgHookCheck', |
| 1154 | lambda _, offer_removal: None).start() |
| 1155 | mock.patch('git_cl.gclient_utils.RunEditor', |
| 1156 | lambda *_, **__: self._mocked_call(['RunEditor'])).start() |
| 1157 | mock.patch('git_cl.DownloadGerritHook', lambda force: self._mocked_call( |
Saagar Sanghavi | 9949ab7 | 2020-07-20 20:56:40 +0000 | [diff] [blame] | 1158 | 'DownloadGerritHook', force)).start() |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 1159 | mock.patch('git_cl.gclient_utils.FileRead', |
| 1160 | lambda path: self._mocked_call(['FileRead', path])).start() |
| 1161 | mock.patch('git_cl.gclient_utils.FileWrite', |
Edward Lemur | 1b52d87 | 2019-05-09 21:12:12 +0000 | [diff] [blame] | 1162 | lambda path, contents: self._mocked_call( |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 1163 | ['FileWrite', path, contents])).start() |
| 1164 | mock.patch('git_cl.datetime_now', |
| 1165 | lambda: datetime.datetime(2017, 3, 16, 20, 0, 41, 0)).start() |
| 1166 | mock.patch('git_cl.tempfile.mkdtemp', lambda: 'TEMP_DIR').start() |
| 1167 | mock.patch('git_cl.TRACES_DIR', 'TRACES_DIR').start() |
| 1168 | mock.patch('git_cl.TRACES_README_FORMAT', |
Edward Lemur | 75391d4 | 2019-05-14 23:35:56 +0000 | [diff] [blame] | 1169 | '%(now)s\n' |
| 1170 | '%(gerrit_host)s\n' |
| 1171 | '%(change_id)s\n' |
| 1172 | '%(title)s\n' |
| 1173 | '%(description)s\n' |
| 1174 | '%(execution_time)s\n' |
| 1175 | '%(exit_code)s\n' |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 1176 | '%(trace_name)s').start() |
| 1177 | mock.patch('git_cl.shutil.make_archive', |
| 1178 | lambda *args: self._mocked_call(['make_archive'] + |
| 1179 | list(args))).start() |
| 1180 | mock.patch('os.path.isfile', |
| 1181 | lambda path: self._mocked_call(['os.path.isfile', path])).start() |
Edward Lemur | 9aa1a96 | 2020-02-25 00:58:38 +0000 | [diff] [blame] | 1182 | mock.patch( |
Edward Lesmes | 0dd5482 | 2020-03-26 18:24:25 +0000 | [diff] [blame] | 1183 | 'git_cl._create_description_from_log', |
| 1184 | return_value=log_description or description).start() |
Edward Lemur | a12175c | 2020-03-09 16:58:26 +0000 | [diff] [blame] | 1185 | mock.patch( |
| 1186 | 'git_cl.Changelist._AddChangeIdToCommitMessage', |
| 1187 | return_value=post_amend_description or description).start() |
Edward Lemur | 1a83da1 | 2020-03-04 21:18:36 +0000 | [diff] [blame] | 1188 | mock.patch( |
Edward Lemur | 5a644f8 | 2020-03-18 16:44:57 +0000 | [diff] [blame] | 1189 | 'git_cl.GenerateGerritChangeId', return_value=change_id).start() |
| 1190 | mock.patch( |
Edward Lesmes | 8c43c3f | 2021-01-20 00:20:26 +0000 | [diff] [blame] | 1191 | 'git_common.get_or_create_merge_base', |
| 1192 | return_value='origin/' + default_branch).start() |
| 1193 | mock.patch( |
Edward Lesmes | ae3586b | 2020-03-23 21:21:14 +0000 | [diff] [blame] | 1194 | 'gclient_utils.AskForData', |
Edward Lemur | 1a83da1 | 2020-03-04 21:18:36 +0000 | [diff] [blame] | 1195 | lambda prompt: self._mocked_call('ask_for_data', prompt)).start() |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1196 | |
Edward Lemur | 2696407 | 2020-02-19 19:18:51 +0000 | [diff] [blame] | 1197 | self.mockGit.config['gerrit.host'] = 'true' |
Edward Lemur | 8515328 | 2020-02-14 22:06:29 +0000 | [diff] [blame] | 1198 | self.mockGit.config['branch.master.gerritissue'] = ( |
| 1199 | str(issue) if issue else None) |
| 1200 | self.mockGit.config['remote.origin.url'] = ( |
| 1201 | 'https://%s.googlesource.com/my/repo' % short_hostname) |
Edward Lemur | 9aa1a96 | 2020-02-25 00:58:38 +0000 | [diff] [blame] | 1202 | self.mockGit.config['user.email'] = 'me@example.com' |
Edward Lemur | 8515328 | 2020-02-14 22:06:29 +0000 | [diff] [blame] | 1203 | |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1204 | self.calls = self._gerrit_base_calls( |
| 1205 | issue=issue, |
Anthony Polito | 8b95534 | 2019-09-24 19:01:36 +0000 | [diff] [blame] | 1206 | fetched_description=fetched_description or description, |
Andrii Shyshkalov | bb86fbb | 2017-03-24 14:59:28 +0100 | [diff] [blame] | 1207 | fetched_status=fetched_status, |
Andrii Shyshkalov | 550e924 | 2017-04-12 17:14:49 +0200 | [diff] [blame] | 1208 | other_cl_owner=other_cl_owner, |
Andrii Shyshkalov | 0da5e8f | 2018-10-30 17:29:18 +0000 | [diff] [blame] | 1209 | custom_cl_base=custom_cl_base, |
Anthony Polito | 8b95534 | 2019-09-24 19:01:36 +0000 | [diff] [blame] | 1210 | short_hostname=short_hostname, |
Josip Sokcevic | c39ab99 | 2020-09-24 20:09:15 +0000 | [diff] [blame] | 1211 | change_id=change_id, |
Edward Lesmes | 8c43c3f | 2021-01-20 00:20:26 +0000 | [diff] [blame] | 1212 | default_branch=default_branch) |
Andrii Shyshkalov | 5c3d0b3 | 2017-02-16 17:47:31 +0100 | [diff] [blame] | 1213 | if fetched_status != 'ABANDONED': |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 1214 | mock.patch( |
Edward Lemur | 1773f37 | 2020-02-22 00:27:14 +0000 | [diff] [blame] | 1215 | 'gclient_utils.temporary_file', TemporaryFileMock()).start() |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 1216 | mock.patch('os.remove', return_value=True).start() |
Andrii Shyshkalov | 5c3d0b3 | 2017-02-16 17:47:31 +0100 | [diff] [blame] | 1217 | self.calls += self._gerrit_upload_calls( |
Josip Sokcevic | f2cfd3d | 2021-03-30 18:39:18 +0000 | [diff] [blame] | 1218 | description, |
| 1219 | reviewers, |
| 1220 | squash, |
Andrii Shyshkalov | 5c3d0b3 | 2017-02-16 17:47:31 +0100 | [diff] [blame] | 1221 | squash_mode=squash_mode, |
Josip Sokcevic | f2cfd3d | 2021-03-30 18:39:18 +0000 | [diff] [blame] | 1222 | title=title, |
| 1223 | notify=notify, |
Andrii Shyshkalov | 5c3d0b3 | 2017-02-16 17:47:31 +0100 | [diff] [blame] | 1224 | post_amend_description=post_amend_description, |
Josip Sokcevic | f2cfd3d | 2021-03-30 18:39:18 +0000 | [diff] [blame] | 1225 | issue=issue, |
| 1226 | cc=cc, |
| 1227 | custom_cl_base=custom_cl_base, |
| 1228 | tbr=tbr, |
Andrii Shyshkalov | e7a7fc4 | 2018-10-30 17:35:09 +0000 | [diff] [blame] | 1229 | short_hostname=short_hostname, |
Edward Lemur | 1b52d87 | 2019-05-09 21:12:12 +0000 | [diff] [blame] | 1230 | labels=labels, |
| 1231 | change_id=change_id, |
Edward Lemur | 1b52d87 | 2019-05-09 21:12:12 +0000 | [diff] [blame] | 1232 | final_description=final_description, |
Anthony Polito | 8b95534 | 2019-09-24 19:01:36 +0000 | [diff] [blame] | 1233 | gitcookies_exists=gitcookies_exists, |
Josip | e827b0f | 2020-01-30 00:07:20 +0000 | [diff] [blame] | 1234 | force=force, |
Josip Sokcevic | c39ab99 | 2020-09-24 20:09:15 +0000 | [diff] [blame] | 1235 | edit_description=edit_description, |
Josip Sokcevic | f2cfd3d | 2021-03-30 18:39:18 +0000 | [diff] [blame] | 1236 | default_branch=default_branch, |
| 1237 | push_opts=push_opts) |
tandrii@chromium.org | 09d7a6a | 2016-03-04 15:44:48 +0000 | [diff] [blame] | 1238 | # Uncomment when debugging. |
Raul Tambre | 80ee78e | 2019-05-06 22:41:05 +0000 | [diff] [blame] | 1239 | # 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] | 1240 | git_cl.main(['upload'] + upload_args) |
Edward Lemur | 8515328 | 2020-02-14 22:06:29 +0000 | [diff] [blame] | 1241 | if squash: |
Edward Lemur | 2696407 | 2020-02-19 19:18:51 +0000 | [diff] [blame] | 1242 | self.assertIssueAndPatchset(patchset=None) |
Edward Lemur | 8515328 | 2020-02-14 22:06:29 +0000 | [diff] [blame] | 1243 | self.assertEqual( |
| 1244 | 'abcdef0123456789', |
Edward Lemur | 2696407 | 2020-02-19 19:18:51 +0000 | [diff] [blame] | 1245 | scm.GIT.GetBranchConfig('', 'master', 'gerritsquashhash')) |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1246 | |
Edward Lemur | 1b52d87 | 2019-05-09 21:12:12 +0000 | [diff] [blame] | 1247 | def test_gerrit_upload_traces_no_gitcookies(self): |
| 1248 | self._run_gerrit_upload_test( |
| 1249 | ['--no-squash'], |
Edward Lemur | 0db01f0 | 2019-11-12 22:01:51 +0000 | [diff] [blame] | 1250 | 'desc ✔\n\nBUG=\n', |
Edward Lemur | 1b52d87 | 2019-05-09 21:12:12 +0000 | [diff] [blame] | 1251 | [], |
| 1252 | squash=False, |
Edward Lemur | 0db01f0 | 2019-11-12 22:01:51 +0000 | [diff] [blame] | 1253 | post_amend_description='desc ✔\n\nBUG=\n\nChange-Id: Ixxx', |
Edward Lemur | 1b52d87 | 2019-05-09 21:12:12 +0000 | [diff] [blame] | 1254 | change_id='Ixxx', |
| 1255 | gitcookies_exists=False) |
| 1256 | |
sivachandra@chromium.org | aebe87f | 2012-10-22 20:34:21 +0000 | [diff] [blame] | 1257 | def test_gerrit_upload_without_change_id(self): |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1258 | self._run_gerrit_upload_test( |
Edward Lemur | 5a644f8 | 2020-03-18 16:44:57 +0000 | [diff] [blame] | 1259 | [], |
| 1260 | 'desc ✔\n\nBUG=\n\nChange-Id: Ixxx', |
| 1261 | [], |
| 1262 | change_id='Ixxx') |
| 1263 | |
| 1264 | def test_gerrit_upload_without_change_id_nosquash(self): |
| 1265 | self._run_gerrit_upload_test( |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1266 | ['--no-squash'], |
Edward Lemur | 0db01f0 | 2019-11-12 22:01:51 +0000 | [diff] [blame] | 1267 | 'desc ✔\n\nBUG=\n', |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1268 | [], |
| 1269 | squash=False, |
Edward Lemur | 0db01f0 | 2019-11-12 22:01:51 +0000 | [diff] [blame] | 1270 | post_amend_description='desc ✔\n\nBUG=\n\nChange-Id: Ixxx', |
Edward Lemur | 1b52d87 | 2019-05-09 21:12:12 +0000 | [diff] [blame] | 1271 | change_id='Ixxx') |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1272 | |
Edward Lesmes | 4de5413 | 2020-05-05 19:41:33 +0000 | [diff] [blame] | 1273 | def test_gerrit_upload_without_change_id_override_nosquash(self): |
| 1274 | self._run_gerrit_upload_test( |
| 1275 | [], |
| 1276 | 'desc ✔\n\nBUG=\n', |
| 1277 | [], |
| 1278 | squash=False, |
| 1279 | squash_mode='override_nosquash', |
| 1280 | post_amend_description='desc ✔\n\nBUG=\n\nChange-Id: Ixxx', |
| 1281 | change_id='Ixxx') |
| 1282 | |
sivachandra@chromium.org | aebe87f | 2012-10-22 20:34:21 +0000 | [diff] [blame] | 1283 | def test_gerrit_no_reviewer(self): |
| 1284 | self._run_gerrit_upload_test( |
Edward Lesmes | 4de5413 | 2020-05-05 19:41:33 +0000 | [diff] [blame] | 1285 | [], |
Edward Lemur | 0db01f0 | 2019-11-12 22:01:51 +0000 | [diff] [blame] | 1286 | 'desc ✔\n\nBUG=\n\nChange-Id: I123456789\n', |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1287 | [], |
| 1288 | squash=False, |
Edward Lesmes | 4de5413 | 2020-05-05 19:41:33 +0000 | [diff] [blame] | 1289 | squash_mode='override_nosquash', |
Edward Lemur | 1b52d87 | 2019-05-09 21:12:12 +0000 | [diff] [blame] | 1290 | change_id='I123456789') |
sivachandra@chromium.org | aebe87f | 2012-10-22 20:34:21 +0000 | [diff] [blame] | 1291 | |
Josip Sokcevic | f2cfd3d | 2021-03-30 18:39:18 +0000 | [diff] [blame] | 1292 | def test_gerrit_push_opts(self): |
| 1293 | self._run_gerrit_upload_test(['-o', 'wip'], |
| 1294 | 'desc ✔\n\nBUG=\n\nChange-Id: I123456789\n', |
| 1295 | [], |
| 1296 | squash=False, |
| 1297 | squash_mode='override_nosquash', |
| 1298 | change_id='I123456789', |
| 1299 | push_opts=['-o', 'wip']) |
| 1300 | |
Andrii Shyshkalov | 0da5e8f | 2018-10-30 17:29:18 +0000 | [diff] [blame] | 1301 | def test_gerrit_no_reviewer_non_chromium_host(self): |
| 1302 | # TODO(crbug/877717): remove this test case. |
Josip Sokcevic | f2cfd3d | 2021-03-30 18:39:18 +0000 | [diff] [blame] | 1303 | self._run_gerrit_upload_test([], |
| 1304 | 'desc ✔\n\nBUG=\n\nChange-Id: I123456789\n', |
| 1305 | [], |
| 1306 | squash=False, |
| 1307 | squash_mode='override_nosquash', |
| 1308 | short_hostname='other', |
| 1309 | change_id='I123456789') |
Andrii Shyshkalov | 0da5e8f | 2018-10-30 17:29:18 +0000 | [diff] [blame] | 1310 | |
Edward Lesmes | 0dd5482 | 2020-03-26 18:24:25 +0000 | [diff] [blame] | 1311 | def test_gerrit_patchset_title_special_chars_nosquash(self): |
Andrii Shyshkalov | febbae9 | 2017-04-05 15:05:20 +0000 | [diff] [blame] | 1312 | self._run_gerrit_upload_test( |
Edward Lesmes | 4de5413 | 2020-05-05 19:41:33 +0000 | [diff] [blame] | 1313 | ['-f', '-t', 'We\'ll escape ^_ ^ special chars...@{u}'], |
Edward Lemur | 0db01f0 | 2019-11-12 22:01:51 +0000 | [diff] [blame] | 1314 | 'desc ✔\n\nBUG=\n\nChange-Id: I123456789', |
Andrii Shyshkalov | febbae9 | 2017-04-05 15:05:20 +0000 | [diff] [blame] | 1315 | squash=False, |
Edward Lesmes | 4de5413 | 2020-05-05 19:41:33 +0000 | [diff] [blame] | 1316 | squash_mode='override_nosquash', |
Edward Lemur | 1b52d87 | 2019-05-09 21:12:12 +0000 | [diff] [blame] | 1317 | change_id='I123456789', |
Edward Lemur | 5a644f8 | 2020-03-18 16:44:57 +0000 | [diff] [blame] | 1318 | title='We\'ll escape ^_ ^ special chars...@{u}') |
Andrii Shyshkalov | febbae9 | 2017-04-05 15:05:20 +0000 | [diff] [blame] | 1319 | |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1320 | def test_gerrit_reviewers_cmd_line(self): |
sivachandra@chromium.org | aebe87f | 2012-10-22 20:34:21 +0000 | [diff] [blame] | 1321 | self._run_gerrit_upload_test( |
Edward Lesmes | 4de5413 | 2020-05-05 19:41:33 +0000 | [diff] [blame] | 1322 | ['-r', 'foo@example.com', '--send-mail'], |
Edward Lemur | 0db01f0 | 2019-11-12 22:01:51 +0000 | [diff] [blame] | 1323 | 'desc ✔\n\nBUG=\n\nChange-Id: I123456789', |
Edward Lemur | 5a644f8 | 2020-03-18 16:44:57 +0000 | [diff] [blame] | 1324 | reviewers=['foo@example.com'], |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1325 | squash=False, |
Edward Lesmes | 4de5413 | 2020-05-05 19:41:33 +0000 | [diff] [blame] | 1326 | squash_mode='override_nosquash', |
Edward Lemur | 1b52d87 | 2019-05-09 21:12:12 +0000 | [diff] [blame] | 1327 | notify=True, |
| 1328 | change_id='I123456789', |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 1329 | final_description=( |
Edward Lemur | 0db01f0 | 2019-11-12 22:01:51 +0000 | [diff] [blame] | 1330 | 'desc ✔\n\nBUG=\nR=foo@example.com\n\nChange-Id: I123456789')) |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1331 | |
Anthony Polito | 8b95534 | 2019-09-24 19:01:36 +0000 | [diff] [blame] | 1332 | def test_gerrit_upload_force_sets_bug(self): |
| 1333 | self._run_gerrit_upload_test( |
| 1334 | ['-b', '10000', '-f'], |
| 1335 | u'desc=\n\nBug: 10000\nChange-Id: Ixxx', |
| 1336 | [], |
| 1337 | force=True, |
Anthony Polito | 8b95534 | 2019-09-24 19:01:36 +0000 | [diff] [blame] | 1338 | fetched_description='desc=\n\nChange-Id: Ixxx', |
Anthony Polito | 8b95534 | 2019-09-24 19:01:36 +0000 | [diff] [blame] | 1339 | change_id='Ixxx') |
| 1340 | |
Edward Lemur | 5fb2224 | 2020-03-12 22:05:13 +0000 | [diff] [blame] | 1341 | def test_gerrit_upload_corrects_wrong_change_id(self): |
Anthony Polito | 8b95534 | 2019-09-24 19:01:36 +0000 | [diff] [blame] | 1342 | self._run_gerrit_upload_test( |
Edward Lemur | 5fb2224 | 2020-03-12 22:05:13 +0000 | [diff] [blame] | 1343 | ['-b', '10000', '-m', 'Title', '--edit-description'], |
| 1344 | u'desc=\n\nBug: 10000\nChange-Id: Ixxxx', |
Anthony Polito | 8b95534 | 2019-09-24 19:01:36 +0000 | [diff] [blame] | 1345 | [], |
Anthony Polito | 8b95534 | 2019-09-24 19:01:36 +0000 | [diff] [blame] | 1346 | issue='123456', |
Edward Lemur | 5fb2224 | 2020-03-12 22:05:13 +0000 | [diff] [blame] | 1347 | edit_description='desc=\n\nBug: 10000\nChange-Id: Izzzz', |
Anthony Polito | 8b95534 | 2019-09-24 19:01:36 +0000 | [diff] [blame] | 1348 | fetched_description='desc=\n\nChange-Id: Ixxxx', |
Anthony Polito | 8b95534 | 2019-09-24 19:01:36 +0000 | [diff] [blame] | 1349 | title='Title', |
Edward Lemur | 5fb2224 | 2020-03-12 22:05:13 +0000 | [diff] [blame] | 1350 | change_id='Ixxxx') |
Anthony Polito | 8b95534 | 2019-09-24 19:01:36 +0000 | [diff] [blame] | 1351 | |
Dan Beam | d8b04ca | 2019-10-10 21:23:26 +0000 | [diff] [blame] | 1352 | def test_gerrit_upload_force_sets_fixed(self): |
| 1353 | self._run_gerrit_upload_test( |
| 1354 | ['-x', '10000', '-f'], |
| 1355 | u'desc=\n\nFixed: 10000\nChange-Id: Ixxx', |
| 1356 | [], |
| 1357 | force=True, |
Dan Beam | d8b04ca | 2019-10-10 21:23:26 +0000 | [diff] [blame] | 1358 | fetched_description='desc=\n\nChange-Id: Ixxx', |
Dan Beam | d8b04ca | 2019-10-10 21:23:26 +0000 | [diff] [blame] | 1359 | change_id='Ixxx') |
| 1360 | |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1361 | def test_gerrit_reviewer_multiple(self): |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 1362 | mock.patch('git_cl.gerrit_util.GetCodeReviewTbrScore', |
| 1363 | lambda *a: self._mocked_call('GetCodeReviewTbrScore', *a)).start() |
sivachandra@chromium.org | aebe87f | 2012-10-22 20:34:21 +0000 | [diff] [blame] | 1364 | self._run_gerrit_upload_test( |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1365 | [], |
Edward Lemur | 0db01f0 | 2019-11-12 22:01:51 +0000 | [diff] [blame] | 1366 | 'desc ✔\nTBR=reviewer@example.com\nBUG=\nR=another@example.com\n' |
bradnelson | d975b30 | 2016-10-23 12:20:23 -0700 | [diff] [blame] | 1367 | 'CC=more@example.com,people@example.com\n\n' |
Yoshisato Yanagisawa | 81e3ff5 | 2017-09-26 15:33:34 +0900 | [diff] [blame] | 1368 | 'Change-Id: 123456789', |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1369 | ['reviewer@example.com', 'another@example.com'], |
Aaron Gable | fd23808 | 2017-06-07 13:42:34 -0700 | [diff] [blame] | 1370 | cc=['more@example.com', 'people@example.com'], |
Edward Lemur | 687ca90 | 2018-12-05 02:30:30 +0000 | [diff] [blame] | 1371 | tbr='reviewer@example.com', |
Edward Lemur | 1b52d87 | 2019-05-09 21:12:12 +0000 | [diff] [blame] | 1372 | labels={'Code-Review': 2}, |
Edward Lemur | 5a644f8 | 2020-03-18 16:44:57 +0000 | [diff] [blame] | 1373 | change_id='123456789') |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1374 | |
| 1375 | def test_gerrit_upload_squash_first_is_default(self): |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1376 | self._run_gerrit_upload_test( |
| 1377 | [], |
Edward Lemur | 0db01f0 | 2019-11-12 22:01:51 +0000 | [diff] [blame] | 1378 | 'desc ✔\nBUG=\n\nChange-Id: 123456789', |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1379 | [], |
Edward Lemur | 5a644f8 | 2020-03-18 16:44:57 +0000 | [diff] [blame] | 1380 | change_id='123456789') |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1381 | |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 1382 | def test_gerrit_upload_squash_first(self): |
bauerb@chromium.org | 27386dd | 2015-02-16 10:45:39 +0000 | [diff] [blame] | 1383 | self._run_gerrit_upload_test( |
| 1384 | ['--squash'], |
Edward Lemur | 0db01f0 | 2019-11-12 22:01:51 +0000 | [diff] [blame] | 1385 | 'desc ✔\nBUG=\n\nChange-Id: 123456789', |
bauerb@chromium.org | 27386dd | 2015-02-16 10:45:39 +0000 | [diff] [blame] | 1386 | [], |
luqui@chromium.org | 609f395 | 2015-05-04 22:47:04 +0000 | [diff] [blame] | 1387 | squash=True, |
Edward Lemur | 5a644f8 | 2020-03-18 16:44:57 +0000 | [diff] [blame] | 1388 | change_id='123456789') |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1389 | |
Edward Lesmes | 0dd5482 | 2020-03-26 18:24:25 +0000 | [diff] [blame] | 1390 | def test_gerrit_upload_squash_first_title(self): |
| 1391 | self._run_gerrit_upload_test( |
| 1392 | ['-f', '-t', 'title'], |
| 1393 | 'title\n\ndesc\n\nChange-Id: 123456789', |
| 1394 | [], |
| 1395 | force=True, |
| 1396 | squash=True, |
| 1397 | log_description='desc', |
| 1398 | change_id='123456789') |
| 1399 | |
Andrii Shyshkalov | e7a7fc4 | 2018-10-30 17:35:09 +0000 | [diff] [blame] | 1400 | def test_gerrit_upload_squash_first_with_labels(self): |
| 1401 | self._run_gerrit_upload_test( |
| 1402 | ['--squash', '--cq-dry-run', '--enable-auto-submit'], |
Edward Lemur | 0db01f0 | 2019-11-12 22:01:51 +0000 | [diff] [blame] | 1403 | 'desc ✔\nBUG=\n\nChange-Id: 123456789', |
Andrii Shyshkalov | e7a7fc4 | 2018-10-30 17:35:09 +0000 | [diff] [blame] | 1404 | [], |
| 1405 | squash=True, |
Edward Lemur | 1b52d87 | 2019-05-09 21:12:12 +0000 | [diff] [blame] | 1406 | labels={'Commit-Queue': 1, 'Auto-Submit': 1}, |
Edward Lemur | 5a644f8 | 2020-03-18 16:44:57 +0000 | [diff] [blame] | 1407 | change_id='123456789') |
Andrii Shyshkalov | e7a7fc4 | 2018-10-30 17:35:09 +0000 | [diff] [blame] | 1408 | |
Andrii Shyshkalov | 550e924 | 2017-04-12 17:14:49 +0200 | [diff] [blame] | 1409 | def test_gerrit_upload_squash_first_against_rev(self): |
| 1410 | custom_cl_base = 'custom_cl_base_rev_or_branch' |
| 1411 | self._run_gerrit_upload_test( |
| 1412 | ['--squash', custom_cl_base], |
Edward Lemur | 0db01f0 | 2019-11-12 22:01:51 +0000 | [diff] [blame] | 1413 | 'desc ✔\nBUG=\n\nChange-Id: 123456789', |
Andrii Shyshkalov | 550e924 | 2017-04-12 17:14:49 +0200 | [diff] [blame] | 1414 | [], |
| 1415 | squash=True, |
Edward Lemur | 1b52d87 | 2019-05-09 21:12:12 +0000 | [diff] [blame] | 1416 | custom_cl_base=custom_cl_base, |
Edward Lemur | 5a644f8 | 2020-03-18 16:44:57 +0000 | [diff] [blame] | 1417 | change_id='123456789') |
Andrii Shyshkalov | 550e924 | 2017-04-12 17:14:49 +0200 | [diff] [blame] | 1418 | self.assertIn( |
| 1419 | 'If you proceed with upload, more than 1 CL may be created by Gerrit', |
| 1420 | sys.stdout.getvalue()) |
| 1421 | |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 1422 | def test_gerrit_upload_squash_reupload(self): |
Edward Lemur | 0db01f0 | 2019-11-12 22:01:51 +0000 | [diff] [blame] | 1423 | description = 'desc ✔\nBUG=\n\nChange-Id: 123456789' |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 1424 | self._run_gerrit_upload_test( |
| 1425 | ['--squash'], |
| 1426 | description, |
| 1427 | [], |
| 1428 | squash=True, |
Edward Lemur | 1b52d87 | 2019-05-09 21:12:12 +0000 | [diff] [blame] | 1429 | issue=123456, |
Edward Lemur | 5a644f8 | 2020-03-18 16:44:57 +0000 | [diff] [blame] | 1430 | change_id='123456789') |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 1431 | |
Edward Lemur | d55c507 | 2020-02-20 01:09:07 +0000 | [diff] [blame] | 1432 | @mock.patch('sys.stderr', StringIO()) |
Andrii Shyshkalov | 5c3d0b3 | 2017-02-16 17:47:31 +0100 | [diff] [blame] | 1433 | def test_gerrit_upload_squash_reupload_to_abandoned(self): |
Edward Lemur | 0db01f0 | 2019-11-12 22:01:51 +0000 | [diff] [blame] | 1434 | description = 'desc ✔\nBUG=\n\nChange-Id: 123456789' |
Andrii Shyshkalov | 5c3d0b3 | 2017-02-16 17:47:31 +0100 | [diff] [blame] | 1435 | with self.assertRaises(SystemExitMock): |
| 1436 | self._run_gerrit_upload_test( |
| 1437 | ['--squash'], |
| 1438 | description, |
| 1439 | [], |
| 1440 | squash=True, |
Andrii Shyshkalov | 5c3d0b3 | 2017-02-16 17:47:31 +0100 | [diff] [blame] | 1441 | issue=123456, |
Edward Lemur | 1b52d87 | 2019-05-09 21:12:12 +0000 | [diff] [blame] | 1442 | fetched_status='ABANDONED', |
| 1443 | change_id='123456789') |
Edward Lemur | d55c507 | 2020-02-20 01:09:07 +0000 | [diff] [blame] | 1444 | self.assertEqual( |
| 1445 | 'Change https://chromium-review.googlesource.com/123456 has been ' |
| 1446 | 'abandoned, new uploads are not allowed\n', |
| 1447 | sys.stderr.getvalue()) |
Andrii Shyshkalov | 5c3d0b3 | 2017-02-16 17:47:31 +0100 | [diff] [blame] | 1448 | |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 1449 | @mock.patch( |
| 1450 | 'gerrit_util.GetAccountDetails', |
| 1451 | return_value={'email': 'yet-another@example.com'}) |
| 1452 | def test_gerrit_upload_squash_reupload_to_not_owned(self, _mock): |
Edward Lemur | 0db01f0 | 2019-11-12 22:01:51 +0000 | [diff] [blame] | 1453 | description = 'desc ✔\nBUG=\n\nChange-Id: 123456789' |
Andrii Shyshkalov | bb86fbb | 2017-03-24 14:59:28 +0100 | [diff] [blame] | 1454 | self._run_gerrit_upload_test( |
| 1455 | ['--squash'], |
| 1456 | description, |
| 1457 | [], |
| 1458 | squash=True, |
Andrii Shyshkalov | bb86fbb | 2017-03-24 14:59:28 +0100 | [diff] [blame] | 1459 | issue=123456, |
Edward Lemur | 1b52d87 | 2019-05-09 21:12:12 +0000 | [diff] [blame] | 1460 | other_cl_owner='other@example.com', |
Edward Lemur | 5a644f8 | 2020-03-18 16:44:57 +0000 | [diff] [blame] | 1461 | change_id='123456789') |
Andrii Shyshkalov | bb86fbb | 2017-03-24 14:59:28 +0100 | [diff] [blame] | 1462 | self.assertIn( |
Quinten Yearsley | 0c62da9 | 2017-05-31 13:39:42 -0700 | [diff] [blame] | 1463 | 'WARNING: Change 123456 is owned by other@example.com, but you ' |
Andrii Shyshkalov | bb86fbb | 2017-03-24 14:59:28 +0100 | [diff] [blame] | 1464 | 'authenticate to Gerrit as yet-another@example.com.\n' |
| 1465 | 'Uploading may fail due to lack of permissions', |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 1466 | sys.stdout.getvalue()) |
Andrii Shyshkalov | bb86fbb | 2017-03-24 14:59:28 +0100 | [diff] [blame] | 1467 | |
Josip | e827b0f | 2020-01-30 00:07:20 +0000 | [diff] [blame] | 1468 | def test_upload_change_description_editor(self): |
| 1469 | fetched_description = 'foo\n\nChange-Id: 123456789' |
| 1470 | description = 'bar\n\nChange-Id: 123456789' |
| 1471 | self._run_gerrit_upload_test( |
| 1472 | ['--squash', '--edit-description'], |
| 1473 | description, |
| 1474 | [], |
| 1475 | fetched_description=fetched_description, |
| 1476 | squash=True, |
Josip | e827b0f | 2020-01-30 00:07:20 +0000 | [diff] [blame] | 1477 | issue=123456, |
| 1478 | change_id='123456789', |
Josip | e827b0f | 2020-01-30 00:07:20 +0000 | [diff] [blame] | 1479 | edit_description=description) |
| 1480 | |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 1481 | @mock.patch('git_cl.RunGit') |
| 1482 | @mock.patch('git_cl.CMDupload') |
Edward Lemur | 1a83da1 | 2020-03-04 21:18:36 +0000 | [diff] [blame] | 1483 | @mock.patch('sys.stdin', StringIO('\n')) |
| 1484 | @mock.patch('sys.stdout', StringIO()) |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 1485 | def test_upload_branch_deps(self, *_mocks): |
rmistry@google.com | 2dd9986 | 2015-06-22 12:22:18 +0000 | [diff] [blame] | 1486 | def mock_run_git(*args, **_kwargs): |
| 1487 | if args[0] == ['for-each-ref', |
| 1488 | '--format=%(refname:short) %(upstream:short)', |
| 1489 | 'refs/heads']: |
| 1490 | # Create a local branch dependency tree that looks like this: |
| 1491 | # test1 -> test2 -> test3 -> test4 -> test5 |
| 1492 | # -> test3.1 |
| 1493 | # test6 -> test0 |
| 1494 | branch_deps = [ |
| 1495 | 'test2 test1', # test1 -> test2 |
| 1496 | 'test3 test2', # test2 -> test3 |
| 1497 | 'test3.1 test2', # test2 -> test3.1 |
| 1498 | 'test4 test3', # test3 -> test4 |
| 1499 | 'test5 test4', # test4 -> test5 |
| 1500 | 'test6 test0', # test0 -> test6 |
| 1501 | 'test7', # test7 |
| 1502 | ] |
| 1503 | return '\n'.join(branch_deps) |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 1504 | git_cl.RunGit.side_effect = mock_run_git |
| 1505 | git_cl.CMDupload.return_value = 0 |
rmistry@google.com | 2dd9986 | 2015-06-22 12:22:18 +0000 | [diff] [blame] | 1506 | |
| 1507 | class MockChangelist(): |
| 1508 | def __init__(self): |
| 1509 | pass |
| 1510 | def GetBranch(self): |
| 1511 | return 'test1' |
| 1512 | def GetIssue(self): |
| 1513 | return '123' |
| 1514 | def GetPatchset(self): |
| 1515 | return '1001' |
tandrii@chromium.org | 4c72b08 | 2016-03-31 22:26:35 +0000 | [diff] [blame] | 1516 | def IsGerrit(self): |
| 1517 | return False |
rmistry@google.com | 2dd9986 | 2015-06-22 12:22:18 +0000 | [diff] [blame] | 1518 | |
| 1519 | ret = git_cl.upload_branch_deps(MockChangelist(), []) |
| 1520 | # CMDupload should have been called 5 times because of 5 dependent branches. |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 1521 | self.assertEqual(5, len(git_cl.CMDupload.mock_calls)) |
Edward Lemur | 1a83da1 | 2020-03-04 21:18:36 +0000 | [diff] [blame] | 1522 | self.assertIn( |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 1523 | 'This command will checkout all dependent branches ' |
| 1524 | 'and run "git cl upload". Press Enter to continue, ' |
Edward Lemur | 1a83da1 | 2020-03-04 21:18:36 +0000 | [diff] [blame] | 1525 | 'or Ctrl+C to abort', |
| 1526 | sys.stdout.getvalue()) |
Raphael Kubo da Costa | e9342a7 | 2019-10-14 17:49:39 +0000 | [diff] [blame] | 1527 | self.assertEqual(0, ret) |
rmistry@google.com | 2dd9986 | 2015-06-22 12:22:18 +0000 | [diff] [blame] | 1528 | |
tandrii@chromium.org | 65874e1 | 2016-03-04 12:03:02 +0000 | [diff] [blame] | 1529 | def test_gerrit_change_id(self): |
| 1530 | self.calls = [ |
| 1531 | ((['git', 'write-tree'], ), |
| 1532 | 'hashtree'), |
| 1533 | ((['git', 'rev-parse', 'HEAD~0'], ), |
| 1534 | 'branch-parent'), |
| 1535 | ((['git', 'var', 'GIT_AUTHOR_IDENT'], ), |
| 1536 | 'A B <a@b.org> 1456848326 +0100'), |
| 1537 | ((['git', 'var', 'GIT_COMMITTER_IDENT'], ), |
| 1538 | 'C D <c@d.org> 1456858326 +0100'), |
| 1539 | ((['git', 'hash-object', '-t', 'commit', '--stdin'], ), |
| 1540 | 'hashchange'), |
| 1541 | ] |
| 1542 | change_id = git_cl.GenerateGerritChangeId('line1\nline2\n') |
| 1543 | self.assertEqual(change_id, 'Ihashchange') |
| 1544 | |
Edward Lesmes | 8170c29 | 2021-03-19 20:04:43 +0000 | [diff] [blame] | 1545 | @mock.patch('gerrit_util.IsCodeOwnersEnabledOnHost') |
Edward Lesmes | 8c43c3f | 2021-01-20 00:20:26 +0000 | [diff] [blame] | 1546 | @mock.patch('git_cl.Settings.GetBugPrefix') |
Edward Lesmes | 8c43c3f | 2021-01-20 00:20:26 +0000 | [diff] [blame] | 1547 | @mock.patch('git_cl.Changelist.FetchDescription') |
| 1548 | @mock.patch('git_cl.Changelist.GetBranch') |
Edward Lesmes | 1eaaab5 | 2021-03-02 23:52:54 +0000 | [diff] [blame] | 1549 | @mock.patch('git_cl.Changelist.GetCommonAncestorWithUpstream') |
Edward Lesmes | e157691 | 2021-02-16 21:53:34 +0000 | [diff] [blame] | 1550 | @mock.patch('git_cl.Changelist.GetGerritHost') |
| 1551 | @mock.patch('git_cl.Changelist.GetGerritProject') |
| 1552 | @mock.patch('git_cl.Changelist.GetRemoteBranch') |
Edward Lesmes | 8c43c3f | 2021-01-20 00:20:26 +0000 | [diff] [blame] | 1553 | @mock.patch('owners_client.OwnersClient.BatchListOwners') |
| 1554 | def getDescriptionForUploadTest( |
Edward Lesmes | e157691 | 2021-02-16 21:53:34 +0000 | [diff] [blame] | 1555 | self, mockBatchListOwners=None, mockGetRemoteBranch=None, |
Edward Lesmes | 1eaaab5 | 2021-03-02 23:52:54 +0000 | [diff] [blame] | 1556 | mockGetGerritProject=None, mockGetGerritHost=None, |
| 1557 | mockGetCommonAncestorWithUpstream=None, mockGetBranch=None, |
Edward Lesmes | e157691 | 2021-02-16 21:53:34 +0000 | [diff] [blame] | 1558 | mockFetchDescription=None, mockGetBugPrefix=None, |
Edward Lesmes | 8170c29 | 2021-03-19 20:04:43 +0000 | [diff] [blame] | 1559 | mockIsCodeOwnersEnabledOnHost=None, |
Edward Lesmes | 8c43c3f | 2021-01-20 00:20:26 +0000 | [diff] [blame] | 1560 | initial_description='desc', bug=None, fixed=None, branch='branch', |
| 1561 | reviewers=None, tbrs=None, add_owners_to=None, |
| 1562 | expected_description='desc'): |
| 1563 | reviewers = reviewers or [] |
| 1564 | tbrs = tbrs or [] |
| 1565 | owners_by_path = { |
| 1566 | 'a': ['a@example.com'], |
| 1567 | 'b': ['b@example.com'], |
| 1568 | 'c': ['c@example.com'], |
| 1569 | } |
Edward Lesmes | 8170c29 | 2021-03-19 20:04:43 +0000 | [diff] [blame] | 1570 | mockIsCodeOwnersEnabledOnHost.return_value = True |
Edward Lesmes | 8c43c3f | 2021-01-20 00:20:26 +0000 | [diff] [blame] | 1571 | mockGetBranch.return_value = branch |
| 1572 | mockGetBugPrefix.return_value = 'prefix' |
Edward Lesmes | 1eaaab5 | 2021-03-02 23:52:54 +0000 | [diff] [blame] | 1573 | mockGetCommonAncestorWithUpstream.return_value = 'upstream' |
Edward Lesmes | e157691 | 2021-02-16 21:53:34 +0000 | [diff] [blame] | 1574 | mockGetRemoteBranch.return_value = ('origin', 'refs/remotes/origin/main') |
Edward Lesmes | 8c43c3f | 2021-01-20 00:20:26 +0000 | [diff] [blame] | 1575 | mockFetchDescription.return_value = 'desc' |
| 1576 | mockBatchListOwners.side_effect = lambda ps: { |
| 1577 | p: owners_by_path.get(p) |
| 1578 | for p in ps |
| 1579 | } |
| 1580 | |
| 1581 | cl = git_cl.Changelist(issue=1234) |
| 1582 | actual = cl._GetDescriptionForUpload( |
| 1583 | options=mock.Mock( |
| 1584 | bug=bug, |
| 1585 | fixed=fixed, |
| 1586 | reviewers=reviewers, |
| 1587 | tbrs=tbrs, |
| 1588 | add_owners_to=add_owners_to), |
| 1589 | git_diff_args=None, |
| 1590 | files=list(owners_by_path)) |
| 1591 | self.assertEqual(expected_description, actual.description) |
| 1592 | |
| 1593 | def testGetDescriptionForUpload(self): |
| 1594 | self.getDescriptionForUploadTest() |
| 1595 | |
| 1596 | def testGetDescriptionForUpload_Bug(self): |
| 1597 | self.getDescriptionForUploadTest( |
| 1598 | bug='1234', |
| 1599 | expected_description='\n'.join([ |
| 1600 | 'desc', |
| 1601 | '', |
| 1602 | 'Bug: prefix:1234', |
| 1603 | ])) |
| 1604 | |
| 1605 | def testGetDescriptionForUpload_Fixed(self): |
| 1606 | self.getDescriptionForUploadTest( |
| 1607 | fixed='1234', |
| 1608 | expected_description='\n'.join([ |
| 1609 | 'desc', |
| 1610 | '', |
| 1611 | 'Fixed: prefix:1234', |
| 1612 | ])) |
| 1613 | |
| 1614 | |
| 1615 | def testGetDescriptionForUpload_BugFromBranch(self): |
| 1616 | self.getDescriptionForUploadTest( |
| 1617 | branch='bug-1234', |
| 1618 | expected_description='\n'.join([ |
| 1619 | 'desc', |
| 1620 | '', |
| 1621 | 'Bug: prefix:1234', |
| 1622 | ])) |
| 1623 | |
| 1624 | def testGetDescriptionForUpload_FixedFromBranch(self): |
| 1625 | self.getDescriptionForUploadTest( |
| 1626 | branch='fix-1234', |
| 1627 | expected_description='\n'.join([ |
| 1628 | 'desc', |
| 1629 | '', |
| 1630 | 'Fixed: prefix:1234', |
| 1631 | ])) |
| 1632 | |
| 1633 | def testGetDescriptionForUpload_AddOwnersToR(self): |
| 1634 | self.getDescriptionForUploadTest( |
| 1635 | reviewers=['a@example.com'], |
| 1636 | tbrs=['b@example.com'], |
| 1637 | add_owners_to='R', |
| 1638 | expected_description='\n'.join([ |
| 1639 | 'desc', |
| 1640 | '', |
| 1641 | 'R=a@example.com, c@example.com', |
| 1642 | 'TBR=b@example.com', |
| 1643 | ])) |
| 1644 | |
| 1645 | def testGetDescriptionForUpload_AddOwnersToTBR(self): |
| 1646 | self.getDescriptionForUploadTest( |
| 1647 | reviewers=['a@example.com'], |
| 1648 | tbrs=['b@example.com'], |
| 1649 | add_owners_to='TBR', |
| 1650 | expected_description='\n'.join([ |
| 1651 | 'desc', |
| 1652 | '', |
| 1653 | 'R=a@example.com', |
| 1654 | 'TBR=b@example.com, c@example.com', |
| 1655 | ])) |
| 1656 | |
| 1657 | def testGetDescriptionForUpload_AddOwnersToNoOwnersNeeded(self): |
| 1658 | self.getDescriptionForUploadTest( |
| 1659 | reviewers=['a@example.com', 'c@example.com'], |
| 1660 | tbrs=['b@example.com'], |
| 1661 | add_owners_to='TBR', |
| 1662 | expected_description='\n'.join([ |
| 1663 | 'desc', |
| 1664 | '', |
| 1665 | 'R=a@example.com, c@example.com', |
| 1666 | 'TBR=b@example.com', |
| 1667 | ])) |
| 1668 | |
| 1669 | def testGetDescriptionForUpload_Reviewers(self): |
| 1670 | self.getDescriptionForUploadTest( |
| 1671 | reviewers=['a@example.com', 'b@example.com'], |
| 1672 | expected_description='\n'.join([ |
| 1673 | 'desc', |
| 1674 | '', |
| 1675 | 'R=a@example.com, b@example.com', |
| 1676 | ])) |
| 1677 | |
| 1678 | def testGetDescriptionForUpload_TBRs(self): |
| 1679 | self.getDescriptionForUploadTest( |
| 1680 | tbrs=['a@example.com', 'b@example.com'], |
| 1681 | expected_description='\n'.join([ |
| 1682 | 'desc', |
| 1683 | '', |
| 1684 | 'TBR=a@example.com, b@example.com', |
| 1685 | ])) |
| 1686 | |
tandrii@chromium.org | 601e1d1 | 2016-06-03 13:03:54 +0000 | [diff] [blame] | 1687 | def test_desecription_append_footer(self): |
| 1688 | for init_desc, footer_line, expected_desc in [ |
| 1689 | # Use unique desc first lines for easy test failure identification. |
| 1690 | ('foo', 'R=one', 'foo\n\nR=one'), |
| 1691 | ('foo\n\nR=one', 'BUG=', 'foo\n\nR=one\nBUG='), |
| 1692 | ('foo\n\nR=one', 'Change-Id: Ixx', 'foo\n\nR=one\n\nChange-Id: Ixx'), |
| 1693 | ('foo\n\nChange-Id: Ixx', 'R=one', 'foo\n\nR=one\n\nChange-Id: Ixx'), |
| 1694 | ('foo\n\nR=one\n\nChange-Id: Ixx', 'TBR=two', |
| 1695 | 'foo\n\nR=one\nTBR=two\n\nChange-Id: Ixx'), |
| 1696 | ('foo\n\nR=one\n\nChange-Id: Ixx', 'Foo-Bar: baz', |
| 1697 | 'foo\n\nR=one\n\nChange-Id: Ixx\nFoo-Bar: baz'), |
| 1698 | ('foo\n\nChange-Id: Ixx', 'Foo-Bak: baz', |
| 1699 | 'foo\n\nChange-Id: Ixx\nFoo-Bak: baz'), |
| 1700 | ('foo', 'Change-Id: Ixx', 'foo\n\nChange-Id: Ixx'), |
| 1701 | ]: |
| 1702 | desc = git_cl.ChangeDescription(init_desc) |
| 1703 | desc.append_footer(footer_line) |
| 1704 | self.assertEqual(desc.description, expected_desc) |
| 1705 | |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 1706 | def test_update_reviewers(self): |
| 1707 | data = [ |
Stephen Martinis | 1c3c939 | 2021-01-07 02:42:33 +0000 | [diff] [blame] | 1708 | ('foo', [], [], |
Robert Iannucci | 6c98dc6 | 2017-04-18 11:38:00 -0700 | [diff] [blame] | 1709 | 'foo'), |
Stephen Martinis | 1c3c939 | 2021-01-07 02:42:33 +0000 | [diff] [blame] | 1710 | ('foo\nR=xx', [], [], |
Robert Iannucci | 6c98dc6 | 2017-04-18 11:38:00 -0700 | [diff] [blame] | 1711 | 'foo\nR=xx'), |
Stephen Martinis | 1c3c939 | 2021-01-07 02:42:33 +0000 | [diff] [blame] | 1712 | ('foo\nTBR=xx', [], [], |
Robert Iannucci | 6c98dc6 | 2017-04-18 11:38:00 -0700 | [diff] [blame] | 1713 | 'foo\nTBR=xx'), |
Stephen Martinis | 1c3c939 | 2021-01-07 02:42:33 +0000 | [diff] [blame] | 1714 | ('foo', ['a@c'], [], |
Robert Iannucci | 6c98dc6 | 2017-04-18 11:38:00 -0700 | [diff] [blame] | 1715 | 'foo\n\nR=a@c'), |
Stephen Martinis | 1c3c939 | 2021-01-07 02:42:33 +0000 | [diff] [blame] | 1716 | ('foo\nR=xx', ['a@c'], [], |
Robert Iannucci | 6c98dc6 | 2017-04-18 11:38:00 -0700 | [diff] [blame] | 1717 | 'foo\n\nR=a@c, xx'), |
Stephen Martinis | 1c3c939 | 2021-01-07 02:42:33 +0000 | [diff] [blame] | 1718 | ('foo\nTBR=xx', ['a@c'], [], |
Robert Iannucci | 6c98dc6 | 2017-04-18 11:38:00 -0700 | [diff] [blame] | 1719 | 'foo\n\nR=a@c\nTBR=xx'), |
Stephen Martinis | 1c3c939 | 2021-01-07 02:42:33 +0000 | [diff] [blame] | 1720 | ('foo\nTBR=xx\nR=yy', ['a@c'], [], |
Robert Iannucci | 6c98dc6 | 2017-04-18 11:38:00 -0700 | [diff] [blame] | 1721 | 'foo\n\nR=a@c, yy\nTBR=xx'), |
Stephen Martinis | 1c3c939 | 2021-01-07 02:42:33 +0000 | [diff] [blame] | 1722 | ('foo\nBUG=', ['a@c'], [], |
Robert Iannucci | 6c98dc6 | 2017-04-18 11:38:00 -0700 | [diff] [blame] | 1723 | 'foo\nBUG=\nR=a@c'), |
Stephen Martinis | 1c3c939 | 2021-01-07 02:42:33 +0000 | [diff] [blame] | 1724 | ('foo\nR=xx\nTBR=yy\nR=bar', ['a@c'], [], |
Robert Iannucci | 6c98dc6 | 2017-04-18 11:38:00 -0700 | [diff] [blame] | 1725 | 'foo\n\nR=a@c, bar, xx\nTBR=yy'), |
Stephen Martinis | 1c3c939 | 2021-01-07 02:42:33 +0000 | [diff] [blame] | 1726 | ('foo', ['a@c', 'b@c'], [], |
Robert Iannucci | 6c98dc6 | 2017-04-18 11:38:00 -0700 | [diff] [blame] | 1727 | 'foo\n\nR=a@c, b@c'), |
Stephen Martinis | 1c3c939 | 2021-01-07 02:42:33 +0000 | [diff] [blame] | 1728 | ('foo\nBar\n\nR=\nBUG=', ['c@c'], [], |
Robert Iannucci | 6c98dc6 | 2017-04-18 11:38:00 -0700 | [diff] [blame] | 1729 | 'foo\nBar\n\nR=c@c\nBUG='), |
Stephen Martinis | 1c3c939 | 2021-01-07 02:42:33 +0000 | [diff] [blame] | 1730 | ('foo\nBar\n\nR=\nBUG=\nR=', ['c@c'], [], |
Robert Iannucci | 6c98dc6 | 2017-04-18 11:38:00 -0700 | [diff] [blame] | 1731 | 'foo\nBar\n\nR=c@c\nBUG='), |
maruel@chromium.org | c6f60e8 | 2013-04-19 17:01:57 +0000 | [diff] [blame] | 1732 | # Same as the line before, but full of whitespaces. |
| 1733 | ( |
Stephen Martinis | 1c3c939 | 2021-01-07 02:42:33 +0000 | [diff] [blame] | 1734 | 'foo\nBar\n\n R = \n BUG = \n R = ', ['c@c'], [], |
maruel@chromium.org | c6f60e8 | 2013-04-19 17:01:57 +0000 | [diff] [blame] | 1735 | 'foo\nBar\n\nR=c@c\n BUG =', |
| 1736 | ), |
| 1737 | # Whitespaces aren't interpreted as new lines. |
Stephen Martinis | 1c3c939 | 2021-01-07 02:42:33 +0000 | [diff] [blame] | 1738 | ('foo BUG=allo R=joe ', ['c@c'], [], |
Robert Iannucci | 6c98dc6 | 2017-04-18 11:38:00 -0700 | [diff] [blame] | 1739 | 'foo BUG=allo R=joe\n\nR=c@c'), |
| 1740 | # Redundant TBRs get promoted to Rs |
Stephen Martinis | 1c3c939 | 2021-01-07 02:42:33 +0000 | [diff] [blame] | 1741 | ('foo\n\nR=a@c\nTBR=t@c', ['b@c', 'a@c'], ['a@c', 't@c'], |
Robert Iannucci | 6c98dc6 | 2017-04-18 11:38:00 -0700 | [diff] [blame] | 1742 | 'foo\n\nR=a@c, b@c\nTBR=t@c'), |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 1743 | ] |
Robert Iannucci | 6c98dc6 | 2017-04-18 11:38:00 -0700 | [diff] [blame] | 1744 | expected = [i[-1] for i in data] |
maruel@chromium.org | c6f60e8 | 2013-04-19 17:01:57 +0000 | [diff] [blame] | 1745 | actual = [] |
Stephen Martinis | 1c3c939 | 2021-01-07 02:42:33 +0000 | [diff] [blame] | 1746 | for orig, reviewers, tbrs, _expected in data: |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 1747 | obj = git_cl.ChangeDescription(orig) |
Edward Lesmes | 8c43c3f | 2021-01-20 00:20:26 +0000 | [diff] [blame] | 1748 | obj.update_reviewers(reviewers, tbrs) |
maruel@chromium.org | c6f60e8 | 2013-04-19 17:01:57 +0000 | [diff] [blame] | 1749 | actual.append(obj.description) |
| 1750 | self.assertEqual(expected, actual) |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 1751 | |
Nodir Turakulov | 23b8214 | 2017-11-16 11:04:25 -0800 | [diff] [blame] | 1752 | def test_get_hash_tags(self): |
Josip Sokcevic | c39ab99 | 2020-09-24 20:09:15 +0000 | [diff] [blame] | 1753 | self.calls = [ |
| 1754 | ((['git', 'show-branch', 'refs/remotes/origin/main'], ), |
| 1755 | callError(1)), |
| 1756 | ] * 9 |
Nodir Turakulov | 23b8214 | 2017-11-16 11:04:25 -0800 | [diff] [blame] | 1757 | cases = [ |
| 1758 | ('', []), |
| 1759 | ('a', []), |
| 1760 | ('[a]', ['a']), |
| 1761 | ('[aa]', ['aa']), |
| 1762 | ('[a ]', ['a']), |
| 1763 | ('[a- ]', ['a']), |
| 1764 | ('[a- b]', ['a-b']), |
| 1765 | ('[a--b]', ['a-b']), |
| 1766 | ('[a', []), |
| 1767 | ('[a]x', ['a']), |
| 1768 | ('[aa]x', ['aa']), |
| 1769 | ('[a b]', ['a-b']), |
| 1770 | ('[a b]', ['a-b']), |
| 1771 | ('[a__b]', ['a-b']), |
| 1772 | ('[a] x', ['a']), |
| 1773 | ('[a][b]', ['a', 'b']), |
| 1774 | ('[a] [b]', ['a', 'b']), |
| 1775 | ('[a][b]x', ['a', 'b']), |
| 1776 | ('[a][b] x', ['a', 'b']), |
| 1777 | ('[a]\n[b]', ['a']), |
| 1778 | ('[a\nb]', []), |
| 1779 | ('[a][', ['a']), |
| 1780 | ('Revert "[a] feature"', ['a']), |
| 1781 | ('Reland "[a] feature"', ['a']), |
| 1782 | ('Revert: [a] feature', ['a']), |
| 1783 | ('Reland: [a] feature', ['a']), |
| 1784 | ('Revert "Reland: [a] feature"', ['a']), |
| 1785 | ('Foo: feature', ['foo']), |
| 1786 | ('Foo Bar: feature', ['foo-bar']), |
Anthony Polito | 02b5af3 | 2019-12-02 19:49:47 +0000 | [diff] [blame] | 1787 | ('Change Foo::Bar', []), |
| 1788 | ('Foo: Change Foo::Bar', ['foo']), |
Nodir Turakulov | 23b8214 | 2017-11-16 11:04:25 -0800 | [diff] [blame] | 1789 | ('Revert "Foo bar: feature"', ['foo-bar']), |
| 1790 | ('Reland "Foo bar: feature"', ['foo-bar']), |
| 1791 | ] |
| 1792 | for desc, expected in cases: |
| 1793 | change_desc = git_cl.ChangeDescription(desc) |
| 1794 | actual = change_desc.get_hash_tags() |
| 1795 | self.assertEqual( |
| 1796 | actual, |
| 1797 | expected, |
| 1798 | 'GetHashTags(%r) == %r, expected %r' % (desc, actual, expected)) |
| 1799 | |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1800 | self.assertEqual(None, git_cl.GetTargetRef('origin', None, 'master')) |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 1801 | self.assertEqual(None, git_cl.GetTargetRef(None, |
| 1802 | 'refs/remotes/origin/master', |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1803 | 'master')) |
bauerb@chromium.org | 27386dd | 2015-02-16 10:45:39 +0000 | [diff] [blame] | 1804 | |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 1805 | # Check default target refs for branches. |
| 1806 | self.assertEqual('refs/heads/master', |
| 1807 | git_cl.GetTargetRef('origin', 'refs/remotes/origin/master', |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1808 | None)) |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 1809 | self.assertEqual('refs/heads/master', |
| 1810 | git_cl.GetTargetRef('origin', 'refs/remotes/origin/lkgr', |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1811 | None)) |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 1812 | self.assertEqual('refs/heads/master', |
| 1813 | git_cl.GetTargetRef('origin', 'refs/remotes/origin/lkcr', |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1814 | None)) |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 1815 | self.assertEqual('refs/branch-heads/123', |
| 1816 | git_cl.GetTargetRef('origin', |
| 1817 | 'refs/remotes/branch-heads/123', |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1818 | None)) |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 1819 | self.assertEqual('refs/diff/test', |
| 1820 | git_cl.GetTargetRef('origin', |
| 1821 | 'refs/remotes/origin/refs/diff/test', |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1822 | None)) |
rmistry@google.com | c68112d | 2015-03-03 12:48:06 +0000 | [diff] [blame] | 1823 | self.assertEqual('refs/heads/chrome/m42', |
| 1824 | git_cl.GetTargetRef('origin', |
| 1825 | 'refs/remotes/origin/chrome/m42', |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1826 | None)) |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 1827 | |
| 1828 | # Check target refs for user-specified target branch. |
| 1829 | for branch in ('branch-heads/123', 'remotes/branch-heads/123', |
| 1830 | 'refs/remotes/branch-heads/123'): |
| 1831 | self.assertEqual('refs/branch-heads/123', |
| 1832 | git_cl.GetTargetRef('origin', |
| 1833 | 'refs/remotes/origin/master', |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1834 | branch)) |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 1835 | for branch in ('origin/master', 'remotes/origin/master', |
| 1836 | 'refs/remotes/origin/master'): |
| 1837 | self.assertEqual('refs/heads/master', |
| 1838 | git_cl.GetTargetRef('origin', |
| 1839 | 'refs/remotes/branch-heads/123', |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1840 | branch)) |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 1841 | for branch in ('master', 'heads/master', 'refs/heads/master'): |
| 1842 | self.assertEqual('refs/heads/master', |
| 1843 | git_cl.GetTargetRef('origin', |
| 1844 | 'refs/remotes/branch-heads/123', |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1845 | branch)) |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 1846 | |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 1847 | @mock.patch('git_common.is_dirty_git_tree', return_value=True) |
| 1848 | def test_patch_when_dirty(self, *_mocks): |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 1849 | # Patch when local tree is dirty. |
wychen@chromium.org | a872e75 | 2015-04-28 23:42:18 +0000 | [diff] [blame] | 1850 | self.assertNotEqual(git_cl.main(['patch', '123456']), 0) |
| 1851 | |
Edward Lemur | 8515328 | 2020-02-14 22:06:29 +0000 | [diff] [blame] | 1852 | def assertIssueAndPatchset( |
| 1853 | self, branch='master', issue='123456', patchset='7', |
| 1854 | git_short_host='chromium'): |
| 1855 | self.assertEqual( |
Edward Lemur | 2696407 | 2020-02-19 19:18:51 +0000 | [diff] [blame] | 1856 | issue, scm.GIT.GetBranchConfig('', branch, 'gerritissue')) |
Edward Lemur | 8515328 | 2020-02-14 22:06:29 +0000 | [diff] [blame] | 1857 | self.assertEqual( |
Edward Lemur | 2696407 | 2020-02-19 19:18:51 +0000 | [diff] [blame] | 1858 | patchset, scm.GIT.GetBranchConfig('', branch, 'gerritpatchset')) |
Edward Lemur | 8515328 | 2020-02-14 22:06:29 +0000 | [diff] [blame] | 1859 | self.assertEqual( |
| 1860 | 'https://%s-review.googlesource.com' % git_short_host, |
Edward Lemur | 2696407 | 2020-02-19 19:18:51 +0000 | [diff] [blame] | 1861 | scm.GIT.GetBranchConfig('', branch, 'gerritserver')) |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1862 | |
Edward Lemur | 8515328 | 2020-02-14 22:06:29 +0000 | [diff] [blame] | 1863 | def _patch_common(self, git_short_host='chromium'): |
Edward Lesmes | 50da770 | 2020-03-30 19:23:43 +0000 | [diff] [blame] | 1864 | mock.patch('scm.GIT.ResolveCommit', return_value='deadbeef').start() |
Edward Lemur | 2696407 | 2020-02-19 19:18:51 +0000 | [diff] [blame] | 1865 | self.mockGit.config['remote.origin.url'] = ( |
| 1866 | 'https://%s.googlesource.com/my/repo' % git_short_host) |
Edward Lesmes | 7677e5c | 2020-02-19 20:39:03 +0000 | [diff] [blame] | 1867 | gerrit_util.GetChangeDetail.return_value = { |
| 1868 | 'current_revision': '7777777777', |
| 1869 | 'revisions': { |
| 1870 | '1111111111': { |
| 1871 | '_number': 1, |
| 1872 | 'fetch': {'http': { |
| 1873 | 'url': 'https://%s.googlesource.com/my/repo' % git_short_host, |
| 1874 | 'ref': 'refs/changes/56/123456/1', |
| 1875 | }}, |
| 1876 | }, |
| 1877 | '7777777777': { |
| 1878 | '_number': 7, |
| 1879 | 'fetch': {'http': { |
| 1880 | 'url': 'https://%s.googlesource.com/my/repo' % git_short_host, |
| 1881 | 'ref': 'refs/changes/56/123456/7', |
| 1882 | }}, |
| 1883 | }, |
| 1884 | }, |
| 1885 | } |
wychen@chromium.org | a872e75 | 2015-04-28 23:42:18 +0000 | [diff] [blame] | 1886 | |
Andrii Shyshkalov | c971239 | 2017-04-11 13:35:21 +0200 | [diff] [blame] | 1887 | def test_patch_gerrit_default(self): |
Edward Lemur | 8515328 | 2020-02-14 22:06:29 +0000 | [diff] [blame] | 1888 | self._patch_common() |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1889 | self.calls += [ |
| 1890 | ((['git', 'fetch', 'https://chromium.googlesource.com/my/repo', |
| 1891 | 'refs/changes/56/123456/7'],), ''), |
Aaron Gable | 62619a3 | 2017-06-16 08:22:09 -0700 | [diff] [blame] | 1892 | ((['git', 'cherry-pick', 'FETCH_HEAD'],), ''), |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1893 | ] |
| 1894 | self.assertEqual(git_cl.main(['patch', '123456']), 0) |
Edward Lemur | 8515328 | 2020-02-14 22:06:29 +0000 | [diff] [blame] | 1895 | self.assertIssueAndPatchset() |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1896 | |
Andrii Shyshkalov | f57841b | 2018-08-28 00:48:53 +0000 | [diff] [blame] | 1897 | def test_patch_gerrit_new_branch(self): |
Edward Lemur | 8515328 | 2020-02-14 22:06:29 +0000 | [diff] [blame] | 1898 | self._patch_common() |
Andrii Shyshkalov | f57841b | 2018-08-28 00:48:53 +0000 | [diff] [blame] | 1899 | self.calls += [ |
| 1900 | ((['git', 'fetch', 'https://chromium.googlesource.com/my/repo', |
| 1901 | 'refs/changes/56/123456/7'],), ''), |
| 1902 | ((['git', 'cherry-pick', 'FETCH_HEAD'],), ''), |
Andrii Shyshkalov | f57841b | 2018-08-28 00:48:53 +0000 | [diff] [blame] | 1903 | ] |
Edward Lemur | 8515328 | 2020-02-14 22:06:29 +0000 | [diff] [blame] | 1904 | self.assertEqual(git_cl.main(['patch', '-b', 'feature', '123456']), 0) |
| 1905 | self.assertIssueAndPatchset(branch='feature') |
Andrii Shyshkalov | f57841b | 2018-08-28 00:48:53 +0000 | [diff] [blame] | 1906 | |
Andrii Shyshkalov | c971239 | 2017-04-11 13:35:21 +0200 | [diff] [blame] | 1907 | def test_patch_gerrit_force(self): |
Edward Lemur | 8515328 | 2020-02-14 22:06:29 +0000 | [diff] [blame] | 1908 | self._patch_common('host') |
tandrii@chromium.org | dde6462 | 2016-04-13 17:11:21 +0000 | [diff] [blame] | 1909 | self.calls += [ |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1910 | ((['git', 'fetch', 'https://host.googlesource.com/my/repo', |
tandrii@chromium.org | dde6462 | 2016-04-13 17:11:21 +0000 | [diff] [blame] | 1911 | 'refs/changes/56/123456/7'],), ''), |
Aaron Gable | 9387b4f | 2017-06-08 10:50:03 -0700 | [diff] [blame] | 1912 | ((['git', 'reset', '--hard', 'FETCH_HEAD'],), ''), |
tandrii@chromium.org | dde6462 | 2016-04-13 17:11:21 +0000 | [diff] [blame] | 1913 | ] |
Edward Lemur | 52969c9 | 2020-02-06 18:15:28 +0000 | [diff] [blame] | 1914 | self.assertEqual(git_cl.main(['patch', '123456', '--force']), 0) |
Edward Lemur | 8515328 | 2020-02-14 22:06:29 +0000 | [diff] [blame] | 1915 | self.assertIssueAndPatchset(git_short_host='host') |
tandrii@chromium.org | dde6462 | 2016-04-13 17:11:21 +0000 | [diff] [blame] | 1916 | |
Andrii Shyshkalov | c971239 | 2017-04-11 13:35:21 +0200 | [diff] [blame] | 1917 | def test_patch_gerrit_guess_by_url(self): |
Edward Lemur | 8515328 | 2020-02-14 22:06:29 +0000 | [diff] [blame] | 1918 | self._patch_common('else') |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1919 | self.calls += [ |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1920 | ((['git', 'fetch', 'https://else.googlesource.com/my/repo', |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1921 | 'refs/changes/56/123456/1'],), ''), |
Aaron Gable | 62619a3 | 2017-06-16 08:22:09 -0700 | [diff] [blame] | 1922 | ((['git', 'cherry-pick', 'FETCH_HEAD'],), ''), |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1923 | ] |
| 1924 | self.assertEqual(git_cl.main( |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1925 | ['patch', 'https://else-review.googlesource.com/#/c/123456/1']), 0) |
Edward Lemur | 8515328 | 2020-02-14 22:06:29 +0000 | [diff] [blame] | 1926 | self.assertIssueAndPatchset(patchset='1', git_short_host='else') |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1927 | |
Aaron Gable | 697a91b | 2018-01-19 15:20:15 -0800 | [diff] [blame] | 1928 | def test_patch_gerrit_guess_by_url_with_repo(self): |
Edward Lemur | 8515328 | 2020-02-14 22:06:29 +0000 | [diff] [blame] | 1929 | self._patch_common('else') |
Aaron Gable | 697a91b | 2018-01-19 15:20:15 -0800 | [diff] [blame] | 1930 | self.calls += [ |
| 1931 | ((['git', 'fetch', 'https://else.googlesource.com/my/repo', |
| 1932 | 'refs/changes/56/123456/1'],), ''), |
| 1933 | ((['git', 'cherry-pick', 'FETCH_HEAD'],), ''), |
Aaron Gable | 697a91b | 2018-01-19 15:20:15 -0800 | [diff] [blame] | 1934 | ] |
| 1935 | self.assertEqual(git_cl.main( |
| 1936 | ['patch', 'https://else-review.googlesource.com/c/my/repo/+/123456/1']), |
| 1937 | 0) |
Edward Lemur | 8515328 | 2020-02-14 22:06:29 +0000 | [diff] [blame] | 1938 | self.assertIssueAndPatchset(patchset='1', git_short_host='else') |
Aaron Gable | 697a91b | 2018-01-19 15:20:15 -0800 | [diff] [blame] | 1939 | |
Edward Lemur | d55c507 | 2020-02-20 01:09:07 +0000 | [diff] [blame] | 1940 | @mock.patch('sys.stderr', StringIO()) |
Andrii Shyshkalov | c971239 | 2017-04-11 13:35:21 +0200 | [diff] [blame] | 1941 | def test_patch_gerrit_conflict(self): |
Edward Lemur | 8515328 | 2020-02-14 22:06:29 +0000 | [diff] [blame] | 1942 | self._patch_common() |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1943 | self.calls += [ |
| 1944 | ((['git', 'fetch', 'https://chromium.googlesource.com/my/repo', |
Andrii Shyshkalov | c971239 | 2017-04-11 13:35:21 +0200 | [diff] [blame] | 1945 | 'refs/changes/56/123456/7'],), ''), |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 1946 | ((['git', 'cherry-pick', 'FETCH_HEAD'],), CERR1), |
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']) |
Edward Lemur | d55c507 | 2020-02-20 01:09:07 +0000 | [diff] [blame] | 1950 | self.assertEqual( |
| 1951 | 'Command "git cherry-pick FETCH_HEAD" failed.\n\n', |
| 1952 | sys.stderr.getvalue()) |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1953 | |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 1954 | @mock.patch( |
Edward Lesmes | 7677e5c | 2020-02-19 20:39:03 +0000 | [diff] [blame] | 1955 | 'gerrit_util.GetChangeDetail', |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 1956 | side_effect=gerrit_util.GerritError(404, '')) |
Edward Lemur | d55c507 | 2020-02-20 01:09:07 +0000 | [diff] [blame] | 1957 | @mock.patch('sys.stderr', StringIO()) |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 1958 | def test_patch_gerrit_not_exists(self, *_mocks): |
Edward Lemur | 2696407 | 2020-02-19 19:18:51 +0000 | [diff] [blame] | 1959 | self.mockGit.config['remote.origin.url'] = ( |
| 1960 | 'https://chromium.googlesource.com/my/repo') |
tandrii | c2405f5 | 2016-10-10 08:13:15 -0700 | [diff] [blame] | 1961 | with self.assertRaises(SystemExitMock): |
Andrii Shyshkalov | c971239 | 2017-04-11 13:35:21 +0200 | [diff] [blame] | 1962 | self.assertEqual(1, git_cl.main(['patch', '123456'])) |
Edward Lemur | d55c507 | 2020-02-20 01:09:07 +0000 | [diff] [blame] | 1963 | self.assertEqual( |
| 1964 | 'change 123456 at https://chromium-review.googlesource.com does not ' |
| 1965 | 'exist or you have no access to it\n', |
| 1966 | sys.stderr.getvalue()) |
tandrii | c2405f5 | 2016-10-10 08:13:15 -0700 | [diff] [blame] | 1967 | |
tandrii@chromium.org | 5df290f | 2016-04-11 16:12:29 +0000 | [diff] [blame] | 1968 | def _checkout_calls(self): |
| 1969 | return [ |
| 1970 | ((['git', 'config', '--local', '--get-regexp', |
tandrii@chromium.org | 5df290f | 2016-04-11 16:12:29 +0000 | [diff] [blame] | 1971 | 'branch\\..*\\.gerritissue'], ), |
| 1972 | ('branch.ger-branch.gerritissue 123456\n' |
| 1973 | 'branch.gbranch654.gerritissue 654321\n')), |
| 1974 | ] |
| 1975 | |
| 1976 | def test_checkout_gerrit(self): |
| 1977 | """Tests git cl checkout <issue>.""" |
| 1978 | self.calls = self._checkout_calls() |
| 1979 | self.calls += [((['git', 'checkout', 'ger-branch'], ), '')] |
| 1980 | self.assertEqual(0, git_cl.main(['checkout', '123456'])) |
| 1981 | |
tandrii@chromium.org | 5df290f | 2016-04-11 16:12:29 +0000 | [diff] [blame] | 1982 | def test_checkout_not_found(self): |
| 1983 | """Tests git cl checkout <issue>.""" |
| 1984 | self.calls = self._checkout_calls() |
| 1985 | self.assertEqual(1, git_cl.main(['checkout', '99999'])) |
| 1986 | |
tandrii@chromium.org | 26c8fd2 | 2016-04-11 21:33:21 +0000 | [diff] [blame] | 1987 | def test_checkout_no_branch_issues(self): |
| 1988 | """Tests git cl checkout <issue>.""" |
| 1989 | self.calls = [ |
| 1990 | ((['git', 'config', '--local', '--get-regexp', |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 1991 | 'branch\\..*\\.gerritissue'], ), CERR1), |
tandrii@chromium.org | 26c8fd2 | 2016-04-11 21:33:21 +0000 | [diff] [blame] | 1992 | ] |
| 1993 | self.assertEqual(1, git_cl.main(['checkout', '99999'])) |
| 1994 | |
Edward Lemur | 2696407 | 2020-02-19 19:18:51 +0000 | [diff] [blame] | 1995 | def _test_gerrit_ensure_authenticated_common(self, auth): |
Edward Lemur | 1a83da1 | 2020-03-04 21:18:36 +0000 | [diff] [blame] | 1996 | mock.patch( |
Edward Lesmes | ae3586b | 2020-03-23 21:21:14 +0000 | [diff] [blame] | 1997 | 'gclient_utils.AskForData', |
Edward Lemur | 1a83da1 | 2020-03-04 21:18:36 +0000 | [diff] [blame] | 1998 | lambda prompt: self._mocked_call('ask_for_data', prompt)).start() |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 1999 | mock.patch('git_cl.gerrit_util.CookiesAuthenticator', |
| 2000 | CookiesAuthenticatorMockFactory(hosts_with_creds=auth)).start() |
Edward Lemur | 2696407 | 2020-02-19 19:18:51 +0000 | [diff] [blame] | 2001 | self.mockGit.config['remote.origin.url'] = ( |
| 2002 | 'https://chromium.googlesource.com/my/repo') |
Edward Lemur | f38bc17 | 2019-09-03 21:02:13 +0000 | [diff] [blame] | 2003 | cl = git_cl.Changelist() |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 2004 | cl.branch = 'master' |
| 2005 | cl.branchref = 'refs/heads/master' |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 2006 | return cl |
| 2007 | |
Edward Lemur | d55c507 | 2020-02-20 01:09:07 +0000 | [diff] [blame] | 2008 | @mock.patch('sys.stderr', StringIO()) |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 2009 | def test_gerrit_ensure_authenticated_missing(self): |
| 2010 | cl = self._test_gerrit_ensure_authenticated_common(auth={ |
Andrii Shyshkalov | bb86fbb | 2017-03-24 14:59:28 +0100 | [diff] [blame] | 2011 | 'chromium.googlesource.com': ('git-is.ok', '', 'but gerrit is missing'), |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 2012 | }) |
Edward Lemur | d55c507 | 2020-02-20 01:09:07 +0000 | [diff] [blame] | 2013 | with self.assertRaises(SystemExitMock): |
| 2014 | cl.EnsureAuthenticated(force=False) |
| 2015 | self.assertEqual( |
| 2016 | 'Credentials for the following hosts are required:\n' |
| 2017 | ' chromium-review.googlesource.com\n' |
Josip Sokcevic | 464e9ff | 2020-03-18 23:48:55 +0000 | [diff] [blame] | 2018 | 'These are read from ~%(sep)s.gitcookies ' |
| 2019 | '(or legacy ~%(sep)s%(netrc)s)\n' |
Edward Lemur | d55c507 | 2020-02-20 01:09:07 +0000 | [diff] [blame] | 2020 | 'You can (re)generate your credentials by visiting ' |
Josip Sokcevic | 464e9ff | 2020-03-18 23:48:55 +0000 | [diff] [blame] | 2021 | 'https://chromium-review.googlesource.com/new-password\n' % { |
| 2022 | 'sep': os.sep, |
| 2023 | 'netrc': NETRC_FILENAME, |
| 2024 | }, sys.stderr.getvalue()) |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 2025 | |
| 2026 | def test_gerrit_ensure_authenticated_conflict(self): |
| 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): |
Edward Lemur | 2696407 | 2020-02-19 19:18:51 +0000 | [diff] [blame] | 2048 | self.mockGit.config['gerrit.skip-ensure-authenticated'] = 'true' |
| 2049 | cl = self._test_gerrit_ensure_authenticated_common(auth={}) |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 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): |
Edward Lemur | 2696407 | 2020-02-19 19:18:51 +0000 | [diff] [blame] | 2065 | self.mockGit.config['remote.origin.url'] = 'custom-scheme://repo' |
Daniel Cheng | cf6269b | 2019-05-18 01:02:12 +0000 | [diff] [blame] | 2066 | self.calls = [ |
Josip | 906bfde | 2020-01-31 22:38:49 +0000 | [diff] [blame] | 2067 | (('logging.warning', |
| 2068 | 'Ignoring branch %(branch)s with non-https remote ' |
| 2069 | '%(remote)s', { |
| 2070 | 'branch': 'master', |
| 2071 | 'remote': 'custom-scheme://repo'} |
| 2072 | ), None), |
Daniel Cheng | cf6269b | 2019-05-18 01:02:12 +0000 | [diff] [blame] | 2073 | ] |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 2074 | mock.patch('git_cl.gerrit_util.CookiesAuthenticator', |
| 2075 | CookiesAuthenticatorMockFactory(hosts_with_creds={})).start() |
| 2076 | mock.patch('logging.warning', |
| 2077 | lambda *a: self._mocked_call('logging.warning', *a)).start() |
Edward Lemur | f38bc17 | 2019-09-03 21:02:13 +0000 | [diff] [blame] | 2078 | cl = git_cl.Changelist() |
Daniel Cheng | cf6269b | 2019-05-18 01:02:12 +0000 | [diff] [blame] | 2079 | cl.branch = 'master' |
| 2080 | cl.branchref = 'refs/heads/master' |
| 2081 | cl.lookedup_issue = True |
| 2082 | self.assertIsNone(cl.EnsureAuthenticated(force=False)) |
| 2083 | |
Florian Mayer | ae510e8 | 2020-01-30 21:04:48 +0000 | [diff] [blame] | 2084 | def test_gerrit_ensure_authenticated_non_url(self): |
Edward Lemur | 2696407 | 2020-02-19 19:18:51 +0000 | [diff] [blame] | 2085 | self.mockGit.config['remote.origin.url'] = ( |
| 2086 | 'git@somehost.example:foo/bar.git') |
Florian Mayer | ae510e8 | 2020-01-30 21:04:48 +0000 | [diff] [blame] | 2087 | self.calls = [ |
Josip | 906bfde | 2020-01-31 22:38:49 +0000 | [diff] [blame] | 2088 | (('logging.error', |
| 2089 | 'Remote "%(remote)s" for branch "%(branch)s" points to "%(url)s", ' |
| 2090 | 'but it doesn\'t exist.', { |
| 2091 | 'remote': 'origin', |
| 2092 | 'branch': 'master', |
| 2093 | 'url': 'git@somehost.example:foo/bar.git'} |
| 2094 | ), None), |
Florian Mayer | ae510e8 | 2020-01-30 21:04:48 +0000 | [diff] [blame] | 2095 | ] |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 2096 | mock.patch('git_cl.gerrit_util.CookiesAuthenticator', |
| 2097 | CookiesAuthenticatorMockFactory(hosts_with_creds={})).start() |
| 2098 | mock.patch('logging.error', |
| 2099 | lambda *a: self._mocked_call('logging.error', *a)).start() |
Florian Mayer | ae510e8 | 2020-01-30 21:04:48 +0000 | [diff] [blame] | 2100 | cl = git_cl.Changelist() |
| 2101 | cl.branch = 'master' |
| 2102 | cl.branchref = 'refs/heads/master' |
| 2103 | cl.lookedup_issue = True |
| 2104 | self.assertIsNone(cl.EnsureAuthenticated(force=False)) |
| 2105 | |
Andrii Shyshkalov | 828701b | 2016-12-09 10:46:47 +0100 | [diff] [blame] | 2106 | def _cmd_set_commit_gerrit_common(self, vote, notify=None): |
Edward Lemur | 8515328 | 2020-02-14 22:06:29 +0000 | [diff] [blame] | 2107 | self.mockGit.config['branch.master.gerritissue'] = '123' |
| 2108 | self.mockGit.config['branch.master.gerritserver'] = ( |
| 2109 | 'https://chromium-review.googlesource.com') |
Edward Lemur | 2696407 | 2020-02-19 19:18:51 +0000 | [diff] [blame] | 2110 | self.mockGit.config['remote.origin.url'] = ( |
| 2111 | 'https://chromium.googlesource.com/infra/infra') |
tandrii@chromium.org | fa330e8 | 2016-04-13 17:09:52 +0000 | [diff] [blame] | 2112 | self.calls = [ |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 2113 | (('SetReview', 'chromium-review.googlesource.com', |
| 2114 | 'infra%2Finfra~123', None, |
| 2115 | {'Commit-Queue': vote}, notify, None), ''), |
tandrii@chromium.org | fa330e8 | 2016-04-13 17:09:52 +0000 | [diff] [blame] | 2116 | ] |
tandrii | d9e5ce5 | 2016-07-13 02:32:59 -0700 | [diff] [blame] | 2117 | |
| 2118 | def test_cmd_set_commit_gerrit_clear(self): |
| 2119 | self._cmd_set_commit_gerrit_common(0) |
| 2120 | self.assertEqual(0, git_cl.main(['set-commit', '-c'])) |
| 2121 | |
| 2122 | def test_cmd_set_commit_gerrit_dry(self): |
Aaron Gable | 75e7872 | 2017-06-09 10:40:16 -0700 | [diff] [blame] | 2123 | self._cmd_set_commit_gerrit_common(1, notify=False) |
tandrii@chromium.org | fa330e8 | 2016-04-13 17:09:52 +0000 | [diff] [blame] | 2124 | self.assertEqual(0, git_cl.main(['set-commit', '-d'])) |
| 2125 | |
tandrii | d9e5ce5 | 2016-07-13 02:32:59 -0700 | [diff] [blame] | 2126 | def test_cmd_set_commit_gerrit(self): |
| 2127 | self._cmd_set_commit_gerrit_common(2) |
| 2128 | self.assertEqual(0, git_cl.main(['set-commit'])) |
| 2129 | |
martiniss@chromium.org | 2b55fe3 | 2016-04-26 20:28:54 +0000 | [diff] [blame] | 2130 | def test_description_display(self): |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 2131 | mock.patch('git_cl.Changelist', ChangelistMock).start() |
martiniss@chromium.org | d6648e2 | 2016-04-29 19:22:16 +0000 | [diff] [blame] | 2132 | ChangelistMock.desc = 'foo\n' |
martiniss@chromium.org | 2b55fe3 | 2016-04-26 20:28:54 +0000 | [diff] [blame] | 2133 | |
| 2134 | self.assertEqual(0, git_cl.main(['description', '-d'])) |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 2135 | self.assertEqual('foo\n', sys.stdout.getvalue()) |
martiniss@chromium.org | 2b55fe3 | 2016-04-26 20:28:54 +0000 | [diff] [blame] | 2136 | |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 2137 | @mock.patch('sys.stderr', StringIO()) |
iannucci | 3c972b9 | 2016-08-17 13:24:10 -0700 | [diff] [blame] | 2138 | def test_StatusFieldOverrideIssueMissingArgs(self): |
iannucci | 3c972b9 | 2016-08-17 13:24:10 -0700 | [diff] [blame] | 2139 | try: |
| 2140 | self.assertEqual(git_cl.main(['status', '--issue', '1']), 0) |
Edward Lemur | d55c507 | 2020-02-20 01:09:07 +0000 | [diff] [blame] | 2141 | except SystemExitMock: |
Edward Lemur | 6c6827c | 2020-02-06 21:15:18 +0000 | [diff] [blame] | 2142 | self.assertIn( |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 2143 | '--field must be given when --issue is set.', sys.stderr.getvalue()) |
iannucci | 3c972b9 | 2016-08-17 13:24:10 -0700 | [diff] [blame] | 2144 | |
| 2145 | def test_StatusFieldOverrideIssue(self): |
iannucci | 3c972b9 | 2016-08-17 13:24:10 -0700 | [diff] [blame] | 2146 | def assertIssue(cl_self, *_args): |
Raphael Kubo da Costa | e9342a7 | 2019-10-14 17:49:39 +0000 | [diff] [blame] | 2147 | self.assertEqual(cl_self.issue, 1) |
iannucci | 3c972b9 | 2016-08-17 13:24:10 -0700 | [diff] [blame] | 2148 | return 'foobar' |
| 2149 | |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 2150 | mock.patch('git_cl.Changelist.FetchDescription', assertIssue).start() |
iannucci | e53c935 | 2016-08-17 14:40:40 -0700 | [diff] [blame] | 2151 | self.assertEqual( |
Edward Lemur | 52969c9 | 2020-02-06 18:15:28 +0000 | [diff] [blame] | 2152 | git_cl.main(['status', '--issue', '1', '--field', 'desc']), |
iannucci | e53c935 | 2016-08-17 14:40:40 -0700 | [diff] [blame] | 2153 | 0) |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 2154 | self.assertEqual(sys.stdout.getvalue(), 'foobar\n') |
iannucci | 3c972b9 | 2016-08-17 13:24:10 -0700 | [diff] [blame] | 2155 | |
iannucci | e53c935 | 2016-08-17 14:40:40 -0700 | [diff] [blame] | 2156 | def test_SetCloseOverrideIssue(self): |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 2157 | |
iannucci | e53c935 | 2016-08-17 14:40:40 -0700 | [diff] [blame] | 2158 | def assertIssue(cl_self, *_args): |
Raphael Kubo da Costa | e9342a7 | 2019-10-14 17:49:39 +0000 | [diff] [blame] | 2159 | self.assertEqual(cl_self.issue, 1) |
iannucci | e53c935 | 2016-08-17 14:40:40 -0700 | [diff] [blame] | 2160 | return 'foobar' |
| 2161 | |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 2162 | mock.patch('git_cl.Changelist.FetchDescription', assertIssue).start() |
| 2163 | mock.patch('git_cl.Changelist.CloseIssue', lambda *_: None).start() |
iannucci | e53c935 | 2016-08-17 14:40:40 -0700 | [diff] [blame] | 2164 | self.assertEqual( |
Edward Lemur | 52969c9 | 2020-02-06 18:15:28 +0000 | [diff] [blame] | 2165 | git_cl.main(['set-close', '--issue', '1']), 0) |
iannucci | e53c935 | 2016-08-17 14:40:40 -0700 | [diff] [blame] | 2166 | |
Andrii Shyshkalov | dd672fb | 2018-10-16 06:09:51 +0000 | [diff] [blame] | 2167 | def test_description(self): |
Edward Lemur | 2696407 | 2020-02-19 19:18:51 +0000 | [diff] [blame] | 2168 | self.mockGit.config['remote.origin.url'] = ( |
| 2169 | 'https://chromium.googlesource.com/my/repo') |
Edward Lesmes | 7677e5c | 2020-02-19 20:39:03 +0000 | [diff] [blame] | 2170 | gerrit_util.GetChangeDetail.return_value = { |
| 2171 | 'current_revision': 'sha1', |
| 2172 | 'revisions': {'sha1': { |
| 2173 | 'commit': {'message': 'foobar'}, |
| 2174 | }}, |
| 2175 | } |
martiniss@chromium.org | 2b55fe3 | 2016-04-26 20:28:54 +0000 | [diff] [blame] | 2176 | self.assertEqual(0, git_cl.main([ |
Andrii Shyshkalov | dd672fb | 2018-10-16 06:09:51 +0000 | [diff] [blame] | 2177 | 'description', |
| 2178 | 'https://chromium-review.googlesource.com/c/my/repo/+/123123', |
| 2179 | '-d'])) |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 2180 | self.assertEqual('foobar\n', sys.stdout.getvalue()) |
martiniss@chromium.org | 2b55fe3 | 2016-04-26 20:28:54 +0000 | [diff] [blame] | 2181 | |
martiniss@chromium.org | d6648e2 | 2016-04-29 19:22:16 +0000 | [diff] [blame] | 2182 | def test_description_set_raw(self): |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 2183 | mock.patch('git_cl.Changelist', ChangelistMock).start() |
| 2184 | mock.patch('git_cl.sys.stdin', StringIO('hihi')).start() |
martiniss@chromium.org | d6648e2 | 2016-04-29 19:22:16 +0000 | [diff] [blame] | 2185 | |
| 2186 | self.assertEqual(0, git_cl.main(['description', '-n', 'hihi'])) |
| 2187 | self.assertEqual('hihi', ChangelistMock.desc) |
| 2188 | |
tandrii@chromium.org | d605a51 | 2016-06-03 09:55:00 +0000 | [diff] [blame] | 2189 | def test_description_appends_bug_line(self): |
tandrii@chromium.org | 601e1d1 | 2016-06-03 13:03:54 +0000 | [diff] [blame] | 2190 | current_desc = 'Some.\n\nChange-Id: xxx' |
tandrii@chromium.org | d605a51 | 2016-06-03 09:55:00 +0000 | [diff] [blame] | 2191 | |
| 2192 | def RunEditor(desc, _, **kwargs): |
Raphael Kubo da Costa | e9342a7 | 2019-10-14 17:49:39 +0000 | [diff] [blame] | 2193 | self.assertEqual( |
tandrii@chromium.org | d605a51 | 2016-06-03 09:55:00 +0000 | [diff] [blame] | 2194 | '# Enter a description of the change.\n' |
| 2195 | '# This will be displayed on the codereview site.\n' |
| 2196 | '# The first line will also be used as the subject of the review.\n' |
| 2197 | '#--------------------This line is 72 characters long' |
tandrii@chromium.org | 601e1d1 | 2016-06-03 13:03:54 +0000 | [diff] [blame] | 2198 | '--------------------\n' |
Aaron Gable | 3a16ed1 | 2017-03-23 10:51:55 -0700 | [diff] [blame] | 2199 | 'Some.\n\nChange-Id: xxx\nBug: ', |
tandrii@chromium.org | d605a51 | 2016-06-03 09:55:00 +0000 | [diff] [blame] | 2200 | desc) |
tandrii@chromium.org | 601e1d1 | 2016-06-03 13:03:54 +0000 | [diff] [blame] | 2201 | # Simulate user changing something. |
Aaron Gable | 3a16ed1 | 2017-03-23 10:51:55 -0700 | [diff] [blame] | 2202 | return 'Some.\n\nChange-Id: xxx\nBug: 123' |
tandrii@chromium.org | d605a51 | 2016-06-03 09:55:00 +0000 | [diff] [blame] | 2203 | |
Edward Lemur | 6c6827c | 2020-02-06 21:15:18 +0000 | [diff] [blame] | 2204 | def UpdateDescription(_, desc, force=False): |
Raphael Kubo da Costa | e9342a7 | 2019-10-14 17:49:39 +0000 | [diff] [blame] | 2205 | self.assertEqual(desc, 'Some.\n\nChange-Id: xxx\nBug: 123') |
tandrii@chromium.org | d605a51 | 2016-06-03 09:55:00 +0000 | [diff] [blame] | 2206 | |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 2207 | mock.patch('git_cl.Changelist.FetchDescription', |
| 2208 | lambda *args: current_desc).start() |
| 2209 | mock.patch('git_cl.Changelist.UpdateDescription', |
| 2210 | UpdateDescription).start() |
| 2211 | mock.patch('git_cl.gclient_utils.RunEditor', RunEditor).start() |
tandrii@chromium.org | d605a51 | 2016-06-03 09:55:00 +0000 | [diff] [blame] | 2212 | |
Edward Lemur | 8515328 | 2020-02-14 22:06:29 +0000 | [diff] [blame] | 2213 | self.mockGit.config['branch.master.gerritissue'] = '123' |
Edward Lemur | 52969c9 | 2020-02-06 18:15:28 +0000 | [diff] [blame] | 2214 | self.assertEqual(0, git_cl.main(['description'])) |
tandrii@chromium.org | d605a51 | 2016-06-03 09:55:00 +0000 | [diff] [blame] | 2215 | |
Dan Beam | d8b04ca | 2019-10-10 21:23:26 +0000 | [diff] [blame] | 2216 | def test_description_does_not_append_bug_line_if_fixed_is_present(self): |
| 2217 | current_desc = 'Some.\n\nFixed: 123\nChange-Id: xxx' |
| 2218 | |
| 2219 | def RunEditor(desc, _, **kwargs): |
Raphael Kubo da Costa | e9342a7 | 2019-10-14 17:49:39 +0000 | [diff] [blame] | 2220 | self.assertEqual( |
Dan Beam | d8b04ca | 2019-10-10 21:23:26 +0000 | [diff] [blame] | 2221 | '# Enter a description of the change.\n' |
| 2222 | '# This will be displayed on the codereview site.\n' |
| 2223 | '# The first line will also be used as the subject of the review.\n' |
| 2224 | '#--------------------This line is 72 characters long' |
| 2225 | '--------------------\n' |
| 2226 | 'Some.\n\nFixed: 123\nChange-Id: xxx', |
| 2227 | desc) |
| 2228 | return desc |
| 2229 | |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 2230 | mock.patch('git_cl.Changelist.FetchDescription', |
| 2231 | lambda *args: current_desc).start() |
| 2232 | mock.patch('git_cl.gclient_utils.RunEditor', RunEditor).start() |
Dan Beam | d8b04ca | 2019-10-10 21:23:26 +0000 | [diff] [blame] | 2233 | |
Edward Lemur | 8515328 | 2020-02-14 22:06:29 +0000 | [diff] [blame] | 2234 | self.mockGit.config['branch.master.gerritissue'] = '123' |
Edward Lemur | 52969c9 | 2020-02-06 18:15:28 +0000 | [diff] [blame] | 2235 | self.assertEqual(0, git_cl.main(['description'])) |
Dan Beam | d8b04ca | 2019-10-10 21:23:26 +0000 | [diff] [blame] | 2236 | |
martiniss@chromium.org | d6648e2 | 2016-04-29 19:22:16 +0000 | [diff] [blame] | 2237 | def test_description_set_stdin(self): |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 2238 | mock.patch('git_cl.Changelist', ChangelistMock).start() |
| 2239 | mock.patch('git_cl.sys.stdin', StringIO('hi \r\n\t there\n\nman')).start() |
martiniss@chromium.org | d6648e2 | 2016-04-29 19:22:16 +0000 | [diff] [blame] | 2240 | |
| 2241 | self.assertEqual(0, git_cl.main(['description', '-n', '-'])) |
| 2242 | self.assertEqual('hi\n\t there\n\nman', ChangelistMock.desc) |
| 2243 | |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 2244 | def test_archive(self): |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 2245 | self.calls = [ |
| 2246 | ((['git', 'for-each-ref', '--format=%(refname)', 'refs/heads'],), |
Edward Lemur | f38bc17 | 2019-09-03 21:02:13 +0000 | [diff] [blame] | 2247 | 'refs/heads/master\nrefs/heads/foo\nrefs/heads/bar'), |
Kevin Marshall | 0e60ecd | 2019-12-04 17:44:13 +0000 | [diff] [blame] | 2248 | ((['git', 'for-each-ref', '--format=%(refname)', 'refs/tags'],), ''), |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 2249 | ((['git', 'tag', 'git-cl-archived-456-foo', 'foo'],), ''), |
Edward Lemur | f38bc17 | 2019-09-03 21:02:13 +0000 | [diff] [blame] | 2250 | ((['git', 'branch', '-D', 'foo'],), '') |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 2251 | ] |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 2252 | |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 2253 | mock.patch('git_cl.get_cl_statuses', |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 2254 | lambda branches, fine_grained, max_processes: |
kmarshall | 9249e01 | 2016-08-23 12:02:16 -0700 | [diff] [blame] | 2255 | [(MockChangelistWithBranchAndIssue('master', 1), 'open'), |
| 2256 | (MockChangelistWithBranchAndIssue('foo', 456), 'closed'), |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 2257 | (MockChangelistWithBranchAndIssue('bar', 789), 'open')]).start() |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 2258 | |
| 2259 | self.assertEqual(0, git_cl.main(['archive', '-f'])) |
| 2260 | |
Kevin Marshall | 0e60ecd | 2019-12-04 17:44:13 +0000 | [diff] [blame] | 2261 | def test_archive_tag_collision(self): |
Kevin Marshall | 0e60ecd | 2019-12-04 17:44:13 +0000 | [diff] [blame] | 2262 | self.calls = [ |
| 2263 | ((['git', 'for-each-ref', '--format=%(refname)', 'refs/heads'],), |
| 2264 | 'refs/heads/master\nrefs/heads/foo\nrefs/heads/bar'), |
| 2265 | ((['git', 'for-each-ref', '--format=%(refname)', 'refs/tags'],), |
| 2266 | 'refs/tags/git-cl-archived-456-foo'), |
Kevin Marshall | 0e60ecd | 2019-12-04 17:44:13 +0000 | [diff] [blame] | 2267 | ((['git', 'tag', 'git-cl-archived-456-foo-2', 'foo'],), ''), |
| 2268 | ((['git', 'branch', '-D', 'foo'],), '') |
| 2269 | ] |
| 2270 | |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 2271 | mock.patch('git_cl.get_cl_statuses', |
Kevin Marshall | 0e60ecd | 2019-12-04 17:44:13 +0000 | [diff] [blame] | 2272 | lambda branches, fine_grained, max_processes: |
| 2273 | [(MockChangelistWithBranchAndIssue('master', 1), 'open'), |
| 2274 | (MockChangelistWithBranchAndIssue('foo', 456), 'closed'), |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 2275 | (MockChangelistWithBranchAndIssue('bar', 789), 'open')]).start() |
Kevin Marshall | 0e60ecd | 2019-12-04 17:44:13 +0000 | [diff] [blame] | 2276 | |
| 2277 | self.assertEqual(0, git_cl.main(['archive', '-f'])) |
| 2278 | |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 2279 | def test_archive_current_branch_fails(self): |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 2280 | self.calls = [ |
| 2281 | ((['git', 'for-each-ref', '--format=%(refname)', 'refs/heads'],), |
| 2282 | 'refs/heads/master'), |
Kevin Marshall | 0e60ecd | 2019-12-04 17:44:13 +0000 | [diff] [blame] | 2283 | ((['git', 'for-each-ref', '--format=%(refname)', 'refs/tags'],), ''), |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 2284 | ] |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 2285 | |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 2286 | mock.patch('git_cl.get_cl_statuses', |
kmarshall | 9249e01 | 2016-08-23 12:02:16 -0700 | [diff] [blame] | 2287 | lambda branches, fine_grained, max_processes: |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 2288 | [(MockChangelistWithBranchAndIssue('master', 1), |
| 2289 | 'closed')]).start() |
kmarshall | 9249e01 | 2016-08-23 12:02:16 -0700 | [diff] [blame] | 2290 | |
| 2291 | self.assertEqual(1, git_cl.main(['archive', '-f'])) |
| 2292 | |
| 2293 | def test_archive_dry_run(self): |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 2294 | self.calls = [ |
| 2295 | ((['git', 'for-each-ref', '--format=%(refname)', 'refs/heads'],), |
| 2296 | 'refs/heads/master\nrefs/heads/foo\nrefs/heads/bar'), |
Kevin Marshall | 0e60ecd | 2019-12-04 17:44:13 +0000 | [diff] [blame] | 2297 | ((['git', 'for-each-ref', '--format=%(refname)', 'refs/tags'],), ''), |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 2298 | ] |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 2299 | |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 2300 | mock.patch('git_cl.get_cl_statuses', |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 2301 | lambda branches, fine_grained, max_processes: |
kmarshall | 9249e01 | 2016-08-23 12:02:16 -0700 | [diff] [blame] | 2302 | [(MockChangelistWithBranchAndIssue('master', 1), 'open'), |
| 2303 | (MockChangelistWithBranchAndIssue('foo', 456), 'closed'), |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 2304 | (MockChangelistWithBranchAndIssue('bar', 789), 'open')]).start() |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 2305 | |
kmarshall | 9249e01 | 2016-08-23 12:02:16 -0700 | [diff] [blame] | 2306 | self.assertEqual(0, git_cl.main(['archive', '-f', '--dry-run'])) |
| 2307 | |
| 2308 | def test_archive_no_tags(self): |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 2309 | self.calls = [ |
| 2310 | ((['git', 'for-each-ref', '--format=%(refname)', 'refs/heads'],), |
| 2311 | 'refs/heads/master\nrefs/heads/foo\nrefs/heads/bar'), |
Kevin Marshall | 0e60ecd | 2019-12-04 17:44:13 +0000 | [diff] [blame] | 2312 | ((['git', 'for-each-ref', '--format=%(refname)', 'refs/tags'],), ''), |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 2313 | ((['git', 'branch', '-D', 'foo'],), '') |
| 2314 | ] |
kmarshall | 9249e01 | 2016-08-23 12:02:16 -0700 | [diff] [blame] | 2315 | |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 2316 | mock.patch('git_cl.get_cl_statuses', |
kmarshall | 9249e01 | 2016-08-23 12:02:16 -0700 | [diff] [blame] | 2317 | lambda branches, fine_grained, max_processes: |
| 2318 | [(MockChangelistWithBranchAndIssue('master', 1), 'open'), |
| 2319 | (MockChangelistWithBranchAndIssue('foo', 456), 'closed'), |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 2320 | (MockChangelistWithBranchAndIssue('bar', 789), 'open')]).start() |
kmarshall | 9249e01 | 2016-08-23 12:02:16 -0700 | [diff] [blame] | 2321 | |
| 2322 | self.assertEqual(0, git_cl.main(['archive', '-f', '--notags'])) |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 2323 | |
Kevin Marshall | 0e60ecd | 2019-12-04 17:44:13 +0000 | [diff] [blame] | 2324 | def test_archive_tag_cleanup_on_branch_deletion_error(self): |
Kevin Marshall | 0e60ecd | 2019-12-04 17:44:13 +0000 | [diff] [blame] | 2325 | self.calls = [ |
| 2326 | ((['git', 'for-each-ref', '--format=%(refname)', 'refs/heads'],), |
| 2327 | 'refs/heads/master\nrefs/heads/foo\nrefs/heads/bar'), |
| 2328 | ((['git', 'for-each-ref', '--format=%(refname)', 'refs/tags'],), ''), |
Kevin Marshall | 0e60ecd | 2019-12-04 17:44:13 +0000 | [diff] [blame] | 2329 | ((['git', 'tag', 'git-cl-archived-456-foo', 'foo'],), |
| 2330 | 'refs/tags/git-cl-archived-456-foo'), |
| 2331 | ((['git', 'branch', '-D', 'foo'],), CERR1), |
| 2332 | ((['git', 'tag', '-d', 'git-cl-archived-456-foo'],), |
| 2333 | 'refs/tags/git-cl-archived-456-foo'), |
| 2334 | ] |
| 2335 | |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 2336 | mock.patch('git_cl.get_cl_statuses', |
Kevin Marshall | 0e60ecd | 2019-12-04 17:44:13 +0000 | [diff] [blame] | 2337 | lambda branches, fine_grained, max_processes: |
| 2338 | [(MockChangelistWithBranchAndIssue('master', 1), 'open'), |
| 2339 | (MockChangelistWithBranchAndIssue('foo', 456), 'closed'), |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 2340 | (MockChangelistWithBranchAndIssue('bar', 789), 'open')]).start() |
Kevin Marshall | 0e60ecd | 2019-12-04 17:44:13 +0000 | [diff] [blame] | 2341 | |
| 2342 | self.assertEqual(0, git_cl.main(['archive', '-f'])) |
| 2343 | |
Tibor Goldschwendt | 7c5efb2 | 2020-03-25 01:23:54 +0000 | [diff] [blame] | 2344 | def test_archive_with_format(self): |
| 2345 | self.calls = [ |
| 2346 | ((['git', 'for-each-ref', '--format=%(refname)', 'refs/heads'], ), |
| 2347 | 'refs/heads/master\nrefs/heads/foo\nrefs/heads/bar'), |
| 2348 | ((['git', 'for-each-ref', '--format=%(refname)', 'refs/tags'], ), ''), |
| 2349 | ((['git', 'tag', 'archived/12-foo', 'foo'], ), ''), |
| 2350 | ((['git', 'branch', '-D', 'foo'], ), ''), |
| 2351 | ] |
| 2352 | |
| 2353 | mock.patch('git_cl.get_cl_statuses', |
| 2354 | lambda branches, fine_grained, max_processes: |
| 2355 | [(MockChangelistWithBranchAndIssue('foo', 12), 'closed')]).start() |
| 2356 | |
| 2357 | self.assertEqual( |
| 2358 | 0, git_cl.main(['archive', '-f', '-p', 'archived/{issue}-{branch}'])) |
| 2359 | |
tandrii@chromium.org | 9b7fd71 | 2016-06-01 13:45:20 +0000 | [diff] [blame] | 2360 | def test_cmd_issue_erase_existing(self): |
Edward Lemur | 8515328 | 2020-02-14 22:06:29 +0000 | [diff] [blame] | 2361 | self.mockGit.config['branch.master.gerritissue'] = '123' |
| 2362 | self.mockGit.config['branch.master.gerritserver'] = ( |
| 2363 | 'https://chromium-review.googlesource.com') |
tandrii@chromium.org | 9b7fd71 | 2016-06-01 13:45:20 +0000 | [diff] [blame] | 2364 | self.calls = [ |
Aaron Gable | ca01e2c | 2017-07-19 11:16:02 -0700 | [diff] [blame] | 2365 | ((['git', 'log', '-1', '--format=%B'],), 'This is a description'), |
tandrii@chromium.org | 9b7fd71 | 2016-06-01 13:45:20 +0000 | [diff] [blame] | 2366 | ] |
| 2367 | self.assertEqual(0, git_cl.main(['issue', '0'])) |
Edward Lemur | 8515328 | 2020-02-14 22:06:29 +0000 | [diff] [blame] | 2368 | self.assertNotIn('branch.master.gerritissue', self.mockGit.config) |
| 2369 | self.assertNotIn('branch.master.gerritserver', self.mockGit.config) |
tandrii@chromium.org | 9b7fd71 | 2016-06-01 13:45:20 +0000 | [diff] [blame] | 2370 | |
Aaron Gable | 400e989 | 2017-07-12 15:31:21 -0700 | [diff] [blame] | 2371 | def test_cmd_issue_erase_existing_with_change_id(self): |
Edward Lemur | 8515328 | 2020-02-14 22:06:29 +0000 | [diff] [blame] | 2372 | self.mockGit.config['branch.master.gerritissue'] = '123' |
| 2373 | self.mockGit.config['branch.master.gerritserver'] = ( |
| 2374 | 'https://chromium-review.googlesource.com') |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 2375 | mock.patch('git_cl.Changelist.FetchDescription', |
| 2376 | lambda _: 'This is a description\n\nChange-Id: Ideadbeef').start() |
Aaron Gable | 400e989 | 2017-07-12 15:31:21 -0700 | [diff] [blame] | 2377 | self.calls = [ |
Aaron Gable | ca01e2c | 2017-07-19 11:16:02 -0700 | [diff] [blame] | 2378 | ((['git', 'log', '-1', '--format=%B'],), |
| 2379 | 'This is a description\n\nChange-Id: Ideadbeef'), |
| 2380 | ((['git', 'commit', '--amend', '-m', 'This is a description\n'],), ''), |
Aaron Gable | 400e989 | 2017-07-12 15:31:21 -0700 | [diff] [blame] | 2381 | ] |
| 2382 | self.assertEqual(0, git_cl.main(['issue', '0'])) |
Edward Lemur | 8515328 | 2020-02-14 22:06:29 +0000 | [diff] [blame] | 2383 | self.assertNotIn('branch.master.gerritissue', self.mockGit.config) |
| 2384 | self.assertNotIn('branch.master.gerritserver', self.mockGit.config) |
Aaron Gable | 400e989 | 2017-07-12 15:31:21 -0700 | [diff] [blame] | 2385 | |
phajdan.jr | e328cf9 | 2016-08-22 04:12:17 -0700 | [diff] [blame] | 2386 | def test_cmd_issue_json(self): |
Edward Lemur | 8515328 | 2020-02-14 22:06:29 +0000 | [diff] [blame] | 2387 | self.mockGit.config['branch.master.gerritissue'] = '123' |
| 2388 | self.mockGit.config['branch.master.gerritserver'] = ( |
| 2389 | 'https://chromium-review.googlesource.com') |
Nodir Turakulov | 2737963 | 2021-03-17 18:53:29 +0000 | [diff] [blame] | 2390 | self.mockGit.config['remote.origin.url'] = ( |
| 2391 | 'https://chromium.googlesource.com/chromium/src' |
| 2392 | ) |
| 2393 | self.calls = [( |
| 2394 | ( |
| 2395 | 'write_json', |
| 2396 | 'output.json', |
| 2397 | { |
| 2398 | 'issue': 123, |
| 2399 | 'issue_url': 'https://chromium-review.googlesource.com/123', |
| 2400 | 'gerrit_host': 'chromium-review.googlesource.com', |
| 2401 | 'gerrit_project': 'chromium/src', |
| 2402 | }, |
| 2403 | ), |
| 2404 | '', |
| 2405 | )] |
phajdan.jr | e328cf9 | 2016-08-22 04:12:17 -0700 | [diff] [blame] | 2406 | self.assertEqual(0, git_cl.main(['issue', '--json', 'output.json'])) |
| 2407 | |
tandrii | 16e0b4e | 2016-06-07 10:34:28 -0700 | [diff] [blame] | 2408 | def _common_GerritCommitMsgHookCheck(self): |
Edward Lemur | 15a9b8c | 2020-02-13 00:52:30 +0000 | [diff] [blame] | 2409 | mock.patch( |
| 2410 | 'git_cl.os.path.abspath', |
| 2411 | lambda path: self._mocked_call(['abspath', path])).start() |
| 2412 | mock.patch( |
| 2413 | 'git_cl.os.path.exists', |
| 2414 | lambda path: self._mocked_call(['exists', path])).start() |
| 2415 | mock.patch( |
| 2416 | 'git_cl.gclient_utils.FileRead', |
| 2417 | lambda path: self._mocked_call(['FileRead', path])).start() |
| 2418 | mock.patch( |
| 2419 | 'git_cl.gclient_utils.rm_file_or_tree', |
| 2420 | lambda path: self._mocked_call(['rm_file_or_tree', path])).start() |
Edward Lemur | 1a83da1 | 2020-03-04 21:18:36 +0000 | [diff] [blame] | 2421 | mock.patch( |
Edward Lesmes | ae3586b | 2020-03-23 21:21:14 +0000 | [diff] [blame] | 2422 | 'gclient_utils.AskForData', |
Edward Lemur | 1a83da1 | 2020-03-04 21:18:36 +0000 | [diff] [blame] | 2423 | lambda prompt: self._mocked_call('ask_for_data', prompt)).start() |
Edward Lemur | f38bc17 | 2019-09-03 21:02:13 +0000 | [diff] [blame] | 2424 | return git_cl.Changelist(issue=123) |
tandrii | 16e0b4e | 2016-06-07 10:34:28 -0700 | [diff] [blame] | 2425 | |
| 2426 | def test_GerritCommitMsgHookCheck_custom_hook(self): |
| 2427 | cl = self._common_GerritCommitMsgHookCheck() |
Josip Sokcevic | 464e9ff | 2020-03-18 23:48:55 +0000 | [diff] [blame] | 2428 | self.calls += [((['exists', |
| 2429 | os.path.join('.git', 'hooks', 'commit-msg')], ), True), |
| 2430 | ((['FileRead', |
| 2431 | os.path.join('.git', 'hooks', 'commit-msg')], ), |
| 2432 | '#!/bin/sh\necho "custom hook"')] |
Edward Lemur | 125d60a | 2019-09-13 18:25:41 +0000 | [diff] [blame] | 2433 | cl._GerritCommitMsgHookCheck(offer_removal=True) |
tandrii | 16e0b4e | 2016-06-07 10:34:28 -0700 | [diff] [blame] | 2434 | |
| 2435 | def test_GerritCommitMsgHookCheck_not_exists(self): |
| 2436 | cl = self._common_GerritCommitMsgHookCheck() |
| 2437 | self.calls += [ |
Josip Sokcevic | 464e9ff | 2020-03-18 23:48:55 +0000 | [diff] [blame] | 2438 | ((['exists', os.path.join('.git', 'hooks', 'commit-msg')], ), False), |
tandrii | 16e0b4e | 2016-06-07 10:34:28 -0700 | [diff] [blame] | 2439 | ] |
Edward Lemur | 125d60a | 2019-09-13 18:25:41 +0000 | [diff] [blame] | 2440 | cl._GerritCommitMsgHookCheck(offer_removal=True) |
tandrii | 16e0b4e | 2016-06-07 10:34:28 -0700 | [diff] [blame] | 2441 | |
| 2442 | def test_GerritCommitMsgHookCheck(self): |
| 2443 | cl = self._common_GerritCommitMsgHookCheck() |
| 2444 | self.calls += [ |
Josip Sokcevic | 464e9ff | 2020-03-18 23:48:55 +0000 | [diff] [blame] | 2445 | ((['exists', os.path.join('.git', 'hooks', 'commit-msg')], ), True), |
| 2446 | ((['FileRead', os.path.join('.git', 'hooks', 'commit-msg')], ), |
tandrii | 16e0b4e | 2016-06-07 10:34:28 -0700 | [diff] [blame] | 2447 | '...\n# From Gerrit Code Review\n...\nadd_ChangeId()\n'), |
Andrii Shyshkalov | abc26ac | 2017-03-14 14:49:38 +0100 | [diff] [blame] | 2448 | (('ask_for_data', 'Do you want to remove it now? [Yes/No]: '), 'Yes'), |
Josip Sokcevic | 464e9ff | 2020-03-18 23:48:55 +0000 | [diff] [blame] | 2449 | ((['rm_file_or_tree', |
| 2450 | os.path.join('.git', 'hooks', 'commit-msg')], ), ''), |
tandrii | 16e0b4e | 2016-06-07 10:34:28 -0700 | [diff] [blame] | 2451 | ] |
Edward Lemur | 125d60a | 2019-09-13 18:25:41 +0000 | [diff] [blame] | 2452 | cl._GerritCommitMsgHookCheck(offer_removal=True) |
tandrii | 16e0b4e | 2016-06-07 10:34:28 -0700 | [diff] [blame] | 2453 | |
tandrii | c4344b5 | 2016-08-29 06:04:54 -0700 | [diff] [blame] | 2454 | def test_GerritCmdLand(self): |
Edward Lemur | 8515328 | 2020-02-14 22:06:29 +0000 | [diff] [blame] | 2455 | self.mockGit.config['branch.master.gerritsquashhash'] = 'deadbeaf' |
| 2456 | self.mockGit.config['branch.master.gerritserver'] = ( |
| 2457 | 'chromium-review.googlesource.com') |
tandrii | c4344b5 | 2016-08-29 06:04:54 -0700 | [diff] [blame] | 2458 | self.calls += [ |
tandrii | c4344b5 | 2016-08-29 06:04:54 -0700 | [diff] [blame] | 2459 | ((['git', 'diff', 'deadbeaf'],), ''), # No diff. |
tandrii | c4344b5 | 2016-08-29 06:04:54 -0700 | [diff] [blame] | 2460 | ] |
Edward Lemur | f38bc17 | 2019-09-03 21:02:13 +0000 | [diff] [blame] | 2461 | cl = git_cl.Changelist(issue=123) |
Edward Lemur | 125d60a | 2019-09-13 18:25:41 +0000 | [diff] [blame] | 2462 | cl._GetChangeDetail = lambda *args, **kwargs: { |
tandrii | c4344b5 | 2016-08-29 06:04:54 -0700 | [diff] [blame] | 2463 | 'labels': {}, |
| 2464 | 'current_revision': 'deadbeaf', |
| 2465 | } |
Edward Lemur | 125d60a | 2019-09-13 18:25:41 +0000 | [diff] [blame] | 2466 | cl._GetChangeCommit = lambda: { |
agable | 32978d9 | 2016-11-01 12:55:02 -0700 | [diff] [blame] | 2467 | 'commit': 'deadbeef', |
Aaron Gable | 02cdbb4 | 2016-12-13 16:24:25 -0800 | [diff] [blame] | 2468 | 'web_links': [{'name': 'gitiles', |
agable | 32978d9 | 2016-11-01 12:55:02 -0700 | [diff] [blame] | 2469 | 'url': 'https://git.googlesource.com/test/+/deadbeef'}], |
| 2470 | } |
Edward Lemur | 125d60a | 2019-09-13 18:25:41 +0000 | [diff] [blame] | 2471 | cl.SubmitIssue = lambda wait_for_merge: None |
Olivier Robin | 75ee725 | 2018-04-13 10:02:56 +0200 | [diff] [blame] | 2472 | self.assertEqual(0, cl.CMDLand(force=True, |
| 2473 | bypass_hooks=True, |
| 2474 | verbose=True, |
Saagar Sanghavi | 03b1513 | 2020-08-10 16:43:41 +0000 | [diff] [blame] | 2475 | parallel=False, |
| 2476 | resultdb=False, |
| 2477 | realm=None)) |
Edward Lemur | 73c7670 | 2020-02-06 23:57:18 +0000 | [diff] [blame] | 2478 | self.assertIn( |
| 2479 | 'Issue chromium-review.googlesource.com/123 has been submitted', |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 2480 | sys.stdout.getvalue()) |
Edward Lemur | 73c7670 | 2020-02-06 23:57:18 +0000 | [diff] [blame] | 2481 | self.assertIn( |
| 2482 | 'Landed as: https://git.googlesource.com/test/+/deadbeef', |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 2483 | sys.stdout.getvalue()) |
tandrii | c4344b5 | 2016-08-29 06:04:54 -0700 | [diff] [blame] | 2484 | |
Andrii Shyshkalov | 258e0a6 | 2017-01-24 16:50:57 +0100 | [diff] [blame] | 2485 | def _mock_gerrit_changes_for_detail_cache(self): |
Edward Lesmes | eeca9c6 | 2020-11-20 00:00:17 +0000 | [diff] [blame] | 2486 | mock.patch('git_cl.Changelist.GetGerritHost', lambda _: 'host').start() |
Andrii Shyshkalov | 258e0a6 | 2017-01-24 16:50:57 +0100 | [diff] [blame] | 2487 | |
Andrii Shyshkalov | 258e0a6 | 2017-01-24 16:50:57 +0100 | [diff] [blame] | 2488 | def test_gerrit_change_detail_cache_simple(self): |
| 2489 | self._mock_gerrit_changes_for_detail_cache() |
Edward Lesmes | 7677e5c | 2020-02-19 20:39:03 +0000 | [diff] [blame] | 2490 | gerrit_util.GetChangeDetail.side_effect = ['a', 'b'] |
Edward Lemur | f38bc17 | 2019-09-03 21:02:13 +0000 | [diff] [blame] | 2491 | cl1 = git_cl.Changelist(issue=1) |
Andrii Shyshkalov | 03e0ed2 | 2018-08-28 19:39:30 +0000 | [diff] [blame] | 2492 | cl1._cached_remote_url = ( |
| 2493 | True, 'https://chromium.googlesource.com/a/my/repo.git/') |
Edward Lemur | f38bc17 | 2019-09-03 21:02:13 +0000 | [diff] [blame] | 2494 | cl2 = git_cl.Changelist(issue=2) |
Andrii Shyshkalov | 03e0ed2 | 2018-08-28 19:39:30 +0000 | [diff] [blame] | 2495 | cl2._cached_remote_url = ( |
| 2496 | True, 'https://chromium.googlesource.com/ab/repo') |
Andrii Shyshkalov | b721460 | 2018-08-22 23:20:26 +0000 | [diff] [blame] | 2497 | self.assertEqual(cl1._GetChangeDetail(), 'a') # Miss. |
| 2498 | self.assertEqual(cl1._GetChangeDetail(), 'a') |
| 2499 | self.assertEqual(cl2._GetChangeDetail(), 'b') # Miss. |
Andrii Shyshkalov | 258e0a6 | 2017-01-24 16:50:57 +0100 | [diff] [blame] | 2500 | |
| 2501 | def test_gerrit_change_detail_cache_options(self): |
| 2502 | self._mock_gerrit_changes_for_detail_cache() |
Edward Lesmes | 7677e5c | 2020-02-19 20:39:03 +0000 | [diff] [blame] | 2503 | gerrit_util.GetChangeDetail.side_effect = ['cab', 'ad'] |
Edward Lemur | f38bc17 | 2019-09-03 21:02:13 +0000 | [diff] [blame] | 2504 | cl = git_cl.Changelist(issue=1) |
Andrii Shyshkalov | 03e0ed2 | 2018-08-28 19:39:30 +0000 | [diff] [blame] | 2505 | cl._cached_remote_url = (True, 'https://chromium.googlesource.com/repo/') |
Andrii Shyshkalov | 258e0a6 | 2017-01-24 16:50:57 +0100 | [diff] [blame] | 2506 | self.assertEqual(cl._GetChangeDetail(options=['C', 'A', 'B']), 'cab') |
| 2507 | self.assertEqual(cl._GetChangeDetail(options=['A', 'B', 'C']), 'cab') |
| 2508 | self.assertEqual(cl._GetChangeDetail(options=['B', 'A']), 'cab') |
| 2509 | self.assertEqual(cl._GetChangeDetail(options=['C']), 'cab') |
| 2510 | self.assertEqual(cl._GetChangeDetail(options=['A']), 'cab') |
| 2511 | self.assertEqual(cl._GetChangeDetail(), 'cab') |
| 2512 | |
| 2513 | self.assertEqual(cl._GetChangeDetail(options=['A', 'D']), 'ad') |
| 2514 | self.assertEqual(cl._GetChangeDetail(options=['A']), 'cab') |
| 2515 | self.assertEqual(cl._GetChangeDetail(options=['D']), 'ad') |
| 2516 | self.assertEqual(cl._GetChangeDetail(), 'cab') |
| 2517 | |
Andrii Shyshkalov | 21fb824 | 2017-02-15 21:09:27 +0100 | [diff] [blame] | 2518 | def test_gerrit_description_caching(self): |
Edward Lesmes | 7677e5c | 2020-02-19 20:39:03 +0000 | [diff] [blame] | 2519 | gerrit_util.GetChangeDetail.return_value = { |
| 2520 | 'current_revision': 'rev1', |
| 2521 | 'revisions': { |
| 2522 | 'rev1': {'commit': {'message': 'desc1'}}, |
| 2523 | }, |
| 2524 | } |
Andrii Shyshkalov | 21fb824 | 2017-02-15 21:09:27 +0100 | [diff] [blame] | 2525 | |
| 2526 | self._mock_gerrit_changes_for_detail_cache() |
Edward Lemur | f38bc17 | 2019-09-03 21:02:13 +0000 | [diff] [blame] | 2527 | cl = git_cl.Changelist(issue=1) |
Andrii Shyshkalov | 03e0ed2 | 2018-08-28 19:39:30 +0000 | [diff] [blame] | 2528 | cl._cached_remote_url = ( |
| 2529 | True, 'https://chromium.googlesource.com/a/my/repo.git/') |
Edward Lemur | 6c6827c | 2020-02-06 21:15:18 +0000 | [diff] [blame] | 2530 | self.assertEqual(cl.FetchDescription(), 'desc1') |
| 2531 | self.assertEqual(cl.FetchDescription(), 'desc1') # cache hit. |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 2532 | |
Andrii Shyshkalov | 34924cd | 2017-03-15 17:08:32 +0100 | [diff] [blame] | 2533 | def test_print_current_creds(self): |
| 2534 | class CookiesAuthenticatorMock(object): |
| 2535 | def __init__(self): |
| 2536 | self.gitcookies = { |
| 2537 | 'host.googlesource.com': ('user', 'pass'), |
| 2538 | 'host-review.googlesource.com': ('user', 'pass'), |
| 2539 | } |
| 2540 | self.netrc = self |
| 2541 | self.netrc.hosts = { |
| 2542 | 'github.com': ('user2', None, 'pass2'), |
| 2543 | 'host2.googlesource.com': ('user3', None, 'pass'), |
| 2544 | } |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 2545 | mock.patch('git_cl.gerrit_util.CookiesAuthenticator', |
| 2546 | CookiesAuthenticatorMock).start() |
Andrii Shyshkalov | 34924cd | 2017-03-15 17:08:32 +0100 | [diff] [blame] | 2547 | git_cl._GitCookiesChecker().print_current_creds(include_netrc=True) |
| 2548 | self.assertEqual(list(sys.stdout.getvalue().splitlines()), [ |
| 2549 | ' Host\t User\t Which file', |
| 2550 | '============================\t=====\t===========', |
| 2551 | 'host-review.googlesource.com\t user\t.gitcookies', |
| 2552 | ' host.googlesource.com\t user\t.gitcookies', |
| 2553 | ' host2.googlesource.com\tuser3\t .netrc', |
| 2554 | ]) |
Edward Lemur | 79d4f99 | 2019-11-11 23:49:02 +0000 | [diff] [blame] | 2555 | sys.stdout.seek(0) |
| 2556 | sys.stdout.truncate(0) |
Andrii Shyshkalov | 34924cd | 2017-03-15 17:08:32 +0100 | [diff] [blame] | 2557 | git_cl._GitCookiesChecker().print_current_creds(include_netrc=False) |
| 2558 | self.assertEqual(list(sys.stdout.getvalue().splitlines()), [ |
| 2559 | ' Host\tUser\t Which file', |
| 2560 | '============================\t====\t===========', |
| 2561 | 'host-review.googlesource.com\tuser\t.gitcookies', |
| 2562 | ' host.googlesource.com\tuser\t.gitcookies', |
| 2563 | ]) |
| 2564 | |
Andrii Shyshkalov | 353637c | 2017-03-14 16:52:18 +0100 | [diff] [blame] | 2565 | def _common_creds_check_mocks(self): |
| 2566 | def exists_mock(path): |
| 2567 | dirname = os.path.dirname(path) |
| 2568 | if dirname == os.path.expanduser('~'): |
| 2569 | dirname = '~' |
| 2570 | base = os.path.basename(path) |
Josip Sokcevic | 464e9ff | 2020-03-18 23:48:55 +0000 | [diff] [blame] | 2571 | if base in (NETRC_FILENAME, '.gitcookies'): |
| 2572 | return self._mocked_call('os.path.exists', os.path.join(dirname, base)) |
Andrii Shyshkalov | 353637c | 2017-03-14 16:52:18 +0100 | [diff] [blame] | 2573 | # git cl also checks for existence other files not relevant to this test. |
| 2574 | return None |
Edward Lemur | 1a83da1 | 2020-03-04 21:18:36 +0000 | [diff] [blame] | 2575 | mock.patch( |
Edward Lesmes | ae3586b | 2020-03-23 21:21:14 +0000 | [diff] [blame] | 2576 | 'gclient_utils.AskForData', |
Edward Lemur | 1a83da1 | 2020-03-04 21:18:36 +0000 | [diff] [blame] | 2577 | lambda prompt: self._mocked_call('ask_for_data', prompt)).start() |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 2578 | mock.patch('os.path.exists', exists_mock).start() |
Andrii Shyshkalov | 353637c | 2017-03-14 16:52:18 +0100 | [diff] [blame] | 2579 | |
| 2580 | def test_creds_check_gitcookies_not_configured(self): |
| 2581 | self._common_creds_check_mocks() |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 2582 | mock.patch('git_cl._GitCookiesChecker.get_hosts_with_creds', |
| 2583 | lambda _, include_netrc=False: []).start() |
Andrii Shyshkalov | 353637c | 2017-03-14 16:52:18 +0100 | [diff] [blame] | 2584 | self.calls = [ |
Josip Sokcevic | 464e9ff | 2020-03-18 23:48:55 +0000 | [diff] [blame] | 2585 | ((['git', 'config', '--path', 'http.cookiefile'], ), CERR1), |
| 2586 | ((['git', 'config', '--global', 'http.cookiefile'], ), CERR1), |
| 2587 | (('os.path.exists', os.path.join('~', NETRC_FILENAME)), True), |
| 2588 | (('ask_for_data', 'Press Enter to setup .gitcookies, ' |
| 2589 | 'or Ctrl+C to abort'), ''), |
| 2590 | (([ |
| 2591 | 'git', 'config', '--global', 'http.cookiefile', |
| 2592 | os.path.expanduser(os.path.join('~', '.gitcookies')) |
| 2593 | ], ), ''), |
Andrii Shyshkalov | 353637c | 2017-03-14 16:52:18 +0100 | [diff] [blame] | 2594 | ] |
| 2595 | self.assertEqual(0, git_cl.main(['creds-check'])) |
Edward Lemur | 73c7670 | 2020-02-06 23:57:18 +0000 | [diff] [blame] | 2596 | self.assertTrue( |
| 2597 | sys.stdout.getvalue().startswith( |
| 2598 | 'You seem to be using outdated .netrc for git credentials:')) |
| 2599 | self.assertIn( |
| 2600 | '\nConfigured git to use .gitcookies from', |
| 2601 | sys.stdout.getvalue()) |
Andrii Shyshkalov | 353637c | 2017-03-14 16:52:18 +0100 | [diff] [blame] | 2602 | |
| 2603 | def test_creds_check_gitcookies_configured_custom_broken(self): |
| 2604 | self._common_creds_check_mocks() |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 2605 | mock.patch('git_cl._GitCookiesChecker.get_hosts_with_creds', |
| 2606 | lambda _, include_netrc=False: []).start() |
Josip Sokcevic | 464e9ff | 2020-03-18 23:48:55 +0000 | [diff] [blame] | 2607 | custom_cookie_path = ('C:\\.gitcookies' |
| 2608 | if sys.platform == 'win32' else '/custom/.gitcookies') |
Andrii Shyshkalov | 353637c | 2017-03-14 16:52:18 +0100 | [diff] [blame] | 2609 | self.calls = [ |
Josip Sokcevic | 464e9ff | 2020-03-18 23:48:55 +0000 | [diff] [blame] | 2610 | ((['git', 'config', '--path', 'http.cookiefile'], ), CERR1), |
| 2611 | ((['git', 'config', '--global', 'http.cookiefile'], ), |
| 2612 | custom_cookie_path), |
| 2613 | (('os.path.exists', custom_cookie_path), False), |
| 2614 | (('ask_for_data', 'Reconfigure git to use default .gitcookies? ' |
| 2615 | 'Press Enter to reconfigure, or Ctrl+C to abort'), ''), |
| 2616 | (([ |
| 2617 | 'git', 'config', '--global', 'http.cookiefile', |
| 2618 | os.path.expanduser(os.path.join('~', '.gitcookies')) |
| 2619 | ], ), ''), |
Andrii Shyshkalov | 353637c | 2017-03-14 16:52:18 +0100 | [diff] [blame] | 2620 | ] |
| 2621 | self.assertEqual(0, git_cl.main(['creds-check'])) |
Edward Lemur | 73c7670 | 2020-02-06 23:57:18 +0000 | [diff] [blame] | 2622 | self.assertIn( |
| 2623 | 'WARNING: You have configured custom path to .gitcookies: ', |
| 2624 | sys.stdout.getvalue()) |
| 2625 | self.assertIn( |
| 2626 | 'However, your configured .gitcookies file is missing.', |
| 2627 | sys.stdout.getvalue()) |
Andrii Shyshkalov | 353637c | 2017-03-14 16:52:18 +0100 | [diff] [blame] | 2628 | |
Andrii Shyshkalov | 625986d | 2017-03-16 00:24:37 +0100 | [diff] [blame] | 2629 | def test_git_cl_comment_add_gerrit(self): |
Edward Lemur | 8515328 | 2020-02-14 22:06:29 +0000 | [diff] [blame] | 2630 | self.mockGit.branchref = None |
Edward Lemur | 2696407 | 2020-02-19 19:18:51 +0000 | [diff] [blame] | 2631 | self.mockGit.config['remote.origin.url'] = ( |
| 2632 | 'https://chromium.googlesource.com/infra/infra') |
Andrii Shyshkalov | 625986d | 2017-03-16 00:24:37 +0100 | [diff] [blame] | 2633 | self.calls = [ |
Andrii Shyshkalov | 889677c | 2018-08-28 20:43:06 +0000 | [diff] [blame] | 2634 | (('SetReview', 'chromium-review.googlesource.com', 'infra%2Finfra~10', |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 2635 | 'msg', None, None, None), |
Aaron Gable | 636b13f | 2017-07-14 10:42:48 -0700 | [diff] [blame] | 2636 | None), |
Andrii Shyshkalov | 625986d | 2017-03-16 00:24:37 +0100 | [diff] [blame] | 2637 | ] |
Edward Lemur | 52969c9 | 2020-02-06 18:15:28 +0000 | [diff] [blame] | 2638 | self.assertEqual(0, git_cl.main(['comment', '-i', '10', '-a', 'msg'])) |
Andrii Shyshkalov | 625986d | 2017-03-16 00:24:37 +0100 | [diff] [blame] | 2639 | |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 2640 | @mock.patch('git_cl.Changelist.GetBranch', return_value='foo') |
| 2641 | def test_git_cl_comments_fetch_gerrit(self, *_mocks): |
Edward Lemur | 2696407 | 2020-02-19 19:18:51 +0000 | [diff] [blame] | 2642 | self.mockGit.config['remote.origin.url'] = ( |
| 2643 | 'https://chromium.googlesource.com/infra/infra') |
Edward Lesmes | 7677e5c | 2020-02-19 20:39:03 +0000 | [diff] [blame] | 2644 | gerrit_util.GetChangeDetail.return_value = { |
Aaron Gable | 0ffdf2d | 2017-06-05 13:01:17 -0700 | [diff] [blame] | 2645 | 'owner': {'email': 'owner@example.com'}, |
Quinten Yearsley | 0e617c0 | 2019-02-20 00:37:03 +0000 | [diff] [blame] | 2646 | 'current_revision': 'ba5eba11', |
| 2647 | 'revisions': { |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 2648 | 'deadbeaf': { |
Quinten Yearsley | 0e617c0 | 2019-02-20 00:37:03 +0000 | [diff] [blame] | 2649 | '_number': 1, |
| 2650 | }, |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 2651 | 'ba5eba11': { |
Quinten Yearsley | 0e617c0 | 2019-02-20 00:37:03 +0000 | [diff] [blame] | 2652 | '_number': 2, |
| 2653 | }, |
| 2654 | }, |
Andrii Shyshkalov | 5a0cf20 | 2017-03-17 16:14:59 +0100 | [diff] [blame] | 2655 | 'messages': [ |
| 2656 | { |
| 2657 | u'_revision_number': 1, |
| 2658 | u'author': { |
| 2659 | u'_account_id': 1111084, |
Andrii Shyshkalov | 8aa9d62 | 2020-03-10 19:15:35 +0000 | [diff] [blame] | 2660 | u'email': u'could-be-anything@example.com', |
| 2661 | u'name': u'LUCI CQ' |
Andrii Shyshkalov | 5a0cf20 | 2017-03-17 16:14:59 +0100 | [diff] [blame] | 2662 | }, |
| 2663 | u'date': u'2017-03-15 20:08:45.000000000', |
| 2664 | u'id': u'f5a6c25ecbd3b3b54a43ae418ed97eff046dc50b', |
Dirk Pranke | f6a5880 | 2017-10-17 12:49:42 -0700 | [diff] [blame] | 2665 | 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] | 2666 | u'tag': u'autogenerated:cq:dry-run' |
| 2667 | }, |
| 2668 | { |
| 2669 | u'_revision_number': 2, |
| 2670 | u'author': { |
| 2671 | u'_account_id': 11151243, |
| 2672 | u'email': u'owner@example.com', |
| 2673 | u'name': u'owner' |
| 2674 | }, |
| 2675 | u'date': u'2017-03-16 20:00:41.000000000', |
| 2676 | u'id': u'f5a6c25ecbd3b3b54a43ae418ed97eff046d1234', |
| 2677 | u'message': u'PTAL', |
| 2678 | }, |
| 2679 | { |
| 2680 | u'_revision_number': 2, |
| 2681 | u'author': { |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 2682 | u'_account_id': 148512, |
Andrii Shyshkalov | 5a0cf20 | 2017-03-17 16:14:59 +0100 | [diff] [blame] | 2683 | u'email': u'reviewer@example.com', |
| 2684 | u'name': u'reviewer' |
| 2685 | }, |
| 2686 | u'date': u'2017-03-17 05:19:37.500000000', |
| 2687 | u'id': u'f5a6c25ecbd3b3b54a43ae418ed97eff046d4568', |
| 2688 | u'message': u'Patch Set 2: Code-Review+1', |
| 2689 | }, |
| 2690 | ] |
Edward Lesmes | 7677e5c | 2020-02-19 20:39:03 +0000 | [diff] [blame] | 2691 | } |
| 2692 | self.calls = [ |
Andrii Shyshkalov | a3762a9 | 2020-11-25 10:20:42 +0000 | [diff] [blame] | 2693 | (('GetChangeComments', 'chromium-review.googlesource.com', |
| 2694 | 'infra%2Finfra~1'), { |
| 2695 | '/COMMIT_MSG': [ |
| 2696 | { |
| 2697 | 'author': { |
| 2698 | 'email': u'reviewer@example.com' |
| 2699 | }, |
| 2700 | 'updated': u'2017-03-17 05:19:37.500000000', |
| 2701 | 'patch_set': 2, |
| 2702 | 'side': 'REVISION', |
| 2703 | 'message': 'Please include a bug link', |
| 2704 | }, |
| 2705 | ], |
| 2706 | 'codereview.settings': [ |
| 2707 | { |
| 2708 | 'author': { |
| 2709 | 'email': u'owner@example.com' |
| 2710 | }, |
| 2711 | 'updated': u'2017-03-16 20:00:41.000000000', |
| 2712 | 'patch_set': 2, |
| 2713 | 'side': 'PARENT', |
| 2714 | 'line': 42, |
| 2715 | 'message': 'I removed this because it is bad', |
| 2716 | }, |
| 2717 | ] |
| 2718 | }), |
| 2719 | (('GetChangeRobotComments', 'chromium-review.googlesource.com', |
| 2720 | 'infra%2Finfra~1'), {}), |
| 2721 | ] * 2 + [(('write_json', 'output.json', [{ |
| 2722 | u'date': |
| 2723 | u'2017-03-16 20:00:41.000000', |
| 2724 | u'message': (u'PTAL\n' + u'\n' + u'codereview.settings\n' + |
| 2725 | u' Base, Line 42: https://crrev.com/c/1/2/' |
| 2726 | u'codereview.settings#b42\n' + |
| 2727 | u' I removed this because it is bad\n'), |
| 2728 | u'autogenerated': |
| 2729 | False, |
| 2730 | u'approval': |
| 2731 | False, |
| 2732 | u'disapproval': |
| 2733 | False, |
| 2734 | u'sender': |
| 2735 | u'owner@example.com' |
| 2736 | }, { |
| 2737 | u'date': |
| 2738 | u'2017-03-17 05:19:37.500000', |
| 2739 | u'message': |
| 2740 | (u'Patch Set 2: Code-Review+1\n' + u'\n' + u'/COMMIT_MSG\n' + |
| 2741 | u' PS2, File comment: https://crrev.com/c/1/2//COMMIT_MSG#\n' + |
| 2742 | u' Please include a bug link\n'), |
| 2743 | u'autogenerated': |
| 2744 | False, |
| 2745 | u'approval': |
| 2746 | False, |
| 2747 | u'disapproval': |
| 2748 | False, |
| 2749 | u'sender': |
| 2750 | u'reviewer@example.com' |
| 2751 | }]), '')] |
Andrii Shyshkalov | 5a0cf20 | 2017-03-17 16:14:59 +0100 | [diff] [blame] | 2752 | expected_comments_summary = [ |
Andrii Shyshkalov | a3762a9 | 2020-11-25 10:20:42 +0000 | [diff] [blame] | 2753 | git_cl._CommentSummary( |
| 2754 | message=(u'PTAL\n' + u'\n' + u'codereview.settings\n' + |
| 2755 | u' Base, Line 42: https://crrev.com/c/1/2/' + |
| 2756 | u'codereview.settings#b42\n' + |
| 2757 | u' I removed this because it is bad\n'), |
| 2758 | date=datetime.datetime(2017, 3, 16, 20, 0, 41, 0), |
| 2759 | autogenerated=False, |
| 2760 | disapproval=False, |
| 2761 | approval=False, |
| 2762 | sender=u'owner@example.com'), |
| 2763 | git_cl._CommentSummary(message=( |
| 2764 | u'Patch Set 2: Code-Review+1\n' + u'\n' + u'/COMMIT_MSG\n' + |
| 2765 | u' PS2, File comment: https://crrev.com/c/1/2//COMMIT_MSG#\n' + |
Aaron Gable | 0ffdf2d | 2017-06-05 13:01:17 -0700 | [diff] [blame] | 2766 | u' Please include a bug link\n'), |
Andrii Shyshkalov | a3762a9 | 2020-11-25 10:20:42 +0000 | [diff] [blame] | 2767 | date=datetime.datetime(2017, 3, 17, 5, 19, 37, |
| 2768 | 500000), |
| 2769 | autogenerated=False, |
| 2770 | disapproval=False, |
| 2771 | approval=False, |
| 2772 | sender=u'reviewer@example.com'), |
Andrii Shyshkalov | 5a0cf20 | 2017-03-17 16:14:59 +0100 | [diff] [blame] | 2773 | ] |
Quinten Yearsley | 0e617c0 | 2019-02-20 00:37:03 +0000 | [diff] [blame] | 2774 | cl = git_cl.Changelist( |
Edward Lemur | f38bc17 | 2019-09-03 21:02:13 +0000 | [diff] [blame] | 2775 | issue=1, branchref='refs/heads/foo') |
Andrii Shyshkalov | 5a0cf20 | 2017-03-17 16:14:59 +0100 | [diff] [blame] | 2776 | self.assertEqual(cl.GetCommentsSummary(), expected_comments_summary) |
Quinten Yearsley | 0e617c0 | 2019-02-20 00:37:03 +0000 | [diff] [blame] | 2777 | self.assertEqual( |
| 2778 | 0, git_cl.main(['comments', '-i', '1', '-j', 'output.json'])) |
| 2779 | |
| 2780 | def test_git_cl_comments_robot_comments(self): |
| 2781 | # git cl comments also fetches robot comments (which are considered a type |
| 2782 | # of autogenerated comment), and unlike other types of comments, only robot |
| 2783 | # comments from the latest patchset are shown. |
Edward Lemur | 2696407 | 2020-02-19 19:18:51 +0000 | [diff] [blame] | 2784 | self.mockGit.config['remote.origin.url'] = ( |
Andrii Shyshkalov | a3762a9 | 2020-11-25 10:20:42 +0000 | [diff] [blame] | 2785 | 'https://x.googlesource.com/infra/infra') |
Edward Lesmes | 7677e5c | 2020-02-19 20:39:03 +0000 | [diff] [blame] | 2786 | gerrit_util.GetChangeDetail.return_value = { |
| 2787 | 'owner': {'email': 'owner@example.com'}, |
| 2788 | 'current_revision': 'ba5eba11', |
| 2789 | 'revisions': { |
| 2790 | 'deadbeaf': { |
| 2791 | '_number': 1, |
Quinten Yearsley | 0e617c0 | 2019-02-20 00:37:03 +0000 | [diff] [blame] | 2792 | }, |
Edward Lesmes | 7677e5c | 2020-02-19 20:39:03 +0000 | [diff] [blame] | 2793 | 'ba5eba11': { |
| 2794 | '_number': 2, |
| 2795 | }, |
| 2796 | }, |
| 2797 | 'messages': [ |
| 2798 | { |
| 2799 | u'_revision_number': 1, |
| 2800 | u'author': { |
| 2801 | u'_account_id': 1111084, |
| 2802 | u'email': u'commit-bot@chromium.org', |
| 2803 | u'name': u'Commit Bot' |
| 2804 | }, |
| 2805 | u'date': u'2017-03-15 20:08:45.000000000', |
| 2806 | u'id': u'f5a6c25ecbd3b3b54a43ae418ed97eff046dc50b', |
| 2807 | u'message': u'Patch Set 1:\n\nDry run: CQ is trying the patch...', |
| 2808 | u'tag': u'autogenerated:cq:dry-run' |
| 2809 | }, |
| 2810 | { |
| 2811 | u'_revision_number': 1, |
| 2812 | u'author': { |
| 2813 | u'_account_id': 123, |
| 2814 | u'email': u'tricium@serviceaccount.com', |
| 2815 | u'name': u'Tricium' |
| 2816 | }, |
| 2817 | u'date': u'2017-03-16 20:00:41.000000000', |
| 2818 | u'id': u'f5a6c25ecbd3b3b54a43ae418ed97eff046d1234', |
| 2819 | u'message': u'(1 comment)', |
| 2820 | u'tag': u'autogenerated:tricium', |
| 2821 | }, |
| 2822 | { |
| 2823 | u'_revision_number': 1, |
| 2824 | u'author': { |
| 2825 | u'_account_id': 123, |
| 2826 | u'email': u'tricium@serviceaccount.com', |
| 2827 | u'name': u'Tricium' |
| 2828 | }, |
| 2829 | u'date': u'2017-03-16 20:00:41.000000000', |
| 2830 | u'id': u'f5a6c25ecbd3b3b54a43ae418ed97eff046d1234', |
| 2831 | u'message': u'(1 comment)', |
| 2832 | u'tag': u'autogenerated:tricium', |
| 2833 | }, |
| 2834 | { |
| 2835 | u'_revision_number': 2, |
| 2836 | u'author': { |
| 2837 | u'_account_id': 123, |
| 2838 | u'email': u'tricium@serviceaccount.com', |
| 2839 | u'name': u'reviewer' |
| 2840 | }, |
| 2841 | u'date': u'2017-03-17 05:30:37.000000000', |
| 2842 | u'tag': u'autogenerated:tricium', |
| 2843 | u'id': u'f5a6c25ecbd3b3b54a43ae418ed97eff046d4568', |
| 2844 | u'message': u'(1 comment)', |
| 2845 | }, |
| 2846 | ] |
| 2847 | } |
| 2848 | self.calls = [ |
Andrii Shyshkalov | a3762a9 | 2020-11-25 10:20:42 +0000 | [diff] [blame] | 2849 | (('GetChangeComments', 'x-review.googlesource.com', 'infra%2Finfra~1'), |
| 2850 | {}), |
| 2851 | (('GetChangeRobotComments', 'x-review.googlesource.com', |
| 2852 | 'infra%2Finfra~1'), { |
| 2853 | 'codereview.settings': [ |
| 2854 | { |
| 2855 | u'author': { |
| 2856 | u'email': u'tricium@serviceaccount.com' |
| 2857 | }, |
| 2858 | u'updated': u'2017-03-17 05:30:37.000000000', |
| 2859 | u'robot_run_id': u'5565031076855808', |
| 2860 | u'robot_id': u'Linter/Category', |
| 2861 | u'tag': u'autogenerated:tricium', |
| 2862 | u'patch_set': 2, |
| 2863 | u'side': u'REVISION', |
| 2864 | u'message': u'Linter warning message text', |
| 2865 | u'line': 32, |
| 2866 | }, |
| 2867 | ], |
| 2868 | }), |
Quinten Yearsley | 0e617c0 | 2019-02-20 00:37:03 +0000 | [diff] [blame] | 2869 | ] |
| 2870 | expected_comments_summary = [ |
Andrii Shyshkalov | a3762a9 | 2020-11-25 10:20:42 +0000 | [diff] [blame] | 2871 | git_cl._CommentSummary( |
| 2872 | date=datetime.datetime(2017, 3, 17, 5, 30, 37), |
| 2873 | message=(u'(1 comment)\n\ncodereview.settings\n' |
| 2874 | u' PS2, Line 32: https://x-review.googlesource.com/c/1/2/' |
| 2875 | u'codereview.settings#32\n' |
| 2876 | u' Linter warning message text\n'), |
| 2877 | sender=u'tricium@serviceaccount.com', |
| 2878 | autogenerated=True, |
| 2879 | approval=False, |
| 2880 | disapproval=False) |
Quinten Yearsley | 0e617c0 | 2019-02-20 00:37:03 +0000 | [diff] [blame] | 2881 | ] |
| 2882 | cl = git_cl.Changelist( |
Edward Lemur | f38bc17 | 2019-09-03 21:02:13 +0000 | [diff] [blame] | 2883 | issue=1, branchref='refs/heads/foo') |
Quinten Yearsley | 0e617c0 | 2019-02-20 00:37:03 +0000 | [diff] [blame] | 2884 | self.assertEqual(cl.GetCommentsSummary(), expected_comments_summary) |
Andrii Shyshkalov | 34924cd | 2017-03-15 17:08:32 +0100 | [diff] [blame] | 2885 | |
Andrii Shyshkalov | 81db1d5 | 2018-08-23 02:17:41 +0000 | [diff] [blame] | 2886 | def test_get_remote_url_with_mirror(self): |
| 2887 | original_os_path_isdir = os.path.isdir |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 2888 | |
Andrii Shyshkalov | 81db1d5 | 2018-08-23 02:17:41 +0000 | [diff] [blame] | 2889 | def selective_os_path_isdir_mock(path): |
| 2890 | if path == '/cache/this-dir-exists': |
| 2891 | return self._mocked_call('os.path.isdir', path) |
| 2892 | return original_os_path_isdir(path) |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 2893 | |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 2894 | mock.patch('os.path.isdir', selective_os_path_isdir_mock).start() |
Andrii Shyshkalov | 81db1d5 | 2018-08-23 02:17:41 +0000 | [diff] [blame] | 2895 | |
| 2896 | url = 'https://chromium.googlesource.com/my/repo' |
Edward Lemur | 2696407 | 2020-02-19 19:18:51 +0000 | [diff] [blame] | 2897 | self.mockGit.config['remote.origin.url'] = ( |
| 2898 | '/cache/this-dir-exists') |
| 2899 | self.mockGit.config['/cache/this-dir-exists:remote.origin.url'] = ( |
| 2900 | url) |
Andrii Shyshkalov | 81db1d5 | 2018-08-23 02:17:41 +0000 | [diff] [blame] | 2901 | self.calls = [ |
Andrii Shyshkalov | 81db1d5 | 2018-08-23 02:17:41 +0000 | [diff] [blame] | 2902 | (('os.path.isdir', '/cache/this-dir-exists'), |
| 2903 | True), |
Andrii Shyshkalov | 81db1d5 | 2018-08-23 02:17:41 +0000 | [diff] [blame] | 2904 | ] |
Edward Lemur | f38bc17 | 2019-09-03 21:02:13 +0000 | [diff] [blame] | 2905 | cl = git_cl.Changelist(issue=1) |
Andrii Shyshkalov | 81db1d5 | 2018-08-23 02:17:41 +0000 | [diff] [blame] | 2906 | self.assertEqual(cl.GetRemoteUrl(), url) |
| 2907 | self.assertEqual(cl.GetRemoteUrl(), url) # Must be cached. |
| 2908 | |
Edward Lemur | 298f2cf | 2019-02-22 21:40:39 +0000 | [diff] [blame] | 2909 | def test_get_remote_url_non_existing_mirror(self): |
| 2910 | original_os_path_isdir = os.path.isdir |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 2911 | |
Edward Lemur | 298f2cf | 2019-02-22 21:40:39 +0000 | [diff] [blame] | 2912 | def selective_os_path_isdir_mock(path): |
| 2913 | if path == '/cache/this-dir-doesnt-exist': |
| 2914 | return self._mocked_call('os.path.isdir', path) |
| 2915 | return original_os_path_isdir(path) |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 2916 | |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 2917 | mock.patch('os.path.isdir', selective_os_path_isdir_mock).start() |
| 2918 | mock.patch('logging.error', |
| 2919 | lambda *a: self._mocked_call('logging.error', *a)).start() |
Edward Lemur | 298f2cf | 2019-02-22 21:40:39 +0000 | [diff] [blame] | 2920 | |
Edward Lemur | 2696407 | 2020-02-19 19:18:51 +0000 | [diff] [blame] | 2921 | self.mockGit.config['remote.origin.url'] = ( |
| 2922 | '/cache/this-dir-doesnt-exist') |
Edward Lemur | 298f2cf | 2019-02-22 21:40:39 +0000 | [diff] [blame] | 2923 | self.calls = [ |
Edward Lemur | 298f2cf | 2019-02-22 21:40:39 +0000 | [diff] [blame] | 2924 | (('os.path.isdir', '/cache/this-dir-doesnt-exist'), |
| 2925 | False), |
| 2926 | (('logging.error', |
Josip | 906bfde | 2020-01-31 22:38:49 +0000 | [diff] [blame] | 2927 | 'Remote "%(remote)s" for branch "%(branch)s" points to "%(url)s", ' |
| 2928 | 'but it doesn\'t exist.', { |
| 2929 | 'remote': 'origin', |
| 2930 | 'branch': 'master', |
| 2931 | 'url': '/cache/this-dir-doesnt-exist'} |
| 2932 | ), None), |
Edward Lemur | 298f2cf | 2019-02-22 21:40:39 +0000 | [diff] [blame] | 2933 | ] |
Edward Lemur | f38bc17 | 2019-09-03 21:02:13 +0000 | [diff] [blame] | 2934 | cl = git_cl.Changelist(issue=1) |
Edward Lemur | 298f2cf | 2019-02-22 21:40:39 +0000 | [diff] [blame] | 2935 | self.assertIsNone(cl.GetRemoteUrl()) |
| 2936 | |
| 2937 | def test_get_remote_url_misconfigured_mirror(self): |
| 2938 | original_os_path_isdir = os.path.isdir |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 2939 | |
Edward Lemur | 298f2cf | 2019-02-22 21:40:39 +0000 | [diff] [blame] | 2940 | def selective_os_path_isdir_mock(path): |
| 2941 | if path == '/cache/this-dir-exists': |
| 2942 | return self._mocked_call('os.path.isdir', path) |
| 2943 | return original_os_path_isdir(path) |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 2944 | |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 2945 | mock.patch('os.path.isdir', selective_os_path_isdir_mock).start() |
| 2946 | mock.patch('logging.error', |
| 2947 | lambda *a: self._mocked_call('logging.error', *a)).start() |
Edward Lemur | 298f2cf | 2019-02-22 21:40:39 +0000 | [diff] [blame] | 2948 | |
Edward Lemur | 2696407 | 2020-02-19 19:18:51 +0000 | [diff] [blame] | 2949 | self.mockGit.config['remote.origin.url'] = ( |
| 2950 | '/cache/this-dir-exists') |
Edward Lemur | 298f2cf | 2019-02-22 21:40:39 +0000 | [diff] [blame] | 2951 | self.calls = [ |
Edward Lemur | 298f2cf | 2019-02-22 21:40:39 +0000 | [diff] [blame] | 2952 | (('os.path.isdir', '/cache/this-dir-exists'), True), |
Edward Lemur | 298f2cf | 2019-02-22 21:40:39 +0000 | [diff] [blame] | 2953 | (('logging.error', |
| 2954 | 'Remote "%(remote)s" for branch "%(branch)s" points to ' |
| 2955 | '"%(cache_path)s", but it is misconfigured.\n' |
| 2956 | '"%(cache_path)s" must be a git repo and must have a remote named ' |
| 2957 | '"%(remote)s" pointing to the git host.', { |
| 2958 | 'remote': 'origin', |
| 2959 | 'cache_path': '/cache/this-dir-exists', |
| 2960 | 'branch': 'master'} |
| 2961 | ), None), |
| 2962 | ] |
Edward Lemur | f38bc17 | 2019-09-03 21:02:13 +0000 | [diff] [blame] | 2963 | cl = git_cl.Changelist(issue=1) |
Edward Lemur | 298f2cf | 2019-02-22 21:40:39 +0000 | [diff] [blame] | 2964 | self.assertIsNone(cl.GetRemoteUrl()) |
| 2965 | |
Andrii Shyshkalov | 2d0e03c | 2018-08-25 04:18:09 +0000 | [diff] [blame] | 2966 | def test_gerrit_change_identifier_with_project(self): |
Edward Lemur | 2696407 | 2020-02-19 19:18:51 +0000 | [diff] [blame] | 2967 | self.mockGit.config['remote.origin.url'] = ( |
| 2968 | 'https://chromium.googlesource.com/a/my/repo.git/') |
Edward Lemur | f38bc17 | 2019-09-03 21:02:13 +0000 | [diff] [blame] | 2969 | cl = git_cl.Changelist(issue=123456) |
Andrii Shyshkalov | 2d0e03c | 2018-08-25 04:18:09 +0000 | [diff] [blame] | 2970 | self.assertEqual(cl._GerritChangeIdentifier(), 'my%2Frepo~123456') |
| 2971 | |
| 2972 | def test_gerrit_change_identifier_without_project(self): |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 2973 | mock.patch('logging.error', |
| 2974 | lambda *a: self._mocked_call('logging.error', *a)).start() |
Josip | 906bfde | 2020-01-31 22:38:49 +0000 | [diff] [blame] | 2975 | |
Andrii Shyshkalov | 2d0e03c | 2018-08-25 04:18:09 +0000 | [diff] [blame] | 2976 | self.calls = [ |
Josip | 906bfde | 2020-01-31 22:38:49 +0000 | [diff] [blame] | 2977 | (('logging.error', |
| 2978 | 'Remote "%(remote)s" for branch "%(branch)s" points to "%(url)s", ' |
| 2979 | 'but it doesn\'t exist.', { |
| 2980 | 'remote': 'origin', |
| 2981 | 'branch': 'master', |
| 2982 | 'url': ''} |
| 2983 | ), None), |
Andrii Shyshkalov | 2d0e03c | 2018-08-25 04:18:09 +0000 | [diff] [blame] | 2984 | ] |
Edward Lemur | f38bc17 | 2019-09-03 21:02:13 +0000 | [diff] [blame] | 2985 | cl = git_cl.Changelist(issue=123456) |
Andrii Shyshkalov | 2d0e03c | 2018-08-25 04:18:09 +0000 | [diff] [blame] | 2986 | self.assertEqual(cl._GerritChangeIdentifier(), '123456') |
Andrii Shyshkalov | 1e82867 | 2018-08-23 22:34:37 +0000 | [diff] [blame] | 2987 | |
Josip Sokcevic | c39ab99 | 2020-09-24 20:09:15 +0000 | [diff] [blame] | 2988 | def test_gerrit_new_default(self): |
| 2989 | self._run_gerrit_upload_test( |
| 2990 | [], |
| 2991 | 'desc ✔\n\nBUG=\n\nChange-Id: I123456789\n', |
| 2992 | [], |
| 2993 | squash=False, |
| 2994 | squash_mode='override_nosquash', |
| 2995 | change_id='I123456789', |
Edward Lesmes | 8c43c3f | 2021-01-20 00:20:26 +0000 | [diff] [blame] | 2996 | default_branch='main') |
Josip Sokcevic | c39ab99 | 2020-09-24 20:09:15 +0000 | [diff] [blame] | 2997 | |
Quinten Yearsley | 0c62da9 | 2017-05-31 13:39:42 -0700 | [diff] [blame] | 2998 | |
Edward Lemur | 9aa1a96 | 2020-02-25 00:58:38 +0000 | [diff] [blame] | 2999 | class ChangelistTest(unittest.TestCase): |
mlcui | 3da9171 | 2021-05-05 10:00:30 +0000 | [diff] [blame^] | 3000 | LAST_COMMIT_SUBJECT = 'Fixes goat teleporter destination to be Australia' |
| 3001 | |
| 3002 | def _mock_run_git(commands): |
| 3003 | if commands == ['show', '-s', '--format=%s', 'HEAD']: |
| 3004 | return ChangelistTest.LAST_COMMIT_SUBJECT |
| 3005 | |
Edward Lemur | 227d510 | 2020-02-25 23:45:35 +0000 | [diff] [blame] | 3006 | def setUp(self): |
| 3007 | super(ChangelistTest, self).setUp() |
| 3008 | mock.patch('gclient_utils.FileRead').start() |
| 3009 | mock.patch('gclient_utils.FileWrite').start() |
| 3010 | mock.patch('gclient_utils.temporary_file', TemporaryFileMock()).start() |
| 3011 | mock.patch( |
| 3012 | 'git_cl.Changelist.GetCodereviewServer', |
| 3013 | return_value='https://chromium-review.googlesource.com').start() |
| 3014 | mock.patch('git_cl.Changelist.GetAuthor', return_value='author').start() |
| 3015 | mock.patch('git_cl.Changelist.GetIssue', return_value=123456).start() |
| 3016 | mock.patch('git_cl.Changelist.GetPatchset', return_value=7).start() |
Edward Lesmes | eb1bd62 | 2021-03-01 19:54:07 +0000 | [diff] [blame] | 3017 | mock.patch( |
| 3018 | 'git_cl.Changelist.GetRemoteBranch', |
| 3019 | return_value=('origin', 'refs/remotes/origin/main')).start() |
Edward Lemur | 227d510 | 2020-02-25 23:45:35 +0000 | [diff] [blame] | 3020 | mock.patch('git_cl.PRESUBMIT_SUPPORT', 'PRESUBMIT_SUPPORT').start() |
| 3021 | mock.patch('git_cl.Settings.GetRoot', return_value='root').start() |
| 3022 | mock.patch('git_cl.time_time').start() |
| 3023 | mock.patch('metrics.collector').start() |
| 3024 | mock.patch('subprocess2.Popen').start() |
Edward Lesmes | eb1bd62 | 2021-03-01 19:54:07 +0000 | [diff] [blame] | 3025 | mock.patch( |
| 3026 | 'git_cl.Changelist.GetGerritProject', return_value='project').start() |
Edward Lemur | 227d510 | 2020-02-25 23:45:35 +0000 | [diff] [blame] | 3027 | self.addCleanup(mock.patch.stopall) |
| 3028 | self.temp_count = 0 |
| 3029 | |
Edward Lemur | 227d510 | 2020-02-25 23:45:35 +0000 | [diff] [blame] | 3030 | def testRunHook(self): |
| 3031 | expected_results = { |
Dirk Pranke | 61bf6e8 | 2021-04-23 00:50:21 +0000 | [diff] [blame] | 3032 | 'more_cc': ['cc@example.com', 'more@example.com'], |
| 3033 | 'errors': [], |
| 3034 | 'notifications': [], |
| 3035 | 'warnings': [], |
Edward Lemur | 227d510 | 2020-02-25 23:45:35 +0000 | [diff] [blame] | 3036 | } |
| 3037 | gclient_utils.FileRead.return_value = json.dumps(expected_results) |
Dirk Pranke | 61bf6e8 | 2021-04-23 00:50:21 +0000 | [diff] [blame] | 3038 | git_cl.time_time.side_effect = [100, 200, 300, 400] |
Edward Lemur | 227d510 | 2020-02-25 23:45:35 +0000 | [diff] [blame] | 3039 | mockProcess = mock.Mock() |
| 3040 | mockProcess.wait.return_value = 0 |
| 3041 | subprocess2.Popen.return_value = mockProcess |
| 3042 | |
| 3043 | cl = git_cl.Changelist() |
| 3044 | results = cl.RunHook( |
| 3045 | committing=True, |
| 3046 | may_prompt=True, |
| 3047 | verbose=2, |
| 3048 | parallel=True, |
| 3049 | upstream='upstream', |
| 3050 | description='description', |
Saagar Sanghavi | 9949ab7 | 2020-07-20 20:56:40 +0000 | [diff] [blame] | 3051 | all_files=True, |
| 3052 | resultdb=False) |
Edward Lemur | 227d510 | 2020-02-25 23:45:35 +0000 | [diff] [blame] | 3053 | |
| 3054 | self.assertEqual(expected_results, results) |
Dirk Pranke | 61bf6e8 | 2021-04-23 00:50:21 +0000 | [diff] [blame] | 3055 | subprocess2.Popen.assert_any_call([ |
Edward Lemur | 227d510 | 2020-02-25 23:45:35 +0000 | [diff] [blame] | 3056 | 'vpython', 'PRESUBMIT_SUPPORT', |
Edward Lemur | 227d510 | 2020-02-25 23:45:35 +0000 | [diff] [blame] | 3057 | '--root', 'root', |
| 3058 | '--upstream', 'upstream', |
| 3059 | '--verbose', '--verbose', |
Edward Lemur | 99df04e | 2020-03-05 19:39:43 +0000 | [diff] [blame] | 3060 | '--gerrit_url', 'https://chromium-review.googlesource.com', |
Edward Lesmes | eb1bd62 | 2021-03-01 19:54:07 +0000 | [diff] [blame] | 3061 | '--gerrit_project', 'project', |
| 3062 | '--gerrit_branch', 'refs/heads/main', |
| 3063 | '--author', 'author', |
Edward Lemur | 227d510 | 2020-02-25 23:45:35 +0000 | [diff] [blame] | 3064 | '--issue', '123456', |
| 3065 | '--patchset', '7', |
Edward Lemur | 7552630 | 2020-02-27 22:31:05 +0000 | [diff] [blame] | 3066 | '--commit', |
Edward Lemur | 227d510 | 2020-02-25 23:45:35 +0000 | [diff] [blame] | 3067 | '--may_prompt', |
| 3068 | '--parallel', |
| 3069 | '--all_files', |
| 3070 | '--json_output', '/tmp/fake-temp2', |
| 3071 | '--description_file', '/tmp/fake-temp1', |
| 3072 | ]) |
Dirk Pranke | 61bf6e8 | 2021-04-23 00:50:21 +0000 | [diff] [blame] | 3073 | subprocess2.Popen.assert_any_call([ |
| 3074 | 'vpython3', 'PRESUBMIT_SUPPORT', |
| 3075 | '--root', 'root', |
| 3076 | '--upstream', 'upstream', |
| 3077 | '--verbose', '--verbose', |
| 3078 | '--gerrit_url', 'https://chromium-review.googlesource.com', |
| 3079 | '--gerrit_project', 'project', |
| 3080 | '--gerrit_branch', 'refs/heads/main', |
| 3081 | '--author', 'author', |
| 3082 | '--issue', '123456', |
| 3083 | '--patchset', '7', |
| 3084 | '--commit', |
| 3085 | '--may_prompt', |
| 3086 | '--parallel', |
| 3087 | '--all_files', |
| 3088 | '--json_output', '/tmp/fake-temp4', |
| 3089 | '--description_file', '/tmp/fake-temp3', |
| 3090 | ]) |
| 3091 | gclient_utils.FileWrite.assert_any_call( |
Edward Lemur | 1a83da1 | 2020-03-04 21:18:36 +0000 | [diff] [blame] | 3092 | '/tmp/fake-temp1', 'description') |
Edward Lemur | 227d510 | 2020-02-25 23:45:35 +0000 | [diff] [blame] | 3093 | metrics.collector.add_repeated('sub_commands', { |
| 3094 | 'command': 'presubmit', |
| 3095 | 'execution_time': 100, |
| 3096 | 'exit_code': 0, |
| 3097 | }) |
| 3098 | |
Edward Lemur | 99df04e | 2020-03-05 19:39:43 +0000 | [diff] [blame] | 3099 | def testRunHook_FewerOptions(self): |
| 3100 | expected_results = { |
Dirk Pranke | 61bf6e8 | 2021-04-23 00:50:21 +0000 | [diff] [blame] | 3101 | 'more_cc': ['cc@example.com', 'more@example.com'], |
| 3102 | 'errors': [], |
| 3103 | 'notifications': [], |
| 3104 | 'warnings': [], |
Edward Lemur | 99df04e | 2020-03-05 19:39:43 +0000 | [diff] [blame] | 3105 | } |
| 3106 | gclient_utils.FileRead.return_value = json.dumps(expected_results) |
Dirk Pranke | 61bf6e8 | 2021-04-23 00:50:21 +0000 | [diff] [blame] | 3107 | git_cl.time_time.side_effect = [100, 200, 300, 400] |
Edward Lemur | 99df04e | 2020-03-05 19:39:43 +0000 | [diff] [blame] | 3108 | mockProcess = mock.Mock() |
| 3109 | mockProcess.wait.return_value = 0 |
| 3110 | subprocess2.Popen.return_value = mockProcess |
| 3111 | |
| 3112 | git_cl.Changelist.GetAuthor.return_value = None |
| 3113 | git_cl.Changelist.GetIssue.return_value = None |
| 3114 | git_cl.Changelist.GetPatchset.return_value = None |
Edward Lemur | 99df04e | 2020-03-05 19:39:43 +0000 | [diff] [blame] | 3115 | |
| 3116 | cl = git_cl.Changelist() |
| 3117 | results = cl.RunHook( |
| 3118 | committing=False, |
| 3119 | may_prompt=False, |
| 3120 | verbose=0, |
| 3121 | parallel=False, |
| 3122 | upstream='upstream', |
| 3123 | description='description', |
Saagar Sanghavi | 9949ab7 | 2020-07-20 20:56:40 +0000 | [diff] [blame] | 3124 | all_files=False, |
| 3125 | resultdb=False) |
Edward Lemur | 99df04e | 2020-03-05 19:39:43 +0000 | [diff] [blame] | 3126 | |
| 3127 | self.assertEqual(expected_results, results) |
Dirk Pranke | 61bf6e8 | 2021-04-23 00:50:21 +0000 | [diff] [blame] | 3128 | subprocess2.Popen.assert_any_call([ |
Edward Lemur | 99df04e | 2020-03-05 19:39:43 +0000 | [diff] [blame] | 3129 | 'vpython', 'PRESUBMIT_SUPPORT', |
| 3130 | '--root', 'root', |
| 3131 | '--upstream', 'upstream', |
Edward Lesmes | eb1bd62 | 2021-03-01 19:54:07 +0000 | [diff] [blame] | 3132 | '--gerrit_url', 'https://chromium-review.googlesource.com', |
| 3133 | '--gerrit_project', 'project', |
| 3134 | '--gerrit_branch', 'refs/heads/main', |
Edward Lemur | 99df04e | 2020-03-05 19:39:43 +0000 | [diff] [blame] | 3135 | '--upload', |
| 3136 | '--json_output', '/tmp/fake-temp2', |
| 3137 | '--description_file', '/tmp/fake-temp1', |
| 3138 | ]) |
Dirk Pranke | 61bf6e8 | 2021-04-23 00:50:21 +0000 | [diff] [blame] | 3139 | gclient_utils.FileWrite.assert_any_call( |
Edward Lemur | 99df04e | 2020-03-05 19:39:43 +0000 | [diff] [blame] | 3140 | '/tmp/fake-temp1', 'description') |
| 3141 | metrics.collector.add_repeated('sub_commands', { |
| 3142 | 'command': 'presubmit', |
| 3143 | 'execution_time': 100, |
| 3144 | 'exit_code': 0, |
| 3145 | }) |
| 3146 | |
Saagar Sanghavi | 9949ab7 | 2020-07-20 20:56:40 +0000 | [diff] [blame] | 3147 | def testRunHook_FewerOptionsResultDB(self): |
| 3148 | expected_results = { |
Dirk Pranke | 61bf6e8 | 2021-04-23 00:50:21 +0000 | [diff] [blame] | 3149 | 'more_cc': ['cc@example.com', 'more@example.com'], |
| 3150 | 'errors': [], |
| 3151 | 'notifications': [], |
| 3152 | 'warnings': [], |
Saagar Sanghavi | 9949ab7 | 2020-07-20 20:56:40 +0000 | [diff] [blame] | 3153 | } |
| 3154 | gclient_utils.FileRead.return_value = json.dumps(expected_results) |
Dirk Pranke | 61bf6e8 | 2021-04-23 00:50:21 +0000 | [diff] [blame] | 3155 | git_cl.time_time.side_effect = [100, 200, 300, 400] |
Saagar Sanghavi | 9949ab7 | 2020-07-20 20:56:40 +0000 | [diff] [blame] | 3156 | mockProcess = mock.Mock() |
| 3157 | mockProcess.wait.return_value = 0 |
| 3158 | subprocess2.Popen.return_value = mockProcess |
| 3159 | |
| 3160 | git_cl.Changelist.GetAuthor.return_value = None |
| 3161 | git_cl.Changelist.GetIssue.return_value = None |
| 3162 | git_cl.Changelist.GetPatchset.return_value = None |
Saagar Sanghavi | 9949ab7 | 2020-07-20 20:56:40 +0000 | [diff] [blame] | 3163 | |
| 3164 | cl = git_cl.Changelist() |
| 3165 | results = cl.RunHook( |
| 3166 | committing=False, |
| 3167 | may_prompt=False, |
| 3168 | verbose=0, |
| 3169 | parallel=False, |
| 3170 | upstream='upstream', |
| 3171 | description='description', |
| 3172 | all_files=False, |
Saagar Sanghavi | 03b1513 | 2020-08-10 16:43:41 +0000 | [diff] [blame] | 3173 | resultdb=True, |
| 3174 | realm='chromium:public') |
Saagar Sanghavi | 9949ab7 | 2020-07-20 20:56:40 +0000 | [diff] [blame] | 3175 | |
| 3176 | self.assertEqual(expected_results, results) |
Dirk Pranke | 61bf6e8 | 2021-04-23 00:50:21 +0000 | [diff] [blame] | 3177 | subprocess2.Popen.assert_any_call([ |
Saagar Sanghavi | 03b1513 | 2020-08-10 16:43:41 +0000 | [diff] [blame] | 3178 | 'rdb', 'stream', '-new', '-realm', 'chromium:public', '--', |
Saagar Sanghavi | 9949ab7 | 2020-07-20 20:56:40 +0000 | [diff] [blame] | 3179 | 'vpython', 'PRESUBMIT_SUPPORT', |
| 3180 | '--root', 'root', |
| 3181 | '--upstream', 'upstream', |
Edward Lesmes | eb1bd62 | 2021-03-01 19:54:07 +0000 | [diff] [blame] | 3182 | '--gerrit_url', 'https://chromium-review.googlesource.com', |
| 3183 | '--gerrit_project', 'project', |
| 3184 | '--gerrit_branch', 'refs/heads/main', |
Saagar Sanghavi | 9949ab7 | 2020-07-20 20:56:40 +0000 | [diff] [blame] | 3185 | '--upload', |
| 3186 | '--json_output', '/tmp/fake-temp2', |
| 3187 | '--description_file', '/tmp/fake-temp1', |
| 3188 | ]) |
| 3189 | |
Edward Lemur | 227d510 | 2020-02-25 23:45:35 +0000 | [diff] [blame] | 3190 | @mock.patch('sys.exit', side_effect=SystemExitMock) |
| 3191 | def testRunHook_Failure(self, _mock): |
| 3192 | git_cl.time_time.side_effect = [100, 200] |
| 3193 | mockProcess = mock.Mock() |
| 3194 | mockProcess.wait.return_value = 2 |
| 3195 | subprocess2.Popen.return_value = mockProcess |
| 3196 | |
| 3197 | cl = git_cl.Changelist() |
| 3198 | with self.assertRaises(SystemExitMock): |
| 3199 | cl.RunHook( |
| 3200 | committing=True, |
| 3201 | may_prompt=True, |
| 3202 | verbose=2, |
| 3203 | parallel=True, |
| 3204 | upstream='upstream', |
| 3205 | description='description', |
Saagar Sanghavi | 9949ab7 | 2020-07-20 20:56:40 +0000 | [diff] [blame] | 3206 | all_files=True, |
| 3207 | resultdb=False) |
Edward Lemur | 227d510 | 2020-02-25 23:45:35 +0000 | [diff] [blame] | 3208 | |
| 3209 | sys.exit.assert_called_once_with(2) |
| 3210 | |
Edward Lemur | 7552630 | 2020-02-27 22:31:05 +0000 | [diff] [blame] | 3211 | def testRunPostUploadHook(self): |
| 3212 | cl = git_cl.Changelist() |
| 3213 | cl.RunPostUploadHook(2, 'upstream', 'description') |
| 3214 | |
| 3215 | subprocess2.Popen.assert_called_once_with([ |
| 3216 | 'vpython', 'PRESUBMIT_SUPPORT', |
Edward Lemur | 7552630 | 2020-02-27 22:31:05 +0000 | [diff] [blame] | 3217 | '--root', 'root', |
| 3218 | '--upstream', 'upstream', |
| 3219 | '--verbose', '--verbose', |
Edward Lemur | 99df04e | 2020-03-05 19:39:43 +0000 | [diff] [blame] | 3220 | '--gerrit_url', 'https://chromium-review.googlesource.com', |
Edward Lesmes | eb1bd62 | 2021-03-01 19:54:07 +0000 | [diff] [blame] | 3221 | '--gerrit_project', 'project', |
| 3222 | '--gerrit_branch', 'refs/heads/main', |
| 3223 | '--author', 'author', |
Edward Lemur | 7552630 | 2020-02-27 22:31:05 +0000 | [diff] [blame] | 3224 | '--issue', '123456', |
| 3225 | '--patchset', '7', |
Edward Lemur | 7552630 | 2020-02-27 22:31:05 +0000 | [diff] [blame] | 3226 | '--post_upload', |
| 3227 | '--description_file', '/tmp/fake-temp1', |
| 3228 | ]) |
| 3229 | gclient_utils.FileWrite.assert_called_once_with( |
Edward Lemur | 1a83da1 | 2020-03-04 21:18:36 +0000 | [diff] [blame] | 3230 | '/tmp/fake-temp1', 'description') |
Edward Lemur | 7552630 | 2020-02-27 22:31:05 +0000 | [diff] [blame] | 3231 | |
mlcui | 3da9171 | 2021-05-05 10:00:30 +0000 | [diff] [blame^] | 3232 | @mock.patch('git_cl.RunGit', _mock_run_git) |
| 3233 | def testDefaultTitleEmptyMessage(self): |
| 3234 | cl = git_cl.Changelist() |
| 3235 | cl.issue = 100 |
| 3236 | options = optparse.Values({ |
| 3237 | 'squash': True, |
| 3238 | 'title': None, |
| 3239 | 'message': None, |
| 3240 | 'force': None, |
| 3241 | 'skip_title': None |
| 3242 | }) |
| 3243 | |
| 3244 | mock.patch('gclient_utils.AskForData', lambda _: user_title).start() |
| 3245 | for user_title in ['', 'y', 'Y']: |
| 3246 | self.assertEqual(cl._GetTitleForUpload(options), self.LAST_COMMIT_SUBJECT) |
| 3247 | |
| 3248 | for user_title in ['not empty', 'yes', 'YES']: |
| 3249 | self.assertEqual(cl._GetTitleForUpload(options), user_title) |
| 3250 | |
Edward Lemur | 9aa1a96 | 2020-02-25 00:58:38 +0000 | [diff] [blame] | 3251 | |
Edward Lemur | baaf6be | 2019-10-09 18:00:44 +0000 | [diff] [blame] | 3252 | class CMDTestCaseBase(unittest.TestCase): |
| 3253 | _STATUSES = [ |
| 3254 | 'STATUS_UNSPECIFIED', 'SCHEDULED', 'STARTED', 'SUCCESS', 'FAILURE', |
| 3255 | 'INFRA_FAILURE', 'CANCELED', |
| 3256 | ] |
| 3257 | _CHANGE_DETAIL = { |
| 3258 | 'project': 'depot_tools', |
| 3259 | 'status': 'OPEN', |
| 3260 | 'owner': {'email': 'owner@e.mail'}, |
| 3261 | 'current_revision': 'beeeeeef', |
| 3262 | 'revisions': { |
Gavin Mak | e61ccc5 | 2020-11-13 00:12:57 +0000 | [diff] [blame] | 3263 | 'deadbeaf': { |
Sigurd Schneider | 9abde8c | 2020-11-17 08:44:52 +0000 | [diff] [blame] | 3264 | '_number': 6, |
Gavin Mak | e61ccc5 | 2020-11-13 00:12:57 +0000 | [diff] [blame] | 3265 | 'kind': 'REWORK', |
| 3266 | }, |
Edward Lemur | baaf6be | 2019-10-09 18:00:44 +0000 | [diff] [blame] | 3267 | 'beeeeeef': { |
| 3268 | '_number': 7, |
Gavin Mak | e61ccc5 | 2020-11-13 00:12:57 +0000 | [diff] [blame] | 3269 | 'kind': 'NO_CODE_CHANGE', |
Edward Lemur | baaf6be | 2019-10-09 18:00:44 +0000 | [diff] [blame] | 3270 | 'fetch': {'http': { |
| 3271 | 'url': 'https://chromium.googlesource.com/depot_tools', |
| 3272 | 'ref': 'refs/changes/56/123456/7' |
| 3273 | }}, |
| 3274 | }, |
| 3275 | }, |
| 3276 | } |
| 3277 | _DEFAULT_RESPONSE = { |
Andrii Shyshkalov | 2cbae8a | 2019-10-11 21:30:27 +0000 | [diff] [blame] | 3278 | 'builds': [{ |
| 3279 | 'id': str(100 + idx), |
| 3280 | 'builder': { |
| 3281 | 'project': 'chromium', |
| 3282 | 'bucket': 'try', |
| 3283 | 'builder': 'bot_' + status.lower(), |
| 3284 | }, |
| 3285 | 'createTime': '2019-10-09T08:00:0%d.854286Z' % (idx % 10), |
| 3286 | 'tags': [], |
| 3287 | 'status': status, |
| 3288 | } for idx, status in enumerate(_STATUSES)] |
Edward Lemur | baaf6be | 2019-10-09 18:00:44 +0000 | [diff] [blame] | 3289 | } |
| 3290 | |
Edward Lemur | 4c707a2 | 2019-09-24 21:13:43 +0000 | [diff] [blame] | 3291 | def setUp(self): |
Edward Lemur | baaf6be | 2019-10-09 18:00:44 +0000 | [diff] [blame] | 3292 | super(CMDTestCaseBase, self).setUp() |
Edward Lemur | 79d4f99 | 2019-11-11 23:49:02 +0000 | [diff] [blame] | 3293 | mock.patch('git_cl.sys.stdout', StringIO()).start() |
Edward Lemur | baaf6be | 2019-10-09 18:00:44 +0000 | [diff] [blame] | 3294 | mock.patch('git_cl.uuid.uuid4', return_value='uuid4').start() |
| 3295 | mock.patch('git_cl.Changelist.GetIssue', return_value=123456).start() |
Edward Lesmes | 7677e5c | 2020-02-19 20:39:03 +0000 | [diff] [blame] | 3296 | mock.patch( |
| 3297 | 'git_cl.Changelist.GetCodereviewServer', |
| 3298 | return_value='https://chromium-review.googlesource.com').start() |
| 3299 | mock.patch( |
Edward Lesmes | eeca9c6 | 2020-11-20 00:00:17 +0000 | [diff] [blame] | 3300 | 'git_cl.Changelist.GetGerritHost', |
Edward Lesmes | 7677e5c | 2020-02-19 20:39:03 +0000 | [diff] [blame] | 3301 | return_value='chromium-review.googlesource.com').start() |
| 3302 | mock.patch( |
| 3303 | 'git_cl.Changelist.GetMostRecentPatchset', |
| 3304 | return_value=7).start() |
| 3305 | mock.patch( |
Gavin Mak | e61ccc5 | 2020-11-13 00:12:57 +0000 | [diff] [blame] | 3306 | 'git_cl.Changelist.GetMostRecentDryRunPatchset', |
| 3307 | return_value=6).start() |
| 3308 | mock.patch( |
Edward Lesmes | 7677e5c | 2020-02-19 20:39:03 +0000 | [diff] [blame] | 3309 | 'git_cl.Changelist.GetRemoteUrl', |
| 3310 | return_value='https://chromium.googlesource.com/depot_tools').start() |
| 3311 | mock.patch( |
| 3312 | 'auth.Authenticator', |
| 3313 | return_value=AuthenticatorMock()).start() |
| 3314 | mock.patch( |
| 3315 | 'gerrit_util.GetChangeDetail', |
| 3316 | return_value=self._CHANGE_DETAIL).start() |
| 3317 | mock.patch( |
| 3318 | 'git_cl._call_buildbucket', |
| 3319 | return_value = self._DEFAULT_RESPONSE).start() |
Edward Lemur | baaf6be | 2019-10-09 18:00:44 +0000 | [diff] [blame] | 3320 | mock.patch('git_common.is_dirty_git_tree', return_value=False).start() |
Edward Lemur | 4c707a2 | 2019-09-24 21:13:43 +0000 | [diff] [blame] | 3321 | self.addCleanup(mock.patch.stopall) |
| 3322 | |
Edward Lemur | 4c707a2 | 2019-09-24 21:13:43 +0000 | [diff] [blame] | 3323 | |
Edward Lemur | 9468eba | 2020-02-27 19:07:22 +0000 | [diff] [blame] | 3324 | class CMDPresubmitTestCase(CMDTestCaseBase): |
| 3325 | def setUp(self): |
| 3326 | super(CMDPresubmitTestCase, self).setUp() |
| 3327 | mock.patch( |
| 3328 | 'git_cl.Changelist.GetCommonAncestorWithUpstream', |
| 3329 | return_value='upstream').start() |
| 3330 | mock.patch( |
| 3331 | 'git_cl.Changelist.FetchDescription', |
| 3332 | return_value='fetch description').start() |
| 3333 | mock.patch( |
Edward Lemur | a12175c | 2020-03-09 16:58:26 +0000 | [diff] [blame] | 3334 | 'git_cl._create_description_from_log', |
Edward Lemur | 9468eba | 2020-02-27 19:07:22 +0000 | [diff] [blame] | 3335 | return_value='get description').start() |
| 3336 | mock.patch('git_cl.Changelist.RunHook').start() |
| 3337 | |
| 3338 | def testDefaultCase(self): |
| 3339 | self.assertEqual(0, git_cl.main(['presubmit'])) |
| 3340 | git_cl.Changelist.RunHook.assert_called_once_with( |
| 3341 | committing=True, |
| 3342 | may_prompt=False, |
| 3343 | verbose=0, |
| 3344 | parallel=None, |
| 3345 | upstream='upstream', |
| 3346 | description='fetch description', |
Saagar Sanghavi | 9949ab7 | 2020-07-20 20:56:40 +0000 | [diff] [blame] | 3347 | all_files=None, |
Saagar Sanghavi | 03b1513 | 2020-08-10 16:43:41 +0000 | [diff] [blame] | 3348 | resultdb=None, |
| 3349 | realm=None) |
Edward Lemur | 9468eba | 2020-02-27 19:07:22 +0000 | [diff] [blame] | 3350 | |
| 3351 | def testNoIssue(self): |
| 3352 | git_cl.Changelist.GetIssue.return_value = None |
| 3353 | self.assertEqual(0, git_cl.main(['presubmit'])) |
| 3354 | git_cl.Changelist.RunHook.assert_called_once_with( |
| 3355 | committing=True, |
| 3356 | may_prompt=False, |
| 3357 | verbose=0, |
| 3358 | parallel=None, |
| 3359 | upstream='upstream', |
| 3360 | description='get description', |
Saagar Sanghavi | 9949ab7 | 2020-07-20 20:56:40 +0000 | [diff] [blame] | 3361 | all_files=None, |
Saagar Sanghavi | 03b1513 | 2020-08-10 16:43:41 +0000 | [diff] [blame] | 3362 | resultdb=None, |
| 3363 | realm=None) |
Edward Lemur | 9468eba | 2020-02-27 19:07:22 +0000 | [diff] [blame] | 3364 | |
| 3365 | def testCustomBranch(self): |
| 3366 | self.assertEqual(0, git_cl.main(['presubmit', 'custom_branch'])) |
| 3367 | git_cl.Changelist.RunHook.assert_called_once_with( |
| 3368 | committing=True, |
| 3369 | may_prompt=False, |
| 3370 | verbose=0, |
| 3371 | parallel=None, |
| 3372 | upstream='custom_branch', |
| 3373 | description='fetch description', |
Saagar Sanghavi | 9949ab7 | 2020-07-20 20:56:40 +0000 | [diff] [blame] | 3374 | all_files=None, |
Saagar Sanghavi | 03b1513 | 2020-08-10 16:43:41 +0000 | [diff] [blame] | 3375 | resultdb=None, |
| 3376 | realm=None) |
Edward Lemur | 9468eba | 2020-02-27 19:07:22 +0000 | [diff] [blame] | 3377 | |
| 3378 | def testOptions(self): |
| 3379 | self.assertEqual( |
Saagar Sanghavi | 9949ab7 | 2020-07-20 20:56:40 +0000 | [diff] [blame] | 3380 | 0, git_cl.main(['presubmit', '-v', '-v', '--all', '--parallel', '-u', |
Saagar Sanghavi | 03b1513 | 2020-08-10 16:43:41 +0000 | [diff] [blame] | 3381 | '--resultdb', '--realm', 'chromium:public'])) |
Edward Lemur | 9468eba | 2020-02-27 19:07:22 +0000 | [diff] [blame] | 3382 | git_cl.Changelist.RunHook.assert_called_once_with( |
| 3383 | committing=False, |
| 3384 | may_prompt=False, |
| 3385 | verbose=2, |
| 3386 | parallel=True, |
| 3387 | upstream='upstream', |
| 3388 | description='fetch description', |
Saagar Sanghavi | 9949ab7 | 2020-07-20 20:56:40 +0000 | [diff] [blame] | 3389 | all_files=True, |
Saagar Sanghavi | 03b1513 | 2020-08-10 16:43:41 +0000 | [diff] [blame] | 3390 | resultdb=True, |
| 3391 | realm='chromium:public') |
Edward Lemur | 9468eba | 2020-02-27 19:07:22 +0000 | [diff] [blame] | 3392 | |
Edward Lemur | baaf6be | 2019-10-09 18:00:44 +0000 | [diff] [blame] | 3393 | class CMDTryResultsTestCase(CMDTestCaseBase): |
| 3394 | _DEFAULT_REQUEST = { |
| 3395 | 'predicate': { |
Andrii Shyshkalov | 2cbae8a | 2019-10-11 21:30:27 +0000 | [diff] [blame] | 3396 | "gerritChanges": [{ |
| 3397 | "project": "depot_tools", |
| 3398 | "host": "chromium-review.googlesource.com", |
Gavin Mak | e61ccc5 | 2020-11-13 00:12:57 +0000 | [diff] [blame] | 3399 | "patchset": 6, |
| 3400 | "change": 123456, |
| 3401 | }], |
| 3402 | }, |
| 3403 | 'fields': ('builds.*.id,builds.*.builder,builds.*.status' + |
| 3404 | ',builds.*.createTime,builds.*.tags'), |
| 3405 | } |
| 3406 | |
| 3407 | _TRIVIAL_REQUEST = { |
| 3408 | 'predicate': { |
| 3409 | "gerritChanges": [{ |
| 3410 | "project": "depot_tools", |
| 3411 | "host": "chromium-review.googlesource.com", |
Andrii Shyshkalov | 2cbae8a | 2019-10-11 21:30:27 +0000 | [diff] [blame] | 3412 | "patchset": 7, |
| 3413 | "change": 123456, |
| 3414 | }], |
Edward Lemur | baaf6be | 2019-10-09 18:00:44 +0000 | [diff] [blame] | 3415 | }, |
Andrii Shyshkalov | 2cbae8a | 2019-10-11 21:30:27 +0000 | [diff] [blame] | 3416 | 'fields': ('builds.*.id,builds.*.builder,builds.*.status' + |
| 3417 | ',builds.*.createTime,builds.*.tags'), |
Edward Lemur | baaf6be | 2019-10-09 18:00:44 +0000 | [diff] [blame] | 3418 | } |
| 3419 | |
| 3420 | def testNoJobs(self): |
| 3421 | git_cl._call_buildbucket.return_value = {} |
| 3422 | |
| 3423 | self.assertEqual(0, git_cl.main(['try-results'])) |
| 3424 | self.assertEqual('No tryjobs scheduled.\n', sys.stdout.getvalue()) |
| 3425 | git_cl._call_buildbucket.assert_called_once_with( |
| 3426 | mock.ANY, 'cr-buildbucket.appspot.com', 'SearchBuilds', |
| 3427 | self._DEFAULT_REQUEST) |
| 3428 | |
Gavin Mak | e61ccc5 | 2020-11-13 00:12:57 +0000 | [diff] [blame] | 3429 | def testTrivialCommits(self): |
| 3430 | self.assertEqual(0, git_cl.main(['try-results'])) |
| 3431 | git_cl._call_buildbucket.assert_called_with( |
| 3432 | mock.ANY, 'cr-buildbucket.appspot.com', 'SearchBuilds', |
| 3433 | self._DEFAULT_REQUEST) |
| 3434 | |
| 3435 | git_cl._call_buildbucket.return_value = {} |
| 3436 | self.assertEqual(0, git_cl.main(['try-results', '--patchset', '7'])) |
| 3437 | git_cl._call_buildbucket.assert_called_with( |
| 3438 | mock.ANY, 'cr-buildbucket.appspot.com', 'SearchBuilds', |
| 3439 | self._TRIVIAL_REQUEST) |
| 3440 | self.assertEqual([ |
| 3441 | 'Successes:', |
| 3442 | ' bot_success https://ci.chromium.org/b/103', |
| 3443 | 'Infra Failures:', |
| 3444 | ' bot_infra_failure https://ci.chromium.org/b/105', |
| 3445 | 'Failures:', |
| 3446 | ' bot_failure https://ci.chromium.org/b/104', |
| 3447 | 'Canceled:', |
| 3448 | ' bot_canceled ', |
| 3449 | 'Started:', |
| 3450 | ' bot_started https://ci.chromium.org/b/102', |
| 3451 | 'Scheduled:', |
| 3452 | ' bot_scheduled id=101', |
| 3453 | 'Other:', |
| 3454 | ' bot_status_unspecified id=100', |
| 3455 | 'Total: 7 tryjobs', |
| 3456 | 'No tryjobs scheduled.', |
| 3457 | ], sys.stdout.getvalue().splitlines()) |
| 3458 | |
Edward Lemur | baaf6be | 2019-10-09 18:00:44 +0000 | [diff] [blame] | 3459 | def testPrintToStdout(self): |
| 3460 | self.assertEqual(0, git_cl.main(['try-results'])) |
| 3461 | self.assertEqual([ |
| 3462 | 'Successes:', |
| 3463 | ' bot_success https://ci.chromium.org/b/103', |
| 3464 | 'Infra Failures:', |
| 3465 | ' bot_infra_failure https://ci.chromium.org/b/105', |
| 3466 | 'Failures:', |
| 3467 | ' bot_failure https://ci.chromium.org/b/104', |
| 3468 | 'Canceled:', |
| 3469 | ' bot_canceled ', |
| 3470 | 'Started:', |
| 3471 | ' bot_started https://ci.chromium.org/b/102', |
| 3472 | 'Scheduled:', |
| 3473 | ' bot_scheduled id=101', |
| 3474 | 'Other:', |
| 3475 | ' bot_status_unspecified id=100', |
| 3476 | 'Total: 7 tryjobs', |
| 3477 | ], sys.stdout.getvalue().splitlines()) |
| 3478 | git_cl._call_buildbucket.assert_called_once_with( |
| 3479 | mock.ANY, 'cr-buildbucket.appspot.com', 'SearchBuilds', |
| 3480 | self._DEFAULT_REQUEST) |
| 3481 | |
| 3482 | def testPrintToStdoutWithMasters(self): |
| 3483 | self.assertEqual(0, git_cl.main(['try-results', '--print-master'])) |
| 3484 | self.assertEqual([ |
| 3485 | 'Successes:', |
| 3486 | ' try bot_success https://ci.chromium.org/b/103', |
| 3487 | 'Infra Failures:', |
| 3488 | ' try bot_infra_failure https://ci.chromium.org/b/105', |
| 3489 | 'Failures:', |
| 3490 | ' try bot_failure https://ci.chromium.org/b/104', |
| 3491 | 'Canceled:', |
| 3492 | ' try bot_canceled ', |
| 3493 | 'Started:', |
| 3494 | ' try bot_started https://ci.chromium.org/b/102', |
| 3495 | 'Scheduled:', |
| 3496 | ' try bot_scheduled id=101', |
| 3497 | 'Other:', |
| 3498 | ' try bot_status_unspecified id=100', |
| 3499 | 'Total: 7 tryjobs', |
| 3500 | ], sys.stdout.getvalue().splitlines()) |
| 3501 | git_cl._call_buildbucket.assert_called_once_with( |
| 3502 | mock.ANY, 'cr-buildbucket.appspot.com', 'SearchBuilds', |
| 3503 | self._DEFAULT_REQUEST) |
| 3504 | |
| 3505 | @mock.patch('git_cl.write_json') |
| 3506 | def testWriteToJson(self, mockJsonDump): |
| 3507 | self.assertEqual(0, git_cl.main(['try-results', '--json', 'file.json'])) |
| 3508 | git_cl._call_buildbucket.assert_called_once_with( |
| 3509 | mock.ANY, 'cr-buildbucket.appspot.com', 'SearchBuilds', |
| 3510 | self._DEFAULT_REQUEST) |
| 3511 | mockJsonDump.assert_called_once_with( |
| 3512 | 'file.json', self._DEFAULT_RESPONSE['builds']) |
| 3513 | |
Andrii Shyshkalov | 2cbae8a | 2019-10-11 21:30:27 +0000 | [diff] [blame] | 3514 | def test_filter_failed_for_one_simple(self): |
Edward Lemur | 4576851 | 2020-03-02 19:03:14 +0000 | [diff] [blame] | 3515 | self.assertEqual([], git_cl._filter_failed_for_retry([])) |
| 3516 | self.assertEqual( |
| 3517 | [ |
| 3518 | ('chromium', 'try', 'bot_failure'), |
| 3519 | ('chromium', 'try', 'bot_infra_failure'), |
| 3520 | ], |
| 3521 | git_cl._filter_failed_for_retry(self._DEFAULT_RESPONSE['builds'])) |
Andrii Shyshkalov | 2cbae8a | 2019-10-11 21:30:27 +0000 | [diff] [blame] | 3522 | |
| 3523 | def test_filter_failed_for_retry_many_builds(self): |
| 3524 | |
| 3525 | def _build(name, created_sec, status, experimental=False): |
| 3526 | assert 0 <= created_sec < 100, created_sec |
| 3527 | b = { |
| 3528 | 'id': 112112, |
| 3529 | 'builder': { |
| 3530 | 'project': 'chromium', |
| 3531 | 'bucket': 'try', |
| 3532 | 'builder': name, |
| 3533 | }, |
| 3534 | 'createTime': '2019-10-09T08:00:%02d.854286Z' % created_sec, |
| 3535 | 'status': status, |
| 3536 | 'tags': [], |
| 3537 | } |
| 3538 | if experimental: |
| 3539 | b['tags'].append({'key': 'cq_experimental', 'value': 'true'}) |
| 3540 | return b |
| 3541 | |
| 3542 | builds = [ |
| 3543 | _build('flaky-last-green', 1, 'FAILURE'), |
| 3544 | _build('flaky-last-green', 2, 'SUCCESS'), |
| 3545 | _build('flaky', 1, 'SUCCESS'), |
| 3546 | _build('flaky', 2, 'FAILURE'), |
| 3547 | _build('running', 1, 'FAILED'), |
| 3548 | _build('running', 2, 'SCHEDULED'), |
| 3549 | _build('yep-still-running', 1, 'STARTED'), |
| 3550 | _build('yep-still-running', 2, 'FAILURE'), |
| 3551 | _build('cq-experimental', 1, 'SUCCESS', experimental=True), |
| 3552 | _build('cq-experimental', 2, 'FAILURE', experimental=True), |
| 3553 | |
| 3554 | # Simulate experimental in CQ builder, which developer decided |
| 3555 | # to retry manually which resulted in 2nd build non-experimental. |
| 3556 | _build('sometimes-experimental', 1, 'FAILURE', experimental=True), |
| 3557 | _build('sometimes-experimental', 2, 'FAILURE', experimental=False), |
| 3558 | ] |
| 3559 | builds.sort(key=lambda b: b['status']) # ~deterministic shuffle. |
Edward Lemur | 4576851 | 2020-03-02 19:03:14 +0000 | [diff] [blame] | 3560 | self.assertEqual( |
| 3561 | [ |
| 3562 | ('chromium', 'try', 'flaky'), |
| 3563 | ('chromium', 'try', 'sometimes-experimental'), |
| 3564 | ], |
| 3565 | git_cl._filter_failed_for_retry(builds)) |
Edward Lemur | baaf6be | 2019-10-09 18:00:44 +0000 | [diff] [blame] | 3566 | |
| 3567 | |
| 3568 | class CMDTryTestCase(CMDTestCaseBase): |
| 3569 | |
| 3570 | @mock.patch('git_cl.Changelist.SetCQState') |
Edward Lemur | 4576851 | 2020-03-02 19:03:14 +0000 | [diff] [blame] | 3571 | def testSetCQDryRunByDefault(self, mockSetCQState): |
Edward Lemur | baaf6be | 2019-10-09 18:00:44 +0000 | [diff] [blame] | 3572 | mockSetCQState.return_value = 0 |
Edward Lemur | 4c707a2 | 2019-09-24 21:13:43 +0000 | [diff] [blame] | 3573 | self.assertEqual(0, git_cl.main(['try'])) |
| 3574 | git_cl.Changelist.SetCQState.assert_called_with(git_cl._CQState.DRY_RUN) |
| 3575 | self.assertEqual( |
| 3576 | sys.stdout.getvalue(), |
| 3577 | 'Scheduling CQ dry run on: ' |
| 3578 | 'https://chromium-review.googlesource.com/123456\n') |
| 3579 | |
Edward Lemur | 4c707a2 | 2019-09-24 21:13:43 +0000 | [diff] [blame] | 3580 | @mock.patch('git_cl._call_buildbucket') |
Edward Lemur | baaf6be | 2019-10-09 18:00:44 +0000 | [diff] [blame] | 3581 | def testScheduleOnBuildbucket(self, mockCallBuildbucket): |
Edward Lemur | 4c707a2 | 2019-09-24 21:13:43 +0000 | [diff] [blame] | 3582 | mockCallBuildbucket.return_value = {} |
Edward Lemur | 4c707a2 | 2019-09-24 21:13:43 +0000 | [diff] [blame] | 3583 | |
| 3584 | self.assertEqual(0, git_cl.main([ |
| 3585 | 'try', '-B', 'luci.chromium.try', '-b', 'win', |
| 3586 | '-p', 'key=val', '-p', 'json=[{"a":1}, null]'])) |
| 3587 | self.assertIn( |
Edward Lemur | 4576851 | 2020-03-02 19:03:14 +0000 | [diff] [blame] | 3588 | 'Scheduling jobs on:\n' |
| 3589 | ' chromium/try: win', |
Edward Lemur | 4c707a2 | 2019-09-24 21:13:43 +0000 | [diff] [blame] | 3590 | git_cl.sys.stdout.getvalue()) |
| 3591 | |
| 3592 | expected_request = { |
| 3593 | "requests": [{ |
| 3594 | "scheduleBuild": { |
| 3595 | "requestId": "uuid4", |
| 3596 | "builder": { |
| 3597 | "project": "chromium", |
| 3598 | "builder": "win", |
| 3599 | "bucket": "try", |
| 3600 | }, |
| 3601 | "gerritChanges": [{ |
| 3602 | "project": "depot_tools", |
| 3603 | "host": "chromium-review.googlesource.com", |
| 3604 | "patchset": 7, |
| 3605 | "change": 123456, |
| 3606 | }], |
| 3607 | "properties": { |
| 3608 | "category": "git_cl_try", |
| 3609 | "json": [{"a": 1}, None], |
| 3610 | "key": "val", |
| 3611 | }, |
| 3612 | "tags": [ |
| 3613 | {"value": "win", "key": "builder"}, |
| 3614 | {"value": "git_cl_try", "key": "user_agent"}, |
| 3615 | ], |
| 3616 | }, |
| 3617 | }], |
| 3618 | } |
| 3619 | mockCallBuildbucket.assert_called_with( |
| 3620 | mock.ANY, 'cr-buildbucket.appspot.com', 'Batch', expected_request) |
| 3621 | |
Anthony Polito | 1a5fe23 | 2020-01-24 23:17:52 +0000 | [diff] [blame] | 3622 | @mock.patch('git_cl._call_buildbucket') |
| 3623 | def testScheduleOnBuildbucketWithRevision(self, mockCallBuildbucket): |
| 3624 | mockCallBuildbucket.return_value = {} |
Josip Sokcevic | 9011a5b | 2021-02-12 18:59:44 +0000 | [diff] [blame] | 3625 | mock.patch('git_cl.Changelist.GetRemoteBranch', |
| 3626 | return_value=('origin', 'refs/remotes/origin/main')).start() |
Anthony Polito | 1a5fe23 | 2020-01-24 23:17:52 +0000 | [diff] [blame] | 3627 | |
| 3628 | self.assertEqual(0, git_cl.main([ |
| 3629 | 'try', '-B', 'luci.chromium.try', '-b', 'win', '-b', 'linux', |
| 3630 | '-p', 'key=val', '-p', 'json=[{"a":1}, null]', |
| 3631 | '-r', 'beeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeef'])) |
| 3632 | self.assertIn( |
Edward Lemur | 4576851 | 2020-03-02 19:03:14 +0000 | [diff] [blame] | 3633 | 'Scheduling jobs on:\n' |
| 3634 | ' chromium/try: linux\n' |
| 3635 | ' chromium/try: win', |
Anthony Polito | 1a5fe23 | 2020-01-24 23:17:52 +0000 | [diff] [blame] | 3636 | git_cl.sys.stdout.getvalue()) |
| 3637 | |
| 3638 | expected_request = { |
| 3639 | "requests": [{ |
| 3640 | "scheduleBuild": { |
| 3641 | "requestId": "uuid4", |
| 3642 | "builder": { |
| 3643 | "project": "chromium", |
| 3644 | "builder": "linux", |
| 3645 | "bucket": "try", |
| 3646 | }, |
| 3647 | "gerritChanges": [{ |
| 3648 | "project": "depot_tools", |
| 3649 | "host": "chromium-review.googlesource.com", |
| 3650 | "patchset": 7, |
| 3651 | "change": 123456, |
| 3652 | }], |
| 3653 | "properties": { |
| 3654 | "category": "git_cl_try", |
| 3655 | "json": [{"a": 1}, None], |
| 3656 | "key": "val", |
| 3657 | }, |
| 3658 | "tags": [ |
| 3659 | {"value": "linux", "key": "builder"}, |
| 3660 | {"value": "git_cl_try", "key": "user_agent"}, |
| 3661 | ], |
| 3662 | "gitilesCommit": { |
| 3663 | "host": "chromium-review.googlesource.com", |
| 3664 | "project": "depot_tools", |
| 3665 | "id": "beeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeef", |
Josip Sokcevic | 9011a5b | 2021-02-12 18:59:44 +0000 | [diff] [blame] | 3666 | "ref": "refs/heads/main", |
Anthony Polito | 1a5fe23 | 2020-01-24 23:17:52 +0000 | [diff] [blame] | 3667 | } |
| 3668 | }, |
| 3669 | }, |
| 3670 | { |
| 3671 | "scheduleBuild": { |
| 3672 | "requestId": "uuid4", |
| 3673 | "builder": { |
| 3674 | "project": "chromium", |
| 3675 | "builder": "win", |
| 3676 | "bucket": "try", |
| 3677 | }, |
| 3678 | "gerritChanges": [{ |
| 3679 | "project": "depot_tools", |
| 3680 | "host": "chromium-review.googlesource.com", |
| 3681 | "patchset": 7, |
| 3682 | "change": 123456, |
| 3683 | }], |
| 3684 | "properties": { |
| 3685 | "category": "git_cl_try", |
| 3686 | "json": [{"a": 1}, None], |
| 3687 | "key": "val", |
| 3688 | }, |
| 3689 | "tags": [ |
| 3690 | {"value": "win", "key": "builder"}, |
| 3691 | {"value": "git_cl_try", "key": "user_agent"}, |
| 3692 | ], |
| 3693 | "gitilesCommit": { |
| 3694 | "host": "chromium-review.googlesource.com", |
| 3695 | "project": "depot_tools", |
| 3696 | "id": "beeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeef", |
Josip Sokcevic | 9011a5b | 2021-02-12 18:59:44 +0000 | [diff] [blame] | 3697 | "ref": "refs/heads/main", |
Anthony Polito | 1a5fe23 | 2020-01-24 23:17:52 +0000 | [diff] [blame] | 3698 | } |
| 3699 | }, |
| 3700 | }], |
| 3701 | } |
| 3702 | mockCallBuildbucket.assert_called_with( |
| 3703 | mock.ANY, 'cr-buildbucket.appspot.com', 'Batch', expected_request) |
| 3704 | |
Edward Lemur | 4576851 | 2020-03-02 19:03:14 +0000 | [diff] [blame] | 3705 | @mock.patch('sys.stderr', StringIO()) |
Edward Lemur | baaf6be | 2019-10-09 18:00:44 +0000 | [diff] [blame] | 3706 | def testScheduleOnBuildbucket_WrongBucket(self): |
Edward Lemur | 4576851 | 2020-03-02 19:03:14 +0000 | [diff] [blame] | 3707 | with self.assertRaises(SystemExit): |
| 3708 | git_cl.main([ |
| 3709 | 'try', '-B', 'not-a-bucket', '-b', 'win', |
| 3710 | '-p', 'key=val', '-p', 'json=[{"a":1}, null]']) |
Edward Lemur | 4c707a2 | 2019-09-24 21:13:43 +0000 | [diff] [blame] | 3711 | self.assertIn( |
Edward Lemur | 4576851 | 2020-03-02 19:03:14 +0000 | [diff] [blame] | 3712 | 'Invalid bucket: not-a-bucket.', |
| 3713 | sys.stderr.getvalue()) |
Edward Lemur | 4c707a2 | 2019-09-24 21:13:43 +0000 | [diff] [blame] | 3714 | |
Andrii Shyshkalov | aeee6a8 | 2019-10-09 21:56:25 +0000 | [diff] [blame] | 3715 | @mock.patch('git_cl._call_buildbucket') |
Quinten Yearsley | 777660f | 2020-03-04 23:37:06 +0000 | [diff] [blame] | 3716 | @mock.patch('git_cl._fetch_tryjobs') |
Andrii Shyshkalov | aeee6a8 | 2019-10-09 21:56:25 +0000 | [diff] [blame] | 3717 | def testScheduleOnBuildbucketRetryFailed( |
| 3718 | self, mockFetchTryJobs, mockCallBuildbucket): |
Quinten Yearsley | 777660f | 2020-03-04 23:37:06 +0000 | [diff] [blame] | 3719 | git_cl._fetch_tryjobs.side_effect = lambda *_, **kw: { |
Andrii Shyshkalov | aeee6a8 | 2019-10-09 21:56:25 +0000 | [diff] [blame] | 3720 | 7: [], |
| 3721 | 6: [{ |
| 3722 | 'id': 112112, |
| 3723 | 'builder': { |
| 3724 | 'project': 'chromium', |
| 3725 | 'bucket': 'try', |
Quinten Yearsley | 777660f | 2020-03-04 23:37:06 +0000 | [diff] [blame] | 3726 | 'builder': 'linux', }, |
Andrii Shyshkalov | 2cbae8a | 2019-10-11 21:30:27 +0000 | [diff] [blame] | 3727 | 'createTime': '2019-10-09T08:00:01.854286Z', |
| 3728 | 'tags': [], |
Quinten Yearsley | 777660f | 2020-03-04 23:37:06 +0000 | [diff] [blame] | 3729 | 'status': 'FAILURE', }], }[kw['patchset']] |
Andrii Shyshkalov | aeee6a8 | 2019-10-09 21:56:25 +0000 | [diff] [blame] | 3730 | mockCallBuildbucket.return_value = {} |
| 3731 | |
| 3732 | self.assertEqual(0, git_cl.main(['try', '--retry-failed'])) |
| 3733 | self.assertIn( |
Edward Lemur | 4576851 | 2020-03-02 19:03:14 +0000 | [diff] [blame] | 3734 | 'Scheduling jobs on:\n' |
| 3735 | ' chromium/try: linux', |
Andrii Shyshkalov | aeee6a8 | 2019-10-09 21:56:25 +0000 | [diff] [blame] | 3736 | git_cl.sys.stdout.getvalue()) |
| 3737 | |
| 3738 | expected_request = { |
| 3739 | "requests": [{ |
| 3740 | "scheduleBuild": { |
| 3741 | "requestId": "uuid4", |
| 3742 | "builder": { |
| 3743 | "project": "chromium", |
| 3744 | "bucket": "try", |
| 3745 | "builder": "linux", |
| 3746 | }, |
| 3747 | "gerritChanges": [{ |
| 3748 | "project": "depot_tools", |
| 3749 | "host": "chromium-review.googlesource.com", |
| 3750 | "patchset": 7, |
| 3751 | "change": 123456, |
| 3752 | }], |
| 3753 | "properties": { |
| 3754 | "category": "git_cl_try", |
| 3755 | }, |
| 3756 | "tags": [ |
| 3757 | {"value": "linux", "key": "builder"}, |
| 3758 | {"value": "git_cl_try", "key": "user_agent"}, |
| 3759 | {"value": "1", "key": "retry_failed"}, |
| 3760 | ], |
| 3761 | }, |
| 3762 | }], |
| 3763 | } |
| 3764 | mockCallBuildbucket.assert_called_with( |
| 3765 | mock.ANY, 'cr-buildbucket.appspot.com', 'Batch', expected_request) |
| 3766 | |
Edward Lemur | 4c707a2 | 2019-09-24 21:13:43 +0000 | [diff] [blame] | 3767 | def test_parse_bucket(self): |
| 3768 | test_cases = [ |
| 3769 | { |
| 3770 | 'bucket': 'chromium/try', |
| 3771 | 'result': ('chromium', 'try'), |
| 3772 | }, |
| 3773 | { |
| 3774 | 'bucket': 'luci.chromium.try', |
| 3775 | 'result': ('chromium', 'try'), |
| 3776 | 'has_warning': True, |
| 3777 | }, |
| 3778 | { |
| 3779 | 'bucket': 'skia.primary', |
| 3780 | 'result': ('skia', 'skia.primary'), |
| 3781 | 'has_warning': True, |
| 3782 | }, |
| 3783 | { |
| 3784 | 'bucket': 'not-a-bucket', |
| 3785 | 'result': (None, None), |
| 3786 | }, |
| 3787 | ] |
| 3788 | |
| 3789 | for test_case in test_cases: |
| 3790 | git_cl.sys.stdout.truncate(0) |
| 3791 | self.assertEqual( |
| 3792 | test_case['result'], git_cl._parse_bucket(test_case['bucket'])) |
| 3793 | if test_case.get('has_warning'): |
Edward Lemur | 6215c79 | 2019-10-03 21:59:05 +0000 | [diff] [blame] | 3794 | expected_warning = 'WARNING Please use %s/%s to specify the bucket' % ( |
| 3795 | test_case['result']) |
| 3796 | self.assertIn(expected_warning, git_cl.sys.stdout.getvalue()) |
Edward Lemur | 4c707a2 | 2019-09-24 21:13:43 +0000 | [diff] [blame] | 3797 | |
| 3798 | |
Edward Lemur | baaf6be | 2019-10-09 18:00:44 +0000 | [diff] [blame] | 3799 | class CMDUploadTestCase(CMDTestCaseBase): |
Quinten Yearsley | ee8be8a | 2020-03-05 21:48:32 +0000 | [diff] [blame] | 3800 | |
Quinten Yearsley | a19d353 | 2019-09-30 21:54:39 +0000 | [diff] [blame] | 3801 | def setUp(self): |
| 3802 | super(CMDUploadTestCase, self).setUp() |
Quinten Yearsley | 777660f | 2020-03-04 23:37:06 +0000 | [diff] [blame] | 3803 | mock.patch('git_cl._fetch_tryjobs').start() |
| 3804 | mock.patch('git_cl._trigger_tryjobs', return_value={}).start() |
Edward Lemur | baaf6be | 2019-10-09 18:00:44 +0000 | [diff] [blame] | 3805 | mock.patch('git_cl.Changelist.CMDUpload', return_value=0).start() |
Edward Lesmes | 0dd5482 | 2020-03-26 18:24:25 +0000 | [diff] [blame] | 3806 | mock.patch('git_cl.Settings.GetRoot', return_value='').start() |
| 3807 | mock.patch( |
| 3808 | 'git_cl.Settings.GetSquashGerritUploads', |
| 3809 | return_value=True).start() |
Quinten Yearsley | a19d353 | 2019-09-30 21:54:39 +0000 | [diff] [blame] | 3810 | self.addCleanup(mock.patch.stopall) |
| 3811 | |
Edward Lesmes | 7677e5c | 2020-02-19 20:39:03 +0000 | [diff] [blame] | 3812 | def testWarmUpChangeDetailCache(self): |
| 3813 | self.assertEqual(0, git_cl.main(['upload'])) |
| 3814 | gerrit_util.GetChangeDetail.assert_called_once_with( |
| 3815 | 'chromium-review.googlesource.com', 'depot_tools~123456', |
| 3816 | frozenset([ |
| 3817 | 'LABELS', 'CURRENT_REVISION', 'DETAILED_ACCOUNTS', |
| 3818 | 'CURRENT_COMMIT'])) |
| 3819 | |
Edward Lemur | baaf6be | 2019-10-09 18:00:44 +0000 | [diff] [blame] | 3820 | def testUploadRetryFailed(self): |
Quinten Yearsley | a19d353 | 2019-09-30 21:54:39 +0000 | [diff] [blame] | 3821 | # This test mocks out the actual upload part, and just asserts that after |
| 3822 | # upload, if --retry-failed is added, then the tool will fetch try jobs |
| 3823 | # from the previous patchset and trigger the right builders on the latest |
| 3824 | # patchset. |
Quinten Yearsley | 777660f | 2020-03-04 23:37:06 +0000 | [diff] [blame] | 3825 | git_cl._fetch_tryjobs.side_effect = [ |
Edward Lemur | baaf6be | 2019-10-09 18:00:44 +0000 | [diff] [blame] | 3826 | # Latest patchset: No builds. |
| 3827 | [], |
| 3828 | # Patchset before latest: Some builds. |
Andrii Shyshkalov | 2cbae8a | 2019-10-11 21:30:27 +0000 | [diff] [blame] | 3829 | [{ |
| 3830 | 'id': str(100 + idx), |
| 3831 | 'builder': { |
| 3832 | 'project': 'chromium', |
| 3833 | 'bucket': 'try', |
| 3834 | 'builder': 'bot_' + status.lower(), |
| 3835 | }, |
| 3836 | 'createTime': '2019-10-09T08:00:0%d.854286Z' % (idx % 10), |
| 3837 | 'tags': [], |
| 3838 | 'status': status, |
| 3839 | } for idx, status in enumerate(self._STATUSES)], |
Andrii Shyshkalov | 1ad5811 | 2019-10-08 01:46:14 +0000 | [diff] [blame] | 3840 | ] |
Edward Lemur | baaf6be | 2019-10-09 18:00:44 +0000 | [diff] [blame] | 3841 | |
Quinten Yearsley | a19d353 | 2019-09-30 21:54:39 +0000 | [diff] [blame] | 3842 | self.assertEqual(0, git_cl.main(['upload', '--retry-failed'])) |
Edward Lemur | baaf6be | 2019-10-09 18:00:44 +0000 | [diff] [blame] | 3843 | self.assertEqual([ |
Edward Lemur | 5b929a4 | 2019-10-21 17:57:39 +0000 | [diff] [blame] | 3844 | mock.call(mock.ANY, 'cr-buildbucket.appspot.com', patchset=7), |
| 3845 | mock.call(mock.ANY, 'cr-buildbucket.appspot.com', patchset=6), |
Quinten Yearsley | 777660f | 2020-03-04 23:37:06 +0000 | [diff] [blame] | 3846 | ], git_cl._fetch_tryjobs.mock_calls) |
Edward Lemur | 4576851 | 2020-03-02 19:03:14 +0000 | [diff] [blame] | 3847 | expected_buckets = [ |
| 3848 | ('chromium', 'try', 'bot_failure'), |
| 3849 | ('chromium', 'try', 'bot_infra_failure'), |
| 3850 | ] |
Quinten Yearsley | 777660f | 2020-03-04 23:37:06 +0000 | [diff] [blame] | 3851 | git_cl._trigger_tryjobs.assert_called_once_with(mock.ANY, expected_buckets, |
| 3852 | mock.ANY, 8) |
Quinten Yearsley | a19d353 | 2019-09-30 21:54:39 +0000 | [diff] [blame] | 3853 | |
Brian Sheedy | 59b06a8 | 2019-10-14 17:03:29 +0000 | [diff] [blame] | 3854 | |
Quinten Yearsley | ee8be8a | 2020-03-05 21:48:32 +0000 | [diff] [blame] | 3855 | class MakeRequestsHelperTestCase(unittest.TestCase): |
| 3856 | |
| 3857 | def exampleGerritChange(self): |
| 3858 | return { |
| 3859 | 'host': 'chromium-review.googlesource.com', |
| 3860 | 'project': 'depot_tools', |
| 3861 | 'change': 1, |
| 3862 | 'patchset': 2, |
| 3863 | } |
| 3864 | |
| 3865 | def testMakeRequestsHelperNoOptions(self): |
| 3866 | # Basic test for the helper function _make_tryjob_schedule_requests; |
| 3867 | # it shouldn't throw AttributeError even when options doesn't have any |
| 3868 | # of the expected values; it will use default option values. |
| 3869 | changelist = ChangelistMock(gerrit_change=self.exampleGerritChange()) |
| 3870 | jobs = [('chromium', 'try', 'my-builder')] |
| 3871 | options = optparse.Values() |
| 3872 | requests = git_cl._make_tryjob_schedule_requests( |
| 3873 | changelist, jobs, options, patchset=None) |
| 3874 | |
| 3875 | # requestId is non-deterministic. Just assert that it's there and has |
| 3876 | # a particular length. |
| 3877 | self.assertEqual(len(requests[0]['scheduleBuild'].pop('requestId')), 36) |
| 3878 | self.assertEqual(requests, [{ |
| 3879 | 'scheduleBuild': { |
| 3880 | 'builder': { |
| 3881 | 'bucket': 'try', |
| 3882 | 'builder': 'my-builder', |
| 3883 | 'project': 'chromium' |
| 3884 | }, |
| 3885 | 'gerritChanges': [self.exampleGerritChange()], |
| 3886 | 'properties': { |
| 3887 | 'category': 'git_cl_try' |
| 3888 | }, |
| 3889 | 'tags': [{ |
| 3890 | 'key': 'builder', |
| 3891 | 'value': 'my-builder' |
| 3892 | }, { |
| 3893 | 'key': 'user_agent', |
| 3894 | 'value': 'git_cl_try' |
| 3895 | }] |
| 3896 | } |
| 3897 | }]) |
| 3898 | |
| 3899 | def testMakeRequestsHelperPresubmitSetsDryRunProperty(self): |
| 3900 | changelist = ChangelistMock(gerrit_change=self.exampleGerritChange()) |
| 3901 | jobs = [('chromium', 'try', 'presubmit')] |
| 3902 | options = optparse.Values() |
| 3903 | requests = git_cl._make_tryjob_schedule_requests( |
| 3904 | changelist, jobs, options, patchset=None) |
| 3905 | self.assertEqual(requests[0]['scheduleBuild']['properties'], { |
| 3906 | 'category': 'git_cl_try', |
| 3907 | 'dry_run': 'true' |
| 3908 | }) |
| 3909 | |
| 3910 | def testMakeRequestsHelperRevisionSet(self): |
| 3911 | # Gitiles commit is specified when revision is in options. |
| 3912 | changelist = ChangelistMock(gerrit_change=self.exampleGerritChange()) |
| 3913 | jobs = [('chromium', 'try', 'my-builder')] |
| 3914 | options = optparse.Values({'revision': 'ba5eba11'}) |
| 3915 | requests = git_cl._make_tryjob_schedule_requests( |
| 3916 | changelist, jobs, options, patchset=None) |
| 3917 | self.assertEqual( |
| 3918 | requests[0]['scheduleBuild']['gitilesCommit'], { |
| 3919 | 'host': 'chromium-review.googlesource.com', |
| 3920 | 'id': 'ba5eba11', |
Josip Sokcevic | 9011a5b | 2021-02-12 18:59:44 +0000 | [diff] [blame] | 3921 | 'project': 'depot_tools', |
| 3922 | 'ref': 'refs/heads/main', |
Quinten Yearsley | ee8be8a | 2020-03-05 21:48:32 +0000 | [diff] [blame] | 3923 | }) |
| 3924 | |
| 3925 | def testMakeRequestsHelperRetryFailedSet(self): |
| 3926 | # An extra tag is added when retry_failed is in options. |
| 3927 | changelist = ChangelistMock(gerrit_change=self.exampleGerritChange()) |
| 3928 | jobs = [('chromium', 'try', 'my-builder')] |
| 3929 | options = optparse.Values({'retry_failed': 'true'}) |
| 3930 | requests = git_cl._make_tryjob_schedule_requests( |
| 3931 | changelist, jobs, options, patchset=None) |
| 3932 | self.assertEqual( |
| 3933 | requests[0]['scheduleBuild']['tags'], [ |
| 3934 | { |
| 3935 | 'key': 'builder', |
| 3936 | 'value': 'my-builder' |
| 3937 | }, |
| 3938 | { |
| 3939 | 'key': 'user_agent', |
| 3940 | 'value': 'git_cl_try' |
| 3941 | }, |
| 3942 | { |
| 3943 | 'key': 'retry_failed', |
| 3944 | 'value': '1' |
| 3945 | } |
| 3946 | ]) |
| 3947 | |
| 3948 | def testMakeRequestsHelperCategorySet(self): |
Quinten Yearsley | 925cedb | 2020-04-13 17:49:39 +0000 | [diff] [blame] | 3949 | # The category property can be overridden with options. |
Quinten Yearsley | ee8be8a | 2020-03-05 21:48:32 +0000 | [diff] [blame] | 3950 | changelist = ChangelistMock(gerrit_change=self.exampleGerritChange()) |
| 3951 | jobs = [('chromium', 'try', 'my-builder')] |
| 3952 | options = optparse.Values({'category': 'my-special-category'}) |
| 3953 | requests = git_cl._make_tryjob_schedule_requests( |
| 3954 | changelist, jobs, options, patchset=None) |
| 3955 | self.assertEqual(requests[0]['scheduleBuild']['properties'], |
| 3956 | {'category': 'my-special-category'}) |
| 3957 | |
| 3958 | |
Edward Lemur | da4b6c6 | 2020-02-13 00:28:40 +0000 | [diff] [blame] | 3959 | class CMDFormatTestCase(unittest.TestCase): |
Brian Sheedy | 59b06a8 | 2019-10-14 17:03:29 +0000 | [diff] [blame] | 3960 | |
| 3961 | def setUp(self): |
| 3962 | super(CMDFormatTestCase, self).setUp() |
Jamie Madill | 5e96ad1 | 2020-01-13 16:08:35 +0000 | [diff] [blame] | 3963 | mock.patch('git_cl.RunCommand').start() |
| 3964 | mock.patch('clang_format.FindClangFormatToolInChromiumTree').start() |
| 3965 | mock.patch('clang_format.FindClangFormatScriptInChromiumTree').start() |
| 3966 | mock.patch('git_cl.settings').start() |
Brian Sheedy | 59b06a8 | 2019-10-14 17:03:29 +0000 | [diff] [blame] | 3967 | self._top_dir = tempfile.mkdtemp() |
Jamie Madill | 5e96ad1 | 2020-01-13 16:08:35 +0000 | [diff] [blame] | 3968 | self.addCleanup(mock.patch.stopall) |
Brian Sheedy | 59b06a8 | 2019-10-14 17:03:29 +0000 | [diff] [blame] | 3969 | |
| 3970 | def tearDown(self): |
| 3971 | shutil.rmtree(self._top_dir) |
| 3972 | super(CMDFormatTestCase, self).tearDown() |
| 3973 | |
Jamie Madill | 5e96ad1 | 2020-01-13 16:08:35 +0000 | [diff] [blame] | 3974 | def _make_temp_file(self, fname, contents): |
Anthony Polito | c64e390 | 2021-04-30 21:55:25 +0000 | [diff] [blame] | 3975 | gclient_utils.FileWrite(os.path.join(self._top_dir, fname), |
| 3976 | ('\n'.join(contents))) |
Jamie Madill | 5e96ad1 | 2020-01-13 16:08:35 +0000 | [diff] [blame] | 3977 | |
Brian Sheedy | 59b06a8 | 2019-10-14 17:03:29 +0000 | [diff] [blame] | 3978 | def _make_yapfignore(self, contents): |
Jamie Madill | 5e96ad1 | 2020-01-13 16:08:35 +0000 | [diff] [blame] | 3979 | self._make_temp_file('.yapfignore', contents) |
Brian Sheedy | 59b06a8 | 2019-10-14 17:03:29 +0000 | [diff] [blame] | 3980 | |
Brian Sheedy | b4307d5 | 2019-12-02 19:18:17 +0000 | [diff] [blame] | 3981 | def _check_yapf_filtering(self, files, expected): |
| 3982 | self.assertEqual(expected, git_cl._FilterYapfIgnoredFiles( |
| 3983 | files, git_cl._GetYapfIgnorePatterns(self._top_dir))) |
Brian Sheedy | 59b06a8 | 2019-10-14 17:03:29 +0000 | [diff] [blame] | 3984 | |
Edward Lemur | 1a83da1 | 2020-03-04 21:18:36 +0000 | [diff] [blame] | 3985 | def _run_command_mock(self, return_value): |
| 3986 | def f(*args, **kwargs): |
| 3987 | if 'stdin' in kwargs: |
| 3988 | self.assertIsInstance(kwargs['stdin'], bytes) |
| 3989 | return return_value |
| 3990 | return f |
| 3991 | |
Jamie Madill | 5e96ad1 | 2020-01-13 16:08:35 +0000 | [diff] [blame] | 3992 | def testClangFormatDiffFull(self): |
| 3993 | self._make_temp_file('test.cc', ['// test']) |
| 3994 | git_cl.settings.GetFormatFullByDefault.return_value = False |
| 3995 | diff_file = [os.path.join(self._top_dir, 'test.cc')] |
| 3996 | mock_opts = mock.Mock(full=True, dry_run=True, diff=False) |
| 3997 | |
| 3998 | # Diff |
Edward Lemur | 1a83da1 | 2020-03-04 21:18:36 +0000 | [diff] [blame] | 3999 | git_cl.RunCommand.side_effect = self._run_command_mock(' // test') |
Jamie Madill | 5e96ad1 | 2020-01-13 16:08:35 +0000 | [diff] [blame] | 4000 | return_value = git_cl._RunClangFormatDiff(mock_opts, diff_file, |
| 4001 | self._top_dir, 'HEAD') |
| 4002 | self.assertEqual(2, return_value) |
| 4003 | |
| 4004 | # No diff |
Edward Lemur | 1a83da1 | 2020-03-04 21:18:36 +0000 | [diff] [blame] | 4005 | git_cl.RunCommand.side_effect = self._run_command_mock('// test') |
Jamie Madill | 5e96ad1 | 2020-01-13 16:08:35 +0000 | [diff] [blame] | 4006 | return_value = git_cl._RunClangFormatDiff(mock_opts, diff_file, |
| 4007 | self._top_dir, 'HEAD') |
| 4008 | self.assertEqual(0, return_value) |
| 4009 | |
| 4010 | def testClangFormatDiff(self): |
| 4011 | git_cl.settings.GetFormatFullByDefault.return_value = False |
Josip Sokcevic | 464e9ff | 2020-03-18 23:48:55 +0000 | [diff] [blame] | 4012 | # A valid file is required, so use this test. |
| 4013 | clang_format.FindClangFormatToolInChromiumTree.return_value = __file__ |
Jamie Madill | 5e96ad1 | 2020-01-13 16:08:35 +0000 | [diff] [blame] | 4014 | mock_opts = mock.Mock(full=False, dry_run=True, diff=False) |
| 4015 | |
| 4016 | # Diff |
Edward Lemur | 1a83da1 | 2020-03-04 21:18:36 +0000 | [diff] [blame] | 4017 | git_cl.RunCommand.side_effect = self._run_command_mock('error') |
| 4018 | return_value = git_cl._RunClangFormatDiff( |
| 4019 | mock_opts, ['.'], self._top_dir, 'HEAD') |
Jamie Madill | 5e96ad1 | 2020-01-13 16:08:35 +0000 | [diff] [blame] | 4020 | self.assertEqual(2, return_value) |
| 4021 | |
| 4022 | # No diff |
Edward Lemur | 1a83da1 | 2020-03-04 21:18:36 +0000 | [diff] [blame] | 4023 | git_cl.RunCommand.side_effect = self._run_command_mock('') |
Jamie Madill | 5e96ad1 | 2020-01-13 16:08:35 +0000 | [diff] [blame] | 4024 | return_value = git_cl._RunClangFormatDiff(mock_opts, ['.'], self._top_dir, |
| 4025 | 'HEAD') |
| 4026 | self.assertEqual(0, return_value) |
| 4027 | |
Brian Sheedy | b4307d5 | 2019-12-02 19:18:17 +0000 | [diff] [blame] | 4028 | def testYapfignoreExplicit(self): |
| 4029 | self._make_yapfignore(['foo/bar.py', 'foo/bar/baz.py']) |
| 4030 | files = [ |
| 4031 | 'bar.py', |
| 4032 | 'foo/bar.py', |
| 4033 | 'foo/baz.py', |
| 4034 | 'foo/bar/baz.py', |
| 4035 | 'foo/bar/foobar.py', |
| 4036 | ] |
| 4037 | expected = [ |
| 4038 | 'bar.py', |
| 4039 | 'foo/baz.py', |
| 4040 | 'foo/bar/foobar.py', |
| 4041 | ] |
| 4042 | self._check_yapf_filtering(files, expected) |
Brian Sheedy | 59b06a8 | 2019-10-14 17:03:29 +0000 | [diff] [blame] | 4043 | |
Brian Sheedy | b4307d5 | 2019-12-02 19:18:17 +0000 | [diff] [blame] | 4044 | def testYapfignoreSingleWildcards(self): |
| 4045 | self._make_yapfignore(['*bar.py', 'foo*', 'baz*.py']) |
| 4046 | files = [ |
| 4047 | 'bar.py', # Matched by *bar.py. |
| 4048 | 'bar.txt', |
| 4049 | 'foobar.py', # Matched by *bar.py, foo*. |
| 4050 | 'foobar.txt', # Matched by foo*. |
| 4051 | 'bazbar.py', # Matched by *bar.py, baz*.py. |
| 4052 | 'bazbar.txt', |
| 4053 | 'foo/baz.txt', # Matched by foo*. |
| 4054 | 'bar/bar.py', # Matched by *bar.py. |
| 4055 | 'baz/foo.py', # Matched by baz*.py, foo*. |
| 4056 | 'baz/foo.txt', |
| 4057 | ] |
| 4058 | expected = [ |
| 4059 | 'bar.txt', |
| 4060 | 'bazbar.txt', |
| 4061 | 'baz/foo.txt', |
| 4062 | ] |
| 4063 | self._check_yapf_filtering(files, expected) |
Brian Sheedy | 59b06a8 | 2019-10-14 17:03:29 +0000 | [diff] [blame] | 4064 | |
Brian Sheedy | b4307d5 | 2019-12-02 19:18:17 +0000 | [diff] [blame] | 4065 | def testYapfignoreMultiplewildcards(self): |
| 4066 | self._make_yapfignore(['*bar*', '*foo*baz.txt']) |
| 4067 | files = [ |
| 4068 | 'bar.py', # Matched by *bar*. |
| 4069 | 'bar.txt', # Matched by *bar*. |
| 4070 | 'abar.py', # Matched by *bar*. |
| 4071 | 'foobaz.txt', # Matched by *foo*baz.txt. |
| 4072 | 'foobaz.py', |
| 4073 | 'afoobaz.txt', # Matched by *foo*baz.txt. |
| 4074 | ] |
| 4075 | expected = [ |
| 4076 | 'foobaz.py', |
| 4077 | ] |
| 4078 | self._check_yapf_filtering(files, expected) |
Brian Sheedy | 59b06a8 | 2019-10-14 17:03:29 +0000 | [diff] [blame] | 4079 | |
| 4080 | def testYapfignoreComments(self): |
| 4081 | self._make_yapfignore(['test.py', '#test2.py']) |
Brian Sheedy | b4307d5 | 2019-12-02 19:18:17 +0000 | [diff] [blame] | 4082 | files = [ |
| 4083 | 'test.py', |
| 4084 | 'test2.py', |
| 4085 | ] |
| 4086 | expected = [ |
| 4087 | 'test2.py', |
| 4088 | ] |
| 4089 | self._check_yapf_filtering(files, expected) |
Brian Sheedy | 59b06a8 | 2019-10-14 17:03:29 +0000 | [diff] [blame] | 4090 | |
Anthony Polito | c64e390 | 2021-04-30 21:55:25 +0000 | [diff] [blame] | 4091 | def testYapfHandleUtf8(self): |
| 4092 | self._make_yapfignore(['test.py', 'test_🌐.py']) |
| 4093 | files = [ |
| 4094 | 'test.py', |
| 4095 | 'test_🌐.py', |
| 4096 | 'test2.py', |
| 4097 | ] |
| 4098 | expected = [ |
| 4099 | 'test2.py', |
| 4100 | ] |
| 4101 | self._check_yapf_filtering(files, expected) |
| 4102 | |
Brian Sheedy | 59b06a8 | 2019-10-14 17:03:29 +0000 | [diff] [blame] | 4103 | def testYapfignoreBlankLines(self): |
| 4104 | self._make_yapfignore(['test.py', '', '', 'test2.py']) |
Brian Sheedy | b4307d5 | 2019-12-02 19:18:17 +0000 | [diff] [blame] | 4105 | files = [ |
| 4106 | 'test.py', |
| 4107 | 'test2.py', |
| 4108 | 'test3.py', |
| 4109 | ] |
| 4110 | expected = [ |
| 4111 | 'test3.py', |
| 4112 | ] |
| 4113 | self._check_yapf_filtering(files, expected) |
Brian Sheedy | 59b06a8 | 2019-10-14 17:03:29 +0000 | [diff] [blame] | 4114 | |
| 4115 | def testYapfignoreWhitespace(self): |
| 4116 | self._make_yapfignore([' test.py ']) |
Brian Sheedy | b4307d5 | 2019-12-02 19:18:17 +0000 | [diff] [blame] | 4117 | files = [ |
| 4118 | 'test.py', |
| 4119 | 'test2.py', |
| 4120 | ] |
| 4121 | expected = [ |
| 4122 | 'test2.py', |
| 4123 | ] |
| 4124 | self._check_yapf_filtering(files, expected) |
Brian Sheedy | 59b06a8 | 2019-10-14 17:03:29 +0000 | [diff] [blame] | 4125 | |
Brian Sheedy | b4307d5 | 2019-12-02 19:18:17 +0000 | [diff] [blame] | 4126 | def testYapfignoreNoFiles(self): |
Brian Sheedy | 59b06a8 | 2019-10-14 17:03:29 +0000 | [diff] [blame] | 4127 | self._make_yapfignore(['test.py']) |
Brian Sheedy | b4307d5 | 2019-12-02 19:18:17 +0000 | [diff] [blame] | 4128 | self._check_yapf_filtering([], []) |
| 4129 | |
| 4130 | def testYapfignoreMissingYapfignore(self): |
| 4131 | files = [ |
| 4132 | 'test.py', |
| 4133 | ] |
| 4134 | expected = [ |
| 4135 | 'test.py', |
| 4136 | ] |
| 4137 | self._check_yapf_filtering(files, expected) |
Brian Sheedy | 59b06a8 | 2019-10-14 17:03:29 +0000 | [diff] [blame] | 4138 | |
| 4139 | |
Sigurd Schneider | 9abde8c | 2020-11-17 08:44:52 +0000 | [diff] [blame] | 4140 | class CMDStatusTestCase(CMDTestCaseBase): |
| 4141 | # Return branch names a,..,f with comitterdates in increasing order, i.e. |
| 4142 | # 'f' is the most-recently changed branch. |
| 4143 | def _mock_run_git(commands): |
| 4144 | if commands == [ |
| 4145 | 'for-each-ref', '--format=%(refname) %(committerdate:unix)', |
| 4146 | 'refs/heads' |
| 4147 | ]: |
| 4148 | branches_and_committerdates = [ |
| 4149 | 'refs/heads/a 1', |
| 4150 | 'refs/heads/b 2', |
| 4151 | 'refs/heads/c 3', |
| 4152 | 'refs/heads/d 4', |
| 4153 | 'refs/heads/e 5', |
| 4154 | 'refs/heads/f 6', |
| 4155 | ] |
| 4156 | return '\n'.join(branches_and_committerdates) |
| 4157 | |
| 4158 | # Mock the status in such a way that the issue number gives us an |
| 4159 | # indication of the commit date (simplifies manual debugging). |
| 4160 | def _mock_get_cl_statuses(branches, fine_grained, max_processes): |
| 4161 | for c in branches: |
| 4162 | c.issue = (100 + int(c.GetCommitDate())) |
| 4163 | yield (c, 'open') |
| 4164 | |
| 4165 | @mock.patch('git_cl.Changelist.EnsureAuthenticated') |
| 4166 | @mock.patch('git_cl.Changelist.FetchDescription', lambda cl, pretty: 'x') |
| 4167 | @mock.patch('git_cl.Changelist.GetIssue', lambda cl: cl.issue) |
| 4168 | @mock.patch('git_cl.RunGit', _mock_run_git) |
| 4169 | @mock.patch('git_cl.get_cl_statuses', _mock_get_cl_statuses) |
| 4170 | @mock.patch('git_cl.Settings.GetRoot', return_value='') |
| 4171 | @mock.patch('scm.GIT.GetBranch', return_value='a') |
| 4172 | def testStatus(self, *_mocks): |
| 4173 | self.assertEqual(0, git_cl.main(['status', '--no-branch-color'])) |
| 4174 | self.maxDiff = None |
| 4175 | self.assertEqual( |
| 4176 | sys.stdout.getvalue(), 'Branches associated with reviews:\n' |
| 4177 | ' * a : https://crrev.com/c/101 (open)\n' |
| 4178 | ' b : https://crrev.com/c/102 (open)\n' |
| 4179 | ' c : https://crrev.com/c/103 (open)\n' |
| 4180 | ' d : https://crrev.com/c/104 (open)\n' |
| 4181 | ' e : https://crrev.com/c/105 (open)\n' |
| 4182 | ' f : https://crrev.com/c/106 (open)\n\n' |
| 4183 | 'Current branch: a\n' |
| 4184 | 'Issue number: 101 (https://chromium-review.googlesource.com/101)\n' |
| 4185 | 'Issue description:\n' |
| 4186 | 'x\n') |
| 4187 | |
| 4188 | @mock.patch('git_cl.Changelist.EnsureAuthenticated') |
| 4189 | @mock.patch('git_cl.Changelist.FetchDescription', lambda cl, pretty: 'x') |
| 4190 | @mock.patch('git_cl.Changelist.GetIssue', lambda cl: cl.issue) |
| 4191 | @mock.patch('git_cl.RunGit', _mock_run_git) |
| 4192 | @mock.patch('git_cl.get_cl_statuses', _mock_get_cl_statuses) |
| 4193 | @mock.patch('git_cl.Settings.GetRoot', return_value='') |
| 4194 | @mock.patch('scm.GIT.GetBranch', return_value='a') |
| 4195 | def testStatusByDate(self, *_mocks): |
| 4196 | self.assertEqual( |
| 4197 | 0, git_cl.main(['status', '--no-branch-color', '--date-order'])) |
| 4198 | self.maxDiff = None |
| 4199 | self.assertEqual( |
| 4200 | sys.stdout.getvalue(), 'Branches associated with reviews:\n' |
| 4201 | ' f : https://crrev.com/c/106 (open)\n' |
| 4202 | ' e : https://crrev.com/c/105 (open)\n' |
| 4203 | ' d : https://crrev.com/c/104 (open)\n' |
| 4204 | ' c : https://crrev.com/c/103 (open)\n' |
| 4205 | ' b : https://crrev.com/c/102 (open)\n' |
| 4206 | ' * a : https://crrev.com/c/101 (open)\n\n' |
| 4207 | 'Current branch: a\n' |
| 4208 | 'Issue number: 101 (https://chromium-review.googlesource.com/101)\n' |
| 4209 | 'Issue description:\n' |
| 4210 | 'x\n') |
| 4211 | |
| 4212 | |
Edward Lesmes | 0e4e5ae | 2021-01-08 18:28:46 +0000 | [diff] [blame] | 4213 | class CMDOwnersTestCase(CMDTestCaseBase): |
| 4214 | def setUp(self): |
| 4215 | super(CMDOwnersTestCase, self).setUp() |
Edward Lesmes | 82b992a | 2021-01-11 23:24:55 +0000 | [diff] [blame] | 4216 | self.owners_by_path = { |
| 4217 | 'foo': ['a@example.com'], |
| 4218 | 'bar': ['b@example.com', 'c@example.com'], |
| 4219 | } |
Edward Lesmes | 0e4e5ae | 2021-01-08 18:28:46 +0000 | [diff] [blame] | 4220 | mock.patch('git_cl.Settings.GetRoot', return_value='root').start() |
| 4221 | mock.patch('git_cl.Changelist.GetAuthor', return_value='author').start() |
| 4222 | mock.patch( |
Edward Lesmes | 82b992a | 2021-01-11 23:24:55 +0000 | [diff] [blame] | 4223 | 'git_cl.Changelist.GetAffectedFiles', |
| 4224 | return_value=list(self.owners_by_path)).start() |
| 4225 | mock.patch( |
Edward Lesmes | 0e4e5ae | 2021-01-08 18:28:46 +0000 | [diff] [blame] | 4226 | 'git_cl.Changelist.GetCommonAncestorWithUpstream', |
| 4227 | return_value='upstream').start() |
Edward Lesmes | 82b992a | 2021-01-11 23:24:55 +0000 | [diff] [blame] | 4228 | mock.patch( |
Edward Lesmes | e157691 | 2021-02-16 21:53:34 +0000 | [diff] [blame] | 4229 | 'git_cl.Changelist.GetGerritHost', |
| 4230 | return_value='host').start() |
| 4231 | mock.patch( |
| 4232 | 'git_cl.Changelist.GetGerritProject', |
| 4233 | return_value='project').start() |
| 4234 | mock.patch( |
| 4235 | 'git_cl.Changelist.GetRemoteBranch', |
| 4236 | return_value=('origin', 'refs/remotes/origin/main')).start() |
| 4237 | mock.patch( |
| 4238 | 'owners_client.OwnersClient.BatchListOwners', |
Edward Lesmes | 82b992a | 2021-01-11 23:24:55 +0000 | [diff] [blame] | 4239 | return_value=self.owners_by_path).start() |
Edward Lesmes | 8170c29 | 2021-03-19 20:04:43 +0000 | [diff] [blame] | 4240 | mock.patch( |
| 4241 | 'gerrit_util.IsCodeOwnersEnabledOnHost', return_value=True).start() |
Edward Lesmes | 0e4e5ae | 2021-01-08 18:28:46 +0000 | [diff] [blame] | 4242 | self.addCleanup(mock.patch.stopall) |
| 4243 | |
| 4244 | def testShowAllNoArgs(self): |
| 4245 | self.assertEqual(0, git_cl.main(['owners', '--show-all'])) |
| 4246 | self.assertEqual( |
| 4247 | 'No files specified for --show-all. Nothing to do.\n', |
| 4248 | git_cl.sys.stdout.getvalue()) |
| 4249 | |
| 4250 | def testShowAll(self): |
Edward Lesmes | 0e4e5ae | 2021-01-08 18:28:46 +0000 | [diff] [blame] | 4251 | self.assertEqual( |
| 4252 | 0, |
| 4253 | git_cl.main(['owners', '--show-all', 'foo', 'bar', 'baz'])) |
Edward Lesmes | e157691 | 2021-02-16 21:53:34 +0000 | [diff] [blame] | 4254 | owners_client.OwnersClient.BatchListOwners.assert_called_once_with( |
Edward Lesmes | 82b992a | 2021-01-11 23:24:55 +0000 | [diff] [blame] | 4255 | ['foo', 'bar', 'baz']) |
Edward Lesmes | 0e4e5ae | 2021-01-08 18:28:46 +0000 | [diff] [blame] | 4256 | self.assertEqual( |
| 4257 | '\n'.join([ |
| 4258 | 'Owners for foo:', |
| 4259 | ' - a@example.com', |
| 4260 | 'Owners for bar:', |
| 4261 | ' - b@example.com', |
| 4262 | ' - c@example.com', |
| 4263 | 'Owners for baz:', |
| 4264 | ' - No owners found', |
| 4265 | '', |
| 4266 | ]), |
| 4267 | sys.stdout.getvalue()) |
| 4268 | |
Edward Lesmes | 82b992a | 2021-01-11 23:24:55 +0000 | [diff] [blame] | 4269 | def testBatch(self): |
| 4270 | self.assertEqual(0, git_cl.main(['owners', '--batch'])) |
| 4271 | self.assertIn('a@example.com', sys.stdout.getvalue()) |
| 4272 | self.assertIn('b@example.com', sys.stdout.getvalue()) |
| 4273 | |
Edward Lesmes | 0e4e5ae | 2021-01-08 18:28:46 +0000 | [diff] [blame] | 4274 | |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 4275 | if __name__ == '__main__': |
Andrii Shyshkalov | 18df0cd | 2017-01-25 15:22:20 +0100 | [diff] [blame] | 4276 | logging.basicConfig( |
| 4277 | level=logging.DEBUG if '-v' in sys.argv else logging.ERROR) |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 4278 | unittest.main() |