/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/tree_implementations/test_inv.py

merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007 Canonical Ltd
 
1
# Copyright (C) 2007, 2008 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
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""Tests for interface conformance of inventories of trees."""
18
18
 
20
20
from cStringIO import StringIO
21
21
import os
22
22
 
23
 
from bzrlib.diff import internal_diff
 
23
from bzrlib import (
 
24
    tests,
 
25
    )
 
26
from bzrlib.tests import tree_implementations
24
27
from bzrlib.mutabletree import MutableTree
25
 
from bzrlib.osutils import (
26
 
    has_symlinks,
27
 
    )
28
 
from bzrlib.tests import TestSkipped
29
 
from bzrlib.tests.tree_implementations import TestCaseWithTree
 
28
from bzrlib.tests import SymlinkFeature, TestSkipped
 
29
from bzrlib.transform import _PreviewTree
30
30
from bzrlib.uncommit import uncommit
31
31
 
32
32
 
33
 
class TestEntryDiffing(TestCaseWithTree):
34
 
 
35
 
    def setUp(self):
36
 
        super(TestEntryDiffing, self).setUp()
37
 
        self.wt = self.make_branch_and_tree('.')
38
 
        self.branch = self.wt.branch
39
 
        print >> open('file', 'wb'), 'foo'
40
 
        print >> open('binfile', 'wb'), 'foo'
41
 
        self.wt.add(['file'], ['fileid'])
42
 
        self.wt.add(['binfile'], ['binfileid'])
43
 
        if has_symlinks():
44
 
            os.symlink('target1', 'symlink')
45
 
            self.wt.add(['symlink'], ['linkid'])
46
 
        self.wt.commit('message_1', rev_id = '1')
47
 
        print >> open('file', 'wb'), 'bar'
48
 
        print >> open('binfile', 'wb'), 'x' * 1023 + '\x00'
49
 
        if has_symlinks():
50
 
            os.unlink('symlink')
51
 
            os.symlink('target2', 'symlink')
52
 
        self.tree_1 = self.branch.repository.revision_tree('1')
53
 
        self.inv_1 = self.branch.repository.get_inventory('1')
54
 
        self.file_1 = self.inv_1['fileid']
55
 
        self.file_1b = self.inv_1['binfileid']
56
 
        self.tree_2 = self.workingtree_to_test_tree(self.wt)
57
 
        self.tree_2.lock_read()
58
 
        self.addCleanup(self.tree_2.unlock)
59
 
        self.inv_2 = self.tree_2.inventory
60
 
        self.file_2 = self.inv_2['fileid']
61
 
        self.file_2b = self.inv_2['binfileid']
62
 
        if has_symlinks():
63
 
            self.link_1 = self.inv_1['linkid']
64
 
            self.link_2 = self.inv_2['linkid']
65
 
 
66
 
    def test_file_diff_deleted(self):
67
 
        output = StringIO()
68
 
        self.file_1.diff(internal_diff, 
69
 
                          "old_label", self.tree_1,
70
 
                          "/dev/null", None, None,
71
 
                          output)
72
 
        self.assertEqual(output.getvalue(), "--- old_label\n"
73
 
                                            "+++ /dev/null\n"
74
 
                                            "@@ -1,1 +0,0 @@\n"
75
 
                                            "-foo\n"
76
 
                                            "\n")
77
 
 
78
 
    def test_file_diff_added(self):
79
 
        output = StringIO()
80
 
        self.file_1.diff(internal_diff, 
81
 
                          "new_label", self.tree_1,
82
 
                          "/dev/null", None, None,
83
 
                          output, reverse=True)
84
 
        self.assertEqual(output.getvalue(), "--- /dev/null\n"
85
 
                                            "+++ new_label\n"
86
 
                                            "@@ -0,0 +1,1 @@\n"
87
 
                                            "+foo\n"
88
 
                                            "\n")
89
 
 
90
 
    def test_file_diff_changed(self):
91
 
        output = StringIO()
92
 
        self.file_1.diff(internal_diff, 
93
 
                          "/dev/null", self.tree_1, 
94
 
                          "new_label", self.file_2, self.tree_2,
95
 
                          output)
96
 
        self.assertEqual(output.getvalue(), "--- /dev/null\n"
97
 
                                            "+++ new_label\n"
98
 
                                            "@@ -1,1 +1,1 @@\n"
99
 
                                            "-foo\n"
100
 
                                            "+bar\n"
101
 
                                            "\n")
102
 
        
103
 
    def test_file_diff_binary(self):
104
 
        output = StringIO()
105
 
        self.file_1.diff(internal_diff, 
106
 
                          "/dev/null", self.tree_1, 
107
 
                          "new_label", self.file_2b, self.tree_2,
108
 
                          output)
109
 
        self.assertEqual(output.getvalue(), 
110
 
                         "Binary files /dev/null and new_label differ\n")
111
 
    def test_link_diff_deleted(self):
112
 
        if not has_symlinks():
113
 
            return
114
 
        output = StringIO()
115
 
        self.link_1.diff(internal_diff, 
116
 
                          "old_label", self.tree_1,
117
 
                          "/dev/null", None, None,
118
 
                          output)
119
 
        self.assertEqual(output.getvalue(),
120
 
                         "=== target was 'target1'\n")
121
 
 
122
 
    def test_link_diff_added(self):
123
 
        if not has_symlinks():
124
 
            return
125
 
        output = StringIO()
126
 
        self.link_1.diff(internal_diff, 
127
 
                          "new_label", self.tree_1,
128
 
                          "/dev/null", None, None,
129
 
                          output, reverse=True)
130
 
        self.assertEqual(output.getvalue(),
131
 
                         "=== target is 'target1'\n")
132
 
 
133
 
    def test_link_diff_changed(self):
134
 
        if not has_symlinks():
135
 
            return
136
 
        output = StringIO()
137
 
        self.link_1.diff(internal_diff, 
138
 
                          "/dev/null", self.tree_1, 
139
 
                          "new_label", self.link_2, self.tree_2,
140
 
                          output)
141
 
        self.assertEqual(output.getvalue(),
142
 
                         "=== target changed 'target1' => 'target2'\n")
143
 
 
144
 
 
145
 
class TestPreviousHeads(TestCaseWithTree):
 
33
def get_entry(tree, file_id):
 
34
    return tree.iter_entries_by_dir([file_id]).next()[1]
 
35
 
 
36
 
 
37
class TestPreviousHeads(tree_implementations.TestCaseWithTree):
146
38
 
147
39
    def setUp(self):
148
40
        # we want several inventories, that respectively
172
64
        self.tree = self.workingtree_to_test_tree(self.wt)
173
65
        self.tree.lock_read()
174
66
        self.addCleanup(self.tree.unlock)
175
 
        self.file_active = self.tree.inventory['fileid']
176
 
        self.weave = self.branch.repository.weave_store.get_weave('fileid',
177
 
            self.branch.repository.get_transaction())
178
 
        
179
 
    def get_previous_heads(self, inventories):
180
 
        return self.file_active.find_previous_heads(
181
 
            inventories, 
182
 
            self.branch.repository.weave_store,
183
 
            self.branch.repository.get_transaction())
184
 
        
185
 
    def test_fileid_in_no_inventory(self):
186
 
        self.assertEqual({}, self.get_previous_heads([self.inv_A]))
187
 
 
188
 
    def test_fileid_in_one_inventory(self):
189
 
        self.assertEqual({'B':self.inv_B['fileid']},
190
 
                         self.get_previous_heads([self.inv_B]))
191
 
        self.assertEqual({'B':self.inv_B['fileid']},
192
 
                         self.get_previous_heads([self.inv_A, self.inv_B]))
193
 
        self.assertEqual({'B':self.inv_B['fileid']},
194
 
                         self.get_previous_heads([self.inv_B, self.inv_A]))
195
 
 
196
 
    def test_fileid_in_two_inventories_gives_both_entries(self):
197
 
        self.assertEqual({'B':self.inv_B['fileid'],
198
 
                          'C':self.inv_C['fileid']},
199
 
                          self.get_previous_heads([self.inv_B, self.inv_C]))
200
 
        self.assertEqual({'B':self.inv_B['fileid'],
201
 
                          'C':self.inv_C['fileid']},
202
 
                          self.get_previous_heads([self.inv_C, self.inv_B]))
203
 
 
204
 
    def test_fileid_in_two_inventories_already_merged_gives_head(self):
205
 
        self.assertEqual({'D':self.inv_D['fileid']},
206
 
                         self.get_previous_heads([self.inv_B, self.inv_D]))
207
 
        self.assertEqual({'D':self.inv_D['fileid']},
208
 
                         self.get_previous_heads([self.inv_D, self.inv_B]))
209
 
 
210
 
    # TODO: test two inventories with the same file revision 
211
 
 
212
 
 
213
 
class TestInventory(TestCaseWithTree):
214
 
 
215
 
    def _set_up(self):
 
67
        self.file_active = get_entry(self.tree, 'fileid')
 
68
 
 
69
    # TODO: test two inventories with the same file revision
 
70
 
 
71
 
 
72
class TestInventoryWithSymlinks(tree_implementations.TestCaseWithTree):
 
73
 
 
74
    _test_needs_features = [tests.SymlinkFeature]
 
75
 
 
76
    def setUp(self):
 
77
        tree_implementations.TestCaseWithTree.setUp(self)
216
78
        self.tree = self.get_tree_with_subdirs_and_all_content_types()
217
79
        self.tree.lock_read()
218
80
        self.addCleanup(self.tree.unlock)
219
 
        # Commenting out the following line still fails.
220
 
        self.inv = self.tree.inventory
221
81
 
222
82
    def test_symlink_target(self):
223
 
        if not has_symlinks():
224
 
            raise TestSkipped('No symlink support')
225
 
        self._set_up()
226
 
        if isinstance(self.tree, MutableTree):
 
83
        if isinstance(self.tree, (MutableTree, _PreviewTree)):
227
84
            raise TestSkipped(
228
 
                'symlinks not accurately represented in working trees')
229
 
        entry = self.inv[self.inv.path2id('symlink')]
 
85
                'symlinks not accurately represented in working trees and'
 
86
                ' preview trees')
 
87
        entry = get_entry(self.tree, self.tree.path2id('symlink'))
230
88
        self.assertEqual(entry.symlink_target, 'link-target')
231
89
 
 
90
    def test_symlink_target_tree(self):
 
91
        self.assertEqual('link-target',
 
92
                         self.tree.get_symlink_target('symlink'))
 
93
 
 
94
    def test_kind_symlink(self):
 
95
        self.assertEqual('symlink', self.tree.kind('symlink'))
 
96
        self.assertIs(None, self.tree.get_file_size('symlink'))
 
97
 
232
98
    def test_symlink(self):
233
 
        if not has_symlinks():
234
 
            raise TestSkipped('No symlink support')
235
 
        self._set_up()
236
 
        entry = self.inv[self.inv.path2id('symlink')]
 
99
        entry = get_entry(self.tree, self.tree.path2id('symlink'))
237
100
        self.assertEqual(entry.kind, 'symlink')
238
101
        self.assertEqual(None, entry.text_size)
 
102
 
 
103
 
 
104
class TestInventory(tree_implementations.TestCaseWithTree):
 
105
 
 
106
    def test_paths2ids_recursive(self):
 
107
        work_tree = self.make_branch_and_tree('tree')
 
108
        self.build_tree(['tree/dir/', 'tree/dir/file'])
 
109
        work_tree.add(['dir', 'dir/file'], ['dir-id', 'file-id'])
 
110
        tree = self._convert_tree(work_tree)
 
111
        tree.lock_read()
 
112
        self.addCleanup(tree.unlock)
 
113
        self.assertEqual(set(['dir-id', 'file-id']), tree.paths2ids(['dir']))
 
114
 
 
115
    def test_paths2ids_forget_old(self):
 
116
        work_tree = self.make_branch_and_tree('tree')
 
117
        self.build_tree(['tree/file'])
 
118
        work_tree.add('file', 'first-id')
 
119
        work_tree.commit('commit old state')
 
120
        work_tree.remove('file')
 
121
        tree = self._convert_tree(work_tree)
 
122
        tree.lock_read()
 
123
        self.addCleanup(tree.unlock)
 
124
        self.assertEqual(set([]), tree.paths2ids(['file'],
 
125
                         require_versioned=False))
 
126
 
 
127
    def _make_canonical_test_tree(self, commit=True):
 
128
        # make a tree used by all the 'canonical' tests below.
 
129
        work_tree = self.make_branch_and_tree('tree')
 
130
        self.build_tree(['tree/dir/', 'tree/dir/file'])
 
131
        work_tree.add(['dir', 'dir/file'])
 
132
        if commit:
 
133
            work_tree.commit('commit 1')
 
134
        return work_tree
 
135
 
 
136
    def test_canonical_path(self):
 
137
        work_tree = self._make_canonical_test_tree()
 
138
        self.assertEqual('dir/file',
 
139
                         work_tree.get_canonical_inventory_path('Dir/File'))
 
140
 
 
141
    def test_canonical_path_before_commit(self):
 
142
        work_tree = self._make_canonical_test_tree(False)
 
143
        # note: not committed.
 
144
        self.assertEqual('dir/file',
 
145
                         work_tree.get_canonical_inventory_path('Dir/File'))
 
146
 
 
147
    def test_canonical_path_dir(self):
 
148
        # check it works when asked for just the directory portion.
 
149
        work_tree = self._make_canonical_test_tree()
 
150
        self.assertEqual('dir', work_tree.get_canonical_inventory_path('Dir'))
 
151
 
 
152
    def test_canonical_path_root(self):
 
153
        work_tree = self._make_canonical_test_tree()
 
154
        self.assertEqual('', work_tree.get_canonical_inventory_path(''))
 
155
        self.assertEqual('/', work_tree.get_canonical_inventory_path('/'))
 
156
 
 
157
    def test_canonical_path_invalid_all(self):
 
158
        work_tree = self._make_canonical_test_tree()
 
159
        self.assertEqual('foo/bar',
 
160
                         work_tree.get_canonical_inventory_path('foo/bar'))
 
161
 
 
162
    def test_canonical_invalid_child(self):
 
163
        work_tree = self._make_canonical_test_tree()
 
164
        self.assertEqual('dir/None',
 
165
                         work_tree.get_canonical_inventory_path('Dir/None'))