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