~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_transform.py

  • Committer: Patch Queue Manager
  • Date: 2016-02-01 19:13:13 UTC
  • mfrom: (6614.2.2 trunk)
  • Revision ID: pqm@pqm.ubuntu.com-20160201191313-wdfvmfff1djde6oq
(vila) Release 2.7.0 (Vincent Ladeuil)

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,
65
66
    TestCaseInTempDir,
87
88
 
88
89
    def setUp(self):
89
90
        super(TestTreeTransform, self).setUp()
90
 
        self.wt = self.make_branch_and_tree('.', format='dirstate-with-subtree')
 
91
        self.wt = self.make_branch_and_tree('.', format='development-subtree')
91
92
        os.chdir('..')
92
93
 
93
94
    def get_transform(self):
398
399
        tree.lock_read()
399
400
        self.addCleanup(tree.unlock)
400
401
        self.assertEqual('subtree-revision',
401
 
                         tree.inventory['subtree-id'].reference_revision)
 
402
                         tree.root_inventory['subtree-id'].reference_revision)
402
403
 
403
404
    def test_conflicts(self):
404
405
        transform, root = self.get_transform()
455
456
        newtip = transform2.new_file('tip', oz_id, 'other', 'tip-id')
456
457
        result = transform2.find_conflicts()
457
458
        fp = FinalPaths(transform2)
458
 
        self.assert_('oz/tip' in transform2._tree_path_ids)
 
459
        self.assertTrue('oz/tip' in transform2._tree_path_ids)
459
460
        self.assertEqual(fp.get_path(newtip), pathjoin('oz', 'tip'))
460
461
        self.assertEqual(len(result), 2)
461
462
        self.assertEqual((result[0][0], result[0][1]),
800
801
        os.symlink = None
801
802
        try:
802
803
            err = self.assertRaises(errors.UnableCreateSymlink, tt_helper)
803
 
            self.assertEquals(
 
804
            self.assertEqual(
804
805
                "Unable to create symlink 'foo' on this platform",
805
806
                str(err))
806
807
        finally:
1476
1477
        # The rename will fail because the target directory is not empty (but
1477
1478
        # raises FileExists anyway).
1478
1479
        err = self.assertRaises(errors.FileExists, tt_helper)
1479
 
        self.assertContainsRe(str(err),
1480
 
            "^File exists: .+/baz")
 
1480
        self.assertEndsWith(err.path, "/baz")
1481
1481
 
1482
1482
    def test_two_directories_clash(self):
1483
1483
        def tt_helper():
1495
1495
                wt.unlock()
1496
1496
                raise
1497
1497
        err = self.assertRaises(errors.FileExists, tt_helper)
1498
 
        self.assertContainsRe(str(err),
1499
 
            "^File exists: .+/foo")
 
1498
        self.assertEndsWith(err.path, "/foo")
1500
1499
 
1501
1500
    def test_two_directories_clash_finalize(self):
1502
1501
        def tt_helper():
1514
1513
                tt.finalize()
1515
1514
                raise
1516
1515
        err = self.assertRaises(errors.FileExists, tt_helper)
1517
 
        self.assertContainsRe(str(err),
1518
 
            "^File exists: .+/foo")
 
1516
        self.assertEndsWith(err.path, "/foo")
1519
1517
 
1520
1518
    def test_file_to_directory(self):
1521
1519
        wt = self.make_branch_and_tree('.')
1534
1532
        self.assertPathExists("foo/bar")
1535
1533
        wt.lock_read()
1536
1534
        try:
1537
 
            self.assertEqual(wt.inventory.get_file_kind(wt.path2id("foo")),
1538
 
                    "directory")
 
1535
            self.assertEqual(wt.kind(wt.path2id("foo")), "directory")
1539
1536
        finally:
1540
1537
            wt.unlock()
1541
1538
        wt.commit("two")
1557
1554
        self.assertPathExists("foo")
1558
1555
        wt.lock_read()
1559
1556
        self.addCleanup(wt.unlock)
1560
 
        self.assertEqual(wt.inventory.get_file_kind(wt.path2id("foo")),
1561
 
                "symlink")
 
1557
        self.assertEqual(wt.kind(wt.path2id("foo")), "symlink")
1562
1558
 
1563
1559
    def test_dir_to_file(self):
1564
1560
        wt = self.make_branch_and_tree('.')
1576
1572
        self.assertPathExists("foo")
1577
1573
        wt.lock_read()
1578
1574
        self.addCleanup(wt.unlock)
1579
 
        self.assertEqual(wt.inventory.get_file_kind(wt.path2id("foo")),
1580
 
                "file")
 
1575
        self.assertEqual(wt.kind(wt.path2id("foo")), "file")
1581
1576
 
1582
1577
    def test_dir_to_hardlink(self):
1583
1578
        self.requireFeature(HardlinkFeature)
1598
1593
        self.assertPathExists("baz")
1599
1594
        wt.lock_read()
1600
1595
        self.addCleanup(wt.unlock)
1601
 
        self.assertEqual(wt.inventory.get_file_kind(wt.path2id("foo")),
1602
 
                "file")
 
1596
        self.assertEqual(wt.kind(wt.path2id("foo")), "file")
1603
1597
 
1604
1598
    def test_no_final_path(self):
1605
1599
        transform, root = self.get_transform()
1651
1645
    def __init__(self, dirname, root_id):
1652
1646
        self.name = dirname
1653
1647
        os.mkdir(dirname)
1654
 
        self.wt = BzrDir.create_standalone_workingtree(dirname)
 
1648
        self.wt = ControlDir.create_standalone_workingtree(dirname)
1655
1649
        self.wt.set_root_id(root_id)
1656
1650
        self.b = self.wt.branch
1657
1651
        self.tt = TreeTransform(self.wt)
1767
1761
        merge_modified = this.wt.merge_modified()
1768
1762
        self.assertSubset(merge_modified, modified)
1769
1763
        self.assertEqual(len(merge_modified), len(modified))
1770
 
        file(this.wt.id2abspath('a'), 'wb').write('booga')
 
1764
        with file(this.wt.id2abspath('a'), 'wb') as f: f.write('booga')
1771
1765
        modified.pop(0)
1772
1766
        merge_modified = this.wt.merge_modified()
1773
1767
        self.assertSubset(merge_modified, modified)
1887
1881
    def test_build_tree_with_symlinks(self):
1888
1882
        self.requireFeature(SymlinkFeature)
1889
1883
        os.mkdir('a')
1890
 
        a = BzrDir.create_standalone_workingtree('a')
 
1884
        a = ControlDir.create_standalone_workingtree('a')
1891
1885
        os.mkdir('a/foo')
1892
 
        file('a/foo/bar', 'wb').write('contents')
 
1886
        with file('a/foo/bar', 'wb') as f: f.write('contents')
1893
1887
        os.symlink('a/foo/bar', 'a/foo/baz')
1894
1888
        a.add(['foo', 'foo/bar', 'foo/baz'])
1895
1889
        a.commit('initial commit')
1896
 
        b = BzrDir.create_standalone_workingtree('b')
 
1890
        b = ControlDir.create_standalone_workingtree('b')
1897
1891
        basis = a.basis_tree()
1898
1892
        basis.lock_read()
1899
1893
        self.addCleanup(basis.unlock)
1904
1898
 
1905
1899
    def test_build_with_references(self):
1906
1900
        tree = self.make_branch_and_tree('source',
1907
 
            format='dirstate-with-subtree')
 
1901
            format='development-subtree')
1908
1902
        subtree = self.make_branch_and_tree('source/subtree',
1909
 
            format='dirstate-with-subtree')
 
1903
            format='development-subtree')
1910
1904
        tree.add_reference(subtree)
1911
1905
        tree.commit('a revision')
1912
1906
        tree.branch.create_checkout('target')
2190
2184
        def rot13(chunks, context=None):
2191
2185
            return [''.join(chunks).encode('rot13')]
2192
2186
        rot13filter = filters.ContentFilter(rot13, rot13)
2193
 
        filters.register_filter_stack_map('rot13', {'yes': [rot13filter]}.get)
 
2187
        filters.filter_stacks_registry.register(
 
2188
            'rot13', {'yes': [rot13filter]}.get)
2194
2189
        os.mkdir(self.test_home_dir + '/.bazaar')
2195
2190
        rules_filename = self.test_home_dir + '/.bazaar/rules'
2196
2191
        f = open(rules_filename, 'wb')
2416
2411
        self.assertEqual('tree', revision.properties['branch-nick'])
2417
2412
 
2418
2413
 
2419
 
class TestBackupName(tests.TestCase):
2420
 
 
2421
 
    def test_deprecations(self):
2422
 
        class MockTransform(object):
2423
 
 
2424
 
            def has_named_child(self, by_parent, parent_id, name):
2425
 
                return name in by_parent.get(parent_id, [])
2426
 
 
2427
 
        class MockEntry(object):
2428
 
 
2429
 
            def __init__(self):
2430
 
                object.__init__(self)
2431
 
                self.name = "name"
2432
 
 
2433
 
        tt = MockTransform()
2434
 
        name1 = self.applyDeprecated(
2435
 
            symbol_versioning.deprecated_in((2, 3, 0)),
2436
 
            transform.get_backup_name, MockEntry(), {'a':[]}, 'a', tt)
2437
 
        self.assertEqual('name.~1~', name1)
2438
 
        name2 = self.applyDeprecated(
2439
 
            symbol_versioning.deprecated_in((2, 3, 0)),
2440
 
            transform._get_backup_name, 'name', {'a':['name.~1~']}, 'a', tt)
2441
 
        self.assertEqual('name.~2~', name2)
2442
 
 
2443
 
 
2444
2414
class TestFileMover(tests.TestCaseWithTransport):
2445
2415
 
2446
2416
    def test_file_mover(self):
2798
2768
 
2799
2769
    def test_iter_changes(self):
2800
2770
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
2801
 
        root = revision_tree.inventory.root.file_id
 
2771
        root = revision_tree.get_root_id()
2802
2772
        self.assertEqual([('a-id', ('a', 'a'), True, (True, True),
2803
2773
                          (root, root), ('a', 'a'), ('file', 'file'),
2804
2774
                          (False, False))],
2808
2778
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
2809
2779
        changes = preview_tree.iter_changes(revision_tree,
2810
2780
                                            include_unchanged=True)
2811
 
        root = revision_tree.inventory.root.file_id
 
2781
        root = revision_tree.get_root_id()
2812
2782
 
2813
2783
        self.assertEqual([ROOT_ENTRY, A_ENTRY], list(changes))
2814
2784
 
3640
3610
        self.assertRaises(NotImplementedError, tt.new_orphan, 'foo', 'bar')
3641
3611
 
3642
3612
    def _set_orphan_policy(self, wt, policy):
3643
 
        wt.branch.get_config().set_user_option('bzr.transform.orphan_policy',
 
3613
        wt.branch.get_config_stack().set('bzr.transform.orphan_policy',
3644
3614
                                               policy)
3645
3615
 
3646
3616
    def _prepare_orphan(self, wt):
3672
3642
            warnings.append(args[0] % args[1:])
3673
3643
        self.overrideAttr(trace, 'warning', warning)
3674
3644
        remaining_conflicts = resolve_conflicts(tt)
3675
 
        self.assertEquals(['dir/foo has been orphaned in bzr-orphans'],
 
3645
        self.assertEqual(['dir/foo has been orphaned in bzr-orphans'],
3676
3646
                          warnings)
3677
3647
        # Yeah for resolved conflicts !
3678
3648
        self.assertLength(0, remaining_conflicts)
3679
3649
        # We have a new orphan
3680
 
        self.assertEquals('foo.~1~', tt.final_name(orphan_tid))
3681
 
        self.assertEquals('bzr-orphans',
 
3650
        self.assertEqual('foo.~1~', tt.final_name(orphan_tid))
 
3651
        self.assertEqual('bzr-orphans',
3682
3652
                          tt.final_name(tt.final_parent(orphan_tid)))
3683
3653
 
3684
3654
    def test_never_orphan(self):
3719
3689
        self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
3720
3690
                         remaining_conflicts.pop())
3721
3691
        self.assertLength(1, warnings)
3722
 
        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)