~bzr-pqm/bzr/bzr.dev

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
2659.3.9 by NamNguyen
branch.py:
24
from bzrlib.delta import TreeDelta
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
25
26
27
class TestCommit(TestCaseWithBranch):
28
29
    def test_commit_nicks(self):
30
        """Nicknames are committed to the revision"""
31
        get_transport(self.get_url()).mkdir('bzr.dev')
32
        wt = self.make_branch_and_tree('bzr.dev')
33
        branch = wt.branch
34
        branch.nick = "My happy branch"
35
        wt.commit('My commit respect da nick.')
36
        committed = branch.repository.get_revision(branch.last_revision())
37
        self.assertEqual(committed.properties["branch-nick"], 
38
                         "My happy branch")
39
40
41
class TestCommitHook(TestCaseWithBranch):
42
43
    def setUp(self):
44
        self.hook_calls = []
45
        TestCaseWithBranch.setUp(self)
46
47
    def capture_post_commit_hook(self, local, master, old_revno,
48
        old_revid, new_revno, new_revid):
49
        """Capture post commit hook calls to self.hook_calls.
50
        
51
        The call is logged, as is some state of the two branches.
52
        """
53
        if local:
54
            local_locked = local.is_locked()
55
            local_base = local.base
56
        else:
57
            local_locked = None
58
            local_base = None
59
        self.hook_calls.append(
60
            ('post_commit', local_base, master.base, old_revno, old_revid,
61
             new_revno, new_revid, local_locked, master.is_locked()))
62
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
63
    def capture_pre_commit_hook(self, local, master, old_revno, old_revid,
2659.3.9 by NamNguyen
branch.py:
64
                                new_revno, new_revid,
65
                                tree_delta, future_tree):
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
66
        self.hook_calls.append(('pre_commit', old_revno, old_revid,
2659.3.9 by NamNguyen
branch.py:
67
                                new_revno, new_revid, tree_delta))
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
68
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
69
    def test_post_commit_to_origin(self):
70
        tree = self.make_branch_and_memory_tree('branch')
71
        Branch.hooks.install_hook('post_commit',
72
            self.capture_post_commit_hook)
73
        tree.lock_write()
74
        tree.add('')
75
        revid = tree.commit('a revision')
76
        # should have had one notification, from origin, and
77
        # have the branch locked at notification time.
78
        self.assertEqual([
79
            ('post_commit', None, tree.branch.base, 0, NULL_REVISION, 1, revid,
80
             None, True)
81
            ],
82
            self.hook_calls)
83
        tree.unlock()
84
85
    def test_post_commit_bound(self):
86
        master = self.make_branch('master')
87
        tree = self.make_branch_and_memory_tree('local')
88
        try:
89
            tree.branch.bind(master)
90
        except errors.UpgradeRequired:
91
            # cant bind this format, the test is irrelevant.
92
            return
93
        Branch.hooks.install_hook('post_commit',
94
            self.capture_post_commit_hook)
95
        tree.lock_write()
96
        tree.add('')
97
        revid = tree.commit('a revision')
98
        # with a bound branch, local is set.
99
        self.assertEqual([
100
            ('post_commit', tree.branch.base, master.base, 0, NULL_REVISION,
101
             1, revid, True, True)
102
            ],
103
            self.hook_calls)
104
        tree.unlock()
105
106
    def test_post_commit_not_to_origin(self):
107
        tree = self.make_branch_and_memory_tree('branch')
108
        tree.lock_write()
109
        tree.add('')
110
        revid = tree.commit('first revision')
111
        Branch.hooks.install_hook('post_commit',
112
            self.capture_post_commit_hook)
113
        revid2 = tree.commit('second revision')
114
        # having committed from up the branch, we should get the
115
        # before and after revnos and revids correctly.
116
        self.assertEqual([
117
            ('post_commit', None, tree.branch.base, 1, revid, 2, revid2,
118
             None, True)
119
            ],
120
            self.hook_calls)
121
        tree.unlock()
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
122
    
123
    def test_pre_commit_passes(self):
2659.3.9 by NamNguyen
branch.py:
124
        empty_delta = TreeDelta()
125
        root_delta = TreeDelta()
126
        root_delta.added = [('', '', 'directory')]
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
127
        tree = self.make_branch_and_memory_tree('branch')
128
        tree.lock_write()
2659.3.9 by NamNguyen
branch.py:
129
        tree.add('', '')
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
130
        Branch.hooks.install_hook("pre_commit", self.capture_pre_commit_hook)
131
        revid1 = tree.commit('first revision')
132
        revid2 = tree.commit('second revision')
133
        self.assertEqual([
2659.3.9 by NamNguyen
branch.py:
134
            ('pre_commit', 0, NULL_REVISION, 1, revid1, root_delta),
135
            ('pre_commit', 1, revid1, 2, revid2, empty_delta)
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
136
            ],
137
            self.hook_calls)
138
        tree.unlock()
139
140
    def test_pre_commit_fails(self):
2659.3.9 by NamNguyen
branch.py:
141
        empty_delta = TreeDelta()
142
        root_delta = TreeDelta()
143
        root_delta.added = [('', '', 'directory')]
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
144
        tree = self.make_branch_and_memory_tree('branch')
145
        tree.lock_write()
2659.3.9 by NamNguyen
branch.py:
146
        tree.add('', '')
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
147
        class PreCommitException(Exception): pass
2659.3.9 by NamNguyen
branch.py:
148
        def hook_func(local, master,
149
                      old_revno, old_revid, new_revno, new_revid,
150
                      tree_delta, future_tree):
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
151
            raise PreCommitException(new_revid)
152
        Branch.hooks.install_hook("pre_commit", self.capture_pre_commit_hook)
153
        Branch.hooks.install_hook("pre_commit", hook_func)
154
        revids = [None, None, None]
2659.3.9 by NamNguyen
branch.py:
155
        # this commit will raise an exception
156
        # so the commit is rolled back and revno unchanged
2659.3.3 by NamNguyen
Changed ``pre_commit`` hook signature.
157
        err = self.assertRaises(PreCommitException, tree.commit, 'message')
158
        # we have to record the revid to use in assertEqual later
2789.1.1 by Ian Clatworthy
(Nam Nguyen) Pre-commit hook
159
        revids[0] = str(err)
2659.3.3 by NamNguyen
Changed ``pre_commit`` hook signature.
160
        # unregister all pre_commit hooks
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
161
        Branch.hooks["pre_commit"] = []
2659.3.3 by NamNguyen
Changed ``pre_commit`` hook signature.
162
        # and re-register the capture hook
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
163
        Branch.hooks.install_hook("pre_commit", self.capture_pre_commit_hook)
2659.3.3 by NamNguyen
Changed ``pre_commit`` hook signature.
164
        # now these commits should go through
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
165
        for i in range(1, 3):
2659.3.3 by NamNguyen
Changed ``pre_commit`` hook signature.
166
            revids[i] = tree.commit('message')
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
167
        self.assertEqual([
2659.3.9 by NamNguyen
branch.py:
168
            ('pre_commit', 0, NULL_REVISION, 1, revids[0], root_delta),
169
            ('pre_commit', 0, NULL_REVISION, 1, revids[1], root_delta),
170
            ('pre_commit', 1, revids[1], 2, revids[2], empty_delta)
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
171
            ],
172
            self.hook_calls)
173
        tree.unlock()
2659.3.9 by NamNguyen
branch.py:
174
175
    def test_pre_commit_delta(self):
176
        # This tests the TreeDelta object passed to pre_commit hook.
177
        # This does not try to validate data correctness in the delta.
2659.3.6 by NamNguyen
branch_implementations/test_commit.py:
178
        self.build_tree(['rootfile', 'dir/', 'dir/subfile'])
179
        tree = self.make_branch_and_tree('.')
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
180
        tree.lock_write()
2659.3.9 by NamNguyen
branch.py:
181
        try:
182
            # setting up a playground
183
            tree.set_root_id('root_id')
184
            tree.add('rootfile', 'rootfile_id')
185
            tree.put_file_bytes_non_atomic('rootfile_id', 'abc')
186
            tree.add('dir', 'dir_id')
187
            tree.add('dir/subfile', 'dir_subfile_id')
188
            tree.mkdir('to_be_unversioned', 'to_be_unversioned_id')
189
            tree.put_file_bytes_non_atomic('dir_subfile_id', 'def')
190
            revid1 = tree.commit('first revision')
191
        finally:
192
            tree.unlock()
193
        
194
        tree.lock_write()
195
        try:
196
            # making changes
197
            tree.put_file_bytes_non_atomic('rootfile_id', 'jkl')
198
            tree.rename_one('dir/subfile', 'dir/subfile_renamed')
199
            tree.unversion(['to_be_unversioned_id'])
200
            tree.mkdir('added_dir', 'added_dir_id')
201
            # start to capture pre_commit delta
202
            Branch.hooks.install_hook("pre_commit", self.capture_pre_commit_hook)
203
            revid2 = tree.commit('second revision')
204
        finally:
205
            tree.unlock()
206
        
207
        expected_delta = TreeDelta()
208
        expected_delta.added = [('added_dir', 'added_dir_id', 'directory')]
209
        expected_delta.removed = [('to_be_unversioned',
210
                                   'to_be_unversioned_id', 'directory')]
211
        expected_delta.renamed = [('dir/subfile', 'dir/subfile_renamed',
212
                                   'dir_subfile_id', 'file', False, False)]
213
        expected_delta.modified=[('rootfile', 'rootfile_id', 'file', True,
214
                                  False)]
215
        self.assertEqual([('pre_commit', 1, revid1, 2, revid2,
2789.1.1 by Ian Clatworthy
(Nam Nguyen) Pre-commit hook
216
                           expected_delta)], self.hook_calls)