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