35
def _check_diff(self, diff=DIFF_1, filename='test_file'):
36
old_tree = self.tree.basis_tree()
40
old_date = _patch_header_date(old_tree,
41
old_tree.inventory.path2id(filename),
43
new_date = _patch_header_date(self.tree,
44
self.tree.inventory.path2id(filename), filename)
48
keys = { 'filename' : filename , 'old_date': old_date,
50
hdr = self.DIFF_HEADER % keys
52
self.assertEqual(self.capture('diff', retcode=1), hdr + diff + '\n')
54
def _check_shelf(self, idx, diff=DIFF_1, filename='test_file',
56
old_tree = self.tree.basis_tree()
59
old_date = _patch_header_date(old_tree,
60
old_tree.inventory.path2id(filename),
64
diff = diff % { 'filename' : filename, 'old_date': old_date,
66
shelf = open(os.path.join(self.tree.basedir,
67
'.shelf/shelves/default/' + idx)).read()
68
shelf = shelf[shelf.index('\n') + 1:] # skip the message
69
self.assertEqual(shelf, diff)
71
27
def test_shelf(self):
74
def test_shelf_multi(self):
77
def __test_loop(self, count):
78
self.tree = self.make_branch_and_tree('.')
79
self.__create_and_add_test_file()
80
for counter in range(count):
81
# Modify the test file
82
# write in binary mode because on win32 line-endings should be
84
f = file('test_file', 'wb')
85
f.write(self.MODIFIED)
90
self.tree.lock_write()
92
new_date = _patch_header_date(self.tree,
93
self.tree.inventory.path2id('test_file'), 'test_file')
98
self.run_bzr('shelve', '--all', retcode=0)
100
# Make sure there is no diff anymore
101
self.assertEqual(self.capture('diff', retcode=0), '')
103
# Make sure the file is actually back the way it was
104
self.assertEqual(file('test_file').read(), self.ORIGINAL)
106
self._check_shelf('00', new_date=new_date)
109
self.run_bzr('unshelve', '--all', retcode=0)
113
# Check the shelved patch was backed up
114
self._check_shelf('00~', new_date=new_date)
116
# Make sure the file is back the way it should be
117
self.assertEqual(file('test_file').read(), self.MODIFIED)
28
from bzrlib.branch import Branch
29
b = Branch.initialize('.')
31
# Create a test file and commit it
32
file('test_file', 'w').write(self.ORIGINAL)
34
b.commit(message='add test_file')
36
# Modify the test file
37
file('test_file', 'w').write(self.MODIFIED)
39
# Check the diff is right
40
self.assertEqual(self.capture('diff', retcode=1),
41
self.DIFF_HEADER + self.DIFF_1 + '\n')
44
self.run_bzr('shelve', '--all', retcode=True)
46
# Make sure there is no diff anymore
47
self.assertEqual(self.capture('diff'), '')
49
# Make sure the file is actually back the way it was
50
self.assertEqual(file('test_file').read(), self.ORIGINAL)
52
# Check the shelf is right
53
shelf = file('.bzr-shelf').read()
54
self.assertEqual(shelf, self.DIFF_1)
57
self.run_bzr('unshelve', retcode=True)
59
# Check the diff is right again
60
self.assertEqual(self.capture('diff', retcode=1),
61
self.DIFF_HEADER + self.DIFF_1 + '\n')
63
# Make sure the file is back the way it should be
64
self.assertEqual(file('test_file').read(), self.MODIFIED)
119
66
def test_shelf_nothing_to_shelve(self):
121
self.tree = self.make_branch_and_tree('.')
122
self.__create_and_add_test_file()
68
from bzrlib.branch import Branch
69
b = Branch.initialize('.')
71
# Create a test file and commit it
72
file('test_file', 'w').write(self.ORIGINAL)
74
b.commit(message='add test_file')
124
76
# Shelve the changes
125
self.run_bzr('shelve', '--all', retcode=3)
77
self.run_bzr('shelve', '--all', retcode=True)
127
if os.path.exists(os.path.join(self.tree.branch.base,
128
'.shelf/shelves/default/00')):
79
if os.path.exists('.bzr-shelf'):
129
80
self.fail("Shelf exists, but it shouldn't")
131
def __create_and_add_test_file(self, filename='test_file'):
132
# write in binary mode because on win32 line-endings should be LF
133
f = open(filename, 'wb')
134
f.write(self.ORIGINAL)
136
self.tree.add(self.tree.relpath(os.path.join(os.getcwd(), filename)))
137
self.tree.commit(message='add %s' % filename)
139
82
def test_shelf_with_revision(self):
140
self.tree = self.make_branch_and_tree('.')
83
from bzrlib.branch import Branch
84
b = Branch.initialize('.')
142
self.__create_and_add_test_file()
86
# Create a test file and commit it
87
file('test_file', 'w').write(self.ORIGINAL)
89
b.commit(message='add test_file')
144
91
# Modify the test file and commit it
145
self.build_tree_contents([('test_file', self.MODIFIED)])
146
self.tree.commit(message='update test_file')
92
file('test_file', 'w').write(self.MODIFIED)
93
b.commit(message='update test_file')
148
95
# Shelve the changes
149
self.run_bzr('shelve', '--all', '-r', '1', retcode=0)
96
self.run_bzr('shelve', '-r', '1', '--all', retcode=True)
151
self._check_diff(self.DIFF_2)
98
# Check the diff is right
99
self.assertEqual(self.capture('diff', retcode=1),
100
self.DIFF_HEADER + self.DIFF_2 + '\n')
153
102
# Make sure the file is the way it should be
154
103
self.assertEqual(file('test_file').read(), self.ORIGINAL)
157
self.run_bzr('unshelve', '--all', retcode=0)
106
self.run_bzr('unshelve', retcode=True)
159
108
# Make sure the file is back the way it should be
160
109
self.assertEqual(file('test_file').read(), self.MODIFIED)
162
111
def test_shelf_with_two_revisions(self):
163
self.tree = self.make_branch_and_tree('.')
112
from bzrlib.branch import Branch
113
b = Branch.initialize('.')
165
cmd = 'shelve --all -r 1..2'
115
cmd = 'shelve -r 1..2'
166
116
(stdout, stderr) = self.run_bzr_captured(cmd.split(), retcode=None)
168
118
self.assertEqual(stderr.split('\n')[0],
169
119
'bzr: ERROR: shelve only accepts a single revision parameter.')
171
def test_shelf_show_basic(self):
172
self.tree = self.make_branch_and_tree('.')
173
self.__create_and_add_test_file()
174
self.__test_show(self.tree, '00')
176
def __test_show(self, tree, patch):
177
# Modify the test file
178
self.build_tree_contents([('test_file', 'patch %s\n' % patch)])
181
self.run_bzr('shelve', '--all', retcode=0)
183
# Make sure there is no diff anymore
184
self.assertEqual(self.capture('diff', retcode=0), '')
186
# Check the shelf is right
187
shelf = open(os.path.join(self.tree.basedir,
188
'.shelf/shelves/default', patch)).read()
189
self.assertTrue('patch %s' % patch in shelf)
191
# Check the shown output is right
192
shown = self.capture('shelf show %s' % patch, retcode=0)
193
self.assertEqual(shown, shelf)
195
def test_shelf_show_multi(self):
196
self.tree = self.make_branch_and_tree('.')
197
self.__create_and_add_test_file()
198
self.__test_show(self.tree, '00')
199
self.__test_show(self.tree, '01')
200
self.__test_show(self.tree, '02')
202
# Now check we can show a previously shelved patch
203
shelf = open(os.path.join(self.tree.basedir,
204
'.shelf/shelves/default/00')).read()
205
self.assertTrue('patch 00' in shelf)
207
# Check the shown output is right
208
shown = self.capture('shelf show 00', retcode=0)
209
self.assertEqual(shown, shelf)
211
def test_shelf_show_unspecified(self):
212
self.tree = self.make_branch_and_tree('.')
213
self.__create_and_add_test_file()
214
self.__test_show(self.tree, '00')
215
self.__test_show(self.tree, '01')
216
self.__test_show(self.tree, '02')
218
# Check that not specifying at patch gets us the most recent
219
shelf = open(os.path.join(self.tree.basedir,
220
'.shelf/shelves/default/02')).read()
221
self.assertTrue('patch 02' in shelf)
223
# Check the shown output is right
224
shown = self.capture('shelf show', retcode=0)
225
self.assertEqual(shown, shelf)
227
def test_shelf_show_with_no_patch(self):
228
self.tree = self.make_branch_and_tree('.')
229
stderr = self.run_bzr_captured(['shelf', 'show', '00'], retcode=None)[1]
230
self.assertTrue("Patch '00' doesn't exist on shelf default!" in stderr)
232
def test_shelf_unshelve_failure(self):
233
self.tree = self.make_branch_and_tree('.')
235
self.__create_and_add_test_file()
237
# Modify the test file
238
file('test_file', 'w').write(self.MODIFIED)
241
self.run_bzr('shelve', '--all', retcode=0)
243
# Write an unapplyable patch into the shelf
244
shelf = open(os.path.join(self.tree.basedir,
245
'.shelf/shelves/default/00'), 'w')
246
shelf.write(self.DIFF_2)
249
# Unshelve, should fail
250
self.run_bzr('unshelve', '--all', retcode=3)
252
# Make sure the patch is still there, eventhough it's broken
253
shelf = open(os.path.join(self.tree.basedir,
254
'.shelf/shelves/default/00')).read()
255
self.assertEqual(shelf, self.DIFF_2)
257
# Working tree should be unchanged
258
diff = self.capture('diff', retcode=0)
259
self.assertEqual(diff, '')
261
def test_shelf_unshelve_failure_two_hunks(self):
262
self.tree = self.make_branch_and_tree('.')
264
self.__create_and_add_test_file()
265
self.__create_and_add_test_file(filename='test_file2')
267
# Modify the test files
268
file('test_file', 'w').write(self.MODIFIED)
269
file('test_file2', 'w').write(self.MODIFIED)
272
self.run_bzr('shelve', '--all', retcode=0)
274
# Put the changes to test_file back, the shelved patch won't apply now
275
file('test_file', 'w').write(self.MODIFIED)
276
self.tree.commit(message='screw up test_file')
278
# Unshelve, should fail
279
self.run_bzr('unshelve', '--all', retcode=3)
281
# Working tree should be unchanged
282
diff = self.capture('diff', retcode=0)
283
self.assertEqual(diff, '')
285
# Force should succeed and modify test_file2, but leave shelf
286
self.run_bzr('unshelve', '--force', '--all', retcode=0)
287
self.assertEqual(open('test_file2').read(), self.MODIFIED)
288
self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
290
def test_shelf_after_unshelve(self):
291
self.tree = self.make_branch_and_tree('.')
293
self.__create_and_add_test_file()
294
self.__create_and_add_test_file(filename='test_file2')
296
# Modify the test files
297
file('test_file', 'w').write(self.MODIFIED)
298
file('test_file2', 'w').write(self.MODIFIED)
301
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
302
self.run_bzr('shelve', '--all', 'test_file2', retcode=0)
305
self.run_bzr('unshelve', '--all', retcode=0)
307
# We should now have 00 and 01~
308
self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
309
self.assertTrue(os.path.exists('.shelf/shelves/default/01~'))
312
lines = self.capture('shelf ls', retcode=0).split('\n')
314
self.assertFalse(line.startswith(' 01'))
316
# Unshelve, if unshelve is confused by the backup it will fail
317
self.run_bzr('unshelve', '--all', retcode=0)
319
def test_shelf_delete(self):
320
self.tree = self.make_branch_and_tree('.')
321
self.tree.lock_write()
323
self.__create_and_add_test_file()
324
self.__create_and_add_test_file(filename='test_file2')
326
# Modify the test files
327
# write in binary mode because on win32 line-endings should be LF
328
f = file('test_file', 'wb')
329
f.write(self.MODIFIED)
331
f = file('test_file2', 'wb')
332
f.write(self.MODIFIED)
334
new_date = _patch_header_date(self.tree,
335
self.tree.inventory.path2id('test_file'), 'test_file')
340
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
341
self.run_bzr('shelve', '--all', 'test_file2', retcode=0)
343
self._check_shelf('00', new_date=new_date)
346
self.run_bzr('shelf', 'delete', '00', retcode=0)
348
# We should now have 01 but not 00, but we should have 00~
349
self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
350
self.assertTrue(os.path.exists('.shelf/shelves/default/00~'))
351
self.assertTrue(os.path.exists('.shelf/shelves/default/01'))
353
# Check the backup is right
354
self._check_shelf('00~', new_date=new_date)
357
lines = self.capture('shelf ls', retcode=0).split('\n')
359
self.assertFalse(line.startswith(' 00'))
361
# Unshelve should unshelve 01
362
self.run_bzr('unshelve', '--all', retcode=0)
363
self.assertEqual(file('test_file2').read(), self.MODIFIED)
365
def test_shelf_gaps(self):
366
self.tree = self.make_branch_and_tree('.')
367
self.__create_and_add_test_file()
368
file('test_file', 'w').write(self.MODIFIED)
369
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
370
file('test_file', 'w').write(self.MODIFIED)
371
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
373
# Now delete 00, leaving 01, next shelve should go into 02
374
self.run_bzr('shelf', 'delete', '0', retcode=0)
375
self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
376
self.assertFalse(os.path.exists('.shelf/shelves/default/02'))
377
file('test_file', 'w').write(self.MODIFIED)
378
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
379
self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
380
self.assertTrue(os.path.exists('.shelf/shelves/default/02'))
382
def test_shelf_upgrade(self):
383
self.tree = self.make_branch_and_tree('.')
385
self.__create_and_add_test_file()
387
# Modify then shelve, so we're not upgrading to 00, just for kicks
388
file('test_file', 'w').write(self.MODIFIED)
389
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
391
open('.bzr-shelf', 'w').write('First old shelf')
392
open('.bzr-shelf-1', 'w').write('Second old shelf')
393
open('.bzr-shelf-3', 'w').write('Fourth old shelf')
395
# shelve and unshelve should bitch and do nothing
396
file('test_file', 'w').write('blah blah blah')
397
self.run_bzr('shelve', '--all', retcode=3)
398
self.assertFalse(os.path.exists('.shelf/shelves/default/01'))
399
self.assertEqual(file('test_file').read(), 'blah blah blah')
400
self.run_bzr('unshelve', '--all', retcode=3)
401
self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
403
# Upgrade, make sure it worked
404
self.run_bzr('shelf', 'upgrade', retcode=0)
405
self.assertEqual(open('.shelf/shelves/default/01').read(),
407
self.assertEqual(open('.shelf/shelves/default/02').read(),
409
self.assertEqual(open('.shelf/shelves/default/03').read(),
412
# Check the old shelves got backed up right
413
self.assertTrue(os.path.exists('.bzr-shelf~'))
414
self.assertTrue(os.path.exists('.bzr-shelf-1~'))
415
self.assertTrue(os.path.exists('.bzr-shelf-3~'))
416
self.assertFalse(os.path.exists('.bzr-shelf'))
417
self.assertFalse(os.path.exists('.bzr-shelf-1'))
418
self.assertFalse(os.path.exists('.bzr-shelf-3'))
420
# Shelve should work now
421
self.run_bzr('shelve', '--all', retcode=0)
423
def test_shelf_p1_patch(self):
424
self.tree = self.make_branch_and_tree('.')
426
self.__create_and_add_test_file()
428
# Run a benign shelf command to setup .shelf for us
429
self.run_bzr('shelf', 'ls', retcode=0)
431
old_tree = self.tree.basis_tree()
433
self.tree.lock_read()
435
old_date = _patch_header_date(old_tree,
436
old_tree.inventory.path2id('test_file'),
438
new_date = _patch_header_date(self.tree,
439
self.tree.inventory.path2id('test_file'), 'test_file')
443
# Fake a -p0 shelved patch
444
diff = self.DIFF_1 % { 'filename' : 'test_file', 'old_date': old_date,
445
'new_date' : new_date}
446
diff = diff.replace('--- ', '--- a/')
447
diff = diff.replace('+++ ', '+++ b/')
448
open('.shelf/shelves/default/00', 'w').write(diff)
451
self.run_bzr('unshelve', '--all', retcode=0)
455
def test_shelf_shelve_in_subdir(self):
456
self.tree = self.make_branch_and_tree('.')
460
self.tree.add(subdir)
463
self.__create_and_add_test_file()
465
# Modify the test file
466
# write in binary mode because on win32 line-endings should be LF
467
f = file('test_file', 'wb')
468
f.write(self.MODIFIED)
472
self.run_bzr('shelve', '--all', retcode=0)
474
# Working tree should be unchanged
475
diff = self.capture('diff', retcode=0)
476
self.assertEqual(diff, '')
478
# Unshelve, should succeed
479
self.run_bzr('unshelve', '--all', retcode=0)
481
self._check_diff(filename='subdir/test_file')
483
# Make sure relative filenames work ok
484
self.run_bzr('shelve', 'test_file', '--all', retcode=0)
486
def test_shelf_shelf_bogus_subcommand(self):
487
self.tree = self.make_branch_and_tree('.')
488
self.run_bzr('shelf', 'foo', retcode=3) # <- retcode == 3
490
def test_shelf_OOO_unshelve(self):
491
self.tree = self.make_branch_and_tree('.')
493
for i in range(1, 5):
494
self.__create_and_add_test_file(filename='test_file%d' % i)
496
# Modify the test files
497
for i in range(1, 5):
498
file('test_file%d' % i, 'w').write(self.MODIFIED)
501
for i in range(1, 5):
502
self.run_bzr('shelve', '--all', 'test_file%d' % i, retcode=0)
504
# Check shelving worked
505
for i in range(1, 5):
506
self.assertEqual(file('test_file%d' % i).read(), self.ORIGINAL)
508
# We should now have 00-03
509
for i in range(0, 4):
510
self.assertTrue(os.path.exists('.shelf/shelves/default/0%d' % i))
513
self.run_bzr('unshelve', '--all', '00', retcode=0)
514
self.assertEqual(file('test_file1').read(), self.MODIFIED)
517
lines = self.capture('shelf ls', retcode=0).split('\n')
519
self.assertFalse(line.startswith(' 00'))
521
# Check we can reshelve once we've unshelved out of order, should be 04
522
self.assertFalse(os.path.exists('.shelf/shelves/default/04'))
523
self.run_bzr('shelve', '--all')
524
self.assertTrue(os.path.exists('.shelf/shelves/default/04'))
527
text = self.capture('shelf ls', retcode=0)
528
for line in text.split('\n'):
529
self.assertFalse(line.startswith(' 00'))
531
# We now have 01,02,03,04
533
self.run_bzr('unshelve', '--all', '02', retcode=0)
534
self.assertEqual(file('test_file3').read(), self.MODIFIED)
536
# Unshelve the default, this is the reshelved 00, hence modifies file 1
537
self.run_bzr('unshelve', '--all', retcode=0)
538
self.assertEqual(file('test_file1').read(), self.MODIFIED)
540
def test_shelf_switch_basic(self):
541
self.tree = self.make_branch_and_tree('.')
542
self.__create_and_add_test_file()
544
# This should go to "default"
545
file('test_file', 'w').write(self.MODIFIED)
546
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
549
self.run_bzr('shelf', 'switch', 'other', retcode=0)
550
file('test_file', 'w').write(self.MODIFIED)
551
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
554
self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
555
self.assertFalse(os.path.exists('.shelf/shelves/default/01'))
556
self.assertTrue(os.path.exists('.shelf/shelves/other/00'))
559
self.run_bzr('shelf', 'switch', 'default', retcode=0)
560
file('test_file', 'w').write(self.MODIFIED)
561
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
564
self.assertTrue(os.path.exists('.shelf/shelves/default/01'))
565
self.assertFalse(os.path.exists('.shelf/shelves/other/01'))
567
def test_shelf_bad_patch_arg(self):
568
self.tree = self.make_branch_and_tree('.')
570
# Check the bad arg handling
571
stdout, error = self.run_bzr_captured(['unshelve', '01'], retcode=3)
572
self.assertTrue("Patch '01' doesn't exist on shelf" in error)
574
stdout, error = self.run_bzr_captured(['unshelve', 'foo'], retcode=3)
575
self.assertTrue("Invalid patch name 'foo'" in error)
577
# Hex and is cracky, so it shouldn't work
578
stdout, error = self.run_bzr_captured(['unshelve', '0x00'], retcode=3)
579
self.assertTrue("Invalid patch name '0x00'" in error)
581
def test_color_hunk_selector(self):
582
"""Make sure NoColor is raised iff terminal.has_ansi_colors is False"""
583
hs = ShelveHunkSelector([])
584
hs = UnshelveHunkSelector([])
586
from bzrlib.plugins.bzrtools import terminal
588
from bzrtools import terminal
589
old_has_ansi_colors = terminal.has_ansi_colors
590
terminal.has_ansi_colors = lambda: False
592
self.assertRaises(NoColor, ShelveHunkSelector, [], True)
593
self.assertRaises(NoColor, UnshelveHunkSelector, [], True)
594
terminal.has_ansi_colors = lambda: True
595
hs = ShelveHunkSelector([], color=True)
596
hs = UnshelveHunkSelector([], color=True)
598
terminal.has_ansi_colors = old_has_ansi_colors
600
def test_no_color(self):
601
"""Ensure --no-color switch can be passed"""
602
self.tree = self.make_branch_and_tree('.')
606
self.tree.add(subdir)
609
self.__create_and_add_test_file()
611
# Modify the test file
612
# write in binary mode because on win32 line-endings should be LF
613
f = file('test_file', 'wb')
614
f.write(self.MODIFIED)
616
stdout, error = self.run_bzr_captured(['shelve', '--all',
618
stdout, error = self.run_bzr_captured(['unshelve', '--all',
621
def test_shelf_help(self):
622
self.assertContainsRe(cmd_shelf().help(),
623
'list\n.*List the patches on the current shelf')