File: test_refdb_backend.py

package info (click to toggle)
python-pygit2 1.4.0%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,328 kB
  • sloc: ansic: 11,016; python: 5,943; sh: 275; makefile: 19
file content (120 lines) | stat: -rw-r--r-- 4,473 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# Copyright 2010-2020 The pygit2 contributors
#
# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2,
# as published by the Free Software Foundation.
#
# In addition to the permissions in the GNU General Public License,
# the authors give you unlimited permission to link the compiled
# version of this file into combinations with other programs,
# and to distribute those combinations without any restriction
# coming from the use of this file.  (The General Public License
# restrictions do apply in other respects; for example, they cover
# modification of the file, and distribution when not linked into
# a combined executable.)
#
# This file is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING.  If not, write to
# the Free Software Foundation, 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.

"""Tests for Refdb objects."""

import os

import pygit2
import pytest


# Note: the refdb abstraction from libgit2 is meant to provide information
# which libgit2 transforms into something more useful, and in general YMMV by
# using the backend directly. So some of these tests are a bit vague or
# incomplete, to avoid hitting the semi-valid states that refdbs produce by
# design.
class ProxyRefdbBackend(pygit2.RefdbBackend):
    def __init__(testrepo, source):
        testrepo.source = source

    def exists(testrepo, ref):
        print(testrepo, testrepo.source, ref)
        return testrepo.source.exists(ref)

    def lookup(testrepo, ref):
        return testrepo.source.lookup(ref)

    def write(testrepo, ref, force, who, message, old, old_target):
        return testrepo.source.write(ref, force, who, message, old, old_target)

    def rename(testrepo, old_name, new_name, force, who, message):
        return testrepo.source.rename(old_name, new_name, force, who, message)

    def delete(testrepo, ref_name, old_id, old_target):
        return testrepo.source.delete(ref_name, old_id, old_target)

    def compress(testrepo):
        return testrepo.source.compress()

    def has_log(testrepo, ref_name):
        return testrepo.source.has_log(ref_name)

    def ensure_log(testrepo, ref_name):
        return testrepo.source.ensure_log(ref_name)

    def __iter__(testrepo):
        return iter(testrepo.source)


@pytest.fixture
def repo(testrepo):
    testrepo.backend = ProxyRefdbBackend(pygit2.RefdbFsBackend(testrepo))
    yield testrepo


def test_exists(repo):
    assert not repo.backend.exists('refs/heads/does-not-exist')
    assert repo.backend.exists('refs/heads/master')

def test_lookup(repo):
    assert repo.backend.lookup('refs/heads/does-not-exist') is None
    assert repo.backend.lookup('refs/heads/master').name == 'refs/heads/master'

def test_write(repo):
    master = repo.backend.lookup('refs/heads/master')
    commit = repo.get(master.target)
    ref = pygit2.Reference("refs/heads/test-write", master.target, None)
    repo.backend.write(ref, False, commit.author,
            "Create test-write", None, None)
    assert repo.backend.lookup("refs/heads/test-write").target == master.target

def test_rename(repo):
    old_ref = repo.backend.lookup('refs/heads/i18n')
    target = repo.get(old_ref.target)
    repo.backend.rename('refs/heads/i18n', 'refs/heads/intl',
            False, target.committer, target.message)
    assert repo.backend.lookup('refs/heads/intl').target == target.id

def test_delete(repo):
    old = repo.backend.lookup('refs/heads/i18n')
    repo.backend.delete('refs/heads/i18n', old.target, None)
    assert not repo.backend.lookup('refs/heads/i18n')

def test_compress(repo):
    repo = repo
    packed_refs_file = os.path.join(repo.path, 'packed-refs')
    assert not os.path.exists(packed_refs_file)
    repo.backend.compress()
    assert os.path.exists(packed_refs_file)

def test_has_log(repo):
    assert repo.backend.has_log('refs/heads/master')
    assert not repo.backend.has_log('refs/heads/does-not-exist')

def test_ensure_log(repo):
    assert not repo.backend.has_log('refs/heads/new-log')
    repo.backend.ensure_log('refs/heads/new-log')
    assert repo.backend.has_log('refs/heads/new-log')