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