~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_inv.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2008-04-07 07:52:50 UTC
  • mfrom: (3340.1.1 208418-1.4)
  • Revision ID: pqm@pqm.ubuntu.com-20080407075250-phs53xnslo8boaeo
Return the correct knit serialisation method in _StreamAccess.
        (Andrew Bennetts, Martin Pool, Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 by Canonical Ltd
2
 
 
 
1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
 
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
7
 
 
 
7
#
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
# GNU General Public License for more details.
12
 
 
 
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
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, ROOT_ID
23
 
import bzrlib.inventory as inventory
24
 
from bzrlib.osutils import has_symlinks
25
 
from bzrlib.selftest import TestCase, TestCaseInTempDir
26
 
 
27
 
 
28
 
class TestInventory(TestCase):
29
 
 
30
 
    def test_is_within(self):
31
 
        from bzrlib.osutils import is_inside_any
32
 
 
33
 
        SRC_FOO_C = os.path.join('src', 'foo.c')
34
 
        for dirs, fn in [(['src', 'doc'], SRC_FOO_C),
35
 
                         (['src'], SRC_FOO_C),
36
 
                         (['src'], 'src'),
37
 
                         ]:
38
 
            self.assert_(is_inside_any(dirs, fn))
39
 
            
40
 
        for dirs, fn in [(['src'], 'srccontrol'),
41
 
                         (['src'], 'srccontrol/foo')]:
42
 
            self.assertFalse(is_inside_any(dirs, fn))
43
 
            
44
 
    def test_ids(self):
45
 
        """Test detection of files within selected directories."""
46
 
        inv = Inventory()
47
 
        
48
 
        for args in [('src', 'directory', 'src-id'), 
49
 
                     ('doc', 'directory', 'doc-id'), 
50
 
                     ('src/hello.c', 'file'),
51
 
                     ('src/bye.c', 'file', 'bye-id'),
52
 
                     ('Makefile', 'file')]:
53
 
            inv.add_path(*args)
54
 
            
55
 
        self.assertEqual(inv.path2id('src'), 'src-id')
56
 
        self.assertEqual(inv.path2id('src/bye.c'), 'bye-id')
57
 
        
58
 
        self.assert_('src-id' in inv)
59
 
 
60
 
 
61
 
    def test_version(self):
62
 
        """Inventory remembers the text's version."""
63
 
        inv = Inventory()
64
 
        ie = inv.add_path('foo.txt', 'file')
65
 
        ## XXX
66
 
 
67
 
 
68
 
class TestInventoryEntry(TestCaseInTempDir):
 
17
 
 
18
from bzrlib import errors, inventory, osutils
 
19
from bzrlib.inventory import (Inventory, ROOT_ID, InventoryFile,
 
20
    InventoryDirectory, InventoryEntry, TreeReference)
 
21
from bzrlib.tests import TestCase
 
22
 
 
23
 
 
24
class TestInventoryEntry(TestCase):
69
25
 
70
26
    def test_file_kind_character(self):
71
27
        file = inventory.InventoryFile('123', 'hello.c', ROOT_ID)
130
86
        link = inventory.InventoryLink('123', 'hello.c', ROOT_ID)
131
87
        self.failIf(link.has_text())
132
88
 
133
 
 
134
 
class TestEntryDiffing(TestCaseInTempDir):
135
 
 
136
 
    def setUp(self):
137
 
        super(TestEntryDiffing, self).setUp()
138
 
        self.branch = Branch.initialize('.')
139
 
        print >> open('file', 'wb'), 'foo'
140
 
        self.branch.add(['file'], ['fileid'])
141
 
        if has_symlinks():
142
 
            os.symlink('target1', 'symlink')
143
 
            self.branch.add(['symlink'], ['linkid'])
144
 
        self.branch.commit('message_1', rev_id = '1')
145
 
        print >> open('file', 'wb'), 'bar'
146
 
        if has_symlinks():
147
 
            os.unlink('symlink')
148
 
            os.symlink('target2', 'symlink')
149
 
        self.tree_1 = self.branch.revision_tree('1')
150
 
        self.inv_1 = self.branch.get_inventory('1')
151
 
        self.file_1 = self.inv_1['fileid']
152
 
        self.tree_2 = self.branch.working_tree()
153
 
        self.inv_2 = self.branch.inventory
154
 
        self.file_2 = self.inv_2['fileid']
155
 
        if has_symlinks():
156
 
            self.link_1 = self.inv_1['linkid']
157
 
            self.link_2 = self.inv_2['linkid']
158
 
 
159
 
    def test_file_diff_deleted(self):
160
 
        output = StringIO()
161
 
        self.file_1.diff(internal_diff, 
162
 
                          "old_label", self.tree_1,
163
 
                          "/dev/null", None, None,
164
 
                          output)
165
 
        self.assertEqual(output.getvalue(), "--- old_label\n"
166
 
                                            "+++ /dev/null\n"
167
 
                                            "@@ -1,1 +0,0 @@\n"
168
 
                                            "-foo\n"
169
 
                                            "\n")
170
 
 
171
 
    def test_file_diff_added(self):
172
 
        output = StringIO()
173
 
        self.file_1.diff(internal_diff, 
174
 
                          "new_label", self.tree_1,
175
 
                          "/dev/null", None, None,
176
 
                          output, reverse=True)
177
 
        self.assertEqual(output.getvalue(), "--- /dev/null\n"
178
 
                                            "+++ new_label\n"
179
 
                                            "@@ -0,0 +1,1 @@\n"
180
 
                                            "+foo\n"
181
 
                                            "\n")
182
 
 
183
 
    def test_file_diff_changed(self):
184
 
        output = StringIO()
185
 
        self.file_1.diff(internal_diff, 
186
 
                          "/dev/null", self.tree_1, 
187
 
                          "new_label", self.file_2, self.tree_2,
188
 
                          output)
189
 
        self.assertEqual(output.getvalue(), "--- /dev/null\n"
190
 
                                            "+++ new_label\n"
191
 
                                            "@@ -1,1 +1,1 @@\n"
192
 
                                            "-foo\n"
193
 
                                            "+bar\n"
194
 
                                            "\n")
195
 
        
196
 
    def test_link_diff_deleted(self):
197
 
        output = StringIO()
198
 
        self.link_1.diff(internal_diff, 
199
 
                          "old_label", self.tree_1,
200
 
                          "/dev/null", None, None,
201
 
                          output)
202
 
        self.assertEqual(output.getvalue(),
203
 
                         "=== target was 'target1'\n")
204
 
 
205
 
    def test_link_diff_added(self):
206
 
        output = StringIO()
207
 
        self.link_1.diff(internal_diff, 
208
 
                          "new_label", self.tree_1,
209
 
                          "/dev/null", None, None,
210
 
                          output, reverse=True)
211
 
        self.assertEqual(output.getvalue(),
212
 
                         "=== target is 'target1'\n")
213
 
 
214
 
    def test_link_diff_changed(self):
215
 
        output = StringIO()
216
 
        self.link_1.diff(internal_diff, 
217
 
                          "/dev/null", self.tree_1, 
218
 
                          "new_label", self.link_2, self.tree_2,
219
 
                          output)
220
 
        self.assertEqual(output.getvalue(),
221
 
                         "=== target changed 'target1' => 'target2'\n")
 
89
    def test_make_entry(self):
 
90
        self.assertIsInstance(inventory.make_entry("file", "name", ROOT_ID),
 
91
            inventory.InventoryFile)
 
92
        self.assertIsInstance(inventory.make_entry("symlink", "name", ROOT_ID),
 
93
            inventory.InventoryLink)
 
94
        self.assertIsInstance(inventory.make_entry("directory", "name", ROOT_ID),
 
95
            inventory.InventoryDirectory)
 
96
 
 
97
    def test_make_entry_non_normalized(self):
 
98
        orig_normalized_filename = osutils.normalized_filename
 
99
 
 
100
        try:
 
101
            osutils.normalized_filename = osutils._accessible_normalized_filename
 
102
            entry = inventory.make_entry("file", u'a\u030a', ROOT_ID)
 
103
            self.assertEqual(u'\xe5', entry.name)
 
104
            self.assertIsInstance(entry, inventory.InventoryFile)
 
105
 
 
106
            osutils.normalized_filename = osutils._inaccessible_normalized_filename
 
107
            self.assertRaises(errors.InvalidNormalization,
 
108
                    inventory.make_entry, 'file', u'a\u030a', ROOT_ID)
 
109
        finally:
 
110
            osutils.normalized_filename = orig_normalized_filename
 
111
 
 
112
 
 
113
class TestDescribeChanges(TestCase):
 
114
 
 
115
    def test_describe_change(self):
 
116
        # we need to test the following change combinations:
 
117
        # rename
 
118
        # reparent
 
119
        # modify
 
120
        # gone
 
121
        # added
 
122
        # renamed/reparented and modified
 
123
        # change kind (perhaps can't be done yet?)
 
124
        # also, merged in combination with all of these?
 
125
        old_a = InventoryFile('a-id', 'a_file', ROOT_ID)
 
126
        old_a.text_sha1 = '123132'
 
127
        old_a.text_size = 0
 
128
        new_a = InventoryFile('a-id', 'a_file', ROOT_ID)
 
129
        new_a.text_sha1 = '123132'
 
130
        new_a.text_size = 0
 
131
 
 
132
        self.assertChangeDescription('unchanged', old_a, new_a)
 
133
 
 
134
        new_a.text_size = 10
 
135
        new_a.text_sha1 = 'abcabc'
 
136
        self.assertChangeDescription('modified', old_a, new_a)
 
137
 
 
138
        self.assertChangeDescription('added', None, new_a)
 
139
        self.assertChangeDescription('removed', old_a, None)
 
140
        # perhaps a bit questionable but seems like the most reasonable thing...
 
141
        self.assertChangeDescription('unchanged', None, None)
 
142
 
 
143
        # in this case it's both renamed and modified; show a rename and 
 
144
        # modification:
 
145
        new_a.name = 'newfilename'
 
146
        self.assertChangeDescription('modified and renamed', old_a, new_a)
 
147
 
 
148
        # reparenting is 'renaming'
 
149
        new_a.name = old_a.name
 
150
        new_a.parent_id = 'somedir-id'
 
151
        self.assertChangeDescription('modified and renamed', old_a, new_a)
 
152
 
 
153
        # reset the content values so its not modified
 
154
        new_a.text_size = old_a.text_size
 
155
        new_a.text_sha1 = old_a.text_sha1
 
156
        new_a.name = old_a.name
 
157
 
 
158
        new_a.name = 'newfilename'
 
159
        self.assertChangeDescription('renamed', old_a, new_a)
 
160
 
 
161
        # reparenting is 'renaming'
 
162
        new_a.name = old_a.name
 
163
        new_a.parent_id = 'somedir-id'
 
164
        self.assertChangeDescription('renamed', old_a, new_a)
 
165
 
 
166
    def assertChangeDescription(self, expected_change, old_ie, new_ie):
 
167
        change = InventoryEntry.describe_change(old_ie, new_ie)
 
168
        self.assertEqual(expected_change, change)