| 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 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 return '~/.gitcookies' | 119 return '~/.gitcookies' |
| 120 @classmethod | 120 @classmethod |
| 121 def get_netrc_path(cls): | 121 def get_netrc_path(cls): |
| 122 return '~/.netrc' | 122 return '~/.netrc' |
| 123 def get_auth_header(self, host): | 123 def get_auth_header(self, host): |
| 124 if same_cookie: | 124 if same_cookie: |
| 125 return same_cookie | 125 return same_cookie |
| 126 return (hosts_with_creds or {}).get(host) | 126 return (hosts_with_creds or {}).get(host) |
| 127 return CookiesAuthenticatorMock | 127 return CookiesAuthenticatorMock |
| 128 | 128 |
| 129 class MockChangelistWithBranchAndIssue(): |
| 130 def __init__(self, branch, issue): |
| 131 self.branch = branch |
| 132 self.issue = issue |
| 133 def GetBranch(self): |
| 134 return self.branch |
| 135 def GetIssue(self): |
| 136 return self.issue |
| 129 | 137 |
| 130 class TestGitClBasic(unittest.TestCase): | 138 class TestGitClBasic(unittest.TestCase): |
| 131 def _test_ParseIssueUrl(self, func, url, issue, patchset, hostname, fail): | 139 def _test_ParseIssueUrl(self, func, url, issue, patchset, hostname, fail): |
| 132 parsed = urlparse.urlparse(url) | 140 parsed = urlparse.urlparse(url) |
| 133 result = func(parsed) | 141 result = func(parsed) |
| 134 if fail: | 142 if fail: |
| 135 self.assertIsNone(result) | 143 self.assertIsNone(result) |
| 136 return None | 144 return None |
| 137 self.assertIsNotNone(result) | 145 self.assertIsNotNone(result) |
| 138 self.assertEqual(result.issue, issue) | 146 self.assertEqual(result.issue, issue) |
| (...skipping 1555 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1694 ((['git', 'config', 'branch.master.rietveldissue'],), '1'), | 1702 ((['git', 'config', 'branch.master.rietveldissue'],), '1'), |
| 1695 ((['git', 'config', 'rietveld.autoupdate'],), CERR1), | 1703 ((['git', 'config', 'rietveld.autoupdate'],), CERR1), |
| 1696 ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'), | 1704 ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'), |
| 1697 ((['git', 'config', 'branch.foo.rietveldissue'],), '456'), | 1705 ((['git', 'config', 'branch.foo.rietveldissue'],), '456'), |
| 1698 ((['git', 'config', 'branch.bar.rietveldissue'],), CERR1), | 1706 ((['git', 'config', 'branch.bar.rietveldissue'],), CERR1), |
| 1699 ((['git', 'config', 'branch.bar.gerritissue'],), '789'), | 1707 ((['git', 'config', 'branch.bar.gerritissue'],), '789'), |
| 1700 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), | 1708 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
| 1701 ((['git', 'tag', 'git-cl-archived-456-foo', 'foo'],), ''), | 1709 ((['git', 'tag', 'git-cl-archived-456-foo', 'foo'],), ''), |
| 1702 ((['git', 'branch', '-D', 'foo'],), '')] | 1710 ((['git', 'branch', '-D', 'foo'],), '')] |
| 1703 | 1711 |
| 1704 class MockChangelist(): | |
| 1705 def __init__(self, branch, issue): | |
| 1706 self.branch = branch | |
| 1707 self.issue = issue | |
| 1708 def GetBranch(self): | |
| 1709 return self.branch | |
| 1710 def GetIssue(self): | |
| 1711 return self.issue | |
| 1712 | |
| 1713 self.mock(git_cl, 'get_cl_statuses', | 1712 self.mock(git_cl, 'get_cl_statuses', |
| 1714 lambda branches, fine_grained, max_processes: | 1713 lambda branches, fine_grained, max_processes: |
| 1715 [(MockChangelist('master', 1), 'open'), | 1714 [(MockChangelistWithBranchAndIssue('master', 1), 'open'), |
| 1716 (MockChangelist('foo', 456), 'closed'), | 1715 (MockChangelistWithBranchAndIssue('foo', 456), 'closed'), |
| 1717 (MockChangelist('bar', 789), 'open')]) | 1716 (MockChangelistWithBranchAndIssue('bar', 789), 'open')]) |
| 1718 | 1717 |
| 1719 self.assertEqual(0, git_cl.main(['archive', '-f'])) | 1718 self.assertEqual(0, git_cl.main(['archive', '-f'])) |
| 1720 | 1719 |
| 1721 def test_archive_current_branch_fails(self): | 1720 def test_archive_current_branch_fails(self): |
| 1722 self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) | 1721 self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
| 1723 self.calls = \ | 1722 self.calls = \ |
| 1724 [((['git', 'for-each-ref', '--format=%(refname)', 'refs/heads'],), | 1723 [((['git', 'for-each-ref', '--format=%(refname)', 'refs/heads'],), |
| 1725 'refs/heads/master'), | 1724 'refs/heads/master'), |
| 1726 ((['git', 'config', 'branch.master.rietveldissue'],), '1'), | 1725 ((['git', 'config', 'branch.master.rietveldissue'],), '1'), |
| 1727 ((['git', 'config', 'rietveld.autoupdate'],), CERR1), | 1726 ((['git', 'config', 'rietveld.autoupdate'],), CERR1), |
| 1728 ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'), | 1727 ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'), |
| 1729 ((['git', 'symbolic-ref', 'HEAD'],), 'master')] | 1728 ((['git', 'symbolic-ref', 'HEAD'],), 'master')] |
| 1730 | 1729 |
| 1731 class MockChangelist(): | 1730 self.mock(git_cl, 'get_cl_statuses', |
| 1732 def __init__(self, branch, issue): | 1731 lambda branches, fine_grained, max_processes: |
| 1733 self.branch = branch | 1732 [(MockChangelistWithBranchAndIssue('master', 1), 'closed')]) |
| 1734 self.issue = issue | 1733 |
| 1735 def GetBranch(self): | 1734 self.assertEqual(1, git_cl.main(['archive', '-f'])) |
| 1736 return self.branch | 1735 |
| 1737 def GetIssue(self): | 1736 def test_archive_dry_run(self): |
| 1738 return self.issue | 1737 self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
| 1738 |
| 1739 self.calls = \ |
| 1740 [((['git', 'for-each-ref', '--format=%(refname)', 'refs/heads'],), |
| 1741 'refs/heads/master\nrefs/heads/foo\nrefs/heads/bar'), |
| 1742 ((['git', 'config', 'branch.master.rietveldissue'],), '1'), |
| 1743 ((['git', 'config', 'rietveld.autoupdate'],), CERR1), |
| 1744 ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'), |
| 1745 ((['git', 'config', 'branch.foo.rietveldissue'],), '456'), |
| 1746 ((['git', 'config', 'branch.bar.rietveldissue'],), CERR1), |
| 1747 ((['git', 'config', 'branch.bar.gerritissue'],), '789'), |
| 1748 ((['git', 'symbolic-ref', 'HEAD'],), 'master'),] |
| 1739 | 1749 |
| 1740 self.mock(git_cl, 'get_cl_statuses', | 1750 self.mock(git_cl, 'get_cl_statuses', |
| 1741 lambda branches, fine_grained, max_processes: | 1751 lambda branches, fine_grained, max_processes: |
| 1742 [(MockChangelist('master', 1), 'closed')]) | 1752 [(MockChangelistWithBranchAndIssue('master', 1), 'open'), |
| 1753 (MockChangelistWithBranchAndIssue('foo', 456), 'closed'), |
| 1754 (MockChangelistWithBranchAndIssue('bar', 789), 'open')]) |
| 1743 | 1755 |
| 1744 self.assertEqual(1, git_cl.main(['archive', '-f'])) | 1756 self.assertEqual(0, git_cl.main(['archive', '-f', '--dry-run'])) |
| 1757 |
| 1758 def test_archive_no_tags(self): |
| 1759 self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
| 1760 |
| 1761 self.calls = \ |
| 1762 [((['git', 'for-each-ref', '--format=%(refname)', 'refs/heads'],), |
| 1763 'refs/heads/master\nrefs/heads/foo\nrefs/heads/bar'), |
| 1764 ((['git', 'config', 'branch.master.rietveldissue'],), '1'), |
| 1765 ((['git', 'config', 'rietveld.autoupdate'],), CERR1), |
| 1766 ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'), |
| 1767 ((['git', 'config', 'branch.foo.rietveldissue'],), '456'), |
| 1768 ((['git', 'config', 'branch.bar.rietveldissue'],), CERR1), |
| 1769 ((['git', 'config', 'branch.bar.gerritissue'],), '789'), |
| 1770 ((['git', 'symbolic-ref', 'HEAD'],), 'master'), |
| 1771 ((['git', 'branch', '-D', 'foo'],), '')] |
| 1772 |
| 1773 self.mock(git_cl, 'get_cl_statuses', |
| 1774 lambda branches, fine_grained, max_processes: |
| 1775 [(MockChangelistWithBranchAndIssue('master', 1), 'open'), |
| 1776 (MockChangelistWithBranchAndIssue('foo', 456), 'closed'), |
| 1777 (MockChangelistWithBranchAndIssue('bar', 789), 'open')]) |
| 1778 |
| 1779 self.assertEqual(0, git_cl.main(['archive', '-f', '--notags'])) |
| 1745 | 1780 |
| 1746 def test_cmd_issue_erase_existing(self): | 1781 def test_cmd_issue_erase_existing(self): |
| 1747 out = StringIO.StringIO() | 1782 out = StringIO.StringIO() |
| 1748 self.mock(git_cl.sys, 'stdout', out) | 1783 self.mock(git_cl.sys, 'stdout', out) |
| 1749 self.calls = [ | 1784 self.calls = [ |
| 1750 ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), | 1785 ((['git', 'symbolic-ref', 'HEAD'],), 'feature'), |
| 1751 ((['git', 'config', 'branch.feature.rietveldissue'],), CERR1), | 1786 ((['git', 'config', 'branch.feature.rietveldissue'],), CERR1), |
| 1752 ((['git', 'config', 'branch.feature.gerritissue'],), '123'), | 1787 ((['git', 'config', 'branch.feature.gerritissue'],), '123'), |
| 1753 # Let this command raise exception (retcode=1) - it should be ignored. | 1788 # Let this command raise exception (retcode=1) - it should be ignored. |
| 1754 ((['git', 'config', '--unset', 'branch.feature.last-upload-hash'],), | 1789 ((['git', 'config', '--unset', 'branch.feature.last-upload-hash'],), |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1857 ((['rm_file_or_tree', '/abs/git_repo_root/.git/hooks/commit-msg'],), | 1892 ((['rm_file_or_tree', '/abs/git_repo_root/.git/hooks/commit-msg'],), |
| 1858 ''), | 1893 ''), |
| 1859 ] | 1894 ] |
| 1860 cl._codereview_impl._GerritCommitMsgHookCheck(offer_removal=True) | 1895 cl._codereview_impl._GerritCommitMsgHookCheck(offer_removal=True) |
| 1861 | 1896 |
| 1862 | 1897 |
| 1863 if __name__ == '__main__': | 1898 if __name__ == '__main__': |
| 1864 git_cl.logging.basicConfig( | 1899 git_cl.logging.basicConfig( |
| 1865 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) | 1900 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) |
| 1866 unittest.main() | 1901 unittest.main() |
| OLD | NEW |