~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_transform.py

  • Committer: Jelmer Vernooij
  • Date: 2011-05-16 13:39:39 UTC
  • mto: (5923.1.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 5925.
  • Revision ID: jelmer@samba.org-20110516133939-8u1pc9utas3uw1lt
Require a unicode prompt to be passed into all methods that prompt.

Show diffs side-by-side

added added

removed removed

Lines of Context:
285
285
        new_trans_id = transform.new_directory('', ROOT_PARENT, 'alt-root-id')
286
286
        self.assertRaises(ValueError, transform.fixup_new_roots)
287
287
 
288
 
    def test_retain_existing_root(self):
289
 
        tt, root = self.get_transform()
290
 
        with tt:
291
 
            tt.new_directory('', ROOT_PARENT, 'new-root-id')
292
 
            tt.fixup_new_roots()
293
 
            self.assertNotEqual('new-root-id', tt.final_file_id(tt.root))
294
 
 
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')
299
 
        tt.fixup_new_roots()
300
 
        self.assertEqual(tt.root, tt.final_parent(child))
301
 
 
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)
308
 
 
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()
316
 
        transform.apply()
317
 
        self.assertEqual(old_root_id, self.wt.get_root_id())
318
 
 
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)
323
 
 
324
 
    def test_apply_retains_root_directory(self):
325
 
        # Do not attempt to delete the physical root directory, because that
326
 
        # is impossible.
327
 
        transform, root = self.get_transform()
328
 
        with transform:
329
 
            transform.delete_contents(root)
330
 
            e = self.assertRaises(AssertionError, self.assertRaises,
331
 
                                  errors.TransformRenameFailed,
332
 
                                  transform.apply)
333
 
        self.assertContainsRe('TransformRenameFailed not raised', str(e))
334
 
 
335
288
    def test_hardlink(self):
336
289
        self.requireFeature(HardlinkFeature)
337
290
        transform, root = self.get_transform()
858
811
        raw_conflicts = resolve_conflicts(tt)
859
812
        cooked_conflicts = cook_conflicts(raw_conflicts, tt)
860
813
        tt.finalize()
861
 
        conflicts_s = [unicode(c) for c in cooked_conflicts]
 
814
        conflicts_s = [str(c) for c in cooked_conflicts]
862
815
        self.assertEqual(len(cooked_conflicts), len(conflicts_s))
863
816
        self.assertEqual(conflicts_s[0], 'Conflict adding file dorothy.  '
864
817
                                         'Moved existing file to '
1646
1599
    return template % ('<' * 7, tree, '=' * 7, merge, '>' * 7)
1647
1600
 
1648
1601
 
1649
 
class TestInventoryAltered(tests.TestCaseWithTransport):
1650
 
 
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())
1657
 
 
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())
1669
 
 
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())
1679
 
 
1680
 
 
1681
1602
class TestTransformMerge(TestCaseInTempDir):
1682
1603
 
1683
1604
    def test_text_merge(self):
2543
2464
        self.assertPathExists('a/b')
2544
2465
 
2545
2466
 
2546
 
class TestFinalizeRobustness(tests.TestCaseWithTransport):
2547
 
    """Ensure treetransform creation errors can be safely cleaned up after"""
2548
 
 
2549
 
    def _override_globals_in_method(self, instance, method_name, globals):
2550
 
        """Replace method on instance with one with updated globals"""
2551
 
        import types
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)
2560
 
 
2561
 
    @staticmethod
2562
 
    def _fake_open_raises_before(name, mode):
2563
 
        """Like open() but raises before doing anything"""
2564
 
        raise RuntimeError
2565
 
 
2566
 
    @staticmethod
2567
 
    def _fake_open_raises_after(name, mode):
2568
 
        """Like open() but raises after creating file without returning"""
2569
 
        open(name, mode).close()
2570
 
        raise RuntimeError
2571
 
 
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)
2577
 
 
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
2587
 
 
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)
2595
 
        tt.finalize()
2596
 
        self.assertPathDoesNotExist(tt._limbodir)
2597
 
 
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)
2605
 
        tt.finalize()
2606
 
        self.assertPathDoesNotExist(path)
2607
 
        self.assertPathDoesNotExist(tt._limbodir)
2608
 
 
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)
2616
 
        tt.finalize()
2617
 
        self.assertPathDoesNotExist(tt._limbodir)
2618
 
 
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)
2626
 
        tt.finalize()
2627
 
        self.assertPathDoesNotExist(path)
2628
 
        self.assertPathDoesNotExist(tt._limbodir)
2629
 
 
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)
2635
 
 
2636
 
        class FakeOSModule(object):
2637
 
            def rename(self, old, new):
2638
 
                os.rename(old, new)
2639
 
                raise RuntimeError
2640
 
        self._override_globals_in_method(tt, "_rename_in_limbo",
2641
 
            {"os": FakeOSModule()})
2642
 
        self.assertRaises(
2643
 
            RuntimeError, tt.adjust_path, "child1", parent2, child1)
2644
 
        path = osutils.pathjoin(tt._limbo_name(parent2), "child1")
2645
 
        self.assertPathExists(path)
2646
 
        tt.finalize()
2647
 
        self.assertPathDoesNotExist(path)
2648
 
        self.assertPathDoesNotExist(tt._limbodir)
2649
 
 
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)
2655
 
 
2656
 
        class FakeOSModule(object):
2657
 
            def rename(self, old, new):
2658
 
                raise RuntimeError
2659
 
        self._override_globals_in_method(tt, "_rename_in_limbo",
2660
 
            {"os": FakeOSModule()})
2661
 
        self.assertRaises(
2662
 
            RuntimeError, tt.adjust_path, "child1", parent2, child1)
2663
 
        path = osutils.pathjoin(tt._limbo_name(parent1), "child1")
2664
 
        self.assertPathExists(path)
2665
 
        tt.finalize()
2666
 
        self.assertPathDoesNotExist(path)
2667
 
        self.assertPathDoesNotExist(tt._limbodir)
2668
 
 
2669
 
 
2670
2467
class TestTransformMissingParent(tests.TestCaseWithTransport):
2671
2468
 
2672
2469
    def make_tt_with_versioned_dir(self):