/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2729.2.5 by Martin Pool
Move per-inventory tests from test_inv to tests.inventory_implementations
1
# Copyright (C) 2005, 2006, 2007 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
2729.2.5 by Martin Pool
Move per-inventory tests from test_inv to tests.inventory_implementations
17
1830.3.5 by John Arbash Meinel
make_entry refuses to create non-normalized entries.
18
from bzrlib import errors, inventory, osutils
3735.2.9 by Robert Collins
Get a working chk_map using inventory implementation bootstrapped.
19
from bzrlib.inventory import (CHKInventory, Inventory, ROOT_ID, InventoryFile,
2100.3.1 by Aaron Bentley
Start roundtripping tree-reference entries
20
    InventoryDirectory, InventoryEntry, TreeReference)
3735.2.9 by Robert Collins
Get a working chk_map using inventory implementation bootstrapped.
21
from bzrlib.tests import TestCase, TestCaseWithTransport
963 by Martin Pool
- add the start of a test for inventory file-id matching
22
969 by Martin Pool
- Add less-sucky is_within_any
23
1407 by Robert Collins
define some expected behaviour for inventory_entry.snapshot
24
class TestInventoryEntry(TestCase):
1399.1.2 by Robert Collins
push kind character creation into InventoryEntry and TreeEntry
25
26
    def test_file_kind_character(self):
1399.1.9 by Robert Collins
factor out file related logic from InventoryEntry to InventoryFile
27
        file = inventory.InventoryFile('123', 'hello.c', ROOT_ID)
1399.1.2 by Robert Collins
push kind character creation into InventoryEntry and TreeEntry
28
        self.assertEqual(file.kind_character(), '')
29
30
    def test_dir_kind_character(self):
1399.1.8 by Robert Collins
factor out inventory directory logic into 'InventoryDirectory' class
31
        dir = inventory.InventoryDirectory('123', 'hello.c', ROOT_ID)
1399.1.2 by Robert Collins
push kind character creation into InventoryEntry and TreeEntry
32
        self.assertEqual(dir.kind_character(), '/')
33
34
    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
35
        dir = inventory.InventoryLink('123', 'hello.c', ROOT_ID)
1399.1.2 by Robert Collins
push kind character creation into InventoryEntry and TreeEntry
36
        self.assertEqual(dir.kind_character(), '')
1399.1.3 by Robert Collins
move change detection for text and metadata from delta to entry.detect_changes
37
38
    def test_dir_detect_changes(self):
1399.1.8 by Robert Collins
factor out inventory directory logic into 'InventoryDirectory' class
39
        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
40
        left.text_sha1 = 123
41
        left.executable = True
42
        left.symlink_target='foo'
1399.1.8 by Robert Collins
factor out inventory directory logic into 'InventoryDirectory' class
43
        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
44
        right.text_sha1 = 321
45
        right.symlink_target='bar'
46
        self.assertEqual((False, False), left.detect_changes(right))
47
        self.assertEqual((False, False), right.detect_changes(left))
48
49
    def test_file_detect_changes(self):
1399.1.9 by Robert Collins
factor out file related logic from InventoryEntry to InventoryFile
50
        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
51
        left.text_sha1 = 123
1399.1.9 by Robert Collins
factor out file related logic from InventoryEntry to InventoryFile
52
        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
53
        right.text_sha1 = 123
54
        self.assertEqual((False, False), left.detect_changes(right))
55
        self.assertEqual((False, False), right.detect_changes(left))
56
        left.executable = True
57
        self.assertEqual((False, True), left.detect_changes(right))
58
        self.assertEqual((False, True), right.detect_changes(left))
59
        right.text_sha1 = 321
60
        self.assertEqual((True, True), left.detect_changes(right))
61
        self.assertEqual((True, True), right.detect_changes(left))
62
63
    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
64
        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
65
        left.text_sha1 = 123
66
        left.executable = True
67
        left.symlink_target='foo'
1399.1.10 by Robert Collins
remove kind from the InventoryEntry constructor - only child classes should be created now
68
        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
69
        right.text_sha1 = 321
70
        right.symlink_target='foo'
71
        self.assertEqual((False, False), left.detect_changes(right))
72
        self.assertEqual((False, False), right.detect_changes(left))
73
        left.symlink_target = 'different'
74
        self.assertEqual((True, False), left.detect_changes(right))
75
        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
76
1399.1.5 by Robert Collins
move checking whether an entry stores text into inventory.py from fetch,py
77
    def test_file_has_text(self):
1399.1.9 by Robert Collins
factor out file related logic from InventoryEntry to InventoryFile
78
        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
79
        self.failUnless(file.has_text())
80
81
    def test_directory_has_text(self):
1399.1.8 by Robert Collins
factor out inventory directory logic into 'InventoryDirectory' class
82
        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
83
        self.failIf(dir.has_text())
84
85
    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
86
        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
87
        self.failIf(link.has_text())
88
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)
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)
1399.1.4 by Robert Collins
move diff and symlink conditionals into inventory.py from diff.py
96
1830.3.5 by John Arbash Meinel
make_entry refuses to create non-normalized entries.
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
1668.1.5 by Martin Pool
[broken] fix up display of files changed by a commit
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)
3735.2.9 by Robert Collins
Get a working chk_map using inventory implementation bootstrapped.
169
170
171
class TestCHKInventory(TestCaseWithTransport):
172
    
173
    def get_chk_bytes(self):
174
        # The eassiest way to get a CHK store is a development3 repository and
175
        # then work with the chk_bytes attribute directly.
176
        repo = self.make_repository(".", format="development3")
177
        repo.lock_write()
178
        self.addCleanup(repo.unlock)
179
        repo.start_write_group()
180
        self.addCleanup(repo.abort_write_group)
181
        return repo.chk_bytes
182
183
    def read_bytes(self, chk_bytes, key):
184
        stream = chk_bytes.get_record_stream([key], 'unordered', True)
185
        return stream.next().get_bytes_as("fulltext")
186
187
    def test_deserialise_gives_CHKInventory(self):
188
        inv = Inventory()
189
        inv.revision_id = "revid"
190
        inv.root.revision = "rootrev"
191
        chk_bytes = self.get_chk_bytes()
192
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
193
        bytes = ''.join(chk_inv.to_lines())
194
        new_inv = CHKInventory.deserialise(chk_bytes, bytes, ("revid",))
195
        self.assertEqual("revid", new_inv.revision_id)
196
        self.assertEqual("directory", new_inv.root.kind)
197
        self.assertEqual(inv.root.file_id, new_inv.root.file_id)
198
        self.assertEqual(inv.root.parent_id, new_inv.root.parent_id)
199
        self.assertEqual(inv.root.name, new_inv.root.name)
200
        self.assertEqual("rootrev", new_inv.root.revision)
201
202
    def test_deserialise_wrong_revid(self):
203
        inv = Inventory()
204
        inv.revision_id = "revid"
205
        inv.root.revision = "rootrev"
206
        chk_bytes = self.get_chk_bytes()
207
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
208
        bytes = ''.join(chk_inv.to_lines())
209
        self.assertRaises(ValueError, CHKInventory.deserialise, chk_bytes,
210
            bytes, ("revid2",))
211
212
    def test_captures_rev_root_byid(self):
213
        inv = Inventory()
214
        inv.revision_id = "foo"
215
        inv.root.revision = "bar"
216
        chk_bytes = self.get_chk_bytes()
217
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
218
        self.assertEqual([
219
            'chkinventory:\n',
220
            'revision_id: foo\n',
221
            'root_id: TREE_ROOT\n',
3735.2.25 by Robert Collins
CHKInventory core tests passing.
222
            'id_to_entry: sha1:36219af8518a9bed1e52db58e99131db2a00b329\n',
3735.2.9 by Robert Collins
Get a working chk_map using inventory implementation bootstrapped.
223
            ],
224
            chk_inv.to_lines())
225
226
    def test_directory_children_on_demand(self):
227
        inv = Inventory()
228
        inv.revision_id = "revid"
229
        inv.root.revision = "rootrev"
230
        inv.add(InventoryFile("fileid", "file", inv.root.file_id))
231
        inv["fileid"].revision = "filerev"
232
        inv["fileid"].executable = True
233
        inv["fileid"].text_sha1 = "ffff"
234
        inv["fileid"].text_size = 1
235
        chk_bytes = self.get_chk_bytes()
236
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
237
        bytes = ''.join(chk_inv.to_lines())
238
        new_inv = CHKInventory.deserialise(chk_bytes, bytes, ("revid",))
239
        root_entry = new_inv[inv.root.file_id]
240
        self.assertEqual(None, root_entry._children)
241
        self.assertEqual(['file'], root_entry.children.keys())
242
        file_direct = new_inv["fileid"]
243
        file_found = root_entry.children['file']
244
        self.assertEqual(file_direct.kind, file_found.kind)
245
        self.assertEqual(file_direct.file_id, file_found.file_id)
246
        self.assertEqual(file_direct.parent_id, file_found.parent_id)
247
        self.assertEqual(file_direct.name, file_found.name)
248
        self.assertEqual(file_direct.revision, file_found.revision)
249
        self.assertEqual(file_direct.text_sha1, file_found.text_sha1)
250
        self.assertEqual(file_direct.text_size, file_found.text_size)
251
        self.assertEqual(file_direct.executable, file_found.executable)
252
253
    def test___iter__(self):
254
        inv = Inventory()
255
        inv.revision_id = "revid"
256
        inv.root.revision = "rootrev"
257
        inv.add(InventoryFile("fileid", "file", inv.root.file_id))
258
        inv["fileid"].revision = "filerev"
259
        inv["fileid"].executable = True
260
        inv["fileid"].text_sha1 = "ffff"
261
        inv["fileid"].text_size = 1
262
        chk_bytes = self.get_chk_bytes()
263
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
264
        bytes = ''.join(chk_inv.to_lines())
265
        new_inv = CHKInventory.deserialise(chk_bytes, bytes, ("revid",))
266
        fileids = list(new_inv.__iter__())
267
        fileids.sort()
268
        self.assertEqual([inv.root.file_id, "fileid"], fileids)
269
270
    def test__len__(self):
271
        inv = Inventory()
272
        inv.revision_id = "revid"
273
        inv.root.revision = "rootrev"
274
        inv.add(InventoryFile("fileid", "file", inv.root.file_id))
275
        inv["fileid"].revision = "filerev"
276
        inv["fileid"].executable = True
277
        inv["fileid"].text_sha1 = "ffff"
278
        inv["fileid"].text_size = 1
279
        chk_bytes = self.get_chk_bytes()
280
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
281
        self.assertEqual(2, len(chk_inv))
282
283
    def test___getitem__(self):
284
        inv = Inventory()
285
        inv.revision_id = "revid"
286
        inv.root.revision = "rootrev"
287
        inv.add(InventoryFile("fileid", "file", inv.root.file_id))
288
        inv["fileid"].revision = "filerev"
289
        inv["fileid"].executable = True
290
        inv["fileid"].text_sha1 = "ffff"
291
        inv["fileid"].text_size = 1
292
        chk_bytes = self.get_chk_bytes()
293
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
294
        bytes = ''.join(chk_inv.to_lines())
295
        new_inv = CHKInventory.deserialise(chk_bytes, bytes, ("revid",))
296
        root_entry = new_inv[inv.root.file_id]
297
        file_entry = new_inv["fileid"]
298
        self.assertEqual("directory", root_entry.kind)
299
        self.assertEqual(inv.root.file_id, root_entry.file_id)
300
        self.assertEqual(inv.root.parent_id, root_entry.parent_id)
301
        self.assertEqual(inv.root.name, root_entry.name)
302
        self.assertEqual("rootrev", root_entry.revision)
303
        self.assertEqual("file", file_entry.kind)
304
        self.assertEqual("fileid", file_entry.file_id)
305
        self.assertEqual(inv.root.file_id, file_entry.parent_id)
306
        self.assertEqual("file", file_entry.name)
307
        self.assertEqual("filerev", file_entry.revision)
308
        self.assertEqual("ffff", file_entry.text_sha1)
309
        self.assertEqual(1, file_entry.text_size)
310
        self.assertEqual(True, file_entry.executable)
311
312
    def test_has_id_true(self):
313
        inv = Inventory()
314
        inv.revision_id = "revid"
315
        inv.root.revision = "rootrev"
316
        inv.add(InventoryFile("fileid", "file", inv.root.file_id))
317
        inv["fileid"].revision = "filerev"
318
        inv["fileid"].executable = True
319
        inv["fileid"].text_sha1 = "ffff"
320
        inv["fileid"].text_size = 1
321
        chk_bytes = self.get_chk_bytes()
322
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
323
        self.assertTrue(chk_inv.has_id('fileid'))
324
        self.assertTrue(chk_inv.has_id(inv.root.file_id))
325
326
    def test_has_id_not(self):
327
        inv = Inventory()
328
        inv.revision_id = "revid"
329
        inv.root.revision = "rootrev"
330
        chk_bytes = self.get_chk_bytes()
331
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
332
        self.assertFalse(chk_inv.has_id('fileid'))
3735.2.10 by Robert Collins
Teach CHKInventory how to make a new inventory from an inventory delta.
333
3735.2.12 by Robert Collins
Implement commit-via-deltas for split inventory repositories.
334
    def test_id2path(self):
335
        inv = Inventory()
336
        inv.revision_id = "revid"
337
        inv.root.revision = "rootrev"
338
        direntry = InventoryDirectory("dirid", "dir", inv.root.file_id)
339
        fileentry = InventoryFile("fileid", "file", "dirid")
340
        inv.add(direntry)
341
        inv.add(fileentry)
342
        inv["fileid"].revision = "filerev"
343
        inv["fileid"].executable = True
344
        inv["fileid"].text_sha1 = "ffff"
345
        inv["fileid"].text_size = 1
346
        inv["dirid"].revision = "filerev"
347
        chk_bytes = self.get_chk_bytes()
348
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
349
        bytes = ''.join(chk_inv.to_lines())
350
        new_inv = CHKInventory.deserialise(chk_bytes, bytes, ("revid",))
351
        self.assertEqual('', new_inv.id2path(inv.root.file_id))
352
        self.assertEqual('dir', new_inv.id2path('dirid'))
353
        self.assertEqual('dir/file', new_inv.id2path('fileid'))
354
355
    def test_path2id(self):
356
        inv = Inventory()
357
        inv.revision_id = "revid"
358
        inv.root.revision = "rootrev"
359
        direntry = InventoryDirectory("dirid", "dir", inv.root.file_id)
360
        fileentry = InventoryFile("fileid", "file", "dirid")
361
        inv.add(direntry)
362
        inv.add(fileentry)
363
        inv["fileid"].revision = "filerev"
364
        inv["fileid"].executable = True
365
        inv["fileid"].text_sha1 = "ffff"
366
        inv["fileid"].text_size = 1
367
        inv["dirid"].revision = "filerev"
368
        chk_bytes = self.get_chk_bytes()
369
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
370
        bytes = ''.join(chk_inv.to_lines())
371
        new_inv = CHKInventory.deserialise(chk_bytes, bytes, ("revid",))
372
        self.assertEqual(inv.root.file_id, new_inv.path2id(''))
373
        self.assertEqual('dirid', new_inv.path2id('dir'))
374
        self.assertEqual('fileid', new_inv.path2id('dir/file'))
375
3735.2.10 by Robert Collins
Teach CHKInventory how to make a new inventory from an inventory delta.
376
    def test_create_by_apply_delta_empty_add_child(self):
377
        inv = Inventory()
378
        inv.revision_id = "revid"
379
        inv.root.revision = "rootrev"
380
        chk_bytes = self.get_chk_bytes()
381
        base_inv = CHKInventory.from_inventory(chk_bytes, inv)
382
        a_entry = InventoryFile("A-id", "A", inv.root.file_id)
383
        a_entry.revision = "filerev"
384
        a_entry.executable = True
385
        a_entry.text_sha1 = "ffff"
386
        a_entry.text_size = 1
387
        inv.add(a_entry)
388
        inv.revision_id = "expectedid"
389
        reference_inv = CHKInventory.from_inventory(chk_bytes, inv)
390
        delta = [(None, "A",  "A-id", a_entry)]
391
        new_inv = base_inv.create_by_apply_delta(delta, "expectedid")
392
        # new_inv should be the same as reference_inv.
393
        self.assertEqual(reference_inv.revision_id, new_inv.revision_id)
394
        self.assertEqual(reference_inv.root_id, new_inv.root_id)
395
        self.assertEqual(reference_inv.id_to_entry._root_node._key,
396
            new_inv.id_to_entry._root_node._key)