~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_shelf.py

  • Committer: Joe Julian
  • Date: 2010-01-10 02:25:31 UTC
  • mto: (4634.119.7 2.0)
  • mto: This revision was merged to the branch mainline in revision 4959.
  • Revision ID: joe@julianfamily.org-20100110022531-wqk61rsagz8xsiga
Added MANIFEST.in to allow bdist_rpm to have all the required include files and tools. bdist_rpm will still fail to build correctly on some distributions due to a disttools bug http://bugs.python.org/issue644744

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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
import os
 
18
 
 
19
from bzrlib import (
 
20
    errors,
 
21
    osutils,
 
22
    pack,
 
23
    shelf,
 
24
    tests,
 
25
    transform,
 
26
    workingtree,
 
27
    )
 
28
 
 
29
 
 
30
EMPTY_SHELF = ("Bazaar pack format 1 (introduced in 0.18)\n"
 
31
               "B23\n"
 
32
               "metadata\n\n"
 
33
               "d11:revision_id5:null:e"
 
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
 
 
41
class TestPrepareShelf(tests.TestCaseWithTransport):
 
42
 
 
43
    def prepare_shelve_rename(self):
 
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')
 
49
        tree.lock_tree_write()
 
50
        self.addCleanup(tree.unlock)
 
51
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
 
52
        self.addCleanup(creator.finalize)
 
53
        self.assertEqual([('rename', 'foo-id', 'foo', 'bar')],
 
54
                          list(creator.iter_shelvable()))
 
55
        return creator
 
56
 
 
57
    def check_shelve_rename(self, creator):
 
58
        work_trans_id = creator.work_transform.trans_id_file_id('foo-id')
 
59
        self.assertEqual('foo', creator.work_transform.final_name(
 
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
 
 
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):
 
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')
 
81
        tree.lock_tree_write()
 
82
        self.addCleanup(tree.unlock)
 
83
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
 
84
        self.addCleanup(creator.finalize)
 
85
        self.assertEqual([('rename', 'baz-id', 'foo/baz', 'bar/baz')],
 
86
                         list(creator.iter_shelvable()))
 
87
        return creator, tree
 
88
 
 
89
    def check_shelve_move(self, creator, tree):
 
90
        work_trans_id = creator.work_transform.trans_id_file_id('baz-id')
 
91
        work_foo = creator.work_transform.trans_id_file_id('foo-id')
 
92
        self.assertEqual(work_foo, creator.work_transform.final_parent(
 
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))
 
98
        creator.transform()
 
99
        self.assertEqual('foo/baz', tree.id2path('baz-id'))
 
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 assertShelvedFileEqual(self, expected_content, creator, file_id):
 
112
        s_trans_id = creator.shelf_transform.trans_id_file_id(file_id)
 
113
        shelf_file = creator.shelf_transform._limbo_name(s_trans_id)
 
114
        self.assertFileEqual(expected_content, shelf_file)
 
115
 
 
116
    def prepare_content_change(self):
 
117
        tree = self.make_branch_and_tree('.')
 
118
        tree.lock_write()
 
119
        self.addCleanup(tree.unlock)
 
120
        self.build_tree_contents([('foo', 'a\n')])
 
121
        tree.add('foo', 'foo-id')
 
122
        tree.commit('Committed foo')
 
123
        self.build_tree_contents([('foo', 'b\na\nc\n')])
 
124
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
 
125
        self.addCleanup(creator.finalize)
 
126
        return creator
 
127
 
 
128
    def test_shelve_content_change(self):
 
129
        creator = self.prepare_content_change()
 
130
        self.assertEqual([('modify text', 'foo-id')],
 
131
                         list(creator.iter_shelvable()))
 
132
        creator.shelve_lines('foo-id', ['a\n', 'c\n'])
 
133
        creator.transform()
 
134
        self.assertFileEqual('a\nc\n', 'foo')
 
135
        self.assertShelvedFileEqual('b\na\n', creator, 'foo-id')
 
136
 
 
137
    def test_shelve_change_handles_modify_text(self):
 
138
        creator = self.prepare_content_change()
 
139
        creator.shelve_change(('modify text', 'foo-id'))
 
140
        creator.transform()
 
141
        self.assertFileEqual('a\n', 'foo')
 
142
        self.assertShelvedFileEqual('b\na\nc\n', creator, 'foo-id')
 
143
 
 
144
    def test_shelve_all(self):
 
145
        creator = self.prepare_content_change()
 
146
        creator.shelve_all()
 
147
        creator.transform()
 
148
        self.assertFileEqual('a\n', 'foo')
 
149
        self.assertShelvedFileEqual('b\na\nc\n', creator, 'foo-id')
 
150
 
 
151
    def prepare_shelve_creation(self):
 
152
        tree = self.make_branch_and_tree('.')
 
153
        tree.lock_write()
 
154
        self.addCleanup(tree.unlock)
 
155
        tree.commit('Empty tree')
 
156
        self.build_tree_contents([('foo', 'a\n'), ('bar/',)])
 
157
        tree.add(['foo', 'bar'], ['foo-id', 'bar-id'])
 
158
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
 
159
        self.addCleanup(creator.finalize)
 
160
        self.assertEqual([('add file', 'bar-id', 'directory', 'bar'),
 
161
                          ('add file', 'foo-id', 'file', 'foo')],
 
162
                          sorted(list(creator.iter_shelvable())))
 
163
        return creator, tree
 
164
 
 
165
    def check_shelve_creation(self, creator, tree):
 
166
        self.assertRaises(StopIteration,
 
167
                          tree.iter_entries_by_dir(['foo-id']).next)
 
168
        s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
 
169
        self.assertEqual('foo-id',
 
170
                         creator.shelf_transform.final_file_id(s_trans_id))
 
171
        self.failIfExists('foo')
 
172
        self.failIfExists('bar')
 
173
        self.assertShelvedFileEqual('a\n', creator, 'foo-id')
 
174
        s_bar_trans_id = creator.shelf_transform.trans_id_file_id('bar-id')
 
175
        self.assertEqual('directory',
 
176
            creator.shelf_transform.final_kind(s_bar_trans_id))
 
177
 
 
178
    def test_shelve_creation(self):
 
179
        creator, tree = self.prepare_shelve_creation()
 
180
        creator.shelve_creation('foo-id')
 
181
        creator.shelve_creation('bar-id')
 
182
        creator.transform()
 
183
        self.check_shelve_creation(creator, tree)
 
184
 
 
185
    def test_shelve_change_handles_creation(self):
 
186
        creator, tree = self.prepare_shelve_creation()
 
187
        creator.shelve_change(('add file', 'foo-id', 'file', 'foo'))
 
188
        creator.shelve_change(('add file', 'bar-id', 'directory', 'bar'))
 
189
        creator.transform()
 
190
        self.check_shelve_creation(creator, tree)
 
191
 
 
192
    def _test_shelve_symlink_creation(self, link_name, link_target,
 
193
                                      shelve_change=False):
 
194
        self.requireFeature(tests.SymlinkFeature)
 
195
        tree = self.make_branch_and_tree('.')
 
196
        tree.lock_write()
 
197
        self.addCleanup(tree.unlock)
 
198
        tree.commit('Empty tree')
 
199
        os.symlink(link_target, link_name)
 
200
        tree.add(link_name, 'foo-id')
 
201
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
 
202
        self.addCleanup(creator.finalize)
 
203
        self.assertEqual([('add file', 'foo-id', 'symlink', link_name)],
 
204
                         list(creator.iter_shelvable()))
 
205
        if shelve_change:
 
206
            creator.shelve_change(('add file', 'foo-id', 'symlink', link_name))
 
207
        else:
 
208
            creator.shelve_creation('foo-id')
 
209
        creator.transform()
 
210
        s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
 
211
        self.failIfExists(link_name)
 
212
        limbo_name = creator.shelf_transform._limbo_name(s_trans_id)
 
213
        self.assertEqual(link_target, osutils.readlink(limbo_name))
 
214
        ptree = creator.shelf_transform.get_preview_tree()
 
215
        self.assertEqual(link_target, ptree.get_symlink_target('foo-id'))
 
216
 
 
217
    def test_shelve_symlink_creation(self):
 
218
        self._test_shelve_symlink_creation('foo', 'bar')
 
219
 
 
220
    def test_shelve_unicode_symlink_creation(self):
 
221
        self.requireFeature(tests.UnicodeFilenameFeature)
 
222
        self._test_shelve_symlink_creation(u'fo\N{Euro Sign}o',
 
223
                                           u'b\N{Euro Sign}ar')
 
224
 
 
225
    def test_shelve_change_handles_symlink_creation(self):
 
226
        self._test_shelve_symlink_creation('foo', 'bar', shelve_change=True)
 
227
 
 
228
    def _test_shelve_symlink_target_change(self, link_name,
 
229
                                           old_target, new_target,
 
230
                                           shelve_change=False):
 
231
        self.requireFeature(tests.SymlinkFeature)
 
232
        tree = self.make_branch_and_tree('.')
 
233
        tree.lock_write()
 
234
        self.addCleanup(tree.unlock)
 
235
        os.symlink(old_target, link_name)
 
236
        tree.add(link_name, 'foo-id')
 
237
        tree.commit("commit symlink")
 
238
        os.unlink(link_name)
 
239
        os.symlink(new_target, link_name)
 
240
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
 
241
        self.addCleanup(creator.finalize)
 
242
        self.assertEqual([('modify target', 'foo-id', link_name,
 
243
                           old_target, new_target)],
 
244
                         list(creator.iter_shelvable()))
 
245
        if shelve_change:
 
246
            creator.shelve_change(('modify target', 'foo-id', link_name,
 
247
                                   old_target, new_target))
 
248
        else:
 
249
            creator.shelve_modify_target('foo-id')
 
250
        creator.transform()
 
251
        self.assertEqual(old_target, osutils.readlink(link_name))
 
252
        s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
 
253
        limbo_name = creator.shelf_transform._limbo_name(s_trans_id)
 
254
        self.assertEqual(new_target, osutils.readlink(limbo_name))
 
255
        ptree = creator.shelf_transform.get_preview_tree()
 
256
        self.assertEqual(new_target, ptree.get_symlink_target('foo-id'))
 
257
 
 
258
    def test_shelve_symlink_target_change(self):
 
259
        self._test_shelve_symlink_target_change('foo', 'bar', 'baz')
 
260
 
 
261
    def test_shelve_unicode_symlink_target_change(self):
 
262
        self.requireFeature(tests.UnicodeFilenameFeature)
 
263
        self._test_shelve_symlink_target_change(
 
264
            u'fo\N{Euro Sign}o', u'b\N{Euro Sign}ar', u'b\N{Euro Sign}az')
 
265
 
 
266
    def test_shelve_change_handles_symlink_target_change(self):
 
267
        self._test_shelve_symlink_target_change('foo', 'bar', 'baz',
 
268
                                                shelve_change=True)
 
269
 
 
270
    def test_shelve_creation_no_contents(self):
 
271
        tree = self.make_branch_and_tree('.')
 
272
        tree.lock_write()
 
273
        self.addCleanup(tree.unlock)
 
274
        tree.commit('Empty tree')
 
275
        self.build_tree(['foo'])
 
276
        tree.add('foo', 'foo-id')
 
277
        os.unlink('foo')
 
278
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
 
279
        self.addCleanup(creator.finalize)
 
280
        self.assertEqual([('add file', 'foo-id', None, 'foo')],
 
281
                         sorted(list(creator.iter_shelvable())))
 
282
        creator.shelve_creation('foo-id')
 
283
        creator.transform()
 
284
        self.assertRaises(StopIteration,
 
285
                          tree.iter_entries_by_dir(['foo-id']).next)
 
286
        self.assertShelvedFileEqual('', creator, 'foo-id')
 
287
        s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
 
288
        self.assertEqual('foo-id',
 
289
                         creator.shelf_transform.final_file_id(s_trans_id))
 
290
        self.failIfExists('foo')
 
291
 
 
292
    def prepare_shelve_deletion(self):
 
293
        tree = self.make_branch_and_tree('tree')
 
294
        tree.lock_write()
 
295
        self.addCleanup(tree.unlock)
 
296
        self.build_tree_contents([('tree/foo/',), ('tree/foo/bar', 'baz')])
 
297
        tree.add(['foo', 'foo/bar'], ['foo-id', 'bar-id'])
 
298
        tree.commit('Added file and directory')
 
299
        tree.unversion(['foo-id', 'bar-id'])
 
300
        os.unlink('tree/foo/bar')
 
301
        os.rmdir('tree/foo')
 
302
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
 
303
        self.addCleanup(creator.finalize)
 
304
        self.assertEqual([('delete file', 'bar-id', 'file', 'foo/bar'),
 
305
                          ('delete file', 'foo-id', 'directory', 'foo')],
 
306
                          sorted(list(creator.iter_shelvable())))
 
307
        return creator, tree
 
308
 
 
309
    def check_shelve_deletion(self, tree):
 
310
        self.assertTrue('foo-id' in tree)
 
311
        self.assertTrue('bar-id' in tree)
 
312
        self.assertFileEqual('baz', 'tree/foo/bar')
 
313
 
 
314
    def test_shelve_deletion(self):
 
315
        creator, tree = self.prepare_shelve_deletion()
 
316
        creator.shelve_deletion('foo-id')
 
317
        creator.shelve_deletion('bar-id')
 
318
        creator.transform()
 
319
        self.check_shelve_deletion(tree)
 
320
 
 
321
    def test_shelve_change_handles_deletion(self):
 
322
        creator, tree = self.prepare_shelve_deletion()
 
323
        creator.shelve_change(('delete file', 'foo-id', 'directory', 'foo'))
 
324
        creator.shelve_change(('delete file', 'bar-id', 'file', 'foo/bar'))
 
325
        creator.transform()
 
326
        self.check_shelve_deletion(tree)
 
327
 
 
328
    def test_shelve_delete_contents(self):
 
329
        tree = self.make_branch_and_tree('tree')
 
330
        self.build_tree(['tree/foo',])
 
331
        tree.add('foo', 'foo-id')
 
332
        tree.commit('Added file and directory')
 
333
        os.unlink('tree/foo')
 
334
        tree.lock_tree_write()
 
335
        self.addCleanup(tree.unlock)
 
336
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
 
337
        self.addCleanup(creator.finalize)
 
338
        self.assertEqual([('delete file', 'foo-id', 'file', 'foo')],
 
339
                         sorted(list(creator.iter_shelvable())))
 
340
        creator.shelve_deletion('foo-id')
 
341
        creator.transform()
 
342
        self.failUnlessExists('tree/foo')
 
343
 
 
344
    def prepare_shelve_change_kind(self):
 
345
        tree = self.make_branch_and_tree('tree')
 
346
        self.build_tree_contents([('tree/foo', 'bar')])
 
347
        tree.add('foo', 'foo-id')
 
348
        tree.commit('Added file and directory')
 
349
        os.unlink('tree/foo')
 
350
        os.mkdir('tree/foo')
 
351
        tree.lock_tree_write()
 
352
        self.addCleanup(tree.unlock)
 
353
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
 
354
        self.addCleanup(creator.finalize)
 
355
        self.assertEqual([('change kind', 'foo-id', 'file', 'directory',
 
356
                           'foo')], sorted(list(creator.iter_shelvable())))
 
357
        return creator
 
358
 
 
359
    def check_shelve_change_kind(self, creator):
 
360
        self.assertFileEqual('bar', 'tree/foo')
 
361
        s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
 
362
        self.assertEqual('directory',
 
363
                         creator.shelf_transform._new_contents[s_trans_id])
 
364
 
 
365
    def test_shelve_change_kind(self):
 
366
        creator = self.prepare_shelve_change_kind()
 
367
        creator.shelve_content_change('foo-id')
 
368
        creator.transform()
 
369
        self.check_shelve_change_kind(creator)
 
370
 
 
371
    def test_shelve_change_handles_change_kind(self):
 
372
        creator = self.prepare_shelve_change_kind()
 
373
        creator.shelve_change(('change kind', 'foo-id', 'file', 'directory',
 
374
                               'foo'))
 
375
        creator.transform()
 
376
        self.check_shelve_change_kind(creator)
 
377
 
 
378
    def test_shelve_change_unknown_change(self):
 
379
        tree = self.make_branch_and_tree('tree')
 
380
        tree.lock_tree_write()
 
381
        self.addCleanup(tree.unlock)
 
382
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
 
383
        self.addCleanup(creator.finalize)
 
384
        e = self.assertRaises(ValueError, creator.shelve_change, ('unknown',))
 
385
        self.assertEqual('Unknown change kind: "unknown"', str(e))
 
386
 
 
387
    def test_shelve_unversion(self):
 
388
        tree = self.make_branch_and_tree('tree')
 
389
        self.build_tree(['tree/foo',])
 
390
        tree.add('foo', 'foo-id')
 
391
        tree.commit('Added file and directory')
 
392
        tree.unversion(['foo-id'])
 
393
        tree.lock_tree_write()
 
394
        self.addCleanup(tree.unlock)
 
395
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
 
396
        self.addCleanup(creator.finalize)
 
397
        self.assertEqual([('delete file', 'foo-id', 'file', 'foo')],
 
398
                         sorted(list(creator.iter_shelvable())))
 
399
        creator.shelve_deletion('foo-id')
 
400
        creator.transform()
 
401
        self.failUnlessExists('tree/foo')
 
402
 
 
403
    def test_shelve_serialization(self):
 
404
        tree = self.make_branch_and_tree('.')
 
405
        tree.lock_tree_write()
 
406
        self.addCleanup(tree.unlock)
 
407
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
 
408
        self.addCleanup(creator.finalize)
 
409
        shelf_file = open('shelf', 'wb')
 
410
        self.addCleanup(shelf_file.close)
 
411
        try:
 
412
            creator.write_shelf(shelf_file)
 
413
        finally:
 
414
            shelf_file.close()
 
415
        self.assertFileEqual(EMPTY_SHELF, 'shelf')
 
416
 
 
417
    def test_write_shelf(self):
 
418
        tree = self.make_branch_and_tree('tree')
 
419
        self.build_tree(['tree/foo'])
 
420
        tree.add('foo', 'foo-id')
 
421
        tree.lock_tree_write()
 
422
        self.addCleanup(tree.unlock)
 
423
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
 
424
        self.addCleanup(creator.finalize)
 
425
        list(creator.iter_shelvable())
 
426
        creator.shelve_creation('foo-id')
 
427
        shelf_file = open('shelf', 'wb')
 
428
        try:
 
429
            creator.write_shelf(shelf_file)
 
430
        finally:
 
431
            shelf_file.close()
 
432
        parser = pack.ContainerPushParser()
 
433
        shelf_file = open('shelf', 'rb')
 
434
        try:
 
435
            parser.accept_bytes(shelf_file.read())
 
436
        finally:
 
437
            shelf_file.close()
 
438
        tt = transform.TransformPreview(tree)
 
439
        self.addCleanup(tt.finalize)
 
440
        records = iter(parser.read_pending_records())
 
441
        #skip revision-id
 
442
        records.next()
 
443
        tt.deserialize(records)
 
444
 
 
445
    def test_shelve_unversioned(self):
 
446
        tree = self.make_branch_and_tree('tree')
 
447
        tree.lock_tree_write()
 
448
        try:
 
449
            self.assertRaises(errors.PathsNotVersionedError,
 
450
                              shelf.ShelfCreator, tree, tree.basis_tree(), ['foo'])
 
451
        finally:
 
452
            tree.unlock()
 
453
        # We should be able to lock/unlock the tree if ShelfCreator cleaned
 
454
        # after itself.
 
455
        wt = workingtree.WorkingTree.open('tree')
 
456
        wt.lock_tree_write()
 
457
        wt.unlock()
 
458
        # And a second tentative should raise the same error (no
 
459
        # limbo/pending_deletion leftovers).
 
460
        tree.lock_tree_write()
 
461
        try:
 
462
            self.assertRaises(errors.PathsNotVersionedError,
 
463
                              shelf.ShelfCreator, tree, tree.basis_tree(), ['foo'])
 
464
        finally:
 
465
            tree.unlock()
 
466
 
 
467
    def test_shelve_skips_added_root(self):
 
468
        """Skip adds of the root when iterating through shelvable changes."""
 
469
        tree = self.make_branch_and_tree('tree')
 
470
        tree.lock_tree_write()
 
471
        self.addCleanup(tree.unlock)
 
472
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
 
473
        self.addCleanup(creator.finalize)
 
474
        self.assertEqual([], list(creator.iter_shelvable()))
 
475
 
 
476
    def test_shelve_skips_added_root(self):
 
477
        """Skip adds of the root when iterating through shelvable changes."""
 
478
        tree = self.make_branch_and_tree('tree')
 
479
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
 
480
        self.addCleanup(creator.finalize)
 
481
        self.assertEqual([], list(creator.iter_shelvable()))
 
482
 
 
483
 
 
484
class TestUnshelver(tests.TestCaseWithTransport):
 
485
 
 
486
    def test_make_merger(self):
 
487
        tree = self.make_branch_and_tree('tree')
 
488
        tree.commit('first commit')
 
489
        self.build_tree_contents([('tree/foo', 'bar')])
 
490
        tree.lock_write()
 
491
        self.addCleanup(tree.unlock)
 
492
        tree.add('foo', 'foo-id')
 
493
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
 
494
        self.addCleanup(creator.finalize)
 
495
        list(creator.iter_shelvable())
 
496
        creator.shelve_creation('foo-id')
 
497
        shelf_file = open('shelf-file', 'w+b')
 
498
        try:
 
499
            creator.write_shelf(shelf_file)
 
500
            creator.transform()
 
501
            shelf_file.seek(0)
 
502
            unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
 
503
            unshelver.make_merger().do_merge()
 
504
            self.assertFileEqual('bar', 'tree/foo')
 
505
        finally:
 
506
            shelf_file.close()
 
507
 
 
508
    def test_unshelve_changed(self):
 
509
        tree = self.make_branch_and_tree('tree')
 
510
        tree.lock_write()
 
511
        self.addCleanup(tree.unlock)
 
512
        self.build_tree_contents([('tree/foo', 'a\nb\nc\n')])
 
513
        tree.add('foo', 'foo-id')
 
514
        tree.commit('first commit')
 
515
        self.build_tree_contents([('tree/foo', 'a\nb\nd\n')])
 
516
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
 
517
        self.addCleanup(creator.finalize)
 
518
        list(creator.iter_shelvable())
 
519
        creator.shelve_lines('foo-id', ['a\n', 'b\n', 'c\n'])
 
520
        shelf_file = open('shelf', 'w+b')
 
521
        self.addCleanup(shelf_file.close)
 
522
        creator.write_shelf(shelf_file)
 
523
        creator.transform()
 
524
        self.build_tree_contents([('tree/foo', 'z\na\nb\nc\n')])
 
525
        shelf_file.seek(0)
 
526
        unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
 
527
        unshelver.make_merger().do_merge()
 
528
        self.assertFileEqual('z\na\nb\nd\n', 'tree/foo')
 
529
 
 
530
    def test_unshelve_deleted(self):
 
531
        tree = self.make_branch_and_tree('tree')
 
532
        tree.lock_write()
 
533
        self.addCleanup(tree.unlock)
 
534
        self.build_tree_contents([('tree/foo/',), ('tree/foo/bar', 'baz')])
 
535
        tree.add(['foo', 'foo/bar'], ['foo-id', 'bar-id'])
 
536
        tree.commit('Added file and directory')
 
537
        tree.unversion(['foo-id', 'bar-id'])
 
538
        os.unlink('tree/foo/bar')
 
539
        os.rmdir('tree/foo')
 
540
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
 
541
        list(creator.iter_shelvable())
 
542
        creator.shelve_deletion('foo-id')
 
543
        creator.shelve_deletion('bar-id')
 
544
        shelf_file = open('shelf', 'w+b')
 
545
        self.addCleanup(shelf_file.close)
 
546
        creator.write_shelf(shelf_file)
 
547
        creator.transform()
 
548
        creator.finalize()
 
549
        # validate the test setup
 
550
        self.assertTrue('foo-id' in tree)
 
551
        self.assertTrue('bar-id' in tree)
 
552
        self.assertFileEqual('baz', 'tree/foo/bar')
 
553
        shelf_file.seek(0)
 
554
        unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
 
555
        unshelver.make_merger().do_merge()
 
556
        self.assertFalse('foo-id' in tree)
 
557
        self.assertFalse('bar-id' in tree)
 
558
 
 
559
    def test_unshelve_base(self):
 
560
        tree = self.make_branch_and_tree('tree')
 
561
        tree.lock_write()
 
562
        self.addCleanup(tree.unlock)
 
563
        tree.commit('rev1', rev_id='rev1')
 
564
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
 
565
        self.addCleanup(creator.finalize)
 
566
        manager = tree.get_shelf_manager()
 
567
        shelf_id, shelf_file = manager.new_shelf()
 
568
        try:
 
569
            creator.write_shelf(shelf_file)
 
570
        finally:
 
571
            shelf_file.close()
 
572
        tree.commit('rev2', rev_id='rev2')
 
573
        shelf_file = manager.read_shelf(1)
 
574
        self.addCleanup(shelf_file.close)
 
575
        unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
 
576
        self.addCleanup(unshelver.finalize)
 
577
        self.assertEqual('rev1', unshelver.base_tree.get_revision_id())
 
578
 
 
579
    def test_unshelve_serialization(self):
 
580
        tree = self.make_branch_and_tree('.')
 
581
        self.build_tree_contents([('shelf', EMPTY_SHELF)])
 
582
        shelf_file = open('shelf', 'rb')
 
583
        self.addCleanup(shelf_file.close)
 
584
        unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
 
585
        unshelver.finalize()
 
586
 
 
587
    def test_corrupt_shelf(self):
 
588
        tree = self.make_branch_and_tree('.')
 
589
        self.build_tree_contents([('shelf', EMPTY_SHELF.replace('metadata',
 
590
                                                                'foo'))])
 
591
        shelf_file = open('shelf', 'rb')
 
592
        self.addCleanup(shelf_file.close)
 
593
        e = self.assertRaises(errors.ShelfCorrupt,
 
594
                              shelf.Unshelver.from_tree_and_shelf, tree,
 
595
                              shelf_file)
 
596
        self.assertEqual('Shelf corrupt.', str(e))
 
597
 
 
598
 
 
599
class TestShelfManager(tests.TestCaseWithTransport):
 
600
 
 
601
    def test_get_shelf_manager(self):
 
602
        tree = self.make_branch_and_tree('.')
 
603
        manager = tree.get_shelf_manager()
 
604
        self.assertEqual(tree._transport.base + 'shelf/',
 
605
                         manager.transport.base)
 
606
 
 
607
    def get_manager(self):
 
608
        return self.make_branch_and_tree('.').get_shelf_manager()
 
609
 
 
610
    def test_get_shelf_filename(self):
 
611
        tree = self.make_branch_and_tree('.')
 
612
        manager = tree.get_shelf_manager()
 
613
        self.assertEqual('shelf-1', manager.get_shelf_filename(1))
 
614
 
 
615
    def test_get_shelf_ids(self):
 
616
        tree = self.make_branch_and_tree('.')
 
617
        manager = tree.get_shelf_manager()
 
618
        self.assertEqual([1, 3], manager.get_shelf_ids(
 
619
                         ['shelf-1', 'shelf-02', 'shelf-3']))
 
620
 
 
621
    def test_new_shelf(self):
 
622
        manager = self.get_manager()
 
623
        shelf_id, shelf_file = manager.new_shelf()
 
624
        shelf_file.close()
 
625
        self.assertEqual(1, shelf_id)
 
626
        shelf_id, shelf_file = manager.new_shelf()
 
627
        shelf_file.close()
 
628
        self.assertEqual(2, shelf_id)
 
629
        manager.delete_shelf(1)
 
630
        shelf_id, shelf_file = manager.new_shelf()
 
631
        shelf_file.close()
 
632
        self.assertEqual(3, shelf_id)
 
633
 
 
634
    def test_active_shelves(self):
 
635
        manager = self.get_manager()
 
636
        self.assertEqual([], manager.active_shelves())
 
637
        shelf_id, shelf_file = manager.new_shelf()
 
638
        shelf_file.close()
 
639
        self.assertEqual([1], manager.active_shelves())
 
640
 
 
641
    def test_delete_shelf(self):
 
642
        manager = self.get_manager()
 
643
        shelf_id, shelf_file = manager.new_shelf()
 
644
        shelf_file.close()
 
645
        self.assertEqual([1], manager.active_shelves())
 
646
        manager.delete_shelf(1)
 
647
        self.assertEqual([], manager.active_shelves())
 
648
 
 
649
    def test_last_shelf(self):
 
650
        manager = self.get_manager()
 
651
        self.assertIs(None, manager.last_shelf())
 
652
        shelf_id, shelf_file = manager.new_shelf()
 
653
        shelf_file.close()
 
654
        self.assertEqual(1, manager.last_shelf())
 
655
 
 
656
    def test_read_shelf(self):
 
657
        manager = self.get_manager()
 
658
        shelf_id, shelf_file = manager.new_shelf()
 
659
        try:
 
660
            shelf_file.write('foo')
 
661
        finally:
 
662
            shelf_file.close()
 
663
        shelf_id, shelf_file = manager.new_shelf()
 
664
        try:
 
665
            shelf_file.write('bar')
 
666
        finally:
 
667
            shelf_file.close()
 
668
        shelf_file = manager.read_shelf(1)
 
669
        try:
 
670
            self.assertEqual('foo', shelf_file.read())
 
671
        finally:
 
672
            shelf_file.close()
 
673
        shelf_file = manager.read_shelf(2)
 
674
        try:
 
675
            self.assertEqual('bar', shelf_file.read())
 
676
        finally:
 
677
            shelf_file.close()
 
678
 
 
679
    def test_read_non_existant(self):
 
680
        manager = self.get_manager()
 
681
        e = self.assertRaises(errors.NoSuchShelfId, manager.read_shelf, 1)
 
682
        self.assertEqual('No changes are shelved with id "1".', str(e))
 
683
 
 
684
    def test_shelve_changes(self):
 
685
        tree = self.make_branch_and_tree('tree')
 
686
        tree.commit('no-change commit')
 
687
        tree.lock_write()
 
688
        self.addCleanup(tree.unlock)
 
689
        self.build_tree_contents([('tree/foo', 'bar')])
 
690
        self.assertFileEqual('bar', 'tree/foo')
 
691
        tree.add('foo', 'foo-id')
 
692
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
 
693
        self.addCleanup(creator.finalize)
 
694
        list(creator.iter_shelvable())
 
695
        creator.shelve_creation('foo-id')
 
696
        shelf_manager = tree.get_shelf_manager()
 
697
        shelf_id = shelf_manager.shelve_changes(creator)
 
698
        self.failIfExists('tree/foo')
 
699
        unshelver = shelf_manager.get_unshelver(shelf_id)
 
700
        unshelver.make_merger().do_merge()
 
701
        self.assertFileEqual('bar', 'tree/foo')
 
702
 
 
703
    def test_get_metadata(self):
 
704
        tree = self.make_branch_and_tree('.')
 
705
        tree.lock_tree_write()
 
706
        self.addCleanup(tree.unlock)
 
707
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
 
708
        self.addCleanup(creator.finalize)
 
709
        shelf_manager = tree.get_shelf_manager()
 
710
        shelf_id = shelf_manager.shelve_changes(creator, 'foo')
 
711
        metadata = shelf_manager.get_metadata(shelf_id)
 
712
        self.assertEqual('foo', metadata['message'])
 
713
        self.assertEqual('null:', metadata['revision_id'])