~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-02-05 10:27:33 UTC
  • mto: (5008.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 5009.
  • Revision ID: v.ladeuil+lp@free.fr-20100205102733-8wpjnqz6g4nvrbfu
All Conflict action method names start with 'action_' to avoid potential namespace collisions

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009 Canonical Ltd
 
1
# Copyright (C) 2009, 2010 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
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_inactive(self):
 
202
        def inactive_factory(merger):
 
203
            # This hook is never active
 
204
            self.hook_log.append(('inactive',))
 
205
            return None
 
206
        _mod_merge.Merger.hooks.install_named_hook(
 
207
            'merge_file_content', inactive_factory, 'test hook (inactive)')
 
208
 
 
209
    def install_hook_noop(self):
 
210
        test = self
 
211
        class HookNA(_mod_merge.AbstractPerFileMerger):
 
212
            def merge_contents(self, merge_params):
 
213
                # This hook unconditionally does nothing.
 
214
                test.hook_log.append(('no-op',))
 
215
                return 'not_applicable', None
 
216
        def hook_na_factory(merger):
 
217
            return HookNA(merger)
 
218
        _mod_merge.Merger.hooks.install_named_hook(
 
219
            'merge_file_content', hook_na_factory, 'test hook (no-op)')
 
220
 
 
221
    def install_hook_success(self):
 
222
        test = self
 
223
        class HookSuccess(_mod_merge.AbstractPerFileMerger):
 
224
            def merge_contents(self, merge_params):
 
225
                test.hook_log.append(('success',))
 
226
                if merge_params.file_id == '1':
 
227
                    return 'success', ['text-merged-by-hook']
 
228
                return 'not_applicable', None
 
229
        def hook_success_factory(merger):
 
230
            return HookSuccess(merger)
 
231
        _mod_merge.Merger.hooks.install_named_hook(
 
232
            'merge_file_content', hook_success_factory, 'test hook (success)')
 
233
 
 
234
    def install_hook_conflict(self):
 
235
        test = self
 
236
        class HookConflict(_mod_merge.AbstractPerFileMerger):
 
237
            def merge_contents(self, merge_params):
 
238
                test.hook_log.append(('conflict',))
 
239
                if merge_params.file_id == '1':
 
240
                    return ('conflicted',
 
241
                        ['text-with-conflict-markers-from-hook'])
 
242
                return 'not_applicable', None
 
243
        def hook_conflict_factory(merger):
 
244
            return HookConflict(merger)
 
245
        _mod_merge.Merger.hooks.install_named_hook(
 
246
            'merge_file_content', hook_conflict_factory, 'test hook (delete)')
 
247
 
 
248
    def install_hook_delete(self):
 
249
        test = self
 
250
        class HookDelete(_mod_merge.AbstractPerFileMerger):
 
251
            def merge_contents(self, merge_params):
 
252
                test.hook_log.append(('delete',))
 
253
                if merge_params.file_id == '1':
 
254
                    return 'delete', None
 
255
                return 'not_applicable', None
 
256
        def hook_delete_factory(merger):
 
257
            return HookDelete(merger)
 
258
        _mod_merge.Merger.hooks.install_named_hook(
 
259
            'merge_file_content', hook_delete_factory, 'test hook (delete)')
 
260
 
 
261
    def install_hook_log_lines(self):
 
262
        """Install a hook that saves the get_lines for the this, base and other
 
263
        versions of the file.
 
264
        """
 
265
        test = self
 
266
        class HookLogLines(_mod_merge.AbstractPerFileMerger):
 
267
            def merge_contents(self, merge_params):
 
268
                test.hook_log.append((
 
269
                    'log_lines',
 
270
                    merge_params.this_lines,
 
271
                    merge_params.other_lines,
 
272
                    merge_params.base_lines,
 
273
                    ))
 
274
                return 'not_applicable', None
 
275
        def hook_log_lines_factory(merger):
 
276
            return HookLogLines(merger)
 
277
        _mod_merge.Merger.hooks.install_named_hook(
 
278
            'merge_file_content', hook_log_lines_factory,
 
279
            'test hook (log_lines)')
 
280
 
 
281
    def make_merge_builder(self):
 
282
        builder = MergeBuilder(self.test_base_dir)
 
283
        self.addCleanup(builder.cleanup)
 
284
        return builder
 
285
 
 
286
    def create_file_needing_contents_merge(self, builder, file_id):
 
287
        builder.add_file(file_id, builder.tree_root, "name1", "text1", True)
 
288
        builder.change_contents(file_id, other="text4", this="text3")
 
289
 
 
290
    def test_change_vs_change(self):
 
291
        """Hook is used for (changed, changed)"""
 
292
        self.install_hook_success()
 
293
        builder = self.make_merge_builder()
 
294
        builder.add_file("1", builder.tree_root, "name1", "text1", True)
 
295
        builder.change_contents("1", other="text4", this="text3")
 
296
        conflicts = builder.merge(self.merge_type)
 
297
        self.assertEqual(conflicts, [])
 
298
        self.assertEqual(
 
299
            builder.this.get_file('1').read(), 'text-merged-by-hook')
 
300
 
 
301
    def test_change_vs_deleted(self):
 
302
        """Hook is used for (changed, deleted)"""
 
303
        self.install_hook_success()
 
304
        builder = self.make_merge_builder()
 
305
        builder.add_file("1", builder.tree_root, "name1", "text1", True)
 
306
        builder.change_contents("1", this="text2")
 
307
        builder.remove_file("1", other=True)
 
308
        conflicts = builder.merge(self.merge_type)
 
309
        self.assertEqual(conflicts, [])
 
310
        self.assertEqual(
 
311
            builder.this.get_file('1').read(), 'text-merged-by-hook')
 
312
 
 
313
    def test_result_can_be_delete(self):
 
314
        """A hook's result can be the deletion of a file."""
 
315
        self.install_hook_delete()
 
316
        builder = self.make_merge_builder()
 
317
        self.create_file_needing_contents_merge(builder, "1")
 
318
        conflicts = builder.merge(self.merge_type)
 
319
        self.assertEqual(conflicts, [])
 
320
        self.assertRaises(errors.NoSuchId, builder.this.id2path, '1')
 
321
        self.assertEqual([], list(builder.this.list_files()))
 
322
 
 
323
    def test_result_can_be_conflict(self):
 
324
        """A hook's result can be a conflict."""
 
325
        self.install_hook_conflict()
 
326
        builder = self.make_merge_builder()
 
327
        self.create_file_needing_contents_merge(builder, "1")
 
328
        conflicts = builder.merge(self.merge_type)
 
329
        self.assertEqual(conflicts, [TextConflict('name1', file_id='1')])
 
330
        # The hook still gets to set the file contents in this case, so that it
 
331
        # can insert custom conflict markers.
 
332
        self.assertEqual(
 
333
            builder.this.get_file('1').read(),
 
334
            'text-with-conflict-markers-from-hook')
 
335
 
 
336
    def test_can_access_this_other_and_base_versions(self):
 
337
        """The hook function can call params.merger.get_lines to access the
 
338
        THIS/OTHER/BASE versions of the file.
 
339
        """
 
340
        self.install_hook_log_lines()
 
341
        builder = self.make_merge_builder()
 
342
        builder.add_file("1", builder.tree_root, "name1", "text1", True)
 
343
        builder.change_contents("1", this="text2", other="text3")
 
344
        conflicts = builder.merge(self.merge_type)
 
345
        self.assertEqual(
 
346
            [('log_lines', ['text2'], ['text3'], ['text1'])], self.hook_log)
 
347
 
 
348
    def test_chain_when_not_active(self):
 
349
        """When a hook function returns None, merging still works."""
 
350
        self.install_hook_inactive()
 
351
        self.install_hook_success()
 
352
        builder = self.make_merge_builder()
 
353
        self.create_file_needing_contents_merge(builder, "1")
 
354
        conflicts = builder.merge(self.merge_type)
 
355
        self.assertEqual(conflicts, [])
 
356
        self.assertEqual(
 
357
            builder.this.get_file('1').read(), 'text-merged-by-hook')
 
358
        self.assertEqual([('inactive',), ('success',)], self.hook_log)
 
359
 
 
360
    def test_chain_when_not_applicable(self):
 
361
        """When a hook function returns not_applicable, the next function is
 
362
        tried (when one exists).
 
363
        """
 
364
        self.install_hook_noop()
 
365
        self.install_hook_success()
 
366
        builder = self.make_merge_builder()
 
367
        self.create_file_needing_contents_merge(builder, "1")
 
368
        conflicts = builder.merge(self.merge_type)
 
369
        self.assertEqual(conflicts, [])
 
370
        self.assertEqual(
 
371
            builder.this.get_file('1').read(), 'text-merged-by-hook')
 
372
        self.assertEqual([('no-op',), ('success',)], self.hook_log)
 
373
 
 
374
    def test_chain_stops_after_success(self):
 
375
        """When a hook function returns success, no later functions are tried.
 
376
        """
 
377
        self.install_hook_success()
 
378
        self.install_hook_noop()
 
379
        builder = self.make_merge_builder()
 
380
        self.create_file_needing_contents_merge(builder, "1")
 
381
        conflicts = builder.merge(self.merge_type)
 
382
        self.assertEqual([('success',)], self.hook_log)
 
383
 
 
384
    def test_chain_stops_after_conflict(self):
 
385
        """When a hook function returns conflict, no later functions are tried.
 
386
        """
 
387
        self.install_hook_conflict()
 
388
        self.install_hook_noop()
 
389
        builder = self.make_merge_builder()
 
390
        self.create_file_needing_contents_merge(builder, "1")
 
391
        conflicts = builder.merge(self.merge_type)
 
392
        self.assertEqual([('conflict',)], self.hook_log)
 
393
 
 
394
    def test_chain_stops_after_delete(self):
 
395
        """When a hook function returns delete, no later functions are tried.
 
396
        """
 
397
        self.install_hook_delete()
 
398
        self.install_hook_noop()
 
399
        builder = self.make_merge_builder()
 
400
        self.create_file_needing_contents_merge(builder, "1")
 
401
        conflicts = builder.merge(self.merge_type)
 
402
        self.assertEqual([('delete',)], self.hook_log)
191
403