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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30
EMPTY_SHELF = ("Bazaar pack format 1 (introduced in 0.18)\n"
33
"d11:revision_id5:null:e"
36
"d10:_id_numberi0e18:_new_executabilityde7:_new_idde"
37
"9:_new_namede11:_new_parentde16:_non_present_idsde"
38
"17:_removed_contentsle11:_removed_idle14:_tree_path_idsdeeE")
41
class TestPrepareShelf(tests.TestCaseWithTransport):
43
def test_shelve_rename(self):
44
tree = self.make_branch_and_tree('.')
45
self.build_tree(['foo'])
46
tree.add(['foo'], ['foo-id'])
48
tree.rename_one('foo', 'bar')
49
creator = shelf.ShelfCreator(tree, tree.basis_tree())
50
self.addCleanup(creator.finalize)
51
self.assertEqual([('rename', 'foo-id', 'foo', 'bar')],
52
list(creator.iter_shelvable()))
53
creator.shelve_rename('foo-id')
54
work_trans_id = creator.work_transform.trans_id_file_id('foo-id')
55
self.assertEqual('foo', creator.work_transform.final_name(
57
shelf_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
58
self.assertEqual('bar', creator.shelf_transform.final_name(
61
def test_shelve_move(self):
62
tree = self.make_branch_and_tree('.')
63
self.build_tree(['foo/', 'bar/', 'foo/baz'])
64
tree.add(['foo', 'bar', 'foo/baz'], ['foo-id', 'bar-id', 'baz-id'])
66
tree.rename_one('foo/baz', 'bar/baz')
67
creator = shelf.ShelfCreator(tree, tree.basis_tree())
68
self.addCleanup(creator.finalize)
69
self.assertEqual([('rename', 'baz-id', 'foo/baz', 'bar/baz')],
70
list(creator.iter_shelvable()))
71
creator.shelve_rename('baz-id')
72
work_trans_id = creator.work_transform.trans_id_file_id('baz-id')
73
work_foo = creator.work_transform.trans_id_file_id('foo-id')
74
self.assertEqual(work_foo, creator.work_transform.final_parent(
76
shelf_trans_id = creator.shelf_transform.trans_id_file_id('baz-id')
77
shelf_bar = creator.shelf_transform.trans_id_file_id('bar-id')
78
self.assertEqual(shelf_bar, creator.shelf_transform.final_parent(
81
self.assertEqual('foo/baz', tree.id2path('baz-id'))
83
def assertShelvedFileEqual(self, expected_content, creator, file_id):
84
s_trans_id = creator.shelf_transform.trans_id_file_id(file_id)
85
shelf_file = creator.shelf_transform._limbo_name(s_trans_id)
86
self.assertFileEqual(expected_content, shelf_file)
88
def test_shelve_content_change(self):
89
tree = self.make_branch_and_tree('.')
91
self.addCleanup(tree.unlock)
92
self.build_tree_contents([('foo', 'a\n')])
93
tree.add('foo', 'foo-id')
94
tree.commit('Committed foo')
95
self.build_tree_contents([('foo', 'b\na\nc\n')])
96
creator = shelf.ShelfCreator(tree, tree.basis_tree())
97
self.addCleanup(creator.finalize)
98
self.assertEqual([('modify text', 'foo-id')],
99
list(creator.iter_shelvable()))
100
creator.shelve_lines('foo-id', ['a\n', 'c\n'])
102
self.assertFileEqual('a\nc\n', 'foo')
103
self.assertShelvedFileEqual('b\na\n', creator, 'foo-id')
105
def test_shelve_creation(self):
106
tree = self.make_branch_and_tree('.')
108
self.addCleanup(tree.unlock)
109
tree.commit('Empty tree')
110
self.build_tree_contents([('foo', 'a\n'), ('bar/',)])
111
tree.add(['foo', 'bar'], ['foo-id', 'bar-id'])
112
creator = shelf.ShelfCreator(tree, tree.basis_tree())
113
self.addCleanup(creator.finalize)
114
self.assertEqual([('add file', 'bar-id', 'directory', 'bar'),
115
('add file', 'foo-id', 'file', 'foo')],
116
sorted(list(creator.iter_shelvable())))
117
creator.shelve_creation('foo-id')
118
creator.shelve_creation('bar-id')
120
self.assertRaises(StopIteration,
121
tree.iter_entries_by_dir(['foo-id']).next)
122
s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
123
self.assertEqual('foo-id',
124
creator.shelf_transform.final_file_id(s_trans_id))
125
self.failIfExists('foo')
126
self.failIfExists('bar')
127
self.assertShelvedFileEqual('a\n', creator, 'foo-id')
128
s_bar_trans_id = creator.shelf_transform.trans_id_file_id('bar-id')
129
self.assertEqual('directory',
130
creator.shelf_transform.final_kind(s_bar_trans_id))
132
def _test_shelve_symlink_creation(self, link_name, link_target):
133
self.requireFeature(tests.SymlinkFeature)
134
tree = self.make_branch_and_tree('.')
136
self.addCleanup(tree.unlock)
137
tree.commit('Empty tree')
138
os.symlink(link_target, link_name)
139
tree.add(link_name, 'foo-id')
140
creator = shelf.ShelfCreator(tree, tree.basis_tree())
141
self.addCleanup(creator.finalize)
142
self.assertEqual([('add file', 'foo-id', 'symlink', link_name)],
143
list(creator.iter_shelvable()))
144
creator.shelve_creation('foo-id')
146
s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
147
self.failIfExists(link_name)
148
limbo_name = creator.shelf_transform._limbo_name(s_trans_id)
149
self.assertEqual(link_target, osutils.readlink(limbo_name))
150
ptree = creator.shelf_transform.get_preview_tree()
151
self.assertEqual(link_target, ptree.get_symlink_target('foo-id'))
153
def test_shelve_symlink_creation(self):
154
self._test_shelve_symlink_creation('foo', 'bar')
156
def test_shelve_unicode_symlink_creation(self):
157
self.requireFeature(tests.UnicodeFilenameFeature)
158
self._test_shelve_symlink_creation(u'fo\N{Euro Sign}o',
161
def _test_shelve_symlink_target_change(self, link_name,
162
old_target, new_target):
163
self.requireFeature(tests.SymlinkFeature)
164
tree = self.make_branch_and_tree('.')
166
self.addCleanup(tree.unlock)
167
os.symlink(old_target, link_name)
168
tree.add(link_name, 'foo-id')
169
tree.commit("commit symlink")
171
os.symlink(new_target, link_name)
172
creator = shelf.ShelfCreator(tree, tree.basis_tree())
173
self.addCleanup(creator.finalize)
174
self.assertEqual([('modify target', 'foo-id', link_name,
175
old_target, new_target)],
176
list(creator.iter_shelvable()))
177
creator.shelve_modify_target('foo-id')
179
self.assertEqual(old_target, osutils.readlink(link_name))
180
s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
181
limbo_name = creator.shelf_transform._limbo_name(s_trans_id)
182
self.assertEqual(new_target, osutils.readlink(limbo_name))
183
ptree = creator.shelf_transform.get_preview_tree()
184
self.assertEqual(new_target, ptree.get_symlink_target('foo-id'))
186
def test_shelve_symlink_target_change(self):
187
self._test_shelve_symlink_target_change('foo', 'bar', 'baz')
189
def test_shelve_unicode_symlink_target_change(self):
190
self.requireFeature(tests.UnicodeFilenameFeature)
191
self._test_shelve_symlink_target_change(
192
u'fo\N{Euro Sign}o', u'b\N{Euro Sign}ar', u'b\N{Euro Sign}az')
194
def test_shelve_creation_no_contents(self):
195
tree = self.make_branch_and_tree('.')
197
self.addCleanup(tree.unlock)
198
tree.commit('Empty tree')
199
self.build_tree(['foo'])
200
tree.add('foo', 'foo-id')
202
creator = shelf.ShelfCreator(tree, tree.basis_tree())
203
self.addCleanup(creator.finalize)
204
self.assertEqual([('add file', 'foo-id', None, 'foo')],
205
sorted(list(creator.iter_shelvable())))
206
creator.shelve_creation('foo-id')
208
self.assertRaises(StopIteration,
209
tree.iter_entries_by_dir(['foo-id']).next)
210
self.assertShelvedFileEqual('', creator, 'foo-id')
211
s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
212
self.assertEqual('foo-id',
213
creator.shelf_transform.final_file_id(s_trans_id))
214
self.failIfExists('foo')
216
def test_shelve_deletion(self):
217
tree = self.make_branch_and_tree('tree')
219
self.addCleanup(tree.unlock)
220
self.build_tree_contents([('tree/foo/',), ('tree/foo/bar', 'baz')])
221
tree.add(['foo', 'foo/bar'], ['foo-id', 'bar-id'])
222
tree.commit('Added file and directory')
223
tree.unversion(['foo-id', 'bar-id'])
224
os.unlink('tree/foo/bar')
226
creator = shelf.ShelfCreator(tree, tree.basis_tree())
227
self.addCleanup(creator.finalize)
228
self.assertEqual([('delete file', 'bar-id', 'file', 'foo/bar'),
229
('delete file', 'foo-id', 'directory', 'foo')],
230
sorted(list(creator.iter_shelvable())))
231
creator.shelve_deletion('foo-id')
232
creator.shelve_deletion('bar-id')
234
self.assertTrue('foo-id' in tree)
235
self.assertTrue('bar-id' in tree)
236
self.assertFileEqual('baz', 'tree/foo/bar')
238
def test_shelve_delete_contents(self):
239
tree = self.make_branch_and_tree('tree')
240
self.build_tree(['tree/foo',])
241
tree.add('foo', 'foo-id')
242
tree.commit('Added file and directory')
243
os.unlink('tree/foo')
244
creator = shelf.ShelfCreator(tree, tree.basis_tree())
245
self.addCleanup(creator.finalize)
246
self.assertEqual([('delete file', 'foo-id', 'file', 'foo')],
247
sorted(list(creator.iter_shelvable())))
248
creator.shelve_deletion('foo-id')
250
self.failUnlessExists('tree/foo')
252
def test_shelve_change_kind(self):
253
tree = self.make_branch_and_tree('tree')
254
self.build_tree_contents([('tree/foo', 'bar')])
255
tree.add('foo', 'foo-id')
256
tree.commit('Added file and directory')
257
os.unlink('tree/foo')
259
creator = shelf.ShelfCreator(tree, tree.basis_tree())
260
self.addCleanup(creator.finalize)
261
self.assertEqual([('change kind', 'foo-id', 'file', 'directory',
262
'foo')], sorted(list(creator.iter_shelvable())))
263
creator.shelve_content_change('foo-id')
265
self.assertFileEqual('bar', 'tree/foo')
266
s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
267
self.assertEqual('directory',
268
creator.shelf_transform._new_contents[s_trans_id])
270
def test_shelve_unversion(self):
271
tree = self.make_branch_and_tree('tree')
272
self.build_tree(['tree/foo',])
273
tree.add('foo', 'foo-id')
274
tree.commit('Added file and directory')
275
tree.unversion(['foo-id'])
276
creator = shelf.ShelfCreator(tree, tree.basis_tree())
277
self.addCleanup(creator.finalize)
278
self.assertEqual([('delete file', 'foo-id', 'file', 'foo')],
279
sorted(list(creator.iter_shelvable())))
280
creator.shelve_deletion('foo-id')
282
self.failUnlessExists('tree/foo')
284
def test_shelve_serialization(self):
285
tree = self.make_branch_and_tree('.')
286
creator = shelf.ShelfCreator(tree, tree.basis_tree())
287
self.addCleanup(creator.finalize)
288
shelf_file = open('shelf', 'wb')
289
self.addCleanup(shelf_file.close)
291
creator.write_shelf(shelf_file)
294
self.assertFileEqual(EMPTY_SHELF, 'shelf')
296
def test_write_shelf(self):
297
tree = self.make_branch_and_tree('tree')
298
self.build_tree(['tree/foo'])
299
tree.add('foo', 'foo-id')
300
creator = shelf.ShelfCreator(tree, tree.basis_tree())
301
self.addCleanup(creator.finalize)
302
list(creator.iter_shelvable())
303
creator.shelve_creation('foo-id')
304
shelf_file = open('shelf', 'wb')
306
creator.write_shelf(shelf_file)
309
parser = pack.ContainerPushParser()
310
shelf_file = open('shelf', 'rb')
312
parser.accept_bytes(shelf_file.read())
315
tt = transform.TransformPreview(tree)
316
self.addCleanup(tt.finalize)
317
records = iter(parser.read_pending_records())
320
tt.deserialize(records)
322
def test_shelve_unversioned(self):
323
tree = self.make_branch_and_tree('tree')
324
self.assertRaises(errors.PathsNotVersionedError,
325
shelf.ShelfCreator, tree, tree.basis_tree(), ['foo'])
326
# We should be able to lock/unlock the tree if ShelfCreator cleaned
328
wt = workingtree.WorkingTree.open('tree')
331
# And a second tentative should raise the same error (no
332
# limbo/pending_deletion leftovers).
333
self.assertRaises(errors.PathsNotVersionedError,
334
shelf.ShelfCreator, tree, tree.basis_tree(), ['foo'])
337
class TestUnshelver(tests.TestCaseWithTransport):
339
def test_make_merger(self):
340
tree = self.make_branch_and_tree('tree')
341
tree.commit('first commit')
342
self.build_tree_contents([('tree/foo', 'bar')])
344
self.addCleanup(tree.unlock)
345
tree.add('foo', 'foo-id')
346
creator = shelf.ShelfCreator(tree, tree.basis_tree())
347
self.addCleanup(creator.finalize)
348
list(creator.iter_shelvable())
349
creator.shelve_creation('foo-id')
350
shelf_file = open('shelf-file', 'w+b')
352
creator.write_shelf(shelf_file)
355
unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
356
unshelver.make_merger().do_merge()
357
self.assertFileEqual('bar', 'tree/foo')
361
def test_unshelve_changed(self):
362
tree = self.make_branch_and_tree('tree')
364
self.addCleanup(tree.unlock)
365
self.build_tree_contents([('tree/foo', 'a\nb\nc\n')])
366
tree.add('foo', 'foo-id')
367
tree.commit('first commit')
368
self.build_tree_contents([('tree/foo', 'a\nb\nd\n')])
369
creator = shelf.ShelfCreator(tree, tree.basis_tree())
370
self.addCleanup(creator.finalize)
371
list(creator.iter_shelvable())
372
creator.shelve_lines('foo-id', ['a\n', 'b\n', 'c\n'])
373
shelf_file = open('shelf', 'w+b')
374
self.addCleanup(shelf_file.close)
375
creator.write_shelf(shelf_file)
377
self.build_tree_contents([('tree/foo', 'z\na\nb\nc\n')])
379
unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
380
unshelver.make_merger().do_merge()
381
self.assertFileEqual('z\na\nb\nd\n', 'tree/foo')
383
def test_unshelve_deleted(self):
384
tree = self.make_branch_and_tree('tree')
386
self.addCleanup(tree.unlock)
387
self.build_tree_contents([('tree/foo/',), ('tree/foo/bar', 'baz')])
388
tree.add(['foo', 'foo/bar'], ['foo-id', 'bar-id'])
389
tree.commit('Added file and directory')
390
tree.unversion(['foo-id', 'bar-id'])
391
os.unlink('tree/foo/bar')
393
creator = shelf.ShelfCreator(tree, tree.basis_tree())
394
list(creator.iter_shelvable())
395
creator.shelve_deletion('foo-id')
396
creator.shelve_deletion('bar-id')
397
shelf_file = open('shelf', 'w+b')
398
self.addCleanup(shelf_file.close)
399
creator.write_shelf(shelf_file)
402
# validate the test setup
403
self.assertTrue('foo-id' in tree)
404
self.assertTrue('bar-id' in tree)
405
self.assertFileEqual('baz', 'tree/foo/bar')
407
unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
408
unshelver.make_merger().do_merge()
409
self.assertFalse('foo-id' in tree)
410
self.assertFalse('bar-id' in tree)
412
def test_unshelve_base(self):
413
tree = self.make_branch_and_tree('tree')
415
self.addCleanup(tree.unlock)
416
tree.commit('rev1', rev_id='rev1')
417
creator = shelf.ShelfCreator(tree, tree.basis_tree())
418
self.addCleanup(creator.finalize)
419
manager = tree.get_shelf_manager()
420
shelf_id, shelf_file = manager.new_shelf()
422
creator.write_shelf(shelf_file)
425
tree.commit('rev2', rev_id='rev2')
426
shelf_file = manager.read_shelf(1)
427
self.addCleanup(shelf_file.close)
428
unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
429
self.addCleanup(unshelver.finalize)
430
self.assertEqual('rev1', unshelver.base_tree.get_revision_id())
432
def test_unshelve_serialization(self):
433
tree = self.make_branch_and_tree('.')
434
self.build_tree_contents([('shelf', EMPTY_SHELF)])
435
shelf_file = open('shelf', 'rb')
436
self.addCleanup(shelf_file.close)
437
unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
439
def test_corrupt_shelf(self):
440
tree = self.make_branch_and_tree('.')
441
self.build_tree_contents([('shelf', EMPTY_SHELF.replace('metadata',
443
shelf_file = open('shelf', 'rb')
444
self.addCleanup(shelf_file.close)
445
e = self.assertRaises(errors.ShelfCorrupt,
446
shelf.Unshelver.from_tree_and_shelf, tree,
448
self.assertEqual('Shelf corrupt.', str(e))
451
class TestShelfManager(tests.TestCaseWithTransport):
453
def test_get_shelf_manager(self):
454
tree = self.make_branch_and_tree('.')
455
manager = tree.get_shelf_manager()
456
self.assertEqual(tree._transport.base + 'shelf/',
457
manager.transport.base)
459
def get_manager(self):
460
return self.make_branch_and_tree('.').get_shelf_manager()
462
def test_get_shelf_filename(self):
463
tree = self.make_branch_and_tree('.')
464
manager = tree.get_shelf_manager()
465
self.assertEqual('shelf-1', manager.get_shelf_filename(1))
467
def test_get_shelf_ids(self):
468
tree = self.make_branch_and_tree('.')
469
manager = tree.get_shelf_manager()
470
self.assertEqual([1, 3], manager.get_shelf_ids(
471
['shelf-1', 'shelf-02', 'shelf-3']))
473
def test_new_shelf(self):
474
manager = self.get_manager()
475
shelf_id, shelf_file = manager.new_shelf()
477
self.assertEqual(1, shelf_id)
478
shelf_id, shelf_file = manager.new_shelf()
480
self.assertEqual(2, shelf_id)
481
manager.delete_shelf(1)
482
shelf_id, shelf_file = manager.new_shelf()
484
self.assertEqual(3, shelf_id)
486
def test_active_shelves(self):
487
manager = self.get_manager()
488
self.assertEqual([], manager.active_shelves())
489
shelf_id, shelf_file = manager.new_shelf()
491
self.assertEqual([1], manager.active_shelves())
493
def test_delete_shelf(self):
494
manager = self.get_manager()
495
shelf_id, shelf_file = manager.new_shelf()
497
self.assertEqual([1], manager.active_shelves())
498
manager.delete_shelf(1)
499
self.assertEqual([], manager.active_shelves())
501
def test_last_shelf(self):
502
manager = self.get_manager()
503
self.assertIs(None, manager.last_shelf())
504
shelf_id, shelf_file = manager.new_shelf()
506
self.assertEqual(1, manager.last_shelf())
508
def test_read_shelf(self):
509
manager = self.get_manager()
510
shelf_id, shelf_file = manager.new_shelf()
512
shelf_file.write('foo')
515
shelf_id, shelf_file = manager.new_shelf()
517
shelf_file.write('bar')
520
shelf_file = manager.read_shelf(1)
522
self.assertEqual('foo', shelf_file.read())
525
shelf_file = manager.read_shelf(2)
527
self.assertEqual('bar', shelf_file.read())
531
def test_read_non_existant(self):
532
manager = self.get_manager()
533
e = self.assertRaises(errors.NoSuchShelfId, manager.read_shelf, 1)
534
self.assertEqual('No changes are shelved with id "1".', str(e))
536
def test_shelve_changes(self):
537
tree = self.make_branch_and_tree('tree')
538
tree.commit('no-change commit')
540
self.addCleanup(tree.unlock)
541
self.build_tree_contents([('tree/foo', 'bar')])
542
self.assertFileEqual('bar', 'tree/foo')
543
tree.add('foo', 'foo-id')
544
creator = shelf.ShelfCreator(tree, tree.basis_tree())
545
self.addCleanup(creator.finalize)
546
list(creator.iter_shelvable())
547
creator.shelve_creation('foo-id')
548
shelf_manager = tree.get_shelf_manager()
549
shelf_id = shelf_manager.shelve_changes(creator)
550
self.failIfExists('tree/foo')
551
unshelver = shelf_manager.get_unshelver(shelf_id)
552
unshelver.make_merger().do_merge()
553
self.assertFileEqual('bar', 'tree/foo')
555
def test_get_metadata(self):
556
tree = self.make_branch_and_tree('.')
557
creator = shelf.ShelfCreator(tree, tree.basis_tree())
558
shelf_manager = tree.get_shelf_manager()
559
shelf_id = shelf_manager.shelve_changes(creator, 'foo')
560
metadata = shelf_manager.get_metadata(shelf_id)
561
self.assertEqual('foo', metadata['message'])
562
self.assertEqual('null:', metadata['revision_id'])