Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import base64 | 5 import base64 |
| 6 from datetime import datetime | 6 from datetime import datetime |
| 7 import json | 7 import json |
| 8 import re | 8 import re |
| 9 | 9 |
| 10 from testing_utils import testing | 10 from testing_utils import testing |
| 11 | 11 |
| 12 from common import git_repository | 12 from lib.gitiles import gitiles_repository |
| 13 from lib.gitiles.change_log import ChangeLog | |
|
stgao
2016/10/28 18:21:00
order of import
wrengr
2016/10/28 19:24:49
Done.
| |
| 14 # TODO(wrengr): we shouldn't have anything in the gitiles directory | |
| 15 # depend on stuff in the common directory. | |
| 13 from common import retry_http_client | 16 from common import retry_http_client |
| 14 from common.change_log import ChangeLog | |
| 15 | 17 |
| 16 | 18 |
| 17 COMMIT_MESSAGE = ('Add popover for snapshot canvas log.\n\n' | 19 COMMIT_MESSAGE = ('Add popover for snapshot canvas log.\n\n' |
| 18 'Review URL: https://codereview.chromium.org/320423004\n\n' | 20 'Review URL: https://codereview.chromium.org/320423004\n\n' |
| 19 'Review URL: https://codereview.chromium.org/328113005\n\n' | 21 'Review URL: https://codereview.chromium.org/328113005\n\n' |
| 20 'Cr-Commit-Position: refs/heads/master@{#175976}') | 22 'Cr-Commit-Position: refs/heads/master@{#175976}') |
| 21 | 23 |
| 22 COMMIT_LOG = """)]}' | 24 COMMIT_LOG = """)]}' |
| 23 { | 25 { |
| 24 "commit": "bcfd5a12eea05588aee98b7cf7e032d8cb5b58bb", | 26 "commit": "bcfd5a12eea05588aee98b7cf7e032d8cb5b58bb", |
| (...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 280 def _Put(self, *_): # pragma: no cover | 282 def _Put(self, *_): # pragma: no cover |
| 281 pass | 283 pass |
| 282 | 284 |
| 283 | 285 |
| 284 class GitRepositoryTest(testing.AppengineTestCase): | 286 class GitRepositoryTest(testing.AppengineTestCase): |
| 285 | 287 |
| 286 def setUp(self): | 288 def setUp(self): |
| 287 super(GitRepositoryTest, self).setUp() | 289 super(GitRepositoryTest, self).setUp() |
| 288 self.http_client_for_git = HttpClientForGit() | 290 self.http_client_for_git = HttpClientForGit() |
| 289 self.repo_url = 'https://repo.test' | 291 self.repo_url = 'https://repo.test' |
| 290 self.git_repo = git_repository.GitRepository(self.repo_url, | 292 self.git_repo = gitiles_repository.GitilesRepository(self.repo_url, |
| 291 self.http_client_for_git) | 293 self.http_client_for_git) |
| 292 | 294 |
| 293 def testGitRepositoryPropertySetters(self): | 295 def testGitRepositoryPropertySetters(self): |
| 294 http_client_for_git = HttpClientForGit() | 296 http_client_for_git = HttpClientForGit() |
| 295 git_repo = git_repository.GitRepository(http_client=http_client_for_git) | 297 git_repo = gitiles_repository.GitilesRepository( |
| 298 http_client=http_client_for_git) | |
| 296 git_repo.repo_url = 'https://repo' | 299 git_repo.repo_url = 'https://repo' |
| 297 self.assertEqual(git_repo.repo_url, 'https://repo') | 300 self.assertEqual(git_repo.repo_url, 'https://repo') |
| 298 | 301 |
| 299 self.assertEqual(git_repo. http_client, http_client_for_git) | 302 self.assertEqual(git_repo. http_client, http_client_for_git) |
| 300 | 303 |
| 301 def testExtractCommitPositionAndCodeReviewUrl(self): | 304 def testExtractCommitPositionAndCodeReviewUrl(self): |
| 302 testcases = [ | 305 testcases = [ |
| 303 { | 306 { |
| 304 'message': | 307 'message': |
| 305 'balabala...\n' | 308 'balabala...\n' |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 345 ] | 348 ] |
| 346 | 349 |
| 347 for testcase in testcases: | 350 for testcase in testcases: |
| 348 (commit_position, | 351 (commit_position, |
| 349 code_review_url) = self.git_repo.ExtractCommitPositionAndCodeReviewUrl( | 352 code_review_url) = self.git_repo.ExtractCommitPositionAndCodeReviewUrl( |
| 350 testcase['message']) | 353 testcase['message']) |
| 351 self.assertEqual(commit_position, testcase['commit_position']) | 354 self.assertEqual(commit_position, testcase['commit_position']) |
| 352 self.assertEqual(code_review_url, testcase['code_review_url']) | 355 self.assertEqual(code_review_url, testcase['code_review_url']) |
| 353 | 356 |
| 354 def testEndingSlashInRepoUrl(self): | 357 def testEndingSlashInRepoUrl(self): |
| 355 git_repo1 = git_repository.GitRepository(self.repo_url, | 358 git_repo1 = gitiles_repository.GitilesRepository( |
| 356 self.http_client_for_git) | 359 self.repo_url, self.http_client_for_git) |
| 357 self.assertEqual(self.repo_url, git_repo1.repo_url) | 360 self.assertEqual(self.repo_url, git_repo1.repo_url) |
| 358 | 361 |
| 359 git_repo2 = git_repository.GitRepository('%s/' % self.repo_url, | 362 git_repo2 = gitiles_repository.GitilesRepository( |
| 360 self.http_client_for_git) | 363 '%s/' % self.repo_url, self.http_client_for_git) |
| 361 self.assertEqual(self.repo_url, git_repo2.repo_url) | 364 self.assertEqual(self.repo_url, git_repo2.repo_url) |
| 362 | 365 |
| 363 def testMalformattedJsonReponse(self): | 366 def testMalformattedJsonReponse(self): |
| 364 self.http_client_for_git.SetResponseForUrl( | 367 self.http_client_for_git.SetResponseForUrl( |
| 365 '%s/+/%s?format=json' % (self.repo_url, 'aaa'), 'abcde{"a": 1}') | 368 '%s/+/%s?format=json' % (self.repo_url, 'aaa'), 'abcde{"a": 1}') |
| 366 self.assertRaisesRegexp( | 369 self.assertRaisesRegexp( |
| 367 Exception, re.escape('Response does not begin with )]}\'\n'), | 370 Exception, re.escape('Response does not begin with )]}\'\n'), |
| 368 self.git_repo.GetChangeLog, 'aaa') | 371 self.git_repo.GetChangeLog, 'aaa') |
| 369 | 372 |
| 370 def testGetChangeLog(self): | 373 def testGetChangeLog(self): |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 482 self.assertIsNone(reverted_revision) | 485 self.assertIsNone(reverted_revision) |
| 483 | 486 |
| 484 def testGetCommitsBetweenRevisions(self): | 487 def testGetCommitsBetweenRevisions(self): |
| 485 def _MockSendRequestForJsonResponse(*_): | 488 def _MockSendRequestForJsonResponse(*_): |
| 486 return { | 489 return { |
| 487 'log': [ | 490 'log': [ |
| 488 {'commit': '3'}, | 491 {'commit': '3'}, |
| 489 {'commit': '2'}, | 492 {'commit': '2'}, |
| 490 {'commit': '1'}] | 493 {'commit': '1'}] |
| 491 } | 494 } |
| 492 self.mock(git_repository.GitRepository, '_SendRequestForJsonResponse', | 495 self.mock(gitiles_repository.GitilesRepository, |
| 493 _MockSendRequestForJsonResponse) | 496 '_SendRequestForJsonResponse', _MockSendRequestForJsonResponse) |
| 494 expected_commits = ['3', '2', '1'] | 497 expected_commits = ['3', '2', '1'] |
| 495 actual_commits = self.git_repo.GetCommitsBetweenRevisions('0', '3') | 498 actual_commits = self.git_repo.GetCommitsBetweenRevisions('0', '3') |
| 496 self.assertEqual(expected_commits, actual_commits) | 499 self.assertEqual(expected_commits, actual_commits) |
| 497 | 500 |
| 498 def testGetCommitsBetweenRevisionsWithEmptyData(self): | 501 def testGetCommitsBetweenRevisionsWithEmptyData(self): |
| 499 def _MockSendRequestForJsonResponse(*_): | 502 def _MockSendRequestForJsonResponse(*_): |
| 500 return None | 503 return None |
| 501 self.mock(git_repository.GitRepository, '_SendRequestForJsonResponse', | 504 self.mock(gitiles_repository.GitilesRepository, |
| 502 _MockSendRequestForJsonResponse) | 505 '_SendRequestForJsonResponse', _MockSendRequestForJsonResponse) |
| 503 expected_commits = [] | 506 expected_commits = [] |
| 504 actual_commits = self.git_repo.GetCommitsBetweenRevisions('0', '3') | 507 actual_commits = self.git_repo.GetCommitsBetweenRevisions('0', '3') |
| 505 self.assertEqual(expected_commits, actual_commits) | 508 self.assertEqual(expected_commits, actual_commits) |
| 506 | 509 |
| 507 def testGetCommitsBetweenRevisionsWithIncompleteData(self): | 510 def testGetCommitsBetweenRevisionsWithIncompleteData(self): |
| 508 def _MockSendRequestForJsonResponse(*_): | 511 def _MockSendRequestForJsonResponse(*_): |
| 509 return { | 512 return { |
| 510 'log': [ | 513 'log': [ |
| 511 {'commit': '1'}, | 514 {'commit': '1'}, |
| 512 {'something_else': '2'} | 515 {'something_else': '2'} |
| 513 ] | 516 ] |
| 514 } | 517 } |
| 515 self.mock(git_repository.GitRepository, '_SendRequestForJsonResponse', | 518 self.mock(gitiles_repository.GitilesRepository, |
| 516 _MockSendRequestForJsonResponse) | 519 '_SendRequestForJsonResponse', _MockSendRequestForJsonResponse) |
| 517 expected_commits = ['1'] | 520 expected_commits = ['1'] |
| 518 actual_commits = self.git_repo.GetCommitsBetweenRevisions('0', '3') | 521 actual_commits = self.git_repo.GetCommitsBetweenRevisions('0', '3') |
| 519 self.assertEqual(expected_commits, actual_commits) | 522 self.assertEqual(expected_commits, actual_commits) |
| 520 | 523 |
| 521 def testGetCommitsBetweenRevisionsWithPaging(self): | 524 def testGetCommitsBetweenRevisionsWithPaging(self): |
| 522 def _MockSendRequestForJsonResponse(*args, **_): | 525 def _MockSendRequestForJsonResponse(*args, **_): |
| 523 url = args[1] | 526 url = args[1] |
| 524 if '0..3' in url: | 527 if '0..3' in url: |
| 525 return { | 528 return { |
| 526 'log': [ | 529 'log': [ |
| 527 {'commit': '3'}, | 530 {'commit': '3'}, |
| 528 {'commit': '2'} | 531 {'commit': '2'} |
| 529 ], | 532 ], |
| 530 'next': '1' | 533 'next': '1' |
| 531 } | 534 } |
| 532 else: | 535 else: |
| 533 return { | 536 return { |
| 534 'log': [ | 537 'log': [ |
| 535 {'commit': '1'} | 538 {'commit': '1'} |
| 536 ] | 539 ] |
| 537 } | 540 } |
| 538 | 541 |
| 539 self.mock(git_repository.GitRepository, '_SendRequestForJsonResponse', | 542 self.mock(gitiles_repository.GitilesRepository, |
| 540 _MockSendRequestForJsonResponse) | 543 '_SendRequestForJsonResponse', _MockSendRequestForJsonResponse) |
| 541 expected_commits = ['3', '2', '1'] | 544 expected_commits = ['3', '2', '1'] |
| 542 actual_commits = self.git_repo.GetCommitsBetweenRevisions('0', '3', n=2) | 545 actual_commits = self.git_repo.GetCommitsBetweenRevisions('0', '3', n=2) |
| 543 self.assertEqual(expected_commits, actual_commits) | 546 self.assertEqual(expected_commits, actual_commits) |
| 544 | 547 |
| 545 def testGetChangeLogs(self): | 548 def testGetChangeLogs(self): |
| 546 def _MockSendRequestForJsonResponse(*_, **kargs): | 549 def _MockSendRequestForJsonResponse(*_, **kargs): |
| 547 self.assertTrue(bool(kargs)) | 550 self.assertTrue(bool(kargs)) |
| 548 return {'log': [json.loads(COMMIT_LOG[5:])]} | 551 return {'log': [json.loads(COMMIT_LOG[5:])]} |
| 549 | 552 |
| 550 self.mock(git_repository.GitRepository, '_SendRequestForJsonResponse', | 553 self.mock(gitiles_repository.GitilesRepository, |
| 551 _MockSendRequestForJsonResponse) | 554 '_SendRequestForJsonResponse', _MockSendRequestForJsonResponse) |
| 552 | 555 |
| 553 changelogs = self.git_repo.GetChangeLogs('0', '2') | 556 changelogs = self.git_repo.GetChangeLogs('0', '2') |
| 554 | 557 |
| 555 self.assertEqual(len(changelogs), 1) | 558 self.assertEqual(len(changelogs), 1) |
| 556 self.assertEqual(changelogs[0].ToDict(), EXPECTED_CHANGE_LOG_JSON) | 559 self.assertEqual(changelogs[0].ToDict(), EXPECTED_CHANGE_LOG_JSON) |
| 557 | 560 |
| 558 def testGetChangeLogsNextPage(self): | 561 def testGetChangeLogsNextPage(self): |
| 559 log1 = json.loads(COMMIT_LOG[5:]) | 562 log1 = json.loads(COMMIT_LOG[5:]) |
| 560 log1['commit'] = 'first_commit' | 563 log1['commit'] = 'first_commit' |
| 561 log2 = log1.copy() | 564 log2 = log1.copy() |
| 562 log2['commit'] = 'next_page_commit' | 565 log2['commit'] = 'next_page_commit' |
| 563 | 566 |
| 564 def _MockSendRequestForJsonResponse(_, url, **kargs): | 567 def _MockSendRequestForJsonResponse(_, url, **kargs): |
| 565 self.assertTrue(bool(kargs)) | 568 self.assertTrue(bool(kargs)) |
| 566 if 'next' in url: | 569 if 'next' in url: |
| 567 return {'log': [log2]} | 570 return {'log': [log2]} |
| 568 | 571 |
| 569 return {'log': [log1], 'next': 'next_page_commit'} | 572 return {'log': [log1], 'next': 'next_page_commit'} |
| 570 | 573 |
| 571 self.mock(git_repository.GitRepository, '_SendRequestForJsonResponse', | 574 self.mock(gitiles_repository.GitilesRepository, |
| 572 _MockSendRequestForJsonResponse) | 575 '_SendRequestForJsonResponse', _MockSendRequestForJsonResponse) |
| 573 | 576 |
| 574 changelogs = self.git_repo.GetChangeLogs('0', '2') | 577 changelogs = self.git_repo.GetChangeLogs('0', '2') |
| 575 | 578 |
| 576 self.assertEqual(len(changelogs), 2) | 579 self.assertEqual(len(changelogs), 2) |
| 577 | 580 |
| 578 def testGetWrappedGitRepositoryClass(self): | 581 def testGetWrappedGitRepositoryClass(self): |
| 579 repo = git_repository.GitRepository('http://repo_url', HttpClientForGit()) | 582 repo = gitiles_repository.GitilesRepository( |
| 583 'http://repo_url', HttpClientForGit()) | |
| 580 | 584 |
| 581 self.assertEqual(repo.repo_url, 'http://repo_url') | 585 self.assertEqual(repo.repo_url, 'http://repo_url') |
| 582 self.assertTrue(isinstance(repo.http_client, HttpClientForGit)) | 586 self.assertTrue(isinstance(repo.http_client, HttpClientForGit)) |
| OLD | NEW |