~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/per_merger.py

  • Committer: John Arbash Meinel
  • Date: 2010-01-29 08:28:47 UTC
  • mfrom: (4797.2.5 2.1)
  • mto: This revision was merged to the branch mainline in revision 4989.
  • Revision ID: john@arbash-meinel.com-20100129082847-pnx07l2xcbenkcrs
Merge the per-file-hook updates.

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
198
198
        TestCaseWithTransport.setUp(self)
199
199
        self.hook_log = []
200
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
 
201
209
    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
 
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)
206
218
        _mod_merge.Merger.hooks.install_named_hook(
207
 
            'merge_file_content', hook_na, 'test hook (no-op)')
 
219
            'merge_file_content', hook_na_factory, 'test hook (no-op)')
208
220
 
209
221
    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
 
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)
215
231
        _mod_merge.Merger.hooks.install_named_hook(
216
 
            'merge_file_content', hook_success, 'test hook (success)')
 
232
            'merge_file_content', hook_success_factory, 'test hook (success)')
217
233
 
218
234
    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
 
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)
224
245
        _mod_merge.Merger.hooks.install_named_hook(
225
 
            'merge_file_content', hook_conflict, 'test hook (delete)')
 
246
            'merge_file_content', hook_conflict_factory, 'test hook (delete)')
226
247
 
227
248
    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
 
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)
233
258
        _mod_merge.Merger.hooks.install_named_hook(
234
 
            'merge_file_content', hook_delete, 'test hook (delete)')
 
259
            'merge_file_content', hook_delete_factory, 'test hook (delete)')
235
260
 
236
261
    def install_hook_log_lines(self):
237
262
        """Install a hook that saves the get_lines for the this, base and other
238
263
        versions of the file.
239
264
        """
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
 
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)
248
277
        _mod_merge.Merger.hooks.install_named_hook(
249
 
            'merge_file_content', hook_log_lines, 'test hook (log_lines)')
 
278
            'merge_file_content', hook_log_lines_factory,
 
279
            'test hook (log_lines)')
250
280
 
251
281
    def make_merge_builder(self):
252
282
        builder = MergeBuilder(self.test_base_dir)
315
345
        self.assertEqual(
316
346
            [('log_lines', ['text2'], ['text3'], ['text1'])], self.hook_log)
317
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
 
318
360
    def test_chain_when_not_applicable(self):
319
361
        """When a hook function returns not_applicable, the next function is
320
362
        tried (when one exists).