1
# Copyright (C) 2007-2010 Canonical Ltd
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.
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.
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
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Tests for interface conformance of inventories of trees."""
20
from cStringIO import StringIO
26
from bzrlib.tests import (
30
from bzrlib.mutabletree import MutableTree
31
from bzrlib.tests import SymlinkFeature, TestSkipped
32
from bzrlib.transform import _PreviewTree
33
from bzrlib.uncommit import uncommit
36
def get_entry(tree, file_id):
37
return tree.iter_entries_by_dir([file_id]).next()[1]
40
class TestPreviousHeads(per_tree.TestCaseWithTree):
43
# we want several inventories, that respectively
44
# give use the following scenarios:
45
# A) fileid not in any inventory (A),
46
# B) fileid present in one inventory (B) and (A,B)
47
# C) fileid present in two inventories, and they
48
# are not mutual descendents (B, C)
49
# D) fileid present in two inventories and one is
50
# a descendent of the other. (B, D)
51
super(TestPreviousHeads, self).setUp()
52
self.wt = self.make_branch_and_tree('.')
53
self.branch = self.wt.branch
54
self.build_tree(['file'])
55
self.wt.commit('new branch', allow_pointless=True, rev_id='A')
56
self.inv_A = self.branch.repository.get_inventory('A')
57
self.wt.add(['file'], ['fileid'])
58
self.wt.commit('add file', rev_id='B')
59
self.inv_B = self.branch.repository.get_inventory('B')
60
uncommit(self.branch, tree=self.wt)
61
self.assertEqual(self.branch.revision_history(), ['A'])
62
self.wt.commit('another add of file', rev_id='C')
63
self.inv_C = self.branch.repository.get_inventory('C')
64
self.wt.add_parent_tree_id('B')
65
self.wt.commit('merge in B', rev_id='D')
66
self.inv_D = self.branch.repository.get_inventory('D')
67
self.tree = self.workingtree_to_test_tree(self.wt)
69
self.addCleanup(self.tree.unlock)
70
self.file_active = get_entry(self.tree, 'fileid')
72
# TODO: test two inventories with the same file revision
75
class TestInventoryWithSymlinks(per_tree.TestCaseWithTree):
77
_test_needs_features = [tests.SymlinkFeature]
80
per_tree.TestCaseWithTree.setUp(self)
81
self.tree = self.get_tree_with_subdirs_and_all_content_types()
83
self.addCleanup(self.tree.unlock)
85
def test_symlink_target(self):
86
if isinstance(self.tree, (MutableTree, _PreviewTree)):
88
'symlinks not accurately represented in working trees and'
90
entry = get_entry(self.tree, self.tree.path2id('symlink'))
91
self.assertEqual(entry.symlink_target, 'link-target')
93
def test_symlink_target_tree(self):
94
self.assertEqual('link-target',
95
self.tree.get_symlink_target('symlink'))
97
def test_kind_symlink(self):
98
self.assertEqual('symlink', self.tree.kind('symlink'))
99
self.assertIs(None, self.tree.get_file_size('symlink'))
101
def test_symlink(self):
102
entry = get_entry(self.tree, self.tree.path2id('symlink'))
103
self.assertEqual(entry.kind, 'symlink')
104
self.assertEqual(None, entry.text_size)
107
class TestInventory(per_tree.TestCaseWithTree):
109
def test_paths2ids_recursive(self):
110
work_tree = self.make_branch_and_tree('tree')
111
self.build_tree(['tree/dir/', 'tree/dir/file'])
112
work_tree.add(['dir', 'dir/file'], ['dir-id', 'file-id'])
113
tree = self._convert_tree(work_tree)
115
self.addCleanup(tree.unlock)
116
self.assertEqual(set(['dir-id', 'file-id']), tree.paths2ids(['dir']))
118
def test_paths2ids_forget_old(self):
119
work_tree = self.make_branch_and_tree('tree')
120
self.build_tree(['tree/file'])
121
work_tree.add('file', 'first-id')
122
work_tree.commit('commit old state')
123
work_tree.remove('file')
124
tree = self._convert_tree(work_tree)
126
self.addCleanup(tree.unlock)
127
self.assertEqual(set([]), tree.paths2ids(['file'],
128
require_versioned=False))
130
def _make_canonical_test_tree(self, commit=True):
131
# make a tree used by all the 'canonical' tests below.
132
work_tree = self.make_branch_and_tree('tree')
133
self.build_tree(['tree/dir/', 'tree/dir/file'])
134
work_tree.add(['dir', 'dir/file'])
136
work_tree.commit('commit 1')
137
# XXX: this isn't actually guaranteed to return the class we want to
138
# test -- mbp 2010-02-12
141
def test_canonical_path(self):
142
work_tree = self._make_canonical_test_tree()
143
self.assertEqual('dir/file',
144
work_tree.get_canonical_inventory_path('Dir/File'))
146
def test_canonical_path_before_commit(self):
147
work_tree = self._make_canonical_test_tree(False)
148
# note: not committed.
149
self.assertEqual('dir/file',
150
work_tree.get_canonical_inventory_path('Dir/File'))
152
def test_canonical_path_dir(self):
153
# check it works when asked for just the directory portion.
154
work_tree = self._make_canonical_test_tree()
155
self.assertEqual('dir', work_tree.get_canonical_inventory_path('Dir'))
157
def test_canonical_path_root(self):
158
work_tree = self._make_canonical_test_tree()
159
self.assertEqual('', work_tree.get_canonical_inventory_path(''))
160
self.assertEqual('/', work_tree.get_canonical_inventory_path('/'))
162
def test_canonical_path_invalid_all(self):
163
work_tree = self._make_canonical_test_tree()
164
self.assertEqual('foo/bar',
165
work_tree.get_canonical_inventory_path('foo/bar'))
167
def test_canonical_invalid_child(self):
168
work_tree = self._make_canonical_test_tree()
169
self.assertEqual('dir/None',
170
work_tree.get_canonical_inventory_path('Dir/None'))
172
def test_canonical_tree_name_mismatch(self):
173
# see <https://bugs.edge.launchpad.net/bzr/+bug/368931>
174
# some of the trees we want to use can only exist on a disk, not in
175
# memory - therefore we can only test this if the filesystem is
177
self.requireFeature(tests.case_sensitive_filesystem_feature)
178
work_tree = self.make_branch_and_tree('.')
179
self.build_tree(['test/', 'test/file', 'Test'])
180
work_tree.add(['test/', 'test/file', 'Test'])
182
test_tree = self._convert_tree(work_tree)
183
test_tree.lock_read()
184
self.addCleanup(test_tree.unlock)
186
self.assertEqual(['test', 'test/file', 'Test', 'test/foo', 'Test/foo'],
187
test_tree.get_canonical_inventory_paths(
188
['test', 'test/file', 'Test', 'test/foo', 'Test/foo']))