/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')
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
40
        Branch.hooks.install_hook('set_rh', self.capture_set_rh_hook)
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
41
        branch.set_revision_history([])
42
        self.assertEqual(self.hook_calls,
43
            [('set_rh', branch, [], True)])
44
45
    def test_set_rh_nonempty_history(self):
2230.3.20 by Aaron Bentley
Add hooking for set_revision_history
46
        tree = self.make_branch_and_memory_tree('source')
47
        tree.lock_write()
48
        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
49
        tree.commit('another commit', rev_id='f\xc2\xb5')
2230.3.20 by Aaron Bentley
Add hooking for set_revision_history
50
        tree.commit('empty commit', rev_id='foo')
51
        tree.unlock()
52
        branch = tree.branch
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
53
        Branch.hooks.install_hook('set_rh', self.capture_set_rh_hook)
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
54
        # some branches require that their history be set to a revision in the
55
        # repository
2309.4.10 by John Arbash Meinel
(fixed) Fix the last few tests that were explicitly passing around unicode ids
56
        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
57
        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
58
            [('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
59
60
    def test_set_rh_branch_is_locked(self):
61
        branch = self.make_branch('source')
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
62
        Branch.hooks.install_hook('set_rh', self.capture_set_rh_hook)
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
63
        branch.set_revision_history([])
64
        self.assertEqual(self.hook_calls,
65
            [('set_rh', branch, [], True)])
66
67
    def test_set_rh_calls_all_hooks_no_errors(self):
68
        branch = self.make_branch('source')
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
69
        Branch.hooks.install_hook('set_rh', self.capture_set_rh_hook)
70
        Branch.hooks.install_hook('set_rh', self.capture_set_rh_hook)
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
71
        branch.set_revision_history([])
72
        self.assertEqual(self.hook_calls,
73
            [('set_rh', branch, [], True),
74
             ('set_rh', branch, [], True),
75
            ])
3331.1.2 by James Henstridge
Add calls to set_last_revision_info hook to both BzrBranch and
76
77
3331.1.4 by James Henstridge
Adjust my tests to pass with Ian's API.
78
class TestPostChangeBranchTip(TestCaseWithMemoryTransport):
3331.1.2 by James Henstridge
Add calls to set_last_revision_info hook to both BzrBranch and
79
80
    def setUp(self):
81
        self.hook_calls = []
82
        TestCaseWithMemoryTransport.setUp(self)
83
3331.1.7 by James Henstridge
Make the branch a member of the ChangeBranchTipParams object.
84
    def capture_post_change_branch_tip_hook(self, params):
3331.1.4 by James Henstridge
Adjust my tests to pass with Ian's API.
85
        """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
86
87
        The call is logged, as is some state of the branch.
88
        """
3331.1.7 by James Henstridge
Make the branch a member of the ChangeBranchTipParams object.
89
        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
90
        self.assertEquals(params.branch.last_revision_info(),
91
                          (params.new_revno, params.new_revid))
3331.1.2 by James Henstridge
Add calls to set_last_revision_info hook to both BzrBranch and
92
3331.1.4 by James Henstridge
Adjust my tests to pass with Ian's API.
93
    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
94
        branch = self.make_branch('source')
3331.1.4 by James Henstridge
Adjust my tests to pass with Ian's API.
95
        Branch.hooks.install_hook('post_change_branch_tip',
96
                                  self.capture_post_change_branch_tip_hook)
3331.1.2 by James Henstridge
Add calls to set_last_revision_info hook to both BzrBranch and
97
        branch.set_last_revision_info(0, NULL_REVISION)
3331.1.4 by James Henstridge
Adjust my tests to pass with Ian's API.
98
        self.assertEqual(len(self.hook_calls), 1)
3331.1.7 by James Henstridge
Make the branch a member of the ChangeBranchTipParams object.
99
        self.assertEqual(self.hook_calls[0][0].branch, branch)
100
        self.assertEqual(self.hook_calls[0][0].old_revid, NULL_REVISION)
101
        self.assertEqual(self.hook_calls[0][0].old_revno, 0)
102
        self.assertEqual(self.hook_calls[0][0].new_revid, NULL_REVISION)
103
        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
104
3331.1.4 by James Henstridge
Adjust my tests to pass with Ian's API.
105
    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
106
        tree = self.make_branch_and_memory_tree('source')
107
        tree.lock_write()
108
        tree.add('')
109
        tree.commit('another commit', rev_id='f\xc2\xb5')
110
        tree.commit('empty commit', rev_id='foo')
111
        tree.unlock()
112
        branch = tree.branch
3331.1.4 by James Henstridge
Adjust my tests to pass with Ian's API.
113
        Branch.hooks.install_hook('post_change_branch_tip',
114
                                  self.capture_post_change_branch_tip_hook)
3331.1.2 by James Henstridge
Add calls to set_last_revision_info hook to both BzrBranch and
115
        # some branches require that their history be set to a revision in the
116
        # repository
117
        branch.set_last_revision_info(1, 'f\xc2\xb5')
3331.1.4 by James Henstridge
Adjust my tests to pass with Ian's API.
118
        self.assertEqual(len(self.hook_calls), 1)
3331.1.7 by James Henstridge
Make the branch a member of the ChangeBranchTipParams object.
119
        self.assertEqual(self.hook_calls[0][0].branch, branch)
120
        self.assertEqual(self.hook_calls[0][0].old_revid, 'foo')
121
        self.assertEqual(self.hook_calls[0][0].old_revno, 2)
122
        self.assertEqual(self.hook_calls[0][0].new_revid, 'f\xc2\xb5')
123
        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.
124
125
    def test_post_change_branch_tip_branch_is_locked(self):
126
        branch = self.make_branch('source')
127
        Branch.hooks.install_hook('post_change_branch_tip',
128
                                  self.capture_post_change_branch_tip_hook)
129
        branch.set_last_revision_info(0, NULL_REVISION)
130
        self.assertEqual(len(self.hook_calls), 1)
3331.1.7 by James Henstridge
Make the branch a member of the ChangeBranchTipParams object.
131
        self.assertEqual(self.hook_calls[0][0].branch, branch)
132
        self.assertEqual(self.hook_calls[0][1], True)
3331.1.4 by James Henstridge
Adjust my tests to pass with Ian's API.
133
134
    def test_post_change_branch_tip_calls_all_hooks_no_errors(self):
135
        branch = self.make_branch('source')
136
        Branch.hooks.install_hook('post_change_branch_tip',
137
                                  self.capture_post_change_branch_tip_hook)
138
        Branch.hooks.install_hook('post_change_branch_tip',
139
                                  self.capture_post_change_branch_tip_hook)
140
        branch.set_last_revision_info(0, NULL_REVISION)
141
        self.assertEqual(len(self.hook_calls), 2)
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[1][0].branch, branch)