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
30
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)
31
from bzrlib.bzrdir import BzrDir
32
tree = BzrDir.create_standalone_workingtree('.')
34
# Create a test file and commit it
35
file('test_file', 'w').write(self.ORIGINAL)
37
tree.commit(message='add test_file')
39
# Modify the test file
40
file('test_file', 'w').write(self.MODIFIED)
42
# Check the diff is right
43
self.assertEqualDiff(self.capture('diff', retcode=1),
44
self.DIFF_HEADER + self.DIFF_1 + '\n')
47
self.run_bzr('shelve', '--all', retcode=1)
49
# Make sure there is no diff anymore
50
self.assertEqual(self.capture('diff'), '')
52
# Make sure the file is actually back the way it was
53
self.assertEqualDiff(file('test_file').read(), self.ORIGINAL)
55
# Check the shelf is right
56
shelf = file('.bzr-shelf').read()
57
self.assertEqual(shelf, self.DIFF_1)
60
self.run_bzr('unshelve', retcode=1)
62
# Check the diff is right again
63
self.assertEqual(self.capture('diff', retcode=1),
64
self.DIFF_HEADER + self.DIFF_1 + '\n')
66
# Make sure the file is back the way it should be
67
self.assertEqual(file('test_file').read(), self.MODIFIED)
102
69
def test_shelf_nothing_to_shelve(self):
104
self.tree = self.make_branch_and_tree('.')
105
self.__create_and_add_test_file()
71
from bzrlib.bzrdir import BzrDir
72
tree = BzrDir.create_standalone_workingtree('.')
74
# Create a test file and commit it
75
file('test_file', 'w').write(self.ORIGINAL)
77
tree.commit(message='add test_file')
107
79
# Shelve the changes
108
self.run_bzr('shelve', '--all', retcode=3)
80
self.run_bzr('shelve', '--all')
110
if os.path.exists(os.path.join(self.tree.branch.base,
111
'.shelf/shelves/default/00')):
82
if os.path.exists('.bzr-shelf'):
112
83
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
85
def test_shelf_with_revision(self):
123
self.tree = self.make_branch_and_tree('.')
86
from bzrlib.bzrdir import BzrDir
87
tree = BzrDir.create_standalone_workingtree('.')
125
self.__create_and_add_test_file()
89
# Create a test file and commit it
90
file('test_file', 'w').write(self.ORIGINAL)
92
tree.commit(message='add test_file')
127
94
# Modify the test file and commit it
128
self.build_tree_contents([('test_file', self.MODIFIED)])
129
self.tree.commit(message='update test_file')
95
file('test_file', 'w').write(self.MODIFIED)
96
tree.commit(message='update test_file')
131
98
# Shelve the changes
132
self.run_bzr('shelve', '--all', '-r', '1', retcode=0)
99
self.run_bzr('shelve', '-r', '1', '--all', retcode=1)
134
self._check_diff(self.DIFF_2)
101
# Check the diff is right
102
self.assertEqual(self.capture('diff', retcode=1),
103
self.DIFF_HEADER + self.DIFF_2 + '\n')
136
105
# Make sure the file is the way it should be
137
106
self.assertEqual(file('test_file').read(), self.ORIGINAL)
140
self.run_bzr('unshelve', '--all', retcode=0)
109
self.run_bzr('unshelve', retcode=1)
142
111
# Make sure the file is back the way it should be
143
112
self.assertEqual(file('test_file').read(), self.MODIFIED)
145
114
def test_shelf_with_two_revisions(self):
146
self.tree = self.make_branch_and_tree('.')
115
from bzrlib.bzrdir import BzrDir
116
tree = BzrDir.create_standalone_workingtree('.')
148
cmd = 'shelve --all -r 1..2'
149
(stdout, stderr) = self.run_bzr_captured(cmd.split(), retcode=None)
118
cmd = 'shelve -r 1..2'
119
(stdout, stderr) = self.run_bzr_captured(cmd.split(), retcode=3)
151
121
self.assertEqual(stderr.split('\n')[0],
152
122
'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)
124
def test_shelf_with_whitespace(self):
125
"""Shows that bzr doesn't handle whitespace well"""
127
file('file name', 'wb').write('hello')
129
self.run_bzr('commit', '-m', 'added')
130
file('file name', 'wb').write('goodbye')
131
self.run_bzr('shelve', '--all', 'file name', retcode=1)
133
def test_whitespace_branches(self):
134
os.mkdir('name with space')
135
os.chdir('name with space')
137
file('filename', 'wb').write('hello')
139
self.run_bzr('commit', '-m', 'added')
140
file('filename', 'wb').write('goodbye')
141
self.run_bzr('shelve', '--all', 'filename', retcode=1)
144
def test_long_filename(self):
145
"""Regression test for diffstat with long filenames.
147
Create a branch with two files, one of which has a long name. Commit.
148
Modify both files. Shelve the file with the short name. If diffstat
149
has regressed, it will generate a diffstat of the file with the long
154
file(filename, 'wb').write('hello')
155
file('foo', 'wb').write('bar')
157
self.run_bzr('commit', '-m', 'added')
158
file(filename, 'wb').write('goodbye')
159
file('foo', 'wb').write('baz')
160
self.run_bzr('shelve', '--all', 'foo', retcode=1)