/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2338.4.6 by Marien Zwart
Move some tests that do not need a working tree from workingtree_implementations to tree_implementations.
1
# Copyright (C) 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
"""Tests for interface conformance of inventories of trees."""
18
19
20
from cStringIO import StringIO
21
import os
22
23
from bzrlib.diff import internal_diff
2338.4.9 by Marien Zwart
More tests for symlinks in tree inventories.
24
from bzrlib.mutabletree import MutableTree
2949.5.1 by Alexander Belchenko
selftest: use SymlinkFeature instead of TestSkipped where appropriate
25
from bzrlib.osutils import has_symlinks
3123.1.1 by John Arbash Meinel
Update from deprecating 0.93 to 1.0, and add a 1.1 deprecation.
26
from bzrlib.symbol_versioning import zero_ninetyone, one_zero
2949.5.1 by Alexander Belchenko
selftest: use SymlinkFeature instead of TestSkipped where appropriate
27
from bzrlib.tests import SymlinkFeature, TestSkipped
2338.4.6 by Marien Zwart
Move some tests that do not need a working tree from workingtree_implementations to tree_implementations.
28
from bzrlib.tests.tree_implementations import TestCaseWithTree
29
from bzrlib.uncommit import uncommit
30
31
32
class TestEntryDiffing(TestCaseWithTree):
33
34
    def setUp(self):
35
        super(TestEntryDiffing, self).setUp()
36
        self.wt = self.make_branch_and_tree('.')
37
        self.branch = self.wt.branch
2911.6.1 by Blake Winton
Change 'print >> f,'s to 'f.write('s.
38
        open('file', 'wb').write('foo\n')
39
        open('binfile', 'wb').write('foo\n')
2338.4.6 by Marien Zwart
Move some tests that do not need a working tree from workingtree_implementations to tree_implementations.
40
        self.wt.add(['file'], ['fileid'])
41
        self.wt.add(['binfile'], ['binfileid'])
42
        if has_symlinks():
43
            os.symlink('target1', 'symlink')
44
            self.wt.add(['symlink'], ['linkid'])
45
        self.wt.commit('message_1', rev_id = '1')
2911.6.1 by Blake Winton
Change 'print >> f,'s to 'f.write('s.
46
        open('file', 'wb').write('bar\n')
47
        open('binfile', 'wb').write('x' * 1023 + '\x00\n')
2338.4.6 by Marien Zwart
Move some tests that do not need a working tree from workingtree_implementations to tree_implementations.
48
        if has_symlinks():
49
            os.unlink('symlink')
50
            os.symlink('target2', 'symlink')
51
        self.tree_1 = self.branch.repository.revision_tree('1')
52
        self.inv_1 = self.branch.repository.get_inventory('1')
53
        self.file_1 = self.inv_1['fileid']
54
        self.file_1b = self.inv_1['binfileid']
55
        self.tree_2 = self.workingtree_to_test_tree(self.wt)
56
        self.tree_2.lock_read()
57
        self.addCleanup(self.tree_2.unlock)
58
        self.inv_2 = self.tree_2.inventory
59
        self.file_2 = self.inv_2['fileid']
60
        self.file_2b = self.inv_2['binfileid']
61
        if has_symlinks():
62
            self.link_1 = self.inv_1['linkid']
63
            self.link_2 = self.inv_2['linkid']
64
65
    def test_file_diff_deleted(self):
66
        output = StringIO()
3123.1.1 by John Arbash Meinel
Update from deprecating 0.93 to 1.0, and add a 1.1 deprecation.
67
        self.applyDeprecated(one_zero,
3009.2.32 by Aaron Bentley
InventoryEntry.diff is now deprecated
68
                             self.file_1.diff,
69
                             internal_diff,
70
                             "old_label", self.tree_1,
71
                             "/dev/null", None, None,
72
                             output)
2338.4.6 by Marien Zwart
Move some tests that do not need a working tree from workingtree_implementations to tree_implementations.
73
        self.assertEqual(output.getvalue(), "--- old_label\n"
74
                                            "+++ /dev/null\n"
75
                                            "@@ -1,1 +0,0 @@\n"
76
                                            "-foo\n"
77
                                            "\n")
78
79
    def test_file_diff_added(self):
80
        output = StringIO()
3123.1.1 by John Arbash Meinel
Update from deprecating 0.93 to 1.0, and add a 1.1 deprecation.
81
        self.applyDeprecated(one_zero,
3009.2.32 by Aaron Bentley
InventoryEntry.diff is now deprecated
82
                             self.file_1.diff,
83
                             internal_diff,
84
                             "new_label", self.tree_1,
85
                             "/dev/null", None, None,
86
                             output, reverse=True)
2338.4.6 by Marien Zwart
Move some tests that do not need a working tree from workingtree_implementations to tree_implementations.
87
        self.assertEqual(output.getvalue(), "--- /dev/null\n"
88
                                            "+++ new_label\n"
89
                                            "@@ -0,0 +1,1 @@\n"
90
                                            "+foo\n"
91
                                            "\n")
92
93
    def test_file_diff_changed(self):
94
        output = StringIO()
3123.1.1 by John Arbash Meinel
Update from deprecating 0.93 to 1.0, and add a 1.1 deprecation.
95
        self.applyDeprecated(one_zero,
3009.2.32 by Aaron Bentley
InventoryEntry.diff is now deprecated
96
                             self.file_1.diff,
97
                             internal_diff,
98
                             "/dev/null", self.tree_1,
99
                             "new_label", self.file_2, self.tree_2,
100
                             output)
2338.4.6 by Marien Zwart
Move some tests that do not need a working tree from workingtree_implementations to tree_implementations.
101
        self.assertEqual(output.getvalue(), "--- /dev/null\n"
102
                                            "+++ new_label\n"
103
                                            "@@ -1,1 +1,1 @@\n"
104
                                            "-foo\n"
105
                                            "+bar\n"
106
                                            "\n")
107
        
108
    def test_file_diff_binary(self):
109
        output = StringIO()
3123.1.1 by John Arbash Meinel
Update from deprecating 0.93 to 1.0, and add a 1.1 deprecation.
110
        self.applyDeprecated(one_zero,
3009.2.32 by Aaron Bentley
InventoryEntry.diff is now deprecated
111
                             self.file_1.diff,
112
                             internal_diff,
113
                             "/dev/null", self.tree_1,
114
                             "new_label", self.file_2b, self.tree_2,
115
                             output)
2338.4.6 by Marien Zwart
Move some tests that do not need a working tree from workingtree_implementations to tree_implementations.
116
        self.assertEqual(output.getvalue(), 
117
                         "Binary files /dev/null and new_label differ\n")
2949.5.1 by Alexander Belchenko
selftest: use SymlinkFeature instead of TestSkipped where appropriate
118
2338.4.6 by Marien Zwart
Move some tests that do not need a working tree from workingtree_implementations to tree_implementations.
119
    def test_link_diff_deleted(self):
2949.5.1 by Alexander Belchenko
selftest: use SymlinkFeature instead of TestSkipped where appropriate
120
        self.requireFeature(SymlinkFeature)
2338.4.6 by Marien Zwart
Move some tests that do not need a working tree from workingtree_implementations to tree_implementations.
121
        output = StringIO()
3123.1.1 by John Arbash Meinel
Update from deprecating 0.93 to 1.0, and add a 1.1 deprecation.
122
        self.applyDeprecated(one_zero,
3009.2.32 by Aaron Bentley
InventoryEntry.diff is now deprecated
123
                             self.link_1.diff,
124
                             internal_diff, "old_label",
125
                             self.tree_1, "/dev/null", None, None,
126
                             output)
2338.4.6 by Marien Zwart
Move some tests that do not need a working tree from workingtree_implementations to tree_implementations.
127
        self.assertEqual(output.getvalue(),
128
                         "=== target was 'target1'\n")
129
130
    def test_link_diff_added(self):
2949.5.1 by Alexander Belchenko
selftest: use SymlinkFeature instead of TestSkipped where appropriate
131
        self.requireFeature(SymlinkFeature)
2338.4.6 by Marien Zwart
Move some tests that do not need a working tree from workingtree_implementations to tree_implementations.
132
        output = StringIO()
3123.1.1 by John Arbash Meinel
Update from deprecating 0.93 to 1.0, and add a 1.1 deprecation.
133
        self.applyDeprecated(one_zero,
3009.2.32 by Aaron Bentley
InventoryEntry.diff is now deprecated
134
                             self.link_1.diff,
135
                             internal_diff,
136
                             "new_label", self.tree_1,
137
                             "/dev/null", None, None,
138
                             output, reverse=True)
2338.4.6 by Marien Zwart
Move some tests that do not need a working tree from workingtree_implementations to tree_implementations.
139
        self.assertEqual(output.getvalue(),
140
                         "=== target is 'target1'\n")
141
142
    def test_link_diff_changed(self):
2949.5.1 by Alexander Belchenko
selftest: use SymlinkFeature instead of TestSkipped where appropriate
143
        self.requireFeature(SymlinkFeature)
2338.4.6 by Marien Zwart
Move some tests that do not need a working tree from workingtree_implementations to tree_implementations.
144
        output = StringIO()
3123.1.1 by John Arbash Meinel
Update from deprecating 0.93 to 1.0, and add a 1.1 deprecation.
145
        self.applyDeprecated(one_zero,
3009.2.32 by Aaron Bentley
InventoryEntry.diff is now deprecated
146
                             self.link_1.diff,
147
                             internal_diff,
148
                             "/dev/null", self.tree_1,
149
                             "new_label", self.link_2, self.tree_2,
150
                             output)
2338.4.6 by Marien Zwart
Move some tests that do not need a working tree from workingtree_implementations to tree_implementations.
151
        self.assertEqual(output.getvalue(),
152
                         "=== target changed 'target1' => 'target2'\n")
153
154
155
class TestPreviousHeads(TestCaseWithTree):
156
157
    def setUp(self):
158
        # we want several inventories, that respectively
159
        # give use the following scenarios:
160
        # A) fileid not in any inventory (A),
161
        # B) fileid present in one inventory (B) and (A,B)
162
        # C) fileid present in two inventories, and they
163
        #   are not mutual descendents (B, C)
164
        # D) fileid present in two inventories and one is
165
        #   a descendent of the other. (B, D)
166
        super(TestPreviousHeads, self).setUp()
167
        self.wt = self.make_branch_and_tree('.')
168
        self.branch = self.wt.branch
169
        self.build_tree(['file'])
170
        self.wt.commit('new branch', allow_pointless=True, rev_id='A')
171
        self.inv_A = self.branch.repository.get_inventory('A')
172
        self.wt.add(['file'], ['fileid'])
173
        self.wt.commit('add file', rev_id='B')
174
        self.inv_B = self.branch.repository.get_inventory('B')
175
        uncommit(self.branch, tree=self.wt)
176
        self.assertEqual(self.branch.revision_history(), ['A'])
177
        self.wt.commit('another add of file', rev_id='C')
178
        self.inv_C = self.branch.repository.get_inventory('C')
179
        self.wt.add_parent_tree_id('B')
180
        self.wt.commit('merge in B', rev_id='D')
181
        self.inv_D = self.branch.repository.get_inventory('D')
182
        self.tree = self.workingtree_to_test_tree(self.wt)
183
        self.tree.lock_read()
184
        self.addCleanup(self.tree.unlock)
185
        self.file_active = self.tree.inventory['fileid']
186
        self.weave = self.branch.repository.weave_store.get_weave('fileid',
187
            self.branch.repository.get_transaction())
188
        
189
    def get_previous_heads(self, inventories):
2776.3.1 by Robert Collins
* Deprecated method ``find_previous_heads`` on
190
        return self.applyDeprecated(zero_ninetyone,
191
            self.file_active.find_previous_heads,
192
            inventories,
2338.4.6 by Marien Zwart
Move some tests that do not need a working tree from workingtree_implementations to tree_implementations.
193
            self.branch.repository.weave_store,
194
            self.branch.repository.get_transaction())
195
        
196
    def test_fileid_in_no_inventory(self):
197
        self.assertEqual({}, self.get_previous_heads([self.inv_A]))
198
199
    def test_fileid_in_one_inventory(self):
200
        self.assertEqual({'B':self.inv_B['fileid']},
201
                         self.get_previous_heads([self.inv_B]))
202
        self.assertEqual({'B':self.inv_B['fileid']},
203
                         self.get_previous_heads([self.inv_A, self.inv_B]))
204
        self.assertEqual({'B':self.inv_B['fileid']},
205
                         self.get_previous_heads([self.inv_B, self.inv_A]))
206
207
    def test_fileid_in_two_inventories_gives_both_entries(self):
208
        self.assertEqual({'B':self.inv_B['fileid'],
209
                          'C':self.inv_C['fileid']},
210
                          self.get_previous_heads([self.inv_B, self.inv_C]))
211
        self.assertEqual({'B':self.inv_B['fileid'],
212
                          'C':self.inv_C['fileid']},
213
                          self.get_previous_heads([self.inv_C, self.inv_B]))
214
215
    def test_fileid_in_two_inventories_already_merged_gives_head(self):
216
        self.assertEqual({'D':self.inv_D['fileid']},
217
                         self.get_previous_heads([self.inv_B, self.inv_D]))
218
        self.assertEqual({'D':self.inv_D['fileid']},
219
                         self.get_previous_heads([self.inv_D, self.inv_B]))
220
221
    # TODO: test two inventories with the same file revision 
2338.4.9 by Marien Zwart
More tests for symlinks in tree inventories.
222
223
224
class TestInventory(TestCaseWithTree):
225
2408.1.3 by Alexander Belchenko
tree_implementations: make usage of symlinks optional
226
    def _set_up(self):
2338.4.9 by Marien Zwart
More tests for symlinks in tree inventories.
227
        self.tree = self.get_tree_with_subdirs_and_all_content_types()
228
        self.tree.lock_read()
229
        self.addCleanup(self.tree.unlock)
230
        # Commenting out the following line still fails.
231
        self.inv = self.tree.inventory
232
233
    def test_symlink_target(self):
2949.5.1 by Alexander Belchenko
selftest: use SymlinkFeature instead of TestSkipped where appropriate
234
        self.requireFeature(SymlinkFeature)
2408.1.3 by Alexander Belchenko
tree_implementations: make usage of symlinks optional
235
        self._set_up()
2338.4.9 by Marien Zwart
More tests for symlinks in tree inventories.
236
        if isinstance(self.tree, MutableTree):
237
            raise TestSkipped(
238
                'symlinks not accurately represented in working trees')
239
        entry = self.inv[self.inv.path2id('symlink')]
240
        self.assertEqual(entry.symlink_target, 'link-target')
241
242
    def test_symlink(self):
2949.5.1 by Alexander Belchenko
selftest: use SymlinkFeature instead of TestSkipped where appropriate
243
        self.requireFeature(SymlinkFeature)
2408.1.3 by Alexander Belchenko
tree_implementations: make usage of symlinks optional
244
        self._set_up()
2338.4.9 by Marien Zwart
More tests for symlinks in tree inventories.
245
        entry = self.inv[self.inv.path2id('symlink')]
246
        self.assertEqual(entry.kind, 'symlink')
247
        self.assertEqual(None, entry.text_size)