1
from bzrlib.diff import _patch_header_date
6
from bzrlib.plugins.bzrtools.hunk_selector import (
10
from bzrlib.plugins.bzrtools.errors import NoColor
12
from bzrtools.hunk_selector import (
16
from bzrtools.errors import NoColor
19
class ShelfTests(bzrlib.tests.TestCaseWithTransport):
20
ORIGINAL = '\n\nhello test world\n\n'
21
MODIFIED = '\n\ngoodbye test world\n\n'
22
DIFF_HEADER = "=== modified file '%(filename)s'\n"
23
DIFF_1 = """--- %(filename)s\t%(old_date)s
24
+++ %(filename)s\t%(new_date)s
32
DIFF_2 = """--- test_file\t%(old_date)s
33
+++ test_file\t%(new_date)s
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)
71
def test_shelf_multi(self):
74
def __test_loop(self, count):
75
self.tree = self.make_branch_and_tree('.')
76
self.__create_and_add_test_file()
81
# Modify the test file
82
# write in binary mode because on win32 line-endings should be LF
83
f = file('test_file', 'wb')
84
f.write(self.MODIFIED)
89
new_date = _patch_header_date(self.tree,
90
self.tree.inventory.path2id('test_file'), 'test_file')
93
self.run_bzr('shelve', '--all', retcode=0)
95
# Make sure there is no diff anymore
96
self.assertEqual(self.capture('diff', retcode=0), '')
98
# Make sure the file is actually back the way it was
99
self.assertEqual(file('test_file').read(), self.ORIGINAL)
101
self._check_shelf('00', new_date=new_date)
104
self.run_bzr('unshelve', '--all', retcode=0)
108
# Check the shelved patch was backed up
109
self._check_shelf('00~', new_date=new_date)
111
# Make sure the file is back the way it should be
112
self.assertEqual(file('test_file').read(), self.MODIFIED)
114
def test_shelf_nothing_to_shelve(self):
116
self.tree = self.make_branch_and_tree('.')
117
self.__create_and_add_test_file()
120
self.run_bzr('shelve', '--all', retcode=3)
122
if os.path.exists(os.path.join(self.tree.branch.base,
123
'.shelf/shelves/default/00')):
124
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')
129
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)
134
def test_shelf_with_revision(self):
135
self.tree = self.make_branch_and_tree('.')
137
self.__create_and_add_test_file()
139
# Modify the test file and commit it
140
self.build_tree_contents([('test_file', self.MODIFIED)])
141
self.tree.commit(message='update test_file')
144
self.run_bzr('shelve', '--all', '-r', '1', retcode=0)
146
self._check_diff(self.DIFF_2)
148
# Make sure the file is the way it should be
149
self.assertEqual(file('test_file').read(), self.ORIGINAL)
152
self.run_bzr('unshelve', '--all', retcode=0)
154
# Make sure the file is back the way it should be
155
self.assertEqual(file('test_file').read(), self.MODIFIED)
157
def test_shelf_with_two_revisions(self):
158
self.tree = self.make_branch_and_tree('.')
160
cmd = 'shelve --all -r 1..2'
161
(stdout, stderr) = self.run_bzr_captured(cmd.split(), retcode=None)
163
self.assertEqual(stderr.split('\n')[0],
164
'bzr: ERROR: shelve only accepts a single revision parameter.')
166
def test_shelf_show_basic(self):
167
self.tree = self.make_branch_and_tree('.')
168
self.__create_and_add_test_file()
169
self.__test_show(self.tree, '00')
171
def __test_show(self, tree, patch):
172
# Modify the test file
173
self.build_tree_contents([('test_file', 'patch %s\n' % patch)])
176
self.run_bzr('shelve', '--all', retcode=0)
178
# Make sure there is no diff anymore
179
self.assertEqual(self.capture('diff', retcode=0), '')
181
# Check the shelf is right
182
shelf = open(os.path.join(self.tree.basedir,
183
'.shelf/shelves/default', patch)).read()
184
self.assertTrue('patch %s' % patch in shelf)
186
# Check the shown output is right
187
shown = self.capture('shelf show %s' % patch, retcode=0)
188
self.assertEqual(shown, shelf)
190
def test_shelf_show_multi(self):
191
self.tree = self.make_branch_and_tree('.')
192
self.__create_and_add_test_file()
193
self.__test_show(self.tree, '00')
194
self.__test_show(self.tree, '01')
195
self.__test_show(self.tree, '02')
197
# Now check we can show a previously shelved patch
198
shelf = open(os.path.join(self.tree.basedir,
199
'.shelf/shelves/default/00')).read()
200
self.assertTrue('patch 00' in shelf)
202
# Check the shown output is right
203
shown = self.capture('shelf show 00', retcode=0)
204
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
def test_shelf_show_with_no_patch(self):
223
self.tree = self.make_branch_and_tree('.')
224
stderr = self.run_bzr_captured(['shelf', 'show', '00'], retcode=None)[1]
225
self.assertTrue("Patch '00' doesn't exist on shelf default!" in stderr)
227
def test_shelf_unshelve_failure(self):
228
self.tree = self.make_branch_and_tree('.')
230
self.__create_and_add_test_file()
232
# Modify the test file
233
file('test_file', 'w').write(self.MODIFIED)
236
self.run_bzr('shelve', '--all', retcode=0)
238
# Write an unapplyable patch into the shelf
239
shelf = open(os.path.join(self.tree.basedir,
240
'.shelf/shelves/default/00'), 'w')
241
shelf.write(self.DIFF_2)
244
# Unshelve, should fail
245
self.run_bzr('unshelve', '--all', retcode=3)
247
# Make sure the patch is still there, eventhough it's broken
248
shelf = open(os.path.join(self.tree.basedir,
249
'.shelf/shelves/default/00')).read()
250
self.assertEqual(shelf, self.DIFF_2)
252
# Working tree should be unchanged
253
diff = self.capture('diff', retcode=0)
254
self.assertEqual(diff, '')
256
def test_shelf_unshelve_failure_two_hunks(self):
257
self.tree = self.make_branch_and_tree('.')
259
self.__create_and_add_test_file()
260
self.__create_and_add_test_file(filename='test_file2')
262
# Modify the test files
263
file('test_file', 'w').write(self.MODIFIED)
264
file('test_file2', 'w').write(self.MODIFIED)
267
self.run_bzr('shelve', '--all', retcode=0)
269
# Put the changes to test_file back, the shelved patch won't apply now
270
file('test_file', 'w').write(self.MODIFIED)
271
self.tree.commit(message='screw up test_file')
273
# Unshelve, should fail
274
self.run_bzr('unshelve', '--all', retcode=3)
276
# Working tree should be unchanged
277
diff = self.capture('diff', retcode=0)
278
self.assertEqual(diff, '')
280
# Force should succeed and modify test_file2, but leave shelf
281
self.run_bzr('unshelve', '--force', '--all', retcode=0)
282
self.assertEqual(open('test_file2').read(), self.MODIFIED)
283
self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
285
def test_shelf_after_unshelve(self):
286
self.tree = self.make_branch_and_tree('.')
288
self.__create_and_add_test_file()
289
self.__create_and_add_test_file(filename='test_file2')
291
# Modify the test files
292
file('test_file', 'w').write(self.MODIFIED)
293
file('test_file2', 'w').write(self.MODIFIED)
296
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
297
self.run_bzr('shelve', '--all', 'test_file2', retcode=0)
300
self.run_bzr('unshelve', '--all', retcode=0)
302
# We should now have 00 and 01~
303
self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
304
self.assertTrue(os.path.exists('.shelf/shelves/default/01~'))
307
lines = self.capture('shelf ls', retcode=0).split('\n')
309
self.assertFalse(line.startswith(' 01'))
311
# Unshelve, if unshelve is confused by the backup it will fail
312
self.run_bzr('unshelve', '--all', retcode=0)
314
def test_shelf_delete(self):
315
self.tree = self.make_branch_and_tree('.')
317
self.__create_and_add_test_file()
318
self.__create_and_add_test_file(filename='test_file2')
320
# 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')
332
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
333
self.run_bzr('shelve', '--all', 'test_file2', retcode=0)
335
self._check_shelf('00', new_date=new_date)
338
self.run_bzr('shelf', 'delete', '00', retcode=0)
340
# We should now have 01 but not 00, but we should have 00~
341
self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
342
self.assertTrue(os.path.exists('.shelf/shelves/default/00~'))
343
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')
351
self.assertFalse(line.startswith(' 00'))
353
# Unshelve should unshelve 01
354
self.run_bzr('unshelve', '--all', retcode=0)
355
self.assertEqual(file('test_file2').read(), self.MODIFIED)
357
def test_shelf_gaps(self):
358
self.tree = self.make_branch_and_tree('.')
359
self.__create_and_add_test_file()
360
file('test_file', 'w').write(self.MODIFIED)
361
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
362
file('test_file', 'w').write(self.MODIFIED)
363
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
365
# Now delete 00, leaving 01, next shelve should go into 02
366
self.run_bzr('shelf', 'delete', '0', retcode=0)
367
self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
368
self.assertFalse(os.path.exists('.shelf/shelves/default/02'))
369
file('test_file', 'w').write(self.MODIFIED)
370
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
371
self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
372
self.assertTrue(os.path.exists('.shelf/shelves/default/02'))
374
def test_shelf_upgrade(self):
375
self.tree = self.make_branch_and_tree('.')
377
self.__create_and_add_test_file()
379
# Modify then shelve, so we're not upgrading to 00, just for kicks
380
file('test_file', 'w').write(self.MODIFIED)
381
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
383
open('.bzr-shelf', 'w').write('First old shelf')
384
open('.bzr-shelf-1', 'w').write('Second old shelf')
385
open('.bzr-shelf-3', 'w').write('Fourth old shelf')
387
# shelve and unshelve should bitch and do nothing
388
file('test_file', 'w').write('blah blah blah')
389
self.run_bzr('shelve', '--all', retcode=3)
390
self.assertFalse(os.path.exists('.shelf/shelves/default/01'))
391
self.assertEqual(file('test_file').read(), 'blah blah blah')
392
self.run_bzr('unshelve', '--all', retcode=3)
393
self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
395
# Upgrade, make sure it worked
396
self.run_bzr('shelf', 'upgrade', retcode=0)
397
self.assertEqual(open('.shelf/shelves/default/01').read(),
399
self.assertEqual(open('.shelf/shelves/default/02').read(),
401
self.assertEqual(open('.shelf/shelves/default/03').read(),
404
# Check the old shelves got backed up right
405
self.assertTrue(os.path.exists('.bzr-shelf~'))
406
self.assertTrue(os.path.exists('.bzr-shelf-1~'))
407
self.assertTrue(os.path.exists('.bzr-shelf-3~'))
408
self.assertFalse(os.path.exists('.bzr-shelf'))
409
self.assertFalse(os.path.exists('.bzr-shelf-1'))
410
self.assertFalse(os.path.exists('.bzr-shelf-3'))
412
# Shelve should work now
413
self.run_bzr('shelve', '--all', retcode=0)
415
def test_shelf_p1_patch(self):
416
self.tree = self.make_branch_and_tree('.')
418
self.__create_and_add_test_file()
420
# Run a benign shelf command to setup .shelf for us
421
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
# 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/')
435
open('.shelf/shelves/default/00', 'w').write(diff)
438
self.run_bzr('unshelve', '--all', retcode=0)
442
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()
452
# 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)
459
self.run_bzr('shelve', '--all', retcode=0)
461
# Working tree should be unchanged
462
diff = self.capture('diff', retcode=0)
463
self.assertEqual(diff, '')
465
# Unshelve, should succeed
466
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',