~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
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
3331.1.4 by James Henstridge
Adjust my tests to pass with Ian's API.
83
class TestPostChangeBranchTip(TestCaseWithMemoryTransport):
3331.1.2 by James Henstridge
Add calls to set_last_revision_info hook to both BzrBranch and
84
85
    def setUp(self):
86
        self.hook_calls = []
87
        TestCaseWithMemoryTransport.setUp(self)
88
3331.1.7 by James Henstridge
Make the branch a member of the ChangeBranchTipParams object.
89
    def capture_post_change_branch_tip_hook(self, params):
3331.1.4 by James Henstridge
Adjust my tests to pass with Ian's API.
90
        """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
91
92
        The call is logged, as is some state of the branch.
93
        """
3331.1.7 by James Henstridge
Make the branch a member of the ChangeBranchTipParams object.
94
        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
95
        self.assertEquals(params.branch.last_revision_info(),
96
                          (params.new_revno, params.new_revid))
3331.1.2 by James Henstridge
Add calls to set_last_revision_info hook to both BzrBranch and
97
3331.1.4 by James Henstridge
Adjust my tests to pass with Ian's API.
98
    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
99
        branch = self.make_branch('source')
3256.2.26 by Daniel Watkins
Updated tests to use install_named_hook.
100
        Branch.hooks.install_named_hook(
101
            'post_change_branch_tip',
102
            self.capture_post_change_branch_tip_hook,
103
            None)
3331.1.2 by James Henstridge
Add calls to set_last_revision_info hook to both BzrBranch and
104
        branch.set_last_revision_info(0, NULL_REVISION)
3331.1.4 by James Henstridge
Adjust my tests to pass with Ian's API.
105
        self.assertEqual(len(self.hook_calls), 1)
3331.1.7 by James Henstridge
Make the branch a member of the ChangeBranchTipParams object.
106
        self.assertEqual(self.hook_calls[0][0].branch, branch)
107
        self.assertEqual(self.hook_calls[0][0].old_revid, NULL_REVISION)
108
        self.assertEqual(self.hook_calls[0][0].old_revno, 0)
109
        self.assertEqual(self.hook_calls[0][0].new_revid, NULL_REVISION)
110
        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
111
3331.1.4 by James Henstridge
Adjust my tests to pass with Ian's API.
112
    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
113
        tree = self.make_branch_and_memory_tree('source')
114
        tree.lock_write()
115
        tree.add('')
116
        tree.commit('another commit', rev_id='f\xc2\xb5')
117
        tree.commit('empty commit', rev_id='foo')
118
        tree.unlock()
119
        branch = tree.branch
3256.2.26 by Daniel Watkins
Updated tests to use install_named_hook.
120
        Branch.hooks.install_named_hook(
121
            'post_change_branch_tip',
122
            self.capture_post_change_branch_tip_hook,
123
            None)
3331.1.2 by James Henstridge
Add calls to set_last_revision_info hook to both BzrBranch and
124
        # some branches require that their history be set to a revision in the
125
        # repository
126
        branch.set_last_revision_info(1, 'f\xc2\xb5')
3331.1.4 by James Henstridge
Adjust my tests to pass with Ian's API.
127
        self.assertEqual(len(self.hook_calls), 1)
3331.1.7 by James Henstridge
Make the branch a member of the ChangeBranchTipParams object.
128
        self.assertEqual(self.hook_calls[0][0].branch, branch)
129
        self.assertEqual(self.hook_calls[0][0].old_revid, 'foo')
130
        self.assertEqual(self.hook_calls[0][0].old_revno, 2)
131
        self.assertEqual(self.hook_calls[0][0].new_revid, 'f\xc2\xb5')
132
        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.
133
134
    def test_post_change_branch_tip_branch_is_locked(self):
135
        branch = self.make_branch('source')
3256.2.26 by Daniel Watkins
Updated tests to use install_named_hook.
136
        Branch.hooks.install_named_hook(
137
            'post_change_branch_tip',
138
            self.capture_post_change_branch_tip_hook,
139
            None)
3331.1.4 by James Henstridge
Adjust my tests to pass with Ian's API.
140
        branch.set_last_revision_info(0, NULL_REVISION)
141
        self.assertEqual(len(self.hook_calls), 1)
3331.1.7 by James Henstridge
Make the branch a member of the ChangeBranchTipParams object.
142
        self.assertEqual(self.hook_calls[0][0].branch, branch)
143
        self.assertEqual(self.hook_calls[0][1], True)
3331.1.4 by James Henstridge
Adjust my tests to pass with Ian's API.
144
145
    def test_post_change_branch_tip_calls_all_hooks_no_errors(self):
146
        branch = self.make_branch('source')
3256.2.26 by Daniel Watkins
Updated tests to use install_named_hook.
147
        Branch.hooks.install_named_hook(
148
            'post_change_branch_tip',
149
            self.capture_post_change_branch_tip_hook,
150
            None)
151
        Branch.hooks.install_named_hook(
152
            'post_change_branch_tip',
153
            self.capture_post_change_branch_tip_hook,
154
            None)
3331.1.4 by James Henstridge
Adjust my tests to pass with Ian's API.
155
        branch.set_last_revision_info(0, NULL_REVISION)
156
        self.assertEqual(len(self.hook_calls), 2)
3331.1.7 by James Henstridge
Make the branch a member of the ChangeBranchTipParams object.
157
        self.assertEqual(self.hook_calls[0][0].branch, branch)
158
        self.assertEqual(self.hook_calls[1][0].branch, branch)