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