/brz/remove-bazaar

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