6
class ShelfTests(bzrlib.tests.TestCaseWithTransport):
7
ORIGINAL = '\n\nhello test world\n\n'
8
MODIFIED = '\n\ngoodbye test world\n\n'
9
DIFF_HEADER = "=== modified file 'a/test_file'\n"
10
DIFF_1 = """--- a/test_file\t
19
DIFF_2 = """--- a/test_file\t
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)
79
def test_shelf_nothing_to_shelve(self):
81
tree = self.make_branch_and_tree('.')
82
self.__create_and_add_test_file(tree)
85
self.run_bzr('shelve', '--all', retcode=3)
87
if os.path.exists(os.path.join(tree.branch.base,
88
'.shelf/shelves/default/00')):
89
self.fail("Shelf exists, but it shouldn't")
91
def __create_and_add_test_file(self, tree, filename='test_file'):
92
f = open(filename, 'w')
93
f.write(self.ORIGINAL)
95
tree.add(tree.relpath(os.path.join(os.getcwd(), filename)))
96
tree.commit(message='add %s' % filename)
98
def test_shelf_with_revision(self):
99
tree = self.make_branch_and_tree('.')
101
self.__create_and_add_test_file(tree)
103
# Modify the test file and commit it
104
self.build_tree_contents([('test_file', self.MODIFIED)])
105
tree.commit(message='update test_file')
108
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')
114
# Make sure the file is the way it should be
115
self.assertEqual(file('test_file').read(), self.ORIGINAL)
118
self.run_bzr('unshelve', '--all', retcode=0)
120
# Make sure the file is back the way it should be
121
self.assertEqual(file('test_file').read(), self.MODIFIED)
123
def test_shelf_with_two_revisions(self):
124
tree = self.make_branch_and_tree('.')
126
cmd = 'shelve --all -r 1..2'
127
(stdout, stderr) = self.run_bzr_captured(cmd.split(), retcode=None)
129
self.assertEqual(stderr.split('\n')[0],
130
'bzr: ERROR: shelve only accepts a single revision parameter.')
132
def test_shelf_show_basic(self):
133
tree = self.make_branch_and_tree('.')
134
self.__create_and_add_test_file(tree)
135
self.__test_show(tree, '00')
137
def __test_show(self, tree, patch):
138
# Modify the test file
139
self.build_tree_contents([('test_file', 'patch %s\n' % patch)])
142
self.run_bzr('shelve', '--all', retcode=0)
144
# Make sure there is no diff anymore
145
self.assertEqual(self.capture('diff', retcode=0), '')
147
# Check the shelf is right
148
shelf = open(os.path.join(tree.branch.base,
149
'.shelf/shelves/default', patch)).read()
150
self.assertTrue('patch %s' % patch in shelf)
152
# Check the shown output is right
153
shown = self.capture('shelf show %s' % patch, retcode=0)
154
self.assertEqual(shown, shelf)
156
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')
163
# Now check we can show a previously shelved patch
164
shelf = open(os.path.join(tree.branch.base,
165
'.shelf/shelves/default/00')).read()
166
self.assertTrue('patch 00' in shelf)
168
# Check the shown output is right
169
shown = self.capture('shelf show 00', retcode=0)
170
self.assertEqual(shown, shelf)
172
def test_shelf_show_with_no_patch(self):
173
tree = self.make_branch_and_tree('.')
174
stderr = self.run_bzr_captured(['shelf', 'show', '00'], retcode=None)[1]
175
self.assertTrue("Patch '00' doesn't exist on shelf default!" in stderr)
177
def test_shelf_unshelve_failure(self):
178
tree = self.make_branch_and_tree('.')
180
self.__create_and_add_test_file(tree)
182
# Modify the test file
183
file('test_file', 'w').write(self.MODIFIED)
186
self.run_bzr('shelve', '--all', retcode=0)
188
# Write an unapplyable patch into the shelf
189
shelf = open(os.path.join(tree.branch.base,
190
'.shelf/shelves/default/00'), 'w')
191
shelf.write(self.DIFF_2)
194
# Unshelve, should fail
195
self.run_bzr('unshelve', '--all', retcode=3)
197
# Make sure the patch is still there, eventhough it's broken
198
shelf = open(os.path.join(tree.branch.base,
199
'.shelf/shelves/default/00')).read()
200
self.assertEqual(shelf, self.DIFF_2)
202
# Working tree should be unchanged
203
diff = self.capture('diff', retcode=0)
204
self.assertEqual(diff, '')
206
def test_shelf_unshelve_failure_two_hunks(self):
207
tree = self.make_branch_and_tree('.')
209
self.__create_and_add_test_file(tree)
210
self.__create_and_add_test_file(tree, filename='test_file2')
212
# Modify the test files
213
file('test_file', 'w').write(self.MODIFIED)
214
file('test_file2', 'w').write(self.MODIFIED)
217
self.run_bzr('shelve', '--all', retcode=0)
219
# Put the changes to test_file back, the shelved patch won't apply now
220
file('test_file', 'w').write(self.MODIFIED)
221
tree.commit(message='screw up test_file')
223
# Unshelve, should fail
224
self.run_bzr('unshelve', '--all', retcode=3)
226
# Working tree should be unchanged
227
diff = self.capture('diff', retcode=0)
228
self.assertEqual(diff, '')
230
# Force should succeed and modify test_file2, but leave shelf
231
self.run_bzr('unshelve', '--force', '--all', retcode=0)
232
self.assertEqual(open('test_file2').read(), self.MODIFIED)
233
self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
235
def test_shelf_after_unshelve(self):
236
tree = self.make_branch_and_tree('.')
238
self.__create_and_add_test_file(tree)
239
self.__create_and_add_test_file(tree, filename='test_file2')
241
# Modify the test files
242
file('test_file', 'w').write(self.MODIFIED)
243
file('test_file2', 'w').write(self.MODIFIED)
246
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
247
self.run_bzr('shelve', '--all', 'test_file2', retcode=0)
250
self.run_bzr('unshelve', '--all', retcode=0)
252
# We should now have 00 and 01~
253
self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
254
self.assertTrue(os.path.exists('.shelf/shelves/default/01~'))
257
list = self.capture('shelf ls', retcode=0).split('\n')
259
self.assertFalse(line.startswith(' 01'))
261
# Unshelve, if unshelve is confused by the backup it will fail
262
self.run_bzr('unshelve', '--all', retcode=0)
264
def test_shelf_delete(self):
265
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')
270
# Modify the test files
271
file('test_file', 'w').write(self.MODIFIED)
272
file('test_file2', 'w').write(self.MODIFIED)
275
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
276
self.run_bzr('shelve', '--all', 'test_file2', retcode=0)
279
self.run_bzr('shelf', 'delete', '00', retcode=0)
281
# We should now have 01 but not 00
282
self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
283
self.assertTrue(os.path.exists('.shelf/shelves/default/01'))
286
list = self.capture('shelf ls', retcode=0).split('\n')
288
self.assertFalse(line.startswith(' 00'))
290
# Unshelve should unshelve 01
291
self.run_bzr('unshelve', '--all', retcode=0)
292
self.assertEqual(file('test_file2').read(), self.MODIFIED)
294
def test_shelf_gaps(self):
295
tree = self.make_branch_and_tree('.')
296
self.__create_and_add_test_file(tree)
297
file('test_file', 'w').write(self.MODIFIED)
298
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
299
file('test_file', 'w').write(self.MODIFIED)
300
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
302
# Now delete 00, leaving 01, next shelve should go into 02
303
self.run_bzr('shelf', 'delete', '0', retcode=0)
304
self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
305
self.assertFalse(os.path.exists('.shelf/shelves/default/02'))
306
file('test_file', 'w').write(self.MODIFIED)
307
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
308
self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
309
self.assertTrue(os.path.exists('.shelf/shelves/default/02'))
311
def test_shelf_upgrade(self):
312
tree = self.make_branch_and_tree('.')
314
self.__create_and_add_test_file(tree)
316
# Modify then shelve, so we're not upgrading to 00, just for kicks
317
file('test_file', 'w').write(self.MODIFIED)
318
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
320
open('.bzr-shelf', 'w').write('First old shelf')
321
open('.bzr-shelf-1', 'w').write('Second old shelf')
322
open('.bzr-shelf-3', 'w').write('Fourth old shelf')
324
# shelve and unshelve should bitch and do nothing
325
file('test_file', 'w').write('blah blah blah')
326
self.run_bzr('shelve', '--all', retcode=3)
327
self.assertFalse(os.path.exists('.shelf/shelves/default/01'))
328
self.assertEqual(file('test_file').read(), 'blah blah blah')
329
self.run_bzr('unshelve', '--all', retcode=3)
330
self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
332
# Upgrade, make sure it worked
333
self.run_bzr('shelf', 'upgrade', retcode=0)
334
self.assertEqual(open('.shelf/shelves/default/01').read(),
336
self.assertEqual(open('.shelf/shelves/default/02').read(),
338
self.assertEqual(open('.shelf/shelves/default/03').read(),
341
# Check the old shelves got backed up right
342
self.assertTrue(os.path.exists('.bzr-shelf~'))
343
self.assertTrue(os.path.exists('.bzr-shelf-1~'))
344
self.assertTrue(os.path.exists('.bzr-shelf-3~'))
345
self.assertFalse(os.path.exists('.bzr-shelf'))
346
self.assertFalse(os.path.exists('.bzr-shelf-1'))
347
self.assertFalse(os.path.exists('.bzr-shelf-3'))
349
# Shelve should work now
350
self.run_bzr('shelve', '--all', retcode=0)
352
def test_shelf_p0_patch(self):
353
tree = self.make_branch_and_tree('.')
355
self.__create_and_add_test_file(tree)
357
# Run a benign shelf command to setup .shelf for us
358
self.run_bzr('shelf', 'ls', retcode=0)
360
# Fake a -p0 shelved patch
362
diff = diff.replace('a/', '')
363
diff = diff.replace('b/', '')
364
open('.shelf/shelves/default/00', 'w').write(diff)
367
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
def test_shelf_shelve_in_subdir(self):
374
tree = self.make_branch_and_tree('.')
376
self.__create_and_add_test_file(tree)
378
# Modify the test file
379
file('test_file', 'w').write(self.MODIFIED)
382
self.run_bzr('shelve', '--all', retcode=0)
384
# Working tree should be unchanged
385
diff = self.capture('diff', retcode=0)
386
self.assertEqual(diff, '')
388
# Unshelve, should succeed
389
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')