forked from duplys/git-issues
-
Notifications
You must be signed in to change notification settings - Fork 0
/
t_gitshelve.py
208 lines (162 loc) · 5.96 KB
/
t_gitshelve.py
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
# -*- coding: utf-8 -*-
import sys
import re
import os
import shutil
import unittest
import gitshelve
import exceptions
try:
from cStringIO import StringIO
except:
from StringIO import StringIO
class t_gitshelve(unittest.TestCase):
def setUp(self):
try: gitshelve.git('branch', '-D', 'test')
except: pass
def tearDown(self):
try: gitshelve.git('branch', '-D', 'test')
except: pass
def testBasicInsertion(self):
shelf = gitshelve.open('test')
text = "Hello, this is a test\n"
shelf['foo/bar/baz.c'] = text
self.assertEqual(text, shelf['foo/bar/baz.c'])
def foo1(shelf):
return shelf['foo/bar']
self.assertRaises(exceptions.KeyError, foo1, shelf)
del shelf
def testBasicDeletion(self):
shelf = gitshelve.open('test')
text = "Hello, this is a test\n"
shelf['foo/bar/baz.c'] = text
del shelf['foo/bar/baz.c']
def foo2(shelf):
return shelf['foo/bar/baz.c']
self.assertRaises(exceptions.KeyError, foo2, shelf)
shelf['foo/bar/baz.c'] = text
del shelf['foo/bar']
def foo4(shelf):
return shelf['foo/bar/baz.c']
self.assertRaises(exceptions.KeyError, foo4, shelf)
del shelf
def testInsertion(self):
shelf = gitshelve.open('test')
text = "Hello, this is a test\n"
shelf['foo/bar/baz.c'] = text
buffer = StringIO()
shelf.dump_objects(buffer)
self.assertEqual(buffer.getvalue(), """tree: foo
tree: bar
blob: baz.c
""")
hash1 = shelf.commit('first\n')
hash2 = shelf.commit('second\n')
self.assertEqual(hash1, hash2)
buffer = StringIO()
shelf.dump_objects(buffer)
self.assertEqual("""tree ca37be3e31987d8ece35001301c0b8f1fccbb888
tree 95b790693f3b5934c63d10b8b007e4758f6134a9: foo
tree c03cdd65fa74c272bed2e9a48e3ed19402576e19: bar
blob ea93d5cc5f34e13d2a55a5866b75e2c58993d253: baz.c
""", buffer.getvalue())
hash3 = shelf.current_head()
self.assertEqual(hash1, hash3)
commit = gitshelve.git('cat-file', 'commit', 'test',
keep_newline = True)
self.assert_(re.search('first\n$', commit))
data = gitshelve.git('cat-file', 'blob', 'test:foo/bar/baz.c',
keep_newline = True)
self.assertEqual(text, data)
del shelf
shelf = gitshelve.open('test')
self.assertEqual("""tree ca37be3e31987d8ece35001301c0b8f1fccbb888
tree 95b790693f3b5934c63d10b8b007e4758f6134a9: foo
tree c03cdd65fa74c272bed2e9a48e3ed19402576e19: bar
blob ea93d5cc5f34e13d2a55a5866b75e2c58993d253: baz.c
""", buffer.getvalue())
self.assertEqual(text, shelf['foo/bar/baz.c'])
del shelf
def testIterator(self):
shelf = gitshelve.open('test')
text = "Hello, this is a test\n"
shelf['foo/bar/baz1.c'] = text
shelf['alpha/beta/baz2.c'] = text
shelf['apple/orange/baz3.c'] = text
buffer = StringIO()
keys = shelf.keys()
keys.sort()
for path in keys:
buffer.write("path: (%s)\n" % path)
self.assertEqual("""path: (alpha/beta/baz2.c)
path: (apple/orange/baz3.c)
path: (foo/bar/baz1.c)
""", buffer.getvalue())
def testVersioning(self):
shelf = gitshelve.open('test')
text = "Hello, this is a test\n"
shelf['foo/bar/baz1.c'] = text
shelf.sync()
buffer = StringIO()
shelf.dump_objects(buffer)
self.assertEqual("""tree 073629aeb0ef56a50a6cfcaf56da9b8393604b56
tree ce9d91f2da4ab3aa920cd5763be48b9aef76f999: foo
tree 2e626f2ae629ea77618e84e79e1bfae1c473452e: bar
blob ea93d5cc5f34e13d2a55a5866b75e2c58993d253: baz1.c
""", buffer.getvalue())
text = "Hello, this is a change\n"
shelf['foo/bar/baz1.c'] = text
shelf['foo/bar/baz2.c'] = text
shelf.sync()
buffer = StringIO()
shelf.dump_objects(buffer)
self.assertEqual("""tree c7c6fd4368460c645d0953349d5577d32f46115a
tree 3936ea8daffe9eef0451b43205d6530374f8ffa3: foo
tree 8f7bfca3bc33c93fb1a878bc79c2bb93d8f41730: bar
blob fb54a7573d864d4b57ffcc8af37e7565e2ba4608: baz1.c
blob fb54a7573d864d4b57ffcc8af37e7565e2ba4608: baz2.c
""", buffer.getvalue())
del shelf
shelf = gitshelve.open('test')
buffer = StringIO()
shelf.dump_objects(buffer)
self.assertEqual("""tree 3936ea8daffe9eef0451b43205d6530374f8ffa3: foo
tree 8f7bfca3bc33c93fb1a878bc79c2bb93d8f41730: bar
blob fb54a7573d864d4b57ffcc8af37e7565e2ba4608: baz1.c
blob fb54a7573d864d4b57ffcc8af37e7565e2ba4608: baz2.c
""", buffer.getvalue())
self.assertEqual(text, shelf['foo/bar/baz1.c'])
self.assertEqual(text, shelf['foo/bar/baz2.c'])
log = gitshelve.git('log', 'test', keep_newline = True)
self.assert_(re.match("""commit [0-9a-f]{40}
Author: .+
Date: .+
commit [0-9a-f]{40}
Author: .+
Date: .+
""", log))
def testDetachedRepo(self):
shelf = gitshelve.open(repository = '/tmp/repo-test')
text = "Hello, world!\n"
shelf['foo.txt'] = text
try:
shelf.sync()
gitshelve.git('clone', '/tmp/repo-test', '/tmp/repo-test-clone')
try:
self.assert_(os.path.isfile('/tmp/repo-test-clone/foo.txt'))
data = open('/tmp/repo-test-clone/foo.txt')
try:
self.assertEqual(text, data.read())
finally:
data.close()
finally:
if os.path.isdir('/tmp/repo-test-clone'):
shutil.rmtree('/tmp/repo-test-clone')
finally:
del shelf
if os.path.isdir('/tmp/repo-test'):
shutil.rmtree('/tmp/repo-test')
def suite():
return unittest.TestLoader().loadTestsFromTestCase(t_gitshelve)
if __name__ == '__main__':
unittest.main()