~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_transform.py

  • Committer: Jelmer Vernooij
  • Date: 2016-04-03 16:32:31 UTC
  • mto: This revision was merged to the branch mainline in revision 6617.
  • Revision ID: jelmer@jelmer.uk-20160403163231-h72bo0uyek2gikw0
Don't put French text in doc/en/user-reference when LANGUAGE=fr_CH.UTF_8.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2011 Canonical Ltd
 
1
# Copyright (C) 2006-2012, 2016 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
34
34
    transform,
35
35
    urlutils,
36
36
    )
37
 
from bzrlib.bzrdir import BzrDir
38
37
from bzrlib.conflicts import (
39
38
    DeletingParent,
40
39
    DuplicateEntry,
44
43
    ParentLoop,
45
44
    UnversionedParent,
46
45
)
 
46
from bzrlib.controldir import ControlDir
47
47
from bzrlib.diff import show_diff_trees
48
48
from bzrlib.errors import (
49
49
    DuplicateKey,
60
60
    pathjoin,
61
61
)
62
62
from bzrlib.merge import Merge3Merger, Merger
 
63
from bzrlib.mutabletree import MutableTree
63
64
from bzrlib.tests import (
64
65
    features,
 
66
    TestCaseInTempDir,
 
67
    TestSkipped,
 
68
    )
 
69
from bzrlib.tests.features import (
65
70
    HardlinkFeature,
66
71
    SymlinkFeature,
67
 
    TestCaseInTempDir,
68
 
    TestSkipped,
69
 
)
 
72
    )
70
73
from bzrlib.transform import (
71
74
    build_tree,
72
75
    create_from_tree,
85
88
 
86
89
    def setUp(self):
87
90
        super(TestTreeTransform, self).setUp()
88
 
        self.wt = self.make_branch_and_tree('.', format='dirstate-with-subtree')
 
91
        self.wt = self.make_branch_and_tree('.', format='development-subtree')
89
92
        os.chdir('..')
90
93
 
91
94
    def get_transform(self):
321
324
        new_trans_id = transform.new_directory('', ROOT_PARENT, 'alt-root-id')
322
325
        self.assertRaises(ValueError, transform.fixup_new_roots)
323
326
 
 
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))
 
334
 
324
335
    def test_apply_retains_root_directory(self):
325
336
        # Do not attempt to delete the physical root directory, because that
326
337
        # is impossible.
332
343
                                  transform.apply)
333
344
        self.assertContainsRe('TransformRenameFailed not raised', str(e))
334
345
 
 
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)
 
350
        transform.apply()
 
351
        self.assertEqual(old_root_id, self.wt.get_root_id())
 
352
 
335
353
    def test_hardlink(self):
336
354
        self.requireFeature(HardlinkFeature)
337
355
        transform, root = self.get_transform()
381
399
        tree.lock_read()
382
400
        self.addCleanup(tree.unlock)
383
401
        self.assertEqual('subtree-revision',
384
 
                         tree.inventory['subtree-id'].reference_revision)
 
402
                         tree.root_inventory['subtree-id'].reference_revision)
385
403
 
386
404
    def test_conflicts(self):
387
405
        transform, root = self.get_transform()
438
456
        newtip = transform2.new_file('tip', oz_id, 'other', 'tip-id')
439
457
        result = transform2.find_conflicts()
440
458
        fp = FinalPaths(transform2)
441
 
        self.assert_('oz/tip' in transform2._tree_path_ids)
 
459
        self.assertTrue('oz/tip' in transform2._tree_path_ids)
442
460
        self.assertEqual(fp.get_path(newtip), pathjoin('oz', 'tip'))
443
461
        self.assertEqual(len(result), 2)
444
462
        self.assertEqual((result[0][0], result[0][1]),
764
782
                            'wizard2', 'behind_curtain')
765
783
 
766
784
    def test_symlinks_unicode(self):
767
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
785
        self.requireFeature(features.UnicodeFilenameFeature)
768
786
        self._test_symlinks(u'\N{Euro Sign}wizard',
769
787
                            u'wizard-targ\N{Euro Sign}t',
770
788
                            u'\N{Euro Sign}wizard2',
783
801
        os.symlink = None
784
802
        try:
785
803
            err = self.assertRaises(errors.UnableCreateSymlink, tt_helper)
786
 
            self.assertEquals(
 
804
            self.assertEqual(
787
805
                "Unable to create symlink 'foo' on this platform",
788
806
                str(err))
789
807
        finally:
1459
1477
        # The rename will fail because the target directory is not empty (but
1460
1478
        # raises FileExists anyway).
1461
1479
        err = self.assertRaises(errors.FileExists, tt_helper)
1462
 
        self.assertContainsRe(str(err),
1463
 
            "^File exists: .+/baz")
 
1480
        self.assertEndsWith(err.path, "/baz")
1464
1481
 
1465
1482
    def test_two_directories_clash(self):
1466
1483
        def tt_helper():
1478
1495
                wt.unlock()
1479
1496
                raise
1480
1497
        err = self.assertRaises(errors.FileExists, tt_helper)
1481
 
        self.assertContainsRe(str(err),
1482
 
            "^File exists: .+/foo")
 
1498
        self.assertEndsWith(err.path, "/foo")
1483
1499
 
1484
1500
    def test_two_directories_clash_finalize(self):
1485
1501
        def tt_helper():
1497
1513
                tt.finalize()
1498
1514
                raise
1499
1515
        err = self.assertRaises(errors.FileExists, tt_helper)
1500
 
        self.assertContainsRe(str(err),
1501
 
            "^File exists: .+/foo")
 
1516
        self.assertEndsWith(err.path, "/foo")
1502
1517
 
1503
1518
    def test_file_to_directory(self):
1504
1519
        wt = self.make_branch_and_tree('.')
1517
1532
        self.assertPathExists("foo/bar")
1518
1533
        wt.lock_read()
1519
1534
        try:
1520
 
            self.assertEqual(wt.inventory.get_file_kind(wt.path2id("foo")),
1521
 
                    "directory")
 
1535
            self.assertEqual(wt.kind(wt.path2id("foo")), "directory")
1522
1536
        finally:
1523
1537
            wt.unlock()
1524
1538
        wt.commit("two")
1540
1554
        self.assertPathExists("foo")
1541
1555
        wt.lock_read()
1542
1556
        self.addCleanup(wt.unlock)
1543
 
        self.assertEqual(wt.inventory.get_file_kind(wt.path2id("foo")),
1544
 
                "symlink")
 
1557
        self.assertEqual(wt.kind(wt.path2id("foo")), "symlink")
1545
1558
 
1546
1559
    def test_dir_to_file(self):
1547
1560
        wt = self.make_branch_and_tree('.')
1559
1572
        self.assertPathExists("foo")
1560
1573
        wt.lock_read()
1561
1574
        self.addCleanup(wt.unlock)
1562
 
        self.assertEqual(wt.inventory.get_file_kind(wt.path2id("foo")),
1563
 
                "file")
 
1575
        self.assertEqual(wt.kind(wt.path2id("foo")), "file")
1564
1576
 
1565
1577
    def test_dir_to_hardlink(self):
1566
1578
        self.requireFeature(HardlinkFeature)
1581
1593
        self.assertPathExists("baz")
1582
1594
        wt.lock_read()
1583
1595
        self.addCleanup(wt.unlock)
1584
 
        self.assertEqual(wt.inventory.get_file_kind(wt.path2id("foo")),
1585
 
                "file")
 
1596
        self.assertEqual(wt.kind(wt.path2id("foo")), "file")
1586
1597
 
1587
1598
    def test_no_final_path(self):
1588
1599
        transform, root = self.get_transform()
1634
1645
    def __init__(self, dirname, root_id):
1635
1646
        self.name = dirname
1636
1647
        os.mkdir(dirname)
1637
 
        self.wt = BzrDir.create_standalone_workingtree(dirname)
 
1648
        self.wt = ControlDir.create_standalone_workingtree(dirname)
1638
1649
        self.wt.set_root_id(root_id)
1639
1650
        self.b = self.wt.branch
1640
1651
        self.tt = TreeTransform(self.wt)
1750
1761
        merge_modified = this.wt.merge_modified()
1751
1762
        self.assertSubset(merge_modified, modified)
1752
1763
        self.assertEqual(len(merge_modified), len(modified))
1753
 
        file(this.wt.id2abspath('a'), 'wb').write('booga')
 
1764
        with file(this.wt.id2abspath('a'), 'wb') as f: f.write('booga')
1754
1765
        modified.pop(0)
1755
1766
        merge_modified = this.wt.merge_modified()
1756
1767
        self.assertSubset(merge_modified, modified)
1870
1881
    def test_build_tree_with_symlinks(self):
1871
1882
        self.requireFeature(SymlinkFeature)
1872
1883
        os.mkdir('a')
1873
 
        a = BzrDir.create_standalone_workingtree('a')
 
1884
        a = ControlDir.create_standalone_workingtree('a')
1874
1885
        os.mkdir('a/foo')
1875
 
        file('a/foo/bar', 'wb').write('contents')
 
1886
        with file('a/foo/bar', 'wb') as f: f.write('contents')
1876
1887
        os.symlink('a/foo/bar', 'a/foo/baz')
1877
1888
        a.add(['foo', 'foo/bar', 'foo/baz'])
1878
1889
        a.commit('initial commit')
1879
 
        b = BzrDir.create_standalone_workingtree('b')
 
1890
        b = ControlDir.create_standalone_workingtree('b')
1880
1891
        basis = a.basis_tree()
1881
1892
        basis.lock_read()
1882
1893
        self.addCleanup(basis.unlock)
1887
1898
 
1888
1899
    def test_build_with_references(self):
1889
1900
        tree = self.make_branch_and_tree('source',
1890
 
            format='dirstate-with-subtree')
 
1901
            format='development-subtree')
1891
1902
        subtree = self.make_branch_and_tree('source/subtree',
1892
 
            format='dirstate-with-subtree')
 
1903
            format='development-subtree')
1893
1904
        tree.add_reference(subtree)
1894
1905
        tree.commit('a revision')
1895
1906
        tree.branch.create_checkout('target')
2173
2184
        def rot13(chunks, context=None):
2174
2185
            return [''.join(chunks).encode('rot13')]
2175
2186
        rot13filter = filters.ContentFilter(rot13, rot13)
2176
 
        filters.register_filter_stack_map('rot13', {'yes': [rot13filter]}.get)
 
2187
        filters.filter_stacks_registry.register(
 
2188
            'rot13', {'yes': [rot13filter]}.get)
2177
2189
        os.mkdir(self.test_home_dir + '/.bazaar')
2178
2190
        rules_filename = self.test_home_dir + '/.bazaar/rules'
2179
2191
        f = open(rules_filename, 'wb')
2209
2221
        self.assertEqualStat(source_stat, target_stat)
2210
2222
 
2211
2223
    def test_case_insensitive_build_tree_inventory(self):
2212
 
        if (tests.CaseInsensitiveFilesystemFeature.available()
2213
 
            or tests.CaseInsCasePresFilenameFeature.available()):
 
2224
        if (features.CaseInsensitiveFilesystemFeature.available()
 
2225
            or features.CaseInsCasePresFilenameFeature.available()):
2214
2226
            raise tests.UnavailableFeature('Fully case sensitive filesystem')
2215
2227
        source = self.make_branch_and_tree('source')
2216
2228
        self.build_tree(['source/file', 'source/FILE'])
2399
2411
        self.assertEqual('tree', revision.properties['branch-nick'])
2400
2412
 
2401
2413
 
2402
 
class TestBackupName(tests.TestCase):
2403
 
 
2404
 
    def test_deprecations(self):
2405
 
        class MockTransform(object):
2406
 
 
2407
 
            def has_named_child(self, by_parent, parent_id, name):
2408
 
                return name in by_parent.get(parent_id, [])
2409
 
 
2410
 
        class MockEntry(object):
2411
 
 
2412
 
            def __init__(self):
2413
 
                object.__init__(self)
2414
 
                self.name = "name"
2415
 
 
2416
 
        tt = MockTransform()
2417
 
        name1 = self.applyDeprecated(
2418
 
            symbol_versioning.deprecated_in((2, 3, 0)),
2419
 
            transform.get_backup_name, MockEntry(), {'a':[]}, 'a', tt)
2420
 
        self.assertEqual('name.~1~', name1)
2421
 
        name2 = self.applyDeprecated(
2422
 
            symbol_versioning.deprecated_in((2, 3, 0)),
2423
 
            transform._get_backup_name, 'name', {'a':['name.~1~']}, 'a', tt)
2424
 
        self.assertEqual('name.~2~', name2)
2425
 
 
2426
 
 
2427
2414
class TestFileMover(tests.TestCaseWithTransport):
2428
2415
 
2429
2416
    def test_file_mover(self):
2781
2768
 
2782
2769
    def test_iter_changes(self):
2783
2770
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
2784
 
        root = revision_tree.inventory.root.file_id
 
2771
        root = revision_tree.get_root_id()
2785
2772
        self.assertEqual([('a-id', ('a', 'a'), True, (True, True),
2786
2773
                          (root, root), ('a', 'a'), ('file', 'file'),
2787
2774
                          (False, False))],
2791
2778
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
2792
2779
        changes = preview_tree.iter_changes(revision_tree,
2793
2780
                                            include_unchanged=True)
2794
 
        root = revision_tree.inventory.root.file_id
 
2781
        root = revision_tree.get_root_id()
2795
2782
 
2796
2783
        self.assertEqual([ROOT_ENTRY, A_ENTRY], list(changes))
2797
2784
 
3329
3316
        self.assertEqual('contents', rev2_tree.get_file_text('file_id'))
3330
3317
 
3331
3318
    def test_ascii_limbo_paths(self):
3332
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
3319
        self.requireFeature(features.UnicodeFilenameFeature)
3333
3320
        branch = self.make_branch('any')
3334
3321
        tree = branch.repository.revision_tree(_mod_revision.NULL_REVISION)
3335
3322
        tt = TransformPreview(tree)
3352
3339
 
3353
3340
class TestSerializeTransform(tests.TestCaseWithTransport):
3354
3341
 
3355
 
    _test_needs_features = [tests.UnicodeFilenameFeature]
 
3342
    _test_needs_features = [features.UnicodeFilenameFeature]
3356
3343
 
3357
3344
    def get_preview(self, tree=None):
3358
3345
        if tree is None:
3433
3420
        return self.make_records(attribs, contents)
3434
3421
 
3435
3422
    def test_serialize_symlink_creation(self):
3436
 
        self.requireFeature(tests.SymlinkFeature)
 
3423
        self.requireFeature(features.SymlinkFeature)
3437
3424
        tt = self.get_preview()
3438
3425
        tt.new_symlink(u'foo\u1234', tt.root, u'bar\u1234')
3439
3426
        self.assertSerializesTo(self.symlink_creation_records(), tt)
3440
3427
 
3441
3428
    def test_deserialize_symlink_creation(self):
3442
 
        self.requireFeature(tests.SymlinkFeature)
 
3429
        self.requireFeature(features.SymlinkFeature)
3443
3430
        tt = self.get_preview()
3444
3431
        tt.deserialize(iter(self.symlink_creation_records()))
3445
3432
        abspath = tt._limbo_name('new-1')
3623
3610
        self.assertRaises(NotImplementedError, tt.new_orphan, 'foo', 'bar')
3624
3611
 
3625
3612
    def _set_orphan_policy(self, wt, policy):
3626
 
        wt.branch.get_config().set_user_option('bzr.transform.orphan_policy',
 
3613
        wt.branch.get_config_stack().set('bzr.transform.orphan_policy',
3627
3614
                                               policy)
3628
3615
 
3629
3616
    def _prepare_orphan(self, wt):
3655
3642
            warnings.append(args[0] % args[1:])
3656
3643
        self.overrideAttr(trace, 'warning', warning)
3657
3644
        remaining_conflicts = resolve_conflicts(tt)
3658
 
        self.assertEquals(['dir/foo has been orphaned in bzr-orphans'],
 
3645
        self.assertEqual(['dir/foo has been orphaned in bzr-orphans'],
3659
3646
                          warnings)
3660
3647
        # Yeah for resolved conflicts !
3661
3648
        self.assertLength(0, remaining_conflicts)
3662
3649
        # We have a new orphan
3663
 
        self.assertEquals('foo.~1~', tt.final_name(orphan_tid))
3664
 
        self.assertEquals('bzr-orphans',
 
3650
        self.assertEqual('foo.~1~', tt.final_name(orphan_tid))
 
3651
        self.assertEqual('bzr-orphans',
3665
3652
                          tt.final_name(tt.final_parent(orphan_tid)))
3666
3653
 
3667
3654
    def test_never_orphan(self):
3702
3689
        self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
3703
3690
                         remaining_conflicts.pop())
3704
3691
        self.assertLength(1, warnings)
3705
 
        self.assertStartsWith(warnings[0], 'donttouchmypreciouuus')
 
3692
        self.assertStartsWith(warnings[0], 'Value "donttouchmypreciouuus" ')
 
3693
 
 
3694
 
 
3695
class TestTransformHooks(tests.TestCaseWithTransport):
 
3696
 
 
3697
    def setUp(self):
 
3698
        super(TestTransformHooks, self).setUp()
 
3699
        self.wt = self.make_branch_and_tree('.')
 
3700
        os.chdir('..')
 
3701
 
 
3702
    def get_transform(self):
 
3703
        transform = TreeTransform(self.wt)
 
3704
        self.addCleanup(transform.finalize)
 
3705
        return transform, transform.root
 
3706
 
 
3707
    def test_pre_commit_hooks(self):
 
3708
        calls = []
 
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)
 
3715
        transform.apply()
 
3716
        self.assertEqual(old_root_id, self.wt.get_root_id())
 
3717
        self.assertEqual([(self.wt, transform)], calls)
 
3718
 
 
3719
    def test_post_commit_hooks(self):
 
3720
        calls = []
 
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)
 
3727
        transform.apply()
 
3728
        self.assertEqual(old_root_id, self.wt.get_root_id())
 
3729
        self.assertEqual([(self.wt, transform)], calls)