190
188
self.assertRaises(errors.LockError, wt.unlock)
193
class TestHookMergeFileContent(TestCaseWithTransport):
194
"""Tests that the 'merge_file_content' hook is invoked."""
197
TestCaseWithTransport.setUp(self)
200
def install_hook_inactive(self):
201
def inactive_factory(merger):
202
# This hook is never active
203
self.hook_log.append(('inactive',))
205
_mod_merge.Merger.hooks.install_named_hook(
206
'merge_file_content', inactive_factory, 'test hook (inactive)')
208
def install_hook_noop(self):
210
class HookNA(_mod_merge.AbstractPerFileMerger):
211
def merge_contents(self, merge_params):
212
# This hook unconditionally does nothing.
213
test.hook_log.append(('no-op',))
214
return 'not_applicable', None
215
def hook_na_factory(merger):
216
return HookNA(merger)
217
_mod_merge.Merger.hooks.install_named_hook(
218
'merge_file_content', hook_na_factory, 'test hook (no-op)')
220
def install_hook_success(self):
222
class HookSuccess(_mod_merge.AbstractPerFileMerger):
223
def merge_contents(self, merge_params):
224
test.hook_log.append(('success',))
225
if merge_params.file_id == '1':
226
return 'success', ['text-merged-by-hook']
227
return 'not_applicable', None
228
def hook_success_factory(merger):
229
return HookSuccess(merger)
230
_mod_merge.Merger.hooks.install_named_hook(
231
'merge_file_content', hook_success_factory, 'test hook (success)')
233
def install_hook_conflict(self):
235
class HookConflict(_mod_merge.AbstractPerFileMerger):
236
def merge_contents(self, merge_params):
237
test.hook_log.append(('conflict',))
238
if merge_params.file_id == '1':
239
return ('conflicted',
240
['text-with-conflict-markers-from-hook'])
241
return 'not_applicable', None
242
def hook_conflict_factory(merger):
243
return HookConflict(merger)
244
_mod_merge.Merger.hooks.install_named_hook(
245
'merge_file_content', hook_conflict_factory, 'test hook (delete)')
247
def install_hook_delete(self):
249
class HookDelete(_mod_merge.AbstractPerFileMerger):
250
def merge_contents(self, merge_params):
251
test.hook_log.append(('delete',))
252
if merge_params.file_id == '1':
253
return 'delete', None
254
return 'not_applicable', None
255
def hook_delete_factory(merger):
256
return HookDelete(merger)
257
_mod_merge.Merger.hooks.install_named_hook(
258
'merge_file_content', hook_delete_factory, 'test hook (delete)')
260
def install_hook_log_lines(self):
261
"""Install a hook that saves the get_lines for the this, base and other
262
versions of the file.
265
class HookLogLines(_mod_merge.AbstractPerFileMerger):
266
def merge_contents(self, merge_params):
267
test.hook_log.append((
269
merge_params.this_lines,
270
merge_params.other_lines,
271
merge_params.base_lines,
273
return 'not_applicable', None
274
def hook_log_lines_factory(merger):
275
return HookLogLines(merger)
276
_mod_merge.Merger.hooks.install_named_hook(
277
'merge_file_content', hook_log_lines_factory,
278
'test hook (log_lines)')
280
def make_merge_builder(self):
281
builder = MergeBuilder(self.test_base_dir)
282
self.addCleanup(builder.cleanup)
285
def create_file_needing_contents_merge(self, builder, file_id):
286
builder.add_file(file_id, builder.tree_root, "name1", "text1", True)
287
builder.change_contents(file_id, other="text4", this="text3")
289
def test_change_vs_change(self):
290
"""Hook is used for (changed, changed)"""
291
self.install_hook_success()
292
builder = self.make_merge_builder()
293
builder.add_file("1", builder.tree_root, "name1", "text1", True)
294
builder.change_contents("1", other="text4", this="text3")
295
conflicts = builder.merge(self.merge_type)
296
self.assertEqual(conflicts, [])
298
builder.this.get_file('1').read(), 'text-merged-by-hook')
300
def test_change_vs_deleted(self):
301
"""Hook is used for (changed, deleted)"""
302
self.install_hook_success()
303
builder = self.make_merge_builder()
304
builder.add_file("1", builder.tree_root, "name1", "text1", True)
305
builder.change_contents("1", this="text2")
306
builder.remove_file("1", other=True)
307
conflicts = builder.merge(self.merge_type)
308
self.assertEqual(conflicts, [])
310
builder.this.get_file('1').read(), 'text-merged-by-hook')
312
def test_result_can_be_delete(self):
313
"""A hook's result can be the deletion of a file."""
314
self.install_hook_delete()
315
builder = self.make_merge_builder()
316
self.create_file_needing_contents_merge(builder, "1")
317
conflicts = builder.merge(self.merge_type)
318
self.assertEqual(conflicts, [])
319
self.assertRaises(errors.NoSuchId, builder.this.id2path, '1')
320
self.assertEqual([], list(builder.this.list_files()))
322
def test_result_can_be_conflict(self):
323
"""A hook's result can be a conflict."""
324
self.install_hook_conflict()
325
builder = self.make_merge_builder()
326
self.create_file_needing_contents_merge(builder, "1")
327
conflicts = builder.merge(self.merge_type)
328
self.assertEqual(conflicts, [TextConflict('name1', file_id='1')])
329
# The hook still gets to set the file contents in this case, so that it
330
# can insert custom conflict markers.
332
builder.this.get_file('1').read(),
333
'text-with-conflict-markers-from-hook')
335
def test_can_access_this_other_and_base_versions(self):
336
"""The hook function can call params.merger.get_lines to access the
337
THIS/OTHER/BASE versions of the file.
339
self.install_hook_log_lines()
340
builder = self.make_merge_builder()
341
builder.add_file("1", builder.tree_root, "name1", "text1", True)
342
builder.change_contents("1", this="text2", other="text3")
343
conflicts = builder.merge(self.merge_type)
345
[('log_lines', ['text2'], ['text3'], ['text1'])], self.hook_log)
347
def test_chain_when_not_active(self):
348
"""When a hook function returns None, merging still works."""
349
self.install_hook_inactive()
350
self.install_hook_success()
351
builder = self.make_merge_builder()
352
self.create_file_needing_contents_merge(builder, "1")
353
conflicts = builder.merge(self.merge_type)
354
self.assertEqual(conflicts, [])
356
builder.this.get_file('1').read(), 'text-merged-by-hook')
357
self.assertEqual([('inactive',), ('success',)], self.hook_log)
359
def test_chain_when_not_applicable(self):
360
"""When a hook function returns not_applicable, the next function is
361
tried (when one exists).
363
self.install_hook_noop()
364
self.install_hook_success()
365
builder = self.make_merge_builder()
366
self.create_file_needing_contents_merge(builder, "1")
367
conflicts = builder.merge(self.merge_type)
368
self.assertEqual(conflicts, [])
370
builder.this.get_file('1').read(), 'text-merged-by-hook')
371
self.assertEqual([('no-op',), ('success',)], self.hook_log)
373
def test_chain_stops_after_success(self):
374
"""When a hook function returns success, no later functions are tried.
376
self.install_hook_success()
377
self.install_hook_noop()
378
builder = self.make_merge_builder()
379
self.create_file_needing_contents_merge(builder, "1")
380
conflicts = builder.merge(self.merge_type)
381
self.assertEqual([('success',)], self.hook_log)
383
def test_chain_stops_after_conflict(self):
384
"""When a hook function returns conflict, no later functions are tried.
386
self.install_hook_conflict()
387
self.install_hook_noop()
388
builder = self.make_merge_builder()
389
self.create_file_needing_contents_merge(builder, "1")
390
conflicts = builder.merge(self.merge_type)
391
self.assertEqual([('conflict',)], self.hook_log)
393
def test_chain_stops_after_delete(self):
394
"""When a hook function returns delete, no later functions are tried.
396
self.install_hook_delete()
397
self.install_hook_noop()
398
builder = self.make_merge_builder()
399
self.create_file_needing_contents_merge(builder, "1")
400
conflicts = builder.merge(self.merge_type)
401
self.assertEqual([('delete',)], self.hook_log)