188
191
self.assertRaises(errors.LockError, wt.unlock)
194
class TestHookMergeFileContent(TestCaseWithTransport):
195
"""Tests that the 'merge_file_content' hook is invoked."""
198
TestCaseWithTransport.setUp(self)
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)')
209
def install_hook_noop(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)')
221
def install_hook_success(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)')
234
def install_hook_conflict(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)')
248
def install_hook_delete(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)')
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.
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)
277
_mod_merge.Merger.hooks.install_named_hook(
278
'merge_file_content', hook_log_lines_factory,
279
'test hook (log_lines)')
281
def make_merge_builder(self):
282
builder = MergeBuilder(self.test_base_dir)
283
self.addCleanup(builder.cleanup)
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")
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, [])
299
builder.this.get_file('1').read(), 'text-merged-by-hook')
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, [])
311
builder.this.get_file('1').read(), 'text-merged-by-hook')
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()))
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.
333
builder.this.get_file('1').read(),
334
'text-with-conflict-markers-from-hook')
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.
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)
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)
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).
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, [])
371
builder.this.get_file('1').read(), 'text-merged-by-hook')
372
self.assertEqual([('no-op',), ('success',)], self.hook_log)
374
def test_chain_stops_after_success(self):
375
"""When a hook function returns success, no later functions are tried.
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)
384
def test_chain_stops_after_conflict(self):
385
"""When a hook function returns conflict, no later functions are tried.
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)
394
def test_chain_stops_after_delete(self):
395
"""When a hook function returns delete, no later functions are tried.
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)