30
28
def test_shelf(self):
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)
31
def test_shelf_multi(self):
34
def __test_loop(self, count):
35
tree = self.make_branch_and_tree('.')
36
self.__create_and_add_test_file(tree)
41
# Modify the test file
42
file('test_file', 'w').write(self.MODIFIED)
44
# Check the diff is right
45
self.assertEqual(self.capture('diff', retcode=1),
46
self.DIFF_HEADER + self.DIFF_1 + '\n')
49
self.run_bzr('shelve', '--all', retcode=0)
51
# Make sure there is no diff anymore
52
self.assertEqual(self.capture('diff', retcode=0), '')
54
# Make sure the file is actually back the way it was
55
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)
64
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
# 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)
76
# Make sure the file is back the way it should be
77
self.assertEqual(file('test_file').read(), self.MODIFIED)
69
79
def test_shelf_nothing_to_shelve(self):
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')
81
tree = self.make_branch_and_tree('.')
82
self.__create_and_add_test_file(tree)
79
84
# Shelve the changes
80
self.run_bzr('shelve', '--all')
85
self.run_bzr('shelve', '--all', retcode=3)
82
if os.path.exists('.bzr-shelf'):
87
if os.path.exists(os.path.join(tree.branch.base,
88
'.shelf/shelves/default/00')):
83
89
self.fail("Shelf exists, but it shouldn't")
91
def __create_and_add_test_file(self, tree, filename='test_file'):
92
self.build_tree_contents([(filename, self.ORIGINAL)])
94
tree.commit(message='add %s' % filename)
85
96
def test_shelf_with_revision(self):
86
from bzrlib.bzrdir import BzrDir
87
tree = BzrDir.create_standalone_workingtree('.')
97
tree = self.make_branch_and_tree('.')
89
# Create a test file and commit it
90
file('test_file', 'w').write(self.ORIGINAL)
92
tree.commit(message='add test_file')
99
self.__create_and_add_test_file(tree)
94
101
# Modify the test file and commit it
95
file('test_file', 'w').write(self.MODIFIED)
102
self.build_tree_contents([('test_file', self.MODIFIED)])
96
103
tree.commit(message='update test_file')
98
105
# Shelve the changes
99
self.run_bzr('shelve', '-r', '1', '--all', retcode=1)
106
self.run_bzr('shelve', '--all', '-r', '1', retcode=0)
101
108
# Check the diff is right
102
109
self.assertEqual(self.capture('diff', retcode=1),
106
113
self.assertEqual(file('test_file').read(), self.ORIGINAL)
109
self.run_bzr('unshelve', retcode=1)
116
self.run_bzr('unshelve', '--all', retcode=0)
111
118
# Make sure the file is back the way it should be
112
119
self.assertEqual(file('test_file').read(), self.MODIFIED)
114
121
def test_shelf_with_two_revisions(self):
115
from bzrlib.bzrdir import BzrDir
116
tree = BzrDir.create_standalone_workingtree('.')
122
tree = self.make_branch_and_tree('.')
118
cmd = 'shelve -r 1..2'
119
(stdout, stderr) = self.run_bzr_captured(cmd.split(), retcode=3)
124
cmd = 'shelve --all -r 1..2'
125
(stdout, stderr) = self.run_bzr_captured(cmd.split(), retcode=None)
121
127
self.assertEqual(stderr.split('\n')[0],
122
128
'bzr: ERROR: shelve only accepts a single revision parameter.')
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)
130
def test_shelf_show_basic(self):
131
tree = self.make_branch_and_tree('.')
132
self.__create_and_add_test_file(tree)
133
self.__test_show(tree, '00')
135
def __test_show(self, tree, patch):
136
# Modify the test file
137
self.build_tree_contents([('test_file', 'patch %s\n' % patch)])
140
self.run_bzr('shelve', '--all', retcode=0)
142
# Make sure there is no diff anymore
143
self.assertEqual(self.capture('diff', retcode=0), '')
145
# Check the shelf is right
146
shelf = open(os.path.join(tree.branch.base,
147
'.shelf/shelves/default', patch)).read()
148
self.assertTrue('patch %s' % patch in shelf)
150
# Check the shown output is right
151
shown = self.capture('shelf show %s' % patch, retcode=0)
152
self.assertEqual(shown, shelf)
154
def test_shelf_show_multi(self):
155
tree = self.make_branch_and_tree('.')
156
self.__create_and_add_test_file(tree)
157
self.__test_show(tree, '00')
158
self.__test_show(tree, '01')
159
self.__test_show(tree, '02')
161
# Now check we can show a previously shelved patch
162
shelf = open(os.path.join(tree.branch.base,
163
'.shelf/shelves/default/00')).read()
164
self.assertTrue('patch 00' in shelf)
166
# Check the shown output is right
167
shown = self.capture('shelf show 00', retcode=0)
168
self.assertEqual(shown, shelf)
170
def test_shelf_show_with_no_patch(self):
171
tree = self.make_branch_and_tree('.')
172
stderr = self.run_bzr_captured(['shelf', 'show', '00'], retcode=None)[1]
173
self.assertTrue("Patch '00' doesn't exist on shelf default!" in stderr)
175
def test_shelf_unshelve_failure(self):
176
tree = self.make_branch_and_tree('.')
178
self.__create_and_add_test_file(tree)
180
# Modify the test file
181
file('test_file', 'w').write(self.MODIFIED)
184
self.run_bzr('shelve', '--all', retcode=0)
186
# Write an unapplyable patch into the shelf
187
shelf = open(os.path.join(tree.branch.base,
188
'.shelf/shelves/default/00'), 'w')
189
shelf.write(self.DIFF_2)
192
# Unshelve, should fail
193
self.run_bzr('unshelve', '--all', retcode=3)
195
# Make sure the patch is still there, eventhough it's broken
196
shelf = open(os.path.join(tree.branch.base,
197
'.shelf/shelves/default/00')).read()
198
self.assertEqual(shelf, self.DIFF_2)
200
# Working tree should be unchanged
201
diff = self.capture('diff', retcode=0)
202
self.assertEqual(diff, '')
204
def test_shelf_unshelve_failure_two_hunks(self):
205
tree = self.make_branch_and_tree('.')
207
self.__create_and_add_test_file(tree)
208
self.__create_and_add_test_file(tree, filename='test_file2')
210
# Modify the test files
211
file('test_file', 'w').write(self.MODIFIED)
212
file('test_file2', 'w').write(self.MODIFIED)
215
self.run_bzr('shelve', '--all', retcode=0)
217
# Put the changes to test_file back, the shelved patch won't apply now
218
file('test_file', 'w').write(self.MODIFIED)
219
tree.commit(message='screw up test_file')
221
# Unshelve, should fail
222
self.run_bzr('unshelve', '--all', retcode=3)
224
# Working tree should be unchanged
225
diff = self.capture('diff', retcode=0)
226
self.assertEqual(diff, '')
228
# Force should succeed and modify test_file2, but leave shelf
229
self.run_bzr('unshelve', '--force', '--all', retcode=0)
230
self.assertEqual(open('test_file2').read(), self.MODIFIED)
231
self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
233
def test_shelf_after_unshelve(self):
234
tree = self.make_branch_and_tree('.')
236
self.__create_and_add_test_file(tree)
237
self.__create_and_add_test_file(tree, filename='test_file2')
239
# Modify the test files
240
file('test_file', 'w').write(self.MODIFIED)
241
file('test_file2', 'w').write(self.MODIFIED)
244
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
245
self.run_bzr('shelve', '--all', 'test_file2', retcode=0)
248
self.run_bzr('unshelve', '--all', retcode=0)
250
# We should now have 00 and 01~
251
self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
252
self.assertTrue(os.path.exists('.shelf/shelves/default/01~'))
255
list = self.capture('shelf ls', retcode=0).split('\n')
257
self.assertFalse(line.startswith(' 01'))
259
# Unshelve, if unshelve is confused by the backup it will fail
260
self.run_bzr('unshelve', '--all', retcode=0)
262
def test_shelf_delete(self):
263
tree = self.make_branch_and_tree('.')
265
self.__create_and_add_test_file(tree)
266
self.__create_and_add_test_file(tree, filename='test_file2')
268
# Modify the test files
269
file('test_file', 'w').write(self.MODIFIED)
270
file('test_file2', 'w').write(self.MODIFIED)
273
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
274
self.run_bzr('shelve', '--all', 'test_file2', retcode=0)
277
self.run_bzr('shelf', 'delete', '00', retcode=0)
279
# We should now have 01 but not 00
280
self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
281
self.assertTrue(os.path.exists('.shelf/shelves/default/01'))
284
list = self.capture('shelf ls', retcode=0).split('\n')
286
self.assertFalse(line.startswith(' 00'))
288
# Unshelve should unshelve 01
289
self.run_bzr('unshelve', '--all', retcode=0)
290
self.assertEqual(file('test_file2').read(), self.MODIFIED)
292
def test_shelf_gaps(self):
293
tree = self.make_branch_and_tree('.')
294
self.__create_and_add_test_file(tree)
295
file('test_file', 'w').write(self.MODIFIED)
296
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
297
file('test_file', 'w').write(self.MODIFIED)
298
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
300
# Now delete 00, leaving 01, next shelve should go into 02
301
self.run_bzr('shelf', 'delete', '0', retcode=0)
302
self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
303
self.assertFalse(os.path.exists('.shelf/shelves/default/02'))
304
file('test_file', 'w').write(self.MODIFIED)
305
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
306
self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
307
self.assertTrue(os.path.exists('.shelf/shelves/default/02'))
309
def test_shelf_upgrade(self):
310
tree = self.make_branch_and_tree('.')
312
self.__create_and_add_test_file(tree)
314
# Modify then shelve, so we're not upgrading to 00, just for kicks
315
file('test_file', 'w').write(self.MODIFIED)
316
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
318
open('.bzr-shelf', 'w').write('First old shelf')
319
open('.bzr-shelf-1', 'w').write('Second old shelf')
320
open('.bzr-shelf-3', 'w').write('Fourth old shelf')
322
# shelve and unshelve should bitch and do nothing
323
file('test_file', 'w').write('blah blah blah')
324
self.run_bzr('shelve', '--all', retcode=3)
325
self.assertFalse(os.path.exists('.shelf/shelves/default/01'))
326
self.assertEqual(file('test_file').read(), 'blah blah blah')
327
self.run_bzr('unshelve', '--all', retcode=3)
328
self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
330
# Upgrade, make sure it worked
331
self.run_bzr('shelf', 'upgrade', retcode=0)
332
self.assertEqual(open('.shelf/shelves/default/01').read(),
334
self.assertEqual(open('.shelf/shelves/default/02').read(),
336
self.assertEqual(open('.shelf/shelves/default/03').read(),
339
# Check the old shelves got backed up right
340
self.assertTrue(os.path.exists('.bzr-shelf~'))
341
self.assertTrue(os.path.exists('.bzr-shelf-1~'))
342
self.assertTrue(os.path.exists('.bzr-shelf-3~'))
343
self.assertFalse(os.path.exists('.bzr-shelf'))
344
self.assertFalse(os.path.exists('.bzr-shelf-1'))
345
self.assertFalse(os.path.exists('.bzr-shelf-3'))
347
# Shelve should work now
348
self.run_bzr('shelve', '--all', retcode=0)
350
def test_shelf_p0_patch(self):
351
tree = self.make_branch_and_tree('.')
353
self.__create_and_add_test_file(tree)
355
# Run a benign shelf command to setup .shelf for us
356
self.run_bzr('shelf', 'ls', retcode=0)
358
# Fake a -p0 shelved patch
360
diff = diff.replace('a/', '')
361
diff = diff.replace('b/', '')
362
open('.shelf/shelves/default/00', 'w').write(diff)
365
self.run_bzr('unshelve', '--all', retcode=0)
367
# Check the diff is right
368
self.assertEqual(self.capture('diff', retcode=1),
369
self.DIFF_HEADER + self.DIFF_1 + '\n')