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 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 lib.gitiles import local_git_parsers | |
| 12 from lib.gitiles import blame | 13 from lib.gitiles import blame |
| 13 from lib.gitiles import change_log | 14 from lib.gitiles import change_log |
| 15 from lib.gitiles import repo_util | |
| 16 from lib.gitiles.local_git_repository import LocalGitRepository | |
| 14 | 17 |
| 15 | 18 |
| 16 class LocalGitParsersTest(testing.AppengineTestCase): | 19 class LocalGitRepositoryTest(testing.AppengineTestCase): |
| 17 | 20 |
| 18 def testGitBlameParser(self): | 21 def setUp(self): |
| 19 output = textwrap.dedent( | 22 super(LocalGitRepositoryTest, self).setUp() |
| 20 """ | 23 def _MockProcessCall(*_, **kwargs): # pylint: disable=W |
| 21 revision_hash 18 18 3 | 24 return |
| 22 author test@google.com | |
| 23 author-mail <test@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | |
| 24 author-time 1363032816 | |
| 25 author-tz +0000 | |
| 26 committer test@google.com | |
| 27 committer-mail <test@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | |
| 28 committer-time 1363032816 | |
| 29 committer-tz +0000 | |
| 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 | 25 |
| 39 revision_hash 29 29 2 | 26 self.mock(subprocess, 'call', _MockProcessCall) |
| 40 * blabla line 4 | 27 self.mock(subprocess, 'check_call', _MockProcessCall) |
| 41 """ | 28 self.local_repo = LocalGitRepository('https://repo/path') |
| 42 ) | |
| 43 | 29 |
| 44 expected_regions = [blame.Region(18, 3, 'revision_hash', 'test@google.com', | 30 def testCloneOrUpdateRepoIfNeeded(self): |
|
wrengr
2016/11/01 20:26:53
This should be broken out as three different tests
Sharu Jiang
2016/11/05 01:18:16
Done.
| |
| 45 'test@google.com', | 31 self.local_repo._CloneOrUpdateRepoIfNeeded() |
| 46 datetime(2013, 03, 11, 17, 13, 36)), | 32 self.assertTrue(self.local_repo.repo_url in |
| 47 blame.Region(29, 2, 'revision_hash', 'test@google.com', | 33 LocalGitRepository._updated_repos) |
| 48 'test@google.com', | |
| 49 datetime(2013, 03, 11, 17, 13, 36))] | |
| 50 expected_blame = blame.Blame('src/core/SkFont.h', 'rev') | |
| 51 for expected_region in expected_regions: | |
| 52 expected_blame.AddRegion(expected_region) | |
| 53 | 34 |
| 54 blame_result = local_git_parsers.GitBlameParser()(output, | 35 LocalGitRepository._updated_repos.remove(self.local_repo.repo_url) |
| 55 'src/core/SkFont.h', | 36 self.mock(os.path, 'exists', lambda path: True) |
| 56 'rev') | 37 self.local_repo._CloneOrUpdateRepoIfNeeded() |
| 57 self.assertTrue(blame_result.revision, expected_blame.revision) | 38 self.assertTrue(self.local_repo.repo_url in |
| 58 self.assertTrue(blame_result.path, expected_blame.path) | 39 LocalGitRepository._updated_repos) |
| 59 for region, expected_region in zip(blame_result, expected_blame): | |
| 60 self.assertTrue(region.ToDict(), expected_region.ToDict()) | |
| 61 | 40 |
| 62 def testGitBlameParserEmptyOutput(self): | 41 self.local_repo._CloneOrUpdateRepoIfNeeded() |
| 63 output = '' | 42 self.assertTrue(self.local_repo.repo_url in |
| 64 blame_result = local_git_parsers.GitBlameParser()(output, | 43 LocalGitRepository._updated_repos) |
| 65 'src/core/SkFont.h', | |
| 66 'rev') | |
| 67 self.assertEqual(len(blame_result), 0) | |
| 68 | 44 |
| 69 def testGetFileChangeInfo(self): | 45 def testRepoUrlSetter(self): |
| 70 self.assertIsNone(local_git_parsers.GetFileChangeInfo('change type', | 46 def _MockCloneOrUpdateRepoIfNeeded(*_): |
| 71 None, None)) | 47 if not hasattr(_MockCloneOrUpdateRepoIfNeeded, 'clone_or_update_count'): |
| 48 _MockCloneOrUpdateRepoIfNeeded.clone_or_update_count = 0 | |
| 49 _MockCloneOrUpdateRepoIfNeeded.clone_or_update_count += 1 | |
| 72 | 50 |
| 73 def testGitChangeLogParser(self): | 51 repo = LocalGitRepository() |
| 52 self.mock(repo, '_CloneOrUpdateRepoIfNeeded', | |
| 53 _MockCloneOrUpdateRepoIfNeeded) | |
| 54 | |
| 55 repo.repo_url = 'https://repo/path' | |
| 56 self.assertEqual(_MockCloneOrUpdateRepoIfNeeded.clone_or_update_count, 1) | |
| 57 | |
| 58 repo.repo_url = 'https://repo/path' | |
| 59 self.assertEqual(_MockCloneOrUpdateRepoIfNeeded.clone_or_update_count, 1) | |
| 60 | |
| 61 repo.repo_url = 'https://repo/path2' | |
| 62 self.assertEqual(_MockCloneOrUpdateRepoIfNeeded.clone_or_update_count, 2) | |
| 63 | |
| 64 repo.repo_url = None | |
| 65 self.assertIsNone(repo.repo_url) | |
| 66 | |
| 67 def testGetChangeLog(self): | |
| 74 output = textwrap.dedent( | 68 output = textwrap.dedent( |
| 75 """ | 69 """ |
| 76 commit revision | 70 commit revision |
| 77 tree tree_revision | 71 tree tree_revision |
| 78 parents parent_revision | 72 parents parent_revision |
| 79 | 73 |
| 80 author Test | 74 author Test |
| 81 author-mail test@google.com | 75 author-mail test@google.com |
| 82 author-time 2016-07-13 20:37:06 | 76 author-time 2016-07-13 20:37:06 |
| 83 | 77 |
| 84 committer Test | 78 committer Test |
| 85 committer-mail test@google.com | 79 committer-mail test@google.com |
| 86 committer-time 2016-07-13 20:37:06 | 80 committer-time 2016-07-13 20:37:06 |
| 87 | 81 |
| 88 --Message start-- | 82 --Message start-- |
| 89 Revert commit messages... | 83 blabla |
| 90 > Committed: https://c.com/+/c9cc182781484f9010f062859cda048afefefefe | |
| 91 Review-Url: https://codereview.chromium.org/2391763002 | |
| 92 Cr-Commit-Position: refs/heads/master@{#425880} | |
| 93 --Message end-- | 84 --Message end-- |
| 94 | 85 |
| 95 :100644 100644 25f95f c766f1 M src/a/b.py | 86 :100644 100644 25f95f c766f1 M src/a/b.py |
| 96 """ | 87 """ |
| 97 ) | 88 ) |
| 98 | 89 |
| 99 message = ('Revert commit messages...\n' | |
| 100 '> Committed: https://c.com/+/' | |
| 101 'c9cc182781484f9010f062859cda048afefefefe\n' | |
| 102 'Review-Url: https://codereview.chromium.org/2391763002\n' | |
| 103 'Cr-Commit-Position: refs/heads/master@{#425880}') | |
| 104 | |
| 105 expected_changelog = change_log.ChangeLog( | 90 expected_changelog = change_log.ChangeLog( |
| 106 'Test', 'test@google.com', datetime(2016, 7, 13, 20, 37, 6), | 91 'Test', 'test@google.com', datetime(2016, 7, 13, 20, 37, 6), |
| 107 'Test', 'test@google.com', datetime(2016, 7, 13, 20, 37, 6), | 92 'Test', 'test@google.com', datetime(2016, 7, 13, 20, 37, 6), |
| 108 'revision', 425880, message, [change_log.FileChangeInfo( | 93 'revision', None, 'blabla', [change_log.FileChangeInfo( |
| 109 'modify', 'src/a/b.py', 'src/a/b.py')], | 94 'modify', 'src/a/b.py', 'src/a/b.py')], |
| 110 'https://repo/+/revision', | 95 'https://repo/path/+/revision', None, None) |
| 111 'https://codereview.chromium.org/2391763002', | 96 self.mock(repo_util, 'GetCommandOutput', lambda *_: output) |
| 112 'c9cc182781484f9010f062859cda048afefefefe') | 97 self.assertEqual(self.local_repo.GetChangeLog('revision').ToDict(), |
| 98 expected_changelog.ToDict()) | |
| 113 | 99 |
| 114 changelog = local_git_parsers.GitChangeLogParser()(output, 'https://repo') | 100 def testGetChangeLogNoneCommandOutput(self): |
| 115 self.assertTrue(expected_changelog.ToDict(), changelog.ToDict()) | 101 self.mock(repo_util, 'GetCommandOutput', lambda *_: None) |
| 102 self.assertIsNone(self.local_repo.GetChangeLog('revision')) | |
| 116 | 103 |
| 117 def testGitChangeLogsParser(self): | 104 def testGetChangeLogs(self): |
| 118 output = textwrap.dedent( | 105 output = textwrap.dedent( |
| 119 """ | 106 """ |
| 120 **Changelog start** | 107 **Changelog start** |
| 121 commit rev1 | 108 commit rev1 |
| 122 tree 27b0421273ed4aea25e497c6d26d9c7db6481852 | 109 tree 27b0421273ed4aea25e497c6d26d9c7db6481852 |
| 123 parents rev22c9e | 110 parents rev22c9e |
| 124 | 111 |
| 125 author author1 | 112 author author1 |
| 126 author-mail author1@chromium.org | 113 author-mail author1@chromium.org |
| 127 author-time 2016-06-02 10:55:38 | 114 author-time 2016-06-02 10:55:38 |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 148 | 135 |
| 149 committer Commit bot | 136 committer Commit bot |
| 150 committer-mail commit-bot@chromium.org | 137 committer-mail commit-bot@chromium.org |
| 151 committer-time 2016-06-02 10:54:14 | 138 committer-time 2016-06-02 10:54:14 |
| 152 | 139 |
| 153 --Message start-- | 140 --Message start-- |
| 154 Message 2 | 141 Message 2 |
| 155 --Message end-- | 142 --Message end-- |
| 156 | 143 |
| 157 :100644 100644 7280f df186 A b/c.py | 144 :100644 100644 7280f df186 A b/c.py |
| 158 | |
| 159 **Changelog start** | |
| 160 commit rev3 | |
| 161 tree d22d3786e135b83183cfeba5f3d8913959f56299 | |
| 162 parents ac7ee4ce7b8d39b22a710c58d110e0039c11cf9a | |
| 163 | |
| 164 author author3 | |
| 165 author-mail author3@chromium.org | |
| 166 author-time 2016-06-02 10:53:03 | |
| 167 | |
| 168 committer Commit bot | |
| 169 committer-mail commit-bot@chromium.org | |
| 170 committer-time 2016-06-02 10:54:14 | |
| 171 | |
| 172 --Message start-- | |
| 173 Message 3 | |
| 174 --Message end-- | |
| 175 | |
| 176 :100644 100644 3f2e 20a5 R078 b/c.py b/cc.py | |
| 177 """ | 145 """ |
| 178 ) | 146 ) |
| 179 | 147 |
| 180 expected_changelogs = [ | 148 expected_changelogs = [ |
| 181 change_log.ChangeLog('author1', | 149 change_log.ChangeLog('author1', |
| 182 'author1@chromium.org', | 150 'author1@chromium.org', |
| 183 datetime(2016, 6, 2, 10, 55, 38), | 151 datetime(2016, 6, 2, 10, 55, 38), |
| 184 'Commit bot', | 152 'Commit bot', |
| 185 'commit-bot@chromium.org', | 153 'commit-bot@chromium.org', |
| 186 datetime(2016, 6, 2, 10, 57, 13), | 154 datetime(2016, 6, 2, 10, 57, 13), |
| 187 'rev1', None, | 155 'rev1', None, |
| 188 'Message 1', [change_log.FileChangeInfo( | 156 'Message 1', [change_log.FileChangeInfo( |
| 189 'delete', 'a/b.py', None)], | 157 'delete', 'a/b.py', None)], |
| 190 'http://repo/+/rev1', None, None), | 158 'https://repo/path/+/rev1', None, None), |
| 191 change_log.ChangeLog('author2', | 159 change_log.ChangeLog('author2', |
| 192 'author2@chromium.org', | 160 'author2@chromium.org', |
| 193 datetime(2016, 6, 2, 10, 53, 3), | 161 datetime(2016, 6, 2, 10, 53, 3), |
| 194 'Commit bot', | 162 'Commit bot', |
| 195 'commit-bot@chromium.org', | 163 'commit-bot@chromium.org', |
| 196 datetime(2016, 6, 2, 10, 54, 14), | 164 datetime(2016, 6, 2, 10, 54, 14), |
| 197 'rev2', None, | 165 'rev2', None, |
| 198 'Message 2', [change_log.FileChangeInfo( | 166 'Message 2', [change_log.FileChangeInfo( |
| 199 'add', None, 'b/c.py')], | 167 'add', None, 'b/c.py')], |
| 200 'http://repo/+/rev2', None, None), | 168 'https://repo/path/+/rev2', None, None), |
| 201 change_log.ChangeLog('author3', | |
| 202 'author3@chromium.org', | |
| 203 datetime(2016, 6, 2, 10, 53, 3), | |
| 204 'Commit bot', | |
| 205 'commit-bot@chromium.org', | |
| 206 datetime(2016, 6, 2, 10, 54, 14), | |
| 207 'rev3', None, | |
| 208 'Message 3', [change_log.FileChangeInfo( | |
| 209 'rename', 'b/c.py', 'b/cc.py')], | |
| 210 'http://repo/+/rev3', None, None), | |
| 211 ] | 169 ] |
| 212 | 170 |
| 213 changelogs = local_git_parsers.GitChangeLogsParser()(output, 'http://repo') | 171 self.mock(repo_util, 'GetCommandOutput', lambda *_: output) |
| 172 changelogs = self.local_repo.GetChangeLogs('rev0', 'rev2') | |
| 214 for changelog, expected_changelog in zip(changelogs, expected_changelogs): | 173 for changelog, expected_changelog in zip(changelogs, expected_changelogs): |
| 215 self.assertEqual(changelog.ToDict(), expected_changelog.ToDict()) | 174 self.assertEqual(changelog.ToDict(), expected_changelog.ToDict()) |
| 216 | 175 |
| 217 def testGitChangeLogsParserWithEmptyChangelog(self): | 176 def testGetChangeLogsNoneCommandOutput(self): |
| 218 output = '**Changelog start**\nblablabla' | 177 self.mock(repo_util, 'GetCommandOutput', lambda *_: None) |
| 219 self.assertEqual(local_git_parsers.GitChangeLogsParser()(output, | 178 self.assertIsNone(self.local_repo.GetChangeLogs('rev0', 'rev2')) |
| 220 'http://repo'), []) | |
| 221 | 179 |
| 222 def testGitDiffParser(self): | 180 def testGetChangeDiff(self): |
| 223 output = 'output' | 181 self.mock(repo_util, 'GetCommandOutput', lambda *_: 'diff') |
| 224 self.assertEqual(output, local_git_parsers.GitDiffParser()(output)) | 182 self.assertEqual(self.local_repo.GetChangeDiff('rev'), 'diff') |
| 183 self.assertEqual(self.local_repo.GetChangeDiff('rev', 'file_path'), 'diff') | |
| 225 | 184 |
| 226 def testGitSourceParser(self): | 185 def testGetChangeDiffNoneCommandOutput(self): |
| 227 output = 'output' | 186 self.mock(repo_util, 'GetCommandOutput', lambda *_: None) |
| 228 self.assertEqual(output, local_git_parsers.GitSourceParser()(output)) | 187 self.assertIsNone(self.local_repo.GetChangeDiff('rev')) |
| 188 | |
| 189 def testGetBlame(self): | |
| 190 output = textwrap.dedent( | |
| 191 """ | |
| 192 revision_hash 18 18 3 | |
| 193 author test@google.com | |
| 194 author-mail <test@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | |
| 195 author-time 2013-03-11 17:13:36 | |
| 196 committer test@google.com | |
| 197 committer-mail <test@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | |
| 198 committer-time 2013-03-11 17:13:36 | |
| 199 summary add (mac) test for ttcindex in SkFontStream | |
| 200 previous fe7533eebe777cc66c7f8fa7a03f00572755c5b4 src/core/SkFont.h | |
| 201 filename src/core/SkFont.h | |
| 202 * blabla line 1 | |
| 203 revision_hash 19 19 | |
| 204 * blabla line 2 | |
| 205 revision_hash 20 20 | |
| 206 * blabla line 3 | |
| 207 """ | |
| 208 ) | |
| 209 self.mock(repo_util, 'GetCommandOutput', lambda *_: output) | |
| 210 expected_regions = [blame.Region(18, 3, 'revision_hash', 'test@google.com', | |
| 211 'test@google.com', | |
| 212 datetime(2013, 03, 11, 17, 13, 36))] | |
| 213 expected_blame = blame.Blame('src/core/SkFont.h', 'rev') | |
| 214 for expected_region in expected_regions: | |
| 215 expected_blame.AddRegion(expected_region) | |
| 216 | |
| 217 blame_result = self.local_repo.GetBlame('src/core/SkFont.h', 'rev') | |
| 218 self.assertTrue(blame_result.revision, expected_blame.revision) | |
| 219 self.assertTrue(blame_result.path, expected_blame.path) | |
| 220 for region, expected_region in zip(blame_result, expected_blame): | |
| 221 self.assertTrue(region.ToDict(), expected_region.ToDict()) | |
|
wrengr
2016/11/01 20:26:53
Why compare the results of ToDict rather than comp
Sharu Jiang
2016/11/05 01:18:15
Added a TODO.
| |
| 222 | |
| 223 def testGetBlameNoneCommandOutput(self): | |
| 224 self.mock(repo_util, 'GetCommandOutput', lambda *_: None) | |
| 225 self.assertIsNone(self.local_repo.GetBlame('src/core/SkFont.h', 'rev')) | |
| 226 | |
| 227 def testGetSource(self): | |
| 228 self.mock(repo_util, 'GetCommandOutput', lambda *_: 'source') | |
| 229 self.assertIsNone(self.local_repo.GetSource('file_path', 'rev')) | |
| 230 self.mock(os.path, 'isfile', lambda path: True) | |
| 231 self.assertEqual(self.local_repo.GetSource('file_path', 'rev'), 'source') | |
| 232 | |
| 233 def testGetSourceNoneCommandOutput(self): | |
| 234 self.mock(repo_util, 'GetCommandOutput', lambda *_: None) | |
| 235 self.mock(os.path, 'isfile', lambda path: True) | |
| 236 self.assertIsNone(self.local_repo.GetSource('file_path', 'rev')) | |
| OLD | NEW |