29
def _check_diff(self, diff=DIFF_1, filename='test_file'):
30
old_tree = self.tree.basis_tree()
31
old_date = _patch_header_date(old_tree,
32
old_tree.inventory.path2id(filename),
34
new_date = _patch_header_date(self.tree,
35
self.tree.inventory.path2id(filename),
37
keys = { 'filename' : filename , 'old_date': old_date,
39
hdr = self.DIFF_HEADER % keys
41
self.assertEqual(self.capture('diff', retcode=1), hdr + diff + '\n')
43
def _check_shelf(self, idx, diff=DIFF_1, filename='test_file',
45
old_tree = self.tree.basis_tree()
46
old_date = _patch_header_date(old_tree,
47
old_tree.inventory.path2id(filename),
49
diff = diff % { 'filename' : filename, 'old_date': old_date,
51
shelf = open(os.path.join(self.tree.basedir,
52
'.shelf/shelves/default/' + idx)).read()
53
shelf = shelf[shelf.index('\n') + 1:] # skip the message
54
self.assertEqual(shelf, diff)
56
27
def test_shelf(self):
59
def test_shelf_multi(self):
62
def __test_loop(self, count):
63
self.tree = self.make_branch_and_tree('.')
64
self.__create_and_add_test_file()
69
# Modify the test file
70
# write in binary mode because on win32 line-endings should be LF
71
f = file('test_file', 'wb')
72
f.write(self.MODIFIED)
77
new_date = _patch_header_date(self.tree,
78
self.tree.inventory.path2id('test_file'), 'test_file')
81
self.run_bzr('shelve', '--all', retcode=0)
83
# Make sure there is no diff anymore
84
self.assertEqual(self.capture('diff', retcode=0), '')
86
# Make sure the file is actually back the way it was
87
self.assertEqual(file('test_file').read(), self.ORIGINAL)
89
self._check_shelf('00', new_date=new_date)
92
self.run_bzr('unshelve', '--all', retcode=0)
96
# Check the shelved patch was backed up
97
self._check_shelf('00~', new_date=new_date)
99
# Make sure the file is back the way it should be
100
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'),
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'),
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)
102
66
def test_shelf_nothing_to_shelve(self):
104
self.tree = self.make_branch_and_tree('.')
105
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')
107
76
# Shelve the changes
108
self.run_bzr('shelve', '--all', retcode=3)
77
self.run_bzr('shelve', '--all', retcode=True)
110
if os.path.exists(os.path.join(self.tree.branch.base,
111
'.shelf/shelves/default/00')):
79
if os.path.exists('.bzr-shelf'):
112
80
self.fail("Shelf exists, but it shouldn't")
114
def __create_and_add_test_file(self, filename='test_file'):
115
# write in binary mode because on win32 line-endings should be LF
116
f = open(filename, 'wb')
117
f.write(self.ORIGINAL)
119
self.tree.add(self.tree.relpath(os.path.join(os.getcwd(), filename)))
120
self.tree.commit(message='add %s' % filename)
122
82
def test_shelf_with_revision(self):
123
self.tree = self.make_branch_and_tree('.')
83
from bzrlib.branch import Branch
84
b = Branch.initialize('.')
125
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')
127
91
# Modify the test file and commit it
128
self.build_tree_contents([('test_file', self.MODIFIED)])
129
self.tree.commit(message='update test_file')
92
file('test_file', 'w').write(self.MODIFIED)
93
b.commit(message='update test_file')
131
95
# Shelve the changes
132
self.run_bzr('shelve', '--all', '-r', '1', retcode=0)
96
self.run_bzr('shelve', '-r', '1', '--all', retcode=True)
134
self._check_diff(self.DIFF_2)
98
# Check the diff is right
99
self.assertEqual(self.capture('diff'),
100
self.DIFF_HEADER + self.DIFF_2 + '\n')
136
102
# Make sure the file is the way it should be
137
103
self.assertEqual(file('test_file').read(), self.ORIGINAL)
140
self.run_bzr('unshelve', '--all', retcode=0)
106
self.run_bzr('unshelve', retcode=True)
142
108
# Make sure the file is back the way it should be
143
109
self.assertEqual(file('test_file').read(), self.MODIFIED)
145
111
def test_shelf_with_two_revisions(self):
146
self.tree = self.make_branch_and_tree('.')
112
from bzrlib.branch import Branch
113
b = Branch.initialize('.')
148
cmd = 'shelve --all -r 1..2'
115
cmd = 'shelve -r 1..2'
149
116
(stdout, stderr) = self.run_bzr_captured(cmd.split(), retcode=None)
151
118
self.assertEqual(stderr.split('\n')[0],
152
119
'bzr: ERROR: shelve only accepts a single revision parameter.')
154
def test_shelf_show_basic(self):
155
self.tree = self.make_branch_and_tree('.')
156
self.__create_and_add_test_file()
157
self.__test_show(self.tree, '00')
159
def __test_show(self, tree, patch):
160
# Modify the test file
161
self.build_tree_contents([('test_file', 'patch %s\n' % patch)])
164
self.run_bzr('shelve', '--all', retcode=0)
166
# Make sure there is no diff anymore
167
self.assertEqual(self.capture('diff', retcode=0), '')
169
# Check the shelf is right
170
shelf = open(os.path.join(self.tree.basedir,
171
'.shelf/shelves/default', patch)).read()
172
self.assertTrue('patch %s' % patch in shelf)
174
# Check the shown output is right
175
shown = self.capture('shelf show %s' % patch, retcode=0)
176
self.assertEqual(shown, shelf)
178
def test_shelf_show_multi(self):
179
self.tree = self.make_branch_and_tree('.')
180
self.__create_and_add_test_file()
181
self.__test_show(self.tree, '00')
182
self.__test_show(self.tree, '01')
183
self.__test_show(self.tree, '02')
185
# Now check we can show a previously shelved patch
186
shelf = open(os.path.join(self.tree.basedir,
187
'.shelf/shelves/default/00')).read()
188
self.assertTrue('patch 00' in shelf)
190
# Check the shown output is right
191
shown = self.capture('shelf show 00', retcode=0)
192
self.assertEqual(shown, shelf)
194
def test_shelf_show_unspecified(self):
195
self.tree = self.make_branch_and_tree('.')
196
self.__create_and_add_test_file()
197
self.__test_show(self.tree, '00')
198
self.__test_show(self.tree, '01')
199
self.__test_show(self.tree, '02')
201
# Check that not specifying at patch gets us the most recent
202
shelf = open(os.path.join(self.tree.basedir,
203
'.shelf/shelves/default/02')).read()
204
self.assertTrue('patch 02' in shelf)
206
# Check the shown output is right
207
shown = self.capture('shelf show', retcode=0)
208
self.assertEqual(shown, shelf)
210
def test_shelf_show_with_no_patch(self):
211
self.tree = self.make_branch_and_tree('.')
212
stderr = self.run_bzr_captured(['shelf', 'show', '00'], retcode=None)[1]
213
self.assertTrue("Patch '00' doesn't exist on shelf default!" in stderr)
215
def test_shelf_unshelve_failure(self):
216
self.tree = self.make_branch_and_tree('.')
218
self.__create_and_add_test_file()
220
# Modify the test file
221
file('test_file', 'w').write(self.MODIFIED)
224
self.run_bzr('shelve', '--all', retcode=0)
226
# Write an unapplyable patch into the shelf
227
shelf = open(os.path.join(self.tree.basedir,
228
'.shelf/shelves/default/00'), 'w')
229
shelf.write(self.DIFF_2)
232
# Unshelve, should fail
233
self.run_bzr('unshelve', '--all', retcode=3)
235
# Make sure the patch is still there, eventhough it's broken
236
shelf = open(os.path.join(self.tree.basedir,
237
'.shelf/shelves/default/00')).read()
238
self.assertEqual(shelf, self.DIFF_2)
240
# Working tree should be unchanged
241
diff = self.capture('diff', retcode=0)
242
self.assertEqual(diff, '')
244
def test_shelf_unshelve_failure_two_hunks(self):
245
self.tree = self.make_branch_and_tree('.')
247
self.__create_and_add_test_file()
248
self.__create_and_add_test_file(filename='test_file2')
250
# Modify the test files
251
file('test_file', 'w').write(self.MODIFIED)
252
file('test_file2', 'w').write(self.MODIFIED)
255
self.run_bzr('shelve', '--all', retcode=0)
257
# Put the changes to test_file back, the shelved patch won't apply now
258
file('test_file', 'w').write(self.MODIFIED)
259
self.tree.commit(message='screw up test_file')
261
# Unshelve, should fail
262
self.run_bzr('unshelve', '--all', retcode=3)
264
# Working tree should be unchanged
265
diff = self.capture('diff', retcode=0)
266
self.assertEqual(diff, '')
268
# Force should succeed and modify test_file2, but leave shelf
269
self.run_bzr('unshelve', '--force', '--all', retcode=0)
270
self.assertEqual(open('test_file2').read(), self.MODIFIED)
271
self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
273
def test_shelf_after_unshelve(self):
274
self.tree = self.make_branch_and_tree('.')
276
self.__create_and_add_test_file()
277
self.__create_and_add_test_file(filename='test_file2')
279
# Modify the test files
280
file('test_file', 'w').write(self.MODIFIED)
281
file('test_file2', 'w').write(self.MODIFIED)
284
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
285
self.run_bzr('shelve', '--all', 'test_file2', retcode=0)
288
self.run_bzr('unshelve', '--all', retcode=0)
290
# We should now have 00 and 01~
291
self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
292
self.assertTrue(os.path.exists('.shelf/shelves/default/01~'))
295
lines = self.capture('shelf ls', retcode=0).split('\n')
297
self.assertFalse(line.startswith(' 01'))
299
# Unshelve, if unshelve is confused by the backup it will fail
300
self.run_bzr('unshelve', '--all', retcode=0)
302
def test_shelf_delete(self):
303
self.tree = self.make_branch_and_tree('.')
305
self.__create_and_add_test_file()
306
self.__create_and_add_test_file(filename='test_file2')
308
# Modify the test files
309
# write in binary mode because on win32 line-endings should be LF
310
f = file('test_file', 'wb')
311
f.write(self.MODIFIED)
313
f = file('test_file2', 'wb')
314
f.write(self.MODIFIED)
316
new_date = _patch_header_date(self.tree,
317
self.tree.inventory.path2id('test_file'), 'test_file')
320
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
321
self.run_bzr('shelve', '--all', 'test_file2', retcode=0)
323
self._check_shelf('00', new_date=new_date)
326
self.run_bzr('shelf', 'delete', '00', retcode=0)
328
# We should now have 01 but not 00, but we should have 00~
329
self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
330
self.assertTrue(os.path.exists('.shelf/shelves/default/00~'))
331
self.assertTrue(os.path.exists('.shelf/shelves/default/01'))
333
# Check the backup is right
334
self._check_shelf('00~', new_date=new_date)
337
lines = self.capture('shelf ls', retcode=0).split('\n')
339
self.assertFalse(line.startswith(' 00'))
341
# Unshelve should unshelve 01
342
self.run_bzr('unshelve', '--all', retcode=0)
343
self.assertEqual(file('test_file2').read(), self.MODIFIED)
345
def test_shelf_gaps(self):
346
self.tree = self.make_branch_and_tree('.')
347
self.__create_and_add_test_file()
348
file('test_file', 'w').write(self.MODIFIED)
349
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
350
file('test_file', 'w').write(self.MODIFIED)
351
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
353
# Now delete 00, leaving 01, next shelve should go into 02
354
self.run_bzr('shelf', 'delete', '0', retcode=0)
355
self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
356
self.assertFalse(os.path.exists('.shelf/shelves/default/02'))
357
file('test_file', 'w').write(self.MODIFIED)
358
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
359
self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
360
self.assertTrue(os.path.exists('.shelf/shelves/default/02'))
362
def test_shelf_upgrade(self):
363
self.tree = self.make_branch_and_tree('.')
365
self.__create_and_add_test_file()
367
# Modify then shelve, so we're not upgrading to 00, just for kicks
368
file('test_file', 'w').write(self.MODIFIED)
369
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
371
open('.bzr-shelf', 'w').write('First old shelf')
372
open('.bzr-shelf-1', 'w').write('Second old shelf')
373
open('.bzr-shelf-3', 'w').write('Fourth old shelf')
375
# shelve and unshelve should bitch and do nothing
376
file('test_file', 'w').write('blah blah blah')
377
self.run_bzr('shelve', '--all', retcode=3)
378
self.assertFalse(os.path.exists('.shelf/shelves/default/01'))
379
self.assertEqual(file('test_file').read(), 'blah blah blah')
380
self.run_bzr('unshelve', '--all', retcode=3)
381
self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
383
# Upgrade, make sure it worked
384
self.run_bzr('shelf', 'upgrade', retcode=0)
385
self.assertEqual(open('.shelf/shelves/default/01').read(),
387
self.assertEqual(open('.shelf/shelves/default/02').read(),
389
self.assertEqual(open('.shelf/shelves/default/03').read(),
392
# Check the old shelves got backed up right
393
self.assertTrue(os.path.exists('.bzr-shelf~'))
394
self.assertTrue(os.path.exists('.bzr-shelf-1~'))
395
self.assertTrue(os.path.exists('.bzr-shelf-3~'))
396
self.assertFalse(os.path.exists('.bzr-shelf'))
397
self.assertFalse(os.path.exists('.bzr-shelf-1'))
398
self.assertFalse(os.path.exists('.bzr-shelf-3'))
400
# Shelve should work now
401
self.run_bzr('shelve', '--all', retcode=0)
403
def test_shelf_p1_patch(self):
404
self.tree = self.make_branch_and_tree('.')
406
self.__create_and_add_test_file()
408
# Run a benign shelf command to setup .shelf for us
409
self.run_bzr('shelf', 'ls', retcode=0)
411
old_tree = self.tree.basis_tree()
412
old_date = _patch_header_date(old_tree,
413
old_tree.inventory.path2id('test_file'),
415
new_date = _patch_header_date(self.tree,
416
self.tree.inventory.path2id('test_file'),
418
# Fake a -p0 shelved patch
419
diff = self.DIFF_1 % { 'filename' : 'test_file', 'old_date': old_date,
420
'new_date' : new_date}
421
diff = diff.replace('--- ', '--- a/')
422
diff = diff.replace('+++ ', '+++ b/')
423
open('.shelf/shelves/default/00', 'w').write(diff)
426
self.run_bzr('unshelve', '--all', retcode=0)
430
def test_shelf_shelve_in_subdir(self):
431
self.tree = self.make_branch_and_tree('.')
435
self.tree.add(subdir)
438
self.__create_and_add_test_file()
440
# Modify the test file
441
# write in binary mode because on win32 line-endings should be LF
442
f = file('test_file', 'wb')
443
f.write(self.MODIFIED)
447
self.run_bzr('shelve', '--all', retcode=0)
449
# Working tree should be unchanged
450
diff = self.capture('diff', retcode=0)
451
self.assertEqual(diff, '')
453
# Unshelve, should succeed
454
self.run_bzr('unshelve', '--all', retcode=0)
456
self._check_diff(filename='subdir/test_file')
458
# Make sure relative filenames work ok
459
self.run_bzr('shelve', 'test_file', '--all', retcode=0)
461
def test_shelf_shelf_bogus_subcommand(self):
462
self.tree = self.make_branch_and_tree('.')
463
self.run_bzr('shelf', 'foo', retcode=3) # <- retcode == 3
465
def test_shelf_OOO_unshelve(self):
466
self.tree = self.make_branch_and_tree('.')
468
for i in range(1, 5):
469
self.__create_and_add_test_file(filename='test_file%d' % i)
471
# Modify the test files
472
for i in range(1, 5):
473
file('test_file%d' % i, 'w').write(self.MODIFIED)
476
for i in range(1, 5):
477
self.run_bzr('shelve', '--all', 'test_file%d' % i, retcode=0)
479
# Check shelving worked
480
for i in range(1, 5):
481
self.assertEqual(file('test_file%d' % i).read(), self.ORIGINAL)
483
# We should now have 00-03
484
for i in range(0, 4):
485
self.assertTrue(os.path.exists('.shelf/shelves/default/0%d' % i))
488
self.run_bzr('unshelve', '--all', '00', retcode=0)
489
self.assertEqual(file('test_file1').read(), self.MODIFIED)
492
lines = self.capture('shelf ls', retcode=0).split('\n')
494
self.assertFalse(line.startswith(' 00'))
496
# Check we can reshelve once we've unshelved out of order, should be 04
497
self.assertFalse(os.path.exists('.shelf/shelves/default/04'))
498
self.run_bzr('shelve', '--all')
499
self.assertTrue(os.path.exists('.shelf/shelves/default/04'))
502
text = self.capture('shelf ls', retcode=0)
503
for line in text.split('\n'):
504
self.assertFalse(line.startswith(' 00'))
506
# We now have 01,02,03,04
508
self.run_bzr('unshelve', '--all', '02', retcode=0)
509
self.assertEqual(file('test_file3').read(), self.MODIFIED)
511
# Unshelve the default, this is the reshelved 00, hence modifies file 1
512
self.run_bzr('unshelve', '--all', retcode=0)
513
self.assertEqual(file('test_file1').read(), self.MODIFIED)
515
def test_shelf_switch_basic(self):
516
self.tree = self.make_branch_and_tree('.')
517
self.__create_and_add_test_file()
519
# This should go to "default"
520
file('test_file', 'w').write(self.MODIFIED)
521
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
524
self.run_bzr('shelf', 'switch', 'other', retcode=0)
525
file('test_file', 'w').write(self.MODIFIED)
526
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
529
self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
530
self.assertFalse(os.path.exists('.shelf/shelves/default/01'))
531
self.assertTrue(os.path.exists('.shelf/shelves/other/00'))
534
self.run_bzr('shelf', 'switch', 'default', retcode=0)
535
file('test_file', 'w').write(self.MODIFIED)
536
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
539
self.assertTrue(os.path.exists('.shelf/shelves/default/01'))
540
self.assertFalse(os.path.exists('.shelf/shelves/other/01'))
542
def test_shelf_bad_patch_arg(self):
543
self.tree = self.make_branch_and_tree('.')
545
# Check the bad arg handling
546
stdout, error = self.run_bzr_captured(['unshelve', '01'], retcode=3)
547
self.assertTrue("Patch '01' doesn't exist on shelf" in error)
549
stdout, error = self.run_bzr_captured(['unshelve', 'foo'], retcode=3)
550
self.assertTrue("Invalid patch name 'foo'" in error)
552
# Hex and is cracky, so it shouldn't work
553
stdout, error = self.run_bzr_captured(['unshelve', '0x00'], retcode=3)
554
self.assertTrue("Invalid patch name '0x00'" in error)