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