~bzr-pqm/bzr/bzr.dev

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
3577.1.1 by Andrew Bennetts
Cherry-pick TipChangeRejected changes from pre-branch-tip-changed-hook loom.
19
from bzrlib.errors import HookFailed, TipChangeRejected
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
20
from bzrlib.branch import Branch, ChangeBranchTipParams
3331.1.2 by James Henstridge
Add calls to set_last_revision_info hook to both BzrBranch and
21
from bzrlib.revision import NULL_REVISION
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
22
from bzrlib.tests import TestCaseWithMemoryTransport
23
24
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
25
class TestSetRevisionHistoryHook(TestCaseWithMemoryTransport):
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
26
27
    def setUp(self):
28
        self.hook_calls = []
29
        TestCaseWithMemoryTransport.setUp(self)
30
31
    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
32
        """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
33
        
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
34
        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
35
        """
36
        self.hook_calls.append(
37
            ('set_rh', branch, rev_history, branch.is_locked()))
38
39
    def test_set_rh_empty_history(self):
40
        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.
41
        Branch.hooks.install_named_hook('set_rh', self.capture_set_rh_hook,
42
                                        None)
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
43
        branch.set_revision_history([])
44
        self.assertEqual(self.hook_calls,
45
            [('set_rh', branch, [], True)])
46
47
    def test_set_rh_nonempty_history(self):
2230.3.20 by Aaron Bentley
Add hooking for set_revision_history
48
        tree = self.make_branch_and_memory_tree('source')
49
        tree.lock_write()
50
        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
51
        tree.commit('another commit', rev_id='f\xc2\xb5')
2230.3.20 by Aaron Bentley
Add hooking for set_revision_history
52
        tree.commit('empty commit', rev_id='foo')
53
        tree.unlock()
54
        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.
55
        Branch.hooks.install_named_hook('set_rh', self.capture_set_rh_hook,
56
                                        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
57
        # some branches require that their history be set to a revision in the
58
        # repository
2309.4.10 by John Arbash Meinel
(fixed) Fix the last few tests that were explicitly passing around unicode ids
59
        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
60
        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
61
            [('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
62
63
    def test_set_rh_branch_is_locked(self):
64
        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.
65
        Branch.hooks.install_named_hook('set_rh', self.capture_set_rh_hook,
66
                                        None)
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
67
        branch.set_revision_history([])
68
        self.assertEqual(self.hook_calls,
69
            [('set_rh', branch, [], True)])
70
71
    def test_set_rh_calls_all_hooks_no_errors(self):
72
        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.
73
        Branch.hooks.install_named_hook('set_rh', self.capture_set_rh_hook,
74
                                        None)
75
        Branch.hooks.install_named_hook('set_rh', self.capture_set_rh_hook,
76
                                        None)
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
77
        branch.set_revision_history([])
78
        self.assertEqual(self.hook_calls,
79
            [('set_rh', branch, [], True),
80
             ('set_rh', branch, [], True),
81
            ])
3331.1.2 by James Henstridge
Add calls to set_last_revision_info hook to both BzrBranch and
82
83
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
84
class ChangeBranchTipTestCase(TestCaseWithMemoryTransport):
85
    """Base TestCase for testing pre/post_change_branch_tip hooks."""
86
87
    def install_logging_hook(self, prefix):
88
        """Add a hook that logs calls made to it.
89
        
90
        :returns: the list that the calls will be appended to.
91
        """
92
        hook_calls = []
93
        Branch.hooks.install_named_hook(
3577.1.1 by Andrew Bennetts
Cherry-pick TipChangeRejected changes from pre-branch-tip-changed-hook loom.
94
            prefix + '_change_branch_tip', hook_calls.append, None)
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
95
        return hook_calls
96
97
    def make_branch_with_revision_ids(self, *revision_ids):
98
        """Makes a branch with the given commits."""
99
        tree = self.make_branch_and_memory_tree('source')
100
        tree.lock_write()
101
        tree.add('')
102
        for revision_id in revision_ids:
3557.1.1 by John Arbash Meinel
Fix bug #247585: decode from utf8 to Unicode when giving a commit message
103
            tree.commit(u'Message of ' + revision_id.decode('utf8'),
104
                        rev_id=revision_id)
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
105
        tree.unlock()
106
        branch = tree.branch
107
        return branch
108
109
110
class TestPreChangeBranchTip(ChangeBranchTipTestCase):
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
111
    """Tests for pre_change_branch_tip hook.
112
    
113
    Most of these tests are very similar to the tests in
114
    TestPostChangeBranchTip.
115
    """
116
117
    def test_hook_runs_before_change(self):
118
        """The hook runs *before* the branch's last_revision_info has changed.
119
        """
120
        branch = self.make_branch_with_revision_ids('revid-one')
121
        def assertBranchAtRevision1(params):
122
            self.assertEquals(
123
                (1, 'revid-one'), params.branch.last_revision_info())
124
        Branch.hooks.install_named_hook(
125
            'pre_change_branch_tip', assertBranchAtRevision1, None)
126
        branch.set_last_revision_info(0, NULL_REVISION)
127
3577.1.1 by Andrew Bennetts
Cherry-pick TipChangeRejected changes from pre-branch-tip-changed-hook loom.
128
    def test_hook_failure_prevents_change(self):
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
129
        """If a hook raises an exception, the change does not take effect.
130
        
3577.1.1 by Andrew Bennetts
Cherry-pick TipChangeRejected changes from pre-branch-tip-changed-hook loom.
131
        Also, a HookFailed exception will be raised.
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
132
        """
3517.2.2 by Andrew Bennetts
Add test for a pre_change_branch_tip hook rejecting a change.
133
        branch = self.make_branch_with_revision_ids(
134
            'one-\xc2\xb5', 'two-\xc2\xb5')
135
        class PearShapedError(Exception):
136
            pass
137
        def hook_that_raises(params):
138
            raise PearShapedError()
139
        Branch.hooks.install_named_hook(
140
            'pre_change_branch_tip', hook_that_raises, None)
3577.1.1 by Andrew Bennetts
Cherry-pick TipChangeRejected changes from pre-branch-tip-changed-hook loom.
141
        hook_failed_exc = self.assertRaises(
142
            HookFailed, branch.set_last_revision_info, 0, NULL_REVISION)
143
        self.assertIsInstance(hook_failed_exc.exc_value, PearShapedError)
3517.2.2 by Andrew Bennetts
Add test for a pre_change_branch_tip hook rejecting a change.
144
        # The revision info is unchanged.
145
        self.assertEqual((2, 'two-\xc2\xb5'), branch.last_revision_info())
146
        
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
147
    def test_empty_history(self):
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
148
        branch = self.make_branch('source')
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
149
        hook_calls = self.install_logging_hook('pre')
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
150
        branch.set_last_revision_info(0, NULL_REVISION)
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
151
        expected_params = ChangeBranchTipParams(
152
            branch, 0, 0, NULL_REVISION, NULL_REVISION)
153
        self.assertEqual([expected_params], hook_calls)
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
154
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
155
    def test_nonempty_history(self):
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
156
        # some branches require that their history be set to a revision in the
157
        # repository, so we need to make a branch with non-empty history for
158
        # this test.
3517.2.2 by Andrew Bennetts
Add test for a pre_change_branch_tip hook rejecting a change.
159
        branch = self.make_branch_with_revision_ids(
160
            'one-\xc2\xb5', 'two-\xc2\xb5')
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
161
        hook_calls = self.install_logging_hook('pre')
3517.2.2 by Andrew Bennetts
Add test for a pre_change_branch_tip hook rejecting a change.
162
        branch.set_last_revision_info(1, 'one-\xc2\xb5')
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
163
        expected_params = ChangeBranchTipParams(
164
            branch, 2, 1, 'two-\xc2\xb5', 'one-\xc2\xb5')
165
        self.assertEqual([expected_params], hook_calls)
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
166
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
167
    def test_branch_is_locked(self):
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
168
        branch = self.make_branch('source')
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
169
        def assertBranchIsLocked(params):
170
            self.assertTrue(params.branch.is_locked())
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
171
        Branch.hooks.install_named_hook(
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
172
            'pre_change_branch_tip', assertBranchIsLocked, None)
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
173
        branch.set_last_revision_info(0, NULL_REVISION)
174
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
175
    def test_calls_all_hooks_no_errors(self):
176
        """If multiple hooks are registered, all are called (if none raise
177
        errors).
178
        """
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
179
        branch = self.make_branch('source')
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
180
        hook_calls_1 = self.install_logging_hook('pre')
181
        hook_calls_2 = self.install_logging_hook('pre')
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
182
        self.assertIsNot(hook_calls_1, hook_calls_2)
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
183
        branch.set_last_revision_info(0, NULL_REVISION)
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
184
        # Both hooks are called.
185
        self.assertEqual(len(hook_calls_1), 1)
186
        self.assertEqual(len(hook_calls_2), 1)
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
187
3577.1.1 by Andrew Bennetts
Cherry-pick TipChangeRejected changes from pre-branch-tip-changed-hook loom.
188
    def test_explicit_reject_by_hook(self):
189
        """If a hook raises TipChangeRejected, the change does not take effect.
190
        
191
        TipChangeRejected exceptions are propagated, not wrapped in HookFailed.
192
        """
193
        branch = self.make_branch_with_revision_ids(
194
            'one-\xc2\xb5', 'two-\xc2\xb5')
195
        def hook_that_rejects(params):
196
            raise TipChangeRejected('rejection message')
197
        Branch.hooks.install_named_hook(
198
            'pre_change_branch_tip', hook_that_rejects, None)
199
        self.assertRaises(
200
            TipChangeRejected, branch.set_last_revision_info, 0, NULL_REVISION)
201
        # The revision info is unchanged.
202
        self.assertEqual((2, 'two-\xc2\xb5'), branch.last_revision_info())
203
        
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
204
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
205
class TestPostChangeBranchTip(ChangeBranchTipTestCase):
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
206
    """Tests for post_change_branch_tip hook.
207
208
    Most of these tests are very similar to the tests in
209
    TestPostChangeBranchTip.
210
    """
211
212
    def test_hook_runs_after_change(self):
213
        """The hook runs *after* the branch's last_revision_info has changed.
214
        """
215
        branch = self.make_branch_with_revision_ids('revid-one')
216
        def assertBranchAtRevision1(params):
217
            self.assertEquals(
218
                (0, NULL_REVISION), params.branch.last_revision_info())
3256.2.26 by Daniel Watkins
Updated tests to use install_named_hook.
219
        Branch.hooks.install_named_hook(
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
220
            'post_change_branch_tip', assertBranchAtRevision1, None)
221
        branch.set_last_revision_info(0, NULL_REVISION)
222
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
223
    def test_empty_history(self):
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
224
        branch = self.make_branch('source')
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
225
        hook_calls = self.install_logging_hook('post')
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
226
        branch.set_last_revision_info(0, NULL_REVISION)
227
        expected_params = ChangeBranchTipParams(
228
            branch, 0, 0, NULL_REVISION, NULL_REVISION)
229
        self.assertEqual([expected_params], hook_calls)
230
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
231
    def test_nonempty_history(self):
3331.1.2 by James Henstridge
Add calls to set_last_revision_info hook to both BzrBranch and
232
        # some branches require that their history be set to a revision in the
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
233
        # repository, so we need to make a branch with non-empty history for
234
        # this test.
235
        branch = self.make_branch_with_revision_ids(
236
            'one-\xc2\xb5', 'two-\xc2\xb5')
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
237
        hook_calls = self.install_logging_hook('post')
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
238
        branch.set_last_revision_info(1, 'one-\xc2\xb5')
239
        expected_params = ChangeBranchTipParams(
240
            branch, 2, 1, 'two-\xc2\xb5', 'one-\xc2\xb5')
241
        self.assertEqual([expected_params], hook_calls)
242
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
243
    def test_branch_is_locked(self):
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
244
        """The branch passed to the hook is locked."""
245
        branch = self.make_branch('source')
246
        def assertBranchIsLocked(params):
247
            self.assertTrue(params.branch.is_locked())
248
        Branch.hooks.install_named_hook(
3517.2.4 by Andrew Bennetts
Fix typo.
249
            'post_change_branch_tip', assertBranchIsLocked, None)
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
250
        branch.set_last_revision_info(0, NULL_REVISION)
251
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
252
    def test_calls_all_hooks_no_errors(self):
253
        """If multiple hooks are registered, all are called (if none raise
254
        errors).
255
        """
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
256
        branch = self.make_branch('source')
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
257
        hook_calls_1 = self.install_logging_hook('post')
258
        hook_calls_2 = self.install_logging_hook('post')
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
259
        self.assertIsNot(hook_calls_1, hook_calls_2)
260
        branch.set_last_revision_info(0, NULL_REVISION)
261
        # Both hooks are called.
262
        self.assertEqual(len(hook_calls_1), 1)
263
        self.assertEqual(len(hook_calls_2), 1)
3577.1.1 by Andrew Bennetts
Cherry-pick TipChangeRejected changes from pre-branch-tip-changed-hook loom.
264
265
266
class TestAllMethodsThatChangeTipWillRunHooks(ChangeBranchTipTestCase):
267
    """Every method of Branch that changes a branch tip will invoke the
268
    pre/post_change_branch_tip hooks.
269
    """
270
271
    def setUp(self):
272
        ChangeBranchTipTestCase.setUp(self)
273
        self.installPreAndPostHooks()
274
        
275
    def installPreAndPostHooks(self):
276
        self.pre_hook_calls = self.install_logging_hook('pre')
277
        self.post_hook_calls = self.install_logging_hook('post')
278
279
    def resetHookCalls(self):
280
        del self.pre_hook_calls[:], self.post_hook_calls[:]
281
282
    def assertPreAndPostHooksWereInvoked(self):
283
        # Check for len == 1, because the hooks should only be be invoked once
284
        # by an operation.
285
        self.assertEqual(1, len(self.pre_hook_calls))
286
        self.assertEqual(1, len(self.post_hook_calls))
287
288
    def test_set_revision_history(self):
289
        branch = self.make_branch('')
290
        branch.set_revision_history([])
291
        self.assertPreAndPostHooksWereInvoked()
292
293
    def test_set_last_revision_info(self):
294
        branch = self.make_branch('')
295
        branch.set_last_revision_info(0, NULL_REVISION)
296
        self.assertPreAndPostHooksWereInvoked()
297
298
    def test_generate_revision_history(self):
299
        branch = self.make_branch('')
300
        branch.generate_revision_history(NULL_REVISION)
301
        self.assertPreAndPostHooksWereInvoked()
302
303
    def test_pull(self):
304
        source_branch = self.make_branch_with_revision_ids('rev-1', 'rev-2')
305
        self.resetHookCalls()
306
        destination_branch = self.make_branch('destination')
307
        destination_branch.pull(source_branch)
308
        self.assertPreAndPostHooksWereInvoked()
309
310
    def test_push(self):
311
        source_branch = self.make_branch_with_revision_ids('rev-1', 'rev-2')
312
        self.resetHookCalls()
313
        destination_branch = self.make_branch('destination')
314
        source_branch.push(destination_branch)
315
        self.assertPreAndPostHooksWereInvoked()
316
317