~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: NamNguyen
  • Date: 2007-08-27 08:38:37 UTC
  • mto: (2789.1.1 ianc-integration)
  • mto: This revision was merged to the branch mainline in revision 2790.
  • Revision ID: namnguyen-20070827083837-t6a5oewzvdct6j8i
branch.py:

   * ``pre_commit`` hook's signature is changed to::

   hook(local, master, old_revno, old_revid, future_revno, future_revid,
        tree_delta, future_tree)

   * made it clear that pre_commit hooks must not modify neither
     the delta nor future_tree

commit.py:

   * replaced ``affected_ids`` with ``tree_delta``

   * only compute ``future_tree`` and ``tree_delta`` once

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):
60
61
             new_revno, new_revid, local_locked, master.is_locked()))
61
62
 
62
63
    def capture_pre_commit_hook(self, local, master, old_revno, old_revid,
63
 
                                new_revno, new_revid, affected, tree):
 
64
                                new_revno, new_revid,
 
65
                                tree_delta, future_tree):
64
66
        self.hook_calls.append(('pre_commit', old_revno, old_revid,
65
 
                                new_revno, new_revid, affected))
 
67
                                new_revno, new_revid, tree_delta))
66
68
 
67
69
    def test_post_commit_to_origin(self):
68
70
        tree = self.make_branch_and_memory_tree('branch')
119
121
        tree.unlock()
120
122
    
121
123
    def test_pre_commit_passes(self):
 
124
        empty_delta = TreeDelta()
 
125
        root_delta = TreeDelta()
 
126
        root_delta.added = [('', '', 'directory')]
122
127
        tree = self.make_branch_and_memory_tree('branch')
123
128
        tree.lock_write()
124
 
        tree.add('')
 
129
        tree.add('', '')
125
130
        Branch.hooks.install_hook("pre_commit", self.capture_pre_commit_hook)
126
131
        revid1 = tree.commit('first revision')
127
132
        revid2 = tree.commit('second revision')
128
133
        self.assertEqual([
129
 
            ('pre_commit', 0, NULL_REVISION, 1, revid1, {}),
130
 
            ('pre_commit', 1, revid1, 2, revid2, {})
 
134
            ('pre_commit', 0, NULL_REVISION, 1, revid1, root_delta),
 
135
            ('pre_commit', 1, revid1, 2, revid2, empty_delta)
131
136
            ],
132
137
            self.hook_calls)
133
138
        tree.unlock()
134
139
 
135
140
    def test_pre_commit_fails(self):
 
141
        empty_delta = TreeDelta()
 
142
        root_delta = TreeDelta()
 
143
        root_delta.added = [('', '', 'directory')]
136
144
        tree = self.make_branch_and_memory_tree('branch')
137
145
        tree.lock_write()
138
 
        tree.add('')
 
146
        tree.add('', '')
139
147
        class PreCommitException(Exception): pass
140
 
        def hook_func(_1, _2, _3, _4, _5, new_revid, _7, _8):
 
148
        def hook_func(local, master,
 
149
                      old_revno, old_revid, new_revno, new_revid,
 
150
                      tree_delta, future_tree):
141
151
            raise PreCommitException(new_revid)
142
152
        Branch.hooks.install_hook("pre_commit", self.capture_pre_commit_hook)
143
153
        Branch.hooks.install_hook("pre_commit", hook_func)
144
154
        revids = [None, None, None]
145
 
        # this commit will raise exception
146
 
        # so the commit is rollbacked and revno unchanged
 
155
        # this commit will raise an exception
 
156
        # so the commit is rolled back and revno unchanged
147
157
        err = self.assertRaises(PreCommitException, tree.commit, 'message')
148
158
        # we have to record the revid to use in assertEqual later
149
159
        revids[0] = err.message
155
165
        for i in range(1, 3):
156
166
            revids[i] = tree.commit('message')
157
167
        self.assertEqual([
158
 
            ('pre_commit', 0, NULL_REVISION, 1, revids[0], {}),
159
 
            ('pre_commit', 0, NULL_REVISION, 1, revids[1], {}),
160
 
            ('pre_commit', 1, revids[1], 2, revids[2], {})
 
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)
161
171
            ],
162
172
            self.hook_calls)
163
173
        tree.unlock()
164
 
    
165
 
    def test_pre_commit_ids(self):
 
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.
166
178
        self.build_tree(['rootfile', 'dir/', 'dir/subfile'])
167
179
        tree = self.make_branch_and_tree('.')
168
180
        tree.lock_write()
169
 
        tree.set_root_id('root_id')
170
 
        tree.add('rootfile', 'rootfile_id')
171
 
        tree.put_file_bytes_non_atomic('rootfile_id', 'abc')
172
 
        tree.add('dir', 'dir_id')
173
 
        tree.add('dir/subfile', 'dir_subfile_id')
174
 
        tree.put_file_bytes_non_atomic('dir_subfile_id', 'def')
175
 
        Branch.hooks.install_hook("pre_commit", self.capture_pre_commit_hook)
176
 
        rev1 = tree.commit('first revision')
177
 
        tree.unversion(['dir_id'])
178
 
        rev2 = tree.commit('second revision')
179
 
        tree.put_file_bytes_non_atomic('rootfile_id', 'ghi')
180
 
        rev3 = tree.commit('third revision')
181
 
        tree.unlock()
182
 
        tree.lock_write()
183
 
        tree.rename_one('rootfile', 'renamed')
184
 
        rev4 = tree.commit('fourth revision')
185
 
        tree.unlock()
186
 
        tree.lock_write()
187
 
        tree.put_file_bytes_non_atomic('rootfile_id', 'jkl')
188
 
        tree.rename_one('renamed', 'rootfile')
189
 
        rev5 = tree.commit('fifth revision')
190
 
        tree.unlock()
191
 
        self.assertEqual([
192
 
            ('pre_commit', 0, NULL_REVISION, 1, rev1,
193
 
             {'added': ['dir_id', 'dir_subfile_id', 'rootfile_id']} ),
194
 
            ('pre_commit', 1, rev1, 2, rev2,
195
 
             {'deleted': ['dir_id', 'dir_subfile_id']} ),
196
 
            ('pre_commit', 2, rev2, 3, rev3,
197
 
             {'modified': ['rootfile_id']} ),
198
 
            ('pre_commit', 3, rev3, 4, rev4,
199
 
             {'renamed': ['rootfile_id']} ),
200
 
            ('pre_commit', 4, rev4, 5, rev5,
201
 
             {'modified and renamed': ['rootfile_id']} )
202
 
            ],
203
 
            self.hook_calls)
 
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)
 
 
b'\\ No newline at end of file'