| 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 1091 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1102 branch, None)) | 1102 branch, None)) |
| 1103 | 1103 |
| 1104 # Check target refs for pending prefix. | 1104 # Check target refs for pending prefix. |
| 1105 self.assertEqual('prefix/heads/master', | 1105 self.assertEqual('prefix/heads/master', |
| 1106 git_cl.GetTargetRef('origin', 'refs/remotes/origin/master', | 1106 git_cl.GetTargetRef('origin', 'refs/remotes/origin/master', |
| 1107 None, 'prefix/')) | 1107 None, 'prefix/')) |
| 1108 | 1108 |
| 1109 def test_patch_when_dirty(self): | 1109 def test_patch_when_dirty(self): |
| 1110 # Patch when local tree is dirty | 1110 # Patch when local tree is dirty |
| 1111 self.mock(git_common, 'is_dirty_git_tree', lambda x: True) | 1111 self.mock(git_common, 'is_dirty_git_tree', lambda x: True) |
| 1112 self.calls = [ | |
| 1113 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), | |
| 1114 ((['git', 'config', 'branch.master.rietveldissue'],), ''), | |
| 1115 ((['git', 'config', 'branch.master.gerritissue'],), ''), | |
| 1116 ((['git', 'config', 'rietveld.autoupdate'],), ''), | |
| 1117 ((['git', 'config', 'gerrit.host'],), ''), | |
| 1118 ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'), | |
| 1119 ] | |
| 1120 self.assertNotEqual(git_cl.main(['patch', '123456']), 0) | 1112 self.assertNotEqual(git_cl.main(['patch', '123456']), 0) |
| 1121 | 1113 |
| 1122 def test_diff_when_dirty(self): | 1114 def test_diff_when_dirty(self): |
| 1123 # Do 'git cl diff' when local tree is dirty | 1115 # Do 'git cl diff' when local tree is dirty |
| 1124 self.mock(git_common, 'is_dirty_git_tree', lambda x: True) | 1116 self.mock(git_common, 'is_dirty_git_tree', lambda x: True) |
| 1125 self.assertNotEqual(git_cl.main(['diff']), 0) | 1117 self.assertNotEqual(git_cl.main(['diff']), 0) |
| 1126 | 1118 |
| 1127 def _patch_common(self, is_gerrit=False, force_codereview=False): | 1119 def _patch_common(self, is_gerrit=False, force_codereview=False): |
| 1128 self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) | 1120 self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
| 1129 self.mock(git_cl._RietveldChangelistImpl, 'GetMostRecentPatchset', | 1121 self.mock(git_cl._RietveldChangelistImpl, 'GetMostRecentPatchset', |
| (...skipping 17 matching lines...) Expand all Loading... |
| 1147 'url': 'https://chromium.googlesource.com/my/repo', | 1139 'url': 'https://chromium.googlesource.com/my/repo', |
| 1148 'ref': 'refs/changes/56/123456/7', | 1140 'ref': 'refs/changes/56/123456/7', |
| 1149 }}, | 1141 }}, |
| 1150 }, | 1142 }, |
| 1151 }, | 1143 }, |
| 1152 }) | 1144 }) |
| 1153 self.mock(git_cl.Changelist, 'GetDescription', | 1145 self.mock(git_cl.Changelist, 'GetDescription', |
| 1154 lambda *args: 'Description') | 1146 lambda *args: 'Description') |
| 1155 self.mock(git_cl, 'IsGitVersionAtLeast', lambda *args: True) | 1147 self.mock(git_cl, 'IsGitVersionAtLeast', lambda *args: True) |
| 1156 | 1148 |
| 1149 self.calls = self.calls or [] |
| 1157 if not force_codereview: | 1150 if not force_codereview: |
| 1158 # These calls detect codereview to use. | 1151 # These calls detect codereview to use. |
| 1159 self.calls = [ | 1152 self.calls += [ |
| 1160 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), | 1153 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
| 1161 ((['git', 'config', 'branch.master.rietveldissue'],), ''), | 1154 ((['git', 'config', 'branch.master.rietveldissue'],), ''), |
| 1162 ((['git', 'config', 'branch.master.gerritissue'],), ''), | 1155 ((['git', 'config', 'branch.master.gerritissue'],), ''), |
| 1163 ((['git', 'config', 'rietveld.autoupdate'],), ''), | 1156 ((['git', 'config', 'rietveld.autoupdate'],), ''), |
| 1164 ] | 1157 ] |
| 1165 else: | |
| 1166 self.calls = [] | |
| 1167 | 1158 |
| 1168 if is_gerrit: | 1159 if is_gerrit: |
| 1169 if not force_codereview: | 1160 if not force_codereview: |
| 1170 self.calls += [ | 1161 self.calls += [ |
| 1171 ((['git', 'config', 'gerrit.host'],), 'true'), | 1162 ((['git', 'config', 'gerrit.host'],), 'true'), |
| 1172 ] | 1163 ] |
| 1173 else: | 1164 else: |
| 1174 self.calls += [ | 1165 self.calls += [ |
| 1175 ((['git', 'config', 'gerrit.host'],), ''), | 1166 ((['git', 'config', 'gerrit.host'],), ''), |
| 1176 ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'), | 1167 ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'), |
| 1177 ((['git', 'rev-parse', '--show-cdup'],), ''), | 1168 ((['git', 'rev-parse', '--show-cdup'],), ''), |
| 1178 ((['sed', '-e', 's|^--- a/|--- |; s|^+++ b/|+++ |'],), ''), | 1169 ((['sed', '-e', 's|^--- a/|--- |; s|^+++ b/|+++ |'],), ''), |
| 1179 ] | 1170 ] |
| 1180 | 1171 |
| 1181 def test_patch_successful(self): | 1172 def _common_patch_successful(self): |
| 1182 self._patch_common() | 1173 self._patch_common() |
| 1183 self.calls += [ | 1174 self.calls += [ |
| 1184 ((['git', 'apply', '--index', '-p0', '--3way'],), ''), | 1175 ((['git', 'apply', '--index', '-p0', '--3way'],), ''), |
| 1185 ((['git', 'commit', '-m', | 1176 ((['git', 'commit', '-m', |
| 1186 'Description\n\n' + | 1177 'Description\n\n' + |
| 1187 'patch from issue 123456 at patchset 60001 ' + | 1178 'patch from issue 123456 at patchset 60001 ' + |
| 1188 '(http://crrev.com/123456#ps60001)'],), ''), | 1179 '(http://crrev.com/123456#ps60001)'],), ''), |
| 1189 ((['git', 'config', 'branch.master.rietveldissue', '123456'],), ''), | 1180 ((['git', 'config', 'branch.master.rietveldissue', '123456'],), ''), |
| 1190 ((['git', 'config', 'branch.master.rietveldserver'],), ''), | 1181 ((['git', 'config', 'branch.master.rietveldserver'],), ''), |
| 1191 ((['git', 'config', 'branch.master.rietveldserver', | 1182 ((['git', 'config', 'branch.master.rietveldserver', |
| 1192 'https://codereview.example.com'],), ''), | 1183 'https://codereview.example.com'],), ''), |
| 1193 ((['git', 'config', 'branch.master.rietveldpatchset', '60001'],), ''), | 1184 ((['git', 'config', 'branch.master.rietveldpatchset', '60001'],), ''), |
| 1194 ] | 1185 ] |
| 1186 |
| 1187 def test_patch_successful(self): |
| 1188 self._common_patch_successful() |
| 1195 self.assertEqual(git_cl.main(['patch', '123456']), 0) | 1189 self.assertEqual(git_cl.main(['patch', '123456']), 0) |
| 1196 | 1190 |
| 1191 def test_patch_successful_new_branch(self): |
| 1192 self.calls = [ ((['git', 'new-branch', 'master'],), ''), ] |
| 1193 self._common_patch_successful() |
| 1194 self.assertEqual(git_cl.main(['patch', '-b', 'master', '123456']), 0) |
| 1195 |
| 1197 def test_patch_conflict(self): | 1196 def test_patch_conflict(self): |
| 1198 self._patch_common() | 1197 self._patch_common() |
| 1199 self.calls += [ | 1198 self.calls += [ |
| 1200 ((['git', 'apply', '--index', '-p0', '--3way'],), '', | 1199 ((['git', 'apply', '--index', '-p0', '--3way'],), '', |
| 1201 subprocess2.CalledProcessError(1, '', '', '', '')), | 1200 subprocess2.CalledProcessError(1, '', '', '', '')), |
| 1202 ] | 1201 ] |
| 1203 self.assertNotEqual(git_cl.main(['patch', '123456']), 0) | 1202 self.assertNotEqual(git_cl.main(['patch', '123456']), 0) |
| 1204 | 1203 |
| 1205 def test_gerrit_patch_successful(self): | 1204 def test_gerrit_patch_successful(self): |
| 1206 self._patch_common(is_gerrit=True) | 1205 self._patch_common(is_gerrit=True) |
| (...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1452 self.mock(git_cl.sys, 'stdin', StringIO.StringIO('hi \r\n\t there\n\nman')) | 1451 self.mock(git_cl.sys, 'stdin', StringIO.StringIO('hi \r\n\t there\n\nman')) |
| 1453 | 1452 |
| 1454 self.assertEqual(0, git_cl.main(['description', '-n', '-'])) | 1453 self.assertEqual(0, git_cl.main(['description', '-n', '-'])) |
| 1455 self.assertEqual('hi\n\t there\n\nman', ChangelistMock.desc) | 1454 self.assertEqual('hi\n\t there\n\nman', ChangelistMock.desc) |
| 1456 | 1455 |
| 1457 | 1456 |
| 1458 if __name__ == '__main__': | 1457 if __name__ == '__main__': |
| 1459 git_cl.logging.basicConfig( | 1458 git_cl.logging.basicConfig( |
| 1460 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) | 1459 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) |
| 1461 unittest.main() | 1460 unittest.main() |
| OLD | NEW |