/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
19
from breezy import (
5010.2.12 by Vincent Ladeuil
Fix imports in per_branch/test_commit.py.
20
    branch,
21
    delta,
22
    errors,
23
    revision,
24
    transport,
25
    )
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
26
from breezy.tests import per_branch
5010.2.12 by Vincent Ladeuil
Fix imports in per_branch/test_commit.py.
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"""
5609.9.4 by Vincent Ladeuil
Use self.get_transport instead of transport.get_transport where possible.
33
        self.get_transport().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())
6820.1.1 by Jelmer Vernooij
add RepositoryFormat.supports_storing_branch_nick.
39
        if branch.repository._format.supports_storing_branch_nick:
40
            self.assertEqual(committed.properties["branch-nick"],
41
                             "My happy branch")
42
        else:
43
            self.assertNotIn("branch-nick", committed.properties)
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
44
45
5010.2.12 by Vincent Ladeuil
Fix imports in per_branch/test_commit.py.
46
class TestCommitHook(per_branch.TestCaseWithBranch):
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
47
48
    def setUp(self):
49
        self.hook_calls = []
5010.2.12 by Vincent Ladeuil
Fix imports in per_branch/test_commit.py.
50
        super(TestCommitHook, self).setUp()
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
51
52
    def capture_post_commit_hook(self, local, master, old_revno,
53
        old_revid, new_revno, new_revid):
54
        """Capture post commit hook calls to self.hook_calls.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
55
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
56
        The call is logged, as is some state of the two branches.
57
        """
58
        if local:
59
            local_locked = local.is_locked()
60
            local_base = local.base
61
        else:
62
            local_locked = None
63
            local_base = None
64
        self.hook_calls.append(
65
            ('post_commit', local_base, master.base, old_revno, old_revid,
66
             new_revno, new_revid, local_locked, master.is_locked()))
67
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
68
    def capture_pre_commit_hook(self, local, master, old_revno, old_revid,
2659.3.9 by NamNguyen
branch.py:
69
                                new_revno, new_revid,
70
                                tree_delta, future_tree):
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
71
        self.hook_calls.append(('pre_commit', old_revno, old_revid,
2659.3.9 by NamNguyen
branch.py:
72
                                new_revno, new_revid, tree_delta))
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
73
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
74
    def test_post_commit_to_origin(self):
75
        tree = self.make_branch_and_memory_tree('branch')
5010.2.12 by Vincent Ladeuil
Fix imports in per_branch/test_commit.py.
76
        branch.Branch.hooks.install_named_hook(
77
            '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
78
        tree.lock_write()
79
        tree.add('')
80
        revid = tree.commit('a revision')
81
        # should have had one notification, from origin, and
82
        # have the branch locked at notification time.
83
        self.assertEqual([
5010.2.12 by Vincent Ladeuil
Fix imports in per_branch/test_commit.py.
84
            ('post_commit', None, tree.branch.base, 0, revision.NULL_REVISION,
85
             1, revid, None, True)
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
86
            ],
87
            self.hook_calls)
88
        tree.unlock()
89
90
    def test_post_commit_bound(self):
91
        master = self.make_branch('master')
92
        tree = self.make_branch_and_memory_tree('local')
93
        try:
94
            tree.branch.bind(master)
95
        except errors.UpgradeRequired:
96
            # cant bind this format, the test is irrelevant.
97
            return
5010.2.12 by Vincent Ladeuil
Fix imports in per_branch/test_commit.py.
98
        branch.Branch.hooks.install_named_hook(
99
            '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
100
        tree.lock_write()
101
        tree.add('')
102
        revid = tree.commit('a revision')
103
        # with a bound branch, local is set.
104
        self.assertEqual([
5010.2.12 by Vincent Ladeuil
Fix imports in per_branch/test_commit.py.
105
            ('post_commit', tree.branch.base, master.base, 0,
106
             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
107
            ],
108
            self.hook_calls)
109
        tree.unlock()
110
111
    def test_post_commit_not_to_origin(self):
112
        tree = self.make_branch_and_memory_tree('branch')
113
        tree.lock_write()
114
        tree.add('')
115
        revid = tree.commit('first revision')
5010.2.12 by Vincent Ladeuil
Fix imports in per_branch/test_commit.py.
116
        branch.Branch.hooks.install_named_hook(
117
            '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
118
        revid2 = tree.commit('second revision')
119
        # having committed from up the branch, we should get the
120
        # before and after revnos and revids correctly.
121
        self.assertEqual([
122
            ('post_commit', None, tree.branch.base, 1, revid, 2, revid2,
123
             None, True)
124
            ],
125
            self.hook_calls)
126
        tree.unlock()
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
127
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
128
    def test_pre_commit_passes(self):
5010.2.12 by Vincent Ladeuil
Fix imports in per_branch/test_commit.py.
129
        empty_delta = delta.TreeDelta()
130
        root_delta = delta.TreeDelta()
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
131
        tree = self.make_branch_and_memory_tree('branch')
132
        tree.lock_write()
3228.3.1 by Robert Collins
* The branch interface tests were invalid for branches using subtree
133
        tree.add('')
134
        root_delta.added = [('', tree.path2id(''), 'directory')]
5010.2.12 by Vincent Ladeuil
Fix imports in per_branch/test_commit.py.
135
        branch.Branch.hooks.install_named_hook(
136
            "pre_commit", self.capture_pre_commit_hook, None)
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
137
        revid1 = tree.commit('first revision')
138
        revid2 = tree.commit('second revision')
139
        self.assertEqual([
5010.2.12 by Vincent Ladeuil
Fix imports in per_branch/test_commit.py.
140
            ('pre_commit', 0, revision.NULL_REVISION, 1, revid1, root_delta),
2659.3.9 by NamNguyen
branch.py:
141
            ('pre_commit', 1, revid1, 2, revid2, empty_delta)
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
142
            ],
143
            self.hook_calls)
144
        tree.unlock()
145
146
    def test_pre_commit_fails(self):
5010.2.12 by Vincent Ladeuil
Fix imports in per_branch/test_commit.py.
147
        empty_delta = delta.TreeDelta()
148
        root_delta = delta.TreeDelta()
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
149
        tree = self.make_branch_and_memory_tree('branch')
150
        tree.lock_write()
3228.3.1 by Robert Collins
* The branch interface tests were invalid for branches using subtree
151
        tree.add('')
152
        root_delta.added = [('', tree.path2id(''), 'directory')]
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
153
        class PreCommitException(Exception): pass
2659.3.9 by NamNguyen
branch.py:
154
        def hook_func(local, master,
155
                      old_revno, old_revid, new_revno, new_revid,
156
                      tree_delta, future_tree):
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
157
            raise PreCommitException(new_revid)
5010.2.12 by Vincent Ladeuil
Fix imports in per_branch/test_commit.py.
158
        branch.Branch.hooks.install_named_hook(
159
            "pre_commit", self.capture_pre_commit_hook, None)
160
        branch.Branch.hooks.install_named_hook("pre_commit", hook_func, None)
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
161
        revids = [None, None, None]
2659.3.9 by NamNguyen
branch.py:
162
        # this commit will raise an exception
163
        # so the commit is rolled back and revno unchanged
2659.3.3 by NamNguyen
Changed ``pre_commit`` hook signature.
164
        err = self.assertRaises(PreCommitException, tree.commit, 'message')
165
        # we have to record the revid to use in assertEqual later
2789.1.1 by Ian Clatworthy
(Nam Nguyen) Pre-commit hook
166
        revids[0] = str(err)
2659.3.3 by NamNguyen
Changed ``pre_commit`` hook signature.
167
        # unregister all pre_commit hooks
5010.2.12 by Vincent Ladeuil
Fix imports in per_branch/test_commit.py.
168
        branch.Branch.hooks["pre_commit"] = []
2659.3.3 by NamNguyen
Changed ``pre_commit`` hook signature.
169
        # and re-register the capture hook
5010.2.12 by Vincent Ladeuil
Fix imports in per_branch/test_commit.py.
170
        branch.Branch.hooks.install_named_hook(
171
            "pre_commit", self.capture_pre_commit_hook, None)
2659.3.3 by NamNguyen
Changed ``pre_commit`` hook signature.
172
        # now these commits should go through
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
173
        for i in range(1, 3):
2659.3.3 by NamNguyen
Changed ``pre_commit`` hook signature.
174
            revids[i] = tree.commit('message')
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
175
        self.assertEqual([
5010.2.12 by Vincent Ladeuil
Fix imports in per_branch/test_commit.py.
176
            ('pre_commit', 0, revision.NULL_REVISION, 1, revids[0], root_delta),
177
            ('pre_commit', 0, revision.NULL_REVISION, 1, revids[1], root_delta),
2659.3.9 by NamNguyen
branch.py:
178
            ('pre_commit', 1, revids[1], 2, revids[2], empty_delta)
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
179
            ],
180
            self.hook_calls)
181
        tree.unlock()
2659.3.9 by NamNguyen
branch.py:
182
183
    def test_pre_commit_delta(self):
184
        # This tests the TreeDelta object passed to pre_commit hook.
185
        # This does not try to validate data correctness in the delta.
2659.3.6 by NamNguyen
branch_implementations/test_commit.py:
186
        self.build_tree(['rootfile', 'dir/', 'dir/subfile'])
187
        tree = self.make_branch_and_tree('.')
6844.1.1 by Jelmer Vernooij
Many more foreign branch fixes.
188
        with tree.lock_write():
2659.3.9 by NamNguyen
branch.py:
189
            # setting up a playground
6793.4.1 by Jelmer Vernooij
Improve set_root_id handling.
190
            tree.add('rootfile')
191
            rootfile_id = tree.path2id('rootfile')
6809.4.8 by Jelmer Vernooij
Fix some test failures.
192
            tree.put_file_bytes_non_atomic('rootfile', 'abc')
6793.4.1 by Jelmer Vernooij
Improve set_root_id handling.
193
            tree.add('dir')
194
            dir_id = tree.path2id('dir')
195
            tree.add('dir/subfile')
196
            dir_subfile_id = tree.path2id('dir/subfile')
6883.5.20 by Jelmer Vernooij
Fix another test for trees without rename tracking.
197
            tree.put_file_bytes_non_atomic('to_be_unversioned', 'blah')
198
            tree.add(['to_be_unversioned'])
6793.4.1 by Jelmer Vernooij
Improve set_root_id handling.
199
            to_be_unversioned_id = tree.path2id('to_be_unversioned')
6844.1.1 by Jelmer Vernooij
Many more foreign branch fixes.
200
            tree.put_file_bytes_non_atomic('dir/subfile', 'def')
2659.3.9 by NamNguyen
branch.py:
201
            revid1 = tree.commit('first revision')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
202
6844.1.1 by Jelmer Vernooij
Many more foreign branch fixes.
203
        with tree.lock_write():
2659.3.9 by NamNguyen
branch.py:
204
            # making changes
6809.4.8 by Jelmer Vernooij
Fix some test failures.
205
            tree.put_file_bytes_non_atomic('rootfile', 'jkl')
2659.3.9 by NamNguyen
branch.py:
206
            tree.rename_one('dir/subfile', 'dir/subfile_renamed')
6809.4.25 by Jelmer Vernooij
Add paths argument to .unversion.
207
            tree.unversion(['to_be_unversioned'])
6793.4.1 by Jelmer Vernooij
Improve set_root_id handling.
208
            tree.mkdir('added_dir')
209
            added_dir_id = tree.path2id('added_dir')
2659.3.9 by NamNguyen
branch.py:
210
            # start to capture pre_commit delta
5010.2.12 by Vincent Ladeuil
Fix imports in per_branch/test_commit.py.
211
            branch.Branch.hooks.install_named_hook(
212
                "pre_commit", self.capture_pre_commit_hook, None)
2659.3.9 by NamNguyen
branch.py:
213
            revid2 = tree.commit('second revision')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
214
5010.2.12 by Vincent Ladeuil
Fix imports in per_branch/test_commit.py.
215
        expected_delta = delta.TreeDelta()
6883.5.20 by Jelmer Vernooij
Fix another test for trees without rename tracking.
216
        if tree.has_versioned_directories():
217
            expected_delta.added.append(('added_dir', added_dir_id, 'directory'))
218
        if tree.supports_rename_tracking():
219
            expected_delta.removed = [('to_be_unversioned',
220
                                       to_be_unversioned_id, 'file')]
221
            expected_delta.renamed = [('dir/subfile', 'dir/subfile_renamed',
222
                                       dir_subfile_id, 'file', False, False)]
223
        else:
224
            expected_delta.added.append(('dir/subfile_renamed',
225
                                        tree.path2id('dir/subfile_renamed'), 'file'))
226
            expected_delta.removed = [
227
                    ('dir/subfile', dir_subfile_id, 'file'),
228
                    ('to_be_unversioned', to_be_unversioned_id, 'file')]
6793.4.1 by Jelmer Vernooij
Improve set_root_id handling.
229
        expected_delta.modified=[('rootfile', rootfile_id, 'file', True,
2659.3.9 by NamNguyen
branch.py:
230
                                  False)]
231
        self.assertEqual([('pre_commit', 1, revid1, 2, revid2,
2789.1.1 by Ian Clatworthy
(Nam Nguyen) Pre-commit hook
232
                           expected_delta)], self.hook_calls)