-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
executable file
·299 lines (226 loc) · 9.59 KB
/
test.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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#!/usr/bin/python3
import unittest
import gitSOC
import gitSOC.cmd.register
import gitSOC.cmd.pull
import gitSOC.cmd.push
import gitSOC.cmd.status
import gitSOC.cmd.fetch
import gitSOC.cmd.cmd
import os
import subprocess
import pdb
if os.path.isdir("__test"):
print("********* nuking")
subprocess.call(["rm","-rf","__test"])
class gitSocTests(unittest.TestCase):
def __init__(self, casenames):
print(casenames)
self.inited = False
self.config_loaded = False
unittest.TestCase.__init__(self, casenames)
def banner(self, testname):
print("#")
print("# " + testname + "########################################")
print("#")
def running(self, what):
print("# Running: " + what)
def pushd(self, newpath):
self.pastdirs.append(os.getcwd())
#print("jumping into " + newpath)
os.chdir(newpath)
def popd(self):
if len(self.pastdirs) < 1:
print("no more dirs to pop")
exit(1)
popeddir = self.pastdirs[-1]
self.pastdirs = self.pastdirs[:-1]
#print("popping out to " + popeddir)
os.chdir(popeddir)
def setup_soc(self):
self.soc = gitSOC.GitSOC()
self.baseargs = self.soc.parse_global_args()
def create_command(self, command):
cmd = command(self.soc, self.baseargs)
self.cmd_args = cmd.parse_args(['-B',self.basedir])
if not self.config_loaded:
self.config_loaded = True
self.soc.load_config_directory('__test/base', self.cmd_args)
return cmd
def create_command_and_run(self, command):
cmd = self.create_command(command)
print(cmd)
print(command)
print("# running " + str(cmd))
cmd.run(self.cmd_args)
def setUp(self):
self.cwd = os.getcwd() + "/"
self.pastdirs = []
self.setup_soc()
# if os.path.isdir("__test"):
# print("nuking")
# self.remove_tests()
self.basedir = self.cwd + "__test/base"
if not os.path.isdir(self.basedir):
os.makedirs(self.basedir)
self.repos = [['repo1', 'repo1'], ['repo2', 'repo2'],
['repo1', 'repo1clone'], ['repo2', 'repo2clone']]
def remove_tests(self):
#print("cleaning up")
subprocess.call(["rm","-rf","__test"])
#def tearDown(self):
#self.remove_tests()
def is_repo_correct(self, repo, args):
#print("testing repo " + repo.path())
args['me'].assertTrue(repo.path() != "")
self.count = self.count + 1
def create_cloned_repo(self, reponame, cloned_to_name = None):
if not cloned_to_name:
cloned_to_name = reponame
if not os.path.isdir("__test/upstreams/" + reponame):
os.makedirs("__test/upstreams/" + reponame)
self.pushd(self.cwd + "__test/upstreams/" + reponame)
subprocess.call(["git", "init", "--bare"])
self.popd()
self.pushd(self.cwd + "__test/")
subprocess.call(["git", "clone", "upstreams/" + reponame,
cloned_to_name])
self.popd()
# set up initial content for establishing a base history tree
if reponame == cloned_to_name:
self.pushd(self.cwd + "__test/" + cloned_to_name)
subprocess.call(["touch", "_soc_test"])
subprocess.call(["git", "add", "_soc_test"])
subprocess.call(["git", "commit", "-m", "init", "_soc_test"])
subprocess.call(["git", "push"])
self.popd()
def create_file(self, reponame, filename, content, commit = False):
self.pushd(self.cwd + "__test/" + reponame)
fileh = open(filename, "w")
fileh.write(content)
fileh.close()
if commit:
subprocess.call(["git", "add", filename])
subprocess.call(["git", "commit", "-m", "commiting " + filename])
self.popd()
def verify_file_contents(self, reponame, filename, content):
self.pushd(self.cwd + "__test/" + reponame)
fileh = open(filename, "r")
result = fileh.read(4096)
fileh.close()
self.popd()
if result != content:
return False
return True
def run_status(self):
self.create_command_and_run(gitSOC.cmd.status.Status)
def sync_everything(self):
"""Cheap hack to push and pull a bunch to make everything
get in sync"""
self.running("Sync")
self.create_command_and_run(gitSOC.cmd.push.Push)
self.create_command_and_run(gitSOC.cmd.pull.Pull)
self.create_command_and_run(gitSOC.cmd.push.Push)
self.create_command_and_run(gitSOC.cmd.pull.Pull)
self.run_status()
def test_10_register(self):
self.banner("REGISTER")
reg = gitSOC.cmd.register.Register(self.soc, self.baseargs)
self.count = 0
for repo in self.repos:
basename = repo[0]
clonename = repo[1]
print("*** " + basename + " => " + clonename)
self.create_cloned_repo(basename, clonename)
self.pushd(self.cwd + "__test/" + clonename)
#print(args)
args = reg.parse_args(['-B',self.basedir, clonename])
reg.run(args)
self.popd()
self.assertTrue(os.path.isdir("__test/base"))
self.assertTrue(os.path.isfile("__test/base/" + clonename + ".yml"))
self.soc.load_config_directory('__test/base', reg)
self.soc.foreach_repo(self.is_repo_correct, { 'me': self })
self.assertEqual(self.count, 4)
def test_20_push_then_pull(self):
self.banner("PUSH then PULL")
push = self.create_command(gitSOC.cmd.push.Push)
self.soc.load_config_directory('__test/base', self.cmd_args)
# create some files
for repo in self.repos:
basename = repo[0]
clonename = repo[1]
self.create_file(clonename, "file_" + clonename,
"my content from " + clonename, True)
# push everything out
push.run(self.cmd_args)
# check status
self.run_status()
# pull everything into the directories
# push everything out
self.create_command_and_run(gitSOC.cmd.pull.Pull)
# check results, should be 2 failed pushes because of conflict
self.assertTrue(os.path.isfile("__test/repo1clone/file_repo1"))
self.assertTrue(os.path.isfile("__test/repo2clone/file_repo2"))
self.assertFalse(os.path.isfile("__test/repo1/file_repo2clone"))
self.assertFalse(os.path.isfile("__test/repo2/file_repo1clone"))
self.run_status()
self.sync_everything()
# the above falses should be fixed now
self.run_status()
self.assertTrue(os.path.isfile("__test/repo1/file_repo1clone"))
self.assertTrue(os.path.isfile("__test/repo2/file_repo2clone"))
def test_30_fetch_only(self):
self.banner("FETCH")
# create some files
for repo in self.repos:
basename = repo[0]
clonename = repo[1]
self.create_file(clonename, "fetch_" + clonename,
"my fetch change " + clonename, True)
# push everything out
self.run_status()
self.create_command_and_run(gitSOC.cmd.push.Push)
self.run_status()
# fetch everything
self.create_command_and_run(gitSOC.cmd.fetch.Fetch)
# make sure everyting didn't create new files
self.assertFalse(os.path.isfile("__test/repo1/fetch_repo1clone"))
self.assertFalse(os.path.isfile("__test/repo1clone/fetch_repo1"))
self.assertFalse(os.path.isfile("__test/repo2/fetch_repo2clone"))
self.assertFalse(os.path.isfile("__test/repo2clone/fetch_repo2"))
self.run_status()
# pull/merge everything
self.create_command_and_run(gitSOC.cmd.pull.Pull)
self.run_status()
# make sure pull did create the new files
self.assertTrue(os.path.isfile("__test/repo1clone/fetch_repo1"))
self.assertTrue(os.path.isfile("__test/repo2clone/fetch_repo2"))
# these should still be missing
self.assertFalse(os.path.isfile("__test/repo11/fetch_repo1clone"))
self.assertFalse(os.path.isfile("__test/repo2/fetch_repo2clone"))
# pull/merge everything to clean up
self.sync_everything()
# these should finally be present
self.assertTrue(os.path.isfile("__test/repo1/fetch_repo1clone"))
self.assertTrue(os.path.isfile("__test/repo2/fetch_repo2clone"))
self.run_status()
def test_40_pull_with_different_file_conflict(self):
self.banner("UNRELATED CONFLICT")
self.create_file(self.repos[0][1], "unrelated_",
"my unrelated local change ", True)
self.create_file(self.repos[2][1], "otherfile_",
"my local change ", False)
self.create_command_and_run(gitSOC.cmd.push.Push)
self.create_command_and_run(gitSOC.cmd.pull.Pull)
self.run_status()
self.assertTrue(self.verify_file_contents(self.repos[2][1],
"unrelated_",
"my unrelated local change "))
self.pushd(self.cwd + "__test/" + self.repos[2][1])
subprocess.call(["git", "add", "otherfile_"])
subprocess.call(["git", "commit", "-m", "otherfile_"])
# pull/merge everything to clean up
self.sync_everything()
if __name__ == '__main__':
unittest.main()