~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Marien Zwart
  • Date: 2007-03-12 02:35:23 UTC
  • mto: (2348.1.1 mz.symlinks)
  • mto: This revision was merged to the branch mainline in revision 2349.
  • Revision ID: marienz@gentoo.org-20070312023523-h4bhx3oyqmbd27tf
Move some tests that do not need a working tree from workingtree_implementations to tree_implementations.

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