Chromium Code Reviews| 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 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 102 | 102 |
| 103 def tearDown(self): | 103 def tearDown(self): |
| 104 if not self.has_failed(): | 104 if not self.has_failed(): |
| 105 self.assertEquals([], self.calls) | 105 self.assertEquals([], self.calls) |
| 106 super(TestGitCl, self).tearDown() | 106 super(TestGitCl, self).tearDown() |
| 107 | 107 |
| 108 def _mocked_call(self, *args, **_kwargs): | 108 def _mocked_call(self, *args, **_kwargs): |
| 109 self.assertTrue( | 109 self.assertTrue( |
| 110 self.calls, | 110 self.calls, |
| 111 '@%d Expected: <Missing> Actual: %r' % (self._calls_done, args)) | 111 '@%d Expected: <Missing> Actual: %r' % (self._calls_done, args)) |
| 112 expected_args, result = self.calls.pop(0) | 112 top = self.calls.pop(0) |
| 113 if len(top) == 2: | |
| 114 expected_args, result = top | |
| 115 exception = None | |
| 116 else: | |
| 117 expected_args, result, exception = top | |
|
Sam Clegg
2015/04/20 23:32:16
nit: Do the raise here and avoid the "exception =
wychen
2015/04/22 00:23:18
Done.
| |
| 118 | |
| 113 # Also logs otherwise it could get caught in a try/finally and be hard to | 119 # Also logs otherwise it could get caught in a try/finally and be hard to |
| 114 # diagnose. | 120 # diagnose. |
| 121 if exception: | |
| 122 raise exception | |
| 115 if expected_args != args: | 123 if expected_args != args: |
| 116 msg = '@%d Expected: %r Actual: %r' % ( | 124 msg = '@%d Expected: %r Actual: %r' % ( |
| 117 self._calls_done, expected_args, args) | 125 self._calls_done, expected_args, args) |
| 118 git_cl.logging.error(msg) | 126 git_cl.logging.error(msg) |
| 119 self.fail(msg) | 127 self.fail(msg) |
| 120 self._calls_done += 1 | 128 self._calls_done += 1 |
| 121 return result | 129 return result |
| 122 | 130 |
| 123 @classmethod | 131 @classmethod |
| 124 def _upload_calls(cls, similarity, find_copies, private): | 132 def _upload_calls(cls, similarity, find_copies, private): |
| (...skipping 721 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 846 self.assertEqual('refs/heads/master', | 854 self.assertEqual('refs/heads/master', |
| 847 git_cl.GetTargetRef('origin', | 855 git_cl.GetTargetRef('origin', |
| 848 'refs/remotes/branch-heads/123', | 856 'refs/remotes/branch-heads/123', |
| 849 branch, None)) | 857 branch, None)) |
| 850 | 858 |
| 851 # Check target refs for pending prefix. | 859 # Check target refs for pending prefix. |
| 852 self.assertEqual('prefix/heads/master', | 860 self.assertEqual('prefix/heads/master', |
| 853 git_cl.GetTargetRef('origin', 'refs/remotes/origin/master', | 861 git_cl.GetTargetRef('origin', 'refs/remotes/origin/master', |
| 854 None, 'prefix/')) | 862 None, 'prefix/')) |
| 855 | 863 |
| 864 def test_patch_when_dirty(self): | |
| 865 # Patch when local tree is dirty | |
| 866 self.mock(git_common, 'is_dirty_git_tree', lambda x: True) | |
| 867 self.assertNotEqual(git_cl.main(['patch', '123456']), 0) | |
| 868 | |
| 869 def test_diff_when_dirty(self): | |
| 870 # Do 'git cl diff' when local tree is dirty | |
| 871 self.mock(git_common, 'is_dirty_git_tree', lambda x: True) | |
| 872 self.assertNotEqual(git_cl.main(['diff']), 0) | |
| 873 | |
| 874 def _patch_common(self): | |
| 875 self.mock(git_cl.Changelist, 'GetMostRecentPatchset', lambda x: '60001') | |
| 876 self.mock(git_cl.Changelist, 'GetPatchSetDiff', lambda *args: None) | |
| 877 self.mock(git_cl.Changelist, 'SetIssue', lambda *args: None) | |
| 878 self.mock(git_cl.Changelist, 'SetPatchset', lambda *args: None) | |
| 879 self.mock(git_cl, 'IsGitVersionAtLeast', lambda *args: True) | |
| 880 | |
| 881 self.calls = [ | |
| 882 ((['git', 'config', 'rietveld.autoupdate'],), ''), | |
| 883 ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'), | |
| 884 ((['git', 'rev-parse', '--show-cdup'],), ''), | |
| 885 ((['sed', '-e', 's|^--- a/|--- |; s|^+++ b/|+++ |'],), ''), | |
| 886 ] | |
| 887 | |
| 888 def test_patch_successful(self): | |
| 889 self._patch_common() | |
| 890 self.calls += [ | |
| 891 ((['git', 'apply', '--index', '-p0', '--3way'],), ''), | |
| 892 ((['git', 'commit', '-m', 'patch from issue 123456 at patchset 60001 (http ://crrev.com/123456#ps60001)'],), ''), | |
| 893 ] | |
| 894 self.assertEqual(git_cl.main(['patch', '123456']), 0) | |
| 895 | |
| 896 def test_patch_conflict(self): | |
| 897 self._patch_common() | |
| 898 self.calls += [ | |
| 899 ((['git', 'apply', '--index', '-p0', '--3way'],), '', subprocess2.CalledPr ocessError(1, '', '', '', '')), | |
| 900 ] | |
| 901 self.assertNotEqual(git_cl.main(['patch', '123456']), 0) | |
| 856 | 902 |
| 857 if __name__ == '__main__': | 903 if __name__ == '__main__': |
| 858 git_cl.logging.basicConfig( | 904 git_cl.logging.basicConfig( |
| 859 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) | 905 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) |
| 860 unittest.main() | 906 unittest.main() |
| OLD | NEW |