~bzr-pqm/bzr/bzr.dev

1852.6.1 by Robert Collins
Start tree implementation tests.
1
# Copyright (C) 2006 by Canonical Ltd
1852.6.7 by Robert Collins
Fix up new tree_implementations __init__.py header.
2
#
1852.6.1 by Robert Collins
Start tree implementation tests.
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.
1852.6.7 by Robert Collins
Fix up new tree_implementations __init__.py header.
7
#
1852.6.1 by Robert Collins
Start tree implementation tests.
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.
1852.6.7 by Robert Collins
Fix up new tree_implementations __init__.py header.
12
#
1852.6.1 by Robert Collins
Start tree implementation tests.
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
16
17
18
"""Tree implementation tests for bzr.
19
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:
22
 - tests/test_tree.py
23
 - tests/test_revision.py
24
 - tests/test_workingtree.py
25
 - tests/workingtree_implementations/*.py.
26
"""
27
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
28
from bzrlib import (
29
    errors,
30
    transform,
31
    )
1852.6.1 by Robert Collins
Start tree implementation tests.
32
from bzrlib.transport import get_transport
33
from bzrlib.tests import (
34
                          adapt_modules,
35
                          default_transport,
36
                          TestCaseWithTransport,
37
                          TestLoader,
38
                          TestSuite,
39
                          )
40
from bzrlib.tests.bzrdir_implementations.test_bzrdir import TestCaseWithBzrDir
41
from bzrlib.tree import RevisionTree
42
from bzrlib.workingtree import (WorkingTreeFormat,
43
                                WorkingTreeTestProviderAdapter,
44
                                _legacy_formats,
45
                                )
46
47
48
def return_parameter(something):
49
    """A trivial thunk to return its input."""
50
    return something
51
52
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)
57
58
59
class TestTreeImplementationSupport(TestCaseWithTransport):
60
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)
65
66
67
class TestCaseWithTree(TestCaseWithBzrDir):
68
1852.8.3 by Robert Collins
Implement an InterTreeTestProvider and a trivial test_compare test case.
69
    def make_branch_and_tree(self, relpath):
70
        made_control = self.make_bzrdir(relpath, format=
71
            self.workingtree_format._matchingbzrdir)
1852.6.1 by Robert Collins
Start tree implementation tests.
72
        made_control.create_repository()
73
        made_control.create_branch()
74
        return self.workingtree_format.initialize(made_control)
75
1852.8.7 by Robert Collins
Update tree_implementation test tree factories to be intertree ready.
76
    def _convert_tree(self, tree, converter=None):
77
        """helper to convert using the converter or a supplied one."""
78
        # convert that to the final shape
79
        if converter is None:
80
            converter = self.workingtree_to_test_tree
81
        return converter(tree)
82
1852.8.3 by Robert Collins
Implement an InterTreeTestProvider and a trivial test_compare test case.
83
    def get_tree_no_parents_no_content(self, empty_tree, converter=None):
84
        """Make a tree with no parents and no contents from empty_tree.
85
        
86
        :param empty_tree: A working tree with no content and no parents to
87
            modify.
88
        """
1852.8.7 by Robert Collins
Update tree_implementation test tree factories to be intertree ready.
89
        return self._convert_tree(empty_tree, converter)
1852.6.1 by Robert Collins
Start tree implementation tests.
90
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
91
    def _make_abc_tree(self, tree):
92
        """setup an abc content tree."""
93
        files = ['a', 'b/', 'b/c']
1982.1.4 by Alexander Belchenko
tree_implementations tests: build_tree with binary (LF) line-endings
94
        self.build_tree(files, line_endings='binary',
95
                        transport=tree.bzrdir.root_transport)
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
96
        tree.add(files, ['a-id', 'b-id', 'c-id'])
97
1852.8.7 by Robert Collins
Update tree_implementation test tree factories to be intertree ready.
98
    def get_tree_no_parents_abc_content(self, tree, converter=None):
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
99
        """return a test tree with a, b/, b/c contents."""
100
        self._make_abc_tree(tree)
1852.8.7 by Robert Collins
Update tree_implementation test tree factories to be intertree ready.
101
        return self._convert_tree(tree, converter)
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
102
1852.8.7 by Robert Collins
Update tree_implementation test tree factories to be intertree ready.
103
    def get_tree_no_parents_abc_content_2(self, tree, converter=None):
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
104
        """return a test tree with a, b/, b/c contents.
105
        
106
        This variation changes the content of 'a' to foobar\n.
107
        """
108
        self._make_abc_tree(tree)
109
        f = open(tree.basedir + '/a', 'wb')
110
        try:
111
            f.write('foobar\n')
112
        finally:
113
            f.close()
1852.8.7 by Robert Collins
Update tree_implementation test tree factories to be intertree ready.
114
        return self._convert_tree(tree, converter)
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
115
1852.8.7 by Robert Collins
Update tree_implementation test tree factories to be intertree ready.
116
    def get_tree_no_parents_abc_content_3(self, tree, converter=None):
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
117
        """return a test tree with a, b/, b/c contents.
118
        
119
        This variation changes the executable flag of b/c to True.
120
        """
121
        self._make_abc_tree(tree)
122
        tt = transform.TreeTransform(tree)
123
        trans_id = tt.trans_id_tree_path('b/c')
124
        tt.set_executability(True, trans_id)
125
        tt.apply()
1852.8.7 by Robert Collins
Update tree_implementation test tree factories to be intertree ready.
126
        return self._convert_tree(tree, converter)
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
127
1852.8.7 by Robert Collins
Update tree_implementation test tree factories to be intertree ready.
128
    def get_tree_no_parents_abc_content_4(self, tree, converter=None):
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
129
        """return a test tree with d, b/, b/c contents.
130
        
131
        This variation renames a to d.
132
        """
133
        self._make_abc_tree(tree)
134
        tree.rename_one('a', 'd')
1852.8.7 by Robert Collins
Update tree_implementation test tree factories to be intertree ready.
135
        return self._convert_tree(tree, converter)
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
136
1852.8.7 by Robert Collins
Update tree_implementation test tree factories to be intertree ready.
137
    def get_tree_no_parents_abc_content_5(self, tree, converter=None):
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
138
        """return a test tree with d, b/, b/c contents.
139
        
140
        This variation renames a to d and alters its content to 'bar\n'.
141
        """
142
        self._make_abc_tree(tree)
143
        tree.rename_one('a', 'd')
144
        f = open(tree.basedir + '/d', 'wb')
145
        try:
146
            f.write('bar\n')
147
        finally:
148
            f.close()
1852.8.7 by Robert Collins
Update tree_implementation test tree factories to be intertree ready.
149
        return self._convert_tree(tree, converter)
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
150
1852.8.7 by Robert Collins
Update tree_implementation test tree factories to be intertree ready.
151
    def get_tree_no_parents_abc_content_6(self, tree, converter=None):
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
152
        """return a test tree with a, b/, e contents.
153
        
154
        This variation renames b/c to e, and makes it executable.
155
        """
156
        self._make_abc_tree(tree)
157
        tt = transform.TreeTransform(tree)
158
        trans_id = tt.trans_id_tree_path('b/c')
159
        parent_trans_id = tt.trans_id_tree_path('')
160
        tt.adjust_path('e', parent_trans_id, trans_id)
161
        tt.set_executability(True, trans_id)
162
        tt.apply()
1852.8.7 by Robert Collins
Update tree_implementation test tree factories to be intertree ready.
163
        return self._convert_tree(tree, converter)
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
164
1852.6.1 by Robert Collins
Start tree implementation tests.
165
166
class TreeTestProviderAdapter(WorkingTreeTestProviderAdapter):
167
    """Generate test suites for each Tree implementation in bzrlib.
168
169
    Currently this covers all working tree formats, and RevisionTree by 
170
    committing a working tree to create the revision tree.
171
    """
172
173
    def adapt(self, test):
174
        result = super(TreeTestProviderAdapter, self).adapt(test)
175
        for adapted_test in result:
176
            # for working tree adapted tests, preserve the tree
177
            adapted_test.workingtree_to_test_tree = return_parameter
178
        default_format = WorkingTreeFormat.get_default_format()
179
        revision_tree_test = self._clone_test(
180
            test,
181
            default_format._matchingbzrdir, 
182
            default_format,
183
            RevisionTree.__name__)
184
        revision_tree_test.workingtree_to_test_tree = revision_tree_from_workingtree
185
        result.addTest(revision_tree_test)
186
        return result
187
188
189
def test_suite():
190
    result = TestSuite()
191
    test_tree_implementations = [
192
        'bzrlib.tests.tree_implementations.test_test_trees',
193
        ]
194
    adapter = TreeTestProviderAdapter(
195
        default_transport,
196
        # None here will cause a readonly decorator to be created
197
        # by the TestCaseWithTransport.get_readonly_transport method.
198
        None,
199
        [(format, format._matchingbzrdir) for format in 
200
         WorkingTreeFormat._formats.values() + _legacy_formats])
201
    loader = TestLoader()
202
    adapt_modules(test_tree_implementations, adapter, loader, result)
203
    result.addTests(loader.loadTestsFromModuleNames(['bzrlib.tests.tree_implementations']))
204
    return result