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_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
206
_mod_merge.Merger.hooks.install_named_hook(
207
'merge_file_content', hook_na, 'test hook (no-op)')
209
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
215
_mod_merge.Merger.hooks.install_named_hook(
216
'merge_file_content', hook_success, 'test hook (success)')
218
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
224
_mod_merge.Merger.hooks.install_named_hook(
225
'merge_file_content', hook_conflict, 'test hook (delete)')
227
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
233
_mod_merge.Merger.hooks.install_named_hook(
234
'merge_file_content', hook_delete, 'test hook (delete)')
236
def install_hook_log_lines(self):
237
"""Install a hook that saves the get_lines for the this, base and other
238
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
248
_mod_merge.Merger.hooks.install_named_hook(
249
'merge_file_content', hook_log_lines, 'test hook (log_lines)')
251
def make_merge_builder(self):
252
builder = MergeBuilder(self.test_base_dir)
253
self.addCleanup(builder.cleanup)
256
def create_file_needing_contents_merge(self, builder, file_id):
257
builder.add_file(file_id, builder.tree_root, "name1", "text1", True)
258
builder.change_contents(file_id, other="text4", this="text3")
260
def test_change_vs_change(self):
261
"""Hook is used for (changed, changed)"""
262
self.install_hook_success()
263
builder = self.make_merge_builder()
264
builder.add_file("1", builder.tree_root, "name1", "text1", True)
265
builder.change_contents("1", other="text4", this="text3")
266
conflicts = builder.merge(self.merge_type)
267
self.assertEqual(conflicts, [])
269
builder.this.get_file('1').read(), 'text-merged-by-hook')
271
def test_change_vs_deleted(self):
272
"""Hook is used for (changed, deleted)"""
273
self.install_hook_success()
274
builder = self.make_merge_builder()
275
builder.add_file("1", builder.tree_root, "name1", "text1", True)
276
builder.change_contents("1", this="text2")
277
builder.remove_file("1", other=True)
278
conflicts = builder.merge(self.merge_type)
279
self.assertEqual(conflicts, [])
281
builder.this.get_file('1').read(), 'text-merged-by-hook')
283
def test_result_can_be_delete(self):
284
"""A hook's result can be the deletion of a file."""
285
self.install_hook_delete()
286
builder = self.make_merge_builder()
287
self.create_file_needing_contents_merge(builder, "1")
288
conflicts = builder.merge(self.merge_type)
289
self.assertEqual(conflicts, [])
290
self.assertRaises(errors.NoSuchId, builder.this.id2path, '1')
291
self.assertEqual([], list(builder.this.list_files()))
293
def test_result_can_be_conflict(self):
294
"""A hook's result can be a conflict."""
295
self.install_hook_conflict()
296
builder = self.make_merge_builder()
297
self.create_file_needing_contents_merge(builder, "1")
298
conflicts = builder.merge(self.merge_type)
299
self.assertEqual(conflicts, [TextConflict('name1', file_id='1')])
300
# The hook still gets to set the file contents in this case, so that it
301
# can insert custom conflict markers.
303
builder.this.get_file('1').read(),
304
'text-with-conflict-markers-from-hook')
306
def test_can_access_this_other_and_base_versions(self):
307
"""The hook function can call params.merger.get_lines to access the
308
THIS/OTHER/BASE versions of the file.
310
self.install_hook_log_lines()
311
builder = self.make_merge_builder()
312
builder.add_file("1", builder.tree_root, "name1", "text1", True)
313
builder.change_contents("1", this="text2", other="text3")
314
conflicts = builder.merge(self.merge_type)
316
[('log_lines', ['text2'], ['text3'], ['text1'])], self.hook_log)
318
def test_chain_when_not_applicable(self):
319
"""When a hook function returns not_applicable, the next function is
320
tried (when one exists).
322
self.install_hook_noop()
323
self.install_hook_success()
324
builder = self.make_merge_builder()
325
self.create_file_needing_contents_merge(builder, "1")
326
conflicts = builder.merge(self.merge_type)
327
self.assertEqual(conflicts, [])
329
builder.this.get_file('1').read(), 'text-merged-by-hook')
330
self.assertEqual([('no-op',), ('success',)], self.hook_log)
332
def test_chain_stops_after_success(self):
333
"""When a hook function returns success, no later functions are tried.
335
self.install_hook_success()
336
self.install_hook_noop()
337
builder = self.make_merge_builder()
338
self.create_file_needing_contents_merge(builder, "1")
339
conflicts = builder.merge(self.merge_type)
340
self.assertEqual([('success',)], self.hook_log)
342
def test_chain_stops_after_conflict(self):
343
"""When a hook function returns conflict, no later functions are tried.
345
self.install_hook_conflict()
346
self.install_hook_noop()
347
builder = self.make_merge_builder()
348
self.create_file_needing_contents_merge(builder, "1")
349
conflicts = builder.merge(self.merge_type)
350
self.assertEqual([('conflict',)], self.hook_log)
352
def test_chain_stops_after_delete(self):
353
"""When a hook function returns delete, no later functions are tried.
355
self.install_hook_delete()
356
self.install_hook_noop()
357
builder = self.make_merge_builder()
358
self.create_file_needing_contents_merge(builder, "1")
359
conflicts = builder.merge(self.merge_type)
360
self.assertEqual([('delete',)], self.hook_log)