~bzr-pqm/bzr/bzr.dev

4988.10.5 by John Arbash Meinel
Merge bzr.dev 5021 to resolve NEWS
1
# Copyright (C) 2007-2010 Canonical Ltd
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
16
17
"""Tests for the contract of commit on branches."""
18
5010.2.12 by Vincent Ladeuil
Fix imports in per_branch/test_commit.py.
19
from bzrlib import (
20
    branch,
21
    delta,
22
    errors,
23
    revision,
24
    transport,
25
    )
26
from bzrlib.tests import per_branch
27
28
29
class TestCommit(per_branch.TestCaseWithBranch):
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
30
31
    def test_commit_nicks(self):
32
        """Nicknames are committed to the revision"""
5010.2.12 by Vincent Ladeuil
Fix imports in per_branch/test_commit.py.
33
        transport.get_transport(self.get_url()).mkdir('bzr.dev')
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
34
        wt = self.make_branch_and_tree('bzr.dev')
35
        branch = wt.branch
36
        branch.nick = "My happy branch"
37
        wt.commit('My commit respect da nick.')
38
        committed = branch.repository.get_revision(branch.last_revision())
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
39
        self.assertEqual(committed.properties["branch-nick"],
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
40
                         "My happy branch")
41
42
5010.2.12 by Vincent Ladeuil
Fix imports in per_branch/test_commit.py.
43
class TestCommitHook(per_branch.TestCaseWithBranch):
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
44
45
    def setUp(self):
46
        self.hook_calls = []
5010.2.12 by Vincent Ladeuil
Fix imports in per_branch/test_commit.py.
47
        super(TestCommitHook, self).setUp()
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
48
49
    def capture_post_commit_hook(self, local, master, old_revno,
50
        old_revid, new_revno, new_revid):
51
        """Capture post commit hook calls to self.hook_calls.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
52
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
53
        The call is logged, as is some state of the two branches.
54
        """
55
        if local:
56
            local_locked = local.is_locked()
57
            local_base = local.base
58
        else:
59
            local_locked = None
60
            local_base = None
61
        self.hook_calls.append(
62
            ('post_commit', local_base, master.base, old_revno, old_revid,
63
             new_revno, new_revid, local_locked, master.is_locked()))
64
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
65
    def capture_pre_commit_hook(self, local, master, old_revno, old_revid,
2659.3.9 by NamNguyen
branch.py:
66
                                new_revno, new_revid,
67
                                tree_delta, future_tree):
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
68
        self.hook_calls.append(('pre_commit', old_revno, old_revid,
2659.3.9 by NamNguyen
branch.py:
69
                                new_revno, new_revid, tree_delta))
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
70
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
71
    def test_post_commit_to_origin(self):
72
        tree = self.make_branch_and_memory_tree('branch')
5010.2.12 by Vincent Ladeuil
Fix imports in per_branch/test_commit.py.
73
        branch.Branch.hooks.install_named_hook(
74
            'post_commit', self.capture_post_commit_hook, None)
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
75
        tree.lock_write()
76
        tree.add('')
77
        revid = tree.commit('a revision')
78
        # should have had one notification, from origin, and
79
        # have the branch locked at notification time.
80
        self.assertEqual([
5010.2.12 by Vincent Ladeuil
Fix imports in per_branch/test_commit.py.
81
            ('post_commit', None, tree.branch.base, 0, revision.NULL_REVISION,
82
             1, revid, None, True)
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
83
            ],
84
            self.hook_calls)
85
        tree.unlock()
86
87
    def test_post_commit_bound(self):
88
        master = self.make_branch('master')
89
        tree = self.make_branch_and_memory_tree('local')
90
        try:
91
            tree.branch.bind(master)
92
        except errors.UpgradeRequired:
93
            # cant bind this format, the test is irrelevant.
94
            return
5010.2.12 by Vincent Ladeuil
Fix imports in per_branch/test_commit.py.
95
        branch.Branch.hooks.install_named_hook(
96
            'post_commit', self.capture_post_commit_hook, None)
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
97
        tree.lock_write()
98
        tree.add('')
99
        revid = tree.commit('a revision')
100
        # with a bound branch, local is set.
101
        self.assertEqual([
5010.2.12 by Vincent Ladeuil
Fix imports in per_branch/test_commit.py.
102
            ('post_commit', tree.branch.base, master.base, 0,
103
             revision.NULL_REVISION, 1, revid, True, True)
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
104
            ],
105
            self.hook_calls)
106
        tree.unlock()
107
108
    def test_post_commit_not_to_origin(self):
109
        tree = self.make_branch_and_memory_tree('branch')
110
        tree.lock_write()
111
        tree.add('')
112
        revid = tree.commit('first revision')
5010.2.12 by Vincent Ladeuil
Fix imports in per_branch/test_commit.py.
113
        branch.Branch.hooks.install_named_hook(
114
            'post_commit', self.capture_post_commit_hook, None)
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
115
        revid2 = tree.commit('second revision')
116
        # having committed from up the branch, we should get the
117
        # before and after revnos and revids correctly.
118
        self.assertEqual([
119
            ('post_commit', None, tree.branch.base, 1, revid, 2, revid2,
120
             None, True)
121
            ],
122
            self.hook_calls)
123
        tree.unlock()
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
124
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
125
    def test_pre_commit_passes(self):
5010.2.12 by Vincent Ladeuil
Fix imports in per_branch/test_commit.py.
126
        empty_delta = delta.TreeDelta()
127
        root_delta = delta.TreeDelta()
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
128
        tree = self.make_branch_and_memory_tree('branch')
129
        tree.lock_write()
3228.3.1 by Robert Collins
* The branch interface tests were invalid for branches using subtree
130
        tree.add('')
131
        root_delta.added = [('', tree.path2id(''), 'directory')]
5010.2.12 by Vincent Ladeuil
Fix imports in per_branch/test_commit.py.
132
        branch.Branch.hooks.install_named_hook(
133
            "pre_commit", self.capture_pre_commit_hook, None)
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
134
        revid1 = tree.commit('first revision')
135
        revid2 = tree.commit('second revision')
136
        self.assertEqual([
5010.2.12 by Vincent Ladeuil
Fix imports in per_branch/test_commit.py.
137
            ('pre_commit', 0, revision.NULL_REVISION, 1, revid1, root_delta),
2659.3.9 by NamNguyen
branch.py:
138
            ('pre_commit', 1, revid1, 2, revid2, empty_delta)
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
139
            ],
140
            self.hook_calls)
141
        tree.unlock()
142
143
    def test_pre_commit_fails(self):
5010.2.12 by Vincent Ladeuil
Fix imports in per_branch/test_commit.py.
144
        empty_delta = delta.TreeDelta()
145
        root_delta = delta.TreeDelta()
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
146
        tree = self.make_branch_and_memory_tree('branch')
147
        tree.lock_write()
3228.3.1 by Robert Collins
* The branch interface tests were invalid for branches using subtree
148
        tree.add('')
149
        root_delta.added = [('', tree.path2id(''), 'directory')]
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
150
        class PreCommitException(Exception): pass
2659.3.9 by NamNguyen
branch.py:
151
        def hook_func(local, master,
152
                      old_revno, old_revid, new_revno, new_revid,
153
                      tree_delta, future_tree):
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
154
            raise PreCommitException(new_revid)
5010.2.12 by Vincent Ladeuil
Fix imports in per_branch/test_commit.py.
155
        branch.Branch.hooks.install_named_hook(
156
            "pre_commit", self.capture_pre_commit_hook, None)
157
        branch.Branch.hooks.install_named_hook("pre_commit", hook_func, None)
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
158
        revids = [None, None, None]
2659.3.9 by NamNguyen
branch.py:
159
        # this commit will raise an exception
160
        # so the commit is rolled back and revno unchanged
2659.3.3 by NamNguyen
Changed ``pre_commit`` hook signature.
161
        err = self.assertRaises(PreCommitException, tree.commit, 'message')
162
        # we have to record the revid to use in assertEqual later
2789.1.1 by Ian Clatworthy
(Nam Nguyen) Pre-commit hook
163
        revids[0] = str(err)
2659.3.3 by NamNguyen
Changed ``pre_commit`` hook signature.
164
        # unregister all pre_commit hooks
5010.2.12 by Vincent Ladeuil
Fix imports in per_branch/test_commit.py.
165
        branch.Branch.hooks["pre_commit"] = []
2659.3.3 by NamNguyen
Changed ``pre_commit`` hook signature.
166
        # and re-register the capture hook
5010.2.12 by Vincent Ladeuil
Fix imports in per_branch/test_commit.py.
167
        branch.Branch.hooks.install_named_hook(
168
            "pre_commit", self.capture_pre_commit_hook, None)
2659.3.3 by NamNguyen
Changed ``pre_commit`` hook signature.
169
        # now these commits should go through
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
170
        for i in range(1, 3):
2659.3.3 by NamNguyen
Changed ``pre_commit`` hook signature.
171
            revids[i] = tree.commit('message')
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
172
        self.assertEqual([
5010.2.12 by Vincent Ladeuil
Fix imports in per_branch/test_commit.py.
173
            ('pre_commit', 0, revision.NULL_REVISION, 1, revids[0], root_delta),
174
            ('pre_commit', 0, revision.NULL_REVISION, 1, revids[1], root_delta),
2659.3.9 by NamNguyen
branch.py:
175
            ('pre_commit', 1, revids[1], 2, revids[2], empty_delta)
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
176
            ],
177
            self.hook_calls)
178
        tree.unlock()
2659.3.9 by NamNguyen
branch.py:
179
180
    def test_pre_commit_delta(self):
181
        # This tests the TreeDelta object passed to pre_commit hook.
182
        # This does not try to validate data correctness in the delta.
2659.3.6 by NamNguyen
branch_implementations/test_commit.py:
183
        self.build_tree(['rootfile', 'dir/', 'dir/subfile'])
184
        tree = self.make_branch_and_tree('.')
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
185
        tree.lock_write()
2659.3.9 by NamNguyen
branch.py:
186
        try:
187
            # setting up a playground
188
            tree.set_root_id('root_id')
189
            tree.add('rootfile', 'rootfile_id')
190
            tree.put_file_bytes_non_atomic('rootfile_id', 'abc')
191
            tree.add('dir', 'dir_id')
192
            tree.add('dir/subfile', 'dir_subfile_id')
193
            tree.mkdir('to_be_unversioned', 'to_be_unversioned_id')
194
            tree.put_file_bytes_non_atomic('dir_subfile_id', 'def')
195
            revid1 = tree.commit('first revision')
196
        finally:
197
            tree.unlock()
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
198
2659.3.9 by NamNguyen
branch.py:
199
        tree.lock_write()
200
        try:
201
            # making changes
202
            tree.put_file_bytes_non_atomic('rootfile_id', 'jkl')
203
            tree.rename_one('dir/subfile', 'dir/subfile_renamed')
204
            tree.unversion(['to_be_unversioned_id'])
205
            tree.mkdir('added_dir', 'added_dir_id')
206
            # start to capture pre_commit delta
5010.2.12 by Vincent Ladeuil
Fix imports in per_branch/test_commit.py.
207
            branch.Branch.hooks.install_named_hook(
208
                "pre_commit", self.capture_pre_commit_hook, None)
2659.3.9 by NamNguyen
branch.py:
209
            revid2 = tree.commit('second revision')
210
        finally:
211
            tree.unlock()
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
212
5010.2.12 by Vincent Ladeuil
Fix imports in per_branch/test_commit.py.
213
        expected_delta = delta.TreeDelta()
2659.3.9 by NamNguyen
branch.py:
214
        expected_delta.added = [('added_dir', 'added_dir_id', 'directory')]
215
        expected_delta.removed = [('to_be_unversioned',
216
                                   'to_be_unversioned_id', 'directory')]
217
        expected_delta.renamed = [('dir/subfile', 'dir/subfile_renamed',
218
                                   'dir_subfile_id', 'file', False, False)]
219
        expected_delta.modified=[('rootfile', 'rootfile_id', 'file', True,
220
                                  False)]
221
        self.assertEqual([('pre_commit', 1, revid1, 2, revid2,
2789.1.1 by Ian Clatworthy
(Nam Nguyen) Pre-commit hook
222
                           expected_delta)], self.hook_calls)