~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
2079.1.1 by John Arbash Meinel
Create a deprecated bzrlib.tree.RevisionTree() in favor of bzrlib.revisiontree.RevisionTree()
41
from bzrlib.revisiontree import RevisionTree
1852.6.1 by Robert Collins
Start tree implementation tests.
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
        """
1731.1.33 by Aaron Bentley
Revert no-special-root changes
89
        empty_tree.set_root_id('empty-root-id')
1852.8.7 by Robert Collins
Update tree_implementation test tree factories to be intertree ready.
90
        return self._convert_tree(empty_tree, converter)
1852.6.1 by Robert Collins
Start tree implementation tests.
91
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
92
    def _make_abc_tree(self, tree):
93
        """setup an abc content tree."""
94
        files = ['a', 'b/', 'b/c']
1731.1.48 by Aaron Bentley
Merge from bzr.dev
95
        self.build_tree(files, line_endings='binary', 
1982.1.4 by Alexander Belchenko
tree_implementations tests: build_tree with binary (LF) line-endings
96
                        transport=tree.bzrdir.root_transport)
1731.1.33 by Aaron Bentley
Revert no-special-root changes
97
        tree.set_root_id('root-id')
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
98
        tree.add(files, ['a-id', 'b-id', 'c-id'])
99
1852.8.7 by Robert Collins
Update tree_implementation test tree factories to be intertree ready.
100
    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.
101
        """return a test tree with a, b/, b/c contents."""
102
        self._make_abc_tree(tree)
1852.8.7 by Robert Collins
Update tree_implementation test tree factories to be intertree ready.
103
        return self._convert_tree(tree, converter)
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
104
1852.8.7 by Robert Collins
Update tree_implementation test tree factories to be intertree ready.
105
    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.
106
        """return a test tree with a, b/, b/c contents.
107
        
108
        This variation changes the content of 'a' to foobar\n.
109
        """
110
        self._make_abc_tree(tree)
111
        f = open(tree.basedir + '/a', 'wb')
112
        try:
113
            f.write('foobar\n')
114
        finally:
115
            f.close()
1852.8.7 by Robert Collins
Update tree_implementation test tree factories to be intertree ready.
116
        return self._convert_tree(tree, converter)
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
117
1852.8.7 by Robert Collins
Update tree_implementation test tree factories to be intertree ready.
118
    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.
119
        """return a test tree with a, b/, b/c contents.
120
        
121
        This variation changes the executable flag of b/c to True.
122
        """
123
        self._make_abc_tree(tree)
124
        tt = transform.TreeTransform(tree)
125
        trans_id = tt.trans_id_tree_path('b/c')
126
        tt.set_executability(True, trans_id)
127
        tt.apply()
1852.8.7 by Robert Collins
Update tree_implementation test tree factories to be intertree ready.
128
        return self._convert_tree(tree, converter)
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
129
1852.8.7 by Robert Collins
Update tree_implementation test tree factories to be intertree ready.
130
    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.
131
        """return a test tree with d, b/, b/c contents.
132
        
133
        This variation renames a to d.
134
        """
135
        self._make_abc_tree(tree)
136
        tree.rename_one('a', 'd')
1852.8.7 by Robert Collins
Update tree_implementation test tree factories to be intertree ready.
137
        return self._convert_tree(tree, converter)
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
138
1852.8.7 by Robert Collins
Update tree_implementation test tree factories to be intertree ready.
139
    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.
140
        """return a test tree with d, b/, b/c contents.
141
        
142
        This variation renames a to d and alters its content to 'bar\n'.
143
        """
144
        self._make_abc_tree(tree)
145
        tree.rename_one('a', 'd')
146
        f = open(tree.basedir + '/d', 'wb')
147
        try:
148
            f.write('bar\n')
149
        finally:
150
            f.close()
1852.8.7 by Robert Collins
Update tree_implementation test tree factories to be intertree ready.
151
        return self._convert_tree(tree, converter)
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
152
1852.8.7 by Robert Collins
Update tree_implementation test tree factories to be intertree ready.
153
    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.
154
        """return a test tree with a, b/, e contents.
155
        
156
        This variation renames b/c to e, and makes it executable.
157
        """
158
        self._make_abc_tree(tree)
159
        tt = transform.TreeTransform(tree)
160
        trans_id = tt.trans_id_tree_path('b/c')
161
        parent_trans_id = tt.trans_id_tree_path('')
162
        tt.adjust_path('e', parent_trans_id, trans_id)
163
        tt.set_executability(True, trans_id)
164
        tt.apply()
1852.8.7 by Robert Collins
Update tree_implementation test tree factories to be intertree ready.
165
        return self._convert_tree(tree, converter)
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
166
1852.6.1 by Robert Collins
Start tree implementation tests.
167
168
class TreeTestProviderAdapter(WorkingTreeTestProviderAdapter):
169
    """Generate test suites for each Tree implementation in bzrlib.
170
171
    Currently this covers all working tree formats, and RevisionTree by 
172
    committing a working tree to create the revision tree.
173
    """
174
175
    def adapt(self, test):
176
        result = super(TreeTestProviderAdapter, self).adapt(test)
177
        for adapted_test in result:
178
            # for working tree adapted tests, preserve the tree
179
            adapted_test.workingtree_to_test_tree = return_parameter
180
        default_format = WorkingTreeFormat.get_default_format()
181
        revision_tree_test = self._clone_test(
182
            test,
183
            default_format._matchingbzrdir, 
184
            default_format,
185
            RevisionTree.__name__)
186
        revision_tree_test.workingtree_to_test_tree = revision_tree_from_workingtree
187
        result.addTest(revision_tree_test)
188
        return result
189
190
191
def test_suite():
192
    result = TestSuite()
193
    test_tree_implementations = [
194
        'bzrlib.tests.tree_implementations.test_test_trees',
195
        ]
196
    adapter = TreeTestProviderAdapter(
197
        default_transport,
198
        # None here will cause a readonly decorator to be created
199
        # by the TestCaseWithTransport.get_readonly_transport method.
200
        None,
201
        [(format, format._matchingbzrdir) for format in 
202
         WorkingTreeFormat._formats.values() + _legacy_formats])
203
    loader = TestLoader()
204
    adapt_modules(test_tree_implementations, adapter, loader, result)
205
    result.addTests(loader.loadTestsFromModuleNames(['bzrlib.tests.tree_implementations']))
206
    return result