/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
1
# Copyright (C) 2008, 2009 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
4205.5.4 by Andrew Bennetts
Fix many tests in test_inventory_delta to actually test what they intended to test.
17
"""Tests for bzrlib.inventory_delta.
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
18
19
See doc/developer/inventory.txt for more information.
20
"""
21
22
from cStringIO import StringIO
23
24
from bzrlib import (
25
    errors,
26
    inventory,
27
    inventory_delta,
28
    )
29
from bzrlib.inventory import Inventory
30
from bzrlib.revision import NULL_REVISION
31
from bzrlib.tests import TestCase
32
33
### DO NOT REFLOW THESE TEXTS. NEW LINES ARE SIGNIFICANT. ###
34
empty_lines = """format: bzr inventory delta v1 (bzr 1.14)
35
parent: null:
36
version: null:
37
versioned_root: true
38
tree_references: true
39
"""
40
41
root_only_lines = """format: bzr inventory delta v1 (bzr 1.14)
42
parent: null:
43
version: entry-version
44
versioned_root: true
45
tree_references: true
4205.5.3 by Andrew Bennetts
Include oldpath in the the serialised delta
46
None\x00/\x00an-id\x00\x00a@e\xc3\xa5ample.com--2004\x00dir
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
47
"""
48
49
50
root_change_lines = """format: bzr inventory delta v1 (bzr 1.14)
51
parent: entry-version
52
version: changed-root
53
versioned_root: true
54
tree_references: true
55
/\x00an-id\x00\x00different-version\x00dir
56
"""
57
58
corrupt_parent_lines = """format: bzr inventory delta v1 (bzr 1.14)
59
parent: entry-version
60
version: changed-root
61
versioned_root: false
62
tree_references: false
63
/\x00an-id\x00\x00different-version\x00dir
64
"""
65
66
root_only_unversioned = """format: bzr inventory delta v1 (bzr 1.14)
67
parent: null:
68
version: entry-version
69
versioned_root: false
70
tree_references: false
4205.5.3 by Andrew Bennetts
Include oldpath in the the serialised delta
71
None\x00/\x00TREE_ROOT\x00\x00null:\x00dir
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
72
"""
73
74
reference_lines = """format: bzr inventory delta v1 (bzr 1.14)
75
parent: null:
76
version: entry-version
77
versioned_root: true
78
tree_references: true
4205.5.3 by Andrew Bennetts
Include oldpath in the the serialised delta
79
None\x00/\x00TREE_ROOT\x00\x00a@e\xc3\xa5ample.com--2004\x00dir
80
None\x00/foo\x00id\x00TREE_ROOT\x00changed\x00tree\x00subtree-version
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
81
"""
82
83
change_tree_lines = """format: bzr inventory delta v1 (bzr 1.14)
84
parent: entry-version
85
version: change-tree
86
versioned_root: false
87
tree_references: false
88
/foo\x00id\x00TREE_ROOT\x00changed-twice\x00tree\x00subtree-version2
89
"""
90
91
4205.5.5 by Andrew Bennetts
Reorganise tests a little.
92
class TestDeserialization(TestCase):
93
    """Test InventoryDeltaSerializer.parse_text_bytes."""
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
94
95
    def test_parse_no_bytes(self):
4476.3.4 by Andrew Bennetts
Network serialisation, and most tests passing with InterDifferingSerializer commented out.
96
        serializer = inventory_delta.InventoryDeltaSerializer()
4205.5.4 by Andrew Bennetts
Fix many tests in test_inventory_delta to actually test what they intended to test.
97
        err = self.assertRaises(
98
            errors.BzrError, serializer.parse_text_bytes, '')
4476.3.26 by Andrew Bennetts
Stricter (de)serialization of leading slashes in paths in inventory deltas.
99
        self.assertContainsRe(str(err), 'last line not empty')
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
100
101
    def test_parse_bad_format(self):
4476.3.4 by Andrew Bennetts
Network serialisation, and most tests passing with InterDifferingSerializer commented out.
102
        serializer = inventory_delta.InventoryDeltaSerializer()
4205.5.4 by Andrew Bennetts
Fix many tests in test_inventory_delta to actually test what they intended to test.
103
        err = self.assertRaises(errors.BzrError,
104
            serializer.parse_text_bytes, 'format: foo\n')
105
        self.assertContainsRe(str(err), 'unknown format')
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
106
107
    def test_parse_no_parent(self):
4476.3.4 by Andrew Bennetts
Network serialisation, and most tests passing with InterDifferingSerializer commented out.
108
        serializer = inventory_delta.InventoryDeltaSerializer()
4205.5.4 by Andrew Bennetts
Fix many tests in test_inventory_delta to actually test what they intended to test.
109
        err = self.assertRaises(errors.BzrError,
110
            serializer.parse_text_bytes,
111
            'format: bzr inventory delta v1 (bzr 1.14)\n')
112
        self.assertContainsRe(str(err), 'missing parent: marker')
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
113
114
    def test_parse_no_version(self):
4476.3.4 by Andrew Bennetts
Network serialisation, and most tests passing with InterDifferingSerializer commented out.
115
        serializer = inventory_delta.InventoryDeltaSerializer()
4205.5.4 by Andrew Bennetts
Fix many tests in test_inventory_delta to actually test what they intended to test.
116
        err = self.assertRaises(errors.BzrError,
117
            serializer.parse_text_bytes,
118
            'format: bzr inventory delta v1 (bzr 1.14)\n'
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
119
            'parent: null:\n')
4205.5.4 by Andrew Bennetts
Fix many tests in test_inventory_delta to actually test what they intended to test.
120
        self.assertContainsRe(str(err), 'missing version: marker')
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
121
            
122
    def test_parse_duplicate_key_errors(self):
4476.3.4 by Andrew Bennetts
Network serialisation, and most tests passing with InterDifferingSerializer commented out.
123
        serializer = inventory_delta.InventoryDeltaSerializer()
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
124
        double_root_lines = \
4205.5.4 by Andrew Bennetts
Fix many tests in test_inventory_delta to actually test what they intended to test.
125
"""format: bzr inventory delta v1 (bzr 1.14)
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
126
parent: null:
127
version: null:
4205.5.4 by Andrew Bennetts
Fix many tests in test_inventory_delta to actually test what they intended to test.
128
versioned_root: true
129
tree_references: true
130
None\x00/\x00an-id\x00\x00a@e\xc3\xa5ample.com--2004\x00dir\x00\x00
131
None\x00/\x00an-id\x00\x00a@e\xc3\xa5ample.com--2004\x00dir\x00\x00
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
132
"""
4205.5.4 by Andrew Bennetts
Fix many tests in test_inventory_delta to actually test what they intended to test.
133
        err = self.assertRaises(errors.BzrError,
134
            serializer.parse_text_bytes, double_root_lines)
135
        self.assertContainsRe(str(err), 'duplicate file id')
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
136
137
    def test_parse_versioned_root_only(self):
4476.3.4 by Andrew Bennetts
Network serialisation, and most tests passing with InterDifferingSerializer commented out.
138
        serializer = inventory_delta.InventoryDeltaSerializer()
139
        serializer.require_flags(versioned_root=True, tree_references=True)
4205.5.4 by Andrew Bennetts
Fix many tests in test_inventory_delta to actually test what they intended to test.
140
        parse_result = serializer.parse_text_bytes(root_only_lines)
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
141
        expected_entry = inventory.make_entry(
142
            'directory', u'', None, 'an-id')
143
        expected_entry.revision = 'a@e\xc3\xa5ample.com--2004'
144
        self.assertEqual(
4476.3.26 by Andrew Bennetts
Stricter (de)serialization of leading slashes in paths in inventory deltas.
145
            ('null:', 'entry-version', True, True,
146
             [(None, '', 'an-id', expected_entry)]),
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
147
            parse_result)
148
149
    def test_parse_special_revid_not_valid_last_mod(self):
4476.3.4 by Andrew Bennetts
Network serialisation, and most tests passing with InterDifferingSerializer commented out.
150
        serializer = inventory_delta.InventoryDeltaSerializer()
151
        serializer.require_flags(versioned_root=False, tree_references=True)
4205.5.4 by Andrew Bennetts
Fix many tests in test_inventory_delta to actually test what they intended to test.
152
        root_only_lines = """format: bzr inventory delta v1 (bzr 1.14)
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
153
parent: null:
154
version: null:
4205.5.4 by Andrew Bennetts
Fix many tests in test_inventory_delta to actually test what they intended to test.
155
versioned_root: false
156
tree_references: true
157
None\x00/\x00TREE_ROOT\x00\x00null:\x00dir\x00\x00
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
158
"""
4205.5.4 by Andrew Bennetts
Fix many tests in test_inventory_delta to actually test what they intended to test.
159
        err = self.assertRaises(errors.BzrError,
160
            serializer.parse_text_bytes, root_only_lines)
161
        self.assertContainsRe(str(err), 'special revisionid found')
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
162
163
    def test_parse_versioned_root_versioned_disabled(self):
4476.3.4 by Andrew Bennetts
Network serialisation, and most tests passing with InterDifferingSerializer commented out.
164
        serializer = inventory_delta.InventoryDeltaSerializer()
165
        serializer.require_flags(versioned_root=False, tree_references=True)
4205.5.4 by Andrew Bennetts
Fix many tests in test_inventory_delta to actually test what they intended to test.
166
        root_only_lines = """format: bzr inventory delta v1 (bzr 1.14)
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
167
parent: null:
168
version: null:
4205.5.4 by Andrew Bennetts
Fix many tests in test_inventory_delta to actually test what they intended to test.
169
versioned_root: false
170
tree_references: true
171
None\x00/\x00TREE_ROOT\x00\x00a@e\xc3\xa5ample.com--2004\x00dir\x00\x00
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
172
"""
4205.5.4 by Andrew Bennetts
Fix many tests in test_inventory_delta to actually test what they intended to test.
173
        err = self.assertRaises(errors.BzrError,
174
            serializer.parse_text_bytes, root_only_lines)
175
        self.assertContainsRe(str(err), 'Versioned root found')
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
176
177
    def test_parse_unique_root_id_root_versioned_disabled(self):
4476.3.4 by Andrew Bennetts
Network serialisation, and most tests passing with InterDifferingSerializer commented out.
178
        serializer = inventory_delta.InventoryDeltaSerializer()
179
        serializer.require_flags(versioned_root=False, tree_references=True)
4205.5.4 by Andrew Bennetts
Fix many tests in test_inventory_delta to actually test what they intended to test.
180
        root_only_lines = """format: bzr inventory delta v1 (bzr 1.14)
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
181
parent: null:
182
version: null:
4205.5.4 by Andrew Bennetts
Fix many tests in test_inventory_delta to actually test what they intended to test.
183
versioned_root: false
184
tree_references: true
185
None\x00/\x00an-id\x00\x00null:\x00dir\x00\x00
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
186
"""
4205.5.4 by Andrew Bennetts
Fix many tests in test_inventory_delta to actually test what they intended to test.
187
        err = self.assertRaises(errors.BzrError,
188
            serializer.parse_text_bytes, root_only_lines)
189
        self.assertContainsRe(str(err), 'Versioned root found')
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
190
191
    def test_parse_unversioned_root_versioning_enabled(self):
4476.3.4 by Andrew Bennetts
Network serialisation, and most tests passing with InterDifferingSerializer commented out.
192
        serializer = inventory_delta.InventoryDeltaSerializer()
193
        serializer.require_flags(versioned_root=True, tree_references=True)
4205.5.4 by Andrew Bennetts
Fix many tests in test_inventory_delta to actually test what they intended to test.
194
        err = self.assertRaises(errors.BzrError,
195
            serializer.parse_text_bytes, root_only_unversioned)
196
        self.assertContainsRe(
197
            str(err), 'serialized versioned_root flag is wrong: False')
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
198
199
    def test_parse_tree_when_disabled(self):
4476.3.4 by Andrew Bennetts
Network serialisation, and most tests passing with InterDifferingSerializer commented out.
200
        serializer = inventory_delta.InventoryDeltaSerializer()
201
        serializer.require_flags(versioned_root=True, tree_references=False)
4205.5.4 by Andrew Bennetts
Fix many tests in test_inventory_delta to actually test what they intended to test.
202
        err = self.assertRaises(errors.BzrError,
203
            serializer.parse_text_bytes, reference_lines)
204
        self.assertContainsRe(
205
            str(err), 'serialized tree_references flag is wrong: True')
206
4476.3.4 by Andrew Bennetts
Network serialisation, and most tests passing with InterDifferingSerializer commented out.
207
    def test_parse_tree_when_header_disallows(self):
208
        # A deserializer that allows tree_references to be set or unset.
209
        serializer = inventory_delta.InventoryDeltaSerializer()
210
        # A serialised inventory delta with a header saying no tree refs, but
211
        # that has a tree ref in its content.
212
        lines = """format: bzr inventory delta v1 (bzr 1.14)
213
parent: null:
214
version: entry-version
215
versioned_root: false
216
tree_references: false
217
None\x00/foo\x00id\x00TREE_ROOT\x00changed\x00tree\x00subtree-version
218
"""
219
        err = self.assertRaises(errors.BzrError,
220
            serializer.parse_text_bytes, lines)
221
        self.assertContainsRe(str(err), 'Tree reference found')
222
223
    def test_parse_versioned_root_when_header_disallows(self):
224
        # A deserializer that allows tree_references to be set or unset.
225
        serializer = inventory_delta.InventoryDeltaSerializer()
226
        # A serialised inventory delta with a header saying no tree refs, but
227
        # that has a tree ref in its content.
228
        lines = """format: bzr inventory delta v1 (bzr 1.14)
229
parent: null:
230
version: entry-version
231
versioned_root: false
232
tree_references: false
233
None\x00/\x00TREE_ROOT\x00\x00a@e\xc3\xa5ample.com--2004\x00dir
234
"""
235
        err = self.assertRaises(errors.BzrError,
236
            serializer.parse_text_bytes, lines)
237
        self.assertContainsRe(str(err), 'Versioned root found')
238
4476.3.26 by Andrew Bennetts
Stricter (de)serialization of leading slashes in paths in inventory deltas.
239
    def test_parse_last_line_not_empty(self):
240
        """newpath must start with / if it is not None."""
241
        # Trim the trailing newline from a valid serialization
242
        lines = root_only_lines[:-1]
243
        serializer = inventory_delta.InventoryDeltaSerializer()
244
        err = self.assertRaises(errors.BzrError,
245
            serializer.parse_text_bytes, lines)
246
        self.assertContainsRe(str(err), 'last line not empty')
247
248
    def test_parse_invalid_newpath(self):
249
        """newpath must start with / if it is not None."""
250
        lines = empty_lines
251
        lines += "None\x00bad\x00TREE_ROOT\x00\x00version\x00dir\n"
252
        serializer = inventory_delta.InventoryDeltaSerializer()
253
        err = self.assertRaises(errors.BzrError,
254
            serializer.parse_text_bytes, lines)
255
        self.assertContainsRe(str(err), 'newpath invalid')
256
257
    def test_parse_invalid_oldpath(self):
258
        """oldpath must start with / if it is not None."""
259
        lines = root_only_lines
260
        lines += "bad\x00/new\x00file-id\x00\x00version\x00dir\n"
261
        serializer = inventory_delta.InventoryDeltaSerializer()
262
        err = self.assertRaises(errors.BzrError,
263
            serializer.parse_text_bytes, lines)
264
        self.assertContainsRe(str(err), 'oldpath invalid')
4476.3.4 by Andrew Bennetts
Network serialisation, and most tests passing with InterDifferingSerializer commented out.
265
4205.5.4 by Andrew Bennetts
Fix many tests in test_inventory_delta to actually test what they intended to test.
266
4205.5.5 by Andrew Bennetts
Reorganise tests a little.
267
class TestSerialization(TestCase):
4205.5.4 by Andrew Bennetts
Fix many tests in test_inventory_delta to actually test what they intended to test.
268
    """Tests for InventoryDeltaSerializer.delta_to_lines."""
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
269
4205.5.5 by Andrew Bennetts
Reorganise tests a little.
270
    def test_empty_delta_to_lines(self):
271
        old_inv = Inventory(None)
272
        new_inv = Inventory(None)
273
        delta = new_inv._make_delta(old_inv)
4476.3.4 by Andrew Bennetts
Network serialisation, and most tests passing with InterDifferingSerializer commented out.
274
        serializer = inventory_delta.InventoryDeltaSerializer()
275
        serializer.require_flags(True, True)
4205.5.5 by Andrew Bennetts
Reorganise tests a little.
276
        self.assertEqual(StringIO(empty_lines).readlines(),
277
            serializer.delta_to_lines(NULL_REVISION, NULL_REVISION, delta))
278
279
    def test_root_only_to_lines(self):
280
        old_inv = Inventory(None)
281
        new_inv = Inventory(None)
282
        root = new_inv.make_entry('directory', '', None, 'an-id')
283
        root.revision = 'a@e\xc3\xa5ample.com--2004'
284
        new_inv.add(root)
285
        delta = new_inv._make_delta(old_inv)
4476.3.4 by Andrew Bennetts
Network serialisation, and most tests passing with InterDifferingSerializer commented out.
286
        serializer = inventory_delta.InventoryDeltaSerializer()
287
        serializer.require_flags(versioned_root=True, tree_references=True)
4205.5.5 by Andrew Bennetts
Reorganise tests a little.
288
        self.assertEqual(StringIO(root_only_lines).readlines(),
289
            serializer.delta_to_lines(NULL_REVISION, 'entry-version', delta))
290
291
    def test_unversioned_root(self):
292
        old_inv = Inventory(None)
293
        new_inv = Inventory(None)
294
        root = new_inv.make_entry('directory', '', None, 'TREE_ROOT')
295
        new_inv.add(root)
296
        delta = new_inv._make_delta(old_inv)
4476.3.4 by Andrew Bennetts
Network serialisation, and most tests passing with InterDifferingSerializer commented out.
297
        serializer = inventory_delta.InventoryDeltaSerializer()
298
        serializer.require_flags(False, False)
4205.5.5 by Andrew Bennetts
Reorganise tests a little.
299
        self.assertEqual(StringIO(root_only_unversioned).readlines(),
300
            serializer.delta_to_lines(NULL_REVISION, 'entry-version', delta))
301
302
    def test_unversioned_non_root_errors(self):
303
        old_inv = Inventory(None)
304
        new_inv = Inventory(None)
305
        root = new_inv.make_entry('directory', '', None, 'TREE_ROOT')
306
        root.revision = 'a@e\xc3\xa5ample.com--2004'
307
        new_inv.add(root)
308
        non_root = new_inv.make_entry('directory', 'foo', root.file_id, 'id')
309
        new_inv.add(non_root)
310
        delta = new_inv._make_delta(old_inv)
4476.3.4 by Andrew Bennetts
Network serialisation, and most tests passing with InterDifferingSerializer commented out.
311
        serializer = inventory_delta.InventoryDeltaSerializer()
312
        serializer.require_flags(versioned_root=True, tree_references=True)
4205.5.5 by Andrew Bennetts
Reorganise tests a little.
313
        err = self.assertRaises(errors.BzrError,
314
            serializer.delta_to_lines, NULL_REVISION, 'entry-version', delta)
315
        self.assertEqual(str(err), 'no version for fileid id')
316
317
    def test_richroot_unversioned_root_errors(self):
318
        old_inv = Inventory(None)
319
        new_inv = Inventory(None)
320
        root = new_inv.make_entry('directory', '', None, 'TREE_ROOT')
321
        new_inv.add(root)
322
        delta = new_inv._make_delta(old_inv)
4476.3.4 by Andrew Bennetts
Network serialisation, and most tests passing with InterDifferingSerializer commented out.
323
        serializer = inventory_delta.InventoryDeltaSerializer()
324
        serializer.require_flags(versioned_root=True, tree_references=True)
4205.5.5 by Andrew Bennetts
Reorganise tests a little.
325
        err = self.assertRaises(errors.BzrError,
326
            serializer.delta_to_lines, NULL_REVISION, 'entry-version', delta)
327
        self.assertEqual(str(err), 'no version for fileid TREE_ROOT')
328
329
    def test_nonrichroot_versioned_root_errors(self):
330
        old_inv = Inventory(None)
331
        new_inv = Inventory(None)
332
        root = new_inv.make_entry('directory', '', None, 'TREE_ROOT')
333
        root.revision = 'a@e\xc3\xa5ample.com--2004'
334
        new_inv.add(root)
335
        delta = new_inv._make_delta(old_inv)
4476.3.4 by Andrew Bennetts
Network serialisation, and most tests passing with InterDifferingSerializer commented out.
336
        serializer = inventory_delta.InventoryDeltaSerializer()
337
        serializer.require_flags(versioned_root=False, tree_references=True)
4205.5.5 by Andrew Bennetts
Reorganise tests a little.
338
        err = self.assertRaises(errors.BzrError,
339
            serializer.delta_to_lines, NULL_REVISION, 'entry-version', delta)
340
        self.assertEqual(str(err), 'Version present for / in TREE_ROOT')
341
342
    def test_nonrichroot_non_TREE_ROOT_id_errors(self):
343
        old_inv = Inventory(None)
344
        new_inv = Inventory(None)
345
        root = new_inv.make_entry('directory', '', None, 'my-rich-root-id')
346
        new_inv.add(root)
347
        delta = new_inv._make_delta(old_inv)
4476.3.4 by Andrew Bennetts
Network serialisation, and most tests passing with InterDifferingSerializer commented out.
348
        serializer = inventory_delta.InventoryDeltaSerializer()
349
        serializer.require_flags(versioned_root=False, tree_references=True)
4205.5.5 by Andrew Bennetts
Reorganise tests a little.
350
        err = self.assertRaises(errors.BzrError,
351
            serializer.delta_to_lines, NULL_REVISION, 'entry-version', delta)
352
        self.assertEqual(
353
            str(err), 'file_id my-rich-root-id is not TREE_ROOT for /')
354
355
    def test_unknown_kind_errors(self):
356
        old_inv = Inventory(None)
357
        new_inv = Inventory(None)
358
        root = new_inv.make_entry('directory', '', None, 'my-rich-root-id')
359
        root.revision = 'changed'
360
        new_inv.add(root)
361
        non_root = new_inv.make_entry('directory', 'foo', root.file_id, 'id')
362
        non_root.revision = 'changed'
363
        non_root.kind = 'strangelove'
364
        new_inv.add(non_root)
365
        delta = new_inv._make_delta(old_inv)
4476.3.4 by Andrew Bennetts
Network serialisation, and most tests passing with InterDifferingSerializer commented out.
366
        serializer = inventory_delta.InventoryDeltaSerializer()
367
        serializer.require_flags(True, True)
4205.5.5 by Andrew Bennetts
Reorganise tests a little.
368
        # we expect keyerror because there is little value wrapping this.
369
        # This test aims to prove that it errors more than how it errors.
370
        err = self.assertRaises(KeyError,
371
            serializer.delta_to_lines, NULL_REVISION, 'entry-version', delta)
372
        self.assertEqual(('strangelove',), err.args)
373
374
    def test_tree_reference_disabled(self):
375
        old_inv = Inventory(None)
376
        new_inv = Inventory(None)
377
        root = new_inv.make_entry('directory', '', None, 'TREE_ROOT')
378
        root.revision = 'a@e\xc3\xa5ample.com--2004'
379
        new_inv.add(root)
380
        non_root = new_inv.make_entry(
381
            'tree-reference', 'foo', root.file_id, 'id')
382
        non_root.revision = 'changed'
383
        non_root.reference_revision = 'subtree-version'
384
        new_inv.add(non_root)
385
        delta = new_inv._make_delta(old_inv)
4476.3.4 by Andrew Bennetts
Network serialisation, and most tests passing with InterDifferingSerializer commented out.
386
        serializer = inventory_delta.InventoryDeltaSerializer()
387
        serializer.require_flags(versioned_root=True, tree_references=False)
4205.5.5 by Andrew Bennetts
Reorganise tests a little.
388
        # we expect keyerror because there is little value wrapping this.
389
        # This test aims to prove that it errors more than how it errors.
390
        err = self.assertRaises(KeyError,
391
            serializer.delta_to_lines, NULL_REVISION, 'entry-version', delta)
392
        self.assertEqual(('tree-reference',), err.args)
393
394
    def test_tree_reference_enabled(self):
395
        old_inv = Inventory(None)
396
        new_inv = Inventory(None)
397
        root = new_inv.make_entry('directory', '', None, 'TREE_ROOT')
398
        root.revision = 'a@e\xc3\xa5ample.com--2004'
399
        new_inv.add(root)
400
        non_root = new_inv.make_entry(
401
            'tree-reference', 'foo', root.file_id, 'id')
402
        non_root.revision = 'changed'
403
        non_root.reference_revision = 'subtree-version'
404
        new_inv.add(non_root)
405
        delta = new_inv._make_delta(old_inv)
4476.3.4 by Andrew Bennetts
Network serialisation, and most tests passing with InterDifferingSerializer commented out.
406
        serializer = inventory_delta.InventoryDeltaSerializer()
407
        serializer.require_flags(versioned_root=True, tree_references=True)
4205.5.5 by Andrew Bennetts
Reorganise tests a little.
408
        self.assertEqual(StringIO(reference_lines).readlines(),
409
            serializer.delta_to_lines(NULL_REVISION, 'entry-version', delta))
410
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
411
    def test_to_inventory_root_id_versioned_not_permitted(self):
4476.3.26 by Andrew Bennetts
Stricter (de)serialization of leading slashes in paths in inventory deltas.
412
        root_entry = inventory.make_entry('directory', '', None, 'TREE_ROOT')
413
        root_entry.revision = 'some-version'
414
        delta = [(None, '', 'TREE_ROOT', root_entry)]
4476.3.4 by Andrew Bennetts
Network serialisation, and most tests passing with InterDifferingSerializer commented out.
415
        serializer = inventory_delta.InventoryDeltaSerializer()
416
        serializer.require_flags(versioned_root=False, tree_references=True)
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
417
        self.assertRaises(
418
            errors.BzrError, serializer.delta_to_lines, 'old-version',
419
            'new-version', delta)
420
421
    def test_to_inventory_root_id_not_versioned(self):
4476.3.26 by Andrew Bennetts
Stricter (de)serialization of leading slashes in paths in inventory deltas.
422
        delta = [(None, '', 'an-id', inventory.make_entry(
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
423
            'directory', '', None, 'an-id'))]
4476.3.4 by Andrew Bennetts
Network serialisation, and most tests passing with InterDifferingSerializer commented out.
424
        serializer = inventory_delta.InventoryDeltaSerializer()
425
        serializer.require_flags(versioned_root=True, tree_references=True)
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
426
        self.assertRaises(
427
            errors.BzrError, serializer.delta_to_lines, 'old-version',
428
            'new-version', delta)
429
430
    def test_to_inventory_has_tree_not_meant_to(self):
431
        make_entry = inventory.make_entry
432
        tree_ref = make_entry('tree-reference', 'foo', 'changed-in', 'ref-id')
433
        tree_ref.reference_revision = 'ref-revision'
434
        delta = [
4476.3.26 by Andrew Bennetts
Stricter (de)serialization of leading slashes in paths in inventory deltas.
435
            (None, '', 'an-id',
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
436
             make_entry('directory', '', 'changed-in', 'an-id')),
4476.3.26 by Andrew Bennetts
Stricter (de)serialization of leading slashes in paths in inventory deltas.
437
            (None, 'foo', 'ref-id', tree_ref)
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
438
            # a file that followed the root move
439
            ]
4476.3.4 by Andrew Bennetts
Network serialisation, and most tests passing with InterDifferingSerializer commented out.
440
        serializer = inventory_delta.InventoryDeltaSerializer()
441
        serializer.require_flags(versioned_root=True, tree_references=True)
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
442
        self.assertRaises(errors.BzrError, serializer.delta_to_lines,
443
            'old-version', 'new-version', delta)
444
445
    def test_to_inventory_torture(self):
446
        def make_entry(kind, name, parent_id, file_id, **attrs):
447
            entry = inventory.make_entry(kind, name, parent_id, file_id)
448
            for name, value in attrs.items():
449
                setattr(entry, name, value)
450
            return entry
451
        # this delta is crafted to have all the following:
452
        # - deletes
453
        # - renamed roots
454
        # - deep dirs
455
        # - files moved after parent dir was renamed
456
        # - files with and without exec bit
457
        delta = [
458
            # new root:
459
            (None, '', 'new-root-id',
460
                make_entry('directory', '', None, 'new-root-id',
461
                    revision='changed-in')),
462
            # an old root:
463
            ('', 'old-root', 'TREE_ROOT',
464
                make_entry('directory', 'subdir-now', 'new-root-id',
465
                'TREE_ROOT', revision='moved-root')),
466
            # a file that followed the root move
467
            ('under-old-root', 'old-root/under-old-root', 'moved-id',
468
                make_entry('file', 'under-old-root', 'TREE_ROOT', 'moved-id',
469
                   revision='old-rev', executable=False, text_size=30,
470
                   text_sha1='some-sha')),
471
            # a deleted path
472
            ('old-file', None, 'deleted-id', None),
473
            # a tree reference moved to the new root
474
            ('ref', 'ref', 'ref-id',
475
                make_entry('tree-reference', 'ref', 'new-root-id', 'ref-id',
476
                    reference_revision='tree-reference-id',
477
                    revision='new-rev')),
478
            # a symlink now in a deep dir
479
            ('dir/link', 'old-root/dir/link', 'link-id',
480
                make_entry('symlink', 'link', 'deep-id', 'link-id',
481
                   symlink_target='target', revision='new-rev')),
482
            # a deep dir
483
            ('dir', 'old-root/dir', 'deep-id',
484
                make_entry('directory', 'dir', 'TREE_ROOT', 'deep-id',
485
                    revision='new-rev')),
486
            # a file with an exec bit set
487
            (None, 'configure', 'exec-id',
488
                make_entry('file', 'configure', 'new-root-id', 'exec-id',
489
                   executable=True, text_size=30, text_sha1='some-sha',
490
                   revision='old-rev')),
491
            ]
4476.3.4 by Andrew Bennetts
Network serialisation, and most tests passing with InterDifferingSerializer commented out.
492
        serializer = inventory_delta.InventoryDeltaSerializer()
493
        serializer.require_flags(versioned_root=True, tree_references=True)
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
494
        lines = serializer.delta_to_lines(NULL_REVISION, 'something', delta)
495
        expected = """format: bzr inventory delta v1 (bzr 1.14)
496
parent: null:
497
version: something
498
versioned_root: true
499
tree_references: true
4205.5.3 by Andrew Bennetts
Include oldpath in the the serialised delta
500
/\x00/old-root\x00TREE_ROOT\x00new-root-id\x00moved-root\x00dir
501
/dir\x00/old-root/dir\x00deep-id\x00TREE_ROOT\x00new-rev\x00dir
502
/dir/link\x00/old-root/dir/link\x00link-id\x00deep-id\x00new-rev\x00link\x00target
503
/old-file\x00None\x00deleted-id\x00\x00null:\x00deleted\x00\x00
504
/ref\x00/ref\x00ref-id\x00new-root-id\x00new-rev\x00tree\x00tree-reference-id
505
/under-old-root\x00/old-root/under-old-root\x00moved-id\x00TREE_ROOT\x00old-rev\x00file\x0030\x00\x00some-sha
506
None\x00/\x00new-root-id\x00\x00changed-in\x00dir
507
None\x00/configure\x00exec-id\x00new-root-id\x00old-rev\x00file\x0030\x00Y\x00some-sha
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
508
"""
4205.5.7 by Andrew Bennetts
Fix nits in spelling and naming.
509
        serialized = ''.join(lines)
510
        self.assertIsInstance(serialized, str)
511
        self.assertEqual(expected, serialized)
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
512
513
514
class TestContent(TestCase):
4205.5.5 by Andrew Bennetts
Reorganise tests a little.
515
    """Test serialization of the content part of a line."""
4205.5.1 by Andrew Bennetts
Initial stab at adapting Robert's journalled_inventory serialisation into inventory_delta serialisation.
516
517
    def test_dir(self):
518
        entry = inventory.make_entry('directory', 'a dir', None)
519
        self.assertEqual('dir', inventory_delta._directory_content(entry))
520
521
    def test_file_0_short_sha(self):
522
        file_entry = inventory.make_entry('file', 'a file', None, 'file-id')
523
        file_entry.text_sha1 = ''
524
        file_entry.text_size = 0
525
        self.assertEqual('file\x000\x00\x00',
526
            inventory_delta._file_content(file_entry))
527
528
    def test_file_10_foo(self):
529
        file_entry = inventory.make_entry('file', 'a file', None, 'file-id')
530
        file_entry.text_sha1 = 'foo'
531
        file_entry.text_size = 10
532
        self.assertEqual('file\x0010\x00\x00foo',
533
            inventory_delta._file_content(file_entry))
534
535
    def test_file_executable(self):
536
        file_entry = inventory.make_entry('file', 'a file', None, 'file-id')
537
        file_entry.executable = True
538
        file_entry.text_sha1 = 'foo'
539
        file_entry.text_size = 10
540
        self.assertEqual('file\x0010\x00Y\x00foo',
541
            inventory_delta._file_content(file_entry))
542
543
    def test_file_without_size(self):
544
        file_entry = inventory.make_entry('file', 'a file', None, 'file-id')
545
        file_entry.text_sha1 = 'foo'
546
        self.assertRaises(errors.BzrError,
547
            inventory_delta._file_content, file_entry)
548
549
    def test_file_without_sha1(self):
550
        file_entry = inventory.make_entry('file', 'a file', None, 'file-id')
551
        file_entry.text_size = 10
552
        self.assertRaises(errors.BzrError,
553
            inventory_delta._file_content, file_entry)
554
555
    def test_link_empty_target(self):
556
        entry = inventory.make_entry('symlink', 'a link', None)
557
        entry.symlink_target = ''
558
        self.assertEqual('link\x00',
559
            inventory_delta._link_content(entry))
560
561
    def test_link_unicode_target(self):
562
        entry = inventory.make_entry('symlink', 'a link', None)
563
        entry.symlink_target = ' \xc3\xa5'.decode('utf8')
564
        self.assertEqual('link\x00 \xc3\xa5',
565
            inventory_delta._link_content(entry))
566
567
    def test_link_space_target(self):
568
        entry = inventory.make_entry('symlink', 'a link', None)
569
        entry.symlink_target = ' '
570
        self.assertEqual('link\x00 ',
571
            inventory_delta._link_content(entry))
572
573
    def test_link_no_target(self):
574
        entry = inventory.make_entry('symlink', 'a link', None)
575
        self.assertRaises(errors.BzrError,
576
            inventory_delta._link_content, entry)
577
578
    def test_reference_null(self):
579
        entry = inventory.make_entry('tree-reference', 'a tree', None)
580
        entry.reference_revision = NULL_REVISION
581
        self.assertEqual('tree\x00null:',
582
            inventory_delta._reference_content(entry))
583
584
    def test_reference_revision(self):
585
        entry = inventory.make_entry('tree-reference', 'a tree', None)
586
        entry.reference_revision = 'foo@\xc3\xa5b-lah'
587
        self.assertEqual('tree\x00foo@\xc3\xa5b-lah',
588
            inventory_delta._reference_content(entry))
589
590
    def test_reference_no_reference(self):
591
        entry = inventory.make_entry('tree-reference', 'a tree', None)
592
        self.assertRaises(errors.BzrError,
593
            inventory_delta._reference_content, entry)