~bzr-pqm/bzr/bzr.dev

5599.1.1 by John Arbash Meinel
Move away from using Tree.inventory[] just to check if this is a root.
1
# Copyright (C) 2008-2011 Canonical Ltd
0.12.12 by Aaron Bentley
Implement shelf creator
2
#
0.12.80 by Aaron Bentley
Re-format GPL notifications
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0.12.12 by Aaron Bentley
Implement shelf creator
16
0.12.17 by Aaron Bentley
Handle creating symlinks
17
import os
18
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
19
from bzrlib import (
20
    errors,
21
    osutils,
22
    pack,
23
    shelf,
24
    tests,
25
    transform,
26
    workingtree,
27
    )
0.12.12 by Aaron Bentley
Implement shelf creator
28
29
0.14.34 by Aaron Bentley
Factor out the empty shelf
30
EMPTY_SHELF = ("Bazaar pack format 1 (introduced in 0.18)\n"
0.15.39 by Aaron Bentley
Update empty shelf serialization
31
               "B23\n"
32
               "metadata\n\n"
33
               "d11:revision_id5:null:e"
0.14.34 by Aaron Bentley
Factor out the empty shelf
34
               "B159\n"
35
               "attribs\n\n"
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")
39
40
0.12.12 by Aaron Bentley
Implement shelf creator
41
class TestPrepareShelf(tests.TestCaseWithTransport):
42
4526.7.3 by Aaron Bentley
Test shelve_change.
43
    def prepare_shelve_rename(self):
0.12.12 by Aaron Bentley
Implement shelf creator
44
        tree = self.make_branch_and_tree('.')
45
        self.build_tree(['foo'])
46
        tree.add(['foo'], ['foo-id'])
47
        tree.commit('foo')
48
        tree.rename_one('foo', 'bar')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
49
        tree.lock_tree_write()
50
        self.addCleanup(tree.unlock)
0.14.7 by Aaron Bentley
Misc test cleanup
51
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
0.12.12 by Aaron Bentley
Implement shelf creator
52
        self.addCleanup(creator.finalize)
0.14.32 by Aaron Bentley
Replace ShelfCreator.__iter__ with ShelfCreator.iter_shelvable
53
        self.assertEqual([('rename', 'foo-id', 'foo', 'bar')],
54
                          list(creator.iter_shelvable()))
4526.7.3 by Aaron Bentley
Test shelve_change.
55
        return creator
56
57
    def check_shelve_rename(self, creator):
0.12.12 by Aaron Bentley
Implement shelf creator
58
        work_trans_id = creator.work_transform.trans_id_file_id('foo-id')
59
        self.assertEqual('foo', creator.work_transform.final_name(
60
                         work_trans_id))
61
        shelf_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
62
        self.assertEqual('bar', creator.shelf_transform.final_name(
63
                         shelf_trans_id))
64
4526.7.3 by Aaron Bentley
Test shelve_change.
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):
0.12.12 by Aaron Bentley
Implement shelf creator
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'])
79
        tree.commit('foo')
80
        tree.rename_one('foo/baz', 'bar/baz')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
81
        tree.lock_tree_write()
82
        self.addCleanup(tree.unlock)
0.14.7 by Aaron Bentley
Misc test cleanup
83
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
0.12.12 by Aaron Bentley
Implement shelf creator
84
        self.addCleanup(creator.finalize)
85
        self.assertEqual([('rename', 'baz-id', 'foo/baz', 'bar/baz')],
0.14.32 by Aaron Bentley
Replace ShelfCreator.__iter__ with ShelfCreator.iter_shelvable
86
                         list(creator.iter_shelvable()))
4526.7.3 by Aaron Bentley
Test shelve_change.
87
        return creator, tree
88
89
    def check_shelve_move(self, creator, tree):
0.12.12 by Aaron Bentley
Implement shelf creator
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(
93
                         work_trans_id))
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(
97
                         shelf_trans_id))
0.12.13 by Aaron Bentley
Implement shelving content
98
        creator.transform()
99
        self.assertEqual('foo/baz', tree.id2path('baz-id'))
100
4526.7.3 by Aaron Bentley
Test shelve_change.
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
4634.123.6 by John Arbash Meinel
Add a direct ShelfCreator test for changing root id.
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
0.12.14 by Aaron Bentley
Add shelving of created files
134
    def assertShelvedFileEqual(self, expected_content, creator, file_id):
135
        s_trans_id = creator.shelf_transform.trans_id_file_id(file_id)
136
        shelf_file = creator.shelf_transform._limbo_name(s_trans_id)
137
        self.assertFileEqual(expected_content, shelf_file)
138
4595.8.1 by Aaron Bentley
shelve_change handles text modification.
139
    def prepare_content_change(self):
0.12.13 by Aaron Bentley
Implement shelving content
140
        tree = self.make_branch_and_tree('.')
141
        tree.lock_write()
142
        self.addCleanup(tree.unlock)
143
        self.build_tree_contents([('foo', 'a\n')])
144
        tree.add('foo', 'foo-id')
145
        tree.commit('Committed foo')
146
        self.build_tree_contents([('foo', 'b\na\nc\n')])
0.14.7 by Aaron Bentley
Misc test cleanup
147
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
0.12.13 by Aaron Bentley
Implement shelving content
148
        self.addCleanup(creator.finalize)
4595.8.2 by Aaron Bentley
Implement shelve_all
149
        return creator
150
151
    def test_shelve_content_change(self):
152
        creator = self.prepare_content_change()
0.14.32 by Aaron Bentley
Replace ShelfCreator.__iter__ with ShelfCreator.iter_shelvable
153
        self.assertEqual([('modify text', 'foo-id')],
154
                         list(creator.iter_shelvable()))
0.14.14 by Aaron Bentley
Change shelf_text to shelve_lines
155
        creator.shelve_lines('foo-id', ['a\n', 'c\n'])
0.12.13 by Aaron Bentley
Implement shelving content
156
        creator.transform()
157
        self.assertFileEqual('a\nc\n', 'foo')
0.12.14 by Aaron Bentley
Add shelving of created files
158
        self.assertShelvedFileEqual('b\na\n', creator, 'foo-id')
159
4595.8.1 by Aaron Bentley
shelve_change handles text modification.
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')
4526.7.3 by Aaron Bentley
Test shelve_change.
166
4595.8.2 by Aaron Bentley
Implement shelve_all
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
4526.7.3 by Aaron Bentley
Test shelve_change.
174
    def prepare_shelve_creation(self):
0.12.14 by Aaron Bentley
Add shelving of created files
175
        tree = self.make_branch_and_tree('.')
176
        tree.lock_write()
177
        self.addCleanup(tree.unlock)
178
        tree.commit('Empty tree')
0.12.16 by Aaron Bentley
Handle shelving directory creation
179
        self.build_tree_contents([('foo', 'a\n'), ('bar/',)])
180
        tree.add(['foo', 'bar'], ['foo-id', 'bar-id'])
0.14.7 by Aaron Bentley
Misc test cleanup
181
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
0.12.14 by Aaron Bentley
Add shelving of created files
182
        self.addCleanup(creator.finalize)
0.14.13 by Aaron Bentley
Provide path and kind when deletes/adds are detected.
183
        self.assertEqual([('add file', 'bar-id', 'directory', 'bar'),
184
                          ('add file', 'foo-id', 'file', 'foo')],
0.14.32 by Aaron Bentley
Replace ShelfCreator.__iter__ with ShelfCreator.iter_shelvable
185
                          sorted(list(creator.iter_shelvable())))
4526.7.3 by Aaron Bentley
Test shelve_change.
186
        return creator, tree
187
188
    def check_shelve_creation(self, creator, tree):
0.12.15 by Aaron Bentley
Handle file-id when shelving creation
189
        self.assertRaises(StopIteration,
190
                          tree.iter_entries_by_dir(['foo-id']).next)
191
        s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
192
        self.assertEqual('foo-id',
193
                         creator.shelf_transform.final_file_id(s_trans_id))
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
194
        self.assertPathDoesNotExist('foo')
195
        self.assertPathDoesNotExist('bar')
0.12.14 by Aaron Bentley
Add shelving of created files
196
        self.assertShelvedFileEqual('a\n', creator, 'foo-id')
0.12.16 by Aaron Bentley
Handle shelving directory creation
197
        s_bar_trans_id = creator.shelf_transform.trans_id_file_id('bar-id')
198
        self.assertEqual('directory',
199
            creator.shelf_transform.final_kind(s_bar_trans_id))
0.12.17 by Aaron Bentley
Handle creating symlinks
200
4526.7.3 by Aaron Bentley
Test shelve_change.
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):
4241.14.16 by Vincent Ladeuil
Fix _PreviewTree.get_symlink_target for unicode symlinks.
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()))
4526.7.3 by Aaron Bentley
Test shelve_change.
228
        if shelve_change:
229
            creator.shelve_change(('add file', 'foo-id', 'symlink', link_name))
230
        else:
231
            creator.shelve_creation('foo-id')
4241.14.16 by Vincent Ladeuil
Fix _PreviewTree.get_symlink_target for unicode symlinks.
232
        creator.transform()
233
        s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
234
        self.assertPathDoesNotExist(link_name)
4241.14.16 by Vincent Ladeuil
Fix _PreviewTree.get_symlink_target for unicode symlinks.
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
0.12.17 by Aaron Bentley
Handle creating symlinks
240
    def test_shelve_symlink_creation(self):
4241.14.16 by Vincent Ladeuil
Fix _PreviewTree.get_symlink_target for unicode symlinks.
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')
0.12.19 by Aaron Bentley
Add support for writing shelves
247
4526.7.3 by Aaron Bentley
Test shelve_change.
248
    def test_shelve_change_handles_symlink_creation(self):
249
        self._test_shelve_symlink_creation('foo', 'bar', shelve_change=True)
250
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
251
    def _test_shelve_symlink_target_change(self, link_name,
4526.7.3 by Aaron Bentley
Test shelve_change.
252
                                           old_target, new_target,
253
                                           shelve_change=False):
4119.5.1 by James Westby
Shelve can now shelve changes to a symlink's target.
254
        self.requireFeature(tests.SymlinkFeature)
255
        tree = self.make_branch_and_tree('.')
256
        tree.lock_write()
257
        self.addCleanup(tree.unlock)
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
258
        os.symlink(old_target, link_name)
259
        tree.add(link_name, 'foo-id')
4119.5.1 by James Westby
Shelve can now shelve changes to a symlink's target.
260
        tree.commit("commit symlink")
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
261
        os.unlink(link_name)
262
        os.symlink(new_target, link_name)
4119.5.1 by James Westby
Shelve can now shelve changes to a symlink's target.
263
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
264
        self.addCleanup(creator.finalize)
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
265
        self.assertEqual([('modify target', 'foo-id', link_name,
266
                           old_target, new_target)],
4119.5.1 by James Westby
Shelve can now shelve changes to a symlink's target.
267
                         list(creator.iter_shelvable()))
4526.7.3 by Aaron Bentley
Test shelve_change.
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')
4119.5.1 by James Westby
Shelve can now shelve changes to a symlink's target.
273
        creator.transform()
4241.14.21 by Vincent Ladeuil
More cleanup.
274
        self.assertEqual(old_target, osutils.readlink(link_name))
4119.5.1 by James Westby
Shelve can now shelve changes to a symlink's target.
275
        s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
276
        limbo_name = creator.shelf_transform._limbo_name(s_trans_id)
4241.14.21 by Vincent Ladeuil
More cleanup.
277
        self.assertEqual(new_target, osutils.readlink(limbo_name))
4241.14.16 by Vincent Ladeuil
Fix _PreviewTree.get_symlink_target for unicode symlinks.
278
        ptree = creator.shelf_transform.get_preview_tree()
279
        self.assertEqual(new_target, ptree.get_symlink_target('foo-id'))
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
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')
4119.5.1 by James Westby
Shelve can now shelve changes to a symlink's target.
288
4526.7.3 by Aaron Bentley
Test shelve_change.
289
    def test_shelve_change_handles_symlink_target_change(self):
290
        self._test_shelve_symlink_target_change('foo', 'bar', 'baz',
291
                                                shelve_change=True)
292
0.14.12 by Aaron Bentley
Handle new dangling ids
293
    def test_shelve_creation_no_contents(self):
294
        tree = self.make_branch_and_tree('.')
295
        tree.lock_write()
296
        self.addCleanup(tree.unlock)
297
        tree.commit('Empty tree')
298
        self.build_tree(['foo'])
299
        tree.add('foo', 'foo-id')
300
        os.unlink('foo')
301
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
302
        self.addCleanup(creator.finalize)
0.14.13 by Aaron Bentley
Provide path and kind when deletes/adds are detected.
303
        self.assertEqual([('add file', 'foo-id', None, 'foo')],
0.14.32 by Aaron Bentley
Replace ShelfCreator.__iter__ with ShelfCreator.iter_shelvable
304
                         sorted(list(creator.iter_shelvable())))
0.14.12 by Aaron Bentley
Handle new dangling ids
305
        creator.shelve_creation('foo-id')
306
        creator.transform()
307
        self.assertRaises(StopIteration,
308
                          tree.iter_entries_by_dir(['foo-id']).next)
309
        self.assertShelvedFileEqual('', creator, 'foo-id')
310
        s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
311
        self.assertEqual('foo-id',
312
                         creator.shelf_transform.final_file_id(s_trans_id))
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
313
        self.assertPathDoesNotExist('foo')
0.14.12 by Aaron Bentley
Handle new dangling ids
314
4526.7.3 by Aaron Bentley
Test shelve_change.
315
    def prepare_shelve_deletion(self):
0.14.4 by Aaron Bentley
Implement shelving deletion
316
        tree = self.make_branch_and_tree('tree')
0.14.11 by Aaron Bentley
Fix re-versioning
317
        tree.lock_write()
318
        self.addCleanup(tree.unlock)
0.14.4 by Aaron Bentley
Implement shelving deletion
319
        self.build_tree_contents([('tree/foo/',), ('tree/foo/bar', 'baz')])
320
        tree.add(['foo', 'foo/bar'], ['foo-id', 'bar-id'])
321
        tree.commit('Added file and directory')
0.14.9 by Aaron Bentley
Shelve deleted files properly
322
        tree.unversion(['foo-id', 'bar-id'])
0.14.4 by Aaron Bentley
Implement shelving deletion
323
        os.unlink('tree/foo/bar')
324
        os.rmdir('tree/foo')
0.14.7 by Aaron Bentley
Misc test cleanup
325
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
0.14.4 by Aaron Bentley
Implement shelving deletion
326
        self.addCleanup(creator.finalize)
0.14.13 by Aaron Bentley
Provide path and kind when deletes/adds are detected.
327
        self.assertEqual([('delete file', 'bar-id', 'file', 'foo/bar'),
328
                          ('delete file', 'foo-id', 'directory', 'foo')],
0.14.32 by Aaron Bentley
Replace ShelfCreator.__iter__ with ShelfCreator.iter_shelvable
329
                          sorted(list(creator.iter_shelvable())))
4526.7.3 by Aaron Bentley
Test shelve_change.
330
        return creator, tree
331
332
    def check_shelve_deletion(self, tree):
0.14.11 by Aaron Bentley
Fix re-versioning
333
        self.assertTrue('foo-id' in tree)
334
        self.assertTrue('bar-id' in tree)
0.14.4 by Aaron Bentley
Implement shelving deletion
335
        self.assertFileEqual('baz', 'tree/foo/bar')
336
4526.7.3 by Aaron Bentley
Test shelve_change.
337
    def test_shelve_deletion(self):
338
        creator, tree = self.prepare_shelve_deletion()
339
        creator.shelve_deletion('foo-id')
340
        creator.shelve_deletion('bar-id')
341
        creator.transform()
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)
350
0.14.10 by Aaron Bentley
Fix behavior with deletions, unversioning, ...
351
    def test_shelve_delete_contents(self):
352
        tree = self.make_branch_and_tree('tree')
353
        self.build_tree(['tree/foo',])
354
        tree.add('foo', 'foo-id')
355
        tree.commit('Added file and directory')
356
        os.unlink('tree/foo')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
357
        tree.lock_tree_write()
358
        self.addCleanup(tree.unlock)
0.14.10 by Aaron Bentley
Fix behavior with deletions, unversioning, ...
359
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
360
        self.addCleanup(creator.finalize)
0.14.13 by Aaron Bentley
Provide path and kind when deletes/adds are detected.
361
        self.assertEqual([('delete file', 'foo-id', 'file', 'foo')],
0.14.32 by Aaron Bentley
Replace ShelfCreator.__iter__ with ShelfCreator.iter_shelvable
362
                         sorted(list(creator.iter_shelvable())))
0.14.10 by Aaron Bentley
Fix behavior with deletions, unversioning, ...
363
        creator.shelve_deletion('foo-id')
364
        creator.transform()
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
365
        self.assertPathExists('tree/foo')
0.14.10 by Aaron Bentley
Fix behavior with deletions, unversioning, ...
366
4526.7.3 by Aaron Bentley
Test shelve_change.
367
    def prepare_shelve_change_kind(self):
0.14.23 by Aaron Bentley
Allow shelving kind change
368
        tree = self.make_branch_and_tree('tree')
369
        self.build_tree_contents([('tree/foo', 'bar')])
370
        tree.add('foo', 'foo-id')
371
        tree.commit('Added file and directory')
372
        os.unlink('tree/foo')
373
        os.mkdir('tree/foo')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
374
        tree.lock_tree_write()
375
        self.addCleanup(tree.unlock)
0.14.23 by Aaron Bentley
Allow shelving kind change
376
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
377
        self.addCleanup(creator.finalize)
378
        self.assertEqual([('change kind', 'foo-id', 'file', 'directory',
0.14.32 by Aaron Bentley
Replace ShelfCreator.__iter__ with ShelfCreator.iter_shelvable
379
                           'foo')], sorted(list(creator.iter_shelvable())))
4526.7.3 by Aaron Bentley
Test shelve_change.
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()
0.14.23 by Aaron Bentley
Allow shelving kind change
390
        creator.shelve_content_change('foo-id')
391
        creator.transform()
4526.7.3 by Aaron Bentley
Test shelve_change.
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')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
403
        tree.lock_tree_write()
404
        self.addCleanup(tree.unlock)
4526.7.3 by Aaron Bentley
Test shelve_change.
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))
0.14.23 by Aaron Bentley
Allow shelving kind change
409
0.14.10 by Aaron Bentley
Fix behavior with deletions, unversioning, ...
410
    def test_shelve_unversion(self):
411
        tree = self.make_branch_and_tree('tree')
412
        self.build_tree(['tree/foo',])
413
        tree.add('foo', 'foo-id')
414
        tree.commit('Added file and directory')
415
        tree.unversion(['foo-id'])
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
416
        tree.lock_tree_write()
417
        self.addCleanup(tree.unlock)
0.14.10 by Aaron Bentley
Fix behavior with deletions, unversioning, ...
418
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
419
        self.addCleanup(creator.finalize)
0.14.13 by Aaron Bentley
Provide path and kind when deletes/adds are detected.
420
        self.assertEqual([('delete file', 'foo-id', 'file', 'foo')],
0.14.32 by Aaron Bentley
Replace ShelfCreator.__iter__ with ShelfCreator.iter_shelvable
421
                         sorted(list(creator.iter_shelvable())))
0.14.10 by Aaron Bentley
Fix behavior with deletions, unversioning, ...
422
        creator.shelve_deletion('foo-id')
423
        creator.transform()
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
424
        self.assertPathExists('tree/foo')
0.14.10 by Aaron Bentley
Fix behavior with deletions, unversioning, ...
425
0.14.33 by Aaron Bentley
Add explicit test of shelf on-disk format
426
    def test_shelve_serialization(self):
427
        tree = self.make_branch_and_tree('.')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
428
        tree.lock_tree_write()
429
        self.addCleanup(tree.unlock)
0.14.33 by Aaron Bentley
Add explicit test of shelf on-disk format
430
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
431
        self.addCleanup(creator.finalize)
0.12.76 by Aaron Bentley
Convert failing tests
432
        shelf_file = open('shelf', 'wb')
433
        self.addCleanup(shelf_file.close)
434
        try:
435
            creator.write_shelf(shelf_file)
436
        finally:
437
            shelf_file.close()
438
        self.assertFileEqual(EMPTY_SHELF, 'shelf')
0.14.33 by Aaron Bentley
Add explicit test of shelf on-disk format
439
0.12.19 by Aaron Bentley
Add support for writing shelves
440
    def test_write_shelf(self):
441
        tree = self.make_branch_and_tree('tree')
442
        self.build_tree(['tree/foo'])
443
        tree.add('foo', 'foo-id')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
444
        tree.lock_tree_write()
445
        self.addCleanup(tree.unlock)
0.14.7 by Aaron Bentley
Misc test cleanup
446
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
447
        self.addCleanup(creator.finalize)
0.14.32 by Aaron Bentley
Replace ShelfCreator.__iter__ with ShelfCreator.iter_shelvable
448
        list(creator.iter_shelvable())
0.14.2 by Aaron Bentley
Somewhat clean up shelving
449
        creator.shelve_creation('foo-id')
0.12.29 by Aaron Bentley
Update failing tests
450
        shelf_file = open('shelf', 'wb')
451
        try:
0.12.61 by Aaron Bentley
Stop assigning result of write_shelf
452
            creator.write_shelf(shelf_file)
0.12.29 by Aaron Bentley
Update failing tests
453
        finally:
454
            shelf_file.close()
0.12.19 by Aaron Bentley
Add support for writing shelves
455
        parser = pack.ContainerPushParser()
0.12.29 by Aaron Bentley
Update failing tests
456
        shelf_file = open('shelf', 'rb')
0.12.19 by Aaron Bentley
Add support for writing shelves
457
        try:
458
            parser.accept_bytes(shelf_file.read())
459
        finally:
460
            shelf_file.close()
461
        tt = transform.TransformPreview(tree)
0.14.7 by Aaron Bentley
Misc test cleanup
462
        self.addCleanup(tt.finalize)
0.12.29 by Aaron Bentley
Update failing tests
463
        records = iter(parser.read_pending_records())
464
        #skip revision-id
465
        records.next()
0.15.26 by Aaron Bentley
Merge with prepare-shelf
466
        tt.deserialize(records)
0.12.21 by Aaron Bentley
Add failing test of unshelver
467
3873.2.4 by Benoît Pierre
Add a test: test_shelve_unversioned; check if tree is in a clean state
468
    def test_shelve_unversioned(self):
469
        tree = self.make_branch_and_tree('tree')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
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()
3873.2.4 by Benoît Pierre
Add a test: test_shelve_unversioned; check if tree is in a clean state
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).
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
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
4595.9.1 by Aaron Bentley
Fix shelve in uncommitted trees.
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')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
493
        tree.lock_tree_write()
494
        self.addCleanup(tree.unlock)
4595.9.1 by Aaron Bentley
Fix shelve in uncommitted trees.
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')
4596.1.6 by Martin Pool
merge trunk
502
        tree.lock_tree_write()
503
        self.addCleanup(tree.unlock)
4595.9.1 by Aaron Bentley
Fix shelve in uncommitted trees.
504
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
505
        self.addCleanup(creator.finalize)
506
        self.assertEqual([], list(creator.iter_shelvable()))
507
0.12.21 by Aaron Bentley
Add failing test of unshelver
508
509
class TestUnshelver(tests.TestCaseWithTransport):
510
0.15.31 by Aaron Bentley
Remove 'unshelve' method, test make_merger
511
    def test_make_merger(self):
0.12.21 by Aaron Bentley
Add failing test of unshelver
512
        tree = self.make_branch_and_tree('tree')
0.12.30 by Aaron Bentley
Fix test by using non NULL base tree
513
        tree.commit('first commit')
0.12.21 by Aaron Bentley
Add failing test of unshelver
514
        self.build_tree_contents([('tree/foo', 'bar')])
0.12.24 by Aaron Bentley
Get unshelve using merge codepath, not applying transform directly
515
        tree.lock_write()
516
        self.addCleanup(tree.unlock)
0.12.21 by Aaron Bentley
Add failing test of unshelver
517
        tree.add('foo', 'foo-id')
0.15.5 by Aaron Bentley
Rename to shelf
518
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
519
        self.addCleanup(creator.finalize)
0.12.73 by Aaron Bentley
Merge unshelve into shelf-manager
520
        list(creator.iter_shelvable())
0.12.23 by Aaron Bentley
Fix up unshelve some more
521
        creator.shelve_creation('foo-id')
0.12.29 by Aaron Bentley
Update failing tests
522
        shelf_file = open('shelf-file', 'w+b')
523
        try:
0.12.61 by Aaron Bentley
Stop assigning result of write_shelf
524
            creator.write_shelf(shelf_file)
0.12.29 by Aaron Bentley
Update failing tests
525
            creator.transform()
526
            shelf_file.seek(0)
0.12.34 by Aaron Bentley
merge with unshelve
527
            unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
0.12.66 by Aaron Bentley
Merge with unshelve
528
            unshelver.make_merger().do_merge()
4659.2.3 by Vincent Ladeuil
Cleanup more bzr-limbo-XXXXXX leaks in /tmp during selftest.
529
            self.addCleanup(unshelver.finalize)
0.12.29 by Aaron Bentley
Update failing tests
530
            self.assertFileEqual('bar', 'tree/foo')
531
        finally:
532
            shelf_file.close()
0.12.26 by Aaron Bentley
Use correct base for shelving
533
0.15.23 by Aaron Bentley
Use correct tree for desrializing transform
534
    def test_unshelve_changed(self):
535
        tree = self.make_branch_and_tree('tree')
536
        tree.lock_write()
537
        self.addCleanup(tree.unlock)
538
        self.build_tree_contents([('tree/foo', 'a\nb\nc\n')])
539
        tree.add('foo', 'foo-id')
540
        tree.commit('first commit')
541
        self.build_tree_contents([('tree/foo', 'a\nb\nd\n')])
542
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
543
        self.addCleanup(creator.finalize)
0.12.73 by Aaron Bentley
Merge unshelve into shelf-manager
544
        list(creator.iter_shelvable())
0.15.23 by Aaron Bentley
Use correct tree for desrializing transform
545
        creator.shelve_lines('foo-id', ['a\n', 'b\n', 'c\n'])
0.12.57 by Aaron Bentley
Update for new Shelf API
546
        shelf_file = open('shelf', 'w+b')
547
        self.addCleanup(shelf_file.close)
548
        creator.write_shelf(shelf_file)
0.15.23 by Aaron Bentley
Use correct tree for desrializing transform
549
        creator.transform()
550
        self.build_tree_contents([('tree/foo', 'z\na\nb\nc\n')])
0.12.57 by Aaron Bentley
Update for new Shelf API
551
        shelf_file.seek(0)
552
        unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
4659.2.3 by Vincent Ladeuil
Cleanup more bzr-limbo-XXXXXX leaks in /tmp during selftest.
553
        self.addCleanup(unshelver.finalize)
0.15.31 by Aaron Bentley
Remove 'unshelve' method, test make_merger
554
        unshelver.make_merger().do_merge()
0.15.23 by Aaron Bentley
Use correct tree for desrializing transform
555
        self.assertFileEqual('z\na\nb\nd\n', 'tree/foo')
556
3981.1.1 by Robert Collins
Fix bug 319790 - unshelve of deleted paths failing.
557
    def test_unshelve_deleted(self):
558
        tree = self.make_branch_and_tree('tree')
559
        tree.lock_write()
560
        self.addCleanup(tree.unlock)
561
        self.build_tree_contents([('tree/foo/',), ('tree/foo/bar', 'baz')])
562
        tree.add(['foo', 'foo/bar'], ['foo-id', 'bar-id'])
563
        tree.commit('Added file and directory')
564
        tree.unversion(['foo-id', 'bar-id'])
565
        os.unlink('tree/foo/bar')
566
        os.rmdir('tree/foo')
567
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
568
        list(creator.iter_shelvable())
569
        creator.shelve_deletion('foo-id')
570
        creator.shelve_deletion('bar-id')
571
        shelf_file = open('shelf', 'w+b')
572
        self.addCleanup(shelf_file.close)
573
        creator.write_shelf(shelf_file)
574
        creator.transform()
575
        creator.finalize()
576
        # validate the test setup
577
        self.assertTrue('foo-id' in tree)
578
        self.assertTrue('bar-id' in tree)
579
        self.assertFileEqual('baz', 'tree/foo/bar')
580
        shelf_file.seek(0)
581
        unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
4659.2.3 by Vincent Ladeuil
Cleanup more bzr-limbo-XXXXXX leaks in /tmp during selftest.
582
        self.addCleanup(unshelver.finalize)
3981.1.1 by Robert Collins
Fix bug 319790 - unshelve of deleted paths failing.
583
        unshelver.make_merger().do_merge()
584
        self.assertFalse('foo-id' in tree)
585
        self.assertFalse('bar-id' in tree)
586
0.12.26 by Aaron Bentley
Use correct base for shelving
587
    def test_unshelve_base(self):
588
        tree = self.make_branch_and_tree('tree')
589
        tree.lock_write()
590
        self.addCleanup(tree.unlock)
591
        tree.commit('rev1', rev_id='rev1')
0.15.5 by Aaron Bentley
Rename to shelf
592
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
0.12.59 by Aaron Bentley
Fix locking bugs in tests
593
        self.addCleanup(creator.finalize)
0.12.42 by Aaron Bentley
Get shelf from tree
594
        manager = tree.get_shelf_manager()
0.12.29 by Aaron Bentley
Update failing tests
595
        shelf_id, shelf_file = manager.new_shelf()
596
        try:
0.12.61 by Aaron Bentley
Stop assigning result of write_shelf
597
            creator.write_shelf(shelf_file)
0.12.29 by Aaron Bentley
Update failing tests
598
        finally:
599
            shelf_file.close()
0.12.26 by Aaron Bentley
Use correct base for shelving
600
        tree.commit('rev2', rev_id='rev2')
0.12.29 by Aaron Bentley
Update failing tests
601
        shelf_file = manager.read_shelf(1)
0.12.59 by Aaron Bentley
Fix locking bugs in tests
602
        self.addCleanup(shelf_file.close)
603
        unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
604
        self.addCleanup(unshelver.finalize)
0.12.26 by Aaron Bentley
Use correct base for shelving
605
        self.assertEqual('rev1', unshelver.base_tree.get_revision_id())
0.12.27 by Aaron Bentley
Implement shelf manager
606
0.15.41 by Aaron Bentley
Replace assert with proper error handling
607
    def test_unshelve_serialization(self):
608
        tree = self.make_branch_and_tree('.')
609
        self.build_tree_contents([('shelf', EMPTY_SHELF)])
0.12.76 by Aaron Bentley
Convert failing tests
610
        shelf_file = open('shelf', 'rb')
611
        self.addCleanup(shelf_file.close)
612
        unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
613
        unshelver.finalize()
0.15.41 by Aaron Bentley
Replace assert with proper error handling
614
615
    def test_corrupt_shelf(self):
616
        tree = self.make_branch_and_tree('.')
617
        self.build_tree_contents([('shelf', EMPTY_SHELF.replace('metadata',
618
                                                                'foo'))])
0.12.76 by Aaron Bentley
Convert failing tests
619
        shelf_file = open('shelf', 'rb')
620
        self.addCleanup(shelf_file.close)
0.15.41 by Aaron Bentley
Replace assert with proper error handling
621
        e = self.assertRaises(errors.ShelfCorrupt,
622
                              shelf.Unshelver.from_tree_and_shelf, tree,
0.12.76 by Aaron Bentley
Convert failing tests
623
                              shelf_file)
0.15.41 by Aaron Bentley
Replace assert with proper error handling
624
        self.assertEqual('Shelf corrupt.', str(e))
0.12.75 by Aaron Bentley
Merge unshelve into shelf-manager
625
5599.1.1 by John Arbash Meinel
Move away from using Tree.inventory[] just to check if this is a root.
626
    def test_unshelve_subdir_in_now_removed_dir(self):
627
        tree = self.make_branch_and_tree('.')
628
        self.addCleanup(tree.lock_write().unlock)
629
        self.build_tree(['dir/', 'dir/subdir/', 'dir/subdir/foo'])
630
        tree.add(['dir'], ['dir-id'])
631
        tree.commit('versioned dir')
632
        tree.add(['dir/subdir', 'dir/subdir/foo'], ['subdir-id', 'foo-id'])
633
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
634
        self.addCleanup(creator.finalize)
635
        for change in creator.iter_shelvable():
636
            creator.shelve_change(change)
637
        shelf_manager = tree.get_shelf_manager()
638
        shelf_id = shelf_manager.shelve_changes(creator)
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
639
        self.assertPathDoesNotExist('dir/subdir')
5599.1.1 by John Arbash Meinel
Move away from using Tree.inventory[] just to check if this is a root.
640
        tree.remove(['dir'])
641
        unshelver = shelf_manager.get_unshelver(shelf_id)
642
        self.addCleanup(unshelver.finalize)
643
        unshelver.make_merger().do_merge()
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
644
        self.assertPathExists('dir/subdir/foo')
5599.1.1 by John Arbash Meinel
Move away from using Tree.inventory[] just to check if this is a root.
645
        self.assertEqual('dir-id', tree.path2id('dir'))
646
        self.assertEqual('subdir-id', tree.path2id('dir/subdir'))
647
        self.assertEqual('foo-id', tree.path2id('dir/subdir/foo'))
648
0.12.27 by Aaron Bentley
Implement shelf manager
649
650
class TestShelfManager(tests.TestCaseWithTransport):
651
0.12.42 by Aaron Bentley
Get shelf from tree
652
    def test_get_shelf_manager(self):
0.12.27 by Aaron Bentley
Implement shelf manager
653
        tree = self.make_branch_and_tree('.')
0.12.42 by Aaron Bentley
Get shelf from tree
654
        manager = tree.get_shelf_manager()
0.12.41 by Aaron Bentley
Change shelf to use WT control dir for shelves
655
        self.assertEqual(tree._transport.base + 'shelf/',
0.12.27 by Aaron Bentley
Implement shelf manager
656
                         manager.transport.base)
657
658
    def get_manager(self):
0.12.42 by Aaron Bentley
Get shelf from tree
659
        return self.make_branch_and_tree('.').get_shelf_manager()
0.12.27 by Aaron Bentley
Implement shelf manager
660
0.12.77 by Aaron Bentley
Use names of the form shelf-5 for shelves
661
    def test_get_shelf_filename(self):
662
        tree = self.make_branch_and_tree('.')
663
        manager = tree.get_shelf_manager()
664
        self.assertEqual('shelf-1', manager.get_shelf_filename(1))
665
666
    def test_get_shelf_ids(self):
667
        tree = self.make_branch_and_tree('.')
668
        manager = tree.get_shelf_manager()
669
        self.assertEqual([1, 3], manager.get_shelf_ids(
670
                         ['shelf-1', 'shelf-02', 'shelf-3']))
671
0.12.27 by Aaron Bentley
Implement shelf manager
672
    def test_new_shelf(self):
673
        manager = self.get_manager()
674
        shelf_id, shelf_file = manager.new_shelf()
675
        shelf_file.close()
676
        self.assertEqual(1, shelf_id)
677
        shelf_id, shelf_file = manager.new_shelf()
678
        shelf_file.close()
679
        self.assertEqual(2, shelf_id)
680
        manager.delete_shelf(1)
681
        shelf_id, shelf_file = manager.new_shelf()
682
        shelf_file.close()
683
        self.assertEqual(3, shelf_id)
684
685
    def test_active_shelves(self):
686
        manager = self.get_manager()
687
        self.assertEqual([], manager.active_shelves())
688
        shelf_id, shelf_file = manager.new_shelf()
689
        shelf_file.close()
690
        self.assertEqual([1], manager.active_shelves())
691
692
    def test_delete_shelf(self):
693
        manager = self.get_manager()
694
        shelf_id, shelf_file = manager.new_shelf()
695
        shelf_file.close()
696
        self.assertEqual([1], manager.active_shelves())
697
        manager.delete_shelf(1)
698
        self.assertEqual([], manager.active_shelves())
699
700
    def test_last_shelf(self):
701
        manager = self.get_manager()
702
        self.assertIs(None, manager.last_shelf())
703
        shelf_id, shelf_file = manager.new_shelf()
704
        shelf_file.close()
705
        self.assertEqual(1, manager.last_shelf())
706
707
    def test_read_shelf(self):
708
        manager = self.get_manager()
709
        shelf_id, shelf_file = manager.new_shelf()
710
        try:
711
            shelf_file.write('foo')
712
        finally:
713
            shelf_file.close()
714
        shelf_id, shelf_file = manager.new_shelf()
715
        try:
716
            shelf_file.write('bar')
717
        finally:
718
            shelf_file.close()
719
        shelf_file = manager.read_shelf(1)
720
        try:
721
            self.assertEqual('foo', shelf_file.read())
722
        finally:
723
            shelf_file.close()
724
        shelf_file = manager.read_shelf(2)
725
        try:
726
            self.assertEqual('bar', shelf_file.read())
727
        finally:
728
            shelf_file.close()
0.12.43 by Aaron Bentley
Make ShelfManager consume ShelfCreator and produce Unshelver
729
0.12.50 by Aaron Bentley
Improve error handling for non-existant shelf-ids
730
    def test_read_non_existant(self):
731
        manager = self.get_manager()
0.12.68 by Aaron Bentley
Update docs, move items to proper files.
732
        e = self.assertRaises(errors.NoSuchShelfId, manager.read_shelf, 1)
0.12.50 by Aaron Bentley
Improve error handling for non-existant shelf-ids
733
        self.assertEqual('No changes are shelved with id "1".', str(e))
734
0.12.43 by Aaron Bentley
Make ShelfManager consume ShelfCreator and produce Unshelver
735
    def test_shelve_changes(self):
736
        tree = self.make_branch_and_tree('tree')
0.12.44 by Aaron Bentley
Give manager responsibility for applying transform
737
        tree.commit('no-change commit')
738
        tree.lock_write()
739
        self.addCleanup(tree.unlock)
740
        self.build_tree_contents([('tree/foo', 'bar')])
741
        self.assertFileEqual('bar', 'tree/foo')
0.12.43 by Aaron Bentley
Make ShelfManager consume ShelfCreator and produce Unshelver
742
        tree.add('foo', 'foo-id')
743
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
744
        self.addCleanup(creator.finalize)
0.12.74 by Aaron Bentley
Update to use iter_shelvable
745
        list(creator.iter_shelvable())
0.12.43 by Aaron Bentley
Make ShelfManager consume ShelfCreator and produce Unshelver
746
        creator.shelve_creation('foo-id')
747
        shelf_manager = tree.get_shelf_manager()
748
        shelf_id = shelf_manager.shelve_changes(creator)
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
749
        self.assertPathDoesNotExist('tree/foo')
0.12.43 by Aaron Bentley
Make ShelfManager consume ShelfCreator and produce Unshelver
750
        unshelver = shelf_manager.get_unshelver(shelf_id)
4659.2.3 by Vincent Ladeuil
Cleanup more bzr-limbo-XXXXXX leaks in /tmp during selftest.
751
        self.addCleanup(unshelver.finalize)
0.12.67 by Aaron Bentley
Update for new Unshelver API
752
        unshelver.make_merger().do_merge()
0.12.44 by Aaron Bentley
Give manager responsibility for applying transform
753
        self.assertFileEqual('bar', 'tree/foo')
0.16.112 by Aaron Bentley
Add tests
754
755
    def test_get_metadata(self):
756
        tree = self.make_branch_and_tree('.')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
757
        tree.lock_tree_write()
758
        self.addCleanup(tree.unlock)
0.16.112 by Aaron Bentley
Add tests
759
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
760
        self.addCleanup(creator.finalize)
0.16.112 by Aaron Bentley
Add tests
761
        shelf_manager = tree.get_shelf_manager()
762
        shelf_id = shelf_manager.shelve_changes(creator, 'foo')
763
        metadata = shelf_manager.get_metadata(shelf_id)
764
        self.assertEqual('foo', metadata['message'])
765
        self.assertEqual('null:', metadata['revision_id'])