2
from bzrlib.diff import _patch_header_date
5
from bzrlib.plugins.bzrtools.hunk_selector import (
9
from bzrlib.plugins.bzrtools.errors import NoColor
10
from bzrlib.plugins.bzrtools import command
11
from bzrlib.plugins.bzrtools.command_classes import cmd_shelf1
14
class ShelfTests(bzrlib.tests.TestCaseWithTransport):
15
ORIGINAL = '\n\nhello test world\n\n'
16
MODIFIED = '\n\ngoodbye test world\n\n'
17
DIFF_HEADER = "=== modified file '%(filename)s'\n"
18
DIFF_1 = """--- %(filename)s\t%(old_date)s
19
+++ %(filename)s\t%(new_date)s
27
DIFF_2 = """--- test_file\t%(old_date)s
28
+++ test_file\t%(new_date)s
37
bzrlib.tests.TestCaseWithTransport.setUp(self)
38
command._testing = True
39
self.addCleanup(command._stop_testing)
41
def _check_diff(self, diff=DIFF_1, filename='test_file'):
42
old_tree = self.tree.basis_tree()
46
old_date = _patch_header_date(old_tree,
47
old_tree.inventory.path2id(filename),
49
new_date = _patch_header_date(self.tree,
50
self.tree.inventory.path2id(filename), filename)
54
keys = { 'filename' : filename , 'old_date': old_date,
56
hdr = self.DIFF_HEADER % keys
58
self.assertEqual(self.run_bzr('diff', retcode=1)[0], hdr + diff + '\n')
60
def _check_shelf(self, idx, diff=DIFF_1, filename='test_file',
62
old_tree = self.tree.basis_tree()
65
old_date = _patch_header_date(old_tree,
66
old_tree.inventory.path2id(filename),
70
diff = diff % { 'filename' : filename, 'old_date': old_date,
72
shelf = open(os.path.join(self.tree.basedir,
73
'.shelf/shelves/default/' + idx)).read()
74
shelf = shelf[shelf.index('\n') + 1:] # skip the message
75
self.assertEqual(shelf, diff)
80
def test_shelf_multi(self):
83
def __test_loop(self, count):
84
self.tree = self.make_branch_and_tree('.')
85
self.__create_and_add_test_file()
86
for counter in range(count):
87
# Modify the test file
88
# write in binary mode because on win32 line-endings should be
90
f = file('test_file', 'wb')
91
f.write(self.MODIFIED)
96
self.tree.lock_write()
98
new_date = _patch_header_date(self.tree,
99
self.tree.inventory.path2id('test_file'), 'test_file')
104
self.run_bzr('shelve1 --all', retcode=0)
106
# Make sure there is no diff anymore
107
self.assertEqual(self.run_bzr('diff', retcode=0)[0], '')
109
# Make sure the file is actually back the way it was
110
self.assertEqual(file('test_file').read(), self.ORIGINAL)
112
self._check_shelf('00', new_date=new_date)
115
self.run_bzr('unshelve1 --all', retcode=0)
119
# Check the shelved patch was backed up
120
self._check_shelf('00~', new_date=new_date)
122
# Make sure the file is back the way it should be
123
self.assertEqual(file('test_file').read(), self.MODIFIED)
125
def test_shelf_nothing_to_shelve(self):
127
self.tree = self.make_branch_and_tree('.')
128
self.__create_and_add_test_file()
131
self.run_bzr('shelve1 --all', retcode=3)
133
if os.path.exists(os.path.join(self.tree.branch.base,
134
'.shelf/shelves/default/00')):
135
self.fail("Shelf exists, but it shouldn't")
137
def __create_and_add_test_file(self, filename='test_file'):
138
# write in binary mode because on win32 line-endings should be LF
139
f = open(filename, 'wb')
140
f.write(self.ORIGINAL)
142
self.tree.add(self.tree.relpath(os.path.join(os.getcwd(), filename)))
143
self.tree.commit(message='add %s' % filename)
145
def test_shelf_with_revision(self):
146
self.tree = self.make_branch_and_tree('.')
148
self.__create_and_add_test_file()
150
# Modify the test file and commit it
151
self.build_tree_contents([('test_file', self.MODIFIED)])
152
self.tree.commit(message='update test_file')
155
self.run_bzr('shelve1 --all -r 1', retcode=0)
157
self._check_diff(self.DIFF_2)
159
# Make sure the file is the way it should be
160
self.assertEqual(file('test_file').read(), self.ORIGINAL)
163
self.run_bzr('unshelve1 --all', retcode=0)
165
# Make sure the file is back the way it should be
166
self.assertEqual(file('test_file').read(), self.MODIFIED)
168
def test_shelf_with_two_revisions(self):
169
self.tree = self.make_branch_and_tree('.')
171
stdout, stderr = self.run_bzr('shelve1 --all -r 1..2', retcode=None)
173
self.assertEqual(stderr.split('\n')[0],
174
'bzr: ERROR: shelve only accepts a single revision parameter.')
176
def test_shelf_show_basic(self):
177
self.tree = self.make_branch_and_tree('.')
178
self.__create_and_add_test_file()
179
self.__test_show(self.tree, '00')
181
def __test_show(self, tree, patch):
182
# Modify the test file
183
self.build_tree_contents([('test_file', 'patch %s\n' % patch)])
186
self.run_bzr('shelve1 --all', retcode=0)
188
# Make sure there is no diff anymore
189
self.assertEqual(self.run_bzr('diff', retcode=0)[0], '')
191
# Check the shelf is right
192
shelf = open(os.path.join(self.tree.basedir,
193
'.shelf/shelves/default', patch)).read()
194
self.assertTrue('patch %s' % patch in shelf)
196
# Check the shown output is right
197
shown = self.run_bzr('shelf1 show %s' % patch, retcode=0)[0]
198
self.assertEqual(shown, shelf)
200
def test_shelf_show_multi(self):
201
self.tree = self.make_branch_and_tree('.')
202
self.__create_and_add_test_file()
203
self.__test_show(self.tree, '00')
204
self.__test_show(self.tree, '01')
205
self.__test_show(self.tree, '02')
207
# Now check we can show a previously shelved patch
208
shelf = open(os.path.join(self.tree.basedir,
209
'.shelf/shelves/default/00')).read()
210
self.assertTrue('patch 00' in shelf)
212
# Check the shown output is right
213
shown = self.run_bzr('shelf1 show 00', retcode=0)[0]
214
self.assertEqual(shown, shelf)
216
def test_shelf_show_unspecified(self):
217
self.tree = self.make_branch_and_tree('.')
218
self.__create_and_add_test_file()
219
self.__test_show(self.tree, '00')
220
self.__test_show(self.tree, '01')
221
self.__test_show(self.tree, '02')
223
# Check that not specifying at patch gets us the most recent
224
shelf = open(os.path.join(self.tree.basedir,
225
'.shelf/shelves/default/02')).read()
226
self.assertTrue('patch 02' in shelf)
228
# Check the shown output is right
229
shown = self.run_bzr('shelf1 show', retcode=0)[0]
230
self.assertEqual(shown, shelf)
232
def test_shelf_show_with_no_patch(self):
233
self.tree = self.make_branch_and_tree('.')
234
stderr = self.run_bzr('shelf1 show 00', retcode=None)[1]
235
self.assertTrue("Patch '00' doesn't exist on shelf default!" in stderr)
237
def test_shelf_unshelve_failure(self):
238
self.tree = self.make_branch_and_tree('.')
240
self.__create_and_add_test_file()
242
# Modify the test file
243
file('test_file', 'w').write(self.MODIFIED)
246
self.run_bzr('shelve1 --all', retcode=0)
248
# Write an unapplyable patch into the shelf
249
shelf = open(os.path.join(self.tree.basedir,
250
'.shelf/shelves/default/00'), 'w')
251
shelf.write(self.DIFF_2)
254
# Unshelve, should fail
255
self.run_bzr('unshelve1 --all', retcode=3)
257
# Make sure the patch is still there, eventhough it's broken
258
shelf = open(os.path.join(self.tree.basedir,
259
'.shelf/shelves/default/00')).read()
260
self.assertEqual(shelf, self.DIFF_2)
262
# Working tree should be unchanged
263
diff = self.run_bzr('diff', retcode=0)[0]
264
self.assertEqual(diff, '')
266
def test_shelf_unshelve_failure_two_hunks(self):
267
self.tree = self.make_branch_and_tree('.')
269
self.__create_and_add_test_file()
270
self.__create_and_add_test_file(filename='test_file2')
272
# Modify the test files
273
file('test_file', 'w').write(self.MODIFIED)
274
file('test_file2', 'w').write(self.MODIFIED)
277
self.run_bzr('shelve1 --all', retcode=0)
279
# Put the changes to test_file back, the shelved patch won't apply now
280
file('test_file', 'w').write(self.MODIFIED)
281
self.tree.commit(message='screw up test_file')
283
# Unshelve, should fail
284
self.run_bzr('unshelve1 --all', retcode=3)
286
# Working tree should be unchanged
287
diff = self.run_bzr('diff', retcode=0)[0]
288
self.assertEqual(diff, '')
290
# Force should succeed and modify test_file2, but leave shelf
291
self.run_bzr('unshelve1 --force --all', retcode=0)
292
self.assertEqual(open('test_file2').read(), self.MODIFIED)
293
self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
295
def test_shelf_after_unshelve(self):
296
self.tree = self.make_branch_and_tree('.')
298
self.__create_and_add_test_file()
299
self.__create_and_add_test_file(filename='test_file2')
301
# Modify the test files
302
file('test_file', 'w').write(self.MODIFIED)
303
file('test_file2', 'w').write(self.MODIFIED)
306
self.run_bzr('shelve1 --all test_file', retcode=0)
307
self.run_bzr('shelve1 --all test_file2', retcode=0)
310
self.run_bzr('unshelve1 --all', retcode=0)
312
# We should now have 00 and 01~
313
self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
314
self.assertTrue(os.path.exists('.shelf/shelves/default/01~'))
317
lines = self.run_bzr('shelf1 ls', retcode=0)[0].split('\n')
319
self.assertFalse(line.startswith(' 01'))
321
# Unshelve, if unshelve is confused by the backup it will fail
322
self.run_bzr('unshelve1 --all', retcode=0)
324
def test_shelf_delete(self):
325
self.tree = self.make_branch_and_tree('.')
326
self.tree.lock_write()
328
self.__create_and_add_test_file()
329
self.__create_and_add_test_file(filename='test_file2')
331
# Modify the test files
332
# write in binary mode because on win32 line-endings should be LF
333
f = file('test_file', 'wb')
334
f.write(self.MODIFIED)
336
f = file('test_file2', 'wb')
337
f.write(self.MODIFIED)
339
new_date = _patch_header_date(self.tree,
340
self.tree.inventory.path2id('test_file'), 'test_file')
345
self.run_bzr('shelve1 --all test_file', retcode=0)
346
self.run_bzr('shelve1 --all test_file2', retcode=0)
348
self._check_shelf('00', new_date=new_date)
351
self.run_bzr('shelf1 delete 00', retcode=0)
353
# We should now have 01 but not 00, but we should have 00~
354
self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
355
self.assertTrue(os.path.exists('.shelf/shelves/default/00~'))
356
self.assertTrue(os.path.exists('.shelf/shelves/default/01'))
358
# Check the backup is right
359
self._check_shelf('00~', new_date=new_date)
362
lines = self.run_bzr('shelf1 ls', retcode=0)[0].split('\n')
364
self.assertFalse(line.startswith(' 00'))
366
# Unshelve should unshelve 01
367
self.run_bzr('unshelve1 --all', retcode=0)
368
self.assertEqual(file('test_file2').read(), self.MODIFIED)
370
def test_shelf_gaps(self):
371
self.tree = self.make_branch_and_tree('.')
372
self.__create_and_add_test_file()
373
file('test_file', 'w').write(self.MODIFIED)
374
self.run_bzr('shelve1 --all test_file', retcode=0)
375
file('test_file', 'w').write(self.MODIFIED)
376
self.run_bzr('shelve1 --all test_file', retcode=0)
378
# Now delete 00, leaving 01, next shelve should go into 02
379
self.run_bzr('shelf1 delete 0', retcode=0)
380
self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
381
self.assertFalse(os.path.exists('.shelf/shelves/default/02'))
382
file('test_file', 'w').write(self.MODIFIED)
383
self.run_bzr('shelve1 --all test_file', retcode=0)
384
self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
385
self.assertTrue(os.path.exists('.shelf/shelves/default/02'))
387
def test_shelf_upgrade(self):
388
self.tree = self.make_branch_and_tree('.')
390
self.__create_and_add_test_file()
392
# Modify then shelve, so we're not upgrading to 00, just for kicks
393
file('test_file', 'w').write(self.MODIFIED)
394
self.run_bzr('shelve1 --all test_file', retcode=0)
396
open('.bzr-shelf', 'w').write('First old shelf')
397
open('.bzr-shelf-1', 'w').write('Second old shelf')
398
open('.bzr-shelf-3', 'w').write('Fourth old shelf')
400
# shelve and unshelve should bitch and do nothing
401
file('test_file', 'w').write('blah blah blah')
402
self.run_bzr('shelve1 --all', retcode=3)
403
self.assertFalse(os.path.exists('.shelf/shelves/default/01'))
404
self.assertEqual(file('test_file').read(), 'blah blah blah')
405
self.run_bzr('unshelve1 --all', retcode=3)
406
self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
408
# Upgrade, make sure it worked
409
self.run_bzr('shelf1 upgrade', retcode=0)
410
self.assertEqual(open('.shelf/shelves/default/01').read(),
412
self.assertEqual(open('.shelf/shelves/default/02').read(),
414
self.assertEqual(open('.shelf/shelves/default/03').read(),
417
# Check the old shelves got backed up right
418
self.assertTrue(os.path.exists('.bzr-shelf~'))
419
self.assertTrue(os.path.exists('.bzr-shelf-1~'))
420
self.assertTrue(os.path.exists('.bzr-shelf-3~'))
421
self.assertFalse(os.path.exists('.bzr-shelf'))
422
self.assertFalse(os.path.exists('.bzr-shelf-1'))
423
self.assertFalse(os.path.exists('.bzr-shelf-3'))
425
# Shelve should work now
426
self.run_bzr('shelve1 --all', retcode=0)
428
def test_shelf_p1_patch(self):
429
self.tree = self.make_branch_and_tree('.')
431
self.__create_and_add_test_file()
433
# Run a benign shelf command to setup .shelf for us
434
self.run_bzr('shelf1 ls', retcode=0)
436
old_tree = self.tree.basis_tree()
438
self.tree.lock_read()
440
old_date = _patch_header_date(old_tree,
441
old_tree.inventory.path2id('test_file'),
443
new_date = _patch_header_date(self.tree,
444
self.tree.inventory.path2id('test_file'), 'test_file')
448
# Fake a -p0 shelved patch
449
diff = self.DIFF_1 % { 'filename' : 'test_file', 'old_date': old_date,
450
'new_date' : new_date}
451
diff = diff.replace('--- ', '--- a/')
452
diff = diff.replace('+++ ', '+++ b/')
453
open('.shelf/shelves/default/00', 'w').write(diff)
456
self.run_bzr('unshelve1 --all', retcode=0)
460
def test_shelf_shelve_in_subdir(self):
461
self.tree = self.make_branch_and_tree('.')
465
self.tree.add(subdir)
468
self.__create_and_add_test_file()
470
# Modify the test file
471
# write in binary mode because on win32 line-endings should be LF
472
f = file('test_file', 'wb')
473
f.write(self.MODIFIED)
477
self.run_bzr('shelve1 --all', retcode=0)
479
# Working tree should be unchanged
480
diff = self.run_bzr('diff', retcode=0)[0]
481
self.assertEqual(diff, '')
483
# Unshelve, should succeed
484
self.run_bzr('unshelve1 --all', retcode=0)
486
self._check_diff(filename='subdir/test_file')
488
# Make sure relative filenames work ok
489
self.run_bzr('shelve1 test_file --all', retcode=0)
491
def test_shelf_shelf_bogus_subcommand(self):
492
self.tree = self.make_branch_and_tree('.')
493
self.run_bzr('shelf1 foo', retcode=3) # <- retcode == 3
495
def test_shelf_OOO_unshelve(self):
496
self.tree = self.make_branch_and_tree('.')
498
for i in range(1, 5):
499
self.__create_and_add_test_file(filename='test_file%d' % i)
501
# Modify the test files
502
for i in range(1, 5):
503
file('test_file%d' % i, 'w').write(self.MODIFIED)
506
for i in range(1, 5):
507
self.run_bzr(['shelve1', '--all', 'test_file%d' % i], retcode=0)
509
# Check shelving worked
510
for i in range(1, 5):
511
self.assertEqual(file('test_file%d' % i).read(), self.ORIGINAL)
513
# We should now have 00-03
514
for i in range(0, 4):
515
self.assertTrue(os.path.exists('.shelf/shelves/default/0%d' % i))
518
self.run_bzr('unshelve1 --all 00', retcode=0)
519
self.assertEqual(file('test_file1').read(), self.MODIFIED)
522
lines = self.run_bzr('shelf1 ls', retcode=0)[0].split('\n')
524
self.assertFalse(line.startswith(' 00'))
526
# Check we can reshelve once we've unshelved out of order, should be 04
527
self.assertFalse(os.path.exists('.shelf/shelves/default/04'))
528
self.run_bzr('shelve1 --all')
529
self.assertTrue(os.path.exists('.shelf/shelves/default/04'))
532
text = self.run_bzr('shelf1 ls', retcode=0)[0]
533
for line in text.split('\n'):
534
self.assertFalse(line.startswith(' 00'))
536
# We now have 01,02,03,04
538
self.run_bzr('unshelve1 --all 02', retcode=0)
539
self.assertEqual(file('test_file3').read(), self.MODIFIED)
541
# Unshelve the default, this is the reshelved 00, hence modifies file 1
542
self.run_bzr('unshelve1 --all', retcode=0)
543
self.assertEqual(file('test_file1').read(), self.MODIFIED)
545
def test_shelf_switch_basic(self):
546
self.tree = self.make_branch_and_tree('.')
547
self.__create_and_add_test_file()
549
# This should go to "default"
550
file('test_file', 'w').write(self.MODIFIED)
551
self.run_bzr('shelve1 --all test_file', retcode=0)
554
self.run_bzr('shelf1 switch other', retcode=0)
555
file('test_file', 'w').write(self.MODIFIED)
556
self.run_bzr('shelve1 --all test_file', retcode=0)
559
self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
560
self.assertFalse(os.path.exists('.shelf/shelves/default/01'))
561
self.assertTrue(os.path.exists('.shelf/shelves/other/00'))
564
self.run_bzr('shelf1 switch default', retcode=0)
565
file('test_file', 'w').write(self.MODIFIED)
566
self.run_bzr('shelve1 --all test_file', retcode=0)
569
self.assertTrue(os.path.exists('.shelf/shelves/default/01'))
570
self.assertFalse(os.path.exists('.shelf/shelves/other/01'))
572
def test_shelf_binary(self):
573
self.tree = self.make_branch_and_tree('.')
574
self.build_tree_contents([('file', '\x00')])
575
self.tree.add('file')
576
self.run_bzr_error(['Changes involve binary files.'], 'shelve1 --all')
578
def test_shelf_bad_patch_arg(self):
579
self.tree = self.make_branch_and_tree('.')
581
# Check the bad arg handling
582
stdout, error = self.run_bzr('unshelve1 01', retcode=3)
583
self.assertTrue("Patch '01' doesn't exist on shelf" in error)
585
stdout, error = self.run_bzr('unshelve1 foo', retcode=3)
586
self.assertTrue("Invalid patch name 'foo'" in error)
588
# Hex and is cracky, so it shouldn't work
589
stdout, error = self.run_bzr(['unshelve1', '0x00'], retcode=3)
590
self.assertTrue("Invalid patch name '0x00'" in error)
592
def test_color_hunk_selector(self):
593
"""Make sure NoColor is raised iff terminal.has_ansi_colors is False"""
594
hs = ShelveHunkSelector([])
595
hs = UnshelveHunkSelector([])
597
from bzrlib.plugins.bzrtools import terminal
599
from bzrtools import terminal
600
old_has_ansi_colors = terminal.has_ansi_colors
601
terminal.has_ansi_colors = lambda: False
603
self.assertRaises(NoColor, ShelveHunkSelector, [], True)
604
self.assertRaises(NoColor, UnshelveHunkSelector, [], True)
605
terminal.has_ansi_colors = lambda: True
606
hs = ShelveHunkSelector([], color=True)
607
hs = UnshelveHunkSelector([], color=True)
609
terminal.has_ansi_colors = old_has_ansi_colors
611
def test_no_color(self):
612
"""Ensure --no-color switch can be passed"""
613
self.tree = self.make_branch_and_tree('.')
617
self.tree.add(subdir)
620
self.__create_and_add_test_file()
622
# Modify the test file
623
# write in binary mode because on win32 line-endings should be LF
624
f = file('test_file', 'wb')
625
f.write(self.MODIFIED)
627
stdout, error = self.run_bzr('shelve1 --all --no-color')
628
stdout, error = self.run_bzr('unshelve1 --all --no-color')
630
def test_shelf_help(self):
631
self.assertContainsRe(cmd_shelf1().help(),
632
'list\n.*List the patches on the current shelf')
634
def test_show_empty_shelf(self):
635
self.tree = self.make_branch_and_tree('.')
636
self.run_bzr_error(('No patches on shelf.',), 'shelf1 show')