~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/tree_implementations/__init__.py

(robertc) Introduce Tree interface implementation tests. This corrects behaviour differences in Tree implementations for the __iter__ interface which were revealed by the new tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006 by Canonical Ltd
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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
 
 
28
from bzrlib import (
 
29
    errors,
 
30
    transform,
 
31
    )
 
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
 
 
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)
 
74
 
 
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)
 
80
 
 
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'])
 
86
 
 
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)
 
92
 
 
93
    def get_tree_no_parents_abc_content_2(self):
 
94
        """return a test tree with a, b/, b/c contents.
 
95
        
 
96
        This variation changes the content of 'a' to foobar\n.
 
97
        """
 
98
        tree = self.make_branch_and_tree('.')
 
99
        self._make_abc_tree(tree)
 
100
        f = open(tree.basedir + '/a', 'wb')
 
101
        try:
 
102
            f.write('foobar\n')
 
103
        finally:
 
104
            f.close()
 
105
        return self.workingtree_to_test_tree(tree)
 
106
 
 
107
    def get_tree_no_parents_abc_content_3(self):
 
108
        """return a test tree with a, b/, b/c contents.
 
109
        
 
110
        This variation changes the executable flag of b/c to True.
 
111
        """
 
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)
 
117
        tt.apply()
 
118
        return self.workingtree_to_test_tree(tree)
 
119
 
 
120
    def get_tree_no_parents_abc_content_4(self):
 
121
        """return a test tree with d, b/, b/c contents.
 
122
        
 
123
        This variation renames a to d.
 
124
        """
 
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)
 
129
 
 
130
    def get_tree_no_parents_abc_content_5(self):
 
131
        """return a test tree with d, b/, b/c contents.
 
132
        
 
133
        This variation renames a to d and alters its content to 'bar\n'.
 
134
        """
 
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')
 
139
        try:
 
140
            f.write('bar\n')
 
141
        finally:
 
142
            f.close()
 
143
        return self.workingtree_to_test_tree(tree)
 
144
 
 
145
    def get_tree_no_parents_abc_content_6(self):
 
146
        """return a test tree with a, b/, e contents.
 
147
        
 
148
        This variation renames b/c to e, and makes it executable.
 
149
        """
 
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)
 
157
        tt.apply()
 
158
        return self.workingtree_to_test_tree(tree)
 
159
 
 
160
 
 
161
class TreeTestProviderAdapter(WorkingTreeTestProviderAdapter):
 
162
    """Generate test suites for each Tree implementation in bzrlib.
 
163
 
 
164
    Currently this covers all working tree formats, and RevisionTree by 
 
165
    committing a working tree to create the revision tree.
 
166
    """
 
167
 
 
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(
 
175
            test,
 
176
            default_format._matchingbzrdir, 
 
177
            default_format,
 
178
            RevisionTree.__name__)
 
179
        revision_tree_test.workingtree_to_test_tree = revision_tree_from_workingtree
 
180
        result.addTest(revision_tree_test)
 
181
        return result
 
182
 
 
183
 
 
184
def test_suite():
 
185
    result = TestSuite()
 
186
    test_tree_implementations = [
 
187
        'bzrlib.tests.tree_implementations.test_test_trees',
 
188
        ]
 
189
    adapter = TreeTestProviderAdapter(
 
190
        default_transport,
 
191
        # None here will cause a readonly decorator to be created
 
192
        # by the TestCaseWithTransport.get_readonly_transport method.
 
193
        None,
 
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']))
 
199
    return result