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
tree.lock_tree_write()
480
self.addCleanup(tree.unlock)
481
creator = shelf.ShelfCreator(tree, tree.basis_tree())
482
self.addCleanup(creator.finalize)
483
self.assertEqual([], list(creator.iter_shelvable()))
486
class TestUnshelver(tests.TestCaseWithTransport):
488
def test_make_merger(self):
489
tree = self.make_branch_and_tree('tree')
490
tree.commit('first commit')
491
self.build_tree_contents([('tree/foo', 'bar')])
493
self.addCleanup(tree.unlock)
494
tree.add('foo', 'foo-id')
495
creator = shelf.ShelfCreator(tree, tree.basis_tree())
496
self.addCleanup(creator.finalize)
497
list(creator.iter_shelvable())
498
creator.shelve_creation('foo-id')
499
shelf_file = open('shelf-file', 'w+b')
501
creator.write_shelf(shelf_file)
504
unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
505
unshelver.make_merger().do_merge()
506
self.addCleanup(unshelver.finalize)
507
self.assertFileEqual('bar', 'tree/foo')
511
def test_unshelve_changed(self):
512
tree = self.make_branch_and_tree('tree')
514
self.addCleanup(tree.unlock)
515
self.build_tree_contents([('tree/foo', 'a\nb\nc\n')])
516
tree.add('foo', 'foo-id')
517
tree.commit('first commit')
518
self.build_tree_contents([('tree/foo', 'a\nb\nd\n')])
519
creator = shelf.ShelfCreator(tree, tree.basis_tree())
520
self.addCleanup(creator.finalize)
521
list(creator.iter_shelvable())
522
creator.shelve_lines('foo-id', ['a\n', 'b\n', 'c\n'])
523
shelf_file = open('shelf', 'w+b')
524
self.addCleanup(shelf_file.close)
525
creator.write_shelf(shelf_file)
527
self.build_tree_contents([('tree/foo', 'z\na\nb\nc\n')])
529
unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
530
self.addCleanup(unshelver.finalize)
531
unshelver.make_merger().do_merge()
532
self.assertFileEqual('z\na\nb\nd\n', 'tree/foo')
534
def test_unshelve_deleted(self):
535
tree = self.make_branch_and_tree('tree')
537
self.addCleanup(tree.unlock)
538
self.build_tree_contents([('tree/foo/',), ('tree/foo/bar', 'baz')])
539
tree.add(['foo', 'foo/bar'], ['foo-id', 'bar-id'])
540
tree.commit('Added file and directory')
541
tree.unversion(['foo-id', 'bar-id'])
542
os.unlink('tree/foo/bar')
544
creator = shelf.ShelfCreator(tree, tree.basis_tree())
545
list(creator.iter_shelvable())
546
creator.shelve_deletion('foo-id')
547
creator.shelve_deletion('bar-id')
548
shelf_file = open('shelf', 'w+b')
549
self.addCleanup(shelf_file.close)
550
creator.write_shelf(shelf_file)
553
# validate the test setup
554
self.assertTrue('foo-id' in tree)
555
self.assertTrue('bar-id' in tree)
556
self.assertFileEqual('baz', 'tree/foo/bar')
558
unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
559
self.addCleanup(unshelver.finalize)
560
unshelver.make_merger().do_merge()
561
self.assertFalse('foo-id' in tree)
562
self.assertFalse('bar-id' in tree)
564
def test_unshelve_base(self):
565
tree = self.make_branch_and_tree('tree')
567
self.addCleanup(tree.unlock)
568
tree.commit('rev1', rev_id='rev1')
569
creator = shelf.ShelfCreator(tree, tree.basis_tree())
570
self.addCleanup(creator.finalize)
571
manager = tree.get_shelf_manager()
572
shelf_id, shelf_file = manager.new_shelf()
574
creator.write_shelf(shelf_file)
577
tree.commit('rev2', rev_id='rev2')
578
shelf_file = manager.read_shelf(1)
579
self.addCleanup(shelf_file.close)
580
unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
581
self.addCleanup(unshelver.finalize)
582
self.assertEqual('rev1', unshelver.base_tree.get_revision_id())
584
def test_unshelve_serialization(self):
585
tree = self.make_branch_and_tree('.')
586
self.build_tree_contents([('shelf', EMPTY_SHELF)])
587
shelf_file = open('shelf', 'rb')
588
self.addCleanup(shelf_file.close)
589
unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
592
def test_corrupt_shelf(self):
593
tree = self.make_branch_and_tree('.')
594
self.build_tree_contents([('shelf', EMPTY_SHELF.replace('metadata',
596
shelf_file = open('shelf', 'rb')
597
self.addCleanup(shelf_file.close)
598
e = self.assertRaises(errors.ShelfCorrupt,
599
shelf.Unshelver.from_tree_and_shelf, tree,
601
self.assertEqual('Shelf corrupt.', str(e))
604
class TestShelfManager(tests.TestCaseWithTransport):
606
def test_get_shelf_manager(self):
607
tree = self.make_branch_and_tree('.')
608
manager = tree.get_shelf_manager()
609
self.assertEqual(tree._transport.base + 'shelf/',
610
manager.transport.base)
612
def get_manager(self):
613
return self.make_branch_and_tree('.').get_shelf_manager()
615
def test_get_shelf_filename(self):
616
tree = self.make_branch_and_tree('.')
617
manager = tree.get_shelf_manager()
618
self.assertEqual('shelf-1', manager.get_shelf_filename(1))
620
def test_get_shelf_ids(self):
621
tree = self.make_branch_and_tree('.')
622
manager = tree.get_shelf_manager()
623
self.assertEqual([1, 3], manager.get_shelf_ids(
624
['shelf-1', 'shelf-02', 'shelf-3']))
626
def test_new_shelf(self):
627
manager = self.get_manager()
628
shelf_id, shelf_file = manager.new_shelf()
630
self.assertEqual(1, shelf_id)
631
shelf_id, shelf_file = manager.new_shelf()
633
self.assertEqual(2, shelf_id)
634
manager.delete_shelf(1)
635
shelf_id, shelf_file = manager.new_shelf()
637
self.assertEqual(3, shelf_id)
639
def test_active_shelves(self):
640
manager = self.get_manager()
641
self.assertEqual([], manager.active_shelves())
642
shelf_id, shelf_file = manager.new_shelf()
644
self.assertEqual([1], manager.active_shelves())
646
def test_delete_shelf(self):
647
manager = self.get_manager()
648
shelf_id, shelf_file = manager.new_shelf()
650
self.assertEqual([1], manager.active_shelves())
651
manager.delete_shelf(1)
652
self.assertEqual([], manager.active_shelves())
654
def test_last_shelf(self):
655
manager = self.get_manager()
656
self.assertIs(None, manager.last_shelf())
657
shelf_id, shelf_file = manager.new_shelf()
659
self.assertEqual(1, manager.last_shelf())
661
def test_read_shelf(self):
662
manager = self.get_manager()
663
shelf_id, shelf_file = manager.new_shelf()
665
shelf_file.write('foo')
668
shelf_id, shelf_file = manager.new_shelf()
670
shelf_file.write('bar')
673
shelf_file = manager.read_shelf(1)
675
self.assertEqual('foo', shelf_file.read())
678
shelf_file = manager.read_shelf(2)
680
self.assertEqual('bar', shelf_file.read())
684
def test_read_non_existant(self):
685
manager = self.get_manager()
686
e = self.assertRaises(errors.NoSuchShelfId, manager.read_shelf, 1)
687
self.assertEqual('No changes are shelved with id "1".', str(e))
689
def test_shelve_changes(self):
690
tree = self.make_branch_and_tree('tree')
691
tree.commit('no-change commit')
693
self.addCleanup(tree.unlock)
694
self.build_tree_contents([('tree/foo', 'bar')])
695
self.assertFileEqual('bar', 'tree/foo')
696
tree.add('foo', 'foo-id')
697
creator = shelf.ShelfCreator(tree, tree.basis_tree())
698
self.addCleanup(creator.finalize)
699
list(creator.iter_shelvable())
700
creator.shelve_creation('foo-id')
701
shelf_manager = tree.get_shelf_manager()
702
shelf_id = shelf_manager.shelve_changes(creator)
703
self.failIfExists('tree/foo')
704
unshelver = shelf_manager.get_unshelver(shelf_id)
705
self.addCleanup(unshelver.finalize)
706
unshelver.make_merger().do_merge()
707
self.assertFileEqual('bar', 'tree/foo')
709
def test_get_metadata(self):
710
tree = self.make_branch_and_tree('.')
711
tree.lock_tree_write()
712
self.addCleanup(tree.unlock)
713
creator = shelf.ShelfCreator(tree, tree.basis_tree())
714
self.addCleanup(creator.finalize)
715
shelf_manager = tree.get_shelf_manager()
716
shelf_id = shelf_manager.shelve_changes(creator, 'foo')
717
metadata = shelf_manager.get_metadata(shelf_id)
718
self.assertEqual('foo', metadata['message'])
719
self.assertEqual('null:', metadata['revision_id'])