File: test_object.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 (136 lines) | stat: -rw-r--r-- 4,507 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# 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 Object objects."""

import pytest

from pygit2 import GIT_OBJ_TREE, GIT_OBJ_TAG, Tree, Tag


BLOB_SHA = 'a520c24d85fbfc815d385957eed41406ca5a860b'
BLOB_CONTENT = """hello world
hola mundo
bonjour le monde
""".encode()
BLOB_NEW_CONTENT = b'foo bar\n'
BLOB_FILE_CONTENT = b'bye world\n'


def test_equality(testrepo):
    # get a commit object twice and see if it equals ittestrepo
    commit_id = testrepo.lookup_reference('refs/heads/master').target
    commit_a = testrepo[commit_id]
    commit_b = testrepo[commit_id]

    assert commit_a is not commit_b
    assert commit_a == commit_b
    assert not(commit_a != commit_b)

def test_hashing(testrepo):
    # get a commit object twice and compare hashes
    commit_id = testrepo.lookup_reference('refs/heads/master').target
    commit_a = testrepo[commit_id]
    commit_b = testrepo[commit_id]

    assert hash(commit_a)

    assert commit_a is not commit_b
    assert commit_a == commit_b
    # if the commits are equal then their hash *must* be equal
    # but different objects can have the same commit
    assert hash(commit_a) == hash(commit_b)

    # sanity check that python container types work as expected
    s = set()
    s.add(commit_a)
    s.add(commit_b)
    assert len(s) == 1
    assert commit_a in s
    assert commit_b in s

    d = {}
    d[commit_a] = True
    assert commit_b in d
    assert d[commit_b]

    l = [commit_a]
    assert commit_b in l

def test_peel_commit(testrepo):
    # start by looking up the commit
    commit_id = testrepo.lookup_reference('refs/heads/master').target
    commit = testrepo[commit_id]
    # and peel to the tree
    tree = commit.peel(GIT_OBJ_TREE)

    assert type(tree) == Tree
    assert str(tree.id) == 'fd937514cb799514d4b81bb24c5fcfeb6472b245'

def test_peel_commit_type(testrepo):
    commit_id = testrepo.lookup_reference('refs/heads/master').target
    commit = testrepo[commit_id]
    tree = commit.peel(Tree)

    assert type(tree) == Tree
    assert str(tree.id) == 'fd937514cb799514d4b81bb24c5fcfeb6472b245'


def test_invalid(testrepo):
    commit_id = testrepo.lookup_reference('refs/heads/master').target
    commit = testrepo[commit_id]

    with pytest.raises(ValueError): commit.peel(GIT_OBJ_TAG)

def test_invalid_type(testrepo):
    commit_id = testrepo.lookup_reference('refs/heads/master').target
    commit = testrepo[commit_id]

    with pytest.raises(ValueError): commit.peel(Tag)

def test_short_id(testrepo):
    seen = {} # from short_id to full hex id
    def test_obj(obj, msg):
        short_id = obj.short_id
        msg = msg+" short_id="+short_id
        already = seen.get(short_id)
        if already:
            assert already == obj.id.hex
        else:
            seen[short_id] = obj.id.hex
            lookup = testrepo[short_id]
            assert obj.id == lookup.id
    for commit in testrepo.walk(testrepo.head.target):
        test_obj(commit, "commit#"+commit.id.hex)
        tree = commit.tree
        test_obj(tree, "tree#"+tree.id.hex)
        for entry in tree:
            test_obj(testrepo[entry.hex], "entry="+entry.name+"#"+entry.hex)


def test_repr(testrepo):
    commit_id = testrepo.lookup_reference('refs/heads/master').target
    commit_a = testrepo[commit_id]
    assert repr(commit_a) == "<pygit2.Object{commit:%s}>" % commit_id