/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/per_merger.py

  • Committer: Vincent Ladeuil
  • Date: 2010-01-25 15:55:48 UTC
  • mto: (4985.1.4 add-attr-cleanup)
  • mto: This revision was merged to the branch mainline in revision 4988.
  • Revision ID: v.ladeuil+lp@free.fr-20100125155548-0l352pujvt5bzl5e
Deploy addAttrCleanup on the whole test suite.

Several use case worth mentioning:

- setting a module or any other object attribute is the majority
by far. In some cases the setting itself is deferred but most of
the time we want to set at the same time we add the cleanup.

- there multiple occurrences of protecting hooks or ui factory
which are now useless (the test framework takes care of that now),

- there was some lambda uses that can now be avoided.

That first cleanup already simplifies things a lot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009, 2010 Canonical Ltd
 
1
# Copyright (C) 2009 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
46
46
class TestMergeImplementation(TestCaseWithTransport):
47
47
 
48
48
    def do_merge(self, target_tree, source_tree, **kwargs):
49
 
        merger = _mod_merge.Merger.from_revision_ids(None,
 
49
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
50
50
            target_tree, source_tree.last_revision(),
51
51
            other_branch=source_tree.branch)
52
52
        merger.merge_type=self.merge_type
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
 
 
209
201
    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)
 
202
        def hook_na(merge_params):
 
203
            # This hook unconditionally does nothing.
 
204
            self.hook_log.append(('no-op',))
 
205
            return 'not_applicable', None
218
206
        _mod_merge.Merger.hooks.install_named_hook(
219
 
            'merge_file_content', hook_na_factory, 'test hook (no-op)')
 
207
            'merge_file_content', hook_na, 'test hook (no-op)')
220
208
 
221
209
    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)
 
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
231
215
        _mod_merge.Merger.hooks.install_named_hook(
232
 
            'merge_file_content', hook_success_factory, 'test hook (success)')
 
216
            'merge_file_content', hook_success, 'test hook (success)')
233
217
 
234
218
    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)
 
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
245
224
        _mod_merge.Merger.hooks.install_named_hook(
246
 
            'merge_file_content', hook_conflict_factory, 'test hook (delete)')
 
225
            'merge_file_content', hook_conflict, 'test hook (delete)')
247
226
 
248
227
    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)
 
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
258
233
        _mod_merge.Merger.hooks.install_named_hook(
259
 
            'merge_file_content', hook_delete_factory, 'test hook (delete)')
 
234
            'merge_file_content', hook_delete, 'test hook (delete)')
260
235
 
261
236
    def install_hook_log_lines(self):
262
237
        """Install a hook that saves the get_lines for the this, base and other
263
238
        versions of the file.
264
239
        """
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)
 
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
277
248
        _mod_merge.Merger.hooks.install_named_hook(
278
 
            'merge_file_content', hook_log_lines_factory,
279
 
            'test hook (log_lines)')
 
249
            'merge_file_content', hook_log_lines, 'test hook (log_lines)')
280
250
 
281
251
    def make_merge_builder(self):
282
252
        builder = MergeBuilder(self.test_base_dir)
345
315
        self.assertEqual(
346
316
            [('log_lines', ['text2'], ['text3'], ['text1'])], self.hook_log)
347
317
 
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
318
    def test_chain_when_not_applicable(self):
361
319
        """When a hook function returns not_applicable, the next function is
362
320
        tried (when one exists).