~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_commit.py

  • Committer: John Arbash Meinel
  • Date: 2006-11-10 15:38:16 UTC
  • mto: This revision was merged to the branch mainline in revision 2129.
  • Revision ID: john@arbash-meinel.com-20061110153816-46acf76fc86a512b
use try/finally to clean up a nested progress bar during weave fetching

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
from bzrlib import (
22
22
    errors,
23
23
    lockdir,
24
 
    osutils,
25
 
    tests,
26
24
    )
27
25
from bzrlib.branch import Branch
28
26
from bzrlib.bzrdir import BzrDir, BzrDirMetaFormat1
223
221
        wt.move(['hello'], 'a')
224
222
        r2 = 'test@rev-2'
225
223
        wt.commit('two', rev_id=r2, allow_pointless=False)
226
 
        wt.lock_read()
227
 
        try:
228
 
            self.check_inventory_shape(wt.read_working_inventory(),
229
 
                                       ['a/', 'a/hello', 'b/'])
230
 
        finally:
231
 
            wt.unlock()
 
224
        self.check_inventory_shape(wt.read_working_inventory(),
 
225
                                   ['a', 'a/hello', 'b'])
232
226
 
233
227
        wt.move(['b'], 'a')
234
228
        r3 = 'test@rev-3'
235
229
        wt.commit('three', rev_id=r3, allow_pointless=False)
236
 
        wt.lock_read()
237
 
        try:
238
 
            self.check_inventory_shape(wt.read_working_inventory(),
239
 
                                       ['a/', 'a/hello', 'a/b/'])
240
 
            self.check_inventory_shape(b.repository.get_revision_inventory(r3),
241
 
                                       ['a/', 'a/hello', 'a/b/'])
242
 
        finally:
243
 
            wt.unlock()
 
230
        self.check_inventory_shape(wt.read_working_inventory(),
 
231
                                   ['a', 'a/hello', 'a/b'])
 
232
        self.check_inventory_shape(b.repository.get_revision_inventory(r3),
 
233
                                   ['a', 'a/hello', 'a/b'])
244
234
 
245
235
        wt.move(['a/hello'], 'a/b')
246
236
        r4 = 'test@rev-4'
247
237
        wt.commit('four', rev_id=r4, allow_pointless=False)
248
 
        wt.lock_read()
249
 
        try:
250
 
            self.check_inventory_shape(wt.read_working_inventory(),
251
 
                                       ['a/', 'a/b/hello', 'a/b/'])
252
 
        finally:
253
 
            wt.unlock()
 
238
        self.check_inventory_shape(wt.read_working_inventory(),
 
239
                                   ['a', 'a/b/hello', 'a/b'])
254
240
 
255
241
        inv = b.repository.get_revision_inventory(r4)
256
242
        eq(inv['hello-id'].revision, r4)
257
243
        eq(inv['a-id'].revision, r1)
258
244
        eq(inv['b-id'].revision, r3)
259
 
 
 
245
        
260
246
    def test_removed_commit(self):
261
247
        """Commit with a removed file"""
262
248
        wt = self.make_branch_and_tree('.')
357
343
                                                      allow_pointless=True,
358
344
                                                      rev_id='B',
359
345
                                                      working_tree=wt)
360
 
            def sign(text):
361
 
                return bzrlib.gpg.LoopbackGPGStrategy(None).sign(text)
362
 
            self.assertEqual(sign(Testament.from_revision(branch.repository,
363
 
                             'B').as_short_text()),
 
346
            self.assertEqual(Testament.from_revision(branch.repository,
 
347
                             'B').as_short_text(),
364
348
                             branch.repository.get_signature_text('B'))
365
349
        finally:
366
350
            bzrlib.gpg.GPGStrategy = oldstrategy
525
509
            ('change', 'added', 'newdir'),
526
510
            ('change', 'added', 'newfile'),
527
511
            ('renamed', 'renamed', 'dirtorename', 'renameddir'),
528
 
            ('renamed', 'renamed', 'filetorename', 'renamedfile'),
529
512
            ('renamed', 'renamed', 'dirtoreparent', 'renameddir/reparenteddir'),
530
513
            ('renamed', 'renamed', 'filetoreparent', 'renameddir/reparentedfile'),
 
514
            ('renamed', 'renamed', 'filetorename', 'renamedfile'),
531
515
            ('deleted', 'dirtoremove'),
532
516
            ('deleted', 'filetoremove'),
533
517
            ],
541
525
        tree.commit('added a, b')
542
526
        tree.remove(['a', 'b'])
543
527
        tree.commit('removed a', specific_files='a')
544
 
        basis = tree.basis_tree()
545
 
        tree.lock_read()
546
 
        try:
547
 
            self.assertIs(None, basis.path2id('a'))
548
 
            self.assertFalse(basis.path2id('b') is None)
549
 
        finally:
550
 
            tree.unlock()
 
528
        basis = tree.basis_tree().inventory
 
529
        self.assertIs(None, basis.path2id('a'))
 
530
        self.assertFalse(basis.path2id('b') is None)
551
531
 
552
532
    def test_commit_saves_1ms_timestamp(self):
553
533
        """Passing in a timestamp is saved with 1ms resolution"""
572
552
        timestamp_1ms = round(timestamp, 3)
573
553
        self.assertEqual(timestamp_1ms, timestamp)
574
554
 
575
 
    def assertBasisTreeKind(self, kind, tree, file_id):
576
 
        basis = tree.basis_tree()
577
 
        basis.lock_read()
578
 
        try:
579
 
            self.assertEqual(kind, basis.kind(file_id))
580
 
        finally:
581
 
            basis.unlock()
582
 
 
583
 
    def test_commit_kind_changes(self):
584
 
        if not osutils.has_symlinks():
585
 
            raise tests.TestSkipped('Test requires symlink support')
586
 
        tree = self.make_branch_and_tree('.')
587
 
        os.symlink('target', 'name')
588
 
        tree.add('name', 'a-file-id')
589
 
        tree.commit('Added a symlink')
590
 
        self.assertBasisTreeKind('symlink', tree, 'a-file-id')
591
 
 
592
 
        os.unlink('name')
593
 
        self.build_tree(['name'])
594
 
        tree.commit('Changed symlink to file')
595
 
        self.assertBasisTreeKind('file', tree, 'a-file-id')
596
 
 
597
 
        os.unlink('name')
598
 
        os.symlink('target', 'name')
599
 
        tree.commit('file to symlink')
600
 
        self.assertBasisTreeKind('symlink', tree, 'a-file-id')
601
 
 
602
 
        os.unlink('name')
603
 
        os.mkdir('name')
604
 
        tree.commit('symlink to directory')
605
 
        self.assertBasisTreeKind('directory', tree, 'a-file-id')
606
 
 
607
 
        os.rmdir('name')
608
 
        os.symlink('target', 'name')
609
 
        tree.commit('directory to symlink')
610
 
        self.assertBasisTreeKind('symlink', tree, 'a-file-id')
611
 
 
612
 
        # prepare for directory <-> file tests
613
 
        os.unlink('name')
614
 
        os.mkdir('name')
615
 
        tree.commit('symlink to directory')
616
 
        self.assertBasisTreeKind('directory', tree, 'a-file-id')
617
 
 
618
 
        os.rmdir('name')
619
 
        self.build_tree(['name'])
620
 
        tree.commit('Changed directory to file')
621
 
        self.assertBasisTreeKind('file', tree, 'a-file-id')
622
 
 
623
 
        os.unlink('name')
624
 
        os.mkdir('name')
625
 
        tree.commit('file to directory')
626
 
        self.assertBasisTreeKind('directory', tree, 'a-file-id')
627
 
 
628
555
    def test_commit_unversioned_specified(self):
629
556
        """Commit should raise if specified files isn't in basis or worktree"""
630
557
        tree = self.make_branch_and_tree('.')
631
558
        self.assertRaises(errors.PathsNotVersionedError, tree.commit, 
632
559
                          'message', specific_files=['bogus'])
633
 
 
634
 
    class Callback(object):
635
 
        
636
 
        def __init__(self, message, testcase):
637
 
            self.called = False
638
 
            self.message = message
639
 
            self.testcase = testcase
640
 
 
641
 
        def __call__(self, commit_obj):
642
 
            self.called = True
643
 
            self.testcase.assertTrue(isinstance(commit_obj, Commit))
644
 
            return self.message
645
 
 
646
 
    def test_commit_callback(self):
647
 
        """Commit should invoke a callback to get the message"""
648
 
 
649
 
        tree = self.make_branch_and_tree('.')
650
 
        try:
651
 
            tree.commit()
652
 
        except Exception, e:
653
 
            self.assertTrue(isinstance(e, BzrError))
654
 
            self.assertEqual('The message or message_callback keyword'
655
 
                             ' parameter is required for commit().', str(e))
656
 
        else:
657
 
            self.fail('exception not raised')
658
 
        cb = self.Callback(u'commit 1', self)
659
 
        tree.commit(message_callback=cb)
660
 
        self.assertTrue(cb.called)
661
 
        repository = tree.branch.repository
662
 
        message = repository.get_revision(tree.last_revision()).message
663
 
        self.assertEqual('commit 1', message)
664
 
 
665
 
    def test_no_callback_pointless(self):
666
 
        """Callback should not be invoked for pointless commit"""
667
 
        tree = self.make_branch_and_tree('.')
668
 
        cb = self.Callback(u'commit 2', self)
669
 
        self.assertRaises(PointlessCommit, tree.commit, message_callback=cb, 
670
 
                          allow_pointless=False)
671
 
        self.assertFalse(cb.called)
672
 
 
673
 
    def test_no_callback_netfailure(self):
674
 
        """Callback should not be invoked if connectivity fails"""
675
 
        tree = self.make_branch_and_tree('.')
676
 
        cb = self.Callback(u'commit 2', self)
677
 
        repository = tree.branch.repository
678
 
        # simulate network failure
679
 
        def raise_(self, arg, arg2):
680
 
            raise errors.NoSuchFile('foo')
681
 
        repository.add_inventory = raise_
682
 
        self.assertRaises(errors.NoSuchFile, tree.commit, message_callback=cb)
683
 
        self.assertFalse(cb.called)
684
 
 
685
 
    def test_selected_file_merge_commit(self):
686
 
        """Ensure the correct error is raised"""
687
 
        tree = self.make_branch_and_tree('foo')
688
 
        # pending merge would turn into a left parent
689
 
        tree.commit('commit 1')
690
 
        tree.add_parent_tree_id('example')
691
 
        self.build_tree(['foo/bar', 'foo/baz'])
692
 
        tree.add(['bar', 'baz'])
693
 
        err = self.assertRaises(errors.CannotCommitSelectedFileMerge,
694
 
            tree.commit, 'commit 2', specific_files=['bar', 'baz'])
695
 
        self.assertEqual(['bar', 'baz'], err.files)
696
 
        self.assertEqual('Selected-file commit of merges is not supported'
697
 
                         ' yet: files bar, baz', str(err))
698
 
 
699
 
    def test_commit_ordering(self):
700
 
        """Test of corner-case commit ordering error"""
701
 
        tree = self.make_branch_and_tree('.')
702
 
        self.build_tree(['a/', 'a/z/', 'a/c/', 'a/z/x', 'a/z/y'])
703
 
        tree.add(['a/', 'a/z/', 'a/c/', 'a/z/x', 'a/z/y'])
704
 
        tree.commit('setup')
705
 
        self.build_tree(['a/c/d/'])
706
 
        tree.add('a/c/d')
707
 
        tree.rename_one('a/z/x', 'a/c/d/x')
708
 
        tree.commit('test', specific_files=['a/z/y'])