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