Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork966
Expand file tree
/
Copy pathtest_base.py
More file actions
144 lines (119 loc) · 5.65 KB
/
test_base.py
File metadata and controls
144 lines (119 loc) · 5.65 KB
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
# Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors
#
# This module is part of GitPython and is released under the
# 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/
importgc
importos
importos.pathasosp
importsys
importtempfile
fromunittestimportskipIf
fromgitimportRepo
fromgit.objectsimportBlob,Commit,TagObject,Tree
importgit.objects.baseasbase
fromgit.objects.utilimportget_object_type_by_name
fromgit.utilimportHIDE_WINDOWS_FREEZE_ERRORS,hex_to_bin
fromtest.libimportTestBaseas_TestBase,with_rw_and_rw_remote_repo,with_rw_repo
classTestBase(_TestBase):
deftearDown(self):
gc.collect()
type_tuples= (
("blob","8741fc1d09d61f02ffd8cded15ff603eff1ec070","blob.py"),
("tree","3a6a5e3eeed3723c09f1ef0399f81ed6b8d82e79","directory"),
("commit","4251bd59fb8e11e40c40548cba38180a9536118c",None),
("tag","e56a60e8e9cd333cfba0140a77cd12b0d9398f10",None),
)
deftest_base_object(self):
# Test interface of base object classes.
types= (Blob,Tree,Commit,TagObject)
self.assertEqual(len(types),len(self.type_tuples))
s=set()
num_objs=0
num_index_objs=0
forobj_type, (typename,hexsha,path)inzip(types,self.type_tuples):
binsha=hex_to_bin(hexsha)
item=None
ifpathisNone:
item=obj_type(self.rorepo,binsha)
else:
item=obj_type(self.rorepo,binsha,0,path)
# END handle index objects
num_objs+=1
self.assertEqual(item.hexsha,hexsha)
self.assertEqual(item.type,typename)
assertitem.size
self.assertEqual(item,item)
self.assertNotEqual(notitem,item)
self.assertEqual(str(item),item.hexsha)
assertrepr(item)
s.add(item)
ifisinstance(item,base.IndexObject):
num_index_objs+=1
ifhasattr(item,"path"):# Never runs here.
assertnotitem.path.startswith("/")# Must be relative.
assertisinstance(item.mode,int)
# END index object check
# Read from stream.
data_stream=item.data_stream
data=data_stream.read()
assertdata
withtempfile.NamedTemporaryFile(suffix="test-stream",delete=False)astmpfile:
self.assertEqual(item,item.stream_data(tmpfile))
tmpfile.seek(0)
self.assertEqual(tmpfile.read(),data)
# Remove the file this way, instead of with a context manager or "finally",
# so it is only removed on success, and we can inspect the file on failure.
os.remove(tmpfile.name)
# END for each object type to create
# Each has a unique sha.
self.assertEqual(len(s),num_objs)
self.assertEqual(len(s|s),num_objs)
self.assertEqual(num_index_objs,2)
deftest_get_object_type_by_name(self):
fortnameinbase.Object.TYPES:
assertbase.Objectinget_object_type_by_name(tname).mro()
# END for each known type
self.assertRaises(ValueError,get_object_type_by_name,b"doesntexist")
deftest_object_resolution(self):
# Objects must be resolved to shas so they compare equal.
self.assertEqual(self.rorepo.head.reference.object,self.rorepo.active_branch.object)
@with_rw_repo("HEAD",bare=True)
deftest_with_bare_rw_repo(self,bare_rw_repo:Repo):
assertbare_rw_repo.config_reader("repository").getboolean("core","bare")
assertosp.isfile(osp.join(bare_rw_repo.git_dir,"HEAD"))
assertosp.isdir(bare_rw_repo.working_dir)
assertbare_rw_repo.working_tree_dirisNone
@with_rw_repo("0.1.6")
deftest_with_rw_repo(self,rw_repo:Repo):
assertnotrw_repo.config_reader("repository").getboolean("core","bare")
assertosp.isdir(rw_repo.working_tree_dir)
assertosp.isdir(osp.join(rw_repo.working_tree_dir,"lib"))
assertosp.isdir(rw_repo.working_dir)
@skipIf(HIDE_WINDOWS_FREEZE_ERRORS,"FIXME: Freezes! sometimes...")
@with_rw_and_rw_remote_repo("0.1.6")
deftest_with_rw_remote_and_rw_repo(self,rw_repo,rw_remote_repo):
assertnotrw_repo.config_reader("repository").getboolean("core","bare")
assertrw_remote_repo.config_reader("repository").getboolean("core","bare")
assertosp.isdir(osp.join(rw_repo.working_tree_dir,"lib"))
@with_rw_repo("0.1.6")
deftest_add_unicode(self,rw_repo):
filename="שלום.txt"
file_path=osp.join(rw_repo.working_dir,filename)
# Verify first that we could encode file name in this environment.
try:
file_path.encode(sys.getfilesystemencoding())
exceptUnicodeEncodeErrorase:
raiseRuntimeError("Environment doesn't support unicode filenames")frome
withopen(file_path,"wb")asfp:
fp.write(b"something")
ifsys.platform=="win32":
# On Windows, there is no way this works, see images on:
# https://github.com/gitpython-developers/GitPython/issues/147#issuecomment-68881897
# Therefore, it must be added using the Python implementation.
rw_repo.index.add([file_path])
# However, when the test winds down, rmtree fails to delete this file, which
# is recognized as ??? only.
else:
# On POSIX, we can just add Unicode files without problems.
rw_repo.git.add(rw_repo.working_dir)
rw_repo.index.commit("message")