~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/per_merger.py

Hook is invoked for delete vs. change conflicts, and may choose to result in deleting the file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
193
193
class TestHookMergeFileContent(TestCaseWithTransport):
194
194
    """Tests that the 'merge_file_content' hook is invoked."""
195
195
 
196
 
    def install_hook_merge_file_content(self):
 
196
    def install_hook_noop(self):
197
197
        def hook_na(merge_params):
198
198
            # This hook unconditionally does nothing.
199
199
            return 'not_applicable', None
200
200
        _mod_merge.Merger.hooks.install_named_hook(
201
201
            'merge_file_content', hook_na, 'test hook (n/a)')
 
202
 
 
203
    def install_hook_success(self):
202
204
        def hook_success(merge_params):
203
205
            if merge_params.file_id == '1':
204
206
                return 'success', ['text-merged-by-hook']
206
208
        _mod_merge.Merger.hooks.install_named_hook(
207
209
            'merge_file_content', hook_success, 'test hook (success)')
208
210
        
 
211
    def install_hook_delete(self):
 
212
        def hook_delete(merge_params):
 
213
            if merge_params.file_id == '1':
 
214
                return 'delete', None
 
215
            return 'not_applicable', None
 
216
        _mod_merge.Merger.hooks.install_named_hook(
 
217
            'merge_file_content', hook_delete, 'test hook (delete)')
 
218
        
209
219
    def make_merge_builder(self):
210
220
        builder = MergeBuilder(self.test_base_dir)
211
221
        self.addCleanup(builder.cleanup)
212
222
        return builder
213
223
 
214
 
    def test_hook_merge_file_content(self):
215
 
        self.install_hook_merge_file_content()
 
224
    def test_change_vs_change(self):
 
225
        """Hook is used for (changed, changed)"""
 
226
        self.install_hook_success()
216
227
        builder = self.make_merge_builder()
217
228
        builder.add_file("1", builder.tree_root, "name1", "text1", True)
218
229
        builder.change_contents("1", other="text4", this="text3")
221
232
        self.assertEqual(
222
233
            builder.this.get_file('1').read(), 'text-merged-by-hook')
223
234
 
 
235
    def test_change_vs_deleted(self):
 
236
        """Hook is used for (changed, deleted)"""
 
237
        self.install_hook_success()
 
238
        builder = self.make_merge_builder()
 
239
        builder.add_file("1", builder.tree_root, "name1", "text1", True)
 
240
        builder.change_contents("1", this="text2")
 
241
        builder.remove_file("1", other=True)
 
242
        conflicts = builder.merge(self.merge_type)
 
243
        self.assertEqual(conflicts, [])
 
244
        self.assertEqual(
 
245
            builder.this.get_file('1').read(), 'text-merged-by-hook')
 
246
 
 
247
    def test_result_can_be_delete(self):
 
248
        """A hook's result can be the deletion of a file."""
 
249
        self.install_hook_delete()
 
250
        builder = self.make_merge_builder()
 
251
        builder.add_file("1", builder.tree_root, "name1", "text1", True)
 
252
        builder.change_contents("1", this="text2", other="text3")
 
253
        conflicts = builder.merge(self.merge_type)
 
254
        self.assertEqual(conflicts, [])
 
255
        self.assertRaises(errors.NoSuchId, builder.this.id2path, '1')
 
256