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 553 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
564 'log', '--pretty=format:%s%n%n%b', 'fake_ancestor_sha...'],), | 564 'log', '--pretty=format:%s%n%n%b', 'fake_ancestor_sha...'],), |
565 'foo'), | 565 'foo'), |
566 ((['git', 'config', 'user.email'],), 'me@example.com'), | 566 ((['git', 'config', 'user.email'],), 'me@example.com'), |
567 ((['git', | 567 ((['git', |
568 'diff', '--no-ext-diff', '--stat', '--find-copies-harder', | 568 'diff', '--no-ext-diff', '--stat', '--find-copies-harder', |
569 '-l100000', '-C50', 'fake_ancestor_sha', 'HEAD'],), | 569 '-l100000', '-C50', 'fake_ancestor_sha', 'HEAD'],), |
570 '+dat'), | 570 '+dat'), |
571 ] | 571 ] |
572 | 572 |
573 @staticmethod | 573 @staticmethod |
574 def _gerrit_upload_calls(description, reviewers): | 574 def _gerrit_upload_calls(description, reviewers, squash): |
575 calls = [ | 575 calls = [ |
576 ((['git', 'config', 'gerrit.host'],), | 576 ((['git', 'config', 'gerrit.host'],), |
577 'gerrit.example.com'), | 577 'gerrit.example.com'), |
578 ((['git', 'log', '--pretty=format:%s\n\n%b', | 578 ((['git', 'log', '--pretty=format:%s\n\n%b', |
579 'fake_ancestor_sha..HEAD'],), | 579 'fake_ancestor_sha..HEAD'],), |
580 description) | 580 description) |
581 ] | 581 ] |
582 if git_cl.CHANGE_ID not in description: | 582 if git_cl.CHANGE_ID not in description: |
583 calls += [ | 583 calls += [ |
584 ((['git', 'log', '--pretty=format:%s\n\n%b', | 584 ((['git', 'log', '--pretty=format:%s\n\n%b', |
585 'fake_ancestor_sha..HEAD'],), | 585 'fake_ancestor_sha..HEAD'],), |
586 description), | 586 description), |
587 ((['git', 'commit', '--amend', '-m', description],), | 587 ((['git', 'commit', '--amend', '-m', description],), |
588 ''), | 588 ''), |
589 ((['git', 'log', '--pretty=format:%s\n\n%b', | 589 ((['git', 'log', '--pretty=format:%s\n\n%b', |
590 'fake_ancestor_sha..HEAD'],), | 590 'fake_ancestor_sha..HEAD'],), |
591 description) | 591 description) |
592 ] | 592 ] |
| 593 if squash: |
| 594 ref_to_push = 'abcdef0123456789' |
| 595 calls += [ |
| 596 ((['git', 'show', '--format=%s\n\n%b', '-s', |
| 597 'refs/heads/git_cl_uploads/master'],), |
| 598 (description, 0)), |
| 599 ((['git', 'config', 'branch.master.merge'],), |
| 600 'refs/heads/master'), |
| 601 ((['git', 'config', 'branch.master.remote'],), |
| 602 'origin'), |
| 603 ((['get_or_create_merge_base', 'master', 'master'],), |
| 604 'origin/master'), |
| 605 ((['git', 'rev-parse', 'HEAD:'],), |
| 606 '0123456789abcdef'), |
| 607 ((['git', 'commit-tree', '0123456789abcdef', '-p', |
| 608 'origin/master', '-m', 'd'],), |
| 609 ref_to_push), |
| 610 ] |
| 611 else: |
| 612 ref_to_push = 'HEAD' |
| 613 |
593 calls += [ | 614 calls += [ |
594 ((['git', 'rev-list', 'origin/master..'],), ''), | 615 ((['git', 'rev-list', 'origin/master..' + ref_to_push],), ''), |
595 ((['git', 'config', 'rietveld.cc'],), '') | 616 ((['git', 'config', 'rietveld.cc'],), '') |
596 ] | 617 ] |
597 receive_pack = '--receive-pack=git receive-pack ' | 618 receive_pack = '--receive-pack=git receive-pack ' |
598 receive_pack += '--cc=joe@example.com' # from watch list | 619 receive_pack += '--cc=joe@example.com' # from watch list |
599 if reviewers: | 620 if reviewers: |
600 receive_pack += ' ' | 621 receive_pack += ' ' |
601 receive_pack += ' '.join( | 622 receive_pack += ' '.join( |
602 '--reviewer=' + email for email in sorted(reviewers)) | 623 '--reviewer=' + email for email in sorted(reviewers)) |
603 receive_pack += '' | 624 receive_pack += '' |
604 calls += [ | 625 calls += [ |
605 ((['git', | 626 ((['git', |
606 'push', receive_pack, 'origin', 'HEAD:refs/for/master'],), | 627 'push', receive_pack, 'origin', ref_to_push + ':refs/for/master'],), |
607 '') | 628 '') |
608 ] | 629 ] |
| 630 if squash: |
| 631 calls += [ |
| 632 ((['git', 'rev-parse', 'HEAD'],), 'abcdef0123456789'), |
| 633 ((['git', 'update-ref', '-m', 'Uploaded abcdef0123456789', |
| 634 'refs/heads/git_cl_uploads/master', 'abcdef0123456789'],), |
| 635 '') |
| 636 ] |
| 637 |
609 return calls | 638 return calls |
610 | 639 |
611 def _run_gerrit_upload_test( | 640 def _run_gerrit_upload_test( |
612 self, | 641 self, |
613 upload_args, | 642 upload_args, |
614 description, | 643 description, |
615 reviewers): | 644 reviewers, |
| 645 squash=False): |
616 """Generic gerrit upload test framework.""" | 646 """Generic gerrit upload test framework.""" |
617 self.calls = self._gerrit_base_calls() | 647 self.calls = self._gerrit_base_calls() |
618 self.calls += self._gerrit_upload_calls(description, reviewers) | 648 self.calls += self._gerrit_upload_calls(description, reviewers, squash) |
619 git_cl.main(['upload'] + upload_args) | 649 git_cl.main(['upload'] + upload_args) |
620 | 650 |
621 def test_gerrit_upload_without_change_id(self): | 651 def test_gerrit_upload_without_change_id(self): |
622 self._run_gerrit_upload_test( | 652 self._run_gerrit_upload_test( |
623 [], | 653 [], |
624 'desc\n\nBUG=\n', | 654 'desc\n\nBUG=\n', |
625 []) | 655 []) |
626 | 656 |
627 def test_gerrit_no_reviewer(self): | 657 def test_gerrit_no_reviewer(self): |
628 self._run_gerrit_upload_test( | 658 self._run_gerrit_upload_test( |
629 [], | 659 [], |
630 'desc\n\nBUG=\nChange-Id:123456789\n', | 660 'desc\n\nBUG=\nChange-Id:123456789\n', |
631 []) | 661 []) |
632 | 662 |
633 def test_gerrit_reviewers_cmd_line(self): | 663 def test_gerrit_reviewers_cmd_line(self): |
634 self._run_gerrit_upload_test( | 664 self._run_gerrit_upload_test( |
635 ['-r', 'foo@example.com'], | 665 ['-r', 'foo@example.com'], |
636 'desc\n\nBUG=\nChange-Id:123456789', | 666 'desc\n\nBUG=\nChange-Id:123456789', |
637 ['foo@example.com']) | 667 ['foo@example.com']) |
638 | 668 |
639 def test_gerrit_reviewer_multiple(self): | 669 def test_gerrit_reviewer_multiple(self): |
640 self._run_gerrit_upload_test( | 670 self._run_gerrit_upload_test( |
641 [], | 671 [], |
642 'desc\nTBR=reviewer@example.com\nBUG=\nR=another@example.com\n' | 672 'desc\nTBR=reviewer@example.com\nBUG=\nR=another@example.com\n' |
643 'Change-Id:123456789\n', | 673 'Change-Id:123456789\n', |
644 ['reviewer@example.com', 'another@example.com']) | 674 ['reviewer@example.com', 'another@example.com']) |
645 | 675 |
| 676 def test_gerrit_upload_squash(self): |
| 677 self._run_gerrit_upload_test( |
| 678 ['--squash'], |
| 679 'desc\n\nBUG=\nChange-Id:123456789\n', |
| 680 [], |
| 681 squash=True) |
646 | 682 |
647 def test_config_gerrit_download_hook(self): | 683 def test_config_gerrit_download_hook(self): |
648 self.mock(git_cl, 'FindCodereviewSettingsFile', CodereviewSettingsFileMock) | 684 self.mock(git_cl, 'FindCodereviewSettingsFile', CodereviewSettingsFileMock) |
649 def ParseCodereviewSettingsContent(content): | 685 def ParseCodereviewSettingsContent(content): |
650 keyvals = {} | 686 keyvals = {} |
651 keyvals['CODE_REVIEW_SERVER'] = 'gerrit.chromium.org' | 687 keyvals['CODE_REVIEW_SERVER'] = 'gerrit.chromium.org' |
652 keyvals['GERRIT_HOST'] = 'gerrit.chromium.org' | 688 keyvals['GERRIT_HOST'] = 'gerrit.chromium.org' |
653 keyvals['GERRIT_PORT'] = '29418' | 689 keyvals['GERRIT_PORT'] = '29418' |
654 return keyvals | 690 return keyvals |
655 self.mock(git_cl.gclient_utils, 'ParseCodereviewSettingsContent', | 691 self.mock(git_cl.gclient_utils, 'ParseCodereviewSettingsContent', |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
755 obj.update_reviewers(reviewers) | 791 obj.update_reviewers(reviewers) |
756 actual.append(obj.description) | 792 actual.append(obj.description) |
757 self.assertEqual(expected, actual) | 793 self.assertEqual(expected, actual) |
758 | 794 |
759 def test_get_target_ref(self): | 795 def test_get_target_ref(self): |
760 # Check remote or remote branch not present. | 796 # Check remote or remote branch not present. |
761 self.assertEqual(None, git_cl.GetTargetRef('origin', None, 'master', None)) | 797 self.assertEqual(None, git_cl.GetTargetRef('origin', None, 'master', None)) |
762 self.assertEqual(None, git_cl.GetTargetRef(None, | 798 self.assertEqual(None, git_cl.GetTargetRef(None, |
763 'refs/remotes/origin/master', | 799 'refs/remotes/origin/master', |
764 'master', None)) | 800 'master', None)) |
765 | 801 |
766 # Check default target refs for branches. | 802 # Check default target refs for branches. |
767 self.assertEqual('refs/heads/master', | 803 self.assertEqual('refs/heads/master', |
768 git_cl.GetTargetRef('origin', 'refs/remotes/origin/master', | 804 git_cl.GetTargetRef('origin', 'refs/remotes/origin/master', |
769 None, None)) | 805 None, None)) |
770 self.assertEqual('refs/heads/master', | 806 self.assertEqual('refs/heads/master', |
771 git_cl.GetTargetRef('origin', 'refs/remotes/origin/lkgr', | 807 git_cl.GetTargetRef('origin', 'refs/remotes/origin/lkgr', |
772 None, None)) | 808 None, None)) |
773 self.assertEqual('refs/heads/master', | 809 self.assertEqual('refs/heads/master', |
774 git_cl.GetTargetRef('origin', 'refs/remotes/origin/lkcr', | 810 git_cl.GetTargetRef('origin', 'refs/remotes/origin/lkcr', |
775 None, None)) | 811 None, None)) |
(...skipping 28 matching lines...) Expand all Loading... |
804 # Check target refs for pending prefix. | 840 # Check target refs for pending prefix. |
805 self.assertEqual('prefix/heads/master', | 841 self.assertEqual('prefix/heads/master', |
806 git_cl.GetTargetRef('origin', 'refs/remotes/origin/master', | 842 git_cl.GetTargetRef('origin', 'refs/remotes/origin/master', |
807 None, 'prefix/')) | 843 None, 'prefix/')) |
808 | 844 |
809 | 845 |
810 if __name__ == '__main__': | 846 if __name__ == '__main__': |
811 git_cl.logging.basicConfig( | 847 git_cl.logging.basicConfig( |
812 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) | 848 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) |
813 unittest.main() | 849 unittest.main() |
OLD | NEW |