Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
pygit2/test/test_tree.py at encoding · libgit2/pygit2 · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
See all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
libgit2
pygit2
Repository navigation
Code
Issues
187
(187)
Pull requests
5
(5)
Discussions
Actions
Projects
Wiki
Security and quality
Insights
More
items
Files
Expand file tree
encoding
Breadcrumbs
pygit2
/
test
/
test_tree.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
234 lines (194 loc) · 8.25 KB
encoding
Breadcrumbs
pygit2
/
test
/
test_tree.py
Copy path
Top
File metadata and controls
Code
Blame
234 lines (194 loc) · 8.25 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# Copyright 2010-2026 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.
import
operator
import
pytest
import
pygit2
from
pygit2
import
Object
,
Repository
,
Tree
from
pygit2
.
enums
import
FileMode
,
ObjectType
from
.
import
utils
TREE_SHA
=
'967fce8df97cc71722d3c2a5930ef3e6f1d27b12'
SUBTREE_SHA
=
'614fd9a3094bf618ea938fffc00e7d1a54f89ad0'
def
assertTreeEntryEqual
(
entry
:
Object
,
sha
:
str
,
name
:
str
,
filemode
:
int
)
->
None
:
assert
entry
.
id
==
sha
assert
entry
.
name
==
name
assert
entry
.
filemode
==
filemode
assert
entry
.
raw_name
==
name
.
encode
(
'utf-8'
)
def
test_read_tree
(
barerepo
:
Repository
)
->
None
:
tree
=
barerepo
[
TREE_SHA
]
assert
isinstance
(
tree
,
Tree
)
with
pytest
.
raises
(
TypeError
):
tree
[()]
# type: ignore
with
pytest
.
raises
(
TypeError
):
tree
/
123
# type: ignore
utils
.
assertRaisesWithArg
(
KeyError
,
'abcd'
,
lambda
:
tree
[
'abcd'
])
utils
.
assertRaisesWithArg
(
IndexError
,
-
4
,
lambda
:
tree
[
-
4
])
utils
.
assertRaisesWithArg
(
IndexError
,
3
,
lambda
:
tree
[
3
])
utils
.
assertRaisesWithArg
(
KeyError
,
'abcd'
,
lambda
:
tree
/
'abcd'
)
assert
3
==
len
(
tree
)
sha
=
'7f129fd57e31e935c6d60a0c794efe4e6927664b'
assert
'a'
in
tree
assertTreeEntryEqual
(
tree
[
0
],
sha
,
'a'
,
0o0100644
)
assertTreeEntryEqual
(
tree
[
-
3
],
sha
,
'a'
,
0o0100644
)
assertTreeEntryEqual
(
tree
[
'a'
],
sha
,
'a'
,
0o0100644
)
assertTreeEntryEqual
(
tree
/
'a'
,
sha
,
'a'
,
0o0100644
)
sha
=
'85f120ee4dac60d0719fd51731e4199aa5a37df6'
assert
'b'
in
tree
assertTreeEntryEqual
(
tree
[
1
],
sha
,
'b'
,
0o0100644
)
assertTreeEntryEqual
(
tree
[
-
2
],
sha
,
'b'
,
0o0100644
)
assertTreeEntryEqual
(
tree
[
'b'
],
sha
,
'b'
,
0o0100644
)
assertTreeEntryEqual
(
tree
/
'b'
,
sha
,
'b'
,
0o0100644
)
sha
=
'297efb891a47de80be0cfe9c639e4b8c9b450989'
assertTreeEntryEqual
(
tree
[
'c/d'
],
sha
,
'd'
,
0o0100644
)
assertTreeEntryEqual
(
tree
/
'c/d'
,
sha
,
'd'
,
0o0100644
)
assertTreeEntryEqual
(
tree
/
'c'
/
'd'
,
sha
,
'd'
,
0o0100644
)
# type: ignore[operator]
assertTreeEntryEqual
(
tree
[
'c'
][
'd'
],
sha
,
'd'
,
0o0100644
)
# type: ignore[index]
assertTreeEntryEqual
((
tree
/
'c'
)[
'd'
],
sha
,
'd'
,
0o0100644
)
# type: ignore[index]
utils
.
assertRaisesWithArg
(
KeyError
,
'ab/cd'
,
lambda
:
tree
[
'ab/cd'
])
utils
.
assertRaisesWithArg
(
KeyError
,
'ab/cd'
,
lambda
:
tree
/
'ab/cd'
)
utils
.
assertRaisesWithArg
(
KeyError
,
'ab'
,
lambda
:
tree
/
'c'
/
'ab'
)
# type: ignore[operator]
with
pytest
.
raises
(
TypeError
):
tree
/
'a'
/
'cd'
# type: ignore
def
test_equality
(
barerepo
:
Repository
)
->
None
:
tree_a
=
barerepo
[
'18e2d2e9db075f9eb43bcb2daa65a2867d29a15e'
]
tree_b
=
barerepo
[
'2ad1d3456c5c4a1c9e40aeeddb9cd20b409623c8'
]
assert
isinstance
(
tree_a
,
Tree
)
assert
isinstance
(
tree_b
,
Tree
)
assert
tree_a
[
'a'
]
!=
tree_b
[
'a'
]
assert
tree_a
[
'a'
]
!=
tree_b
[
'b'
]
assert
tree_a
[
'b'
]
==
tree_b
[
'b'
]
def
test_sorting
(
barerepo
:
Repository
)
->
None
:
tree_a
=
barerepo
[
'18e2d2e9db075f9eb43bcb2daa65a2867d29a15e'
]
assert
isinstance
(
tree_a
,
Tree
)
assert
list
(
tree_a
)
==
sorted
(
reversed
(
list
(
tree_a
)),
key
=
pygit2
.
tree_entry_key
)
assert
list
(
tree_a
)
!=
reversed
(
list
(
tree_a
))
def
test_read_subtree
(
barerepo
:
Repository
)
->
None
:
tree
=
barerepo
[
TREE_SHA
]
assert
isinstance
(
tree
,
Tree
)
subtree_entry
=
tree
[
'c'
]
assertTreeEntryEqual
(
subtree_entry
,
SUBTREE_SHA
,
'c'
,
0o0040000
)
assert
subtree_entry
.
type
==
int
(
ObjectType
.
TREE
)
assert
subtree_entry
.
type_str
==
'tree'
subtree_entry
=
tree
/
'c'
assertTreeEntryEqual
(
subtree_entry
,
SUBTREE_SHA
,
'c'
,
0o0040000
)
assert
subtree_entry
.
type
==
int
(
ObjectType
.
TREE
)
assert
subtree_entry
.
type_str
==
'tree'
subtree
=
barerepo
[
subtree_entry
.
id
]
assert
isinstance
(
subtree
,
Tree
)
assert
1
==
len
(
subtree
)
sha
=
'297efb891a47de80be0cfe9c639e4b8c9b450989'
assertTreeEntryEqual
(
subtree
[
0
],
sha
,
'd'
,
0o0100644
)
subtree_entry
=
tree
/
'c'
assert
subtree_entry
==
barerepo
[
subtree_entry
.
id
]
def
test_new_tree
(
barerepo
:
Repository
)
->
None
:
repo
=
barerepo
b0
=
repo
.
create_blob
(
'1'
)
b1
=
repo
.
create_blob
(
'2'
)
st
=
repo
.
TreeBuilder
()
st
.
insert
(
'a'
,
b0
,
0o0100644
)
subtree
=
repo
[
st
.
write
()]
t
=
repo
.
TreeBuilder
()
t
.
insert
(
'x'
,
b0
,
0o0100644
)
t
.
insert
(
'y'
,
b1
,
0o0100755
)
t
.
insert
(
'z'
,
subtree
.
id
,
FileMode
.
TREE
)
tree
=
repo
[
t
.
write
()]
for
name
,
oid
,
cls
,
filemode
,
type
,
type_str
in
[
(
'x'
,
b0
,
pygit2
.
Blob
,
FileMode
.
BLOB
,
ObjectType
.
BLOB
,
'blob'
),
(
'y'
,
b1
,
pygit2
.
Blob
,
FileMode
.
BLOB_EXECUTABLE
,
ObjectType
.
BLOB
,
'blob'
),
(
'z'
,
subtree
.
id
,
pygit2
.
Tree
,
FileMode
.
TREE
,
ObjectType
.
TREE
,
'tree'
),
]:
assert
name
in
tree
# type: ignore[operator]
obj
=
tree
[
name
]
# type: ignore[index]
assert
isinstance
(
obj
,
cls
)
assert
obj
.
name
==
name
assert
obj
.
filemode
==
filemode
assert
obj
.
type
==
type
assert
obj
.
type_str
==
type_str
assert
repo
[
obj
.
id
].
id
==
oid
assert
obj
==
repo
[
obj
.
id
]
obj
=
tree
/
name
# type: ignore[operator]
assert
isinstance
(
obj
,
cls
)
assert
obj
.
name
==
name
assert
obj
.
filemode
==
filemode
assert
obj
.
type
==
type
assert
obj
.
type_str
==
type_str
assert
repo
[
obj
.
id
].
id
==
oid
assert
obj
==
repo
[
obj
.
id
]
def
test_modify_tree
(
barerepo
:
Repository
)
->
None
:
tree
=
barerepo
[
TREE_SHA
]
with
pytest
.
raises
(
TypeError
):
operator
.
setitem
(
'c'
,
tree
[
'a'
])
# type: ignore
with
pytest
.
raises
(
TypeError
):
operator
.
delitem
(
'c'
)
# type: ignore
def
test_iterate_tree
(
barerepo
:
Repository
)
->
None
:
"""
Testing that we're able to iterate of a Tree object and that the
resulting sha strings are consistent with the sha strings we could
get with other Tree access methods.
"""
tree
=
barerepo
[
TREE_SHA
]
assert
isinstance
(
tree
,
Tree
)
for
tree_entry
in
tree
:
assert
tree_entry
.
name
is
not
None
assert
tree_entry
==
tree
[
tree_entry
.
name
]
def
test_iterate_tree_nested
(
barerepo
:
Repository
)
->
None
:
"""
Testing that we're able to iterate of a Tree object and then iterate
trees we receive as a result.
"""
tree
=
barerepo
[
TREE_SHA
]
assert
isinstance
(
tree
,
Tree
)
for
tree_entry
in
tree
:
if
isinstance
(
tree_entry
,
pygit2
.
Tree
):
for
tree_entry2
in
tree_entry
:
pass
def
test_deep_contains
(
barerepo
:
Repository
)
->
None
:
tree
=
barerepo
[
TREE_SHA
]
assert
isinstance
(
tree
,
Tree
)
assert
'a'
in
tree
assert
'c'
in
tree
assert
'c/d'
in
tree
assert
'c/e'
not
in
tree
assert
'd'
not
in
tree
assert
isinstance
(
tree
[
'c'
],
Tree
)
assert
'd'
in
tree
[
'c'
]
assert
'e'
not
in
tree
[
'c'
]
@
pytest
.
mark
.
parametrize
(
'name'
,
[
'café.txt'
,
# NFC
'café.txt'
,
# NFD
b'caf
\xc3
\xa9
.txt'
,
# NFC as raw UTF-8 bytes
],
)
def
test_tree_non_ascii_name
(
barerepo
:
Repository
,
name
:
str
|
bytes
)
->
None
:
"""Tree lookups must work for non-ASCII names, str or bytes."""
oid
=
barerepo
.
create_blob
(
b'hello'
)
bld
=
barerepo
.
TreeBuilder
()
bld
.
insert
(
name
,
oid
,
FileMode
.
BLOB
)
tree
=
barerepo
[
bld
.
write
()]
assert
isinstance
(
tree
,
Tree
)
assert
name
in
tree
assert
tree
[
name
].
id
==
oid
assert
(
tree
/
name
).
id
==
oid
You can’t perform that action at this time.