~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_shelf.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-02-18 13:27:08 UTC
  • mfrom: (4011.4.3 ssh-hints)
  • Revision ID: pqm@pqm.ubuntu.com-20090218132708-okubrahz9exvae9r
(Jelmer) Point out bzr+ssh:// to the user when they use ssh://.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2008 Canonical Ltd
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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
 
16
 
 
17
import os
 
18
 
 
19
from bzrlib import errors, pack, shelf, tests, transform
 
20
 
 
21
 
 
22
EMPTY_SHELF = ("Bazaar pack format 1 (introduced in 0.18)\n"
 
23
               "B23\n"
 
24
               "metadata\n\n"
 
25
               "d11:revision_id5:null:e"
 
26
               "B159\n"
 
27
               "attribs\n\n"
 
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")
 
31
 
 
32
 
 
33
class TestPrepareShelf(tests.TestCaseWithTransport):
 
34
 
 
35
    def test_shelve_rename(self):
 
36
        tree = self.make_branch_and_tree('.')
 
37
        self.build_tree(['foo'])
 
38
        tree.add(['foo'], ['foo-id'])
 
39
        tree.commit('foo')
 
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(
 
48
                         work_trans_id))
 
49
        shelf_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
 
50
        self.assertEqual('bar', creator.shelf_transform.final_name(
 
51
                         shelf_trans_id))
 
52
 
 
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'])
 
57
        tree.commit('foo')
 
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(
 
67
                         work_trans_id))
 
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(
 
71
                         shelf_trans_id))
 
72
        creator.transform()
 
73
        self.assertEqual('foo/baz', tree.id2path('baz-id'))
 
74
 
 
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)
 
79
 
 
80
    def test_shelve_content_change(self):
 
81
        tree = self.make_branch_and_tree('.')
 
82
        tree.lock_write()
 
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'])
 
93
        creator.transform()
 
94
        self.assertFileEqual('a\nc\n', 'foo')
 
95
        self.assertShelvedFileEqual('b\na\n', creator, 'foo-id')
 
96
 
 
97
    def test_shelve_creation(self):
 
98
        tree = self.make_branch_and_tree('.')
 
99
        tree.lock_write()
 
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')
 
111
        creator.transform()
 
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))
 
123
 
 
124
    def test_shelve_symlink_creation(self):
 
125
        self.requireFeature(tests.SymlinkFeature)
 
126
        tree = self.make_branch_and_tree('.')
 
127
        tree.lock_write()
 
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')
 
137
        creator.transform()
 
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))
 
142
 
 
143
    def test_shelve_creation_no_contents(self):
 
144
        tree = self.make_branch_and_tree('.')
 
145
        tree.lock_write()
 
146
        self.addCleanup(tree.unlock)
 
147
        tree.commit('Empty tree')
 
148
        self.build_tree(['foo'])
 
149
        tree.add('foo', 'foo-id')
 
150
        os.unlink('foo')
 
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')
 
156
        creator.transform()
 
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')
 
164
 
 
165
    def test_shelve_deletion(self):
 
166
        tree = self.make_branch_and_tree('tree')
 
167
        tree.lock_write()
 
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')
 
174
        os.rmdir('tree/foo')
 
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')
 
182
        creator.transform()
 
183
        self.assertTrue('foo-id' in tree)
 
184
        self.assertTrue('bar-id' in tree)
 
185
        self.assertFileEqual('baz', 'tree/foo/bar')
 
186
 
 
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')
 
198
        creator.transform()
 
199
        self.failUnlessExists('tree/foo')
 
200
 
 
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')
 
207
        os.mkdir('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')
 
213
        creator.transform()
 
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])
 
218
 
 
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')
 
230
        creator.transform()
 
231
        self.failUnlessExists('tree/foo')
 
232
 
 
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)
 
239
        try:
 
240
            creator.write_shelf(shelf_file)
 
241
        finally:
 
242
            shelf_file.close()
 
243
        self.assertFileEqual(EMPTY_SHELF, 'shelf')
 
244
 
 
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')
 
254
        try:
 
255
            creator.write_shelf(shelf_file)
 
256
        finally:
 
257
            shelf_file.close()
 
258
        parser = pack.ContainerPushParser()
 
259
        shelf_file = open('shelf', 'rb')
 
260
        try:
 
261
            parser.accept_bytes(shelf_file.read())
 
262
        finally:
 
263
            shelf_file.close()
 
264
        tt = transform.TransformPreview(tree)
 
265
        self.addCleanup(tt.finalize)
 
266
        records = iter(parser.read_pending_records())
 
267
        #skip revision-id
 
268
        records.next()
 
269
        tt.deserialize(records)
 
270
 
 
271
 
 
272
class TestUnshelver(tests.TestCaseWithTransport):
 
273
 
 
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')])
 
278
        tree.lock_write()
 
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')
 
286
        try:
 
287
            creator.write_shelf(shelf_file)
 
288
            creator.transform()
 
289
            shelf_file.seek(0)
 
290
            unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
 
291
            unshelver.make_merger().do_merge()
 
292
            self.assertFileEqual('bar', 'tree/foo')
 
293
        finally:
 
294
            shelf_file.close()
 
295
 
 
296
    def test_unshelve_changed(self):
 
297
        tree = self.make_branch_and_tree('tree')
 
298
        tree.lock_write()
 
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)
 
311
        creator.transform()
 
312
        self.build_tree_contents([('tree/foo', 'z\na\nb\nc\n')])
 
313
        shelf_file.seek(0)
 
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')
 
317
 
 
318
    def test_unshelve_deleted(self):
 
319
        tree = self.make_branch_and_tree('tree')
 
320
        tree.lock_write()
 
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')
 
327
        os.rmdir('tree/foo')
 
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)
 
335
        creator.transform()
 
336
        creator.finalize()
 
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')
 
341
        shelf_file.seek(0)
 
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)
 
346
 
 
347
    def test_unshelve_base(self):
 
348
        tree = self.make_branch_and_tree('tree')
 
349
        tree.lock_write()
 
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()
 
356
        try:
 
357
            creator.write_shelf(shelf_file)
 
358
        finally:
 
359
            shelf_file.close()
 
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())
 
366
 
 
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)
 
373
 
 
374
    def test_corrupt_shelf(self):
 
375
        tree = self.make_branch_and_tree('.')
 
376
        self.build_tree_contents([('shelf', EMPTY_SHELF.replace('metadata',
 
377
                                                                'foo'))])
 
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,
 
382
                              shelf_file)
 
383
        self.assertEqual('Shelf corrupt.', str(e))
 
384
 
 
385
 
 
386
class TestShelfManager(tests.TestCaseWithTransport):
 
387
 
 
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)
 
393
 
 
394
    def get_manager(self):
 
395
        return self.make_branch_and_tree('.').get_shelf_manager()
 
396
 
 
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))
 
401
 
 
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']))
 
407
 
 
408
    def test_new_shelf(self):
 
409
        manager = self.get_manager()
 
410
        shelf_id, shelf_file = manager.new_shelf()
 
411
        shelf_file.close()
 
412
        self.assertEqual(1, shelf_id)
 
413
        shelf_id, shelf_file = manager.new_shelf()
 
414
        shelf_file.close()
 
415
        self.assertEqual(2, shelf_id)
 
416
        manager.delete_shelf(1)
 
417
        shelf_id, shelf_file = manager.new_shelf()
 
418
        shelf_file.close()
 
419
        self.assertEqual(3, shelf_id)
 
420
 
 
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()
 
425
        shelf_file.close()
 
426
        self.assertEqual([1], manager.active_shelves())
 
427
 
 
428
    def test_delete_shelf(self):
 
429
        manager = self.get_manager()
 
430
        shelf_id, shelf_file = manager.new_shelf()
 
431
        shelf_file.close()
 
432
        self.assertEqual([1], manager.active_shelves())
 
433
        manager.delete_shelf(1)
 
434
        self.assertEqual([], manager.active_shelves())
 
435
 
 
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()
 
440
        shelf_file.close()
 
441
        self.assertEqual(1, manager.last_shelf())
 
442
 
 
443
    def test_read_shelf(self):
 
444
        manager = self.get_manager()
 
445
        shelf_id, shelf_file = manager.new_shelf()
 
446
        try:
 
447
            shelf_file.write('foo')
 
448
        finally:
 
449
            shelf_file.close()
 
450
        shelf_id, shelf_file = manager.new_shelf()
 
451
        try:
 
452
            shelf_file.write('bar')
 
453
        finally:
 
454
            shelf_file.close()
 
455
        shelf_file = manager.read_shelf(1)
 
456
        try:
 
457
            self.assertEqual('foo', shelf_file.read())
 
458
        finally:
 
459
            shelf_file.close()
 
460
        shelf_file = manager.read_shelf(2)
 
461
        try:
 
462
            self.assertEqual('bar', shelf_file.read())
 
463
        finally:
 
464
            shelf_file.close()
 
465
 
 
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))
 
470
 
 
471
    def test_shelve_changes(self):
 
472
        tree = self.make_branch_and_tree('tree')
 
473
        tree.commit('no-change commit')
 
474
        tree.lock_write()
 
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')
 
489
 
 
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'])