1
# Copyright (C) 2006 by 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
18
"""Tree implementation tests for bzr.
20
These test the conformance of all the tree variations to the expected API.
21
Specific tests for individual variations are in other places such as:
23
- tests/test_revision.py
24
- tests/test_workingtree.py
25
- tests/workingtree_implementations/*.py.
32
from bzrlib.transport import get_transport
33
from bzrlib.tests import (
36
TestCaseWithTransport,
40
from bzrlib.tests.bzrdir_implementations.test_bzrdir import TestCaseWithBzrDir
41
from bzrlib.tree import RevisionTree
42
from bzrlib.workingtree import (WorkingTreeFormat,
43
WorkingTreeTestProviderAdapter,
48
def return_parameter(something):
49
"""A trivial thunk to return its input."""
53
def revision_tree_from_workingtree(tree):
54
"""Create a revision tree from a working tree."""
55
revid = tree.commit('save tree', allow_pointless=True)
56
return tree.branch.repository.revision_tree(revid)
59
class TestTreeImplementationSupport(TestCaseWithTransport):
61
def test_revision_tree_from_workingtree(self):
62
tree = self.make_branch_and_tree('.')
63
tree = revision_tree_from_workingtree(tree)
64
self.assertIsInstance(tree, RevisionTree)
67
class TestCaseWithTree(TestCaseWithBzrDir):
69
def make_branch_and_tree(self, relpath, format=None):
70
made_control = self.make_bzrdir(relpath, format=format)
71
made_control.create_repository()
72
made_control.create_branch()
73
return self.workingtree_format.initialize(made_control)
75
def get_tree_no_parents_no_content(self):
76
# make a working tree with the right shape
77
tree = self.make_branch_and_tree('.')
78
# convert that to the final shape
79
return self.workingtree_to_test_tree(tree)
81
def _make_abc_tree(self, tree):
82
"""setup an abc content tree."""
83
files = ['a', 'b/', 'b/c']
84
self.build_tree(files, transport=tree.bzrdir.root_transport)
85
tree.add(files, ['a-id', 'b-id', 'c-id'])
87
def get_tree_no_parents_abc_content(self):
88
"""return a test tree with a, b/, b/c contents."""
89
tree = self.make_branch_and_tree('.')
90
self._make_abc_tree(tree)
91
return self.workingtree_to_test_tree(tree)
93
def get_tree_no_parents_abc_content_2(self):
94
"""return a test tree with a, b/, b/c contents.
96
This variation changes the content of 'a' to foobar\n.
98
tree = self.make_branch_and_tree('.')
99
self._make_abc_tree(tree)
100
f = open(tree.basedir + '/a', 'wb')
105
return self.workingtree_to_test_tree(tree)
107
def get_tree_no_parents_abc_content_3(self):
108
"""return a test tree with a, b/, b/c contents.
110
This variation changes the executable flag of b/c to True.
112
tree = self.make_branch_and_tree('.')
113
self._make_abc_tree(tree)
114
tt = transform.TreeTransform(tree)
115
trans_id = tt.trans_id_tree_path('b/c')
116
tt.set_executability(True, trans_id)
118
return self.workingtree_to_test_tree(tree)
120
def get_tree_no_parents_abc_content_4(self):
121
"""return a test tree with d, b/, b/c contents.
123
This variation renames a to d.
125
tree = self.make_branch_and_tree('.')
126
self._make_abc_tree(tree)
127
tree.rename_one('a', 'd')
128
return self.workingtree_to_test_tree(tree)
130
def get_tree_no_parents_abc_content_5(self):
131
"""return a test tree with d, b/, b/c contents.
133
This variation renames a to d and alters its content to 'bar\n'.
135
tree = self.make_branch_and_tree('.')
136
self._make_abc_tree(tree)
137
tree.rename_one('a', 'd')
138
f = open(tree.basedir + '/d', 'wb')
143
return self.workingtree_to_test_tree(tree)
145
def get_tree_no_parents_abc_content_6(self):
146
"""return a test tree with a, b/, e contents.
148
This variation renames b/c to e, and makes it executable.
150
tree = self.make_branch_and_tree('.')
151
self._make_abc_tree(tree)
152
tt = transform.TreeTransform(tree)
153
trans_id = tt.trans_id_tree_path('b/c')
154
parent_trans_id = tt.trans_id_tree_path('')
155
tt.adjust_path('e', parent_trans_id, trans_id)
156
tt.set_executability(True, trans_id)
158
return self.workingtree_to_test_tree(tree)
161
class TreeTestProviderAdapter(WorkingTreeTestProviderAdapter):
162
"""Generate test suites for each Tree implementation in bzrlib.
164
Currently this covers all working tree formats, and RevisionTree by
165
committing a working tree to create the revision tree.
168
def adapt(self, test):
169
result = super(TreeTestProviderAdapter, self).adapt(test)
170
for adapted_test in result:
171
# for working tree adapted tests, preserve the tree
172
adapted_test.workingtree_to_test_tree = return_parameter
173
default_format = WorkingTreeFormat.get_default_format()
174
revision_tree_test = self._clone_test(
176
default_format._matchingbzrdir,
178
RevisionTree.__name__)
179
revision_tree_test.workingtree_to_test_tree = revision_tree_from_workingtree
180
result.addTest(revision_tree_test)
186
test_tree_implementations = [
187
'bzrlib.tests.tree_implementations.test_test_trees',
189
adapter = TreeTestProviderAdapter(
191
# None here will cause a readonly decorator to be created
192
# by the TestCaseWithTransport.get_readonly_transport method.
194
[(format, format._matchingbzrdir) for format in
195
WorkingTreeFormat._formats.values() + _legacy_formats])
196
loader = TestLoader()
197
adapt_modules(test_tree_implementations, adapter, loader, result)
198
result.addTests(loader.loadTestsFromModuleNames(['bzrlib.tests.tree_implementations']))