~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_shelf.py

  • Committer: Martin Pool
  • Date: 2009-07-10 06:46:10 UTC
  • mto: (4525.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 4526.
  • Revision ID: mbp@sourcefrog.net-20090710064610-sqviksbqp5i34sw2
Rename to per_interrepository

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008, 2009, 2010 Canonical Ltd
 
1
# Copyright (C) 2008 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
40
40
 
41
41
class TestPrepareShelf(tests.TestCaseWithTransport):
42
42
 
43
 
    def prepare_shelve_rename(self):
 
43
    def test_shelve_rename(self):
44
44
        tree = self.make_branch_and_tree('.')
45
45
        self.build_tree(['foo'])
46
46
        tree.add(['foo'], ['foo-id'])
47
47
        tree.commit('foo')
48
48
        tree.rename_one('foo', 'bar')
49
 
        tree.lock_tree_write()
50
 
        self.addCleanup(tree.unlock)
51
49
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
52
50
        self.addCleanup(creator.finalize)
53
51
        self.assertEqual([('rename', 'foo-id', 'foo', 'bar')],
54
52
                          list(creator.iter_shelvable()))
55
 
        return creator
56
 
 
57
 
    def check_shelve_rename(self, creator):
 
53
        creator.shelve_rename('foo-id')
58
54
        work_trans_id = creator.work_transform.trans_id_file_id('foo-id')
59
55
        self.assertEqual('foo', creator.work_transform.final_name(
60
56
                         work_trans_id))
62
58
        self.assertEqual('bar', creator.shelf_transform.final_name(
63
59
                         shelf_trans_id))
64
60
 
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):
 
61
    def test_shelve_move(self):
76
62
        tree = self.make_branch_and_tree('.')
77
63
        self.build_tree(['foo/', 'bar/', 'foo/baz'])
78
64
        tree.add(['foo', 'bar', 'foo/baz'], ['foo-id', 'bar-id', 'baz-id'])
79
65
        tree.commit('foo')
80
66
        tree.rename_one('foo/baz', 'bar/baz')
81
 
        tree.lock_tree_write()
82
 
        self.addCleanup(tree.unlock)
83
67
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
84
68
        self.addCleanup(creator.finalize)
85
69
        self.assertEqual([('rename', 'baz-id', 'foo/baz', 'bar/baz')],
86
70
                         list(creator.iter_shelvable()))
87
 
        return creator, tree
88
 
 
89
 
    def check_shelve_move(self, creator, tree):
 
71
        creator.shelve_rename('baz-id')
90
72
        work_trans_id = creator.work_transform.trans_id_file_id('baz-id')
91
73
        work_foo = creator.work_transform.trans_id_file_id('foo-id')
92
74
        self.assertEqual(work_foo, creator.work_transform.final_parent(
98
80
        creator.transform()
99
81
        self.assertEqual('foo/baz', tree.id2path('baz-id'))
100
82
 
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
 
 
134
83
    def assertShelvedFileEqual(self, expected_content, creator, file_id):
135
84
        s_trans_id = creator.shelf_transform.trans_id_file_id(file_id)
136
85
        shelf_file = creator.shelf_transform._limbo_name(s_trans_id)
137
86
        self.assertFileEqual(expected_content, shelf_file)
138
87
 
139
 
    def prepare_content_change(self):
 
88
    def test_shelve_content_change(self):
140
89
        tree = self.make_branch_and_tree('.')
141
90
        tree.lock_write()
142
91
        self.addCleanup(tree.unlock)
146
95
        self.build_tree_contents([('foo', 'b\na\nc\n')])
147
96
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
148
97
        self.addCleanup(creator.finalize)
149
 
        return creator
150
 
 
151
 
    def test_shelve_content_change(self):
152
 
        creator = self.prepare_content_change()
153
98
        self.assertEqual([('modify text', 'foo-id')],
154
99
                         list(creator.iter_shelvable()))
155
100
        creator.shelve_lines('foo-id', ['a\n', 'c\n'])
157
102
        self.assertFileEqual('a\nc\n', 'foo')
158
103
        self.assertShelvedFileEqual('b\na\n', creator, 'foo-id')
159
104
 
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):
 
105
    def test_shelve_creation(self):
175
106
        tree = self.make_branch_and_tree('.')
176
107
        tree.lock_write()
177
108
        self.addCleanup(tree.unlock)
183
114
        self.assertEqual([('add file', 'bar-id', 'directory', 'bar'),
184
115
                          ('add file', 'foo-id', 'file', 'foo')],
185
116
                          sorted(list(creator.iter_shelvable())))
186
 
        return creator, tree
187
 
 
188
 
    def check_shelve_creation(self, creator, tree):
 
117
        creator.shelve_creation('foo-id')
 
118
        creator.shelve_creation('bar-id')
 
119
        creator.transform()
189
120
        self.assertRaises(StopIteration,
190
121
                          tree.iter_entries_by_dir(['foo-id']).next)
191
122
        s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
198
129
        self.assertEqual('directory',
199
130
            creator.shelf_transform.final_kind(s_bar_trans_id))
200
131
 
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):
 
132
    def _test_shelve_symlink_creation(self, link_name, link_target):
217
133
        self.requireFeature(tests.SymlinkFeature)
218
134
        tree = self.make_branch_and_tree('.')
219
135
        tree.lock_write()
225
141
        self.addCleanup(creator.finalize)
226
142
        self.assertEqual([('add file', 'foo-id', 'symlink', link_name)],
227
143
                         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')
 
144
        creator.shelve_creation('foo-id')
232
145
        creator.transform()
233
146
        s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
234
147
        self.failIfExists(link_name)
245
158
        self._test_shelve_symlink_creation(u'fo\N{Euro Sign}o',
246
159
                                           u'b\N{Euro Sign}ar')
247
160
 
248
 
    def test_shelve_change_handles_symlink_creation(self):
249
 
        self._test_shelve_symlink_creation('foo', 'bar', shelve_change=True)
250
 
 
251
161
    def _test_shelve_symlink_target_change(self, link_name,
252
 
                                           old_target, new_target,
253
 
                                           shelve_change=False):
 
162
                                           old_target, new_target):
254
163
        self.requireFeature(tests.SymlinkFeature)
255
164
        tree = self.make_branch_and_tree('.')
256
165
        tree.lock_write()
265
174
        self.assertEqual([('modify target', 'foo-id', link_name,
266
175
                           old_target, new_target)],
267
176
                         list(creator.iter_shelvable()))
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')
 
177
        creator.shelve_modify_target('foo-id')
273
178
        creator.transform()
274
179
        self.assertEqual(old_target, osutils.readlink(link_name))
275
180
        s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
286
191
        self._test_shelve_symlink_target_change(
287
192
            u'fo\N{Euro Sign}o', u'b\N{Euro Sign}ar', u'b\N{Euro Sign}az')
288
193
 
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
 
 
293
194
    def test_shelve_creation_no_contents(self):
294
195
        tree = self.make_branch_and_tree('.')
295
196
        tree.lock_write()
312
213
                         creator.shelf_transform.final_file_id(s_trans_id))
313
214
        self.failIfExists('foo')
314
215
 
315
 
    def prepare_shelve_deletion(self):
 
216
    def test_shelve_deletion(self):
316
217
        tree = self.make_branch_and_tree('tree')
317
218
        tree.lock_write()
318
219
        self.addCleanup(tree.unlock)
327
228
        self.assertEqual([('delete file', 'bar-id', 'file', 'foo/bar'),
328
229
                          ('delete file', 'foo-id', 'directory', 'foo')],
329
230
                          sorted(list(creator.iter_shelvable())))
330
 
        return creator, tree
331
 
 
332
 
    def check_shelve_deletion(self, tree):
 
231
        creator.shelve_deletion('foo-id')
 
232
        creator.shelve_deletion('bar-id')
 
233
        creator.transform()
333
234
        self.assertTrue('foo-id' in tree)
334
235
        self.assertTrue('bar-id' in tree)
335
236
        self.assertFileEqual('baz', 'tree/foo/bar')
336
237
 
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
 
 
351
238
    def test_shelve_delete_contents(self):
352
239
        tree = self.make_branch_and_tree('tree')
353
240
        self.build_tree(['tree/foo',])
354
241
        tree.add('foo', 'foo-id')
355
242
        tree.commit('Added file and directory')
356
243
        os.unlink('tree/foo')
357
 
        tree.lock_tree_write()
358
 
        self.addCleanup(tree.unlock)
359
244
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
360
245
        self.addCleanup(creator.finalize)
361
246
        self.assertEqual([('delete file', 'foo-id', 'file', 'foo')],
364
249
        creator.transform()
365
250
        self.failUnlessExists('tree/foo')
366
251
 
367
 
    def prepare_shelve_change_kind(self):
 
252
    def test_shelve_change_kind(self):
368
253
        tree = self.make_branch_and_tree('tree')
369
254
        self.build_tree_contents([('tree/foo', 'bar')])
370
255
        tree.add('foo', 'foo-id')
371
256
        tree.commit('Added file and directory')
372
257
        os.unlink('tree/foo')
373
258
        os.mkdir('tree/foo')
374
 
        tree.lock_tree_write()
375
 
        self.addCleanup(tree.unlock)
376
259
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
377
260
        self.addCleanup(creator.finalize)
378
261
        self.assertEqual([('change kind', 'foo-id', 'file', 'directory',
379
262
                           'foo')], sorted(list(creator.iter_shelvable())))
380
 
        return creator
381
 
 
382
 
    def check_shelve_change_kind(self, creator):
 
263
        creator.shelve_content_change('foo-id')
 
264
        creator.transform()
383
265
        self.assertFileEqual('bar', 'tree/foo')
384
266
        s_trans_id = creator.shelf_transform.trans_id_file_id('foo-id')
385
267
        self.assertEqual('directory',
386
268
                         creator.shelf_transform._new_contents[s_trans_id])
387
269
 
388
 
    def test_shelve_change_kind(self):
389
 
        creator = self.prepare_shelve_change_kind()
390
 
        creator.shelve_content_change('foo-id')
391
 
        creator.transform()
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))
409
 
 
410
270
    def test_shelve_unversion(self):
411
271
        tree = self.make_branch_and_tree('tree')
412
272
        self.build_tree(['tree/foo',])
413
273
        tree.add('foo', 'foo-id')
414
274
        tree.commit('Added file and directory')
415
275
        tree.unversion(['foo-id'])
416
 
        tree.lock_tree_write()
417
 
        self.addCleanup(tree.unlock)
418
276
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
419
277
        self.addCleanup(creator.finalize)
420
278
        self.assertEqual([('delete file', 'foo-id', 'file', 'foo')],
425
283
 
426
284
    def test_shelve_serialization(self):
427
285
        tree = self.make_branch_and_tree('.')
428
 
        tree.lock_tree_write()
429
 
        self.addCleanup(tree.unlock)
430
286
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
431
287
        self.addCleanup(creator.finalize)
432
288
        shelf_file = open('shelf', 'wb')
441
297
        tree = self.make_branch_and_tree('tree')
442
298
        self.build_tree(['tree/foo'])
443
299
        tree.add('foo', 'foo-id')
444
 
        tree.lock_tree_write()
445
 
        self.addCleanup(tree.unlock)
446
300
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
447
301
        self.addCleanup(creator.finalize)
448
302
        list(creator.iter_shelvable())
467
321
 
468
322
    def test_shelve_unversioned(self):
469
323
        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()
 
324
        self.assertRaises(errors.PathsNotVersionedError,
 
325
                          shelf.ShelfCreator, tree, tree.basis_tree(), ['foo'])
476
326
        # We should be able to lock/unlock the tree if ShelfCreator cleaned
477
327
        # after itself.
478
328
        wt = workingtree.WorkingTree.open('tree')
480
330
        wt.unlock()
481
331
        # And a second tentative should raise the same error (no
482
332
        # 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()))
 
333
        self.assertRaises(errors.PathsNotVersionedError,
 
334
                          shelf.ShelfCreator, tree, tree.basis_tree(), ['foo'])
507
335
 
508
336
 
509
337
class TestUnshelver(tests.TestCaseWithTransport):
526
354
            shelf_file.seek(0)
527
355
            unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
528
356
            unshelver.make_merger().do_merge()
529
 
            self.addCleanup(unshelver.finalize)
530
357
            self.assertFileEqual('bar', 'tree/foo')
531
358
        finally:
532
359
            shelf_file.close()
550
377
        self.build_tree_contents([('tree/foo', 'z\na\nb\nc\n')])
551
378
        shelf_file.seek(0)
552
379
        unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
553
 
        self.addCleanup(unshelver.finalize)
554
380
        unshelver.make_merger().do_merge()
555
381
        self.assertFileEqual('z\na\nb\nd\n', 'tree/foo')
556
382
 
579
405
        self.assertFileEqual('baz', 'tree/foo/bar')
580
406
        shelf_file.seek(0)
581
407
        unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
582
 
        self.addCleanup(unshelver.finalize)
583
408
        unshelver.make_merger().do_merge()
584
409
        self.assertFalse('foo-id' in tree)
585
410
        self.assertFalse('bar-id' in tree)
610
435
        shelf_file = open('shelf', 'rb')
611
436
        self.addCleanup(shelf_file.close)
612
437
        unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
613
 
        unshelver.finalize()
614
438
 
615
439
    def test_corrupt_shelf(self):
616
440
        tree = self.make_branch_and_tree('.')
725
549
        shelf_id = shelf_manager.shelve_changes(creator)
726
550
        self.failIfExists('tree/foo')
727
551
        unshelver = shelf_manager.get_unshelver(shelf_id)
728
 
        self.addCleanup(unshelver.finalize)
729
552
        unshelver.make_merger().do_merge()
730
553
        self.assertFileEqual('bar', 'tree/foo')
731
554
 
732
555
    def test_get_metadata(self):
733
556
        tree = self.make_branch_and_tree('.')
734
 
        tree.lock_tree_write()
735
 
        self.addCleanup(tree.unlock)
736
557
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
737
 
        self.addCleanup(creator.finalize)
738
558
        shelf_manager = tree.get_shelf_manager()
739
559
        shelf_id = shelf_manager.shelve_changes(creator, 'foo')
740
560
        metadata = shelf_manager.get_metadata(shelf_id)