/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

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

First attempt to merge .dev and resolve the conflicts (but tests are 
failing)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006, 2007 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
from bzrlib import (
 
18
    errors,
 
19
    tests,
 
20
    conflicts,
 
21
    )
 
22
from bzrlib.tests import TestSkipped
 
23
from bzrlib.tests.tree_implementations import TestCaseWithTree
 
24
 
 
25
class TestAnnotate(TestCaseWithTree):
 
26
 
 
27
    def test_annotate(self):
 
28
        work_tree = self.make_branch_and_tree('wt')
 
29
        tree = self.get_tree_no_parents_abc_content(work_tree)
 
30
        tree_revision = getattr(tree, 'get_revision_id', lambda: 'current:')()
 
31
        tree.lock_read()
 
32
        self.addCleanup(tree.unlock)
 
33
        for revision, line in tree.annotate_iter('a-id'):
 
34
            self.assertEqual('contents of a\n', line)
 
35
            self.assertEqual(tree_revision, revision)
 
36
        tree_revision = getattr(tree, 'get_revision_id', lambda: 'random:')()
 
37
        for revision, line in tree.annotate_iter('a-id', 'random:'):
 
38
            self.assertEqual('contents of a\n', line)
 
39
            self.assertEqual(tree_revision, revision)
 
40
 
 
41
 
 
42
class TestPlanFileMerge(TestCaseWithTree):
 
43
 
 
44
    def test_plan_file_merge(self):
 
45
        work_a = self.make_branch_and_tree('wta')
 
46
        self.build_tree_contents([('wta/file', 'a\nb\nc\nd\n')])
 
47
        work_a.add('file', 'file-id')
 
48
        work_a.commit('base version')
 
49
        work_b = work_a.bzrdir.sprout('wtb').open_workingtree()
 
50
        self.build_tree_contents([('wta/file', 'b\nc\nd\ne\n')])
 
51
        tree_a = self.workingtree_to_test_tree(work_a)
 
52
        tree_a.lock_read()
 
53
        self.addCleanup(tree_a.unlock)
 
54
        self.build_tree_contents([('wtb/file', 'a\nc\nd\nf\n')])
 
55
        tree_b = self.workingtree_to_test_tree(work_b)
 
56
        tree_b.lock_read()
 
57
        self.addCleanup(tree_b.unlock)
 
58
        self.assertEqual([
 
59
            ('killed-a', 'a\n'),
 
60
            ('killed-b', 'b\n'),
 
61
            ('unchanged', 'c\n'),
 
62
            ('unchanged', 'd\n'),
 
63
            ('new-a', 'e\n'),
 
64
            ('new-b', 'f\n'),
 
65
        ], list(tree_a.plan_file_merge('file-id', tree_b)))
 
66
 
 
67
 
 
68
class TestReference(TestCaseWithTree):
 
69
 
 
70
    def skip_if_no_reference(self, tree):
 
71
        if not getattr(tree, 'supports_tree_reference', lambda: False)():
 
72
            raise tests.TestNotApplicable('Tree references not supported')
 
73
 
 
74
    def create_nested(self):
 
75
        work_tree = self.make_branch_and_tree('wt')
 
76
        work_tree.lock_write()
 
77
        try:
 
78
            self.skip_if_no_reference(work_tree)
 
79
            subtree = self.make_branch_and_tree('wt/subtree')
 
80
            subtree.set_root_id('sub-root')
 
81
            subtree.commit('foo', rev_id='sub-1')
 
82
            work_tree.add_reference(subtree)
 
83
        finally:
 
84
            work_tree.unlock()
 
85
        tree = self._convert_tree(work_tree)
 
86
        self.skip_if_no_reference(tree)
 
87
        return tree
 
88
 
 
89
    def test_get_reference_revision(self):
 
90
        tree = self.create_nested()
 
91
        tree.lock_read()
 
92
        self.addCleanup(tree.unlock)
 
93
        path = tree.id2path('sub-root')
 
94
        self.assertEqual('sub-1', tree.get_reference_revision('sub-root', path))
 
95
 
 
96
    def test_iter_references(self):
 
97
        tree = self.create_nested()
 
98
        tree.lock_read()
 
99
        self.addCleanup(tree.unlock)
 
100
        entry = tree.inventory['sub-root']
 
101
        self.assertEqual([(u'subtree', 'sub-root')],
 
102
            list(tree.iter_references()))
 
103
 
 
104
    def test_get_root_id(self):
 
105
        # trees should return some kind of root id; it can be none
 
106
        tree = self.make_branch_and_tree('tree')
 
107
        root_id = tree.get_root_id()
 
108
        if root_id is not None:
 
109
            self.assertIsInstance(root_id, str)
 
110
 
 
111
 
 
112
class TestFileIds(TestCaseWithTree):
 
113
 
 
114
    def test_id2path(self):
 
115
        # translate from file-id back to path
 
116
        work_tree = self.make_branch_and_tree('wt')
 
117
        tree = self.get_tree_no_parents_abc_content(work_tree)
 
118
        tree.lock_read()
 
119
        try:
 
120
            self.assertEqual(u'a', tree.id2path('a-id'))
 
121
            # other ids give an error- don't return None for this case
 
122
            self.assertRaises(errors.NoSuchId, tree.id2path, 'a')
 
123
        finally:
 
124
            tree.unlock()
 
125
 
 
126
    def test_all_file_ids(self):
 
127
        work_tree = self.make_branch_and_tree('wt')
 
128
        tree = self.get_tree_no_parents_abc_content(work_tree)
 
129
        tree.lock_read()
 
130
        self.addCleanup(tree.unlock)
 
131
        self.assertEqual(tree.all_file_ids(),
 
132
                         set(['b-id', 'root-id', 'c-id', 'a-id']))
 
133
 
 
134
 
 
135
class TestStoredKind(TestCaseWithTree):
 
136
 
 
137
    def test_stored_kind(self):
 
138
        tree = self.make_branch_and_tree('tree')
 
139
        work_tree = self.make_branch_and_tree('wt')
 
140
        tree = self.get_tree_no_parents_abc_content(work_tree)
 
141
        tree.lock_read()
 
142
        self.addCleanup(tree.unlock)
 
143
        self.assertEqual('file', tree.stored_kind('a-id'))
 
144
        self.assertEqual('directory', tree.stored_kind('b-id'))
 
145
 
 
146
 
 
147
class TestFileContent(TestCaseWithTree):
 
148
 
 
149
    def test_get_file(self):
 
150
        work_tree = self.make_branch_and_tree('wt')
 
151
        tree = self.get_tree_no_parents_abc_content_2(work_tree)
 
152
        tree.lock_read()
 
153
        try:
 
154
            # Test lookup without path works
 
155
            lines = tree.get_file('a-id').readlines()
 
156
            self.assertEqual(['foobar\n'], lines)
 
157
            # Test lookup with path works
 
158
            lines = tree.get_file('a-id', path='a').readlines()
 
159
            self.assertEqual(['foobar\n'], lines)
 
160
        finally:
 
161
            tree.unlock()
 
162
 
 
163
 
 
164
class TestExtractFilesBytes(TestCaseWithTree):
 
165
 
 
166
    def test_iter_files_bytes(self):
 
167
        work_tree = self.make_branch_and_tree('wt')
 
168
        self.build_tree_contents([('wt/foo', 'foo'),
 
169
                                  ('wt/bar', 'bar'),
 
170
                                  ('wt/baz', 'baz')])
 
171
        work_tree.add(['foo', 'bar', 'baz'], ['foo-id', 'bar-id', 'baz-id'])
 
172
        tree = self._convert_tree(work_tree)
 
173
        tree.lock_read()
 
174
        self.addCleanup(tree.unlock)
 
175
        extracted = dict((i, ''.join(b)) for i, b in
 
176
                         tree.iter_files_bytes([('foo-id', 'id1'),
 
177
                                                ('bar-id', 'id2'),
 
178
                                                ('baz-id', 'id3')]))
 
179
        self.assertEqual('foo', extracted['id1'])
 
180
        self.assertEqual('bar', extracted['id2'])
 
181
        self.assertEqual('baz', extracted['id3'])
 
182
        self.assertRaises(errors.NoSuchId, lambda: list(
 
183
                          tree.iter_files_bytes(
 
184
                          [('qux-id', 'file1-notpresent')])))
 
185
 
 
186
 
 
187
class TestConflicts(TestCaseWithTree):
 
188
 
 
189
    def test_conflicts(self):
 
190
        """Tree.conflicts() should return a ConflictList instance."""
 
191
        work_tree = self.make_branch_and_tree('wt')
 
192
        tree = self._convert_tree(work_tree)
 
193
        self.assertIsInstance(tree.conflicts(), conflicts.ConflictList)
 
194
 
 
195
 
 
196
class TestIterEntriesByDir(TestCaseWithTree):
 
197
 
 
198
    def test_iteration_order(self):
 
199
        work_tree = self.make_branch_and_tree('.')
 
200
        self.build_tree(['a/', 'a/b/', 'a/b/c', 'a/d/', 'a/d/e', 'f/', 'f/g'])
 
201
        work_tree.add(['a', 'a/b', 'a/b/c', 'a/d', 'a/d/e', 'f', 'f/g'])
 
202
        tree = self._convert_tree(work_tree)
 
203
        output_order = [p for p, e in tree.iter_entries_by_dir()]
 
204
        self.assertEqual(['', 'a', 'f', 'a/b', 'a/d', 'a/b/c', 'a/d/e', 'f/g'],
 
205
                         output_order)