|
| 1 | +# Copyright 2010-2020 The pygit2 contributors |
| 2 | +# |
| 3 | +# This file is free software; you can redistribute it and/or modify |
| 4 | +# it under the terms of the GNU General Public License, version 2, |
| 5 | +# as published by the Free Software Foundation. |
| 6 | +# |
| 7 | +# In addition to the permissions in the GNU General Public License, |
| 8 | +# the authors give you unlimited permission to link the compiled |
| 9 | +# version of this file into combinations with other programs, |
| 10 | +# and to distribute those combinations without any restriction |
| 11 | +# coming from the use of this file. (The General Public License |
| 12 | +# restrictions do apply in other respects; for example, they cover |
| 13 | +# modification of the file, and distribution when not linked into |
| 14 | +# a combined executable.) |
| 15 | +# |
| 16 | +# This file is distributed in the hope that it will be useful, but |
| 17 | +# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 19 | +# General Public License for more details. |
| 20 | +# |
| 21 | +# You should have received a copy of the GNU General Public License |
| 22 | +# along with this program; see the file COPYING. If not, write to |
| 23 | +# the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
| 24 | +# Boston, MA 02110-1301, USA. |
| 25 | + |
| 26 | +import pygit2 |
| 27 | +import pytest |
| 28 | + |
| 29 | + |
| 30 | +ORIGIN_MASTER_COMMIT = '784855caf26449a1914d2cf62d12b9374d76ae78' |
| 31 | + |
| 32 | + |
| 33 | +@pytest.fixture |
| 34 | +def repo(emptyrepo): |
| 35 | + remote = emptyrepo.remotes[0] |
| 36 | + remote.fetch() |
| 37 | + yield emptyrepo |
| 38 | + |
| 39 | + |
| 40 | +def test_branches_remote_get(repo): |
| 41 | + branch = repo.branches.remote.get('origin/master') |
| 42 | + assert branch.target.hex == ORIGIN_MASTER_COMMIT |
| 43 | + |
| 44 | + assert repo.branches.remote.get('origin/not-exists') is None |
| 45 | + |
| 46 | +def test_branches_remote(repo): |
| 47 | + branches = sorted(repo.branches.remote) |
| 48 | + assert branches == ['origin/master'] |
| 49 | + |
| 50 | +def test_branches_remote_getitem(repo): |
| 51 | + branch = repo.branches.remote['origin/master'] |
| 52 | + assert branch.remote_name == 'origin' |
| 53 | + |
| 54 | +def test_branches_upstream(repo): |
| 55 | + remote_master = repo.branches.remote['origin/master'] |
| 56 | + master = repo.branches.create('master', repo[remote_master.target.hex]) |
| 57 | + |
| 58 | + assert master.upstream is None |
| 59 | + master.upstream = remote_master |
| 60 | + assert master.upstream.branch_name == 'origin/master' |
| 61 | + |
| 62 | + def set_bad_upstream(): |
| 63 | + master.upstream = 2.5 |
| 64 | + |
| 65 | + with pytest.raises(TypeError): set_bad_upstream() |
| 66 | + |
| 67 | + master.upstream = None |
| 68 | + assert master.upstream is None |
| 69 | + |
| 70 | + |
| 71 | +def test_branches_upstream_name(repo): |
| 72 | + remote_master = repo.branches.remote['origin/master'] |
| 73 | + master = repo.branches.create('master', |
| 74 | + repo[remote_master.target.hex]) |
| 75 | + |
| 76 | + master.upstream = remote_master |
| 77 | + assert master.upstream_name == 'refs/remotes/origin/master' |
| 78 | + |
| 79 | + |
| 80 | +# |
| 81 | +# Low level API written in C, repo.remotes call these. |
| 82 | +# |
| 83 | + |
| 84 | +def test_lookup_branch_remote(repo): |
| 85 | + branch = repo.lookup_branch('origin/master', pygit2.GIT_BRANCH_REMOTE) |
| 86 | + assert branch.target.hex == ORIGIN_MASTER_COMMIT |
| 87 | + |
| 88 | + assert repo.lookup_branch('origin/not-exists', pygit2.GIT_BRANCH_REMOTE) is None |
| 89 | + |
| 90 | +def test_listall_branches(repo): |
| 91 | + branches = sorted(repo.listall_branches(pygit2.GIT_BRANCH_REMOTE)) |
| 92 | + assert branches == ['origin/master'] |
| 93 | + |
| 94 | +def test_branch_remote_name(repo): |
| 95 | + branch = repo.lookup_branch('origin/master', pygit2.GIT_BRANCH_REMOTE) |
| 96 | + assert branch.remote_name == 'origin' |
| 97 | + |
| 98 | +def test_branch_upstream(repo): |
| 99 | + remote_master = repo.lookup_branch('origin/master', pygit2.GIT_BRANCH_REMOTE) |
| 100 | + master = repo.create_branch('master', repo[remote_master.target.hex]) |
| 101 | + |
| 102 | + assert master.upstream is None |
| 103 | + master.upstream = remote_master |
| 104 | + assert master.upstream.branch_name == 'origin/master' |
| 105 | + |
| 106 | + def set_bad_upstream(): |
| 107 | + master.upstream = 2.5 |
| 108 | + |
| 109 | + with pytest.raises(TypeError): set_bad_upstream() |
| 110 | + |
| 111 | + master.upstream = None |
| 112 | + assert master.upstream is None |
| 113 | + |
| 114 | +def test_branch_upstream_name(repo): |
| 115 | + remote_master = repo.lookup_branch('origin/master', pygit2.GIT_BRANCH_REMOTE) |
| 116 | + master = repo.create_branch('master', repo[remote_master.target.hex]) |
| 117 | + |
| 118 | + master.upstream = remote_master |
| 119 | + assert master.upstream_name == 'refs/remotes/origin/master' |
0 commit comments