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)
28
56
def test_shelf(self):
29
57
self.__test_loop(1)
54
83
# Make sure the file is actually back the way it was
55
84
self.assertEqual(file('test_file').read(), self.ORIGINAL)
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)
86
self._check_shelf('00', new_date=new_date)
64
89
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')
70
93
# Check the shelved patch was backed up
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)
94
self._check_shelf('00~', new_date=new_date)
76
96
# Make sure the file is back the way it should be
77
97
self.assertEqual(file('test_file').read(), self.MODIFIED)
79
99
def test_shelf_nothing_to_shelve(self):
81
tree = self.make_branch_and_tree('.')
82
self.__create_and_add_test_file(tree)
101
self.tree = self.make_branch_and_tree('.')
102
self.__create_and_add_test_file()
84
104
# Shelve the changes
85
105
self.run_bzr('shelve', '--all', retcode=3)
87
if os.path.exists(os.path.join(tree.branch.base,
107
if os.path.exists(os.path.join(self.tree.branch.base,
88
108
'.shelf/shelves/default/00')):
89
109
self.fail("Shelf exists, but it shouldn't")
91
def __create_and_add_test_file(self, tree, filename='test_file'):
111
def __create_and_add_test_file(self, filename='test_file'):
92
112
f = open(filename, 'w')
93
113
f.write(self.ORIGINAL)
95
tree.add(tree.relpath(os.path.join(os.getcwd(), filename)))
96
tree.commit(message='add %s' % filename)
115
self.tree.add(self.tree.relpath(os.path.join(os.getcwd(), filename)))
116
self.tree.commit(message='add %s' % filename)
98
118
def test_shelf_with_revision(self):
99
tree = self.make_branch_and_tree('.')
119
self.tree = self.make_branch_and_tree('.')
101
self.__create_and_add_test_file(tree)
121
self.__create_and_add_test_file()
103
123
# Modify the test file and commit it
104
124
self.build_tree_contents([('test_file', self.MODIFIED)])
105
tree.commit(message='update test_file')
125
self.tree.commit(message='update test_file')
107
127
# Shelve the changes
108
128
self.run_bzr('shelve', '--all', '-r', '1', retcode=0)
110
# Check the diff is right
111
self.assertEqual(self.capture('diff', retcode=1),
112
self.DIFF_HEADER + self.DIFF_2 + '\n')
130
self._check_diff(self.DIFF_2)
114
132
# Make sure the file is the way it should be
115
133
self.assertEqual(file('test_file').read(), self.ORIGINAL)
154
172
self.assertEqual(shown, shelf)
156
174
def test_shelf_show_multi(self):
157
tree = self.make_branch_and_tree('.')
158
self.__create_and_add_test_file(tree)
159
self.__test_show(tree, '00')
160
self.__test_show(tree, '01')
161
self.__test_show(tree, '02')
175
self.tree = self.make_branch_and_tree('.')
176
self.__create_and_add_test_file()
177
self.__test_show(self.tree, '00')
178
self.__test_show(self.tree, '01')
179
self.__test_show(self.tree, '02')
163
181
# Now check we can show a previously shelved patch
164
shelf = open(os.path.join(tree.branch.base,
182
shelf = open(os.path.join(self.tree.basedir,
165
183
'.shelf/shelves/default/00')).read()
166
184
self.assertTrue('patch 00' in shelf)
169
187
shown = self.capture('shelf show 00', retcode=0)
170
188
self.assertEqual(shown, shelf)
190
def test_shelf_show_unspecified(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
# Check that not specifying at patch gets us the most recent
198
shelf = open(os.path.join(self.tree.basedir,
199
'.shelf/shelves/default/02')).read()
200
self.assertTrue('patch 02' in shelf)
202
# Check the shown output is right
203
shown = self.capture('shelf show', retcode=0)
204
self.assertEqual(shown, shelf)
172
206
def test_shelf_show_with_no_patch(self):
173
tree = self.make_branch_and_tree('.')
207
self.tree = self.make_branch_and_tree('.')
174
208
stderr = self.run_bzr_captured(['shelf', 'show', '00'], retcode=None)[1]
175
209
self.assertTrue("Patch '00' doesn't exist on shelf default!" in stderr)
177
211
def test_shelf_unshelve_failure(self):
178
tree = self.make_branch_and_tree('.')
212
self.tree = self.make_branch_and_tree('.')
180
self.__create_and_add_test_file(tree)
214
self.__create_and_add_test_file()
182
216
# Modify the test file
183
217
file('test_file', 'w').write(self.MODIFIED)
254
288
self.assertTrue(os.path.exists('.shelf/shelves/default/01~'))
257
list = self.capture('shelf ls', retcode=0).split('\n')
291
lines = self.capture('shelf ls', retcode=0).split('\n')
259
293
self.assertFalse(line.startswith(' 01'))
261
295
# Unshelve, if unshelve is confused by the backup it will fail
262
296
self.run_bzr('unshelve', '--all', retcode=0)
264
298
def test_shelf_delete(self):
265
tree = self.make_branch_and_tree('.')
299
self.tree = self.make_branch_and_tree('.')
267
self.__create_and_add_test_file(tree)
268
self.__create_and_add_test_file(tree, filename='test_file2')
301
self.__create_and_add_test_file()
302
self.__create_and_add_test_file(filename='test_file2')
270
304
# Modify the test files
271
305
file('test_file', 'w').write(self.MODIFIED)
272
306
file('test_file2', 'w').write(self.MODIFIED)
307
new_date = _patch_header_date(self.tree,
308
self.tree.inventory.path2id('test_file'), 'test_file')
274
310
# Shelve the changes
275
311
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
276
312
self.run_bzr('shelve', '--all', 'test_file2', retcode=0)
314
self._check_shelf('00', new_date=new_date)
279
317
self.run_bzr('shelf', 'delete', '00', retcode=0)
281
# We should now have 01 but not 00
319
# We should now have 01 but not 00, but we should have 00~
282
320
self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
321
self.assertTrue(os.path.exists('.shelf/shelves/default/00~'))
283
322
self.assertTrue(os.path.exists('.shelf/shelves/default/01'))
324
# Check the backup is right
325
self._check_shelf('00~', new_date=new_date)
286
list = self.capture('shelf ls', retcode=0).split('\n')
328
lines = self.capture('shelf ls', retcode=0).split('\n')
288
330
self.assertFalse(line.startswith(' 00'))
290
332
# Unshelve should unshelve 01
349
391
# Shelve should work now
350
392
self.run_bzr('shelve', '--all', retcode=0)
352
def test_shelf_p0_patch(self):
353
tree = self.make_branch_and_tree('.')
394
def test_shelf_p1_patch(self):
395
self.tree = self.make_branch_and_tree('.')
355
self.__create_and_add_test_file(tree)
397
self.__create_and_add_test_file()
357
399
# Run a benign shelf command to setup .shelf for us
358
400
self.run_bzr('shelf', 'ls', retcode=0)
402
old_tree = self.tree.basis_tree()
403
old_date = _patch_header_date(old_tree,
404
old_tree.inventory.path2id('test_file'),
406
new_date = _patch_header_date(self.tree,
407
self.tree.inventory.path2id('test_file'),
360
409
# Fake a -p0 shelved patch
362
diff = diff.replace('a/', '')
363
diff = diff.replace('b/', '')
410
diff = self.DIFF_1 % { 'filename' : 'test_file', 'old_date': old_date,
411
'new_date' : new_date}
412
diff = diff.replace('--- ', '--- a/')
413
diff = diff.replace('+++ ', '+++ b/')
364
414
open('.shelf/shelves/default/00', 'w').write(diff)
366
416
# This should work
367
417
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')
373
421
def test_shelf_shelve_in_subdir(self):
374
tree = self.make_branch_and_tree('.')
376
self.__create_and_add_test_file(tree)
422
self.tree = self.make_branch_and_tree('.')
426
self.tree.add(subdir)
429
self.__create_and_add_test_file()
378
431
# Modify the test file
379
432
file('test_file', 'w').write(self.MODIFIED)
388
441
# Unshelve, should succeed
389
442
self.run_bzr('unshelve', '--all', retcode=0)
391
# Check the diff is right
392
self.assertEqual(self.capture('diff', retcode=1),
393
self.DIFF_HEADER + self.DIFF_1 + '\n')
444
self._check_diff(filename='subdir/test_file')
446
# Make sure relative filenames work ok
447
self.run_bzr('shelve', 'test_file', '--all', retcode=0)
449
def test_shelf_shelf_bogus_subcommand(self):
450
self.tree = self.make_branch_and_tree('.')
451
self.run_bzr('shelf', 'foo', retcode=3) # <- retcode == 3
453
def test_shelf_OOO_unshelve(self):
454
self.tree = self.make_branch_and_tree('.')
456
for i in range(1, 5):
457
self.__create_and_add_test_file(filename='test_file%d' % i)
459
# Modify the test files
460
for i in range(1, 5):
461
file('test_file%d' % i, 'w').write(self.MODIFIED)
464
for i in range(1, 5):
465
self.run_bzr('shelve', '--all', 'test_file%d' % i, retcode=0)
467
# Check shelving worked
468
for i in range(1, 5):
469
self.assertEqual(file('test_file%d' % i).read(), self.ORIGINAL)
471
# We should now have 00-03
472
for i in range(0, 4):
473
self.assertTrue(os.path.exists('.shelf/shelves/default/0%d' % i))
476
self.run_bzr('unshelve', '--all', '00', retcode=0)
477
self.assertEqual(file('test_file1').read(), self.MODIFIED)
480
lines = self.capture('shelf ls', retcode=0).split('\n')
482
self.assertFalse(line.startswith(' 00'))
484
# Check we can reshelve once we've unshelved out of order, should be 04
485
self.assertFalse(os.path.exists('.shelf/shelves/default/04'))
486
self.run_bzr('shelve', '--all')
487
self.assertTrue(os.path.exists('.shelf/shelves/default/04'))
490
text = self.capture('shelf ls', retcode=0)
491
for line in text.split('\n'):
492
self.assertFalse(line.startswith(' 00'))
494
# We now have 01,02,03,04
496
self.run_bzr('unshelve', '--all', '02', retcode=0)
497
self.assertEqual(file('test_file3').read(), self.MODIFIED)
499
# Unshelve the default, this is the reshelved 00, hence modifies file 1
500
self.run_bzr('unshelve', '--all', retcode=0)
501
self.assertEqual(file('test_file1').read(), self.MODIFIED)
503
def test_shelf_switch_basic(self):
504
self.tree = self.make_branch_and_tree('.')
505
self.__create_and_add_test_file()
507
# This should go to "default"
508
file('test_file', 'w').write(self.MODIFIED)
509
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
512
self.run_bzr('shelf', 'switch', 'other', retcode=0)
513
file('test_file', 'w').write(self.MODIFIED)
514
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
517
self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
518
self.assertFalse(os.path.exists('.shelf/shelves/default/01'))
519
self.assertTrue(os.path.exists('.shelf/shelves/other/00'))
522
self.run_bzr('shelf', 'switch', 'default', retcode=0)
523
file('test_file', 'w').write(self.MODIFIED)
524
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
527
self.assertTrue(os.path.exists('.shelf/shelves/default/01'))
528
self.assertFalse(os.path.exists('.shelf/shelves/other/01'))
530
def test_shelf_bad_patch_arg(self):
531
self.tree = self.make_branch_and_tree('.')
533
# Check the bad arg handling
534
stdout, error = self.run_bzr_captured(['unshelve', '01'], retcode=3)
535
self.assertTrue("Patch '01' doesn't exist on shelf" in error)
537
stdout, error = self.run_bzr_captured(['unshelve', 'foo'], retcode=3)
538
self.assertTrue("Invalid patch name 'foo'" in error)
540
# Hex and is cracky, so it shouldn't work
541
stdout, error = self.run_bzr_captured(['unshelve', '0x00'], retcode=3)
542
self.assertTrue("Invalid patch name '0x00'" in error)