~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/branch_implementations/test_commit.py

Merge from bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""Tests for the contract of commit on branches."""
 
18
 
 
19
from bzrlib.branch import Branch
 
20
from bzrlib import errors
 
21
from bzrlib.tests.branch_implementations.test_branch import TestCaseWithBranch
 
22
from bzrlib.revision import NULL_REVISION
 
23
from bzrlib.transport import get_transport
 
24
 
 
25
 
 
26
class TestCommit(TestCaseWithBranch):
 
27
 
 
28
    def test_commit_nicks(self):
 
29
        """Nicknames are committed to the revision"""
 
30
        get_transport(self.get_url()).mkdir('bzr.dev')
 
31
        wt = self.make_branch_and_tree('bzr.dev')
 
32
        branch = wt.branch
 
33
        branch.nick = "My happy branch"
 
34
        wt.commit('My commit respect da nick.')
 
35
        committed = branch.repository.get_revision(branch.last_revision())
 
36
        self.assertEqual(committed.properties["branch-nick"], 
 
37
                         "My happy branch")
 
38
 
 
39
 
 
40
class TestCommitHook(TestCaseWithBranch):
 
41
 
 
42
    def setUp(self):
 
43
        self.hook_calls = []
 
44
        TestCaseWithBranch.setUp(self)
 
45
 
 
46
    def capture_post_commit_hook(self, local, master, old_revno,
 
47
        old_revid, new_revno, new_revid):
 
48
        """Capture post commit hook calls to self.hook_calls.
 
49
        
 
50
        The call is logged, as is some state of the two branches.
 
51
        """
 
52
        if local:
 
53
            local_locked = local.is_locked()
 
54
            local_base = local.base
 
55
        else:
 
56
            local_locked = None
 
57
            local_base = None
 
58
        self.hook_calls.append(
 
59
            ('post_commit', local_base, master.base, old_revno, old_revid,
 
60
             new_revno, new_revid, local_locked, master.is_locked()))
 
61
 
 
62
    def test_post_commit_to_origin(self):
 
63
        tree = self.make_branch_and_memory_tree('branch')
 
64
        Branch.hooks.install_hook('post_commit',
 
65
            self.capture_post_commit_hook)
 
66
        tree.lock_write()
 
67
        tree.add('')
 
68
        revid = tree.commit('a revision')
 
69
        # should have had one notification, from origin, and
 
70
        # have the branch locked at notification time.
 
71
        self.assertEqual([
 
72
            ('post_commit', None, tree.branch.base, 0, NULL_REVISION, 1, revid,
 
73
             None, True)
 
74
            ],
 
75
            self.hook_calls)
 
76
        tree.unlock()
 
77
 
 
78
    def test_post_commit_bound(self):
 
79
        master = self.make_branch('master')
 
80
        tree = self.make_branch_and_memory_tree('local')
 
81
        try:
 
82
            tree.branch.bind(master)
 
83
        except errors.UpgradeRequired:
 
84
            # cant bind this format, the test is irrelevant.
 
85
            return
 
86
        Branch.hooks.install_hook('post_commit',
 
87
            self.capture_post_commit_hook)
 
88
        tree.lock_write()
 
89
        tree.add('')
 
90
        revid = tree.commit('a revision')
 
91
        # with a bound branch, local is set.
 
92
        self.assertEqual([
 
93
            ('post_commit', tree.branch.base, master.base, 0, NULL_REVISION,
 
94
             1, revid, True, True)
 
95
            ],
 
96
            self.hook_calls)
 
97
        tree.unlock()
 
98
 
 
99
    def test_post_commit_not_to_origin(self):
 
100
        tree = self.make_branch_and_memory_tree('branch')
 
101
        tree.lock_write()
 
102
        tree.add('')
 
103
        revid = tree.commit('first revision')
 
104
        Branch.hooks.install_hook('post_commit',
 
105
            self.capture_post_commit_hook)
 
106
        revid2 = tree.commit('second revision')
 
107
        # having committed from up the branch, we should get the
 
108
        # before and after revnos and revids correctly.
 
109
        self.assertEqual([
 
110
            ('post_commit', None, tree.branch.base, 1, revid, 2, revid2,
 
111
             None, True)
 
112
            ],
 
113
            self.hook_calls)
 
114
        tree.unlock()