/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
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
19
from bzrlib.branch import Branch, ChangeBranchTipParams
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.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
84
    """Tests for pre_change_branch_tip hook.
85
    
86
    Most of these tests are very similar to the tests in
87
    TestPostChangeBranchTip.
88
    """
89
90
    def install_logging_hook(self):
91
        """Add a hook that logs calls made to it.
92
        
93
        :returns: the list that the calls will be appended to.
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
94
        """
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
95
        hook_calls = []
96
        Branch.hooks.install_named_hook(
97
            'pre_change_branch_tip', hook_calls.append, None)
98
        return hook_calls
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
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.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
111
    def test_hook_runs_before_change(self):
112
        """The hook runs *before* the branch's last_revision_info has changed.
113
        """
114
        branch = self.make_branch_with_revision_ids('revid-one')
115
        def assertBranchAtRevision1(params):
116
            self.assertEquals(
117
                (1, 'revid-one'), params.branch.last_revision_info())
118
        Branch.hooks.install_named_hook(
119
            'pre_change_branch_tip', assertBranchAtRevision1, None)
120
        branch.set_last_revision_info(0, NULL_REVISION)
121
3517.2.2 by Andrew Bennetts
Add test for a pre_change_branch_tip hook rejecting a change.
122
    def test_reject_by_hook(self):
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
123
        """If a hook raises an exception, the change does not take effect.
124
        
125
        Also, the exception will be propogated.
126
        """
3517.2.2 by Andrew Bennetts
Add test for a pre_change_branch_tip hook rejecting a change.
127
        branch = self.make_branch_with_revision_ids(
128
            'one-\xc2\xb5', 'two-\xc2\xb5')
129
        class PearShapedError(Exception):
130
            pass
131
        def hook_that_raises(params):
132
            raise PearShapedError()
133
        Branch.hooks.install_named_hook(
134
            'pre_change_branch_tip', hook_that_raises, None)
135
        self.assertRaises(
136
            PearShapedError, branch.set_last_revision_info, 0, NULL_REVISION)
137
        # The revision info is unchanged.
138
        self.assertEqual((2, 'two-\xc2\xb5'), branch.last_revision_info())
139
        
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
140
    def test_pre_change_branch_tip_empty_history(self):
141
        branch = self.make_branch('source')
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
142
        hook_calls = self.install_logging_hook()
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
143
        branch.set_last_revision_info(0, NULL_REVISION)
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
144
        expected_params = ChangeBranchTipParams(
145
            branch, 0, 0, NULL_REVISION, NULL_REVISION)
146
        self.assertEqual([expected_params], hook_calls)
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
147
148
    def test_pre_change_branch_tip_nonempty_history(self):
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
149
        # some branches require that their history be set to a revision in the
150
        # repository, so we need to make a branch with non-empty history for
151
        # this test.
3517.2.2 by Andrew Bennetts
Add test for a pre_change_branch_tip hook rejecting a change.
152
        branch = self.make_branch_with_revision_ids(
153
            'one-\xc2\xb5', 'two-\xc2\xb5')
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
154
        hook_calls = self.install_logging_hook()
3517.2.2 by Andrew Bennetts
Add test for a pre_change_branch_tip hook rejecting a change.
155
        branch.set_last_revision_info(1, 'one-\xc2\xb5')
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
156
        expected_params = ChangeBranchTipParams(
157
            branch, 2, 1, 'two-\xc2\xb5', 'one-\xc2\xb5')
158
        self.assertEqual([expected_params], hook_calls)
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
159
160
    def test_pre_change_branch_tip_branch_is_locked(self):
161
        branch = self.make_branch('source')
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
162
        def assertBranchIsLocked(params):
163
            self.assertTrue(params.branch.is_locked())
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
164
        Branch.hooks.install_named_hook(
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
165
            'pre_change_branch_tip', assertBranchIsLocked, None)
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
166
        branch.set_last_revision_info(0, NULL_REVISION)
167
168
    def test_pre_change_branch_tip_calls_all_hooks_no_errors(self):
169
        branch = self.make_branch('source')
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
170
        hook_calls_1 = self.install_logging_hook()
171
        hook_calls_2 = self.install_logging_hook()
172
        self.assertIsNot(hook_calls_1, hook_calls_2)
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
173
        branch.set_last_revision_info(0, NULL_REVISION)
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
174
        # Both hooks are called.
175
        self.assertEqual(len(hook_calls_1), 1)
176
        self.assertEqual(len(hook_calls_2), 1)
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
177
178
3331.1.4 by James Henstridge
Adjust my tests to pass with Ian's API.
179
class TestPostChangeBranchTip(TestCaseWithMemoryTransport):
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
180
    """Tests for post_change_branch_tip hook.
181
182
    Most of these tests are very similar to the tests in
183
    TestPostChangeBranchTip.
184
    """
185
186
    def install_logging_hook(self):
187
        """Add a hook that logs calls made to it.
188
        
189
        :returns: the list that the calls will be appended to.
3331.1.2 by James Henstridge
Add calls to set_last_revision_info hook to both BzrBranch and
190
        """
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
191
        hook_calls = []
3256.2.26 by Daniel Watkins
Updated tests to use install_named_hook.
192
        Branch.hooks.install_named_hook(
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
193
            'post_change_branch_tip', hook_calls.append, None)
194
        return hook_calls
3331.1.2 by James Henstridge
Add calls to set_last_revision_info hook to both BzrBranch and
195
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
196
    def make_branch_with_revision_ids(self, *revision_ids):
197
        """Makes a branch with the given commits."""
3331.1.2 by James Henstridge
Add calls to set_last_revision_info hook to both BzrBranch and
198
        tree = self.make_branch_and_memory_tree('source')
199
        tree.lock_write()
200
        tree.add('')
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
201
        for revision_id in revision_ids:
202
            tree.commit('Message of ' + revision_id, rev_id=revision_id)
3331.1.2 by James Henstridge
Add calls to set_last_revision_info hook to both BzrBranch and
203
        tree.unlock()
204
        branch = tree.branch
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
205
        return branch
206
207
    def test_hook_runs_after_change(self):
208
        """The hook runs *after* the branch's last_revision_info has changed.
209
        """
210
        branch = self.make_branch_with_revision_ids('revid-one')
211
        def assertBranchAtRevision1(params):
212
            self.assertEquals(
213
                (0, NULL_REVISION), params.branch.last_revision_info())
3256.2.26 by Daniel Watkins
Updated tests to use install_named_hook.
214
        Branch.hooks.install_named_hook(
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
215
            'post_change_branch_tip', assertBranchAtRevision1, None)
216
        branch.set_last_revision_info(0, NULL_REVISION)
217
218
    def test_change_branch_tip_empty_history(self):
219
        branch = self.make_branch('source')
220
        hook_calls = self.install_logging_hook()
221
        branch.set_last_revision_info(0, NULL_REVISION)
222
        expected_params = ChangeBranchTipParams(
223
            branch, 0, 0, NULL_REVISION, NULL_REVISION)
224
        self.assertEqual([expected_params], hook_calls)
225
226
    def test_change_branch_tip_nonempty_history(self):
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
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
228
        # repository, so we need to make a branch with non-empty history for
229
        # this test.
230
        branch = self.make_branch_with_revision_ids(
231
            'one-\xc2\xb5', 'two-\xc2\xb5')
232
        hook_calls = self.install_logging_hook()
233
        branch.set_last_revision_info(1, 'one-\xc2\xb5')
234
        expected_params = ChangeBranchTipParams(
235
            branch, 2, 1, 'two-\xc2\xb5', 'one-\xc2\xb5')
236
        self.assertEqual([expected_params], hook_calls)
237
238
    def test_tip_branch_is_locked(self):
239
        """The branch passed to the hook is locked."""
240
        branch = self.make_branch('source')
241
        def assertBranchIsLocked(params):
242
            self.assertTrue(params.branch.is_locked())
243
        Branch.hooks.install_named_hook(
244
            'pre_change_branch_tip', assertBranchIsLocked, None)
245
        branch.set_last_revision_info(0, NULL_REVISION)
246
247
    def test_change_branch_tip_calls_all_hooks_no_errors(self):
248
        """If multiple hooks are registered, all are called."""
249
        branch = self.make_branch('source')
250
        hook_calls_1 = self.install_logging_hook()
251
        hook_calls_2 = self.install_logging_hook()
252
        self.assertIsNot(hook_calls_1, hook_calls_2)
253
        branch.set_last_revision_info(0, NULL_REVISION)
254
        # Both hooks are called.
255
        self.assertEqual(len(hook_calls_1), 1)
256
        self.assertEqual(len(hook_calls_2), 1)