/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
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
2245.1.2 by Robert Collins
Remove the static DefaultHooks method from Branch, replacing it with a derived dict BranchHooks object, which is easier to use and provides a place to put the policy-checking add method discussed on list.
17
"""Tests that branch classes implement hook callouts correctly."""
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
18
19
from bzrlib.branch import Branch
3331.1.2 by James Henstridge
Add calls to set_last_revision_info hook to both BzrBranch and
20
from bzrlib.revision import NULL_REVISION
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
21
from bzrlib.tests import TestCaseWithMemoryTransport
22
23
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
24
class TestSetRevisionHistoryHook(TestCaseWithMemoryTransport):
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
25
26
    def setUp(self):
27
        self.hook_calls = []
28
        TestCaseWithMemoryTransport.setUp(self)
29
30
    def capture_set_rh_hook(self, branch, rev_history):
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
31
        """Capture post set-rh hook calls to self.hook_calls.
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
32
        
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
33
        The call is logged, as is some state of the branch.
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
34
        """
35
        self.hook_calls.append(
36
            ('set_rh', branch, rev_history, branch.is_locked()))
37
38
    def test_set_rh_empty_history(self):
39
        branch = self.make_branch('source')
3256.2.15 by Daniel Watkins
Updated uses of Hooks.install_hook to Hooks.install_named_hook in tests.branch_implementation.test_hooks.
40
        Branch.hooks.install_named_hook('set_rh', self.capture_set_rh_hook,
41
                                        None)
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
42
        branch.set_revision_history([])
43
        self.assertEqual(self.hook_calls,
44
            [('set_rh', branch, [], True)])
45
46
    def test_set_rh_nonempty_history(self):
2230.3.20 by Aaron Bentley
Add hooking for set_revision_history
47
        tree = self.make_branch_and_memory_tree('source')
48
        tree.lock_write()
49
        tree.add('')
2696.3.7 by Martin Pool
Update hook test to cope with branches that can't set their last revision to one that's not present
50
        tree.commit('another commit', rev_id='f\xc2\xb5')
2230.3.20 by Aaron Bentley
Add hooking for set_revision_history
51
        tree.commit('empty commit', rev_id='foo')
52
        tree.unlock()
53
        branch = tree.branch
3256.2.15 by Daniel Watkins
Updated uses of Hooks.install_hook to Hooks.install_named_hook in tests.branch_implementation.test_hooks.
54
        Branch.hooks.install_named_hook('set_rh', self.capture_set_rh_hook,
55
                                        None)
2696.3.7 by Martin Pool
Update hook test to cope with branches that can't set their last revision to one that's not present
56
        # some branches require that their history be set to a revision in the
57
        # repository
2309.4.10 by John Arbash Meinel
(fixed) Fix the last few tests that were explicitly passing around unicode ids
58
        branch.set_revision_history(['f\xc2\xb5'])
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
59
        self.assertEqual(self.hook_calls,
2309.4.10 by John Arbash Meinel
(fixed) Fix the last few tests that were explicitly passing around unicode ids
60
            [('set_rh', branch, ['f\xc2\xb5'], True)])
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
61
62
    def test_set_rh_branch_is_locked(self):
63
        branch = self.make_branch('source')
3256.2.15 by Daniel Watkins
Updated uses of Hooks.install_hook to Hooks.install_named_hook in tests.branch_implementation.test_hooks.
64
        Branch.hooks.install_named_hook('set_rh', self.capture_set_rh_hook,
65
                                        None)
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
66
        branch.set_revision_history([])
67
        self.assertEqual(self.hook_calls,
68
            [('set_rh', branch, [], True)])
69
70
    def test_set_rh_calls_all_hooks_no_errors(self):
71
        branch = self.make_branch('source')
3256.2.15 by Daniel Watkins
Updated uses of Hooks.install_hook to Hooks.install_named_hook in tests.branch_implementation.test_hooks.
72
        Branch.hooks.install_named_hook('set_rh', self.capture_set_rh_hook,
73
                                        None)
74
        Branch.hooks.install_named_hook('set_rh', self.capture_set_rh_hook,
75
                                        None)
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
76
        branch.set_revision_history([])
77
        self.assertEqual(self.hook_calls,
78
            [('set_rh', branch, [], True),
79
             ('set_rh', branch, [], True),
80
            ])
3331.1.2 by James Henstridge
Add calls to set_last_revision_info hook to both BzrBranch and
81
82
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
83
class TestPreChangeBranchTip(TestCaseWithMemoryTransport):
3517.2.2 by Andrew Bennetts
Add test for a pre_change_branch_tip hook rejecting a change.
84
    """Tests for pre_change_branch_tip hook."""
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
85
86
    def setUp(self):
87
        self.hook_calls = []
88
        TestCaseWithMemoryTransport.setUp(self)
89
90
    def capture_pre_change_branch_tip_hook(self, params):
91
        """Capture post_change_branch_tip hook calls to self.hook_calls.
92
93
        The call is logged, as is some state of the branch.
94
        """
95
        self.hook_calls.append((params, params.branch.is_locked()))
3517.2.2 by Andrew Bennetts
Add test for a pre_change_branch_tip hook rejecting a change.
96
        # The branch hasn't changed -- yet.
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
97
        self.assertEquals(params.branch.last_revision_info(),
98
                          (params.old_revno, params.old_revid))
99
3517.2.2 by Andrew Bennetts
Add test for a pre_change_branch_tip hook rejecting a change.
100
    def make_branch_with_revision_ids(self, *revision_ids):
101
        """Makes a branch with the given commits."""
102
        tree = self.make_branch_and_memory_tree('source')
103
        tree.lock_write()
104
        tree.add('')
105
        for revision_id in revision_ids:
106
            tree.commit('Message of ' + revision_id, rev_id=revision_id)
107
        tree.unlock()
108
        branch = tree.branch
109
        return branch
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
110
3517.2.2 by Andrew Bennetts
Add test for a pre_change_branch_tip hook rejecting a change.
111
    def test_reject_by_hook(self):
112
        """If a hook raises an exception, the change take effect."""
113
        branch = self.make_branch_with_revision_ids(
114
            'one-\xc2\xb5', 'two-\xc2\xb5')
115
        class PearShapedError(Exception):
116
            pass
117
        def hook_that_raises(params):
118
            raise PearShapedError()
119
        Branch.hooks.install_named_hook(
120
            'pre_change_branch_tip', hook_that_raises, None)
121
        self.assertRaises(
122
            PearShapedError, branch.set_last_revision_info, 0, NULL_REVISION)
123
        # The revision info is unchanged.
124
        self.assertEqual((2, 'two-\xc2\xb5'), branch.last_revision_info())
125
        
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
126
    # Tests from TestPostChangeBranchTip:
127
    def test_pre_change_branch_tip_empty_history(self):
128
        branch = self.make_branch('source')
129
        Branch.hooks.install_named_hook(
130
            'pre_change_branch_tip',
131
            self.capture_pre_change_branch_tip_hook,
132
            None)
133
        branch.set_last_revision_info(0, NULL_REVISION)
134
        self.assertEqual(len(self.hook_calls), 1)
135
        self.assertEqual(self.hook_calls[0][0].branch, branch)
136
        self.assertEqual(self.hook_calls[0][0].old_revid, NULL_REVISION)
137
        self.assertEqual(self.hook_calls[0][0].old_revno, 0)
138
        self.assertEqual(self.hook_calls[0][0].new_revid, NULL_REVISION)
139
        self.assertEqual(self.hook_calls[0][0].new_revno, 0)
140
141
    def test_pre_change_branch_tip_nonempty_history(self):
3517.2.2 by Andrew Bennetts
Add test for a pre_change_branch_tip hook rejecting a change.
142
        branch = self.make_branch_with_revision_ids(
143
            'one-\xc2\xb5', 'two-\xc2\xb5')
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
144
        Branch.hooks.install_named_hook(
145
            'pre_change_branch_tip',
146
            self.capture_pre_change_branch_tip_hook,
147
            None)
148
        # some branches require that their history be set to a revision in the
149
        # repository
3517.2.2 by Andrew Bennetts
Add test for a pre_change_branch_tip hook rejecting a change.
150
        branch.set_last_revision_info(1, 'one-\xc2\xb5')
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
151
        self.assertEqual(len(self.hook_calls), 1)
152
        self.assertEqual(self.hook_calls[0][0].branch, branch)
3517.2.2 by Andrew Bennetts
Add test for a pre_change_branch_tip hook rejecting a change.
153
        self.assertEqual(self.hook_calls[0][0].old_revid, 'two-\xc2\xb5')
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
154
        self.assertEqual(self.hook_calls[0][0].old_revno, 2)
3517.2.2 by Andrew Bennetts
Add test for a pre_change_branch_tip hook rejecting a change.
155
        self.assertEqual(self.hook_calls[0][0].new_revid, 'one-\xc2\xb5')
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
156
        self.assertEqual(self.hook_calls[0][0].new_revno, 1)
157
158
    def test_pre_change_branch_tip_branch_is_locked(self):
159
        branch = self.make_branch('source')
160
        Branch.hooks.install_named_hook(
161
            'pre_change_branch_tip',
162
            self.capture_pre_change_branch_tip_hook,
163
            None)
164
        branch.set_last_revision_info(0, NULL_REVISION)
165
        self.assertEqual(len(self.hook_calls), 1)
166
        self.assertEqual(self.hook_calls[0][0].branch, branch)
167
        self.assertEqual(self.hook_calls[0][1], True)
168
169
    def test_pre_change_branch_tip_calls_all_hooks_no_errors(self):
170
        branch = self.make_branch('source')
171
        Branch.hooks.install_named_hook(
172
            'pre_change_branch_tip',
173
            self.capture_pre_change_branch_tip_hook,
174
            None)
175
        Branch.hooks.install_named_hook(
176
            'pre_change_branch_tip',
177
            self.capture_pre_change_branch_tip_hook,
178
            None)
179
        branch.set_last_revision_info(0, NULL_REVISION)
180
        self.assertEqual(len(self.hook_calls), 2)
181
        self.assertEqual(self.hook_calls[0][0].branch, branch)
182
        self.assertEqual(self.hook_calls[1][0].branch, branch)
183
184
3331.1.4 by James Henstridge
Adjust my tests to pass with Ian's API.
185
class TestPostChangeBranchTip(TestCaseWithMemoryTransport):
3517.2.2 by Andrew Bennetts
Add test for a pre_change_branch_tip hook rejecting a change.
186
    """Tests for post_change_branch_tip hook."""
3331.1.2 by James Henstridge
Add calls to set_last_revision_info hook to both BzrBranch and
187
188
    def setUp(self):
189
        self.hook_calls = []
190
        TestCaseWithMemoryTransport.setUp(self)
191
3331.1.7 by James Henstridge
Make the branch a member of the ChangeBranchTipParams object.
192
    def capture_post_change_branch_tip_hook(self, params):
3331.1.4 by James Henstridge
Adjust my tests to pass with Ian's API.
193
        """Capture post_change_branch_tip hook calls to self.hook_calls.
3331.1.2 by James Henstridge
Add calls to set_last_revision_info hook to both BzrBranch and
194
195
        The call is logged, as is some state of the branch.
196
        """
3331.1.7 by James Henstridge
Make the branch a member of the ChangeBranchTipParams object.
197
        self.hook_calls.append((params, params.branch.is_locked()))
3331.1.12 by James Henstridge
make sure that last_revision_info() matches data in params
198
        self.assertEquals(params.branch.last_revision_info(),
199
                          (params.new_revno, params.new_revid))
3331.1.2 by James Henstridge
Add calls to set_last_revision_info hook to both BzrBranch and
200
3331.1.4 by James Henstridge
Adjust my tests to pass with Ian's API.
201
    def test_post_change_branch_tip_empty_history(self):
3331.1.2 by James Henstridge
Add calls to set_last_revision_info hook to both BzrBranch and
202
        branch = self.make_branch('source')
3256.2.26 by Daniel Watkins
Updated tests to use install_named_hook.
203
        Branch.hooks.install_named_hook(
204
            'post_change_branch_tip',
205
            self.capture_post_change_branch_tip_hook,
206
            None)
3331.1.2 by James Henstridge
Add calls to set_last_revision_info hook to both BzrBranch and
207
        branch.set_last_revision_info(0, NULL_REVISION)
3331.1.4 by James Henstridge
Adjust my tests to pass with Ian's API.
208
        self.assertEqual(len(self.hook_calls), 1)
3331.1.7 by James Henstridge
Make the branch a member of the ChangeBranchTipParams object.
209
        self.assertEqual(self.hook_calls[0][0].branch, branch)
210
        self.assertEqual(self.hook_calls[0][0].old_revid, NULL_REVISION)
211
        self.assertEqual(self.hook_calls[0][0].old_revno, 0)
212
        self.assertEqual(self.hook_calls[0][0].new_revid, NULL_REVISION)
213
        self.assertEqual(self.hook_calls[0][0].new_revno, 0)
3331.1.2 by James Henstridge
Add calls to set_last_revision_info hook to both BzrBranch and
214
3331.1.4 by James Henstridge
Adjust my tests to pass with Ian's API.
215
    def test_post_change_branch_tip_nonempty_history(self):
3331.1.2 by James Henstridge
Add calls to set_last_revision_info hook to both BzrBranch and
216
        tree = self.make_branch_and_memory_tree('source')
217
        tree.lock_write()
218
        tree.add('')
219
        tree.commit('another commit', rev_id='f\xc2\xb5')
220
        tree.commit('empty commit', rev_id='foo')
221
        tree.unlock()
222
        branch = tree.branch
3256.2.26 by Daniel Watkins
Updated tests to use install_named_hook.
223
        Branch.hooks.install_named_hook(
224
            'post_change_branch_tip',
225
            self.capture_post_change_branch_tip_hook,
226
            None)
3331.1.2 by James Henstridge
Add calls to set_last_revision_info hook to both BzrBranch and
227
        # some branches require that their history be set to a revision in the
228
        # repository
229
        branch.set_last_revision_info(1, 'f\xc2\xb5')
3331.1.4 by James Henstridge
Adjust my tests to pass with Ian's API.
230
        self.assertEqual(len(self.hook_calls), 1)
3331.1.7 by James Henstridge
Make the branch a member of the ChangeBranchTipParams object.
231
        self.assertEqual(self.hook_calls[0][0].branch, branch)
232
        self.assertEqual(self.hook_calls[0][0].old_revid, 'foo')
233
        self.assertEqual(self.hook_calls[0][0].old_revno, 2)
234
        self.assertEqual(self.hook_calls[0][0].new_revid, 'f\xc2\xb5')
235
        self.assertEqual(self.hook_calls[0][0].new_revno, 1)
3331.1.4 by James Henstridge
Adjust my tests to pass with Ian's API.
236
237
    def test_post_change_branch_tip_branch_is_locked(self):
238
        branch = self.make_branch('source')
3256.2.26 by Daniel Watkins
Updated tests to use install_named_hook.
239
        Branch.hooks.install_named_hook(
240
            'post_change_branch_tip',
241
            self.capture_post_change_branch_tip_hook,
242
            None)
3331.1.4 by James Henstridge
Adjust my tests to pass with Ian's API.
243
        branch.set_last_revision_info(0, NULL_REVISION)
244
        self.assertEqual(len(self.hook_calls), 1)
3331.1.7 by James Henstridge
Make the branch a member of the ChangeBranchTipParams object.
245
        self.assertEqual(self.hook_calls[0][0].branch, branch)
246
        self.assertEqual(self.hook_calls[0][1], True)
3331.1.4 by James Henstridge
Adjust my tests to pass with Ian's API.
247
248
    def test_post_change_branch_tip_calls_all_hooks_no_errors(self):
249
        branch = self.make_branch('source')
3256.2.26 by Daniel Watkins
Updated tests to use install_named_hook.
250
        Branch.hooks.install_named_hook(
251
            'post_change_branch_tip',
252
            self.capture_post_change_branch_tip_hook,
253
            None)
254
        Branch.hooks.install_named_hook(
255
            'post_change_branch_tip',
256
            self.capture_post_change_branch_tip_hook,
257
            None)
3331.1.4 by James Henstridge
Adjust my tests to pass with Ian's API.
258
        branch.set_last_revision_info(0, NULL_REVISION)
259
        self.assertEqual(len(self.hook_calls), 2)
3331.1.7 by James Henstridge
Make the branch a member of the ChangeBranchTipParams object.
260
        self.assertEqual(self.hook_calls[0][0].branch, branch)
261
        self.assertEqual(self.hook_calls[1][0].branch, branch)