~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Robert Collins
  • Date: 2007-07-04 08:08:13 UTC
  • mfrom: (2572 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2587.
  • Revision ID: robertc@robertcollins.net-20070704080813-wzebx0r88fvwj5rq
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

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