This repository has been archived by the owner on May 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
executable file
·503 lines (361 loc) · 11.7 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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
#!/usr/bin/env python2.7
import os
import hashlib
import shutil
from cStringIO import StringIO
import subprocess
import sys
from nose.tools import *
from unittest import skipIf
import svnstash
from svnstash import cmds
run = 'all'
svnstash.interactive = False
TEST_DIR = os.path.abspath('test')
TEST_FILES = '%s/files' % TEST_DIR
def _mv(file, new):
subprocess.check_output(['svn', 'mv', file, new])
def _rm(path):
subprocess.check_output(['svn', 'rm', path])
def _add():
subprocess.check_output('svn add --force %s/*' % CLIENT_DIR, shell=True)
def _commit():
subprocess.check_output(['svn', 'commit', CLIENT_DIR, '-m', 'commit'])
def _command(cmd, args=[], interactive_response=False):
""" Run an svnstash command with the given argument(s) """
sys.argv = ['svnstash', cmd]
svnstash.interactive_response = interactive_response
if isinstance(args, list):
sys.argv += args
else:
sys.argv.append(args)
#capture the output from the commands
stdout = StringIO()
stderr = StringIO()
sys.stdout = stdout
sys.stderr = stderr
svnstash.main()
sys.stdout = STDOUT
sys.stderr = STDERR
ret = (stdout.getvalue(), stderr.getvalue())
stdout.close()
stderr.close()
return ret
def _test_file(file_path):
return '%s/%s' % (TEST_FILES, file_path)
def _client_file(client_path):
return '%s/%s' % (CLIENT_DIR, client_path)
def _copy(file_path, client_path=''):
subprocess.check_output(['cp', '-a', _test_file(file_path), _client_file(client_path)])
_add()
def _md5(file_path):
md5 = hashlib.md5()
with open(file_path,'rb') as f:
for chunk in iter(lambda: f.read(8192), ''):
md5.update(chunk)
return md5.hexdigest()
class Base(object):
""" Basic setup for every test case """
def setUp(self):
#allow multiprocess testing
gs = globals()
gs['TEST_ZONE'] = '%s/test_zone_%d' % (TEST_DIR, os.getpid())
gs['CLIENT_DIR'] = '%s/client' % TEST_ZONE
gs['SERVER_DIR'] = '%s/server' % TEST_ZONE
gs['STDOUT'] = sys.stdout
gs['STDERR'] = sys.stderr
if os.path.exists(TEST_ZONE):
shutil.rmtree(TEST_ZONE)
#create a test space to work in
os.mkdir(TEST_ZONE)
subprocess.check_output(['svnadmin', 'create', SERVER_DIR])
subprocess.check_output(['svn', 'checkout', 'file://%s' % SERVER_DIR, CLIENT_DIR])
#we always operate inside the svn reo
os.chdir(CLIENT_DIR)
def tearDown(self):
#clear all the test data
shutil.rmtree(TEST_ZONE)
@skipIf(run not in ('all', 'TestCoverage'), '')
class TestCoverage(Base):
""" Make coverage happier """
def test_import(self):
reload(svnstash)
svnstash.interactive = False
def test_dependencies(self):
_command('dependencies')
@skipIf(run not in ('all', 'TestBash'), '')
class TestBash(Base):
""" Make sure that bash autocompletes work properly """
def test_autocomplete(self):
#make sure all commands are listed
assert len(_command('bash', '')[0].strip().split('\n')) == len(svnstash.commands)
#a minor cross-section of the possible autocompletions, just make sure it works
assert _command('bash', 'a')[0].strip() == 'apply'
assert _command('bash', 'ls')[0].strip() == 'ls'
assert _command('bash', 'rem')[0].strip() == 'remove'
assert _command('bash', 'sa')[0].strip() == 'save'
@skipIf(run not in ('all', 'TestClean'), '')
class TestClean(Base):
""" Test a clean repo with nothing in it. """
file = 'a_few_lines'
def test_ls(self):
#Make sure nothing is stashed
assert len(_command('list')[0]) == 0
def test_pop(self):
#Make sure nothing can be popped
assert_raises(SystemExit, _command, 'pop')
assert not cmds.svn_changes_exist(CLIENT_DIR)
def test_push(self):
#Make sure nothing can be pushed
assert_raises(SystemExit, _command, 'push')
self.test_ls()
def test_extreme_pop(self):
_copy(self.file)
cmds.svn_add(_client_file(self.file))
_command('push')
assert_raises(SystemExit, _command, 'pop', ['1'])
assert_raises(SystemExit, _command, 'pop', ['2'])
assert_raises(SystemExit, _command, 'pop', ['-1'])
def test_extreme_show(self):
_copy(self.file)
cmds.svn_add(_client_file(self.file))
_command('push')
assert_raises(SystemExit, _command, 'show')
assert_raises(SystemExit, _command, 'show', ['1'])
assert_raises(SystemExit, _command, 'show', ['2'])
assert_raises(SystemExit, _command, 'show', ['-2'])
def test_extreme_delete(self):
_copy(self.file)
cmds.svn_add(_client_file(self.file))
_command('push')
assert_raises(SystemExit, _command, 'remove')
assert_raises(SystemExit, _command, 'remove', ['1'])
assert_raises(SystemExit, _command, 'remove', ['-1'])
assert len(_command('remove', ['0'])) == 2
def test_help(self):
assert len(_command('help')[0]) > 1
assert len(_command('help', 'push')[0]) > 1
@skipIf(run not in ('all', 'TestBasicCommands'), '')
class TestBasicCommands(Base):
file = 'a_few_lines'
def test_1_pop(self):
_copy(self.file)
_command('push')
assert not cmds.svn_changes_exist(CLIENT_DIR)
_command('pop', '0')
assert cmds.svn_changes_exist(CLIENT_DIR)
assert_raises(SystemExit, _command, 'pop', '1')
_command('push')
_command('pop')
stashes = svnstash.Stashes()
assert len(stashes) == 0
def test_2_apply(self):
_copy(self.file)
_command('push')
assert not cmds.svn_changes_exist(CLIENT_DIR)
_command('apply', '0')
assert cmds.svn_changes_exist(CLIENT_DIR)
assert_raises(SystemExit, _command, 'apply', '1')
_command('push')
_command('apply')
stashes = svnstash.Stashes()
assert len(stashes) == 2
def test_3_save(self):
_copy(self.file)
_command('save', ['some message', 'that splits'])
s = svnstash.Stashes()[0]
assert s.comment == 'some message that splits'
assert len(s.get_affected_files()) == 1
_copy(self.file)
_command('save')
s = svnstash.Stashes()[0]
assert s.comment == ''
assert len(s.get_affected_files()) == 1
@skipIf(run not in ('all', 'TestSingleTextChanges'), '')
class TestSingleTextChanges(Base):
file = 'a_few_lines'
file_moved = file + '_moved'
file_changed = 'SingleTextChanges/a_few_lines'
@classmethod
def setUpClass(cls):
pass
def test_0_single_file(self):
_copy(self.file)
cmds.svn_add(_client_file(self.file))
_command('push')
assert not cmds.svn_changes_exist(CLIENT_DIR)
stashes = svnstash.Stashes()
assert len(stashes) == 1
s = stashes[0]
assert s.comment == ''
assert len(s.get_affected_files()) == 1
_command('pop')
assert cmds.svn_changes_exist(CLIENT_DIR)
assert len(cmds.svn_changes(CLIENT_DIR)) == 1
def test_1_file_mod(self):
_copy(self.file)
_command('push')
_copy(self.file_changed, self.file)
_commit()
assert not cmds.svn_changes_exist(CLIENT_DIR)
_command('pop')
assert cmds.svn_changes_exist(CLIENT_DIR)
def test_2_moved_file(self):
#Test response to committing a file, making changes to it, stashing it, moving that file,
#then unstashing (the unstash will have the old file location, and it should update + conflict
#with the new file, and patch should reject it)
_copy(self.file)
_commit()
_copy(self.file_changed, self.file)
_command('push')
_copy(self.file_changed, self.file)
_mv(self.file, self.file_moved)
_commit()
_command('pop')
assert os.path.exists('%s/%s' % (CLIENT_DIR, self.file_moved))
assert not os.path.exists('%s/%s.orig' % (CLIENT_DIR, self.file_moved))
assert os.path.exists('%s/%s.rej' % (CLIENT_DIR, self.file_moved))
assert not os.path.exists('%s/%s' % (CLIENT_DIR, self.file))
def test_3_simple_moved_filed(self):
#like test2, but without the conflict
_copy(self.file)
_commit()
_copy(self.file_changed, self.file)
_command('push')
_mv(self.file, self.file_moved)
_commit()
_command('pop')
assert os.path.exists('%s/%s' % (CLIENT_DIR, self.file_moved))
assert not os.path.exists('%s/%s.orig' % (CLIENT_DIR, self.file_moved))
assert not os.path.exists('%s/%s.rej' % (CLIENT_DIR, self.file_moved))
assert not os.path.exists('%s/%s' % (CLIENT_DIR, self.file))
def test_4_file_deleted(self):
_copy(self.file)
_commit()
_copy(self.file_changed, self.file)
_command('push')
_rm(self.file)
_commit()
_command('pop', interactive_response=True)
assert not os.path.exists('%s/%s' % (CLIENT_DIR, self.file))
assert not os.path.exists('%s/%s.orig' % (CLIENT_DIR, self.file))
assert not os.path.exists('%s/%s.rej' % (CLIENT_DIR, self.file))
assert not os.path.exists('%s/%s' % (CLIENT_DIR, self.file))
def test_5_dirty_pop(self):
_copy(self.file)
_command('push')
_copy(self.file_changed)
assert_raises(SystemExit, _command, 'pop')
@skipIf(run not in ('all', 'TestDirectories'), '')
class TestDirectories(Base):
""" Test that directory stashing works """
foo = 'TestDirectories/foo'
foo_mod = 'TestDirectories/foo_mod'
bar = 'TestDirectories/bar'
def test_1(self):
_copy(self.foo)
_command('push')
ss = svnstash.Stashes()
assert len(ss) == 1
s = ss[0]
assert len(s.get_affected_files()) == 2 #directory doesn't count
assert not cmds.svn_changes_exist(CLIENT_DIR)
_command('pop')
assert cmds.svn_changes_exist(CLIENT_DIR)
assert len(cmds.svn_changes(CLIENT_DIR)) == 3
def test_2(self):
_copy(self.foo)
_commit()
_copy(self.foo_mod)
_command('push')
_copy(self.bar)
_commit()
_command('pop')
assert len(cmds.svn_changes(CLIENT_DIR)) == 3
def test_3(self):
_copy(self.foo)
_add()
_command('push')
_copy(self.foo_mod)
_add()
_command('push')
_command('pop')
_commit()
_command('pop')
@skipIf(run not in ('all', 'TestBinary'), '')
class TestBinary(Base):
""" Test modifications to binary files """
hi = 'TestBinary/hi.jpg'
hi_mod = 'TestBinary/hi_mod.jpg'
file = 'a_few_lines'
def test_1(self):
#make sure basic binary stashing works
_copy(self.hi)
_commit()
_copy(self.hi_mod, 'hi.jpg')
_command('push')
_command('pop')
assert _md5(_test_file(self.hi_mod)) == _md5(_client_file('hi.jpg'))
def test_2(self):
#binary with text
_copy(self.hi)
_copy(self.file)
files = ('hi.jpg', self.file)
_command('push')
s = svnstash.Stashes()[0]
fs = s.get_affected_files()
assert len(fs) == len(files)
for f in fs:
assert f in files
_command('pop')
fs = cmds.svn_changes(CLIENT_DIR)
assert len(fs) == len(files)
for f in fs:
assert os.path.basename(f[1]) in files
@skipIf(run not in ('all', 'TestMoved'), '')
class TestMoved(Base):
"""
Test files that were `svn mv`'d (svn handles this poorly, and it fails). If svn fixes it,
this should fail!
"""
file = 'a_few_lines'
file_moved = file + '_moved'
file_mod = 'SingleTextChanges/a_few_lines'
def test_1(self):
_copy(self.file)
_commit()
_mv(self.file, self.file_moved)
fs = cmds.svn_changes(CLIENT_DIR)
assert len(fs) == 2
_command('push')
assert not cmds.svn_changes_exist(CLIENT_DIR)
_command('pop')
fs = cmds.svn_changes(CLIENT_DIR)
assert len(fs) == 1
assert os.path.basename(fs[0][0]) == svnstash.STATUS.DELETED
assert os.path.basename(fs[0][1]) == self.file
def test_2(self):
_copy(self.file)
_commit()
_mv(self.file, self.file_moved)
#modify the copied file...still doesn't work :(
_copy(self.file_mod, self.file_moved)
fs = cmds.svn_changes(CLIENT_DIR)
assert len(fs) == 2
_command('push')
print _command('show', '0')[0]
assert not cmds.svn_changes_exist(CLIENT_DIR)
_command('pop')
fs = cmds.svn_changes(CLIENT_DIR)
assert len(fs) == 1
assert os.path.basename(fs[0][0]) == svnstash.STATUS.DELETED
assert os.path.basename(fs[0][1]) == self.file
if __name__== "__main__":
import nose
if len(sys.argv) > 1 and sys.argv[1] == 'p':
sys.argv.pop(1)
add = ['--processes=4']
else:
add = ['--with-coverage', '--cover-package=svnstash']
sys.argv += ['--exe', '--nocapture'] + add
nose.main(sys.argv)