-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_syncdir_MasterSession.py
363 lines (344 loc) · 18.3 KB
/
test_syncdir_MasterSession.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import abc
import copy
import io
import os
import stat
import syncdir
import sys
import tempfile
import unittest
TIMEVAL1 = 1234567890
TIMEVAL2 = 1234567895
if sys.platform == "linux":
TIMESTR1 = " 9 Sat Feb 14 00:31:30 2009"
TIMESTR2 = " 9 Sat Feb 14 00:31:35 2009"
else:
TIMESTR1 = "10 Sat Feb 14 00:31:30 2009"
TIMESTR2 = "10 Sat Feb 14 00:31:35 2009"
class Params:
pass
class MasterSessionTestCase(unittest.TestCase, metaclass=abc.ABCMeta):
def __init__(self, params : Params):
if isinstance(params, Params):
# Real test case, explicitly created in load_tests.
unittest.TestCase.__init__(self, "runTest")
self.params = copy.copy(params)
else:
# Despite the presence of load_tests, unittest/suite instantiates
# one with a methodName argument for each class, but (luckily)
# never tries to run them (at least in python 3.4.3), even if
# we invoke the base constructor. Shrugs shoulders.
pass
def setUp(self):
self.src = tempfile.TemporaryDirectory(prefix="test_syncdir_", suffix=".lhs")
self.dst = tempfile.TemporaryDirectory(prefix="test_syncdir_", suffix=".rhs")
self.out = io.StringIO()
def tearDown(self):
self.dst.cleanup()
self.src.cleanup()
self.out.close()
def runTest(self):
class TestChooser:
def __init__(self, answers, out):
self.answers = answers
self.out = out
self.prompts = 0
def ask(self, prompt):
self.out.write(prompt + "\n")
self.prompts += 1
if self.prompts > len(self.answers):
raise RuntimeError('Starved for answers after %i prompts, at "%s"' % (self.prompts, prompt))
return self.answers[self.prompts-1]
chooser = TestChooser(answers=self.params.answers, out=self.out)
with self.subTest(clean=self.params.clean, do_nothing=self.params.do_nothing, do_everything=self.params.do_everything, follow_link=self.params.follow_link, ignore_time=self.params.ignore_time, trust_time=self.params.trust_time, answers=self.params.answers):
syncdir.MasterSession(self.src.name, self.dst.name, chooser=chooser, out=self.out, clean=self.params.clean, do_nothing=self.params.do_nothing, do_everything=self.params.do_everything, follow_link=self.params.follow_link, ignore_time=self.params.ignore_time, trust_time=self.params.trust_time).run()
self.assertEqual(chooser.prompts, len(self.params.answers))
self.check()
@abc.abstractmethod
def check(self):
pass
def affirmative(self):
return self.params.do_everything or (self.params.answers and self.params.answers[0] in ['y', 'Y', 'A'])
class MasterSessionTestCase_empty(MasterSessionTestCase):
def check(self):
self.assertEqual(self.out.getvalue(), "")
for folder in self.src, self.dst:
self.assertEqual(os.listdir(folder.name), [])
class MasterSessionTestCase_2_files_identical(MasterSessionTestCase):
def setUp(self):
super().setUp()
for folder in self.src, self.dst:
with open(os.path.join(folder.name, "phile"), "w") as f:
f.write("contents\n")
self._check_folders(cleaned=False)
def check(self):
if self.params.clean:
self.assertEqual(self.out.getvalue()[:29], "phile has not changed, remove")
else:
self.assertEqual(self.out.getvalue(), "\rphile has not changed")
cleaned = self.affirmative() and self.params.clean
self._check_folders(cleaned=cleaned)
def _check_folders(self, cleaned):
if cleaned:
self.assertEqual(os.listdir(self.src.name), [])
else:
self.assertEqual(os.listdir(self.src.name), ["phile"])
self.assertEqual(os.listdir(self.dst.name), ["phile"])
class MasterSessionTestCase_2_files_same_name_size_time(MasterSessionTestCase):
def setUp(self):
super().setUp()
with open(os.path.join(self.src.name, "phile"), "w") as f:
f.write("lhs contents\n")
with open(os.path.join(self.dst.name, "phile"), "w") as f:
f.write("rhs contents\n") # same file size as src file
for folder in self.src, self.dst:
os.utime(os.path.join(folder.name, "phile"), (TIMEVAL1,TIMEVAL1))
self._check_folders(cleaned=False, copied=False)
def check(self):
if self.params.trust_time:
if self.params.clean:
self.assertEqual(self.out.getvalue()[:29], "phile has not changed, remove")
else:
self.assertEqual(self.out.getvalue(), "\rphile has not changed")
else:
out = self.out.getvalue().split('\n\n')
self.assertEqual(len(out), 2)
self.assertEqual(out[0], "\rphile - difference:\n- lhs contents\n? ^\n+ rhs contents\n? ^")
if self.params.clean:
self.assertEqual(out[1][:34], "phile has changed as shown, remove")
else:
self.assertEqual(out[1][:37], "phile has changed as shown, overwrite")
cleaned = self.affirmative() and self.params.clean
copied = self.affirmative() and not self.params.clean and not self.params.trust_time
self._check_folders(cleaned=cleaned, copied=copied)
def _check_folders(self, cleaned, copied):
if cleaned:
self.assertEqual(os.listdir(self.src.name), [])
else:
self.assertEqual(os.listdir(self.src.name), ["phile"])
with open(os.path.join(self.src.name, "phile"), "r") as f:
self.assertEqual(f.read(), "lhs contents\n")
self.assertEqual(os.listdir(self.dst.name), ["phile"])
with open(os.path.join(self.dst.name, "phile"), "r") as f:
if copied:
self.assertEqual(f.read(), "lhs contents\n")
else:
self.assertEqual(f.read(), "rhs contents\n")
class MasterSessionTestCase_2_files_same_name_size(MasterSessionTestCase):
def setUp(self):
super().setUp()
with open(os.path.join(self.src.name, "phile"), "w") as f:
f.write("lhs contents\n")
with open(os.path.join(self.dst.name, "phile"), "w") as f:
f.write("rhs contents\n") # same file size as src file
os.utime(os.path.join(self.src.name, "phile"), (TIMEVAL1,TIMEVAL1))
self._check_folders(cleaned=False, copied=False)
def check(self):
out = self.out.getvalue().split('\n\n')
self.assertEqual(len(out), 2)
self.assertEqual(out[0], "\rphile - difference:\n- lhs contents\n? ^\n+ rhs contents\n? ^")
if self.params.clean:
self.assertEqual(out[1][:34], "phile has changed as shown, remove")
else:
self.assertEqual(out[1][:37], "phile has changed as shown, overwrite")
cleaned = self.affirmative() and self.params.clean
copied = self.affirmative() and not self.params.clean
self._check_folders(cleaned=cleaned, copied=copied)
def _check_folders(self, cleaned, copied):
if cleaned:
self.assertEqual(os.listdir(self.src.name), [])
else:
self.assertEqual(os.listdir(self.src.name), ["phile"])
with open(os.path.join(self.src.name, "phile"), "r") as f:
self.assertEqual(f.read(), "lhs contents\n")
self.assertEqual(os.listdir(self.dst.name), ["phile"])
with open(os.path.join(self.dst.name, "phile"), "r") as f:
if copied:
self.assertEqual(f.read(), "lhs contents\n")
else:
self.assertEqual(f.read(), "rhs contents\n")
class MasterSessionTestCase_2_files_same_name_contents(MasterSessionTestCase):
def setUp(self):
super().setUp()
with open(os.path.join(self.src.name, "phile"), "w") as f:
f.write("contents\n")
with open(os.path.join(self.dst.name, "phile"), "w") as f:
f.write("contents\n")
os.utime(os.path.join(self.src.name, "phile"), (TIMEVAL1,TIMEVAL1))
os.utime(os.path.join(self.dst.name, "phile"), (TIMEVAL2,TIMEVAL2))
self._check_folders(cleaned=False, touched=False)
def check(self):
if self.params.ignore_time:
if self.params.clean:
self.assertEqual(self.out.getvalue()[:29], "phile has not changed, remove")
else:
self.assertEqual(self.out.getvalue(), "\rphile has not changed")
else:
out = self.out.getvalue().split('\n')
self.assertEqual(out[0], " %s %s" % (TIMESTR1, self.src.name))
self.assertEqual(out[1], " %s %s" % (TIMESTR2, self.dst.name))
if self.params.do_everything or self.params.do_nothing:
if self.params.clean:
self.assertEqual(out[2], "phile has different time, remove")
else:
self.assertEqual(out[2], "phile has different time, touch")
else:
if self.params.clean:
self.assertEqual(out[2], "phile has different time, remove? [yes no AllYes ZeroYes Quit] n\b")
else:
self.assertEqual(out[2], "phile has different time, touch? [yes no AllYes ZeroYes Quit] n\b")
self.assertEqual(out[3], "")
self.assertEqual(len(out), 4)
cleaned = self.affirmative() and self.params.clean
touched = self.affirmative() and not self.params.clean and not self.params.ignore_time
self._check_folders(cleaned=cleaned, touched=touched)
def _check_folders(self, cleaned, touched):
if cleaned:
self.assertEqual(os.listdir(self.src.name), [])
else:
self.assertEqual(os.listdir(self.src.name), ["phile"])
with open(os.path.join(self.src.name, "phile"), "r") as f:
self.assertEqual(f.read(), "contents\n")
self.assertEqual(os.stat(os.path.join(self.src.name, "phile"))[stat.ST_MTIME], TIMEVAL1)
self.assertEqual(os.listdir(self.dst.name), ["phile"])
with open(os.path.join(self.dst.name, "phile"), "r") as f:
self.assertEqual(f.read(), "contents\n")
if touched:
self.assertEqual(os.stat(os.path.join(self.dst.name, "phile"))[stat.ST_MTIME], TIMEVAL1)
else:
self.assertEqual(os.stat(os.path.join(self.dst.name, "phile"))[stat.ST_MTIME], TIMEVAL2)
class MasterSessionTestCase_2_different_files(MasterSessionTestCase):
def setUp(self):
super().setUp()
with open(os.path.join(self.src.name, "lhs phile"), "w") as f:
f.write("lhs contents\n")
with open(os.path.join(self.dst.name, "rhs phile"), "w") as f:
f.write("rhs contents\n")
self._check_folders(cleaned=False, copied=False)
def check(self):
out = self.out.getvalue().split('\n')
self.assertEqual(out[-1], "")
del out[-1]
if self.params.clean:
self.assertEqual(out[0][:24], "lhs phile is new, remove")
self.assertEqual(len(out), 1)
else:
self.assertEqual(out[0][:24], "lhs phile is new, create")
self.assertEqual(out[1][:33], "rhs phile has disappeared, remove")
self.assertEqual(len(out), 2)
cleaned = self.affirmative() and self.params.clean
copied = self.affirmative() and not self.params.clean
self._check_folders(cleaned=cleaned, copied=copied)
def _check_folders(self, cleaned, copied):
for folder in self.src, self.dst:
if folder is self.src and cleaned:
self.assertEqual(os.listdir(self.src.name), [])
elif folder is self.src or copied:
self.assertEqual(os.listdir(folder.name), ["lhs phile"])
with open(os.path.join(folder.name, "lhs phile"), "r") as f:
self.assertEqual(f.read(), "lhs contents\n")
else:
self.assertEqual(os.listdir(folder.name), ["rhs phile"])
with open(os.path.join(folder.name, "rhs phile"), "r") as f:
self.assertEqual(f.read(), "rhs contents\n")
class MasterSessionTestCase_2_folders(MasterSessionTestCase):
def setUp(self):
super().setUp()
for folder in self.src, self.dst:
for subf in "pholder A", "pholder B":
os.mkdir(os.path.join(folder.name, subf))
for philename in "phile 1", "phile 2":
with open(os.path.join(folder.name, subf, philename), "w") as f:
if folder is self.src:
f.write("left contents in %s\n" % subf)
else:
assert folder is self.dst
f.write("right contents in %s\n" % subf)
self._check_folders(cleaned=False, copied=False)
def check(self):
cleaned = self.affirmative() and self.params.clean
copied = self.affirmative() and not self.params.clean
self._check_folders(cleaned=cleaned, copied=copied)
def _check_folders(self, cleaned, copied):
for folder in self.src, self.dst:
if folder is self.src and cleaned:
self.assertEqual(os.listdir(folder.name), [])
else:
self.assertEqual(sorted(os.listdir(folder.name)), ["pholder A", "pholder B"])
self.assertEqual(sorted(os.listdir(os.path.join(folder.name, "pholder A"))), ["phile 1", "phile 2"])
self.assertEqual(sorted(os.listdir(os.path.join(folder.name, "pholder B"))), ["phile 1", "phile 2"])
for philename in "phile 1", "phile 2":
if folder is self.src or copied:
with open(os.path.join(folder.name, "pholder A", philename), "r") as f:
self.assertEqual(f.read(), "left contents in pholder A\n")
with open(os.path.join(folder.name, "pholder B", philename), "r") as f:
self.assertEqual(f.read(), "left contents in pholder B\n")
elif folder is self.dst or not cleaned:
with open(os.path.join(folder.name, "pholder A", philename), "r") as f:
self.assertEqual(f.read(), "right contents in pholder A\n")
with open(os.path.join(folder.name, "pholder B", philename), "r") as f:
self.assertEqual(f.read(), "right contents in pholder B\n")
def load_tests(loader, tests, pattern):
suite = unittest.TestSuite()
params = Params()
for params.clean in False, True:
for params.follow_link in False, True:
for params.ignore_time,params.trust_time in (False,False), (False,True), (True,False):
for params.do_nothing,params.do_everything in (False,False), (False,True), (True,False):
# MasterSessionTestCase_empty
params.answers = []
suite.addTest(MasterSessionTestCase_empty(params))
if params.do_everything or params.do_nothing or not params.clean:
params.answers = []
suite.addTest(MasterSessionTestCase_2_files_identical(params))
else:
for answer in ['', 'n', 'N', 'Z', 'y', 'Y', 'A']:
params.answers = [answer]
suite.addTest(MasterSessionTestCase_2_files_identical(params))
# MasterSessionTestCase_2_files_same_name_size_time
if params.do_everything or params.do_nothing or (not params.clean and params.trust_time):
params.answers = []
suite.addTest(MasterSessionTestCase_2_files_same_name_size_time(params))
else:
for answer in ['', 'n', 'N', 'Z', 'y', 'Y', 'A']:
params.answers = [answer]
suite.addTest(MasterSessionTestCase_2_files_same_name_size_time(params))
# MasterSessionTestCase_2_files_same_name_size
if params.do_everything or params.do_nothing:
params.answers = []
suite.addTest(MasterSessionTestCase_2_files_same_name_size(params))
else:
for answer in ['', 'n', 'N', 'Z', 'y', 'Y', 'A']:
params.answers = [answer]
suite.addTest(MasterSessionTestCase_2_files_same_name_size(params))
# MasterSessionTestCase_2_files_same_name_contents
if params.do_everything or params.do_nothing or (not params.clean and params.ignore_time):
params.answers = []
suite.addTest(MasterSessionTestCase_2_files_same_name_contents(params))
else:
for answer in ['', 'n', 'N', 'Z', 'y', 'Y', 'A']:
params.answers = [answer]
suite.addTest(MasterSessionTestCase_2_files_same_name_contents(params))
# MasterSessionTestCase_2_different_files
if params.do_everything or params.do_nothing:
params.answers = []
suite.addTest(MasterSessionTestCase_2_different_files(params))
else:
for answer in ['', 'n', 'N', 'Z', 'y', 'Y', 'A']:
if params.clean:
params.answers = [answer]
else:
params.answers = [answer] * 2
suite.addTest(MasterSessionTestCase_2_different_files(params))
# MasterSessionTestCase_2_folders
# mostly checking the number of answers used.
if params.do_everything or params.do_nothing:
params.answers = []
suite.addTest(MasterSessionTestCase_2_folders(params))
else:
for params.answers in [['']*4, ['n']*4, ['N']*2, ['Z'], ['y']*4, ['Y']*2, ['A']]:
suite.addTest(MasterSessionTestCase_2_folders(params))
return suite
del MasterSessionTestCase # hide from overzealous discovery (superseded by load_tests)
if __name__ == '__main__':
unittest.main()