~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/per_merger.py

  • Committer: Vincent Ladeuil
  • Date: 2010-01-20 16:31:24 UTC
  • mfrom: (4869.3.34 per-file-merge-hook)
  • mto: This revision was merged to the branch mainline in revision 4975.
  • Revision ID: v.ladeuil+lp@free.fr-20100120163124-fhf3nh8e1nu7w3y7
Implement a per-file merge hook

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
import os
20
20
 
 
21
from bzrlib.conflicts import TextConflict
21
22
from bzrlib import (
22
23
    errors,
23
24
    merge as _mod_merge,
28
29
    multiply_tests,
29
30
    TestCaseWithTransport,
30
31
    )
 
32
from bzrlib.tests.test_merge_core import MergeBuilder
31
33
from bzrlib.transform import TreeTransform
32
34
 
33
35
 
140
142
        this_tree = branch.bzrdir.create_workingtree()
141
143
        this_tree.lock_write()
142
144
        self.addCleanup(this_tree.unlock)
143
 
        other_tree = this_tree.bzrdir.sprout('other', 'OTHER-id').open_workingtree()
 
145
        other_tree = this_tree.bzrdir.sprout('other',
 
146
                                             'OTHER-id').open_workingtree()
144
147
        self.do_merge(this_tree, other_tree)
145
148
        if self.merge_type is _mod_merge.LCAMerger:
146
149
            self.expectFailure("lca merge doesn't track deleted lines",
172
175
        deletiondir = transform._deletiondir
173
176
        transform.finalize()
174
177
        return (limbodir, deletiondir)
175
 
    
 
178
 
176
179
    def test_merge_with_existing_limbo(self):
177
180
        wt = self.make_branch_and_tree('this')
178
181
        (limbodir, deletiondir) =  self.get_limbodir_deletiondir(wt)
188
191
        self.assertRaises(errors.LockError, wt.unlock)
189
192
 
190
193
 
 
194
class TestHookMergeFileContent(TestCaseWithTransport):
 
195
    """Tests that the 'merge_file_content' hook is invoked."""
 
196
 
 
197
    def setUp(self):
 
198
        TestCaseWithTransport.setUp(self)
 
199
        self.hook_log = []
 
200
 
 
201
    def install_hook_noop(self):
 
202
        def hook_na(merge_params):
 
203
            # This hook unconditionally does nothing.
 
204
            self.hook_log.append(('no-op',))
 
205
            return 'not_applicable', None
 
206
        _mod_merge.Merger.hooks.install_named_hook(
 
207
            'merge_file_content', hook_na, 'test hook (no-op)')
 
208
 
 
209
    def install_hook_success(self):
 
210
        def hook_success(merge_params):
 
211
            self.hook_log.append(('success',))
 
212
            if merge_params.file_id == '1':
 
213
                return 'success', ['text-merged-by-hook']
 
214
            return 'not_applicable', None
 
215
        _mod_merge.Merger.hooks.install_named_hook(
 
216
            'merge_file_content', hook_success, 'test hook (success)')
 
217
 
 
218
    def install_hook_conflict(self):
 
219
        def hook_conflict(merge_params):
 
220
            self.hook_log.append(('conflict',))
 
221
            if merge_params.file_id == '1':
 
222
                return 'conflicted', ['text-with-conflict-markers-from-hook']
 
223
            return 'not_applicable', None
 
224
        _mod_merge.Merger.hooks.install_named_hook(
 
225
            'merge_file_content', hook_conflict, 'test hook (delete)')
 
226
 
 
227
    def install_hook_delete(self):
 
228
        def hook_delete(merge_params):
 
229
            self.hook_log.append(('delete',))
 
230
            if merge_params.file_id == '1':
 
231
                return 'delete', None
 
232
            return 'not_applicable', None
 
233
        _mod_merge.Merger.hooks.install_named_hook(
 
234
            'merge_file_content', hook_delete, 'test hook (delete)')
 
235
 
 
236
    def install_hook_log_lines(self):
 
237
        """Install a hook that saves the get_lines for the this, base and other
 
238
        versions of the file.
 
239
        """
 
240
        def hook_log_lines(merge_params):
 
241
            self.hook_log.append((
 
242
                'log_lines',
 
243
                merge_params.this_lines,
 
244
                merge_params.other_lines,
 
245
                merge_params.base_lines,
 
246
                ))
 
247
            return 'not_applicable', None
 
248
        _mod_merge.Merger.hooks.install_named_hook(
 
249
            'merge_file_content', hook_log_lines, 'test hook (log_lines)')
 
250
 
 
251
    def make_merge_builder(self):
 
252
        builder = MergeBuilder(self.test_base_dir)
 
253
        self.addCleanup(builder.cleanup)
 
254
        return builder
 
255
 
 
256
    def create_file_needing_contents_merge(self, builder, file_id):
 
257
        builder.add_file(file_id, builder.tree_root, "name1", "text1", True)
 
258
        builder.change_contents(file_id, other="text4", this="text3")
 
259
 
 
260
    def test_change_vs_change(self):
 
261
        """Hook is used for (changed, changed)"""
 
262
        self.install_hook_success()
 
263
        builder = self.make_merge_builder()
 
264
        builder.add_file("1", builder.tree_root, "name1", "text1", True)
 
265
        builder.change_contents("1", other="text4", this="text3")
 
266
        conflicts = builder.merge(self.merge_type)
 
267
        self.assertEqual(conflicts, [])
 
268
        self.assertEqual(
 
269
            builder.this.get_file('1').read(), 'text-merged-by-hook')
 
270
 
 
271
    def test_change_vs_deleted(self):
 
272
        """Hook is used for (changed, deleted)"""
 
273
        self.install_hook_success()
 
274
        builder = self.make_merge_builder()
 
275
        builder.add_file("1", builder.tree_root, "name1", "text1", True)
 
276
        builder.change_contents("1", this="text2")
 
277
        builder.remove_file("1", other=True)
 
278
        conflicts = builder.merge(self.merge_type)
 
279
        self.assertEqual(conflicts, [])
 
280
        self.assertEqual(
 
281
            builder.this.get_file('1').read(), 'text-merged-by-hook')
 
282
 
 
283
    def test_result_can_be_delete(self):
 
284
        """A hook's result can be the deletion of a file."""
 
285
        self.install_hook_delete()
 
286
        builder = self.make_merge_builder()
 
287
        self.create_file_needing_contents_merge(builder, "1")
 
288
        conflicts = builder.merge(self.merge_type)
 
289
        self.assertEqual(conflicts, [])
 
290
        self.assertRaises(errors.NoSuchId, builder.this.id2path, '1')
 
291
        self.assertEqual([], list(builder.this.list_files()))
 
292
 
 
293
    def test_result_can_be_conflict(self):
 
294
        """A hook's result can be a conflict."""
 
295
        self.install_hook_conflict()
 
296
        builder = self.make_merge_builder()
 
297
        self.create_file_needing_contents_merge(builder, "1")
 
298
        conflicts = builder.merge(self.merge_type)
 
299
        self.assertEqual(conflicts, [TextConflict('name1', file_id='1')])
 
300
        # The hook still gets to set the file contents in this case, so that it
 
301
        # can insert custom conflict markers.
 
302
        self.assertEqual(
 
303
            builder.this.get_file('1').read(),
 
304
            'text-with-conflict-markers-from-hook')
 
305
 
 
306
    def test_can_access_this_other_and_base_versions(self):
 
307
        """The hook function can call params.merger.get_lines to access the
 
308
        THIS/OTHER/BASE versions of the file.
 
309
        """
 
310
        self.install_hook_log_lines()
 
311
        builder = self.make_merge_builder()
 
312
        builder.add_file("1", builder.tree_root, "name1", "text1", True)
 
313
        builder.change_contents("1", this="text2", other="text3")
 
314
        conflicts = builder.merge(self.merge_type)
 
315
        self.assertEqual(
 
316
            [('log_lines', ['text2'], ['text3'], ['text1'])], self.hook_log)
 
317
 
 
318
    def test_chain_when_not_applicable(self):
 
319
        """When a hook function returns not_applicable, the next function is
 
320
        tried (when one exists).
 
321
        """
 
322
        self.install_hook_noop()
 
323
        self.install_hook_success()
 
324
        builder = self.make_merge_builder()
 
325
        self.create_file_needing_contents_merge(builder, "1")
 
326
        conflicts = builder.merge(self.merge_type)
 
327
        self.assertEqual(conflicts, [])
 
328
        self.assertEqual(
 
329
            builder.this.get_file('1').read(), 'text-merged-by-hook')
 
330
        self.assertEqual([('no-op',), ('success',)], self.hook_log)
 
331
 
 
332
    def test_chain_stops_after_success(self):
 
333
        """When a hook function returns success, no later functions are tried.
 
334
        """
 
335
        self.install_hook_success()
 
336
        self.install_hook_noop()
 
337
        builder = self.make_merge_builder()
 
338
        self.create_file_needing_contents_merge(builder, "1")
 
339
        conflicts = builder.merge(self.merge_type)
 
340
        self.assertEqual([('success',)], self.hook_log)
 
341
 
 
342
    def test_chain_stops_after_conflict(self):
 
343
        """When a hook function returns conflict, no later functions are tried.
 
344
        """
 
345
        self.install_hook_conflict()
 
346
        self.install_hook_noop()
 
347
        builder = self.make_merge_builder()
 
348
        self.create_file_needing_contents_merge(builder, "1")
 
349
        conflicts = builder.merge(self.merge_type)
 
350
        self.assertEqual([('conflict',)], self.hook_log)
 
351
 
 
352
    def test_chain_stops_after_delete(self):
 
353
        """When a hook function returns delete, no later functions are tried.
 
354
        """
 
355
        self.install_hook_delete()
 
356
        self.install_hook_noop()
 
357
        builder = self.make_merge_builder()
 
358
        self.create_file_needing_contents_merge(builder, "1")
 
359
        conflicts = builder.merge(self.merge_type)
 
360
        self.assertEqual([('delete',)], self.hook_log)
191
361