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 1219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1230 def test_patch_when_dirty(self): | 1230 def test_patch_when_dirty(self): |
1231 # Patch when local tree is dirty | 1231 # Patch when local tree is dirty |
1232 self.mock(git_common, 'is_dirty_git_tree', lambda x: True) | 1232 self.mock(git_common, 'is_dirty_git_tree', lambda x: True) |
1233 self.assertNotEqual(git_cl.main(['patch', '123456']), 0) | 1233 self.assertNotEqual(git_cl.main(['patch', '123456']), 0) |
1234 | 1234 |
1235 def test_diff_when_dirty(self): | 1235 def test_diff_when_dirty(self): |
1236 # Do 'git cl diff' when local tree is dirty | 1236 # Do 'git cl diff' when local tree is dirty |
1237 self.mock(git_common, 'is_dirty_git_tree', lambda x: True) | 1237 self.mock(git_common, 'is_dirty_git_tree', lambda x: True) |
1238 self.assertNotEqual(git_cl.main(['diff']), 0) | 1238 self.assertNotEqual(git_cl.main(['diff']), 0) |
1239 | 1239 |
1240 def _patch_common(self, is_gerrit=False, force_codereview=False): | 1240 def _patch_common(self, is_gerrit=False, force_codereview=False, |
| 1241 new_branch=False): |
1241 self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) | 1242 self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
1242 self.mock(git_cl._RietveldChangelistImpl, 'GetMostRecentPatchset', | 1243 self.mock(git_cl._RietveldChangelistImpl, 'GetMostRecentPatchset', |
1243 lambda x: '60001') | 1244 lambda x: '60001') |
1244 self.mock(git_cl._RietveldChangelistImpl, 'GetPatchSetDiff', | 1245 self.mock(git_cl._RietveldChangelistImpl, 'GetPatchSetDiff', |
1245 lambda *args: None) | 1246 lambda *args: None) |
1246 self.mock(git_cl._GerritChangelistImpl, '_GetChangeDetail', | 1247 self.mock(git_cl._GerritChangelistImpl, '_GetChangeDetail', |
1247 lambda *args: { | 1248 lambda *args: { |
1248 'current_revision': '7777777777', | 1249 'current_revision': '7777777777', |
1249 'revisions': { | 1250 'revisions': { |
1250 '1111111111': { | 1251 '1111111111': { |
1251 '_number': 1, | 1252 '_number': 1, |
1252 'fetch': {'http': { | 1253 'fetch': {'http': { |
1253 'url': 'https://chromium.googlesource.com/my/repo', | 1254 'url': 'https://chromium.googlesource.com/my/repo', |
1254 'ref': 'refs/changes/56/123456/1', | 1255 'ref': 'refs/changes/56/123456/1', |
1255 }}, | 1256 }}, |
1256 }, | 1257 }, |
1257 '7777777777': { | 1258 '7777777777': { |
1258 '_number': 7, | 1259 '_number': 7, |
1259 'fetch': {'http': { | 1260 'fetch': {'http': { |
1260 'url': 'https://chromium.googlesource.com/my/repo', | 1261 'url': 'https://chromium.googlesource.com/my/repo', |
1261 'ref': 'refs/changes/56/123456/7', | 1262 'ref': 'refs/changes/56/123456/7', |
1262 }}, | 1263 }}, |
1263 }, | 1264 }, |
1264 }, | 1265 }, |
1265 }) | 1266 }) |
1266 self.mock(git_cl.Changelist, 'GetDescription', | 1267 self.mock(git_cl.Changelist, 'GetDescription', |
1267 lambda *args: 'Description') | 1268 lambda *args: 'Description') |
1268 self.mock(git_cl, 'IsGitVersionAtLeast', lambda *args: True) | 1269 self.mock(git_cl, 'IsGitVersionAtLeast', lambda *args: True) |
1269 | 1270 |
1270 self.calls = self.calls or [] | 1271 if new_branch: |
| 1272 self.calls = [((['git', 'new-branch', 'master'],), ''),] |
| 1273 else: |
| 1274 self.calls = [((['git', 'symbolic-ref', 'HEAD'],), 'master')] |
1271 if not force_codereview: | 1275 if not force_codereview: |
1272 # These calls detect codereview to use. | 1276 # These calls detect codereview to use. |
1273 self.calls += [ | 1277 self.calls += [ |
1274 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), | 1278 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
1275 ((['git', 'config', '--int', 'branch.master.rietveldissue'],), CERR1), | 1279 ((['git', 'config', '--int', 'branch.master.rietveldissue'],), CERR1), |
1276 ((['git', 'config', '--int', 'branch.master.gerritissue'],), CERR1), | 1280 ((['git', 'config', '--int', 'branch.master.gerritissue'],), CERR1), |
1277 ((['git', 'config', 'rietveld.autoupdate'],), CERR1), | 1281 ((['git', 'config', 'rietveld.autoupdate'],), CERR1), |
1278 ] | 1282 ] |
1279 | 1283 |
1280 if is_gerrit: | 1284 if is_gerrit: |
1281 if not force_codereview: | 1285 if not force_codereview: |
1282 self.calls += [ | 1286 self.calls += [ |
1283 ((['git', 'config', 'gerrit.host'],), 'true'), | 1287 ((['git', 'config', 'gerrit.host'],), 'true'), |
1284 ] | 1288 ] |
1285 else: | 1289 else: |
1286 self.calls += [ | 1290 self.calls += [ |
1287 ((['git', 'config', 'gerrit.host'],), CERR1), | 1291 ((['git', 'config', 'gerrit.host'],), CERR1), |
1288 ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'), | 1292 ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'), |
1289 ((['git', 'rev-parse', '--show-cdup'],), ''), | 1293 ((['git', 'rev-parse', '--show-cdup'],), ''), |
1290 ((['sed', '-e', 's|^--- a/|--- |; s|^+++ b/|+++ |'],), ''), | 1294 ((['sed', '-e', 's|^--- a/|--- |; s|^+++ b/|+++ |'],), ''), |
1291 ] | 1295 ] |
1292 | 1296 |
1293 def _common_patch_successful(self): | 1297 def _common_patch_successful(self, new_branch=False): |
1294 self._patch_common() | 1298 self._patch_common(new_branch=new_branch) |
1295 self.calls += [ | 1299 self.calls += [ |
1296 ((['git', 'apply', '--index', '-p0', '--3way'],), ''), | 1300 ((['git', 'apply', '--index', '-p0', '--3way'],), ''), |
1297 ((['git', 'commit', '-m', | 1301 ((['git', 'commit', '-m', |
1298 'Description\n\n' + | 1302 'Description\n\n' + |
1299 'patch from issue 123456 at patchset 60001 ' + | 1303 'patch from issue 123456 at patchset 60001 ' + |
1300 '(http://crrev.com/123456#ps60001)'],), ''), | 1304 '(http://crrev.com/123456#ps60001)'],), ''), |
1301 ((['git', 'config', '--int', 'branch.master.rietveldissue', '123456'],), | 1305 ((['git', 'config', '--int', 'branch.master.rietveldissue', '123456'],), |
1302 ''), | 1306 ''), |
1303 ((['git', 'config', 'branch.master.rietveldserver'],), CERR1), | 1307 ((['git', 'config', 'branch.master.rietveldserver'],), CERR1), |
1304 ((['git', 'config', 'branch.master.rietveldserver', | 1308 ((['git', 'config', 'branch.master.rietveldserver', |
1305 'https://codereview.example.com'],), ''), | 1309 'https://codereview.example.com'],), ''), |
1306 ((['git', 'config', '--int', 'branch.master.rietveldpatchset', '60001'],), | 1310 ((['git', 'config', '--int', 'branch.master.rietveldpatchset', '60001'],), |
1307 ''), | 1311 ''), |
1308 ] | 1312 ] |
1309 | 1313 |
1310 def test_patch_successful(self): | 1314 def test_patch_successful(self): |
1311 self._common_patch_successful() | 1315 self._common_patch_successful() |
1312 self.assertEqual(git_cl.main(['patch', '123456']), 0) | 1316 self.assertEqual(git_cl.main(['patch', '123456']), 0) |
1313 | 1317 |
1314 def test_patch_successful_new_branch(self): | 1318 def test_patch_successful_new_branch(self): |
1315 self.calls = [ ((['git', 'new-branch', 'master'],), ''), ] | 1319 self._common_patch_successful(new_branch=True) |
1316 self._common_patch_successful() | |
1317 self.assertEqual(git_cl.main(['patch', '-b', 'master', '123456']), 0) | 1320 self.assertEqual(git_cl.main(['patch', '-b', 'master', '123456']), 0) |
1318 | 1321 |
1319 def test_patch_conflict(self): | 1322 def test_patch_conflict(self): |
1320 self._patch_common() | 1323 self._patch_common() |
1321 self.calls += [ | 1324 self.calls += [ |
1322 ((['git', 'apply', '--index', '-p0', '--3way'],), CERR1), | 1325 ((['git', 'apply', '--index', '-p0', '--3way'],), CERR1), |
1323 ] | 1326 ] |
1324 self.assertNotEqual(git_cl.main(['patch', '123456']), 0) | 1327 self.assertNotEqual(git_cl.main(['patch', '123456']), 0) |
1325 | 1328 |
1326 def test_gerrit_patch_successful(self): | 1329 def test_gerrit_patch_successful(self): |
(...skipping 509 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1836 ((['rm_file_or_tree', '/abs/git_repo_root/.git/hooks/commit-msg'],), | 1839 ((['rm_file_or_tree', '/abs/git_repo_root/.git/hooks/commit-msg'],), |
1837 ''), | 1840 ''), |
1838 ] | 1841 ] |
1839 cl._codereview_impl._GerritCommitMsgHookCheck(offer_removal=True) | 1842 cl._codereview_impl._GerritCommitMsgHookCheck(offer_removal=True) |
1840 | 1843 |
1841 | 1844 |
1842 if __name__ == '__main__': | 1845 if __name__ == '__main__': |
1843 git_cl.logging.basicConfig( | 1846 git_cl.logging.basicConfig( |
1844 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) | 1847 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) |
1845 unittest.main() | 1848 unittest.main() |
OLD | NEW |