1
# Copyright (C) 2008 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
from bzrlib import errors, pack, shelf, tests, transform
22
EMPTY_SHELF = ("Bazaar pack format 1 (introduced in 0.18)\n"
25
"d11:revision_id5:null:e"
28
"d10:_id_numberi0e18:_new_executabilityde7:_new_idde"
29
"9:_new_namede11:_new_parentde16:_non_present_idsde"
30
"17:_removed_contentsle11:_removed_idle14:_tree_path_idsdeeE")
33
class TestPrepareShelf(tests.TestCaseWithTransport):
35
def test_shelve_rename(self):
36
tree = self.make_branch_and_tree('.')
37
self.build_tree(['foo'])
38
tree.add(['foo'], ['foo-id'])
40
tree.rename_one('foo', 'bar')
41
creator = shelf.ShelfCreator(tree, tree.basis_tree())
42
self.addCleanup(creator.finalize)
43
self.assertEqual([('rename', 'foo-id', 'foo', 'bar')],
44
list(creator.iter_shelvable()))
45
creator.shelve_rename('foo-id')
46
work_trans_id = creator.work_transform.trans_id_file_id('foo-id')
47
self.assertEqual('foo', creator.work_transform.final_name(
49
shelf_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
50
self.assertEqual('bar', creator.shelf_transform.final_name(
53
def test_shelve_move(self):
54
tree = self.make_branch_and_tree('.')
55
self.build_tree(['foo/', 'bar/', 'foo/baz'])
56
tree.add(['foo', 'bar', 'foo/baz'], ['foo-id', 'bar-id', 'baz-id'])
58
tree.rename_one('foo/baz', 'bar/baz')
59
creator = shelf.ShelfCreator(tree, tree.basis_tree())
60
self.addCleanup(creator.finalize)
61
self.assertEqual([('rename', 'baz-id', 'foo/baz', 'bar/baz')],
62
list(creator.iter_shelvable()))
63
creator.shelve_rename('baz-id')
64
work_trans_id = creator.work_transform.trans_id_file_id('baz-id')
65
work_foo = creator.work_transform.trans_id_file_id('foo-id')
66
self.assertEqual(work_foo, creator.work_transform.final_parent(
68
shelf_trans_id = creator.shelf_transform.trans_id_file_id('baz-id')
69
shelf_bar = creator.shelf_transform.trans_id_file_id('bar-id')
70
self.assertEqual(shelf_bar, creator.shelf_transform.final_parent(
73
self.assertEqual('foo/baz', tree.id2path('baz-id'))
75
def assertShelvedFileEqual(self, expected_content, creator, file_id):
76
s_trans_id = creator.shelf_transform.trans_id_file_id(file_id)
77
shelf_file = creator.shelf_transform._limbo_name(s_trans_id)
78
self.assertFileEqual(expected_content, shelf_file)
80
def test_shelve_content_change(self):
81
tree = self.make_branch_and_tree('.')
83
self.addCleanup(tree.unlock)
84
self.build_tree_contents([('foo', 'a\n')])
85
tree.add('foo', 'foo-id')
86
tree.commit('Committed foo')
87
self.build_tree_contents([('foo', 'b\na\nc\n')])
88
creator = shelf.ShelfCreator(tree, tree.basis_tree())
89
self.addCleanup(creator.finalize)
90
self.assertEqual([('modify text', 'foo-id')],
91
list(creator.iter_shelvable()))
92
creator.shelve_lines('foo-id', ['a\n', 'c\n'])
94
self.assertFileEqual('a\nc\n', 'foo')
95
self.assertShelvedFileEqual('b\na\n', creator, 'foo-id')
97
def test_shelve_creation(self):
98
tree = self.make_branch_and_tree('.')
100
self.addCleanup(tree.unlock)
101
tree.commit('Empty tree')
102
self.build_tree_contents([('foo', 'a\n'), ('bar/',)])
103
tree.add(['foo', 'bar'], ['foo-id', 'bar-id'])
104
creator = shelf.ShelfCreator(tree, tree.basis_tree())
105
self.addCleanup(creator.finalize)
106
self.assertEqual([('add file', 'bar-id', 'directory', 'bar'),
107
('add file', 'foo-id', 'file', 'foo')],
108
sorted(list(creator.iter_shelvable())))
109
creator.shelve_creation('foo-id')
110
creator.shelve_creation('bar-id')
112
self.assertRaises(StopIteration,
113
tree.iter_entries_by_dir(['foo-id']).next)
114
s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
115
self.assertEqual('foo-id',
116
creator.shelf_transform.final_file_id(s_trans_id))
117
self.failIfExists('foo')
118
self.failIfExists('bar')
119
self.assertShelvedFileEqual('a\n', creator, 'foo-id')
120
s_bar_trans_id = creator.shelf_transform.trans_id_file_id('bar-id')
121
self.assertEqual('directory',
122
creator.shelf_transform.final_kind(s_bar_trans_id))
124
def test_shelve_symlink_creation(self):
125
self.requireFeature(tests.SymlinkFeature)
126
tree = self.make_branch_and_tree('.')
128
self.addCleanup(tree.unlock)
129
tree.commit('Empty tree')
130
os.symlink('bar', 'foo')
131
tree.add('foo', 'foo-id')
132
creator = shelf.ShelfCreator(tree, tree.basis_tree())
133
self.addCleanup(creator.finalize)
134
self.assertEqual([('add file', 'foo-id', 'symlink', 'foo')],
135
list(creator.iter_shelvable()))
136
creator.shelve_creation('foo-id')
138
s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
139
self.failIfExists('foo')
140
limbo_name = creator.shelf_transform._limbo_name(s_trans_id)
141
self.assertEqual('bar', os.readlink(limbo_name))
143
def test_shelve_creation_no_contents(self):
144
tree = self.make_branch_and_tree('.')
146
self.addCleanup(tree.unlock)
147
tree.commit('Empty tree')
148
self.build_tree(['foo'])
149
tree.add('foo', 'foo-id')
151
creator = shelf.ShelfCreator(tree, tree.basis_tree())
152
self.addCleanup(creator.finalize)
153
self.assertEqual([('add file', 'foo-id', None, 'foo')],
154
sorted(list(creator.iter_shelvable())))
155
creator.shelve_creation('foo-id')
157
self.assertRaises(StopIteration,
158
tree.iter_entries_by_dir(['foo-id']).next)
159
self.assertShelvedFileEqual('', creator, 'foo-id')
160
s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
161
self.assertEqual('foo-id',
162
creator.shelf_transform.final_file_id(s_trans_id))
163
self.failIfExists('foo')
165
def test_shelve_deletion(self):
166
tree = self.make_branch_and_tree('tree')
168
self.addCleanup(tree.unlock)
169
self.build_tree_contents([('tree/foo/',), ('tree/foo/bar', 'baz')])
170
tree.add(['foo', 'foo/bar'], ['foo-id', 'bar-id'])
171
tree.commit('Added file and directory')
172
tree.unversion(['foo-id', 'bar-id'])
173
os.unlink('tree/foo/bar')
175
creator = shelf.ShelfCreator(tree, tree.basis_tree())
176
self.addCleanup(creator.finalize)
177
self.assertEqual([('delete file', 'bar-id', 'file', 'foo/bar'),
178
('delete file', 'foo-id', 'directory', 'foo')],
179
sorted(list(creator.iter_shelvable())))
180
creator.shelve_deletion('foo-id')
181
creator.shelve_deletion('bar-id')
183
self.assertTrue('foo-id' in tree)
184
self.assertTrue('bar-id' in tree)
185
self.assertFileEqual('baz', 'tree/foo/bar')
187
def test_shelve_delete_contents(self):
188
tree = self.make_branch_and_tree('tree')
189
self.build_tree(['tree/foo',])
190
tree.add('foo', 'foo-id')
191
tree.commit('Added file and directory')
192
os.unlink('tree/foo')
193
creator = shelf.ShelfCreator(tree, tree.basis_tree())
194
self.addCleanup(creator.finalize)
195
self.assertEqual([('delete file', 'foo-id', 'file', 'foo')],
196
sorted(list(creator.iter_shelvable())))
197
creator.shelve_deletion('foo-id')
199
self.failUnlessExists('tree/foo')
201
def test_shelve_change_kind(self):
202
tree = self.make_branch_and_tree('tree')
203
self.build_tree_contents([('tree/foo', 'bar')])
204
tree.add('foo', 'foo-id')
205
tree.commit('Added file and directory')
206
os.unlink('tree/foo')
208
creator = shelf.ShelfCreator(tree, tree.basis_tree())
209
self.addCleanup(creator.finalize)
210
self.assertEqual([('change kind', 'foo-id', 'file', 'directory',
211
'foo')], sorted(list(creator.iter_shelvable())))
212
creator.shelve_content_change('foo-id')
214
self.assertFileEqual('bar', 'tree/foo')
215
s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
216
self.assertEqual('directory',
217
creator.shelf_transform._new_contents[s_trans_id])
219
def test_shelve_unversion(self):
220
tree = self.make_branch_and_tree('tree')
221
self.build_tree(['tree/foo',])
222
tree.add('foo', 'foo-id')
223
tree.commit('Added file and directory')
224
tree.unversion(['foo-id'])
225
creator = shelf.ShelfCreator(tree, tree.basis_tree())
226
self.addCleanup(creator.finalize)
227
self.assertEqual([('delete file', 'foo-id', 'file', 'foo')],
228
sorted(list(creator.iter_shelvable())))
229
creator.shelve_deletion('foo-id')
231
self.failUnlessExists('tree/foo')
233
def test_shelve_serialization(self):
234
tree = self.make_branch_and_tree('.')
235
creator = shelf.ShelfCreator(tree, tree.basis_tree())
236
self.addCleanup(creator.finalize)
237
shelf_file = open('shelf', 'wb')
238
self.addCleanup(shelf_file.close)
240
creator.write_shelf(shelf_file)
243
self.assertFileEqual(EMPTY_SHELF, 'shelf')
245
def test_write_shelf(self):
246
tree = self.make_branch_and_tree('tree')
247
self.build_tree(['tree/foo'])
248
tree.add('foo', 'foo-id')
249
creator = shelf.ShelfCreator(tree, tree.basis_tree())
250
self.addCleanup(creator.finalize)
251
list(creator.iter_shelvable())
252
creator.shelve_creation('foo-id')
253
shelf_file = open('shelf', 'wb')
255
creator.write_shelf(shelf_file)
258
parser = pack.ContainerPushParser()
259
shelf_file = open('shelf', 'rb')
261
parser.accept_bytes(shelf_file.read())
264
tt = transform.TransformPreview(tree)
265
self.addCleanup(tt.finalize)
266
records = iter(parser.read_pending_records())
269
tt.deserialize(records)
272
class TestUnshelver(tests.TestCaseWithTransport):
274
def test_make_merger(self):
275
tree = self.make_branch_and_tree('tree')
276
tree.commit('first commit')
277
self.build_tree_contents([('tree/foo', 'bar')])
279
self.addCleanup(tree.unlock)
280
tree.add('foo', 'foo-id')
281
creator = shelf.ShelfCreator(tree, tree.basis_tree())
282
self.addCleanup(creator.finalize)
283
list(creator.iter_shelvable())
284
creator.shelve_creation('foo-id')
285
shelf_file = open('shelf-file', 'w+b')
287
creator.write_shelf(shelf_file)
290
unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
291
unshelver.make_merger().do_merge()
292
self.assertFileEqual('bar', 'tree/foo')
296
def test_unshelve_changed(self):
297
tree = self.make_branch_and_tree('tree')
299
self.addCleanup(tree.unlock)
300
self.build_tree_contents([('tree/foo', 'a\nb\nc\n')])
301
tree.add('foo', 'foo-id')
302
tree.commit('first commit')
303
self.build_tree_contents([('tree/foo', 'a\nb\nd\n')])
304
creator = shelf.ShelfCreator(tree, tree.basis_tree())
305
self.addCleanup(creator.finalize)
306
list(creator.iter_shelvable())
307
creator.shelve_lines('foo-id', ['a\n', 'b\n', 'c\n'])
308
shelf_file = open('shelf', 'w+b')
309
self.addCleanup(shelf_file.close)
310
creator.write_shelf(shelf_file)
312
self.build_tree_contents([('tree/foo', 'z\na\nb\nc\n')])
314
unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
315
unshelver.make_merger().do_merge()
316
self.assertFileEqual('z\na\nb\nd\n', 'tree/foo')
318
def test_unshelve_deleted(self):
319
tree = self.make_branch_and_tree('tree')
321
self.addCleanup(tree.unlock)
322
self.build_tree_contents([('tree/foo/',), ('tree/foo/bar', 'baz')])
323
tree.add(['foo', 'foo/bar'], ['foo-id', 'bar-id'])
324
tree.commit('Added file and directory')
325
tree.unversion(['foo-id', 'bar-id'])
326
os.unlink('tree/foo/bar')
328
creator = shelf.ShelfCreator(tree, tree.basis_tree())
329
list(creator.iter_shelvable())
330
creator.shelve_deletion('foo-id')
331
creator.shelve_deletion('bar-id')
332
shelf_file = open('shelf', 'w+b')
333
self.addCleanup(shelf_file.close)
334
creator.write_shelf(shelf_file)
337
# validate the test setup
338
self.assertTrue('foo-id' in tree)
339
self.assertTrue('bar-id' in tree)
340
self.assertFileEqual('baz', 'tree/foo/bar')
342
unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
343
unshelver.make_merger().do_merge()
344
self.assertFalse('foo-id' in tree)
345
self.assertFalse('bar-id' in tree)
347
def test_unshelve_base(self):
348
tree = self.make_branch_and_tree('tree')
350
self.addCleanup(tree.unlock)
351
tree.commit('rev1', rev_id='rev1')
352
creator = shelf.ShelfCreator(tree, tree.basis_tree())
353
self.addCleanup(creator.finalize)
354
manager = tree.get_shelf_manager()
355
shelf_id, shelf_file = manager.new_shelf()
357
creator.write_shelf(shelf_file)
360
tree.commit('rev2', rev_id='rev2')
361
shelf_file = manager.read_shelf(1)
362
self.addCleanup(shelf_file.close)
363
unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
364
self.addCleanup(unshelver.finalize)
365
self.assertEqual('rev1', unshelver.base_tree.get_revision_id())
367
def test_unshelve_serialization(self):
368
tree = self.make_branch_and_tree('.')
369
self.build_tree_contents([('shelf', EMPTY_SHELF)])
370
shelf_file = open('shelf', 'rb')
371
self.addCleanup(shelf_file.close)
372
unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
374
def test_corrupt_shelf(self):
375
tree = self.make_branch_and_tree('.')
376
self.build_tree_contents([('shelf', EMPTY_SHELF.replace('metadata',
378
shelf_file = open('shelf', 'rb')
379
self.addCleanup(shelf_file.close)
380
e = self.assertRaises(errors.ShelfCorrupt,
381
shelf.Unshelver.from_tree_and_shelf, tree,
383
self.assertEqual('Shelf corrupt.', str(e))
386
class TestShelfManager(tests.TestCaseWithTransport):
388
def test_get_shelf_manager(self):
389
tree = self.make_branch_and_tree('.')
390
manager = tree.get_shelf_manager()
391
self.assertEqual(tree._transport.base + 'shelf/',
392
manager.transport.base)
394
def get_manager(self):
395
return self.make_branch_and_tree('.').get_shelf_manager()
397
def test_get_shelf_filename(self):
398
tree = self.make_branch_and_tree('.')
399
manager = tree.get_shelf_manager()
400
self.assertEqual('shelf-1', manager.get_shelf_filename(1))
402
def test_get_shelf_ids(self):
403
tree = self.make_branch_and_tree('.')
404
manager = tree.get_shelf_manager()
405
self.assertEqual([1, 3], manager.get_shelf_ids(
406
['shelf-1', 'shelf-02', 'shelf-3']))
408
def test_new_shelf(self):
409
manager = self.get_manager()
410
shelf_id, shelf_file = manager.new_shelf()
412
self.assertEqual(1, shelf_id)
413
shelf_id, shelf_file = manager.new_shelf()
415
self.assertEqual(2, shelf_id)
416
manager.delete_shelf(1)
417
shelf_id, shelf_file = manager.new_shelf()
419
self.assertEqual(3, shelf_id)
421
def test_active_shelves(self):
422
manager = self.get_manager()
423
self.assertEqual([], manager.active_shelves())
424
shelf_id, shelf_file = manager.new_shelf()
426
self.assertEqual([1], manager.active_shelves())
428
def test_delete_shelf(self):
429
manager = self.get_manager()
430
shelf_id, shelf_file = manager.new_shelf()
432
self.assertEqual([1], manager.active_shelves())
433
manager.delete_shelf(1)
434
self.assertEqual([], manager.active_shelves())
436
def test_last_shelf(self):
437
manager = self.get_manager()
438
self.assertIs(None, manager.last_shelf())
439
shelf_id, shelf_file = manager.new_shelf()
441
self.assertEqual(1, manager.last_shelf())
443
def test_read_shelf(self):
444
manager = self.get_manager()
445
shelf_id, shelf_file = manager.new_shelf()
447
shelf_file.write('foo')
450
shelf_id, shelf_file = manager.new_shelf()
452
shelf_file.write('bar')
455
shelf_file = manager.read_shelf(1)
457
self.assertEqual('foo', shelf_file.read())
460
shelf_file = manager.read_shelf(2)
462
self.assertEqual('bar', shelf_file.read())
466
def test_read_non_existant(self):
467
manager = self.get_manager()
468
e = self.assertRaises(errors.NoSuchShelfId, manager.read_shelf, 1)
469
self.assertEqual('No changes are shelved with id "1".', str(e))
471
def test_shelve_changes(self):
472
tree = self.make_branch_and_tree('tree')
473
tree.commit('no-change commit')
475
self.addCleanup(tree.unlock)
476
self.build_tree_contents([('tree/foo', 'bar')])
477
self.assertFileEqual('bar', 'tree/foo')
478
tree.add('foo', 'foo-id')
479
creator = shelf.ShelfCreator(tree, tree.basis_tree())
480
self.addCleanup(creator.finalize)
481
list(creator.iter_shelvable())
482
creator.shelve_creation('foo-id')
483
shelf_manager = tree.get_shelf_manager()
484
shelf_id = shelf_manager.shelve_changes(creator)
485
self.failIfExists('tree/foo')
486
unshelver = shelf_manager.get_unshelver(shelf_id)
487
unshelver.make_merger().do_merge()
488
self.assertFileEqual('bar', 'tree/foo')
490
def test_get_metadata(self):
491
tree = self.make_branch_and_tree('.')
492
creator = shelf.ShelfCreator(tree, tree.basis_tree())
493
shelf_manager = tree.get_shelf_manager()
494
shelf_id = shelf_manager.shelve_changes(creator, 'foo')
495
metadata = shelf_manager.get_metadata(shelf_id)
496
self.assertEqual('foo', metadata['message'])
497
self.assertEqual('null:', metadata['revision_id'])