maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
maruel@chromium.org | eb5edbc | 2012-01-16 17:03:28 +0000 | [diff] [blame] | 2 | # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Unit tests for git_cl.py.""" |
| 7 | |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 8 | import contextlib |
Andrii Shyshkalov | d8aa49f | 2017-03-17 16:05:49 +0100 | [diff] [blame] | 9 | import datetime |
tandrii | de281ae | 2016-10-12 06:02:30 -0700 | [diff] [blame] | 10 | import json |
Andrii Shyshkalov | 18df0cd | 2017-01-25 15:22:20 +0100 | [diff] [blame] | 11 | import logging |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 12 | import os |
maruel@chromium.org | a335365 | 2011-11-30 14:26:57 +0000 | [diff] [blame] | 13 | import StringIO |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 14 | import sys |
| 15 | import unittest |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 16 | import urlparse |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 17 | |
| 18 | sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 19 | |
| 20 | from testing_support.auto_stub import TestCase |
| 21 | |
| 22 | import git_cl |
iannucci@chromium.org | 9e84927 | 2014-04-04 00:31:55 +0000 | [diff] [blame] | 23 | import git_common |
tandrii@chromium.org | 57d8654 | 2016-03-04 16:11:32 +0000 | [diff] [blame] | 24 | import git_footers |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 25 | import subprocess2 |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 26 | |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 27 | def callError(code=1, cmd='', cwd='', stdout='', stderr=''): |
| 28 | return subprocess2.CalledProcessError(code, cmd, cwd, stdout, stderr) |
| 29 | |
| 30 | |
| 31 | CERR1 = callError(1) |
| 32 | |
| 33 | |
martiniss@chromium.org | d6648e2 | 2016-04-29 19:22:16 +0000 | [diff] [blame] | 34 | class ChangelistMock(object): |
| 35 | # A class variable so we can access it when we don't have access to the |
| 36 | # instance that's being set. |
| 37 | desc = "" |
| 38 | def __init__(self, **kwargs): |
| 39 | pass |
| 40 | def GetIssue(self): |
| 41 | return 1 |
Kenneth Russell | 61e2ed4 | 2017-02-15 11:47:13 -0800 | [diff] [blame] | 42 | def GetDescription(self, force=False): |
martiniss@chromium.org | d6648e2 | 2016-04-29 19:22:16 +0000 | [diff] [blame] | 43 | return ChangelistMock.desc |
dsansome | e2d6fd9 | 2016-09-08 00:10:47 -0700 | [diff] [blame] | 44 | def UpdateDescription(self, desc, force=False): |
martiniss@chromium.org | d6648e2 | 2016-04-29 19:22:16 +0000 | [diff] [blame] | 45 | ChangelistMock.desc = desc |
| 46 | |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 47 | |
maruel@chromium.org | 2e72bb1 | 2012-01-17 15:18:35 +0000 | [diff] [blame] | 48 | class PresubmitMock(object): |
| 49 | def __init__(self, *args, **kwargs): |
| 50 | self.reviewers = [] |
| 51 | @staticmethod |
| 52 | def should_continue(): |
| 53 | return True |
| 54 | |
| 55 | |
| 56 | class RietveldMock(object): |
| 57 | def __init__(self, *args, **kwargs): |
| 58 | pass |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 59 | |
maruel@chromium.org | 2e72bb1 | 2012-01-17 15:18:35 +0000 | [diff] [blame] | 60 | @staticmethod |
Kenneth Russell | 61e2ed4 | 2017-02-15 11:47:13 -0800 | [diff] [blame] | 61 | def get_description(issue, force=False): |
maruel@chromium.org | 2e72bb1 | 2012-01-17 15:18:35 +0000 | [diff] [blame] | 62 | return 'Issue: %d' % issue |
| 63 | |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 64 | @staticmethod |
| 65 | def get_issue_properties(_issue, _messages): |
| 66 | return { |
| 67 | 'reviewers': ['joe@chromium.org', 'john@chromium.org'], |
| 68 | 'messages': [ |
| 69 | { |
| 70 | 'approval': True, |
Aaron Gable | a1bab27 | 2017-04-11 16:38:18 -0700 | [diff] [blame] | 71 | 'disapproval': False, |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 72 | 'sender': 'john@chromium.org', |
| 73 | }, |
| 74 | ], |
Andrii Shyshkalov | 06a2502 | 2016-11-24 16:47:00 +0100 | [diff] [blame] | 75 | 'patchsets': [1, 20001], |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 76 | } |
| 77 | |
iannucci | e53c935 | 2016-08-17 14:40:40 -0700 | [diff] [blame] | 78 | @staticmethod |
| 79 | def close_issue(_issue): |
| 80 | return 'Closed' |
| 81 | |
skobes | 6468b90 | 2016-10-24 08:45:10 -0700 | [diff] [blame] | 82 | @staticmethod |
| 83 | def get_patch(issue, patchset): |
| 84 | return 'patch set from issue %s patchset %s' % (issue, patchset) |
| 85 | |
Andrii Shyshkalov | 06a2502 | 2016-11-24 16:47:00 +0100 | [diff] [blame] | 86 | @staticmethod |
| 87 | def update_description(_issue, _description): |
| 88 | return 'Updated' |
| 89 | |
| 90 | @staticmethod |
| 91 | def add_comment(_issue, _comment): |
| 92 | return 'Commented' |
| 93 | |
| 94 | |
| 95 | |
skobes | 6468b90 | 2016-10-24 08:45:10 -0700 | [diff] [blame] | 96 | |
| 97 | class GitCheckoutMock(object): |
| 98 | def __init__(self, *args, **kwargs): |
| 99 | pass |
| 100 | |
| 101 | @staticmethod |
| 102 | def reset(): |
| 103 | GitCheckoutMock.conflict = False |
| 104 | |
| 105 | def apply_patch(self, p): |
| 106 | if GitCheckoutMock.conflict: |
| 107 | raise Exception('failed') |
| 108 | |
maruel@chromium.org | 2e72bb1 | 2012-01-17 15:18:35 +0000 | [diff] [blame] | 109 | |
| 110 | class WatchlistsMock(object): |
| 111 | def __init__(self, _): |
| 112 | pass |
| 113 | @staticmethod |
| 114 | def GetWatchersForPaths(_): |
| 115 | return ['joe@example.com'] |
| 116 | |
| 117 | |
ukai@chromium.org | 78c4b98 | 2012-02-14 02:20:26 +0000 | [diff] [blame] | 118 | class CodereviewSettingsFileMock(object): |
| 119 | def __init__(self): |
| 120 | pass |
Quinten Yearsley | b2cc4a9 | 2016-12-15 13:53:26 -0800 | [diff] [blame] | 121 | # pylint: disable=no-self-use |
ukai@chromium.org | 78c4b98 | 2012-02-14 02:20:26 +0000 | [diff] [blame] | 122 | def read(self): |
| 123 | return ("CODE_REVIEW_SERVER: gerrit.chromium.org\n" + |
andybons@chromium.org | 11f46eb | 2016-02-02 19:26:51 +0000 | [diff] [blame] | 124 | "GERRIT_HOST: True\n") |
ukai@chromium.org | 78c4b98 | 2012-02-14 02:20:26 +0000 | [diff] [blame] | 125 | |
| 126 | |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 127 | class AuthenticatorMock(object): |
| 128 | def __init__(self, *_args): |
| 129 | pass |
| 130 | def has_cached_credentials(self): |
| 131 | return True |
tandrii | 221ab25 | 2016-10-06 08:12:04 -0700 | [diff] [blame] | 132 | def authorize(self, http): |
| 133 | return http |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 134 | |
| 135 | |
Andrii Shyshkalov | bb86fbb | 2017-03-24 14:59:28 +0100 | [diff] [blame] | 136 | def CookiesAuthenticatorMockFactory(hosts_with_creds=None, same_auth=False): |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 137 | """Use to mock Gerrit/Git credentials from ~/.netrc or ~/.gitcookies. |
| 138 | |
| 139 | Usage: |
| 140 | >>> self.mock(git_cl.gerrit_util, "CookiesAuthenticator", |
Andrii Shyshkalov | bb86fbb | 2017-03-24 14:59:28 +0100 | [diff] [blame] | 141 | CookiesAuthenticatorMockFactory({'host': ('user', _, 'pass')}) |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 142 | |
| 143 | OR |
| 144 | >>> self.mock(git_cl.gerrit_util, "CookiesAuthenticator", |
Andrii Shyshkalov | bb86fbb | 2017-03-24 14:59:28 +0100 | [diff] [blame] | 145 | CookiesAuthenticatorMockFactory( |
| 146 | same_auth=('user', '', 'pass')) |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 147 | """ |
| 148 | class CookiesAuthenticatorMock(git_cl.gerrit_util.CookiesAuthenticator): |
Quinten Yearsley | b2cc4a9 | 2016-12-15 13:53:26 -0800 | [diff] [blame] | 149 | def __init__(self): # pylint: disable=super-init-not-called |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 150 | # Intentionally not calling super() because it reads actual cookie files. |
| 151 | pass |
| 152 | @classmethod |
| 153 | def get_gitcookies_path(cls): |
| 154 | return '~/.gitcookies' |
| 155 | @classmethod |
| 156 | def get_netrc_path(cls): |
| 157 | return '~/.netrc' |
Andrii Shyshkalov | bb86fbb | 2017-03-24 14:59:28 +0100 | [diff] [blame] | 158 | def _get_auth_for_host(self, host): |
| 159 | if same_auth: |
| 160 | return same_auth |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 161 | return (hosts_with_creds or {}).get(host) |
| 162 | return CookiesAuthenticatorMock |
| 163 | |
kmarshall | 9249e01 | 2016-08-23 12:02:16 -0700 | [diff] [blame] | 164 | class MockChangelistWithBranchAndIssue(): |
| 165 | def __init__(self, branch, issue): |
| 166 | self.branch = branch |
| 167 | self.issue = issue |
| 168 | def GetBranch(self): |
| 169 | return self.branch |
| 170 | def GetIssue(self): |
| 171 | return self.issue |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 172 | |
tandrii | c2405f5 | 2016-10-10 08:13:15 -0700 | [diff] [blame] | 173 | |
| 174 | class SystemExitMock(Exception): |
| 175 | pass |
| 176 | |
| 177 | |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 178 | class TestGitClBasic(unittest.TestCase): |
Andrii Shyshkalov | 3186301 | 2017-02-08 11:35:12 +0100 | [diff] [blame] | 179 | def test_get_description(self): |
| 180 | cl = git_cl.Changelist(issue=1, codereview='rietveld', |
| 181 | codereview_host='host') |
| 182 | cl.description = 'x' |
| 183 | cl.has_description = True |
Kenneth Russell | 61e2ed4 | 2017-02-15 11:47:13 -0800 | [diff] [blame] | 184 | cl._codereview_impl.FetchDescription = lambda *a, **kw: 'y' |
Andrii Shyshkalov | 3186301 | 2017-02-08 11:35:12 +0100 | [diff] [blame] | 185 | self.assertEquals(cl.GetDescription(), 'x') |
| 186 | self.assertEquals(cl.GetDescription(force=True), 'y') |
| 187 | self.assertEquals(cl.GetDescription(), 'y') |
| 188 | |
Robert Iannucci | 09f1f3d | 2017-03-28 16:54:32 -0700 | [diff] [blame] | 189 | def test_description_footers(self): |
| 190 | cl = git_cl.Changelist(issue=1, codereview='gerrit', |
| 191 | codereview_host='host') |
| 192 | cl.description = '\n'.join([ |
| 193 | 'This is some message', |
| 194 | '', |
| 195 | 'It has some lines', |
| 196 | 'and, also', |
| 197 | '', |
| 198 | 'Some: Really', |
| 199 | 'Awesome: Footers', |
| 200 | ]) |
| 201 | cl.has_description = True |
| 202 | cl._codereview_impl.UpdateDescriptionRemote = lambda *a, **kw: 'y' |
| 203 | msg, footers = cl.GetDescriptionFooters() |
| 204 | self.assertEquals( |
| 205 | msg, ['This is some message', '', 'It has some lines', 'and, also']) |
| 206 | self.assertEquals(footers, [('Some', 'Really'), ('Awesome', 'Footers')]) |
| 207 | |
| 208 | msg.append('wut') |
| 209 | footers.append(('gnarly-dude', 'beans')) |
| 210 | cl.UpdateDescriptionFooters(msg, footers) |
| 211 | self.assertEquals(cl.GetDescription().splitlines(), [ |
| 212 | 'This is some message', |
| 213 | '', |
| 214 | 'It has some lines', |
| 215 | 'and, also', |
| 216 | 'wut' |
| 217 | '', |
| 218 | 'Some: Really', |
| 219 | 'Awesome: Footers', |
| 220 | 'Gnarly-Dude: beans', |
| 221 | ]) |
| 222 | |
tandrii | f9aefb7 | 2016-07-01 09:06:51 -0700 | [diff] [blame] | 223 | def test_get_bug_line_values(self): |
| 224 | f = lambda p, bugs: list(git_cl._get_bug_line_values(p, bugs)) |
| 225 | self.assertEqual(f('', ''), []) |
| 226 | self.assertEqual(f('', '123,v8:456'), ['123', 'v8:456']) |
| 227 | self.assertEqual(f('v8', '456'), ['v8:456']) |
| 228 | self.assertEqual(f('v8', 'chromium:123,456'), ['v8:456', 'chromium:123']) |
| 229 | # Not nice, but not worth carying. |
| 230 | self.assertEqual(f('v8', 'chromium:123,456,v8:123'), |
| 231 | ['v8:456', 'chromium:123', 'v8:123']) |
| 232 | |
Andrii Shyshkalov | 15e50cc | 2016-12-02 14:34:08 +0100 | [diff] [blame] | 233 | def _test_git_number(self, parent_msg, dest_ref, child_msg, |
| 234 | parent_hash='parenthash'): |
| 235 | desc = git_cl.ChangeDescription(child_msg) |
| 236 | desc.update_with_git_number_footers(parent_hash, parent_msg, dest_ref) |
| 237 | return desc.description |
| 238 | |
| 239 | def assertEqualByLine(self, actual, expected): |
| 240 | self.assertEqual(actual.splitlines(), expected.splitlines()) |
| 241 | |
| 242 | def test_git_number_bad_parent(self): |
| 243 | with self.assertRaises(ValueError): |
| 244 | self._test_git_number('Parent', 'refs/heads/master', 'Child') |
| 245 | |
| 246 | def test_git_number_bad_parent_footer(self): |
| 247 | with self.assertRaises(AssertionError): |
| 248 | self._test_git_number( |
| 249 | 'Parent\n' |
| 250 | '\n' |
| 251 | 'Cr-Commit-Position: wrong', |
| 252 | 'refs/heads/master', 'Child') |
| 253 | |
| 254 | def test_git_number_bad_lineage_ignored(self): |
| 255 | actual = self._test_git_number( |
| 256 | 'Parent\n' |
| 257 | '\n' |
| 258 | 'Cr-Commit-Position: refs/heads/master@{#1}\n' |
| 259 | 'Cr-Branched-From: mustBeReal40CharHash-branch@{#pos}', |
| 260 | 'refs/heads/master', 'Child') |
| 261 | self.assertEqualByLine( |
| 262 | actual, |
| 263 | 'Child\n' |
| 264 | '\n' |
| 265 | 'Cr-Commit-Position: refs/heads/master@{#2}\n' |
| 266 | 'Cr-Branched-From: mustBeReal40CharHash-branch@{#pos}') |
| 267 | |
| 268 | def test_git_number_same_branch(self): |
| 269 | actual = self._test_git_number( |
| 270 | 'Parent\n' |
| 271 | '\n' |
| 272 | 'Cr-Commit-Position: refs/heads/master@{#12}', |
| 273 | dest_ref='refs/heads/master', |
| 274 | child_msg='Child') |
| 275 | self.assertEqualByLine( |
| 276 | actual, |
| 277 | 'Child\n' |
| 278 | '\n' |
| 279 | 'Cr-Commit-Position: refs/heads/master@{#13}') |
| 280 | |
Andrii Shyshkalov | de37c01 | 2017-07-06 21:06:50 +0200 | [diff] [blame] | 281 | def test_git_number_same_branch_mixed_footers(self): |
| 282 | actual = self._test_git_number( |
| 283 | 'Parent\n' |
| 284 | '\n' |
| 285 | 'Cr-Commit-Position: refs/heads/master@{#12}', |
| 286 | dest_ref='refs/heads/master', |
| 287 | child_msg='Child\n' |
| 288 | '\n' |
| 289 | 'Broken-by: design\n' |
| 290 | 'BUG=123') |
| 291 | self.assertEqualByLine( |
| 292 | actual, |
| 293 | 'Child\n' |
| 294 | '\n' |
| 295 | 'Broken-by: design\n' |
| 296 | 'BUG=123\n' |
| 297 | 'Cr-Commit-Position: refs/heads/master@{#13}') |
| 298 | |
Andrii Shyshkalov | 15e50cc | 2016-12-02 14:34:08 +0100 | [diff] [blame] | 299 | def test_git_number_same_branch_with_originals(self): |
| 300 | actual = self._test_git_number( |
| 301 | 'Parent\n' |
| 302 | '\n' |
| 303 | 'Cr-Commit-Position: refs/heads/master@{#12}', |
| 304 | dest_ref='refs/heads/master', |
| 305 | child_msg='Child\n' |
| 306 | '\n' |
| 307 | 'Some users are smart and insert their own footers\n' |
| 308 | '\n' |
| 309 | 'Cr-Whatever: value\n' |
| 310 | 'Cr-Commit-Position: refs/copy/paste@{#22}') |
| 311 | self.assertEqualByLine( |
| 312 | actual, |
| 313 | 'Child\n' |
| 314 | '\n' |
| 315 | 'Some users are smart and insert their own footers\n' |
| 316 | '\n' |
| 317 | 'Cr-Original-Whatever: value\n' |
| 318 | 'Cr-Original-Commit-Position: refs/copy/paste@{#22}\n' |
| 319 | 'Cr-Commit-Position: refs/heads/master@{#13}') |
| 320 | |
| 321 | def test_git_number_new_branch(self): |
| 322 | actual = self._test_git_number( |
| 323 | 'Parent\n' |
| 324 | '\n' |
| 325 | 'Cr-Commit-Position: refs/heads/master@{#12}', |
| 326 | dest_ref='refs/heads/branch', |
| 327 | child_msg='Child') |
| 328 | self.assertEqualByLine( |
| 329 | actual, |
| 330 | 'Child\n' |
| 331 | '\n' |
| 332 | 'Cr-Commit-Position: refs/heads/branch@{#1}\n' |
| 333 | 'Cr-Branched-From: parenthash-refs/heads/master@{#12}') |
| 334 | |
| 335 | def test_git_number_lineage(self): |
| 336 | actual = self._test_git_number( |
| 337 | 'Parent\n' |
| 338 | '\n' |
| 339 | 'Cr-Commit-Position: refs/heads/branch@{#1}\n' |
| 340 | 'Cr-Branched-From: somehash-refs/heads/master@{#12}', |
| 341 | dest_ref='refs/heads/branch', |
| 342 | child_msg='Child') |
| 343 | self.assertEqualByLine( |
| 344 | actual, |
| 345 | 'Child\n' |
| 346 | '\n' |
| 347 | 'Cr-Commit-Position: refs/heads/branch@{#2}\n' |
| 348 | 'Cr-Branched-From: somehash-refs/heads/master@{#12}') |
| 349 | |
| 350 | def test_git_number_moooooooore_lineage(self): |
| 351 | actual = self._test_git_number( |
| 352 | 'Parent\n' |
| 353 | '\n' |
| 354 | 'Cr-Commit-Position: refs/heads/branch@{#5}\n' |
| 355 | 'Cr-Branched-From: somehash-refs/heads/master@{#12}', |
| 356 | dest_ref='refs/heads/mooore', |
| 357 | child_msg='Child') |
| 358 | self.assertEqualByLine( |
| 359 | actual, |
| 360 | 'Child\n' |
| 361 | '\n' |
| 362 | 'Cr-Commit-Position: refs/heads/mooore@{#1}\n' |
| 363 | 'Cr-Branched-From: parenthash-refs/heads/branch@{#5}\n' |
| 364 | 'Cr-Branched-From: somehash-refs/heads/master@{#12}') |
| 365 | |
Andrii Shyshkalov | b5effa1 | 2016-12-14 19:35:12 +0100 | [diff] [blame] | 366 | def test_git_number_ever_moooooooore_lineage(self): |
| 367 | self.maxDiff = 10000 |
| 368 | actual = self._test_git_number( |
| 369 | 'CQ commit on fresh new branch + numbering.\n' |
| 370 | '\n' |
| 371 | 'NOTRY=True\n' |
| 372 | 'NOPRESUBMIT=True\n' |
| 373 | 'BUG=\n' |
| 374 | '\n' |
| 375 | 'Review-Url: https://codereview.chromium.org/2577703003\n' |
| 376 | 'Cr-Commit-Position: refs/heads/gnumb-test/br@{#1}\n' |
| 377 | 'Cr-Branched-From: 0749ff9edc-refs/heads/gnumb-test/cq@{#4}\n' |
| 378 | 'Cr-Branched-From: 5c49df2da6-refs/heads/master@{#41618}', |
| 379 | dest_ref='refs/heads/gnumb-test/cl', |
| 380 | child_msg='git cl on fresh new branch + numbering.\n' |
| 381 | '\n' |
| 382 | 'Review-Url: https://codereview.chromium.org/2575043003 .\n') |
| 383 | self.assertEqualByLine( |
| 384 | actual, |
| 385 | 'git cl on fresh new branch + numbering.\n' |
| 386 | '\n' |
| 387 | 'Review-Url: https://codereview.chromium.org/2575043003 .\n' |
| 388 | 'Cr-Commit-Position: refs/heads/gnumb-test/cl@{#1}\n' |
| 389 | 'Cr-Branched-From: parenthash-refs/heads/gnumb-test/br@{#1}\n' |
| 390 | 'Cr-Branched-From: 0749ff9edc-refs/heads/gnumb-test/cq@{#4}\n' |
| 391 | 'Cr-Branched-From: 5c49df2da6-refs/heads/master@{#41618}') |
Andrii Shyshkalov | 15e50cc | 2016-12-02 14:34:08 +0100 | [diff] [blame] | 392 | |
| 393 | def test_git_number_cherry_pick(self): |
| 394 | actual = self._test_git_number( |
| 395 | 'Parent\n' |
| 396 | '\n' |
| 397 | 'Cr-Commit-Position: refs/heads/branch@{#1}\n' |
| 398 | 'Cr-Branched-From: somehash-refs/heads/master@{#12}', |
| 399 | dest_ref='refs/heads/branch', |
| 400 | child_msg='Child, which is cherry-pick from master\n' |
| 401 | '\n' |
| 402 | 'Cr-Commit-Position: refs/heads/master@{#100}\n' |
| 403 | '(cherry picked from commit deadbeef12345678deadbeef12345678deadbeef)') |
| 404 | self.assertEqualByLine( |
| 405 | actual, |
| 406 | 'Child, which is cherry-pick from master\n' |
| 407 | '\n' |
| 408 | '(cherry picked from commit deadbeef12345678deadbeef12345678deadbeef)\n' |
| 409 | '\n' |
| 410 | 'Cr-Original-Commit-Position: refs/heads/master@{#100}\n' |
| 411 | 'Cr-Commit-Position: refs/heads/branch@{#2}\n' |
| 412 | 'Cr-Branched-From: somehash-refs/heads/master@{#12}') |
| 413 | |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 414 | |
Andrii Shyshkalov | 90f3192 | 2017-04-10 16:10:21 +0200 | [diff] [blame] | 415 | class TestParseIssueURL(unittest.TestCase): |
| 416 | def _validate(self, parsed, issue=None, patchset=None, hostname=None, |
| 417 | codereview=None, fail=False): |
| 418 | self.assertIsNotNone(parsed) |
| 419 | if fail: |
| 420 | self.assertFalse(parsed.valid) |
| 421 | return |
| 422 | self.assertTrue(parsed.valid) |
| 423 | self.assertEqual(parsed.issue, issue) |
| 424 | self.assertEqual(parsed.patchset, patchset) |
| 425 | self.assertEqual(parsed.hostname, hostname) |
| 426 | self.assertEqual(parsed.codereview, codereview) |
| 427 | |
| 428 | def _run_and_validate(self, func, url, *args, **kwargs): |
| 429 | result = func(urlparse.urlparse(url)) |
| 430 | if kwargs.pop('fail', False): |
| 431 | self.assertIsNone(result) |
| 432 | return None |
| 433 | self._validate(result, *args, fail=False, **kwargs) |
| 434 | |
| 435 | def test_rietveld(self): |
| 436 | def test(url, *args, **kwargs): |
| 437 | self._run_and_validate(git_cl._RietveldChangelistImpl.ParseIssueURL, url, |
| 438 | *args, codereview='rietveld', **kwargs) |
| 439 | |
| 440 | test('http://codereview.chromium.org/123', |
| 441 | 123, None, 'codereview.chromium.org') |
| 442 | test('https://codereview.chromium.org/123', |
| 443 | 123, None, 'codereview.chromium.org') |
| 444 | test('https://codereview.chromium.org/123/', |
| 445 | 123, None, 'codereview.chromium.org') |
| 446 | test('https://codereview.chromium.org/123/whatever', |
| 447 | 123, None, 'codereview.chromium.org') |
| 448 | test('https://codereview.chromium.org/123/#ps20001', |
| 449 | 123, 20001, 'codereview.chromium.org') |
| 450 | test('http://codereview.chromium.org/download/issue123_4.diff', |
| 451 | 123, 4, 'codereview.chromium.org') |
| 452 | # This looks like bad Gerrit, but is actually valid Rietveld, too. |
| 453 | test('https://chrome-review.source.com/123/4/', |
| 454 | 123, None, 'chrome-review.source.com') |
| 455 | |
| 456 | test('https://codereview.chromium.org/deadbeaf', fail=True) |
| 457 | test('https://codereview.chromium.org/api/123', fail=True) |
| 458 | test('bad://codereview.chromium.org/123', fail=True) |
| 459 | test('http://codereview.chromium.org/download/issue123_4.diffff', fail=True) |
| 460 | |
| 461 | def test_gerrit(self): |
| 462 | def test(url, issue=None, patchset=None, hostname=None, fail=None): |
| 463 | self._test_ParseIssueUrl( |
| 464 | git_cl._GerritChangelistImpl.ParseIssueURL, |
| 465 | url, issue, patchset, hostname, fail) |
| 466 | def test(url, *args, **kwargs): |
| 467 | self._run_and_validate(git_cl._GerritChangelistImpl.ParseIssueURL, url, |
| 468 | *args, codereview='gerrit', **kwargs) |
| 469 | |
| 470 | test('http://chrome-review.source.com/c/123', |
| 471 | 123, None, 'chrome-review.source.com') |
| 472 | test('https://chrome-review.source.com/c/123/', |
| 473 | 123, None, 'chrome-review.source.com') |
| 474 | test('https://chrome-review.source.com/c/123/4', |
| 475 | 123, 4, 'chrome-review.source.com') |
| 476 | test('https://chrome-review.source.com/#/c/123/4', |
| 477 | 123, 4, 'chrome-review.source.com') |
| 478 | test('https://chrome-review.source.com/c/123/4', |
| 479 | 123, 4, 'chrome-review.source.com') |
| 480 | test('https://chrome-review.source.com/123', |
| 481 | 123, None, 'chrome-review.source.com') |
| 482 | test('https://chrome-review.source.com/123/4', |
| 483 | 123, 4, 'chrome-review.source.com') |
| 484 | |
| 485 | test('https://chrome-review.source.com/c/123/1/whatisthis', fail=True) |
| 486 | test('https://chrome-review.source.com/c/abc/', fail=True) |
| 487 | test('ssh://chrome-review.source.com/c/123/1/', fail=True) |
| 488 | |
| 489 | def test_ParseIssueNumberArgument(self): |
| 490 | def test(arg, *args, **kwargs): |
Andrii Shyshkalov | c971239 | 2017-04-11 13:35:21 +0200 | [diff] [blame] | 491 | codereview_hint = kwargs.pop('hint', None) |
| 492 | self._validate(git_cl.ParseIssueNumberArgument(arg, codereview_hint), |
| 493 | *args, **kwargs) |
Andrii Shyshkalov | 90f3192 | 2017-04-10 16:10:21 +0200 | [diff] [blame] | 494 | |
| 495 | test('123', 123) |
| 496 | test('', fail=True) |
| 497 | test('abc', fail=True) |
| 498 | test('123/1', fail=True) |
| 499 | test('123a', fail=True) |
| 500 | test('ssh://chrome-review.source.com/#/c/123/4/', fail=True) |
| 501 | # Rietveld. |
| 502 | test('https://codereview.source.com/www123', fail=True) |
Andrii Shyshkalov | c971239 | 2017-04-11 13:35:21 +0200 | [diff] [blame] | 503 | # Matches both, but we take Rietveld now by default. |
Andrii Shyshkalov | 90f3192 | 2017-04-10 16:10:21 +0200 | [diff] [blame] | 504 | test('https://codereview.source.com/123', |
| 505 | 123, None, 'codereview.source.com', 'rietveld') |
Andrii Shyshkalov | c971239 | 2017-04-11 13:35:21 +0200 | [diff] [blame] | 506 | # Matches both, but we take Gerrit if specifically requested. |
| 507 | test('https://codereview.source.com/123', |
| 508 | 123, None, 'codereview.source.com', 'gerrit', |
| 509 | hint='gerrit') |
Andrii Shyshkalov | 90f3192 | 2017-04-10 16:10:21 +0200 | [diff] [blame] | 510 | # Gerrrit. |
| 511 | test('https://chrome-review.source.com/c/123/4', |
| 512 | 123, 4, 'chrome-review.source.com', 'gerrit') |
| 513 | test('https://chrome-review.source.com/bad/123/4', fail=True) |
| 514 | |
| 515 | |
Andrii Shyshkalov | 0a0b067 | 2017-03-16 16:27:48 +0100 | [diff] [blame] | 516 | class GitCookiesCheckerTest(TestCase): |
Andrii Shyshkalov | 9780050 | 2017-03-16 16:04:32 +0100 | [diff] [blame] | 517 | def setUp(self): |
| 518 | super(GitCookiesCheckerTest, self).setUp() |
| 519 | self.c = git_cl._GitCookiesChecker() |
Andrii Shyshkalov | 0a0b067 | 2017-03-16 16:27:48 +0100 | [diff] [blame] | 520 | self.c._all_hosts = [] |
Andrii Shyshkalov | 9780050 | 2017-03-16 16:04:32 +0100 | [diff] [blame] | 521 | |
| 522 | def mock_hosts_creds(self, subhost_identity_pairs): |
| 523 | def ensure_googlesource(h): |
| 524 | if not h.endswith(self.c._GOOGLESOURCE): |
| 525 | assert not h.endswith('.') |
| 526 | return h + '.' + self.c._GOOGLESOURCE |
| 527 | return h |
| 528 | self.c._all_hosts = [(ensure_googlesource(h), i, '.gitcookies') |
| 529 | for h, i in subhost_identity_pairs] |
| 530 | |
Andrii Shyshkalov | 0d2dea0 | 2017-07-17 15:17:55 +0200 | [diff] [blame] | 531 | def test_identity_parsing(self): |
| 532 | self.assertEqual(self.c._parse_identity('ldap.google.com'), |
| 533 | ('ldap', 'google.com')) |
| 534 | self.assertEqual(self.c._parse_identity('git-ldap.example.com'), |
| 535 | ('ldap', 'example.com')) |
| 536 | # Specical case because we know there are no subdomains in chromium.org. |
| 537 | self.assertEqual(self.c._parse_identity('git-note.period.chromium.org'), |
| 538 | ('note.period', 'chromium.org')) |
| 539 | # Pathological: .period. can be either ldap OR domain, more likely domain. |
| 540 | self.assertEqual(self.c._parse_identity('git-note.period.example.com'), |
| 541 | ('note', 'period.example.com')) |
| 542 | |
Andrii Shyshkalov | 9780050 | 2017-03-16 16:04:32 +0100 | [diff] [blame] | 543 | def test_analysis_nothing(self): |
| 544 | self.c._all_hosts = [] |
| 545 | self.assertFalse(self.c.has_generic_host()) |
| 546 | self.assertEqual(set(), self.c.get_conflicting_hosts()) |
| 547 | self.assertEqual(set(), self.c.get_duplicated_hosts()) |
| 548 | self.assertEqual(set(), self.c.get_partially_configured_hosts()) |
| 549 | self.assertEqual(set(), self.c.get_hosts_with_wrong_identities()) |
| 550 | |
| 551 | def test_analysis(self): |
| 552 | self.mock_hosts_creds([ |
| 553 | ('.googlesource.com', 'git-example.chromium.org'), |
| 554 | |
| 555 | ('chromium', 'git-example.google.com'), |
| 556 | ('chromium-review', 'git-example.google.com'), |
| 557 | ('chrome-internal', 'git-example.chromium.org'), |
| 558 | ('chrome-internal-review', 'git-example.chromium.org'), |
| 559 | ('conflict', 'git-example.google.com'), |
| 560 | ('conflict-review', 'git-example.chromium.org'), |
| 561 | ('dup', 'git-example.google.com'), |
| 562 | ('dup', 'git-example.google.com'), |
| 563 | ('dup-review', 'git-example.google.com'), |
| 564 | ('partial', 'git-example.google.com'), |
Andrii Shyshkalov | c817382 | 2017-07-10 12:10:53 +0200 | [diff] [blame] | 565 | ('gpartial-review', 'git-example.google.com'), |
Andrii Shyshkalov | 9780050 | 2017-03-16 16:04:32 +0100 | [diff] [blame] | 566 | ]) |
| 567 | self.assertTrue(self.c.has_generic_host()) |
| 568 | self.assertEqual(set(['conflict.googlesource.com']), |
| 569 | self.c.get_conflicting_hosts()) |
| 570 | self.assertEqual(set(['dup.googlesource.com']), |
| 571 | self.c.get_duplicated_hosts()) |
Andrii Shyshkalov | c817382 | 2017-07-10 12:10:53 +0200 | [diff] [blame] | 572 | self.assertEqual(set(['partial.googlesource.com', |
| 573 | 'gpartial-review.googlesource.com']), |
Andrii Shyshkalov | 9780050 | 2017-03-16 16:04:32 +0100 | [diff] [blame] | 574 | self.c.get_partially_configured_hosts()) |
| 575 | self.assertEqual(set(['chromium.googlesource.com', |
| 576 | 'chrome-internal.googlesource.com']), |
| 577 | self.c.get_hosts_with_wrong_identities()) |
| 578 | |
Andrii Shyshkalov | 0a0b067 | 2017-03-16 16:27:48 +0100 | [diff] [blame] | 579 | def test_report_no_problems(self): |
| 580 | self.test_analysis_nothing() |
| 581 | self.mock(sys, 'stdout', StringIO.StringIO()) |
| 582 | self.assertFalse(self.c.find_and_report_problems()) |
| 583 | self.assertEqual(sys.stdout.getvalue(), '') |
| 584 | |
| 585 | def test_report(self): |
| 586 | self.test_analysis() |
| 587 | self.mock(sys, 'stdout', StringIO.StringIO()) |
Andrii Shyshkalov | c817382 | 2017-07-10 12:10:53 +0200 | [diff] [blame] | 588 | self.mock(git_cl.gerrit_util.CookiesAuthenticator, 'get_gitcookies_path', |
| 589 | classmethod(lambda _: '~/.gitcookies')) |
Andrii Shyshkalov | 0a0b067 | 2017-03-16 16:27:48 +0100 | [diff] [blame] | 590 | self.assertTrue(self.c.find_and_report_problems()) |
| 591 | with open(os.path.join(os.path.dirname(__file__), |
| 592 | 'git_cl_creds_check_report.txt')) as f: |
| 593 | expected = f.read() |
| 594 | def by_line(text): |
| 595 | return [l.rstrip() for l in text.rstrip().splitlines()] |
Andrii Shyshkalov | c817382 | 2017-07-10 12:10:53 +0200 | [diff] [blame] | 596 | self.maxDiff = 10000 |
Andrii Shyshkalov | 4812e61 | 2017-03-27 17:22:57 +0200 | [diff] [blame] | 597 | self.assertEqual(by_line(sys.stdout.getvalue().strip()), by_line(expected)) |
Andrii Shyshkalov | 9780050 | 2017-03-16 16:04:32 +0100 | [diff] [blame] | 598 | |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 599 | class TestGitCl(TestCase): |
| 600 | def setUp(self): |
| 601 | super(TestGitCl, self).setUp() |
| 602 | self.calls = [] |
tandrii | 9d20675 | 2016-06-20 11:32:47 -0700 | [diff] [blame] | 603 | self._calls_done = [] |
maruel@chromium.org | 2e72bb1 | 2012-01-17 15:18:35 +0000 | [diff] [blame] | 604 | self.mock(subprocess2, 'call', self._mocked_call) |
| 605 | self.mock(subprocess2, 'check_call', self._mocked_call) |
| 606 | self.mock(subprocess2, 'check_output', self._mocked_call) |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 607 | self.mock(subprocess2, 'communicate', |
| 608 | lambda *a, **kw: ([self._mocked_call(*a, **kw), ''], 0)) |
tandrii@chromium.org | a342c92 | 2016-03-16 07:08:25 +0000 | [diff] [blame] | 609 | self.mock(git_cl.gclient_utils, 'CheckCallAndFilter', self._mocked_call) |
sbc@chromium.org | 71437c0 | 2015-04-09 19:29:40 +0000 | [diff] [blame] | 610 | self.mock(git_common, 'is_dirty_git_tree', lambda x: False) |
iannucci@chromium.org | 9e84927 | 2014-04-04 00:31:55 +0000 | [diff] [blame] | 611 | self.mock(git_common, 'get_or_create_merge_base', |
| 612 | lambda *a: ( |
| 613 | self._mocked_call(['get_or_create_merge_base']+list(a)))) |
pgervais@chromium.org | 8ba38ff | 2015-06-11 21:41:25 +0000 | [diff] [blame] | 614 | self.mock(git_cl, 'BranchExists', lambda _: True) |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 615 | self.mock(git_cl, 'FindCodereviewSettingsFile', lambda: '') |
Andrii Shyshkalov | abc26ac | 2017-03-14 14:49:38 +0100 | [diff] [blame] | 616 | self.mock(git_cl, 'ask_for_data', lambda *a, **k: self._mocked_call( |
| 617 | *(['ask_for_data'] + list(a)), **k)) |
phajdan.jr | e328cf9 | 2016-08-22 04:12:17 -0700 | [diff] [blame] | 618 | self.mock(git_cl, 'write_json', lambda path, contents: |
| 619 | self._mocked_call('write_json', path, contents)) |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 620 | self.mock(git_cl.presubmit_support, 'DoPresubmitChecks', PresubmitMock) |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 621 | self.mock(git_cl.rietveld, 'Rietveld', RietveldMock) |
maruel@chromium.org | 4bac4b5 | 2012-11-27 20:33:52 +0000 | [diff] [blame] | 622 | self.mock(git_cl.rietveld, 'CachingRietveld', RietveldMock) |
skobes | 6468b90 | 2016-10-24 08:45:10 -0700 | [diff] [blame] | 623 | self.mock(git_cl.checkout, 'GitCheckout', GitCheckoutMock) |
| 624 | GitCheckoutMock.reset() |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 625 | self.mock(git_cl.upload, 'RealMain', self.fail) |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 626 | self.mock(git_cl.watchlists, 'Watchlists', WatchlistsMock) |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 627 | self.mock(git_cl.auth, 'get_authenticator_for_host', AuthenticatorMock) |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 628 | self.mock(git_cl.gerrit_util, 'GetChangeDetail', |
| 629 | lambda *args, **kwargs: self._mocked_call( |
| 630 | 'GetChangeDetail', *args, **kwargs)) |
Aaron Gable | 0ffdf2d | 2017-06-05 13:01:17 -0700 | [diff] [blame] | 631 | self.mock(git_cl.gerrit_util, 'GetChangeComments', |
| 632 | lambda *args, **kwargs: self._mocked_call( |
| 633 | 'GetChangeComments', *args, **kwargs)) |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 634 | self.mock(git_cl.gerrit_util, 'AddReviewers', |
Aaron Gable | 6dadfbf | 2017-05-09 14:27:58 -0700 | [diff] [blame] | 635 | lambda h, i, reviewers, ccs, notify: self._mocked_call( |
| 636 | 'AddReviewers', h, i, reviewers, ccs, notify)) |
Aaron Gable | fd23808 | 2017-06-07 13:42:34 -0700 | [diff] [blame] | 637 | self.mock(git_cl.gerrit_util, 'SetReview', |
Aaron Gable | fc62f76 | 2017-07-17 11:12:07 -0700 | [diff] [blame] | 638 | lambda h, i, msg=None, labels=None, notify=None: |
| 639 | self._mocked_call('SetReview', h, i, msg, labels, notify)) |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 640 | self.mock(git_cl.gerrit_util.GceAuthenticator, 'is_gce', |
| 641 | classmethod(lambda _: False)) |
tandrii | c2405f5 | 2016-10-10 08:13:15 -0700 | [diff] [blame] | 642 | self.mock(git_cl, 'DieWithError', |
Christopher Lam | f732cd5 | 2017-01-24 12:40:11 +1100 | [diff] [blame] | 643 | lambda msg, change=None: self._mocked_call(['DieWithError', msg])) |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 644 | # It's important to reset settings to not have inter-tests interference. |
| 645 | git_cl.settings = None |
| 646 | |
| 647 | def tearDown(self): |
wychen@chromium.org | 445c896 | 2015-04-28 23:30:05 +0000 | [diff] [blame] | 648 | try: |
Andrii Shyshkalov | 18df0cd | 2017-01-25 15:22:20 +0100 | [diff] [blame] | 649 | self.assertEquals([], self.calls) |
| 650 | except AssertionError: |
wychen@chromium.org | 445c896 | 2015-04-28 23:30:05 +0000 | [diff] [blame] | 651 | if not self.has_failed(): |
Andrii Shyshkalov | 18df0cd | 2017-01-25 15:22:20 +0100 | [diff] [blame] | 652 | raise |
| 653 | # Sadly, has_failed() returns True if this OR any other tests before this |
| 654 | # one have failed. |
Andrii Shyshkalov | e05d488 | 2017-04-12 14:34:49 +0200 | [diff] [blame] | 655 | git_cl.logging.error( |
| 656 | '!!!!!! IF YOU SEE THIS, READ BELOW, IT WILL SAVE YOUR TIME !!!!!\n' |
Andrii Shyshkalov | 18df0cd | 2017-01-25 15:22:20 +0100 | [diff] [blame] | 657 | 'There are un-consumed self.calls after this test has finished.\n' |
| 658 | 'If you don\'t know which test this is, run:\n' |
Aaron Gable | 3a16ed1 | 2017-03-23 10:51:55 -0700 | [diff] [blame] | 659 | ' tests/git_cl_tests.py -v\n' |
Andrii Shyshkalov | e05d488 | 2017-04-12 14:34:49 +0200 | [diff] [blame] | 660 | 'If you are already running only this test, then **first** fix the ' |
| 661 | 'problem whose exception is emitted below by unittest runner.\n' |
Andrii Shyshkalov | 18df0cd | 2017-01-25 15:22:20 +0100 | [diff] [blame] | 662 | 'Else, to be sure what\'s going on, run this test **alone** with \n' |
Aaron Gable | 3a16ed1 | 2017-03-23 10:51:55 -0700 | [diff] [blame] | 663 | ' tests/git_cl_tests.py TestGitCl.<name>\n' |
Andrii Shyshkalov | 18df0cd | 2017-01-25 15:22:20 +0100 | [diff] [blame] | 664 | 'and follow instructions above.\n' + |
| 665 | '=' * 80) |
wychen@chromium.org | 445c896 | 2015-04-28 23:30:05 +0000 | [diff] [blame] | 666 | finally: |
| 667 | super(TestGitCl, self).tearDown() |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 668 | |
iannucci@chromium.org | 9e84927 | 2014-04-04 00:31:55 +0000 | [diff] [blame] | 669 | def _mocked_call(self, *args, **_kwargs): |
maruel@chromium.org | 2e72bb1 | 2012-01-17 15:18:35 +0000 | [diff] [blame] | 670 | self.assertTrue( |
| 671 | self.calls, |
tandrii | 9d20675 | 2016-06-20 11:32:47 -0700 | [diff] [blame] | 672 | '@%d Expected: <Missing> Actual: %r' % (len(self._calls_done), args)) |
wychen@chromium.org | a872e75 | 2015-04-28 23:42:18 +0000 | [diff] [blame] | 673 | top = self.calls.pop(0) |
wychen@chromium.org | a872e75 | 2015-04-28 23:42:18 +0000 | [diff] [blame] | 674 | expected_args, result = top |
| 675 | |
maruel@chromium.org | e52678e | 2013-04-26 18:34:44 +0000 | [diff] [blame] | 676 | # Also logs otherwise it could get caught in a try/finally and be hard to |
| 677 | # diagnose. |
| 678 | if expected_args != args: |
tandrii | 9d20675 | 2016-06-20 11:32:47 -0700 | [diff] [blame] | 679 | N = 5 |
| 680 | prior_calls = '\n '.join( |
| 681 | '@%d: %r' % (len(self._calls_done) - N + i, c[0]) |
| 682 | for i, c in enumerate(self._calls_done[-N:])) |
| 683 | following_calls = '\n '.join( |
| 684 | '@%d: %r' % (len(self._calls_done) + i + 1, c[0]) |
| 685 | for i, c in enumerate(self.calls[:N])) |
| 686 | extended_msg = ( |
| 687 | 'A few prior calls:\n %s\n\n' |
| 688 | 'This (expected):\n @%d: %r\n' |
| 689 | 'This (actual):\n @%d: %r\n\n' |
| 690 | 'A few following expected calls:\n %s' % |
| 691 | (prior_calls, len(self._calls_done), expected_args, |
| 692 | len(self._calls_done), args, following_calls)) |
| 693 | git_cl.logging.error(extended_msg) |
| 694 | |
tandrii | 99a72f2 | 2016-08-17 14:33:24 -0700 | [diff] [blame] | 695 | self.fail('@%d\n' |
| 696 | ' Expected: %r\n' |
| 697 | ' Actual: %r' % ( |
tandrii | 9d20675 | 2016-06-20 11:32:47 -0700 | [diff] [blame] | 698 | len(self._calls_done), expected_args, args)) |
| 699 | |
| 700 | self._calls_done.append(top) |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 701 | if isinstance(result, Exception): |
| 702 | raise result |
maruel@chromium.org | 2e72bb1 | 2012-01-17 15:18:35 +0000 | [diff] [blame] | 703 | return result |
| 704 | |
Andrii Shyshkalov | abc26ac | 2017-03-14 14:49:38 +0100 | [diff] [blame] | 705 | def test_ask_for_explicit_yes_true(self): |
| 706 | self.calls = [ |
| 707 | (('ask_for_data', 'prompt [Yes/No]: '), 'blah'), |
| 708 | (('ask_for_data', 'Please, type yes or no: '), 'ye'), |
| 709 | ] |
| 710 | self.assertTrue(git_cl.ask_for_explicit_yes('prompt')) |
| 711 | |
| 712 | def test_ask_for_explicit_yes_true(self): |
| 713 | self.calls = [ |
| 714 | (('ask_for_data', 'prompt [Yes/No]: '), 'yesish'), |
| 715 | (('ask_for_data', 'Please, type yes or no: '), 'nO'), |
| 716 | ] |
| 717 | self.assertFalse(git_cl.ask_for_explicit_yes('prompt')) |
| 718 | |
tandrii | 48df581 | 2016-10-17 03:55:37 -0700 | [diff] [blame] | 719 | def test_LoadCodereviewSettingsFromFile_gerrit(self): |
| 720 | codereview_file = StringIO.StringIO('GERRIT_HOST: true') |
| 721 | self.calls = [ |
| 722 | ((['git', 'config', '--unset-all', 'rietveld.cc'],), CERR1), |
| 723 | ((['git', 'config', '--unset-all', 'rietveld.private'],), CERR1), |
| 724 | ((['git', 'config', '--unset-all', 'rietveld.tree-status-url'],), CERR1), |
| 725 | ((['git', 'config', '--unset-all', 'rietveld.viewvc-url'],), CERR1), |
| 726 | ((['git', 'config', '--unset-all', 'rietveld.bug-prefix'],), CERR1), |
| 727 | ((['git', 'config', '--unset-all', 'rietveld.cpplint-regex'],), CERR1), |
tandrii | 48df581 | 2016-10-17 03:55:37 -0700 | [diff] [blame] | 728 | ((['git', 'config', '--unset-all', 'rietveld.cpplint-ignore-regex'],), |
| 729 | CERR1), |
| 730 | ((['git', 'config', '--unset-all', 'rietveld.project'],), CERR1), |
tandrii | 48df581 | 2016-10-17 03:55:37 -0700 | [diff] [blame] | 731 | ((['git', 'config', '--unset-all', 'rietveld.run-post-upload-hook'],), |
| 732 | CERR1), |
| 733 | ((['git', 'config', 'gerrit.host', 'true'],), ''), |
| 734 | ] |
| 735 | self.assertIsNone(git_cl.LoadCodereviewSettingsFromFile(codereview_file)) |
| 736 | |
maruel@chromium.org | a335365 | 2011-11-30 14:26:57 +0000 | [diff] [blame] | 737 | @classmethod |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 738 | def _is_gerrit_calls(cls, gerrit=False): |
| 739 | return [((['git', 'config', 'rietveld.autoupdate'],), ''), |
| 740 | ((['git', 'config', 'gerrit.host'],), 'True' if gerrit else '')] |
| 741 | |
| 742 | @classmethod |
tyoshino@chromium.org | c1737d0 | 2013-05-29 14:17:28 +0000 | [diff] [blame] | 743 | def _upload_calls(cls, similarity, find_copies, private): |
Andrii Shyshkalov | 73827d1 | 2017-04-12 15:05:11 +0200 | [diff] [blame] | 744 | return (cls._rietveld_git_base_calls(similarity, find_copies) + |
tyoshino@chromium.org | c1737d0 | 2013-05-29 14:17:28 +0000 | [diff] [blame] | 745 | cls._git_upload_calls(private)) |
maruel@chromium.org | a335365 | 2011-11-30 14:26:57 +0000 | [diff] [blame] | 746 | |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 747 | @classmethod |
Andrii Shyshkalov | 73827d1 | 2017-04-12 15:05:11 +0200 | [diff] [blame] | 748 | def _rietveld_upload_no_rev_calls(cls, similarity, find_copies): |
| 749 | return (cls._rietveld_git_base_calls(similarity, find_copies) + [ |
| 750 | ((['git', 'config', 'core.editor'],), ''), |
| 751 | ]) |
jbroman@chromium.org | 615a262 | 2013-05-03 13:20:14 +0000 | [diff] [blame] | 752 | |
| 753 | @classmethod |
Andrii Shyshkalov | 73827d1 | 2017-04-12 15:05:11 +0200 | [diff] [blame] | 754 | def _rietveld_git_base_calls(cls, similarity, find_copies): |
iannucci@chromium.org | 53937ba | 2012-10-02 18:20:43 +0000 | [diff] [blame] | 755 | if similarity is None: |
| 756 | similarity = '50' |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 757 | similarity_call = ((['git', 'config', |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 758 | 'branch.master.git-cl-similarity'],), CERR1) |
iannucci@chromium.org | 53937ba | 2012-10-02 18:20:43 +0000 | [diff] [blame] | 759 | else: |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 760 | similarity_call = ((['git', 'config', |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 761 | 'branch.master.git-cl-similarity', similarity],), '') |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 762 | |
| 763 | if find_copies is None: |
| 764 | find_copies = True |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 765 | find_copies_call = ((['git', 'config', '--bool', |
| 766 | 'branch.master.git-find-copies'],), CERR1) |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 767 | else: |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 768 | val = str(find_copies).lower() |
| 769 | find_copies_call = ((['git', 'config', '--bool', |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 770 | 'branch.master.git-find-copies', val],), '') |
| 771 | |
| 772 | if find_copies: |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 773 | stat_call = ((['git', 'diff', '--no-ext-diff', '--stat', |
scottmg | b84b5e3 | 2016-11-10 09:25:33 -0800 | [diff] [blame] | 774 | '-l100000', '-C'+similarity, |
| 775 | 'fake_ancestor_sha', 'HEAD'],), '+dat') |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 776 | else: |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 777 | stat_call = ((['git', 'diff', '--no-ext-diff', '--stat', |
sbc@chromium.org | 5e07e06 | 2013-02-28 23:55:44 +0000 | [diff] [blame] | 778 | '-M'+similarity, 'fake_ancestor_sha', 'HEAD'],), '+dat') |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 779 | |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 780 | return [ |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 781 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
tandrii@chromium.org | 87985d2 | 2016-03-24 17:33:33 +0000 | [diff] [blame] | 782 | similarity_call, |
| 783 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
| 784 | find_copies_call, |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 785 | ] + cls._is_gerrit_calls() + [ |
tandrii@chromium.org | 87985d2 | 2016-03-24 17:33:33 +0000 | [diff] [blame] | 786 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 787 | ((['git', 'config', 'branch.master.rietveldissue'],), CERR1), |
| 788 | ((['git', 'config', 'branch.master.gerritissue'],), CERR1), |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 789 | ((['git', 'config', 'rietveld.server'],), |
| 790 | 'codereview.example.com'), |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 791 | ((['git', 'config', 'branch.master.merge'],), 'master'), |
| 792 | ((['git', 'config', 'branch.master.remote'],), 'origin'), |
iannucci@chromium.org | 9e84927 | 2014-04-04 00:31:55 +0000 | [diff] [blame] | 793 | ((['get_or_create_merge_base', 'master', 'master'],), |
bratell@opera.com | f267b0e | 2013-05-02 09:11:43 +0000 | [diff] [blame] | 794 | 'fake_ancestor_sha'), |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 795 | ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [ |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 796 | ((['git', 'rev-parse', '--show-cdup'],), ''), |
| 797 | ((['git', 'rev-parse', 'HEAD'],), '12345'), |
| 798 | ((['git', 'diff', '--name-status', '--no-renames', '-r', |
bratell@opera.com | f267b0e | 2013-05-02 09:11:43 +0000 | [diff] [blame] | 799 | 'fake_ancestor_sha...', '.'],), |
maruel@chromium.org | 2e72bb1 | 2012-01-17 15:18:35 +0000 | [diff] [blame] | 800 | 'M\t.gitignore\n'), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 801 | ((['git', 'config', 'branch.master.rietveldpatchset'],), CERR1), |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 802 | ((['git', 'log', '--pretty=format:%s%n%n%b', |
bratell@opera.com | f267b0e | 2013-05-02 09:11:43 +0000 | [diff] [blame] | 803 | 'fake_ancestor_sha...'],), |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 804 | 'foo'), |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 805 | ((['git', 'config', 'user.email'],), 'me@example.com'), |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 806 | stat_call, |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 807 | ((['git', 'log', '--pretty=format:%s\n\n%b', |
bratell@opera.com | f267b0e | 2013-05-02 09:11:43 +0000 | [diff] [blame] | 808 | 'fake_ancestor_sha..HEAD'],), |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 809 | 'desc\n'), |
Aaron Gable | 3a16ed1 | 2017-03-23 10:51:55 -0700 | [diff] [blame] | 810 | ((['git', 'config', 'rietveld.bug-prefix'],), ''), |
maruel@chromium.org | a335365 | 2011-11-30 14:26:57 +0000 | [diff] [blame] | 811 | ] |
| 812 | |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 813 | @classmethod |
tyoshino@chromium.org | c1737d0 | 2013-05-29 14:17:28 +0000 | [diff] [blame] | 814 | def _git_upload_calls(cls, private): |
| 815 | if private: |
tyoshino@chromium.org | 99918ab | 2013-09-30 06:17:28 +0000 | [diff] [blame] | 816 | cc_call = [] |
tyoshino@chromium.org | c1737d0 | 2013-05-29 14:17:28 +0000 | [diff] [blame] | 817 | private_call = [] |
| 818 | else: |
tyoshino@chromium.org | 99918ab | 2013-09-30 06:17:28 +0000 | [diff] [blame] | 819 | cc_call = [((['git', 'config', 'rietveld.cc'],), '')] |
tyoshino@chromium.org | c1737d0 | 2013-05-29 14:17:28 +0000 | [diff] [blame] | 820 | private_call = [ |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 821 | ((['git', 'config', 'rietveld.private'],), '')] |
tyoshino@chromium.org | c1737d0 | 2013-05-29 14:17:28 +0000 | [diff] [blame] | 822 | |
maruel@chromium.org | a335365 | 2011-11-30 14:26:57 +0000 | [diff] [blame] | 823 | return [ |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 824 | ((['git', 'config', 'core.editor'],), ''), |
tyoshino@chromium.org | 99918ab | 2013-09-30 06:17:28 +0000 | [diff] [blame] | 825 | ] + cc_call + private_call + [ |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 826 | ((['git', 'config', 'branch.master.base-url'],), ''), |
Aaron Gable | 1bc7bfe | 2016-12-19 10:08:14 -0800 | [diff] [blame] | 827 | ((['git', 'config', 'remote.origin.url'],), ''), |
sheyang@chromium.org | 152cf83 | 2014-06-11 21:37:49 +0000 | [diff] [blame] | 828 | ((['git', 'config', 'rietveld.project'],), ''), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 829 | ((['git', 'config', 'branch.master.rietveldissue', '1'],), ''), |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 830 | ((['git', 'config', 'branch.master.rietveldserver', |
tyoshino@chromium.org | c1737d0 | 2013-05-29 14:17:28 +0000 | [diff] [blame] | 831 | 'https://codereview.example.com'],), ''), |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 832 | ((['git', |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 833 | 'config', 'branch.master.rietveldpatchset', '2'],), ''), |
tandrii@chromium.org | 1e67bb7 | 2016-02-11 12:15:49 +0000 | [diff] [blame] | 834 | ] + cls._git_post_upload_calls() |
| 835 | |
| 836 | @classmethod |
| 837 | def _git_post_upload_calls(cls): |
| 838 | return [ |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 839 | ((['git', 'rev-parse', 'HEAD'],), 'hash'), |
| 840 | ((['git', 'symbolic-ref', 'HEAD'],), 'hash'), |
| 841 | ((['git', |
tyoshino@chromium.org | c1737d0 | 2013-05-29 14:17:28 +0000 | [diff] [blame] | 842 | 'config', 'branch.hash.last-upload-hash', 'hash'],), ''), |
rmistry@google.com | 5626a92 | 2015-02-26 14:03:30 +0000 | [diff] [blame] | 843 | ((['git', 'config', 'rietveld.run-post-upload-hook'],), ''), |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 844 | ] |
| 845 | |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 846 | @staticmethod |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 847 | def _git_sanity_checks(diff_base, working_branch, get_remote_branch=True): |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 848 | fake_ancestor = 'fake_ancestor' |
| 849 | fake_cl = 'fake_cl_for_patch' |
| 850 | return [ |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 851 | ((['git', |
bratell@opera.com | f267b0e | 2013-05-02 09:11:43 +0000 | [diff] [blame] | 852 | 'rev-parse', '--verify', diff_base],), fake_ancestor), |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 853 | ((['git', |
bratell@opera.com | f267b0e | 2013-05-02 09:11:43 +0000 | [diff] [blame] | 854 | 'merge-base', fake_ancestor, 'HEAD'],), fake_ancestor), |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 855 | ((['git', |
bratell@opera.com | f267b0e | 2013-05-02 09:11:43 +0000 | [diff] [blame] | 856 | 'rev-list', '^' + fake_ancestor, 'HEAD'],), fake_cl), |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 857 | # Mock a config miss (error code 1) |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 858 | ((['git', |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 859 | 'config', 'gitcl.remotebranch'],), CERR1), |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 860 | ] + ([ |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 861 | # Call to GetRemoteBranch() |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 862 | ((['git', |
bratell@opera.com | f267b0e | 2013-05-02 09:11:43 +0000 | [diff] [blame] | 863 | 'config', 'branch.%s.merge' % working_branch],), |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 864 | 'refs/heads/master'), |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 865 | ((['git', |
bratell@opera.com | f267b0e | 2013-05-02 09:11:43 +0000 | [diff] [blame] | 866 | 'config', 'branch.%s.remote' % working_branch],), 'origin'), |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 867 | ] if get_remote_branch else []) + [ |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 868 | ((['git', 'rev-list', '^' + fake_ancestor, |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 869 | 'refs/remotes/origin/master'],), ''), |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 870 | ] |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 871 | |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 872 | @staticmethod |
bradnelson | d975b30 | 2016-10-23 12:20:23 -0700 | [diff] [blame] | 873 | def _cmd_line(description, args, similarity, find_copies, private, cc): |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 874 | """Returns the upload command line passed to upload.RealMain().""" |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 875 | return [ |
| 876 | 'upload', '--assume_yes', '--server', |
maruel@chromium.org | eb5edbc | 2012-01-16 17:03:28 +0000 | [diff] [blame] | 877 | 'https://codereview.example.com', |
maruel@chromium.org | 71e12a9 | 2012-02-14 02:34:15 +0000 | [diff] [blame] | 878 | '--message', description |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 879 | ] + args + [ |
bradnelson | d975b30 | 2016-10-23 12:20:23 -0700 | [diff] [blame] | 880 | '--cc', ','.join(['joe@example.com'] + cc), |
tyoshino@chromium.org | c1737d0 | 2013-05-29 14:17:28 +0000 | [diff] [blame] | 881 | ] + (['--private'] if private else []) + [ |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 882 | '--git_similarity', similarity or '50' |
Andrii Shyshkalov | 1897532 | 2017-01-25 16:44:13 +0100 | [diff] [blame] | 883 | ] + (['--git_no_find_copies'] if find_copies is False else []) + [ |
sbc@chromium.org | 5e07e06 | 2013-02-28 23:55:44 +0000 | [diff] [blame] | 884 | 'fake_ancestor_sha', 'HEAD' |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 885 | ] |
| 886 | |
| 887 | def _run_reviewer_test( |
| 888 | self, |
| 889 | upload_args, |
| 890 | expected_description, |
| 891 | returned_description, |
| 892 | final_description, |
tyoshino@chromium.org | c1737d0 | 2013-05-29 14:17:28 +0000 | [diff] [blame] | 893 | reviewers, |
bradnelson | d975b30 | 2016-10-23 12:20:23 -0700 | [diff] [blame] | 894 | private=False, |
| 895 | cc=None): |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 896 | """Generic reviewer test framework.""" |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 897 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
iannucci@chromium.org | 53937ba | 2012-10-02 18:20:43 +0000 | [diff] [blame] | 898 | try: |
| 899 | similarity = upload_args[upload_args.index('--similarity')+1] |
| 900 | except ValueError: |
| 901 | similarity = None |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 902 | |
| 903 | if '--find-copies' in upload_args: |
| 904 | find_copies = True |
| 905 | elif '--no-find-copies' in upload_args: |
| 906 | find_copies = False |
| 907 | else: |
| 908 | find_copies = None |
| 909 | |
tyoshino@chromium.org | c1737d0 | 2013-05-29 14:17:28 +0000 | [diff] [blame] | 910 | private = '--private' in upload_args |
bradnelson | d975b30 | 2016-10-23 12:20:23 -0700 | [diff] [blame] | 911 | cc = cc or [] |
tyoshino@chromium.org | c1737d0 | 2013-05-29 14:17:28 +0000 | [diff] [blame] | 912 | |
| 913 | self.calls = self._upload_calls(similarity, find_copies, private) |
pgervais@chromium.org | 87884cc | 2014-01-03 22:23:41 +0000 | [diff] [blame] | 914 | |
jbroman@chromium.org | 615a262 | 2013-05-03 13:20:14 +0000 | [diff] [blame] | 915 | def RunEditor(desc, _, **kwargs): |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 916 | self.assertEquals( |
| 917 | '# Enter a description of the change.\n' |
janx@chromium.org | 104b2db | 2013-04-18 12:58:40 +0000 | [diff] [blame] | 918 | '# This will be displayed on the codereview site.\n' |
alancutter@chromium.org | 63a4d7f | 2013-05-31 02:22:45 +0000 | [diff] [blame] | 919 | '# The first line will also be used as the subject of the review.\n' |
alancutter@chromium.org | bd1073e | 2013-06-01 00:34:38 +0000 | [diff] [blame] | 920 | '#--------------------This line is 72 characters long' |
| 921 | '--------------------\n' + |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 922 | expected_description, |
| 923 | desc) |
| 924 | return returned_description |
| 925 | self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor) |
pgervais@chromium.org | 87884cc | 2014-01-03 22:23:41 +0000 | [diff] [blame] | 926 | |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 927 | def check_upload(args): |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 928 | cmd_line = self._cmd_line(final_description, reviewers, similarity, |
bradnelson | d975b30 | 2016-10-23 12:20:23 -0700 | [diff] [blame] | 929 | find_copies, private, cc) |
iannucci@chromium.org | 53937ba | 2012-10-02 18:20:43 +0000 | [diff] [blame] | 930 | self.assertEquals(cmd_line, args) |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 931 | return 1, 2 |
| 932 | self.mock(git_cl.upload, 'RealMain', check_upload) |
pgervais@chromium.org | 87884cc | 2014-01-03 22:23:41 +0000 | [diff] [blame] | 933 | |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 934 | git_cl.main(['upload'] + upload_args) |
| 935 | |
| 936 | def test_no_reviewer(self): |
| 937 | self._run_reviewer_test( |
| 938 | [], |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 939 | 'desc\n\nBUG=', |
| 940 | '# Blah blah comment.\ndesc\n\nBUG=', |
| 941 | 'desc\n\nBUG=', |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 942 | []) |
| 943 | |
iannucci@chromium.org | 53937ba | 2012-10-02 18:20:43 +0000 | [diff] [blame] | 944 | def test_keep_similarity(self): |
| 945 | self._run_reviewer_test( |
| 946 | ['--similarity', '70'], |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 947 | 'desc\n\nBUG=', |
| 948 | '# Blah blah comment.\ndesc\n\nBUG=', |
| 949 | 'desc\n\nBUG=', |
iannucci@chromium.org | 53937ba | 2012-10-02 18:20:43 +0000 | [diff] [blame] | 950 | []) |
| 951 | |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 952 | def test_keep_find_copies(self): |
| 953 | self._run_reviewer_test( |
| 954 | ['--no-find-copies'], |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 955 | 'desc\n\nBUG=', |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 956 | '# Blah blah comment.\ndesc\n\nBUG=\n', |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 957 | 'desc\n\nBUG=', |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 958 | []) |
| 959 | |
tyoshino@chromium.org | c1737d0 | 2013-05-29 14:17:28 +0000 | [diff] [blame] | 960 | def test_private(self): |
| 961 | self._run_reviewer_test( |
| 962 | ['--private'], |
| 963 | 'desc\n\nBUG=', |
| 964 | '# Blah blah comment.\ndesc\n\nBUG=\n', |
| 965 | 'desc\n\nBUG=', |
| 966 | []) |
| 967 | |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 968 | def test_reviewers_cmd_line(self): |
| 969 | # Reviewer is passed as-is |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 970 | description = 'desc\n\nR=foo@example.com\nBUG=' |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 971 | self._run_reviewer_test( |
| 972 | ['-r' 'foo@example.com'], |
| 973 | description, |
| 974 | '\n%s\n' % description, |
| 975 | description, |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 976 | ['--reviewers=foo@example.com']) |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 977 | |
| 978 | def test_reviewer_tbr_overriden(self): |
| 979 | # Reviewer is overriden with TBR |
| 980 | # Also verifies the regexp work without a trailing LF |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 981 | description = 'Foo Bar\n\nTBR=reviewer@example.com' |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 982 | self._run_reviewer_test( |
| 983 | ['-r' 'foo@example.com'], |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 984 | 'desc\n\nR=foo@example.com\nBUG=', |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 985 | description.strip('\n'), |
| 986 | description, |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 987 | ['--reviewers=reviewer@example.com']) |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 988 | |
| 989 | def test_reviewer_multiple(self): |
| 990 | # Handles multiple R= or TBR= lines. |
| 991 | description = ( |
bradnelson | d975b30 | 2016-10-23 12:20:23 -0700 | [diff] [blame] | 992 | 'Foo Bar\nTBR=reviewer@example.com\nBUG=\nR=another@example.com\n' |
| 993 | 'CC=more@example.com,people@example.com') |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 994 | self._run_reviewer_test( |
| 995 | [], |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 996 | 'desc\n\nBUG=', |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 997 | description, |
| 998 | description, |
bradnelson | d975b30 | 2016-10-23 12:20:23 -0700 | [diff] [blame] | 999 | ['--reviewers=another@example.com,reviewer@example.com'], |
| 1000 | cc=['more@example.com', 'people@example.com']) |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 1001 | |
maruel@chromium.org | a335365 | 2011-11-30 14:26:57 +0000 | [diff] [blame] | 1002 | def test_reviewer_send_mail(self): |
| 1003 | # --send-mail can be used without -r if R= is used |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 1004 | description = 'Foo Bar\nR=reviewer@example.com' |
maruel@chromium.org | a335365 | 2011-11-30 14:26:57 +0000 | [diff] [blame] | 1005 | self._run_reviewer_test( |
| 1006 | ['--send-mail'], |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 1007 | 'desc\n\nBUG=', |
maruel@chromium.org | a335365 | 2011-11-30 14:26:57 +0000 | [diff] [blame] | 1008 | description.strip('\n'), |
| 1009 | description, |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 1010 | ['--reviewers=reviewer@example.com', '--send_mail']) |
maruel@chromium.org | a335365 | 2011-11-30 14:26:57 +0000 | [diff] [blame] | 1011 | |
| 1012 | def test_reviewer_send_mail_no_rev(self): |
| 1013 | # Fails without a reviewer. |
maruel@chromium.org | 2e23ce3 | 2013-05-07 12:42:28 +0000 | [diff] [blame] | 1014 | stdout = StringIO.StringIO() |
Andrii Shyshkalov | 73827d1 | 2017-04-12 15:05:11 +0200 | [diff] [blame] | 1015 | self.calls = self._rietveld_upload_no_rev_calls(None, None) + [ |
tandrii | c2405f5 | 2016-10-10 08:13:15 -0700 | [diff] [blame] | 1016 | ((['DieWithError', 'Must specify reviewers to send email.'],), |
| 1017 | SystemExitMock()) |
| 1018 | ] |
| 1019 | |
| 1020 | def RunEditor(desc, _, **kwargs): |
| 1021 | return desc |
| 1022 | self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor) |
| 1023 | self.mock(sys, 'stdout', stdout) |
| 1024 | with self.assertRaises(SystemExitMock): |
maruel@chromium.org | a335365 | 2011-11-30 14:26:57 +0000 | [diff] [blame] | 1025 | git_cl.main(['upload', '--send-mail']) |
Aaron Gable | c4c40d1 | 2017-05-22 11:49:53 -0700 | [diff] [blame] | 1026 | self.assertEqual( |
| 1027 | '=====================================\n' |
| 1028 | 'NOTICE: Rietveld is being deprecated. ' |
| 1029 | 'You can upload changes to Gerrit with\n' |
| 1030 | ' git cl upload --gerrit\n' |
| 1031 | 'or set Gerrit to be your default code review tool with\n' |
| 1032 | ' git config gerrit.host true\n' |
| 1033 | '=====================================\n', |
| 1034 | stdout.getvalue()) |
maruel@chromium.org | a335365 | 2011-11-30 14:26:57 +0000 | [diff] [blame] | 1035 | |
tandrii | f9aefb7 | 2016-07-01 09:06:51 -0700 | [diff] [blame] | 1036 | def test_bug_on_cmd(self): |
| 1037 | self._run_reviewer_test( |
| 1038 | ['--bug=500658,proj:123'], |
| 1039 | 'desc\n\nBUG=500658\nBUG=proj:123', |
| 1040 | '# Blah blah comment.\ndesc\n\nBUG=500658\nBUG=proj:1234', |
| 1041 | 'desc\n\nBUG=500658\nBUG=proj:1234', |
| 1042 | []) |
| 1043 | |
Andrii Shyshkalov | aa31b97 | 2017-03-24 16:16:33 +0100 | [diff] [blame] | 1044 | def _land_rietveld_calls(self, repo_url, git_numberer_enabled=False, |
| 1045 | parent_msg=None, |
| 1046 | new_msg=None, |
| 1047 | fail_after_parent_msg=False, |
| 1048 | fail_cherry_pick=False, |
| 1049 | fail_push_count=0, |
| 1050 | debug=False): |
Andrii Shyshkalov | 768f1d8 | 2016-12-08 15:10:13 +0100 | [diff] [blame] | 1051 | if debug: |
| 1052 | # Very useful due to finally clause in git cl land raising exceptions and |
| 1053 | # shadowing real cause of failure. |
| 1054 | self.mock(git_cl, '_IS_BEING_TESTED', True) |
| 1055 | else: |
Andrii Shyshkalov | 06a2502 | 2016-11-24 16:47:00 +0100 | [diff] [blame] | 1056 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
Andrii Shyshkalov | 768f1d8 | 2016-12-08 15:10:13 +0100 | [diff] [blame] | 1057 | |
Andrii Shyshkalov | aa31b97 | 2017-03-24 16:16:33 +0100 | [diff] [blame] | 1058 | if git_numberer_enabled: |
| 1059 | # Special mocks to check validity of timestamp. |
| 1060 | original_git_amend_head = git_cl._git_amend_head |
| 1061 | def _git_amend_head_mock(msg, tstamp): |
| 1062 | self._mocked_call(['git_amend_head committer timestamp', tstamp]) |
| 1063 | return original_git_amend_head(msg, tstamp) |
| 1064 | self.mock(git_cl, '_git_amend_head', _git_amend_head_mock) |
| 1065 | |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1066 | self.mock(git_cl, '_is_git_numberer_enabled', lambda url, ref: |
| 1067 | self._mocked_call('_is_git_numberer_enabled', url, ref)) |
Andrii Shyshkalov | 06a2502 | 2016-11-24 16:47:00 +0100 | [diff] [blame] | 1068 | self.mock(RietveldMock, 'update_description', staticmethod( |
| 1069 | lambda i, d: self._mocked_call(['update_description', i, d]))) |
| 1070 | self.mock(RietveldMock, 'add_comment', staticmethod( |
| 1071 | lambda i, c: self._mocked_call(['add_comment', i, c]))) |
| 1072 | self.calls = [ |
Andrii Shyshkalov | 06a2502 | 2016-11-24 16:47:00 +0100 | [diff] [blame] | 1073 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
| 1074 | ((['git', 'config', 'branch.feature.git-cl-similarity'],), CERR1), |
| 1075 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
| 1076 | ((['git', 'config', '--bool', 'branch.feature.git-find-copies'],), |
| 1077 | CERR1), |
| 1078 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
| 1079 | ((['git', 'config', 'branch.feature.rietveldissue'],), '123'), |
Aaron Gable | 1bc7bfe | 2016-12-19 10:08:14 -0800 | [diff] [blame] | 1080 | ((['git', 'config', 'rietveld.autoupdate'],), ''), |
Andrii Shyshkalov | 06a2502 | 2016-11-24 16:47:00 +0100 | [diff] [blame] | 1081 | ((['git', 'config', 'rietveld.server'],), |
| 1082 | 'https://codereview.chromium.org'), |
| 1083 | ((['git', 'config', 'branch.feature.merge'],), 'refs/heads/master'), |
| 1084 | ((['git', 'config', 'branch.feature.remote'],), 'origin'), |
| 1085 | ((['git', 'config', 'branch.feature.merge'],), 'refs/heads/master'), |
| 1086 | ((['git', 'config', 'branch.feature.remote'],), 'origin'), |
Andrii Shyshkalov | 06a2502 | 2016-11-24 16:47:00 +0100 | [diff] [blame] | 1087 | ((['git', 'rev-list', '^feature', 'refs/remotes/origin/master'],), |
| 1088 | ''), # No commits to rebase, according to local view of origin. |
| 1089 | ((['git', 'merge-base', 'refs/remotes/origin/master', 'HEAD'],), |
| 1090 | 'fake_ancestor_sha'), |
| 1091 | ] + self._git_sanity_checks('fake_ancestor_sha', 'feature') + [ |
| 1092 | ((['git', 'rev-parse', '--show-cdup'],), ''), |
| 1093 | ((['git', 'rev-parse', 'HEAD'],), 'fake_sha'), |
| 1094 | ((['git', 'diff', '--name-status', '--no-renames', '-r', |
| 1095 | 'fake_ancestor_sha...', '.'],), |
| 1096 | 'M\tfile1.cpp'), |
| 1097 | ((['git', 'config', 'branch.feature.rietveldpatchset'],), '20001'), |
| 1098 | ((['git', 'config', 'branch.feature.rietveldserver'],), |
| 1099 | 'https://codereview.chromium.org'), |
| 1100 | ((['git', 'config', 'user.email'],), 'user@e.mail'), |
| 1101 | ((['git', 'config', 'rietveld.tree-status-url'],), CERR1), |
| 1102 | ((['git', 'diff', '--no-ext-diff', '--stat', '-l100000', '-C50', |
| 1103 | 'fake_ancestor_sha', 'feature'],), |
Quinten Yearsley | 0c62da9 | 2017-05-31 13:39:42 -0700 | [diff] [blame] | 1104 | # This command just prints something like this: |
Andrii Shyshkalov | 06a2502 | 2016-11-24 16:47:00 +0100 | [diff] [blame] | 1105 | # file1.cpp | 53 ++++++-- |
| 1106 | # 1 file changed, 33 insertions(+), 20 deletions(-)\n |
| 1107 | ''), |
| 1108 | ((['git', 'show-ref', '--quiet', '--verify', |
| 1109 | 'refs/heads/git-cl-commit'],), |
| 1110 | ''), # 0 return code means branch exists. |
| 1111 | ((['git', 'branch', '-D', 'git-cl-commit'],), ''), |
| 1112 | ((['git', 'show-ref', '--quiet', '--verify', |
| 1113 | 'refs/heads/git-cl-cherry-pick'],), |
| 1114 | CERR1), # This means git-cl-cherry-pick branch does not exist. |
| 1115 | ((['git', 'rev-parse', '--show-cdup'],), ''), |
| 1116 | ((['git', 'checkout', '-q', '-b', 'git-cl-commit'],), ''), |
| 1117 | ((['git', 'reset', '--soft', 'fake_ancestor_sha'],), ''), |
| 1118 | ((['git', 'commit', '-m', |
| 1119 | 'Issue: 123\n\nR=john@chromium.org\n\n' |
Andrii Shyshkalov | f01be22 | 2016-12-08 19:21:13 +0100 | [diff] [blame] | 1120 | 'Review-Url: https://codereview.chromium.org/123 .'],), ''), |
Andrii Shyshkalov | 06a2502 | 2016-11-24 16:47:00 +0100 | [diff] [blame] | 1121 | ((['git', 'config', 'branch.feature.merge'],), 'refs/heads/master'), |
| 1122 | ((['git', 'config', 'branch.feature.remote'],), 'origin'), |
| 1123 | ((['git', 'config', '--get', 'remote.origin.url'],), |
Andrii Shyshkalov | aa31b97 | 2017-03-24 16:16:33 +0100 | [diff] [blame] | 1124 | repo_url), |
| 1125 | |
| 1126 | ((['git', 'config', 'remote.origin.url'],), repo_url), |
| 1127 | (('_is_git_numberer_enabled', repo_url, 'refs/heads/master'), |
| 1128 | git_numberer_enabled), |
| 1129 | |
| 1130 | # Auto-rebase init. |
| 1131 | ((['git', 'rev-parse', 'HEAD'],), 'sha1_for_cherry_pick'), |
| 1132 | ] |
| 1133 | # Auto-rebase loops. |
| 1134 | for attempt in xrange(fail_push_count + 1): |
| 1135 | self.calls += [ |
| 1136 | # Auto-rebase loop start. |
| 1137 | ((['git', 'retry', 'fetch', 'origin', |
| 1138 | '+refs/heads/master:refs/heads/git-cl-cherry-pick'],), ''), |
| 1139 | ((['git', 'checkout', 'refs/heads/git-cl-cherry-pick'],), ''), |
| 1140 | # NOTE: if fail_push_count>0, this sha1_of_parent should probably be |
| 1141 | # different in each loop iteration for truly realistic test. |
| 1142 | ((['git', 'rev-parse', 'HEAD'],), 'sha1_of_parent'), |
| 1143 | ((['git', 'cherry-pick', 'sha1_for_cherry_pick'],), |
| 1144 | callError(1, 'failed to cherry pick') if fail_cherry_pick |
| 1145 | else ''), |
| 1146 | ] |
| 1147 | if fail_cherry_pick: |
| 1148 | return |
| 1149 | |
| 1150 | if git_numberer_enabled: |
| 1151 | assert parent_msg |
| 1152 | self.calls += [ |
| 1153 | ((['git', 'show', '-s', '--format=%B', 'sha1_of_parent'],), |
| 1154 | parent_msg), |
| 1155 | ] |
| 1156 | if fail_after_parent_msg: |
| 1157 | return |
| 1158 | assert new_msg |
| 1159 | self.calls += [ |
| 1160 | ((['git', 'show', '-s', '--format=%ct', 'sha1_of_parent'],), |
| 1161 | '1480022355'), # Committer's unix timestamp. |
| 1162 | ((['git', 'show', '-s', '--format=%ct', 'HEAD'],), |
| 1163 | '1480024000'), |
| 1164 | ((['git_amend_head committer timestamp', 1480024000],), ''), |
| 1165 | ((['git', 'commit', '--amend', '-m', new_msg],), ''), |
| 1166 | ] |
| 1167 | self.calls += [ |
| 1168 | ((['git', 'push', '--porcelain', 'origin', 'HEAD:refs/heads/master'],), |
| 1169 | CERR1 if attempt < fail_push_count else ''), |
| 1170 | ] |
| 1171 | # End of autorebase + push. |
| 1172 | |
| 1173 | self.calls += [ |
| 1174 | ((['git', 'rev-parse', 'HEAD'],), 'sha1_committed'), |
| 1175 | ] |
| 1176 | if git_numberer_enabled: |
| 1177 | self.calls += [ |
| 1178 | ((['git', 'show', '-s', '--format=%B', 'HEAD'],), new_msg), |
| 1179 | ] |
| 1180 | |
| 1181 | # Cleanup calls. |
| 1182 | self.calls += [ |
| 1183 | ((['git', 'checkout', '-q', 'feature'],), ''), |
| 1184 | ((['git', 'branch', '-D', 'git-cl-commit'],), ''), |
| 1185 | ((['git', 'branch', '-D', 'git-cl-cherry-pick'],), ''), |
Andrii Shyshkalov | 06a2502 | 2016-11-24 16:47:00 +0100 | [diff] [blame] | 1186 | ] |
| 1187 | |
| 1188 | def test_land_rietveld(self): |
Andrii Shyshkalov | aa31b97 | 2017-03-24 16:16:33 +0100 | [diff] [blame] | 1189 | self._land_rietveld_calls( |
| 1190 | repo_url='https://chromium.googlesource.com/infra/infra', |
| 1191 | git_numberer_enabled=False, |
| 1192 | debug=False) |
Andrii Shyshkalov | 06a2502 | 2016-11-24 16:47:00 +0100 | [diff] [blame] | 1193 | self.calls += [ |
Andrii Shyshkalov | 06a2502 | 2016-11-24 16:47:00 +0100 | [diff] [blame] | 1194 | ((['git', 'config', 'rietveld.viewvc-url'],), |
| 1195 | 'https://chromium.googlesource.com/infra/infra/+/'), |
| 1196 | ((['update_description', 123, |
| 1197 | 'Issue: 123\n\nR=john@chromium.org\n\nCommitted: ' |
Andrii Shyshkalov | aa31b97 | 2017-03-24 16:16:33 +0100 | [diff] [blame] | 1198 | 'https://chromium.googlesource.com/infra/infra/+/sha1_committed'],), |
Andrii Shyshkalov | 06a2502 | 2016-11-24 16:47:00 +0100 | [diff] [blame] | 1199 | ''), |
| 1200 | ((['add_comment', 123, 'Committed patchset #2 (id:20001) manually as ' |
Andrii Shyshkalov | aa31b97 | 2017-03-24 16:16:33 +0100 | [diff] [blame] | 1201 | 'sha1_committed (presubmit successful).'],), ''), |
Andrii Shyshkalov | 06a2502 | 2016-11-24 16:47:00 +0100 | [diff] [blame] | 1202 | ] |
| 1203 | git_cl.main(['land']) |
| 1204 | |
Andrii Shyshkalov | aa31b97 | 2017-03-24 16:16:33 +0100 | [diff] [blame] | 1205 | def test_land_rietveld_fail_cherry_pick(self): |
| 1206 | self._land_rietveld_calls( |
| 1207 | repo_url='https://chromium.googlesource.com/infra/infra', |
| 1208 | git_numberer_enabled=False, |
| 1209 | fail_cherry_pick=True, |
| 1210 | debug=False) |
Andrii Shyshkalov | 15e50cc | 2016-12-02 14:34:08 +0100 | [diff] [blame] | 1211 | self.calls += [ |
Andrii Shyshkalov | aa31b97 | 2017-03-24 16:16:33 +0100 | [diff] [blame] | 1212 | ((['git', 'diff', '--name-status', '--diff-filter=U'],), |
| 1213 | 'U path/file1\n' |
| 1214 | 'U file2.cpp\n'), |
| 1215 | ((['git', 'cherry-pick', '--abort'],), ''), |
Andrii Shyshkalov | 15e50cc | 2016-12-02 14:34:08 +0100 | [diff] [blame] | 1216 | ((['git', 'checkout', '-q', 'feature'],), ''), |
| 1217 | ((['git', 'branch', '-D', 'git-cl-commit'],), ''), |
Andrii Shyshkalov | aa31b97 | 2017-03-24 16:16:33 +0100 | [diff] [blame] | 1218 | ((['git', 'branch', '-D', 'git-cl-cherry-pick'],), ''), |
| 1219 | ] |
| 1220 | git_cl.main(['land']) |
| 1221 | |
| 1222 | def test_land_rietveld_git_numberer_fail_1_push(self): |
| 1223 | return self.test_land_rietveld_git_numberer(fail_push_count=1) |
| 1224 | |
| 1225 | def test_land_rietveld_git_numberer(self, fail_push_count=0): |
| 1226 | self._land_rietveld_calls( |
| 1227 | repo_url='https://chromium.googlesource.com/chromium/src', |
| 1228 | git_numberer_enabled=True, |
| 1229 | parent_msg=('This is parent commit.\n' |
| 1230 | '\n' |
| 1231 | 'Cr-Commit-Position: refs/heads/master@{#543}\n' |
| 1232 | 'Cr-Branched-From: refs/svn/2014@{#2208}'), |
| 1233 | new_msg=('Issue: 123\n\nR=john@chromium.org\n' |
| 1234 | '\n' |
| 1235 | 'Review-Url: https://codereview.chromium.org/123 .\n' |
| 1236 | 'Cr-Commit-Position: refs/heads/master@{#544}\n' |
| 1237 | 'Cr-Branched-From: refs/svn/2014@{#2208}'), |
| 1238 | fail_push_count=fail_push_count, |
| 1239 | debug=False) |
| 1240 | |
| 1241 | self.calls += [ |
Andrii Shyshkalov | 15e50cc | 2016-12-02 14:34:08 +0100 | [diff] [blame] | 1242 | ((['git', 'config', 'rietveld.viewvc-url'],), |
Andrii Shyshkalov | aa31b97 | 2017-03-24 16:16:33 +0100 | [diff] [blame] | 1243 | 'https://chromium.googlesource.com/chromium/src/+/'), |
Andrii Shyshkalov | 15e50cc | 2016-12-02 14:34:08 +0100 | [diff] [blame] | 1244 | ((['update_description', 123, |
| 1245 | 'Issue: 123\n\nR=john@chromium.org\n' |
| 1246 | '\n' |
Andrii Shyshkalov | f01be22 | 2016-12-08 19:21:13 +0100 | [diff] [blame] | 1247 | 'Review-Url: https://codereview.chromium.org/123 .\n' |
Andrii Shyshkalov | 15e50cc | 2016-12-02 14:34:08 +0100 | [diff] [blame] | 1248 | 'Cr-Commit-Position: refs/heads/master@{#544}\n' |
| 1249 | 'Cr-Branched-From: refs/svn/2014@{#2208}\n' |
| 1250 | 'Committed: ' |
Andrii Shyshkalov | aa31b97 | 2017-03-24 16:16:33 +0100 | [diff] [blame] | 1251 | 'https://chromium.googlesource.com/chromium/src/+/sha1_committed'],), |
Andrii Shyshkalov | 15e50cc | 2016-12-02 14:34:08 +0100 | [diff] [blame] | 1252 | ''), |
| 1253 | ((['add_comment', 123, 'Committed patchset #2 (id:20001) manually as ' |
Andrii Shyshkalov | aa31b97 | 2017-03-24 16:16:33 +0100 | [diff] [blame] | 1254 | 'sha1_committed (presubmit successful).'],), ''), |
Andrii Shyshkalov | 15e50cc | 2016-12-02 14:34:08 +0100 | [diff] [blame] | 1255 | ] |
| 1256 | git_cl.main(['land']) |
| 1257 | |
| 1258 | def test_land_rietveld_git_numberer_bad_parent(self): |
Andrii Shyshkalov | aa31b97 | 2017-03-24 16:16:33 +0100 | [diff] [blame] | 1259 | self._land_rietveld_calls( |
| 1260 | repo_url='https://chromium.googlesource.com/v8/v8', |
| 1261 | git_numberer_enabled=True, |
| 1262 | parent_msg='This is parent commit with no footer.', |
| 1263 | fail_after_parent_msg=True, |
| 1264 | debug=False) |
Andrii Shyshkalov | 15e50cc | 2016-12-02 14:34:08 +0100 | [diff] [blame] | 1265 | self.calls += [ |
Andrii Shyshkalov | aa31b97 | 2017-03-24 16:16:33 +0100 | [diff] [blame] | 1266 | # Cleanup calls to restore original state. |
Andrii Shyshkalov | 15e50cc | 2016-12-02 14:34:08 +0100 | [diff] [blame] | 1267 | ((['git', 'checkout', '-q', 'feature'],), ''), |
| 1268 | ((['git', 'branch', '-D', 'git-cl-commit'],), ''), |
Andrii Shyshkalov | aa31b97 | 2017-03-24 16:16:33 +0100 | [diff] [blame] | 1269 | ((['git', 'branch', '-D', 'git-cl-cherry-pick'],), ''), |
Andrii Shyshkalov | 15e50cc | 2016-12-02 14:34:08 +0100 | [diff] [blame] | 1270 | ] |
| 1271 | with self.assertRaises(ValueError) as cm: |
| 1272 | git_cl.main(['land']) |
| 1273 | self.assertEqual(cm.exception.message, |
| 1274 | 'Unable to infer commit position from footers') |
| 1275 | |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1276 | def test_git_numberer_not_whitelisted_repo(self): |
| 1277 | self.calls = [] |
| 1278 | res = git_cl._is_git_numberer_enabled( |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1279 | remote_url='https://chromium.googlesource.com/chromium/tools/build', |
| 1280 | remote_ref='refs/whatever') |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1281 | self.assertEqual(res, False) |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1282 | |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1283 | def test_git_numberer_fail_fetch(self): |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1284 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
| 1285 | self.calls = [ |
| 1286 | ((['git', 'fetch', 'https://chromium.googlesource.com/chromium/src', |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1287 | '+refs/meta/config:refs/git_cl/meta/config'],), CERR1), |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1288 | ] |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1289 | res = git_cl._is_git_numberer_enabled( |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1290 | remote_url='https://chromium.googlesource.com/chromium/src', |
| 1291 | remote_ref='refs/whatever') |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1292 | self.assertEqual(res, False) |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1293 | |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1294 | def test_git_numberer_valid_configs(self): |
Andrii Shyshkalov | 351c61d | 2017-01-21 20:40:16 +0000 | [diff] [blame] | 1295 | with git_cl.gclient_utils.temporary_directory() as tempdir: |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1296 | @contextlib.contextmanager |
Andrii Shyshkalov | 351c61d | 2017-01-21 20:40:16 +0000 | [diff] [blame] | 1297 | def fake_temporary_directory(**kwargs): |
| 1298 | yield tempdir |
| 1299 | self.mock(git_cl.gclient_utils, 'temporary_directory', |
| 1300 | fake_temporary_directory) |
| 1301 | self._test_GitNumbererState_valid_configs_inner(tempdir) |
| 1302 | |
| 1303 | def _test_GitNumbererState_valid_configs_inner(self, tempdir): |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1304 | self.calls = [ |
| 1305 | ((['git', 'fetch', 'https://chromium.googlesource.com/chromium/src', |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1306 | '+refs/meta/config:refs/git_cl/meta/config'],), ''), |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1307 | ((['git', 'show', 'refs/git_cl/meta/config:project.config'],), |
| 1308 | ''' |
| 1309 | [plugin "git-numberer"] |
| 1310 | validate-enabled-refglob = refs/else/* |
| 1311 | validate-enabled-refglob = refs/heads/* |
| 1312 | validate-disabled-refglob = refs/heads/disabled |
| 1313 | validate-disabled-refglob = refs/branch-heads/* |
| 1314 | '''), |
Andrii Shyshkalov | 351c61d | 2017-01-21 20:40:16 +0000 | [diff] [blame] | 1315 | ((['git', 'config', '-f', os.path.join(tempdir, 'project.config') , |
| 1316 | '--get-all', 'plugin.git-numberer.validate-enabled-refglob'],), |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1317 | 'refs/else/*\n' |
| 1318 | 'refs/heads/*\n'), |
Andrii Shyshkalov | 351c61d | 2017-01-21 20:40:16 +0000 | [diff] [blame] | 1319 | ((['git', 'config', '-f', os.path.join(tempdir, 'project.config') , |
| 1320 | '--get-all', 'plugin.git-numberer.validate-disabled-refglob'],), |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1321 | 'refs/heads/disabled\n' |
| 1322 | 'refs/branch-heads/*\n'), |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1323 | ] * 3 # 3 tests below have exactly the same IO. |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1324 | |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1325 | res = git_cl._is_git_numberer_enabled( |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1326 | remote_url='https://chromium.googlesource.com/chromium/src', |
| 1327 | remote_ref='refs/heads/test') |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1328 | self.assertEqual(res, True) |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1329 | |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1330 | res = git_cl._is_git_numberer_enabled( |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1331 | remote_url='https://chromium.googlesource.com/chromium/src', |
| 1332 | remote_ref='refs/heads/disabled') |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1333 | self.assertEqual(res, False) |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1334 | |
Andrii Shyshkalov | 0a0b067 | 2017-03-16 16:27:48 +0100 | [diff] [blame] | 1335 | # Validator is disabled by default, even if it's not explicitly in disabled |
Andrii Shyshkalov | cbec8ef | 2016-12-09 13:54:51 +0100 | [diff] [blame] | 1336 | # refglobs. |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1337 | res = git_cl._is_git_numberer_enabled( |
Andrii Shyshkalov | cbec8ef | 2016-12-09 13:54:51 +0100 | [diff] [blame] | 1338 | remote_url='https://chromium.googlesource.com/chromium/src', |
| 1339 | remote_ref='refs/arbitrary/ref') |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1340 | self.assertEqual(res, False) |
Andrii Shyshkalov | cbec8ef | 2016-12-09 13:54:51 +0100 | [diff] [blame] | 1341 | |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 1342 | @classmethod |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 1343 | def _gerrit_ensure_auth_calls(cls, issue=None, skip_auth_check=False): |
shinyak@chromium.org | 00dbccd | 2016-04-15 07:24:43 +0000 | [diff] [blame] | 1344 | cmd = ['git', 'config', '--bool', 'gerrit.skip-ensure-authenticated'] |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 1345 | if skip_auth_check: |
| 1346 | return [((cmd, ), 'true')] |
| 1347 | |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 1348 | calls = [((cmd, ), CERR1)] |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 1349 | if issue: |
| 1350 | calls.extend([ |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1351 | ((['git', 'config', 'branch.master.gerritserver'],), CERR1), |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 1352 | ]) |
| 1353 | calls.extend([ |
| 1354 | ((['git', 'config', 'branch.master.merge'],), 'refs/heads/master'), |
| 1355 | ((['git', 'config', 'branch.master.remote'],), 'origin'), |
| 1356 | ((['git', 'config', 'remote.origin.url'],), |
| 1357 | 'https://chromium.googlesource.com/my/repo'), |
| 1358 | ((['git', 'config', 'remote.origin.url'],), |
| 1359 | 'https://chromium.googlesource.com/my/repo'), |
| 1360 | ]) |
| 1361 | return calls |
| 1362 | |
| 1363 | @classmethod |
Andrii Shyshkalov | 5c3d0b3 | 2017-02-16 17:47:31 +0100 | [diff] [blame] | 1364 | def _gerrit_base_calls(cls, issue=None, fetched_description=None, |
Andrii Shyshkalov | 550e924 | 2017-04-12 17:14:49 +0200 | [diff] [blame] | 1365 | fetched_status=None, other_cl_owner=None, |
| 1366 | custom_cl_base=None): |
Andrii Shyshkalov | 0293956 | 2017-02-16 17:47:17 +0100 | [diff] [blame] | 1367 | calls = [ |
Andrii Shyshkalov | 550e924 | 2017-04-12 17:14:49 +0200 | [diff] [blame] | 1368 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
| 1369 | ((['git', 'config', 'branch.master.git-cl-similarity'],), CERR1), |
| 1370 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
| 1371 | ((['git', 'config', '--bool', 'branch.master.git-find-copies'],), CERR1), |
Andrii Shyshkalov | 0293956 | 2017-02-16 17:47:17 +0100 | [diff] [blame] | 1372 | ] |
| 1373 | calls += cls._is_gerrit_calls(True) |
| 1374 | calls += [ |
Andrii Shyshkalov | 550e924 | 2017-04-12 17:14:49 +0200 | [diff] [blame] | 1375 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
| 1376 | ((['git', 'config', 'branch.master.rietveldissue'],), CERR1), |
| 1377 | ((['git', 'config', 'branch.master.gerritissue'],), |
| 1378 | CERR1 if issue is None else str(issue)), |
| 1379 | ] |
| 1380 | |
| 1381 | if custom_cl_base: |
| 1382 | ancestor_revision = custom_cl_base |
| 1383 | else: |
| 1384 | # Determine ancestor_revision to be merge base. |
| 1385 | ancestor_revision = 'fake_ancestor_sha' |
| 1386 | calls += [ |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 1387 | ((['git', 'config', 'branch.master.merge'],), 'refs/heads/master'), |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 1388 | ((['git', 'config', 'branch.master.remote'],), 'origin'), |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 1389 | ((['get_or_create_merge_base', 'master', |
Andrii Shyshkalov | 550e924 | 2017-04-12 17:14:49 +0200 | [diff] [blame] | 1390 | 'refs/remotes/origin/master'],), ancestor_revision), |
| 1391 | ] |
| 1392 | |
| 1393 | # Calls to verify branch point is ancestor |
Andrii Shyshkalov | 0293956 | 2017-02-16 17:47:17 +0100 | [diff] [blame] | 1394 | calls += cls._gerrit_ensure_auth_calls(issue=issue) |
| 1395 | |
| 1396 | if issue: |
| 1397 | calls += [ |
Andrii Shyshkalov | 3e63142 | 2017-02-16 17:46:44 +0100 | [diff] [blame] | 1398 | (('GetChangeDetail', 'chromium-review.googlesource.com', |
Andrii Shyshkalov | bb86fbb | 2017-03-24 14:59:28 +0100 | [diff] [blame] | 1399 | '123456', |
| 1400 | ['DETAILED_ACCOUNTS', 'CURRENT_REVISION', 'CURRENT_COMMIT']), |
Andrii Shyshkalov | 3e63142 | 2017-02-16 17:46:44 +0100 | [diff] [blame] | 1401 | { |
Andrii Shyshkalov | bb86fbb | 2017-03-24 14:59:28 +0100 | [diff] [blame] | 1402 | 'owner': {'email': (other_cl_owner or 'owner@example.com')}, |
Andrii Shyshkalov | 3e63142 | 2017-02-16 17:46:44 +0100 | [diff] [blame] | 1403 | 'change_id': '123456789', |
| 1404 | 'current_revision': 'sha1_of_current_revision', |
| 1405 | 'revisions': { 'sha1_of_current_revision': { |
| 1406 | 'commit': {'message': fetched_description}, |
| 1407 | }}, |
Andrii Shyshkalov | 5c3d0b3 | 2017-02-16 17:47:31 +0100 | [diff] [blame] | 1408 | 'status': fetched_status or 'NEW', |
Andrii Shyshkalov | 3e63142 | 2017-02-16 17:46:44 +0100 | [diff] [blame] | 1409 | }), |
Andrii Shyshkalov | 0293956 | 2017-02-16 17:47:17 +0100 | [diff] [blame] | 1410 | ] |
Andrii Shyshkalov | 5c3d0b3 | 2017-02-16 17:47:31 +0100 | [diff] [blame] | 1411 | if fetched_status == 'ABANDONED': |
| 1412 | calls += [ |
| 1413 | (('DieWithError', 'Change https://chromium-review.googlesource.com/' |
| 1414 | '123456 has been abandoned, new uploads are not ' |
| 1415 | 'allowed'), SystemExitMock()), |
| 1416 | ] |
| 1417 | return calls |
Andrii Shyshkalov | bb86fbb | 2017-03-24 14:59:28 +0100 | [diff] [blame] | 1418 | if other_cl_owner: |
| 1419 | calls += [ |
| 1420 | (('ask_for_data', 'Press Enter to upload, or Ctrl+C to abort'), ''), |
| 1421 | ] |
Andrii Shyshkalov | 0293956 | 2017-02-16 17:47:17 +0100 | [diff] [blame] | 1422 | |
Andrii Shyshkalov | 550e924 | 2017-04-12 17:14:49 +0200 | [diff] [blame] | 1423 | calls += cls._git_sanity_checks(ancestor_revision, 'master', |
| 1424 | get_remote_branch=False) |
Andrii Shyshkalov | 0293956 | 2017-02-16 17:47:17 +0100 | [diff] [blame] | 1425 | calls += [ |
Andrii Shyshkalov | 550e924 | 2017-04-12 17:14:49 +0200 | [diff] [blame] | 1426 | ((['git', 'rev-parse', '--show-cdup'],), ''), |
| 1427 | ((['git', 'rev-parse', 'HEAD'],), '12345'), |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 1428 | |
Andrii Shyshkalov | 550e924 | 2017-04-12 17:14:49 +0200 | [diff] [blame] | 1429 | ((['git', 'diff', '--name-status', '--no-renames', '-r', |
| 1430 | ancestor_revision + '...', '.'],), |
| 1431 | 'M\t.gitignore\n'), |
| 1432 | ((['git', 'config', 'branch.master.gerritpatchset'],), CERR1), |
Andrii Shyshkalov | 0293956 | 2017-02-16 17:47:17 +0100 | [diff] [blame] | 1433 | ] |
| 1434 | |
| 1435 | if not issue: |
| 1436 | calls += [ |
Andrii Shyshkalov | 550e924 | 2017-04-12 17:14:49 +0200 | [diff] [blame] | 1437 | ((['git', 'log', '--pretty=format:%s%n%n%b', |
| 1438 | ancestor_revision + '...'],), |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 1439 | 'foo'), |
Andrii Shyshkalov | 0293956 | 2017-02-16 17:47:17 +0100 | [diff] [blame] | 1440 | ] |
| 1441 | |
| 1442 | calls += [ |
Andrii Shyshkalov | 550e924 | 2017-04-12 17:14:49 +0200 | [diff] [blame] | 1443 | ((['git', 'config', 'user.email'],), 'me@example.com'), |
| 1444 | ((['git', 'diff', '--no-ext-diff', '--stat', '-l100000', '-C50'] + |
| 1445 | ([custom_cl_base] if custom_cl_base else |
| 1446 | [ancestor_revision, 'HEAD']),), |
| 1447 | '+dat'), |
Andrii Shyshkalov | 0293956 | 2017-02-16 17:47:17 +0100 | [diff] [blame] | 1448 | ] |
| 1449 | return calls |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1450 | |
tandrii@chromium.org | 1e67bb7 | 2016-02-11 12:15:49 +0000 | [diff] [blame] | 1451 | @classmethod |
| 1452 | def _gerrit_upload_calls(cls, description, reviewers, squash, |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1453 | squash_mode='default', |
tandrii@chromium.org | 1062500 | 2016-03-04 20:03:47 +0000 | [diff] [blame] | 1454 | expected_upstream_ref='origin/refs/heads/master', |
Aaron Gable | fd23808 | 2017-06-07 13:42:34 -0700 | [diff] [blame] | 1455 | title=None, notify=False, |
Andrii Shyshkalov | e9c78ff | 2017-02-06 15:53:13 +0100 | [diff] [blame] | 1456 | post_amend_description=None, issue=None, cc=None, |
Aaron Gable | fd23808 | 2017-06-07 13:42:34 -0700 | [diff] [blame] | 1457 | git_mirror=None, custom_cl_base=None, tbr=None): |
tandrii@chromium.org | 1062500 | 2016-03-04 20:03:47 +0000 | [diff] [blame] | 1458 | if post_amend_description is None: |
| 1459 | post_amend_description = description |
bradnelson | d975b30 | 2016-10-23 12:20:23 -0700 | [diff] [blame] | 1460 | cc = cc or [] |
Andrii Shyshkalov | 550e924 | 2017-04-12 17:14:49 +0200 | [diff] [blame] | 1461 | # Determined in `_gerrit_base_calls`. |
| 1462 | determined_ancestor_revision = custom_cl_base or 'fake_ancestor_sha' |
| 1463 | |
| 1464 | calls = [] |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 1465 | |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1466 | if squash_mode == 'default': |
| 1467 | calls.extend([ |
| 1468 | ((['git', 'config', '--bool', 'gerrit.override-squash-uploads'],), ''), |
| 1469 | ((['git', 'config', '--bool', 'gerrit.squash-uploads'],), ''), |
| 1470 | ]) |
| 1471 | elif squash_mode in ('override_squash', 'override_nosquash'): |
| 1472 | calls.extend([ |
| 1473 | ((['git', 'config', '--bool', 'gerrit.override-squash-uploads'],), |
| 1474 | 'true' if squash_mode == 'override_squash' else 'false'), |
| 1475 | ]) |
| 1476 | else: |
| 1477 | assert squash_mode in ('squash', 'nosquash') |
| 1478 | |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 1479 | # If issue is given, then description is fetched from Gerrit instead. |
| 1480 | if issue is None: |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 1481 | calls += [ |
Andrii Shyshkalov | 550e924 | 2017-04-12 17:14:49 +0200 | [diff] [blame] | 1482 | ((['git', 'log', '--pretty=format:%s\n\n%b', |
| 1483 | ((custom_cl_base + '..') if custom_cl_base else |
| 1484 | 'fake_ancestor_sha..HEAD')],), |
| 1485 | description), |
| 1486 | ] |
Aaron Gable | b56ad33 | 2017-01-06 15:24:31 -0800 | [diff] [blame] | 1487 | if squash: |
Andrii Shyshkalov | febbae9 | 2017-04-05 15:05:20 +0000 | [diff] [blame] | 1488 | title = 'Initial_upload' |
Aaron Gable | b56ad33 | 2017-01-06 15:24:31 -0800 | [diff] [blame] | 1489 | else: |
| 1490 | if not title: |
| 1491 | calls += [ |
Andrii Shyshkalov | 550e924 | 2017-04-12 17:14:49 +0200 | [diff] [blame] | 1492 | ((['git', 'show', '-s', '--format=%s', 'HEAD'],), ''), |
| 1493 | (('ask_for_data', 'Title for patchset []: '), 'User input'), |
Aaron Gable | b56ad33 | 2017-01-06 15:24:31 -0800 | [diff] [blame] | 1494 | ] |
Andrii Shyshkalov | febbae9 | 2017-04-05 15:05:20 +0000 | [diff] [blame] | 1495 | title = 'User_input' |
tandrii@chromium.org | 57d8654 | 2016-03-04 16:11:32 +0000 | [diff] [blame] | 1496 | 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] | 1497 | calls += [ |
Andrii Shyshkalov | 550e924 | 2017-04-12 17:14:49 +0200 | [diff] [blame] | 1498 | (('DownloadGerritHook', False), ''), |
| 1499 | # Amending of commit message to get the Change-Id. |
| 1500 | ((['git', 'log', '--pretty=format:%s\n\n%b', |
| 1501 | determined_ancestor_revision + '..HEAD'],), |
| 1502 | description), |
| 1503 | ((['git', 'commit', '--amend', '-m', description],), ''), |
| 1504 | ((['git', 'log', '--pretty=format:%s\n\n%b', |
| 1505 | determined_ancestor_revision + '..HEAD'],), |
| 1506 | post_amend_description) |
| 1507 | ] |
bauerb@chromium.org | 27386dd | 2015-02-16 10:45:39 +0000 | [diff] [blame] | 1508 | if squash: |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 1509 | if not issue: |
| 1510 | # Prompting to edit description on first upload. |
| 1511 | calls += [ |
Andrii Shyshkalov | 550e924 | 2017-04-12 17:14:49 +0200 | [diff] [blame] | 1512 | ((['git', 'config', 'core.editor'],), ''), |
| 1513 | ((['RunEditor'],), description), |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 1514 | ] |
bauerb@chromium.org | 27386dd | 2015-02-16 10:45:39 +0000 | [diff] [blame] | 1515 | ref_to_push = 'abcdef0123456789' |
| 1516 | calls += [ |
Andrii Shyshkalov | 550e924 | 2017-04-12 17:14:49 +0200 | [diff] [blame] | 1517 | ((['git', 'config', 'branch.master.merge'],), 'refs/heads/master'), |
| 1518 | ((['git', 'config', 'branch.master.remote'],), 'origin'), |
| 1519 | ] |
| 1520 | |
| 1521 | if custom_cl_base is None: |
| 1522 | calls += [ |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 1523 | ((['get_or_create_merge_base', 'master', |
| 1524 | 'refs/remotes/origin/master'],), |
bauerb@chromium.org | 27386dd | 2015-02-16 10:45:39 +0000 | [diff] [blame] | 1525 | 'origin/master'), |
Andrii Shyshkalov | 550e924 | 2017-04-12 17:14:49 +0200 | [diff] [blame] | 1526 | ] |
| 1527 | parent = 'origin/master' |
| 1528 | else: |
| 1529 | calls += [ |
| 1530 | ((['git', 'merge-base', '--is-ancestor', custom_cl_base, |
| 1531 | 'refs/remotes/origin/master'],), |
| 1532 | callError(1)), # Means not ancenstor. |
| 1533 | (('ask_for_data', |
| 1534 | 'Do you take responsibility for cleaning up potential mess ' |
| 1535 | 'resulting from proceeding with upload? Press Enter to upload, ' |
| 1536 | 'or Ctrl+C to abort'), ''), |
| 1537 | ] |
| 1538 | parent = custom_cl_base |
| 1539 | |
| 1540 | calls += [ |
| 1541 | ((['git', 'rev-parse', 'HEAD:'],), # `HEAD:` means HEAD's tree hash. |
| 1542 | '0123456789abcdef'), |
| 1543 | ((['git', 'commit-tree', '0123456789abcdef', '-p', parent, |
| 1544 | '-m', description],), |
| 1545 | ref_to_push), |
| 1546 | ] |
bauerb@chromium.org | 27386dd | 2015-02-16 10:45:39 +0000 | [diff] [blame] | 1547 | else: |
| 1548 | ref_to_push = 'HEAD' |
| 1549 | |
sivachandra@chromium.org | aebe87f | 2012-10-22 20:34:21 +0000 | [diff] [blame] | 1550 | calls += [ |
Andrii Shyshkalov | 550e924 | 2017-04-12 17:14:49 +0200 | [diff] [blame] | 1551 | ((['git', 'rev-list', |
| 1552 | (custom_cl_base if custom_cl_base else expected_upstream_ref) + '..' + |
| 1553 | ref_to_push],), |
| 1554 | '1hashPerLine\n'), |
| 1555 | ] |
tandrii@chromium.org | 8da4540 | 2016-05-24 23:11:03 +0000 | [diff] [blame] | 1556 | |
Aaron Gable | afd5277 | 2017-06-27 16:40:10 -0700 | [diff] [blame] | 1557 | if notify: |
Aaron Gable | 844cf29 | 2017-06-28 11:32:59 -0700 | [diff] [blame] | 1558 | ref_suffix = '%ready,notify=ALL' |
| 1559 | else: |
| 1560 | if not issue: |
| 1561 | ref_suffix = '%wip' |
| 1562 | else: |
| 1563 | ref_suffix = '%notify=NONE' |
Aaron Gable | 9b713dd | 2016-12-14 16:04:21 -0800 | [diff] [blame] | 1564 | |
Aaron Gable | 70f4e24 | 2017-06-26 10:45:59 -0700 | [diff] [blame] | 1565 | if title: |
Aaron Gable | afd5277 | 2017-06-27 16:40:10 -0700 | [diff] [blame] | 1566 | ref_suffix += ',m=' + title |
Andrii Shyshkalov | febbae9 | 2017-04-05 15:05:20 +0000 | [diff] [blame] | 1567 | |
Andrii Shyshkalov | e9c78ff | 2017-02-06 15:53:13 +0100 | [diff] [blame] | 1568 | if git_mirror is None: |
| 1569 | calls += [ |
| 1570 | ((['git', 'config', 'remote.origin.url'],), |
| 1571 | 'https://chromium.googlesource.com/yyy/zzz'), |
| 1572 | ] |
| 1573 | else: |
| 1574 | calls += [ |
| 1575 | ((['git', 'config', 'remote.origin.url'],), |
| 1576 | '/cache/this-dir-exists'), |
| 1577 | (('os.path.isdir', '/cache/this-dir-exists'), |
| 1578 | True), |
| 1579 | # Runs in /cache/this-dir-exists. |
| 1580 | ((['git', 'config', 'remote.origin.url'],), |
| 1581 | 'https://chromium.googlesource.com/yyy/zzz'), |
| 1582 | ] |
| 1583 | |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1584 | calls += [ |
Andrii Shyshkalov | febbae9 | 2017-04-05 15:05:20 +0000 | [diff] [blame] | 1585 | ((['git', 'push', |
| 1586 | 'https://chromium.googlesource.com/yyy/zzz', |
| 1587 | ref_to_push + ':refs/for/refs/heads/master' + ref_suffix],), |
tandrii@chromium.org | a342c92 | 2016-03-16 07:08:25 +0000 | [diff] [blame] | 1588 | ('remote:\n' |
Andrii Shyshkalov | febbae9 | 2017-04-05 15:05:20 +0000 | [diff] [blame] | 1589 | 'remote: Processing changes: (\)\n' |
| 1590 | 'remote: Processing changes: (|)\n' |
| 1591 | 'remote: Processing changes: (/)\n' |
| 1592 | 'remote: Processing changes: (-)\n' |
| 1593 | 'remote: Processing changes: new: 1 (/)\n' |
| 1594 | 'remote: Processing changes: new: 1, done\n' |
| 1595 | 'remote:\n' |
| 1596 | 'remote: New Changes:\n' |
Aaron Gable | 289b431 | 2017-09-13 14:06:16 -0700 | [diff] [blame] | 1597 | 'remote: https://chromium-review.googlesource.com/#/c/foo/+/123456 ' |
| 1598 | 'XXX\n' |
Andrii Shyshkalov | febbae9 | 2017-04-05 15:05:20 +0000 | [diff] [blame] | 1599 | 'remote:\n' |
| 1600 | 'To https://chromium.googlesource.com/yyy/zzz\n' |
| 1601 | ' * [new branch] hhhh -> refs/for/refs/heads/master\n')), |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1602 | ] |
bauerb@chromium.org | 27386dd | 2015-02-16 10:45:39 +0000 | [diff] [blame] | 1603 | if squash: |
| 1604 | calls += [ |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 1605 | ((['git', 'config', 'branch.master.gerritissue', '123456'],), |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 1606 | ''), |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 1607 | ((['git', 'config', 'branch.master.gerritserver', |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 1608 | 'https://chromium-review.googlesource.com'],), ''), |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 1609 | ((['git', 'config', 'branch.master.gerritsquashhash', |
| 1610 | 'abcdef0123456789'],), ''), |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 1611 | ] |
tandrii | 8818977 | 2016-09-29 04:29:57 -0700 | [diff] [blame] | 1612 | calls += [ |
| 1613 | ((['git', 'config', 'rietveld.cc'],), ''), |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1614 | (('AddReviewers', 'chromium-review.googlesource.com', |
Aaron Gable | 6dadfbf | 2017-05-09 14:27:58 -0700 | [diff] [blame] | 1615 | 123456 if squash else None, sorted(reviewers), |
| 1616 | ['joe@example.com'] + cc, notify), ''), |
tandrii | 8818977 | 2016-09-29 04:29:57 -0700 | [diff] [blame] | 1617 | ] |
Aaron Gable | fd23808 | 2017-06-07 13:42:34 -0700 | [diff] [blame] | 1618 | if tbr: |
| 1619 | calls += [ |
| 1620 | (('SetReview', 'chromium-review.googlesource.com', |
Aaron Gable | fc62f76 | 2017-07-17 11:12:07 -0700 | [diff] [blame] | 1621 | 123456 if squash else None, 'Self-approving for TBR', |
| 1622 | {'Code-Review': 1}, None), ''), |
Aaron Gable | fd23808 | 2017-06-07 13:42:34 -0700 | [diff] [blame] | 1623 | ] |
tandrii@chromium.org | 1e67bb7 | 2016-02-11 12:15:49 +0000 | [diff] [blame] | 1624 | calls += cls._git_post_upload_calls() |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1625 | return calls |
| 1626 | |
sivachandra@chromium.org | aebe87f | 2012-10-22 20:34:21 +0000 | [diff] [blame] | 1627 | def _run_gerrit_upload_test( |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1628 | self, |
| 1629 | upload_args, |
| 1630 | description, |
tandrii@chromium.org | bf766ba | 2016-04-13 12:51:23 +0000 | [diff] [blame] | 1631 | reviewers=None, |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1632 | squash=True, |
| 1633 | squash_mode=None, |
tandrii@chromium.org | 1062500 | 2016-03-04 20:03:47 +0000 | [diff] [blame] | 1634 | expected_upstream_ref='origin/refs/heads/master', |
Aaron Gable | 9b713dd | 2016-12-14 16:04:21 -0800 | [diff] [blame] | 1635 | title=None, |
tandrii@chromium.org | 8da4540 | 2016-05-24 23:11:03 +0000 | [diff] [blame] | 1636 | notify=False, |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 1637 | post_amend_description=None, |
bradnelson | d975b30 | 2016-10-23 12:20:23 -0700 | [diff] [blame] | 1638 | issue=None, |
Andrii Shyshkalov | e9c78ff | 2017-02-06 15:53:13 +0100 | [diff] [blame] | 1639 | cc=None, |
Andrii Shyshkalov | 5c3d0b3 | 2017-02-16 17:47:31 +0100 | [diff] [blame] | 1640 | git_mirror=None, |
Andrii Shyshkalov | bb86fbb | 2017-03-24 14:59:28 +0100 | [diff] [blame] | 1641 | fetched_status=None, |
Andrii Shyshkalov | 550e924 | 2017-04-12 17:14:49 +0200 | [diff] [blame] | 1642 | other_cl_owner=None, |
Aaron Gable | fd23808 | 2017-06-07 13:42:34 -0700 | [diff] [blame] | 1643 | custom_cl_base=None, |
| 1644 | tbr=None): |
sivachandra@chromium.org | aebe87f | 2012-10-22 20:34:21 +0000 | [diff] [blame] | 1645 | """Generic gerrit upload test framework.""" |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1646 | if squash_mode is None: |
| 1647 | if '--no-squash' in upload_args: |
| 1648 | squash_mode = 'nosquash' |
| 1649 | elif '--squash' in upload_args: |
| 1650 | squash_mode = 'squash' |
| 1651 | else: |
| 1652 | squash_mode = 'default' |
| 1653 | |
tandrii@chromium.org | bf766ba | 2016-04-13 12:51:23 +0000 | [diff] [blame] | 1654 | reviewers = reviewers or [] |
bradnelson | d975b30 | 2016-10-23 12:20:23 -0700 | [diff] [blame] | 1655 | cc = cc or [] |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 1656 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
tandrii | 16e0b4e | 2016-06-07 10:34:28 -0700 | [diff] [blame] | 1657 | self.mock(git_cl.gerrit_util, 'CookiesAuthenticator', |
Andrii Shyshkalov | bb86fbb | 2017-03-24 14:59:28 +0100 | [diff] [blame] | 1658 | CookiesAuthenticatorMockFactory( |
| 1659 | same_auth=('git-owner.example.com', '', 'pass'))) |
tandrii | 16e0b4e | 2016-06-07 10:34:28 -0700 | [diff] [blame] | 1660 | self.mock(git_cl._GerritChangelistImpl, '_GerritCommitMsgHookCheck', |
| 1661 | lambda _, offer_removal: None) |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1662 | self.mock(git_cl.gclient_utils, 'RunEditor', |
| 1663 | lambda *_, **__: self._mocked_call(['RunEditor'])) |
Andrii Shyshkalov | 550e924 | 2017-04-12 17:14:49 +0200 | [diff] [blame] | 1664 | self.mock(git_cl, 'DownloadGerritHook', lambda force: self._mocked_call( |
| 1665 | 'DownloadGerritHook', force)) |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1666 | |
Andrii Shyshkalov | e9c78ff | 2017-02-06 15:53:13 +0100 | [diff] [blame] | 1667 | original_os_path_isdir = os.path.isdir |
| 1668 | def selective_os_path_isdir_mock(path): |
| 1669 | if path == '/cache/this-dir-exists': |
| 1670 | return self._mocked_call('os.path.isdir', path) |
| 1671 | return original_os_path_isdir(path) |
| 1672 | self.mock(os.path, 'isdir', selective_os_path_isdir_mock) |
| 1673 | |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1674 | self.calls = self._gerrit_base_calls( |
| 1675 | issue=issue, |
Andrii Shyshkalov | 5c3d0b3 | 2017-02-16 17:47:31 +0100 | [diff] [blame] | 1676 | fetched_description=description, |
Andrii Shyshkalov | bb86fbb | 2017-03-24 14:59:28 +0100 | [diff] [blame] | 1677 | fetched_status=fetched_status, |
Andrii Shyshkalov | 550e924 | 2017-04-12 17:14:49 +0200 | [diff] [blame] | 1678 | other_cl_owner=other_cl_owner, |
| 1679 | custom_cl_base=custom_cl_base) |
Andrii Shyshkalov | 5c3d0b3 | 2017-02-16 17:47:31 +0100 | [diff] [blame] | 1680 | if fetched_status != 'ABANDONED': |
| 1681 | self.calls += self._gerrit_upload_calls( |
| 1682 | description, reviewers, squash, |
| 1683 | squash_mode=squash_mode, |
| 1684 | expected_upstream_ref=expected_upstream_ref, |
Aaron Gable | fd23808 | 2017-06-07 13:42:34 -0700 | [diff] [blame] | 1685 | title=title, notify=notify, |
Andrii Shyshkalov | 5c3d0b3 | 2017-02-16 17:47:31 +0100 | [diff] [blame] | 1686 | post_amend_description=post_amend_description, |
Andrii Shyshkalov | 550e924 | 2017-04-12 17:14:49 +0200 | [diff] [blame] | 1687 | issue=issue, cc=cc, git_mirror=git_mirror, |
Aaron Gable | fd23808 | 2017-06-07 13:42:34 -0700 | [diff] [blame] | 1688 | custom_cl_base=custom_cl_base, tbr=tbr) |
tandrii@chromium.org | 09d7a6a | 2016-03-04 15:44:48 +0000 | [diff] [blame] | 1689 | # Uncomment when debugging. |
| 1690 | # 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] | 1691 | git_cl.main(['upload'] + upload_args) |
| 1692 | |
sivachandra@chromium.org | aebe87f | 2012-10-22 20:34:21 +0000 | [diff] [blame] | 1693 | def test_gerrit_upload_without_change_id(self): |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1694 | self._run_gerrit_upload_test( |
| 1695 | ['--no-squash'], |
| 1696 | 'desc\n\nBUG=\n', |
| 1697 | [], |
| 1698 | squash=False, |
| 1699 | post_amend_description='desc\n\nBUG=\n\nChange-Id: Ixxx') |
| 1700 | |
| 1701 | def test_gerrit_upload_without_change_id_override_nosquash(self): |
sivachandra@chromium.org | aebe87f | 2012-10-22 20:34:21 +0000 | [diff] [blame] | 1702 | self._run_gerrit_upload_test( |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1703 | [], |
jamesr@chromium.org | 35d1a84 | 2012-07-27 00:20:43 +0000 | [diff] [blame] | 1704 | 'desc\n\nBUG=\n', |
tandrii@chromium.org | 1062500 | 2016-03-04 20:03:47 +0000 | [diff] [blame] | 1705 | [], |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1706 | squash=False, |
| 1707 | squash_mode='override_nosquash', |
tandrii@chromium.org | 1062500 | 2016-03-04 20:03:47 +0000 | [diff] [blame] | 1708 | post_amend_description='desc\n\nBUG=\n\nChange-Id: Ixxx') |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1709 | |
sivachandra@chromium.org | aebe87f | 2012-10-22 20:34:21 +0000 | [diff] [blame] | 1710 | def test_gerrit_no_reviewer(self): |
| 1711 | self._run_gerrit_upload_test( |
| 1712 | [], |
tandrii@chromium.org | 09d7a6a | 2016-03-04 15:44:48 +0000 | [diff] [blame] | 1713 | 'desc\n\nBUG=\n\nChange-Id: I123456789\n', |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1714 | [], |
| 1715 | squash=False, |
| 1716 | squash_mode='override_nosquash') |
sivachandra@chromium.org | aebe87f | 2012-10-22 20:34:21 +0000 | [diff] [blame] | 1717 | |
Andrii Shyshkalov | c971239 | 2017-04-11 13:35:21 +0200 | [diff] [blame] | 1718 | def test_gerrit_patchset_title_bad_chars(self): |
Andrii Shyshkalov | febbae9 | 2017-04-05 15:05:20 +0000 | [diff] [blame] | 1719 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
| 1720 | self._run_gerrit_upload_test( |
| 1721 | ['-f', '-t', 'Don\'t put bad cha,.rs'], |
| 1722 | 'desc\n\nBUG=\n\nChange-Id: I123456789', |
| 1723 | squash=False, |
| 1724 | squash_mode='override_nosquash', |
| 1725 | title='Dont_put_bad_chars') |
| 1726 | self.assertIn( |
| 1727 | 'WARNING: Patchset title may only contain alphanumeric chars ' |
| 1728 | 'and spaces. You can edit it in the UI. See https://crbug.com/663787.\n' |
| 1729 | 'Cleaned up title: Dont put bad chars\n', |
| 1730 | git_cl.sys.stdout.getvalue()) |
| 1731 | |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1732 | def test_gerrit_reviewers_cmd_line(self): |
sivachandra@chromium.org | aebe87f | 2012-10-22 20:34:21 +0000 | [diff] [blame] | 1733 | self._run_gerrit_upload_test( |
tandrii@chromium.org | 8da4540 | 2016-05-24 23:11:03 +0000 | [diff] [blame] | 1734 | ['-r', 'foo@example.com', '--send-mail'], |
tandrii@chromium.org | 09d7a6a | 2016-03-04 15:44:48 +0000 | [diff] [blame] | 1735 | 'desc\n\nBUG=\n\nChange-Id: I123456789', |
tandrii@chromium.org | 8da4540 | 2016-05-24 23:11:03 +0000 | [diff] [blame] | 1736 | ['foo@example.com'], |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1737 | squash=False, |
| 1738 | squash_mode='override_nosquash', |
tandrii@chromium.org | 8da4540 | 2016-05-24 23:11:03 +0000 | [diff] [blame] | 1739 | notify=True) |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1740 | |
| 1741 | def test_gerrit_reviewer_multiple(self): |
sivachandra@chromium.org | aebe87f | 2012-10-22 20:34:21 +0000 | [diff] [blame] | 1742 | self._run_gerrit_upload_test( |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1743 | [], |
bradnelson | d975b30 | 2016-10-23 12:20:23 -0700 | [diff] [blame] | 1744 | 'desc\nTBR=reviewer@example.com\nBUG=\nR=another@example.com\n' |
| 1745 | 'CC=more@example.com,people@example.com\n\n' |
tandrii@chromium.org | 09d7a6a | 2016-03-04 15:44:48 +0000 | [diff] [blame] | 1746 | 'Change-Id: 123456789\n', |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1747 | ['reviewer@example.com', 'another@example.com'], |
| 1748 | squash=False, |
tandrii | 99a72f2 | 2016-08-17 14:33:24 -0700 | [diff] [blame] | 1749 | squash_mode='override_nosquash', |
Aaron Gable | fd23808 | 2017-06-07 13:42:34 -0700 | [diff] [blame] | 1750 | cc=['more@example.com', 'people@example.com'], |
| 1751 | tbr='reviewer@example.com') |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1752 | |
| 1753 | def test_gerrit_upload_squash_first_is_default(self): |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1754 | self._run_gerrit_upload_test( |
| 1755 | [], |
| 1756 | 'desc\nBUG=\n\nChange-Id: 123456789', |
| 1757 | [], |
| 1758 | expected_upstream_ref='origin/master') |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1759 | |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 1760 | def test_gerrit_upload_squash_first(self): |
bauerb@chromium.org | 27386dd | 2015-02-16 10:45:39 +0000 | [diff] [blame] | 1761 | self._run_gerrit_upload_test( |
| 1762 | ['--squash'], |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 1763 | 'desc\nBUG=\n\nChange-Id: 123456789', |
bauerb@chromium.org | 27386dd | 2015-02-16 10:45:39 +0000 | [diff] [blame] | 1764 | [], |
luqui@chromium.org | 609f395 | 2015-05-04 22:47:04 +0000 | [diff] [blame] | 1765 | squash=True, |
| 1766 | expected_upstream_ref='origin/master') |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1767 | |
Andrii Shyshkalov | 550e924 | 2017-04-12 17:14:49 +0200 | [diff] [blame] | 1768 | def test_gerrit_upload_squash_first_against_rev(self): |
| 1769 | custom_cl_base = 'custom_cl_base_rev_or_branch' |
| 1770 | self._run_gerrit_upload_test( |
| 1771 | ['--squash', custom_cl_base], |
| 1772 | 'desc\nBUG=\n\nChange-Id: 123456789', |
| 1773 | [], |
| 1774 | squash=True, |
| 1775 | expected_upstream_ref='origin/master', |
| 1776 | custom_cl_base=custom_cl_base) |
| 1777 | self.assertIn( |
| 1778 | 'If you proceed with upload, more than 1 CL may be created by Gerrit', |
| 1779 | sys.stdout.getvalue()) |
| 1780 | |
Andrii Shyshkalov | e9c78ff | 2017-02-06 15:53:13 +0100 | [diff] [blame] | 1781 | def test_gerrit_upload_squash_first_with_mirror(self): |
| 1782 | self._run_gerrit_upload_test( |
| 1783 | ['--squash'], |
| 1784 | 'desc\nBUG=\n\nChange-Id: 123456789', |
| 1785 | [], |
| 1786 | squash=True, |
| 1787 | expected_upstream_ref='origin/master', |
| 1788 | git_mirror='https://chromium.googlesource.com/yyy/zzz') |
| 1789 | |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 1790 | def test_gerrit_upload_squash_reupload(self): |
| 1791 | description = 'desc\nBUG=\n\nChange-Id: 123456789' |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 1792 | self._run_gerrit_upload_test( |
| 1793 | ['--squash'], |
| 1794 | description, |
| 1795 | [], |
| 1796 | squash=True, |
| 1797 | expected_upstream_ref='origin/master', |
| 1798 | issue=123456) |
| 1799 | |
Andrii Shyshkalov | 5c3d0b3 | 2017-02-16 17:47:31 +0100 | [diff] [blame] | 1800 | def test_gerrit_upload_squash_reupload_to_abandoned(self): |
| 1801 | self.mock(git_cl, 'DieWithError', |
| 1802 | lambda msg, change=None: self._mocked_call('DieWithError', msg)) |
| 1803 | description = 'desc\nBUG=\n\nChange-Id: 123456789' |
| 1804 | with self.assertRaises(SystemExitMock): |
| 1805 | self._run_gerrit_upload_test( |
| 1806 | ['--squash'], |
| 1807 | description, |
| 1808 | [], |
| 1809 | squash=True, |
| 1810 | expected_upstream_ref='origin/master', |
| 1811 | issue=123456, |
| 1812 | fetched_status='ABANDONED') |
| 1813 | |
Andrii Shyshkalov | bb86fbb | 2017-03-24 14:59:28 +0100 | [diff] [blame] | 1814 | def test_gerrit_upload_squash_reupload_to_not_owned(self): |
| 1815 | self.mock(git_cl.gerrit_util, 'GetAccountDetails', |
| 1816 | lambda *_, **__: {'email': 'yet-another@example.com'}) |
| 1817 | description = 'desc\nBUG=\n\nChange-Id: 123456789' |
| 1818 | self._run_gerrit_upload_test( |
| 1819 | ['--squash'], |
| 1820 | description, |
| 1821 | [], |
| 1822 | squash=True, |
| 1823 | expected_upstream_ref='origin/master', |
| 1824 | issue=123456, |
| 1825 | other_cl_owner='other@example.com') |
| 1826 | self.assertIn( |
Quinten Yearsley | 0c62da9 | 2017-05-31 13:39:42 -0700 | [diff] [blame] | 1827 | 'WARNING: Change 123456 is owned by other@example.com, but you ' |
Andrii Shyshkalov | bb86fbb | 2017-03-24 14:59:28 +0100 | [diff] [blame] | 1828 | 'authenticate to Gerrit as yet-another@example.com.\n' |
| 1829 | 'Uploading may fail due to lack of permissions', |
| 1830 | git_cl.sys.stdout.getvalue()) |
| 1831 | |
rmistry@google.com | 2dd9986 | 2015-06-22 12:22:18 +0000 | [diff] [blame] | 1832 | def test_upload_branch_deps(self): |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 1833 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
rmistry@google.com | 2dd9986 | 2015-06-22 12:22:18 +0000 | [diff] [blame] | 1834 | def mock_run_git(*args, **_kwargs): |
| 1835 | if args[0] == ['for-each-ref', |
| 1836 | '--format=%(refname:short) %(upstream:short)', |
| 1837 | 'refs/heads']: |
| 1838 | # Create a local branch dependency tree that looks like this: |
| 1839 | # test1 -> test2 -> test3 -> test4 -> test5 |
| 1840 | # -> test3.1 |
| 1841 | # test6 -> test0 |
| 1842 | branch_deps = [ |
| 1843 | 'test2 test1', # test1 -> test2 |
| 1844 | 'test3 test2', # test2 -> test3 |
| 1845 | 'test3.1 test2', # test2 -> test3.1 |
| 1846 | 'test4 test3', # test3 -> test4 |
| 1847 | 'test5 test4', # test4 -> test5 |
| 1848 | 'test6 test0', # test0 -> test6 |
| 1849 | 'test7', # test7 |
| 1850 | ] |
| 1851 | return '\n'.join(branch_deps) |
| 1852 | self.mock(git_cl, 'RunGit', mock_run_git) |
| 1853 | |
| 1854 | class RecordCalls: |
| 1855 | times_called = 0 |
| 1856 | record_calls = RecordCalls() |
| 1857 | def mock_CMDupload(*args, **_kwargs): |
| 1858 | record_calls.times_called += 1 |
| 1859 | return 0 |
| 1860 | self.mock(git_cl, 'CMDupload', mock_CMDupload) |
| 1861 | |
| 1862 | self.calls = [ |
Andrii Shyshkalov | abc26ac | 2017-03-14 14:49:38 +0100 | [diff] [blame] | 1863 | (('ask_for_data', 'This command will checkout all dependent branches ' |
| 1864 | 'and run "git cl upload". Press Enter to continue, ' |
| 1865 | 'or Ctrl+C to abort'), ''), |
| 1866 | ] |
rmistry@google.com | 2dd9986 | 2015-06-22 12:22:18 +0000 | [diff] [blame] | 1867 | |
| 1868 | class MockChangelist(): |
| 1869 | def __init__(self): |
| 1870 | pass |
| 1871 | def GetBranch(self): |
| 1872 | return 'test1' |
| 1873 | def GetIssue(self): |
| 1874 | return '123' |
| 1875 | def GetPatchset(self): |
| 1876 | return '1001' |
tandrii@chromium.org | 4c72b08 | 2016-03-31 22:26:35 +0000 | [diff] [blame] | 1877 | def IsGerrit(self): |
| 1878 | return False |
rmistry@google.com | 2dd9986 | 2015-06-22 12:22:18 +0000 | [diff] [blame] | 1879 | |
| 1880 | ret = git_cl.upload_branch_deps(MockChangelist(), []) |
| 1881 | # CMDupload should have been called 5 times because of 5 dependent branches. |
| 1882 | self.assertEquals(5, record_calls.times_called) |
| 1883 | self.assertEquals(0, ret) |
| 1884 | |
tandrii@chromium.org | 65874e1 | 2016-03-04 12:03:02 +0000 | [diff] [blame] | 1885 | def test_gerrit_change_id(self): |
| 1886 | self.calls = [ |
| 1887 | ((['git', 'write-tree'], ), |
| 1888 | 'hashtree'), |
| 1889 | ((['git', 'rev-parse', 'HEAD~0'], ), |
| 1890 | 'branch-parent'), |
| 1891 | ((['git', 'var', 'GIT_AUTHOR_IDENT'], ), |
| 1892 | 'A B <a@b.org> 1456848326 +0100'), |
| 1893 | ((['git', 'var', 'GIT_COMMITTER_IDENT'], ), |
| 1894 | 'C D <c@d.org> 1456858326 +0100'), |
| 1895 | ((['git', 'hash-object', '-t', 'commit', '--stdin'], ), |
| 1896 | 'hashchange'), |
| 1897 | ] |
| 1898 | change_id = git_cl.GenerateGerritChangeId('line1\nline2\n') |
| 1899 | self.assertEqual(change_id, 'Ihashchange') |
| 1900 | |
tandrii@chromium.org | 601e1d1 | 2016-06-03 13:03:54 +0000 | [diff] [blame] | 1901 | def test_desecription_append_footer(self): |
| 1902 | for init_desc, footer_line, expected_desc in [ |
| 1903 | # Use unique desc first lines for easy test failure identification. |
| 1904 | ('foo', 'R=one', 'foo\n\nR=one'), |
| 1905 | ('foo\n\nR=one', 'BUG=', 'foo\n\nR=one\nBUG='), |
| 1906 | ('foo\n\nR=one', 'Change-Id: Ixx', 'foo\n\nR=one\n\nChange-Id: Ixx'), |
| 1907 | ('foo\n\nChange-Id: Ixx', 'R=one', 'foo\n\nR=one\n\nChange-Id: Ixx'), |
| 1908 | ('foo\n\nR=one\n\nChange-Id: Ixx', 'TBR=two', |
| 1909 | 'foo\n\nR=one\nTBR=two\n\nChange-Id: Ixx'), |
| 1910 | ('foo\n\nR=one\n\nChange-Id: Ixx', 'Foo-Bar: baz', |
| 1911 | 'foo\n\nR=one\n\nChange-Id: Ixx\nFoo-Bar: baz'), |
| 1912 | ('foo\n\nChange-Id: Ixx', 'Foo-Bak: baz', |
| 1913 | 'foo\n\nChange-Id: Ixx\nFoo-Bak: baz'), |
| 1914 | ('foo', 'Change-Id: Ixx', 'foo\n\nChange-Id: Ixx'), |
| 1915 | ]: |
| 1916 | desc = git_cl.ChangeDescription(init_desc) |
| 1917 | desc.append_footer(footer_line) |
| 1918 | self.assertEqual(desc.description, expected_desc) |
| 1919 | |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 1920 | def test_update_reviewers(self): |
| 1921 | data = [ |
Robert Iannucci | 6c98dc6 | 2017-04-18 11:38:00 -0700 | [diff] [blame] | 1922 | ('foo', [], [], |
| 1923 | 'foo'), |
| 1924 | ('foo\nR=xx', [], [], |
| 1925 | 'foo\nR=xx'), |
| 1926 | ('foo\nTBR=xx', [], [], |
| 1927 | 'foo\nTBR=xx'), |
| 1928 | ('foo', ['a@c'], [], |
| 1929 | 'foo\n\nR=a@c'), |
| 1930 | ('foo\nR=xx', ['a@c'], [], |
| 1931 | 'foo\n\nR=a@c, xx'), |
| 1932 | ('foo\nTBR=xx', ['a@c'], [], |
| 1933 | 'foo\n\nR=a@c\nTBR=xx'), |
| 1934 | ('foo\nTBR=xx\nR=yy', ['a@c'], [], |
| 1935 | 'foo\n\nR=a@c, yy\nTBR=xx'), |
| 1936 | ('foo\nBUG=', ['a@c'], [], |
| 1937 | 'foo\nBUG=\nR=a@c'), |
| 1938 | ('foo\nR=xx\nTBR=yy\nR=bar', ['a@c'], [], |
| 1939 | 'foo\n\nR=a@c, bar, xx\nTBR=yy'), |
| 1940 | ('foo', ['a@c', 'b@c'], [], |
| 1941 | 'foo\n\nR=a@c, b@c'), |
| 1942 | ('foo\nBar\n\nR=\nBUG=', ['c@c'], [], |
| 1943 | 'foo\nBar\n\nR=c@c\nBUG='), |
| 1944 | ('foo\nBar\n\nR=\nBUG=\nR=', ['c@c'], [], |
| 1945 | 'foo\nBar\n\nR=c@c\nBUG='), |
maruel@chromium.org | c6f60e8 | 2013-04-19 17:01:57 +0000 | [diff] [blame] | 1946 | # Same as the line before, but full of whitespaces. |
| 1947 | ( |
Robert Iannucci | 6c98dc6 | 2017-04-18 11:38:00 -0700 | [diff] [blame] | 1948 | 'foo\nBar\n\n R = \n BUG = \n R = ', ['c@c'], [], |
maruel@chromium.org | c6f60e8 | 2013-04-19 17:01:57 +0000 | [diff] [blame] | 1949 | 'foo\nBar\n\nR=c@c\n BUG =', |
| 1950 | ), |
| 1951 | # Whitespaces aren't interpreted as new lines. |
Robert Iannucci | 6c98dc6 | 2017-04-18 11:38:00 -0700 | [diff] [blame] | 1952 | ('foo BUG=allo R=joe ', ['c@c'], [], |
| 1953 | 'foo BUG=allo R=joe\n\nR=c@c'), |
| 1954 | # Redundant TBRs get promoted to Rs |
| 1955 | ('foo\n\nR=a@c\nTBR=t@c', ['b@c', 'a@c'], ['a@c', 't@c'], |
| 1956 | 'foo\n\nR=a@c, b@c\nTBR=t@c'), |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 1957 | ] |
Robert Iannucci | 6c98dc6 | 2017-04-18 11:38:00 -0700 | [diff] [blame] | 1958 | expected = [i[-1] for i in data] |
maruel@chromium.org | c6f60e8 | 2013-04-19 17:01:57 +0000 | [diff] [blame] | 1959 | actual = [] |
Robert Iannucci | 6c98dc6 | 2017-04-18 11:38:00 -0700 | [diff] [blame] | 1960 | for orig, reviewers, tbrs, _expected in data: |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 1961 | obj = git_cl.ChangeDescription(orig) |
Robert Iannucci | 6c98dc6 | 2017-04-18 11:38:00 -0700 | [diff] [blame] | 1962 | obj.update_reviewers(reviewers, tbrs) |
maruel@chromium.org | c6f60e8 | 2013-04-19 17:01:57 +0000 | [diff] [blame] | 1963 | actual.append(obj.description) |
| 1964 | self.assertEqual(expected, actual) |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 1965 | |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 1966 | def test_get_target_ref(self): |
| 1967 | # Check remote or remote branch not present. |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1968 | self.assertEqual(None, git_cl.GetTargetRef('origin', None, 'master')) |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 1969 | self.assertEqual(None, git_cl.GetTargetRef(None, |
| 1970 | 'refs/remotes/origin/master', |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1971 | 'master')) |
bauerb@chromium.org | 27386dd | 2015-02-16 10:45:39 +0000 | [diff] [blame] | 1972 | |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 1973 | # Check default target refs for branches. |
| 1974 | self.assertEqual('refs/heads/master', |
| 1975 | git_cl.GetTargetRef('origin', 'refs/remotes/origin/master', |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1976 | None)) |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 1977 | self.assertEqual('refs/heads/master', |
| 1978 | git_cl.GetTargetRef('origin', 'refs/remotes/origin/lkgr', |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1979 | None)) |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 1980 | self.assertEqual('refs/heads/master', |
| 1981 | git_cl.GetTargetRef('origin', 'refs/remotes/origin/lkcr', |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1982 | None)) |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 1983 | self.assertEqual('refs/branch-heads/123', |
| 1984 | git_cl.GetTargetRef('origin', |
| 1985 | 'refs/remotes/branch-heads/123', |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1986 | None)) |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 1987 | self.assertEqual('refs/diff/test', |
| 1988 | git_cl.GetTargetRef('origin', |
| 1989 | 'refs/remotes/origin/refs/diff/test', |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1990 | None)) |
rmistry@google.com | c68112d | 2015-03-03 12:48:06 +0000 | [diff] [blame] | 1991 | self.assertEqual('refs/heads/chrome/m42', |
| 1992 | git_cl.GetTargetRef('origin', |
| 1993 | 'refs/remotes/origin/chrome/m42', |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1994 | None)) |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 1995 | |
| 1996 | # Check target refs for user-specified target branch. |
| 1997 | for branch in ('branch-heads/123', 'remotes/branch-heads/123', |
| 1998 | 'refs/remotes/branch-heads/123'): |
| 1999 | self.assertEqual('refs/branch-heads/123', |
| 2000 | git_cl.GetTargetRef('origin', |
| 2001 | 'refs/remotes/origin/master', |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 2002 | branch)) |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 2003 | for branch in ('origin/master', 'remotes/origin/master', |
| 2004 | 'refs/remotes/origin/master'): |
| 2005 | self.assertEqual('refs/heads/master', |
| 2006 | git_cl.GetTargetRef('origin', |
| 2007 | 'refs/remotes/branch-heads/123', |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 2008 | branch)) |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 2009 | for branch in ('master', 'heads/master', 'refs/heads/master'): |
| 2010 | self.assertEqual('refs/heads/master', |
| 2011 | git_cl.GetTargetRef('origin', |
| 2012 | 'refs/remotes/branch-heads/123', |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 2013 | branch)) |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 2014 | |
wychen@chromium.org | a872e75 | 2015-04-28 23:42:18 +0000 | [diff] [blame] | 2015 | def test_patch_when_dirty(self): |
| 2016 | # Patch when local tree is dirty |
| 2017 | self.mock(git_common, 'is_dirty_git_tree', lambda x: True) |
| 2018 | self.assertNotEqual(git_cl.main(['patch', '123456']), 0) |
| 2019 | |
| 2020 | def test_diff_when_dirty(self): |
| 2021 | # Do 'git cl diff' when local tree is dirty |
| 2022 | self.mock(git_common, 'is_dirty_git_tree', lambda x: True) |
| 2023 | self.assertNotEqual(git_cl.main(['diff']), 0) |
| 2024 | |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 2025 | @staticmethod |
| 2026 | def _get_gerrit_codereview_server_calls(branch, value=None, |
| 2027 | git_short_host='host', |
| 2028 | detect_branch=True): |
| 2029 | """Returns calls executed by _GerritChangelistImpl.GetCodereviewServer. |
| 2030 | |
| 2031 | If value is given, branch.<BRANCH>.gerritcodereview is already set. |
| 2032 | """ |
| 2033 | calls = [] |
| 2034 | if detect_branch: |
| 2035 | calls.append(((['git', 'symbolic-ref', 'HEAD'],), branch)) |
| 2036 | calls.append(((['git', 'config', 'branch.' + branch + '.gerritserver'],), |
| 2037 | CERR1 if value is None else value)) |
| 2038 | if value is None: |
| 2039 | calls += [ |
| 2040 | ((['git', 'config', 'branch.' + branch + '.merge'],), |
| 2041 | 'refs/heads' + branch), |
| 2042 | ((['git', 'config', 'branch.' + branch + '.remote'],), |
| 2043 | 'origin'), |
| 2044 | ((['git', 'config', 'remote.origin.url'],), |
| 2045 | 'https://%s.googlesource.com/my/repo' % git_short_host), |
| 2046 | ] |
| 2047 | return calls |
| 2048 | |
Andrii Shyshkalov | c971239 | 2017-04-11 13:35:21 +0200 | [diff] [blame] | 2049 | def _patch_common(self, default_codereview=None, force_codereview=False, |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 2050 | new_branch=False, git_short_host='host', |
Andrii Shyshkalov | c971239 | 2017-04-11 13:35:21 +0200 | [diff] [blame] | 2051 | detect_gerrit_server=True, |
| 2052 | actual_codereview=None): |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 2053 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 2054 | self.mock(git_cl._RietveldChangelistImpl, 'GetMostRecentPatchset', |
| 2055 | lambda x: '60001') |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 2056 | self.mock(git_cl._RietveldChangelistImpl, 'FetchDescription', |
Kenneth Russell | 61e2ed4 | 2017-02-15 11:47:13 -0800 | [diff] [blame] | 2057 | lambda *a, **kw: 'Description') |
wychen@chromium.org | a872e75 | 2015-04-28 23:42:18 +0000 | [diff] [blame] | 2058 | self.mock(git_cl, 'IsGitVersionAtLeast', lambda *args: True) |
| 2059 | |
tandrii | df09a46 | 2016-08-18 16:23:55 -0700 | [diff] [blame] | 2060 | if new_branch: |
| 2061 | self.calls = [((['git', 'new-branch', 'master'],), ''),] |
| 2062 | else: |
| 2063 | self.calls = [((['git', 'symbolic-ref', 'HEAD'],), 'master')] |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 2064 | |
Andrii Shyshkalov | c971239 | 2017-04-11 13:35:21 +0200 | [diff] [blame] | 2065 | if default_codereview: |
tandrii@chromium.org | dde6462 | 2016-04-13 17:11:21 +0000 | [diff] [blame] | 2066 | if not force_codereview: |
Andrii Shyshkalov | c971239 | 2017-04-11 13:35:21 +0200 | [diff] [blame] | 2067 | # These calls detect codereview to use. |
tandrii@chromium.org | dde6462 | 2016-04-13 17:11:21 +0000 | [diff] [blame] | 2068 | self.calls += [ |
Andrii Shyshkalov | c971239 | 2017-04-11 13:35:21 +0200 | [diff] [blame] | 2069 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
| 2070 | ((['git', 'config', 'branch.master.rietveldissue'],), CERR1), |
| 2071 | ((['git', 'config', 'branch.master.gerritissue'],), CERR1), |
| 2072 | ((['git', 'config', 'rietveld.autoupdate'],), CERR1), |
tandrii@chromium.org | dde6462 | 2016-04-13 17:11:21 +0000 | [diff] [blame] | 2073 | ] |
Andrii Shyshkalov | c971239 | 2017-04-11 13:35:21 +0200 | [diff] [blame] | 2074 | if default_codereview == 'gerrit': |
| 2075 | if not force_codereview: |
| 2076 | self.calls += [ |
| 2077 | ((['git', 'config', 'gerrit.host'],), 'true'), |
| 2078 | ] |
| 2079 | if detect_gerrit_server: |
| 2080 | self.calls += self._get_gerrit_codereview_server_calls( |
| 2081 | 'master', git_short_host=git_short_host, |
| 2082 | detect_branch=not new_branch and force_codereview) |
| 2083 | actual_codereview = 'gerrit' |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 2084 | |
Andrii Shyshkalov | c971239 | 2017-04-11 13:35:21 +0200 | [diff] [blame] | 2085 | elif default_codereview == 'rietveld': |
| 2086 | self.calls += [ |
| 2087 | ((['git', 'config', 'gerrit.host'],), CERR1), |
| 2088 | ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'), |
| 2089 | ((['git', 'config', 'branch.master.rietveldserver',],), CERR1), |
| 2090 | ] |
| 2091 | actual_codereview = 'rietveld' |
| 2092 | |
| 2093 | if actual_codereview == 'rietveld': |
| 2094 | self.calls += [ |
| 2095 | ((['git', 'rev-parse', '--show-cdup'],), ''), |
| 2096 | ] |
| 2097 | if not default_codereview: |
| 2098 | self.calls += [ |
| 2099 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
| 2100 | ] |
| 2101 | elif actual_codereview == 'gerrit': |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 2102 | self.calls += [ |
| 2103 | (('GetChangeDetail', git_short_host + '-review.googlesource.com', |
Andrii Shyshkalov | 8039be7 | 2017-01-26 09:38:18 +0100 | [diff] [blame] | 2104 | '123456', ['ALL_REVISIONS', 'CURRENT_COMMIT']), |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 2105 | { |
| 2106 | 'current_revision': '7777777777', |
| 2107 | 'revisions': { |
| 2108 | '1111111111': { |
| 2109 | '_number': 1, |
| 2110 | 'fetch': {'http': { |
| 2111 | 'url': 'https://%s.googlesource.com/my/repo' % git_short_host, |
| 2112 | 'ref': 'refs/changes/56/123456/1', |
| 2113 | }}, |
| 2114 | }, |
| 2115 | '7777777777': { |
| 2116 | '_number': 7, |
| 2117 | 'fetch': {'http': { |
| 2118 | 'url': 'https://%s.googlesource.com/my/repo' % git_short_host, |
| 2119 | 'ref': 'refs/changes/56/123456/7', |
| 2120 | }}, |
| 2121 | }, |
| 2122 | }, |
| 2123 | }), |
| 2124 | ] |
wychen@chromium.org | a872e75 | 2015-04-28 23:42:18 +0000 | [diff] [blame] | 2125 | |
Andrii Shyshkalov | c971239 | 2017-04-11 13:35:21 +0200 | [diff] [blame] | 2126 | def _common_patch_rietveld_successful(self, **kwargs): |
| 2127 | self._patch_common(**kwargs) |
wychen@chromium.org | a872e75 | 2015-04-28 23:42:18 +0000 | [diff] [blame] | 2128 | self.calls += [ |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 2129 | ((['git', 'config', 'branch.master.rietveldissue', '123456'],), |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 2130 | ''), |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 2131 | ((['git', 'config', 'branch.master.rietveldserver', |
| 2132 | 'https://codereview.example.com'],), ''), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 2133 | ((['git', 'config', 'branch.master.rietveldpatchset', '60001'],), |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 2134 | ''), |
Aaron Gable | d343c63 | 2017-03-15 11:02:26 -0700 | [diff] [blame] | 2135 | ((['git', 'commit', '-m', |
| 2136 | 'Description\n\n' + |
| 2137 | 'patch from issue 123456 at patchset 60001 ' + |
| 2138 | '(http://crrev.com/123456#ps60001)'],), ''), |
wychen@chromium.org | a872e75 | 2015-04-28 23:42:18 +0000 | [diff] [blame] | 2139 | ] |
tandrii@chromium.org | c2786d9 | 2016-05-31 19:53:50 +0000 | [diff] [blame] | 2140 | |
Andrii Shyshkalov | c971239 | 2017-04-11 13:35:21 +0200 | [diff] [blame] | 2141 | def test_patch_rietveld_default(self): |
| 2142 | self._common_patch_rietveld_successful(default_codereview='rietveld') |
wychen@chromium.org | a872e75 | 2015-04-28 23:42:18 +0000 | [diff] [blame] | 2143 | self.assertEqual(git_cl.main(['patch', '123456']), 0) |
| 2144 | |
Andrii Shyshkalov | c971239 | 2017-04-11 13:35:21 +0200 | [diff] [blame] | 2145 | def test_patch_rietveld_successful_new_branch(self): |
| 2146 | self._common_patch_rietveld_successful(default_codereview='rietveld', |
| 2147 | new_branch=True) |
tandrii@chromium.org | c2786d9 | 2016-05-31 19:53:50 +0000 | [diff] [blame] | 2148 | self.assertEqual(git_cl.main(['patch', '-b', 'master', '123456']), 0) |
| 2149 | |
Andrii Shyshkalov | c971239 | 2017-04-11 13:35:21 +0200 | [diff] [blame] | 2150 | def test_patch_rietveld_guess_by_url(self): |
| 2151 | self._common_patch_rietveld_successful( |
| 2152 | default_codereview=None, # It doesn't matter. |
| 2153 | actual_codereview='rietveld') |
| 2154 | self.assertEqual(git_cl.main( |
| 2155 | ['patch', 'https://codereview.example.com/123456']), 0) |
| 2156 | |
| 2157 | def test_patch_rietveld_conflict(self): |
| 2158 | self._patch_common(default_codereview='rietveld') |
skobes | 6468b90 | 2016-10-24 08:45:10 -0700 | [diff] [blame] | 2159 | GitCheckoutMock.conflict = True |
wychen@chromium.org | a872e75 | 2015-04-28 23:42:18 +0000 | [diff] [blame] | 2160 | self.assertNotEqual(git_cl.main(['patch', '123456']), 0) |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 2161 | |
Andrii Shyshkalov | c971239 | 2017-04-11 13:35:21 +0200 | [diff] [blame] | 2162 | def test_patch_gerrit_default(self): |
| 2163 | self._patch_common(default_codereview='gerrit', git_short_host='chromium', |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 2164 | detect_gerrit_server=True) |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 2165 | self.calls += [ |
| 2166 | ((['git', 'fetch', 'https://chromium.googlesource.com/my/repo', |
| 2167 | 'refs/changes/56/123456/7'],), ''), |
Aaron Gable | 62619a3 | 2017-06-16 08:22:09 -0700 | [diff] [blame] | 2168 | ((['git', 'cherry-pick', 'FETCH_HEAD'],), ''), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 2169 | ((['git', 'config', 'branch.master.gerritissue', '123456'],), |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 2170 | ''), |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 2171 | ((['git', 'config', 'branch.master.gerritserver', |
| 2172 | 'https://chromium-review.googlesource.com'],), ''), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 2173 | ((['git', 'config', 'branch.master.gerritpatchset', '7'],), ''), |
Aaron Gable | 9387b4f | 2017-06-08 10:50:03 -0700 | [diff] [blame] | 2174 | ((['git', 'rev-parse', 'FETCH_HEAD'],), 'deadbeef'), |
| 2175 | ((['git', 'config', 'branch.master.last-upload-hash', 'deadbeef'],), ''), |
| 2176 | ((['git', 'config', 'branch.master.gerritsquashhash', 'deadbeef'],), ''), |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 2177 | ] |
| 2178 | self.assertEqual(git_cl.main(['patch', '123456']), 0) |
| 2179 | |
Andrii Shyshkalov | c971239 | 2017-04-11 13:35:21 +0200 | [diff] [blame] | 2180 | def test_patch_gerrit_force(self): |
| 2181 | self._patch_common(default_codereview='gerrit', force_codereview=True, |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 2182 | git_short_host='host', detect_gerrit_server=True) |
tandrii@chromium.org | dde6462 | 2016-04-13 17:11:21 +0000 | [diff] [blame] | 2183 | self.calls += [ |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 2184 | ((['git', 'fetch', 'https://host.googlesource.com/my/repo', |
tandrii@chromium.org | dde6462 | 2016-04-13 17:11:21 +0000 | [diff] [blame] | 2185 | 'refs/changes/56/123456/7'],), ''), |
Aaron Gable | 9387b4f | 2017-06-08 10:50:03 -0700 | [diff] [blame] | 2186 | ((['git', 'reset', '--hard', 'FETCH_HEAD'],), ''), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 2187 | ((['git', 'config', 'branch.master.gerritissue', '123456'],), |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 2188 | ''), |
tandrii@chromium.org | dde6462 | 2016-04-13 17:11:21 +0000 | [diff] [blame] | 2189 | ((['git', 'config', 'branch.master.gerritserver', |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 2190 | 'https://host-review.googlesource.com'],), ''), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 2191 | ((['git', 'config', 'branch.master.gerritpatchset', '7'],), ''), |
Aaron Gable | 9387b4f | 2017-06-08 10:50:03 -0700 | [diff] [blame] | 2192 | ((['git', 'rev-parse', 'FETCH_HEAD'],), 'deadbeef'), |
| 2193 | ((['git', 'config', 'branch.master.last-upload-hash', 'deadbeef'],), ''), |
| 2194 | ((['git', 'config', 'branch.master.gerritsquashhash', 'deadbeef'],), ''), |
tandrii@chromium.org | dde6462 | 2016-04-13 17:11:21 +0000 | [diff] [blame] | 2195 | ] |
Aaron Gable | 62619a3 | 2017-06-16 08:22:09 -0700 | [diff] [blame] | 2196 | self.assertEqual(git_cl.main(['patch', '--gerrit', '123456', '--force']), 0) |
tandrii@chromium.org | dde6462 | 2016-04-13 17:11:21 +0000 | [diff] [blame] | 2197 | |
Andrii Shyshkalov | c971239 | 2017-04-11 13:35:21 +0200 | [diff] [blame] | 2198 | def test_patch_gerrit_guess_by_url(self): |
| 2199 | self._patch_common( |
| 2200 | default_codereview=None, # It doesn't matter what's default. |
| 2201 | actual_codereview='gerrit', |
| 2202 | git_short_host='else', detect_gerrit_server=False) |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 2203 | self.calls += [ |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 2204 | ((['git', 'fetch', 'https://else.googlesource.com/my/repo', |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 2205 | 'refs/changes/56/123456/1'],), ''), |
Aaron Gable | 62619a3 | 2017-06-16 08:22:09 -0700 | [diff] [blame] | 2206 | ((['git', 'cherry-pick', 'FETCH_HEAD'],), ''), |
Andrii Shyshkalov | c971239 | 2017-04-11 13:35:21 +0200 | [diff] [blame] | 2207 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 2208 | ((['git', 'config', 'branch.master.gerritissue', '123456'],), |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 2209 | ''), |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 2210 | ((['git', 'config', 'branch.master.gerritserver', |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 2211 | 'https://else-review.googlesource.com'],), ''), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 2212 | ((['git', 'config', 'branch.master.gerritpatchset', '1'],), ''), |
Aaron Gable | 9387b4f | 2017-06-08 10:50:03 -0700 | [diff] [blame] | 2213 | ((['git', 'rev-parse', 'FETCH_HEAD'],), 'deadbeef'), |
| 2214 | ((['git', 'config', 'branch.master.last-upload-hash', 'deadbeef'],), ''), |
| 2215 | ((['git', 'config', 'branch.master.gerritsquashhash', 'deadbeef'],), ''), |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 2216 | ] |
| 2217 | self.assertEqual(git_cl.main( |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 2218 | ['patch', 'https://else-review.googlesource.com/#/c/123456/1']), 0) |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 2219 | |
Andrii Shyshkalov | c971239 | 2017-04-11 13:35:21 +0200 | [diff] [blame] | 2220 | def test_patch_gerrit_guess_by_url_like_rietveld(self): |
| 2221 | self._patch_common( |
| 2222 | default_codereview=None, # It doesn't matter what's default. |
| 2223 | actual_codereview='gerrit', |
| 2224 | git_short_host='else', detect_gerrit_server=False) |
| 2225 | self.calls += [ |
| 2226 | ((['git', 'fetch', 'https://else.googlesource.com/my/repo', |
| 2227 | 'refs/changes/56/123456/1'],), ''), |
Aaron Gable | 62619a3 | 2017-06-16 08:22:09 -0700 | [diff] [blame] | 2228 | ((['git', 'cherry-pick', 'FETCH_HEAD'],), ''), |
Andrii Shyshkalov | c971239 | 2017-04-11 13:35:21 +0200 | [diff] [blame] | 2229 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
| 2230 | ((['git', 'config', 'branch.master.gerritissue', '123456'],), |
| 2231 | ''), |
| 2232 | ((['git', 'config', 'branch.master.gerritserver', |
| 2233 | 'https://else-review.googlesource.com'],), ''), |
| 2234 | ((['git', 'config', 'branch.master.gerritpatchset', '1'],), ''), |
Aaron Gable | 9387b4f | 2017-06-08 10:50:03 -0700 | [diff] [blame] | 2235 | ((['git', 'rev-parse', 'FETCH_HEAD'],), 'deadbeef'), |
| 2236 | ((['git', 'config', 'branch.master.last-upload-hash', 'deadbeef'],), ''), |
| 2237 | ((['git', 'config', 'branch.master.gerritsquashhash', 'deadbeef'],), ''), |
Andrii Shyshkalov | c971239 | 2017-04-11 13:35:21 +0200 | [diff] [blame] | 2238 | ] |
| 2239 | self.assertEqual(git_cl.main( |
| 2240 | ['patch', 'https://else-review.googlesource.com/123456/1']), 0) |
| 2241 | |
| 2242 | def test_patch_gerrit_conflict(self): |
| 2243 | self._patch_common(default_codereview='gerrit', detect_gerrit_server=True, |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 2244 | git_short_host='chromium') |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 2245 | self.calls += [ |
| 2246 | ((['git', 'fetch', 'https://chromium.googlesource.com/my/repo', |
Andrii Shyshkalov | c971239 | 2017-04-11 13:35:21 +0200 | [diff] [blame] | 2247 | 'refs/changes/56/123456/7'],), ''), |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 2248 | ((['git', 'cherry-pick', 'FETCH_HEAD'],), CERR1), |
| 2249 | ((['DieWithError', 'Command "git cherry-pick FETCH_HEAD" failed.\n'],), |
| 2250 | SystemExitMock()), |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 2251 | ] |
| 2252 | with self.assertRaises(SystemExitMock): |
Andrii Shyshkalov | c971239 | 2017-04-11 13:35:21 +0200 | [diff] [blame] | 2253 | git_cl.main(['patch', '123456']) |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 2254 | |
Andrii Shyshkalov | c971239 | 2017-04-11 13:35:21 +0200 | [diff] [blame] | 2255 | def test_patch_gerrit_not_exists(self): |
Andrii Shyshkalov | c6c8b4c | 2016-11-09 20:51:20 +0100 | [diff] [blame] | 2256 | def notExists(_issue, *_, **kwargs): |
Andrii Shyshkalov | c6c8b4c | 2016-11-09 20:51:20 +0100 | [diff] [blame] | 2257 | raise git_cl.gerrit_util.GerritError(404, '') |
| 2258 | self.mock(git_cl.gerrit_util, 'GetChangeDetail', notExists) |
| 2259 | |
tandrii | c2405f5 | 2016-10-10 08:13:15 -0700 | [diff] [blame] | 2260 | self.calls = [ |
| 2261 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
| 2262 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
| 2263 | ((['git', 'config', 'branch.master.rietveldissue'],), CERR1), |
| 2264 | ((['git', 'config', 'branch.master.gerritissue'],), CERR1), |
| 2265 | ((['git', 'config', 'rietveld.autoupdate'],), CERR1), |
| 2266 | ((['git', 'config', 'gerrit.host'],), 'true'), |
Andrii Shyshkalov | c971239 | 2017-04-11 13:35:21 +0200 | [diff] [blame] | 2267 | ((['git', 'config', 'branch.master.gerritserver'],), CERR1), |
| 2268 | ((['git', 'config', 'branch.master.merge'],), 'refs/heads/master'), |
| 2269 | ((['git', 'config', 'branch.master.remote'],), 'origin'), |
| 2270 | ((['git', 'config', 'remote.origin.url'],), |
| 2271 | 'https://chromium.googlesource.com/my/repo'), |
| 2272 | ((['DieWithError', |
| 2273 | 'change 123456 at https://chromium-review.googlesource.com does not ' |
| 2274 | 'exist or you have no access to it'],), SystemExitMock()), |
tandrii | c2405f5 | 2016-10-10 08:13:15 -0700 | [diff] [blame] | 2275 | ] |
| 2276 | with self.assertRaises(SystemExitMock): |
Andrii Shyshkalov | c971239 | 2017-04-11 13:35:21 +0200 | [diff] [blame] | 2277 | self.assertEqual(1, git_cl.main(['patch', '123456'])) |
tandrii | c2405f5 | 2016-10-10 08:13:15 -0700 | [diff] [blame] | 2278 | |
tandrii@chromium.org | 5df290f | 2016-04-11 16:12:29 +0000 | [diff] [blame] | 2279 | def _checkout_calls(self): |
| 2280 | return [ |
| 2281 | ((['git', 'config', '--local', '--get-regexp', |
| 2282 | 'branch\\..*\\.rietveldissue'], ), |
| 2283 | ('branch.retrying.rietveldissue 1111111111\n' |
| 2284 | 'branch.some-fix.rietveldissue 2222222222\n')), |
| 2285 | ((['git', 'config', '--local', '--get-regexp', |
| 2286 | 'branch\\..*\\.gerritissue'], ), |
| 2287 | ('branch.ger-branch.gerritissue 123456\n' |
| 2288 | 'branch.gbranch654.gerritissue 654321\n')), |
| 2289 | ] |
| 2290 | |
| 2291 | def test_checkout_gerrit(self): |
| 2292 | """Tests git cl checkout <issue>.""" |
| 2293 | self.calls = self._checkout_calls() |
| 2294 | self.calls += [((['git', 'checkout', 'ger-branch'], ), '')] |
| 2295 | self.assertEqual(0, git_cl.main(['checkout', '123456'])) |
| 2296 | |
| 2297 | def test_checkout_rietveld(self): |
| 2298 | """Tests git cl checkout <issue>.""" |
| 2299 | self.calls = self._checkout_calls() |
| 2300 | self.calls += [((['git', 'checkout', 'some-fix'], ), '')] |
| 2301 | self.assertEqual(0, git_cl.main(['checkout', '2222222222'])) |
| 2302 | |
| 2303 | def test_checkout_not_found(self): |
| 2304 | """Tests git cl checkout <issue>.""" |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 2305 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
tandrii@chromium.org | 5df290f | 2016-04-11 16:12:29 +0000 | [diff] [blame] | 2306 | self.calls = self._checkout_calls() |
| 2307 | self.assertEqual(1, git_cl.main(['checkout', '99999'])) |
| 2308 | |
tandrii@chromium.org | 26c8fd2 | 2016-04-11 21:33:21 +0000 | [diff] [blame] | 2309 | def test_checkout_no_branch_issues(self): |
| 2310 | """Tests git cl checkout <issue>.""" |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 2311 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
tandrii@chromium.org | 26c8fd2 | 2016-04-11 21:33:21 +0000 | [diff] [blame] | 2312 | self.calls = [ |
| 2313 | ((['git', 'config', '--local', '--get-regexp', |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 2314 | 'branch\\..*\\.rietveldissue'], ), CERR1), |
tandrii@chromium.org | 26c8fd2 | 2016-04-11 21:33:21 +0000 | [diff] [blame] | 2315 | ((['git', 'config', '--local', '--get-regexp', |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 2316 | 'branch\\..*\\.gerritissue'], ), CERR1), |
tandrii@chromium.org | 26c8fd2 | 2016-04-11 21:33:21 +0000 | [diff] [blame] | 2317 | ] |
| 2318 | self.assertEqual(1, git_cl.main(['checkout', '99999'])) |
| 2319 | |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 2320 | def _test_gerrit_ensure_authenticated_common(self, auth, |
| 2321 | skip_auth_check=False): |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 2322 | self.mock(git_cl.gerrit_util, 'CookiesAuthenticator', |
| 2323 | CookiesAuthenticatorMockFactory(hosts_with_creds=auth)) |
| 2324 | self.mock(git_cl, 'DieWithError', |
Christopher Lam | f732cd5 | 2017-01-24 12:40:11 +1100 | [diff] [blame] | 2325 | lambda msg, change=None: self._mocked_call(['DieWithError', msg])) |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 2326 | self.calls = self._gerrit_ensure_auth_calls(skip_auth_check=skip_auth_check) |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 2327 | cl = git_cl.Changelist(codereview='gerrit') |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 2328 | cl.branch = 'master' |
| 2329 | cl.branchref = 'refs/heads/master' |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 2330 | cl.lookedup_issue = True |
| 2331 | return cl |
| 2332 | |
| 2333 | def test_gerrit_ensure_authenticated_missing(self): |
| 2334 | cl = self._test_gerrit_ensure_authenticated_common(auth={ |
Andrii Shyshkalov | bb86fbb | 2017-03-24 14:59:28 +0100 | [diff] [blame] | 2335 | 'chromium.googlesource.com': ('git-is.ok', '', 'but gerrit is missing'), |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 2336 | }) |
| 2337 | self.calls.append( |
| 2338 | ((['DieWithError', |
| 2339 | 'Credentials for the following hosts are required:\n' |
| 2340 | ' chromium-review.googlesource.com\n' |
| 2341 | 'These are read from ~/.gitcookies (or legacy ~/.netrc)\n' |
Andrii Shyshkalov | 0a0b067 | 2017-03-16 16:27:48 +0100 | [diff] [blame] | 2342 | 'You can (re)generate your credentials by visiting ' |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 2343 | 'https://chromium-review.googlesource.com/new-password'],), ''),) |
| 2344 | self.assertIsNone(cl.EnsureAuthenticated(force=False)) |
| 2345 | |
| 2346 | def test_gerrit_ensure_authenticated_conflict(self): |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 2347 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 2348 | cl = self._test_gerrit_ensure_authenticated_common(auth={ |
Andrii Shyshkalov | bb86fbb | 2017-03-24 14:59:28 +0100 | [diff] [blame] | 2349 | 'chromium.googlesource.com': |
| 2350 | ('git-one.example.com', None, 'secret1'), |
| 2351 | 'chromium-review.googlesource.com': |
| 2352 | ('git-other.example.com', None, 'secret2'), |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 2353 | }) |
| 2354 | self.calls.append( |
Andrii Shyshkalov | abc26ac | 2017-03-14 14:49:38 +0100 | [diff] [blame] | 2355 | (('ask_for_data', 'If you know what you are doing ' |
| 2356 | 'press Enter to continue, or Ctrl+C to abort'), '')) |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 2357 | self.assertIsNone(cl.EnsureAuthenticated(force=False)) |
| 2358 | |
| 2359 | def test_gerrit_ensure_authenticated_ok(self): |
| 2360 | cl = self._test_gerrit_ensure_authenticated_common(auth={ |
Andrii Shyshkalov | bb86fbb | 2017-03-24 14:59:28 +0100 | [diff] [blame] | 2361 | 'chromium.googlesource.com': |
| 2362 | ('git-same.example.com', None, 'secret'), |
| 2363 | 'chromium-review.googlesource.com': |
| 2364 | ('git-same.example.com', None, 'secret'), |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 2365 | }) |
| 2366 | self.assertIsNone(cl.EnsureAuthenticated(force=False)) |
| 2367 | |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 2368 | def test_gerrit_ensure_authenticated_skipped(self): |
| 2369 | cl = self._test_gerrit_ensure_authenticated_common( |
| 2370 | auth={}, skip_auth_check=True) |
| 2371 | self.assertIsNone(cl.EnsureAuthenticated(force=False)) |
| 2372 | |
tandrii@chromium.org | fa330e8 | 2016-04-13 17:09:52 +0000 | [diff] [blame] | 2373 | def test_cmd_set_commit_rietveld(self): |
tandrii | 4d84359 | 2016-07-27 08:22:56 -0700 | [diff] [blame] | 2374 | self.mock(git_cl._RietveldChangelistImpl, 'SetFlags', |
| 2375 | lambda _, v: self._mocked_call(['SetFlags', v])) |
tandrii@chromium.org | fa330e8 | 2016-04-13 17:09:52 +0000 | [diff] [blame] | 2376 | self.calls = [ |
| 2377 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 2378 | ((['git', 'config', 'branch.feature.rietveldissue'],), '123'), |
tandrii@chromium.org | fa330e8 | 2016-04-13 17:09:52 +0000 | [diff] [blame] | 2379 | ((['git', 'config', 'rietveld.autoupdate'],), ''), |
| 2380 | ((['git', 'config', 'rietveld.server'],), ''), |
| 2381 | ((['git', 'config', 'rietveld.server'],), ''), |
| 2382 | ((['git', 'config', 'branch.feature.rietveldserver'],), |
| 2383 | 'https://codereview.chromium.org'), |
tandrii | 4d84359 | 2016-07-27 08:22:56 -0700 | [diff] [blame] | 2384 | ((['SetFlags', {'commit': '1', 'cq_dry_run': '0'}], ), ''), |
tandrii@chromium.org | fa330e8 | 2016-04-13 17:09:52 +0000 | [diff] [blame] | 2385 | ] |
| 2386 | self.assertEqual(0, git_cl.main(['set-commit'])) |
| 2387 | |
Andrii Shyshkalov | 828701b | 2016-12-09 10:46:47 +0100 | [diff] [blame] | 2388 | def _cmd_set_commit_gerrit_common(self, vote, notify=None): |
tandrii@chromium.org | fa330e8 | 2016-04-13 17:09:52 +0000 | [diff] [blame] | 2389 | self.mock(git_cl.gerrit_util, 'SetReview', |
Andrii Shyshkalov | 828701b | 2016-12-09 10:46:47 +0100 | [diff] [blame] | 2390 | lambda h, i, labels, notify=None: |
| 2391 | self._mocked_call(['SetReview', h, i, labels, notify])) |
| 2392 | |
tandrii@chromium.org | fa330e8 | 2016-04-13 17:09:52 +0000 | [diff] [blame] | 2393 | self.calls = [ |
| 2394 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 2395 | ((['git', 'config', 'branch.feature.rietveldissue'],), CERR1), |
| 2396 | ((['git', 'config', 'branch.feature.gerritissue'],), '123'), |
tandrii@chromium.org | fa330e8 | 2016-04-13 17:09:52 +0000 | [diff] [blame] | 2397 | ((['git', 'config', 'branch.feature.gerritserver'],), |
| 2398 | 'https://chromium-review.googlesource.com'), |
| 2399 | ((['SetReview', 'chromium-review.googlesource.com', 123, |
Andrii Shyshkalov | 828701b | 2016-12-09 10:46:47 +0100 | [diff] [blame] | 2400 | {'Commit-Queue': vote}, notify],), ''), |
tandrii@chromium.org | fa330e8 | 2016-04-13 17:09:52 +0000 | [diff] [blame] | 2401 | ] |
tandrii | d9e5ce5 | 2016-07-13 02:32:59 -0700 | [diff] [blame] | 2402 | |
| 2403 | def test_cmd_set_commit_gerrit_clear(self): |
| 2404 | self._cmd_set_commit_gerrit_common(0) |
| 2405 | self.assertEqual(0, git_cl.main(['set-commit', '-c'])) |
| 2406 | |
| 2407 | def test_cmd_set_commit_gerrit_dry(self): |
Aaron Gable | 75e7872 | 2017-06-09 10:40:16 -0700 | [diff] [blame] | 2408 | self._cmd_set_commit_gerrit_common(1, notify=False) |
tandrii@chromium.org | fa330e8 | 2016-04-13 17:09:52 +0000 | [diff] [blame] | 2409 | self.assertEqual(0, git_cl.main(['set-commit', '-d'])) |
| 2410 | |
tandrii | d9e5ce5 | 2016-07-13 02:32:59 -0700 | [diff] [blame] | 2411 | def test_cmd_set_commit_gerrit(self): |
| 2412 | self._cmd_set_commit_gerrit_common(2) |
| 2413 | self.assertEqual(0, git_cl.main(['set-commit'])) |
| 2414 | |
martiniss@chromium.org | 2b55fe3 | 2016-04-26 20:28:54 +0000 | [diff] [blame] | 2415 | def test_description_display(self): |
| 2416 | out = StringIO.StringIO() |
| 2417 | self.mock(git_cl.sys, 'stdout', out) |
| 2418 | |
martiniss@chromium.org | d6648e2 | 2016-04-29 19:22:16 +0000 | [diff] [blame] | 2419 | self.mock(git_cl, 'Changelist', ChangelistMock) |
| 2420 | ChangelistMock.desc = 'foo\n' |
martiniss@chromium.org | 2b55fe3 | 2016-04-26 20:28:54 +0000 | [diff] [blame] | 2421 | |
| 2422 | self.assertEqual(0, git_cl.main(['description', '-d'])) |
| 2423 | self.assertEqual('foo\n', out.getvalue()) |
| 2424 | |
| 2425 | def test_description_rietveld(self): |
| 2426 | out = StringIO.StringIO() |
| 2427 | self.mock(git_cl.sys, 'stdout', out) |
martiniss | 6eda05f | 2016-06-30 10:18:35 -0700 | [diff] [blame] | 2428 | self.mock(git_cl.Changelist, 'GetDescription', lambda *args: 'foobar') |
martiniss@chromium.org | 2b55fe3 | 2016-04-26 20:28:54 +0000 | [diff] [blame] | 2429 | |
martiniss@chromium.org | 2b55fe3 | 2016-04-26 20:28:54 +0000 | [diff] [blame] | 2430 | self.assertEqual(0, git_cl.main([ |
Andrii Shyshkalov | 28d840e | 2017-04-10 15:45:09 +0200 | [diff] [blame] | 2431 | 'description', '-d', '--rietveld', 'https://code.review.org/123123'])) |
martiniss@chromium.org | 2b55fe3 | 2016-04-26 20:28:54 +0000 | [diff] [blame] | 2432 | self.assertEqual('foobar\n', out.getvalue()) |
| 2433 | |
iannucci | 3c972b9 | 2016-08-17 13:24:10 -0700 | [diff] [blame] | 2434 | def test_StatusFieldOverrideIssueMissingArgs(self): |
| 2435 | out = StringIO.StringIO() |
| 2436 | self.mock(git_cl.sys, 'stderr', out) |
| 2437 | |
| 2438 | try: |
| 2439 | self.assertEqual(git_cl.main(['status', '--issue', '1']), 0) |
| 2440 | except SystemExit as ex: |
| 2441 | self.assertEqual(ex.code, 2) |
iannucci | e53c935 | 2016-08-17 14:40:40 -0700 | [diff] [blame] | 2442 | self.assertRegexpMatches(out.getvalue(), r'--issue must be specified') |
iannucci | 3c972b9 | 2016-08-17 13:24:10 -0700 | [diff] [blame] | 2443 | |
| 2444 | out = StringIO.StringIO() |
| 2445 | self.mock(git_cl.sys, 'stderr', out) |
| 2446 | |
| 2447 | try: |
| 2448 | self.assertEqual(git_cl.main(['status', '--issue', '1', '--rietveld']), 0) |
| 2449 | except SystemExit as ex: |
| 2450 | self.assertEqual(ex.code, 2) |
iannucci | e53c935 | 2016-08-17 14:40:40 -0700 | [diff] [blame] | 2451 | self.assertRegexpMatches(out.getvalue(), r'--field must be specified') |
iannucci | 3c972b9 | 2016-08-17 13:24:10 -0700 | [diff] [blame] | 2452 | |
| 2453 | def test_StatusFieldOverrideIssue(self): |
| 2454 | out = StringIO.StringIO() |
| 2455 | self.mock(git_cl.sys, 'stdout', out) |
| 2456 | |
| 2457 | def assertIssue(cl_self, *_args): |
| 2458 | self.assertEquals(cl_self.issue, 1) |
| 2459 | return 'foobar' |
| 2460 | |
| 2461 | self.mock(git_cl.Changelist, 'GetDescription', assertIssue) |
| 2462 | self.calls = [ |
| 2463 | ((['git', 'config', 'rietveld.autoupdate'],), ''), |
| 2464 | ((['git', 'config', 'rietveld.server'],), ''), |
| 2465 | ((['git', 'config', 'rietveld.server'],), ''), |
| 2466 | ] |
iannucci | e53c935 | 2016-08-17 14:40:40 -0700 | [diff] [blame] | 2467 | self.assertEqual( |
| 2468 | git_cl.main(['status', '--issue', '1', '--rietveld', '--field', 'desc']), |
| 2469 | 0) |
iannucci | 3c972b9 | 2016-08-17 13:24:10 -0700 | [diff] [blame] | 2470 | self.assertEqual(out.getvalue(), 'foobar\n') |
| 2471 | |
iannucci | e53c935 | 2016-08-17 14:40:40 -0700 | [diff] [blame] | 2472 | def test_SetCloseOverrideIssue(self): |
| 2473 | def assertIssue(cl_self, *_args): |
| 2474 | self.assertEquals(cl_self.issue, 1) |
| 2475 | return 'foobar' |
| 2476 | |
| 2477 | self.mock(git_cl.Changelist, 'GetDescription', assertIssue) |
| 2478 | self.mock(git_cl.Changelist, 'CloseIssue', lambda *_: None) |
| 2479 | self.calls = [ |
| 2480 | ((['git', 'config', 'rietveld.autoupdate'],), ''), |
| 2481 | ((['git', 'config', 'rietveld.server'],), ''), |
| 2482 | ((['git', 'config', 'rietveld.server'],), ''), |
| 2483 | ] |
| 2484 | self.assertEqual( |
| 2485 | git_cl.main(['set-close', '--issue', '1', '--rietveld']), 0) |
| 2486 | |
| 2487 | def test_SetCommitOverrideIssue(self): |
| 2488 | def assertIssue(cl_self, *_args): |
| 2489 | self.assertEquals(cl_self.issue, 1) |
| 2490 | return 'foobar' |
| 2491 | |
| 2492 | self.mock(git_cl.Changelist, 'GetDescription', assertIssue) |
| 2493 | self.mock(git_cl.Changelist, 'SetCQState', lambda *_: None) |
| 2494 | self.calls = [ |
| 2495 | ((['git', 'config', 'rietveld.autoupdate'],), ''), |
| 2496 | ((['git', 'config', 'rietveld.server'],), ''), |
| 2497 | ((['git', 'config', 'rietveld.server'],), ''), |
| 2498 | ((['git', 'symbolic-ref', 'HEAD'],), ''), |
| 2499 | ((['git', 'config', 'rietveld.server'],), ''), |
| 2500 | ((['git', 'config', 'rietveld.server'],), ''), |
| 2501 | ] |
| 2502 | self.assertEqual( |
| 2503 | git_cl.main(['set-close', '--issue', '1', '--rietveld']), 0) |
| 2504 | |
martiniss@chromium.org | 2b55fe3 | 2016-04-26 20:28:54 +0000 | [diff] [blame] | 2505 | def test_description_gerrit(self): |
| 2506 | out = StringIO.StringIO() |
| 2507 | self.mock(git_cl.sys, 'stdout', out) |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 2508 | self.calls = [ |
Andrii Shyshkalov | 8039be7 | 2017-01-26 09:38:18 +0100 | [diff] [blame] | 2509 | (('GetChangeDetail', 'code.review.org', |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 2510 | '123123', ['CURRENT_REVISION', 'CURRENT_COMMIT']), |
| 2511 | { |
| 2512 | 'current_revision': 'sha1', |
| 2513 | 'revisions': {'sha1': { |
| 2514 | 'commit': {'message': 'foobar'}, |
| 2515 | }}, |
| 2516 | }), |
| 2517 | ] |
martiniss@chromium.org | 2b55fe3 | 2016-04-26 20:28:54 +0000 | [diff] [blame] | 2518 | self.assertEqual(0, git_cl.main([ |
| 2519 | 'description', 'https://code.review.org/123123', '-d', '--gerrit'])) |
| 2520 | self.assertEqual('foobar\n', out.getvalue()) |
| 2521 | |
martiniss@chromium.org | d6648e2 | 2016-04-29 19:22:16 +0000 | [diff] [blame] | 2522 | def test_description_set_raw(self): |
| 2523 | out = StringIO.StringIO() |
| 2524 | self.mock(git_cl.sys, 'stdout', out) |
| 2525 | |
| 2526 | self.mock(git_cl, 'Changelist', ChangelistMock) |
| 2527 | self.mock(git_cl.sys, 'stdin', StringIO.StringIO('hihi')) |
| 2528 | |
| 2529 | self.assertEqual(0, git_cl.main(['description', '-n', 'hihi'])) |
| 2530 | self.assertEqual('hihi', ChangelistMock.desc) |
| 2531 | |
tandrii@chromium.org | d605a51 | 2016-06-03 09:55:00 +0000 | [diff] [blame] | 2532 | def test_description_appends_bug_line(self): |
tandrii@chromium.org | 601e1d1 | 2016-06-03 13:03:54 +0000 | [diff] [blame] | 2533 | current_desc = 'Some.\n\nChange-Id: xxx' |
tandrii@chromium.org | d605a51 | 2016-06-03 09:55:00 +0000 | [diff] [blame] | 2534 | |
| 2535 | def RunEditor(desc, _, **kwargs): |
| 2536 | self.assertEquals( |
| 2537 | '# Enter a description of the change.\n' |
| 2538 | '# This will be displayed on the codereview site.\n' |
| 2539 | '# The first line will also be used as the subject of the review.\n' |
| 2540 | '#--------------------This line is 72 characters long' |
tandrii@chromium.org | 601e1d1 | 2016-06-03 13:03:54 +0000 | [diff] [blame] | 2541 | '--------------------\n' |
Aaron Gable | 3a16ed1 | 2017-03-23 10:51:55 -0700 | [diff] [blame] | 2542 | 'Some.\n\nChange-Id: xxx\nBug: ', |
tandrii@chromium.org | d605a51 | 2016-06-03 09:55:00 +0000 | [diff] [blame] | 2543 | desc) |
tandrii@chromium.org | 601e1d1 | 2016-06-03 13:03:54 +0000 | [diff] [blame] | 2544 | # Simulate user changing something. |
Aaron Gable | 3a16ed1 | 2017-03-23 10:51:55 -0700 | [diff] [blame] | 2545 | return 'Some.\n\nChange-Id: xxx\nBug: 123' |
tandrii@chromium.org | d605a51 | 2016-06-03 09:55:00 +0000 | [diff] [blame] | 2546 | |
dsansome | e2d6fd9 | 2016-09-08 00:10:47 -0700 | [diff] [blame] | 2547 | def UpdateDescriptionRemote(_, desc, force=False): |
Aaron Gable | 3a16ed1 | 2017-03-23 10:51:55 -0700 | [diff] [blame] | 2548 | self.assertEquals(desc, 'Some.\n\nChange-Id: xxx\nBug: 123') |
tandrii@chromium.org | d605a51 | 2016-06-03 09:55:00 +0000 | [diff] [blame] | 2549 | |
| 2550 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
| 2551 | self.mock(git_cl.Changelist, 'GetDescription', |
| 2552 | lambda *args: current_desc) |
| 2553 | self.mock(git_cl._GerritChangelistImpl, 'UpdateDescriptionRemote', |
| 2554 | UpdateDescriptionRemote) |
| 2555 | self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor) |
| 2556 | |
| 2557 | self.calls = [ |
| 2558 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 2559 | ((['git', 'config', 'branch.feature.gerritissue'],), '123'), |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 2560 | ((['git', 'config', 'rietveld.autoupdate'],), CERR1), |
| 2561 | ((['git', 'config', 'rietveld.bug-prefix'],), CERR1), |
tandrii@chromium.org | d605a51 | 2016-06-03 09:55:00 +0000 | [diff] [blame] | 2562 | ((['git', 'config', 'core.editor'],), 'vi'), |
| 2563 | ] |
| 2564 | self.assertEqual(0, git_cl.main(['description', '--gerrit'])) |
| 2565 | |
martiniss@chromium.org | d6648e2 | 2016-04-29 19:22:16 +0000 | [diff] [blame] | 2566 | def test_description_set_stdin(self): |
| 2567 | out = StringIO.StringIO() |
| 2568 | self.mock(git_cl.sys, 'stdout', out) |
| 2569 | |
| 2570 | self.mock(git_cl, 'Changelist', ChangelistMock) |
| 2571 | self.mock(git_cl.sys, 'stdin', StringIO.StringIO('hi \r\n\t there\n\nman')) |
| 2572 | |
| 2573 | self.assertEqual(0, git_cl.main(['description', '-n', '-'])) |
| 2574 | self.assertEqual('hi\n\t there\n\nman', ChangelistMock.desc) |
| 2575 | |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 2576 | def test_archive(self): |
tandrii | 1c67da6 | 2016-06-10 07:35:53 -0700 | [diff] [blame] | 2577 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
| 2578 | |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 2579 | self.calls = \ |
| 2580 | [((['git', 'for-each-ref', '--format=%(refname)', 'refs/heads'],), |
| 2581 | 'refs/heads/master\nrefs/heads/foo\nrefs/heads/bar'), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 2582 | ((['git', 'config', 'branch.master.rietveldissue'],), '1'), |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 2583 | ((['git', 'config', 'rietveld.autoupdate'],), CERR1), |
| 2584 | ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 2585 | ((['git', 'config', 'branch.foo.rietveldissue'],), '456'), |
| 2586 | ((['git', 'config', 'branch.bar.rietveldissue'],), CERR1), |
| 2587 | ((['git', 'config', 'branch.bar.gerritissue'],), '789'), |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 2588 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
| 2589 | ((['git', 'tag', 'git-cl-archived-456-foo', 'foo'],), ''), |
| 2590 | ((['git', 'branch', '-D', 'foo'],), '')] |
| 2591 | |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 2592 | self.mock(git_cl, 'get_cl_statuses', |
| 2593 | lambda branches, fine_grained, max_processes: |
kmarshall | 9249e01 | 2016-08-23 12:02:16 -0700 | [diff] [blame] | 2594 | [(MockChangelistWithBranchAndIssue('master', 1), 'open'), |
| 2595 | (MockChangelistWithBranchAndIssue('foo', 456), 'closed'), |
| 2596 | (MockChangelistWithBranchAndIssue('bar', 789), 'open')]) |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 2597 | |
| 2598 | self.assertEqual(0, git_cl.main(['archive', '-f'])) |
| 2599 | |
| 2600 | def test_archive_current_branch_fails(self): |
tandrii | 1c67da6 | 2016-06-10 07:35:53 -0700 | [diff] [blame] | 2601 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 2602 | self.calls = \ |
| 2603 | [((['git', 'for-each-ref', '--format=%(refname)', 'refs/heads'],), |
| 2604 | 'refs/heads/master'), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 2605 | ((['git', 'config', 'branch.master.rietveldissue'],), '1'), |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 2606 | ((['git', 'config', 'rietveld.autoupdate'],), CERR1), |
| 2607 | ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'), |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 2608 | ((['git', 'symbolic-ref', 'HEAD'],), 'master')] |
| 2609 | |
kmarshall | 9249e01 | 2016-08-23 12:02:16 -0700 | [diff] [blame] | 2610 | self.mock(git_cl, 'get_cl_statuses', |
| 2611 | lambda branches, fine_grained, max_processes: |
| 2612 | [(MockChangelistWithBranchAndIssue('master', 1), 'closed')]) |
| 2613 | |
| 2614 | self.assertEqual(1, git_cl.main(['archive', '-f'])) |
| 2615 | |
| 2616 | def test_archive_dry_run(self): |
| 2617 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
| 2618 | |
| 2619 | self.calls = \ |
| 2620 | [((['git', 'for-each-ref', '--format=%(refname)', 'refs/heads'],), |
| 2621 | 'refs/heads/master\nrefs/heads/foo\nrefs/heads/bar'), |
| 2622 | ((['git', 'config', 'branch.master.rietveldissue'],), '1'), |
| 2623 | ((['git', 'config', 'rietveld.autoupdate'],), CERR1), |
| 2624 | ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'), |
| 2625 | ((['git', 'config', 'branch.foo.rietveldissue'],), '456'), |
| 2626 | ((['git', 'config', 'branch.bar.rietveldissue'],), CERR1), |
| 2627 | ((['git', 'config', 'branch.bar.gerritissue'],), '789'), |
| 2628 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'),] |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 2629 | |
| 2630 | self.mock(git_cl, 'get_cl_statuses', |
| 2631 | lambda branches, fine_grained, max_processes: |
kmarshall | 9249e01 | 2016-08-23 12:02:16 -0700 | [diff] [blame] | 2632 | [(MockChangelistWithBranchAndIssue('master', 1), 'open'), |
| 2633 | (MockChangelistWithBranchAndIssue('foo', 456), 'closed'), |
| 2634 | (MockChangelistWithBranchAndIssue('bar', 789), 'open')]) |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 2635 | |
kmarshall | 9249e01 | 2016-08-23 12:02:16 -0700 | [diff] [blame] | 2636 | self.assertEqual(0, git_cl.main(['archive', '-f', '--dry-run'])) |
| 2637 | |
| 2638 | def test_archive_no_tags(self): |
| 2639 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
| 2640 | |
| 2641 | self.calls = \ |
| 2642 | [((['git', 'for-each-ref', '--format=%(refname)', 'refs/heads'],), |
| 2643 | 'refs/heads/master\nrefs/heads/foo\nrefs/heads/bar'), |
| 2644 | ((['git', 'config', 'branch.master.rietveldissue'],), '1'), |
| 2645 | ((['git', 'config', 'rietveld.autoupdate'],), CERR1), |
| 2646 | ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'), |
| 2647 | ((['git', 'config', 'branch.foo.rietveldissue'],), '456'), |
| 2648 | ((['git', 'config', 'branch.bar.rietveldissue'],), CERR1), |
| 2649 | ((['git', 'config', 'branch.bar.gerritissue'],), '789'), |
| 2650 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
| 2651 | ((['git', 'branch', '-D', 'foo'],), '')] |
| 2652 | |
| 2653 | self.mock(git_cl, 'get_cl_statuses', |
| 2654 | lambda branches, fine_grained, max_processes: |
| 2655 | [(MockChangelistWithBranchAndIssue('master', 1), 'open'), |
| 2656 | (MockChangelistWithBranchAndIssue('foo', 456), 'closed'), |
| 2657 | (MockChangelistWithBranchAndIssue('bar', 789), 'open')]) |
| 2658 | |
| 2659 | self.assertEqual(0, git_cl.main(['archive', '-f', '--notags'])) |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 2660 | |
tandrii@chromium.org | 9b7fd71 | 2016-06-01 13:45:20 +0000 | [diff] [blame] | 2661 | def test_cmd_issue_erase_existing(self): |
| 2662 | out = StringIO.StringIO() |
| 2663 | self.mock(git_cl.sys, 'stdout', out) |
| 2664 | self.calls = [ |
| 2665 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 2666 | ((['git', 'config', 'branch.feature.rietveldissue'],), CERR1), |
| 2667 | ((['git', 'config', 'branch.feature.gerritissue'],), '123'), |
tandrii@chromium.org | 9b7fd71 | 2016-06-01 13:45:20 +0000 | [diff] [blame] | 2668 | # Let this command raise exception (retcode=1) - it should be ignored. |
| 2669 | ((['git', 'config', '--unset', 'branch.feature.last-upload-hash'],), |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 2670 | CERR1), |
| 2671 | ((['git', 'config', '--unset', 'branch.feature.gerritissue'],), ''), |
| 2672 | ((['git', 'config', '--unset', 'branch.feature.gerritpatchset'],), ''), |
tandrii@chromium.org | 9b7fd71 | 2016-06-01 13:45:20 +0000 | [diff] [blame] | 2673 | ((['git', 'config', '--unset', 'branch.feature.gerritserver'],), ''), |
| 2674 | ((['git', 'config', '--unset', 'branch.feature.gerritsquashhash'],), |
| 2675 | ''), |
Aaron Gable | ca01e2c | 2017-07-19 11:16:02 -0700 | [diff] [blame] | 2676 | ((['git', 'log', '-1', '--format=%B'],), 'This is a description'), |
tandrii@chromium.org | 9b7fd71 | 2016-06-01 13:45:20 +0000 | [diff] [blame] | 2677 | ] |
| 2678 | self.assertEqual(0, git_cl.main(['issue', '0'])) |
| 2679 | |
Aaron Gable | 400e989 | 2017-07-12 15:31:21 -0700 | [diff] [blame] | 2680 | def test_cmd_issue_erase_existing_with_change_id(self): |
| 2681 | out = StringIO.StringIO() |
| 2682 | self.mock(git_cl.sys, 'stdout', out) |
| 2683 | self.mock(git_cl.Changelist, 'GetDescription', |
| 2684 | lambda _: 'This is a description\n\nChange-Id: Ideadbeef') |
| 2685 | self.calls = [ |
| 2686 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
| 2687 | ((['git', 'config', 'branch.feature.rietveldissue'],), CERR1), |
| 2688 | ((['git', 'config', 'branch.feature.gerritissue'],), '123'), |
Aaron Gable | 400e989 | 2017-07-12 15:31:21 -0700 | [diff] [blame] | 2689 | # Let this command raise exception (retcode=1) - it should be ignored. |
| 2690 | ((['git', 'config', '--unset', 'branch.feature.last-upload-hash'],), |
| 2691 | CERR1), |
| 2692 | ((['git', 'config', '--unset', 'branch.feature.gerritissue'],), ''), |
| 2693 | ((['git', 'config', '--unset', 'branch.feature.gerritpatchset'],), ''), |
| 2694 | ((['git', 'config', '--unset', 'branch.feature.gerritserver'],), ''), |
| 2695 | ((['git', 'config', '--unset', 'branch.feature.gerritsquashhash'],), |
| 2696 | ''), |
Aaron Gable | ca01e2c | 2017-07-19 11:16:02 -0700 | [diff] [blame] | 2697 | ((['git', 'log', '-1', '--format=%B'],), |
| 2698 | 'This is a description\n\nChange-Id: Ideadbeef'), |
| 2699 | ((['git', 'commit', '--amend', '-m', 'This is a description\n'],), ''), |
Aaron Gable | 400e989 | 2017-07-12 15:31:21 -0700 | [diff] [blame] | 2700 | ] |
| 2701 | self.assertEqual(0, git_cl.main(['issue', '0'])) |
| 2702 | |
phajdan.jr | e328cf9 | 2016-08-22 04:12:17 -0700 | [diff] [blame] | 2703 | def test_cmd_issue_json(self): |
| 2704 | out = StringIO.StringIO() |
| 2705 | self.mock(git_cl.sys, 'stdout', out) |
| 2706 | self.calls = [ |
| 2707 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 2708 | ((['git', 'config', 'branch.feature.rietveldissue'],), '123'), |
phajdan.jr | e328cf9 | 2016-08-22 04:12:17 -0700 | [diff] [blame] | 2709 | ((['git', 'config', 'rietveld.autoupdate'],), ''), |
| 2710 | ((['git', 'config', 'rietveld.server'],), |
| 2711 | 'https://codereview.chromium.org'), |
| 2712 | ((['git', 'config', 'branch.feature.rietveldserver'],), ''), |
| 2713 | (('write_json', 'output.json', |
| 2714 | {'issue': 123, 'issue_url': 'https://codereview.chromium.org/123'}), |
| 2715 | ''), |
| 2716 | ] |
| 2717 | self.assertEqual(0, git_cl.main(['issue', '--json', 'output.json'])) |
| 2718 | |
tandrii | de281ae | 2016-10-12 06:02:30 -0700 | [diff] [blame] | 2719 | def test_git_cl_try_default_cq_dry_run(self): |
tandrii | 9de9ec6 | 2016-07-13 03:01:59 -0700 | [diff] [blame] | 2720 | self.mock(git_cl.Changelist, 'GetChange', |
| 2721 | lambda _, *a: ( |
| 2722 | self._mocked_call(['GetChange']+list(a)))) |
| 2723 | self.mock(git_cl.presubmit_support, 'DoGetTryMasters', |
| 2724 | lambda *_, **__: ( |
| 2725 | self._mocked_call(['DoGetTryMasters']))) |
tandrii | 9de9ec6 | 2016-07-13 03:01:59 -0700 | [diff] [blame] | 2726 | self.mock(git_cl._RietveldChangelistImpl, 'SetCQState', |
| 2727 | lambda _, s: self._mocked_call(['SetCQState', s])) |
| 2728 | self.calls = [ |
| 2729 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 2730 | ((['git', 'config', 'branch.feature.rietveldissue'],), '123'), |
tandrii | 9de9ec6 | 2016-07-13 03:01:59 -0700 | [diff] [blame] | 2731 | ((['git', 'config', 'rietveld.autoupdate'],), ''), |
| 2732 | ((['git', 'config', 'rietveld.server'],), |
| 2733 | 'https://codereview.chromium.org'), |
| 2734 | ((['git', 'config', 'branch.feature.rietveldserver'],), ''), |
| 2735 | ((['git', 'config', 'branch.feature.merge'],), 'feature'), |
| 2736 | ((['git', 'config', 'branch.feature.remote'],), 'origin'), |
| 2737 | ((['get_or_create_merge_base', 'feature', 'feature'],), |
| 2738 | 'fake_ancestor_sha'), |
| 2739 | ((['GetChange', 'fake_ancestor_sha', None], ), |
| 2740 | git_cl.presubmit_support.GitChange( |
| 2741 | '', '', '', '', '', '', '', '')), |
| 2742 | ((['git', 'rev-parse', '--show-cdup'],), '../'), |
| 2743 | ((['DoGetTryMasters'], ), None), |
tandrii | 9de9ec6 | 2016-07-13 03:01:59 -0700 | [diff] [blame] | 2744 | ((['SetCQState', git_cl._CQState.DRY_RUN], ), None), |
| 2745 | ] |
| 2746 | out = StringIO.StringIO() |
| 2747 | self.mock(git_cl.sys, 'stdout', out) |
| 2748 | self.assertEqual(0, git_cl.main(['try'])) |
| 2749 | self.assertEqual( |
| 2750 | out.getvalue(), |
Quinten Yearsley | fc5fd92 | 2017-05-31 11:50:52 -0700 | [diff] [blame] | 2751 | 'Scheduling CQ dry run on: https://codereview.chromium.org/123\n') |
tandrii | 9de9ec6 | 2016-07-13 03:01:59 -0700 | [diff] [blame] | 2752 | |
tandrii | 8c5a353 | 2016-11-04 07:52:02 -0700 | [diff] [blame] | 2753 | def test_git_cl_try_default_cq_dry_run_gerrit(self): |
| 2754 | self.mock(git_cl.Changelist, 'GetChange', |
| 2755 | lambda _, *a: ( |
| 2756 | self._mocked_call(['GetChange']+list(a)))) |
| 2757 | self.mock(git_cl.presubmit_support, 'DoGetTryMasters', |
| 2758 | lambda *_, **__: ( |
| 2759 | self._mocked_call(['DoGetTryMasters']))) |
| 2760 | self.mock(git_cl._GerritChangelistImpl, 'SetCQState', |
| 2761 | lambda _, s: self._mocked_call(['SetCQState', s])) |
| 2762 | |
tandrii | 8c5a353 | 2016-11-04 07:52:02 -0700 | [diff] [blame] | 2763 | self.calls = [ |
| 2764 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
| 2765 | ((['git', 'config', 'branch.feature.rietveldissue'],), CERR1), |
| 2766 | ((['git', 'config', 'branch.feature.gerritissue'],), '123456'), |
| 2767 | ((['git', 'config', 'branch.feature.gerritserver'],), |
| 2768 | 'https://chromium-review.googlesource.com'), |
Andrii Shyshkalov | eadad92 | 2017-01-26 09:38:30 +0100 | [diff] [blame] | 2769 | (('GetChangeDetail', 'chromium-review.googlesource.com', '123456', |
| 2770 | ['DETAILED_ACCOUNTS', 'ALL_REVISIONS', 'CURRENT_COMMIT']), { |
| 2771 | 'project': 'depot_tools', |
| 2772 | 'status': 'OPEN', |
| 2773 | 'owner': {'email': 'owner@e.mail'}, |
| 2774 | 'revisions': { |
| 2775 | 'deadbeaf': { |
| 2776 | '_number': 6, |
| 2777 | }, |
| 2778 | 'beeeeeef': { |
| 2779 | '_number': 7, |
| 2780 | 'fetch': {'http': { |
| 2781 | 'url': 'https://chromium.googlesource.com/depot_tools', |
| 2782 | 'ref': 'refs/changes/56/123456/7' |
| 2783 | }}, |
| 2784 | }, |
| 2785 | }, |
| 2786 | }), |
tandrii | 8c5a353 | 2016-11-04 07:52:02 -0700 | [diff] [blame] | 2787 | ((['git', 'config', 'branch.feature.merge'],), 'feature'), |
| 2788 | ((['git', 'config', 'branch.feature.remote'],), 'origin'), |
| 2789 | ((['get_or_create_merge_base', 'feature', 'feature'],), |
| 2790 | 'fake_ancestor_sha'), |
| 2791 | ((['GetChange', 'fake_ancestor_sha', None], ), |
| 2792 | git_cl.presubmit_support.GitChange( |
| 2793 | '', '', '', '', '', '', '', '')), |
| 2794 | ((['git', 'rev-parse', '--show-cdup'],), '../'), |
| 2795 | ((['DoGetTryMasters'], ), None), |
| 2796 | ((['SetCQState', git_cl._CQState.DRY_RUN], ), None), |
| 2797 | ] |
| 2798 | out = StringIO.StringIO() |
| 2799 | self.mock(git_cl.sys, 'stdout', out) |
| 2800 | self.assertEqual(0, git_cl.main(['try'])) |
| 2801 | self.assertEqual( |
| 2802 | out.getvalue(), |
Quinten Yearsley | fc5fd92 | 2017-05-31 11:50:52 -0700 | [diff] [blame] | 2803 | 'Scheduling CQ dry run on: ' |
tandrii | 8c5a353 | 2016-11-04 07:52:02 -0700 | [diff] [blame] | 2804 | 'https://chromium-review.googlesource.com/123456\n') |
| 2805 | |
| 2806 | def test_git_cl_try_buildbucket_with_properties_rietveld(self): |
| 2807 | self.mock(git_cl._RietveldChangelistImpl, 'GetIssueProperties', |
| 2808 | lambda _: { |
| 2809 | 'owner_email': 'owner@e.mail', |
| 2810 | 'private': False, |
| 2811 | 'closed': False, |
| 2812 | 'project': 'depot_tools', |
| 2813 | 'patchsets': [20001], |
| 2814 | }) |
tandrii | de281ae | 2016-10-12 06:02:30 -0700 | [diff] [blame] | 2815 | self.mock(git_cl.uuid, 'uuid4', lambda: 'uuid4') |
| 2816 | self.calls = [ |
| 2817 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
| 2818 | ((['git', 'config', 'branch.feature.rietveldissue'],), '123'), |
| 2819 | ((['git', 'config', 'rietveld.autoupdate'],), CERR1), |
| 2820 | ((['git', 'config', 'rietveld.server'],), |
| 2821 | 'https://codereview.chromium.org'), |
tandrii | de281ae | 2016-10-12 06:02:30 -0700 | [diff] [blame] | 2822 | ((['git', 'config', 'branch.feature.rietveldpatchset'],), '20001'), |
tandrii | 8c5a353 | 2016-11-04 07:52:02 -0700 | [diff] [blame] | 2823 | ((['git', 'config', 'branch.feature.rietveldserver'],), CERR1), |
tandrii | de281ae | 2016-10-12 06:02:30 -0700 | [diff] [blame] | 2824 | ] |
| 2825 | |
| 2826 | def _buildbucket_retry(*_, **kw): |
| 2827 | # self.maxDiff = 10000 |
| 2828 | body = json.loads(kw['body']) |
| 2829 | self.assertEqual(len(body['builds']), 1) |
| 2830 | build = body['builds'][0] |
| 2831 | params = json.loads(build.pop('parameters_json')) |
| 2832 | self.assertEqual(params, { |
| 2833 | u'builder_name': u'win', |
| 2834 | u'changes': [{u'author': {u'email': u'owner@e.mail'}, |
| 2835 | u'revision': None}], |
| 2836 | u'properties': { |
| 2837 | u'category': u'git_cl_try', |
| 2838 | u'issue': 123, |
| 2839 | u'key': u'val', |
| 2840 | u'json': [{u'a': 1}, None], |
| 2841 | u'master': u'tryserver.chromium', |
| 2842 | u'patch_project': u'depot_tools', |
| 2843 | u'patch_storage': u'rietveld', |
| 2844 | u'patchset': 20001, |
tandrii | de281ae | 2016-10-12 06:02:30 -0700 | [diff] [blame] | 2845 | u'rietveld': u'https://codereview.chromium.org', |
| 2846 | } |
| 2847 | }) |
| 2848 | self.assertEqual(build, { |
| 2849 | u'bucket': u'master.tryserver.chromium', |
| 2850 | u'client_operation_id': u'uuid4', |
| 2851 | u'tags': [u'builder:win', |
| 2852 | u'buildset:patch/rietveld/codereview.chromium.org/123/20001', |
borenet | 6c0efe6 | 2016-10-19 08:13:29 -0700 | [diff] [blame] | 2853 | u'user_agent:git_cl_try', |
| 2854 | u'master:tryserver.chromium'], |
tandrii | de281ae | 2016-10-12 06:02:30 -0700 | [diff] [blame] | 2855 | }) |
| 2856 | |
| 2857 | self.mock(git_cl, '_buildbucket_retry', _buildbucket_retry) |
| 2858 | |
| 2859 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
| 2860 | self.assertEqual(0, git_cl.main([ |
| 2861 | 'try', '-m', 'tryserver.chromium', '-b', 'win', |
| 2862 | '-p', 'key=val', '-p', 'json=[{"a":1}, null]'])) |
| 2863 | self.assertRegexpMatches( |
| 2864 | git_cl.sys.stdout.getvalue(), |
borenet | 6c0efe6 | 2016-10-19 08:13:29 -0700 | [diff] [blame] | 2865 | 'Tried jobs on:\nBucket: master.tryserver.chromium') |
| 2866 | |
tandrii | 8c5a353 | 2016-11-04 07:52:02 -0700 | [diff] [blame] | 2867 | def test_git_cl_try_buildbucket_with_properties_gerrit(self): |
| 2868 | self.mock(git_cl.Changelist, 'GetMostRecentPatchset', lambda _: 7) |
| 2869 | self.mock(git_cl.uuid, 'uuid4', lambda: 'uuid4') |
| 2870 | |
tandrii | 8c5a353 | 2016-11-04 07:52:02 -0700 | [diff] [blame] | 2871 | self.calls = [ |
| 2872 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
| 2873 | ((['git', 'config', 'branch.feature.rietveldissue'],), CERR1), |
| 2874 | ((['git', 'config', 'branch.feature.gerritissue'],), '123456'), |
| 2875 | ((['git', 'config', 'branch.feature.gerritserver'],), |
| 2876 | 'https://chromium-review.googlesource.com'), |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 2877 | (('GetChangeDetail', 'chromium-review.googlesource.com', '123456', |
Andrii Shyshkalov | eadad92 | 2017-01-26 09:38:30 +0100 | [diff] [blame] | 2878 | ['DETAILED_ACCOUNTS', 'ALL_REVISIONS', 'CURRENT_COMMIT']), { |
tandrii | 8c5a353 | 2016-11-04 07:52:02 -0700 | [diff] [blame] | 2879 | 'project': 'depot_tools', |
Andrii Shyshkalov | eadad92 | 2017-01-26 09:38:30 +0100 | [diff] [blame] | 2880 | 'status': 'OPEN', |
| 2881 | 'owner': {'email': 'owner@e.mail'}, |
tandrii | 8c5a353 | 2016-11-04 07:52:02 -0700 | [diff] [blame] | 2882 | 'revisions': { |
| 2883 | 'deadbeaf': { |
| 2884 | '_number': 6, |
| 2885 | }, |
| 2886 | 'beeeeeef': { |
| 2887 | '_number': 7, |
| 2888 | 'fetch': {'http': { |
| 2889 | 'url': 'https://chromium.googlesource.com/depot_tools', |
| 2890 | 'ref': 'refs/changes/56/123456/7' |
| 2891 | }}, |
| 2892 | }, |
| 2893 | }, |
| 2894 | }), |
| 2895 | ] |
| 2896 | |
| 2897 | def _buildbucket_retry(*_, **kw): |
| 2898 | # self.maxDiff = 10000 |
| 2899 | body = json.loads(kw['body']) |
| 2900 | self.assertEqual(len(body['builds']), 1) |
| 2901 | build = body['builds'][0] |
| 2902 | params = json.loads(build.pop('parameters_json')) |
| 2903 | self.assertEqual(params, { |
| 2904 | u'builder_name': u'win', |
| 2905 | u'changes': [{u'author': {u'email': u'owner@e.mail'}, |
| 2906 | u'revision': None}], |
| 2907 | u'properties': { |
| 2908 | u'category': u'git_cl_try', |
| 2909 | u'key': u'val', |
| 2910 | u'json': [{u'a': 1}, None], |
| 2911 | u'master': u'tryserver.chromium', |
| 2912 | |
| 2913 | u'patch_gerrit_url': |
| 2914 | u'https://chromium-review.googlesource.com', |
| 2915 | u'patch_issue': 123456, |
| 2916 | u'patch_project': u'depot_tools', |
| 2917 | u'patch_ref': u'refs/changes/56/123456/7', |
| 2918 | u'patch_repository_url': |
| 2919 | u'https://chromium.googlesource.com/depot_tools', |
| 2920 | u'patch_set': 7, |
| 2921 | u'patch_storage': u'gerrit', |
| 2922 | } |
| 2923 | }) |
| 2924 | self.assertEqual(build, { |
| 2925 | u'bucket': u'master.tryserver.chromium', |
| 2926 | u'client_operation_id': u'uuid4', |
| 2927 | u'tags': [ |
| 2928 | u'builder:win', |
| 2929 | u'buildset:patch/gerrit/chromium-review.googlesource.com/123456/7', |
| 2930 | u'user_agent:git_cl_try', |
| 2931 | u'master:tryserver.chromium'], |
| 2932 | }) |
| 2933 | |
| 2934 | self.mock(git_cl, '_buildbucket_retry', _buildbucket_retry) |
| 2935 | |
| 2936 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
| 2937 | self.assertEqual(0, git_cl.main([ |
| 2938 | 'try', '-m', 'tryserver.chromium', '-b', 'win', |
| 2939 | '-p', 'key=val', '-p', 'json=[{"a":1}, null]'])) |
| 2940 | self.assertRegexpMatches( |
| 2941 | git_cl.sys.stdout.getvalue(), |
| 2942 | 'Tried jobs on:\nBucket: master.tryserver.chromium') |
| 2943 | |
borenet | 6c0efe6 | 2016-10-19 08:13:29 -0700 | [diff] [blame] | 2944 | def test_git_cl_try_buildbucket_bucket_flag(self): |
tandrii | 8c5a353 | 2016-11-04 07:52:02 -0700 | [diff] [blame] | 2945 | self.mock(git_cl._RietveldChangelistImpl, 'GetIssueProperties', |
| 2946 | lambda _: { |
| 2947 | 'owner_email': 'owner@e.mail', |
| 2948 | 'private': False, |
| 2949 | 'closed': False, |
| 2950 | 'project': 'depot_tools', |
| 2951 | 'patchsets': [20001], |
| 2952 | }) |
borenet | 6c0efe6 | 2016-10-19 08:13:29 -0700 | [diff] [blame] | 2953 | self.mock(git_cl.uuid, 'uuid4', lambda: 'uuid4') |
| 2954 | self.calls = [ |
| 2955 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
| 2956 | ((['git', 'config', 'branch.feature.rietveldissue'],), '123'), |
| 2957 | ((['git', 'config', 'rietveld.autoupdate'],), CERR1), |
| 2958 | ((['git', 'config', 'rietveld.server'],), |
| 2959 | 'https://codereview.chromium.org'), |
borenet | 6c0efe6 | 2016-10-19 08:13:29 -0700 | [diff] [blame] | 2960 | ((['git', 'config', 'branch.feature.rietveldpatchset'],), '20001'), |
tandrii | 8c5a353 | 2016-11-04 07:52:02 -0700 | [diff] [blame] | 2961 | ((['git', 'config', 'branch.feature.rietveldserver'],), CERR1), |
borenet | 6c0efe6 | 2016-10-19 08:13:29 -0700 | [diff] [blame] | 2962 | ] |
| 2963 | |
| 2964 | def _buildbucket_retry(*_, **kw): |
borenet | 6c0efe6 | 2016-10-19 08:13:29 -0700 | [diff] [blame] | 2965 | body = json.loads(kw['body']) |
| 2966 | self.assertEqual(len(body['builds']), 1) |
| 2967 | build = body['builds'][0] |
| 2968 | params = json.loads(build.pop('parameters_json')) |
| 2969 | self.assertEqual(params, { |
| 2970 | u'builder_name': u'win', |
| 2971 | u'changes': [{u'author': {u'email': u'owner@e.mail'}, |
| 2972 | u'revision': None}], |
| 2973 | u'properties': { |
| 2974 | u'category': u'git_cl_try', |
| 2975 | u'issue': 123, |
| 2976 | u'patch_project': u'depot_tools', |
| 2977 | u'patch_storage': u'rietveld', |
| 2978 | u'patchset': 20001, |
borenet | 6c0efe6 | 2016-10-19 08:13:29 -0700 | [diff] [blame] | 2979 | u'rietveld': u'https://codereview.chromium.org', |
| 2980 | } |
| 2981 | }) |
| 2982 | self.assertEqual(build, { |
| 2983 | u'bucket': u'test.bucket', |
| 2984 | u'client_operation_id': u'uuid4', |
| 2985 | u'tags': [u'builder:win', |
| 2986 | u'buildset:patch/rietveld/codereview.chromium.org/123/20001', |
| 2987 | u'user_agent:git_cl_try'], |
| 2988 | }) |
| 2989 | |
| 2990 | self.mock(git_cl, '_buildbucket_retry', _buildbucket_retry) |
| 2991 | |
| 2992 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
| 2993 | self.assertEqual(0, git_cl.main([ |
| 2994 | 'try', '-B', 'test.bucket', '-b', 'win'])) |
| 2995 | self.assertRegexpMatches( |
| 2996 | git_cl.sys.stdout.getvalue(), |
| 2997 | 'Tried jobs on:\nBucket: test.bucket') |
tandrii | de281ae | 2016-10-12 06:02:30 -0700 | [diff] [blame] | 2998 | |
qyearsley | 123a468 | 2016-10-26 09:12:17 -0700 | [diff] [blame] | 2999 | def test_git_cl_try_bots_on_multiple_masters(self): |
| 3000 | self.mock(git_cl.Changelist, 'GetMostRecentPatchset', lambda _: 20001) |
| 3001 | self.calls = [ |
| 3002 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
| 3003 | ((['git', 'config', 'branch.feature.rietveldissue'],), '123'), |
| 3004 | ((['git', 'config', 'rietveld.autoupdate'],), CERR1), |
| 3005 | ((['git', 'config', 'rietveld.server'],), |
tandrii | 8c5a353 | 2016-11-04 07:52:02 -0700 | [diff] [blame] | 3006 | 'https://codereview.chromium.org'), |
qyearsley | 123a468 | 2016-10-26 09:12:17 -0700 | [diff] [blame] | 3007 | ((['git', 'config', 'branch.feature.rietveldserver'],), CERR1), |
| 3008 | ((['git', 'config', 'branch.feature.rietveldpatchset'],), '20001'), |
| 3009 | ] |
| 3010 | |
| 3011 | def _buildbucket_retry(*_, **kw): |
| 3012 | body = json.loads(kw['body']) |
| 3013 | self.assertEqual(len(body['builds']), 2) |
| 3014 | |
| 3015 | first_build_params = json.loads(body['builds'][0]['parameters_json']) |
| 3016 | self.assertEqual(first_build_params['builder_name'], 'builder1') |
| 3017 | self.assertEqual(first_build_params['properties']['master'], 'master1') |
| 3018 | |
| 3019 | first_build_params = json.loads(body['builds'][1]['parameters_json']) |
| 3020 | self.assertEqual(first_build_params['builder_name'], 'builder2') |
| 3021 | self.assertEqual(first_build_params['properties']['master'], 'master2') |
| 3022 | |
| 3023 | self.mock(git_cl, '_buildbucket_retry', _buildbucket_retry) |
| 3024 | |
| 3025 | self.mock(git_cl.urllib2, 'urlopen', lambda _: StringIO.StringIO( |
| 3026 | json.dumps({'builder1': ['master1'], 'builder2': ['master2']}))) |
| 3027 | |
| 3028 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
| 3029 | self.assertEqual( |
| 3030 | 0, git_cl.main(['try', '-b', 'builder1', '-b', 'builder2'])) |
| 3031 | self.assertEqual( |
| 3032 | git_cl.sys.stdout.getvalue(), |
| 3033 | 'Tried jobs on:\n' |
| 3034 | 'Bucket: master.master1\n' |
| 3035 | ' builder1: []\n' |
| 3036 | 'Bucket: master.master2\n' |
| 3037 | ' builder2: []\n' |
| 3038 | 'To see results here, run: git cl try-results\n' |
| 3039 | 'To see results in browser, run: git cl web\n') |
| 3040 | |
tandrii | 16e0b4e | 2016-06-07 10:34:28 -0700 | [diff] [blame] | 3041 | def _common_GerritCommitMsgHookCheck(self): |
| 3042 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
| 3043 | self.mock(git_cl.os.path, 'abspath', |
| 3044 | lambda path: self._mocked_call(['abspath', path])) |
| 3045 | self.mock(git_cl.os.path, 'exists', |
| 3046 | lambda path: self._mocked_call(['exists', path])) |
| 3047 | self.mock(git_cl.gclient_utils, 'FileRead', |
| 3048 | lambda path: self._mocked_call(['FileRead', path])) |
| 3049 | self.mock(git_cl.gclient_utils, 'rm_file_or_tree', |
| 3050 | lambda path: self._mocked_call(['rm_file_or_tree', path])) |
| 3051 | self.calls = [ |
| 3052 | ((['git', 'rev-parse', '--show-cdup'],), '../'), |
| 3053 | ((['abspath', '../'],), '/abs/git_repo_root'), |
| 3054 | ] |
| 3055 | return git_cl.Changelist(codereview='gerrit', issue=123) |
| 3056 | |
| 3057 | def test_GerritCommitMsgHookCheck_custom_hook(self): |
| 3058 | cl = self._common_GerritCommitMsgHookCheck() |
| 3059 | self.calls += [ |
| 3060 | ((['exists', '/abs/git_repo_root/.git/hooks/commit-msg'],), True), |
| 3061 | ((['FileRead', '/abs/git_repo_root/.git/hooks/commit-msg'],), |
| 3062 | '#!/bin/sh\necho "custom hook"') |
| 3063 | ] |
| 3064 | cl._codereview_impl._GerritCommitMsgHookCheck(offer_removal=True) |
| 3065 | |
| 3066 | def test_GerritCommitMsgHookCheck_not_exists(self): |
| 3067 | cl = self._common_GerritCommitMsgHookCheck() |
| 3068 | self.calls += [ |
| 3069 | ((['exists', '/abs/git_repo_root/.git/hooks/commit-msg'],), False), |
| 3070 | ] |
| 3071 | cl._codereview_impl._GerritCommitMsgHookCheck(offer_removal=True) |
| 3072 | |
| 3073 | def test_GerritCommitMsgHookCheck(self): |
| 3074 | cl = self._common_GerritCommitMsgHookCheck() |
| 3075 | self.calls += [ |
| 3076 | ((['exists', '/abs/git_repo_root/.git/hooks/commit-msg'],), True), |
| 3077 | ((['FileRead', '/abs/git_repo_root/.git/hooks/commit-msg'],), |
| 3078 | '...\n# From Gerrit Code Review\n...\nadd_ChangeId()\n'), |
Andrii Shyshkalov | abc26ac | 2017-03-14 14:49:38 +0100 | [diff] [blame] | 3079 | (('ask_for_data', 'Do you want to remove it now? [Yes/No]: '), 'Yes'), |
tandrii | 16e0b4e | 2016-06-07 10:34:28 -0700 | [diff] [blame] | 3080 | ((['rm_file_or_tree', '/abs/git_repo_root/.git/hooks/commit-msg'],), |
| 3081 | ''), |
| 3082 | ] |
| 3083 | cl._codereview_impl._GerritCommitMsgHookCheck(offer_removal=True) |
| 3084 | |
tandrii | c4344b5 | 2016-08-29 06:04:54 -0700 | [diff] [blame] | 3085 | def test_GerritCmdLand(self): |
| 3086 | self.calls += [ |
| 3087 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
| 3088 | ((['git', 'config', 'branch.feature.gerritsquashhash'],), |
| 3089 | 'deadbeaf'), |
| 3090 | ((['git', 'diff', 'deadbeaf'],), ''), # No diff. |
| 3091 | ((['git', 'config', 'branch.feature.gerritserver'],), |
| 3092 | 'chromium-review.googlesource.com'), |
| 3093 | ] |
| 3094 | cl = git_cl.Changelist(issue=123, codereview='gerrit') |
| 3095 | cl._codereview_impl._GetChangeDetail = lambda _: { |
| 3096 | 'labels': {}, |
| 3097 | 'current_revision': 'deadbeaf', |
| 3098 | } |
agable | 32978d9 | 2016-11-01 12:55:02 -0700 | [diff] [blame] | 3099 | cl._codereview_impl._GetChangeCommit = lambda: { |
| 3100 | 'commit': 'deadbeef', |
Aaron Gable | 02cdbb4 | 2016-12-13 16:24:25 -0800 | [diff] [blame] | 3101 | 'web_links': [{'name': 'gitiles', |
agable | 32978d9 | 2016-11-01 12:55:02 -0700 | [diff] [blame] | 3102 | 'url': 'https://git.googlesource.com/test/+/deadbeef'}], |
| 3103 | } |
tandrii | c4344b5 | 2016-08-29 06:04:54 -0700 | [diff] [blame] | 3104 | cl._codereview_impl.SubmitIssue = lambda wait_for_merge: None |
tandrii | 8da412c | 2016-09-07 16:01:07 -0700 | [diff] [blame] | 3105 | out = StringIO.StringIO() |
| 3106 | self.mock(sys, 'stdout', out) |
tandrii | c4344b5 | 2016-08-29 06:04:54 -0700 | [diff] [blame] | 3107 | self.assertEqual(0, cl.CMDLand(force=True, bypass_hooks=True, verbose=True)) |
tandrii | 8da412c | 2016-09-07 16:01:07 -0700 | [diff] [blame] | 3108 | self.assertRegexpMatches(out.getvalue(), 'Issue.*123 has been submitted') |
Quinten Yearsley | 0c62da9 | 2017-05-31 13:39:42 -0700 | [diff] [blame] | 3109 | self.assertRegexpMatches(out.getvalue(), 'Landed as: .*deadbeef') |
tandrii | c4344b5 | 2016-08-29 06:04:54 -0700 | [diff] [blame] | 3110 | |
tandrii | 221ab25 | 2016-10-06 08:12:04 -0700 | [diff] [blame] | 3111 | BUILDBUCKET_BUILDS_MAP = { |
| 3112 | '9000': { |
| 3113 | 'id': '9000', |
| 3114 | 'status': 'STARTED', |
| 3115 | 'url': 'http://build.cr.org/p/x.y/builders/my-builder/builds/2', |
| 3116 | 'result_details_json': '{"properties": {}}', |
| 3117 | 'bucket': 'master.x.y', |
| 3118 | 'created_by': 'user:someone@chromium.org', |
| 3119 | 'created_ts': '147200002222000', |
| 3120 | 'parameters_json': '{"builder_name": "my-builder", "category": ""}', |
| 3121 | }, |
| 3122 | '8000': { |
| 3123 | 'id': '8000', |
| 3124 | 'status': 'COMPLETED', |
| 3125 | 'result': 'FAILURE', |
| 3126 | 'failure_reason': 'BUILD_FAILURE', |
| 3127 | 'url': 'http://build.cr.org/p/x.y/builders/my-builder/builds/1', |
| 3128 | 'result_details_json': '{"properties": {}}', |
| 3129 | 'bucket': 'master.x.y', |
| 3130 | 'created_by': 'user:someone@chromium.org', |
| 3131 | 'created_ts': '147200001111000', |
| 3132 | 'parameters_json': '{"builder_name": "my-builder", "category": ""}', |
| 3133 | }, |
| 3134 | } |
| 3135 | |
| 3136 | def test_write_try_results_json(self): |
| 3137 | expected_output = [ |
| 3138 | { |
| 3139 | 'buildbucket_id': '8000', |
| 3140 | 'bucket': 'master.x.y', |
| 3141 | 'builder_name': 'my-builder', |
| 3142 | 'status': 'COMPLETED', |
| 3143 | 'result': 'FAILURE', |
| 3144 | 'failure_reason': 'BUILD_FAILURE', |
| 3145 | 'url': 'http://build.cr.org/p/x.y/builders/my-builder/builds/1', |
| 3146 | }, |
| 3147 | { |
| 3148 | 'buildbucket_id': '9000', |
| 3149 | 'bucket': 'master.x.y', |
| 3150 | 'builder_name': 'my-builder', |
| 3151 | 'status': 'STARTED', |
| 3152 | 'result': None, |
| 3153 | 'failure_reason': None, |
| 3154 | 'url': 'http://build.cr.org/p/x.y/builders/my-builder/builds/2', |
| 3155 | } |
| 3156 | ] |
| 3157 | self.calls = [(('write_json', 'output.json', expected_output), '')] |
| 3158 | git_cl.write_try_results_json('output.json', self.BUILDBUCKET_BUILDS_MAP) |
| 3159 | |
tandrii | 45b2a58 | 2016-10-11 03:14:16 -0700 | [diff] [blame] | 3160 | def _setup_fetch_try_jobs(self, most_recent_patchset=20001): |
tandrii | 221ab25 | 2016-10-06 08:12:04 -0700 | [diff] [blame] | 3161 | out = StringIO.StringIO() |
| 3162 | self.mock(sys, 'stdout', out) |
tandrii | 45b2a58 | 2016-10-11 03:14:16 -0700 | [diff] [blame] | 3163 | self.mock(git_cl.Changelist, 'GetMostRecentPatchset', |
| 3164 | lambda *args: most_recent_patchset) |
tandrii | 221ab25 | 2016-10-06 08:12:04 -0700 | [diff] [blame] | 3165 | self.mock(git_cl.auth, 'get_authenticator_for_host', lambda host, _cfg: |
| 3166 | self._mocked_call(['get_authenticator_for_host', host])) |
| 3167 | self.mock(git_cl, '_buildbucket_retry', lambda *_, **__: |
| 3168 | self._mocked_call(['_buildbucket_retry'])) |
tandrii | 45b2a58 | 2016-10-11 03:14:16 -0700 | [diff] [blame] | 3169 | |
| 3170 | def _setup_fetch_try_jobs_rietveld(self, *request_results): |
| 3171 | self._setup_fetch_try_jobs(most_recent_patchset=20001) |
tandrii | 221ab25 | 2016-10-06 08:12:04 -0700 | [diff] [blame] | 3172 | self.calls += [ |
| 3173 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
| 3174 | ((['git', 'config', 'branch.feature.rietveldissue'],), '1'), |
| 3175 | ((['git', 'config', 'rietveld.autoupdate'],), CERR1), |
| 3176 | ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'), |
| 3177 | ((['git', 'config', 'branch.feature.rietveldpatchset'],), '20001'), |
| 3178 | ((['git', 'config', 'branch.feature.rietveldserver'],), |
| 3179 | 'codereview.example.com'), |
| 3180 | ((['get_authenticator_for_host', 'codereview.example.com'],), |
| 3181 | AuthenticatorMock()), |
| 3182 | ] + [((['_buildbucket_retry'],), r) for r in request_results] |
| 3183 | |
| 3184 | def test_fetch_try_jobs_none_rietveld(self): |
| 3185 | self._setup_fetch_try_jobs_rietveld({}) |
tandrii | 45b2a58 | 2016-10-11 03:14:16 -0700 | [diff] [blame] | 3186 | # Simulate that user isn't logged in. |
| 3187 | self.mock(AuthenticatorMock, 'has_cached_credentials', lambda _: False) |
tandrii | 221ab25 | 2016-10-06 08:12:04 -0700 | [diff] [blame] | 3188 | self.assertEqual(0, git_cl.main(['try-results'])) |
tandrii | 45b2a58 | 2016-10-11 03:14:16 -0700 | [diff] [blame] | 3189 | self.assertRegexpMatches(sys.stdout.getvalue(), |
| 3190 | 'Warning: Some results might be missing') |
tandrii | 221ab25 | 2016-10-06 08:12:04 -0700 | [diff] [blame] | 3191 | self.assertRegexpMatches(sys.stdout.getvalue(), 'No try jobs') |
| 3192 | |
| 3193 | def test_fetch_try_jobs_some_rietveld(self): |
| 3194 | self._setup_fetch_try_jobs_rietveld({ |
| 3195 | 'builds': self.BUILDBUCKET_BUILDS_MAP.values(), |
| 3196 | }) |
| 3197 | self.assertEqual(0, git_cl.main(['try-results'])) |
tandrii | 45b2a58 | 2016-10-11 03:14:16 -0700 | [diff] [blame] | 3198 | self.assertRegexpMatches(sys.stdout.getvalue(), '^Failures:') |
| 3199 | self.assertRegexpMatches(sys.stdout.getvalue(), 'Started:') |
| 3200 | self.assertRegexpMatches(sys.stdout.getvalue(), '2 try jobs') |
| 3201 | |
| 3202 | def _setup_fetch_try_jobs_gerrit(self, *request_results): |
| 3203 | self._setup_fetch_try_jobs(most_recent_patchset=13) |
| 3204 | self.calls += [ |
| 3205 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
| 3206 | ((['git', 'config', 'branch.feature.rietveldissue'],), CERR1), |
| 3207 | ((['git', 'config', 'branch.feature.gerritissue'],), '1'), |
Ravi Mistry | fda50ca | 2016-11-14 10:19:18 -0500 | [diff] [blame] | 3208 | # TODO(tandrii): Uncomment the below if we decide to support checking |
| 3209 | # patchsets for Gerrit. |
tandrii | 45b2a58 | 2016-10-11 03:14:16 -0700 | [diff] [blame] | 3210 | # Simulate that Gerrit has more patchsets than local. |
Ravi Mistry | fda50ca | 2016-11-14 10:19:18 -0500 | [diff] [blame] | 3211 | # ((['git', 'config', 'branch.feature.gerritpatchset'],), '12'), |
tandrii | 45b2a58 | 2016-10-11 03:14:16 -0700 | [diff] [blame] | 3212 | ((['git', 'config', 'branch.feature.gerritserver'],), |
| 3213 | 'https://x-review.googlesource.com'), |
| 3214 | ((['get_authenticator_for_host', 'x-review.googlesource.com'],), |
| 3215 | AuthenticatorMock()), |
| 3216 | ] + [((['_buildbucket_retry'],), r) for r in request_results] |
| 3217 | |
| 3218 | def test_fetch_try_jobs_none_gerrit(self): |
| 3219 | self._setup_fetch_try_jobs_gerrit({}) |
| 3220 | self.assertEqual(0, git_cl.main(['try-results'])) |
Ravi Mistry | fda50ca | 2016-11-14 10:19:18 -0500 | [diff] [blame] | 3221 | # TODO(tandrii): Uncomment the below if we decide to support checking |
| 3222 | # patchsets for Gerrit. |
| 3223 | # self.assertRegexpMatches( |
| 3224 | # sys.stdout.getvalue(), |
| 3225 | # r'Warning: Codereview server has newer patchsets \(13\)') |
tandrii | 45b2a58 | 2016-10-11 03:14:16 -0700 | [diff] [blame] | 3226 | self.assertRegexpMatches(sys.stdout.getvalue(), 'No try jobs') |
| 3227 | |
| 3228 | def test_fetch_try_jobs_some_gerrit(self): |
| 3229 | self._setup_fetch_try_jobs_gerrit({ |
| 3230 | 'builds': self.BUILDBUCKET_BUILDS_MAP.values(), |
| 3231 | }) |
Ravi Mistry | fda50ca | 2016-11-14 10:19:18 -0500 | [diff] [blame] | 3232 | # TODO(tandrii): Uncomment the below if we decide to support checking |
| 3233 | # patchsets for Gerrit. |
| 3234 | # self.calls.remove( |
| 3235 | # ((['git', 'config', 'branch.feature.gerritpatchset'],), '12')) |
tandrii | 45b2a58 | 2016-10-11 03:14:16 -0700 | [diff] [blame] | 3236 | self.assertEqual(0, git_cl.main(['try-results', '--patchset', '5'])) |
| 3237 | |
| 3238 | # ... and doesn't result in warning. |
| 3239 | self.assertNotRegexpMatches(sys.stdout.getvalue(), 'Warning') |
| 3240 | self.assertRegexpMatches(sys.stdout.getvalue(), '^Failures:') |
tandrii | 221ab25 | 2016-10-06 08:12:04 -0700 | [diff] [blame] | 3241 | self.assertRegexpMatches(sys.stdout.getvalue(), 'Started:') |
| 3242 | self.assertRegexpMatches(sys.stdout.getvalue(), '2 try jobs') |
| 3243 | |
Andrii Shyshkalov | 258e0a6 | 2017-01-24 16:50:57 +0100 | [diff] [blame] | 3244 | def _mock_gerrit_changes_for_detail_cache(self): |
| 3245 | self.mock(git_cl._GerritChangelistImpl, '_GetGerritHost', lambda _: 'host') |
Andrii Shyshkalov | 258e0a6 | 2017-01-24 16:50:57 +0100 | [diff] [blame] | 3246 | |
| 3247 | def test_gerrit_change_detail_cache_normalize(self): |
| 3248 | self._mock_gerrit_changes_for_detail_cache() |
| 3249 | self.calls = [ |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 3250 | (('GetChangeDetail', 'host', '2', ['CASE']), 'b'), |
Andrii Shyshkalov | 258e0a6 | 2017-01-24 16:50:57 +0100 | [diff] [blame] | 3251 | ] |
| 3252 | cl = git_cl.Changelist(codereview='gerrit') |
| 3253 | self.assertEqual(cl._GetChangeDetail(issue=2, options=['CaSe']), 'b') |
| 3254 | self.assertEqual(cl._GetChangeDetail(issue=2, options=['CASE']), 'b') |
| 3255 | self.assertEqual(cl._GetChangeDetail(issue='2'), 'b') |
| 3256 | self.assertEqual(cl._GetChangeDetail(issue=2), 'b') |
| 3257 | |
| 3258 | def test_gerrit_change_detail_cache_simple(self): |
| 3259 | self._mock_gerrit_changes_for_detail_cache() |
| 3260 | self.calls = [ |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 3261 | (('GetChangeDetail', 'host', '1', []), 'a'), |
| 3262 | (('GetChangeDetail', 'host', '2', []), 'b'), |
| 3263 | (('GetChangeDetail', 'host', '2', []), 'b2'), |
Andrii Shyshkalov | 258e0a6 | 2017-01-24 16:50:57 +0100 | [diff] [blame] | 3264 | ] |
| 3265 | cl = git_cl.Changelist(issue=1, codereview='gerrit') |
| 3266 | self.assertEqual(cl._GetChangeDetail(), 'a') # Miss. |
| 3267 | self.assertEqual(cl._GetChangeDetail(), 'a') |
| 3268 | self.assertEqual(cl._GetChangeDetail(issue=2), 'b') # Miss. |
Andrii Shyshkalov | 1897532 | 2017-01-25 16:44:13 +0100 | [diff] [blame] | 3269 | self.assertEqual(cl._GetChangeDetail(issue=2, no_cache=True), 'b2') # Miss. |
Andrii Shyshkalov | 258e0a6 | 2017-01-24 16:50:57 +0100 | [diff] [blame] | 3270 | self.assertEqual(cl._GetChangeDetail(), 'a') |
| 3271 | self.assertEqual(cl._GetChangeDetail(issue=2), 'b2') |
| 3272 | |
| 3273 | def test_gerrit_change_detail_cache_options(self): |
| 3274 | self._mock_gerrit_changes_for_detail_cache() |
| 3275 | self.calls = [ |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 3276 | (('GetChangeDetail', 'host', '1', ['C', 'A', 'B']), 'cab'), |
| 3277 | (('GetChangeDetail', 'host', '1', ['A', 'D']), 'ad'), |
| 3278 | (('GetChangeDetail', 'host', '1', ['A']), 'a'), # no_cache=True |
| 3279 | (('GetChangeDetail', 'host', '1', ['B']), 'b'), # no longer in cache. |
Andrii Shyshkalov | 258e0a6 | 2017-01-24 16:50:57 +0100 | [diff] [blame] | 3280 | ] |
| 3281 | cl = git_cl.Changelist(issue=1, codereview='gerrit') |
| 3282 | self.assertEqual(cl._GetChangeDetail(options=['C', 'A', 'B']), 'cab') |
| 3283 | self.assertEqual(cl._GetChangeDetail(options=['A', 'B', 'C']), 'cab') |
| 3284 | self.assertEqual(cl._GetChangeDetail(options=['B', 'A']), 'cab') |
| 3285 | self.assertEqual(cl._GetChangeDetail(options=['C']), 'cab') |
| 3286 | self.assertEqual(cl._GetChangeDetail(options=['A']), 'cab') |
| 3287 | self.assertEqual(cl._GetChangeDetail(), 'cab') |
| 3288 | |
| 3289 | self.assertEqual(cl._GetChangeDetail(options=['A', 'D']), 'ad') |
| 3290 | self.assertEqual(cl._GetChangeDetail(options=['A']), 'cab') |
| 3291 | self.assertEqual(cl._GetChangeDetail(options=['D']), 'ad') |
| 3292 | self.assertEqual(cl._GetChangeDetail(), 'cab') |
| 3293 | |
| 3294 | # Finally, no_cache should invalidate all caches for given change. |
| 3295 | self.assertEqual(cl._GetChangeDetail(options=['A'], no_cache=True), 'a') |
| 3296 | self.assertEqual(cl._GetChangeDetail(options=['B']), 'b') |
| 3297 | |
Andrii Shyshkalov | 21fb824 | 2017-02-15 21:09:27 +0100 | [diff] [blame] | 3298 | def test_gerrit_description_caching(self): |
| 3299 | def gen_detail(rev, desc): |
| 3300 | return { |
| 3301 | 'current_revision': rev, |
| 3302 | 'revisions': {rev: {'commit': {'message': desc}}} |
| 3303 | } |
| 3304 | self.calls = [ |
| 3305 | (('GetChangeDetail', 'host', '1', |
| 3306 | ['CURRENT_REVISION', 'CURRENT_COMMIT']), |
| 3307 | gen_detail('rev1', 'desc1')), |
| 3308 | (('GetChangeDetail', 'host', '1', |
| 3309 | ['CURRENT_REVISION', 'CURRENT_COMMIT']), |
| 3310 | gen_detail('rev2', 'desc2')), |
| 3311 | ] |
| 3312 | |
| 3313 | self._mock_gerrit_changes_for_detail_cache() |
| 3314 | cl = git_cl.Changelist(issue=1, codereview='gerrit') |
| 3315 | self.assertEqual(cl.GetDescription(), 'desc1') |
| 3316 | self.assertEqual(cl.GetDescription(), 'desc1') # cache hit. |
| 3317 | self.assertEqual(cl.GetDescription(force=True), 'desc2') |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 3318 | |
Andrii Shyshkalov | 34924cd | 2017-03-15 17:08:32 +0100 | [diff] [blame] | 3319 | def test_print_current_creds(self): |
| 3320 | class CookiesAuthenticatorMock(object): |
| 3321 | def __init__(self): |
| 3322 | self.gitcookies = { |
| 3323 | 'host.googlesource.com': ('user', 'pass'), |
| 3324 | 'host-review.googlesource.com': ('user', 'pass'), |
| 3325 | } |
| 3326 | self.netrc = self |
| 3327 | self.netrc.hosts = { |
| 3328 | 'github.com': ('user2', None, 'pass2'), |
| 3329 | 'host2.googlesource.com': ('user3', None, 'pass'), |
| 3330 | } |
| 3331 | self.mock(git_cl.gerrit_util, 'CookiesAuthenticator', |
| 3332 | CookiesAuthenticatorMock) |
| 3333 | self.mock(sys, 'stdout', StringIO.StringIO()) |
| 3334 | git_cl._GitCookiesChecker().print_current_creds(include_netrc=True) |
| 3335 | self.assertEqual(list(sys.stdout.getvalue().splitlines()), [ |
| 3336 | ' Host\t User\t Which file', |
| 3337 | '============================\t=====\t===========', |
| 3338 | 'host-review.googlesource.com\t user\t.gitcookies', |
| 3339 | ' host.googlesource.com\t user\t.gitcookies', |
| 3340 | ' host2.googlesource.com\tuser3\t .netrc', |
| 3341 | ]) |
| 3342 | sys.stdout.buf = '' |
| 3343 | git_cl._GitCookiesChecker().print_current_creds(include_netrc=False) |
| 3344 | self.assertEqual(list(sys.stdout.getvalue().splitlines()), [ |
| 3345 | ' Host\tUser\t Which file', |
| 3346 | '============================\t====\t===========', |
| 3347 | 'host-review.googlesource.com\tuser\t.gitcookies', |
| 3348 | ' host.googlesource.com\tuser\t.gitcookies', |
| 3349 | ]) |
| 3350 | |
Andrii Shyshkalov | 353637c | 2017-03-14 16:52:18 +0100 | [diff] [blame] | 3351 | def _common_creds_check_mocks(self): |
| 3352 | def exists_mock(path): |
| 3353 | dirname = os.path.dirname(path) |
| 3354 | if dirname == os.path.expanduser('~'): |
| 3355 | dirname = '~' |
| 3356 | base = os.path.basename(path) |
| 3357 | if base in ('.netrc', '.gitcookies'): |
| 3358 | return self._mocked_call('os.path.exists', '%s/%s' % (dirname, base)) |
| 3359 | # git cl also checks for existence other files not relevant to this test. |
| 3360 | return None |
| 3361 | self.mock(os.path, 'exists', exists_mock) |
| 3362 | self.mock(sys, 'stdout', StringIO.StringIO()) |
| 3363 | |
| 3364 | def test_creds_check_gitcookies_not_configured(self): |
| 3365 | self._common_creds_check_mocks() |
Andrii Shyshkalov | 34924cd | 2017-03-15 17:08:32 +0100 | [diff] [blame] | 3366 | self.mock(git_cl._GitCookiesChecker, 'get_hosts_with_creds', |
Andrii Shyshkalov | 0a0b067 | 2017-03-16 16:27:48 +0100 | [diff] [blame] | 3367 | lambda _, include_netrc=False: []) |
Andrii Shyshkalov | 353637c | 2017-03-14 16:52:18 +0100 | [diff] [blame] | 3368 | self.calls = [ |
| 3369 | ((['git', 'config', '--global', 'http.cookiefile'],), CERR1), |
| 3370 | (('os.path.exists', '~/.netrc'), True), |
| 3371 | (('ask_for_data', 'Press Enter to setup .gitcookies, ' |
| 3372 | 'or Ctrl+C to abort'), ''), |
| 3373 | ((['git', 'config', '--global', 'http.cookiefile', |
| 3374 | os.path.expanduser('~/.gitcookies')], ), ''), |
| 3375 | ] |
| 3376 | self.assertEqual(0, git_cl.main(['creds-check'])) |
| 3377 | self.assertRegexpMatches( |
| 3378 | sys.stdout.getvalue(), |
| 3379 | '^You seem to be using outdated .netrc for git credentials:') |
| 3380 | self.assertRegexpMatches( |
| 3381 | sys.stdout.getvalue(), |
| 3382 | '\nConfigured git to use .gitcookies from') |
| 3383 | |
| 3384 | def test_creds_check_gitcookies_configured_custom_broken(self): |
| 3385 | self._common_creds_check_mocks() |
Andrii Shyshkalov | 34924cd | 2017-03-15 17:08:32 +0100 | [diff] [blame] | 3386 | self.mock(git_cl._GitCookiesChecker, 'get_hosts_with_creds', |
Andrii Shyshkalov | 0a0b067 | 2017-03-16 16:27:48 +0100 | [diff] [blame] | 3387 | lambda _, include_netrc=False: []) |
Andrii Shyshkalov | 353637c | 2017-03-14 16:52:18 +0100 | [diff] [blame] | 3388 | self.calls = [ |
| 3389 | ((['git', 'config', '--global', 'http.cookiefile'],), |
| 3390 | '/custom/.gitcookies'), |
| 3391 | (('os.path.exists', '/custom/.gitcookies'), False), |
| 3392 | (('ask_for_data', 'Reconfigure git to use default .gitcookies? ' |
| 3393 | 'Press Enter to reconfigure, or Ctrl+C to abort'), ''), |
| 3394 | ((['git', 'config', '--global', 'http.cookiefile', |
| 3395 | os.path.expanduser('~/.gitcookies')], ), ''), |
| 3396 | ] |
| 3397 | self.assertEqual(0, git_cl.main(['creds-check'])) |
| 3398 | self.assertRegexpMatches( |
| 3399 | sys.stdout.getvalue(), |
Quinten Yearsley | 0c62da9 | 2017-05-31 13:39:42 -0700 | [diff] [blame] | 3400 | 'WARNING: You have configured custom path to .gitcookies: ') |
Andrii Shyshkalov | 353637c | 2017-03-14 16:52:18 +0100 | [diff] [blame] | 3401 | self.assertRegexpMatches( |
| 3402 | sys.stdout.getvalue(), |
| 3403 | 'However, your configured .gitcookies file is missing.') |
| 3404 | |
Andrii Shyshkalov | 0d6b46e | 2017-03-17 22:23:22 +0100 | [diff] [blame] | 3405 | def test_git_cl_comment_add_rietveld(self): |
Andrii Shyshkalov | 625986d | 2017-03-16 00:24:37 +0100 | [diff] [blame] | 3406 | self.mock(git_cl._RietveldChangelistImpl, 'AddComment', |
Aaron Gable | 636b13f | 2017-07-14 10:42:48 -0700 | [diff] [blame] | 3407 | lambda _, message, publish: self._mocked_call( |
| 3408 | 'AddComment', message, publish)) |
Andrii Shyshkalov | 625986d | 2017-03-16 00:24:37 +0100 | [diff] [blame] | 3409 | self.calls = [ |
| 3410 | ((['git', 'config', 'rietveld.autoupdate'],), CERR1), |
| 3411 | ((['git', 'config', 'rietveld.server'],), 'codereview.chromium.org'), |
Aaron Gable | 636b13f | 2017-07-14 10:42:48 -0700 | [diff] [blame] | 3412 | (('AddComment', 'msg', None), ''), |
Andrii Shyshkalov | 625986d | 2017-03-16 00:24:37 +0100 | [diff] [blame] | 3413 | ] |
Andrii Shyshkalov | 0d6b46e | 2017-03-17 22:23:22 +0100 | [diff] [blame] | 3414 | self.assertEqual(0, git_cl.main(['comment', '--rietveld', |
| 3415 | '-i', '10', '-a', 'msg'])) |
Andrii Shyshkalov | 625986d | 2017-03-16 00:24:37 +0100 | [diff] [blame] | 3416 | |
| 3417 | def test_git_cl_comment_add_gerrit(self): |
| 3418 | self.mock(git_cl.gerrit_util, 'SetReview', |
Aaron Gable | 636b13f | 2017-07-14 10:42:48 -0700 | [diff] [blame] | 3419 | lambda host, change, msg, ready: |
| 3420 | self._mocked_call('SetReview', host, change, msg, ready)) |
Andrii Shyshkalov | 625986d | 2017-03-16 00:24:37 +0100 | [diff] [blame] | 3421 | self.calls = [ |
| 3422 | ((['git', 'symbolic-ref', 'HEAD'],), CERR1), |
| 3423 | ((['git', 'symbolic-ref', 'HEAD'],), CERR1), |
| 3424 | ((['git', 'config', 'rietveld.upstream-branch'],), CERR1), |
| 3425 | ((['git', 'branch', '-r'],), 'origin/HEAD -> origin/master\n' |
| 3426 | 'origin/master'), |
| 3427 | ((['git', 'config', 'remote.origin.url'],), |
| 3428 | 'https://chromium.googlesource.com/infra/infra'), |
Aaron Gable | 636b13f | 2017-07-14 10:42:48 -0700 | [diff] [blame] | 3429 | (('SetReview', 'chromium-review.googlesource.com', 10, 'msg', None), |
| 3430 | None), |
Andrii Shyshkalov | 625986d | 2017-03-16 00:24:37 +0100 | [diff] [blame] | 3431 | ] |
| 3432 | self.assertEqual(0, git_cl.main(['comment', '--gerrit', '-i', '10', |
| 3433 | '-a', 'msg'])) |
| 3434 | |
Andrii Shyshkalov | d8aa49f | 2017-03-17 16:05:49 +0100 | [diff] [blame] | 3435 | def test_git_cl_comments_fetch_rietveld(self): |
| 3436 | self.mock(sys, 'stdout', StringIO.StringIO()) |
| 3437 | self.calls = [ |
| 3438 | ((['git', 'config', 'rietveld.autoupdate'],), CERR1), |
| 3439 | ((['git', 'config', 'rietveld.server'],), 'codereview.chromium.org'), |
| 3440 | ] * 2 |
| 3441 | self.mock(git_cl._RietveldChangelistImpl, 'GetIssueProperties', lambda _: { |
| 3442 | 'messages': [ |
| 3443 | {'text': 'lgtm', 'date': '2017-03-13 20:49:34.515270', |
| 3444 | 'disapproval': False, 'approval': True, 'sender': 'r@example.com'}, |
| 3445 | {'text': 'not lgtm', 'date': '2017-03-13 21:50:34.515270', |
| 3446 | 'disapproval': True, 'approval': False, 'sender': 'r2@example.com'}, |
| 3447 | # Intentionally wrong order here. |
| 3448 | {'text': 'PTAL', 'date': '2000-03-13 20:49:34.515270', |
| 3449 | 'disapproval': False, 'approval': False, |
| 3450 | 'sender': 'owner@example.com'}, |
| 3451 | ], |
| 3452 | 'owner_email': 'owner@example.com', |
| 3453 | }) |
| 3454 | expected_comments_summary = [ |
| 3455 | git_cl._CommentSummary( |
| 3456 | message='lgtm', |
| 3457 | date=datetime.datetime(2017, 3, 13, 20, 49, 34, 515270), |
| 3458 | disapproval=False, approval=True, sender='r@example.com'), |
| 3459 | git_cl._CommentSummary( |
| 3460 | message='not lgtm', |
| 3461 | date=datetime.datetime(2017, 3, 13, 21, 50, 34, 515270), |
| 3462 | disapproval=True, approval=False, sender='r2@example.com'), |
| 3463 | # Note: same order as in whatever Rietveld returns. |
| 3464 | git_cl._CommentSummary( |
| 3465 | message='PTAL', |
| 3466 | date=datetime.datetime(2000, 3, 13, 20, 49, 34, 515270), |
| 3467 | disapproval=False, approval=False, sender='owner@example.com'), |
| 3468 | ] |
| 3469 | cl = git_cl.Changelist(codereview='rietveld', issue=1) |
| 3470 | self.assertEqual(cl.GetCommentsSummary(), expected_comments_summary) |
| 3471 | |
| 3472 | with git_cl.gclient_utils.temporary_directory() as tempdir: |
| 3473 | out_file = os.path.abspath(os.path.join(tempdir, 'out.json')) |
| 3474 | self.assertEqual(0, git_cl.main(['comment', '--rietveld', '-i', '10', |
| 3475 | '-j', out_file])) |
| 3476 | with open(out_file) as f: |
| 3477 | read = json.load(f) |
| 3478 | self.assertEqual(len(read), 3) |
| 3479 | self.assertEqual(read[0], { |
| 3480 | 'date': '2000-03-13 20:49:34.515270', |
| 3481 | 'message': 'PTAL', |
| 3482 | 'approval': False, |
| 3483 | 'disapproval': False, |
| 3484 | 'sender': 'owner@example.com'}) |
| 3485 | self.assertEqual(read[1]['date'], '2017-03-13 20:49:34.515270') |
| 3486 | self.assertEqual(read[2]['date'], '2017-03-13 21:50:34.515270') |
| 3487 | |
Andrii Shyshkalov | 5a0cf20 | 2017-03-17 16:14:59 +0100 | [diff] [blame] | 3488 | def test_git_cl_comments_fetch_gerrit(self): |
| 3489 | self.mock(sys, 'stdout', StringIO.StringIO()) |
| 3490 | self.calls = [ |
| 3491 | ((['git', 'symbolic-ref', 'HEAD'],), CERR1), |
| 3492 | ((['git', 'symbolic-ref', 'HEAD'],), CERR1), |
| 3493 | ((['git', 'config', 'rietveld.upstream-branch'],), CERR1), |
| 3494 | ((['git', 'branch', '-r'],), 'origin/HEAD -> origin/master\n' |
| 3495 | 'origin/master'), |
| 3496 | ((['git', 'config', 'remote.origin.url'],), |
| 3497 | 'https://chromium.googlesource.com/infra/infra'), |
| 3498 | (('GetChangeDetail', 'chromium-review.googlesource.com', '1', |
| 3499 | ['MESSAGES', 'DETAILED_ACCOUNTS']), { |
Aaron Gable | 0ffdf2d | 2017-06-05 13:01:17 -0700 | [diff] [blame] | 3500 | 'owner': {'email': 'owner@example.com'}, |
Andrii Shyshkalov | 5a0cf20 | 2017-03-17 16:14:59 +0100 | [diff] [blame] | 3501 | 'messages': [ |
| 3502 | { |
| 3503 | u'_revision_number': 1, |
| 3504 | u'author': { |
| 3505 | u'_account_id': 1111084, |
| 3506 | u'email': u'commit-bot@chromium.org', |
| 3507 | u'name': u'Commit Bot' |
| 3508 | }, |
| 3509 | u'date': u'2017-03-15 20:08:45.000000000', |
| 3510 | u'id': u'f5a6c25ecbd3b3b54a43ae418ed97eff046dc50b', |
| 3511 | u'message': u'Patch Set 1:\n\nDry run: CQ is trying da patch...', |
| 3512 | u'tag': u'autogenerated:cq:dry-run' |
| 3513 | }, |
| 3514 | { |
| 3515 | u'_revision_number': 2, |
| 3516 | u'author': { |
| 3517 | u'_account_id': 11151243, |
| 3518 | u'email': u'owner@example.com', |
| 3519 | u'name': u'owner' |
| 3520 | }, |
| 3521 | u'date': u'2017-03-16 20:00:41.000000000', |
| 3522 | u'id': u'f5a6c25ecbd3b3b54a43ae418ed97eff046d1234', |
| 3523 | u'message': u'PTAL', |
| 3524 | }, |
| 3525 | { |
| 3526 | u'_revision_number': 2, |
| 3527 | u'author': { |
| 3528 | u'_account_id': 148512 , |
| 3529 | u'email': u'reviewer@example.com', |
| 3530 | u'name': u'reviewer' |
| 3531 | }, |
| 3532 | u'date': u'2017-03-17 05:19:37.500000000', |
| 3533 | u'id': u'f5a6c25ecbd3b3b54a43ae418ed97eff046d4568', |
| 3534 | u'message': u'Patch Set 2: Code-Review+1', |
| 3535 | }, |
| 3536 | ] |
Aaron Gable | 0ffdf2d | 2017-06-05 13:01:17 -0700 | [diff] [blame] | 3537 | }), |
| 3538 | (('GetChangeComments', 'chromium-review.googlesource.com', 1), { |
| 3539 | '/COMMIT_MSG': [ |
| 3540 | { |
| 3541 | 'author': {'email': u'reviewer@example.com'}, |
| 3542 | 'updated': u'2017-03-17 05:19:37.500000000', |
| 3543 | 'patch_set': 2, |
| 3544 | 'side': 'REVISION', |
| 3545 | 'message': 'Please include a bug link', |
| 3546 | }, |
| 3547 | ], |
| 3548 | 'codereview.settings': [ |
| 3549 | { |
| 3550 | 'author': {'email': u'owner@example.com'}, |
| 3551 | 'updated': u'2017-03-16 20:00:41.000000000', |
| 3552 | 'patch_set': 2, |
| 3553 | 'side': 'PARENT', |
| 3554 | 'line': 42, |
| 3555 | 'message': 'I removed this because it is bad', |
| 3556 | }, |
| 3557 | ] |
| 3558 | }), |
Andrii Shyshkalov | 5a0cf20 | 2017-03-17 16:14:59 +0100 | [diff] [blame] | 3559 | ] * 2 |
| 3560 | expected_comments_summary = [ |
| 3561 | git_cl._CommentSummary( |
Aaron Gable | 0ffdf2d | 2017-06-05 13:01:17 -0700 | [diff] [blame] | 3562 | message=( |
| 3563 | u'PTAL\n' + |
| 3564 | u'\n' + |
| 3565 | u'codereview.settings\n' + |
| 3566 | u' Base, Line 42: https://chromium-review.googlesource.com/' + |
| 3567 | u'c/1/2/codereview.settings#b42\n' + |
| 3568 | u' I removed this because it is bad\n'), |
Andrii Shyshkalov | 5a0cf20 | 2017-03-17 16:14:59 +0100 | [diff] [blame] | 3569 | date=datetime.datetime(2017, 3, 16, 20, 0, 41, 0), |
| 3570 | disapproval=False, approval=False, sender=u'owner@example.com'), |
| 3571 | git_cl._CommentSummary( |
Aaron Gable | 0ffdf2d | 2017-06-05 13:01:17 -0700 | [diff] [blame] | 3572 | message=( |
| 3573 | u'Patch Set 2: Code-Review+1\n' + |
| 3574 | u'\n' + |
| 3575 | u'/COMMIT_MSG\n' + |
| 3576 | u' PS2, File comment: https://chromium-review.googlesource.com/' + |
| 3577 | u'c/1/2//COMMIT_MSG#\n' + |
| 3578 | u' Please include a bug link\n'), |
Andrii Shyshkalov | 5a0cf20 | 2017-03-17 16:14:59 +0100 | [diff] [blame] | 3579 | date=datetime.datetime(2017, 3, 17, 5, 19, 37, 500000), |
| 3580 | disapproval=False, approval=False, sender=u'reviewer@example.com'), |
| 3581 | ] |
| 3582 | cl = git_cl.Changelist(codereview='gerrit', issue=1) |
| 3583 | self.assertEqual(cl.GetCommentsSummary(), expected_comments_summary) |
| 3584 | |
| 3585 | with git_cl.gclient_utils.temporary_directory() as tempdir: |
| 3586 | out_file = os.path.abspath(os.path.join(tempdir, 'out.json')) |
| 3587 | self.assertEqual(0, git_cl.main(['comment', '--gerrit', '-i', '1', |
| 3588 | '-j', out_file])) |
| 3589 | with open(out_file) as f: |
| 3590 | read = json.load(f) |
Aaron Gable | 0ffdf2d | 2017-06-05 13:01:17 -0700 | [diff] [blame] | 3591 | self.assertEqual(len(read), 2) |
| 3592 | self.assertEqual(read[0], { |
| 3593 | u'date': u'2017-03-16 20:00:41.000000', |
| 3594 | u'message': ( |
| 3595 | u'PTAL\n' + |
| 3596 | u'\n' + |
| 3597 | u'codereview.settings\n' + |
| 3598 | u' Base, Line 42: https://chromium-review.googlesource.com/' + |
| 3599 | u'c/1/2/codereview.settings#b42\n' + |
| 3600 | u' I removed this because it is bad\n'), |
| 3601 | u'approval': False, |
| 3602 | u'disapproval': False, |
| 3603 | u'sender': u'owner@example.com'}) |
| 3604 | self.assertEqual(read[1], { |
| 3605 | u'date': u'2017-03-17 05:19:37.500000', |
| 3606 | u'message': ( |
| 3607 | u'Patch Set 2: Code-Review+1\n' + |
| 3608 | u'\n' + |
| 3609 | u'/COMMIT_MSG\n' + |
| 3610 | u' PS2, File comment: https://chromium-review.googlesource.com/' + |
| 3611 | u'c/1/2//COMMIT_MSG#\n' + |
| 3612 | u' Please include a bug link\n'), |
| 3613 | u'approval': False, |
| 3614 | u'disapproval': False, |
| 3615 | u'sender': u'reviewer@example.com'}) |
Andrii Shyshkalov | 34924cd | 2017-03-15 17:08:32 +0100 | [diff] [blame] | 3616 | |
Quinten Yearsley | 0c62da9 | 2017-05-31 13:39:42 -0700 | [diff] [blame] | 3617 | |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 3618 | if __name__ == '__main__': |
Andrii Shyshkalov | 18df0cd | 2017-01-25 15:22:20 +0100 | [diff] [blame] | 3619 | logging.basicConfig( |
| 3620 | level=logging.DEBUG if '-v' in sys.argv else logging.ERROR) |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 3621 | unittest.main() |