forked from keredson/ygit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_localhost.py
205 lines (178 loc) · 7.47 KB
/
test_localhost.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
import os, sh, shutil, tempfile, io
import ygit
# start test http server with:
# nginx -c "$(pwd)/misc/test_nginx.conf" -e stderr
REPOS_DIR = '/tmp/ygit_test_repos'
if os.path.isdir(REPOS_DIR): shutil.rmtree(REPOS_DIR)
os.mkdir(REPOS_DIR)
def build_repo():
d = tempfile.mkdtemp(dir=REPOS_DIR, suffix='.git', prefix='ygit_')
if os.path.isdir(d): shutil.rmtree(d)
os.mkdir(d)
git = sh.git.bake(C=d)
git.init()
git.config('--bool', 'http.receivepack', 'true')
git.config('--bool', 'receive.denyCurrentBranch', 'false')
print('build_repo', d)
return git, d
def test_clone():
git, d = build_repo()
with open(os.path.join(d,'test.txt'),'w') as f:
f.write('woot!')
git.add('test.txt')
git.commit('test.txt', message='-')
with tempfile.TemporaryDirectory() as td:
ygit.clone('http://localhost:8889/'+os.path.basename(d),td)
assert sorted(os.listdir(td)) == ['.ygit', 'test.txt']
assert sorted([s for s in os.listdir(os.path.join(td,'.ygit')) if not s.endswith('.pack')]) == ['config', 'idx', 'refs']
assert len([s for s in os.listdir(os.path.join(td,'.ygit')) if s.endswith('.pack')]) == 1
with open(os.path.join(td,'test.txt')) as f:
assert f.read()=='woot!'
def test_deep_clone():
git, d = build_repo()
with open(os.path.join(d,'test.txt'),'w') as f:
f.write('woot!')
os.mkdir(os.path.join(d,'subdir'))
with open(os.path.join(d,'subdir/subtext.txt'),'w') as f:
f.write('woot!')
git.add('test.txt', 'subdir/subtext.txt')
git.commit('test.txt', 'subdir/subtext.txt', message='-')
with open(os.path.join(d,'test.txt'),'w') as f:
f.write('woot2')
git.commit('test.txt', message='-')
with open(os.path.join(d,'test.txt'),'w') as f:
f.write('woot3')
git.commit('test.txt', message='-')
with tempfile.TemporaryDirectory() as td:
ygit.clone('http://localhost:8889/'+os.path.basename(d),td, shallow=False)
assert sorted(os.listdir(td)) == ['.ygit', 'subdir', 'test.txt']
assert sorted([s for s in os.listdir(os.path.join(td,'.ygit')) if not s.endswith('.pack')]) == ['config', 'idx', 'refs']
assert len([s for s in os.listdir(os.path.join(td,'.ygit')) if s.endswith('.pack')]) == 1
with open(os.path.join(td,'test.txt')) as f:
assert f.read()=='woot3'
def test_clone_empty_repo():
git, d = build_repo()
with tempfile.TemporaryDirectory() as td:
ygit.clone('http://localhost:8889/'+os.path.basename(d),td)
assert os.path.isdir(os.path.join(td,'.ygit'))
assert os.path.isfile(os.path.join(td,'.ygit','config'))
assert os.path.isfile(os.path.join(td,'.ygit','idx'))
assert sorted(os.listdir(os.path.join(td,'.ygit'))) == ['config', 'idx', 'refs']
def test_fetch_no_update():
git, d = build_repo()
with open(os.path.join(d,'test.txt'),'w') as f:
f.write('woot!')
git.add('test.txt')
git.commit('test.txt', message='-')
with tempfile.TemporaryDirectory() as td:
repo = ygit.clone('http://localhost:8889/'+os.path.basename(d),td)
assert len([s for s in os.listdir(os.path.join(td,'.ygit')) if s.endswith('.pack')]) == 1
repo.fetch()
assert len([s for s in os.listdir(os.path.join(td,'.ygit')) if s.endswith('.pack')]) == 1
def test_fetch_after_adding_to_empty_repo():
git, d = build_repo()
with tempfile.TemporaryDirectory() as td:
repo = ygit.clone('http://localhost:8889/'+os.path.basename(d),td)
assert len([s for s in os.listdir(os.path.join(td,'.ygit')) if s.endswith('.pack')]) == 0
with open(os.path.join(d,'test.txt'),'w') as f:
f.write('woot!')
git.add('test.txt')
git.commit('test.txt', message='-')
repo.fetch()
assert len([s for s in os.listdir(os.path.join(td,'.ygit')) if s.endswith('.pack')]) == 1
def test_updated_file():
git, d = build_repo()
with open(os.path.join(d,'test.txt'),'w') as f:
f.write('v1')
git.add('test.txt')
git.commit('test.txt', message='v1')
with tempfile.TemporaryDirectory() as td:
repo = ygit.clone('http://localhost:8889/'+os.path.basename(d),td)
with open(os.path.join(d,'test.txt')) as f:
assert f.read()=='v1'
with open(os.path.join(d,'test.txt'),'w') as f:
f.write('v2')
git.commit('test.txt', message='v2')
repo.pull()
with open(os.path.join(td,'test.txt')) as f:
assert f.read()=='v2'
assert len([s for s in os.listdir(os.path.join(td,'.ygit')) if s.endswith('.pack')]) == 2
def test_deleted_file():
git, d = build_repo()
with open(os.path.join(d,'test.txt'),'w') as f:
f.write('v1')
git.add('test.txt')
os.mkdir(os.path.join(d,'subdir'))
with open(os.path.join(d,'subdir', 'test2.txt'),'w') as f:
f.write('test2')
git.add('test.txt','subdir/test2.txt')
git.commit('test.txt', 'subdir/test2.txt', message='v1')
assert os.path.isfile(os.path.join(d,'test.txt'))
assert os.path.isfile(os.path.join(d,'subdir','test2.txt'))
with tempfile.TemporaryDirectory() as td:
repo = ygit.clone('http://localhost:8889/'+os.path.basename(d),td)
assert os.path.isfile(os.path.join(td,'test.txt'))
git.rm('test.txt')
git.rm('subdir/test2.txt')
git.commit('test.txt', 'subdir/test2.txt', message='removed')
repo.pull()
assert not os.path.isfile(os.path.join(td,'test.txt'))
assert not os.path.isfile(os.path.join(td,'subdir/test2.txt'))
def test_deleted_file_in_cone():
git, d = build_repo()
with open(os.path.join(d,'test.txt'),'w') as f:
f.write('v1')
git.add('test.txt')
os.mkdir(os.path.join(d,'subdir'))
with open(os.path.join(d,'subdir', 'test2.txt'),'w') as f:
f.write('test2')
git.add('test.txt','subdir/test2.txt')
git.commit('test.txt', 'subdir/test2.txt', message='v1')
assert os.path.isfile(os.path.join(d,'test.txt'))
assert os.path.isfile(os.path.join(d,'subdir','test2.txt'))
with tempfile.TemporaryDirectory() as td:
repo = ygit.clone('http://localhost:8889/'+os.path.basename(d),td, cone='subdir')
assert os.path.isfile(os.path.join(td,'test2.txt'))
git.rm('test.txt')
git.rm('subdir/test2.txt')
git.commit('test.txt', 'subdir/test2.txt', message='removed')
repo.pull()
print('sdfkjldsfjhkldsf', td, os.path.isfile(os.path.join(td,'test2.txt')), os.listdir(td))
assert not os.path.isfile(os.path.join(td,'test2.txt'))
def test_status():
git, d = build_repo()
with open(os.path.join(d,'test.txt'),'w') as f:
f.write('v1')
git.add('test.txt')
git.commit('test.txt', message='v1')
with tempfile.TemporaryDirectory() as td:
repo = ygit.clone('http://localhost:8889/'+os.path.basename(d),td)
out = io.StringIO()
repo.status(out=out)
assert ''==out.getvalue()
with open(os.path.join(td,'test.txt'),'w') as f:
f.write('v2')
out = io.StringIO()
repo.status(out=out)
assert 'M /test.txt\n'==out.getvalue()
def test_branch():
git, d = build_repo()
with open(os.path.join(d,'test.txt'),'w') as f:
f.write('v1')
git.add('test.txt')
git.commit('test.txt', message='v1')
main_branch = git.branch(show_current=True).strip()
git.checkout('-b', 'abranch')
with open(os.path.join(d,'branch.txt'),'w') as f:
f.write('abranch')
git.add('branch.txt')
git.commit('branch.txt', message='added a branch')
git.checkout(main_branch)
with tempfile.TemporaryDirectory() as td:
repo = ygit.clone('http://localhost:8889/'+os.path.basename(d),td)
with open(os.path.join(td,'test.txt')) as f:
assert f.read()=='v1'
assert not os.path.isfile(os.path.join(td,'branch.txt'))
repo.checkout(ref='abranch')
with open(os.path.join(td,'branch.txt')) as f:
assert f.read()=='abranch'