~bzr-pqm/bzr/bzr.dev

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