41
def _check_diff(self, diff=DIFF_1, filename='test_file'):
42
old_tree = self.tree.basis_tree()
43
old_date = _patch_header_date(old_tree,
44
old_tree.inventory.path2id(filename),
46
new_date = _patch_header_date(self.tree,
47
self.tree.inventory.path2id(filename),
49
keys = { 'filename' : filename , 'old_date': old_date,
51
hdr = self.DIFF_HEADER % keys
53
self.assertEqual(self.capture('diff', retcode=1), hdr + diff + '\n')
55
def _check_shelf(self, idx, diff=DIFF_1, filename='test_file',
57
old_tree = self.tree.basis_tree()
58
old_date = _patch_header_date(old_tree,
59
old_tree.inventory.path2id(filename),
61
diff = diff % { 'filename' : filename, 'old_date': old_date,
63
shelf = open(os.path.join(self.tree.basedir,
64
'.shelf/shelves/default/' + idx)).read()
65
shelf = shelf[shelf.index('\n') + 1:] # skip the message
66
self.assertEqual(shelf, diff)
68
28
def test_shelf(self):
69
29
self.__test_loop(1)
98
54
# Make sure the file is actually back the way it was
99
55
self.assertEqual(file('test_file').read(), self.ORIGINAL)
101
self._check_shelf('00', new_date=new_date)
57
# Check the shelf is right
58
shelf = open(os.path.join(tree.branch.base,
59
'.shelf/shelves/default/00')).read()
60
shelf = shelf[shelf.index('\n') + 1:] # skip the message
61
self.assertEqual(shelf, self.DIFF_1)
104
64
self.run_bzr('unshelve', '--all', retcode=0)
66
# Check the diff is right again
67
self.assertEqual(self.capture('diff', retcode=1),
68
self.DIFF_HEADER + self.DIFF_1 + '\n')
108
70
# Check the shelved patch was backed up
109
self._check_shelf('00~', new_date=new_date)
71
shelf = open(os.path.join(tree.branch.base,
72
'.shelf/shelves/default/00~')).read()
73
shelf = shelf[shelf.index('\n') + 1:] # skip the message
74
self.assertEqual(shelf, self.DIFF_1)
111
76
# Make sure the file is back the way it should be
112
77
self.assertEqual(file('test_file').read(), self.MODIFIED)
114
79
def test_shelf_nothing_to_shelve(self):
116
self.tree = self.make_branch_and_tree('.')
117
self.__create_and_add_test_file()
81
tree = self.make_branch_and_tree('.')
82
self.__create_and_add_test_file(tree)
119
84
# Shelve the changes
120
85
self.run_bzr('shelve', '--all', retcode=3)
122
if os.path.exists(os.path.join(self.tree.branch.base,
87
if os.path.exists(os.path.join(tree.branch.base,
123
88
'.shelf/shelves/default/00')):
124
89
self.fail("Shelf exists, but it shouldn't")
126
def __create_and_add_test_file(self, filename='test_file'):
127
# write in binary mode because on win32 line-endings should be LF
128
f = open(filename, 'wb')
91
def __create_and_add_test_file(self, tree, filename='test_file'):
92
f = open(filename, 'w')
129
93
f.write(self.ORIGINAL)
131
self.tree.add(self.tree.relpath(os.path.join(os.getcwd(), filename)))
132
self.tree.commit(message='add %s' % filename)
95
tree.add(tree.relpath(os.path.join(os.getcwd(), filename)))
96
tree.commit(message='add %s' % filename)
134
98
def test_shelf_with_revision(self):
135
self.tree = self.make_branch_and_tree('.')
99
tree = self.make_branch_and_tree('.')
137
self.__create_and_add_test_file()
101
self.__create_and_add_test_file(tree)
139
103
# Modify the test file and commit it
140
104
self.build_tree_contents([('test_file', self.MODIFIED)])
141
self.tree.commit(message='update test_file')
105
tree.commit(message='update test_file')
143
107
# Shelve the changes
144
108
self.run_bzr('shelve', '--all', '-r', '1', retcode=0)
146
self._check_diff(self.DIFF_2)
110
# Check the diff is right
111
self.assertEqual(self.capture('diff', retcode=1),
112
self.DIFF_HEADER + self.DIFF_2 + '\n')
148
114
# Make sure the file is the way it should be
149
115
self.assertEqual(file('test_file').read(), self.ORIGINAL)
203
169
shown = self.capture('shelf show 00', retcode=0)
204
170
self.assertEqual(shown, shelf)
206
def test_shelf_show_unspecified(self):
207
self.tree = self.make_branch_and_tree('.')
208
self.__create_and_add_test_file()
209
self.__test_show(self.tree, '00')
210
self.__test_show(self.tree, '01')
211
self.__test_show(self.tree, '02')
213
# Check that not specifying at patch gets us the most recent
214
shelf = open(os.path.join(self.tree.basedir,
215
'.shelf/shelves/default/02')).read()
216
self.assertTrue('patch 02' in shelf)
218
# Check the shown output is right
219
shown = self.capture('shelf show', retcode=0)
220
self.assertEqual(shown, shelf)
222
172
def test_shelf_show_with_no_patch(self):
223
self.tree = self.make_branch_and_tree('.')
173
tree = self.make_branch_and_tree('.')
224
174
stderr = self.run_bzr_captured(['shelf', 'show', '00'], retcode=None)[1]
225
175
self.assertTrue("Patch '00' doesn't exist on shelf default!" in stderr)
227
177
def test_shelf_unshelve_failure(self):
228
self.tree = self.make_branch_and_tree('.')
178
tree = self.make_branch_and_tree('.')
230
self.__create_and_add_test_file()
180
self.__create_and_add_test_file(tree)
232
182
# Modify the test file
233
183
file('test_file', 'w').write(self.MODIFIED)
304
254
self.assertTrue(os.path.exists('.shelf/shelves/default/01~'))
307
lines = self.capture('shelf ls', retcode=0).split('\n')
257
list = self.capture('shelf ls', retcode=0).split('\n')
309
259
self.assertFalse(line.startswith(' 01'))
311
261
# Unshelve, if unshelve is confused by the backup it will fail
312
262
self.run_bzr('unshelve', '--all', retcode=0)
314
264
def test_shelf_delete(self):
315
self.tree = self.make_branch_and_tree('.')
265
tree = self.make_branch_and_tree('.')
317
self.__create_and_add_test_file()
318
self.__create_and_add_test_file(filename='test_file2')
267
self.__create_and_add_test_file(tree)
268
self.__create_and_add_test_file(tree, filename='test_file2')
320
270
# Modify the test files
321
# write in binary mode because on win32 line-endings should be LF
322
f = file('test_file', 'wb')
323
f.write(self.MODIFIED)
325
f = file('test_file2', 'wb')
326
f.write(self.MODIFIED)
328
new_date = _patch_header_date(self.tree,
329
self.tree.inventory.path2id('test_file'), 'test_file')
271
file('test_file', 'w').write(self.MODIFIED)
272
file('test_file2', 'w').write(self.MODIFIED)
331
274
# Shelve the changes
332
275
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
333
276
self.run_bzr('shelve', '--all', 'test_file2', retcode=0)
335
self._check_shelf('00', new_date=new_date)
338
279
self.run_bzr('shelf', 'delete', '00', retcode=0)
340
# We should now have 01 but not 00, but we should have 00~
281
# We should now have 01 but not 00
341
282
self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
342
self.assertTrue(os.path.exists('.shelf/shelves/default/00~'))
343
283
self.assertTrue(os.path.exists('.shelf/shelves/default/01'))
345
# Check the backup is right
346
self._check_shelf('00~', new_date=new_date)
349
lines = self.capture('shelf ls', retcode=0).split('\n')
286
list = self.capture('shelf ls', retcode=0).split('\n')
351
288
self.assertFalse(line.startswith(' 00'))
353
290
# Unshelve should unshelve 01
412
349
# Shelve should work now
413
350
self.run_bzr('shelve', '--all', retcode=0)
415
def test_shelf_p1_patch(self):
416
self.tree = self.make_branch_and_tree('.')
352
def test_shelf_p0_patch(self):
353
tree = self.make_branch_and_tree('.')
418
self.__create_and_add_test_file()
355
self.__create_and_add_test_file(tree)
420
357
# Run a benign shelf command to setup .shelf for us
421
358
self.run_bzr('shelf', 'ls', retcode=0)
423
old_tree = self.tree.basis_tree()
424
old_date = _patch_header_date(old_tree,
425
old_tree.inventory.path2id('test_file'),
427
new_date = _patch_header_date(self.tree,
428
self.tree.inventory.path2id('test_file'),
430
360
# Fake a -p0 shelved patch
431
diff = self.DIFF_1 % { 'filename' : 'test_file', 'old_date': old_date,
432
'new_date' : new_date}
433
diff = diff.replace('--- ', '--- a/')
434
diff = diff.replace('+++ ', '+++ b/')
362
diff = diff.replace('a/', '')
363
diff = diff.replace('b/', '')
435
364
open('.shelf/shelves/default/00', 'w').write(diff)
437
366
# This should work
438
367
self.run_bzr('unshelve', '--all', retcode=0)
369
# Check the diff is right
370
self.assertEqual(self.capture('diff', retcode=1),
371
self.DIFF_HEADER + self.DIFF_1 + '\n')
442
373
def test_shelf_shelve_in_subdir(self):
443
self.tree = self.make_branch_and_tree('.')
447
self.tree.add(subdir)
450
self.__create_and_add_test_file()
374
tree = self.make_branch_and_tree('.')
376
self.__create_and_add_test_file(tree)
452
378
# Modify the test file
453
# write in binary mode because on win32 line-endings should be LF
454
f = file('test_file', 'wb')
455
f.write(self.MODIFIED)
379
file('test_file', 'w').write(self.MODIFIED)
458
381
# Shelve the changes
459
382
self.run_bzr('shelve', '--all', retcode=0)
465
388
# Unshelve, should succeed
466
389
self.run_bzr('unshelve', '--all', retcode=0)
468
self._check_diff(filename='subdir/test_file')
470
# Make sure relative filenames work ok
471
self.run_bzr('shelve', 'test_file', '--all', retcode=0)
473
def test_shelf_shelf_bogus_subcommand(self):
474
self.tree = self.make_branch_and_tree('.')
475
self.run_bzr('shelf', 'foo', retcode=3) # <- retcode == 3
477
def test_shelf_OOO_unshelve(self):
478
self.tree = self.make_branch_and_tree('.')
480
for i in range(1, 5):
481
self.__create_and_add_test_file(filename='test_file%d' % i)
483
# Modify the test files
484
for i in range(1, 5):
485
file('test_file%d' % i, 'w').write(self.MODIFIED)
488
for i in range(1, 5):
489
self.run_bzr('shelve', '--all', 'test_file%d' % i, retcode=0)
491
# Check shelving worked
492
for i in range(1, 5):
493
self.assertEqual(file('test_file%d' % i).read(), self.ORIGINAL)
495
# We should now have 00-03
496
for i in range(0, 4):
497
self.assertTrue(os.path.exists('.shelf/shelves/default/0%d' % i))
500
self.run_bzr('unshelve', '--all', '00', retcode=0)
501
self.assertEqual(file('test_file1').read(), self.MODIFIED)
504
lines = self.capture('shelf ls', retcode=0).split('\n')
506
self.assertFalse(line.startswith(' 00'))
508
# Check we can reshelve once we've unshelved out of order, should be 04
509
self.assertFalse(os.path.exists('.shelf/shelves/default/04'))
510
self.run_bzr('shelve', '--all')
511
self.assertTrue(os.path.exists('.shelf/shelves/default/04'))
514
text = self.capture('shelf ls', retcode=0)
515
for line in text.split('\n'):
516
self.assertFalse(line.startswith(' 00'))
518
# We now have 01,02,03,04
520
self.run_bzr('unshelve', '--all', '02', retcode=0)
521
self.assertEqual(file('test_file3').read(), self.MODIFIED)
523
# Unshelve the default, this is the reshelved 00, hence modifies file 1
524
self.run_bzr('unshelve', '--all', retcode=0)
525
self.assertEqual(file('test_file1').read(), self.MODIFIED)
527
def test_shelf_switch_basic(self):
528
self.tree = self.make_branch_and_tree('.')
529
self.__create_and_add_test_file()
531
# This should go to "default"
532
file('test_file', 'w').write(self.MODIFIED)
533
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
536
self.run_bzr('shelf', 'switch', 'other', retcode=0)
537
file('test_file', 'w').write(self.MODIFIED)
538
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
541
self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
542
self.assertFalse(os.path.exists('.shelf/shelves/default/01'))
543
self.assertTrue(os.path.exists('.shelf/shelves/other/00'))
546
self.run_bzr('shelf', 'switch', 'default', retcode=0)
547
file('test_file', 'w').write(self.MODIFIED)
548
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
551
self.assertTrue(os.path.exists('.shelf/shelves/default/01'))
552
self.assertFalse(os.path.exists('.shelf/shelves/other/01'))
554
def test_shelf_bad_patch_arg(self):
555
self.tree = self.make_branch_and_tree('.')
557
# Check the bad arg handling
558
stdout, error = self.run_bzr_captured(['unshelve', '01'], retcode=3)
559
self.assertTrue("Patch '01' doesn't exist on shelf" in error)
561
stdout, error = self.run_bzr_captured(['unshelve', 'foo'], retcode=3)
562
self.assertTrue("Invalid patch name 'foo'" in error)
564
# Hex and is cracky, so it shouldn't work
565
stdout, error = self.run_bzr_captured(['unshelve', '0x00'], retcode=3)
566
self.assertTrue("Invalid patch name '0x00'" in error)
568
def test_color_hunk_selector(self):
569
"""Make sure NoColor is raised iff terminal.has_ansi_colors is False"""
570
hs = ShelveHunkSelector([])
571
hs = UnshelveHunkSelector([])
573
from bzrlib.plugins.bzrtools import terminal
575
from bzrtools import terminal
576
old_has_ansi_colors = terminal.has_ansi_colors
577
terminal.has_ansi_colors = lambda: False
579
self.assertRaises(NoColor, ShelveHunkSelector, [], True)
580
self.assertRaises(NoColor, UnshelveHunkSelector, [], True)
581
terminal.has_ansi_colors = lambda: True
582
hs = ShelveHunkSelector([], color=True)
583
hs = UnshelveHunkSelector([], color=True)
585
terminal.has_ansi_colors = old_has_ansi_colors
587
def test_no_color(self):
588
"""Ensure --no-color switch can be passed"""
589
self.tree = self.make_branch_and_tree('.')
593
self.tree.add(subdir)
596
self.__create_and_add_test_file()
598
# Modify the test file
599
# write in binary mode because on win32 line-endings should be LF
600
f = file('test_file', 'wb')
601
f.write(self.MODIFIED)
603
stdout, error = self.run_bzr_captured(['shelve', '--all',
605
stdout, error = self.run_bzr_captured(['unshelve', '--all',
391
# Check the diff is right
392
self.assertEqual(self.capture('diff', retcode=1),
393
self.DIFF_HEADER + self.DIFF_1 + '\n')