~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_shelf.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-09-01 08:02:42 UTC
  • mfrom: (5390.3.3 faster-revert-593560)
  • Revision ID: pqm@pqm.ubuntu.com-20100901080242-esg62ody4frwmy66
(spiv) Avoid repeatedly calling self.target.all_file_ids() in
 InterTree.iter_changes. (Andrew Bennetts)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008 Canonical Ltd
 
1
# Copyright (C) 2008, 2009, 2010 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
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)
49
51
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
50
52
        self.addCleanup(creator.finalize)
51
53
        self.assertEqual([('rename', 'foo-id', 'foo', 'bar')],
76
78
        tree.add(['foo', 'bar', 'foo/baz'], ['foo-id', 'bar-id', 'baz-id'])
77
79
        tree.commit('foo')
78
80
        tree.rename_one('foo/baz', 'bar/baz')
 
81
        tree.lock_tree_write()
 
82
        self.addCleanup(tree.unlock)
79
83
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
80
84
        self.addCleanup(creator.finalize)
81
85
        self.assertEqual([('rename', 'baz-id', 'foo/baz', 'bar/baz')],
104
108
        creator.shelve_change(('rename', 'baz-id', 'foo/baz', 'bar/baz'))
105
109
        self.check_shelve_move(creator, tree)
106
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
 
107
134
    def assertShelvedFileEqual(self, expected_content, creator, file_id):
108
135
        s_trans_id = creator.shelf_transform.trans_id_file_id(file_id)
109
136
        shelf_file = creator.shelf_transform._limbo_name(s_trans_id)
110
137
        self.assertFileEqual(expected_content, shelf_file)
111
138
 
112
 
    def test_shelve_content_change(self):
 
139
    def prepare_content_change(self):
113
140
        tree = self.make_branch_and_tree('.')
114
141
        tree.lock_write()
115
142
        self.addCleanup(tree.unlock)
119
146
        self.build_tree_contents([('foo', 'b\na\nc\n')])
120
147
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
121
148
        self.addCleanup(creator.finalize)
 
149
        return creator
 
150
 
 
151
    def test_shelve_content_change(self):
 
152
        creator = self.prepare_content_change()
122
153
        self.assertEqual([('modify text', 'foo-id')],
123
154
                         list(creator.iter_shelvable()))
124
155
        creator.shelve_lines('foo-id', ['a\n', 'c\n'])
126
157
        self.assertFileEqual('a\nc\n', 'foo')
127
158
        self.assertShelvedFileEqual('b\na\n', creator, 'foo-id')
128
159
 
 
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')
129
173
 
130
174
    def prepare_shelve_creation(self):
131
175
        tree = self.make_branch_and_tree('.')
310
354
        tree.add('foo', 'foo-id')
311
355
        tree.commit('Added file and directory')
312
356
        os.unlink('tree/foo')
 
357
        tree.lock_tree_write()
 
358
        self.addCleanup(tree.unlock)
313
359
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
314
360
        self.addCleanup(creator.finalize)
315
361
        self.assertEqual([('delete file', 'foo-id', 'file', 'foo')],
325
371
        tree.commit('Added file and directory')
326
372
        os.unlink('tree/foo')
327
373
        os.mkdir('tree/foo')
 
374
        tree.lock_tree_write()
 
375
        self.addCleanup(tree.unlock)
328
376
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
329
377
        self.addCleanup(creator.finalize)
330
378
        self.assertEqual([('change kind', 'foo-id', 'file', 'directory',
352
400
 
353
401
    def test_shelve_change_unknown_change(self):
354
402
        tree = self.make_branch_and_tree('tree')
 
403
        tree.lock_tree_write()
 
404
        self.addCleanup(tree.unlock)
355
405
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
356
406
        self.addCleanup(creator.finalize)
357
407
        e = self.assertRaises(ValueError, creator.shelve_change, ('unknown',))
363
413
        tree.add('foo', 'foo-id')
364
414
        tree.commit('Added file and directory')
365
415
        tree.unversion(['foo-id'])
 
416
        tree.lock_tree_write()
 
417
        self.addCleanup(tree.unlock)
366
418
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
367
419
        self.addCleanup(creator.finalize)
368
420
        self.assertEqual([('delete file', 'foo-id', 'file', 'foo')],
373
425
 
374
426
    def test_shelve_serialization(self):
375
427
        tree = self.make_branch_and_tree('.')
 
428
        tree.lock_tree_write()
 
429
        self.addCleanup(tree.unlock)
376
430
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
377
431
        self.addCleanup(creator.finalize)
378
432
        shelf_file = open('shelf', 'wb')
387
441
        tree = self.make_branch_and_tree('tree')
388
442
        self.build_tree(['tree/foo'])
389
443
        tree.add('foo', 'foo-id')
 
444
        tree.lock_tree_write()
 
445
        self.addCleanup(tree.unlock)
390
446
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
391
447
        self.addCleanup(creator.finalize)
392
448
        list(creator.iter_shelvable())
411
467
 
412
468
    def test_shelve_unversioned(self):
413
469
        tree = self.make_branch_and_tree('tree')
414
 
        self.assertRaises(errors.PathsNotVersionedError,
415
 
                          shelf.ShelfCreator, tree, tree.basis_tree(), ['foo'])
 
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()
416
476
        # We should be able to lock/unlock the tree if ShelfCreator cleaned
417
477
        # after itself.
418
478
        wt = workingtree.WorkingTree.open('tree')
420
480
        wt.unlock()
421
481
        # And a second tentative should raise the same error (no
422
482
        # limbo/pending_deletion leftovers).
423
 
        self.assertRaises(errors.PathsNotVersionedError,
424
 
                          shelf.ShelfCreator, tree, tree.basis_tree(), ['foo'])
 
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()))
425
507
 
426
508
 
427
509
class TestUnshelver(tests.TestCaseWithTransport):
444
526
            shelf_file.seek(0)
445
527
            unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
446
528
            unshelver.make_merger().do_merge()
 
529
            self.addCleanup(unshelver.finalize)
447
530
            self.assertFileEqual('bar', 'tree/foo')
448
531
        finally:
449
532
            shelf_file.close()
467
550
        self.build_tree_contents([('tree/foo', 'z\na\nb\nc\n')])
468
551
        shelf_file.seek(0)
469
552
        unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
 
553
        self.addCleanup(unshelver.finalize)
470
554
        unshelver.make_merger().do_merge()
471
555
        self.assertFileEqual('z\na\nb\nd\n', 'tree/foo')
472
556
 
495
579
        self.assertFileEqual('baz', 'tree/foo/bar')
496
580
        shelf_file.seek(0)
497
581
        unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
 
582
        self.addCleanup(unshelver.finalize)
498
583
        unshelver.make_merger().do_merge()
499
584
        self.assertFalse('foo-id' in tree)
500
585
        self.assertFalse('bar-id' in tree)
525
610
        shelf_file = open('shelf', 'rb')
526
611
        self.addCleanup(shelf_file.close)
527
612
        unshelver = shelf.Unshelver.from_tree_and_shelf(tree, shelf_file)
 
613
        unshelver.finalize()
528
614
 
529
615
    def test_corrupt_shelf(self):
530
616
        tree = self.make_branch_and_tree('.')
639
725
        shelf_id = shelf_manager.shelve_changes(creator)
640
726
        self.failIfExists('tree/foo')
641
727
        unshelver = shelf_manager.get_unshelver(shelf_id)
 
728
        self.addCleanup(unshelver.finalize)
642
729
        unshelver.make_merger().do_merge()
643
730
        self.assertFileEqual('bar', 'tree/foo')
644
731
 
645
732
    def test_get_metadata(self):
646
733
        tree = self.make_branch_and_tree('.')
 
734
        tree.lock_tree_write()
 
735
        self.addCleanup(tree.unlock)
647
736
        creator = shelf.ShelfCreator(tree, tree.basis_tree())
 
737
        self.addCleanup(creator.finalize)
648
738
        shelf_manager = tree.get_shelf_manager()
649
739
        shelf_id = shelf_manager.shelve_changes(creator, 'foo')
650
740
        metadata = shelf_manager.get_metadata(shelf_id)