~bzr-pqm/bzr/bzr.dev

2338.4.2 by Marien Zwart
Move the workingtree-related inventory tests to a separate file.
1
# Copyright (C) 2007 Canonical Ltd
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2338.4.2 by Marien Zwart
Move the workingtree-related inventory tests to a separate file.
16
2338.4.6 by Marien Zwart
Move some tests that do not need a working tree from workingtree_implementations to tree_implementations.
17
"""Tests for interface conformance of inventories of working trees."""
18
19
2338.4.2 by Marien Zwart
Move the workingtree-related inventory tests to a separate file.
20
import os
21
5786.1.1 by John Arbash Meinel
Fix bug #764677. WT.inventory should correctly return TreeReference
22
from bzrlib import inventory, tests
4523.1.4 by Martin Pool
Rename remaining *_implementations tests
23
from bzrlib.tests.per_workingtree import TestCaseWithWorkingTree
2338.4.2 by Marien Zwart
Move the workingtree-related inventory tests to a separate file.
24
25
2338.4.3 by Marien Zwart
Make the workingtree inventory tests actually use the different workingtree implementations.
26
class TestRevert(TestCaseWithWorkingTree):
2338.4.2 by Marien Zwart
Move the workingtree-related inventory tests to a separate file.
27
28
    def test_dangling_id(self):
29
        wt = self.make_branch_and_tree('b1')
30
        wt.lock_tree_write()
31
        self.addCleanup(wt.unlock)
5807.1.3 by Jelmer Vernooij
Avoid inventory.
32
        self.assertEqual(len(wt.all_file_ids()), 1)
6437.20.3 by Wouter van Heyst
mechanically replace file().write() pattern with a with-keyword version
33
        with open('b1/a', 'wb') as f: f.write('a test\n')
2338.4.2 by Marien Zwart
Move the workingtree-related inventory tests to a separate file.
34
        wt.add('a')
5807.1.3 by Jelmer Vernooij
Avoid inventory.
35
        self.assertEqual(len(wt.all_file_ids()), 2)
2338.4.2 by Marien Zwart
Move the workingtree-related inventory tests to a separate file.
36
        wt.flush() # workaround revert doing wt._write_inventory for now.
37
        os.unlink('b1/a')
2748.3.2 by Aaron Bentley
Fix revert, remove-tree, and various tests to use None for 'no files specified'
38
        wt.revert()
5807.1.3 by Jelmer Vernooij
Avoid inventory.
39
        self.assertEqual(len(wt.all_file_ids()), 1)
2338.4.2 by Marien Zwart
Move the workingtree-related inventory tests to a separate file.
40
41
2376.2.1 by Aaron Bentley
Implement MutableTree.apply_inventory_delta
42
class TestApplyInventoryDelta(TestCaseWithWorkingTree):
43
5858.3.3 by Jelmer Vernooij
Skip tests early, before creating a tree.
44
    def setUp(self):
45
        super(TestApplyInventoryDelta, self).setUp()
46
        if not self.bzrdir_format.repository_format.supports_full_versioned_files:
47
            raise tests.TestNotApplicable(
48
                "format does not support inventory deltas")
49
2376.2.1 by Aaron Bentley
Implement MutableTree.apply_inventory_delta
50
    def test_add(self):
51
        wt = self.make_branch_and_tree('.')
52
        wt.lock_write()
53
        self.addCleanup(wt.unlock)
54
        root_id = wt.get_root_id()
55
        wt.apply_inventory_delta([(None, 'bar/foo', 'foo-id',
56
            inventory.InventoryFile('foo-id', 'foo', parent_id='bar-id')),
57
            (None, 'bar', 'bar-id', inventory.InventoryDirectory('bar-id',
58
            'bar', parent_id=root_id))])
6405.2.10 by Jelmer Vernooij
Fix more tests.
59
        self.assertEqual('bar/foo', wt.id2path('foo-id'))
60
        self.assertEqual('bar', wt.id2path('bar-id'))
2376.2.1 by Aaron Bentley
Implement MutableTree.apply_inventory_delta
61
62
    def test_remove(self):
63
        wt = self.make_branch_and_tree('.')
64
        wt.lock_write()
65
        self.addCleanup(wt.unlock)
66
        self.build_tree(['foo/', 'foo/bar'])
67
        wt.add(['foo', 'foo/bar'], ['foo-id', 'bar-id'])
68
        wt.apply_inventory_delta([('foo', None, 'foo-id', None),
69
                                  ('foo/bar', None, 'bar-id', None)])
70
        self.assertIs(None, wt.path2id('foo'))
71
2865.1.1 by Robert Collins
Create new mutable tree method update_to_one_parent_via_delta for eventual use by commit.
72
    def test_rename_dir_with_children(self):
73
        wt = self.make_branch_and_tree('.')
74
        wt.lock_write()
75
        root_id = wt.get_root_id()
76
        self.addCleanup(wt.unlock)
77
        self.build_tree(['foo/', 'foo/bar'])
78
        wt.add(['foo', 'foo/bar'],
79
               ['foo-id', 'bar-id'])
80
        wt.apply_inventory_delta([('foo', 'baz', 'foo-id',
81
            inventory.InventoryDirectory('foo-id', 'baz', root_id))])
82
        # foo/bar should have been followed the rename of its parent to baz/bar
3146.8.5 by Aaron Bentley
Get apply_inventory_delta kinda working, but not children
83
        self.assertEqual('baz', wt.id2path('foo-id'))
2865.1.1 by Robert Collins
Create new mutable tree method update_to_one_parent_via_delta for eventual use by commit.
84
        self.assertEqual('baz/bar', wt.id2path('bar-id'))
85
86
    def test_rename_dir_with_children_with_children(self):
87
        wt = self.make_branch_and_tree('.')
88
        wt.lock_write()
89
        root_id = wt.get_root_id()
90
        self.addCleanup(wt.unlock)
91
        self.build_tree(['foo/', 'foo/bar/', 'foo/bar/baz'])
92
        wt.add(['foo', 'foo/bar', 'foo/bar/baz'],
93
               ['foo-id', 'bar-id', 'baz-id'])
94
        wt.apply_inventory_delta([('foo', 'quux', 'foo-id',
95
            inventory.InventoryDirectory('foo-id', 'quux', root_id))])
96
        # foo/bar/baz should have been followed the rename of its parent's
97
        # parent to quux/bar/baz
98
        self.assertEqual('quux/bar/baz', wt.id2path('baz-id'))
99
2376.2.1 by Aaron Bentley
Implement MutableTree.apply_inventory_delta
100
    def test_rename_file(self):
101
        wt = self.make_branch_and_tree('.')
102
        wt.lock_write()
103
        self.addCleanup(wt.unlock)
104
        self.build_tree(['foo/', 'foo/bar', 'baz/'])
105
        wt.add(['foo', 'foo/bar', 'baz'],
106
               ['foo-id', 'bar-id', 'baz-id'])
107
        wt.apply_inventory_delta([('foo/bar', 'baz/bar', 'bar-id',
108
            inventory.InventoryFile('bar-id', 'bar', 'baz-id'))])
109
        self.assertEqual('baz/bar', wt.id2path('bar-id'))
110
111
    def test_rename_swap(self):
112
        """Test the swap-names edge case.
113
114
        foo and bar should swap names, but retain their children.  If this
115
        works, any simpler rename ought to work.
116
        """
117
        wt = self.make_branch_and_tree('.')
118
        wt.lock_write()
119
        root_id = wt.get_root_id()
120
        self.addCleanup(wt.unlock)
121
        self.build_tree(['foo/', 'foo/bar', 'baz/', 'baz/qux'])
122
        wt.add(['foo', 'foo/bar', 'baz', 'baz/qux'],
123
               ['foo-id', 'bar-id', 'baz-id', 'qux-id'])
124
        wt.apply_inventory_delta([('foo', 'baz', 'foo-id',
125
            inventory.InventoryDirectory('foo-id', 'baz', root_id)),
126
            ('baz', 'foo', 'baz-id',
127
            inventory.InventoryDirectory('baz-id', 'foo', root_id))])
128
        self.assertEqual('baz/bar', wt.id2path('bar-id'))
129
        self.assertEqual('foo/qux', wt.id2path('qux-id'))
2376.2.3 by Aaron Bentley
Fix root replacement for apply_inventory_delta
130
2376.2.7 by Aaron Bentley
Add another test case (suggested by John Meinel)
131
    def test_child_rename_ordering(self):
132
        """Test the rename-parent, move child edge case.
133
134
        (A naive implementation may move the parent first, and then be
135
         unable to find the child.)
136
        """
137
        wt = self.make_branch_and_tree('.')
138
        root_id = wt.get_root_id()
139
        self.build_tree(['dir/', 'dir/child', 'other/'])
140
        wt.add(['dir', 'dir/child', 'other'],
141
               ['dir-id', 'child-id', 'other-id'])
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
142
        # this delta moves dir-id to dir2 and reparents
2865.1.3 by Robert Collins
Review feedback.
143
        # child-id to a parent of other-id
2376.2.7 by Aaron Bentley
Add another test case (suggested by John Meinel)
144
        wt.apply_inventory_delta([('dir', 'dir2', 'dir-id',
145
            inventory.InventoryDirectory('dir-id', 'dir2', root_id)),
146
            ('dir/child', 'other/child', 'child-id',
147
             inventory.InventoryFile('child-id', 'child', 'other-id'))])
148
        self.assertEqual('dir2', wt.id2path('dir-id'))
149
        self.assertEqual('other/child', wt.id2path('child-id'))
150
2376.2.3 by Aaron Bentley
Fix root replacement for apply_inventory_delta
151
    def test_replace_root(self):
152
        wt = self.make_branch_and_tree('.')
153
        wt.lock_write()
154
        self.addCleanup(wt.unlock)
155
156
        root_id = wt.get_root_id()
157
        wt.apply_inventory_delta([('', None, root_id, None),
158
            (None, '', 'root-id',
159
             inventory.InventoryDirectory('root-id', '', None))])
5786.1.1 by John Arbash Meinel
Fix bug #764677. WT.inventory should correctly return TreeReference
160
161
162
class TestTreeReference(TestCaseWithWorkingTree):
163
164
    def test_tree_reference_matches_inv(self):
5858.3.2 by Jelmer Vernooij
Fix spacing.
165
        base = self.make_branch_and_tree('base')
5858.3.1 by Jelmer Vernooij
Raise TestNotApplicable for things that don't have an inventory delta.
166
        if base.branch.repository._format.supports_full_versioned_files:
167
            raise tests.TestNotApplicable(
168
                "format does not support inventory deltas")
5786.1.6 by John Arbash Meinel
Clean up some of the old tests that I had updated.
169
        if not base.supports_tree_reference():
170
            raise tests.TestNotApplicable("wt doesn't support nested trees")
171
        # We add it as a directory, but it becomes a tree-reference
172
        base.add(['subdir'], ['subdir-id'], ['directory'])
5786.1.1 by John Arbash Meinel
Fix bug #764677. WT.inventory should correctly return TreeReference
173
        subdir = self.make_branch_and_tree('base/subdir')
174
        self.addCleanup(base.lock_read().unlock)
5786.1.6 by John Arbash Meinel
Clean up some of the old tests that I had updated.
175
        # Note: we aren't strict about ie.kind being 'directory' here, what we
176
        # are strict about is that wt.inventory should match
177
        # wt.current_dirstate()'s idea about what files are where.
5786.1.1 by John Arbash Meinel
Fix bug #764677. WT.inventory should correctly return TreeReference
178
        ie = base.inventory['subdir-id']
5786.1.6 by John Arbash Meinel
Clean up some of the old tests that I had updated.
179
        self.assertEqual('directory', ie.kind)
5786.1.1 by John Arbash Meinel
Fix bug #764677. WT.inventory should correctly return TreeReference
180
        path, ie = base.iter_entries_by_dir(['subdir-id']).next()
181
        self.assertEqual('subdir', path)
182
        self.assertEqual('tree-reference', ie.kind)