| OLD | NEW |
| (Empty) |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 import base64 | |
| 6 from datetime import datetime | |
| 7 import json | |
| 8 import re | |
| 9 | |
| 10 from testing_utils import testing | |
| 11 | |
| 12 from common import git_repository | |
| 13 from common import retry_http_client | |
| 14 from common.change_log import ChangeLog | |
| 15 | |
| 16 | |
| 17 COMMIT_MESSAGE = ('Add popover for snapshot canvas log.\n\n' | |
| 18 'Review URL: https://codereview.chromium.org/320423004\n\n' | |
| 19 'Review URL: https://codereview.chromium.org/328113005\n\n' | |
| 20 'Cr-Commit-Position: refs/heads/master@{#175976}') | |
| 21 | |
| 22 COMMIT_LOG = """)]}' | |
| 23 { | |
| 24 "commit": "bcfd5a12eea05588aee98b7cf7e032d8cb5b58bb", | |
| 25 "tree": "481fd0f3bdf6eda5ca29ec6cbc6aa476b3684143", | |
| 26 "parents": [ | |
| 27 "42a94bb5e2ef8525d7dadbd8eae37fe7cb8d77d0" | |
| 28 ], | |
| 29 "author": { | |
| 30 "name": "test1@chromium.org", | |
| 31 "email": "test1@chromium.org@bbb929c8-8fbe-4397-9dbb-9b2b20218538", | |
| 32 "time": "Wed Jun 11 19:35:32 2014 -0400" | |
| 33 }, | |
| 34 "committer": { | |
| 35 "name": "test1@chromium.org", | |
| 36 "email": "test1@chromium.org", | |
| 37 "time": "Wed Jun 11 19:35:32 2014" | |
| 38 }, | |
| 39 "message": %s, | |
| 40 "tree_diff": [ | |
| 41 { | |
| 42 "type": "add", | |
| 43 "old_id": "f71f1167c2204626057d26912b8a2ff096fe4bd2", | |
| 44 "old_mode": 33188, | |
| 45 "old_path": "/dev/null", | |
| 46 "new_id": "165fb11e0658f41d66038199056a53bcfab5dda0", | |
| 47 "new_mode": 33188, | |
| 48 "new_path": "Source/devtools/front_end/layers/added_file.js" | |
| 49 }, | |
| 50 { | |
| 51 "type": "delete", | |
| 52 "old_id": "f71f1167c2204626057d26912b8a2ff096fe4bd3", | |
| 53 "old_mode": 33188, | |
| 54 "old_path": "Source/devtools/front_end/layers/deleted_file.js", | |
| 55 "new_id": "165fb11e0658f41d66038199056a53bcfab5dda1", | |
| 56 "new_mode": 33188, | |
| 57 "new_path": "/dev/null" | |
| 58 }, | |
| 59 { | |
| 60 "type": "modify", | |
| 61 "old_id": "f71f1167c2204626057d26912b8a2ff096fe4bd1", | |
| 62 "old_mode": 33188, | |
| 63 "old_path": "Source/devtools/front_end/layers/modified_file.js", | |
| 64 "new_id": "165fb11e0658f41d66038199056a53bcfab5dda9", | |
| 65 "new_mode": 33188, | |
| 66 "new_path": "Source/devtools/front_end/layers/modified_file.js" | |
| 67 }, | |
| 68 { | |
| 69 "type": "copy", | |
| 70 "old_id": "f71f1167c2204626057d26912b8a2ff096fe4bd4", | |
| 71 "old_mode": 33188, | |
| 72 "old_path": "Source/devtools/front_end/layers/file.js", | |
| 73 "new_id": "165fb11e0658f41d66038199056a53bcfab5dda2", | |
| 74 "new_mode": 33188, | |
| 75 "new_path": "Source/devtools/front_end/layers/copied_file.js" | |
| 76 }, | |
| 77 { | |
| 78 "type": "rename", | |
| 79 "old_id": "f71f1167c2204626057d26912b8a2ff096fe4bd5", | |
| 80 "old_mode": 33188, | |
| 81 "old_path": "Source/devtools/front_end/layers/file.js", | |
| 82 "new_id": "165fb11e0658f41d66038199056a53bcfab5dda3", | |
| 83 "new_mode": 33188, | |
| 84 "new_path": "Source/devtools/front_end/layers/renamed_file.js" | |
| 85 } | |
| 86 ] | |
| 87 }""" % json.JSONEncoder().encode(COMMIT_MESSAGE) | |
| 88 | |
| 89 EXPECTED_CHANGE_LOG_JSON = { | |
| 90 'author_name': 'test1@chromium.org', | |
| 91 'message': COMMIT_MESSAGE, | |
| 92 'committer_email': 'test1@chromium.org', | |
| 93 'commit_position': 175976, | |
| 94 'author_email': 'test1@chromium.org', | |
| 95 'touched_files': [ | |
| 96 { | |
| 97 'change_type': 'add', | |
| 98 'new_path': 'Source/devtools/front_end/layers/added_file.js', | |
| 99 'old_path': '/dev/null' | |
| 100 }, | |
| 101 { | |
| 102 'change_type': 'delete', | |
| 103 'new_path': '/dev/null', | |
| 104 'old_path': 'Source/devtools/front_end/layers/deleted_file.js' | |
| 105 }, | |
| 106 { | |
| 107 'change_type': 'modify', | |
| 108 'new_path': 'Source/devtools/front_end/layers/modified_file.js', | |
| 109 'old_path': 'Source/devtools/front_end/layers/modified_file.js' | |
| 110 }, | |
| 111 { | |
| 112 'change_type': 'copy', | |
| 113 'new_path': 'Source/devtools/front_end/layers/copied_file.js', | |
| 114 'old_path': 'Source/devtools/front_end/layers/file.js' | |
| 115 }, | |
| 116 { | |
| 117 'change_type': 'rename', | |
| 118 'new_path': 'Source/devtools/front_end/layers/renamed_file.js', | |
| 119 'old_path': 'Source/devtools/front_end/layers/file.js' | |
| 120 } | |
| 121 ], | |
| 122 'author_time': datetime(2014, 06, 11, 23, 35, 32), | |
| 123 'committer_time': datetime(2014, 06, 11, 19, 35, 32), | |
| 124 'commit_url': | |
| 125 'https://repo.test/+/bcfd5a12eea05588aee98b7cf7e032d8cb5b58bb', | |
| 126 'code_review_url': 'https://codereview.chromium.org/328113005', | |
| 127 'committer_name': 'test1@chromium.org', | |
| 128 'revision': 'bcfd5a12eea05588aee98b7cf7e032d8cb5b58bb', | |
| 129 'reverted_revision': None | |
| 130 } | |
| 131 | |
| 132 COMMIT_LOG_WITH_UNKNOWN_FILE_CHANGE_TYPE = """)]}' | |
| 133 { | |
| 134 "commit": "bcfd5a12eea05588aee98b7cf7e032d8cb5b58bb", | |
| 135 "tree": "481fd0f3bdf6eda5ca29ec6cbc6aa476b3684143", | |
| 136 "parents": [ | |
| 137 "42a94bb5e2ef8525d7dadbd8eae37fe7cb8d77d0" | |
| 138 ], | |
| 139 "author": { | |
| 140 "name": "test1@chromium.org", | |
| 141 "email": "test1@chromium.org@bbb929c8-8fbe-4397-9dbb-9b2b20218538", | |
| 142 "time": "Wed Jun 11 19:35:32 2014" | |
| 143 }, | |
| 144 "committer": { | |
| 145 "name": "test1@chromium.org", | |
| 146 "email": "test1@chromium.org@bbb929c8-8fbe-4397-9dbb-9b2b20218538", | |
| 147 "time": "Wed Jun 11 19:35:32 2014" | |
| 148 }, | |
| 149 "message": "message", | |
| 150 "tree_diff": [ | |
| 151 { | |
| 152 "type": "unknown_change_type", | |
| 153 "old_id": "f71f1167c2204626057d26912b8a2ff096fe4bd2", | |
| 154 "old_mode": 33188, | |
| 155 "old_path": "/dev/null", | |
| 156 "new_id": "165fb11e0658f41d66038199056a53bcfab5dda0", | |
| 157 "new_mode": 33188, | |
| 158 "new_path": "Source/devtools/front_end/layers/added_file.js" | |
| 159 } | |
| 160 ] | |
| 161 }""" | |
| 162 | |
| 163 GITILES_FILE_BLAME_RESULT = """)]}' | |
| 164 { | |
| 165 "regions": [ | |
| 166 { | |
| 167 "start": 1, | |
| 168 "count": 6, | |
| 169 "path": "chrome/test/chromedriver/element_commands.cc", | |
| 170 "commit": "584ae1f26b070150f65a03dba75fc8af6b6f6ece", | |
| 171 "author": { | |
| 172 "name": "test2@chromium.org", | |
| 173 "email": "test2@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98", | |
| 174 "time": "2013-02-11 20:18:51" | |
| 175 } | |
| 176 }, | |
| 177 { | |
| 178 "start": 7, | |
| 179 "count": 1, | |
| 180 "path": "chrome/test/chromedriver/element_commands.cc", | |
| 181 "commit": "030b5d9bb7d6c9f673cd8f0c86d8f1e921de7076", | |
| 182 "author": { | |
| 183 "name": "test3@chromium.org", | |
| 184 "email": "test3@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98", | |
| 185 "time": "2014-02-06 10:02:10 +0400" | |
| 186 } | |
| 187 }, | |
| 188 { | |
| 189 "start": 8, | |
| 190 "count": 1, | |
| 191 "path": "chrome/test/chromedriver/element_commands.cc", | |
| 192 "commit": "584ae1f26b070150f65a03dba75fc8af6b6f6ece", | |
| 193 "author": { | |
| 194 "name": "test2@chromium.org", | |
| 195 "email": "test2@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98", | |
| 196 "time": "2013-02-11 20:18:51" | |
| 197 } | |
| 198 } | |
| 199 ] | |
| 200 }""" | |
| 201 | |
| 202 EXPECTED_FILE_BLAME_JSON = { | |
| 203 'regions': [ | |
| 204 { | |
| 205 'count': 6, | |
| 206 'author_email': u'test2@chromium.org', | |
| 207 'author_time': datetime(2013, 02, 11, 20, 18, 51), | |
| 208 'author_name': u'test2@chromium.org', | |
| 209 'start': 1, | |
| 210 'revision': u'584ae1f26b070150f65a03dba75fc8af6b6f6ece' | |
| 211 }, | |
| 212 { | |
| 213 'count': 1, | |
| 214 'author_email': u'test3@chromium.org', | |
| 215 'author_time': datetime(2014, 02, 06, 06, 02, 10), | |
| 216 'author_name': u'test3@chromium.org', | |
| 217 'start': 7, | |
| 218 'revision': u'030b5d9bb7d6c9f673cd8f0c86d8f1e921de7076' | |
| 219 }, | |
| 220 { | |
| 221 'count': 1, | |
| 222 'author_email': u'test2@chromium.org', | |
| 223 'author_time': datetime(2013, 02, 11, 20, 18, 51), | |
| 224 'author_name': u'test2@chromium.org', | |
| 225 'start': 8, | |
| 226 'revision': u'584ae1f26b070150f65a03dba75fc8af6b6f6ece' | |
| 227 } | |
| 228 ], | |
| 229 'path': 'a/b/c.cc', | |
| 230 'revision': 'dummy_abcd1234' | |
| 231 } | |
| 232 | |
| 233 DUMMY_CHANGELOG_JSON = { | |
| 234 'author_name': 'test@chromium.org', | |
| 235 'message': 'dummy', | |
| 236 'committer_email': 'test@chromium.org', | |
| 237 'commit_position': 175976, | |
| 238 'author_email': 'test1@chromium.org', | |
| 239 'touched_files': [ | |
| 240 { | |
| 241 'change_type': 'add', | |
| 242 'new_path': 'Source/devtools/added_file.js', | |
| 243 'old_path': '/dev/null' | |
| 244 } | |
| 245 ], | |
| 246 'author_time': datetime(2016, 01, 11, 23, 35, 32), | |
| 247 'committer_time': datetime(2016, 01, 11, 19, 35, 32), | |
| 248 'commit_url': | |
| 249 'https://repo.test/+/bcfd', | |
| 250 'code_review_url': 'https://codereview.chromium.org/328113005', | |
| 251 'committer_name': 'test1@chromium.org', | |
| 252 'revision': 'bcfd', | |
| 253 'reverted_revision': None | |
| 254 } | |
| 255 | |
| 256 | |
| 257 class HttpClientForGit(retry_http_client.RetryHttpClient): | |
| 258 | |
| 259 def __init__(self): | |
| 260 super(HttpClientForGit, self).__init__() | |
| 261 self.response_for_url = {} | |
| 262 | |
| 263 def SetResponseForUrl(self, url, response): | |
| 264 self.response_for_url[url] = response | |
| 265 | |
| 266 def GetBackoff(self, *_): # pragma: no cover | |
| 267 """Override to avoid sleep.""" | |
| 268 return 0 | |
| 269 | |
| 270 def _Get(self, url, *_): | |
| 271 response = self.response_for_url.get(url) | |
| 272 if response is None: | |
| 273 return 404, 'Not Found' | |
| 274 else: | |
| 275 return 200, response | |
| 276 | |
| 277 def _Post(self, *_): # pragma: no cover | |
| 278 pass | |
| 279 | |
| 280 def _Put(self, *_): # pragma: no cover | |
| 281 pass | |
| 282 | |
| 283 | |
| 284 class GitRepositoryTest(testing.AppengineTestCase): | |
| 285 | |
| 286 def setUp(self): | |
| 287 super(GitRepositoryTest, self).setUp() | |
| 288 self.http_client_for_git = HttpClientForGit() | |
| 289 self.repo_url = 'https://repo.test' | |
| 290 self.git_repo = git_repository.GitRepository(self.repo_url, | |
| 291 self.http_client_for_git) | |
| 292 | |
| 293 def testExtractCommitPositionAndCodeReviewUrl(self): | |
| 294 testcases = [ | |
| 295 { | |
| 296 'message': | |
| 297 'balabala...\n' | |
| 298 '\n' | |
| 299 'BUG=604502\n' | |
| 300 '\n' | |
| 301 'Review-Url: https://codereview.chromium.org/1927593004\n' | |
| 302 'Cr-Commit-Position: refs/heads/master@{#390254}\n', | |
| 303 'commit_position': 390254, | |
| 304 'code_review_url': 'https://codereview.chromium.org/1927593004', | |
| 305 }, | |
| 306 { | |
| 307 'message': | |
| 308 'balabala...\n' | |
| 309 '\n' | |
| 310 'BUG=409934\n' | |
| 311 '\n' | |
| 312 'Review URL: https://codereview.chromium.org/547753003\n' | |
| 313 '\n' | |
| 314 'Cr-Commit-Position: refs/heads/master@{#293661}', | |
| 315 'commit_position': 293661, | |
| 316 'code_review_url': 'https://codereview.chromium.org/547753003', | |
| 317 }, | |
| 318 { | |
| 319 'message': | |
| 320 'balabala...\n' | |
| 321 '\n' | |
| 322 'balabala...\n' | |
| 323 '\n' | |
| 324 'R=test4@chromium.org\n' | |
| 325 '\n' | |
| 326 'Review URL: https://codereview.chromium.org/469523002\n' | |
| 327 '\n' | |
| 328 'Cr-Commit-Position: refs/heads/master@{#289120}', | |
| 329 'commit_position': 289120, | |
| 330 'code_review_url': 'https://codereview.chromium.org/469523002', | |
| 331 }, | |
| 332 { | |
| 333 'message': None, | |
| 334 'commit_position': None, | |
| 335 'code_review_url': None | |
| 336 } | |
| 337 ] | |
| 338 | |
| 339 for testcase in testcases: | |
| 340 (commit_position, | |
| 341 code_review_url) = self.git_repo.ExtractCommitPositionAndCodeReviewUrl( | |
| 342 testcase['message']) | |
| 343 self.assertEqual(commit_position, testcase['commit_position']) | |
| 344 self.assertEqual(code_review_url, testcase['code_review_url']) | |
| 345 | |
| 346 def testEndingSlashInRepoUrl(self): | |
| 347 git_repo1 = git_repository.GitRepository(self.repo_url, | |
| 348 self.http_client_for_git) | |
| 349 self.assertEqual(self.repo_url, git_repo1.repo_url) | |
| 350 | |
| 351 git_repo2 = git_repository.GitRepository('%s/' % self.repo_url, | |
| 352 self.http_client_for_git) | |
| 353 self.assertEqual(self.repo_url, git_repo2.repo_url) | |
| 354 | |
| 355 def testMalformattedJsonReponse(self): | |
| 356 self.http_client_for_git.SetResponseForUrl( | |
| 357 '%s/+/%s?format=json' % (self.repo_url, 'aaa'), 'abcde{"a": 1}') | |
| 358 self.assertRaisesRegexp( | |
| 359 Exception, re.escape('Response does not begin with )]}\'\n'), | |
| 360 self.git_repo.GetChangeLog, 'aaa') | |
| 361 | |
| 362 def testGetChangeLog(self): | |
| 363 self.http_client_for_git.SetResponseForUrl( | |
| 364 '%s/+/%s?format=json' % ( | |
| 365 self.repo_url, 'bcfd5a12eea05588aee98b7cf7e032d8cb5b58bb'), | |
| 366 COMMIT_LOG) | |
| 367 | |
| 368 self.assertIsNone(self.git_repo.GetChangeLog('not_existing_revision')) | |
| 369 | |
| 370 change_log = self.git_repo.GetChangeLog( | |
| 371 'bcfd5a12eea05588aee98b7cf7e032d8cb5b58bb') | |
| 372 self.assertEqual(EXPECTED_CHANGE_LOG_JSON, change_log.ToDict()) | |
| 373 | |
| 374 def testUnknownChangeType(self): | |
| 375 self.http_client_for_git.SetResponseForUrl( | |
| 376 '%s/+/%s?format=json' % ( | |
| 377 self.repo_url, 'bcfd5a12eea05588aee98b7cf7e032d8cb5b58bb'), | |
| 378 COMMIT_LOG_WITH_UNKNOWN_FILE_CHANGE_TYPE) | |
| 379 self.assertRaisesRegexp( | |
| 380 Exception, 'Unknown change type "unknown_change_type"', | |
| 381 self.git_repo.GetChangeLog, 'bcfd5a12eea05588aee98b7cf7e032d8cb5b58bb') | |
| 382 | |
| 383 def testGetChangeDiff(self): | |
| 384 self.assertIsNone(self.git_repo.GetChangeDiff('not_existing_revision')) | |
| 385 | |
| 386 git_revision = 'dummy_abcd1234' | |
| 387 original_diff = 'dummy diff' | |
| 388 self.http_client_for_git.SetResponseForUrl( | |
| 389 '%s/+/%s%%5E%%21/?format=text' % (self.repo_url, git_revision), | |
| 390 base64.b64encode(original_diff)) | |
| 391 diff = self.git_repo.GetChangeDiff(git_revision) | |
| 392 self.assertEqual(original_diff, diff) | |
| 393 | |
| 394 def testGetBlame(self): | |
| 395 self.assertIsNone(self.git_repo.GetBlame('path', 'not_existing_revision')) | |
| 396 | |
| 397 path = 'a/b/c.cc' | |
| 398 git_revision = 'dummy_abcd1234' | |
| 399 self.http_client_for_git.SetResponseForUrl( | |
| 400 '%s/+blame/%s/%s?format=json' % (self.repo_url, git_revision, path), | |
| 401 GITILES_FILE_BLAME_RESULT) | |
| 402 | |
| 403 blame = self.git_repo.GetBlame(path, git_revision) | |
| 404 self.assertEqual(EXPECTED_FILE_BLAME_JSON, blame.ToDict()) | |
| 405 | |
| 406 def testGetSource(self): | |
| 407 self.assertIsNone(self.git_repo.GetSource('path', 'not_existing_revision')) | |
| 408 | |
| 409 path = 'a/b/c.cc' | |
| 410 git_revision = 'dummy_abcd1234' | |
| 411 original_source = 'dummy source' | |
| 412 self.http_client_for_git.SetResponseForUrl( | |
| 413 '%s/+/%s/%s?format=text' % (self.repo_url, git_revision, path), | |
| 414 base64.b64encode(original_source)) | |
| 415 source = self.git_repo.GetSource(path, git_revision) | |
| 416 self.assertEqual(original_source, source) | |
| 417 | |
| 418 def testTimeConversion(self): | |
| 419 datetime_with_timezone = 'Wed Jul 22 19:35:32 2014 +0400' | |
| 420 expected_datetime = datetime(2014, 7, 22, 15, 35, 32) | |
| 421 utc_datetime = self.git_repo._GetDateTimeFromString(datetime_with_timezone) | |
| 422 | |
| 423 self.assertEqual(expected_datetime, utc_datetime) | |
| 424 | |
| 425 def testGetRevertedRevision(self): | |
| 426 message = ( | |
| 427 'Revert of test1\n\nReason for revert:\nrevert test1\n\n' | |
| 428 'Original issue\'s description:\n> test 1\n>\n' | |
| 429 '> description of test 1.\n>\n> BUG=none\n> TEST=none\n' | |
| 430 '> R=test@chromium.org\n> TBR=test@chromium.org\n>\n' | |
| 431 '> Committed: https://chromium.googlesource.com/chromium/src/+/' | |
| 432 'c9cc182781484f9010f062859cda048afefefefe\n' | |
| 433 '> Cr-Commit-Position: refs/heads/master@{#341992}\n\n' | |
| 434 'TBR=test@chromium.org\nNOPRESUBMIT=true\nNOTREECHECKS=true\n' | |
| 435 'NOTRY=true\nBUG=none\n\n' | |
| 436 'Review URL: https://codereview.chromium.org/1278653002\n\n' | |
| 437 'Cr-Commit-Position: refs/heads/master@{#342013}\n') | |
| 438 | |
| 439 reverted_revision = self.git_repo.GetRevertedRevision(message) | |
| 440 self.assertEqual('c9cc182781484f9010f062859cda048afefefefe', | |
| 441 reverted_revision) | |
| 442 | |
| 443 def testGetRevertedRevisionRevertOfRevert(self): | |
| 444 message = ( | |
| 445 'Revert of Revert\n\nReason for revert:\nRevert of revert\n\n' | |
| 446 'Original issue\'s description:\n> test case of revert of revert\n>\n' | |
| 447 '> Reason for revert:\n> reason\n>\n> Original issue\'s description:\n' | |
| 448 '> > base cl\n> >\n> > R=kalman\n> > BUG=424661\n> >\n' | |
| 449 '> > Committed: https://crrev.com/34ea66b8ac1d56dadd670431063857ffdd\n' | |
| 450 '> > Cr-Commit-Position: refs/heads/master@{#326953}\n>\n' | |
| 451 '> TBR=test@chromium.org\n> NOPRESUBMIT=true\n' | |
| 452 '> NOTREECHECKS=true\n> NOTRY=true\n> BUG=424661\n>\n' | |
| 453 '> Committed: https://crrev.com/76a7e3446188256ca240dc31f78de29511a' | |
| 454 '2c322\n' | |
| 455 '> Cr-Commit-Position: refs/heads/master@{#327021}\n\n' | |
| 456 'TBR=test@chromium.org\nNOPRESUBMIT=true\n' | |
| 457 'NOTREECHECKS=true\nNOTRY=true\nBUG=424661\n\n' | |
| 458 'Review URL: https://codereview.chromium.org/1161773008\n\n' | |
| 459 'Cr-Commit-Position: refs/heads/master@{#332062}\n') | |
| 460 | |
| 461 reverted_revision = self.git_repo.GetRevertedRevision(message) | |
| 462 self.assertEqual('76a7e3446188256ca240dc31f78de29511a2c322', | |
| 463 reverted_revision) | |
| 464 | |
| 465 def testGetRevertedRevisionNoRevertedCL(self): | |
| 466 message = ( | |
| 467 'Test for not revert cl\n\n' | |
| 468 'TBR=test@chromium.org\nNOPRESUBMIT=true\n' | |
| 469 'NOTREECHECKS=true\nNOTRY=true\nBUG=424661\n\n' | |
| 470 'Review URL: https://codereview.chromium.org/1161773008\n\n' | |
| 471 'Cr-Commit-Position: refs/heads/master@{#332062}\n') | |
| 472 | |
| 473 reverted_revision = self.git_repo.GetRevertedRevision(message) | |
| 474 self.assertIsNone(reverted_revision) | |
| 475 | |
| 476 def testGetCommitsBetweenRevisions(self): | |
| 477 def _MockSendRequestForJsonResponse(*_): | |
| 478 return { | |
| 479 'log': [ | |
| 480 {'commit': '3'}, | |
| 481 {'commit': '2'}, | |
| 482 {'commit': '1'}] | |
| 483 } | |
| 484 self.mock(git_repository.GitRepository, '_SendRequestForJsonResponse', | |
| 485 _MockSendRequestForJsonResponse) | |
| 486 expected_commits = ['3', '2', '1'] | |
| 487 actual_commits = self.git_repo.GetCommitsBetweenRevisions('0', '3') | |
| 488 self.assertEqual(expected_commits, actual_commits) | |
| 489 | |
| 490 def testGetCommitsBetweenRevisionsWithEmptyData(self): | |
| 491 def _MockSendRequestForJsonResponse(*_): | |
| 492 return None | |
| 493 self.mock(git_repository.GitRepository, '_SendRequestForJsonResponse', | |
| 494 _MockSendRequestForJsonResponse) | |
| 495 expected_commits = [] | |
| 496 actual_commits = self.git_repo.GetCommitsBetweenRevisions('0', '3') | |
| 497 self.assertEqual(expected_commits, actual_commits) | |
| 498 | |
| 499 def testGetCommitsBetweenRevisionsWithIncompleteData(self): | |
| 500 def _MockSendRequestForJsonResponse(*_): | |
| 501 return { | |
| 502 'log': [ | |
| 503 {'commit': '1'}, | |
| 504 {'something_else': '2'} | |
| 505 ] | |
| 506 } | |
| 507 self.mock(git_repository.GitRepository, '_SendRequestForJsonResponse', | |
| 508 _MockSendRequestForJsonResponse) | |
| 509 expected_commits = ['1'] | |
| 510 actual_commits = self.git_repo.GetCommitsBetweenRevisions('0', '3') | |
| 511 self.assertEqual(expected_commits, actual_commits) | |
| 512 | |
| 513 def testGetCommitsBetweenRevisionsWithPaging(self): | |
| 514 def _MockSendRequestForJsonResponse(*args, **_): | |
| 515 url = args[1] | |
| 516 if '0..3' in url: | |
| 517 return { | |
| 518 'log': [ | |
| 519 {'commit': '3'}, | |
| 520 {'commit': '2'} | |
| 521 ], | |
| 522 'next': '1' | |
| 523 } | |
| 524 else: | |
| 525 return { | |
| 526 'log': [ | |
| 527 {'commit': '1'} | |
| 528 ] | |
| 529 } | |
| 530 | |
| 531 self.mock(git_repository.GitRepository, '_SendRequestForJsonResponse', | |
| 532 _MockSendRequestForJsonResponse) | |
| 533 expected_commits = ['3', '2', '1'] | |
| 534 actual_commits = self.git_repo.GetCommitsBetweenRevisions('0', '3', n=2) | |
| 535 self.assertEqual(expected_commits, actual_commits) | |
| 536 | |
| 537 def testGetChangeLogs(self): | |
| 538 def _MockSendRequestForJsonResponse(*_, **kargs): | |
| 539 self.assertTrue(bool(kargs)) | |
| 540 return {'log': [json.loads(COMMIT_LOG[5:])]} | |
| 541 | |
| 542 self.mock(git_repository.GitRepository, '_SendRequestForJsonResponse', | |
| 543 _MockSendRequestForJsonResponse) | |
| 544 | |
| 545 changelogs = self.git_repo.GetChangeLogs('0', '2') | |
| 546 | |
| 547 self.assertEqual(len(changelogs), 1) | |
| 548 self.assertEqual(changelogs[0].ToDict(), EXPECTED_CHANGE_LOG_JSON) | |
| 549 | |
| 550 def testGetChangeLogsNextPage(self): | |
| 551 log1 = json.loads(COMMIT_LOG[5:]) | |
| 552 log1['commit'] = 'first_commit' | |
| 553 log2 = log1.copy() | |
| 554 log2['commit'] = 'next_page_commit' | |
| 555 | |
| 556 def _MockSendRequestForJsonResponse(_, url, **kargs): | |
| 557 self.assertTrue(bool(kargs)) | |
| 558 if 'next' in url: | |
| 559 return {'log': [log2]} | |
| 560 | |
| 561 return {'log': [log1], 'next': 'next_page_commit'} | |
| 562 | |
| 563 self.mock(git_repository.GitRepository, '_SendRequestForJsonResponse', | |
| 564 _MockSendRequestForJsonResponse) | |
| 565 | |
| 566 changelogs = self.git_repo.GetChangeLogs('0', '2') | |
| 567 | |
| 568 self.assertEqual(len(changelogs), 2) | |
| OLD | NEW |