| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """Unit tests for git_cl.py.""" | 6 """Unit tests for git_cl.py.""" |
| 7 | 7 |
| 8 import os | 8 import os |
| 9 import StringIO | 9 import StringIO |
| 10 import stat | 10 import stat |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 class CodereviewSettingsFileMock(object): | 59 class CodereviewSettingsFileMock(object): |
| 60 def __init__(self): | 60 def __init__(self): |
| 61 pass | 61 pass |
| 62 # pylint: disable=R0201 | 62 # pylint: disable=R0201 |
| 63 def read(self): | 63 def read(self): |
| 64 return ("CODE_REVIEW_SERVER: gerrit.chromium.org\n" + | 64 return ("CODE_REVIEW_SERVER: gerrit.chromium.org\n" + |
| 65 "GERRIT_HOST: gerrit.chromium.org\n" + | 65 "GERRIT_HOST: gerrit.chromium.org\n" + |
| 66 "GERRIT_PORT: 29418\n") | 66 "GERRIT_PORT: 29418\n") |
| 67 | 67 |
| 68 | 68 |
| 69 class AuthenticatorMock(object): |
| 70 def __init__(self, *_args): |
| 71 pass |
| 72 def has_cached_credentials(self): |
| 73 return True |
| 74 |
| 75 |
| 69 class TestGitCl(TestCase): | 76 class TestGitCl(TestCase): |
| 70 def setUp(self): | 77 def setUp(self): |
| 71 super(TestGitCl, self).setUp() | 78 super(TestGitCl, self).setUp() |
| 72 self.calls = [] | 79 self.calls = [] |
| 73 self._calls_done = 0 | 80 self._calls_done = 0 |
| 74 self.mock(subprocess2, 'call', self._mocked_call) | 81 self.mock(subprocess2, 'call', self._mocked_call) |
| 75 self.mock(subprocess2, 'check_call', self._mocked_call) | 82 self.mock(subprocess2, 'check_call', self._mocked_call) |
| 76 self.mock(subprocess2, 'check_output', self._mocked_call) | 83 self.mock(subprocess2, 'check_output', self._mocked_call) |
| 77 self.mock(subprocess2, 'communicate', self._mocked_call) | 84 self.mock(subprocess2, 'communicate', self._mocked_call) |
| 78 self.mock(git_common, 'is_dirty_git_tree', lambda x: False) | 85 self.mock(git_common, 'is_dirty_git_tree', lambda x: False) |
| 79 self.mock(git_common, 'get_or_create_merge_base', | 86 self.mock(git_common, 'get_or_create_merge_base', |
| 80 lambda *a: ( | 87 lambda *a: ( |
| 81 self._mocked_call(['get_or_create_merge_base']+list(a)))) | 88 self._mocked_call(['get_or_create_merge_base']+list(a)))) |
| 82 self.mock(git_cl, 'FindCodereviewSettingsFile', lambda: '') | 89 self.mock(git_cl, 'FindCodereviewSettingsFile', lambda: '') |
| 83 self.mock(git_cl, 'ask_for_data', self._mocked_call) | 90 self.mock(git_cl, 'ask_for_data', self._mocked_call) |
| 84 self.mock(git_cl.breakpad, 'post', self._mocked_call) | 91 self.mock(git_cl.breakpad, 'post', self._mocked_call) |
| 85 self.mock(git_cl.breakpad, 'SendStack', self._mocked_call) | 92 self.mock(git_cl.breakpad, 'SendStack', self._mocked_call) |
| 86 self.mock(git_cl.presubmit_support, 'DoPresubmitChecks', PresubmitMock) | 93 self.mock(git_cl.presubmit_support, 'DoPresubmitChecks', PresubmitMock) |
| 87 self.mock(git_cl.rietveld, 'Rietveld', RietveldMock) | 94 self.mock(git_cl.rietveld, 'Rietveld', RietveldMock) |
| 88 self.mock(git_cl.rietveld, 'CachingRietveld', RietveldMock) | 95 self.mock(git_cl.rietveld, 'CachingRietveld', RietveldMock) |
| 89 self.mock(git_cl.upload, 'RealMain', self.fail) | 96 self.mock(git_cl.upload, 'RealMain', self.fail) |
| 90 self.mock(git_cl.watchlists, 'Watchlists', WatchlistsMock) | 97 self.mock(git_cl.watchlists, 'Watchlists', WatchlistsMock) |
| 98 self.mock(git_cl.auth, 'get_authenticator_for_host', AuthenticatorMock) |
| 91 # It's important to reset settings to not have inter-tests interference. | 99 # It's important to reset settings to not have inter-tests interference. |
| 92 git_cl.settings = None | 100 git_cl.settings = None |
| 93 | 101 |
| 94 def tearDown(self): | 102 def tearDown(self): |
| 95 if not self.has_failed(): | 103 if not self.has_failed(): |
| 96 self.assertEquals([], self.calls) | 104 self.assertEquals([], self.calls) |
| 97 super(TestGitCl, self).tearDown() | 105 super(TestGitCl, self).tearDown() |
| 98 | 106 |
| 99 def _mocked_call(self, *args, **_kwargs): | 107 def _mocked_call(self, *args, **_kwargs): |
| 100 self.assertTrue( | 108 self.assertTrue( |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 'codereview.example.com'), | 162 'codereview.example.com'), |
| 155 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), | 163 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
| 156 similarity_call, | 164 similarity_call, |
| 157 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), | 165 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
| 158 find_copies_call, | 166 find_copies_call, |
| 159 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), | 167 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
| 160 ((['git', 'config', 'branch.master.merge'],), 'master'), | 168 ((['git', 'config', 'branch.master.merge'],), 'master'), |
| 161 ((['git', 'config', 'branch.master.remote'],), 'origin'), | 169 ((['git', 'config', 'branch.master.remote'],), 'origin'), |
| 162 ((['get_or_create_merge_base', 'master', 'master'],), | 170 ((['get_or_create_merge_base', 'master', 'master'],), |
| 163 'fake_ancestor_sha'), | 171 'fake_ancestor_sha'), |
| 172 ((['git', 'config', 'gerrit.host'],), ''), |
| 173 ((['git', 'config', 'branch.master.rietveldissue'],), ''), |
| 164 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [ | 174 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [ |
| 165 ((['git', 'rev-parse', '--show-cdup'],), ''), | 175 ((['git', 'rev-parse', '--show-cdup'],), ''), |
| 166 ((['git', 'rev-parse', 'HEAD'],), '12345'), | 176 ((['git', 'rev-parse', 'HEAD'],), '12345'), |
| 167 ((['git', 'diff', '--name-status', '--no-renames', '-r', | 177 ((['git', 'diff', '--name-status', '--no-renames', '-r', |
| 168 'fake_ancestor_sha...', '.'],), | 178 'fake_ancestor_sha...', '.'],), |
| 169 'M\t.gitignore\n'), | 179 'M\t.gitignore\n'), |
| 170 ((['git', 'config', 'branch.master.rietveldissue'],), ''), | |
| 171 ((['git', 'config', 'branch.master.rietveldpatchset'],), | 180 ((['git', 'config', 'branch.master.rietveldpatchset'],), |
| 172 ''), | 181 ''), |
| 173 ((['git', 'log', '--pretty=format:%s%n%n%b', | 182 ((['git', 'log', '--pretty=format:%s%n%n%b', |
| 174 'fake_ancestor_sha...'],), | 183 'fake_ancestor_sha...'],), |
| 175 'foo'), | 184 'foo'), |
| 176 ((['git', 'config', 'user.email'],), 'me@example.com'), | 185 ((['git', 'config', 'user.email'],), 'me@example.com'), |
| 177 stat_call, | 186 stat_call, |
| 178 ((['git', 'config', 'gerrit.host'],), ''), | |
| 179 ((['git', 'log', '--pretty=format:%s\n\n%b', | 187 ((['git', 'log', '--pretty=format:%s\n\n%b', |
| 180 'fake_ancestor_sha..HEAD'],), | 188 'fake_ancestor_sha..HEAD'],), |
| 181 'desc\n'), | 189 'desc\n'), |
| 182 ((['git', 'config', 'rietveld.bug-prefix'],), ''), | 190 ((['git', 'config', 'rietveld.bug-prefix'],), ''), |
| 183 ] | 191 ] |
| 184 | 192 |
| 185 @classmethod | 193 @classmethod |
| 186 def _git_upload_no_rev_calls(cls): | 194 def _git_upload_no_rev_calls(cls): |
| 187 return [ | 195 return [ |
| 188 ((['git', 'config', 'core.editor'],), ''), | 196 ((['git', 'config', 'core.editor'],), ''), |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 354 ((['git', 'checkout', '-q', 'working'],), ''), | 362 ((['git', 'checkout', '-q', 'working'],), ''), |
| 355 ((['git', 'branch', '-D', 'git-cl-commit'],), ''), | 363 ((['git', 'branch', '-D', 'git-cl-commit'],), ''), |
| 356 ] | 364 ] |
| 357 | 365 |
| 358 @staticmethod | 366 @staticmethod |
| 359 def _cmd_line(description, args, similarity, find_copies, private): | 367 def _cmd_line(description, args, similarity, find_copies, private): |
| 360 """Returns the upload command line passed to upload.RealMain().""" | 368 """Returns the upload command line passed to upload.RealMain().""" |
| 361 return [ | 369 return [ |
| 362 'upload', '--assume_yes', '--server', | 370 'upload', '--assume_yes', '--server', |
| 363 'https://codereview.example.com', | 371 'https://codereview.example.com', |
| 364 '--no-oauth2', '--auth-host-port', '8090', | |
| 365 '--message', description | 372 '--message', description |
| 366 ] + args + [ | 373 ] + args + [ |
| 367 '--cc', 'joe@example.com', | 374 '--cc', 'joe@example.com', |
| 368 ] + (['--private'] if private else []) + [ | 375 ] + (['--private'] if private else []) + [ |
| 369 '--git_similarity', similarity or '50' | 376 '--git_similarity', similarity or '50' |
| 370 ] + (['--git_no_find_copies'] if find_copies == False else []) + [ | 377 ] + (['--git_no_find_copies'] if find_copies == False else []) + [ |
| 371 'fake_ancestor_sha', 'HEAD' | 378 'fake_ancestor_sha', 'HEAD' |
| 372 ] | 379 ] |
| 373 | 380 |
| 374 def _run_reviewer_test( | 381 def _run_reviewer_test( |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 539 ((['git', 'config', '--int', '--get', | 546 ((['git', 'config', '--int', '--get', |
| 540 'branch.master.git-cl-similarity'],), ''), | 547 'branch.master.git-cl-similarity'],), ''), |
| 541 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), | 548 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
| 542 ((['git', 'config', '--int', '--get', | 549 ((['git', 'config', '--int', '--get', |
| 543 'branch.master.git-find-copies'],), ''), | 550 'branch.master.git-find-copies'],), ''), |
| 544 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), | 551 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
| 545 ((['git', 'config', 'branch.master.merge'],), 'master'), | 552 ((['git', 'config', 'branch.master.merge'],), 'master'), |
| 546 ((['git', 'config', 'branch.master.remote'],), 'origin'), | 553 ((['git', 'config', 'branch.master.remote'],), 'origin'), |
| 547 ((['get_or_create_merge_base', 'master', 'master'],), | 554 ((['get_or_create_merge_base', 'master', 'master'],), |
| 548 'fake_ancestor_sha'), | 555 'fake_ancestor_sha'), |
| 556 ((['git', 'config', 'gerrit.host'],), 'gerrit.example.com'), |
| 549 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [ | 557 ] + cls._git_sanity_checks('fake_ancestor_sha', 'master') + [ |
| 550 ((['git', 'rev-parse', '--show-cdup'],), ''), | 558 ((['git', 'rev-parse', '--show-cdup'],), ''), |
| 551 ((['git', 'rev-parse', 'HEAD'],), '12345'), | 559 ((['git', 'rev-parse', 'HEAD'],), '12345'), |
| 552 ((['git', | 560 ((['git', |
| 553 'diff', '--name-status', '--no-renames', '-r', | 561 'diff', '--name-status', '--no-renames', '-r', |
| 554 'fake_ancestor_sha...', '.'],), | 562 'fake_ancestor_sha...', '.'],), |
| 555 'M\t.gitignore\n'), | 563 'M\t.gitignore\n'), |
| 556 ((['git', 'config', 'branch.master.rietveldissue'],), ''), | 564 ((['git', 'config', 'branch.master.rietveldissue'],), ''), |
| 557 ((['git', | 565 ((['git', |
| 558 'config', 'branch.master.rietveldpatchset'],), ''), | 566 'config', 'branch.master.rietveldpatchset'],), ''), |
| 559 ((['git', | 567 ((['git', |
| 560 'log', '--pretty=format:%s%n%n%b', 'fake_ancestor_sha...'],), | 568 'log', '--pretty=format:%s%n%n%b', 'fake_ancestor_sha...'],), |
| 561 'foo'), | 569 'foo'), |
| 562 ((['git', 'config', 'user.email'],), 'me@example.com'), | 570 ((['git', 'config', 'user.email'],), 'me@example.com'), |
| 563 ((['git', | 571 ((['git', |
| 564 'diff', '--no-ext-diff', '--stat', '--find-copies-harder', | 572 'diff', '--no-ext-diff', '--stat', '--find-copies-harder', |
| 565 '-l100000', '-C50', 'fake_ancestor_sha', 'HEAD'],), | 573 '-l100000', '-C50', 'fake_ancestor_sha', 'HEAD'],), |
| 566 '+dat'), | 574 '+dat'), |
| 567 ] | 575 ] |
| 568 | 576 |
| 569 @staticmethod | 577 @staticmethod |
| 570 def _gerrit_upload_calls(description, reviewers, squash): | 578 def _gerrit_upload_calls(description, reviewers, squash): |
| 571 calls = [ | 579 calls = [ |
| 572 ((['git', 'config', 'gerrit.host'],), | |
| 573 'gerrit.example.com'), | |
| 574 ((['git', 'log', '--pretty=format:%s\n\n%b', | 580 ((['git', 'log', '--pretty=format:%s\n\n%b', |
| 575 'fake_ancestor_sha..HEAD'],), | 581 'fake_ancestor_sha..HEAD'],), |
| 576 description) | 582 description) |
| 577 ] | 583 ] |
| 578 if git_cl.CHANGE_ID not in description: | 584 if git_cl.CHANGE_ID not in description: |
| 579 calls += [ | 585 calls += [ |
| 580 ((['git', 'log', '--pretty=format:%s\n\n%b', | 586 ((['git', 'log', '--pretty=format:%s\n\n%b', |
| 581 'fake_ancestor_sha..HEAD'],), | 587 'fake_ancestor_sha..HEAD'],), |
| 582 description), | 588 description), |
| 583 ((['git', 'commit', '--amend', '-m', description],), | 589 ((['git', 'commit', '--amend', '-m', description],), |
| (...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 844 # Check target refs for pending prefix. | 850 # Check target refs for pending prefix. |
| 845 self.assertEqual('prefix/heads/master', | 851 self.assertEqual('prefix/heads/master', |
| 846 git_cl.GetTargetRef('origin', 'refs/remotes/origin/master', | 852 git_cl.GetTargetRef('origin', 'refs/remotes/origin/master', |
| 847 None, 'prefix/')) | 853 None, 'prefix/')) |
| 848 | 854 |
| 849 | 855 |
| 850 if __name__ == '__main__': | 856 if __name__ == '__main__': |
| 851 git_cl.logging.basicConfig( | 857 git_cl.logging.basicConfig( |
| 852 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) | 858 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) |
| 853 unittest.main() | 859 unittest.main() |
| OLD | NEW |