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 prepare_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
tree.lock_tree_write()
50
self.addCleanup(tree.unlock)
51
creator = shelf.ShelfCreator(tree, tree.basis_tree())
52
self.addCleanup(creator.finalize)
53
self.assertEqual([('rename', 'foo-id', 'foo', 'bar')],
54
list(creator.iter_shelvable()))
57
def check_shelve_rename(self, creator):
58
work_trans_id = creator.work_transform.trans_id_file_id('foo-id')
59
self.assertEqual('foo', creator.work_transform.final_name(
61
shelf_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
62
self.assertEqual('bar', creator.shelf_transform.final_name(
65
def test_shelve_rename(self):
66
creator = self.prepare_shelve_rename()
67
creator.shelve_rename('foo-id')
68
self.check_shelve_rename(creator)
70
def test_shelve_change_handles_rename(self):
71
creator = self.prepare_shelve_rename()
72
creator.shelve_change(('rename', 'foo-id', 'foo', 'bar'))
73
self.check_shelve_rename(creator)
75
def prepare_shelve_move(self):
76
tree = self.make_branch_and_tree('.')
77
self.build_tree(['foo/', 'bar/', 'foo/baz'])
78
tree.add(['foo', 'bar', 'foo/baz'], ['foo-id', 'bar-id', 'baz-id'])
80
tree.rename_one('foo/baz', 'bar/baz')
81
tree.lock_tree_write()
82
self.addCleanup(tree.unlock)
83
creator = shelf.ShelfCreator(tree, tree.basis_tree())
84
self.addCleanup(creator.finalize)
85
self.assertEqual([('rename', 'baz-id', 'foo/baz', 'bar/baz')],
86
list(creator.iter_shelvable()))
89
def check_shelve_move(self, creator, tree):
90
work_trans_id = creator.work_transform.trans_id_file_id('baz-id')
91
work_foo = creator.work_transform.trans_id_file_id('foo-id')
92
self.assertEqual(work_foo, creator.work_transform.final_parent(
94
shelf_trans_id = creator.shelf_transform.trans_id_file_id('baz-id')
95
shelf_bar = creator.shelf_transform.trans_id_file_id('bar-id')
96
self.assertEqual(shelf_bar, creator.shelf_transform.final_parent(
99
self.assertEqual('foo/baz', tree.id2path('baz-id'))
101
def test_shelve_move(self):
102
creator, tree = self.prepare_shelve_move()
103
creator.shelve_rename('baz-id')
104
self.check_shelve_move(creator, tree)
106
def test_shelve_change_handles_move(self):
107
creator, tree = self.prepare_shelve_move()
108
creator.shelve_change(('rename', 'baz-id', 'foo/baz', 'bar/baz'))
109
self.check_shelve_move(creator, tree)
111
def assertShelvedFileEqual(self, expected_content, creator, file_id):
112
s_trans_id = creator.shelf_transform.trans_id_file_id(file_id)
113
shelf_file = creator.shelf_transform._limbo_name(s_trans_id)
114
self.assertFileEqual(expected_content, shelf_file)
116
def prepare_content_change(self):
117
tree = self.make_branch_and_tree('.')
119
self.addCleanup(tree.unlock)
120
self.build_tree_contents([('foo', 'a\n')])
121
tree.add('foo', 'foo-id')
122
tree.commit('Committed foo')
123
self.build_tree_contents([('foo', 'b\na\nc\n')])
124
creator = shelf.ShelfCreator(tree, tree.basis_tree())
125
self.addCleanup(creator.finalize)
128
def test_shelve_content_change(self):
129
creator = self.prepare_content_change()
130
self.assertEqual([('modify text', 'foo-id')],
131
list(creator.iter_shelvable()))
132
creator.shelve_lines('foo-id', ['a\n', 'c\n'])
134
self.assertFileEqual('a\nc\n', 'foo')
135
self.assertShelvedFileEqual('b\na\n', creator, 'foo-id')
137
def test_shelve_change_handles_modify_text(self):
138
creator = self.prepare_content_change()
139
creator.shelve_change(('modify text', 'foo-id'))
141
self.assertFileEqual('a\n', 'foo')
142
self.assertShelvedFileEqual('b\na\nc\n', creator, 'foo-id')
144
def test_shelve_all(self):
145
creator = self.prepare_content_change()
148
self.assertFileEqual('a\n', 'foo')
149
self.assertShelvedFileEqual('b\na\nc\n', creator, 'foo-id')
151
def prepare_shelve_creation(self):
152
tree = self.make_branch_and_tree('.')
154
self.addCleanup(tree.unlock)
155
tree.commit('Empty tree')
156
self.build_tree_contents([('foo', 'a\n'), ('bar/',)])
157
tree.add(['foo', 'bar'], ['foo-id', 'bar-id'])
158
creator = shelf.ShelfCreator(tree, tree.basis_tree())
159
self.addCleanup(creator.finalize)
160
self.assertEqual([('add file', 'bar-id', 'directory', 'bar'),
161
('add file', 'foo-id', 'file', 'foo')],
162
sorted(list(creator.iter_shelvable())))
165
def check_shelve_creation(self, creator, tree):
166
self.assertRaises(StopIteration,
167
tree.iter_entries_by_dir(['foo-id']).next)
168
s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
169
self.assertEqual('foo-id',
170
creator.shelf_transform.final_file_id(s_trans_id))
171
self.failIfExists('foo')
172
self.failIfExists('bar')
173
self.assertShelvedFileEqual('a\n', creator, 'foo-id')
174
s_bar_trans_id = creator.shelf_transform.trans_id_file_id('bar-id')
175
self.assertEqual('directory',
176
creator.shelf_transform.final_kind(s_bar_trans_id))
178
def test_shelve_creation(self):
179
creator, tree = self.prepare_shelve_creation()
180
creator.shelve_creation('foo-id')
181
creator.shelve_creation('bar-id')
183
self.check_shelve_creation(creator, tree)
185
def test_shelve_change_handles_creation(self):
186
creator, tree = self.prepare_shelve_creation()
187
creator.shelve_change(('add file', 'foo-id', 'file', 'foo'))
188
creator.shelve_change(('add file', 'bar-id', 'directory', 'bar'))
190
self.check_shelve_creation(creator, tree)
192
def _test_shelve_symlink_creation(self, link_name, link_target,
193
shelve_change=False):
194
self.requireFeature(tests.SymlinkFeature)
195
tree = self.make_branch_and_tree('.')
197
self.addCleanup(tree.unlock)
198
tree.commit('Empty tree')
199
os.symlink(link_target, link_name)
200
tree.add(link_name, 'foo-id')
201
creator = shelf.ShelfCreator(tree, tree.basis_tree())
202
self.addCleanup(creator.finalize)
203
self.assertEqual([('add file', 'foo-id', 'symlink', link_name)],
204
list(creator.iter_shelvable()))
206
creator.shelve_change(('add file', 'foo-id', 'symlink', link_name))
208
creator.shelve_creation('foo-id')
210
s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
211
self.failIfExists(link_name)
212
limbo_name = creator.shelf_transform._limbo_name(s_trans_id)
213
self.assertEqual(link_target, osutils.readlink(limbo_name))
214
ptree = creator.shelf_transform.get_preview_tree()
215
self.assertEqual(link_target, ptree.get_symlink_target('foo-id'))
217
def test_shelve_symlink_creation(self):
218
self._test_shelve_symlink_creation('foo', 'bar')
220
def test_shelve_unicode_symlink_creation(self):
221
self.requireFeature(tests.UnicodeFilenameFeature)
222
self._test_shelve_symlink_creation(u'fo\N{Euro Sign}o',
225
def test_shelve_change_handles_symlink_creation(self):
226
self._test_shelve_symlink_creation('foo', 'bar', shelve_change=True)
228
def _test_shelve_symlink_target_change(self, link_name,
229
old_target, new_target,
230
shelve_change=False):
231
self.requireFeature(tests.SymlinkFeature)
232
tree = self.make_branch_and_tree('.')
234
self.addCleanup(tree.unlock)
235
os.symlink(old_target, link_name)
236
tree.add(link_name, 'foo-id')
237
tree.commit("commit symlink")
239
os.symlink(new_target, link_name)
240
creator = shelf.ShelfCreator(tree, tree.basis_tree())
241
self.addCleanup(creator.finalize)
242
self.assertEqual([('modify target', 'foo-id', link_name,
243
old_target, new_target)],
244
list(creator.iter_shelvable()))
246
creator.shelve_change(('modify target', 'foo-id', link_name,
247
old_target, new_target))
249
creator.shelve_modify_target('foo-id')
251
self.assertEqual(old_target, osutils.readlink(link_name))
252
s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
253
limbo_name = creator.shelf_transform._limbo_name(s_trans_id)
254
self.assertEqual(new_target, osutils.readlink(limbo_name))
255
ptree = creator.shelf_transform.get_preview_tree()
256
self.assertEqual(new_target, ptree.get_symlink_target('foo-id'))
258
def test_shelve_symlink_target_change(self):
259
self._test_shelve_symlink_target_change('foo', 'bar', 'baz')
261
def test_shelve_unicode_symlink_target_change(self):
262
self.requireFeature(tests.UnicodeFilenameFeature)
263
self._test_shelve_symlink_target_change(
264
u'fo\N{Euro Sign}o', u'b\N{Euro Sign}ar', u'b\N{Euro Sign}az')
266
def test_shelve_change_handles_symlink_target_change(self):
267
self._test_shelve_symlink_target_change('foo', 'bar', 'baz',
270
def test_shelve_creation_no_contents(self):
271
tree = self.make_branch_and_tree('.')
273
self.addCleanup(tree.unlock)
274
tree.commit('Empty tree')
275
self.build_tree(['foo'])
276
tree.add('foo', 'foo-id')
278
creator = shelf.ShelfCreator(tree, tree.basis_tree())
279
self.addCleanup(creator.finalize)
280
self.assertEqual([('add file', 'foo-id', None, 'foo')],
281
sorted(list(creator.iter_shelvable())))
282
creator.shelve_creation('foo-id')
284
self.assertRaises(StopIteration,
285
tree.iter_entries_by_dir(['foo-id']).next)
286
self.assertShelvedFileEqual('', creator, 'foo-id')
287
s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
288
self.assertEqual('foo-id',
289
creator.shelf_transform.final_file_id(s_trans_id))
290
self.failIfExists('foo')
292
def prepare_shelve_deletion(self):
293
tree = self.make_branch_and_tree('tree')
295
self.addCleanup(tree.unlock)
296
self.build_tree_contents([('tree/foo/',), ('tree/foo/bar', 'baz')])
297
tree.add(['foo', 'foo/bar'], ['foo-id', 'bar-id'])
298
tree.commit('Added file and directory')
299
tree.unversion(['foo-id', 'bar-id'])
300
os.unlink('tree/foo/bar')
302
creator = shelf.ShelfCreator(tree, tree.basis_tree())
303
self.addCleanup(creator.finalize)
304
self.assertEqual([('delete file', 'bar-id', 'file', 'foo/bar'),
305
('delete file', 'foo-id', 'directory', 'foo')],
306
sorted(list(creator.iter_shelvable())))
309
def check_shelve_deletion(self, tree):
310
self.assertTrue('foo-id' in tree)
311
self.assertTrue('bar-id' in tree)
312
self.assertFileEqual('baz', 'tree/foo/bar')
314
def test_shelve_deletion(self):
315
creator, tree = self.prepare_shelve_deletion()
316
creator.shelve_deletion('foo-id')
317
creator.shelve_deletion('bar-id')
319
self.check_shelve_deletion(tree)
321
def test_shelve_change_handles_deletion(self):
322
creator, tree = self.prepare_shelve_deletion()
323
creator.shelve_change(('delete file', 'foo-id', 'directory', 'foo'))
324
creator.shelve_change(('delete file', 'bar-id', 'file', 'foo/bar'))
326
self.check_shelve_deletion(tree)
328
def test_shelve_delete_contents(self):
329
tree = self.make_branch_and_tree('tree')
330
self.build_tree(['tree/foo',])
331
tree.add('foo', 'foo-id')
332
tree.commit('Added file and directory')
333
os.unlink('tree/foo')
334
tree.lock_tree_write()
335
self.addCleanup(tree.unlock)
336
creator = shelf.ShelfCreator(tree, tree.basis_tree())
337
self.addCleanup(creator.finalize)
338
self.assertEqual([('delete file', 'foo-id', 'file', 'foo')],
339
sorted(list(creator.iter_shelvable())))
340
creator.shelve_deletion('foo-id')
342
self.failUnlessExists('tree/foo')
344
def prepare_shelve_change_kind(self):
345
tree = self.make_branch_and_tree('tree')
346
self.build_tree_contents([('tree/foo', 'bar')])
347
tree.add('foo', 'foo-id')
348
tree.commit('Added file and directory')
349
os.unlink('tree/foo')
351
tree.lock_tree_write()
352
self.addCleanup(tree.unlock)
353
creator = shelf.ShelfCreator(tree, tree.basis_tree())
354
self.addCleanup(creator.finalize)
355
self.assertEqual([('change kind', 'foo-id', 'file', 'directory',
356
'foo')], sorted(list(creator.iter_shelvable())))
359
def check_shelve_change_kind(self, creator):
360
self.assertFileEqual('bar', 'tree/foo')
361
s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
362
self.assertEqual('directory',
363
creator.shelf_transform._new_contents[s_trans_id])
365
def test_shelve_change_kind(self):
366
creator = self.prepare_shelve_change_kind()
367
creator.shelve_content_change('foo-id')
369
self.check_shelve_change_kind(creator)
371
def test_shelve_change_handles_change_kind(self):
372
creator = self.prepare_shelve_change_kind()
373
creator.shelve_change(('change kind', 'foo-id', 'file', 'directory',
376
self.check_shelve_change_kind(creator)
378
def test_shelve_change_unknown_change(self):
379
tree = self.make_branch_and_tree('tree')
380
tree.lock_tree_write()
381
self.addCleanup(tree.unlock)
382
creator = shelf.ShelfCreator(tree, tree.basis_tree())
383
self.addCleanup(creator.finalize)
384
e = self.assertRaises(ValueError, creator.shelve_change, ('unknown',))
385
self.assertEqual('Unknown change kind: "unknown"', str(e))
387
def test_shelve_unversion(self):
388
tree = self.make_branch_and_tree('tree')
389
self.build_tree(['tree/foo',])
390
tree.add('foo', 'foo-id')
391
tree.commit('Added file and directory')
392
tree.unversion(['foo-id'])
393
tree.lock_tree_write()
394
self.addCleanup(tree.unlock)
395
creator = shelf.ShelfCreator(tree, tree.basis_tree())
396
self.addCleanup(creator.finalize)
397
self.assertEqual([('delete file', 'foo-id', 'file', 'foo')],
398
sorted(list(creator.iter_shelvable())))
399
creator.shelve_deletion('foo-id')
401
self.failUnlessExists('tree/foo')
403
def test_shelve_serialization(self):
404
tree = self.make_branch_and_tree('.')
405
tree.lock_tree_write()
406
self.addCleanup(tree.unlock)
407
creator = shelf.ShelfCreator(tree, tree.basis_tree())
408
self.addCleanup(creator.finalize)
409
shelf_file = open('shelf', 'wb')
410
self.addCleanup(shelf_file.close)
412
creator.write_shelf(shelf_file)
415
self.assertFileEqual(EMPTY_SHELF, 'shelf')
417
def test_write_shelf(self):
418
tree = self.make_branch_and_tree('tree')
419
self.build_tree(['tree/foo'])
420
tree.add('foo', 'foo-id')
421
tree.lock_tree_write()
422
self.addCleanup(tree.unlock)
423
creator = shelf.ShelfCreator(tree, tree.basis_tree())
424
self.addCleanup(creator.finalize)
425
list(creator.iter_shelvable())
426
creator.shelve_creation('foo-id')
427
shelf_file = open('shelf', 'wb')
429
creator.write_shelf(shelf_file)
432
parser = pack.ContainerPushParser()
433
shelf_file = open('shelf', 'rb')
435
parser.accept_bytes(shelf_file.read())
438
tt = transform.TransformPreview(tree)
439
self.addCleanup(tt.finalize)
440
records = iter(parser.read_pending_records())
443
tt.deserialize(records)
445
def test_shelve_unversioned(self):
446
tree = self.make_branch_and_tree('tree')
447
tree.lock_tree_write()
449
self.assertRaises(errors.PathsNotVersionedError,
450
shelf.ShelfCreator, tree, tree.basis_tree(), ['foo'])
453
# We should be able to lock/unlock the tree if ShelfCreator cleaned
455
wt = workingtree.WorkingTree.open('tree')
458
# And a second tentative should raise the same error (no
459
# limbo/pending_deletion leftovers).
460
tree.lock_tree_write()
462
self.assertRaises(errors.PathsNotVersionedError,
463
shelf.ShelfCreator, tree, tree.basis_tree(), ['foo'])
467
def test_shelve_skips_added_root(self):
468
"""Skip adds of the root when iterating through shelvable changes."""
469
tree = self.make_branch_and_tree('tree')
470
tree.lock_tree_write()
471
self.addCleanup(tree.unlock)
472
creator = shelf.ShelfCreator(tree, tree.basis_tree())
473
self.addCleanup(creator.finalize)
474
self.assertEqual([], list(creator.iter_shelvable()))
476
def test_shelve_skips_added_root(self):
477
"""Skip adds of the root when iterating through shelvable changes."""
478
tree = self.make_branch_and_tree('tree')
479
creator = shelf.ShelfCreator(tree, tree.basis_tree())
480
self.addCleanup(creator.finalize)
481
self.assertEqual([], list(creator.iter_shelvable()))
484
class TestUnshelver(tests.TestCaseWithTransport):
486
def test_make_merger(self):
487
tree = self.make_branch_and_tree('tree')
488
tree.commit('first commit')
489
self.build_tree_contents([('tree/foo', 'bar')])
491
self.addCleanup(tree.unlock)
492
tree.add('foo', 'foo-id')
493
creator = shelf.ShelfCreator(tree, tree.basis_tree())
494
self.addCleanup(creator.finalize)
495
list(creator.iter_shelvable())
496
creator.shelve_creation('foo-id')
497
shelf_file = open('shelf-file', 'w+b')
499
creator.write_shelf(shelf_file)
502
unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
503
unshelver.make_merger().do_merge()
504
self.assertFileEqual('bar', 'tree/foo')
508
def test_unshelve_changed(self):
509
tree = self.make_branch_and_tree('tree')
511
self.addCleanup(tree.unlock)
512
self.build_tree_contents([('tree/foo', 'a\nb\nc\n')])
513
tree.add('foo', 'foo-id')
514
tree.commit('first commit')
515
self.build_tree_contents([('tree/foo', 'a\nb\nd\n')])
516
creator = shelf.ShelfCreator(tree, tree.basis_tree())
517
self.addCleanup(creator.finalize)
518
list(creator.iter_shelvable())
519
creator.shelve_lines('foo-id', ['a\n', 'b\n', 'c\n'])
520
shelf_file = open('shelf', 'w+b')
521
self.addCleanup(shelf_file.close)
522
creator.write_shelf(shelf_file)
524
self.build_tree_contents([('tree/foo', 'z\na\nb\nc\n')])
526
unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
527
unshelver.make_merger().do_merge()
528
self.assertFileEqual('z\na\nb\nd\n', 'tree/foo')
530
def test_unshelve_deleted(self):
531
tree = self.make_branch_and_tree('tree')
533
self.addCleanup(tree.unlock)
534
self.build_tree_contents([('tree/foo/',), ('tree/foo/bar', 'baz')])
535
tree.add(['foo', 'foo/bar'], ['foo-id', 'bar-id'])
536
tree.commit('Added file and directory')
537
tree.unversion(['foo-id', 'bar-id'])
538
os.unlink('tree/foo/bar')
540
creator = shelf.ShelfCreator(tree, tree.basis_tree())
541
list(creator.iter_shelvable())
542
creator.shelve_deletion('foo-id')
543
creator.shelve_deletion('bar-id')
544
shelf_file = open('shelf', 'w+b')
545
self.addCleanup(shelf_file.close)
546
creator.write_shelf(shelf_file)
549
# validate the test setup
550
self.assertTrue('foo-id' in tree)
551
self.assertTrue('bar-id' in tree)
552
self.assertFileEqual('baz', 'tree/foo/bar')
554
unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
555
unshelver.make_merger().do_merge()
556
self.assertFalse('foo-id' in tree)
557
self.assertFalse('bar-id' in tree)
559
def test_unshelve_base(self):
560
tree = self.make_branch_and_tree('tree')
562
self.addCleanup(tree.unlock)
563
tree.commit('rev1', rev_id='rev1')
564
creator = shelf.ShelfCreator(tree, tree.basis_tree())
565
self.addCleanup(creator.finalize)
566
manager = tree.get_shelf_manager()
567
shelf_id, shelf_file = manager.new_shelf()
569
creator.write_shelf(shelf_file)
572
tree.commit('rev2', rev_id='rev2')
573
shelf_file = manager.read_shelf(1)
574
self.addCleanup(shelf_file.close)
575
unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
576
self.addCleanup(unshelver.finalize)
577
self.assertEqual('rev1', unshelver.base_tree.get_revision_id())
579
def test_unshelve_serialization(self):
580
tree = self.make_branch_and_tree('.')
581
self.build_tree_contents([('shelf', EMPTY_SHELF)])
582
shelf_file = open('shelf', 'rb')
583
self.addCleanup(shelf_file.close)
584
unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
587
def test_corrupt_shelf(self):
588
tree = self.make_branch_and_tree('.')
589
self.build_tree_contents([('shelf', EMPTY_SHELF.replace('metadata',
591
shelf_file = open('shelf', 'rb')
592
self.addCleanup(shelf_file.close)
593
e = self.assertRaises(errors.ShelfCorrupt,
594
shelf.Unshelver.from_tree_and_shelf, tree,
596
self.assertEqual('Shelf corrupt.', str(e))
599
class TestShelfManager(tests.TestCaseWithTransport):
601
def test_get_shelf_manager(self):
602
tree = self.make_branch_and_tree('.')
603
manager = tree.get_shelf_manager()
604
self.assertEqual(tree._transport.base + 'shelf/',
605
manager.transport.base)
607
def get_manager(self):
608
return self.make_branch_and_tree('.').get_shelf_manager()
610
def test_get_shelf_filename(self):
611
tree = self.make_branch_and_tree('.')
612
manager = tree.get_shelf_manager()
613
self.assertEqual('shelf-1', manager.get_shelf_filename(1))
615
def test_get_shelf_ids(self):
616
tree = self.make_branch_and_tree('.')
617
manager = tree.get_shelf_manager()
618
self.assertEqual([1, 3], manager.get_shelf_ids(
619
['shelf-1', 'shelf-02', 'shelf-3']))
621
def test_new_shelf(self):
622
manager = self.get_manager()
623
shelf_id, shelf_file = manager.new_shelf()
625
self.assertEqual(1, shelf_id)
626
shelf_id, shelf_file = manager.new_shelf()
628
self.assertEqual(2, shelf_id)
629
manager.delete_shelf(1)
630
shelf_id, shelf_file = manager.new_shelf()
632
self.assertEqual(3, shelf_id)
634
def test_active_shelves(self):
635
manager = self.get_manager()
636
self.assertEqual([], manager.active_shelves())
637
shelf_id, shelf_file = manager.new_shelf()
639
self.assertEqual([1], manager.active_shelves())
641
def test_delete_shelf(self):
642
manager = self.get_manager()
643
shelf_id, shelf_file = manager.new_shelf()
645
self.assertEqual([1], manager.active_shelves())
646
manager.delete_shelf(1)
647
self.assertEqual([], manager.active_shelves())
649
def test_last_shelf(self):
650
manager = self.get_manager()
651
self.assertIs(None, manager.last_shelf())
652
shelf_id, shelf_file = manager.new_shelf()
654
self.assertEqual(1, manager.last_shelf())
656
def test_read_shelf(self):
657
manager = self.get_manager()
658
shelf_id, shelf_file = manager.new_shelf()
660
shelf_file.write('foo')
663
shelf_id, shelf_file = manager.new_shelf()
665
shelf_file.write('bar')
668
shelf_file = manager.read_shelf(1)
670
self.assertEqual('foo', shelf_file.read())
673
shelf_file = manager.read_shelf(2)
675
self.assertEqual('bar', shelf_file.read())
679
def test_read_non_existant(self):
680
manager = self.get_manager()
681
e = self.assertRaises(errors.NoSuchShelfId, manager.read_shelf, 1)
682
self.assertEqual('No changes are shelved with id "1".', str(e))
684
def test_shelve_changes(self):
685
tree = self.make_branch_and_tree('tree')
686
tree.commit('no-change commit')
688
self.addCleanup(tree.unlock)
689
self.build_tree_contents([('tree/foo', 'bar')])
690
self.assertFileEqual('bar', 'tree/foo')
691
tree.add('foo', 'foo-id')
692
creator = shelf.ShelfCreator(tree, tree.basis_tree())
693
self.addCleanup(creator.finalize)
694
list(creator.iter_shelvable())
695
creator.shelve_creation('foo-id')
696
shelf_manager = tree.get_shelf_manager()
697
shelf_id = shelf_manager.shelve_changes(creator)
698
self.failIfExists('tree/foo')
699
unshelver = shelf_manager.get_unshelver(shelf_id)
700
unshelver.make_merger().do_merge()
701
self.assertFileEqual('bar', 'tree/foo')
703
def test_get_metadata(self):
704
tree = self.make_branch_and_tree('.')
705
tree.lock_tree_write()
706
self.addCleanup(tree.unlock)
707
creator = shelf.ShelfCreator(tree, tree.basis_tree())
708
self.addCleanup(creator.finalize)
709
shelf_manager = tree.get_shelf_manager()
710
shelf_id = shelf_manager.shelve_changes(creator, 'foo')
711
metadata = shelf_manager.get_metadata(shelf_id)
712
self.assertEqual('foo', metadata['message'])
713
self.assertEqual('null:', metadata['revision_id'])