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