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 |
tandrii | de281ae | 2016-10-12 06:02:30 -0700 | [diff] [blame] | 9 | import json |
Andrii Shyshkalov | 18df0cd | 2017-01-25 15:22:20 +0100 | [diff] [blame] | 10 | import logging |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 11 | import os |
maruel@chromium.org | a335365 | 2011-11-30 14:26:57 +0000 | [diff] [blame] | 12 | import StringIO |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 13 | import sys |
| 14 | import unittest |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 15 | import urlparse |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 16 | |
| 17 | sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 18 | |
| 19 | from testing_support.auto_stub import TestCase |
| 20 | |
| 21 | import git_cl |
iannucci@chromium.org | 9e84927 | 2014-04-04 00:31:55 +0000 | [diff] [blame] | 22 | import git_common |
tandrii@chromium.org | 57d8654 | 2016-03-04 16:11:32 +0000 | [diff] [blame] | 23 | import git_footers |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 24 | import subprocess2 |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 25 | |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 26 | def callError(code=1, cmd='', cwd='', stdout='', stderr=''): |
| 27 | return subprocess2.CalledProcessError(code, cmd, cwd, stdout, stderr) |
| 28 | |
| 29 | |
| 30 | CERR1 = callError(1) |
| 31 | |
| 32 | |
martiniss@chromium.org | d6648e2 | 2016-04-29 19:22:16 +0000 | [diff] [blame] | 33 | class ChangelistMock(object): |
| 34 | # A class variable so we can access it when we don't have access to the |
| 35 | # instance that's being set. |
| 36 | desc = "" |
| 37 | def __init__(self, **kwargs): |
| 38 | pass |
| 39 | def GetIssue(self): |
| 40 | return 1 |
| 41 | def GetDescription(self): |
| 42 | return ChangelistMock.desc |
dsansome | e2d6fd9 | 2016-09-08 00:10:47 -0700 | [diff] [blame] | 43 | def UpdateDescription(self, desc, force=False): |
martiniss@chromium.org | d6648e2 | 2016-04-29 19:22:16 +0000 | [diff] [blame] | 44 | ChangelistMock.desc = desc |
| 45 | |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 46 | |
maruel@chromium.org | 2e72bb1 | 2012-01-17 15:18:35 +0000 | [diff] [blame] | 47 | class PresubmitMock(object): |
| 48 | def __init__(self, *args, **kwargs): |
| 49 | self.reviewers = [] |
| 50 | @staticmethod |
| 51 | def should_continue(): |
| 52 | return True |
| 53 | |
| 54 | |
| 55 | class RietveldMock(object): |
| 56 | def __init__(self, *args, **kwargs): |
| 57 | pass |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 58 | |
maruel@chromium.org | 2e72bb1 | 2012-01-17 15:18:35 +0000 | [diff] [blame] | 59 | @staticmethod |
| 60 | def get_description(issue): |
| 61 | return 'Issue: %d' % issue |
| 62 | |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 63 | @staticmethod |
| 64 | def get_issue_properties(_issue, _messages): |
| 65 | return { |
| 66 | 'reviewers': ['joe@chromium.org', 'john@chromium.org'], |
| 67 | 'messages': [ |
| 68 | { |
| 69 | 'approval': True, |
| 70 | 'sender': 'john@chromium.org', |
| 71 | }, |
| 72 | ], |
Andrii Shyshkalov | 06a2502 | 2016-11-24 16:47:00 +0100 | [diff] [blame] | 73 | 'patchsets': [1, 20001], |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 74 | } |
| 75 | |
iannucci | e53c935 | 2016-08-17 14:40:40 -0700 | [diff] [blame] | 76 | @staticmethod |
| 77 | def close_issue(_issue): |
| 78 | return 'Closed' |
| 79 | |
skobes | 6468b90 | 2016-10-24 08:45:10 -0700 | [diff] [blame] | 80 | @staticmethod |
| 81 | def get_patch(issue, patchset): |
| 82 | return 'patch set from issue %s patchset %s' % (issue, patchset) |
| 83 | |
Andrii Shyshkalov | 06a2502 | 2016-11-24 16:47:00 +0100 | [diff] [blame] | 84 | @staticmethod |
| 85 | def update_description(_issue, _description): |
| 86 | return 'Updated' |
| 87 | |
| 88 | @staticmethod |
| 89 | def add_comment(_issue, _comment): |
| 90 | return 'Commented' |
| 91 | |
| 92 | |
| 93 | |
skobes | 6468b90 | 2016-10-24 08:45:10 -0700 | [diff] [blame] | 94 | |
| 95 | class GitCheckoutMock(object): |
| 96 | def __init__(self, *args, **kwargs): |
| 97 | pass |
| 98 | |
| 99 | @staticmethod |
| 100 | def reset(): |
| 101 | GitCheckoutMock.conflict = False |
| 102 | |
| 103 | def apply_patch(self, p): |
| 104 | if GitCheckoutMock.conflict: |
| 105 | raise Exception('failed') |
| 106 | |
maruel@chromium.org | 2e72bb1 | 2012-01-17 15:18:35 +0000 | [diff] [blame] | 107 | |
| 108 | class WatchlistsMock(object): |
| 109 | def __init__(self, _): |
| 110 | pass |
| 111 | @staticmethod |
| 112 | def GetWatchersForPaths(_): |
| 113 | return ['joe@example.com'] |
| 114 | |
| 115 | |
ukai@chromium.org | 78c4b98 | 2012-02-14 02:20:26 +0000 | [diff] [blame] | 116 | class CodereviewSettingsFileMock(object): |
| 117 | def __init__(self): |
| 118 | pass |
Quinten Yearsley | b2cc4a9 | 2016-12-15 13:53:26 -0800 | [diff] [blame] | 119 | # pylint: disable=no-self-use |
ukai@chromium.org | 78c4b98 | 2012-02-14 02:20:26 +0000 | [diff] [blame] | 120 | def read(self): |
| 121 | return ("CODE_REVIEW_SERVER: gerrit.chromium.org\n" + |
andybons@chromium.org | 11f46eb | 2016-02-02 19:26:51 +0000 | [diff] [blame] | 122 | "GERRIT_HOST: True\n") |
ukai@chromium.org | 78c4b98 | 2012-02-14 02:20:26 +0000 | [diff] [blame] | 123 | |
| 124 | |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 125 | class AuthenticatorMock(object): |
| 126 | def __init__(self, *_args): |
| 127 | pass |
| 128 | def has_cached_credentials(self): |
| 129 | return True |
tandrii | 221ab25 | 2016-10-06 08:12:04 -0700 | [diff] [blame] | 130 | def authorize(self, http): |
| 131 | return http |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 132 | |
| 133 | |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 134 | def CookiesAuthenticatorMockFactory(hosts_with_creds=None, same_cookie=False): |
| 135 | """Use to mock Gerrit/Git credentials from ~/.netrc or ~/.gitcookies. |
| 136 | |
| 137 | Usage: |
| 138 | >>> self.mock(git_cl.gerrit_util, "CookiesAuthenticator", |
| 139 | CookiesAuthenticatorMockFactory({'host1': 'cookie1'})) |
| 140 | |
| 141 | OR |
| 142 | >>> self.mock(git_cl.gerrit_util, "CookiesAuthenticator", |
| 143 | CookiesAuthenticatorMockFactory(cookie='cookie')) |
| 144 | """ |
| 145 | class CookiesAuthenticatorMock(git_cl.gerrit_util.CookiesAuthenticator): |
Quinten Yearsley | b2cc4a9 | 2016-12-15 13:53:26 -0800 | [diff] [blame] | 146 | def __init__(self): # pylint: disable=super-init-not-called |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 147 | # Intentionally not calling super() because it reads actual cookie files. |
| 148 | pass |
| 149 | @classmethod |
| 150 | def get_gitcookies_path(cls): |
| 151 | return '~/.gitcookies' |
| 152 | @classmethod |
| 153 | def get_netrc_path(cls): |
| 154 | return '~/.netrc' |
| 155 | def get_auth_header(self, host): |
| 156 | if same_cookie: |
| 157 | return same_cookie |
| 158 | return (hosts_with_creds or {}).get(host) |
| 159 | return CookiesAuthenticatorMock |
| 160 | |
kmarshall | 9249e01 | 2016-08-23 12:02:16 -0700 | [diff] [blame] | 161 | class MockChangelistWithBranchAndIssue(): |
| 162 | def __init__(self, branch, issue): |
| 163 | self.branch = branch |
| 164 | self.issue = issue |
| 165 | def GetBranch(self): |
| 166 | return self.branch |
| 167 | def GetIssue(self): |
| 168 | return self.issue |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 169 | |
tandrii | c2405f5 | 2016-10-10 08:13:15 -0700 | [diff] [blame] | 170 | |
| 171 | class SystemExitMock(Exception): |
| 172 | pass |
| 173 | |
| 174 | |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 175 | class TestGitClBasic(unittest.TestCase): |
| 176 | def _test_ParseIssueUrl(self, func, url, issue, patchset, hostname, fail): |
| 177 | parsed = urlparse.urlparse(url) |
| 178 | result = func(parsed) |
| 179 | if fail: |
| 180 | self.assertIsNone(result) |
| 181 | return None |
| 182 | self.assertIsNotNone(result) |
| 183 | self.assertEqual(result.issue, issue) |
| 184 | self.assertEqual(result.patchset, patchset) |
| 185 | self.assertEqual(result.hostname, hostname) |
| 186 | return result |
| 187 | |
| 188 | def test_ParseIssueURL_rietveld(self): |
skobes | 6468b90 | 2016-10-24 08:45:10 -0700 | [diff] [blame] | 189 | def test(url, issue=None, patchset=None, hostname=None, fail=None): |
| 190 | self._test_ParseIssueUrl( |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 191 | git_cl._RietveldChangelistImpl.ParseIssueURL, |
| 192 | url, issue, patchset, hostname, fail) |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 193 | |
| 194 | test('http://codereview.chromium.org/123', |
| 195 | 123, None, 'codereview.chromium.org') |
| 196 | test('https://codereview.chromium.org/123', |
| 197 | 123, None, 'codereview.chromium.org') |
| 198 | test('https://codereview.chromium.org/123/', |
| 199 | 123, None, 'codereview.chromium.org') |
| 200 | test('https://codereview.chromium.org/123/whatever', |
| 201 | 123, None, 'codereview.chromium.org') |
wychen | 3c1c172 | 2016-08-04 11:46:36 -0700 | [diff] [blame] | 202 | test('https://codereview.chromium.org/123/#ps20001', |
| 203 | 123, 20001, 'codereview.chromium.org') |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 204 | test('http://codereview.chromium.org/download/issue123_4.diff', |
skobes | 6468b90 | 2016-10-24 08:45:10 -0700 | [diff] [blame] | 205 | 123, 4, 'codereview.chromium.org') |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 206 | # This looks like bad Gerrit, but is actually valid Rietveld. |
| 207 | test('https://chrome-review.source.com/123/4/', |
| 208 | 123, None, 'chrome-review.source.com') |
| 209 | |
| 210 | test('https://codereview.chromium.org/deadbeaf', fail=True) |
| 211 | test('https://codereview.chromium.org/api/123', fail=True) |
| 212 | test('bad://codereview.chromium.org/123', fail=True) |
| 213 | test('http://codereview.chromium.org/download/issue123_4.diffff', fail=True) |
| 214 | |
| 215 | def test_ParseIssueURL_gerrit(self): |
| 216 | def test(url, issue=None, patchset=None, hostname=None, fail=None): |
| 217 | self._test_ParseIssueUrl( |
| 218 | git_cl._GerritChangelistImpl.ParseIssueURL, |
| 219 | url, issue, patchset, hostname, fail) |
| 220 | |
| 221 | test('http://chrome-review.source.com/c/123', |
| 222 | 123, None, 'chrome-review.source.com') |
| 223 | test('https://chrome-review.source.com/c/123/', |
| 224 | 123, None, 'chrome-review.source.com') |
| 225 | test('https://chrome-review.source.com/c/123/4', |
| 226 | 123, 4, 'chrome-review.source.com') |
| 227 | test('https://chrome-review.source.com/#/c/123/4', |
| 228 | 123, 4, 'chrome-review.source.com') |
| 229 | test('https://chrome-review.source.com/c/123/4', |
| 230 | 123, 4, 'chrome-review.source.com') |
| 231 | test('https://chrome-review.source.com/123', |
| 232 | 123, None, 'chrome-review.source.com') |
| 233 | test('https://chrome-review.source.com/123/4', |
| 234 | 123, 4, 'chrome-review.source.com') |
| 235 | |
| 236 | test('https://chrome-review.source.com/c/123/1/whatisthis', fail=True) |
| 237 | test('https://chrome-review.source.com/c/abc/', fail=True) |
| 238 | test('ssh://chrome-review.source.com/c/123/1/', fail=True) |
| 239 | |
| 240 | def test_ParseIssueNumberArgument(self): |
| 241 | def test(arg, issue=None, patchset=None, hostname=None, fail=False): |
| 242 | result = git_cl.ParseIssueNumberArgument(arg) |
| 243 | self.assertIsNotNone(result) |
| 244 | if fail: |
| 245 | self.assertFalse(result.valid) |
| 246 | else: |
| 247 | self.assertEqual(result.issue, issue) |
| 248 | self.assertEqual(result.patchset, patchset) |
| 249 | self.assertEqual(result.hostname, hostname) |
| 250 | |
| 251 | test('123', 123) |
| 252 | test('', fail=True) |
| 253 | test('abc', fail=True) |
| 254 | test('123/1', fail=True) |
| 255 | test('123a', fail=True) |
| 256 | test('ssh://chrome-review.source.com/#/c/123/4/', fail=True) |
| 257 | # Rietveld. |
| 258 | test('https://codereview.source.com/123', |
| 259 | 123, None, 'codereview.source.com') |
| 260 | test('https://codereview.source.com/www123', fail=True) |
| 261 | # Gerrrit. |
| 262 | test('https://chrome-review.source.com/c/123/4', |
| 263 | 123, 4, 'chrome-review.source.com') |
| 264 | test('https://chrome-review.source.com/bad/123/4', fail=True) |
| 265 | |
tandrii | f9aefb7 | 2016-07-01 09:06:51 -0700 | [diff] [blame] | 266 | def test_get_bug_line_values(self): |
| 267 | f = lambda p, bugs: list(git_cl._get_bug_line_values(p, bugs)) |
| 268 | self.assertEqual(f('', ''), []) |
| 269 | self.assertEqual(f('', '123,v8:456'), ['123', 'v8:456']) |
| 270 | self.assertEqual(f('v8', '456'), ['v8:456']) |
| 271 | self.assertEqual(f('v8', 'chromium:123,456'), ['v8:456', 'chromium:123']) |
| 272 | # Not nice, but not worth carying. |
| 273 | self.assertEqual(f('v8', 'chromium:123,456,v8:123'), |
| 274 | ['v8:456', 'chromium:123', 'v8:123']) |
| 275 | |
Andrii Shyshkalov | 15e50cc | 2016-12-02 14:34:08 +0100 | [diff] [blame] | 276 | def _test_git_number(self, parent_msg, dest_ref, child_msg, |
| 277 | parent_hash='parenthash'): |
| 278 | desc = git_cl.ChangeDescription(child_msg) |
| 279 | desc.update_with_git_number_footers(parent_hash, parent_msg, dest_ref) |
| 280 | return desc.description |
| 281 | |
| 282 | def assertEqualByLine(self, actual, expected): |
| 283 | self.assertEqual(actual.splitlines(), expected.splitlines()) |
| 284 | |
| 285 | def test_git_number_bad_parent(self): |
| 286 | with self.assertRaises(ValueError): |
| 287 | self._test_git_number('Parent', 'refs/heads/master', 'Child') |
| 288 | |
| 289 | def test_git_number_bad_parent_footer(self): |
| 290 | with self.assertRaises(AssertionError): |
| 291 | self._test_git_number( |
| 292 | 'Parent\n' |
| 293 | '\n' |
| 294 | 'Cr-Commit-Position: wrong', |
| 295 | 'refs/heads/master', 'Child') |
| 296 | |
| 297 | def test_git_number_bad_lineage_ignored(self): |
| 298 | actual = self._test_git_number( |
| 299 | 'Parent\n' |
| 300 | '\n' |
| 301 | 'Cr-Commit-Position: refs/heads/master@{#1}\n' |
| 302 | 'Cr-Branched-From: mustBeReal40CharHash-branch@{#pos}', |
| 303 | 'refs/heads/master', 'Child') |
| 304 | self.assertEqualByLine( |
| 305 | actual, |
| 306 | 'Child\n' |
| 307 | '\n' |
| 308 | 'Cr-Commit-Position: refs/heads/master@{#2}\n' |
| 309 | 'Cr-Branched-From: mustBeReal40CharHash-branch@{#pos}') |
| 310 | |
| 311 | def test_git_number_same_branch(self): |
| 312 | actual = self._test_git_number( |
| 313 | 'Parent\n' |
| 314 | '\n' |
| 315 | 'Cr-Commit-Position: refs/heads/master@{#12}', |
| 316 | dest_ref='refs/heads/master', |
| 317 | child_msg='Child') |
| 318 | self.assertEqualByLine( |
| 319 | actual, |
| 320 | 'Child\n' |
| 321 | '\n' |
| 322 | 'Cr-Commit-Position: refs/heads/master@{#13}') |
| 323 | |
| 324 | def test_git_number_same_branch_with_originals(self): |
| 325 | actual = self._test_git_number( |
| 326 | 'Parent\n' |
| 327 | '\n' |
| 328 | 'Cr-Commit-Position: refs/heads/master@{#12}', |
| 329 | dest_ref='refs/heads/master', |
| 330 | child_msg='Child\n' |
| 331 | '\n' |
| 332 | 'Some users are smart and insert their own footers\n' |
| 333 | '\n' |
| 334 | 'Cr-Whatever: value\n' |
| 335 | 'Cr-Commit-Position: refs/copy/paste@{#22}') |
| 336 | self.assertEqualByLine( |
| 337 | actual, |
| 338 | 'Child\n' |
| 339 | '\n' |
| 340 | 'Some users are smart and insert their own footers\n' |
| 341 | '\n' |
| 342 | 'Cr-Original-Whatever: value\n' |
| 343 | 'Cr-Original-Commit-Position: refs/copy/paste@{#22}\n' |
| 344 | 'Cr-Commit-Position: refs/heads/master@{#13}') |
| 345 | |
| 346 | def test_git_number_new_branch(self): |
| 347 | actual = self._test_git_number( |
| 348 | 'Parent\n' |
| 349 | '\n' |
| 350 | 'Cr-Commit-Position: refs/heads/master@{#12}', |
| 351 | dest_ref='refs/heads/branch', |
| 352 | child_msg='Child') |
| 353 | self.assertEqualByLine( |
| 354 | actual, |
| 355 | 'Child\n' |
| 356 | '\n' |
| 357 | 'Cr-Commit-Position: refs/heads/branch@{#1}\n' |
| 358 | 'Cr-Branched-From: parenthash-refs/heads/master@{#12}') |
| 359 | |
| 360 | def test_git_number_lineage(self): |
| 361 | actual = self._test_git_number( |
| 362 | 'Parent\n' |
| 363 | '\n' |
| 364 | 'Cr-Commit-Position: refs/heads/branch@{#1}\n' |
| 365 | 'Cr-Branched-From: somehash-refs/heads/master@{#12}', |
| 366 | dest_ref='refs/heads/branch', |
| 367 | child_msg='Child') |
| 368 | self.assertEqualByLine( |
| 369 | actual, |
| 370 | 'Child\n' |
| 371 | '\n' |
| 372 | 'Cr-Commit-Position: refs/heads/branch@{#2}\n' |
| 373 | 'Cr-Branched-From: somehash-refs/heads/master@{#12}') |
| 374 | |
| 375 | def test_git_number_moooooooore_lineage(self): |
| 376 | actual = self._test_git_number( |
| 377 | 'Parent\n' |
| 378 | '\n' |
| 379 | 'Cr-Commit-Position: refs/heads/branch@{#5}\n' |
| 380 | 'Cr-Branched-From: somehash-refs/heads/master@{#12}', |
| 381 | dest_ref='refs/heads/mooore', |
| 382 | child_msg='Child') |
| 383 | self.assertEqualByLine( |
| 384 | actual, |
| 385 | 'Child\n' |
| 386 | '\n' |
| 387 | 'Cr-Commit-Position: refs/heads/mooore@{#1}\n' |
| 388 | 'Cr-Branched-From: parenthash-refs/heads/branch@{#5}\n' |
| 389 | 'Cr-Branched-From: somehash-refs/heads/master@{#12}') |
| 390 | |
Andrii Shyshkalov | b5effa1 | 2016-12-14 19:35:12 +0100 | [diff] [blame] | 391 | def test_git_number_ever_moooooooore_lineage(self): |
| 392 | self.maxDiff = 10000 |
| 393 | actual = self._test_git_number( |
| 394 | 'CQ commit on fresh new branch + numbering.\n' |
| 395 | '\n' |
| 396 | 'NOTRY=True\n' |
| 397 | 'NOPRESUBMIT=True\n' |
| 398 | 'BUG=\n' |
| 399 | '\n' |
| 400 | 'Review-Url: https://codereview.chromium.org/2577703003\n' |
| 401 | 'Cr-Commit-Position: refs/heads/gnumb-test/br@{#1}\n' |
| 402 | 'Cr-Branched-From: 0749ff9edc-refs/heads/gnumb-test/cq@{#4}\n' |
| 403 | 'Cr-Branched-From: 5c49df2da6-refs/heads/master@{#41618}', |
| 404 | dest_ref='refs/heads/gnumb-test/cl', |
| 405 | child_msg='git cl on fresh new branch + numbering.\n' |
| 406 | '\n' |
| 407 | 'Review-Url: https://codereview.chromium.org/2575043003 .\n') |
| 408 | self.assertEqualByLine( |
| 409 | actual, |
| 410 | 'git cl on fresh new branch + numbering.\n' |
| 411 | '\n' |
| 412 | 'Review-Url: https://codereview.chromium.org/2575043003 .\n' |
| 413 | 'Cr-Commit-Position: refs/heads/gnumb-test/cl@{#1}\n' |
| 414 | 'Cr-Branched-From: parenthash-refs/heads/gnumb-test/br@{#1}\n' |
| 415 | 'Cr-Branched-From: 0749ff9edc-refs/heads/gnumb-test/cq@{#4}\n' |
| 416 | 'Cr-Branched-From: 5c49df2da6-refs/heads/master@{#41618}') |
Andrii Shyshkalov | 15e50cc | 2016-12-02 14:34:08 +0100 | [diff] [blame] | 417 | |
| 418 | def test_git_number_cherry_pick(self): |
| 419 | actual = self._test_git_number( |
| 420 | 'Parent\n' |
| 421 | '\n' |
| 422 | 'Cr-Commit-Position: refs/heads/branch@{#1}\n' |
| 423 | 'Cr-Branched-From: somehash-refs/heads/master@{#12}', |
| 424 | dest_ref='refs/heads/branch', |
| 425 | child_msg='Child, which is cherry-pick from master\n' |
| 426 | '\n' |
| 427 | 'Cr-Commit-Position: refs/heads/master@{#100}\n' |
| 428 | '(cherry picked from commit deadbeef12345678deadbeef12345678deadbeef)') |
| 429 | self.assertEqualByLine( |
| 430 | actual, |
| 431 | 'Child, which is cherry-pick from master\n' |
| 432 | '\n' |
| 433 | '(cherry picked from commit deadbeef12345678deadbeef12345678deadbeef)\n' |
| 434 | '\n' |
| 435 | 'Cr-Original-Commit-Position: refs/heads/master@{#100}\n' |
| 436 | 'Cr-Commit-Position: refs/heads/branch@{#2}\n' |
| 437 | 'Cr-Branched-From: somehash-refs/heads/master@{#12}') |
| 438 | |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 439 | |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 440 | class TestGitCl(TestCase): |
| 441 | def setUp(self): |
| 442 | super(TestGitCl, self).setUp() |
| 443 | self.calls = [] |
tandrii | 9d20675 | 2016-06-20 11:32:47 -0700 | [diff] [blame] | 444 | self._calls_done = [] |
maruel@chromium.org | 2e72bb1 | 2012-01-17 15:18:35 +0000 | [diff] [blame] | 445 | self.mock(subprocess2, 'call', self._mocked_call) |
| 446 | self.mock(subprocess2, 'check_call', self._mocked_call) |
| 447 | self.mock(subprocess2, 'check_output', self._mocked_call) |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 448 | self.mock(subprocess2, 'communicate', |
| 449 | lambda *a, **kw: ([self._mocked_call(*a, **kw), ''], 0)) |
tandrii@chromium.org | a342c92 | 2016-03-16 07:08:25 +0000 | [diff] [blame] | 450 | self.mock(git_cl.gclient_utils, 'CheckCallAndFilter', self._mocked_call) |
sbc@chromium.org | 71437c0 | 2015-04-09 19:29:40 +0000 | [diff] [blame] | 451 | self.mock(git_common, 'is_dirty_git_tree', lambda x: False) |
iannucci@chromium.org | 9e84927 | 2014-04-04 00:31:55 +0000 | [diff] [blame] | 452 | self.mock(git_common, 'get_or_create_merge_base', |
| 453 | lambda *a: ( |
| 454 | self._mocked_call(['get_or_create_merge_base']+list(a)))) |
pgervais@chromium.org | 8ba38ff | 2015-06-11 21:41:25 +0000 | [diff] [blame] | 455 | self.mock(git_cl, 'BranchExists', lambda _: True) |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 456 | self.mock(git_cl, 'FindCodereviewSettingsFile', lambda: '') |
maruel@chromium.org | 2e72bb1 | 2012-01-17 15:18:35 +0000 | [diff] [blame] | 457 | self.mock(git_cl, 'ask_for_data', self._mocked_call) |
phajdan.jr | e328cf9 | 2016-08-22 04:12:17 -0700 | [diff] [blame] | 458 | self.mock(git_cl, 'write_json', lambda path, contents: |
| 459 | self._mocked_call('write_json', path, contents)) |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 460 | self.mock(git_cl.presubmit_support, 'DoPresubmitChecks', PresubmitMock) |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 461 | self.mock(git_cl.rietveld, 'Rietveld', RietveldMock) |
maruel@chromium.org | 4bac4b5 | 2012-11-27 20:33:52 +0000 | [diff] [blame] | 462 | self.mock(git_cl.rietveld, 'CachingRietveld', RietveldMock) |
skobes | 6468b90 | 2016-10-24 08:45:10 -0700 | [diff] [blame] | 463 | self.mock(git_cl.checkout, 'GitCheckout', GitCheckoutMock) |
| 464 | GitCheckoutMock.reset() |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 465 | self.mock(git_cl.upload, 'RealMain', self.fail) |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 466 | self.mock(git_cl.watchlists, 'Watchlists', WatchlistsMock) |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 467 | self.mock(git_cl.auth, 'get_authenticator_for_host', AuthenticatorMock) |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 468 | self.mock(git_cl.gerrit_util, 'GetChangeDetail', |
| 469 | lambda *args, **kwargs: self._mocked_call( |
| 470 | 'GetChangeDetail', *args, **kwargs)) |
| 471 | self.mock(git_cl.gerrit_util, 'AddReviewers', |
| 472 | lambda h, i, add, is_reviewer, notify: self._mocked_call( |
| 473 | 'AddReviewers', h, i, add, is_reviewer, notify)) |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 474 | self.mock(git_cl.gerrit_util.GceAuthenticator, 'is_gce', |
| 475 | classmethod(lambda _: False)) |
tandrii | c2405f5 | 2016-10-10 08:13:15 -0700 | [diff] [blame] | 476 | self.mock(git_cl, 'DieWithError', |
Christopher Lam | f732cd5 | 2017-01-24 12:40:11 +1100 | [diff] [blame] | 477 | lambda msg, change=None: self._mocked_call(['DieWithError', msg])) |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 478 | # It's important to reset settings to not have inter-tests interference. |
| 479 | git_cl.settings = None |
| 480 | |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 481 | |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 482 | def tearDown(self): |
wychen@chromium.org | 445c896 | 2015-04-28 23:30:05 +0000 | [diff] [blame] | 483 | try: |
Andrii Shyshkalov | 18df0cd | 2017-01-25 15:22:20 +0100 | [diff] [blame] | 484 | self.assertEquals([], self.calls) |
| 485 | except AssertionError: |
wychen@chromium.org | 445c896 | 2015-04-28 23:30:05 +0000 | [diff] [blame] | 486 | if not self.has_failed(): |
Andrii Shyshkalov | 18df0cd | 2017-01-25 15:22:20 +0100 | [diff] [blame] | 487 | raise |
| 488 | # Sadly, has_failed() returns True if this OR any other tests before this |
| 489 | # one have failed. |
| 490 | git_cl.logging.exception( |
| 491 | '\nIF YOU SEE THIS, READ BELOW, IT WILL SAVE YOUR TIME!\n' |
| 492 | 'There are un-consumed self.calls after this test has finished.\n' |
| 493 | 'If you don\'t know which test this is, run:\n' |
| 494 | ' tests/git_cl_tests.py -v\n' |
| 495 | '\n' |
| 496 | 'If you are already running just this single test, then **first** ' |
| 497 | 'fix the problem whose exception is emitted below by unittest ' |
| 498 | 'runner.\n' |
| 499 | '\n' |
| 500 | 'Else, to be sure what\'s going on, run this test **alone** with \n' |
| 501 | ' tests/git_cl_tests.py TestGitCl.<name>\n' |
| 502 | 'and follow instructions above.\n' + |
| 503 | '=' * 80) |
wychen@chromium.org | 445c896 | 2015-04-28 23:30:05 +0000 | [diff] [blame] | 504 | finally: |
| 505 | super(TestGitCl, self).tearDown() |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 506 | |
iannucci@chromium.org | 9e84927 | 2014-04-04 00:31:55 +0000 | [diff] [blame] | 507 | def _mocked_call(self, *args, **_kwargs): |
maruel@chromium.org | 2e72bb1 | 2012-01-17 15:18:35 +0000 | [diff] [blame] | 508 | self.assertTrue( |
| 509 | self.calls, |
tandrii | 9d20675 | 2016-06-20 11:32:47 -0700 | [diff] [blame] | 510 | '@%d Expected: <Missing> Actual: %r' % (len(self._calls_done), args)) |
wychen@chromium.org | a872e75 | 2015-04-28 23:42:18 +0000 | [diff] [blame] | 511 | top = self.calls.pop(0) |
wychen@chromium.org | a872e75 | 2015-04-28 23:42:18 +0000 | [diff] [blame] | 512 | expected_args, result = top |
| 513 | |
maruel@chromium.org | e52678e | 2013-04-26 18:34:44 +0000 | [diff] [blame] | 514 | # Also logs otherwise it could get caught in a try/finally and be hard to |
| 515 | # diagnose. |
| 516 | if expected_args != args: |
tandrii | 9d20675 | 2016-06-20 11:32:47 -0700 | [diff] [blame] | 517 | N = 5 |
| 518 | prior_calls = '\n '.join( |
| 519 | '@%d: %r' % (len(self._calls_done) - N + i, c[0]) |
| 520 | for i, c in enumerate(self._calls_done[-N:])) |
| 521 | following_calls = '\n '.join( |
| 522 | '@%d: %r' % (len(self._calls_done) + i + 1, c[0]) |
| 523 | for i, c in enumerate(self.calls[:N])) |
| 524 | extended_msg = ( |
| 525 | 'A few prior calls:\n %s\n\n' |
| 526 | 'This (expected):\n @%d: %r\n' |
| 527 | 'This (actual):\n @%d: %r\n\n' |
| 528 | 'A few following expected calls:\n %s' % |
| 529 | (prior_calls, len(self._calls_done), expected_args, |
| 530 | len(self._calls_done), args, following_calls)) |
| 531 | git_cl.logging.error(extended_msg) |
| 532 | |
tandrii | 99a72f2 | 2016-08-17 14:33:24 -0700 | [diff] [blame] | 533 | self.fail('@%d\n' |
| 534 | ' Expected: %r\n' |
| 535 | ' Actual: %r' % ( |
tandrii | 9d20675 | 2016-06-20 11:32:47 -0700 | [diff] [blame] | 536 | len(self._calls_done), expected_args, args)) |
| 537 | |
| 538 | self._calls_done.append(top) |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 539 | if isinstance(result, Exception): |
| 540 | raise result |
maruel@chromium.org | 2e72bb1 | 2012-01-17 15:18:35 +0000 | [diff] [blame] | 541 | return result |
| 542 | |
tandrii | 48df581 | 2016-10-17 03:55:37 -0700 | [diff] [blame] | 543 | def test_LoadCodereviewSettingsFromFile_gerrit(self): |
| 544 | codereview_file = StringIO.StringIO('GERRIT_HOST: true') |
| 545 | self.calls = [ |
| 546 | ((['git', 'config', '--unset-all', 'rietveld.cc'],), CERR1), |
| 547 | ((['git', 'config', '--unset-all', 'rietveld.private'],), CERR1), |
| 548 | ((['git', 'config', '--unset-all', 'rietveld.tree-status-url'],), CERR1), |
| 549 | ((['git', 'config', '--unset-all', 'rietveld.viewvc-url'],), CERR1), |
| 550 | ((['git', 'config', '--unset-all', 'rietveld.bug-prefix'],), CERR1), |
| 551 | ((['git', 'config', '--unset-all', 'rietveld.cpplint-regex'],), CERR1), |
tandrii | 48df581 | 2016-10-17 03:55:37 -0700 | [diff] [blame] | 552 | ((['git', 'config', '--unset-all', 'rietveld.cpplint-ignore-regex'],), |
| 553 | CERR1), |
| 554 | ((['git', 'config', '--unset-all', 'rietveld.project'],), CERR1), |
tandrii | 48df581 | 2016-10-17 03:55:37 -0700 | [diff] [blame] | 555 | ((['git', 'config', '--unset-all', 'rietveld.run-post-upload-hook'],), |
| 556 | CERR1), |
| 557 | ((['git', 'config', 'gerrit.host', 'true'],), ''), |
| 558 | ] |
| 559 | self.assertIsNone(git_cl.LoadCodereviewSettingsFromFile(codereview_file)) |
| 560 | |
maruel@chromium.org | a335365 | 2011-11-30 14:26:57 +0000 | [diff] [blame] | 561 | @classmethod |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 562 | def _is_gerrit_calls(cls, gerrit=False): |
| 563 | return [((['git', 'config', 'rietveld.autoupdate'],), ''), |
| 564 | ((['git', 'config', 'gerrit.host'],), 'True' if gerrit else '')] |
| 565 | |
| 566 | @classmethod |
tyoshino@chromium.org | c1737d0 | 2013-05-29 14:17:28 +0000 | [diff] [blame] | 567 | def _upload_calls(cls, similarity, find_copies, private): |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 568 | return (cls._git_base_calls(similarity, find_copies) + |
tyoshino@chromium.org | c1737d0 | 2013-05-29 14:17:28 +0000 | [diff] [blame] | 569 | cls._git_upload_calls(private)) |
maruel@chromium.org | a335365 | 2011-11-30 14:26:57 +0000 | [diff] [blame] | 570 | |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 571 | @classmethod |
jbroman@chromium.org | 615a262 | 2013-05-03 13:20:14 +0000 | [diff] [blame] | 572 | def _upload_no_rev_calls(cls, similarity, find_copies): |
| 573 | return (cls._git_base_calls(similarity, find_copies) + |
| 574 | cls._git_upload_no_rev_calls()) |
| 575 | |
| 576 | @classmethod |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 577 | def _git_base_calls(cls, similarity, find_copies): |
iannucci@chromium.org | 53937ba | 2012-10-02 18:20:43 +0000 | [diff] [blame] | 578 | if similarity is None: |
| 579 | similarity = '50' |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 580 | similarity_call = ((['git', 'config', |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 581 | 'branch.master.git-cl-similarity'],), CERR1) |
iannucci@chromium.org | 53937ba | 2012-10-02 18:20:43 +0000 | [diff] [blame] | 582 | else: |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 583 | similarity_call = ((['git', 'config', |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 584 | 'branch.master.git-cl-similarity', similarity],), '') |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 585 | |
| 586 | if find_copies is None: |
| 587 | find_copies = True |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 588 | find_copies_call = ((['git', 'config', '--bool', |
| 589 | 'branch.master.git-find-copies'],), CERR1) |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 590 | else: |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 591 | val = str(find_copies).lower() |
| 592 | find_copies_call = ((['git', 'config', '--bool', |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 593 | 'branch.master.git-find-copies', val],), '') |
| 594 | |
| 595 | if find_copies: |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 596 | stat_call = ((['git', 'diff', '--no-ext-diff', '--stat', |
scottmg | b84b5e3 | 2016-11-10 09:25:33 -0800 | [diff] [blame] | 597 | '-l100000', '-C'+similarity, |
| 598 | 'fake_ancestor_sha', 'HEAD'],), '+dat') |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 599 | else: |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 600 | stat_call = ((['git', 'diff', '--no-ext-diff', '--stat', |
sbc@chromium.org | 5e07e06 | 2013-02-28 23:55:44 +0000 | [diff] [blame] | 601 | '-M'+similarity, 'fake_ancestor_sha', 'HEAD'],), '+dat') |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 602 | |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 603 | return [ |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 604 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
tandrii@chromium.org | 87985d2 | 2016-03-24 17:33:33 +0000 | [diff] [blame] | 605 | similarity_call, |
| 606 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
| 607 | find_copies_call, |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 608 | ] + cls._is_gerrit_calls() + [ |
tandrii@chromium.org | 87985d2 | 2016-03-24 17:33:33 +0000 | [diff] [blame] | 609 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 610 | ((['git', 'config', 'branch.master.rietveldissue'],), CERR1), |
| 611 | ((['git', 'config', 'branch.master.gerritissue'],), CERR1), |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 612 | ((['git', 'config', 'rietveld.server'],), |
| 613 | 'codereview.example.com'), |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 614 | ((['git', 'config', 'branch.master.merge'],), 'master'), |
| 615 | ((['git', 'config', 'branch.master.remote'],), 'origin'), |
iannucci@chromium.org | 9e84927 | 2014-04-04 00:31:55 +0000 | [diff] [blame] | 616 | ((['get_or_create_merge_base', 'master', 'master'],), |
bratell@opera.com | f267b0e | 2013-05-02 09:11:43 +0000 | [diff] [blame] | 617 | 'fake_ancestor_sha'), |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 618 | ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [ |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 619 | ((['git', 'rev-parse', '--show-cdup'],), ''), |
| 620 | ((['git', 'rev-parse', 'HEAD'],), '12345'), |
| 621 | ((['git', 'diff', '--name-status', '--no-renames', '-r', |
bratell@opera.com | f267b0e | 2013-05-02 09:11:43 +0000 | [diff] [blame] | 622 | 'fake_ancestor_sha...', '.'],), |
maruel@chromium.org | 2e72bb1 | 2012-01-17 15:18:35 +0000 | [diff] [blame] | 623 | 'M\t.gitignore\n'), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 624 | ((['git', 'config', 'branch.master.rietveldpatchset'],), CERR1), |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 625 | ((['git', 'log', '--pretty=format:%s%n%n%b', |
bratell@opera.com | f267b0e | 2013-05-02 09:11:43 +0000 | [diff] [blame] | 626 | 'fake_ancestor_sha...'],), |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 627 | 'foo'), |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 628 | ((['git', 'config', 'user.email'],), 'me@example.com'), |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 629 | stat_call, |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 630 | ((['git', 'log', '--pretty=format:%s\n\n%b', |
bratell@opera.com | f267b0e | 2013-05-02 09:11:43 +0000 | [diff] [blame] | 631 | 'fake_ancestor_sha..HEAD'],), |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 632 | 'desc\n'), |
rmistry@google.com | 9075258 | 2014-01-14 21:04:50 +0000 | [diff] [blame] | 633 | ((['git', 'config', 'rietveld.bug-prefix'],), ''), |
maruel@chromium.org | a335365 | 2011-11-30 14:26:57 +0000 | [diff] [blame] | 634 | ] |
| 635 | |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 636 | @classmethod |
jbroman@chromium.org | 615a262 | 2013-05-03 13:20:14 +0000 | [diff] [blame] | 637 | def _git_upload_no_rev_calls(cls): |
| 638 | return [ |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 639 | ((['git', 'config', 'core.editor'],), ''), |
jbroman@chromium.org | 615a262 | 2013-05-03 13:20:14 +0000 | [diff] [blame] | 640 | ] |
| 641 | |
| 642 | @classmethod |
tyoshino@chromium.org | c1737d0 | 2013-05-29 14:17:28 +0000 | [diff] [blame] | 643 | def _git_upload_calls(cls, private): |
| 644 | if private: |
tyoshino@chromium.org | 99918ab | 2013-09-30 06:17:28 +0000 | [diff] [blame] | 645 | cc_call = [] |
tyoshino@chromium.org | c1737d0 | 2013-05-29 14:17:28 +0000 | [diff] [blame] | 646 | private_call = [] |
| 647 | else: |
tyoshino@chromium.org | 99918ab | 2013-09-30 06:17:28 +0000 | [diff] [blame] | 648 | cc_call = [((['git', 'config', 'rietveld.cc'],), '')] |
tyoshino@chromium.org | c1737d0 | 2013-05-29 14:17:28 +0000 | [diff] [blame] | 649 | private_call = [ |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 650 | ((['git', 'config', 'rietveld.private'],), '')] |
tyoshino@chromium.org | c1737d0 | 2013-05-29 14:17:28 +0000 | [diff] [blame] | 651 | |
maruel@chromium.org | a335365 | 2011-11-30 14:26:57 +0000 | [diff] [blame] | 652 | return [ |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 653 | ((['git', 'config', 'core.editor'],), ''), |
tyoshino@chromium.org | 99918ab | 2013-09-30 06:17:28 +0000 | [diff] [blame] | 654 | ] + cc_call + private_call + [ |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 655 | ((['git', 'config', 'branch.master.base-url'],), ''), |
Aaron Gable | 1bc7bfe | 2016-12-19 10:08:14 -0800 | [diff] [blame] | 656 | ((['git', 'config', 'remote.origin.url'],), ''), |
sheyang@chromium.org | 152cf83 | 2014-06-11 21:37:49 +0000 | [diff] [blame] | 657 | ((['git', 'config', 'rietveld.project'],), ''), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 658 | ((['git', 'config', 'branch.master.rietveldissue', '1'],), ''), |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 659 | ((['git', 'config', 'branch.master.rietveldserver', |
tyoshino@chromium.org | c1737d0 | 2013-05-29 14:17:28 +0000 | [diff] [blame] | 660 | 'https://codereview.example.com'],), ''), |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 661 | ((['git', |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 662 | 'config', 'branch.master.rietveldpatchset', '2'],), ''), |
tandrii@chromium.org | 1e67bb7 | 2016-02-11 12:15:49 +0000 | [diff] [blame] | 663 | ] + cls._git_post_upload_calls() |
| 664 | |
| 665 | @classmethod |
| 666 | def _git_post_upload_calls(cls): |
| 667 | return [ |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 668 | ((['git', 'rev-parse', 'HEAD'],), 'hash'), |
| 669 | ((['git', 'symbolic-ref', 'HEAD'],), 'hash'), |
| 670 | ((['git', |
tyoshino@chromium.org | c1737d0 | 2013-05-29 14:17:28 +0000 | [diff] [blame] | 671 | 'config', 'branch.hash.last-upload-hash', 'hash'],), ''), |
rmistry@google.com | 5626a92 | 2015-02-26 14:03:30 +0000 | [diff] [blame] | 672 | ((['git', 'config', 'rietveld.run-post-upload-hook'],), ''), |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 673 | ] |
| 674 | |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 675 | @staticmethod |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 676 | 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] | 677 | fake_ancestor = 'fake_ancestor' |
| 678 | fake_cl = 'fake_cl_for_patch' |
| 679 | return [ |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 680 | ((['git', |
bratell@opera.com | f267b0e | 2013-05-02 09:11:43 +0000 | [diff] [blame] | 681 | 'rev-parse', '--verify', diff_base],), fake_ancestor), |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 682 | ((['git', |
bratell@opera.com | f267b0e | 2013-05-02 09:11:43 +0000 | [diff] [blame] | 683 | 'merge-base', fake_ancestor, 'HEAD'],), fake_ancestor), |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 684 | ((['git', |
bratell@opera.com | f267b0e | 2013-05-02 09:11:43 +0000 | [diff] [blame] | 685 | 'rev-list', '^' + fake_ancestor, 'HEAD'],), fake_cl), |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 686 | # Mock a config miss (error code 1) |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 687 | ((['git', |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 688 | 'config', 'gitcl.remotebranch'],), CERR1), |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 689 | ] + ([ |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 690 | # Call to GetRemoteBranch() |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 691 | ((['git', |
bratell@opera.com | f267b0e | 2013-05-02 09:11:43 +0000 | [diff] [blame] | 692 | 'config', 'branch.%s.merge' % working_branch],), |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 693 | 'refs/heads/master'), |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 694 | ((['git', |
bratell@opera.com | f267b0e | 2013-05-02 09:11:43 +0000 | [diff] [blame] | 695 | 'config', 'branch.%s.remote' % working_branch],), 'origin'), |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 696 | ] if get_remote_branch else []) + [ |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 697 | ((['git', 'rev-list', '^' + fake_ancestor, |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 698 | 'refs/remotes/origin/master'],), ''), |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 699 | ] |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 700 | |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 701 | @staticmethod |
bradnelson | d975b30 | 2016-10-23 12:20:23 -0700 | [diff] [blame] | 702 | def _cmd_line(description, args, similarity, find_copies, private, cc): |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 703 | """Returns the upload command line passed to upload.RealMain().""" |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 704 | return [ |
| 705 | 'upload', '--assume_yes', '--server', |
maruel@chromium.org | eb5edbc | 2012-01-16 17:03:28 +0000 | [diff] [blame] | 706 | 'https://codereview.example.com', |
maruel@chromium.org | 71e12a9 | 2012-02-14 02:34:15 +0000 | [diff] [blame] | 707 | '--message', description |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 708 | ] + args + [ |
bradnelson | d975b30 | 2016-10-23 12:20:23 -0700 | [diff] [blame] | 709 | '--cc', ','.join(['joe@example.com'] + cc), |
tyoshino@chromium.org | c1737d0 | 2013-05-29 14:17:28 +0000 | [diff] [blame] | 710 | ] + (['--private'] if private else []) + [ |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 711 | '--git_similarity', similarity or '50' |
Andrii Shyshkalov | 1897532 | 2017-01-25 16:44:13 +0100 | [diff] [blame] | 712 | ] + (['--git_no_find_copies'] if find_copies is False else []) + [ |
sbc@chromium.org | 5e07e06 | 2013-02-28 23:55:44 +0000 | [diff] [blame] | 713 | 'fake_ancestor_sha', 'HEAD' |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 714 | ] |
| 715 | |
| 716 | def _run_reviewer_test( |
| 717 | self, |
| 718 | upload_args, |
| 719 | expected_description, |
| 720 | returned_description, |
| 721 | final_description, |
tyoshino@chromium.org | c1737d0 | 2013-05-29 14:17:28 +0000 | [diff] [blame] | 722 | reviewers, |
bradnelson | d975b30 | 2016-10-23 12:20:23 -0700 | [diff] [blame] | 723 | private=False, |
| 724 | cc=None): |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 725 | """Generic reviewer test framework.""" |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 726 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
iannucci@chromium.org | 53937ba | 2012-10-02 18:20:43 +0000 | [diff] [blame] | 727 | try: |
| 728 | similarity = upload_args[upload_args.index('--similarity')+1] |
| 729 | except ValueError: |
| 730 | similarity = None |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 731 | |
| 732 | if '--find-copies' in upload_args: |
| 733 | find_copies = True |
| 734 | elif '--no-find-copies' in upload_args: |
| 735 | find_copies = False |
| 736 | else: |
| 737 | find_copies = None |
| 738 | |
tyoshino@chromium.org | c1737d0 | 2013-05-29 14:17:28 +0000 | [diff] [blame] | 739 | private = '--private' in upload_args |
bradnelson | d975b30 | 2016-10-23 12:20:23 -0700 | [diff] [blame] | 740 | cc = cc or [] |
tyoshino@chromium.org | c1737d0 | 2013-05-29 14:17:28 +0000 | [diff] [blame] | 741 | |
| 742 | self.calls = self._upload_calls(similarity, find_copies, private) |
pgervais@chromium.org | 87884cc | 2014-01-03 22:23:41 +0000 | [diff] [blame] | 743 | |
jbroman@chromium.org | 615a262 | 2013-05-03 13:20:14 +0000 | [diff] [blame] | 744 | def RunEditor(desc, _, **kwargs): |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 745 | self.assertEquals( |
| 746 | '# Enter a description of the change.\n' |
janx@chromium.org | 104b2db | 2013-04-18 12:58:40 +0000 | [diff] [blame] | 747 | '# This will be displayed on the codereview site.\n' |
alancutter@chromium.org | 63a4d7f | 2013-05-31 02:22:45 +0000 | [diff] [blame] | 748 | '# 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] | 749 | '#--------------------This line is 72 characters long' |
| 750 | '--------------------\n' + |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 751 | expected_description, |
| 752 | desc) |
| 753 | return returned_description |
| 754 | self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor) |
pgervais@chromium.org | 87884cc | 2014-01-03 22:23:41 +0000 | [diff] [blame] | 755 | |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 756 | def check_upload(args): |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 757 | cmd_line = self._cmd_line(final_description, reviewers, similarity, |
bradnelson | d975b30 | 2016-10-23 12:20:23 -0700 | [diff] [blame] | 758 | find_copies, private, cc) |
iannucci@chromium.org | 53937ba | 2012-10-02 18:20:43 +0000 | [diff] [blame] | 759 | self.assertEquals(cmd_line, args) |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 760 | return 1, 2 |
| 761 | self.mock(git_cl.upload, 'RealMain', check_upload) |
pgervais@chromium.org | 87884cc | 2014-01-03 22:23:41 +0000 | [diff] [blame] | 762 | |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 763 | git_cl.main(['upload'] + upload_args) |
| 764 | |
| 765 | def test_no_reviewer(self): |
| 766 | self._run_reviewer_test( |
| 767 | [], |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 768 | 'desc\n\nBUG=', |
| 769 | '# Blah blah comment.\ndesc\n\nBUG=', |
| 770 | 'desc\n\nBUG=', |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 771 | []) |
| 772 | |
iannucci@chromium.org | 53937ba | 2012-10-02 18:20:43 +0000 | [diff] [blame] | 773 | def test_keep_similarity(self): |
| 774 | self._run_reviewer_test( |
| 775 | ['--similarity', '70'], |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 776 | 'desc\n\nBUG=', |
| 777 | '# Blah blah comment.\ndesc\n\nBUG=', |
| 778 | 'desc\n\nBUG=', |
iannucci@chromium.org | 53937ba | 2012-10-02 18:20:43 +0000 | [diff] [blame] | 779 | []) |
| 780 | |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 781 | def test_keep_find_copies(self): |
| 782 | self._run_reviewer_test( |
| 783 | ['--no-find-copies'], |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 784 | 'desc\n\nBUG=', |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 785 | '# Blah blah comment.\ndesc\n\nBUG=\n', |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 786 | 'desc\n\nBUG=', |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 787 | []) |
| 788 | |
tyoshino@chromium.org | c1737d0 | 2013-05-29 14:17:28 +0000 | [diff] [blame] | 789 | def test_private(self): |
| 790 | self._run_reviewer_test( |
| 791 | ['--private'], |
| 792 | 'desc\n\nBUG=', |
| 793 | '# Blah blah comment.\ndesc\n\nBUG=\n', |
| 794 | 'desc\n\nBUG=', |
| 795 | []) |
| 796 | |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 797 | def test_reviewers_cmd_line(self): |
| 798 | # Reviewer is passed as-is |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 799 | description = 'desc\n\nR=foo@example.com\nBUG=' |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 800 | self._run_reviewer_test( |
| 801 | ['-r' 'foo@example.com'], |
| 802 | description, |
| 803 | '\n%s\n' % description, |
| 804 | description, |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 805 | ['--reviewers=foo@example.com']) |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 806 | |
| 807 | def test_reviewer_tbr_overriden(self): |
| 808 | # Reviewer is overriden with TBR |
| 809 | # Also verifies the regexp work without a trailing LF |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 810 | description = 'Foo Bar\n\nTBR=reviewer@example.com' |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 811 | self._run_reviewer_test( |
| 812 | ['-r' 'foo@example.com'], |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 813 | 'desc\n\nR=foo@example.com\nBUG=', |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 814 | description.strip('\n'), |
| 815 | description, |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 816 | ['--reviewers=reviewer@example.com']) |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 817 | |
| 818 | def test_reviewer_multiple(self): |
| 819 | # Handles multiple R= or TBR= lines. |
| 820 | description = ( |
bradnelson | d975b30 | 2016-10-23 12:20:23 -0700 | [diff] [blame] | 821 | 'Foo Bar\nTBR=reviewer@example.com\nBUG=\nR=another@example.com\n' |
| 822 | 'CC=more@example.com,people@example.com') |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 823 | self._run_reviewer_test( |
| 824 | [], |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 825 | 'desc\n\nBUG=', |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 826 | description, |
| 827 | description, |
bradnelson | d975b30 | 2016-10-23 12:20:23 -0700 | [diff] [blame] | 828 | ['--reviewers=another@example.com,reviewer@example.com'], |
| 829 | cc=['more@example.com', 'people@example.com']) |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 830 | |
maruel@chromium.org | a335365 | 2011-11-30 14:26:57 +0000 | [diff] [blame] | 831 | def test_reviewer_send_mail(self): |
| 832 | # --send-mail can be used without -r if R= is used |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 833 | description = 'Foo Bar\nR=reviewer@example.com' |
maruel@chromium.org | a335365 | 2011-11-30 14:26:57 +0000 | [diff] [blame] | 834 | self._run_reviewer_test( |
| 835 | ['--send-mail'], |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 836 | 'desc\n\nBUG=', |
maruel@chromium.org | a335365 | 2011-11-30 14:26:57 +0000 | [diff] [blame] | 837 | description.strip('\n'), |
| 838 | description, |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 839 | ['--reviewers=reviewer@example.com', '--send_mail']) |
maruel@chromium.org | a335365 | 2011-11-30 14:26:57 +0000 | [diff] [blame] | 840 | |
| 841 | def test_reviewer_send_mail_no_rev(self): |
| 842 | # Fails without a reviewer. |
maruel@chromium.org | 2e23ce3 | 2013-05-07 12:42:28 +0000 | [diff] [blame] | 843 | stdout = StringIO.StringIO() |
tandrii | c2405f5 | 2016-10-10 08:13:15 -0700 | [diff] [blame] | 844 | self.calls = self._upload_no_rev_calls(None, None) + [ |
| 845 | ((['DieWithError', 'Must specify reviewers to send email.'],), |
| 846 | SystemExitMock()) |
| 847 | ] |
| 848 | |
| 849 | def RunEditor(desc, _, **kwargs): |
| 850 | return desc |
| 851 | self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor) |
| 852 | self.mock(sys, 'stdout', stdout) |
| 853 | with self.assertRaises(SystemExitMock): |
maruel@chromium.org | a335365 | 2011-11-30 14:26:57 +0000 | [diff] [blame] | 854 | git_cl.main(['upload', '--send-mail']) |
tandrii | c2405f5 | 2016-10-10 08:13:15 -0700 | [diff] [blame] | 855 | self.assertEqual( |
| 856 | 'Using 50% similarity for rename/copy detection. Override with ' |
| 857 | '--similarity.\n', |
| 858 | stdout.getvalue()) |
maruel@chromium.org | a335365 | 2011-11-30 14:26:57 +0000 | [diff] [blame] | 859 | |
tandrii | f9aefb7 | 2016-07-01 09:06:51 -0700 | [diff] [blame] | 860 | def test_bug_on_cmd(self): |
| 861 | self._run_reviewer_test( |
| 862 | ['--bug=500658,proj:123'], |
| 863 | 'desc\n\nBUG=500658\nBUG=proj:123', |
| 864 | '# Blah blah comment.\ndesc\n\nBUG=500658\nBUG=proj:1234', |
| 865 | 'desc\n\nBUG=500658\nBUG=proj:1234', |
| 866 | []) |
| 867 | |
Andrii Shyshkalov | 768f1d8 | 2016-12-08 15:10:13 +0100 | [diff] [blame] | 868 | def _land_rietveld_common(self, debug=False): |
| 869 | if debug: |
| 870 | # Very useful due to finally clause in git cl land raising exceptions and |
| 871 | # shadowing real cause of failure. |
| 872 | self.mock(git_cl, '_IS_BEING_TESTED', True) |
| 873 | else: |
Andrii Shyshkalov | 06a2502 | 2016-11-24 16:47:00 +0100 | [diff] [blame] | 874 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
Andrii Shyshkalov | 768f1d8 | 2016-12-08 15:10:13 +0100 | [diff] [blame] | 875 | |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 876 | self.mock(git_cl, '_is_git_numberer_enabled', lambda url, ref: |
| 877 | self._mocked_call('_is_git_numberer_enabled', url, ref)) |
Andrii Shyshkalov | 06a2502 | 2016-11-24 16:47:00 +0100 | [diff] [blame] | 878 | self.mock(RietveldMock, 'update_description', staticmethod( |
| 879 | lambda i, d: self._mocked_call(['update_description', i, d]))) |
| 880 | self.mock(RietveldMock, 'add_comment', staticmethod( |
| 881 | lambda i, c: self._mocked_call(['add_comment', i, c]))) |
| 882 | self.calls = [ |
Andrii Shyshkalov | 06a2502 | 2016-11-24 16:47:00 +0100 | [diff] [blame] | 883 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
| 884 | ((['git', 'config', 'branch.feature.git-cl-similarity'],), CERR1), |
| 885 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
| 886 | ((['git', 'config', '--bool', 'branch.feature.git-find-copies'],), |
| 887 | CERR1), |
| 888 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
| 889 | ((['git', 'config', 'branch.feature.rietveldissue'],), '123'), |
Aaron Gable | 1bc7bfe | 2016-12-19 10:08:14 -0800 | [diff] [blame] | 890 | ((['git', 'config', 'rietveld.autoupdate'],), ''), |
Andrii Shyshkalov | 06a2502 | 2016-11-24 16:47:00 +0100 | [diff] [blame] | 891 | ((['git', 'config', 'rietveld.server'],), |
| 892 | 'https://codereview.chromium.org'), |
| 893 | ((['git', 'config', 'branch.feature.merge'],), 'refs/heads/master'), |
| 894 | ((['git', 'config', 'branch.feature.remote'],), 'origin'), |
| 895 | ((['git', 'config', 'branch.feature.merge'],), 'refs/heads/master'), |
| 896 | ((['git', 'config', 'branch.feature.remote'],), 'origin'), |
Andrii Shyshkalov | 06a2502 | 2016-11-24 16:47:00 +0100 | [diff] [blame] | 897 | ((['git', 'rev-list', '^feature', 'refs/remotes/origin/master'],), |
| 898 | ''), # No commits to rebase, according to local view of origin. |
| 899 | ((['git', 'merge-base', 'refs/remotes/origin/master', 'HEAD'],), |
| 900 | 'fake_ancestor_sha'), |
| 901 | ] + self._git_sanity_checks('fake_ancestor_sha', 'feature') + [ |
| 902 | ((['git', 'rev-parse', '--show-cdup'],), ''), |
| 903 | ((['git', 'rev-parse', 'HEAD'],), 'fake_sha'), |
| 904 | ((['git', 'diff', '--name-status', '--no-renames', '-r', |
| 905 | 'fake_ancestor_sha...', '.'],), |
| 906 | 'M\tfile1.cpp'), |
| 907 | ((['git', 'config', 'branch.feature.rietveldpatchset'],), '20001'), |
| 908 | ((['git', 'config', 'branch.feature.rietveldserver'],), |
| 909 | 'https://codereview.chromium.org'), |
| 910 | ((['git', 'config', 'user.email'],), 'user@e.mail'), |
| 911 | ((['git', 'config', 'rietveld.tree-status-url'],), CERR1), |
| 912 | ((['git', 'diff', '--no-ext-diff', '--stat', '-l100000', '-C50', |
| 913 | 'fake_ancestor_sha', 'feature'],), |
| 914 | # This command just prints smth like this: |
| 915 | # file1.cpp | 53 ++++++-- |
| 916 | # 1 file changed, 33 insertions(+), 20 deletions(-)\n |
| 917 | ''), |
| 918 | ((['git', 'show-ref', '--quiet', '--verify', |
| 919 | 'refs/heads/git-cl-commit'],), |
| 920 | ''), # 0 return code means branch exists. |
| 921 | ((['git', 'branch', '-D', 'git-cl-commit'],), ''), |
| 922 | ((['git', 'show-ref', '--quiet', '--verify', |
| 923 | 'refs/heads/git-cl-cherry-pick'],), |
| 924 | CERR1), # This means git-cl-cherry-pick branch does not exist. |
| 925 | ((['git', 'rev-parse', '--show-cdup'],), ''), |
| 926 | ((['git', 'checkout', '-q', '-b', 'git-cl-commit'],), ''), |
| 927 | ((['git', 'reset', '--soft', 'fake_ancestor_sha'],), ''), |
| 928 | ((['git', 'commit', '-m', |
| 929 | 'Issue: 123\n\nR=john@chromium.org\n\n' |
Andrii Shyshkalov | f01be22 | 2016-12-08 19:21:13 +0100 | [diff] [blame] | 930 | 'Review-Url: https://codereview.chromium.org/123 .'],), ''), |
Andrii Shyshkalov | 06a2502 | 2016-11-24 16:47:00 +0100 | [diff] [blame] | 931 | ((['git', 'config', 'branch.feature.merge'],), 'refs/heads/master'), |
| 932 | ((['git', 'config', 'branch.feature.remote'],), 'origin'), |
| 933 | ((['git', 'config', '--get', 'remote.origin.url'],), |
| 934 | 'https://chromium.googlesource.com/infra/infra'), |
| 935 | ] |
| 936 | |
| 937 | def test_land_rietveld(self): |
Andrii Shyshkalov | 768f1d8 | 2016-12-08 15:10:13 +0100 | [diff] [blame] | 938 | self._land_rietveld_common(debug=False) |
Andrii Shyshkalov | 06a2502 | 2016-11-24 16:47:00 +0100 | [diff] [blame] | 939 | self.calls += [ |
Andrii Shyshkalov | 768f1d8 | 2016-12-08 15:10:13 +0100 | [diff] [blame] | 940 | ((['git', 'config', 'remote.origin.url'],), |
| 941 | 'https://chromium.googlesource.com/infra/infra'), |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 942 | (('_is_git_numberer_enabled', |
| 943 | 'https://chromium.googlesource.com/infra/infra', |
| 944 | 'refs/heads/master'), |
| 945 | False), |
Andrii Shyshkalov | 06a2502 | 2016-11-24 16:47:00 +0100 | [diff] [blame] | 946 | ((['git', 'push', '--porcelain', 'origin', 'HEAD:refs/heads/master'],), |
| 947 | ''), |
| 948 | ((['git', 'rev-parse', 'HEAD'],), 'fake_sha_rebased'), |
| 949 | ((['git', 'checkout', '-q', 'feature'],), ''), |
| 950 | ((['git', 'branch', '-D', 'git-cl-commit'],), ''), |
| 951 | ((['git', 'config', 'rietveld.viewvc-url'],), |
| 952 | 'https://chromium.googlesource.com/infra/infra/+/'), |
| 953 | ((['update_description', 123, |
| 954 | 'Issue: 123\n\nR=john@chromium.org\n\nCommitted: ' |
| 955 | 'https://chromium.googlesource.com/infra/infra/+/fake_sha_rebased'],), |
| 956 | ''), |
| 957 | ((['add_comment', 123, 'Committed patchset #2 (id:20001) manually as ' |
| 958 | 'fake_sha_rebased (presubmit successful).'],), ''), |
| 959 | ] |
| 960 | git_cl.main(['land']) |
| 961 | |
Andrii Shyshkalov | 15e50cc | 2016-12-02 14:34:08 +0100 | [diff] [blame] | 962 | def test_land_rietveld_git_numberer(self): |
Andrii Shyshkalov | 768f1d8 | 2016-12-08 15:10:13 +0100 | [diff] [blame] | 963 | self._land_rietveld_common(debug=False) |
Andrii Shyshkalov | a669581 | 2016-12-06 17:47:09 +0100 | [diff] [blame] | 964 | |
| 965 | # Special mocks to check validity of timestamp. |
| 966 | original_git_amend_head = git_cl._git_amend_head |
| 967 | def _git_amend_head_mock(msg, tstamp): |
| 968 | self._mocked_call(['git_amend_head committer timestamp', tstamp]) |
| 969 | return original_git_amend_head(msg, tstamp) |
| 970 | self.mock(git_cl, '_git_amend_head', _git_amend_head_mock) |
| 971 | |
Andrii Shyshkalov | 15e50cc | 2016-12-02 14:34:08 +0100 | [diff] [blame] | 972 | self.calls += [ |
Andrii Shyshkalov | 768f1d8 | 2016-12-08 15:10:13 +0100 | [diff] [blame] | 973 | ((['git', 'config', 'remote.origin.url'],), |
| 974 | 'https://chromium.googlesource.com/chromium/src'), |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 975 | (('_is_git_numberer_enabled', |
| 976 | 'https://chromium.googlesource.com/chromium/src', |
| 977 | 'refs/heads/master'), |
| 978 | True), |
Andrii Shyshkalov | 15e50cc | 2016-12-02 14:34:08 +0100 | [diff] [blame] | 979 | ((['git', 'show', '-s', '--format=%B', 'fake_ancestor_sha'],), |
| 980 | 'This is parent commit.\n' |
| 981 | '\n' |
| 982 | 'Cr-Commit-Position: refs/heads/master@{#543}\n' |
| 983 | 'Cr-Branched-From: refs/svn/2014@{#2208}'), |
Andrii Shyshkalov | a669581 | 2016-12-06 17:47:09 +0100 | [diff] [blame] | 984 | ((['git', 'show', '-s', '--format=%ct', 'fake_ancestor_sha'],), |
| 985 | '1480022355'), # Committer's unix timestamp. |
| 986 | ((['git', 'show', '-s', '--format=%ct', 'HEAD'],), |
| 987 | '1480024000'), |
Andrii Shyshkalov | 15e50cc | 2016-12-02 14:34:08 +0100 | [diff] [blame] | 988 | |
Andrii Shyshkalov | a669581 | 2016-12-06 17:47:09 +0100 | [diff] [blame] | 989 | ((['git_amend_head committer timestamp', 1480024000],), None), |
Andrii Shyshkalov | 15e50cc | 2016-12-02 14:34:08 +0100 | [diff] [blame] | 990 | ((['git', 'commit', '--amend', '-m', |
| 991 | 'Issue: 123\n\nR=john@chromium.org\n' |
| 992 | '\n' |
Andrii Shyshkalov | f01be22 | 2016-12-08 19:21:13 +0100 | [diff] [blame] | 993 | 'Review-Url: https://codereview.chromium.org/123 .\n' |
Andrii Shyshkalov | 15e50cc | 2016-12-02 14:34:08 +0100 | [diff] [blame] | 994 | 'Cr-Commit-Position: refs/heads/master@{#544}\n' |
| 995 | 'Cr-Branched-From: refs/svn/2014@{#2208}'],), ''), |
| 996 | |
| 997 | ((['git', 'push', '--porcelain', 'origin', 'HEAD:refs/heads/master'],), |
| 998 | ''), |
| 999 | ((['git', 'rev-parse', 'HEAD'],), 'fake_sha_rebased'), |
| 1000 | ((['git', 'checkout', '-q', 'feature'],), ''), |
| 1001 | ((['git', 'branch', '-D', 'git-cl-commit'],), ''), |
| 1002 | ((['git', 'config', 'rietveld.viewvc-url'],), |
| 1003 | 'https://chromium.googlesource.com/infra/infra/+/'), |
| 1004 | ((['update_description', 123, |
| 1005 | 'Issue: 123\n\nR=john@chromium.org\n' |
| 1006 | '\n' |
Andrii Shyshkalov | f01be22 | 2016-12-08 19:21:13 +0100 | [diff] [blame] | 1007 | 'Review-Url: https://codereview.chromium.org/123 .\n' |
Andrii Shyshkalov | 15e50cc | 2016-12-02 14:34:08 +0100 | [diff] [blame] | 1008 | 'Cr-Commit-Position: refs/heads/master@{#544}\n' |
| 1009 | 'Cr-Branched-From: refs/svn/2014@{#2208}\n' |
| 1010 | 'Committed: ' |
| 1011 | 'https://chromium.googlesource.com/infra/infra/+/fake_sha_rebased'],), |
| 1012 | ''), |
| 1013 | ((['add_comment', 123, 'Committed patchset #2 (id:20001) manually as ' |
| 1014 | 'fake_sha_rebased (presubmit successful).'],), ''), |
| 1015 | ] |
| 1016 | git_cl.main(['land']) |
| 1017 | |
| 1018 | def test_land_rietveld_git_numberer_bad_parent(self): |
Andrii Shyshkalov | 768f1d8 | 2016-12-08 15:10:13 +0100 | [diff] [blame] | 1019 | self._land_rietveld_common(debug=False) |
Andrii Shyshkalov | 15e50cc | 2016-12-02 14:34:08 +0100 | [diff] [blame] | 1020 | self.calls += [ |
Andrii Shyshkalov | 768f1d8 | 2016-12-08 15:10:13 +0100 | [diff] [blame] | 1021 | ((['git', 'config', 'remote.origin.url'],), |
| 1022 | 'https://chromium.googlesource.com/v8/v8'), |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1023 | (('_is_git_numberer_enabled', |
| 1024 | 'https://chromium.googlesource.com/v8/v8', 'refs/heads/master'), |
| 1025 | True), |
Andrii Shyshkalov | 15e50cc | 2016-12-02 14:34:08 +0100 | [diff] [blame] | 1026 | |
| 1027 | ((['git', 'show', '-s', '--format=%B', 'fake_ancestor_sha'],), |
| 1028 | 'This is parent commit with no footer.'), |
| 1029 | |
| 1030 | ((['git', 'checkout', '-q', 'feature'],), ''), |
| 1031 | ((['git', 'branch', '-D', 'git-cl-commit'],), ''), |
| 1032 | ] |
| 1033 | with self.assertRaises(ValueError) as cm: |
| 1034 | git_cl.main(['land']) |
| 1035 | self.assertEqual(cm.exception.message, |
| 1036 | 'Unable to infer commit position from footers') |
| 1037 | |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1038 | def test_git_numberer_not_whitelisted_repo(self): |
| 1039 | self.calls = [] |
| 1040 | res = git_cl._is_git_numberer_enabled( |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1041 | remote_url='https://chromium.googlesource.com/chromium/tools/build', |
| 1042 | remote_ref='refs/whatever') |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1043 | self.assertEqual(res, False) |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1044 | |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1045 | def test_git_numberer_fail_fetch(self): |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1046 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
| 1047 | self.calls = [ |
| 1048 | ((['git', 'fetch', 'https://chromium.googlesource.com/chromium/src', |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1049 | '+refs/meta/config:refs/git_cl/meta/config'],), CERR1), |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1050 | ] |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1051 | res = git_cl._is_git_numberer_enabled( |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1052 | remote_url='https://chromium.googlesource.com/chromium/src', |
| 1053 | remote_ref='refs/whatever') |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1054 | self.assertEqual(res, False) |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1055 | |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1056 | def test_git_numberer_valid_configs(self): |
Andrii Shyshkalov | 351c61d | 2017-01-21 20:40:16 +0000 | [diff] [blame] | 1057 | with git_cl.gclient_utils.temporary_directory() as tempdir: |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1058 | @contextlib.contextmanager |
Andrii Shyshkalov | 351c61d | 2017-01-21 20:40:16 +0000 | [diff] [blame] | 1059 | def fake_temporary_directory(**kwargs): |
| 1060 | yield tempdir |
| 1061 | self.mock(git_cl.gclient_utils, 'temporary_directory', |
| 1062 | fake_temporary_directory) |
| 1063 | self._test_GitNumbererState_valid_configs_inner(tempdir) |
| 1064 | |
| 1065 | def _test_GitNumbererState_valid_configs_inner(self, tempdir): |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1066 | self.calls = [ |
| 1067 | ((['git', 'fetch', 'https://chromium.googlesource.com/chromium/src', |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1068 | '+refs/meta/config:refs/git_cl/meta/config'],), ''), |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1069 | ((['git', 'show', 'refs/git_cl/meta/config:project.config'],), |
| 1070 | ''' |
| 1071 | [plugin "git-numberer"] |
| 1072 | validate-enabled-refglob = refs/else/* |
| 1073 | validate-enabled-refglob = refs/heads/* |
| 1074 | validate-disabled-refglob = refs/heads/disabled |
| 1075 | validate-disabled-refglob = refs/branch-heads/* |
| 1076 | '''), |
Andrii Shyshkalov | 351c61d | 2017-01-21 20:40:16 +0000 | [diff] [blame] | 1077 | ((['git', 'config', '-f', os.path.join(tempdir, 'project.config') , |
| 1078 | '--get-all', 'plugin.git-numberer.validate-enabled-refglob'],), |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1079 | 'refs/else/*\n' |
| 1080 | 'refs/heads/*\n'), |
Andrii Shyshkalov | 351c61d | 2017-01-21 20:40:16 +0000 | [diff] [blame] | 1081 | ((['git', 'config', '-f', os.path.join(tempdir, 'project.config') , |
| 1082 | '--get-all', 'plugin.git-numberer.validate-disabled-refglob'],), |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1083 | 'refs/heads/disabled\n' |
| 1084 | 'refs/branch-heads/*\n'), |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1085 | ] * 3 # 3 tests below have exactly the same IO. |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1086 | |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1087 | res = git_cl._is_git_numberer_enabled( |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1088 | remote_url='https://chromium.googlesource.com/chromium/src', |
| 1089 | remote_ref='refs/heads/test') |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1090 | self.assertEqual(res, True) |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1091 | |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1092 | res = git_cl._is_git_numberer_enabled( |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1093 | remote_url='https://chromium.googlesource.com/chromium/src', |
| 1094 | remote_ref='refs/heads/disabled') |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1095 | self.assertEqual(res, False) |
Andrii Shyshkalov | cd6a936 | 2016-12-07 12:04:12 +0100 | [diff] [blame] | 1096 | |
Andrii Shyshkalov | cbec8ef | 2016-12-09 13:54:51 +0100 | [diff] [blame] | 1097 | # Validator is disabled by default, even if it's not explicitely in disabled |
| 1098 | # refglobs. |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1099 | res = git_cl._is_git_numberer_enabled( |
Andrii Shyshkalov | cbec8ef | 2016-12-09 13:54:51 +0100 | [diff] [blame] | 1100 | remote_url='https://chromium.googlesource.com/chromium/src', |
| 1101 | remote_ref='refs/arbitrary/ref') |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1102 | self.assertEqual(res, False) |
Andrii Shyshkalov | cbec8ef | 2016-12-09 13:54:51 +0100 | [diff] [blame] | 1103 | |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 1104 | @classmethod |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 1105 | 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] | 1106 | cmd = ['git', 'config', '--bool', 'gerrit.skip-ensure-authenticated'] |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 1107 | if skip_auth_check: |
| 1108 | return [((cmd, ), 'true')] |
| 1109 | |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 1110 | calls = [((cmd, ), CERR1)] |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 1111 | if issue: |
| 1112 | calls.extend([ |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1113 | ((['git', 'config', 'branch.master.gerritserver'],), CERR1), |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 1114 | ]) |
| 1115 | calls.extend([ |
| 1116 | ((['git', 'config', 'branch.master.merge'],), 'refs/heads/master'), |
| 1117 | ((['git', 'config', 'branch.master.remote'],), 'origin'), |
| 1118 | ((['git', 'config', 'remote.origin.url'],), |
| 1119 | 'https://chromium.googlesource.com/my/repo'), |
| 1120 | ((['git', 'config', 'remote.origin.url'],), |
| 1121 | 'https://chromium.googlesource.com/my/repo'), |
| 1122 | ]) |
| 1123 | return calls |
| 1124 | |
| 1125 | @classmethod |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1126 | def _gerrit_base_calls(cls, issue=None, fetched_description=None): |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1127 | return [ |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 1128 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 1129 | ((['git', 'config', 'branch.master.git-cl-similarity'],), |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 1130 | CERR1), |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 1131 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 1132 | ((['git', 'config', '--bool', 'branch.master.git-find-copies'],), |
| 1133 | CERR1), |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 1134 | ] + cls._is_gerrit_calls(True) + [ |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 1135 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 1136 | ((['git', 'config', 'branch.master.rietveldissue'],), CERR1), |
| 1137 | ((['git', 'config', 'branch.master.gerritissue'],), |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 1138 | CERR1 if issue is None else str(issue)), |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 1139 | ((['git', 'config', 'branch.master.merge'],), 'refs/heads/master'), |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 1140 | ((['git', 'config', 'branch.master.remote'],), 'origin'), |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 1141 | ((['get_or_create_merge_base', 'master', |
| 1142 | 'refs/remotes/origin/master'],), |
iannucci@chromium.org | 9e84927 | 2014-04-04 00:31:55 +0000 | [diff] [blame] | 1143 | 'fake_ancestor_sha'), |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 1144 | # Calls to verify branch point is ancestor |
| 1145 | ] + (cls._gerrit_ensure_auth_calls(issue=issue) + |
| 1146 | cls._git_sanity_checks('fake_ancestor_sha', 'master', |
| 1147 | get_remote_branch=False)) + [ |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 1148 | ((['git', 'rev-parse', '--show-cdup'],), ''), |
| 1149 | ((['git', 'rev-parse', 'HEAD'],), '12345'), |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 1150 | |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 1151 | ((['git', |
mcgrathr@chromium.org | 9249f64 | 2013-06-03 21:36:18 +0000 | [diff] [blame] | 1152 | 'diff', '--name-status', '--no-renames', '-r', |
| 1153 | 'fake_ancestor_sha...', '.'],), |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1154 | 'M\t.gitignore\n'), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 1155 | ((['git', 'config', 'branch.master.gerritpatchset'],), CERR1), |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1156 | ] + ([ |
| 1157 | (('GetChangeDetail', 'chromium-review.googlesource.com', |
| 1158 | '123456', ['CURRENT_REVISION', 'CURRENT_COMMIT']), |
| 1159 | { |
| 1160 | 'change_id': '123456789', |
| 1161 | 'current_revision': 'sha1_of_current_revision', |
| 1162 | 'revisions': { 'sha1_of_current_revision': { |
| 1163 | 'commit': {'message': fetched_description}, |
| 1164 | }}, |
| 1165 | }), |
| 1166 | ] if issue else [ |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 1167 | ((['git', |
bratell@opera.com | f267b0e | 2013-05-02 09:11:43 +0000 | [diff] [blame] | 1168 | 'log', '--pretty=format:%s%n%n%b', 'fake_ancestor_sha...'],), |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 1169 | 'foo'), |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 1170 | ]) + [ |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 1171 | ((['git', 'config', 'user.email'],), 'me@example.com'), |
| 1172 | ((['git', |
scottmg | b84b5e3 | 2016-11-10 09:25:33 -0800 | [diff] [blame] | 1173 | 'diff', '--no-ext-diff', '--stat', '-l100000', '-C50', |
| 1174 | 'fake_ancestor_sha', 'HEAD'],), |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1175 | '+dat'), |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 1176 | ] |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1177 | |
tandrii@chromium.org | 1e67bb7 | 2016-02-11 12:15:49 +0000 | [diff] [blame] | 1178 | @classmethod |
| 1179 | def _gerrit_upload_calls(cls, description, reviewers, squash, |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1180 | squash_mode='default', |
tandrii@chromium.org | 1062500 | 2016-03-04 20:03:47 +0000 | [diff] [blame] | 1181 | expected_upstream_ref='origin/refs/heads/master', |
Aaron Gable | 9b713dd | 2016-12-14 16:04:21 -0800 | [diff] [blame] | 1182 | ref_suffix='', title=None, notify=False, |
bradnelson | d975b30 | 2016-10-23 12:20:23 -0700 | [diff] [blame] | 1183 | post_amend_description=None, issue=None, cc=None): |
tandrii@chromium.org | 1062500 | 2016-03-04 20:03:47 +0000 | [diff] [blame] | 1184 | if post_amend_description is None: |
| 1185 | post_amend_description = description |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1186 | calls = [] |
bradnelson | d975b30 | 2016-10-23 12:20:23 -0700 | [diff] [blame] | 1187 | cc = cc or [] |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 1188 | |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1189 | if squash_mode == 'default': |
| 1190 | calls.extend([ |
| 1191 | ((['git', 'config', '--bool', 'gerrit.override-squash-uploads'],), ''), |
| 1192 | ((['git', 'config', '--bool', 'gerrit.squash-uploads'],), ''), |
| 1193 | ]) |
| 1194 | elif squash_mode in ('override_squash', 'override_nosquash'): |
| 1195 | calls.extend([ |
| 1196 | ((['git', 'config', '--bool', 'gerrit.override-squash-uploads'],), |
| 1197 | 'true' if squash_mode == 'override_squash' else 'false'), |
| 1198 | ]) |
| 1199 | else: |
| 1200 | assert squash_mode in ('squash', 'nosquash') |
| 1201 | |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 1202 | # If issue is given, then description is fetched from Gerrit instead. |
| 1203 | if issue is None: |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 1204 | calls += [ |
| 1205 | ((['git', 'log', '--pretty=format:%s\n\n%b', |
| 1206 | 'fake_ancestor_sha..HEAD'],), |
| 1207 | description)] |
Aaron Gable | b56ad33 | 2017-01-06 15:24:31 -0800 | [diff] [blame] | 1208 | if squash: |
| 1209 | title = 'Initial_upload' |
| 1210 | else: |
| 1211 | if not title: |
| 1212 | calls += [ |
| 1213 | ((['git', 'show', '-s', '--format=%s', 'HEAD'],), ''), |
| 1214 | (('Title for patchset []: ',), 'User input'), |
| 1215 | ] |
| 1216 | title = 'User_input' |
tandrii@chromium.org | 57d8654 | 2016-03-04 16:11:32 +0000 | [diff] [blame] | 1217 | 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] | 1218 | calls += [ |
tandrii@chromium.org | 1062500 | 2016-03-04 20:03:47 +0000 | [diff] [blame] | 1219 | # DownloadGerritHook(False) |
| 1220 | ((False, ), |
| 1221 | ''), |
| 1222 | # Amending of commit message to get the Change-Id. |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 1223 | ((['git', 'log', '--pretty=format:%s\n\n%b', |
sbc@chromium.org | 5e07e06 | 2013-02-28 23:55:44 +0000 | [diff] [blame] | 1224 | 'fake_ancestor_sha..HEAD'],), |
sivachandra@chromium.org | aebe87f | 2012-10-22 20:34:21 +0000 | [diff] [blame] | 1225 | description), |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 1226 | ((['git', 'commit', '--amend', '-m', description],), |
sivachandra@chromium.org | aebe87f | 2012-10-22 20:34:21 +0000 | [diff] [blame] | 1227 | ''), |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 1228 | ((['git', 'log', '--pretty=format:%s\n\n%b', |
sbc@chromium.org | 5e07e06 | 2013-02-28 23:55:44 +0000 | [diff] [blame] | 1229 | 'fake_ancestor_sha..HEAD'],), |
tandrii@chromium.org | 1062500 | 2016-03-04 20:03:47 +0000 | [diff] [blame] | 1230 | post_amend_description) |
sivachandra@chromium.org | aebe87f | 2012-10-22 20:34:21 +0000 | [diff] [blame] | 1231 | ] |
bauerb@chromium.org | 27386dd | 2015-02-16 10:45:39 +0000 | [diff] [blame] | 1232 | if squash: |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 1233 | if not issue: |
| 1234 | # Prompting to edit description on first upload. |
| 1235 | calls += [ |
| 1236 | ((['git', 'config', 'core.editor'],), ''), |
| 1237 | ((['RunEditor'],), description), |
| 1238 | ] |
bauerb@chromium.org | 27386dd | 2015-02-16 10:45:39 +0000 | [diff] [blame] | 1239 | ref_to_push = 'abcdef0123456789' |
| 1240 | calls += [ |
bauerb@chromium.org | 27386dd | 2015-02-16 10:45:39 +0000 | [diff] [blame] | 1241 | ((['git', 'config', 'branch.master.merge'],), |
| 1242 | 'refs/heads/master'), |
| 1243 | ((['git', 'config', 'branch.master.remote'],), |
| 1244 | 'origin'), |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 1245 | ((['get_or_create_merge_base', 'master', |
| 1246 | 'refs/remotes/origin/master'],), |
bauerb@chromium.org | 27386dd | 2015-02-16 10:45:39 +0000 | [diff] [blame] | 1247 | 'origin/master'), |
| 1248 | ((['git', 'rev-parse', 'HEAD:'],), |
| 1249 | '0123456789abcdef'), |
| 1250 | ((['git', 'commit-tree', '0123456789abcdef', '-p', |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 1251 | 'origin/master', '-m', description],), |
bauerb@chromium.org | 27386dd | 2015-02-16 10:45:39 +0000 | [diff] [blame] | 1252 | ref_to_push), |
| 1253 | ] |
| 1254 | else: |
| 1255 | ref_to_push = 'HEAD' |
| 1256 | |
sivachandra@chromium.org | aebe87f | 2012-10-22 20:34:21 +0000 | [diff] [blame] | 1257 | calls += [ |
luqui@chromium.org | 609f395 | 2015-05-04 22:47:04 +0000 | [diff] [blame] | 1258 | ((['git', 'rev-list', |
| 1259 | expected_upstream_ref + '..' + ref_to_push],), ''), |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1260 | ] |
tandrii@chromium.org | 8da4540 | 2016-05-24 23:11:03 +0000 | [diff] [blame] | 1261 | |
Aaron Gable | 9b713dd | 2016-12-14 16:04:21 -0800 | [diff] [blame] | 1262 | if title: |
| 1263 | if ref_suffix: |
| 1264 | ref_suffix += ',m=' + title |
| 1265 | else: |
| 1266 | ref_suffix = '%m=' + title |
| 1267 | |
tandrii@chromium.org | 8da4540 | 2016-05-24 23:11:03 +0000 | [diff] [blame] | 1268 | notify_suffix = 'notify=%s' % ('ALL' if notify else 'NONE') |
| 1269 | if ref_suffix: |
| 1270 | ref_suffix += ',' + notify_suffix |
| 1271 | else: |
| 1272 | ref_suffix = '%' + notify_suffix |
tandrii@chromium.org | 074c2af | 2016-06-03 23:18:40 +0000 | [diff] [blame] | 1273 | |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1274 | if reviewers: |
tandrii@chromium.org | 8da4540 | 2016-05-24 23:11:03 +0000 | [diff] [blame] | 1275 | ref_suffix += ',' + ','.join('r=%s' % email |
| 1276 | for email in sorted(reviewers)) |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1277 | calls += [ |
tandrii@chromium.org | 8acd833 | 2016-04-13 12:56:03 +0000 | [diff] [blame] | 1278 | ((['git', 'push', 'origin', |
tandrii@chromium.org | bf766ba | 2016-04-13 12:51:23 +0000 | [diff] [blame] | 1279 | ref_to_push + ':refs/for/refs/heads/master' + ref_suffix],), |
tandrii@chromium.org | a342c92 | 2016-03-16 07:08:25 +0000 | [diff] [blame] | 1280 | ('remote:\n' |
| 1281 | 'remote: Processing changes: (\)\n' |
| 1282 | 'remote: Processing changes: (|)\n' |
| 1283 | 'remote: Processing changes: (/)\n' |
| 1284 | 'remote: Processing changes: (-)\n' |
| 1285 | 'remote: Processing changes: new: 1 (/)\n' |
| 1286 | 'remote: Processing changes: new: 1, done\n' |
| 1287 | 'remote:\n' |
| 1288 | 'remote: New Changes:\n' |
| 1289 | 'remote: https://chromium-review.googlesource.com/123456 XXX.\n' |
| 1290 | 'remote:\n' |
| 1291 | 'To https://chromium.googlesource.com/yyy/zzz\n' |
| 1292 | ' * [new branch] hhhh -> refs/for/refs/heads/master\n')), |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1293 | ] |
bauerb@chromium.org | 27386dd | 2015-02-16 10:45:39 +0000 | [diff] [blame] | 1294 | if squash: |
| 1295 | calls += [ |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 1296 | ((['git', 'config', 'branch.master.gerritissue', '123456'],), |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 1297 | ''), |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 1298 | ((['git', 'config', 'branch.master.gerritserver', |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 1299 | 'https://chromium-review.googlesource.com'],), ''), |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 1300 | ((['git', 'config', 'branch.master.gerritsquashhash', |
| 1301 | 'abcdef0123456789'],), ''), |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 1302 | ] |
tandrii | 8818977 | 2016-09-29 04:29:57 -0700 | [diff] [blame] | 1303 | calls += [ |
| 1304 | ((['git', 'config', 'rietveld.cc'],), ''), |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1305 | (('AddReviewers', 'chromium-review.googlesource.com', |
tandrii | 8818977 | 2016-09-29 04:29:57 -0700 | [diff] [blame] | 1306 | 123456 if squash else None, |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1307 | ['joe@example.com'] + cc, False, notify), ''), |
tandrii | 8818977 | 2016-09-29 04:29:57 -0700 | [diff] [blame] | 1308 | ] |
tandrii@chromium.org | 1e67bb7 | 2016-02-11 12:15:49 +0000 | [diff] [blame] | 1309 | calls += cls._git_post_upload_calls() |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1310 | return calls |
| 1311 | |
sivachandra@chromium.org | aebe87f | 2012-10-22 20:34:21 +0000 | [diff] [blame] | 1312 | def _run_gerrit_upload_test( |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1313 | self, |
| 1314 | upload_args, |
| 1315 | description, |
tandrii@chromium.org | bf766ba | 2016-04-13 12:51:23 +0000 | [diff] [blame] | 1316 | reviewers=None, |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1317 | squash=True, |
| 1318 | squash_mode=None, |
tandrii@chromium.org | 1062500 | 2016-03-04 20:03:47 +0000 | [diff] [blame] | 1319 | expected_upstream_ref='origin/refs/heads/master', |
tandrii@chromium.org | bf766ba | 2016-04-13 12:51:23 +0000 | [diff] [blame] | 1320 | ref_suffix='', |
Aaron Gable | 9b713dd | 2016-12-14 16:04:21 -0800 | [diff] [blame] | 1321 | title=None, |
tandrii@chromium.org | 8da4540 | 2016-05-24 23:11:03 +0000 | [diff] [blame] | 1322 | notify=False, |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 1323 | post_amend_description=None, |
bradnelson | d975b30 | 2016-10-23 12:20:23 -0700 | [diff] [blame] | 1324 | issue=None, |
| 1325 | cc=None): |
sivachandra@chromium.org | aebe87f | 2012-10-22 20:34:21 +0000 | [diff] [blame] | 1326 | """Generic gerrit upload test framework.""" |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1327 | if squash_mode is None: |
| 1328 | if '--no-squash' in upload_args: |
| 1329 | squash_mode = 'nosquash' |
| 1330 | elif '--squash' in upload_args: |
| 1331 | squash_mode = 'squash' |
| 1332 | else: |
| 1333 | squash_mode = 'default' |
| 1334 | |
tandrii@chromium.org | bf766ba | 2016-04-13 12:51:23 +0000 | [diff] [blame] | 1335 | reviewers = reviewers or [] |
bradnelson | d975b30 | 2016-10-23 12:20:23 -0700 | [diff] [blame] | 1336 | cc = cc or [] |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 1337 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
tandrii | 16e0b4e | 2016-06-07 10:34:28 -0700 | [diff] [blame] | 1338 | self.mock(git_cl.gerrit_util, 'CookiesAuthenticator', |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 1339 | CookiesAuthenticatorMockFactory(same_cookie='same_cred')) |
tandrii | 16e0b4e | 2016-06-07 10:34:28 -0700 | [diff] [blame] | 1340 | self.mock(git_cl._GerritChangelistImpl, '_GerritCommitMsgHookCheck', |
| 1341 | lambda _, offer_removal: None) |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1342 | self.mock(git_cl.gclient_utils, 'RunEditor', |
| 1343 | lambda *_, **__: self._mocked_call(['RunEditor'])) |
| 1344 | self.mock(git_cl, 'DownloadGerritHook', self._mocked_call) |
| 1345 | |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1346 | self.calls = self._gerrit_base_calls( |
| 1347 | issue=issue, |
| 1348 | fetched_description=description) |
luqui@chromium.org | 609f395 | 2015-05-04 22:47:04 +0000 | [diff] [blame] | 1349 | self.calls += self._gerrit_upload_calls( |
| 1350 | description, reviewers, squash, |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1351 | squash_mode=squash_mode, |
tandrii@chromium.org | 1062500 | 2016-03-04 20:03:47 +0000 | [diff] [blame] | 1352 | expected_upstream_ref=expected_upstream_ref, |
Aaron Gable | 9b713dd | 2016-12-14 16:04:21 -0800 | [diff] [blame] | 1353 | ref_suffix=ref_suffix, title=title, notify=notify, |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 1354 | post_amend_description=post_amend_description, |
bradnelson | d975b30 | 2016-10-23 12:20:23 -0700 | [diff] [blame] | 1355 | issue=issue, cc=cc) |
tandrii@chromium.org | 09d7a6a | 2016-03-04 15:44:48 +0000 | [diff] [blame] | 1356 | # Uncomment when debugging. |
| 1357 | # 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] | 1358 | git_cl.main(['upload'] + upload_args) |
| 1359 | |
sivachandra@chromium.org | aebe87f | 2012-10-22 20:34:21 +0000 | [diff] [blame] | 1360 | def test_gerrit_upload_without_change_id(self): |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1361 | self._run_gerrit_upload_test( |
| 1362 | ['--no-squash'], |
| 1363 | 'desc\n\nBUG=\n', |
| 1364 | [], |
| 1365 | squash=False, |
| 1366 | post_amend_description='desc\n\nBUG=\n\nChange-Id: Ixxx') |
| 1367 | |
| 1368 | def test_gerrit_upload_without_change_id_override_nosquash(self): |
tandrii@chromium.org | 1062500 | 2016-03-04 20:03:47 +0000 | [diff] [blame] | 1369 | self.mock(git_cl, 'DownloadGerritHook', self._mocked_call) |
sivachandra@chromium.org | aebe87f | 2012-10-22 20:34:21 +0000 | [diff] [blame] | 1370 | self._run_gerrit_upload_test( |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1371 | [], |
jamesr@chromium.org | 35d1a84 | 2012-07-27 00:20:43 +0000 | [diff] [blame] | 1372 | 'desc\n\nBUG=\n', |
tandrii@chromium.org | 1062500 | 2016-03-04 20:03:47 +0000 | [diff] [blame] | 1373 | [], |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1374 | squash=False, |
| 1375 | squash_mode='override_nosquash', |
tandrii@chromium.org | 1062500 | 2016-03-04 20:03:47 +0000 | [diff] [blame] | 1376 | post_amend_description='desc\n\nBUG=\n\nChange-Id: Ixxx') |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1377 | |
sivachandra@chromium.org | aebe87f | 2012-10-22 20:34:21 +0000 | [diff] [blame] | 1378 | def test_gerrit_no_reviewer(self): |
| 1379 | self._run_gerrit_upload_test( |
| 1380 | [], |
tandrii@chromium.org | 09d7a6a | 2016-03-04 15:44:48 +0000 | [diff] [blame] | 1381 | 'desc\n\nBUG=\n\nChange-Id: I123456789\n', |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1382 | [], |
| 1383 | squash=False, |
| 1384 | squash_mode='override_nosquash') |
sivachandra@chromium.org | aebe87f | 2012-10-22 20:34:21 +0000 | [diff] [blame] | 1385 | |
tandrii | eefe832 | 2016-08-17 10:12:24 -0700 | [diff] [blame] | 1386 | def test_gerrit_patch_bad_chars(self): |
| 1387 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
tandrii@chromium.org | bf766ba | 2016-04-13 12:51:23 +0000 | [diff] [blame] | 1388 | self._run_gerrit_upload_test( |
tandrii | eefe832 | 2016-08-17 10:12:24 -0700 | [diff] [blame] | 1389 | ['-f', '-t', 'Don\'t put bad cha,.rs'], |
tandrii@chromium.org | bf766ba | 2016-04-13 12:51:23 +0000 | [diff] [blame] | 1390 | 'desc\n\nBUG=\n\nChange-Id: I123456789', |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1391 | squash=False, |
| 1392 | squash_mode='override_nosquash', |
Aaron Gable | 9b713dd | 2016-12-14 16:04:21 -0800 | [diff] [blame] | 1393 | title='Dont_put_bad_chars') |
tandrii | eefe832 | 2016-08-17 10:12:24 -0700 | [diff] [blame] | 1394 | self.assertIn( |
| 1395 | 'WARNING: Patchset title may only contain alphanumeric chars ' |
Aaron Gable | eb3af71 | 2017-02-02 12:42:02 -0800 | [diff] [blame] | 1396 | 'and spaces. You can edit it in the UI. See https://crbug.com/663787.\n' |
| 1397 | 'Cleaned up title: Dont put bad chars\n', |
tandrii | eefe832 | 2016-08-17 10:12:24 -0700 | [diff] [blame] | 1398 | git_cl.sys.stdout.getvalue()) |
tandrii@chromium.org | bf766ba | 2016-04-13 12:51:23 +0000 | [diff] [blame] | 1399 | |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1400 | def test_gerrit_reviewers_cmd_line(self): |
sivachandra@chromium.org | aebe87f | 2012-10-22 20:34:21 +0000 | [diff] [blame] | 1401 | self._run_gerrit_upload_test( |
tandrii@chromium.org | 8da4540 | 2016-05-24 23:11:03 +0000 | [diff] [blame] | 1402 | ['-r', 'foo@example.com', '--send-mail'], |
tandrii@chromium.org | 09d7a6a | 2016-03-04 15:44:48 +0000 | [diff] [blame] | 1403 | 'desc\n\nBUG=\n\nChange-Id: I123456789', |
tandrii@chromium.org | 8da4540 | 2016-05-24 23:11:03 +0000 | [diff] [blame] | 1404 | ['foo@example.com'], |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1405 | squash=False, |
| 1406 | squash_mode='override_nosquash', |
tandrii@chromium.org | 8da4540 | 2016-05-24 23:11:03 +0000 | [diff] [blame] | 1407 | notify=True) |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1408 | |
| 1409 | def test_gerrit_reviewer_multiple(self): |
sivachandra@chromium.org | aebe87f | 2012-10-22 20:34:21 +0000 | [diff] [blame] | 1410 | self._run_gerrit_upload_test( |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1411 | [], |
bradnelson | d975b30 | 2016-10-23 12:20:23 -0700 | [diff] [blame] | 1412 | 'desc\nTBR=reviewer@example.com\nBUG=\nR=another@example.com\n' |
| 1413 | 'CC=more@example.com,people@example.com\n\n' |
tandrii@chromium.org | 09d7a6a | 2016-03-04 15:44:48 +0000 | [diff] [blame] | 1414 | 'Change-Id: 123456789\n', |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1415 | ['reviewer@example.com', 'another@example.com'], |
| 1416 | squash=False, |
tandrii | 99a72f2 | 2016-08-17 14:33:24 -0700 | [diff] [blame] | 1417 | squash_mode='override_nosquash', |
bradnelson | d975b30 | 2016-10-23 12:20:23 -0700 | [diff] [blame] | 1418 | ref_suffix='%l=Code-Review+1', |
| 1419 | cc=['more@example.com', 'people@example.com']) |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1420 | |
| 1421 | def test_gerrit_upload_squash_first_is_default(self): |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 1422 | self._run_gerrit_upload_test( |
| 1423 | [], |
| 1424 | 'desc\nBUG=\n\nChange-Id: 123456789', |
| 1425 | [], |
| 1426 | expected_upstream_ref='origin/master') |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1427 | |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 1428 | def test_gerrit_upload_squash_first(self): |
bauerb@chromium.org | 27386dd | 2015-02-16 10:45:39 +0000 | [diff] [blame] | 1429 | self._run_gerrit_upload_test( |
| 1430 | ['--squash'], |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 1431 | 'desc\nBUG=\n\nChange-Id: 123456789', |
bauerb@chromium.org | 27386dd | 2015-02-16 10:45:39 +0000 | [diff] [blame] | 1432 | [], |
luqui@chromium.org | 609f395 | 2015-05-04 22:47:04 +0000 | [diff] [blame] | 1433 | squash=True, |
| 1434 | expected_upstream_ref='origin/master') |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1435 | |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 1436 | def test_gerrit_upload_squash_reupload(self): |
| 1437 | description = 'desc\nBUG=\n\nChange-Id: 123456789' |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 1438 | self._run_gerrit_upload_test( |
| 1439 | ['--squash'], |
| 1440 | description, |
| 1441 | [], |
| 1442 | squash=True, |
| 1443 | expected_upstream_ref='origin/master', |
| 1444 | issue=123456) |
| 1445 | |
rmistry@google.com | 2dd9986 | 2015-06-22 12:22:18 +0000 | [diff] [blame] | 1446 | def test_upload_branch_deps(self): |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 1447 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
rmistry@google.com | 2dd9986 | 2015-06-22 12:22:18 +0000 | [diff] [blame] | 1448 | def mock_run_git(*args, **_kwargs): |
| 1449 | if args[0] == ['for-each-ref', |
| 1450 | '--format=%(refname:short) %(upstream:short)', |
| 1451 | 'refs/heads']: |
| 1452 | # Create a local branch dependency tree that looks like this: |
| 1453 | # test1 -> test2 -> test3 -> test4 -> test5 |
| 1454 | # -> test3.1 |
| 1455 | # test6 -> test0 |
| 1456 | branch_deps = [ |
| 1457 | 'test2 test1', # test1 -> test2 |
| 1458 | 'test3 test2', # test2 -> test3 |
| 1459 | 'test3.1 test2', # test2 -> test3.1 |
| 1460 | 'test4 test3', # test3 -> test4 |
| 1461 | 'test5 test4', # test4 -> test5 |
| 1462 | 'test6 test0', # test0 -> test6 |
| 1463 | 'test7', # test7 |
| 1464 | ] |
| 1465 | return '\n'.join(branch_deps) |
| 1466 | self.mock(git_cl, 'RunGit', mock_run_git) |
| 1467 | |
| 1468 | class RecordCalls: |
| 1469 | times_called = 0 |
| 1470 | record_calls = RecordCalls() |
| 1471 | def mock_CMDupload(*args, **_kwargs): |
| 1472 | record_calls.times_called += 1 |
| 1473 | return 0 |
| 1474 | self.mock(git_cl, 'CMDupload', mock_CMDupload) |
| 1475 | |
| 1476 | self.calls = [ |
| 1477 | (('[Press enter to continue or ctrl-C to quit]',), ''), |
| 1478 | ] |
| 1479 | |
| 1480 | class MockChangelist(): |
| 1481 | def __init__(self): |
| 1482 | pass |
| 1483 | def GetBranch(self): |
| 1484 | return 'test1' |
| 1485 | def GetIssue(self): |
| 1486 | return '123' |
| 1487 | def GetPatchset(self): |
| 1488 | return '1001' |
tandrii@chromium.org | 4c72b08 | 2016-03-31 22:26:35 +0000 | [diff] [blame] | 1489 | def IsGerrit(self): |
| 1490 | return False |
rmistry@google.com | 2dd9986 | 2015-06-22 12:22:18 +0000 | [diff] [blame] | 1491 | |
| 1492 | ret = git_cl.upload_branch_deps(MockChangelist(), []) |
| 1493 | # CMDupload should have been called 5 times because of 5 dependent branches. |
| 1494 | self.assertEquals(5, record_calls.times_called) |
| 1495 | self.assertEquals(0, ret) |
| 1496 | |
tandrii@chromium.org | 65874e1 | 2016-03-04 12:03:02 +0000 | [diff] [blame] | 1497 | def test_gerrit_change_id(self): |
| 1498 | self.calls = [ |
| 1499 | ((['git', 'write-tree'], ), |
| 1500 | 'hashtree'), |
| 1501 | ((['git', 'rev-parse', 'HEAD~0'], ), |
| 1502 | 'branch-parent'), |
| 1503 | ((['git', 'var', 'GIT_AUTHOR_IDENT'], ), |
| 1504 | 'A B <a@b.org> 1456848326 +0100'), |
| 1505 | ((['git', 'var', 'GIT_COMMITTER_IDENT'], ), |
| 1506 | 'C D <c@d.org> 1456858326 +0100'), |
| 1507 | ((['git', 'hash-object', '-t', 'commit', '--stdin'], ), |
| 1508 | 'hashchange'), |
| 1509 | ] |
| 1510 | change_id = git_cl.GenerateGerritChangeId('line1\nline2\n') |
| 1511 | self.assertEqual(change_id, 'Ihashchange') |
| 1512 | |
tandrii@chromium.org | 601e1d1 | 2016-06-03 13:03:54 +0000 | [diff] [blame] | 1513 | def test_desecription_append_footer(self): |
| 1514 | for init_desc, footer_line, expected_desc in [ |
| 1515 | # Use unique desc first lines for easy test failure identification. |
| 1516 | ('foo', 'R=one', 'foo\n\nR=one'), |
| 1517 | ('foo\n\nR=one', 'BUG=', 'foo\n\nR=one\nBUG='), |
| 1518 | ('foo\n\nR=one', 'Change-Id: Ixx', 'foo\n\nR=one\n\nChange-Id: Ixx'), |
| 1519 | ('foo\n\nChange-Id: Ixx', 'R=one', 'foo\n\nR=one\n\nChange-Id: Ixx'), |
| 1520 | ('foo\n\nR=one\n\nChange-Id: Ixx', 'TBR=two', |
| 1521 | 'foo\n\nR=one\nTBR=two\n\nChange-Id: Ixx'), |
| 1522 | ('foo\n\nR=one\n\nChange-Id: Ixx', 'Foo-Bar: baz', |
| 1523 | 'foo\n\nR=one\n\nChange-Id: Ixx\nFoo-Bar: baz'), |
| 1524 | ('foo\n\nChange-Id: Ixx', 'Foo-Bak: baz', |
| 1525 | 'foo\n\nChange-Id: Ixx\nFoo-Bak: baz'), |
| 1526 | ('foo', 'Change-Id: Ixx', 'foo\n\nChange-Id: Ixx'), |
| 1527 | ]: |
| 1528 | desc = git_cl.ChangeDescription(init_desc) |
| 1529 | desc.append_footer(footer_line) |
| 1530 | self.assertEqual(desc.description, expected_desc) |
| 1531 | |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 1532 | def test_update_reviewers(self): |
| 1533 | data = [ |
| 1534 | ('foo', [], 'foo'), |
agable@chromium.org | 42c2079 | 2013-09-12 17:34:49 +0000 | [diff] [blame] | 1535 | ('foo\nR=xx', [], 'foo\nR=xx'), |
| 1536 | ('foo\nTBR=xx', [], 'foo\nTBR=xx'), |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 1537 | ('foo', ['a@c'], 'foo\n\nR=a@c'), |
agable@chromium.org | 42c2079 | 2013-09-12 17:34:49 +0000 | [diff] [blame] | 1538 | ('foo\nR=xx', ['a@c'], 'foo\n\nR=a@c, xx'), |
| 1539 | ('foo\nTBR=xx', ['a@c'], 'foo\n\nR=a@c\nTBR=xx'), |
| 1540 | ('foo\nTBR=xx\nR=yy', ['a@c'], 'foo\n\nR=a@c, yy\nTBR=xx'), |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 1541 | ('foo\nBUG=', ['a@c'], 'foo\nBUG=\nR=a@c'), |
agable@chromium.org | 42c2079 | 2013-09-12 17:34:49 +0000 | [diff] [blame] | 1542 | ('foo\nR=xx\nTBR=yy\nR=bar', ['a@c'], 'foo\n\nR=a@c, xx, bar\nTBR=yy'), |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 1543 | ('foo', ['a@c', 'b@c'], 'foo\n\nR=a@c, b@c'), |
maruel@chromium.org | c6f60e8 | 2013-04-19 17:01:57 +0000 | [diff] [blame] | 1544 | ('foo\nBar\n\nR=\nBUG=', ['c@c'], 'foo\nBar\n\nR=c@c\nBUG='), |
| 1545 | ('foo\nBar\n\nR=\nBUG=\nR=', ['c@c'], 'foo\nBar\n\nR=c@c\nBUG='), |
| 1546 | # Same as the line before, but full of whitespaces. |
| 1547 | ( |
| 1548 | 'foo\nBar\n\n R = \n BUG = \n R = ', ['c@c'], |
| 1549 | 'foo\nBar\n\nR=c@c\n BUG =', |
| 1550 | ), |
| 1551 | # Whitespaces aren't interpreted as new lines. |
| 1552 | ('foo BUG=allo R=joe ', ['c@c'], 'foo BUG=allo R=joe\n\nR=c@c'), |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 1553 | ] |
maruel@chromium.org | c6f60e8 | 2013-04-19 17:01:57 +0000 | [diff] [blame] | 1554 | expected = [i[2] for i in data] |
| 1555 | actual = [] |
| 1556 | for orig, reviewers, _expected in data: |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 1557 | obj = git_cl.ChangeDescription(orig) |
| 1558 | obj.update_reviewers(reviewers) |
maruel@chromium.org | c6f60e8 | 2013-04-19 17:01:57 +0000 | [diff] [blame] | 1559 | actual.append(obj.description) |
| 1560 | self.assertEqual(expected, actual) |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 1561 | |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 1562 | def test_get_target_ref(self): |
| 1563 | # Check remote or remote branch not present. |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1564 | self.assertEqual(None, git_cl.GetTargetRef('origin', None, 'master')) |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 1565 | self.assertEqual(None, git_cl.GetTargetRef(None, |
| 1566 | 'refs/remotes/origin/master', |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1567 | 'master')) |
bauerb@chromium.org | 27386dd | 2015-02-16 10:45:39 +0000 | [diff] [blame] | 1568 | |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 1569 | # Check default target refs for branches. |
| 1570 | self.assertEqual('refs/heads/master', |
| 1571 | git_cl.GetTargetRef('origin', 'refs/remotes/origin/master', |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1572 | None)) |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 1573 | self.assertEqual('refs/heads/master', |
| 1574 | git_cl.GetTargetRef('origin', 'refs/remotes/origin/lkgr', |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1575 | None)) |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 1576 | self.assertEqual('refs/heads/master', |
| 1577 | git_cl.GetTargetRef('origin', 'refs/remotes/origin/lkcr', |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1578 | None)) |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 1579 | self.assertEqual('refs/branch-heads/123', |
| 1580 | git_cl.GetTargetRef('origin', |
| 1581 | 'refs/remotes/branch-heads/123', |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1582 | None)) |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 1583 | self.assertEqual('refs/diff/test', |
| 1584 | git_cl.GetTargetRef('origin', |
| 1585 | 'refs/remotes/origin/refs/diff/test', |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1586 | None)) |
rmistry@google.com | c68112d | 2015-03-03 12:48:06 +0000 | [diff] [blame] | 1587 | self.assertEqual('refs/heads/chrome/m42', |
| 1588 | git_cl.GetTargetRef('origin', |
| 1589 | 'refs/remotes/origin/chrome/m42', |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1590 | None)) |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 1591 | |
| 1592 | # Check target refs for user-specified target branch. |
| 1593 | for branch in ('branch-heads/123', 'remotes/branch-heads/123', |
| 1594 | 'refs/remotes/branch-heads/123'): |
| 1595 | self.assertEqual('refs/branch-heads/123', |
| 1596 | git_cl.GetTargetRef('origin', |
| 1597 | 'refs/remotes/origin/master', |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1598 | branch)) |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 1599 | for branch in ('origin/master', 'remotes/origin/master', |
| 1600 | 'refs/remotes/origin/master'): |
| 1601 | self.assertEqual('refs/heads/master', |
| 1602 | git_cl.GetTargetRef('origin', |
| 1603 | 'refs/remotes/branch-heads/123', |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1604 | branch)) |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 1605 | for branch in ('master', 'heads/master', 'refs/heads/master'): |
| 1606 | self.assertEqual('refs/heads/master', |
| 1607 | git_cl.GetTargetRef('origin', |
| 1608 | 'refs/remotes/branch-heads/123', |
Andrii Shyshkalov | f3a20ae | 2017-01-24 21:23:57 +0100 | [diff] [blame] | 1609 | branch)) |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 1610 | |
wychen@chromium.org | a872e75 | 2015-04-28 23:42:18 +0000 | [diff] [blame] | 1611 | def test_patch_when_dirty(self): |
| 1612 | # Patch when local tree is dirty |
| 1613 | self.mock(git_common, 'is_dirty_git_tree', lambda x: True) |
| 1614 | self.assertNotEqual(git_cl.main(['patch', '123456']), 0) |
| 1615 | |
| 1616 | def test_diff_when_dirty(self): |
| 1617 | # Do 'git cl diff' when local tree is dirty |
| 1618 | self.mock(git_common, 'is_dirty_git_tree', lambda x: True) |
| 1619 | self.assertNotEqual(git_cl.main(['diff']), 0) |
| 1620 | |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1621 | @staticmethod |
| 1622 | def _get_gerrit_codereview_server_calls(branch, value=None, |
| 1623 | git_short_host='host', |
| 1624 | detect_branch=True): |
| 1625 | """Returns calls executed by _GerritChangelistImpl.GetCodereviewServer. |
| 1626 | |
| 1627 | If value is given, branch.<BRANCH>.gerritcodereview is already set. |
| 1628 | """ |
| 1629 | calls = [] |
| 1630 | if detect_branch: |
| 1631 | calls.append(((['git', 'symbolic-ref', 'HEAD'],), branch)) |
| 1632 | calls.append(((['git', 'config', 'branch.' + branch + '.gerritserver'],), |
| 1633 | CERR1 if value is None else value)) |
| 1634 | if value is None: |
| 1635 | calls += [ |
| 1636 | ((['git', 'config', 'branch.' + branch + '.merge'],), |
| 1637 | 'refs/heads' + branch), |
| 1638 | ((['git', 'config', 'branch.' + branch + '.remote'],), |
| 1639 | 'origin'), |
| 1640 | ((['git', 'config', 'remote.origin.url'],), |
| 1641 | 'https://%s.googlesource.com/my/repo' % git_short_host), |
| 1642 | ] |
| 1643 | return calls |
| 1644 | |
tandrii | df09a46 | 2016-08-18 16:23:55 -0700 | [diff] [blame] | 1645 | def _patch_common(self, is_gerrit=False, force_codereview=False, |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1646 | new_branch=False, git_short_host='host', |
| 1647 | detect_gerrit_server=True): |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 1648 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 1649 | self.mock(git_cl._RietveldChangelistImpl, 'GetMostRecentPatchset', |
| 1650 | lambda x: '60001') |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1651 | self.mock(git_cl._RietveldChangelistImpl, 'FetchDescription', |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 1652 | lambda *args: 'Description') |
wychen@chromium.org | a872e75 | 2015-04-28 23:42:18 +0000 | [diff] [blame] | 1653 | self.mock(git_cl, 'IsGitVersionAtLeast', lambda *args: True) |
| 1654 | |
tandrii | df09a46 | 2016-08-18 16:23:55 -0700 | [diff] [blame] | 1655 | if new_branch: |
| 1656 | self.calls = [((['git', 'new-branch', 'master'],), ''),] |
| 1657 | else: |
| 1658 | self.calls = [((['git', 'symbolic-ref', 'HEAD'],), 'master')] |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1659 | |
tandrii@chromium.org | dde6462 | 2016-04-13 17:11:21 +0000 | [diff] [blame] | 1660 | if not force_codereview: |
| 1661 | # These calls detect codereview to use. |
tandrii@chromium.org | c2786d9 | 2016-05-31 19:53:50 +0000 | [diff] [blame] | 1662 | self.calls += [ |
tandrii@chromium.org | dde6462 | 2016-04-13 17:11:21 +0000 | [diff] [blame] | 1663 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 1664 | ((['git', 'config', 'branch.master.rietveldissue'],), CERR1), |
| 1665 | ((['git', 'config', 'branch.master.gerritissue'],), CERR1), |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 1666 | ((['git', 'config', 'rietveld.autoupdate'],), CERR1), |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1667 | ] |
tandrii@chromium.org | dde6462 | 2016-04-13 17:11:21 +0000 | [diff] [blame] | 1668 | |
| 1669 | if is_gerrit: |
| 1670 | if not force_codereview: |
| 1671 | self.calls += [ |
| 1672 | ((['git', 'config', 'gerrit.host'],), 'true'), |
| 1673 | ] |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1674 | if detect_gerrit_server: |
| 1675 | self.calls += self._get_gerrit_codereview_server_calls( |
| 1676 | 'master', git_short_host=git_short_host, |
| 1677 | detect_branch=not new_branch and force_codereview) |
| 1678 | |
| 1679 | self.calls += [ |
| 1680 | (('GetChangeDetail', git_short_host + '-review.googlesource.com', |
Andrii Shyshkalov | 8039be7 | 2017-01-26 09:38:18 +0100 | [diff] [blame] | 1681 | '123456', ['ALL_REVISIONS', 'CURRENT_COMMIT']), |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1682 | { |
| 1683 | 'current_revision': '7777777777', |
| 1684 | 'revisions': { |
| 1685 | '1111111111': { |
| 1686 | '_number': 1, |
| 1687 | 'fetch': {'http': { |
| 1688 | 'url': 'https://%s.googlesource.com/my/repo' % git_short_host, |
| 1689 | 'ref': 'refs/changes/56/123456/1', |
| 1690 | }}, |
| 1691 | }, |
| 1692 | '7777777777': { |
| 1693 | '_number': 7, |
| 1694 | 'fetch': {'http': { |
| 1695 | 'url': 'https://%s.googlesource.com/my/repo' % git_short_host, |
| 1696 | 'ref': 'refs/changes/56/123456/7', |
| 1697 | }}, |
| 1698 | }, |
| 1699 | }, |
| 1700 | }), |
| 1701 | ] |
tandrii@chromium.org | dde6462 | 2016-04-13 17:11:21 +0000 | [diff] [blame] | 1702 | else: |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1703 | self.calls += [ |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 1704 | ((['git', 'config', 'gerrit.host'],), CERR1), |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1705 | ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'), |
skobes | 6468b90 | 2016-10-24 08:45:10 -0700 | [diff] [blame] | 1706 | ((['git', 'config', 'branch.master.rietveldserver',],), CERR1), |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1707 | ((['git', 'rev-parse', '--show-cdup'],), ''), |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1708 | ] |
wychen@chromium.org | a872e75 | 2015-04-28 23:42:18 +0000 | [diff] [blame] | 1709 | |
tandrii | df09a46 | 2016-08-18 16:23:55 -0700 | [diff] [blame] | 1710 | def _common_patch_successful(self, new_branch=False): |
| 1711 | self._patch_common(new_branch=new_branch) |
wychen@chromium.org | a872e75 | 2015-04-28 23:42:18 +0000 | [diff] [blame] | 1712 | self.calls += [ |
wychen@chromium.org | a872e75 | 2015-04-28 23:42:18 +0000 | [diff] [blame] | 1713 | ((['git', 'commit', '-m', |
wychen@chromium.org | 5b3bebb | 2015-05-28 21:41:43 +0000 | [diff] [blame] | 1714 | 'Description\n\n' + |
wychen@chromium.org | a872e75 | 2015-04-28 23:42:18 +0000 | [diff] [blame] | 1715 | 'patch from issue 123456 at patchset 60001 ' + |
| 1716 | '(http://crrev.com/123456#ps60001)'],), ''), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 1717 | ((['git', 'config', 'branch.master.rietveldissue', '123456'],), |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 1718 | ''), |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1719 | ((['git', 'config', 'branch.master.rietveldserver', |
| 1720 | 'https://codereview.example.com'],), ''), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 1721 | ((['git', 'config', 'branch.master.rietveldpatchset', '60001'],), |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 1722 | ''), |
wychen@chromium.org | a872e75 | 2015-04-28 23:42:18 +0000 | [diff] [blame] | 1723 | ] |
tandrii@chromium.org | c2786d9 | 2016-05-31 19:53:50 +0000 | [diff] [blame] | 1724 | |
| 1725 | def test_patch_successful(self): |
| 1726 | self._common_patch_successful() |
wychen@chromium.org | a872e75 | 2015-04-28 23:42:18 +0000 | [diff] [blame] | 1727 | self.assertEqual(git_cl.main(['patch', '123456']), 0) |
| 1728 | |
tandrii@chromium.org | c2786d9 | 2016-05-31 19:53:50 +0000 | [diff] [blame] | 1729 | def test_patch_successful_new_branch(self): |
tandrii | df09a46 | 2016-08-18 16:23:55 -0700 | [diff] [blame] | 1730 | self._common_patch_successful(new_branch=True) |
tandrii@chromium.org | c2786d9 | 2016-05-31 19:53:50 +0000 | [diff] [blame] | 1731 | self.assertEqual(git_cl.main(['patch', '-b', 'master', '123456']), 0) |
| 1732 | |
wychen@chromium.org | a872e75 | 2015-04-28 23:42:18 +0000 | [diff] [blame] | 1733 | def test_patch_conflict(self): |
| 1734 | self._patch_common() |
skobes | 6468b90 | 2016-10-24 08:45:10 -0700 | [diff] [blame] | 1735 | GitCheckoutMock.conflict = True |
wychen@chromium.org | a872e75 | 2015-04-28 23:42:18 +0000 | [diff] [blame] | 1736 | self.assertNotEqual(git_cl.main(['patch', '123456']), 0) |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 1737 | |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1738 | def test_gerrit_patch_successful(self): |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1739 | self._patch_common(is_gerrit=True, git_short_host='chromium', |
| 1740 | detect_gerrit_server=True) |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1741 | self.calls += [ |
| 1742 | ((['git', 'fetch', 'https://chromium.googlesource.com/my/repo', |
| 1743 | 'refs/changes/56/123456/7'],), ''), |
| 1744 | ((['git', 'cherry-pick', 'FETCH_HEAD'],), ''), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 1745 | ((['git', 'config', 'branch.master.gerritissue', '123456'],), |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 1746 | ''), |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1747 | ((['git', 'config', 'branch.master.gerritserver', |
| 1748 | 'https://chromium-review.googlesource.com'],), ''), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 1749 | ((['git', 'config', 'branch.master.gerritpatchset', '7'],), ''), |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1750 | ] |
| 1751 | self.assertEqual(git_cl.main(['patch', '123456']), 0) |
| 1752 | |
tandrii@chromium.org | dde6462 | 2016-04-13 17:11:21 +0000 | [diff] [blame] | 1753 | def test_patch_force_codereview(self): |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1754 | self._patch_common(is_gerrit=True, force_codereview=True, |
| 1755 | git_short_host='host', detect_gerrit_server=True) |
tandrii@chromium.org | dde6462 | 2016-04-13 17:11:21 +0000 | [diff] [blame] | 1756 | self.calls += [ |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1757 | ((['git', 'fetch', 'https://host.googlesource.com/my/repo', |
tandrii@chromium.org | dde6462 | 2016-04-13 17:11:21 +0000 | [diff] [blame] | 1758 | 'refs/changes/56/123456/7'],), ''), |
| 1759 | ((['git', 'cherry-pick', 'FETCH_HEAD'],), ''), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 1760 | ((['git', 'config', 'branch.master.gerritissue', '123456'],), |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 1761 | ''), |
tandrii@chromium.org | dde6462 | 2016-04-13 17:11:21 +0000 | [diff] [blame] | 1762 | ((['git', 'config', 'branch.master.gerritserver', |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1763 | 'https://host-review.googlesource.com'],), ''), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 1764 | ((['git', 'config', 'branch.master.gerritpatchset', '7'],), ''), |
tandrii@chromium.org | dde6462 | 2016-04-13 17:11:21 +0000 | [diff] [blame] | 1765 | ] |
| 1766 | self.assertEqual(git_cl.main(['patch', '--gerrit', '123456']), 0) |
| 1767 | |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1768 | def test_gerrit_patch_url_successful(self): |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1769 | self._patch_common(is_gerrit=True, git_short_host='else', |
| 1770 | detect_gerrit_server=False) |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1771 | self.calls += [ |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1772 | ((['git', 'fetch', 'https://else.googlesource.com/my/repo', |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1773 | 'refs/changes/56/123456/1'],), ''), |
| 1774 | ((['git', 'cherry-pick', 'FETCH_HEAD'],), ''), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 1775 | ((['git', 'config', 'branch.master.gerritissue', '123456'],), |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 1776 | ''), |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1777 | ((['git', 'config', 'branch.master.gerritserver', |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1778 | 'https://else-review.googlesource.com'],), ''), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 1779 | ((['git', 'config', 'branch.master.gerritpatchset', '1'],), ''), |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1780 | ] |
| 1781 | self.assertEqual(git_cl.main( |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1782 | ['patch', 'https://else-review.googlesource.com/#/c/123456/1']), 0) |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1783 | |
| 1784 | def test_gerrit_patch_conflict(self): |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 1785 | self._patch_common(is_gerrit=True, detect_gerrit_server=False, |
| 1786 | git_short_host='chromium') |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1787 | self.calls += [ |
| 1788 | ((['git', 'fetch', 'https://chromium.googlesource.com/my/repo', |
| 1789 | 'refs/changes/56/123456/1'],), ''), |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 1790 | ((['git', 'cherry-pick', 'FETCH_HEAD'],), CERR1), |
| 1791 | ((['DieWithError', 'Command "git cherry-pick FETCH_HEAD" failed.\n'],), |
| 1792 | SystemExitMock()), |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1793 | ] |
| 1794 | with self.assertRaises(SystemExitMock): |
| 1795 | git_cl.main(['patch', |
| 1796 | 'https://chromium-review.googlesource.com/#/c/123456/1']) |
| 1797 | |
tandrii | c2405f5 | 2016-10-10 08:13:15 -0700 | [diff] [blame] | 1798 | def test_gerrit_patch_not_exists(self): |
Andrii Shyshkalov | c6c8b4c | 2016-11-09 20:51:20 +0100 | [diff] [blame] | 1799 | def notExists(_issue, *_, **kwargs): |
| 1800 | self.assertFalse(kwargs['ignore_404']) |
| 1801 | raise git_cl.gerrit_util.GerritError(404, '') |
| 1802 | self.mock(git_cl.gerrit_util, 'GetChangeDetail', notExists) |
| 1803 | |
tandrii | c2405f5 | 2016-10-10 08:13:15 -0700 | [diff] [blame] | 1804 | url = 'https://chromium-review.googlesource.com' |
tandrii | c2405f5 | 2016-10-10 08:13:15 -0700 | [diff] [blame] | 1805 | self.calls = [ |
| 1806 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
| 1807 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
| 1808 | ((['git', 'config', 'branch.master.rietveldissue'],), CERR1), |
| 1809 | ((['git', 'config', 'branch.master.gerritissue'],), CERR1), |
| 1810 | ((['git', 'config', 'rietveld.autoupdate'],), CERR1), |
| 1811 | ((['git', 'config', 'gerrit.host'],), 'true'), |
Aaron Gable | a45ee11 | 2016-11-22 15:14:38 -0800 | [diff] [blame] | 1812 | ((['DieWithError', 'change 123456 at ' + url + ' does not exist ' |
tandrii | c2405f5 | 2016-10-10 08:13:15 -0700 | [diff] [blame] | 1813 | 'or you have no access to it'],), SystemExitMock()), |
| 1814 | ] |
| 1815 | with self.assertRaises(SystemExitMock): |
| 1816 | self.assertEqual(1, git_cl.main(['patch', url + '/#/c/123456/1'])) |
| 1817 | |
tandrii@chromium.org | 5df290f | 2016-04-11 16:12:29 +0000 | [diff] [blame] | 1818 | def _checkout_calls(self): |
| 1819 | return [ |
| 1820 | ((['git', 'config', '--local', '--get-regexp', |
| 1821 | 'branch\\..*\\.rietveldissue'], ), |
| 1822 | ('branch.retrying.rietveldissue 1111111111\n' |
| 1823 | 'branch.some-fix.rietveldissue 2222222222\n')), |
| 1824 | ((['git', 'config', '--local', '--get-regexp', |
| 1825 | 'branch\\..*\\.gerritissue'], ), |
| 1826 | ('branch.ger-branch.gerritissue 123456\n' |
| 1827 | 'branch.gbranch654.gerritissue 654321\n')), |
| 1828 | ] |
| 1829 | |
| 1830 | def test_checkout_gerrit(self): |
| 1831 | """Tests git cl checkout <issue>.""" |
| 1832 | self.calls = self._checkout_calls() |
| 1833 | self.calls += [((['git', 'checkout', 'ger-branch'], ), '')] |
| 1834 | self.assertEqual(0, git_cl.main(['checkout', '123456'])) |
| 1835 | |
| 1836 | def test_checkout_rietveld(self): |
| 1837 | """Tests git cl checkout <issue>.""" |
| 1838 | self.calls = self._checkout_calls() |
| 1839 | self.calls += [((['git', 'checkout', 'some-fix'], ), '')] |
| 1840 | self.assertEqual(0, git_cl.main(['checkout', '2222222222'])) |
| 1841 | |
| 1842 | def test_checkout_not_found(self): |
| 1843 | """Tests git cl checkout <issue>.""" |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 1844 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
tandrii@chromium.org | 5df290f | 2016-04-11 16:12:29 +0000 | [diff] [blame] | 1845 | self.calls = self._checkout_calls() |
| 1846 | self.assertEqual(1, git_cl.main(['checkout', '99999'])) |
| 1847 | |
tandrii@chromium.org | 26c8fd2 | 2016-04-11 21:33:21 +0000 | [diff] [blame] | 1848 | def test_checkout_no_branch_issues(self): |
| 1849 | """Tests git cl checkout <issue>.""" |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 1850 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
tandrii@chromium.org | 26c8fd2 | 2016-04-11 21:33:21 +0000 | [diff] [blame] | 1851 | self.calls = [ |
| 1852 | ((['git', 'config', '--local', '--get-regexp', |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 1853 | 'branch\\..*\\.rietveldissue'], ), CERR1), |
tandrii@chromium.org | 26c8fd2 | 2016-04-11 21:33:21 +0000 | [diff] [blame] | 1854 | ((['git', 'config', '--local', '--get-regexp', |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 1855 | 'branch\\..*\\.gerritissue'], ), CERR1), |
tandrii@chromium.org | 26c8fd2 | 2016-04-11 21:33:21 +0000 | [diff] [blame] | 1856 | ] |
| 1857 | self.assertEqual(1, git_cl.main(['checkout', '99999'])) |
| 1858 | |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 1859 | def _test_gerrit_ensure_authenticated_common(self, auth, |
| 1860 | skip_auth_check=False): |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 1861 | self.mock(git_cl.gerrit_util, 'CookiesAuthenticator', |
| 1862 | CookiesAuthenticatorMockFactory(hosts_with_creds=auth)) |
| 1863 | self.mock(git_cl, 'DieWithError', |
Christopher Lam | f732cd5 | 2017-01-24 12:40:11 +1100 | [diff] [blame] | 1864 | lambda msg, change=None: self._mocked_call(['DieWithError', msg])) |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 1865 | self.mock(git_cl, 'ask_for_data', |
| 1866 | lambda msg: self._mocked_call(['ask_for_data', msg])) |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 1867 | 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] | 1868 | cl = git_cl.Changelist(codereview='gerrit') |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 1869 | cl.branch = 'master' |
| 1870 | cl.branchref = 'refs/heads/master' |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 1871 | cl.lookedup_issue = True |
| 1872 | return cl |
| 1873 | |
| 1874 | def test_gerrit_ensure_authenticated_missing(self): |
| 1875 | cl = self._test_gerrit_ensure_authenticated_common(auth={ |
| 1876 | 'chromium.googlesource.com': 'git is ok, but gerrit one is missing', |
| 1877 | }) |
| 1878 | self.calls.append( |
| 1879 | ((['DieWithError', |
| 1880 | 'Credentials for the following hosts are required:\n' |
| 1881 | ' chromium-review.googlesource.com\n' |
| 1882 | 'These are read from ~/.gitcookies (or legacy ~/.netrc)\n' |
| 1883 | 'You can (re)generate your credentails by visiting ' |
| 1884 | 'https://chromium-review.googlesource.com/new-password'],), ''),) |
| 1885 | self.assertIsNone(cl.EnsureAuthenticated(force=False)) |
| 1886 | |
| 1887 | def test_gerrit_ensure_authenticated_conflict(self): |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 1888 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 1889 | cl = self._test_gerrit_ensure_authenticated_common(auth={ |
| 1890 | 'chromium.googlesource.com': 'one', |
| 1891 | 'chromium-review.googlesource.com': 'other', |
| 1892 | }) |
| 1893 | self.calls.append( |
| 1894 | ((['ask_for_data', 'If you know what you are doing, ' |
qyearsley | 53f48a1 | 2016-09-01 10:45:13 -0700 | [diff] [blame] | 1895 | 'press Enter to continue, Ctrl+C to abort.'],), '')) |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 1896 | self.assertIsNone(cl.EnsureAuthenticated(force=False)) |
| 1897 | |
| 1898 | def test_gerrit_ensure_authenticated_ok(self): |
| 1899 | cl = self._test_gerrit_ensure_authenticated_common(auth={ |
| 1900 | 'chromium.googlesource.com': 'same', |
| 1901 | 'chromium-review.googlesource.com': 'same', |
| 1902 | }) |
| 1903 | self.assertIsNone(cl.EnsureAuthenticated(force=False)) |
| 1904 | |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 1905 | def test_gerrit_ensure_authenticated_skipped(self): |
| 1906 | cl = self._test_gerrit_ensure_authenticated_common( |
| 1907 | auth={}, skip_auth_check=True) |
| 1908 | self.assertIsNone(cl.EnsureAuthenticated(force=False)) |
| 1909 | |
tandrii@chromium.org | fa330e8 | 2016-04-13 17:09:52 +0000 | [diff] [blame] | 1910 | def test_cmd_set_commit_rietveld(self): |
tandrii | 4d84359 | 2016-07-27 08:22:56 -0700 | [diff] [blame] | 1911 | self.mock(git_cl._RietveldChangelistImpl, 'SetFlags', |
| 1912 | lambda _, v: self._mocked_call(['SetFlags', v])) |
tandrii@chromium.org | fa330e8 | 2016-04-13 17:09:52 +0000 | [diff] [blame] | 1913 | self.calls = [ |
| 1914 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 1915 | ((['git', 'config', 'branch.feature.rietveldissue'],), '123'), |
tandrii@chromium.org | fa330e8 | 2016-04-13 17:09:52 +0000 | [diff] [blame] | 1916 | ((['git', 'config', 'rietveld.autoupdate'],), ''), |
| 1917 | ((['git', 'config', 'rietveld.server'],), ''), |
| 1918 | ((['git', 'config', 'rietveld.server'],), ''), |
| 1919 | ((['git', 'config', 'branch.feature.rietveldserver'],), |
| 1920 | 'https://codereview.chromium.org'), |
tandrii | 4d84359 | 2016-07-27 08:22:56 -0700 | [diff] [blame] | 1921 | ((['SetFlags', {'commit': '1', 'cq_dry_run': '0'}], ), ''), |
tandrii@chromium.org | fa330e8 | 2016-04-13 17:09:52 +0000 | [diff] [blame] | 1922 | ] |
| 1923 | self.assertEqual(0, git_cl.main(['set-commit'])) |
| 1924 | |
Andrii Shyshkalov | 828701b | 2016-12-09 10:46:47 +0100 | [diff] [blame] | 1925 | def _cmd_set_commit_gerrit_common(self, vote, notify=None): |
tandrii@chromium.org | fa330e8 | 2016-04-13 17:09:52 +0000 | [diff] [blame] | 1926 | self.mock(git_cl.gerrit_util, 'SetReview', |
Andrii Shyshkalov | 828701b | 2016-12-09 10:46:47 +0100 | [diff] [blame] | 1927 | lambda h, i, labels, notify=None: |
| 1928 | self._mocked_call(['SetReview', h, i, labels, notify])) |
| 1929 | |
tandrii@chromium.org | fa330e8 | 2016-04-13 17:09:52 +0000 | [diff] [blame] | 1930 | self.calls = [ |
| 1931 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 1932 | ((['git', 'config', 'branch.feature.rietveldissue'],), CERR1), |
| 1933 | ((['git', 'config', 'branch.feature.gerritissue'],), '123'), |
tandrii@chromium.org | fa330e8 | 2016-04-13 17:09:52 +0000 | [diff] [blame] | 1934 | ((['git', 'config', 'branch.feature.gerritserver'],), |
| 1935 | 'https://chromium-review.googlesource.com'), |
| 1936 | ((['SetReview', 'chromium-review.googlesource.com', 123, |
Andrii Shyshkalov | 828701b | 2016-12-09 10:46:47 +0100 | [diff] [blame] | 1937 | {'Commit-Queue': vote}, notify],), ''), |
tandrii@chromium.org | fa330e8 | 2016-04-13 17:09:52 +0000 | [diff] [blame] | 1938 | ] |
tandrii | d9e5ce5 | 2016-07-13 02:32:59 -0700 | [diff] [blame] | 1939 | |
| 1940 | def test_cmd_set_commit_gerrit_clear(self): |
| 1941 | self._cmd_set_commit_gerrit_common(0) |
| 1942 | self.assertEqual(0, git_cl.main(['set-commit', '-c'])) |
| 1943 | |
| 1944 | def test_cmd_set_commit_gerrit_dry(self): |
Andrii Shyshkalov | 828701b | 2016-12-09 10:46:47 +0100 | [diff] [blame] | 1945 | self._cmd_set_commit_gerrit_common(1, notify='NONE') |
tandrii@chromium.org | fa330e8 | 2016-04-13 17:09:52 +0000 | [diff] [blame] | 1946 | self.assertEqual(0, git_cl.main(['set-commit', '-d'])) |
| 1947 | |
tandrii | d9e5ce5 | 2016-07-13 02:32:59 -0700 | [diff] [blame] | 1948 | def test_cmd_set_commit_gerrit(self): |
| 1949 | self._cmd_set_commit_gerrit_common(2) |
| 1950 | self.assertEqual(0, git_cl.main(['set-commit'])) |
| 1951 | |
martiniss@chromium.org | 2b55fe3 | 2016-04-26 20:28:54 +0000 | [diff] [blame] | 1952 | def test_description_display(self): |
| 1953 | out = StringIO.StringIO() |
| 1954 | self.mock(git_cl.sys, 'stdout', out) |
| 1955 | |
martiniss@chromium.org | d6648e2 | 2016-04-29 19:22:16 +0000 | [diff] [blame] | 1956 | self.mock(git_cl, 'Changelist', ChangelistMock) |
| 1957 | ChangelistMock.desc = 'foo\n' |
martiniss@chromium.org | 2b55fe3 | 2016-04-26 20:28:54 +0000 | [diff] [blame] | 1958 | |
| 1959 | self.assertEqual(0, git_cl.main(['description', '-d'])) |
| 1960 | self.assertEqual('foo\n', out.getvalue()) |
| 1961 | |
| 1962 | def test_description_rietveld(self): |
| 1963 | out = StringIO.StringIO() |
| 1964 | self.mock(git_cl.sys, 'stdout', out) |
martiniss | 6eda05f | 2016-06-30 10:18:35 -0700 | [diff] [blame] | 1965 | self.mock(git_cl.Changelist, 'GetDescription', lambda *args: 'foobar') |
martiniss@chromium.org | 2b55fe3 | 2016-04-26 20:28:54 +0000 | [diff] [blame] | 1966 | |
martiniss@chromium.org | 2b55fe3 | 2016-04-26 20:28:54 +0000 | [diff] [blame] | 1967 | self.assertEqual(0, git_cl.main([ |
| 1968 | 'description', 'https://code.review.org/123123', '-d', '--rietveld'])) |
| 1969 | self.assertEqual('foobar\n', out.getvalue()) |
| 1970 | |
iannucci | 3c972b9 | 2016-08-17 13:24:10 -0700 | [diff] [blame] | 1971 | def test_StatusFieldOverrideIssueMissingArgs(self): |
| 1972 | out = StringIO.StringIO() |
| 1973 | self.mock(git_cl.sys, 'stderr', out) |
| 1974 | |
| 1975 | try: |
| 1976 | self.assertEqual(git_cl.main(['status', '--issue', '1']), 0) |
| 1977 | except SystemExit as ex: |
| 1978 | self.assertEqual(ex.code, 2) |
iannucci | e53c935 | 2016-08-17 14:40:40 -0700 | [diff] [blame] | 1979 | self.assertRegexpMatches(out.getvalue(), r'--issue must be specified') |
iannucci | 3c972b9 | 2016-08-17 13:24:10 -0700 | [diff] [blame] | 1980 | |
| 1981 | out = StringIO.StringIO() |
| 1982 | self.mock(git_cl.sys, 'stderr', out) |
| 1983 | |
| 1984 | try: |
| 1985 | self.assertEqual(git_cl.main(['status', '--issue', '1', '--rietveld']), 0) |
| 1986 | except SystemExit as ex: |
| 1987 | self.assertEqual(ex.code, 2) |
iannucci | e53c935 | 2016-08-17 14:40:40 -0700 | [diff] [blame] | 1988 | self.assertRegexpMatches(out.getvalue(), r'--field must be specified') |
iannucci | 3c972b9 | 2016-08-17 13:24:10 -0700 | [diff] [blame] | 1989 | |
| 1990 | def test_StatusFieldOverrideIssue(self): |
| 1991 | out = StringIO.StringIO() |
| 1992 | self.mock(git_cl.sys, 'stdout', out) |
| 1993 | |
| 1994 | def assertIssue(cl_self, *_args): |
| 1995 | self.assertEquals(cl_self.issue, 1) |
| 1996 | return 'foobar' |
| 1997 | |
| 1998 | self.mock(git_cl.Changelist, 'GetDescription', assertIssue) |
| 1999 | self.calls = [ |
| 2000 | ((['git', 'config', 'rietveld.autoupdate'],), ''), |
| 2001 | ((['git', 'config', 'rietveld.server'],), ''), |
| 2002 | ((['git', 'config', 'rietveld.server'],), ''), |
| 2003 | ] |
iannucci | e53c935 | 2016-08-17 14:40:40 -0700 | [diff] [blame] | 2004 | self.assertEqual( |
| 2005 | git_cl.main(['status', '--issue', '1', '--rietveld', '--field', 'desc']), |
| 2006 | 0) |
iannucci | 3c972b9 | 2016-08-17 13:24:10 -0700 | [diff] [blame] | 2007 | self.assertEqual(out.getvalue(), 'foobar\n') |
| 2008 | |
iannucci | e53c935 | 2016-08-17 14:40:40 -0700 | [diff] [blame] | 2009 | def test_SetCloseOverrideIssue(self): |
| 2010 | def assertIssue(cl_self, *_args): |
| 2011 | self.assertEquals(cl_self.issue, 1) |
| 2012 | return 'foobar' |
| 2013 | |
| 2014 | self.mock(git_cl.Changelist, 'GetDescription', assertIssue) |
| 2015 | self.mock(git_cl.Changelist, 'CloseIssue', lambda *_: None) |
| 2016 | self.calls = [ |
| 2017 | ((['git', 'config', 'rietveld.autoupdate'],), ''), |
| 2018 | ((['git', 'config', 'rietveld.server'],), ''), |
| 2019 | ((['git', 'config', 'rietveld.server'],), ''), |
| 2020 | ] |
| 2021 | self.assertEqual( |
| 2022 | git_cl.main(['set-close', '--issue', '1', '--rietveld']), 0) |
| 2023 | |
| 2024 | def test_SetCommitOverrideIssue(self): |
| 2025 | def assertIssue(cl_self, *_args): |
| 2026 | self.assertEquals(cl_self.issue, 1) |
| 2027 | return 'foobar' |
| 2028 | |
| 2029 | self.mock(git_cl.Changelist, 'GetDescription', assertIssue) |
| 2030 | self.mock(git_cl.Changelist, 'SetCQState', lambda *_: None) |
| 2031 | self.calls = [ |
| 2032 | ((['git', 'config', 'rietveld.autoupdate'],), ''), |
| 2033 | ((['git', 'config', 'rietveld.server'],), ''), |
| 2034 | ((['git', 'config', 'rietveld.server'],), ''), |
| 2035 | ((['git', 'symbolic-ref', 'HEAD'],), ''), |
| 2036 | ((['git', 'config', 'rietveld.server'],), ''), |
| 2037 | ((['git', 'config', 'rietveld.server'],), ''), |
| 2038 | ] |
| 2039 | self.assertEqual( |
| 2040 | git_cl.main(['set-close', '--issue', '1', '--rietveld']), 0) |
| 2041 | |
martiniss@chromium.org | 2b55fe3 | 2016-04-26 20:28:54 +0000 | [diff] [blame] | 2042 | def test_description_gerrit(self): |
| 2043 | out = StringIO.StringIO() |
| 2044 | self.mock(git_cl.sys, 'stdout', out) |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 2045 | self.calls = [ |
Andrii Shyshkalov | 8039be7 | 2017-01-26 09:38:18 +0100 | [diff] [blame] | 2046 | (('GetChangeDetail', 'code.review.org', |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 2047 | '123123', ['CURRENT_REVISION', 'CURRENT_COMMIT']), |
| 2048 | { |
| 2049 | 'current_revision': 'sha1', |
| 2050 | 'revisions': {'sha1': { |
| 2051 | 'commit': {'message': 'foobar'}, |
| 2052 | }}, |
| 2053 | }), |
| 2054 | ] |
martiniss@chromium.org | 2b55fe3 | 2016-04-26 20:28:54 +0000 | [diff] [blame] | 2055 | self.assertEqual(0, git_cl.main([ |
| 2056 | 'description', 'https://code.review.org/123123', '-d', '--gerrit'])) |
| 2057 | self.assertEqual('foobar\n', out.getvalue()) |
| 2058 | |
martiniss@chromium.org | d6648e2 | 2016-04-29 19:22:16 +0000 | [diff] [blame] | 2059 | def test_description_set_raw(self): |
| 2060 | out = StringIO.StringIO() |
| 2061 | self.mock(git_cl.sys, 'stdout', out) |
| 2062 | |
| 2063 | self.mock(git_cl, 'Changelist', ChangelistMock) |
| 2064 | self.mock(git_cl.sys, 'stdin', StringIO.StringIO('hihi')) |
| 2065 | |
| 2066 | self.assertEqual(0, git_cl.main(['description', '-n', 'hihi'])) |
| 2067 | self.assertEqual('hihi', ChangelistMock.desc) |
| 2068 | |
tandrii@chromium.org | d605a51 | 2016-06-03 09:55:00 +0000 | [diff] [blame] | 2069 | def test_description_appends_bug_line(self): |
tandrii@chromium.org | 601e1d1 | 2016-06-03 13:03:54 +0000 | [diff] [blame] | 2070 | current_desc = 'Some.\n\nChange-Id: xxx' |
tandrii@chromium.org | d605a51 | 2016-06-03 09:55:00 +0000 | [diff] [blame] | 2071 | |
| 2072 | def RunEditor(desc, _, **kwargs): |
| 2073 | self.assertEquals( |
| 2074 | '# Enter a description of the change.\n' |
| 2075 | '# This will be displayed on the codereview site.\n' |
| 2076 | '# The first line will also be used as the subject of the review.\n' |
| 2077 | '#--------------------This line is 72 characters long' |
tandrii@chromium.org | 601e1d1 | 2016-06-03 13:03:54 +0000 | [diff] [blame] | 2078 | '--------------------\n' |
| 2079 | 'Some.\n\nBUG=\n\nChange-Id: xxx', |
tandrii@chromium.org | d605a51 | 2016-06-03 09:55:00 +0000 | [diff] [blame] | 2080 | desc) |
tandrii@chromium.org | 601e1d1 | 2016-06-03 13:03:54 +0000 | [diff] [blame] | 2081 | # Simulate user changing something. |
| 2082 | return 'Some.\n\nBUG=123\n\nChange-Id: xxx' |
tandrii@chromium.org | d605a51 | 2016-06-03 09:55:00 +0000 | [diff] [blame] | 2083 | |
dsansome | e2d6fd9 | 2016-09-08 00:10:47 -0700 | [diff] [blame] | 2084 | def UpdateDescriptionRemote(_, desc, force=False): |
tandrii@chromium.org | 601e1d1 | 2016-06-03 13:03:54 +0000 | [diff] [blame] | 2085 | self.assertEquals(desc, 'Some.\n\nBUG=123\n\nChange-Id: xxx') |
tandrii@chromium.org | d605a51 | 2016-06-03 09:55:00 +0000 | [diff] [blame] | 2086 | |
| 2087 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
| 2088 | self.mock(git_cl.Changelist, 'GetDescription', |
| 2089 | lambda *args: current_desc) |
| 2090 | self.mock(git_cl._GerritChangelistImpl, 'UpdateDescriptionRemote', |
| 2091 | UpdateDescriptionRemote) |
| 2092 | self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor) |
| 2093 | |
| 2094 | self.calls = [ |
| 2095 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 2096 | ((['git', 'config', 'branch.feature.gerritissue'],), '123'), |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 2097 | ((['git', 'config', 'rietveld.autoupdate'],), CERR1), |
| 2098 | ((['git', 'config', 'rietveld.bug-prefix'],), CERR1), |
tandrii@chromium.org | d605a51 | 2016-06-03 09:55:00 +0000 | [diff] [blame] | 2099 | ((['git', 'config', 'core.editor'],), 'vi'), |
| 2100 | ] |
| 2101 | self.assertEqual(0, git_cl.main(['description', '--gerrit'])) |
| 2102 | |
martiniss@chromium.org | d6648e2 | 2016-04-29 19:22:16 +0000 | [diff] [blame] | 2103 | def test_description_set_stdin(self): |
| 2104 | out = StringIO.StringIO() |
| 2105 | self.mock(git_cl.sys, 'stdout', out) |
| 2106 | |
| 2107 | self.mock(git_cl, 'Changelist', ChangelistMock) |
| 2108 | self.mock(git_cl.sys, 'stdin', StringIO.StringIO('hi \r\n\t there\n\nman')) |
| 2109 | |
| 2110 | self.assertEqual(0, git_cl.main(['description', '-n', '-'])) |
| 2111 | self.assertEqual('hi\n\t there\n\nman', ChangelistMock.desc) |
| 2112 | |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 2113 | def test_archive(self): |
tandrii | 1c67da6 | 2016-06-10 07:35:53 -0700 | [diff] [blame] | 2114 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
| 2115 | |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 2116 | self.calls = \ |
| 2117 | [((['git', 'for-each-ref', '--format=%(refname)', 'refs/heads'],), |
| 2118 | 'refs/heads/master\nrefs/heads/foo\nrefs/heads/bar'), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 2119 | ((['git', 'config', 'branch.master.rietveldissue'],), '1'), |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 2120 | ((['git', 'config', 'rietveld.autoupdate'],), CERR1), |
| 2121 | ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 2122 | ((['git', 'config', 'branch.foo.rietveldissue'],), '456'), |
| 2123 | ((['git', 'config', 'branch.bar.rietveldissue'],), CERR1), |
| 2124 | ((['git', 'config', 'branch.bar.gerritissue'],), '789'), |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 2125 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
| 2126 | ((['git', 'tag', 'git-cl-archived-456-foo', 'foo'],), ''), |
| 2127 | ((['git', 'branch', '-D', 'foo'],), '')] |
| 2128 | |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 2129 | self.mock(git_cl, 'get_cl_statuses', |
| 2130 | lambda branches, fine_grained, max_processes: |
kmarshall | 9249e01 | 2016-08-23 12:02:16 -0700 | [diff] [blame] | 2131 | [(MockChangelistWithBranchAndIssue('master', 1), 'open'), |
| 2132 | (MockChangelistWithBranchAndIssue('foo', 456), 'closed'), |
| 2133 | (MockChangelistWithBranchAndIssue('bar', 789), 'open')]) |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 2134 | |
| 2135 | self.assertEqual(0, git_cl.main(['archive', '-f'])) |
| 2136 | |
| 2137 | def test_archive_current_branch_fails(self): |
tandrii | 1c67da6 | 2016-06-10 07:35:53 -0700 | [diff] [blame] | 2138 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 2139 | self.calls = \ |
| 2140 | [((['git', 'for-each-ref', '--format=%(refname)', 'refs/heads'],), |
| 2141 | 'refs/heads/master'), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 2142 | ((['git', 'config', 'branch.master.rietveldissue'],), '1'), |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 2143 | ((['git', 'config', 'rietveld.autoupdate'],), CERR1), |
| 2144 | ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'), |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 2145 | ((['git', 'symbolic-ref', 'HEAD'],), 'master')] |
| 2146 | |
kmarshall | 9249e01 | 2016-08-23 12:02:16 -0700 | [diff] [blame] | 2147 | self.mock(git_cl, 'get_cl_statuses', |
| 2148 | lambda branches, fine_grained, max_processes: |
| 2149 | [(MockChangelistWithBranchAndIssue('master', 1), 'closed')]) |
| 2150 | |
| 2151 | self.assertEqual(1, git_cl.main(['archive', '-f'])) |
| 2152 | |
| 2153 | def test_archive_dry_run(self): |
| 2154 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
| 2155 | |
| 2156 | self.calls = \ |
| 2157 | [((['git', 'for-each-ref', '--format=%(refname)', 'refs/heads'],), |
| 2158 | 'refs/heads/master\nrefs/heads/foo\nrefs/heads/bar'), |
| 2159 | ((['git', 'config', 'branch.master.rietveldissue'],), '1'), |
| 2160 | ((['git', 'config', 'rietveld.autoupdate'],), CERR1), |
| 2161 | ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'), |
| 2162 | ((['git', 'config', 'branch.foo.rietveldissue'],), '456'), |
| 2163 | ((['git', 'config', 'branch.bar.rietveldissue'],), CERR1), |
| 2164 | ((['git', 'config', 'branch.bar.gerritissue'],), '789'), |
| 2165 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'),] |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 2166 | |
| 2167 | self.mock(git_cl, 'get_cl_statuses', |
| 2168 | lambda branches, fine_grained, max_processes: |
kmarshall | 9249e01 | 2016-08-23 12:02:16 -0700 | [diff] [blame] | 2169 | [(MockChangelistWithBranchAndIssue('master', 1), 'open'), |
| 2170 | (MockChangelistWithBranchAndIssue('foo', 456), 'closed'), |
| 2171 | (MockChangelistWithBranchAndIssue('bar', 789), 'open')]) |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 2172 | |
kmarshall | 9249e01 | 2016-08-23 12:02:16 -0700 | [diff] [blame] | 2173 | self.assertEqual(0, git_cl.main(['archive', '-f', '--dry-run'])) |
| 2174 | |
| 2175 | def test_archive_no_tags(self): |
| 2176 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
| 2177 | |
| 2178 | self.calls = \ |
| 2179 | [((['git', 'for-each-ref', '--format=%(refname)', 'refs/heads'],), |
| 2180 | 'refs/heads/master\nrefs/heads/foo\nrefs/heads/bar'), |
| 2181 | ((['git', 'config', 'branch.master.rietveldissue'],), '1'), |
| 2182 | ((['git', 'config', 'rietveld.autoupdate'],), CERR1), |
| 2183 | ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'), |
| 2184 | ((['git', 'config', 'branch.foo.rietveldissue'],), '456'), |
| 2185 | ((['git', 'config', 'branch.bar.rietveldissue'],), CERR1), |
| 2186 | ((['git', 'config', 'branch.bar.gerritissue'],), '789'), |
| 2187 | ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
| 2188 | ((['git', 'branch', '-D', 'foo'],), '')] |
| 2189 | |
| 2190 | self.mock(git_cl, 'get_cl_statuses', |
| 2191 | lambda branches, fine_grained, max_processes: |
| 2192 | [(MockChangelistWithBranchAndIssue('master', 1), 'open'), |
| 2193 | (MockChangelistWithBranchAndIssue('foo', 456), 'closed'), |
| 2194 | (MockChangelistWithBranchAndIssue('bar', 789), 'open')]) |
| 2195 | |
| 2196 | self.assertEqual(0, git_cl.main(['archive', '-f', '--notags'])) |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 2197 | |
tandrii@chromium.org | 9b7fd71 | 2016-06-01 13:45:20 +0000 | [diff] [blame] | 2198 | def test_cmd_issue_erase_existing(self): |
| 2199 | out = StringIO.StringIO() |
| 2200 | self.mock(git_cl.sys, 'stdout', out) |
| 2201 | self.calls = [ |
| 2202 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 2203 | ((['git', 'config', 'branch.feature.rietveldissue'],), CERR1), |
| 2204 | ((['git', 'config', 'branch.feature.gerritissue'],), '123'), |
tandrii@chromium.org | 9b7fd71 | 2016-06-01 13:45:20 +0000 | [diff] [blame] | 2205 | # Let this command raise exception (retcode=1) - it should be ignored. |
| 2206 | ((['git', 'config', '--unset', 'branch.feature.last-upload-hash'],), |
tandrii | 5d48c32 | 2016-08-18 16:19:37 -0700 | [diff] [blame] | 2207 | CERR1), |
| 2208 | ((['git', 'config', '--unset', 'branch.feature.gerritissue'],), ''), |
| 2209 | ((['git', 'config', '--unset', 'branch.feature.gerritpatchset'],), ''), |
tandrii@chromium.org | 9b7fd71 | 2016-06-01 13:45:20 +0000 | [diff] [blame] | 2210 | ((['git', 'config', '--unset', 'branch.feature.gerritserver'],), ''), |
| 2211 | ((['git', 'config', '--unset', 'branch.feature.gerritsquashhash'],), |
| 2212 | ''), |
| 2213 | ] |
| 2214 | self.assertEqual(0, git_cl.main(['issue', '0'])) |
| 2215 | |
phajdan.jr | e328cf9 | 2016-08-22 04:12:17 -0700 | [diff] [blame] | 2216 | def test_cmd_issue_json(self): |
| 2217 | out = StringIO.StringIO() |
| 2218 | self.mock(git_cl.sys, 'stdout', out) |
| 2219 | self.calls = [ |
| 2220 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 2221 | ((['git', 'config', 'branch.feature.rietveldissue'],), '123'), |
phajdan.jr | e328cf9 | 2016-08-22 04:12:17 -0700 | [diff] [blame] | 2222 | ((['git', 'config', 'rietveld.autoupdate'],), ''), |
| 2223 | ((['git', 'config', 'rietveld.server'],), |
| 2224 | 'https://codereview.chromium.org'), |
| 2225 | ((['git', 'config', 'branch.feature.rietveldserver'],), ''), |
| 2226 | (('write_json', 'output.json', |
| 2227 | {'issue': 123, 'issue_url': 'https://codereview.chromium.org/123'}), |
| 2228 | ''), |
| 2229 | ] |
| 2230 | self.assertEqual(0, git_cl.main(['issue', '--json', 'output.json'])) |
| 2231 | |
tandrii | de281ae | 2016-10-12 06:02:30 -0700 | [diff] [blame] | 2232 | def test_git_cl_try_default_cq_dry_run(self): |
tandrii | 9de9ec6 | 2016-07-13 03:01:59 -0700 | [diff] [blame] | 2233 | self.mock(git_cl.Changelist, 'GetChange', |
| 2234 | lambda _, *a: ( |
| 2235 | self._mocked_call(['GetChange']+list(a)))) |
| 2236 | self.mock(git_cl.presubmit_support, 'DoGetTryMasters', |
| 2237 | lambda *_, **__: ( |
| 2238 | self._mocked_call(['DoGetTryMasters']))) |
tandrii | 9de9ec6 | 2016-07-13 03:01:59 -0700 | [diff] [blame] | 2239 | self.mock(git_cl._RietveldChangelistImpl, 'SetCQState', |
| 2240 | lambda _, s: self._mocked_call(['SetCQState', s])) |
| 2241 | self.calls = [ |
| 2242 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
tandrii | 33a46ff | 2016-08-23 05:53:40 -0700 | [diff] [blame] | 2243 | ((['git', 'config', 'branch.feature.rietveldissue'],), '123'), |
tandrii | 9de9ec6 | 2016-07-13 03:01:59 -0700 | [diff] [blame] | 2244 | ((['git', 'config', 'rietveld.autoupdate'],), ''), |
| 2245 | ((['git', 'config', 'rietveld.server'],), |
| 2246 | 'https://codereview.chromium.org'), |
| 2247 | ((['git', 'config', 'branch.feature.rietveldserver'],), ''), |
| 2248 | ((['git', 'config', 'branch.feature.merge'],), 'feature'), |
| 2249 | ((['git', 'config', 'branch.feature.remote'],), 'origin'), |
| 2250 | ((['get_or_create_merge_base', 'feature', 'feature'],), |
| 2251 | 'fake_ancestor_sha'), |
| 2252 | ((['GetChange', 'fake_ancestor_sha', None], ), |
| 2253 | git_cl.presubmit_support.GitChange( |
| 2254 | '', '', '', '', '', '', '', '')), |
| 2255 | ((['git', 'rev-parse', '--show-cdup'],), '../'), |
| 2256 | ((['DoGetTryMasters'], ), None), |
tandrii | 9de9ec6 | 2016-07-13 03:01:59 -0700 | [diff] [blame] | 2257 | ((['SetCQState', git_cl._CQState.DRY_RUN], ), None), |
| 2258 | ] |
| 2259 | out = StringIO.StringIO() |
| 2260 | self.mock(git_cl.sys, 'stdout', out) |
| 2261 | self.assertEqual(0, git_cl.main(['try'])) |
| 2262 | self.assertEqual( |
| 2263 | out.getvalue(), |
| 2264 | 'scheduled CQ Dry Run on https://codereview.chromium.org/123\n') |
| 2265 | |
tandrii | 8c5a353 | 2016-11-04 07:52:02 -0700 | [diff] [blame] | 2266 | def test_git_cl_try_default_cq_dry_run_gerrit(self): |
| 2267 | self.mock(git_cl.Changelist, 'GetChange', |
| 2268 | lambda _, *a: ( |
| 2269 | self._mocked_call(['GetChange']+list(a)))) |
| 2270 | self.mock(git_cl.presubmit_support, 'DoGetTryMasters', |
| 2271 | lambda *_, **__: ( |
| 2272 | self._mocked_call(['DoGetTryMasters']))) |
| 2273 | self.mock(git_cl._GerritChangelistImpl, 'SetCQState', |
| 2274 | lambda _, s: self._mocked_call(['SetCQState', s])) |
| 2275 | |
tandrii | 8c5a353 | 2016-11-04 07:52:02 -0700 | [diff] [blame] | 2276 | self.calls = [ |
| 2277 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
| 2278 | ((['git', 'config', 'branch.feature.rietveldissue'],), CERR1), |
| 2279 | ((['git', 'config', 'branch.feature.gerritissue'],), '123456'), |
| 2280 | ((['git', 'config', 'branch.feature.gerritserver'],), |
| 2281 | 'https://chromium-review.googlesource.com'), |
Andrii Shyshkalov | eadad92 | 2017-01-26 09:38:30 +0100 | [diff] [blame] | 2282 | (('GetChangeDetail', 'chromium-review.googlesource.com', '123456', |
| 2283 | ['DETAILED_ACCOUNTS', 'ALL_REVISIONS', 'CURRENT_COMMIT']), { |
| 2284 | 'project': 'depot_tools', |
| 2285 | 'status': 'OPEN', |
| 2286 | 'owner': {'email': 'owner@e.mail'}, |
| 2287 | 'revisions': { |
| 2288 | 'deadbeaf': { |
| 2289 | '_number': 6, |
| 2290 | }, |
| 2291 | 'beeeeeef': { |
| 2292 | '_number': 7, |
| 2293 | 'fetch': {'http': { |
| 2294 | 'url': 'https://chromium.googlesource.com/depot_tools', |
| 2295 | 'ref': 'refs/changes/56/123456/7' |
| 2296 | }}, |
| 2297 | }, |
| 2298 | }, |
| 2299 | }), |
tandrii | 8c5a353 | 2016-11-04 07:52:02 -0700 | [diff] [blame] | 2300 | ((['git', 'config', 'branch.feature.merge'],), 'feature'), |
| 2301 | ((['git', 'config', 'branch.feature.remote'],), 'origin'), |
| 2302 | ((['get_or_create_merge_base', 'feature', 'feature'],), |
| 2303 | 'fake_ancestor_sha'), |
| 2304 | ((['GetChange', 'fake_ancestor_sha', None], ), |
| 2305 | git_cl.presubmit_support.GitChange( |
| 2306 | '', '', '', '', '', '', '', '')), |
| 2307 | ((['git', 'rev-parse', '--show-cdup'],), '../'), |
| 2308 | ((['DoGetTryMasters'], ), None), |
| 2309 | ((['SetCQState', git_cl._CQState.DRY_RUN], ), None), |
| 2310 | ] |
| 2311 | out = StringIO.StringIO() |
| 2312 | self.mock(git_cl.sys, 'stdout', out) |
| 2313 | self.assertEqual(0, git_cl.main(['try'])) |
| 2314 | self.assertEqual( |
| 2315 | out.getvalue(), |
| 2316 | 'scheduled CQ Dry Run on ' |
| 2317 | 'https://chromium-review.googlesource.com/123456\n') |
| 2318 | |
| 2319 | def test_git_cl_try_buildbucket_with_properties_rietveld(self): |
| 2320 | self.mock(git_cl._RietveldChangelistImpl, 'GetIssueProperties', |
| 2321 | lambda _: { |
| 2322 | 'owner_email': 'owner@e.mail', |
| 2323 | 'private': False, |
| 2324 | 'closed': False, |
| 2325 | 'project': 'depot_tools', |
| 2326 | 'patchsets': [20001], |
| 2327 | }) |
tandrii | de281ae | 2016-10-12 06:02:30 -0700 | [diff] [blame] | 2328 | self.mock(git_cl.uuid, 'uuid4', lambda: 'uuid4') |
| 2329 | self.calls = [ |
| 2330 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
| 2331 | ((['git', 'config', 'branch.feature.rietveldissue'],), '123'), |
| 2332 | ((['git', 'config', 'rietveld.autoupdate'],), CERR1), |
| 2333 | ((['git', 'config', 'rietveld.server'],), |
| 2334 | 'https://codereview.chromium.org'), |
tandrii | de281ae | 2016-10-12 06:02:30 -0700 | [diff] [blame] | 2335 | ((['git', 'config', 'branch.feature.rietveldpatchset'],), '20001'), |
tandrii | 8c5a353 | 2016-11-04 07:52:02 -0700 | [diff] [blame] | 2336 | ((['git', 'config', 'branch.feature.rietveldserver'],), CERR1), |
tandrii | de281ae | 2016-10-12 06:02:30 -0700 | [diff] [blame] | 2337 | ] |
| 2338 | |
| 2339 | def _buildbucket_retry(*_, **kw): |
| 2340 | # self.maxDiff = 10000 |
| 2341 | body = json.loads(kw['body']) |
| 2342 | self.assertEqual(len(body['builds']), 1) |
| 2343 | build = body['builds'][0] |
| 2344 | params = json.loads(build.pop('parameters_json')) |
| 2345 | self.assertEqual(params, { |
| 2346 | u'builder_name': u'win', |
| 2347 | u'changes': [{u'author': {u'email': u'owner@e.mail'}, |
| 2348 | u'revision': None}], |
| 2349 | u'properties': { |
| 2350 | u'category': u'git_cl_try', |
| 2351 | u'issue': 123, |
| 2352 | u'key': u'val', |
| 2353 | u'json': [{u'a': 1}, None], |
| 2354 | u'master': u'tryserver.chromium', |
| 2355 | u'patch_project': u'depot_tools', |
| 2356 | u'patch_storage': u'rietveld', |
| 2357 | u'patchset': 20001, |
tandrii | de281ae | 2016-10-12 06:02:30 -0700 | [diff] [blame] | 2358 | u'rietveld': u'https://codereview.chromium.org', |
| 2359 | } |
| 2360 | }) |
| 2361 | self.assertEqual(build, { |
| 2362 | u'bucket': u'master.tryserver.chromium', |
| 2363 | u'client_operation_id': u'uuid4', |
| 2364 | u'tags': [u'builder:win', |
| 2365 | u'buildset:patch/rietveld/codereview.chromium.org/123/20001', |
borenet | 6c0efe6 | 2016-10-19 08:13:29 -0700 | [diff] [blame] | 2366 | u'user_agent:git_cl_try', |
| 2367 | u'master:tryserver.chromium'], |
tandrii | de281ae | 2016-10-12 06:02:30 -0700 | [diff] [blame] | 2368 | }) |
| 2369 | |
| 2370 | self.mock(git_cl, '_buildbucket_retry', _buildbucket_retry) |
| 2371 | |
| 2372 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
| 2373 | self.assertEqual(0, git_cl.main([ |
| 2374 | 'try', '-m', 'tryserver.chromium', '-b', 'win', |
| 2375 | '-p', 'key=val', '-p', 'json=[{"a":1}, null]'])) |
| 2376 | self.assertRegexpMatches( |
| 2377 | git_cl.sys.stdout.getvalue(), |
borenet | 6c0efe6 | 2016-10-19 08:13:29 -0700 | [diff] [blame] | 2378 | 'Tried jobs on:\nBucket: master.tryserver.chromium') |
| 2379 | |
tandrii | 8c5a353 | 2016-11-04 07:52:02 -0700 | [diff] [blame] | 2380 | def test_git_cl_try_buildbucket_with_properties_gerrit(self): |
| 2381 | self.mock(git_cl.Changelist, 'GetMostRecentPatchset', lambda _: 7) |
| 2382 | self.mock(git_cl.uuid, 'uuid4', lambda: 'uuid4') |
| 2383 | |
tandrii | 8c5a353 | 2016-11-04 07:52:02 -0700 | [diff] [blame] | 2384 | self.calls = [ |
| 2385 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
| 2386 | ((['git', 'config', 'branch.feature.rietveldissue'],), CERR1), |
| 2387 | ((['git', 'config', 'branch.feature.gerritissue'],), '123456'), |
| 2388 | ((['git', 'config', 'branch.feature.gerritserver'],), |
| 2389 | 'https://chromium-review.googlesource.com'), |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 2390 | (('GetChangeDetail', 'chromium-review.googlesource.com', '123456', |
Andrii Shyshkalov | eadad92 | 2017-01-26 09:38:30 +0100 | [diff] [blame] | 2391 | ['DETAILED_ACCOUNTS', 'ALL_REVISIONS', 'CURRENT_COMMIT']), { |
tandrii | 8c5a353 | 2016-11-04 07:52:02 -0700 | [diff] [blame] | 2392 | 'project': 'depot_tools', |
Andrii Shyshkalov | eadad92 | 2017-01-26 09:38:30 +0100 | [diff] [blame] | 2393 | 'status': 'OPEN', |
| 2394 | 'owner': {'email': 'owner@e.mail'}, |
tandrii | 8c5a353 | 2016-11-04 07:52:02 -0700 | [diff] [blame] | 2395 | 'revisions': { |
| 2396 | 'deadbeaf': { |
| 2397 | '_number': 6, |
| 2398 | }, |
| 2399 | 'beeeeeef': { |
| 2400 | '_number': 7, |
| 2401 | 'fetch': {'http': { |
| 2402 | 'url': 'https://chromium.googlesource.com/depot_tools', |
| 2403 | 'ref': 'refs/changes/56/123456/7' |
| 2404 | }}, |
| 2405 | }, |
| 2406 | }, |
| 2407 | }), |
| 2408 | ] |
| 2409 | |
| 2410 | def _buildbucket_retry(*_, **kw): |
| 2411 | # self.maxDiff = 10000 |
| 2412 | body = json.loads(kw['body']) |
| 2413 | self.assertEqual(len(body['builds']), 1) |
| 2414 | build = body['builds'][0] |
| 2415 | params = json.loads(build.pop('parameters_json')) |
| 2416 | self.assertEqual(params, { |
| 2417 | u'builder_name': u'win', |
| 2418 | u'changes': [{u'author': {u'email': u'owner@e.mail'}, |
| 2419 | u'revision': None}], |
| 2420 | u'properties': { |
| 2421 | u'category': u'git_cl_try', |
| 2422 | u'key': u'val', |
| 2423 | u'json': [{u'a': 1}, None], |
| 2424 | u'master': u'tryserver.chromium', |
| 2425 | |
| 2426 | u'patch_gerrit_url': |
| 2427 | u'https://chromium-review.googlesource.com', |
| 2428 | u'patch_issue': 123456, |
| 2429 | u'patch_project': u'depot_tools', |
| 2430 | u'patch_ref': u'refs/changes/56/123456/7', |
| 2431 | u'patch_repository_url': |
| 2432 | u'https://chromium.googlesource.com/depot_tools', |
| 2433 | u'patch_set': 7, |
| 2434 | u'patch_storage': u'gerrit', |
| 2435 | } |
| 2436 | }) |
| 2437 | self.assertEqual(build, { |
| 2438 | u'bucket': u'master.tryserver.chromium', |
| 2439 | u'client_operation_id': u'uuid4', |
| 2440 | u'tags': [ |
| 2441 | u'builder:win', |
| 2442 | u'buildset:patch/gerrit/chromium-review.googlesource.com/123456/7', |
| 2443 | u'user_agent:git_cl_try', |
| 2444 | u'master:tryserver.chromium'], |
| 2445 | }) |
| 2446 | |
| 2447 | self.mock(git_cl, '_buildbucket_retry', _buildbucket_retry) |
| 2448 | |
| 2449 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
| 2450 | self.assertEqual(0, git_cl.main([ |
| 2451 | 'try', '-m', 'tryserver.chromium', '-b', 'win', |
| 2452 | '-p', 'key=val', '-p', 'json=[{"a":1}, null]'])) |
| 2453 | self.assertRegexpMatches( |
| 2454 | git_cl.sys.stdout.getvalue(), |
| 2455 | 'Tried jobs on:\nBucket: master.tryserver.chromium') |
| 2456 | |
borenet | 6c0efe6 | 2016-10-19 08:13:29 -0700 | [diff] [blame] | 2457 | def test_git_cl_try_buildbucket_bucket_flag(self): |
tandrii | 8c5a353 | 2016-11-04 07:52:02 -0700 | [diff] [blame] | 2458 | self.mock(git_cl._RietveldChangelistImpl, 'GetIssueProperties', |
| 2459 | lambda _: { |
| 2460 | 'owner_email': 'owner@e.mail', |
| 2461 | 'private': False, |
| 2462 | 'closed': False, |
| 2463 | 'project': 'depot_tools', |
| 2464 | 'patchsets': [20001], |
| 2465 | }) |
borenet | 6c0efe6 | 2016-10-19 08:13:29 -0700 | [diff] [blame] | 2466 | self.mock(git_cl.uuid, 'uuid4', lambda: 'uuid4') |
| 2467 | self.calls = [ |
| 2468 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
| 2469 | ((['git', 'config', 'branch.feature.rietveldissue'],), '123'), |
| 2470 | ((['git', 'config', 'rietveld.autoupdate'],), CERR1), |
| 2471 | ((['git', 'config', 'rietveld.server'],), |
| 2472 | 'https://codereview.chromium.org'), |
borenet | 6c0efe6 | 2016-10-19 08:13:29 -0700 | [diff] [blame] | 2473 | ((['git', 'config', 'branch.feature.rietveldpatchset'],), '20001'), |
tandrii | 8c5a353 | 2016-11-04 07:52:02 -0700 | [diff] [blame] | 2474 | ((['git', 'config', 'branch.feature.rietveldserver'],), CERR1), |
borenet | 6c0efe6 | 2016-10-19 08:13:29 -0700 | [diff] [blame] | 2475 | ] |
| 2476 | |
| 2477 | def _buildbucket_retry(*_, **kw): |
borenet | 6c0efe6 | 2016-10-19 08:13:29 -0700 | [diff] [blame] | 2478 | body = json.loads(kw['body']) |
| 2479 | self.assertEqual(len(body['builds']), 1) |
| 2480 | build = body['builds'][0] |
| 2481 | params = json.loads(build.pop('parameters_json')) |
| 2482 | self.assertEqual(params, { |
| 2483 | u'builder_name': u'win', |
| 2484 | u'changes': [{u'author': {u'email': u'owner@e.mail'}, |
| 2485 | u'revision': None}], |
| 2486 | u'properties': { |
| 2487 | u'category': u'git_cl_try', |
| 2488 | u'issue': 123, |
| 2489 | u'patch_project': u'depot_tools', |
| 2490 | u'patch_storage': u'rietveld', |
| 2491 | u'patchset': 20001, |
borenet | 6c0efe6 | 2016-10-19 08:13:29 -0700 | [diff] [blame] | 2492 | u'rietveld': u'https://codereview.chromium.org', |
| 2493 | } |
| 2494 | }) |
| 2495 | self.assertEqual(build, { |
| 2496 | u'bucket': u'test.bucket', |
| 2497 | u'client_operation_id': u'uuid4', |
| 2498 | u'tags': [u'builder:win', |
| 2499 | u'buildset:patch/rietveld/codereview.chromium.org/123/20001', |
| 2500 | u'user_agent:git_cl_try'], |
| 2501 | }) |
| 2502 | |
| 2503 | self.mock(git_cl, '_buildbucket_retry', _buildbucket_retry) |
| 2504 | |
| 2505 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
| 2506 | self.assertEqual(0, git_cl.main([ |
| 2507 | 'try', '-B', 'test.bucket', '-b', 'win'])) |
| 2508 | self.assertRegexpMatches( |
| 2509 | git_cl.sys.stdout.getvalue(), |
| 2510 | 'Tried jobs on:\nBucket: test.bucket') |
tandrii | de281ae | 2016-10-12 06:02:30 -0700 | [diff] [blame] | 2511 | |
qyearsley | 123a468 | 2016-10-26 09:12:17 -0700 | [diff] [blame] | 2512 | def test_git_cl_try_bots_on_multiple_masters(self): |
| 2513 | self.mock(git_cl.Changelist, 'GetMostRecentPatchset', lambda _: 20001) |
| 2514 | self.calls = [ |
| 2515 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
| 2516 | ((['git', 'config', 'branch.feature.rietveldissue'],), '123'), |
| 2517 | ((['git', 'config', 'rietveld.autoupdate'],), CERR1), |
| 2518 | ((['git', 'config', 'rietveld.server'],), |
tandrii | 8c5a353 | 2016-11-04 07:52:02 -0700 | [diff] [blame] | 2519 | 'https://codereview.chromium.org'), |
qyearsley | 123a468 | 2016-10-26 09:12:17 -0700 | [diff] [blame] | 2520 | ((['git', 'config', 'branch.feature.rietveldserver'],), CERR1), |
| 2521 | ((['git', 'config', 'branch.feature.rietveldpatchset'],), '20001'), |
| 2522 | ] |
| 2523 | |
| 2524 | def _buildbucket_retry(*_, **kw): |
| 2525 | body = json.loads(kw['body']) |
| 2526 | self.assertEqual(len(body['builds']), 2) |
| 2527 | |
| 2528 | first_build_params = json.loads(body['builds'][0]['parameters_json']) |
| 2529 | self.assertEqual(first_build_params['builder_name'], 'builder1') |
| 2530 | self.assertEqual(first_build_params['properties']['master'], 'master1') |
| 2531 | |
| 2532 | first_build_params = json.loads(body['builds'][1]['parameters_json']) |
| 2533 | self.assertEqual(first_build_params['builder_name'], 'builder2') |
| 2534 | self.assertEqual(first_build_params['properties']['master'], 'master2') |
| 2535 | |
| 2536 | self.mock(git_cl, '_buildbucket_retry', _buildbucket_retry) |
| 2537 | |
| 2538 | self.mock(git_cl.urllib2, 'urlopen', lambda _: StringIO.StringIO( |
| 2539 | json.dumps({'builder1': ['master1'], 'builder2': ['master2']}))) |
| 2540 | |
| 2541 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
| 2542 | self.assertEqual( |
| 2543 | 0, git_cl.main(['try', '-b', 'builder1', '-b', 'builder2'])) |
| 2544 | self.assertEqual( |
| 2545 | git_cl.sys.stdout.getvalue(), |
| 2546 | 'Tried jobs on:\n' |
| 2547 | 'Bucket: master.master1\n' |
| 2548 | ' builder1: []\n' |
| 2549 | 'Bucket: master.master2\n' |
| 2550 | ' builder2: []\n' |
| 2551 | 'To see results here, run: git cl try-results\n' |
| 2552 | 'To see results in browser, run: git cl web\n') |
| 2553 | |
tandrii | 16e0b4e | 2016-06-07 10:34:28 -0700 | [diff] [blame] | 2554 | def _common_GerritCommitMsgHookCheck(self): |
| 2555 | self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
| 2556 | self.mock(git_cl.os.path, 'abspath', |
| 2557 | lambda path: self._mocked_call(['abspath', path])) |
| 2558 | self.mock(git_cl.os.path, 'exists', |
| 2559 | lambda path: self._mocked_call(['exists', path])) |
| 2560 | self.mock(git_cl.gclient_utils, 'FileRead', |
| 2561 | lambda path: self._mocked_call(['FileRead', path])) |
| 2562 | self.mock(git_cl.gclient_utils, 'rm_file_or_tree', |
| 2563 | lambda path: self._mocked_call(['rm_file_or_tree', path])) |
| 2564 | self.calls = [ |
| 2565 | ((['git', 'rev-parse', '--show-cdup'],), '../'), |
| 2566 | ((['abspath', '../'],), '/abs/git_repo_root'), |
| 2567 | ] |
| 2568 | return git_cl.Changelist(codereview='gerrit', issue=123) |
| 2569 | |
| 2570 | def test_GerritCommitMsgHookCheck_custom_hook(self): |
| 2571 | cl = self._common_GerritCommitMsgHookCheck() |
| 2572 | self.calls += [ |
| 2573 | ((['exists', '/abs/git_repo_root/.git/hooks/commit-msg'],), True), |
| 2574 | ((['FileRead', '/abs/git_repo_root/.git/hooks/commit-msg'],), |
| 2575 | '#!/bin/sh\necho "custom hook"') |
| 2576 | ] |
| 2577 | cl._codereview_impl._GerritCommitMsgHookCheck(offer_removal=True) |
| 2578 | |
| 2579 | def test_GerritCommitMsgHookCheck_not_exists(self): |
| 2580 | cl = self._common_GerritCommitMsgHookCheck() |
| 2581 | self.calls += [ |
| 2582 | ((['exists', '/abs/git_repo_root/.git/hooks/commit-msg'],), False), |
| 2583 | ] |
| 2584 | cl._codereview_impl._GerritCommitMsgHookCheck(offer_removal=True) |
| 2585 | |
| 2586 | def test_GerritCommitMsgHookCheck(self): |
| 2587 | cl = self._common_GerritCommitMsgHookCheck() |
| 2588 | self.calls += [ |
| 2589 | ((['exists', '/abs/git_repo_root/.git/hooks/commit-msg'],), True), |
| 2590 | ((['FileRead', '/abs/git_repo_root/.git/hooks/commit-msg'],), |
| 2591 | '...\n# From Gerrit Code Review\n...\nadd_ChangeId()\n'), |
| 2592 | (('Do you want to remove it now? [Yes/No]',), 'Yes'), |
| 2593 | ((['rm_file_or_tree', '/abs/git_repo_root/.git/hooks/commit-msg'],), |
| 2594 | ''), |
| 2595 | ] |
| 2596 | cl._codereview_impl._GerritCommitMsgHookCheck(offer_removal=True) |
| 2597 | |
tandrii | c4344b5 | 2016-08-29 06:04:54 -0700 | [diff] [blame] | 2598 | def test_GerritCmdLand(self): |
| 2599 | self.calls += [ |
| 2600 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
| 2601 | ((['git', 'config', 'branch.feature.gerritsquashhash'],), |
| 2602 | 'deadbeaf'), |
| 2603 | ((['git', 'diff', 'deadbeaf'],), ''), # No diff. |
| 2604 | ((['git', 'config', 'branch.feature.gerritserver'],), |
| 2605 | 'chromium-review.googlesource.com'), |
| 2606 | ] |
| 2607 | cl = git_cl.Changelist(issue=123, codereview='gerrit') |
| 2608 | cl._codereview_impl._GetChangeDetail = lambda _: { |
| 2609 | 'labels': {}, |
| 2610 | 'current_revision': 'deadbeaf', |
| 2611 | } |
agable | 32978d9 | 2016-11-01 12:55:02 -0700 | [diff] [blame] | 2612 | cl._codereview_impl._GetChangeCommit = lambda: { |
| 2613 | 'commit': 'deadbeef', |
Aaron Gable | 02cdbb4 | 2016-12-13 16:24:25 -0800 | [diff] [blame] | 2614 | 'web_links': [{'name': 'gitiles', |
agable | 32978d9 | 2016-11-01 12:55:02 -0700 | [diff] [blame] | 2615 | 'url': 'https://git.googlesource.com/test/+/deadbeef'}], |
| 2616 | } |
tandrii | c4344b5 | 2016-08-29 06:04:54 -0700 | [diff] [blame] | 2617 | cl._codereview_impl.SubmitIssue = lambda wait_for_merge: None |
tandrii | 8da412c | 2016-09-07 16:01:07 -0700 | [diff] [blame] | 2618 | out = StringIO.StringIO() |
| 2619 | self.mock(sys, 'stdout', out) |
tandrii | c4344b5 | 2016-08-29 06:04:54 -0700 | [diff] [blame] | 2620 | self.assertEqual(0, cl.CMDLand(force=True, bypass_hooks=True, verbose=True)) |
tandrii | 8da412c | 2016-09-07 16:01:07 -0700 | [diff] [blame] | 2621 | self.assertRegexpMatches(out.getvalue(), 'Issue.*123 has been submitted') |
agable | 32978d9 | 2016-11-01 12:55:02 -0700 | [diff] [blame] | 2622 | self.assertRegexpMatches(out.getvalue(), 'Landed as .*deadbeef') |
tandrii | c4344b5 | 2016-08-29 06:04:54 -0700 | [diff] [blame] | 2623 | |
tandrii | 221ab25 | 2016-10-06 08:12:04 -0700 | [diff] [blame] | 2624 | BUILDBUCKET_BUILDS_MAP = { |
| 2625 | '9000': { |
| 2626 | 'id': '9000', |
| 2627 | 'status': 'STARTED', |
| 2628 | 'url': 'http://build.cr.org/p/x.y/builders/my-builder/builds/2', |
| 2629 | 'result_details_json': '{"properties": {}}', |
| 2630 | 'bucket': 'master.x.y', |
| 2631 | 'created_by': 'user:someone@chromium.org', |
| 2632 | 'created_ts': '147200002222000', |
| 2633 | 'parameters_json': '{"builder_name": "my-builder", "category": ""}', |
| 2634 | }, |
| 2635 | '8000': { |
| 2636 | 'id': '8000', |
| 2637 | 'status': 'COMPLETED', |
| 2638 | 'result': 'FAILURE', |
| 2639 | 'failure_reason': 'BUILD_FAILURE', |
| 2640 | 'url': 'http://build.cr.org/p/x.y/builders/my-builder/builds/1', |
| 2641 | 'result_details_json': '{"properties": {}}', |
| 2642 | 'bucket': 'master.x.y', |
| 2643 | 'created_by': 'user:someone@chromium.org', |
| 2644 | 'created_ts': '147200001111000', |
| 2645 | 'parameters_json': '{"builder_name": "my-builder", "category": ""}', |
| 2646 | }, |
| 2647 | } |
| 2648 | |
| 2649 | def test_write_try_results_json(self): |
| 2650 | expected_output = [ |
| 2651 | { |
| 2652 | 'buildbucket_id': '8000', |
| 2653 | 'bucket': 'master.x.y', |
| 2654 | 'builder_name': 'my-builder', |
| 2655 | 'status': 'COMPLETED', |
| 2656 | 'result': 'FAILURE', |
| 2657 | 'failure_reason': 'BUILD_FAILURE', |
| 2658 | 'url': 'http://build.cr.org/p/x.y/builders/my-builder/builds/1', |
| 2659 | }, |
| 2660 | { |
| 2661 | 'buildbucket_id': '9000', |
| 2662 | 'bucket': 'master.x.y', |
| 2663 | 'builder_name': 'my-builder', |
| 2664 | 'status': 'STARTED', |
| 2665 | 'result': None, |
| 2666 | 'failure_reason': None, |
| 2667 | 'url': 'http://build.cr.org/p/x.y/builders/my-builder/builds/2', |
| 2668 | } |
| 2669 | ] |
| 2670 | self.calls = [(('write_json', 'output.json', expected_output), '')] |
| 2671 | git_cl.write_try_results_json('output.json', self.BUILDBUCKET_BUILDS_MAP) |
| 2672 | |
tandrii | 45b2a58 | 2016-10-11 03:14:16 -0700 | [diff] [blame] | 2673 | def _setup_fetch_try_jobs(self, most_recent_patchset=20001): |
tandrii | 221ab25 | 2016-10-06 08:12:04 -0700 | [diff] [blame] | 2674 | out = StringIO.StringIO() |
| 2675 | self.mock(sys, 'stdout', out) |
tandrii | 45b2a58 | 2016-10-11 03:14:16 -0700 | [diff] [blame] | 2676 | self.mock(git_cl.Changelist, 'GetMostRecentPatchset', |
| 2677 | lambda *args: most_recent_patchset) |
tandrii | 221ab25 | 2016-10-06 08:12:04 -0700 | [diff] [blame] | 2678 | self.mock(git_cl.auth, 'get_authenticator_for_host', lambda host, _cfg: |
| 2679 | self._mocked_call(['get_authenticator_for_host', host])) |
| 2680 | self.mock(git_cl, '_buildbucket_retry', lambda *_, **__: |
| 2681 | self._mocked_call(['_buildbucket_retry'])) |
tandrii | 45b2a58 | 2016-10-11 03:14:16 -0700 | [diff] [blame] | 2682 | |
| 2683 | def _setup_fetch_try_jobs_rietveld(self, *request_results): |
| 2684 | self._setup_fetch_try_jobs(most_recent_patchset=20001) |
tandrii | 221ab25 | 2016-10-06 08:12:04 -0700 | [diff] [blame] | 2685 | self.calls += [ |
| 2686 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
| 2687 | ((['git', 'config', 'branch.feature.rietveldissue'],), '1'), |
| 2688 | ((['git', 'config', 'rietveld.autoupdate'],), CERR1), |
| 2689 | ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'), |
| 2690 | ((['git', 'config', 'branch.feature.rietveldpatchset'],), '20001'), |
| 2691 | ((['git', 'config', 'branch.feature.rietveldserver'],), |
| 2692 | 'codereview.example.com'), |
| 2693 | ((['get_authenticator_for_host', 'codereview.example.com'],), |
| 2694 | AuthenticatorMock()), |
| 2695 | ] + [((['_buildbucket_retry'],), r) for r in request_results] |
| 2696 | |
| 2697 | def test_fetch_try_jobs_none_rietveld(self): |
| 2698 | self._setup_fetch_try_jobs_rietveld({}) |
tandrii | 45b2a58 | 2016-10-11 03:14:16 -0700 | [diff] [blame] | 2699 | # Simulate that user isn't logged in. |
| 2700 | self.mock(AuthenticatorMock, 'has_cached_credentials', lambda _: False) |
tandrii | 221ab25 | 2016-10-06 08:12:04 -0700 | [diff] [blame] | 2701 | self.assertEqual(0, git_cl.main(['try-results'])) |
tandrii | 45b2a58 | 2016-10-11 03:14:16 -0700 | [diff] [blame] | 2702 | self.assertRegexpMatches(sys.stdout.getvalue(), |
| 2703 | 'Warning: Some results might be missing') |
tandrii | 221ab25 | 2016-10-06 08:12:04 -0700 | [diff] [blame] | 2704 | self.assertRegexpMatches(sys.stdout.getvalue(), 'No try jobs') |
| 2705 | |
| 2706 | def test_fetch_try_jobs_some_rietveld(self): |
| 2707 | self._setup_fetch_try_jobs_rietveld({ |
| 2708 | 'builds': self.BUILDBUCKET_BUILDS_MAP.values(), |
| 2709 | }) |
| 2710 | self.assertEqual(0, git_cl.main(['try-results'])) |
tandrii | 45b2a58 | 2016-10-11 03:14:16 -0700 | [diff] [blame] | 2711 | self.assertRegexpMatches(sys.stdout.getvalue(), '^Failures:') |
| 2712 | self.assertRegexpMatches(sys.stdout.getvalue(), 'Started:') |
| 2713 | self.assertRegexpMatches(sys.stdout.getvalue(), '2 try jobs') |
| 2714 | |
| 2715 | def _setup_fetch_try_jobs_gerrit(self, *request_results): |
| 2716 | self._setup_fetch_try_jobs(most_recent_patchset=13) |
| 2717 | self.calls += [ |
| 2718 | ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
| 2719 | ((['git', 'config', 'branch.feature.rietveldissue'],), CERR1), |
| 2720 | ((['git', 'config', 'branch.feature.gerritissue'],), '1'), |
Ravi Mistry | fda50ca | 2016-11-14 10:19:18 -0500 | [diff] [blame] | 2721 | # TODO(tandrii): Uncomment the below if we decide to support checking |
| 2722 | # patchsets for Gerrit. |
tandrii | 45b2a58 | 2016-10-11 03:14:16 -0700 | [diff] [blame] | 2723 | # Simulate that Gerrit has more patchsets than local. |
Ravi Mistry | fda50ca | 2016-11-14 10:19:18 -0500 | [diff] [blame] | 2724 | # ((['git', 'config', 'branch.feature.gerritpatchset'],), '12'), |
tandrii | 45b2a58 | 2016-10-11 03:14:16 -0700 | [diff] [blame] | 2725 | ((['git', 'config', 'branch.feature.gerritserver'],), |
| 2726 | 'https://x-review.googlesource.com'), |
| 2727 | ((['get_authenticator_for_host', 'x-review.googlesource.com'],), |
| 2728 | AuthenticatorMock()), |
| 2729 | ] + [((['_buildbucket_retry'],), r) for r in request_results] |
| 2730 | |
| 2731 | def test_fetch_try_jobs_none_gerrit(self): |
| 2732 | self._setup_fetch_try_jobs_gerrit({}) |
| 2733 | self.assertEqual(0, git_cl.main(['try-results'])) |
Ravi Mistry | fda50ca | 2016-11-14 10:19:18 -0500 | [diff] [blame] | 2734 | # TODO(tandrii): Uncomment the below if we decide to support checking |
| 2735 | # patchsets for Gerrit. |
| 2736 | # self.assertRegexpMatches( |
| 2737 | # sys.stdout.getvalue(), |
| 2738 | # r'Warning: Codereview server has newer patchsets \(13\)') |
tandrii | 45b2a58 | 2016-10-11 03:14:16 -0700 | [diff] [blame] | 2739 | self.assertRegexpMatches(sys.stdout.getvalue(), 'No try jobs') |
| 2740 | |
| 2741 | def test_fetch_try_jobs_some_gerrit(self): |
| 2742 | self._setup_fetch_try_jobs_gerrit({ |
| 2743 | 'builds': self.BUILDBUCKET_BUILDS_MAP.values(), |
| 2744 | }) |
Ravi Mistry | fda50ca | 2016-11-14 10:19:18 -0500 | [diff] [blame] | 2745 | # TODO(tandrii): Uncomment the below if we decide to support checking |
| 2746 | # patchsets for Gerrit. |
| 2747 | # self.calls.remove( |
| 2748 | # ((['git', 'config', 'branch.feature.gerritpatchset'],), '12')) |
tandrii | 45b2a58 | 2016-10-11 03:14:16 -0700 | [diff] [blame] | 2749 | self.assertEqual(0, git_cl.main(['try-results', '--patchset', '5'])) |
| 2750 | |
| 2751 | # ... and doesn't result in warning. |
| 2752 | self.assertNotRegexpMatches(sys.stdout.getvalue(), 'Warning') |
| 2753 | self.assertRegexpMatches(sys.stdout.getvalue(), '^Failures:') |
tandrii | 221ab25 | 2016-10-06 08:12:04 -0700 | [diff] [blame] | 2754 | self.assertRegexpMatches(sys.stdout.getvalue(), 'Started:') |
| 2755 | self.assertRegexpMatches(sys.stdout.getvalue(), '2 try jobs') |
| 2756 | |
Andrii Shyshkalov | 258e0a6 | 2017-01-24 16:50:57 +0100 | [diff] [blame] | 2757 | def _mock_gerrit_changes_for_detail_cache(self): |
| 2758 | self.mock(git_cl._GerritChangelistImpl, '_GetGerritHost', lambda _: 'host') |
Andrii Shyshkalov | 258e0a6 | 2017-01-24 16:50:57 +0100 | [diff] [blame] | 2759 | |
| 2760 | def test_gerrit_change_detail_cache_normalize(self): |
| 2761 | self._mock_gerrit_changes_for_detail_cache() |
| 2762 | self.calls = [ |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 2763 | (('GetChangeDetail', 'host', '2', ['CASE']), 'b'), |
Andrii Shyshkalov | 258e0a6 | 2017-01-24 16:50:57 +0100 | [diff] [blame] | 2764 | ] |
| 2765 | cl = git_cl.Changelist(codereview='gerrit') |
| 2766 | self.assertEqual(cl._GetChangeDetail(issue=2, options=['CaSe']), 'b') |
| 2767 | self.assertEqual(cl._GetChangeDetail(issue=2, options=['CASE']), 'b') |
| 2768 | self.assertEqual(cl._GetChangeDetail(issue='2'), 'b') |
| 2769 | self.assertEqual(cl._GetChangeDetail(issue=2), 'b') |
| 2770 | |
| 2771 | def test_gerrit_change_detail_cache_simple(self): |
| 2772 | self._mock_gerrit_changes_for_detail_cache() |
| 2773 | self.calls = [ |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 2774 | (('GetChangeDetail', 'host', '1', []), 'a'), |
| 2775 | (('GetChangeDetail', 'host', '2', []), 'b'), |
| 2776 | (('GetChangeDetail', 'host', '2', []), 'b2'), |
Andrii Shyshkalov | 258e0a6 | 2017-01-24 16:50:57 +0100 | [diff] [blame] | 2777 | ] |
| 2778 | cl = git_cl.Changelist(issue=1, codereview='gerrit') |
| 2779 | self.assertEqual(cl._GetChangeDetail(), 'a') # Miss. |
| 2780 | self.assertEqual(cl._GetChangeDetail(), 'a') |
| 2781 | self.assertEqual(cl._GetChangeDetail(issue=2), 'b') # Miss. |
Andrii Shyshkalov | 1897532 | 2017-01-25 16:44:13 +0100 | [diff] [blame] | 2782 | self.assertEqual(cl._GetChangeDetail(issue=2, no_cache=True), 'b2') # Miss. |
Andrii Shyshkalov | 258e0a6 | 2017-01-24 16:50:57 +0100 | [diff] [blame] | 2783 | self.assertEqual(cl._GetChangeDetail(), 'a') |
| 2784 | self.assertEqual(cl._GetChangeDetail(issue=2), 'b2') |
| 2785 | |
| 2786 | def test_gerrit_change_detail_cache_options(self): |
| 2787 | self._mock_gerrit_changes_for_detail_cache() |
| 2788 | self.calls = [ |
Andrii Shyshkalov | 8fc0c1d | 2017-01-26 09:38:10 +0100 | [diff] [blame] | 2789 | (('GetChangeDetail', 'host', '1', ['C', 'A', 'B']), 'cab'), |
| 2790 | (('GetChangeDetail', 'host', '1', ['A', 'D']), 'ad'), |
| 2791 | (('GetChangeDetail', 'host', '1', ['A']), 'a'), # no_cache=True |
| 2792 | (('GetChangeDetail', 'host', '1', ['B']), 'b'), # no longer in cache. |
Andrii Shyshkalov | 258e0a6 | 2017-01-24 16:50:57 +0100 | [diff] [blame] | 2793 | ] |
| 2794 | cl = git_cl.Changelist(issue=1, codereview='gerrit') |
| 2795 | self.assertEqual(cl._GetChangeDetail(options=['C', 'A', 'B']), 'cab') |
| 2796 | self.assertEqual(cl._GetChangeDetail(options=['A', 'B', 'C']), 'cab') |
| 2797 | self.assertEqual(cl._GetChangeDetail(options=['B', 'A']), 'cab') |
| 2798 | self.assertEqual(cl._GetChangeDetail(options=['C']), 'cab') |
| 2799 | self.assertEqual(cl._GetChangeDetail(options=['A']), 'cab') |
| 2800 | self.assertEqual(cl._GetChangeDetail(), 'cab') |
| 2801 | |
| 2802 | self.assertEqual(cl._GetChangeDetail(options=['A', 'D']), 'ad') |
| 2803 | self.assertEqual(cl._GetChangeDetail(options=['A']), 'cab') |
| 2804 | self.assertEqual(cl._GetChangeDetail(options=['D']), 'ad') |
| 2805 | self.assertEqual(cl._GetChangeDetail(), 'cab') |
| 2806 | |
| 2807 | # Finally, no_cache should invalidate all caches for given change. |
| 2808 | self.assertEqual(cl._GetChangeDetail(options=['A'], no_cache=True), 'a') |
| 2809 | self.assertEqual(cl._GetChangeDetail(options=['B']), 'b') |
| 2810 | |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 2811 | |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 2812 | if __name__ == '__main__': |
Andrii Shyshkalov | 18df0cd | 2017-01-25 15:22:20 +0100 | [diff] [blame] | 2813 | logging.basicConfig( |
| 2814 | level=logging.DEBUG if '-v' in sys.argv else logging.ERROR) |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 2815 | unittest.main() |