198
198
TestCaseWithTransport.setUp(self)
199
199
self.hook_log = []
201
def install_hook_inactive(self):
202
def inactive_factory(merger):
203
# This hook is never active
204
self.hook_log.append(('inactive',))
206
_mod_merge.Merger.hooks.install_named_hook(
207
'merge_file_content', inactive_factory, 'test hook (inactive)')
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
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)')
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
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)')
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
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)')
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
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)')
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.
240
def hook_log_lines(merge_params):
241
self.hook_log.append((
243
merge_params.this_lines,
244
merge_params.other_lines,
245
merge_params.base_lines,
247
return 'not_applicable', None
266
class HookLogLines(_mod_merge.AbstractPerFileMerger):
267
def merge_contents(self, merge_params):
268
test.hook_log.append((
270
merge_params.this_lines,
271
merge_params.other_lines,
272
merge_params.base_lines,
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)')
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)
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, [])
357
builder.this.get_file('1').read(), 'text-merged-by-hook')
358
self.assertEqual([('inactive',), ('success',)], self.hook_log)
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).