~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_transform.py

  • Committer: Martin
  • Date: 2011-05-21 16:29:38 UTC
  • mto: This revision was merged to the branch mainline in revision 5907.
  • Revision ID: gzlist@googlemail.com-20110521162938-1vrw3hp0197l3vrl
Add tests for non-ascii conflict serialisation

Show diffs side-by-side

added added

removed removed

Lines of Context:
62
62
from bzrlib.merge import Merge3Merger, Merger
63
63
from bzrlib.tests import (
64
64
    features,
 
65
    HardlinkFeature,
 
66
    SymlinkFeature,
65
67
    TestCaseInTempDir,
66
68
    TestSkipped,
67
 
    )
68
 
from bzrlib.tests.features import (
69
 
    HardlinkFeature,
70
 
    SymlinkFeature,
71
 
    )
 
69
)
72
70
from bzrlib.transform import (
73
71
    build_tree,
74
72
    create_from_tree,
287
285
        new_trans_id = transform.new_directory('', ROOT_PARENT, 'alt-root-id')
288
286
        self.assertRaises(ValueError, transform.fixup_new_roots)
289
287
 
290
 
    def test_retain_existing_root(self):
291
 
        tt, root = self.get_transform()
292
 
        with tt:
293
 
            tt.new_directory('', ROOT_PARENT, 'new-root-id')
294
 
            tt.fixup_new_roots()
295
 
            self.assertNotEqual('new-root-id', tt.final_file_id(tt.root))
296
 
 
297
 
    def test_retain_existing_root_added_file(self):
298
 
        tt, root = self.get_transform()
299
 
        new_trans_id = tt.new_directory('', ROOT_PARENT, 'new-root-id')
300
 
        child = tt.new_directory('child', new_trans_id, 'child-id')
301
 
        tt.fixup_new_roots()
302
 
        self.assertEqual(tt.root, tt.final_parent(child))
303
 
 
304
 
    def test_add_unversioned_root(self):
305
 
        transform, root = self.get_transform()
306
 
        new_trans_id = transform.new_directory('', ROOT_PARENT, None)
307
 
        transform.delete_contents(transform.root)
308
 
        transform.fixup_new_roots()
309
 
        self.assertNotIn(transform.root, transform._new_id)
310
 
 
311
 
    def test_remove_root_fixup(self):
312
 
        transform, root = self.get_transform()
313
 
        old_root_id = self.wt.get_root_id()
314
 
        self.assertNotEqual('new-root-id', old_root_id)
315
 
        transform.delete_contents(root)
316
 
        transform.unversion_file(root)
317
 
        transform.fixup_new_roots()
318
 
        transform.apply()
319
 
        self.assertEqual(old_root_id, self.wt.get_root_id())
320
 
 
321
 
        transform, root = self.get_transform()
322
 
        new_trans_id = transform.new_directory('', ROOT_PARENT, 'new-root-id')
323
 
        new_trans_id = transform.new_directory('', ROOT_PARENT, 'alt-root-id')
324
 
        self.assertRaises(ValueError, transform.fixup_new_roots)
325
 
 
326
 
    def test_fixup_new_roots_permits_empty_tree(self):
327
 
        transform, root = self.get_transform()
328
 
        transform.delete_contents(root)
329
 
        transform.unversion_file(root)
330
 
        transform.fixup_new_roots()
331
 
        self.assertIs(None, transform.final_kind(root))
332
 
        self.assertIs(None, transform.final_file_id(root))
333
 
 
334
 
    def test_apply_retains_root_directory(self):
335
 
        # Do not attempt to delete the physical root directory, because that
336
 
        # is impossible.
337
 
        transform, root = self.get_transform()
338
 
        with transform:
339
 
            transform.delete_contents(root)
340
 
            e = self.assertRaises(AssertionError, self.assertRaises,
341
 
                                  errors.TransformRenameFailed,
342
 
                                  transform.apply)
343
 
        self.assertContainsRe('TransformRenameFailed not raised', str(e))
344
 
 
345
 
    def test_apply_retains_file_id(self):
346
 
        transform, root = self.get_transform()
347
 
        old_root_id = transform.tree_file_id(root)
348
 
        transform.unversion_file(root)
349
 
        transform.apply()
350
 
        self.assertEqual(old_root_id, self.wt.get_root_id())
351
 
 
352
288
    def test_hardlink(self):
353
289
        self.requireFeature(HardlinkFeature)
354
290
        transform, root = self.get_transform()
781
717
                            'wizard2', 'behind_curtain')
782
718
 
783
719
    def test_symlinks_unicode(self):
784
 
        self.requireFeature(features.UnicodeFilenameFeature)
 
720
        self.requireFeature(tests.UnicodeFilenameFeature)
785
721
        self._test_symlinks(u'\N{Euro Sign}wizard',
786
722
                            u'wizard-targ\N{Euro Sign}t',
787
723
                            u'\N{Euro Sign}wizard2',
875
811
        raw_conflicts = resolve_conflicts(tt)
876
812
        cooked_conflicts = cook_conflicts(raw_conflicts, tt)
877
813
        tt.finalize()
878
 
        conflicts_s = [unicode(c) for c in cooked_conflicts]
 
814
        conflicts_s = [str(c) for c in cooked_conflicts]
879
815
        self.assertEqual(len(cooked_conflicts), len(conflicts_s))
880
816
        self.assertEqual(conflicts_s[0], 'Conflict adding file dorothy.  '
881
817
                                         'Moved existing file to '
1663
1599
    return template % ('<' * 7, tree, '=' * 7, merge, '>' * 7)
1664
1600
 
1665
1601
 
1666
 
class TestInventoryAltered(tests.TestCaseWithTransport):
1667
 
 
1668
 
    def test_inventory_altered_unchanged(self):
1669
 
        tree = self.make_branch_and_tree('tree')
1670
 
        self.build_tree(['tree/foo'])
1671
 
        tree.add('foo', 'foo-id')
1672
 
        with TransformPreview(tree) as tt:
1673
 
            self.assertEqual([], tt._inventory_altered())
1674
 
 
1675
 
    def test_inventory_altered_changed_parent_id(self):
1676
 
        tree = self.make_branch_and_tree('tree')
1677
 
        self.build_tree(['tree/foo'])
1678
 
        tree.add('foo', 'foo-id')
1679
 
        with TransformPreview(tree) as tt:
1680
 
            tt.unversion_file(tt.root)
1681
 
            tt.version_file('new-id', tt.root)
1682
 
            foo_trans_id = tt.trans_id_tree_file_id('foo-id')
1683
 
            foo_tuple = ('foo', foo_trans_id)
1684
 
            root_tuple = ('', tt.root)
1685
 
            self.assertEqual([root_tuple, foo_tuple], tt._inventory_altered())
1686
 
 
1687
 
    def test_inventory_altered_noop_changed_parent_id(self):
1688
 
        tree = self.make_branch_and_tree('tree')
1689
 
        self.build_tree(['tree/foo'])
1690
 
        tree.add('foo', 'foo-id')
1691
 
        with TransformPreview(tree) as tt:
1692
 
            tt.unversion_file(tt.root)
1693
 
            tt.version_file(tree.get_root_id(), tt.root)
1694
 
            foo_trans_id = tt.trans_id_tree_file_id('foo-id')
1695
 
            self.assertEqual([], tt._inventory_altered())
1696
 
 
1697
 
 
1698
1602
class TestTransformMerge(TestCaseInTempDir):
1699
1603
 
1700
1604
    def test_text_merge(self):
2226
2130
        self.assertEqualStat(source_stat, target_stat)
2227
2131
 
2228
2132
    def test_case_insensitive_build_tree_inventory(self):
2229
 
        if (features.CaseInsensitiveFilesystemFeature.available()
2230
 
            or features.CaseInsCasePresFilenameFeature.available()):
 
2133
        if (tests.CaseInsensitiveFilesystemFeature.available()
 
2134
            or tests.CaseInsCasePresFilenameFeature.available()):
2231
2135
            raise tests.UnavailableFeature('Fully case sensitive filesystem')
2232
2136
        source = self.make_branch_and_tree('source')
2233
2137
        self.build_tree(['source/file', 'source/FILE'])
2560
2464
        self.assertPathExists('a/b')
2561
2465
 
2562
2466
 
2563
 
class TestFinalizeRobustness(tests.TestCaseWithTransport):
2564
 
    """Ensure treetransform creation errors can be safely cleaned up after"""
2565
 
 
2566
 
    def _override_globals_in_method(self, instance, method_name, globals):
2567
 
        """Replace method on instance with one with updated globals"""
2568
 
        import types
2569
 
        func = getattr(instance, method_name).im_func
2570
 
        new_globals = dict(func.func_globals)
2571
 
        new_globals.update(globals)
2572
 
        new_func = types.FunctionType(func.func_code, new_globals,
2573
 
            func.func_name, func.func_defaults)
2574
 
        setattr(instance, method_name,
2575
 
            types.MethodType(new_func, instance, instance.__class__))
2576
 
        self.addCleanup(delattr, instance, method_name)
2577
 
 
2578
 
    @staticmethod
2579
 
    def _fake_open_raises_before(name, mode):
2580
 
        """Like open() but raises before doing anything"""
2581
 
        raise RuntimeError
2582
 
 
2583
 
    @staticmethod
2584
 
    def _fake_open_raises_after(name, mode):
2585
 
        """Like open() but raises after creating file without returning"""
2586
 
        open(name, mode).close()
2587
 
        raise RuntimeError
2588
 
 
2589
 
    def create_transform_and_root_trans_id(self):
2590
 
        """Setup a transform creating a file in limbo"""
2591
 
        tree = self.make_branch_and_tree('.')
2592
 
        tt = TreeTransform(tree)
2593
 
        return tt, tt.create_path("a", tt.root)
2594
 
 
2595
 
    def create_transform_and_subdir_trans_id(self):
2596
 
        """Setup a transform creating a directory containing a file in limbo"""
2597
 
        tree = self.make_branch_and_tree('.')
2598
 
        tt = TreeTransform(tree)
2599
 
        d_trans_id = tt.create_path("d", tt.root)
2600
 
        tt.create_directory(d_trans_id)
2601
 
        f_trans_id = tt.create_path("a", d_trans_id)
2602
 
        tt.adjust_path("a", d_trans_id, f_trans_id)
2603
 
        return tt, f_trans_id
2604
 
 
2605
 
    def test_root_create_file_open_raises_before_creation(self):
2606
 
        tt, trans_id = self.create_transform_and_root_trans_id()
2607
 
        self._override_globals_in_method(tt, "create_file",
2608
 
            {"open": self._fake_open_raises_before})
2609
 
        self.assertRaises(RuntimeError, tt.create_file, ["contents"], trans_id)
2610
 
        path = tt._limbo_name(trans_id)
2611
 
        self.assertPathDoesNotExist(path)
2612
 
        tt.finalize()
2613
 
        self.assertPathDoesNotExist(tt._limbodir)
2614
 
 
2615
 
    def test_root_create_file_open_raises_after_creation(self):
2616
 
        tt, trans_id = self.create_transform_and_root_trans_id()
2617
 
        self._override_globals_in_method(tt, "create_file",
2618
 
            {"open": self._fake_open_raises_after})
2619
 
        self.assertRaises(RuntimeError, tt.create_file, ["contents"], trans_id)
2620
 
        path = tt._limbo_name(trans_id)
2621
 
        self.assertPathExists(path)
2622
 
        tt.finalize()
2623
 
        self.assertPathDoesNotExist(path)
2624
 
        self.assertPathDoesNotExist(tt._limbodir)
2625
 
 
2626
 
    def test_subdir_create_file_open_raises_before_creation(self):
2627
 
        tt, trans_id = self.create_transform_and_subdir_trans_id()
2628
 
        self._override_globals_in_method(tt, "create_file",
2629
 
            {"open": self._fake_open_raises_before})
2630
 
        self.assertRaises(RuntimeError, tt.create_file, ["contents"], trans_id)
2631
 
        path = tt._limbo_name(trans_id)
2632
 
        self.assertPathDoesNotExist(path)
2633
 
        tt.finalize()
2634
 
        self.assertPathDoesNotExist(tt._limbodir)
2635
 
 
2636
 
    def test_subdir_create_file_open_raises_after_creation(self):
2637
 
        tt, trans_id = self.create_transform_and_subdir_trans_id()
2638
 
        self._override_globals_in_method(tt, "create_file",
2639
 
            {"open": self._fake_open_raises_after})
2640
 
        self.assertRaises(RuntimeError, tt.create_file, ["contents"], trans_id)
2641
 
        path = tt._limbo_name(trans_id)
2642
 
        self.assertPathExists(path)
2643
 
        tt.finalize()
2644
 
        self.assertPathDoesNotExist(path)
2645
 
        self.assertPathDoesNotExist(tt._limbodir)
2646
 
 
2647
 
    def test_rename_in_limbo_rename_raises_after_rename(self):
2648
 
        tt, trans_id = self.create_transform_and_root_trans_id()
2649
 
        parent1 = tt.new_directory('parent1', tt.root)
2650
 
        child1 = tt.new_file('child1', parent1, 'contents')
2651
 
        parent2 = tt.new_directory('parent2', tt.root)
2652
 
 
2653
 
        class FakeOSModule(object):
2654
 
            def rename(self, old, new):
2655
 
                os.rename(old, new)
2656
 
                raise RuntimeError
2657
 
        self._override_globals_in_method(tt, "_rename_in_limbo",
2658
 
            {"os": FakeOSModule()})
2659
 
        self.assertRaises(
2660
 
            RuntimeError, tt.adjust_path, "child1", parent2, child1)
2661
 
        path = osutils.pathjoin(tt._limbo_name(parent2), "child1")
2662
 
        self.assertPathExists(path)
2663
 
        tt.finalize()
2664
 
        self.assertPathDoesNotExist(path)
2665
 
        self.assertPathDoesNotExist(tt._limbodir)
2666
 
 
2667
 
    def test_rename_in_limbo_rename_raises_before_rename(self):
2668
 
        tt, trans_id = self.create_transform_and_root_trans_id()
2669
 
        parent1 = tt.new_directory('parent1', tt.root)
2670
 
        child1 = tt.new_file('child1', parent1, 'contents')
2671
 
        parent2 = tt.new_directory('parent2', tt.root)
2672
 
 
2673
 
        class FakeOSModule(object):
2674
 
            def rename(self, old, new):
2675
 
                raise RuntimeError
2676
 
        self._override_globals_in_method(tt, "_rename_in_limbo",
2677
 
            {"os": FakeOSModule()})
2678
 
        self.assertRaises(
2679
 
            RuntimeError, tt.adjust_path, "child1", parent2, child1)
2680
 
        path = osutils.pathjoin(tt._limbo_name(parent1), "child1")
2681
 
        self.assertPathExists(path)
2682
 
        tt.finalize()
2683
 
        self.assertPathDoesNotExist(path)
2684
 
        self.assertPathDoesNotExist(tt._limbodir)
2685
 
 
2686
 
 
2687
2467
class TestTransformMissingParent(tests.TestCaseWithTransport):
2688
2468
 
2689
2469
    def make_tt_with_versioned_dir(self):
3346
3126
        self.assertEqual('contents', rev2_tree.get_file_text('file_id'))
3347
3127
 
3348
3128
    def test_ascii_limbo_paths(self):
3349
 
        self.requireFeature(features.UnicodeFilenameFeature)
 
3129
        self.requireFeature(tests.UnicodeFilenameFeature)
3350
3130
        branch = self.make_branch('any')
3351
3131
        tree = branch.repository.revision_tree(_mod_revision.NULL_REVISION)
3352
3132
        tt = TransformPreview(tree)
3369
3149
 
3370
3150
class TestSerializeTransform(tests.TestCaseWithTransport):
3371
3151
 
3372
 
    _test_needs_features = [features.UnicodeFilenameFeature]
 
3152
    _test_needs_features = [tests.UnicodeFilenameFeature]
3373
3153
 
3374
3154
    def get_preview(self, tree=None):
3375
3155
        if tree is None:
3450
3230
        return self.make_records(attribs, contents)
3451
3231
 
3452
3232
    def test_serialize_symlink_creation(self):
3453
 
        self.requireFeature(features.SymlinkFeature)
 
3233
        self.requireFeature(tests.SymlinkFeature)
3454
3234
        tt = self.get_preview()
3455
3235
        tt.new_symlink(u'foo\u1234', tt.root, u'bar\u1234')
3456
3236
        self.assertSerializesTo(self.symlink_creation_records(), tt)
3457
3237
 
3458
3238
    def test_deserialize_symlink_creation(self):
3459
 
        self.requireFeature(features.SymlinkFeature)
 
3239
        self.requireFeature(tests.SymlinkFeature)
3460
3240
        tt = self.get_preview()
3461
3241
        tt.deserialize(iter(self.symlink_creation_records()))
3462
3242
        abspath = tt._limbo_name('new-1')