~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/branch_implementations/test_commit.py

Merge Robert and indirectly trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
from bzrlib.tests.branch_implementations.test_branch import TestCaseWithBranch
22
22
from bzrlib.revision import NULL_REVISION
23
23
from bzrlib.transport import get_transport
 
24
from bzrlib.delta import TreeDelta
24
25
 
25
26
 
26
27
class TestCommit(TestCaseWithBranch):
59
60
            ('post_commit', local_base, master.base, old_revno, old_revid,
60
61
             new_revno, new_revid, local_locked, master.is_locked()))
61
62
 
 
63
    def capture_pre_commit_hook(self, local, master, old_revno, old_revid,
 
64
                                new_revno, new_revid,
 
65
                                tree_delta, future_tree):
 
66
        self.hook_calls.append(('pre_commit', old_revno, old_revid,
 
67
                                new_revno, new_revid, tree_delta))
 
68
 
62
69
    def test_post_commit_to_origin(self):
63
70
        tree = self.make_branch_and_memory_tree('branch')
64
71
        Branch.hooks.install_hook('post_commit',
112
119
            ],
113
120
            self.hook_calls)
114
121
        tree.unlock()
 
122
    
 
123
    def test_pre_commit_passes(self):
 
124
        empty_delta = TreeDelta()
 
125
        root_delta = TreeDelta()
 
126
        root_delta.added = [('', '', 'directory')]
 
127
        tree = self.make_branch_and_memory_tree('branch')
 
128
        tree.lock_write()
 
129
        tree.add('', '')
 
130
        Branch.hooks.install_hook("pre_commit", self.capture_pre_commit_hook)
 
131
        revid1 = tree.commit('first revision')
 
132
        revid2 = tree.commit('second revision')
 
133
        self.assertEqual([
 
134
            ('pre_commit', 0, NULL_REVISION, 1, revid1, root_delta),
 
135
            ('pre_commit', 1, revid1, 2, revid2, empty_delta)
 
136
            ],
 
137
            self.hook_calls)
 
138
        tree.unlock()
 
139
 
 
140
    def test_pre_commit_fails(self):
 
141
        empty_delta = TreeDelta()
 
142
        root_delta = TreeDelta()
 
143
        root_delta.added = [('', '', 'directory')]
 
144
        tree = self.make_branch_and_memory_tree('branch')
 
145
        tree.lock_write()
 
146
        tree.add('', '')
 
147
        class PreCommitException(Exception): pass
 
148
        def hook_func(local, master,
 
149
                      old_revno, old_revid, new_revno, new_revid,
 
150
                      tree_delta, future_tree):
 
151
            raise PreCommitException(new_revid)
 
152
        Branch.hooks.install_hook("pre_commit", self.capture_pre_commit_hook)
 
153
        Branch.hooks.install_hook("pre_commit", hook_func)
 
154
        revids = [None, None, None]
 
155
        # this commit will raise an exception
 
156
        # so the commit is rolled back and revno unchanged
 
157
        err = self.assertRaises(PreCommitException, tree.commit, 'message')
 
158
        # we have to record the revid to use in assertEqual later
 
159
        revids[0] = str(err)
 
160
        # unregister all pre_commit hooks
 
161
        Branch.hooks["pre_commit"] = []
 
162
        # and re-register the capture hook
 
163
        Branch.hooks.install_hook("pre_commit", self.capture_pre_commit_hook)
 
164
        # now these commits should go through
 
165
        for i in range(1, 3):
 
166
            revids[i] = tree.commit('message')
 
167
        self.assertEqual([
 
168
            ('pre_commit', 0, NULL_REVISION, 1, revids[0], root_delta),
 
169
            ('pre_commit', 0, NULL_REVISION, 1, revids[1], root_delta),
 
170
            ('pre_commit', 1, revids[1], 2, revids[2], empty_delta)
 
171
            ],
 
172
            self.hook_calls)
 
173
        tree.unlock()
 
174
 
 
175
    def test_pre_commit_delta(self):
 
176
        # This tests the TreeDelta object passed to pre_commit hook.
 
177
        # This does not try to validate data correctness in the delta.
 
178
        self.build_tree(['rootfile', 'dir/', 'dir/subfile'])
 
179
        tree = self.make_branch_and_tree('.')
 
180
        tree.lock_write()
 
181
        try:
 
182
            # setting up a playground
 
183
            tree.set_root_id('root_id')
 
184
            tree.add('rootfile', 'rootfile_id')
 
185
            tree.put_file_bytes_non_atomic('rootfile_id', 'abc')
 
186
            tree.add('dir', 'dir_id')
 
187
            tree.add('dir/subfile', 'dir_subfile_id')
 
188
            tree.mkdir('to_be_unversioned', 'to_be_unversioned_id')
 
189
            tree.put_file_bytes_non_atomic('dir_subfile_id', 'def')
 
190
            revid1 = tree.commit('first revision')
 
191
        finally:
 
192
            tree.unlock()
 
193
        
 
194
        tree.lock_write()
 
195
        try:
 
196
            # making changes
 
197
            tree.put_file_bytes_non_atomic('rootfile_id', 'jkl')
 
198
            tree.rename_one('dir/subfile', 'dir/subfile_renamed')
 
199
            tree.unversion(['to_be_unversioned_id'])
 
200
            tree.mkdir('added_dir', 'added_dir_id')
 
201
            # start to capture pre_commit delta
 
202
            Branch.hooks.install_hook("pre_commit", self.capture_pre_commit_hook)
 
203
            revid2 = tree.commit('second revision')
 
204
        finally:
 
205
            tree.unlock()
 
206
        
 
207
        expected_delta = TreeDelta()
 
208
        expected_delta.added = [('added_dir', 'added_dir_id', 'directory')]
 
209
        expected_delta.removed = [('to_be_unversioned',
 
210
                                   'to_be_unversioned_id', 'directory')]
 
211
        expected_delta.renamed = [('dir/subfile', 'dir/subfile_renamed',
 
212
                                   'dir_subfile_id', 'file', False, False)]
 
213
        expected_delta.modified=[('rootfile', 'rootfile_id', 'file', True,
 
214
                                  False)]
 
215
        self.assertEqual([('pre_commit', 1, revid1, 2, revid2,
 
216
                           expected_delta)], self.hook_calls)