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 509 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
520 | 520 |
521 def _run_reviewer_test( | 521 def _run_reviewer_test( |
522 self, | 522 self, |
523 upload_args, | 523 upload_args, |
524 expected_description, | 524 expected_description, |
525 returned_description, | 525 returned_description, |
526 final_description, | 526 final_description, |
527 reviewers, | 527 reviewers, |
528 private=False): | 528 private=False): |
529 """Generic reviewer test framework.""" | 529 """Generic reviewer test framework.""" |
| 530 self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
530 try: | 531 try: |
531 similarity = upload_args[upload_args.index('--similarity')+1] | 532 similarity = upload_args[upload_args.index('--similarity')+1] |
532 except ValueError: | 533 except ValueError: |
533 similarity = None | 534 similarity = None |
534 | 535 |
535 if '--find-copies' in upload_args: | 536 if '--find-copies' in upload_args: |
536 find_copies = True | 537 find_copies = True |
537 elif '--no-find-copies' in upload_args: | 538 elif '--no-find-copies' in upload_args: |
538 find_copies = False | 539 find_copies = False |
539 else: | 540 else: |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
653 self.fail() | 654 self.fail() |
654 except SystemExit: | 655 except SystemExit: |
655 self.assertEqual( | 656 self.assertEqual( |
656 'Using 50% similarity for rename/copy detection. Override with ' | 657 'Using 50% similarity for rename/copy detection. Override with ' |
657 '--similarity.\n', | 658 '--similarity.\n', |
658 stdout.getvalue()) | 659 stdout.getvalue()) |
659 self.assertEqual( | 660 self.assertEqual( |
660 'Must specify reviewers to send email.\n', stderr.getvalue()) | 661 'Must specify reviewers to send email.\n', stderr.getvalue()) |
661 | 662 |
662 def test_dcommit(self): | 663 def test_dcommit(self): |
| 664 self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
663 self.calls = ( | 665 self.calls = ( |
664 self._dcommit_calls_1() + | 666 self._dcommit_calls_1() + |
665 self._git_sanity_checks('fake_ancestor_sha', 'working') + | 667 self._git_sanity_checks('fake_ancestor_sha', 'working') + |
666 self._dcommit_calls_normal() + | 668 self._dcommit_calls_normal() + |
667 self._dcommit_calls_3()) | 669 self._dcommit_calls_3()) |
668 git_cl.main(['dcommit']) | 670 git_cl.main(['dcommit']) |
669 | 671 |
670 def test_dcommit_bypass_hooks(self): | 672 def test_dcommit_bypass_hooks(self): |
| 673 self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
671 self.calls = ( | 674 self.calls = ( |
672 self._dcommit_calls_1() + | 675 self._dcommit_calls_1() + |
673 self._dcommit_calls_bypassed() + | 676 self._dcommit_calls_bypassed() + |
674 self._dcommit_calls_3()) | 677 self._dcommit_calls_3()) |
675 git_cl.main(['dcommit', '--bypass-hooks']) | 678 git_cl.main(['dcommit', '--bypass-hooks']) |
676 | 679 |
677 | 680 |
678 @classmethod | 681 @classmethod |
679 def _gerrit_ensure_auth_calls(cls, issue=None): | 682 def _gerrit_ensure_auth_calls(cls, issue=None, skip_auth_check=False): |
680 calls = [] | 683 cmd = ['git', 'config', '--bool', 'gerrit.skip_ensure_authenticated'] |
| 684 if skip_auth_check: |
| 685 return [((cmd, ), 'true')] |
| 686 |
| 687 calls = [((cmd, ), '', subprocess2.CalledProcessError(1, '', '', '', ''))] |
681 if issue: | 688 if issue: |
682 calls.extend([ | 689 calls.extend([ |
683 ((['git', 'config', 'branch.master.gerritserver'],), ''), | 690 ((['git', 'config', 'branch.master.gerritserver'],), ''), |
684 ]) | 691 ]) |
685 calls.extend([ | 692 calls.extend([ |
686 ((['git', 'config', 'branch.master.merge'],), 'refs/heads/master'), | 693 ((['git', 'config', 'branch.master.merge'],), 'refs/heads/master'), |
687 ((['git', 'config', 'branch.master.remote'],), 'origin'), | 694 ((['git', 'config', 'branch.master.remote'],), 'origin'), |
688 ((['git', 'config', 'remote.origin.url'],), | 695 ((['git', 'config', 'remote.origin.url'],), |
689 'https://chromium.googlesource.com/my/repo'), | 696 'https://chromium.googlesource.com/my/repo'), |
690 ((['git', 'config', 'remote.origin.url'],), | 697 ((['git', 'config', 'remote.origin.url'],), |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
842 upload_args, | 849 upload_args, |
843 description, | 850 description, |
844 reviewers=None, | 851 reviewers=None, |
845 squash=False, | 852 squash=False, |
846 expected_upstream_ref='origin/refs/heads/master', | 853 expected_upstream_ref='origin/refs/heads/master', |
847 ref_suffix='', | 854 ref_suffix='', |
848 post_amend_description=None, | 855 post_amend_description=None, |
849 issue=None): | 856 issue=None): |
850 """Generic gerrit upload test framework.""" | 857 """Generic gerrit upload test framework.""" |
851 reviewers = reviewers or [] | 858 reviewers = reviewers or [] |
| 859 self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
852 self.mock(git_cl.gerrit_util, "CookiesAuthenticator", | 860 self.mock(git_cl.gerrit_util, "CookiesAuthenticator", |
853 CookiesAuthenticatorMockFactory(same_cookie='same_cred')) | 861 CookiesAuthenticatorMockFactory(same_cookie='same_cred')) |
854 self.calls = self._gerrit_base_calls(issue=issue) | 862 self.calls = self._gerrit_base_calls(issue=issue) |
855 self.calls += self._gerrit_upload_calls( | 863 self.calls += self._gerrit_upload_calls( |
856 description, reviewers, squash, | 864 description, reviewers, squash, |
857 expected_upstream_ref=expected_upstream_ref, | 865 expected_upstream_ref=expected_upstream_ref, |
858 ref_suffix=ref_suffix, | 866 ref_suffix=ref_suffix, |
859 post_amend_description=post_amend_description, | 867 post_amend_description=post_amend_description, |
860 issue=issue) | 868 issue=issue) |
861 # Uncomment when debugging. | 869 # Uncomment when debugging. |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
919 lambda *args: {'change_id': '123456789'}) | 927 lambda *args: {'change_id': '123456789'}) |
920 self._run_gerrit_upload_test( | 928 self._run_gerrit_upload_test( |
921 ['--squash'], | 929 ['--squash'], |
922 description, | 930 description, |
923 [], | 931 [], |
924 squash=True, | 932 squash=True, |
925 expected_upstream_ref='origin/master', | 933 expected_upstream_ref='origin/master', |
926 issue=123456) | 934 issue=123456) |
927 | 935 |
928 def test_upload_branch_deps(self): | 936 def test_upload_branch_deps(self): |
| 937 self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
929 def mock_run_git(*args, **_kwargs): | 938 def mock_run_git(*args, **_kwargs): |
930 if args[0] == ['for-each-ref', | 939 if args[0] == ['for-each-ref', |
931 '--format=%(refname:short) %(upstream:short)', | 940 '--format=%(refname:short) %(upstream:short)', |
932 'refs/heads']: | 941 'refs/heads']: |
933 # Create a local branch dependency tree that looks like this: | 942 # Create a local branch dependency tree that looks like this: |
934 # test1 -> test2 -> test3 -> test4 -> test5 | 943 # test1 -> test2 -> test3 -> test4 -> test5 |
935 # -> test3.1 | 944 # -> test3.1 |
936 # test6 -> test0 | 945 # test6 -> test0 |
937 branch_deps = [ | 946 branch_deps = [ |
938 'test2 test1', # test1 -> test2 | 947 'test2 test1', # test1 -> test2 |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1087 ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'), | 1096 ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'), |
1088 ] | 1097 ] |
1089 self.assertNotEqual(git_cl.main(['patch', '123456']), 0) | 1098 self.assertNotEqual(git_cl.main(['patch', '123456']), 0) |
1090 | 1099 |
1091 def test_diff_when_dirty(self): | 1100 def test_diff_when_dirty(self): |
1092 # Do 'git cl diff' when local tree is dirty | 1101 # Do 'git cl diff' when local tree is dirty |
1093 self.mock(git_common, 'is_dirty_git_tree', lambda x: True) | 1102 self.mock(git_common, 'is_dirty_git_tree', lambda x: True) |
1094 self.assertNotEqual(git_cl.main(['diff']), 0) | 1103 self.assertNotEqual(git_cl.main(['diff']), 0) |
1095 | 1104 |
1096 def _patch_common(self, is_gerrit=False, force_codereview=False): | 1105 def _patch_common(self, is_gerrit=False, force_codereview=False): |
| 1106 self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
1097 self.mock(git_cl._RietveldChangelistImpl, 'GetMostRecentPatchset', | 1107 self.mock(git_cl._RietveldChangelistImpl, 'GetMostRecentPatchset', |
1098 lambda x: '60001') | 1108 lambda x: '60001') |
1099 self.mock(git_cl._RietveldChangelistImpl, 'GetPatchSetDiff', | 1109 self.mock(git_cl._RietveldChangelistImpl, 'GetPatchSetDiff', |
1100 lambda *args: None) | 1110 lambda *args: None) |
1101 self.mock(git_cl._GerritChangelistImpl, '_GetChangeDetail', | 1111 self.mock(git_cl._GerritChangelistImpl, '_GetChangeDetail', |
1102 lambda *args: { | 1112 lambda *args: { |
1103 'current_revision': '7777777777', | 1113 'current_revision': '7777777777', |
1104 'revisions': { | 1114 'revisions': { |
1105 '1111111111': { | 1115 '1111111111': { |
1106 '_number': 1, | 1116 '_number': 1, |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1258 self.assertEqual(0, git_cl.main(['checkout', '123456'])) | 1268 self.assertEqual(0, git_cl.main(['checkout', '123456'])) |
1259 | 1269 |
1260 def test_checkout_rietveld(self): | 1270 def test_checkout_rietveld(self): |
1261 """Tests git cl checkout <issue>.""" | 1271 """Tests git cl checkout <issue>.""" |
1262 self.calls = self._checkout_calls() | 1272 self.calls = self._checkout_calls() |
1263 self.calls += [((['git', 'checkout', 'some-fix'], ), '')] | 1273 self.calls += [((['git', 'checkout', 'some-fix'], ), '')] |
1264 self.assertEqual(0, git_cl.main(['checkout', '2222222222'])) | 1274 self.assertEqual(0, git_cl.main(['checkout', '2222222222'])) |
1265 | 1275 |
1266 def test_checkout_not_found(self): | 1276 def test_checkout_not_found(self): |
1267 """Tests git cl checkout <issue>.""" | 1277 """Tests git cl checkout <issue>.""" |
| 1278 self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
1268 self.calls = self._checkout_calls() | 1279 self.calls = self._checkout_calls() |
1269 self.assertEqual(1, git_cl.main(['checkout', '99999'])) | 1280 self.assertEqual(1, git_cl.main(['checkout', '99999'])) |
1270 | 1281 |
1271 def test_checkout_no_branch_issues(self): | 1282 def test_checkout_no_branch_issues(self): |
1272 """Tests git cl checkout <issue>.""" | 1283 """Tests git cl checkout <issue>.""" |
| 1284 self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
1273 self.calls = [ | 1285 self.calls = [ |
1274 ((['git', 'config', '--local', '--get-regexp', | 1286 ((['git', 'config', '--local', '--get-regexp', |
1275 'branch\\..*\\.rietveldissue'], ), '', | 1287 'branch\\..*\\.rietveldissue'], ), '', |
1276 subprocess2.CalledProcessError(1, '', '', '', '')), | 1288 subprocess2.CalledProcessError(1, '', '', '', '')), |
1277 ((['git', 'config', '--local', '--get-regexp', | 1289 ((['git', 'config', '--local', '--get-regexp', |
1278 'branch\\..*\\.gerritissue'], ), '', | 1290 'branch\\..*\\.gerritissue'], ), '', |
1279 subprocess2.CalledProcessError(1, '', '', '', '')), | 1291 subprocess2.CalledProcessError(1, '', '', '', '')), |
1280 | 1292 |
1281 ] | 1293 ] |
1282 self.assertEqual(1, git_cl.main(['checkout', '99999'])) | 1294 self.assertEqual(1, git_cl.main(['checkout', '99999'])) |
1283 | 1295 |
1284 def _test_gerrit_ensure_authenticated_common(self, auth): | 1296 def _test_gerrit_ensure_authenticated_common(self, auth, |
| 1297 skip_auth_check=False): |
1285 self.mock(git_cl.gerrit_util, 'CookiesAuthenticator', | 1298 self.mock(git_cl.gerrit_util, 'CookiesAuthenticator', |
1286 CookiesAuthenticatorMockFactory(hosts_with_creds=auth)) | 1299 CookiesAuthenticatorMockFactory(hosts_with_creds=auth)) |
1287 self.mock(git_cl, 'DieWithError', | 1300 self.mock(git_cl, 'DieWithError', |
1288 lambda msg: self._mocked_call(['DieWithError', msg])) | 1301 lambda msg: self._mocked_call(['DieWithError', msg])) |
1289 self.mock(git_cl, 'ask_for_data', | 1302 self.mock(git_cl, 'ask_for_data', |
1290 lambda msg: self._mocked_call(['ask_for_data', msg])) | 1303 lambda msg: self._mocked_call(['ask_for_data', msg])) |
1291 self.calls = [ | 1304 self.calls = self._gerrit_ensure_auth_calls(skip_auth_check=skip_auth_check) |
1292 ((['git', 'symbolic-ref', 'HEAD'],), 'master') | |
1293 ] + self._gerrit_ensure_auth_calls() | |
1294 cl = git_cl.Changelist(codereview='gerrit') | 1305 cl = git_cl.Changelist(codereview='gerrit') |
| 1306 cl.branch = 'master' |
| 1307 cl.branchref = 'refs/heads/master' |
1295 cl.lookedup_issue = True | 1308 cl.lookedup_issue = True |
1296 return cl | 1309 return cl |
1297 | 1310 |
1298 def test_gerrit_ensure_authenticated_missing(self): | 1311 def test_gerrit_ensure_authenticated_missing(self): |
1299 cl = self._test_gerrit_ensure_authenticated_common(auth={ | 1312 cl = self._test_gerrit_ensure_authenticated_common(auth={ |
1300 'chromium.googlesource.com': 'git is ok, but gerrit one is missing', | 1313 'chromium.googlesource.com': 'git is ok, but gerrit one is missing', |
1301 }) | 1314 }) |
1302 self.calls.append( | 1315 self.calls.append( |
1303 ((['DieWithError', | 1316 ((['DieWithError', |
1304 'Credentials for the following hosts are required:\n' | 1317 'Credentials for the following hosts are required:\n' |
1305 ' chromium-review.googlesource.com\n' | 1318 ' chromium-review.googlesource.com\n' |
1306 'These are read from ~/.gitcookies (or legacy ~/.netrc)\n' | 1319 'These are read from ~/.gitcookies (or legacy ~/.netrc)\n' |
1307 'You can (re)generate your credentails by visiting ' | 1320 'You can (re)generate your credentails by visiting ' |
1308 'https://chromium-review.googlesource.com/new-password'],), ''),) | 1321 'https://chromium-review.googlesource.com/new-password'],), ''),) |
1309 self.assertIsNone(cl.EnsureAuthenticated(force=False)) | 1322 self.assertIsNone(cl.EnsureAuthenticated(force=False)) |
1310 | 1323 |
1311 def test_gerrit_ensure_authenticated_conflict(self): | 1324 def test_gerrit_ensure_authenticated_conflict(self): |
| 1325 self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
1312 cl = self._test_gerrit_ensure_authenticated_common(auth={ | 1326 cl = self._test_gerrit_ensure_authenticated_common(auth={ |
1313 'chromium.googlesource.com': 'one', | 1327 'chromium.googlesource.com': 'one', |
1314 'chromium-review.googlesource.com': 'other', | 1328 'chromium-review.googlesource.com': 'other', |
1315 }) | 1329 }) |
1316 self.calls.append( | 1330 self.calls.append( |
1317 ((['ask_for_data', 'If you know what you are doing, ' | 1331 ((['ask_for_data', 'If you know what you are doing, ' |
1318
'press Enter to continue, Ctrl+C to abort.'],), '')) | 1332
'press Enter to continue, Ctrl+C to abort.'],), '')) |
1319 self.assertIsNone(cl.EnsureAuthenticated(force=False)) | 1333 self.assertIsNone(cl.EnsureAuthenticated(force=False)) |
1320 | 1334 |
1321 def test_gerrit_ensure_authenticated_ok(self): | 1335 def test_gerrit_ensure_authenticated_ok(self): |
1322 cl = self._test_gerrit_ensure_authenticated_common(auth={ | 1336 cl = self._test_gerrit_ensure_authenticated_common(auth={ |
1323 'chromium.googlesource.com': 'same', | 1337 'chromium.googlesource.com': 'same', |
1324 'chromium-review.googlesource.com': 'same', | 1338 'chromium-review.googlesource.com': 'same', |
1325 }) | 1339 }) |
1326 self.assertIsNone(cl.EnsureAuthenticated(force=False)) | 1340 self.assertIsNone(cl.EnsureAuthenticated(force=False)) |
1327 | 1341 |
| 1342 def test_gerrit_ensure_authenticated_skipped(self): |
| 1343 cl = self._test_gerrit_ensure_authenticated_common( |
| 1344 auth={}, skip_auth_check=True) |
| 1345 self.assertIsNone(cl.EnsureAuthenticated(force=False)) |
| 1346 |
1328 def test_cmd_set_commit_rietveld(self): | 1347 def test_cmd_set_commit_rietveld(self): |
1329 self.mock(git_cl._RietveldChangelistImpl, 'SetFlag', | 1348 self.mock(git_cl._RietveldChangelistImpl, 'SetFlag', |
1330 lambda _, f, v: self._mocked_call(['SetFlag', f, v])) | 1349 lambda _, f, v: self._mocked_call(['SetFlag', f, v])) |
1331 self.calls = [ | 1350 self.calls = [ |
1332 ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), | 1351 ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
1333 ((['git', 'config', 'branch.feature.rietveldissue'],), '123'), | 1352 ((['git', 'config', 'branch.feature.rietveldissue'],), '123'), |
1334 ((['git', 'config', 'rietveld.autoupdate'],), ''), | 1353 ((['git', 'config', 'rietveld.autoupdate'],), ''), |
1335 ((['git', 'config', 'rietveld.server'],), ''), | 1354 ((['git', 'config', 'rietveld.server'],), ''), |
1336 ((['git', 'config', 'rietveld.server'],), ''), | 1355 ((['git', 'config', 'rietveld.server'],), ''), |
1337 ((['git', 'config', 'branch.feature.rietveldserver'],), | 1356 ((['git', 'config', 'branch.feature.rietveldserver'],), |
(...skipping 18 matching lines...) Expand all Loading... |
1356 # TODO(tandrii): consider testing just set-commit and set-commit --clear, | 1375 # TODO(tandrii): consider testing just set-commit and set-commit --clear, |
1357 # but without copy-pasting tons of expectations, as modifying them later is | 1376 # but without copy-pasting tons of expectations, as modifying them later is |
1358 # super tedious. | 1377 # super tedious. |
1359 self.assertEqual(0, git_cl.main(['set-commit', '-d'])) | 1378 self.assertEqual(0, git_cl.main(['set-commit', '-d'])) |
1360 | 1379 |
1361 | 1380 |
1362 if __name__ == '__main__': | 1381 if __name__ == '__main__': |
1363 git_cl.logging.basicConfig( | 1382 git_cl.logging.basicConfig( |
1364 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) | 1383 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) |
1365 unittest.main() | 1384 unittest.main() |
OLD | NEW |