~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_shelf.py

  • Committer: Martin Pool
  • Date: 2011-07-04 21:15:24 UTC
  • mto: (6034.1.1 filter-tree)
  • mto: This revision was merged to the branch mainline in revision 6035.
  • Revision ID: mbp@canonical.com-20110704211524-ilb7l6g4i3iizke7
Test exporting filtered tree to a zip (already works)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008 Canonical Ltd
 
1
# Copyright (C) 2008-2011 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
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
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
import os
18
18
 
19
 
from bzrlib import errors, pack, shelf, tests, transform
 
19
from bzrlib import (
 
20
    errors,
 
21
    osutils,
 
22
    pack,
 
23
    shelf,
 
24
    tests,
 
25
    transform,
 
26
    workingtree,
 
27
    )
20
28
 
21
29
 
22
30
EMPTY_SHELF = ("Bazaar pack format 1 (introduced in 0.18)\n"
32
40
 
33
41
class TestPrepareShelf(tests.TestCaseWithTransport):
34
42
 
35
 
    def test_shelve_rename(self):
 
43
    def prepare_shelve_rename(self):
36
44
        tree = self.make_branch_and_tree('.')
37
45
        self.build_tree(['foo'])
38
46
        tree.add(['foo'], ['foo-id'])
39
47
        tree.commit('foo')
40
48
        tree.rename_one('foo', 'bar')
 
49
        tree.lock_tree_write()
 
50
        self.addCleanup(tree.unlock)
41
51
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
42
52
        self.addCleanup(creator.finalize)
43
53
        self.assertEqual([('rename', 'foo-id', 'foo', 'bar')],
44
54
                          list(creator.iter_shelvable()))
45
 
        creator.shelve_rename('foo-id')
 
55
        return creator
 
56
 
 
57
    def check_shelve_rename(self, creator):
46
58
        work_trans_id = creator.work_transform.trans_id_file_id('foo-id')
47
59
        self.assertEqual('foo', creator.work_transform.final_name(
48
60
                         work_trans_id))
50
62
        self.assertEqual('bar', creator.shelf_transform.final_name(
51
63
                         shelf_trans_id))
52
64
 
53
 
    def test_shelve_move(self):
 
65
    def test_shelve_rename(self):
 
66
        creator = self.prepare_shelve_rename()
 
67
        creator.shelve_rename('foo-id')
 
68
        self.check_shelve_rename(creator)
 
69
 
 
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)
 
74
 
 
75
    def prepare_shelve_move(self):
54
76
        tree = self.make_branch_and_tree('.')
55
77
        self.build_tree(['foo/', 'bar/', 'foo/baz'])
56
78
        tree.add(['foo', 'bar', 'foo/baz'], ['foo-id', 'bar-id', 'baz-id'])
57
79
        tree.commit('foo')
58
80
        tree.rename_one('foo/baz', 'bar/baz')
 
81
        tree.lock_tree_write()
 
82
        self.addCleanup(tree.unlock)
59
83
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
60
84
        self.addCleanup(creator.finalize)
61
85
        self.assertEqual([('rename', 'baz-id', 'foo/baz', 'bar/baz')],
62
86
                         list(creator.iter_shelvable()))
63
 
        creator.shelve_rename('baz-id')
 
87
        return creator, tree
 
88
 
 
89
    def check_shelve_move(self, creator, tree):
64
90
        work_trans_id = creator.work_transform.trans_id_file_id('baz-id')
65
91
        work_foo = creator.work_transform.trans_id_file_id('foo-id')
66
92
        self.assertEqual(work_foo, creator.work_transform.final_parent(
72
98
        creator.transform()
73
99
        self.assertEqual('foo/baz', tree.id2path('baz-id'))
74
100
 
 
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)
 
105
 
 
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)
 
110
 
 
111
    def test_shelve_changed_root_id(self):
 
112
        tree = self.make_branch_and_tree('.')
 
113
        self.build_tree(['foo'])
 
114
        tree.set_root_id('first-root-id')
 
115
        tree.add(['foo'], ['foo-id'])
 
116
        tree.commit('foo')
 
117
        tree.set_root_id('second-root-id')
 
118
        tree.lock_tree_write()
 
119
        self.addCleanup(tree.unlock)
 
120
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
 
121
        self.addCleanup(creator.finalize)
 
122
        self.expectFailure('shelf doesn\'t support shelving root changes yet',
 
123
            self.assertEqual, [
 
124
                ('delete file', 'first-root-id', 'directory', ''),
 
125
                ('add file', 'second-root-id', 'directory', ''),
 
126
                ('rename', 'foo-id', u'foo', u'foo'),
 
127
                ], list(creator.iter_shelvable()))
 
128
 
 
129
        self.assertEqual([('delete file', 'first-root-id', 'directory', ''),
 
130
                          ('add file', 'second-root-id', 'directory', ''),
 
131
                          ('rename', 'foo-id', u'foo', u'foo'),
 
132
                         ], list(creator.iter_shelvable()))
 
133
 
75
134
    def assertShelvedFileEqual(self, expected_content, creator, file_id):
76
135
        s_trans_id = creator.shelf_transform.trans_id_file_id(file_id)
77
136
        shelf_file = creator.shelf_transform._limbo_name(s_trans_id)
78
137
        self.assertFileEqual(expected_content, shelf_file)
79
138
 
80
 
    def test_shelve_content_change(self):
 
139
    def prepare_content_change(self):
81
140
        tree = self.make_branch_and_tree('.')
82
141
        tree.lock_write()
83
142
        self.addCleanup(tree.unlock)
87
146
        self.build_tree_contents([('foo', 'b\na\nc\n')])
88
147
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
89
148
        self.addCleanup(creator.finalize)
 
149
        return creator
 
150
 
 
151
    def test_shelve_content_change(self):
 
152
        creator = self.prepare_content_change()
90
153
        self.assertEqual([('modify text', 'foo-id')],
91
154
                         list(creator.iter_shelvable()))
92
155
        creator.shelve_lines('foo-id', ['a\n', 'c\n'])
94
157
        self.assertFileEqual('a\nc\n', 'foo')
95
158
        self.assertShelvedFileEqual('b\na\n', creator, 'foo-id')
96
159
 
97
 
    def test_shelve_creation(self):
 
160
    def test_shelve_change_handles_modify_text(self):
 
161
        creator = self.prepare_content_change()
 
162
        creator.shelve_change(('modify text', 'foo-id'))
 
163
        creator.transform()
 
164
        self.assertFileEqual('a\n', 'foo')
 
165
        self.assertShelvedFileEqual('b\na\nc\n', creator, 'foo-id')
 
166
 
 
167
    def test_shelve_all(self):
 
168
        creator = self.prepare_content_change()
 
169
        creator.shelve_all()
 
170
        creator.transform()
 
171
        self.assertFileEqual('a\n', 'foo')
 
172
        self.assertShelvedFileEqual('b\na\nc\n', creator, 'foo-id')
 
173
 
 
174
    def prepare_shelve_creation(self):
98
175
        tree = self.make_branch_and_tree('.')
99
176
        tree.lock_write()
100
177
        self.addCleanup(tree.unlock)
106
183
        self.assertEqual([('add file', 'bar-id', 'directory', 'bar'),
107
184
                          ('add file', 'foo-id', 'file', 'foo')],
108
185
                          sorted(list(creator.iter_shelvable())))
109
 
        creator.shelve_creation('foo-id')
110
 
        creator.shelve_creation('bar-id')
111
 
        creator.transform()
 
186
        return creator, tree
 
187
 
 
188
    def check_shelve_creation(self, creator, tree):
112
189
        self.assertRaises(StopIteration,
113
190
                          tree.iter_entries_by_dir(['foo-id']).next)
114
191
        s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
115
192
        self.assertEqual('foo-id',
116
193
                         creator.shelf_transform.final_file_id(s_trans_id))
117
 
        self.failIfExists('foo')
118
 
        self.failIfExists('bar')
 
194
        self.assertPathDoesNotExist('foo')
 
195
        self.assertPathDoesNotExist('bar')
119
196
        self.assertShelvedFileEqual('a\n', creator, 'foo-id')
120
197
        s_bar_trans_id = creator.shelf_transform.trans_id_file_id('bar-id')
121
198
        self.assertEqual('directory',
122
199
            creator.shelf_transform.final_kind(s_bar_trans_id))
123
200
 
 
201
    def test_shelve_creation(self):
 
202
        creator, tree = self.prepare_shelve_creation()
 
203
        creator.shelve_creation('foo-id')
 
204
        creator.shelve_creation('bar-id')
 
205
        creator.transform()
 
206
        self.check_shelve_creation(creator, tree)
 
207
 
 
208
    def test_shelve_change_handles_creation(self):
 
209
        creator, tree = self.prepare_shelve_creation()
 
210
        creator.shelve_change(('add file', 'foo-id', 'file', 'foo'))
 
211
        creator.shelve_change(('add file', 'bar-id', 'directory', 'bar'))
 
212
        creator.transform()
 
213
        self.check_shelve_creation(creator, tree)
 
214
 
 
215
    def _test_shelve_symlink_creation(self, link_name, link_target,
 
216
                                      shelve_change=False):
 
217
        self.requireFeature(tests.SymlinkFeature)
 
218
        tree = self.make_branch_and_tree('.')
 
219
        tree.lock_write()
 
220
        self.addCleanup(tree.unlock)
 
221
        tree.commit('Empty tree')
 
222
        os.symlink(link_target, link_name)
 
223
        tree.add(link_name, 'foo-id')
 
224
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
 
225
        self.addCleanup(creator.finalize)
 
226
        self.assertEqual([('add file', 'foo-id', 'symlink', link_name)],
 
227
                         list(creator.iter_shelvable()))
 
228
        if shelve_change:
 
229
            creator.shelve_change(('add file', 'foo-id', 'symlink', link_name))
 
230
        else:
 
231
            creator.shelve_creation('foo-id')
 
232
        creator.transform()
 
233
        s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
 
234
        self.assertPathDoesNotExist(link_name)
 
235
        limbo_name = creator.shelf_transform._limbo_name(s_trans_id)
 
236
        self.assertEqual(link_target, osutils.readlink(limbo_name))
 
237
        ptree = creator.shelf_transform.get_preview_tree()
 
238
        self.assertEqual(link_target, ptree.get_symlink_target('foo-id'))
 
239
 
124
240
    def test_shelve_symlink_creation(self):
 
241
        self._test_shelve_symlink_creation('foo', 'bar')
 
242
 
 
243
    def test_shelve_unicode_symlink_creation(self):
 
244
        self.requireFeature(tests.UnicodeFilenameFeature)
 
245
        self._test_shelve_symlink_creation(u'fo\N{Euro Sign}o',
 
246
                                           u'b\N{Euro Sign}ar')
 
247
 
 
248
    def test_shelve_change_handles_symlink_creation(self):
 
249
        self._test_shelve_symlink_creation('foo', 'bar', shelve_change=True)
 
250
 
 
251
    def _test_shelve_symlink_target_change(self, link_name,
 
252
                                           old_target, new_target,
 
253
                                           shelve_change=False):
125
254
        self.requireFeature(tests.SymlinkFeature)
126
255
        tree = self.make_branch_and_tree('.')
127
256
        tree.lock_write()
128
257
        self.addCleanup(tree.unlock)
129
 
        tree.commit('Empty tree')
130
 
        os.symlink('bar', 'foo')
131
 
        tree.add('foo', 'foo-id')
 
258
        os.symlink(old_target, link_name)
 
259
        tree.add(link_name, 'foo-id')
 
260
        tree.commit("commit symlink")
 
261
        os.unlink(link_name)
 
262
        os.symlink(new_target, link_name)
132
263
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
133
264
        self.addCleanup(creator.finalize)
134
 
        self.assertEqual([('add file', 'foo-id', 'symlink', 'foo')],
 
265
        self.assertEqual([('modify target', 'foo-id', link_name,
 
266
                           old_target, new_target)],
135
267
                         list(creator.iter_shelvable()))
136
 
        creator.shelve_creation('foo-id')
 
268
        if shelve_change:
 
269
            creator.shelve_change(('modify target', 'foo-id', link_name,
 
270
                                   old_target, new_target))
 
271
        else:
 
272
            creator.shelve_modify_target('foo-id')
137
273
        creator.transform()
 
274
        self.assertEqual(old_target, osutils.readlink(link_name))
138
275
        s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
139
 
        self.failIfExists('foo')
140
276
        limbo_name = creator.shelf_transform._limbo_name(s_trans_id)
141
 
        self.assertEqual('bar', os.readlink(limbo_name))
 
277
        self.assertEqual(new_target, osutils.readlink(limbo_name))
 
278
        ptree = creator.shelf_transform.get_preview_tree()
 
279
        self.assertEqual(new_target, ptree.get_symlink_target('foo-id'))
 
280
 
 
281
    def test_shelve_symlink_target_change(self):
 
282
        self._test_shelve_symlink_target_change('foo', 'bar', 'baz')
 
283
 
 
284
    def test_shelve_unicode_symlink_target_change(self):
 
285
        self.requireFeature(tests.UnicodeFilenameFeature)
 
286
        self._test_shelve_symlink_target_change(
 
287
            u'fo\N{Euro Sign}o', u'b\N{Euro Sign}ar', u'b\N{Euro Sign}az')
 
288
 
 
289
    def test_shelve_change_handles_symlink_target_change(self):
 
290
        self._test_shelve_symlink_target_change('foo', 'bar', 'baz',
 
291
                                                shelve_change=True)
142
292
 
143
293
    def test_shelve_creation_no_contents(self):
144
294
        tree = self.make_branch_and_tree('.')
160
310
        s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
161
311
        self.assertEqual('foo-id',
162
312
                         creator.shelf_transform.final_file_id(s_trans_id))
163
 
        self.failIfExists('foo')
 
313
        self.assertPathDoesNotExist('foo')
164
314
 
165
 
    def test_shelve_deletion(self):
 
315
    def prepare_shelve_deletion(self):
166
316
        tree = self.make_branch_and_tree('tree')
167
317
        tree.lock_write()
168
318
        self.addCleanup(tree.unlock)
177
327
        self.assertEqual([('delete file', 'bar-id', 'file', 'foo/bar'),
178
328
                          ('delete file', 'foo-id', 'directory', 'foo')],
179
329
                          sorted(list(creator.iter_shelvable())))
 
330
        return creator, tree
 
331
 
 
332
    def check_shelve_deletion(self, tree):
 
333
        self.assertTrue(tree.has_id('foo-id'))
 
334
        self.assertTrue(tree.has_id('bar-id'))
 
335
        self.assertFileEqual('baz', 'tree/foo/bar')
 
336
 
 
337
    def test_shelve_deletion(self):
 
338
        creator, tree = self.prepare_shelve_deletion()
180
339
        creator.shelve_deletion('foo-id')
181
340
        creator.shelve_deletion('bar-id')
182
341
        creator.transform()
183
 
        self.assertTrue('foo-id' in tree)
184
 
        self.assertTrue('bar-id' in tree)
185
 
        self.assertFileEqual('baz', 'tree/foo/bar')
 
342
        self.check_shelve_deletion(tree)
 
343
 
 
344
    def test_shelve_change_handles_deletion(self):
 
345
        creator, tree = self.prepare_shelve_deletion()
 
346
        creator.shelve_change(('delete file', 'foo-id', 'directory', 'foo'))
 
347
        creator.shelve_change(('delete file', 'bar-id', 'file', 'foo/bar'))
 
348
        creator.transform()
 
349
        self.check_shelve_deletion(tree)
186
350
 
187
351
    def test_shelve_delete_contents(self):
188
352
        tree = self.make_branch_and_tree('tree')
190
354
        tree.add('foo', 'foo-id')
191
355
        tree.commit('Added file and directory')
192
356
        os.unlink('tree/foo')
 
357
        tree.lock_tree_write()
 
358
        self.addCleanup(tree.unlock)
193
359
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
194
360
        self.addCleanup(creator.finalize)
195
361
        self.assertEqual([('delete file', 'foo-id', 'file', 'foo')],
196
362
                         sorted(list(creator.iter_shelvable())))
197
363
        creator.shelve_deletion('foo-id')
198
364
        creator.transform()
199
 
        self.failUnlessExists('tree/foo')
 
365
        self.assertPathExists('tree/foo')
200
366
 
201
 
    def test_shelve_change_kind(self):
 
367
    def prepare_shelve_change_kind(self):
202
368
        tree = self.make_branch_and_tree('tree')
203
369
        self.build_tree_contents([('tree/foo', 'bar')])
204
370
        tree.add('foo', 'foo-id')
205
371
        tree.commit('Added file and directory')
206
372
        os.unlink('tree/foo')
207
373
        os.mkdir('tree/foo')
 
374
        tree.lock_tree_write()
 
375
        self.addCleanup(tree.unlock)
208
376
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
209
377
        self.addCleanup(creator.finalize)
210
378
        self.assertEqual([('change kind', 'foo-id', 'file', 'directory',
211
379
                           'foo')], sorted(list(creator.iter_shelvable())))
 
380
        return creator
 
381
 
 
382
    def check_shelve_change_kind(self, creator):
 
383
        self.assertFileEqual('bar', 'tree/foo')
 
384
        s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
 
385
        self.assertEqual('directory',
 
386
                         creator.shelf_transform._new_contents[s_trans_id])
 
387
 
 
388
    def test_shelve_change_kind(self):
 
389
        creator = self.prepare_shelve_change_kind()
212
390
        creator.shelve_content_change('foo-id')
213
391
        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])
 
392
        self.check_shelve_change_kind(creator)
 
393
 
 
394
    def test_shelve_change_handles_change_kind(self):
 
395
        creator = self.prepare_shelve_change_kind()
 
396
        creator.shelve_change(('change kind', 'foo-id', 'file', 'directory',
 
397
                               'foo'))
 
398
        creator.transform()
 
399
        self.check_shelve_change_kind(creator)
 
400
 
 
401
    def test_shelve_change_unknown_change(self):
 
402
        tree = self.make_branch_and_tree('tree')
 
403
        tree.lock_tree_write()
 
404
        self.addCleanup(tree.unlock)
 
405
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
 
406
        self.addCleanup(creator.finalize)
 
407
        e = self.assertRaises(ValueError, creator.shelve_change, ('unknown',))
 
408
        self.assertEqual('Unknown change kind: "unknown"', str(e))
218
409
 
219
410
    def test_shelve_unversion(self):
220
411
        tree = self.make_branch_and_tree('tree')
222
413
        tree.add('foo', 'foo-id')
223
414
        tree.commit('Added file and directory')
224
415
        tree.unversion(['foo-id'])
 
416
        tree.lock_tree_write()
 
417
        self.addCleanup(tree.unlock)
225
418
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
226
419
        self.addCleanup(creator.finalize)
227
420
        self.assertEqual([('delete file', 'foo-id', 'file', 'foo')],
228
421
                         sorted(list(creator.iter_shelvable())))
229
422
        creator.shelve_deletion('foo-id')
230
423
        creator.transform()
231
 
        self.failUnlessExists('tree/foo')
 
424
        self.assertPathExists('tree/foo')
232
425
 
233
426
    def test_shelve_serialization(self):
234
427
        tree = self.make_branch_and_tree('.')
 
428
        tree.lock_tree_write()
 
429
        self.addCleanup(tree.unlock)
235
430
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
236
431
        self.addCleanup(creator.finalize)
237
432
        shelf_file = open('shelf', 'wb')
246
441
        tree = self.make_branch_and_tree('tree')
247
442
        self.build_tree(['tree/foo'])
248
443
        tree.add('foo', 'foo-id')
 
444
        tree.lock_tree_write()
 
445
        self.addCleanup(tree.unlock)
249
446
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
250
447
        self.addCleanup(creator.finalize)
251
448
        list(creator.iter_shelvable())
268
465
        records.next()
269
466
        tt.deserialize(records)
270
467
 
 
468
    def test_shelve_unversioned(self):
 
469
        tree = self.make_branch_and_tree('tree')
 
470
        tree.lock_tree_write()
 
471
        try:
 
472
            self.assertRaises(errors.PathsNotVersionedError,
 
473
                              shelf.ShelfCreator, tree, tree.basis_tree(), ['foo'])
 
474
        finally:
 
475
            tree.unlock()
 
476
        # We should be able to lock/unlock the tree if ShelfCreator cleaned
 
477
        # after itself.
 
478
        wt = workingtree.WorkingTree.open('tree')
 
479
        wt.lock_tree_write()
 
480
        wt.unlock()
 
481
        # And a second tentative should raise the same error (no
 
482
        # limbo/pending_deletion leftovers).
 
483
        tree.lock_tree_write()
 
484
        try:
 
485
            self.assertRaises(errors.PathsNotVersionedError,
 
486
                              shelf.ShelfCreator, tree, tree.basis_tree(), ['foo'])
 
487
        finally:
 
488
            tree.unlock()
 
489
 
 
490
    def test_shelve_skips_added_root(self):
 
491
        """Skip adds of the root when iterating through shelvable changes."""
 
492
        tree = self.make_branch_and_tree('tree')
 
493
        tree.lock_tree_write()
 
494
        self.addCleanup(tree.unlock)
 
495
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
 
496
        self.addCleanup(creator.finalize)
 
497
        self.assertEqual([], list(creator.iter_shelvable()))
 
498
 
 
499
    def test_shelve_skips_added_root(self):
 
500
        """Skip adds of the root when iterating through shelvable changes."""
 
501
        tree = self.make_branch_and_tree('tree')
 
502
        tree.lock_tree_write()
 
503
        self.addCleanup(tree.unlock)
 
504
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
 
505
        self.addCleanup(creator.finalize)
 
506
        self.assertEqual([], list(creator.iter_shelvable()))
 
507
 
271
508
 
272
509
class TestUnshelver(tests.TestCaseWithTransport):
273
510
 
289
526
            shelf_file.seek(0)
290
527
            unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
291
528
            unshelver.make_merger().do_merge()
 
529
            self.addCleanup(unshelver.finalize)
292
530
            self.assertFileEqual('bar', 'tree/foo')
293
531
        finally:
294
532
            shelf_file.close()
312
550
        self.build_tree_contents([('tree/foo', 'z\na\nb\nc\n')])
313
551
        shelf_file.seek(0)
314
552
        unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
 
553
        self.addCleanup(unshelver.finalize)
315
554
        unshelver.make_merger().do_merge()
316
555
        self.assertFileEqual('z\na\nb\nd\n', 'tree/foo')
317
556
 
329
568
        list(creator.iter_shelvable())
330
569
        creator.shelve_deletion('foo-id')
331
570
        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()
 
571
        with open('shelf', 'w+b') as shelf_file:
 
572
            creator.write_shelf(shelf_file)
 
573
            creator.transform()
 
574
            creator.finalize()
337
575
        # validate the test setup
338
 
        self.assertTrue('foo-id' in tree)
339
 
        self.assertTrue('bar-id' in tree)
 
576
        self.assertTrue(tree.has_id('foo-id'))
 
577
        self.assertTrue(tree.has_id('bar-id'))
340
578
        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)
 
579
        with open('shelf', 'r+b') as shelf_file:
 
580
            unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
 
581
            self.addCleanup(unshelver.finalize)
 
582
            unshelver.make_merger().do_merge()
 
583
        self.assertFalse(tree.has_id('foo-id'))
 
584
        self.assertFalse(tree.has_id('bar-id'))
346
585
 
347
586
    def test_unshelve_base(self):
348
587
        tree = self.make_branch_and_tree('tree')
370
609
        shelf_file = open('shelf', 'rb')
371
610
        self.addCleanup(shelf_file.close)
372
611
        unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
 
612
        unshelver.finalize()
373
613
 
374
614
    def test_corrupt_shelf(self):
375
615
        tree = self.make_branch_and_tree('.')
382
622
                              shelf_file)
383
623
        self.assertEqual('Shelf corrupt.', str(e))
384
624
 
 
625
    def test_unshelve_subdir_in_now_removed_dir(self):
 
626
        tree = self.make_branch_and_tree('.')
 
627
        self.addCleanup(tree.lock_write().unlock)
 
628
        self.build_tree(['dir/', 'dir/subdir/', 'dir/subdir/foo'])
 
629
        tree.add(['dir'], ['dir-id'])
 
630
        tree.commit('versioned dir')
 
631
        tree.add(['dir/subdir', 'dir/subdir/foo'], ['subdir-id', 'foo-id'])
 
632
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
 
633
        self.addCleanup(creator.finalize)
 
634
        for change in creator.iter_shelvable():
 
635
            creator.shelve_change(change)
 
636
        shelf_manager = tree.get_shelf_manager()
 
637
        shelf_id = shelf_manager.shelve_changes(creator)
 
638
        self.assertPathDoesNotExist('dir/subdir')
 
639
        tree.remove(['dir'])
 
640
        unshelver = shelf_manager.get_unshelver(shelf_id)
 
641
        self.addCleanup(unshelver.finalize)
 
642
        unshelver.make_merger().do_merge()
 
643
        self.assertPathExists('dir/subdir/foo')
 
644
        self.assertEqual('dir-id', tree.path2id('dir'))
 
645
        self.assertEqual('subdir-id', tree.path2id('dir/subdir'))
 
646
        self.assertEqual('foo-id', tree.path2id('dir/subdir/foo'))
 
647
 
385
648
 
386
649
class TestShelfManager(tests.TestCaseWithTransport):
387
650
 
482
745
        creator.shelve_creation('foo-id')
483
746
        shelf_manager = tree.get_shelf_manager()
484
747
        shelf_id = shelf_manager.shelve_changes(creator)
485
 
        self.failIfExists('tree/foo')
 
748
        self.assertPathDoesNotExist('tree/foo')
486
749
        unshelver = shelf_manager.get_unshelver(shelf_id)
 
750
        self.addCleanup(unshelver.finalize)
487
751
        unshelver.make_merger().do_merge()
488
752
        self.assertFileEqual('bar', 'tree/foo')
489
753
 
490
754
    def test_get_metadata(self):
491
755
        tree = self.make_branch_and_tree('.')
 
756
        tree.lock_tree_write()
 
757
        self.addCleanup(tree.unlock)
492
758
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
 
759
        self.addCleanup(creator.finalize)
493
760
        shelf_manager = tree.get_shelf_manager()
494
761
        shelf_id = shelf_manager.shelve_changes(creator, 'foo')
495
762
        metadata = shelf_manager.get_metadata(shelf_id)