/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2052.3.2 by John Arbash Meinel
Change Copyright .. by Canonical to Copyright ... Canonical
1
# Copyright (C) 2005, 2006 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
963 by Martin Pool
- add the start of a test for inventory file-id matching
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
963 by Martin Pool
- add the start of a test for inventory file-id matching
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
963 by Martin Pool
- add the start of a test for inventory file-id matching
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
1830.3.5 by John Arbash Meinel
make_entry refuses to create non-normalized entries.
17
from bzrlib import errors, inventory, osutils
1668.1.5 by Martin Pool
[broken] fix up display of files changed by a commit
18
from bzrlib.inventory import (Inventory, ROOT_ID, InventoryFile,
2100.3.1 by Aaron Bentley
Start roundtripping tree-reference entries
19
    InventoryDirectory, InventoryEntry, TreeReference)
2338.4.2 by Marien Zwart
Move the workingtree-related inventory tests to a separate file.
20
from bzrlib.osutils import (pathjoin, is_inside_any, 
1740.3.4 by Jelmer Vernooij
Move inventory to commit builder.
21
    is_inside_or_parent_of_any)
2338.4.2 by Marien Zwart
Move the workingtree-related inventory tests to a separate file.
22
from bzrlib.tests import TestCase
963 by Martin Pool
- add the start of a test for inventory file-id matching
23
969 by Martin Pool
- Add less-sucky is_within_any
24
1102 by Martin Pool
- merge test refactoring from robertc
25
class TestInventory(TestCase):
26
27
    def test_is_within(self):
1185.1.40 by Robert Collins
Merge what applied of Alexander Belchenko's win32 patch.
28
1185.31.32 by John Arbash Meinel
Updated the bzr sourcecode to use bzrlib.osutils.pathjoin rather than os.path.join to enforce internal use of / instead of \
29
        SRC_FOO_C = pathjoin('src', 'foo.c')
1185.1.40 by Robert Collins
Merge what applied of Alexander Belchenko's win32 patch.
30
        for dirs, fn in [(['src', 'doc'], SRC_FOO_C),
31
                         (['src'], SRC_FOO_C),
968 by Martin Pool
- add some passing tests for is_inside_any
32
                         (['src'], 'src'),
33
                         ]:
34
            self.assert_(is_inside_any(dirs, fn))
35
            
969 by Martin Pool
- Add less-sucky is_within_any
36
        for dirs, fn in [(['src'], 'srccontrol'),
37
                         (['src'], 'srccontrol/foo')]:
38
            self.assertFalse(is_inside_any(dirs, fn))
1740.3.4 by Jelmer Vernooij
Move inventory to commit builder.
39
40
    def test_is_within_or_parent(self):
41
        for dirs, fn in [(['src', 'doc'], 'src/foo.c'),
42
                         (['src'], 'src/foo.c'),
43
                         (['src/bar.c'], 'src'),
44
                         (['src/bar.c', 'bla/foo.c'], 'src'),
45
                         (['src'], 'src'),
46
                         ]:
47
            self.assert_(is_inside_or_parent_of_any(dirs, fn))
969 by Martin Pool
- Add less-sucky is_within_any
48
            
1740.3.4 by Jelmer Vernooij
Move inventory to commit builder.
49
        for dirs, fn in [(['src'], 'srccontrol'),
50
                         (['srccontrol/foo.c'], 'src'),
51
                         (['src'], 'srccontrol/foo')]:
52
            self.assertFalse(is_inside_or_parent_of_any(dirs, fn))
53
1102 by Martin Pool
- merge test refactoring from robertc
54
    def test_ids(self):
963 by Martin Pool
- add the start of a test for inventory file-id matching
55
        """Test detection of files within selected directories."""
56
        inv = Inventory()
57
        
58
        for args in [('src', 'directory', 'src-id'), 
59
                     ('doc', 'directory', 'doc-id'), 
60
                     ('src/hello.c', 'file'),
61
                     ('src/bye.c', 'file', 'bye-id'),
62
                     ('Makefile', 'file')]:
63
            inv.add_path(*args)
64
            
65
        self.assertEqual(inv.path2id('src'), 'src-id')
66
        self.assertEqual(inv.path2id('src/bye.c'), 'bye-id')
67
        
68
        self.assert_('src-id' in inv)
1180 by Martin Pool
- start splitting code for xml (de)serialization away from objects
69
2091.3.1 by Aaron Bentley
When 'directory' path element isn't a directory, return None from path2id
70
    def test_non_directory_children(self):
71
        """Test path2id when a parent directory has no children"""
72
        inv = inventory.Inventory('tree_root')
73
        inv.add(inventory.InventoryFile('file-id','file', 
74
                                        parent_id='tree_root'))
75
        inv.add(inventory.InventoryLink('link-id','link', 
76
                                        parent_id='tree_root'))
77
        self.assertIs(None, inv.path2id('file/subfile'))
78
        self.assertIs(None, inv.path2id('link/subfile'))
79
1732.1.23 by John Arbash Meinel
Switch iter_entries from being a recursive function and using pathjoin
80
    def test_iter_entries(self):
81
        inv = Inventory()
82
        
83
        for args in [('src', 'directory', 'src-id'), 
84
                     ('doc', 'directory', 'doc-id'), 
85
                     ('src/hello.c', 'file', 'hello-id'),
86
                     ('src/bye.c', 'file', 'bye-id'),
87
                     ('Makefile', 'file', 'makefile-id')]:
88
            inv.add_path(*args)
89
90
        self.assertEqual([
1852.6.3 by Robert Collins
Make iter(Tree) consistent for all tree types.
91
            ('', ROOT_ID),
1732.1.23 by John Arbash Meinel
Switch iter_entries from being a recursive function and using pathjoin
92
            ('Makefile', 'makefile-id'),
93
            ('doc', 'doc-id'),
94
            ('src', 'src-id'),
95
            ('src/bye.c', 'bye-id'),
96
            ('src/hello.c', 'hello-id'),
97
            ], [(path, ie.file_id) for path, ie in inv.iter_entries()])
98
            
1711.2.36 by John Arbash Meinel
Add an iter_entries_by_dir which returns directory children before their children.
99
    def test_iter_entries_by_dir(self):
100
        inv = Inventory()
101
        
102
        for args in [('src', 'directory', 'src-id'), 
103
                     ('doc', 'directory', 'doc-id'), 
104
                     ('src/hello.c', 'file', 'hello-id'),
105
                     ('src/bye.c', 'file', 'bye-id'),
106
                     ('zz', 'file', 'zz-id'),
107
                     ('src/sub/', 'directory', 'sub-id'),
108
                     ('src/zz.c', 'file', 'zzc-id'),
109
                     ('src/sub/a', 'file', 'a-id'),
110
                     ('Makefile', 'file', 'makefile-id')]:
111
            inv.add_path(*args)
112
113
        self.assertEqual([
1852.6.6 by Robert Collins
Finish updating iter_entries change to make all tests pass.
114
            ('', ROOT_ID),
1711.2.36 by John Arbash Meinel
Add an iter_entries_by_dir which returns directory children before their children.
115
            ('Makefile', 'makefile-id'),
116
            ('doc', 'doc-id'),
117
            ('src', 'src-id'),
118
            ('zz', 'zz-id'),
119
            ('src/bye.c', 'bye-id'),
120
            ('src/hello.c', 'hello-id'),
121
            ('src/sub', 'sub-id'),
122
            ('src/zz.c', 'zzc-id'),
123
            ('src/sub/a', 'a-id'),
124
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir()])
125
            
1551.9.29 by Aaron Bentley
Optimize Tree._iter_changes with specific file_ids
126
        self.assertEqual([
127
            ('', ROOT_ID),
128
            ('Makefile', 'makefile-id'),
129
            ('doc', 'doc-id'),
130
            ('src', 'src-id'),
131
            ('zz', 'zz-id'),
132
            ('src/bye.c', 'bye-id'),
133
            ('src/hello.c', 'hello-id'),
134
            ('src/sub', 'sub-id'),
135
            ('src/zz.c', 'zzc-id'),
136
            ('src/sub/a', 'a-id'),
137
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
138
                specific_file_ids=('a-id', 'zzc-id', 'doc-id', ROOT_ID,
139
                'hello-id', 'bye-id', 'zz-id', 'src-id', 'makefile-id', 
140
                'sub-id'))])
141
142
        self.assertEqual([
143
            ('Makefile', 'makefile-id'),
144
            ('doc', 'doc-id'),
145
            ('zz', 'zz-id'),
146
            ('src/bye.c', 'bye-id'),
147
            ('src/hello.c', 'hello-id'),
148
            ('src/zz.c', 'zzc-id'),
149
            ('src/sub/a', 'a-id'),
150
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
151
                specific_file_ids=('a-id', 'zzc-id', 'doc-id',
152
                'hello-id', 'bye-id', 'zz-id', 'makefile-id'))])
153
154
        self.assertEqual([
155
            ('Makefile', 'makefile-id'),
156
            ('src/bye.c', 'bye-id'),
157
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
158
                specific_file_ids=('bye-id', 'makefile-id'))])
159
160
        self.assertEqual([
161
            ('Makefile', 'makefile-id'),
162
            ('src/bye.c', 'bye-id'),
163
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
164
                specific_file_ids=('bye-id', 'makefile-id'))])
165
166
        self.assertEqual([
167
            ('src/bye.c', 'bye-id'),
168
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
169
                specific_file_ids=('bye-id',))])
170
2100.3.6 by Aaron Bentley
Make add recursive for children of added entries
171
    def test_add_recursive(self):
172
        parent = InventoryDirectory('src-id', 'src', ROOT_ID)
173
        child = InventoryFile('hello-id', 'hello.c', 'src-id')
174
        parent.children[child.file_id] = child
175
        inv = Inventory()
176
        inv.add(parent)
177
        self.assertEqual('src/hello.c', inv.id2path('hello-id'))
178
1399.1.2 by Robert Collins
push kind character creation into InventoryEntry and TreeEntry
179
1407 by Robert Collins
define some expected behaviour for inventory_entry.snapshot
180
class TestInventoryEntry(TestCase):
1399.1.2 by Robert Collins
push kind character creation into InventoryEntry and TreeEntry
181
182
    def test_file_kind_character(self):
1399.1.9 by Robert Collins
factor out file related logic from InventoryEntry to InventoryFile
183
        file = inventory.InventoryFile('123', 'hello.c', ROOT_ID)
1399.1.2 by Robert Collins
push kind character creation into InventoryEntry and TreeEntry
184
        self.assertEqual(file.kind_character(), '')
185
186
    def test_dir_kind_character(self):
1399.1.8 by Robert Collins
factor out inventory directory logic into 'InventoryDirectory' class
187
        dir = inventory.InventoryDirectory('123', 'hello.c', ROOT_ID)
1399.1.2 by Robert Collins
push kind character creation into InventoryEntry and TreeEntry
188
        self.assertEqual(dir.kind_character(), '/')
189
190
    def test_link_kind_character(self):
1399.1.10 by Robert Collins
remove kind from the InventoryEntry constructor - only child classes should be created now
191
        dir = inventory.InventoryLink('123', 'hello.c', ROOT_ID)
1399.1.2 by Robert Collins
push kind character creation into InventoryEntry and TreeEntry
192
        self.assertEqual(dir.kind_character(), '')
1399.1.3 by Robert Collins
move change detection for text and metadata from delta to entry.detect_changes
193
194
    def test_dir_detect_changes(self):
1399.1.8 by Robert Collins
factor out inventory directory logic into 'InventoryDirectory' class
195
        left = inventory.InventoryDirectory('123', 'hello.c', ROOT_ID)
1399.1.3 by Robert Collins
move change detection for text and metadata from delta to entry.detect_changes
196
        left.text_sha1 = 123
197
        left.executable = True
198
        left.symlink_target='foo'
1399.1.8 by Robert Collins
factor out inventory directory logic into 'InventoryDirectory' class
199
        right = inventory.InventoryDirectory('123', 'hello.c', ROOT_ID)
1399.1.3 by Robert Collins
move change detection for text and metadata from delta to entry.detect_changes
200
        right.text_sha1 = 321
201
        right.symlink_target='bar'
202
        self.assertEqual((False, False), left.detect_changes(right))
203
        self.assertEqual((False, False), right.detect_changes(left))
204
205
    def test_file_detect_changes(self):
1399.1.9 by Robert Collins
factor out file related logic from InventoryEntry to InventoryFile
206
        left = inventory.InventoryFile('123', 'hello.c', ROOT_ID)
1399.1.3 by Robert Collins
move change detection for text and metadata from delta to entry.detect_changes
207
        left.text_sha1 = 123
1399.1.9 by Robert Collins
factor out file related logic from InventoryEntry to InventoryFile
208
        right = inventory.InventoryFile('123', 'hello.c', ROOT_ID)
1399.1.3 by Robert Collins
move change detection for text and metadata from delta to entry.detect_changes
209
        right.text_sha1 = 123
210
        self.assertEqual((False, False), left.detect_changes(right))
211
        self.assertEqual((False, False), right.detect_changes(left))
212
        left.executable = True
213
        self.assertEqual((False, True), left.detect_changes(right))
214
        self.assertEqual((False, True), right.detect_changes(left))
215
        right.text_sha1 = 321
216
        self.assertEqual((True, True), left.detect_changes(right))
217
        self.assertEqual((True, True), right.detect_changes(left))
218
219
    def test_symlink_detect_changes(self):
1399.1.10 by Robert Collins
remove kind from the InventoryEntry constructor - only child classes should be created now
220
        left = inventory.InventoryLink('123', 'hello.c', ROOT_ID)
1399.1.3 by Robert Collins
move change detection for text and metadata from delta to entry.detect_changes
221
        left.text_sha1 = 123
222
        left.executable = True
223
        left.symlink_target='foo'
1399.1.10 by Robert Collins
remove kind from the InventoryEntry constructor - only child classes should be created now
224
        right = inventory.InventoryLink('123', 'hello.c', ROOT_ID)
1399.1.3 by Robert Collins
move change detection for text and metadata from delta to entry.detect_changes
225
        right.text_sha1 = 321
226
        right.symlink_target='foo'
227
        self.assertEqual((False, False), left.detect_changes(right))
228
        self.assertEqual((False, False), right.detect_changes(left))
229
        left.symlink_target = 'different'
230
        self.assertEqual((True, False), left.detect_changes(right))
231
        self.assertEqual((True, False), right.detect_changes(left))
1399.1.4 by Robert Collins
move diff and symlink conditionals into inventory.py from diff.py
232
1399.1.5 by Robert Collins
move checking whether an entry stores text into inventory.py from fetch,py
233
    def test_file_has_text(self):
1399.1.9 by Robert Collins
factor out file related logic from InventoryEntry to InventoryFile
234
        file = inventory.InventoryFile('123', 'hello.c', ROOT_ID)
1399.1.5 by Robert Collins
move checking whether an entry stores text into inventory.py from fetch,py
235
        self.failUnless(file.has_text())
236
237
    def test_directory_has_text(self):
1399.1.8 by Robert Collins
factor out inventory directory logic into 'InventoryDirectory' class
238
        dir = inventory.InventoryDirectory('123', 'hello.c', ROOT_ID)
1399.1.5 by Robert Collins
move checking whether an entry stores text into inventory.py from fetch,py
239
        self.failIf(dir.has_text())
240
241
    def test_link_has_text(self):
1399.1.10 by Robert Collins
remove kind from the InventoryEntry constructor - only child classes should be created now
242
        link = inventory.InventoryLink('123', 'hello.c', ROOT_ID)
1399.1.5 by Robert Collins
move checking whether an entry stores text into inventory.py from fetch,py
243
        self.failIf(link.has_text())
244
1713.1.11 by Robert Collins
refactor smart_add to pass around the parent inventory entry and use that, resulting in another 100bzrlib/inventory.py performance improvement, and making inventory writing the dominating factory in add. (Robert Collins)
245
    def test_make_entry(self):
246
        self.assertIsInstance(inventory.make_entry("file", "name", ROOT_ID),
247
            inventory.InventoryFile)
248
        self.assertIsInstance(inventory.make_entry("symlink", "name", ROOT_ID),
249
            inventory.InventoryLink)
250
        self.assertIsInstance(inventory.make_entry("directory", "name", ROOT_ID),
251
            inventory.InventoryDirectory)
1399.1.4 by Robert Collins
move diff and symlink conditionals into inventory.py from diff.py
252
1830.3.5 by John Arbash Meinel
make_entry refuses to create non-normalized entries.
253
    def test_make_entry_non_normalized(self):
254
        orig_normalized_filename = osutils.normalized_filename
255
256
        try:
257
            osutils.normalized_filename = osutils._accessible_normalized_filename
258
            entry = inventory.make_entry("file", u'a\u030a', ROOT_ID)
259
            self.assertEqual(u'\xe5', entry.name)
260
            self.assertIsInstance(entry, inventory.InventoryFile)
261
262
            osutils.normalized_filename = osutils._inaccessible_normalized_filename
263
            self.assertRaises(errors.InvalidNormalization,
264
                    inventory.make_entry, 'file', u'a\u030a', ROOT_ID)
265
        finally:
266
            osutils.normalized_filename = orig_normalized_filename
267
268
1668.1.5 by Martin Pool
[broken] fix up display of files changed by a commit
269
class TestDescribeChanges(TestCase):
270
271
    def test_describe_change(self):
272
        # we need to test the following change combinations:
273
        # rename
274
        # reparent
275
        # modify
276
        # gone
277
        # added
278
        # renamed/reparented and modified
279
        # change kind (perhaps can't be done yet?)
280
        # also, merged in combination with all of these?
281
        old_a = InventoryFile('a-id', 'a_file', ROOT_ID)
282
        old_a.text_sha1 = '123132'
283
        old_a.text_size = 0
284
        new_a = InventoryFile('a-id', 'a_file', ROOT_ID)
285
        new_a.text_sha1 = '123132'
286
        new_a.text_size = 0
287
288
        self.assertChangeDescription('unchanged', old_a, new_a)
289
290
        new_a.text_size = 10
291
        new_a.text_sha1 = 'abcabc'
292
        self.assertChangeDescription('modified', old_a, new_a)
293
294
        self.assertChangeDescription('added', None, new_a)
295
        self.assertChangeDescription('removed', old_a, None)
296
        # perhaps a bit questionable but seems like the most reasonable thing...
297
        self.assertChangeDescription('unchanged', None, None)
298
299
        # in this case it's both renamed and modified; show a rename and 
300
        # modification:
301
        new_a.name = 'newfilename'
302
        self.assertChangeDescription('modified and renamed', old_a, new_a)
303
304
        # reparenting is 'renaming'
305
        new_a.name = old_a.name
306
        new_a.parent_id = 'somedir-id'
307
        self.assertChangeDescription('modified and renamed', old_a, new_a)
308
309
        # reset the content values so its not modified
310
        new_a.text_size = old_a.text_size
311
        new_a.text_sha1 = old_a.text_sha1
312
        new_a.name = old_a.name
313
314
        new_a.name = 'newfilename'
315
        self.assertChangeDescription('renamed', old_a, new_a)
316
317
        # reparenting is 'renaming'
318
        new_a.name = old_a.name
319
        new_a.parent_id = 'somedir-id'
320
        self.assertChangeDescription('renamed', old_a, new_a)
321
322
    def assertChangeDescription(self, expected_change, old_ie, new_ie):
323
        change = InventoryEntry.describe_change(old_ie, new_ie)
324
        self.assertEqual(expected_change, change)
325
326
1731.1.39 by Aaron Bentley
Reject removing is_root
327
class TestIsRoot(TestCase):
328
    """Ensure our root-checking code is accurate."""
329
330
    def test_is_root(self):
331
        inv = Inventory('TREE_ROOT')
332
        self.assertTrue(inv.is_root('TREE_ROOT'))
333
        self.assertFalse(inv.is_root('booga'))
334
        inv.root.file_id = 'booga'
335
        self.assertFalse(inv.is_root('TREE_ROOT'))
336
        self.assertTrue(inv.is_root('booga'))
337
        # works properly even if no root is set
338
        inv.root = None
339
        self.assertFalse(inv.is_root('TREE_ROOT'))
340
        self.assertFalse(inv.is_root('booga'))
2100.3.1 by Aaron Bentley
Start roundtripping tree-reference entries
341
342
343
class TestTreeReference(TestCase):
344
    
345
    def test_create(self):
346
        inv = Inventory('tree-root-123')
347
        inv.add(TreeReference('nested-id', 'nested', parent_id='tree-root-123',
348
                              revision='rev', reference_revision='rev2'))
1551.13.12 by Aaron Bentley
Fix encoding of 'already versioned' error
349
350
351
class TestEncoding(TestCase):
352
353
    def test_error_encoding(self):
354
        inv = Inventory('tree-root')
355
        inv.add(InventoryFile('a-id', u'\u1234', 'tree-root'))
356
        try:
357
            inv.add(InventoryFile('b-id', u'\u1234', 'tree-root'))
358
        except errors.BzrError, e:
359
            self.assertContainsRe(str(e), u'\u1234'.encode('utf-8'))
1551.13.13 by Aaron Bentley
Fail if BzrError not raised
360
        else:
361
            self.fail('BzrError not raised')