~bzr-pqm/bzr/bzr.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# Copyright (C) 2007 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

"""Tests that branch classes implement hook callouts correctly."""

from bzrlib.branch import Branch
from bzrlib.revision import NULL_REVISION
from bzrlib.tests import TestCaseWithMemoryTransport


class TestSetRevisionHistoryHook(TestCaseWithMemoryTransport):

    def setUp(self):
        self.hook_calls = []
        TestCaseWithMemoryTransport.setUp(self)

    def capture_set_rh_hook(self, branch, rev_history):
        """Capture post set-rh hook calls to self.hook_calls.
        
        The call is logged, as is some state of the branch.
        """
        self.hook_calls.append(
            ('set_rh', branch, rev_history, branch.is_locked()))

    def test_set_rh_empty_history(self):
        branch = self.make_branch('source')
        Branch.hooks.install_named_hook('set_rh', self.capture_set_rh_hook,
                                        None)
        branch.set_revision_history([])
        self.assertEqual(self.hook_calls,
            [('set_rh', branch, [], True)])

    def test_set_rh_nonempty_history(self):
        tree = self.make_branch_and_memory_tree('source')
        tree.lock_write()
        tree.add('')
        tree.commit('another commit', rev_id='f\xc2\xb5')
        tree.commit('empty commit', rev_id='foo')
        tree.unlock()
        branch = tree.branch
        Branch.hooks.install_named_hook('set_rh', self.capture_set_rh_hook,
                                        None)
        # some branches require that their history be set to a revision in the
        # repository
        branch.set_revision_history(['f\xc2\xb5'])
        self.assertEqual(self.hook_calls,
            [('set_rh', branch, ['f\xc2\xb5'], True)])

    def test_set_rh_branch_is_locked(self):
        branch = self.make_branch('source')
        Branch.hooks.install_named_hook('set_rh', self.capture_set_rh_hook,
                                        None)
        branch.set_revision_history([])
        self.assertEqual(self.hook_calls,
            [('set_rh', branch, [], True)])

    def test_set_rh_calls_all_hooks_no_errors(self):
        branch = self.make_branch('source')
        Branch.hooks.install_named_hook('set_rh', self.capture_set_rh_hook,
                                        None)
        Branch.hooks.install_named_hook('set_rh', self.capture_set_rh_hook,
                                        None)
        branch.set_revision_history([])
        self.assertEqual(self.hook_calls,
            [('set_rh', branch, [], True),
             ('set_rh', branch, [], True),
            ])


class TestPostChangeBranchTip(TestCaseWithMemoryTransport):

    def setUp(self):
        self.hook_calls = []
        TestCaseWithMemoryTransport.setUp(self)

    def capture_post_change_branch_tip_hook(self, params):
        """Capture post_change_branch_tip hook calls to self.hook_calls.

        The call is logged, as is some state of the branch.
        """
        self.hook_calls.append((params, params.branch.is_locked()))
        self.assertEquals(params.branch.last_revision_info(),
                          (params.new_revno, params.new_revid))

    def test_post_change_branch_tip_empty_history(self):
        branch = self.make_branch('source')
        Branch.hooks.install_named_hook(
            'post_change_branch_tip',
            self.capture_post_change_branch_tip_hook,
            None)
        branch.set_last_revision_info(0, NULL_REVISION)
        self.assertEqual(len(self.hook_calls), 1)
        self.assertEqual(self.hook_calls[0][0].branch, branch)
        self.assertEqual(self.hook_calls[0][0].old_revid, NULL_REVISION)
        self.assertEqual(self.hook_calls[0][0].old_revno, 0)
        self.assertEqual(self.hook_calls[0][0].new_revid, NULL_REVISION)
        self.assertEqual(self.hook_calls[0][0].new_revno, 0)

    def test_post_change_branch_tip_nonempty_history(self):
        tree = self.make_branch_and_memory_tree('source')
        tree.lock_write()
        tree.add('')
        tree.commit('another commit', rev_id='f\xc2\xb5')
        tree.commit('empty commit', rev_id='foo')
        tree.unlock()
        branch = tree.branch
        Branch.hooks.install_named_hook(
            'post_change_branch_tip',
            self.capture_post_change_branch_tip_hook,
            None)
        # some branches require that their history be set to a revision in the
        # repository
        branch.set_last_revision_info(1, 'f\xc2\xb5')
        self.assertEqual(len(self.hook_calls), 1)
        self.assertEqual(self.hook_calls[0][0].branch, branch)
        self.assertEqual(self.hook_calls[0][0].old_revid, 'foo')
        self.assertEqual(self.hook_calls[0][0].old_revno, 2)
        self.assertEqual(self.hook_calls[0][0].new_revid, 'f\xc2\xb5')
        self.assertEqual(self.hook_calls[0][0].new_revno, 1)

    def test_post_change_branch_tip_branch_is_locked(self):
        branch = self.make_branch('source')
        Branch.hooks.install_named_hook(
            'post_change_branch_tip',
            self.capture_post_change_branch_tip_hook,
            None)
        branch.set_last_revision_info(0, NULL_REVISION)
        self.assertEqual(len(self.hook_calls), 1)
        self.assertEqual(self.hook_calls[0][0].branch, branch)
        self.assertEqual(self.hook_calls[0][1], True)

    def test_post_change_branch_tip_calls_all_hooks_no_errors(self):
        branch = self.make_branch('source')
        Branch.hooks.install_named_hook(
            'post_change_branch_tip',
            self.capture_post_change_branch_tip_hook,
            None)
        Branch.hooks.install_named_hook(
            'post_change_branch_tip',
            self.capture_post_change_branch_tip_hook,
            None)
        branch.set_last_revision_info(0, NULL_REVISION)
        self.assertEqual(len(self.hook_calls), 2)
        self.assertEqual(self.hook_calls[0][0].branch, branch)
        self.assertEqual(self.hook_calls[1][0].branch, branch)