285
285
new_trans_id = transform.new_directory('', ROOT_PARENT, 'alt-root-id')
286
286
self.assertRaises(ValueError, transform.fixup_new_roots)
288
def test_retain_existing_root(self):
289
tt, root = self.get_transform()
291
tt.new_directory('', ROOT_PARENT, 'new-root-id')
293
self.assertNotEqual('new-root-id', tt.final_file_id(tt.root))
295
def test_retain_existing_root_added_file(self):
296
tt, root = self.get_transform()
297
new_trans_id = tt.new_directory('', ROOT_PARENT, 'new-root-id')
298
child = tt.new_directory('child', new_trans_id, 'child-id')
300
self.assertEqual(tt.root, tt.final_parent(child))
302
def test_add_unversioned_root(self):
303
transform, root = self.get_transform()
304
new_trans_id = transform.new_directory('', ROOT_PARENT, None)
305
transform.delete_contents(transform.root)
306
transform.fixup_new_roots()
307
self.assertNotIn(transform.root, transform._new_id)
309
def test_remove_root_fixup(self):
310
transform, root = self.get_transform()
311
old_root_id = self.wt.get_root_id()
312
self.assertNotEqual('new-root-id', old_root_id)
313
transform.delete_contents(root)
314
transform.unversion_file(root)
315
transform.fixup_new_roots()
317
self.assertEqual(old_root_id, self.wt.get_root_id())
319
transform, root = self.get_transform()
320
new_trans_id = transform.new_directory('', ROOT_PARENT, 'new-root-id')
321
new_trans_id = transform.new_directory('', ROOT_PARENT, 'alt-root-id')
322
self.assertRaises(ValueError, transform.fixup_new_roots)
324
def test_apply_retains_root_directory(self):
325
# Do not attempt to delete the physical root directory, because that
327
transform, root = self.get_transform()
329
transform.delete_contents(root)
330
e = self.assertRaises(AssertionError, self.assertRaises,
331
errors.TransformRenameFailed,
333
self.assertContainsRe('TransformRenameFailed not raised', str(e))
335
288
def test_hardlink(self):
336
289
self.requireFeature(HardlinkFeature)
337
290
transform, root = self.get_transform()
1646
1599
return template % ('<' * 7, tree, '=' * 7, merge, '>' * 7)
1649
class TestInventoryAltered(tests.TestCaseWithTransport):
1651
def test_inventory_altered_unchanged(self):
1652
tree = self.make_branch_and_tree('tree')
1653
self.build_tree(['tree/foo'])
1654
tree.add('foo', 'foo-id')
1655
with TransformPreview(tree) as tt:
1656
self.assertEqual([], tt._inventory_altered())
1658
def test_inventory_altered_changed_parent_id(self):
1659
tree = self.make_branch_and_tree('tree')
1660
self.build_tree(['tree/foo'])
1661
tree.add('foo', 'foo-id')
1662
with TransformPreview(tree) as tt:
1663
tt.unversion_file(tt.root)
1664
tt.version_file('new-id', tt.root)
1665
foo_trans_id = tt.trans_id_tree_file_id('foo-id')
1666
foo_tuple = ('foo', foo_trans_id)
1667
root_tuple = ('', tt.root)
1668
self.assertEqual([root_tuple, foo_tuple], tt._inventory_altered())
1670
def test_inventory_altered_noop_changed_parent_id(self):
1671
tree = self.make_branch_and_tree('tree')
1672
self.build_tree(['tree/foo'])
1673
tree.add('foo', 'foo-id')
1674
with TransformPreview(tree) as tt:
1675
tt.unversion_file(tt.root)
1676
tt.version_file(tree.get_root_id(), tt.root)
1677
foo_trans_id = tt.trans_id_tree_file_id('foo-id')
1678
self.assertEqual([], tt._inventory_altered())
1681
1602
class TestTransformMerge(TestCaseInTempDir):
1683
1604
def test_text_merge(self):
2543
2464
self.assertPathExists('a/b')
2546
class TestFinalizeRobustness(tests.TestCaseWithTransport):
2547
"""Ensure treetransform creation errors can be safely cleaned up after"""
2549
def _override_globals_in_method(self, instance, method_name, globals):
2550
"""Replace method on instance with one with updated globals"""
2552
func = getattr(instance, method_name).im_func
2553
new_globals = dict(func.func_globals)
2554
new_globals.update(globals)
2555
new_func = types.FunctionType(func.func_code, new_globals,
2556
func.func_name, func.func_defaults)
2557
setattr(instance, method_name,
2558
types.MethodType(new_func, instance, instance.__class__))
2559
self.addCleanup(delattr, instance, method_name)
2562
def _fake_open_raises_before(name, mode):
2563
"""Like open() but raises before doing anything"""
2567
def _fake_open_raises_after(name, mode):
2568
"""Like open() but raises after creating file without returning"""
2569
open(name, mode).close()
2572
def create_transform_and_root_trans_id(self):
2573
"""Setup a transform creating a file in limbo"""
2574
tree = self.make_branch_and_tree('.')
2575
tt = TreeTransform(tree)
2576
return tt, tt.create_path("a", tt.root)
2578
def create_transform_and_subdir_trans_id(self):
2579
"""Setup a transform creating a directory containing a file in limbo"""
2580
tree = self.make_branch_and_tree('.')
2581
tt = TreeTransform(tree)
2582
d_trans_id = tt.create_path("d", tt.root)
2583
tt.create_directory(d_trans_id)
2584
f_trans_id = tt.create_path("a", d_trans_id)
2585
tt.adjust_path("a", d_trans_id, f_trans_id)
2586
return tt, f_trans_id
2588
def test_root_create_file_open_raises_before_creation(self):
2589
tt, trans_id = self.create_transform_and_root_trans_id()
2590
self._override_globals_in_method(tt, "create_file",
2591
{"open": self._fake_open_raises_before})
2592
self.assertRaises(RuntimeError, tt.create_file, ["contents"], trans_id)
2593
path = tt._limbo_name(trans_id)
2594
self.assertPathDoesNotExist(path)
2596
self.assertPathDoesNotExist(tt._limbodir)
2598
def test_root_create_file_open_raises_after_creation(self):
2599
tt, trans_id = self.create_transform_and_root_trans_id()
2600
self._override_globals_in_method(tt, "create_file",
2601
{"open": self._fake_open_raises_after})
2602
self.assertRaises(RuntimeError, tt.create_file, ["contents"], trans_id)
2603
path = tt._limbo_name(trans_id)
2604
self.assertPathExists(path)
2606
self.assertPathDoesNotExist(path)
2607
self.assertPathDoesNotExist(tt._limbodir)
2609
def test_subdir_create_file_open_raises_before_creation(self):
2610
tt, trans_id = self.create_transform_and_subdir_trans_id()
2611
self._override_globals_in_method(tt, "create_file",
2612
{"open": self._fake_open_raises_before})
2613
self.assertRaises(RuntimeError, tt.create_file, ["contents"], trans_id)
2614
path = tt._limbo_name(trans_id)
2615
self.assertPathDoesNotExist(path)
2617
self.assertPathDoesNotExist(tt._limbodir)
2619
def test_subdir_create_file_open_raises_after_creation(self):
2620
tt, trans_id = self.create_transform_and_subdir_trans_id()
2621
self._override_globals_in_method(tt, "create_file",
2622
{"open": self._fake_open_raises_after})
2623
self.assertRaises(RuntimeError, tt.create_file, ["contents"], trans_id)
2624
path = tt._limbo_name(trans_id)
2625
self.assertPathExists(path)
2627
self.assertPathDoesNotExist(path)
2628
self.assertPathDoesNotExist(tt._limbodir)
2630
def test_rename_in_limbo_rename_raises_after_rename(self):
2631
tt, trans_id = self.create_transform_and_root_trans_id()
2632
parent1 = tt.new_directory('parent1', tt.root)
2633
child1 = tt.new_file('child1', parent1, 'contents')
2634
parent2 = tt.new_directory('parent2', tt.root)
2636
class FakeOSModule(object):
2637
def rename(self, old, new):
2640
self._override_globals_in_method(tt, "_rename_in_limbo",
2641
{"os": FakeOSModule()})
2643
RuntimeError, tt.adjust_path, "child1", parent2, child1)
2644
path = osutils.pathjoin(tt._limbo_name(parent2), "child1")
2645
self.assertPathExists(path)
2647
self.assertPathDoesNotExist(path)
2648
self.assertPathDoesNotExist(tt._limbodir)
2650
def test_rename_in_limbo_rename_raises_before_rename(self):
2651
tt, trans_id = self.create_transform_and_root_trans_id()
2652
parent1 = tt.new_directory('parent1', tt.root)
2653
child1 = tt.new_file('child1', parent1, 'contents')
2654
parent2 = tt.new_directory('parent2', tt.root)
2656
class FakeOSModule(object):
2657
def rename(self, old, new):
2659
self._override_globals_in_method(tt, "_rename_in_limbo",
2660
{"os": FakeOSModule()})
2662
RuntimeError, tt.adjust_path, "child1", parent2, child1)
2663
path = osutils.pathjoin(tt._limbo_name(parent1), "child1")
2664
self.assertPathExists(path)
2666
self.assertPathDoesNotExist(path)
2667
self.assertPathDoesNotExist(tt._limbodir)
2670
2467
class TestTransformMissingParent(tests.TestCaseWithTransport):
2672
2469
def make_tt_with_versioned_dir(self):