| 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 from datetime import datetime | 5 from datetime import datetime |
| 6 from datetime import timedelta | 6 import pytz |
| 7 import os |
| 8 import subprocess |
| 7 import textwrap | 9 import textwrap |
| 8 | 10 |
| 9 from testing_utils import testing | 11 from testing_utils import testing |
| 10 | 12 |
| 11 from common import local_git_parsers | |
| 12 from common import blame | 13 from common import blame |
| 13 from common import change_log | 14 from common import change_log |
| 15 from common import repo_util |
| 16 from common.local_git_repository import LocalGitRepository |
| 17 |
| 18 CHECKOUT_ROOT_DIR = os.path.join(os.path.expanduser('~'), '.local_checkouts') |
| 14 | 19 |
| 15 | 20 |
| 16 class LocalGitParsersTest(testing.AppengineTestCase): | 21 class LocalGitRepositoryTest(testing.AppengineTestCase): |
| 17 | 22 |
| 18 def testGitBlameParser(self): | 23 def setUp(self): |
| 19 output = textwrap.dedent( | 24 super(LocalGitRepositoryTest, self).setUp() |
| 20 """ | 25 def _MockProcessCall(*_, **kwargs): # pylint: disable=W |
| 21 revision_hash 18 18 3 | 26 return |
| 22 author test@google.com | |
| 23 author-mail <test@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | |
| 24 author-time 1363032816 | |
| 25 author-tz +0300 | |
| 26 committer test@google.com | |
| 27 committer-mail <test@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | |
| 28 committer-time 1363032816 | |
| 29 committer-tz +0300 | |
| 30 summary add (mac) test for ttcindex in SkFontStream | |
| 31 previous fe7533eebe777cc66c7f8fa7a03f00572755c5b4 src/core/SkFont.h | |
| 32 filename src/core/SkFont.h | |
| 33 * blabla line 1 | |
| 34 revision_hash 19 19 | |
| 35 * blabla line 2 | |
| 36 revision_hash 20 20 | |
| 37 * blabla line 3 | |
| 38 """ | |
| 39 ) | |
| 40 | 27 |
| 41 expected_regions = [blame.Region(18, 3, 'revision_hash', 'test@google.com', | 28 self.mock(subprocess, 'call', _MockProcessCall) |
| 42 'test@google.com', | 29 self.mock(subprocess, 'check_call', _MockProcessCall) |
| 43 datetime(2013, 03, 11, 17, 13, 36))] | 30 self.local_repo = LocalGitRepository( |
| 44 expected_blames = [blame.Blame('revision_hash', | 31 'https://chromium.googlesource.com/repo/path') |
| 45 'src/core/SkFont.h', | |
| 46 regions=expected_regions)] | |
| 47 | 32 |
| 48 blames = local_git_parsers.GitBlameParser()(output) | 33 def testCloneOrUpdateRepoIfNeeded(self): |
| 49 for blame_result, expected_blame in zip(blames, expected_blames): | 34 self.local_repo._CloneOrUpdateRepoIfNeeded() |
| 50 self.assertTrue(blame_result.revision, expected_blame.revision) | 35 self.assertTrue(self.local_repo.repo_url in |
| 51 self.assertTrue(blame_result.path, expected_blame.path) | 36 LocalGitRepository.updated_repos) |
| 52 for region, expected_region in zip(blame_result, expected_blame): | |
| 53 self.assertTrue(region.ToDict(), expected_region.ToDict()) | |
| 54 | 37 |
| 55 def testGetFileChangeInfo(self): | 38 LocalGitRepository.updated_repos.remove(self.local_repo.repo_url) |
| 56 self.assertIsNone(local_git_parsers.GetFileChangeInfo('change type', | 39 self.mock(os.path, 'exists', lambda path: True) |
| 57 None, None)) | 40 self.local_repo._CloneOrUpdateRepoIfNeeded() |
| 41 self.assertTrue(self.local_repo.repo_url in |
| 42 LocalGitRepository.updated_repos) |
| 58 | 43 |
| 59 def testGitChangeLogParser(self): | 44 self.local_repo._CloneOrUpdateRepoIfNeeded() |
| 45 self.assertTrue(self.local_repo.repo_url in |
| 46 LocalGitRepository.updated_repos) |
| 47 |
| 48 def testRepoUrlSetter(self): |
| 49 def _MockCloneOrUpdateRepoIfNeeded(*_): |
| 50 if not hasattr(_MockCloneOrUpdateRepoIfNeeded, 'clone_or_update_count'): |
| 51 _MockCloneOrUpdateRepoIfNeeded.clone_or_update_count = 0 |
| 52 _MockCloneOrUpdateRepoIfNeeded.clone_or_update_count += 1 |
| 53 |
| 54 repo = LocalGitRepository() |
| 55 self.mock(repo, '_CloneOrUpdateRepoIfNeeded', |
| 56 _MockCloneOrUpdateRepoIfNeeded) |
| 57 |
| 58 repo.repo_url = 'https://chromium.googlesource.com/repo/path' |
| 59 self.assertEqual(_MockCloneOrUpdateRepoIfNeeded.clone_or_update_count, 1) |
| 60 |
| 61 repo.repo_url = 'https://chromium.googlesource.com/repo/path' |
| 62 self.assertEqual(_MockCloneOrUpdateRepoIfNeeded.clone_or_update_count, 1) |
| 63 |
| 64 repo.repo_url = 'https://chromium.googlesource.com/repo/path2' |
| 65 self.assertEqual(_MockCloneOrUpdateRepoIfNeeded.clone_or_update_count, 2) |
| 66 |
| 67 def testGetLocalGitCommandOutput(self): |
| 68 class _MockProcess(object): |
| 69 def __init__(self, command, *_): |
| 70 self.command = command |
| 71 |
| 72 def communicate(self, *_): |
| 73 return self.command, 'dummy' |
| 74 |
| 75 def _MockPopen(command, **_): |
| 76 return _MockProcess(command) |
| 77 |
| 78 self.mock(subprocess, 'Popen', _MockPopen) |
| 79 output = self.local_repo._GetLocalGitCommandOutput('command') |
| 80 self.assertEqual(output, |
| 81 'cd %s/repo/path; command' % CHECKOUT_ROOT_DIR) |
| 82 |
| 83 def testGetChangeLog(self): |
| 60 output = textwrap.dedent( | 84 output = textwrap.dedent( |
| 61 """ | 85 """ |
| 62 commit revision | 86 commit revision |
| 63 tree tree_revision | 87 tree tree_revision |
| 64 parents parent_revision | 88 parents parent_revision |
| 65 | 89 |
| 66 author Test | 90 author Test |
| 67 author-mail test@google.com | 91 author-mail test@google.com |
| 68 author-time 1468442226 | 92 author-time 1468442226 |
| 69 | 93 |
| 70 committer Test | 94 committer Test |
| 71 committer-mail test@google.com | 95 committer-mail test@google.com |
| 72 committer-time 1468442226 | 96 committer-time 1468442226 |
| 73 | 97 |
| 74 --Message start-- | 98 --Message start-- |
| 75 Revert commit messages... | 99 blabla |
| 76 > Committed: https://c.com/+/c9cc182781484f9010f062859cda048afefefefe | |
| 77 Review-Url: https://codereview.chromium.org/2391763002 | |
| 78 Cr-Commit-Position: refs/heads/master@{#425880} | |
| 79 --Message end-- | 100 --Message end-- |
| 80 | 101 |
| 81 :100644 100644 25f95f c766f1 M src/a/b.py | 102 :100644 100644 25f95f c766f1 M src/a/b.py |
| 82 """ | 103 """ |
| 83 ) | 104 ) |
| 84 | 105 |
| 85 message = ('Revert commit messages...\n' | 106 def _MockGetLocalGitCommandOutput(*_): |
| 86 '> Committed: https://c.com/+/' | 107 return output |
| 87 'c9cc182781484f9010f062859cda048afefefefe\n' | |
| 88 'Review-Url: https://codereview.chromium.org/2391763002\n' | |
| 89 'Cr-Commit-Position: refs/heads/master@{#425880}') | |
| 90 | 108 |
| 91 expected_changelog = change_log.ChangeLog( | 109 expected_changelog = change_log.ChangeLog( |
| 92 'Test', 'test@google.com', datetime(2016, 7, 13, 20, 37, 6), | 110 'Test', 'test@google.com', datetime(2016, 7, 13, 20, 37, 6), |
| 93 'Test', 'test@google.com', datetime(2016, 7, 13, 20, 37, 6), | 111 'Test', 'test@google.com', datetime(2016, 7, 13, 20, 37, 6), |
| 94 'revision', 425880, message, [change_log.FileChangeInfo( | 112 'revision', message='blabla', touched_files=[change_log.FileChangeInfo( |
| 95 'modify', 'src/a/b.py', 'src/a/b.py')], | 113 'modify', 'src/a/b.py', 'src/a/b.py')], |
| 96 code_review_url='https://codereview.chromium.org/2391763002', | 114 commit_url='https://chromium.googlesource.com/repo/path/+/revision') |
| 97 reverted_revision='c9cc182781484f9010f062859cda048afefefefe') | |
| 98 | 115 |
| 99 changelog = local_git_parsers.GitChangeLogParser()(output) | 116 self.mock(self.local_repo, '_GetLocalGitCommandOutput', |
| 100 self.assertTrue(expected_changelog.ToDict(), changelog.ToDict()) | 117 _MockGetLocalGitCommandOutput) |
| 101 | 118 |
| 102 def testGitChangeLogsParser(self): | 119 self.assertEqual(self.local_repo.GetChangeLog('revision').ToDict(), |
| 120 expected_changelog.ToDict()) |
| 121 |
| 122 def testGetChangeLogs(self): |
| 103 output = textwrap.dedent( | 123 output = textwrap.dedent( |
| 104 """ | 124 """ |
| 105 **Changelog start** | 125 **Changelog start** |
| 106 commit 9af040a364c15bdc2adeea794e173a2c529a | 126 commit rev1 |
| 107 tree 27b0421273ed4aea25e497c6d26d9c7db6481852 | 127 tree 27b0421273ed4aea25e497c6d26d9c7db6481852 |
| 108 parents c39b0cc8a516de1fa57d032dc0135a4eadfe2c9e | 128 parents c39b0cc8a516de1fa57d032dc0135a4eadfe2c9e |
| 109 | 129 |
| 110 author author1 | 130 author author1 |
| 111 author-mail author1@chromium.org | 131 author-mail author1@chromium.org |
| 112 author-time 1464864938 | 132 author-time 1464864938 |
| 113 | 133 |
| 114 committer Commit bot | 134 committer Commit bot |
| 115 committer-mail commit-bot@chromium.org | 135 committer-mail commit-bot@chromium.org |
| 116 committer-time 1464865033 | 136 committer-time 1464865033 |
| 117 | 137 |
| 118 --Message start-- | 138 --Message start-- |
| 119 Message 1 | 139 Message 1 |
| 120 --Message end-- | 140 --Message end-- |
| 121 | 141 |
| 122 :100644 100644 28e117 f12d3 D a/b.py | 142 :100644 100644 28e117 f12d3 D a/b.py |
| 123 | 143 |
| 124 | 144 |
| 125 **Changelog start** | 145 **Changelog start** |
| 126 commit c39b0cc8a516de1fa57d032dc0135a4eadfe | 146 commit rev2 |
| 127 tree d22d3786e135b83183cfeba5f3d8913959f56299 | 147 tree d22d3786e135b83183cfeba5f3d8913959f56299 |
| 128 parents ac7ee4ce7b8d39b22a710c58d110e0039c11cf9a | 148 parents ac7ee4ce7b8d39b22a710c58d110e0039c11cf9a |
| 129 | 149 |
| 130 author author2 | 150 author author2 |
| 131 author-mail author2@chromium.org | 151 author-mail author2@chromium.org |
| 132 author-time 1464864783 | 152 author-time 1464864783 |
| 133 | 153 |
| 134 committer Commit bot | 154 committer Commit bot |
| 135 committer-mail commit-bot@chromium.org | 155 committer-mail commit-bot@chromium.org |
| 136 committer-time 1464864854 | 156 committer-time 1464864854 |
| 137 | 157 |
| 138 --Message start-- | 158 --Message start-- |
| 139 Message 2 | 159 Message 2 |
| 140 --Message end-- | 160 --Message end-- |
| 141 | 161 |
| 142 :100644 100644 7280f df186 A b/c.py | 162 :100644 100644 7280f df186 A b/c.py |
| 143 | |
| 144 **Changelog start** | |
| 145 commit c39b0cc8a516de1fa57d032dc0135a4eadfed | |
| 146 tree d22d3786e135b83183cfeba5f3d8913959f56299 | |
| 147 parents ac7ee4ce7b8d39b22a710c58d110e0039c11cf9a | |
| 148 | |
| 149 author author3 | |
| 150 author-mail author3@chromium.org | |
| 151 author-time 1464864783 | |
| 152 | |
| 153 committer Commit bot | |
| 154 committer-mail commit-bot@chromium.org | |
| 155 committer-time 1464864854 | |
| 156 | |
| 157 --Message start-- | |
| 158 Message 3 | |
| 159 --Message end-- | |
| 160 | |
| 161 :100644 100644 3f2e 20a5 R078 b/c.py b/cc.py | |
| 162 """ | 163 """ |
| 163 ) | 164 ) |
| 164 | 165 |
| 165 expected_changelogs = [ | 166 expected_changelogs = [ |
| 166 change_log.ChangeLog(author_name='author1', | 167 change_log.ChangeLog(author_name='author1', |
| 167 author_email='author1@chromium.org', | 168 author_email='author1@chromium.org', |
| 168 author_time=datetime(2016, 6, 2, 10, 55, 38), | 169 author_time=datetime(2016, 6, 2, 10, 55, 38), |
| 169 committer_name='Commit bot', | 170 committer_name='Commit bot', |
| 170 committer_email='commit-bot@chromium.org', | 171 committer_email='commit-bot@chromium.org', |
| 171 committer_time=datetime(2016, 6, 2, 10, 57, 13), | 172 committer_time=datetime(2016, 6, 2, 10, 57, 13), |
| 172 message='Message 1', | 173 message='Message 1', |
| 173 revision='9af040a364c15bdc2adeea794e173a2c529a', | 174 revision='rev1', |
| 174 touched_files=[change_log.FileChangeInfo( | 175 touched_files=[change_log.FileChangeInfo( |
| 175 'delete', 'a/b.py', None)]), | 176 'delete', 'a/b.py', None)]), |
| 176 change_log.ChangeLog(author_name='author2', | 177 change_log.ChangeLog(author_name='author2', |
| 177 author_email='author2@chromium.org', | 178 author_email='author2@chromium.org', |
| 178 author_time=datetime(2016, 6, 2, 10, 53, 3), | 179 author_time=datetime(2016, 6, 2, 10, 53, 3), |
| 179 committer_name='Commit bot', | 180 committer_name='Commit bot', |
| 180 committer_email='commit-bot@chromium.org', | 181 committer_email='commit-bot@chromium.org', |
| 181 committer_time=datetime(2016, 6, 2, 10, 54, 14), | 182 committer_time=datetime(2016, 6, 2, 10, 54, 14), |
| 182 message='Message 2', | 183 message='Message 2', |
| 183 revision='c39b0cc8a516de1fa57d032dc0135a4eadfe', | 184 revision='rev2', |
| 184 touched_files=[change_log.FileChangeInfo( | 185 touched_files=[change_log.FileChangeInfo( |
| 185 'add', None, 'b/c.py')]), | 186 'add', None, 'b/c.py')]), |
| 186 change_log.ChangeLog(author_name='author3', | |
| 187 author_email='author3@chromium.org', | |
| 188 author_time=datetime(2016, 6, 2, 10, 53, 3), | |
| 189 committer_name='Commit bot', | |
| 190 committer_email='commit-bot@chromium.org', | |
| 191 committer_time=datetime(2016, 6, 2, 10, 54, 14), | |
| 192 message='Message 3', | |
| 193 revision='c39b0cc8a516de1fa57d032dc0135a4eadfed', | |
| 194 touched_files=[change_log.FileChangeInfo( | |
| 195 'rename', 'b/c.py', 'b/cc.py')]), | |
| 196 | |
| 197 ] | 187 ] |
| 198 | 188 |
| 199 changelogs = local_git_parsers.GitChangeLogsParser()(output) | 189 |
| 190 def _MockGetLocalGitCommandOutput(*_): |
| 191 return output |
| 192 |
| 193 self.mock(self.local_repo, '_GetLocalGitCommandOutput', |
| 194 _MockGetLocalGitCommandOutput) |
| 195 |
| 196 changelogs = self.local_repo.GetChangeLogs('rev0', 'rev2') |
| 200 for changelog, expected_changelog in zip(changelogs, expected_changelogs): | 197 for changelog, expected_changelog in zip(changelogs, expected_changelogs): |
| 201 self.assertEqual(changelog.ToDict(), expected_changelog.ToDict()) | 198 self.assertEqual(changelog.ToDict(), expected_changelog.ToDict()) |
| 202 | 199 |
| 203 def testGitChangeLogsParserWithEmptyChangelog(self): | 200 def testGetChangeDiff(self): |
| 204 output = '**Changelog start**\nblablabla' | 201 def _MockGetLocalGitCommandOutput(*_): |
| 205 self.assertEqual(local_git_parsers.GitChangeLogsParser()(output), []) | 202 return 'diff' |
| 206 | 203 |
| 207 def testGitDiffParser(self): | 204 self.mock(self.local_repo, '_GetLocalGitCommandOutput', |
| 208 output = 'output' | 205 _MockGetLocalGitCommandOutput) |
| 209 self.assertEqual(output, local_git_parsers.GitDiffParser()(output)) | 206 self.assertEqual(self.local_repo.GetChangeDiff('rev'), 'diff') |
| 207 self.assertEqual(self.local_repo.GetChangeDiff('rev', 'file_path'), 'diff') |
| 210 | 208 |
| 211 def testGitSourceParser(self): | 209 def testGetBlame(self): |
| 212 output = 'output' | 210 output = textwrap.dedent( |
| 213 self.assertEqual(output, local_git_parsers.GitSourceParser()(output)) | 211 """ |
| 212 revision_hash 18 18 3 |
| 213 author test@google.com |
| 214 author-mail <test@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> |
| 215 author-time 1363032816 |
| 216 author-tz +0300 |
| 217 committer test@google.com |
| 218 committer-mail <test@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> |
| 219 committer-time 1363032816 |
| 220 committer-tz +0300 |
| 221 summary add (mac) test for ttcindex in SkFontStream |
| 222 previous fe7533eebe777cc66c7f8fa7a03f00572755c5b4 src/core/SkFont.h |
| 223 filename src/core/SkFont.h |
| 224 * blabla line 1 |
| 225 revision_hash 19 19 |
| 226 * blabla line 2 |
| 227 revision_hash 20 20 |
| 228 * blabla line 3 |
| 229 """ |
| 230 ) |
| 231 |
| 232 expected_regions = [blame.Region(18, 3, 'revision_hash', 'test@google.com', |
| 233 'test@google.com', |
| 234 datetime(2013, 03, 11, 17, 13, 36, |
| 235 tzinfo=pytz.utc))] |
| 236 expected_blames = [blame.Blame('revision_hash', |
| 237 'src/core/SkFont.h', |
| 238 regions=expected_regions)] |
| 239 |
| 240 def _MockGetLocalGitCommandOutput(*_): |
| 241 return output |
| 242 |
| 243 self.mock(self.local_repo, '_GetLocalGitCommandOutput', |
| 244 _MockGetLocalGitCommandOutput) |
| 245 |
| 246 blames = self.local_repo.GetBlame('path', 'revision') |
| 247 for blame_result, expected_blame in zip(blames, expected_blames): |
| 248 self.assertTrue(blame_result.revision, expected_blame.revision) |
| 249 self.assertTrue(blame_result.path, expected_blame.path) |
| 250 for region, expected_region in zip(blame_result, expected_blame): |
| 251 self.assertTrue(region.ToDict(), expected_region.ToDict()) |
| 252 |
| 253 def testGetSource(self): |
| 254 def _MockGetLocalGitCommandOutput(*_): |
| 255 return 'source' |
| 256 |
| 257 self.mock(self.local_repo, '_GetLocalGitCommandOutput', |
| 258 _MockGetLocalGitCommandOutput) |
| 259 self.assertIsNone(self.local_repo.GetSource('file_path', 'rev')) |
| 260 self.mock(os.path, 'isfile', lambda path: True) |
| 261 self.assertEqual(self.local_repo.GetSource('file_path', 'rev'), 'source') |
| OLD | NEW |