285
288
new_trans_id = transform.new_directory('', ROOT_PARENT, 'alt-root-id')
286
289
self.assertRaises(ValueError, transform.fixup_new_roots)
291
def test_retain_existing_root(self):
292
tt, root = self.get_transform()
294
tt.new_directory('', ROOT_PARENT, 'new-root-id')
296
self.assertNotEqual('new-root-id', tt.final_file_id(tt.root))
298
def test_retain_existing_root_added_file(self):
299
tt, root = self.get_transform()
300
new_trans_id = tt.new_directory('', ROOT_PARENT, 'new-root-id')
301
child = tt.new_directory('child', new_trans_id, 'child-id')
303
self.assertEqual(tt.root, tt.final_parent(child))
305
def test_add_unversioned_root(self):
306
transform, root = self.get_transform()
307
new_trans_id = transform.new_directory('', ROOT_PARENT, None)
308
transform.delete_contents(transform.root)
309
transform.fixup_new_roots()
310
self.assertNotIn(transform.root, transform._new_id)
312
def test_remove_root_fixup(self):
313
transform, root = self.get_transform()
314
old_root_id = self.wt.get_root_id()
315
self.assertNotEqual('new-root-id', old_root_id)
316
transform.delete_contents(root)
317
transform.unversion_file(root)
318
transform.fixup_new_roots()
320
self.assertEqual(old_root_id, self.wt.get_root_id())
322
transform, root = self.get_transform()
323
new_trans_id = transform.new_directory('', ROOT_PARENT, 'new-root-id')
324
new_trans_id = transform.new_directory('', ROOT_PARENT, 'alt-root-id')
325
self.assertRaises(ValueError, transform.fixup_new_roots)
327
def test_fixup_new_roots_permits_empty_tree(self):
328
transform, root = self.get_transform()
329
transform.delete_contents(root)
330
transform.unversion_file(root)
331
transform.fixup_new_roots()
332
self.assertIs(None, transform.final_kind(root))
333
self.assertIs(None, transform.final_file_id(root))
335
def test_apply_retains_root_directory(self):
336
# Do not attempt to delete the physical root directory, because that
338
transform, root = self.get_transform()
340
transform.delete_contents(root)
341
e = self.assertRaises(AssertionError, self.assertRaises,
342
errors.TransformRenameFailed,
344
self.assertContainsRe('TransformRenameFailed not raised', str(e))
346
def test_apply_retains_file_id(self):
347
transform, root = self.get_transform()
348
old_root_id = transform.tree_file_id(root)
349
transform.unversion_file(root)
351
self.assertEqual(old_root_id, self.wt.get_root_id())
288
353
def test_hardlink(self):
289
354
self.requireFeature(HardlinkFeature)
290
355
transform, root = self.get_transform()
1599
1657
return template % ('<' * 7, tree, '=' * 7, merge, '>' * 7)
1660
class TestInventoryAltered(tests.TestCaseWithTransport):
1662
def test_inventory_altered_unchanged(self):
1663
tree = self.make_branch_and_tree('tree')
1664
self.build_tree(['tree/foo'])
1665
tree.add('foo', 'foo-id')
1666
with TransformPreview(tree) as tt:
1667
self.assertEqual([], tt._inventory_altered())
1669
def test_inventory_altered_changed_parent_id(self):
1670
tree = self.make_branch_and_tree('tree')
1671
self.build_tree(['tree/foo'])
1672
tree.add('foo', 'foo-id')
1673
with TransformPreview(tree) as tt:
1674
tt.unversion_file(tt.root)
1675
tt.version_file('new-id', tt.root)
1676
foo_trans_id = tt.trans_id_tree_file_id('foo-id')
1677
foo_tuple = ('foo', foo_trans_id)
1678
root_tuple = ('', tt.root)
1679
self.assertEqual([root_tuple, foo_tuple], tt._inventory_altered())
1681
def test_inventory_altered_noop_changed_parent_id(self):
1682
tree = self.make_branch_and_tree('tree')
1683
self.build_tree(['tree/foo'])
1684
tree.add('foo', 'foo-id')
1685
with TransformPreview(tree) as tt:
1686
tt.unversion_file(tt.root)
1687
tt.version_file(tree.get_root_id(), tt.root)
1688
foo_trans_id = tt.trans_id_tree_file_id('foo-id')
1689
self.assertEqual([], tt._inventory_altered())
1602
1692
class TestTransformMerge(TestCaseInTempDir):
1604
1694
def test_text_merge(self):
2320
2411
self.assertEqual('tree', revision.properties['branch-nick'])
2323
class TestBackupName(tests.TestCase):
2325
def test_deprecations(self):
2326
class MockTransform(object):
2328
def has_named_child(self, by_parent, parent_id, name):
2329
return name in by_parent.get(parent_id, [])
2331
class MockEntry(object):
2334
object.__init__(self)
2337
tt = MockTransform()
2338
name1 = self.applyDeprecated(
2339
symbol_versioning.deprecated_in((2, 3, 0)),
2340
transform.get_backup_name, MockEntry(), {'a':[]}, 'a', tt)
2341
self.assertEqual('name.~1~', name1)
2342
name2 = self.applyDeprecated(
2343
symbol_versioning.deprecated_in((2, 3, 0)),
2344
transform._get_backup_name, 'name', {'a':['name.~1~']}, 'a', tt)
2345
self.assertEqual('name.~2~', name2)
2348
2414
class TestFileMover(tests.TestCaseWithTransport):
2350
2416
def test_file_mover(self):
2464
2530
self.assertPathExists('a/b')
2533
class TestFinalizeRobustness(tests.TestCaseWithTransport):
2534
"""Ensure treetransform creation errors can be safely cleaned up after"""
2536
def _override_globals_in_method(self, instance, method_name, globals):
2537
"""Replace method on instance with one with updated globals"""
2539
func = getattr(instance, method_name).im_func
2540
new_globals = dict(func.func_globals)
2541
new_globals.update(globals)
2542
new_func = types.FunctionType(func.func_code, new_globals,
2543
func.func_name, func.func_defaults)
2544
setattr(instance, method_name,
2545
types.MethodType(new_func, instance, instance.__class__))
2546
self.addCleanup(delattr, instance, method_name)
2549
def _fake_open_raises_before(name, mode):
2550
"""Like open() but raises before doing anything"""
2554
def _fake_open_raises_after(name, mode):
2555
"""Like open() but raises after creating file without returning"""
2556
open(name, mode).close()
2559
def create_transform_and_root_trans_id(self):
2560
"""Setup a transform creating a file in limbo"""
2561
tree = self.make_branch_and_tree('.')
2562
tt = TreeTransform(tree)
2563
return tt, tt.create_path("a", tt.root)
2565
def create_transform_and_subdir_trans_id(self):
2566
"""Setup a transform creating a directory containing a file in limbo"""
2567
tree = self.make_branch_and_tree('.')
2568
tt = TreeTransform(tree)
2569
d_trans_id = tt.create_path("d", tt.root)
2570
tt.create_directory(d_trans_id)
2571
f_trans_id = tt.create_path("a", d_trans_id)
2572
tt.adjust_path("a", d_trans_id, f_trans_id)
2573
return tt, f_trans_id
2575
def test_root_create_file_open_raises_before_creation(self):
2576
tt, trans_id = self.create_transform_and_root_trans_id()
2577
self._override_globals_in_method(tt, "create_file",
2578
{"open": self._fake_open_raises_before})
2579
self.assertRaises(RuntimeError, tt.create_file, ["contents"], trans_id)
2580
path = tt._limbo_name(trans_id)
2581
self.assertPathDoesNotExist(path)
2583
self.assertPathDoesNotExist(tt._limbodir)
2585
def test_root_create_file_open_raises_after_creation(self):
2586
tt, trans_id = self.create_transform_and_root_trans_id()
2587
self._override_globals_in_method(tt, "create_file",
2588
{"open": self._fake_open_raises_after})
2589
self.assertRaises(RuntimeError, tt.create_file, ["contents"], trans_id)
2590
path = tt._limbo_name(trans_id)
2591
self.assertPathExists(path)
2593
self.assertPathDoesNotExist(path)
2594
self.assertPathDoesNotExist(tt._limbodir)
2596
def test_subdir_create_file_open_raises_before_creation(self):
2597
tt, trans_id = self.create_transform_and_subdir_trans_id()
2598
self._override_globals_in_method(tt, "create_file",
2599
{"open": self._fake_open_raises_before})
2600
self.assertRaises(RuntimeError, tt.create_file, ["contents"], trans_id)
2601
path = tt._limbo_name(trans_id)
2602
self.assertPathDoesNotExist(path)
2604
self.assertPathDoesNotExist(tt._limbodir)
2606
def test_subdir_create_file_open_raises_after_creation(self):
2607
tt, trans_id = self.create_transform_and_subdir_trans_id()
2608
self._override_globals_in_method(tt, "create_file",
2609
{"open": self._fake_open_raises_after})
2610
self.assertRaises(RuntimeError, tt.create_file, ["contents"], trans_id)
2611
path = tt._limbo_name(trans_id)
2612
self.assertPathExists(path)
2614
self.assertPathDoesNotExist(path)
2615
self.assertPathDoesNotExist(tt._limbodir)
2617
def test_rename_in_limbo_rename_raises_after_rename(self):
2618
tt, trans_id = self.create_transform_and_root_trans_id()
2619
parent1 = tt.new_directory('parent1', tt.root)
2620
child1 = tt.new_file('child1', parent1, 'contents')
2621
parent2 = tt.new_directory('parent2', tt.root)
2623
class FakeOSModule(object):
2624
def rename(self, old, new):
2627
self._override_globals_in_method(tt, "_rename_in_limbo",
2628
{"os": FakeOSModule()})
2630
RuntimeError, tt.adjust_path, "child1", parent2, child1)
2631
path = osutils.pathjoin(tt._limbo_name(parent2), "child1")
2632
self.assertPathExists(path)
2634
self.assertPathDoesNotExist(path)
2635
self.assertPathDoesNotExist(tt._limbodir)
2637
def test_rename_in_limbo_rename_raises_before_rename(self):
2638
tt, trans_id = self.create_transform_and_root_trans_id()
2639
parent1 = tt.new_directory('parent1', tt.root)
2640
child1 = tt.new_file('child1', parent1, 'contents')
2641
parent2 = tt.new_directory('parent2', tt.root)
2643
class FakeOSModule(object):
2644
def rename(self, old, new):
2646
self._override_globals_in_method(tt, "_rename_in_limbo",
2647
{"os": FakeOSModule()})
2649
RuntimeError, tt.adjust_path, "child1", parent2, child1)
2650
path = osutils.pathjoin(tt._limbo_name(parent1), "child1")
2651
self.assertPathExists(path)
2653
self.assertPathDoesNotExist(path)
2654
self.assertPathDoesNotExist(tt._limbodir)
2467
2657
class TestTransformMissingParent(tests.TestCaseWithTransport):
2469
2659
def make_tt_with_versioned_dir(self):
3068
3270
merger.merge_type = Merge3Merger
3069
3271
merger.do_merge()
3273
def test_has_filename(self):
3274
wt = self.make_branch_and_tree('tree')
3275
self.build_tree(['tree/unmodified', 'tree/removed', 'tree/modified'])
3276
tt = TransformPreview(wt)
3277
removed_id = tt.trans_id_tree_path('removed')
3278
tt.delete_contents(removed_id)
3279
tt.new_file('new', tt.root, 'contents')
3280
modified_id = tt.trans_id_tree_path('modified')
3281
tt.delete_contents(modified_id)
3282
tt.create_file('modified-contents', modified_id)
3283
self.addCleanup(tt.finalize)
3284
tree = tt.get_preview_tree()
3285
self.assertTrue(tree.has_filename('unmodified'))
3286
self.assertFalse(tree.has_filename('not-present'))
3287
self.assertFalse(tree.has_filename('removed'))
3288
self.assertTrue(tree.has_filename('new'))
3289
self.assertTrue(tree.has_filename('modified'))
3071
3291
def test_is_executable(self):
3072
3292
tree = self.make_branch_and_tree('tree')
3073
3293
preview = TransformPreview(tree)
3469
3689
self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
3470
3690
remaining_conflicts.pop())
3471
3691
self.assertLength(1, warnings)
3472
self.assertStartsWith(warnings[0], 'donttouchmypreciouuus')
3692
self.assertStartsWith(warnings[0], 'Value "donttouchmypreciouuus" ')
3695
class TestTransformHooks(tests.TestCaseWithTransport):
3698
super(TestTransformHooks, self).setUp()
3699
self.wt = self.make_branch_and_tree('.')
3702
def get_transform(self):
3703
transform = TreeTransform(self.wt)
3704
self.addCleanup(transform.finalize)
3705
return transform, transform.root
3707
def test_pre_commit_hooks(self):
3709
def record_pre_transform(tree, tt):
3710
calls.append((tree, tt))
3711
MutableTree.hooks.install_named_hook('pre_transform',
3712
record_pre_transform, "Pre transform")
3713
transform, root = self.get_transform()
3714
old_root_id = transform.tree_file_id(root)
3716
self.assertEqual(old_root_id, self.wt.get_root_id())
3717
self.assertEquals([(self.wt, transform)], calls)
3719
def test_post_commit_hooks(self):
3721
def record_post_transform(tree, tt):
3722
calls.append((tree, tt))
3723
MutableTree.hooks.install_named_hook('post_transform',
3724
record_post_transform, "Post transform")
3725
transform, root = self.get_transform()
3726
old_root_id = transform.tree_file_id(root)
3728
self.assertEqual(old_root_id, self.wt.get_root_id())
3729
self.assertEquals([(self.wt, transform)], calls)