/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/selftest/testinv.py

  • Committer: Robert Collins
  • Date: 2005-10-03 05:54:35 UTC
  • mto: (1393.1.30)
  • mto: This revision was merged to the branch mainline in revision 1400.
  • Revision ID: robertc@robertcollins.net-20051003055434-c8ebd30d1de10247
move exporting functionality into inventory.py - uncovers bug in symlink support

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005 by 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
from cStringIO import StringIO
 
18
import os
 
19
 
 
20
from bzrlib.branch import Branch
 
21
from bzrlib.diff import internal_diff
 
22
from bzrlib.inventory import Inventory, InventoryEntry, ROOT_ID
 
23
from bzrlib.osutils import has_symlinks
 
24
from bzrlib.selftest import TestCase, TestCaseInTempDir
 
25
 
 
26
 
 
27
class TestInventory(TestCase):
 
28
 
 
29
    def test_is_within(self):
 
30
        from bzrlib.osutils import is_inside_any
 
31
 
 
32
        SRC_FOO_C = os.path.join('src', 'foo.c')
 
33
        for dirs, fn in [(['src', 'doc'], SRC_FOO_C),
 
34
                         (['src'], SRC_FOO_C),
 
35
                         (['src'], 'src'),
 
36
                         ]:
 
37
            self.assert_(is_inside_any(dirs, fn))
 
38
            
 
39
        for dirs, fn in [(['src'], 'srccontrol'),
 
40
                         (['src'], 'srccontrol/foo')]:
 
41
            self.assertFalse(is_inside_any(dirs, fn))
 
42
            
 
43
    def test_ids(self):
 
44
        """Test detection of files within selected directories."""
 
45
        inv = Inventory()
 
46
        
 
47
        for args in [('src', 'directory', 'src-id'), 
 
48
                     ('doc', 'directory', 'doc-id'), 
 
49
                     ('src/hello.c', 'file'),
 
50
                     ('src/bye.c', 'file', 'bye-id'),
 
51
                     ('Makefile', 'file')]:
 
52
            inv.add_path(*args)
 
53
            
 
54
        self.assertEqual(inv.path2id('src'), 'src-id')
 
55
        self.assertEqual(inv.path2id('src/bye.c'), 'bye-id')
 
56
        
 
57
        self.assert_('src-id' in inv)
 
58
 
 
59
 
 
60
    def test_version(self):
 
61
        """Inventory remembers the text's version."""
 
62
        inv = Inventory()
 
63
        ie = inv.add_path('foo.txt', 'file')
 
64
        ## XXX
 
65
 
 
66
 
 
67
class TestInventoryEntry(TestCaseInTempDir):
 
68
 
 
69
    def test_file_kind_character(self):
 
70
        file = InventoryEntry('123', 'hello.c', 'file', ROOT_ID)
 
71
        self.assertEqual(file.kind_character(), '')
 
72
 
 
73
    def test_dir_kind_character(self):
 
74
        dir = InventoryEntry('123', 'hello.c', 'directory', ROOT_ID)
 
75
        self.assertEqual(dir.kind_character(), '/')
 
76
 
 
77
    def test_link_kind_character(self):
 
78
        dir = InventoryEntry('123', 'hello.c', 'symlink', ROOT_ID)
 
79
        self.assertEqual(dir.kind_character(), '')
 
80
 
 
81
    def test_dir_detect_changes(self):
 
82
        left = InventoryEntry('123', 'hello.c', 'directory', ROOT_ID)
 
83
        left.text_sha1 = 123
 
84
        left.executable = True
 
85
        left.symlink_target='foo'
 
86
        right = InventoryEntry('123', 'hello.c', 'directory', ROOT_ID)
 
87
        right.text_sha1 = 321
 
88
        right.symlink_target='bar'
 
89
        self.assertEqual((False, False), left.detect_changes(right))
 
90
        self.assertEqual((False, False), right.detect_changes(left))
 
91
 
 
92
    def test_file_detect_changes(self):
 
93
        left = InventoryEntry('123', 'hello.c', 'file', ROOT_ID)
 
94
        left.text_sha1 = 123
 
95
        right = InventoryEntry('123', 'hello.c', 'file', ROOT_ID)
 
96
        right.text_sha1 = 123
 
97
        self.assertEqual((False, False), left.detect_changes(right))
 
98
        self.assertEqual((False, False), right.detect_changes(left))
 
99
        left.executable = True
 
100
        self.assertEqual((False, True), left.detect_changes(right))
 
101
        self.assertEqual((False, True), right.detect_changes(left))
 
102
        right.text_sha1 = 321
 
103
        self.assertEqual((True, True), left.detect_changes(right))
 
104
        self.assertEqual((True, True), right.detect_changes(left))
 
105
 
 
106
    def test_symlink_detect_changes(self):
 
107
        left = InventoryEntry('123', 'hello.c', 'symlink', ROOT_ID)
 
108
        left.text_sha1 = 123
 
109
        left.executable = True
 
110
        left.symlink_target='foo'
 
111
        right = InventoryEntry('123', 'hello.c', 'symlink', ROOT_ID)
 
112
        right.text_sha1 = 321
 
113
        right.symlink_target='foo'
 
114
        self.assertEqual((False, False), left.detect_changes(right))
 
115
        self.assertEqual((False, False), right.detect_changes(left))
 
116
        left.symlink_target = 'different'
 
117
        self.assertEqual((True, False), left.detect_changes(right))
 
118
        self.assertEqual((True, False), right.detect_changes(left))
 
119
 
 
120
    def test_file_has_text(self):
 
121
        file = InventoryEntry('123', 'hello.c', 'file', ROOT_ID)
 
122
        self.failUnless(file.has_text())
 
123
 
 
124
    def test_directory_has_text(self):
 
125
        dir = InventoryEntry('123', 'hello.c', 'directory', ROOT_ID)
 
126
        self.failIf(dir.has_text())
 
127
 
 
128
    def test_link_has_text(self):
 
129
        link = InventoryEntry('123', 'hello.c', 'symlink', ROOT_ID)
 
130
        self.failIf(link.has_text())
 
131
 
 
132
 
 
133
class TestEntryDiffing(TestCaseInTempDir):
 
134
 
 
135
    def setUp(self):
 
136
        super(TestEntryDiffing, self).setUp()
 
137
        self.branch = Branch.initialize('.')
 
138
        print >> open('file', 'wb'), 'foo'
 
139
        self.branch.add(['file'], ['fileid'])
 
140
        if has_symlinks():
 
141
            os.symlink('target1', 'symlink')
 
142
            self.branch.add(['symlink'], ['linkid'])
 
143
        self.branch.commit('message_1', rev_id = '1')
 
144
        print >> open('file', 'wb'), 'bar'
 
145
        if has_symlinks():
 
146
            os.unlink('symlink')
 
147
            os.symlink('target2', 'symlink')
 
148
        self.tree_1 = self.branch.revision_tree('1')
 
149
        self.inv_1 = self.branch.get_inventory('1')
 
150
        self.file_1 = self.inv_1['fileid']
 
151
        self.tree_2 = self.branch.working_tree()
 
152
        self.inv_2 = self.branch.inventory
 
153
        self.file_2 = self.inv_2['fileid']
 
154
        if has_symlinks():
 
155
            self.link_1 = self.inv_1['linkid']
 
156
            self.link_2 = self.inv_2['linkid']
 
157
 
 
158
    def test_file_diff_deleted(self):
 
159
        output = StringIO()
 
160
        self.file_1.diff(internal_diff, 
 
161
                          "old_label", self.tree_1,
 
162
                          "/dev/null", None, None,
 
163
                          output)
 
164
        self.assertEqual(output.getvalue(), "--- old_label\n"
 
165
                                            "+++ /dev/null\n"
 
166
                                            "@@ -1,1 +0,0 @@\n"
 
167
                                            "-foo\n"
 
168
                                            "\n")
 
169
 
 
170
    def test_file_diff_added(self):
 
171
        output = StringIO()
 
172
        self.file_1.diff(internal_diff, 
 
173
                          "new_label", self.tree_1,
 
174
                          "/dev/null", None, None,
 
175
                          output, reverse=True)
 
176
        self.assertEqual(output.getvalue(), "--- /dev/null\n"
 
177
                                            "+++ new_label\n"
 
178
                                            "@@ -0,0 +1,1 @@\n"
 
179
                                            "+foo\n"
 
180
                                            "\n")
 
181
 
 
182
    def test_file_diff_changed(self):
 
183
        output = StringIO()
 
184
        self.file_1.diff(internal_diff, 
 
185
                          "/dev/null", self.tree_1, 
 
186
                          "new_label", self.file_2, self.tree_2,
 
187
                          output)
 
188
        self.assertEqual(output.getvalue(), "--- /dev/null\n"
 
189
                                            "+++ new_label\n"
 
190
                                            "@@ -1,1 +1,1 @@\n"
 
191
                                            "-foo\n"
 
192
                                            "+bar\n"
 
193
                                            "\n")
 
194
        
 
195
    def test_link_diff_deleted(self):
 
196
        output = StringIO()
 
197
        self.link_1.diff(internal_diff, 
 
198
                          "old_label", self.tree_1,
 
199
                          "/dev/null", None, None,
 
200
                          output)
 
201
        self.assertEqual(output.getvalue(),
 
202
                         "=== target was 'target1'\n")
 
203
 
 
204
    def test_link_diff_added(self):
 
205
        output = StringIO()
 
206
        self.link_1.diff(internal_diff, 
 
207
                          "new_label", self.tree_1,
 
208
                          "/dev/null", None, None,
 
209
                          output, reverse=True)
 
210
        self.assertEqual(output.getvalue(),
 
211
                         "=== target is 'target1'\n")
 
212
 
 
213
    def test_link_diff_changed(self):
 
214
        output = StringIO()
 
215
        self.link_1.diff(internal_diff, 
 
216
                          "/dev/null", self.tree_1, 
 
217
                          "new_label", self.link_2, self.tree_2,
 
218
                          output)
 
219
        self.assertEqual(output.getvalue(),
 
220
                         "=== target changed 'target1' => 'target2'\n")