1
# Copyright (C) 2007, 2008 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Tests for interface conformance of inventories of trees."""
20
from cStringIO import StringIO
23
from bzrlib.diff import internal_diff
24
from bzrlib.mutabletree import MutableTree
25
from bzrlib.osutils import has_symlinks
26
from bzrlib.tests import SymlinkFeature, TestSkipped
27
from bzrlib.tests.tree_implementations import TestCaseWithTree
28
from bzrlib.transform import _PreviewTree
29
from bzrlib.uncommit import uncommit
32
def get_entry(tree, file_id):
33
return tree.iter_entries_by_dir([file_id]).next()[1]
36
class TestPreviousHeads(TestCaseWithTree):
39
# we want several inventories, that respectively
40
# give use the following scenarios:
41
# A) fileid not in any inventory (A),
42
# B) fileid present in one inventory (B) and (A,B)
43
# C) fileid present in two inventories, and they
44
# are not mutual descendents (B, C)
45
# D) fileid present in two inventories and one is
46
# a descendent of the other. (B, D)
47
super(TestPreviousHeads, self).setUp()
48
self.wt = self.make_branch_and_tree('.')
49
self.branch = self.wt.branch
50
self.build_tree(['file'])
51
self.wt.commit('new branch', allow_pointless=True, rev_id='A')
52
self.inv_A = self.branch.repository.get_inventory('A')
53
self.wt.add(['file'], ['fileid'])
54
self.wt.commit('add file', rev_id='B')
55
self.inv_B = self.branch.repository.get_inventory('B')
56
uncommit(self.branch, tree=self.wt)
57
self.assertEqual(self.branch.revision_history(), ['A'])
58
self.wt.commit('another add of file', rev_id='C')
59
self.inv_C = self.branch.repository.get_inventory('C')
60
self.wt.add_parent_tree_id('B')
61
self.wt.commit('merge in B', rev_id='D')
62
self.inv_D = self.branch.repository.get_inventory('D')
63
self.tree = self.workingtree_to_test_tree(self.wt)
65
self.addCleanup(self.tree.unlock)
66
self.file_active = get_entry(self.tree, 'fileid')
68
# TODO: test two inventories with the same file revision
71
class TestInventory(TestCaseWithTree):
74
self.tree = self.get_tree_with_subdirs_and_all_content_types()
76
self.addCleanup(self.tree.unlock)
78
def test_symlink_target(self):
79
self.requireFeature(SymlinkFeature)
81
if isinstance(self.tree, (MutableTree, _PreviewTree)):
83
'symlinks not accurately represented in working trees and'
85
entry = get_entry(self.tree, self.tree.path2id('symlink'))
86
self.assertEqual(entry.symlink_target, 'link-target')
88
def test_symlink_target_tree(self):
89
self.requireFeature(SymlinkFeature)
91
self.assertEqual('link-target',
92
self.tree.get_symlink_target('symlink'))
94
def test_kind_symlink(self):
95
self.requireFeature(SymlinkFeature)
97
self.assertEqual('symlink', self.tree.kind('symlink'))
98
self.assertIs(None, self.tree.get_file_size('symlink'))
100
def test_symlink(self):
101
self.requireFeature(SymlinkFeature)
103
entry = get_entry(self.tree, self.tree.path2id('symlink'))
104
self.assertEqual(entry.kind, 'symlink')
105
self.assertEqual(None, entry.text_size)
107
def test_paths2ids_recursive(self):
108
work_tree = self.make_branch_and_tree('tree')
109
self.build_tree(['tree/dir/', 'tree/dir/file'])
110
work_tree.add(['dir', 'dir/file'], ['dir-id', 'file-id'])
111
tree = self._convert_tree(work_tree)
113
self.addCleanup(tree.unlock)
114
self.assertEqual(set(['dir-id', 'file-id']), tree.paths2ids(['dir']))
116
def test_paths2ids_forget_old(self):
117
work_tree = self.make_branch_and_tree('tree')
118
self.build_tree(['tree/file'])
119
work_tree.add('file', 'first-id')
120
work_tree.commit('commit old state')
121
work_tree.remove('file')
122
tree = self._convert_tree(work_tree)
124
self.addCleanup(tree.unlock)
125
self.assertEqual(set([]), tree.paths2ids(['file'],
126
require_versioned=False))