~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_bundle.py

Add source index to the index iteration API to allow mapping back to the origin of retrieved data.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2004, 2005, 2006, 2007 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
from cStringIO import StringIO
 
18
import os
 
19
import sys
 
20
import tempfile
 
21
 
 
22
from bzrlib import (
 
23
    bzrdir,
 
24
    errors,
 
25
    inventory,
 
26
    repository,
 
27
    revision as _mod_revision,
 
28
    treebuilder,
 
29
    )
 
30
from bzrlib.builtins import _merge_helper
 
31
from bzrlib.bzrdir import BzrDir
 
32
from bzrlib.bundle.apply_bundle import install_bundle, merge_bundle
 
33
from bzrlib.bundle.bundle_data import BundleTree
 
34
from bzrlib.bundle.serializer import write_bundle, read_bundle, v09, v4
 
35
from bzrlib.bundle.serializer.v08 import BundleSerializerV08
 
36
from bzrlib.bundle.serializer.v09 import BundleSerializerV09
 
37
from bzrlib.bundle.serializer.v4 import BundleSerializerV4
 
38
from bzrlib.branch import Branch
 
39
from bzrlib.diff import internal_diff
 
40
from bzrlib.errors import (BzrError, TestamentMismatch, NotABundle, BadBundle, 
 
41
                           NoSuchFile,)
 
42
from bzrlib.merge import Merge3Merger
 
43
from bzrlib.repofmt import knitrepo
 
44
from bzrlib.osutils import has_symlinks, sha_file
 
45
from bzrlib.tests import (TestCaseInTempDir, TestCaseWithTransport,
 
46
                          TestCase, TestSkipped, test_commit)
 
47
from bzrlib.transform import TreeTransform
 
48
from bzrlib.workingtree import WorkingTree
 
49
 
 
50
 
 
51
class MockTree(object):
 
52
    def __init__(self):
 
53
        from bzrlib.inventory import InventoryDirectory, ROOT_ID
 
54
        object.__init__(self)
 
55
        self.paths = {ROOT_ID: ""}
 
56
        self.ids = {"": ROOT_ID}
 
57
        self.contents = {}
 
58
        self.root = InventoryDirectory(ROOT_ID, '', None)
 
59
 
 
60
    inventory = property(lambda x:x)
 
61
 
 
62
    def __iter__(self):
 
63
        return self.paths.iterkeys()
 
64
 
 
65
    def __getitem__(self, file_id):
 
66
        if file_id == self.root.file_id:
 
67
            return self.root
 
68
        else:
 
69
            return self.make_entry(file_id, self.paths[file_id])
 
70
 
 
71
    def parent_id(self, file_id):
 
72
        parent_dir = os.path.dirname(self.paths[file_id])
 
73
        if parent_dir == "":
 
74
            return None
 
75
        return self.ids[parent_dir]
 
76
 
 
77
    def iter_entries(self):
 
78
        for path, file_id in self.ids.iteritems():
 
79
            yield path, self[file_id]
 
80
 
 
81
    def get_file_kind(self, file_id):
 
82
        if file_id in self.contents:
 
83
            kind = 'file'
 
84
        else:
 
85
            kind = 'directory'
 
86
        return kind
 
87
 
 
88
    def make_entry(self, file_id, path):
 
89
        from bzrlib.inventory import (InventoryEntry, InventoryFile
 
90
                                    , InventoryDirectory, InventoryLink)
 
91
        name = os.path.basename(path)
 
92
        kind = self.get_file_kind(file_id)
 
93
        parent_id = self.parent_id(file_id)
 
94
        text_sha_1, text_size = self.contents_stats(file_id)
 
95
        if kind == 'directory':
 
96
            ie = InventoryDirectory(file_id, name, parent_id)
 
97
        elif kind == 'file':
 
98
            ie = InventoryFile(file_id, name, parent_id)
 
99
        elif kind == 'symlink':
 
100
            ie = InventoryLink(file_id, name, parent_id)
 
101
        else:
 
102
            raise BzrError('unknown kind %r' % kind)
 
103
        ie.text_sha1 = text_sha_1
 
104
        ie.text_size = text_size
 
105
        return ie
 
106
 
 
107
    def add_dir(self, file_id, path):
 
108
        self.paths[file_id] = path
 
109
        self.ids[path] = file_id
 
110
    
 
111
    def add_file(self, file_id, path, contents):
 
112
        self.add_dir(file_id, path)
 
113
        self.contents[file_id] = contents
 
114
 
 
115
    def path2id(self, path):
 
116
        return self.ids.get(path)
 
117
 
 
118
    def id2path(self, file_id):
 
119
        return self.paths.get(file_id)
 
120
 
 
121
    def has_id(self, file_id):
 
122
        return self.id2path(file_id) is not None
 
123
 
 
124
    def get_file(self, file_id):
 
125
        result = StringIO()
 
126
        result.write(self.contents[file_id])
 
127
        result.seek(0,0)
 
128
        return result
 
129
 
 
130
    def contents_stats(self, file_id):
 
131
        if file_id not in self.contents:
 
132
            return None, None
 
133
        text_sha1 = sha_file(self.get_file(file_id))
 
134
        return text_sha1, len(self.contents[file_id])
 
135
 
 
136
 
 
137
class BTreeTester(TestCase):
 
138
    """A simple unittest tester for the BundleTree class."""
 
139
 
 
140
    def make_tree_1(self):
 
141
        mtree = MockTree()
 
142
        mtree.add_dir("a", "grandparent")
 
143
        mtree.add_dir("b", "grandparent/parent")
 
144
        mtree.add_file("c", "grandparent/parent/file", "Hello\n")
 
145
        mtree.add_dir("d", "grandparent/alt_parent")
 
146
        return BundleTree(mtree, ''), mtree
 
147
        
 
148
    def test_renames(self):
 
149
        """Ensure that file renames have the proper effect on children"""
 
150
        btree = self.make_tree_1()[0]
 
151
        self.assertEqual(btree.old_path("grandparent"), "grandparent")
 
152
        self.assertEqual(btree.old_path("grandparent/parent"), 
 
153
                         "grandparent/parent")
 
154
        self.assertEqual(btree.old_path("grandparent/parent/file"),
 
155
                         "grandparent/parent/file")
 
156
 
 
157
        self.assertEqual(btree.id2path("a"), "grandparent")
 
158
        self.assertEqual(btree.id2path("b"), "grandparent/parent")
 
159
        self.assertEqual(btree.id2path("c"), "grandparent/parent/file")
 
160
 
 
161
        self.assertEqual(btree.path2id("grandparent"), "a")
 
162
        self.assertEqual(btree.path2id("grandparent/parent"), "b")
 
163
        self.assertEqual(btree.path2id("grandparent/parent/file"), "c")
 
164
 
 
165
        assert btree.path2id("grandparent2") is None
 
166
        assert btree.path2id("grandparent2/parent") is None
 
167
        assert btree.path2id("grandparent2/parent/file") is None
 
168
 
 
169
        btree.note_rename("grandparent", "grandparent2")
 
170
        assert btree.old_path("grandparent") is None
 
171
        assert btree.old_path("grandparent/parent") is None
 
172
        assert btree.old_path("grandparent/parent/file") is None
 
173
 
 
174
        self.assertEqual(btree.id2path("a"), "grandparent2")
 
175
        self.assertEqual(btree.id2path("b"), "grandparent2/parent")
 
176
        self.assertEqual(btree.id2path("c"), "grandparent2/parent/file")
 
177
 
 
178
        self.assertEqual(btree.path2id("grandparent2"), "a")
 
179
        self.assertEqual(btree.path2id("grandparent2/parent"), "b")
 
180
        self.assertEqual(btree.path2id("grandparent2/parent/file"), "c")
 
181
 
 
182
        assert btree.path2id("grandparent") is None
 
183
        assert btree.path2id("grandparent/parent") is None
 
184
        assert btree.path2id("grandparent/parent/file") is None
 
185
 
 
186
        btree.note_rename("grandparent/parent", "grandparent2/parent2")
 
187
        self.assertEqual(btree.id2path("a"), "grandparent2")
 
188
        self.assertEqual(btree.id2path("b"), "grandparent2/parent2")
 
189
        self.assertEqual(btree.id2path("c"), "grandparent2/parent2/file")
 
190
 
 
191
        self.assertEqual(btree.path2id("grandparent2"), "a")
 
192
        self.assertEqual(btree.path2id("grandparent2/parent2"), "b")
 
193
        self.assertEqual(btree.path2id("grandparent2/parent2/file"), "c")
 
194
 
 
195
        assert btree.path2id("grandparent2/parent") is None
 
196
        assert btree.path2id("grandparent2/parent/file") is None
 
197
 
 
198
        btree.note_rename("grandparent/parent/file", 
 
199
                          "grandparent2/parent2/file2")
 
200
        self.assertEqual(btree.id2path("a"), "grandparent2")
 
201
        self.assertEqual(btree.id2path("b"), "grandparent2/parent2")
 
202
        self.assertEqual(btree.id2path("c"), "grandparent2/parent2/file2")
 
203
 
 
204
        self.assertEqual(btree.path2id("grandparent2"), "a")
 
205
        self.assertEqual(btree.path2id("grandparent2/parent2"), "b")
 
206
        self.assertEqual(btree.path2id("grandparent2/parent2/file2"), "c")
 
207
 
 
208
        assert btree.path2id("grandparent2/parent2/file") is None
 
209
 
 
210
    def test_moves(self):
 
211
        """Ensure that file moves have the proper effect on children"""
 
212
        btree = self.make_tree_1()[0]
 
213
        btree.note_rename("grandparent/parent/file", 
 
214
                          "grandparent/alt_parent/file")
 
215
        self.assertEqual(btree.id2path("c"), "grandparent/alt_parent/file")
 
216
        self.assertEqual(btree.path2id("grandparent/alt_parent/file"), "c")
 
217
        assert btree.path2id("grandparent/parent/file") is None
 
218
 
 
219
    def unified_diff(self, old, new):
 
220
        out = StringIO()
 
221
        internal_diff("old", old, "new", new, out)
 
222
        out.seek(0,0)
 
223
        return out.read()
 
224
 
 
225
    def make_tree_2(self):
 
226
        btree = self.make_tree_1()[0]
 
227
        btree.note_rename("grandparent/parent/file", 
 
228
                          "grandparent/alt_parent/file")
 
229
        assert btree.id2path("e") is None
 
230
        assert btree.path2id("grandparent/parent/file") is None
 
231
        btree.note_id("e", "grandparent/parent/file")
 
232
        return btree
 
233
 
 
234
    def test_adds(self):
 
235
        """File/inventory adds"""
 
236
        btree = self.make_tree_2()
 
237
        add_patch = self.unified_diff([], ["Extra cheese\n"])
 
238
        btree.note_patch("grandparent/parent/file", add_patch)
 
239
        btree.note_id('f', 'grandparent/parent/symlink', kind='symlink')
 
240
        btree.note_target('grandparent/parent/symlink', 'venus')
 
241
        self.adds_test(btree)
 
242
 
 
243
    def adds_test(self, btree):
 
244
        self.assertEqual(btree.id2path("e"), "grandparent/parent/file")
 
245
        self.assertEqual(btree.path2id("grandparent/parent/file"), "e")
 
246
        self.assertEqual(btree.get_file("e").read(), "Extra cheese\n")
 
247
        self.assertEqual(btree.get_symlink_target('f'), 'venus')
 
248
 
 
249
    def test_adds2(self):
 
250
        """File/inventory adds, with patch-compatibile renames"""
 
251
        btree = self.make_tree_2()
 
252
        btree.contents_by_id = False
 
253
        add_patch = self.unified_diff(["Hello\n"], ["Extra cheese\n"])
 
254
        btree.note_patch("grandparent/parent/file", add_patch)
 
255
        btree.note_id('f', 'grandparent/parent/symlink', kind='symlink')
 
256
        btree.note_target('grandparent/parent/symlink', 'venus')
 
257
        self.adds_test(btree)
 
258
 
 
259
    def make_tree_3(self):
 
260
        btree, mtree = self.make_tree_1()
 
261
        mtree.add_file("e", "grandparent/parent/topping", "Anchovies\n")
 
262
        btree.note_rename("grandparent/parent/file", 
 
263
                          "grandparent/alt_parent/file")
 
264
        btree.note_rename("grandparent/parent/topping", 
 
265
                          "grandparent/alt_parent/stopping")
 
266
        return btree
 
267
 
 
268
    def get_file_test(self, btree):
 
269
        self.assertEqual(btree.get_file("e").read(), "Lemon\n")
 
270
        self.assertEqual(btree.get_file("c").read(), "Hello\n")
 
271
 
 
272
    def test_get_file(self):
 
273
        """Get file contents"""
 
274
        btree = self.make_tree_3()
 
275
        mod_patch = self.unified_diff(["Anchovies\n"], ["Lemon\n"])
 
276
        btree.note_patch("grandparent/alt_parent/stopping", mod_patch)
 
277
        self.get_file_test(btree)
 
278
 
 
279
    def test_get_file2(self):
 
280
        """Get file contents, with patch-compatibile renames"""
 
281
        btree = self.make_tree_3()
 
282
        btree.contents_by_id = False
 
283
        mod_patch = self.unified_diff([], ["Lemon\n"])
 
284
        btree.note_patch("grandparent/alt_parent/stopping", mod_patch)
 
285
        mod_patch = self.unified_diff([], ["Hello\n"])
 
286
        btree.note_patch("grandparent/alt_parent/file", mod_patch)
 
287
        self.get_file_test(btree)
 
288
 
 
289
    def test_delete(self):
 
290
        "Deletion by bundle"
 
291
        btree = self.make_tree_1()[0]
 
292
        self.assertEqual(btree.get_file("c").read(), "Hello\n")
 
293
        btree.note_deletion("grandparent/parent/file")
 
294
        assert btree.id2path("c") is None
 
295
        assert btree.path2id("grandparent/parent/file") is None
 
296
 
 
297
    def sorted_ids(self, tree):
 
298
        ids = list(tree)
 
299
        ids.sort()
 
300
        return ids
 
301
 
 
302
    def test_iteration(self):
 
303
        """Ensure that iteration through ids works properly"""
 
304
        btree = self.make_tree_1()[0]
 
305
        self.assertEqual(self.sorted_ids(btree),
 
306
            [inventory.ROOT_ID, 'a', 'b', 'c', 'd'])
 
307
        btree.note_deletion("grandparent/parent/file")
 
308
        btree.note_id("e", "grandparent/alt_parent/fool", kind="directory")
 
309
        btree.note_last_changed("grandparent/alt_parent/fool", 
 
310
                                "revisionidiguess")
 
311
        self.assertEqual(self.sorted_ids(btree),
 
312
            [inventory.ROOT_ID, 'a', 'b', 'd', 'e'])
 
313
 
 
314
 
 
315
class BundleTester1(TestCaseWithTransport):
 
316
 
 
317
    def test_mismatched_bundle(self):
 
318
        format = bzrdir.BzrDirMetaFormat1()
 
319
        format.repository_format = knitrepo.RepositoryFormatKnit3()
 
320
        serializer = BundleSerializerV08('0.8')
 
321
        b = self.make_branch('.', format=format)
 
322
        self.assertRaises(errors.IncompatibleBundleFormat, serializer.write, 
 
323
                          b.repository, [], {}, StringIO())
 
324
 
 
325
    def test_matched_bundle(self):
 
326
        """Don't raise IncompatibleBundleFormat for knit2 and bundle0.9"""
 
327
        format = bzrdir.BzrDirMetaFormat1()
 
328
        format.repository_format = knitrepo.RepositoryFormatKnit3()
 
329
        serializer = BundleSerializerV09('0.9')
 
330
        b = self.make_branch('.', format=format)
 
331
        serializer.write(b.repository, [], {}, StringIO())
 
332
 
 
333
    def test_mismatched_model(self):
 
334
        """Try copying a bundle from knit2 to knit1"""
 
335
        format = bzrdir.BzrDirMetaFormat1()
 
336
        format.repository_format = knitrepo.RepositoryFormatKnit3()
 
337
        source = self.make_branch_and_tree('source', format=format)
 
338
        source.commit('one', rev_id='one-id')
 
339
        source.commit('two', rev_id='two-id')
 
340
        text = StringIO()
 
341
        write_bundle(source.branch.repository, 'two-id', 'null:', text,
 
342
                     format='0.9')
 
343
        text.seek(0)
 
344
 
 
345
        format = bzrdir.BzrDirMetaFormat1()
 
346
        format.repository_format = knitrepo.RepositoryFormatKnit1()
 
347
        target = self.make_branch('target', format=format)
 
348
        self.assertRaises(errors.IncompatibleRevision, install_bundle, 
 
349
                          target.repository, read_bundle(text))
 
350
 
 
351
 
 
352
class BundleTester(object):
 
353
 
 
354
    def bzrdir_format(self):
 
355
        format = bzrdir.BzrDirMetaFormat1()
 
356
        format.repository_format = knitrepo.RepositoryFormatKnit1()
 
357
        return format
 
358
 
 
359
    def make_branch_and_tree(self, path, format=None):
 
360
        if format is None:
 
361
            format = self.bzrdir_format()
 
362
        return TestCaseWithTransport.make_branch_and_tree(self, path, format)
 
363
 
 
364
    def make_branch(self, path, format=None):
 
365
        if format is None:
 
366
            format = self.bzrdir_format()
 
367
        return TestCaseWithTransport.make_branch(self, path, format)
 
368
 
 
369
    def create_bundle_text(self, base_rev_id, rev_id):
 
370
        bundle_txt = StringIO()
 
371
        rev_ids = write_bundle(self.b1.repository, rev_id, base_rev_id, 
 
372
                               bundle_txt, format=self.format)
 
373
        bundle_txt.seek(0)
 
374
        self.assertEqual(bundle_txt.readline(), 
 
375
                         '# Bazaar revision bundle v%s\n' % self.format)
 
376
        self.assertEqual(bundle_txt.readline(), '#\n')
 
377
 
 
378
        rev = self.b1.repository.get_revision(rev_id)
 
379
        self.assertEqual(bundle_txt.readline().decode('utf-8'),
 
380
                         u'# message:\n')
 
381
        bundle_txt.seek(0)
 
382
        return bundle_txt, rev_ids
 
383
 
 
384
    def get_valid_bundle(self, base_rev_id, rev_id, checkout_dir=None):
 
385
        """Create a bundle from base_rev_id -> rev_id in built-in branch.
 
386
        Make sure that the text generated is valid, and that it
 
387
        can be applied against the base, and generate the same information.
 
388
        
 
389
        :return: The in-memory bundle 
 
390
        """
 
391
        bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
 
392
 
 
393
        # This should also validate the generated bundle 
 
394
        bundle = read_bundle(bundle_txt)
 
395
        repository = self.b1.repository
 
396
        for bundle_rev in bundle.real_revisions:
 
397
            # These really should have already been checked when we read the
 
398
            # bundle, since it computes the sha1 hash for the revision, which
 
399
            # only will match if everything is okay, but lets be explicit about
 
400
            # it
 
401
            branch_rev = repository.get_revision(bundle_rev.revision_id)
 
402
            for a in ('inventory_sha1', 'revision_id', 'parent_ids',
 
403
                      'timestamp', 'timezone', 'message', 'committer', 
 
404
                      'parent_ids', 'properties'):
 
405
                self.assertEqual(getattr(branch_rev, a), 
 
406
                                 getattr(bundle_rev, a))
 
407
            self.assertEqual(len(branch_rev.parent_ids), 
 
408
                             len(bundle_rev.parent_ids))
 
409
        self.assertEqual(rev_ids, 
 
410
                         [r.revision_id for r in bundle.real_revisions])
 
411
        self.valid_apply_bundle(base_rev_id, bundle,
 
412
                                   checkout_dir=checkout_dir)
 
413
 
 
414
        return bundle
 
415
 
 
416
    def get_invalid_bundle(self, base_rev_id, rev_id):
 
417
        """Create a bundle from base_rev_id -> rev_id in built-in branch.
 
418
        Munge the text so that it's invalid.
 
419
        
 
420
        :return: The in-memory bundle
 
421
        """
 
422
        bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
 
423
        new_text = bundle_txt.getvalue().replace('executable:no', 
 
424
                                               'executable:yes')
 
425
        bundle_txt = StringIO(new_text)
 
426
        bundle = read_bundle(bundle_txt)
 
427
        self.valid_apply_bundle(base_rev_id, bundle)
 
428
        return bundle 
 
429
 
 
430
    def test_non_bundle(self):
 
431
        self.assertRaises(NotABundle, read_bundle, StringIO('#!/bin/sh\n'))
 
432
 
 
433
    def test_malformed(self):
 
434
        self.assertRaises(BadBundle, read_bundle, 
 
435
                          StringIO('# Bazaar revision bundle v'))
 
436
 
 
437
    def test_crlf_bundle(self):
 
438
        try:
 
439
            read_bundle(StringIO('# Bazaar revision bundle v0.8\r\n'))
 
440
        except BadBundle:
 
441
            # It is currently permitted for bundles with crlf line endings to
 
442
            # make read_bundle raise a BadBundle, but this should be fixed.
 
443
            # Anything else, especially NotABundle, is an error.
 
444
            pass
 
445
 
 
446
    def get_checkout(self, rev_id, checkout_dir=None):
 
447
        """Get a new tree, with the specified revision in it.
 
448
        """
 
449
 
 
450
        if checkout_dir is None:
 
451
            checkout_dir = tempfile.mkdtemp(prefix='test-branch-', dir='.')
 
452
        else:
 
453
            if not os.path.exists(checkout_dir):
 
454
                os.mkdir(checkout_dir)
 
455
        tree = self.make_branch_and_tree(checkout_dir)
 
456
        s = StringIO()
 
457
        ancestors = write_bundle(self.b1.repository, rev_id, 'null:', s,
 
458
                                 format=self.format)
 
459
        s.seek(0)
 
460
        assert isinstance(s.getvalue(), str), (
 
461
            "Bundle isn't a bytestring:\n %s..." % repr(s.getvalue())[:40])
 
462
        install_bundle(tree.branch.repository, read_bundle(s))
 
463
        for ancestor in ancestors:
 
464
            old = self.b1.repository.revision_tree(ancestor)
 
465
            new = tree.branch.repository.revision_tree(ancestor)
 
466
 
 
467
            # Check that there aren't any inventory level changes
 
468
            delta = new.changes_from(old)
 
469
            self.assertFalse(delta.has_changed(),
 
470
                             'Revision %s not copied correctly.'
 
471
                             % (ancestor,))
 
472
 
 
473
            # Now check that the file contents are all correct
 
474
            for inventory_id in old:
 
475
                try:
 
476
                    old_file = old.get_file(inventory_id)
 
477
                except NoSuchFile:
 
478
                    continue
 
479
                if old_file is None:
 
480
                    continue
 
481
                self.assertEqual(old_file.read(),
 
482
                                 new.get_file(inventory_id).read())
 
483
        if not _mod_revision.is_null(rev_id):
 
484
            rh = self.b1.revision_history()
 
485
            tree.branch.set_revision_history(rh[:rh.index(rev_id)+1])
 
486
            tree.update()
 
487
            delta = tree.changes_from(self.b1.repository.revision_tree(rev_id))
 
488
            self.assertFalse(delta.has_changed(),
 
489
                             'Working tree has modifications: %s' % delta)
 
490
        return tree
 
491
 
 
492
    def valid_apply_bundle(self, base_rev_id, info, checkout_dir=None):
 
493
        """Get the base revision, apply the changes, and make
 
494
        sure everything matches the builtin branch.
 
495
        """
 
496
        to_tree = self.get_checkout(base_rev_id, checkout_dir=checkout_dir)
 
497
        original_parents = to_tree.get_parent_ids()
 
498
        repository = to_tree.branch.repository
 
499
        original_parents = to_tree.get_parent_ids()
 
500
        self.assertIs(repository.has_revision(base_rev_id), True)
 
501
        for rev in info.real_revisions:
 
502
            self.assert_(not repository.has_revision(rev.revision_id),
 
503
                'Revision {%s} present before applying bundle' 
 
504
                % rev.revision_id)
 
505
        merge_bundle(info, to_tree, True, Merge3Merger, False, False)
 
506
 
 
507
        for rev in info.real_revisions:
 
508
            self.assert_(repository.has_revision(rev.revision_id),
 
509
                'Missing revision {%s} after applying bundle' 
 
510
                % rev.revision_id)
 
511
 
 
512
        self.assert_(to_tree.branch.repository.has_revision(info.target))
 
513
        # Do we also want to verify that all the texts have been added?
 
514
 
 
515
        self.assertEqual(original_parents + [info.target],
 
516
            to_tree.get_parent_ids())
 
517
 
 
518
        rev = info.real_revisions[-1]
 
519
        base_tree = self.b1.repository.revision_tree(rev.revision_id)
 
520
        to_tree = to_tree.branch.repository.revision_tree(rev.revision_id)
 
521
        
 
522
        # TODO: make sure the target tree is identical to base tree
 
523
        #       we might also check the working tree.
 
524
 
 
525
        base_files = list(base_tree.list_files())
 
526
        to_files = list(to_tree.list_files())
 
527
        self.assertEqual(len(base_files), len(to_files))
 
528
        for base_file, to_file in zip(base_files, to_files):
 
529
            self.assertEqual(base_file, to_file)
 
530
 
 
531
        for path, status, kind, fileid, entry in base_files:
 
532
            # Check that the meta information is the same
 
533
            self.assertEqual(base_tree.get_file_size(fileid),
 
534
                    to_tree.get_file_size(fileid))
 
535
            self.assertEqual(base_tree.get_file_sha1(fileid),
 
536
                    to_tree.get_file_sha1(fileid))
 
537
            # Check that the contents are the same
 
538
            # This is pretty expensive
 
539
            # self.assertEqual(base_tree.get_file(fileid).read(),
 
540
            #         to_tree.get_file(fileid).read())
 
541
 
 
542
    def test_bundle(self):
 
543
        self.tree1 = self.make_branch_and_tree('b1')
 
544
        self.b1 = self.tree1.branch
 
545
 
 
546
        open('b1/one', 'wb').write('one\n')
 
547
        self.tree1.add('one')
 
548
        self.tree1.commit('add one', rev_id='a@cset-0-1')
 
549
 
 
550
        bundle = self.get_valid_bundle('null:', 'a@cset-0-1')
 
551
 
 
552
        # Make sure we can handle files with spaces, tabs, other
 
553
        # bogus characters
 
554
        self.build_tree([
 
555
                'b1/with space.txt'
 
556
                , 'b1/dir/'
 
557
                , 'b1/dir/filein subdir.c'
 
558
                , 'b1/dir/WithCaps.txt'
 
559
                , 'b1/dir/ pre space'
 
560
                , 'b1/sub/'
 
561
                , 'b1/sub/sub/'
 
562
                , 'b1/sub/sub/nonempty.txt'
 
563
                ])
 
564
        open('b1/sub/sub/emptyfile.txt', 'wb').close()
 
565
        open('b1/dir/nolastnewline.txt', 'wb').write('bloop')
 
566
        tt = TreeTransform(self.tree1)
 
567
        tt.new_file('executable', tt.root, '#!/bin/sh\n', 'exe-1', True)
 
568
        tt.apply()
 
569
        # have to fix length of file-id so that we can predictably rewrite
 
570
        # a (length-prefixed) record containing it later.
 
571
        self.tree1.add('with space.txt', 'withspace-id')
 
572
        self.tree1.add([
 
573
                  'dir'
 
574
                , 'dir/filein subdir.c'
 
575
                , 'dir/WithCaps.txt'
 
576
                , 'dir/ pre space'
 
577
                , 'dir/nolastnewline.txt'
 
578
                , 'sub'
 
579
                , 'sub/sub'
 
580
                , 'sub/sub/nonempty.txt'
 
581
                , 'sub/sub/emptyfile.txt'
 
582
                ])
 
583
        self.tree1.commit('add whitespace', rev_id='a@cset-0-2')
 
584
 
 
585
        bundle = self.get_valid_bundle('a@cset-0-1', 'a@cset-0-2')
 
586
 
 
587
        # Check a rollup bundle 
 
588
        bundle = self.get_valid_bundle('null:', 'a@cset-0-2')
 
589
 
 
590
        # Now delete entries
 
591
        self.tree1.remove(
 
592
                ['sub/sub/nonempty.txt'
 
593
                , 'sub/sub/emptyfile.txt'
 
594
                , 'sub/sub'
 
595
                ])
 
596
        tt = TreeTransform(self.tree1)
 
597
        trans_id = tt.trans_id_tree_file_id('exe-1')
 
598
        tt.set_executability(False, trans_id)
 
599
        tt.apply()
 
600
        self.tree1.commit('removed', rev_id='a@cset-0-3')
 
601
        
 
602
        bundle = self.get_valid_bundle('a@cset-0-2', 'a@cset-0-3')
 
603
        self.assertRaises((TestamentMismatch,
 
604
            errors.VersionedFileInvalidChecksum), self.get_invalid_bundle,
 
605
            'a@cset-0-2', 'a@cset-0-3')
 
606
        # Check a rollup bundle 
 
607
        bundle = self.get_valid_bundle('null:', 'a@cset-0-3')
 
608
 
 
609
        # Now move the directory
 
610
        self.tree1.rename_one('dir', 'sub/dir')
 
611
        self.tree1.commit('rename dir', rev_id='a@cset-0-4')
 
612
 
 
613
        bundle = self.get_valid_bundle('a@cset-0-3', 'a@cset-0-4')
 
614
        # Check a rollup bundle 
 
615
        bundle = self.get_valid_bundle('null:', 'a@cset-0-4')
 
616
 
 
617
        # Modified files
 
618
        open('b1/sub/dir/WithCaps.txt', 'ab').write('\nAdding some text\n')
 
619
        open('b1/sub/dir/ pre space', 'ab').write(
 
620
             '\r\nAdding some\r\nDOS format lines\r\n')
 
621
        open('b1/sub/dir/nolastnewline.txt', 'ab').write('\n')
 
622
        self.tree1.rename_one('sub/dir/ pre space', 
 
623
                              'sub/ start space')
 
624
        self.tree1.commit('Modified files', rev_id='a@cset-0-5')
 
625
        bundle = self.get_valid_bundle('a@cset-0-4', 'a@cset-0-5')
 
626
 
 
627
        self.tree1.rename_one('sub/dir/WithCaps.txt', 'temp')
 
628
        self.tree1.rename_one('with space.txt', 'WithCaps.txt')
 
629
        self.tree1.rename_one('temp', 'with space.txt')
 
630
        self.tree1.commit(u'swap filenames', rev_id='a@cset-0-6',
 
631
                          verbose=False)
 
632
        bundle = self.get_valid_bundle('a@cset-0-5', 'a@cset-0-6')
 
633
        other = self.get_checkout('a@cset-0-5')
 
634
        tree1_inv = self.tree1.branch.repository.get_inventory_xml(
 
635
            'a@cset-0-5')
 
636
        tree2_inv = other.branch.repository.get_inventory_xml('a@cset-0-5')
 
637
        self.assertEqualDiff(tree1_inv, tree2_inv)
 
638
        other.rename_one('sub/dir/nolastnewline.txt', 'sub/nolastnewline.txt')
 
639
        other.commit('rename file', rev_id='a@cset-0-6b')
 
640
        _merge_helper([other.basedir, -1], [None, None],
 
641
                      this_dir=self.tree1.basedir)
 
642
        self.tree1.commit(u'Merge', rev_id='a@cset-0-7',
 
643
                          verbose=False)
 
644
        bundle = self.get_valid_bundle('a@cset-0-6', 'a@cset-0-7')
 
645
 
 
646
    def test_symlink_bundle(self):
 
647
        if not has_symlinks():
 
648
            raise TestSkipped("No symlink support")
 
649
        self.tree1 = self.make_branch_and_tree('b1')
 
650
        self.b1 = self.tree1.branch
 
651
        tt = TreeTransform(self.tree1)
 
652
        tt.new_symlink('link', tt.root, 'bar/foo', 'link-1')
 
653
        tt.apply()
 
654
        self.tree1.commit('add symlink', rev_id='l@cset-0-1')
 
655
        self.get_valid_bundle('null:', 'l@cset-0-1')
 
656
        tt = TreeTransform(self.tree1)
 
657
        trans_id = tt.trans_id_tree_file_id('link-1')
 
658
        tt.adjust_path('link2', tt.root, trans_id)
 
659
        tt.delete_contents(trans_id)
 
660
        tt.create_symlink('mars', trans_id)
 
661
        tt.apply()
 
662
        self.tree1.commit('rename and change symlink', rev_id='l@cset-0-2')
 
663
        self.get_valid_bundle('l@cset-0-1', 'l@cset-0-2')
 
664
        tt = TreeTransform(self.tree1)
 
665
        trans_id = tt.trans_id_tree_file_id('link-1')
 
666
        tt.delete_contents(trans_id)
 
667
        tt.create_symlink('jupiter', trans_id)
 
668
        tt.apply()
 
669
        self.tree1.commit('just change symlink target', rev_id='l@cset-0-3')
 
670
        self.get_valid_bundle('l@cset-0-2', 'l@cset-0-3')
 
671
        tt = TreeTransform(self.tree1)
 
672
        trans_id = tt.trans_id_tree_file_id('link-1')
 
673
        tt.delete_contents(trans_id)
 
674
        tt.apply()
 
675
        self.tree1.commit('Delete symlink', rev_id='l@cset-0-4')
 
676
        self.get_valid_bundle('l@cset-0-3', 'l@cset-0-4')
 
677
 
 
678
    def test_binary_bundle(self):
 
679
        self.tree1 = self.make_branch_and_tree('b1')
 
680
        self.b1 = self.tree1.branch
 
681
        tt = TreeTransform(self.tree1)
 
682
        
 
683
        # Add
 
684
        tt.new_file('file', tt.root, '\x00\n\x00\r\x01\n\x02\r\xff', 'binary-1')
 
685
        tt.new_file('file2', tt.root, '\x01\n\x02\r\x03\n\x04\r\xff',
 
686
            'binary-2')
 
687
        tt.apply()
 
688
        self.tree1.commit('add binary', rev_id='b@cset-0-1')
 
689
        self.get_valid_bundle('null:', 'b@cset-0-1')
 
690
 
 
691
        # Delete
 
692
        tt = TreeTransform(self.tree1)
 
693
        trans_id = tt.trans_id_tree_file_id('binary-1')
 
694
        tt.delete_contents(trans_id)
 
695
        tt.apply()
 
696
        self.tree1.commit('delete binary', rev_id='b@cset-0-2')
 
697
        self.get_valid_bundle('b@cset-0-1', 'b@cset-0-2')
 
698
 
 
699
        # Rename & modify
 
700
        tt = TreeTransform(self.tree1)
 
701
        trans_id = tt.trans_id_tree_file_id('binary-2')
 
702
        tt.adjust_path('file3', tt.root, trans_id)
 
703
        tt.delete_contents(trans_id)
 
704
        tt.create_file('file\rcontents\x00\n\x00', trans_id)
 
705
        tt.apply()
 
706
        self.tree1.commit('rename and modify binary', rev_id='b@cset-0-3')
 
707
        self.get_valid_bundle('b@cset-0-2', 'b@cset-0-3')
 
708
 
 
709
        # Modify
 
710
        tt = TreeTransform(self.tree1)
 
711
        trans_id = tt.trans_id_tree_file_id('binary-2')
 
712
        tt.delete_contents(trans_id)
 
713
        tt.create_file('\x00file\rcontents', trans_id)
 
714
        tt.apply()
 
715
        self.tree1.commit('just modify binary', rev_id='b@cset-0-4')
 
716
        self.get_valid_bundle('b@cset-0-3', 'b@cset-0-4')
 
717
 
 
718
        # Rollup
 
719
        self.get_valid_bundle('null:', 'b@cset-0-4')
 
720
 
 
721
    def test_last_modified(self):
 
722
        self.tree1 = self.make_branch_and_tree('b1')
 
723
        self.b1 = self.tree1.branch
 
724
        tt = TreeTransform(self.tree1)
 
725
        tt.new_file('file', tt.root, 'file', 'file')
 
726
        tt.apply()
 
727
        self.tree1.commit('create file', rev_id='a@lmod-0-1')
 
728
 
 
729
        tt = TreeTransform(self.tree1)
 
730
        trans_id = tt.trans_id_tree_file_id('file')
 
731
        tt.delete_contents(trans_id)
 
732
        tt.create_file('file2', trans_id)
 
733
        tt.apply()
 
734
        self.tree1.commit('modify text', rev_id='a@lmod-0-2a')
 
735
 
 
736
        other = self.get_checkout('a@lmod-0-1')
 
737
        tt = TreeTransform(other)
 
738
        trans_id = tt.trans_id_tree_file_id('file')
 
739
        tt.delete_contents(trans_id)
 
740
        tt.create_file('file2', trans_id)
 
741
        tt.apply()
 
742
        other.commit('modify text in another tree', rev_id='a@lmod-0-2b')
 
743
        _merge_helper([other.basedir, -1], [None, None],
 
744
                      this_dir=self.tree1.basedir)
 
745
        self.tree1.commit(u'Merge', rev_id='a@lmod-0-3',
 
746
                          verbose=False)
 
747
        self.tree1.commit(u'Merge', rev_id='a@lmod-0-4')
 
748
        bundle = self.get_valid_bundle('a@lmod-0-2a', 'a@lmod-0-4')
 
749
 
 
750
    def test_hide_history(self):
 
751
        self.tree1 = self.make_branch_and_tree('b1')
 
752
        self.b1 = self.tree1.branch
 
753
 
 
754
        open('b1/one', 'wb').write('one\n')
 
755
        self.tree1.add('one')
 
756
        self.tree1.commit('add file', rev_id='a@cset-0-1')
 
757
        open('b1/one', 'wb').write('two\n')
 
758
        self.tree1.commit('modify', rev_id='a@cset-0-2')
 
759
        open('b1/one', 'wb').write('three\n')
 
760
        self.tree1.commit('modify', rev_id='a@cset-0-3')
 
761
        bundle_file = StringIO()
 
762
        rev_ids = write_bundle(self.tree1.branch.repository, 'a@cset-0-3',
 
763
                               'a@cset-0-1', bundle_file, format=self.format)
 
764
        self.assertNotContainsRe(bundle_file.getvalue(), '\btwo\b')
 
765
        self.assertContainsRe(self.get_raw(bundle_file), 'one')
 
766
        self.assertContainsRe(self.get_raw(bundle_file), 'three')
 
767
 
 
768
    def test_bundle_same_basis(self):
 
769
        """Ensure using the basis as the target doesn't cause an error"""
 
770
        self.tree1 = self.make_branch_and_tree('b1')
 
771
        self.tree1.commit('add file', rev_id='a@cset-0-1')
 
772
        bundle_file = StringIO()
 
773
        rev_ids = write_bundle(self.tree1.branch.repository, 'a@cset-0-1',
 
774
                               'a@cset-0-1', bundle_file)
 
775
 
 
776
    @staticmethod
 
777
    def get_raw(bundle_file):
 
778
        return bundle_file.getvalue()
 
779
 
 
780
    def test_unicode_bundle(self):
 
781
        # Handle international characters
 
782
        os.mkdir('b1')
 
783
        try:
 
784
            f = open(u'b1/with Dod\xe9', 'wb')
 
785
        except UnicodeEncodeError:
 
786
            raise TestSkipped("Filesystem doesn't support unicode")
 
787
 
 
788
        self.tree1 = self.make_branch_and_tree('b1')
 
789
        self.b1 = self.tree1.branch
 
790
 
 
791
        f.write((u'A file\n'
 
792
            u'With international man of mystery\n'
 
793
            u'William Dod\xe9\n').encode('utf-8'))
 
794
        f.close()
 
795
 
 
796
        self.tree1.add([u'with Dod\xe9'], ['withdod-id'])
 
797
        self.tree1.commit(u'i18n commit from William Dod\xe9',
 
798
                          rev_id='i18n-1', committer=u'William Dod\xe9')
 
799
 
 
800
        if sys.platform == 'darwin':
 
801
            # On Mac the '\xe9' gets changed to 'e\u0301'
 
802
            self.assertEqual([u'.bzr', u'with Dode\u0301'],
 
803
                             sorted(os.listdir(u'b1')))
 
804
            delta = self.tree1.changes_from(self.tree1.basis_tree())
 
805
            self.assertEqual([(u'with Dod\xe9', 'withdod-id', 'file')],
 
806
                             delta.removed)
 
807
            self.knownFailure("Mac OSX doesn't preserve unicode"
 
808
                              " combining characters.")
 
809
 
 
810
        # Add
 
811
        bundle = self.get_valid_bundle('null:', 'i18n-1')
 
812
 
 
813
        # Modified
 
814
        f = open(u'b1/with Dod\xe9', 'wb')
 
815
        f.write(u'Modified \xb5\n'.encode('utf8'))
 
816
        f.close()
 
817
        self.tree1.commit(u'modified', rev_id='i18n-2')
 
818
 
 
819
        bundle = self.get_valid_bundle('i18n-1', 'i18n-2')
 
820
        
 
821
        # Renamed
 
822
        self.tree1.rename_one(u'with Dod\xe9', u'B\xe5gfors')
 
823
        self.tree1.commit(u'renamed, the new i18n man', rev_id='i18n-3',
 
824
                          committer=u'Erik B\xe5gfors')
 
825
 
 
826
        bundle = self.get_valid_bundle('i18n-2', 'i18n-3')
 
827
 
 
828
        # Removed
 
829
        self.tree1.remove([u'B\xe5gfors'])
 
830
        self.tree1.commit(u'removed', rev_id='i18n-4')
 
831
 
 
832
        bundle = self.get_valid_bundle('i18n-3', 'i18n-4')
 
833
 
 
834
        # Rollup
 
835
        bundle = self.get_valid_bundle('null:', 'i18n-4')
 
836
 
 
837
 
 
838
    def test_whitespace_bundle(self):
 
839
        if sys.platform in ('win32', 'cygwin'):
 
840
            raise TestSkipped('Windows doesn\'t support filenames'
 
841
                              ' with tabs or trailing spaces')
 
842
        self.tree1 = self.make_branch_and_tree('b1')
 
843
        self.b1 = self.tree1.branch
 
844
 
 
845
        self.build_tree(['b1/trailing space '])
 
846
        self.tree1.add(['trailing space '])
 
847
        # TODO: jam 20060701 Check for handling files with '\t' characters
 
848
        #       once we actually support them
 
849
 
 
850
        # Added
 
851
        self.tree1.commit('funky whitespace', rev_id='white-1')
 
852
 
 
853
        bundle = self.get_valid_bundle('null:', 'white-1')
 
854
 
 
855
        # Modified
 
856
        open('b1/trailing space ', 'ab').write('add some text\n')
 
857
        self.tree1.commit('add text', rev_id='white-2')
 
858
 
 
859
        bundle = self.get_valid_bundle('white-1', 'white-2')
 
860
 
 
861
        # Renamed
 
862
        self.tree1.rename_one('trailing space ', ' start and end space ')
 
863
        self.tree1.commit('rename', rev_id='white-3')
 
864
 
 
865
        bundle = self.get_valid_bundle('white-2', 'white-3')
 
866
 
 
867
        # Removed
 
868
        self.tree1.remove([' start and end space '])
 
869
        self.tree1.commit('removed', rev_id='white-4')
 
870
 
 
871
        bundle = self.get_valid_bundle('white-3', 'white-4')
 
872
        
 
873
        # Now test a complet roll-up
 
874
        bundle = self.get_valid_bundle('null:', 'white-4')
 
875
 
 
876
    def test_alt_timezone_bundle(self):
 
877
        self.tree1 = self.make_branch_and_memory_tree('b1')
 
878
        self.b1 = self.tree1.branch
 
879
        builder = treebuilder.TreeBuilder()
 
880
 
 
881
        self.tree1.lock_write()
 
882
        builder.start_tree(self.tree1)
 
883
        builder.build(['newfile'])
 
884
        builder.finish_tree()
 
885
 
 
886
        # Asia/Colombo offset = 5 hours 30 minutes
 
887
        self.tree1.commit('non-hour offset timezone', rev_id='tz-1',
 
888
                          timezone=19800, timestamp=1152544886.0)
 
889
 
 
890
        bundle = self.get_valid_bundle('null:', 'tz-1')
 
891
        
 
892
        rev = bundle.revisions[0]
 
893
        self.assertEqual('Mon 2006-07-10 20:51:26.000000000 +0530', rev.date)
 
894
        self.assertEqual(19800, rev.timezone)
 
895
        self.assertEqual(1152544886.0, rev.timestamp)
 
896
        self.tree1.unlock()
 
897
 
 
898
    def test_bundle_root_id(self):
 
899
        self.tree1 = self.make_branch_and_tree('b1')
 
900
        self.b1 = self.tree1.branch
 
901
        self.tree1.commit('message', rev_id='revid1')
 
902
        bundle = self.get_valid_bundle('null:', 'revid1')
 
903
        tree = self.get_bundle_tree(bundle, 'revid1')
 
904
        self.assertEqual('revid1', tree.inventory.root.revision)
 
905
 
 
906
    def test_install_revisions(self):
 
907
        self.tree1 = self.make_branch_and_tree('b1')
 
908
        self.b1 = self.tree1.branch
 
909
        self.tree1.commit('message', rev_id='rev2a')
 
910
        bundle = self.get_valid_bundle('null:', 'rev2a')
 
911
        branch2 = self.make_branch('b2')
 
912
        self.assertFalse(branch2.repository.has_revision('rev2a'))
 
913
        target_revision = bundle.install_revisions(branch2.repository)
 
914
        self.assertTrue(branch2.repository.has_revision('rev2a'))
 
915
        self.assertEqual('rev2a', target_revision)
 
916
 
 
917
    def test_bundle_empty_property(self):
 
918
        """Test serializing revision properties with an empty value."""
 
919
        tree = self.make_branch_and_memory_tree('tree')
 
920
        tree.lock_write()
 
921
        self.addCleanup(tree.unlock)
 
922
        tree.add([''], ['TREE_ROOT'])
 
923
        tree.commit('One', revprops={'one':'two', 'empty':''}, rev_id='rev1')
 
924
        self.b1 = tree.branch
 
925
        bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
 
926
        bundle = read_bundle(bundle_sio)
 
927
        revision_info = bundle.revisions[0]
 
928
        self.assertEqual('rev1', revision_info.revision_id)
 
929
        rev = revision_info.as_revision()
 
930
        self.assertEqual({'branch-nick':'tree', 'empty':'', 'one':'two'},
 
931
                         rev.properties)
 
932
 
 
933
    def test_bundle_sorted_properties(self):
 
934
        """For stability the writer should write properties in sorted order."""
 
935
        tree = self.make_branch_and_memory_tree('tree')
 
936
        tree.lock_write()
 
937
        self.addCleanup(tree.unlock)
 
938
 
 
939
        tree.add([''], ['TREE_ROOT'])
 
940
        tree.commit('One', rev_id='rev1',
 
941
                    revprops={'a':'4', 'b':'3', 'c':'2', 'd':'1'})
 
942
        self.b1 = tree.branch
 
943
        bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
 
944
        bundle = read_bundle(bundle_sio)
 
945
        revision_info = bundle.revisions[0]
 
946
        self.assertEqual('rev1', revision_info.revision_id)
 
947
        rev = revision_info.as_revision()
 
948
        self.assertEqual({'branch-nick':'tree', 'a':'4', 'b':'3', 'c':'2',
 
949
                          'd':'1'}, rev.properties)
 
950
 
 
951
    def test_bundle_unicode_properties(self):
 
952
        """We should be able to round trip a non-ascii property."""
 
953
        tree = self.make_branch_and_memory_tree('tree')
 
954
        tree.lock_write()
 
955
        self.addCleanup(tree.unlock)
 
956
 
 
957
        tree.add([''], ['TREE_ROOT'])
 
958
        # Revisions themselves do not require anything about revision property
 
959
        # keys, other than that they are a basestring, and do not contain
 
960
        # whitespace.
 
961
        # However, Testaments assert than they are str(), and thus should not
 
962
        # be Unicode.
 
963
        tree.commit('One', rev_id='rev1',
 
964
                    revprops={'omega':u'\u03a9', 'alpha':u'\u03b1'})
 
965
        self.b1 = tree.branch
 
966
        bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
 
967
        bundle = read_bundle(bundle_sio)
 
968
        revision_info = bundle.revisions[0]
 
969
        self.assertEqual('rev1', revision_info.revision_id)
 
970
        rev = revision_info.as_revision()
 
971
        self.assertEqual({'branch-nick':'tree', 'omega':u'\u03a9',
 
972
                          'alpha':u'\u03b1'}, rev.properties)
 
973
 
 
974
    def test_bundle_with_ghosts(self):
 
975
        tree = self.make_branch_and_tree('tree')
 
976
        self.b1 = tree.branch
 
977
        self.build_tree_contents([('tree/file', 'content1')])
 
978
        tree.add(['file'])
 
979
        tree.commit('rev1')
 
980
        self.build_tree_contents([('tree/file', 'content2')])
 
981
        tree.add_parent_tree_id('ghost')
 
982
        tree.commit('rev2', rev_id='rev2')
 
983
        bundle = self.get_valid_bundle('null:', 'rev2')
 
984
 
 
985
    def make_simple_tree(self, format=None):
 
986
        tree = self.make_branch_and_tree('b1', format=format)
 
987
        self.b1 = tree.branch
 
988
        self.build_tree(['b1/file'])
 
989
        tree.add('file')
 
990
        return tree
 
991
 
 
992
    def test_across_serializers(self):
 
993
        tree = self.make_simple_tree('knit')
 
994
        tree.commit('hello', rev_id='rev1')
 
995
        tree.commit('hello', rev_id='rev2')
 
996
        bundle = read_bundle(self.create_bundle_text('null:', 'rev2')[0])
 
997
        repo = self.make_repository('repo', format='dirstate-with-subtree')
 
998
        bundle.install_revisions(repo)
 
999
        inv_text = repo.get_inventory_xml('rev2')
 
1000
        self.assertNotContainsRe(inv_text, 'format="5"')
 
1001
        self.assertContainsRe(inv_text, 'format="7"')
 
1002
 
 
1003
    def test_across_models(self):
 
1004
        tree = self.make_simple_tree('knit')
 
1005
        tree.commit('hello', rev_id='rev1')
 
1006
        tree.commit('hello', rev_id='rev2')
 
1007
        bundle = read_bundle(self.create_bundle_text('null:', 'rev2')[0])
 
1008
        repo = self.make_repository('repo', format='dirstate-with-subtree')
 
1009
        bundle.install_revisions(repo)
 
1010
        inv = repo.get_inventory('rev2')
 
1011
        self.assertEqual('rev2', inv.root.revision)
 
1012
        root_vf = repo.weave_store.get_weave(inv.root.file_id,
 
1013
                                             repo.get_transaction())
 
1014
        self.assertEqual(root_vf.versions(), ['rev1', 'rev2'])
 
1015
 
 
1016
    def test_across_models_incompatible(self):
 
1017
        tree = self.make_simple_tree('dirstate-with-subtree')
 
1018
        tree.commit('hello', rev_id='rev1')
 
1019
        tree.commit('hello', rev_id='rev2')
 
1020
        try:
 
1021
            bundle = read_bundle(self.create_bundle_text('null:', 'rev1')[0])
 
1022
        except errors.IncompatibleBundleFormat:
 
1023
            raise TestSkipped("Format 0.8 doesn't work with knit3")
 
1024
        repo = self.make_repository('repo', format='knit')
 
1025
        bundle.install_revisions(repo)
 
1026
 
 
1027
        bundle = read_bundle(self.create_bundle_text('null:', 'rev2')[0])
 
1028
        self.assertRaises(errors.IncompatibleRevision,
 
1029
                          bundle.install_revisions, repo)
 
1030
 
 
1031
    def test_get_merge_request(self):
 
1032
        tree = self.make_simple_tree()
 
1033
        tree.commit('hello', rev_id='rev1')
 
1034
        tree.commit('hello', rev_id='rev2')
 
1035
        bundle = read_bundle(self.create_bundle_text('null:', 'rev1')[0])
 
1036
        result = bundle.get_merge_request(tree.branch.repository)
 
1037
        self.assertEqual((None, 'rev1', 'inapplicable'), result)
 
1038
 
 
1039
    def test_with_subtree(self):
 
1040
        tree = self.make_branch_and_tree('tree',
 
1041
                                         format='dirstate-with-subtree')
 
1042
        self.b1 = tree.branch
 
1043
        subtree = self.make_branch_and_tree('tree/subtree',
 
1044
                                            format='dirstate-with-subtree')
 
1045
        tree.add('subtree')
 
1046
        tree.commit('hello', rev_id='rev1')
 
1047
        try:
 
1048
            bundle = read_bundle(self.create_bundle_text('null:', 'rev1')[0])
 
1049
        except errors.IncompatibleBundleFormat:
 
1050
            raise TestSkipped("Format 0.8 doesn't work with knit3")
 
1051
        if isinstance(bundle, v09.BundleInfo09):
 
1052
            raise TestSkipped("Format 0.9 doesn't work with subtrees")
 
1053
        repo = self.make_repository('repo', format='knit')
 
1054
        self.assertRaises(errors.IncompatibleRevision,
 
1055
                          bundle.install_revisions, repo)
 
1056
        repo2 = self.make_repository('repo2', format='dirstate-with-subtree')
 
1057
        bundle.install_revisions(repo2)
 
1058
 
 
1059
    def test_revision_id_with_slash(self):
 
1060
        self.tree1 = self.make_branch_and_tree('tree')
 
1061
        self.b1 = self.tree1.branch
 
1062
        try:
 
1063
            self.tree1.commit('Revision/id/with/slashes', rev_id='rev/id')
 
1064
        except ValueError:
 
1065
            raise TestSkipped("Repository doesn't support revision ids with"
 
1066
                              " slashes")
 
1067
        bundle = self.get_valid_bundle('null:', 'rev/id')
 
1068
 
 
1069
 
 
1070
class V08BundleTester(BundleTester, TestCaseWithTransport):
 
1071
 
 
1072
    format = '0.8'
 
1073
 
 
1074
    def test_bundle_empty_property(self):
 
1075
        """Test serializing revision properties with an empty value."""
 
1076
        tree = self.make_branch_and_memory_tree('tree')
 
1077
        tree.lock_write()
 
1078
        self.addCleanup(tree.unlock)
 
1079
        tree.add([''], ['TREE_ROOT'])
 
1080
        tree.commit('One', revprops={'one':'two', 'empty':''}, rev_id='rev1')
 
1081
        self.b1 = tree.branch
 
1082
        bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
 
1083
        self.assertContainsRe(bundle_sio.getvalue(),
 
1084
                              '# properties:\n'
 
1085
                              '#   branch-nick: tree\n'
 
1086
                              '#   empty: \n'
 
1087
                              '#   one: two\n'
 
1088
                             )
 
1089
        bundle = read_bundle(bundle_sio)
 
1090
        revision_info = bundle.revisions[0]
 
1091
        self.assertEqual('rev1', revision_info.revision_id)
 
1092
        rev = revision_info.as_revision()
 
1093
        self.assertEqual({'branch-nick':'tree', 'empty':'', 'one':'two'},
 
1094
                         rev.properties)
 
1095
 
 
1096
    def get_bundle_tree(self, bundle, revision_id):
 
1097
        repository = self.make_repository('repo')
 
1098
        return bundle.revision_tree(repository, 'revid1')
 
1099
 
 
1100
    def test_bundle_empty_property_alt(self):
 
1101
        """Test serializing revision properties with an empty value.
 
1102
 
 
1103
        Older readers had a bug when reading an empty property.
 
1104
        They assumed that all keys ended in ': \n'. However they would write an
 
1105
        empty value as ':\n'. This tests make sure that all newer bzr versions
 
1106
        can handle th second form.
 
1107
        """
 
1108
        tree = self.make_branch_and_memory_tree('tree')
 
1109
        tree.lock_write()
 
1110
        self.addCleanup(tree.unlock)
 
1111
        tree.add([''], ['TREE_ROOT'])
 
1112
        tree.commit('One', revprops={'one':'two', 'empty':''}, rev_id='rev1')
 
1113
        self.b1 = tree.branch
 
1114
        bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
 
1115
        txt = bundle_sio.getvalue()
 
1116
        loc = txt.find('#   empty: ') + len('#   empty:')
 
1117
        # Create a new bundle, which strips the trailing space after empty
 
1118
        bundle_sio = StringIO(txt[:loc] + txt[loc+1:])
 
1119
 
 
1120
        self.assertContainsRe(bundle_sio.getvalue(),
 
1121
                              '# properties:\n'
 
1122
                              '#   branch-nick: tree\n'
 
1123
                              '#   empty:\n'
 
1124
                              '#   one: two\n'
 
1125
                             )
 
1126
        bundle = read_bundle(bundle_sio)
 
1127
        revision_info = bundle.revisions[0]
 
1128
        self.assertEqual('rev1', revision_info.revision_id)
 
1129
        rev = revision_info.as_revision()
 
1130
        self.assertEqual({'branch-nick':'tree', 'empty':'', 'one':'two'},
 
1131
                         rev.properties)
 
1132
 
 
1133
    def test_bundle_sorted_properties(self):
 
1134
        """For stability the writer should write properties in sorted order."""
 
1135
        tree = self.make_branch_and_memory_tree('tree')
 
1136
        tree.lock_write()
 
1137
        self.addCleanup(tree.unlock)
 
1138
 
 
1139
        tree.add([''], ['TREE_ROOT'])
 
1140
        tree.commit('One', rev_id='rev1',
 
1141
                    revprops={'a':'4', 'b':'3', 'c':'2', 'd':'1'})
 
1142
        self.b1 = tree.branch
 
1143
        bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
 
1144
        self.assertContainsRe(bundle_sio.getvalue(),
 
1145
                              '# properties:\n'
 
1146
                              '#   a: 4\n'
 
1147
                              '#   b: 3\n'
 
1148
                              '#   branch-nick: tree\n'
 
1149
                              '#   c: 2\n'
 
1150
                              '#   d: 1\n'
 
1151
                             )
 
1152
        bundle = read_bundle(bundle_sio)
 
1153
        revision_info = bundle.revisions[0]
 
1154
        self.assertEqual('rev1', revision_info.revision_id)
 
1155
        rev = revision_info.as_revision()
 
1156
        self.assertEqual({'branch-nick':'tree', 'a':'4', 'b':'3', 'c':'2',
 
1157
                          'd':'1'}, rev.properties)
 
1158
 
 
1159
    def test_bundle_unicode_properties(self):
 
1160
        """We should be able to round trip a non-ascii property."""
 
1161
        tree = self.make_branch_and_memory_tree('tree')
 
1162
        tree.lock_write()
 
1163
        self.addCleanup(tree.unlock)
 
1164
 
 
1165
        tree.add([''], ['TREE_ROOT'])
 
1166
        # Revisions themselves do not require anything about revision property
 
1167
        # keys, other than that they are a basestring, and do not contain
 
1168
        # whitespace.
 
1169
        # However, Testaments assert than they are str(), and thus should not
 
1170
        # be Unicode.
 
1171
        tree.commit('One', rev_id='rev1',
 
1172
                    revprops={'omega':u'\u03a9', 'alpha':u'\u03b1'})
 
1173
        self.b1 = tree.branch
 
1174
        bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
 
1175
        self.assertContainsRe(bundle_sio.getvalue(),
 
1176
                              '# properties:\n'
 
1177
                              '#   alpha: \xce\xb1\n'
 
1178
                              '#   branch-nick: tree\n'
 
1179
                              '#   omega: \xce\xa9\n'
 
1180
                             )
 
1181
        bundle = read_bundle(bundle_sio)
 
1182
        revision_info = bundle.revisions[0]
 
1183
        self.assertEqual('rev1', revision_info.revision_id)
 
1184
        rev = revision_info.as_revision()
 
1185
        self.assertEqual({'branch-nick':'tree', 'omega':u'\u03a9',
 
1186
                          'alpha':u'\u03b1'}, rev.properties)
 
1187
 
 
1188
 
 
1189
class V09BundleKnit2Tester(V08BundleTester):
 
1190
 
 
1191
    format = '0.9'
 
1192
 
 
1193
    def bzrdir_format(self):
 
1194
        format = bzrdir.BzrDirMetaFormat1()
 
1195
        format.repository_format = knitrepo.RepositoryFormatKnit3()
 
1196
        return format
 
1197
 
 
1198
 
 
1199
class V09BundleKnit1Tester(V08BundleTester):
 
1200
 
 
1201
    format = '0.9'
 
1202
 
 
1203
    def bzrdir_format(self):
 
1204
        format = bzrdir.BzrDirMetaFormat1()
 
1205
        format.repository_format = knitrepo.RepositoryFormatKnit1()
 
1206
        return format
 
1207
 
 
1208
 
 
1209
class V4BundleTester(BundleTester, TestCaseWithTransport):
 
1210
 
 
1211
    format = '4'
 
1212
 
 
1213
    def get_valid_bundle(self, base_rev_id, rev_id, checkout_dir=None):
 
1214
        """Create a bundle from base_rev_id -> rev_id in built-in branch.
 
1215
        Make sure that the text generated is valid, and that it
 
1216
        can be applied against the base, and generate the same information.
 
1217
        
 
1218
        :return: The in-memory bundle 
 
1219
        """
 
1220
        bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
 
1221
 
 
1222
        # This should also validate the generated bundle 
 
1223
        bundle = read_bundle(bundle_txt)
 
1224
        repository = self.b1.repository
 
1225
        for bundle_rev in bundle.real_revisions:
 
1226
            # These really should have already been checked when we read the
 
1227
            # bundle, since it computes the sha1 hash for the revision, which
 
1228
            # only will match if everything is okay, but lets be explicit about
 
1229
            # it
 
1230
            branch_rev = repository.get_revision(bundle_rev.revision_id)
 
1231
            for a in ('inventory_sha1', 'revision_id', 'parent_ids',
 
1232
                      'timestamp', 'timezone', 'message', 'committer', 
 
1233
                      'parent_ids', 'properties'):
 
1234
                self.assertEqual(getattr(branch_rev, a), 
 
1235
                                 getattr(bundle_rev, a))
 
1236
            self.assertEqual(len(branch_rev.parent_ids), 
 
1237
                             len(bundle_rev.parent_ids))
 
1238
        self.assertEqual(set(rev_ids),
 
1239
                         set([r.revision_id for r in bundle.real_revisions]))
 
1240
        self.valid_apply_bundle(base_rev_id, bundle,
 
1241
                                   checkout_dir=checkout_dir)
 
1242
 
 
1243
        return bundle
 
1244
 
 
1245
    def get_invalid_bundle(self, base_rev_id, rev_id):
 
1246
        """Create a bundle from base_rev_id -> rev_id in built-in branch.
 
1247
        Munge the text so that it's invalid.
 
1248
 
 
1249
        :return: The in-memory bundle
 
1250
        """
 
1251
        from bzrlib.bundle import serializer
 
1252
        bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
 
1253
        new_text = self.get_raw(StringIO(''.join(bundle_txt)))
 
1254
        new_text = new_text.replace('<file file_id="exe-1"',
 
1255
                                    '<file executable="y" file_id="exe-1"')
 
1256
        new_text = new_text.replace('B372', 'B387')
 
1257
        bundle_txt = StringIO()
 
1258
        bundle_txt.write(serializer._get_bundle_header('4'))
 
1259
        bundle_txt.write('\n')
 
1260
        bundle_txt.write(new_text.encode('bz2'))
 
1261
        bundle_txt.seek(0)
 
1262
        bundle = read_bundle(bundle_txt)
 
1263
        self.valid_apply_bundle(base_rev_id, bundle)
 
1264
        return bundle
 
1265
 
 
1266
    def create_bundle_text(self, base_rev_id, rev_id):
 
1267
        bundle_txt = StringIO()
 
1268
        rev_ids = write_bundle(self.b1.repository, rev_id, base_rev_id, 
 
1269
                               bundle_txt, format=self.format)
 
1270
        bundle_txt.seek(0)
 
1271
        self.assertEqual(bundle_txt.readline(), 
 
1272
                         '# Bazaar revision bundle v%s\n' % self.format)
 
1273
        self.assertEqual(bundle_txt.readline(), '#\n')
 
1274
        rev = self.b1.repository.get_revision(rev_id)
 
1275
        bundle_txt.seek(0)
 
1276
        return bundle_txt, rev_ids
 
1277
 
 
1278
    def get_bundle_tree(self, bundle, revision_id):
 
1279
        repository = self.make_repository('repo')
 
1280
        bundle.install_revisions(repository)
 
1281
        return repository.revision_tree(revision_id)
 
1282
 
 
1283
    def test_creation(self):
 
1284
        tree = self.make_branch_and_tree('tree')
 
1285
        self.build_tree_contents([('tree/file', 'contents1\nstatic\n')])
 
1286
        tree.add('file', 'fileid-2')
 
1287
        tree.commit('added file', rev_id='rev1')
 
1288
        self.build_tree_contents([('tree/file', 'contents2\nstatic\n')])
 
1289
        tree.commit('changed file', rev_id='rev2')
 
1290
        s = StringIO()
 
1291
        serializer = BundleSerializerV4('1.0')
 
1292
        serializer.write(tree.branch.repository, ['rev1', 'rev2'], {}, s)
 
1293
        s.seek(0)
 
1294
        tree2 = self.make_branch_and_tree('target')
 
1295
        target_repo = tree2.branch.repository
 
1296
        install_bundle(target_repo, serializer.read(s))
 
1297
        vf = target_repo.weave_store.get_weave('fileid-2',
 
1298
            target_repo.get_transaction())
 
1299
        self.assertEqual('contents1\nstatic\n', vf.get_text('rev1'))
 
1300
        self.assertEqual('contents2\nstatic\n', vf.get_text('rev2'))
 
1301
        rtree = target_repo.revision_tree('rev2')
 
1302
        inventory_vf = target_repo.get_inventory_weave()
 
1303
        self.assertEqual(['rev1'], inventory_vf.get_parents('rev2'))
 
1304
        self.assertEqual('changed file',
 
1305
                         target_repo.get_revision('rev2').message)
 
1306
 
 
1307
    @staticmethod
 
1308
    def get_raw(bundle_file):
 
1309
        bundle_file.seek(0)
 
1310
        line = bundle_file.readline()
 
1311
        line = bundle_file.readline()
 
1312
        lines = bundle_file.readlines()
 
1313
        return ''.join(lines).decode('bz2')
 
1314
 
 
1315
    def test_copy_signatures(self):
 
1316
        tree_a = self.make_branch_and_tree('tree_a')
 
1317
        import bzrlib.gpg
 
1318
        import bzrlib.commit as commit
 
1319
        oldstrategy = bzrlib.gpg.GPGStrategy
 
1320
        branch = tree_a.branch
 
1321
        repo_a = branch.repository
 
1322
        tree_a.commit("base", allow_pointless=True, rev_id='A')
 
1323
        self.failIf(branch.repository.has_signature_for_revision_id('A'))
 
1324
        try:
 
1325
            from bzrlib.testament import Testament
 
1326
            # monkey patch gpg signing mechanism
 
1327
            bzrlib.gpg.GPGStrategy = bzrlib.gpg.LoopbackGPGStrategy
 
1328
            new_config = test_commit.MustSignConfig(branch)
 
1329
            commit.Commit(config=new_config).commit(message="base",
 
1330
                                                    allow_pointless=True,
 
1331
                                                    rev_id='B',
 
1332
                                                    working_tree=tree_a)
 
1333
            def sign(text):
 
1334
                return bzrlib.gpg.LoopbackGPGStrategy(None).sign(text)
 
1335
            self.assertTrue(repo_a.has_signature_for_revision_id('B'))
 
1336
        finally:
 
1337
            bzrlib.gpg.GPGStrategy = oldstrategy
 
1338
        tree_b = self.make_branch_and_tree('tree_b')
 
1339
        repo_b = tree_b.branch.repository
 
1340
        s = StringIO()
 
1341
        serializer = BundleSerializerV4('4')
 
1342
        serializer.write(tree_a.branch.repository, ['A', 'B'], {}, s)
 
1343
        s.seek(0)
 
1344
        install_bundle(repo_b, serializer.read(s))
 
1345
        self.assertTrue(repo_b.has_signature_for_revision_id('B'))
 
1346
        self.assertEqual(repo_b.get_signature_text('B'),
 
1347
                         repo_a.get_signature_text('B'))
 
1348
        s.seek(0)
 
1349
        # ensure repeat installs are harmless
 
1350
        install_bundle(repo_b, serializer.read(s))
 
1351
 
 
1352
 
 
1353
class V4WeaveBundleTester(V4BundleTester):
 
1354
 
 
1355
    def bzrdir_format(self):
 
1356
        return 'metaweave'
 
1357
 
 
1358
 
 
1359
class MungedBundleTester(object):
 
1360
 
 
1361
    def build_test_bundle(self):
 
1362
        wt = self.make_branch_and_tree('b1')
 
1363
 
 
1364
        self.build_tree(['b1/one'])
 
1365
        wt.add('one')
 
1366
        wt.commit('add one', rev_id='a@cset-0-1')
 
1367
        self.build_tree(['b1/two'])
 
1368
        wt.add('two')
 
1369
        wt.commit('add two', rev_id='a@cset-0-2',
 
1370
                  revprops={'branch-nick':'test'})
 
1371
 
 
1372
        bundle_txt = StringIO()
 
1373
        rev_ids = write_bundle(wt.branch.repository, 'a@cset-0-2',
 
1374
                               'a@cset-0-1', bundle_txt, self.format)
 
1375
        self.assertEqual(set(['a@cset-0-2']), set(rev_ids))
 
1376
        bundle_txt.seek(0, 0)
 
1377
        return bundle_txt
 
1378
 
 
1379
    def check_valid(self, bundle):
 
1380
        """Check that after whatever munging, the final object is valid."""
 
1381
        self.assertEqual(['a@cset-0-2'],
 
1382
            [r.revision_id for r in bundle.real_revisions])
 
1383
 
 
1384
    def test_extra_whitespace(self):
 
1385
        bundle_txt = self.build_test_bundle()
 
1386
 
 
1387
        # Seek to the end of the file
 
1388
        # Adding one extra newline used to give us
 
1389
        # TypeError: float() argument must be a string or a number
 
1390
        bundle_txt.seek(0, 2)
 
1391
        bundle_txt.write('\n')
 
1392
        bundle_txt.seek(0)
 
1393
 
 
1394
        bundle = read_bundle(bundle_txt)
 
1395
        self.check_valid(bundle)
 
1396
 
 
1397
    def test_extra_whitespace_2(self):
 
1398
        bundle_txt = self.build_test_bundle()
 
1399
 
 
1400
        # Seek to the end of the file
 
1401
        # Adding two extra newlines used to give us
 
1402
        # MalformedPatches: The first line of all patches should be ...
 
1403
        bundle_txt.seek(0, 2)
 
1404
        bundle_txt.write('\n\n')
 
1405
        bundle_txt.seek(0)
 
1406
 
 
1407
        bundle = read_bundle(bundle_txt)
 
1408
        self.check_valid(bundle)
 
1409
 
 
1410
 
 
1411
class MungedBundleTesterV09(TestCaseWithTransport, MungedBundleTester):
 
1412
 
 
1413
    format = '0.9'
 
1414
 
 
1415
    def test_missing_trailing_whitespace(self):
 
1416
        bundle_txt = self.build_test_bundle()
 
1417
 
 
1418
        # Remove a trailing newline, it shouldn't kill the parser
 
1419
        raw = bundle_txt.getvalue()
 
1420
        # The contents of the bundle don't have to be this, but this
 
1421
        # test is concerned with the exact case where the serializer
 
1422
        # creates a blank line at the end, and fails if that
 
1423
        # line is stripped
 
1424
        self.assertEqual('\n\n', raw[-2:])
 
1425
        bundle_txt = StringIO(raw[:-1])
 
1426
 
 
1427
        bundle = read_bundle(bundle_txt)
 
1428
        self.check_valid(bundle)
 
1429
 
 
1430
    def test_opening_text(self):
 
1431
        bundle_txt = self.build_test_bundle()
 
1432
 
 
1433
        bundle_txt = StringIO("Some random\nemail comments\n"
 
1434
                              + bundle_txt.getvalue())
 
1435
 
 
1436
        bundle = read_bundle(bundle_txt)
 
1437
        self.check_valid(bundle)
 
1438
 
 
1439
    def test_trailing_text(self):
 
1440
        bundle_txt = self.build_test_bundle()
 
1441
 
 
1442
        bundle_txt = StringIO(bundle_txt.getvalue() +
 
1443
                              "Some trailing\nrandom\ntext\n")
 
1444
 
 
1445
        bundle = read_bundle(bundle_txt)
 
1446
        self.check_valid(bundle)
 
1447
 
 
1448
 
 
1449
class MungedBundleTesterV4(TestCaseWithTransport, MungedBundleTester):
 
1450
 
 
1451
    format = '4'
 
1452
 
 
1453
 
 
1454
class TestBundleWriterReader(TestCase):
 
1455
 
 
1456
    def test_roundtrip_record(self):
 
1457
        fileobj = StringIO()
 
1458
        writer = v4.BundleWriter(fileobj)
 
1459
        writer.begin()
 
1460
        writer.add_info_record(foo='bar')
 
1461
        writer._add_record("Record body", {'parents': ['1', '3'],
 
1462
            'storage_kind':'fulltext'}, 'file', 'revid', 'fileid')
 
1463
        writer.end()
 
1464
        fileobj.seek(0)
 
1465
        record_iter = v4.BundleReader(fileobj).iter_records()
 
1466
        record = record_iter.next()
 
1467
        self.assertEqual((None, {'foo': 'bar', 'storage_kind': 'header'},
 
1468
            'info', None, None), record)
 
1469
        record = record_iter.next()
 
1470
        self.assertEqual(("Record body", {'storage_kind': 'fulltext',
 
1471
                          'parents': ['1', '3']}, 'file', 'revid', 'fileid'),
 
1472
                          record)
 
1473
 
 
1474
    def test_encode_name(self):
 
1475
        self.assertEqual('revision/rev1',
 
1476
            v4.BundleWriter.encode_name('revision', 'rev1'))
 
1477
        self.assertEqual('file/rev//1/file-id-1',
 
1478
            v4.BundleWriter.encode_name('file', 'rev/1', 'file-id-1'))
 
1479
        self.assertEqual('info',
 
1480
            v4.BundleWriter.encode_name('info', None, None))
 
1481
 
 
1482
    def test_decode_name(self):
 
1483
        self.assertEqual(('revision', 'rev1', None),
 
1484
            v4.BundleReader.decode_name('revision/rev1'))
 
1485
        self.assertEqual(('file', 'rev/1', 'file-id-1'),
 
1486
            v4.BundleReader.decode_name('file/rev//1/file-id-1'))
 
1487
        self.assertEqual(('info', None, None),
 
1488
                         v4.BundleReader.decode_name('info'))
 
1489
 
 
1490
    def test_too_many_names(self):
 
1491
        fileobj = StringIO()
 
1492
        writer = v4.BundleWriter(fileobj)
 
1493
        writer.begin()
 
1494
        writer.add_info_record(foo='bar')
 
1495
        writer._container.add_bytes_record('blah', ['two', 'names'])
 
1496
        writer.end()
 
1497
        fileobj.seek(0)
 
1498
        record_iter = v4.BundleReader(fileobj).iter_records()
 
1499
        record = record_iter.next()
 
1500
        self.assertEqual((None, {'foo': 'bar', 'storage_kind': 'header'},
 
1501
            'info', None, None), record)
 
1502
        self.assertRaises(BadBundle, record_iter.next)