/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
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
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
62
    def capture_pre_commit_hook(self, local, master, old_revno, old_revid,
63
                                new_revno, new_revid, deleted, added, tree):
64
        self.hook_calls.append(('pre_commit', old_revno, old_revid,
65
                                new_revno, new_revid, deleted, added))
66
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
67
    def test_post_commit_to_origin(self):
68
        tree = self.make_branch_and_memory_tree('branch')
69
        Branch.hooks.install_hook('post_commit',
70
            self.capture_post_commit_hook)
71
        tree.lock_write()
72
        tree.add('')
73
        revid = tree.commit('a revision')
74
        # should have had one notification, from origin, and
75
        # have the branch locked at notification time.
76
        self.assertEqual([
77
            ('post_commit', None, tree.branch.base, 0, NULL_REVISION, 1, revid,
78
             None, True)
79
            ],
80
            self.hook_calls)
81
        tree.unlock()
82
83
    def test_post_commit_bound(self):
84
        master = self.make_branch('master')
85
        tree = self.make_branch_and_memory_tree('local')
86
        try:
87
            tree.branch.bind(master)
88
        except errors.UpgradeRequired:
89
            # cant bind this format, the test is irrelevant.
90
            return
91
        Branch.hooks.install_hook('post_commit',
92
            self.capture_post_commit_hook)
93
        tree.lock_write()
94
        tree.add('')
95
        revid = tree.commit('a revision')
96
        # with a bound branch, local is set.
97
        self.assertEqual([
98
            ('post_commit', tree.branch.base, master.base, 0, NULL_REVISION,
99
             1, revid, True, True)
100
            ],
101
            self.hook_calls)
102
        tree.unlock()
103
104
    def test_post_commit_not_to_origin(self):
105
        tree = self.make_branch_and_memory_tree('branch')
106
        tree.lock_write()
107
        tree.add('')
108
        revid = tree.commit('first revision')
109
        Branch.hooks.install_hook('post_commit',
110
            self.capture_post_commit_hook)
111
        revid2 = tree.commit('second revision')
112
        # having committed from up the branch, we should get the
113
        # before and after revnos and revids correctly.
114
        self.assertEqual([
115
            ('post_commit', None, tree.branch.base, 1, revid, 2, revid2,
116
             None, True)
117
            ],
118
            self.hook_calls)
119
        tree.unlock()
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
120
    
121
    def test_pre_commit_passes(self):
122
        tree = self.make_branch_and_memory_tree('branch')
123
        tree.lock_write()
124
        tree.add('')
125
        Branch.hooks.install_hook("pre_commit", self.capture_pre_commit_hook)
126
        revid1 = tree.commit('first revision')
127
        revid2 = tree.commit('second revision')
128
        self.assertEqual([
129
            ('pre_commit', 0, NULL_REVISION, 1, revid1, [], []),
130
            ('pre_commit', 1, revid1, 2, revid2, [], [])
131
            ],
132
            self.hook_calls)
133
        tree.unlock()
134
135
    def test_pre_commit_fails(self):
136
        tree = self.make_branch_and_memory_tree('branch')
137
        tree.lock_write()
138
        tree.add('')
139
        class PreCommitException(Exception): pass
140
        def hook_func(_1, _2, _3, _4, _5, new_revid, _7, _8, _9):
141
            raise PreCommitException(new_revid)
142
        Branch.hooks.install_hook("pre_commit", self.capture_pre_commit_hook)
143
        Branch.hooks.install_hook("pre_commit", hook_func)
144
        revids = [None, None, None]
145
        try:
146
            tree.commit('message')
147
        except PreCommitException, e:
148
            revids[0] = e.message
149
        Branch.hooks["pre_commit"] = []
150
        Branch.hooks.install_hook("pre_commit", self.capture_pre_commit_hook)
151
        for i in range(1, 3):
152
            revids[i] = tree.commit('third revision')
153
        self.assertEqual([
154
            ('pre_commit', 0, NULL_REVISION, 1, revids[0], [], []),
155
            ('pre_commit', 0, NULL_REVISION, 1, revids[1], [], []),
156
            ('pre_commit', 1, revids[1], 2, revids[2], [], [])
157
            ],
158
            self.hook_calls)
159
        tree.unlock()
160
    
161
    def test_pre_commit_paths(self):
162
        tree = self.make_branch_and_memory_tree('branch')
163
        tree.lock_write()
164
        tree.add('')
165
        tree.add('file', 'bang')
166
        tree.put_file_bytes_non_atomic('bang', 'die')
167
        tree.mkdir('dir', 'dirid')
168
        tree.add('dir/file', 'swoosh')
169
        tree.put_file_bytes_non_atomic('swoosh', 'swaash')
170
        Branch.hooks.install_hook("pre_commit", self.capture_pre_commit_hook)
171
        rev1 = tree.commit('first revision')
172
        tree.unversion(['dirid'])
173
        rev2 = tree.commit('second revision')
174
        self.assertEqual([
175
            ('pre_commit', 0, NULL_REVISION, 1, rev1, [], ['dir', 'dir/file', 'file']),
176
            ('pre_commit', 1, rev1, 2, rev2, ['dir', 'dir/file'], [])
177
            ],
178
            self.hook_calls)
179
        tree.unlock()