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
file('test_file', 'w').write(self.MODIFIED)
74
new_date = _patch_header_date(self.tree,
75
self.tree.inventory.path2id('test_file'), 'test_file')
78
self.run_bzr('shelve', '--all', retcode=0)
80
# Make sure there is no diff anymore
81
self.assertEqual(self.capture('diff', retcode=0), '')
83
# Make sure the file is actually back the way it was
84
self.assertEqual(file('test_file').read(), self.ORIGINAL)
86
self._check_shelf('00', new_date=new_date)
89
self.run_bzr('unshelve', '--all', retcode=0)
93
# Check the shelved patch was backed up
94
self._check_shelf('00~', new_date=new_date)
96
# Make sure the file is back the way it should be
97
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)
99
69
def test_shelf_nothing_to_shelve(self):
101
self.tree = self.make_branch_and_tree('.')
102
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')
104
79
# Shelve the changes
105
self.run_bzr('shelve', '--all', retcode=3)
80
self.run_bzr('shelve', '--all')
107
if os.path.exists(os.path.join(self.tree.branch.base,
108
'.shelf/shelves/default/00')):
82
if os.path.exists('.bzr-shelf'):
109
83
self.fail("Shelf exists, but it shouldn't")
111
def __create_and_add_test_file(self, filename='test_file'):
112
f = open(filename, 'w')
113
f.write(self.ORIGINAL)
115
self.tree.add(self.tree.relpath(os.path.join(os.getcwd(), filename)))
116
self.tree.commit(message='add %s' % filename)
118
85
def test_shelf_with_revision(self):
119
self.tree = self.make_branch_and_tree('.')
86
from bzrlib.bzrdir import BzrDir
87
tree = BzrDir.create_standalone_workingtree('.')
121
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')
123
94
# Modify the test file and commit it
124
self.build_tree_contents([('test_file', self.MODIFIED)])
125
self.tree.commit(message='update test_file')
95
file('test_file', 'w').write(self.MODIFIED)
96
tree.commit(message='update test_file')
127
98
# Shelve the changes
128
self.run_bzr('shelve', '--all', '-r', '1', retcode=0)
99
self.run_bzr('shelve', '-r', '1', '--all', retcode=1)
130
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')
132
105
# Make sure the file is the way it should be
133
106
self.assertEqual(file('test_file').read(), self.ORIGINAL)
136
self.run_bzr('unshelve', '--all', retcode=0)
109
self.run_bzr('unshelve', retcode=1)
138
111
# Make sure the file is back the way it should be
139
112
self.assertEqual(file('test_file').read(), self.MODIFIED)
141
114
def test_shelf_with_two_revisions(self):
142
self.tree = self.make_branch_and_tree('.')
115
from bzrlib.bzrdir import BzrDir
116
tree = BzrDir.create_standalone_workingtree('.')
144
cmd = 'shelve --all -r 1..2'
145
(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)
147
121
self.assertEqual(stderr.split('\n')[0],
148
122
'bzr: ERROR: shelve only accepts a single revision parameter.')
150
def test_shelf_show_basic(self):
151
self.tree = self.make_branch_and_tree('.')
152
self.__create_and_add_test_file()
153
self.__test_show(self.tree, '00')
155
def __test_show(self, tree, patch):
156
# Modify the test file
157
self.build_tree_contents([('test_file', 'patch %s\n' % patch)])
160
self.run_bzr('shelve', '--all', retcode=0)
162
# Make sure there is no diff anymore
163
self.assertEqual(self.capture('diff', retcode=0), '')
165
# Check the shelf is right
166
shelf = open(os.path.join(self.tree.basedir,
167
'.shelf/shelves/default', patch)).read()
168
self.assertTrue('patch %s' % patch in shelf)
170
# Check the shown output is right
171
shown = self.capture('shelf show %s' % patch, retcode=0)
172
self.assertEqual(shown, shelf)
174
def test_shelf_show_multi(self):
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')
181
# Now check we can show a previously shelved patch
182
shelf = open(os.path.join(self.tree.basedir,
183
'.shelf/shelves/default/00')).read()
184
self.assertTrue('patch 00' in shelf)
186
# Check the shown output is right
187
shown = self.capture('shelf show 00', retcode=0)
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)
206
def test_shelf_show_with_no_patch(self):
207
self.tree = self.make_branch_and_tree('.')
208
stderr = self.run_bzr_captured(['shelf', 'show', '00'], retcode=None)[1]
209
self.assertTrue("Patch '00' doesn't exist on shelf default!" in stderr)
211
def test_shelf_unshelve_failure(self):
212
self.tree = self.make_branch_and_tree('.')
214
self.__create_and_add_test_file()
216
# Modify the test file
217
file('test_file', 'w').write(self.MODIFIED)
220
self.run_bzr('shelve', '--all', retcode=0)
222
# Write an unapplyable patch into the shelf
223
shelf = open(os.path.join(self.tree.basedir,
224
'.shelf/shelves/default/00'), 'w')
225
shelf.write(self.DIFF_2)
228
# Unshelve, should fail
229
self.run_bzr('unshelve', '--all', retcode=3)
231
# Make sure the patch is still there, eventhough it's broken
232
shelf = open(os.path.join(self.tree.basedir,
233
'.shelf/shelves/default/00')).read()
234
self.assertEqual(shelf, self.DIFF_2)
236
# Working tree should be unchanged
237
diff = self.capture('diff', retcode=0)
238
self.assertEqual(diff, '')
240
def test_shelf_unshelve_failure_two_hunks(self):
241
self.tree = self.make_branch_and_tree('.')
243
self.__create_and_add_test_file()
244
self.__create_and_add_test_file(filename='test_file2')
246
# Modify the test files
247
file('test_file', 'w').write(self.MODIFIED)
248
file('test_file2', 'w').write(self.MODIFIED)
251
self.run_bzr('shelve', '--all', retcode=0)
253
# Put the changes to test_file back, the shelved patch won't apply now
254
file('test_file', 'w').write(self.MODIFIED)
255
self.tree.commit(message='screw up test_file')
257
# Unshelve, should fail
258
self.run_bzr('unshelve', '--all', retcode=3)
260
# Working tree should be unchanged
261
diff = self.capture('diff', retcode=0)
262
self.assertEqual(diff, '')
264
# Force should succeed and modify test_file2, but leave shelf
265
self.run_bzr('unshelve', '--force', '--all', retcode=0)
266
self.assertEqual(open('test_file2').read(), self.MODIFIED)
267
self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
269
def test_shelf_after_unshelve(self):
270
self.tree = self.make_branch_and_tree('.')
272
self.__create_and_add_test_file()
273
self.__create_and_add_test_file(filename='test_file2')
275
# Modify the test files
276
file('test_file', 'w').write(self.MODIFIED)
277
file('test_file2', 'w').write(self.MODIFIED)
280
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
281
self.run_bzr('shelve', '--all', 'test_file2', retcode=0)
284
self.run_bzr('unshelve', '--all', retcode=0)
286
# We should now have 00 and 01~
287
self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
288
self.assertTrue(os.path.exists('.shelf/shelves/default/01~'))
291
lines = self.capture('shelf ls', retcode=0).split('\n')
293
self.assertFalse(line.startswith(' 01'))
295
# Unshelve, if unshelve is confused by the backup it will fail
296
self.run_bzr('unshelve', '--all', retcode=0)
298
def test_shelf_delete(self):
299
self.tree = self.make_branch_and_tree('.')
301
self.__create_and_add_test_file()
302
self.__create_and_add_test_file(filename='test_file2')
304
# Modify the test files
305
file('test_file', 'w').write(self.MODIFIED)
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')
311
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
312
self.run_bzr('shelve', '--all', 'test_file2', retcode=0)
314
self._check_shelf('00', new_date=new_date)
317
self.run_bzr('shelf', 'delete', '00', retcode=0)
319
# We should now have 01 but not 00, but we should have 00~
320
self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
321
self.assertTrue(os.path.exists('.shelf/shelves/default/00~'))
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)
328
lines = self.capture('shelf ls', retcode=0).split('\n')
330
self.assertFalse(line.startswith(' 00'))
332
# Unshelve should unshelve 01
333
self.run_bzr('unshelve', '--all', retcode=0)
334
self.assertEqual(file('test_file2').read(), self.MODIFIED)
336
def test_shelf_gaps(self):
337
self.tree = self.make_branch_and_tree('.')
338
self.__create_and_add_test_file()
339
file('test_file', 'w').write(self.MODIFIED)
340
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
341
file('test_file', 'w').write(self.MODIFIED)
342
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
344
# Now delete 00, leaving 01, next shelve should go into 02
345
self.run_bzr('shelf', 'delete', '0', retcode=0)
346
self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
347
self.assertFalse(os.path.exists('.shelf/shelves/default/02'))
348
file('test_file', 'w').write(self.MODIFIED)
349
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
350
self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
351
self.assertTrue(os.path.exists('.shelf/shelves/default/02'))
353
def test_shelf_upgrade(self):
354
self.tree = self.make_branch_and_tree('.')
356
self.__create_and_add_test_file()
358
# Modify then shelve, so we're not upgrading to 00, just for kicks
359
file('test_file', 'w').write(self.MODIFIED)
360
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
362
open('.bzr-shelf', 'w').write('First old shelf')
363
open('.bzr-shelf-1', 'w').write('Second old shelf')
364
open('.bzr-shelf-3', 'w').write('Fourth old shelf')
366
# shelve and unshelve should bitch and do nothing
367
file('test_file', 'w').write('blah blah blah')
368
self.run_bzr('shelve', '--all', retcode=3)
369
self.assertFalse(os.path.exists('.shelf/shelves/default/01'))
370
self.assertEqual(file('test_file').read(), 'blah blah blah')
371
self.run_bzr('unshelve', '--all', retcode=3)
372
self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
374
# Upgrade, make sure it worked
375
self.run_bzr('shelf', 'upgrade', retcode=0)
376
self.assertEqual(open('.shelf/shelves/default/01').read(),
378
self.assertEqual(open('.shelf/shelves/default/02').read(),
380
self.assertEqual(open('.shelf/shelves/default/03').read(),
383
# Check the old shelves got backed up right
384
self.assertTrue(os.path.exists('.bzr-shelf~'))
385
self.assertTrue(os.path.exists('.bzr-shelf-1~'))
386
self.assertTrue(os.path.exists('.bzr-shelf-3~'))
387
self.assertFalse(os.path.exists('.bzr-shelf'))
388
self.assertFalse(os.path.exists('.bzr-shelf-1'))
389
self.assertFalse(os.path.exists('.bzr-shelf-3'))
391
# Shelve should work now
392
self.run_bzr('shelve', '--all', retcode=0)
394
def test_shelf_p1_patch(self):
395
self.tree = self.make_branch_and_tree('.')
397
self.__create_and_add_test_file()
399
# Run a benign shelf command to setup .shelf for us
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'),
409
# Fake a -p0 shelved patch
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/')
414
open('.shelf/shelves/default/00', 'w').write(diff)
417
self.run_bzr('unshelve', '--all', retcode=0)
421
def test_shelf_shelve_in_subdir(self):
422
self.tree = self.make_branch_and_tree('.')
426
self.tree.add(subdir)
429
self.__create_and_add_test_file()
431
# Modify the test file
432
file('test_file', 'w').write(self.MODIFIED)
435
self.run_bzr('shelve', '--all', retcode=0)
437
# Working tree should be unchanged
438
diff = self.capture('diff', retcode=0)
439
self.assertEqual(diff, '')
441
# Unshelve, should succeed
442
self.run_bzr('unshelve', '--all', retcode=0)
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)
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)