~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_bundle.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2006-07-06 03:15:29 UTC
  • mfrom: (1711.2.78 jam-integration)
  • Revision ID: pqm@pqm.ubuntu.com-20060706031529-e189d8c3f42076be
(jam) allow plugins to include benchmarks

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2004, 2005, 2006, 2007 Canonical Ltd
2
 
#
 
1
# Copyright (C) 2004-2006 by Canonical Ltd
 
2
 
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
7
 
#
 
7
 
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
# GNU General Public License for more details.
12
 
#
 
12
 
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
19
import sys
20
20
import tempfile
21
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
 
22
from bzrlib.builtins import merge
31
23
from bzrlib.bzrdir import BzrDir
32
24
from bzrlib.bundle.apply_bundle import install_bundle, merge_bundle
33
25
from bzrlib.bundle.bundle_data import BundleTree
34
26
from bzrlib.bundle.serializer import write_bundle, read_bundle
35
 
from bzrlib.bundle.serializer.v08 import BundleSerializerV08
36
 
from bzrlib.bundle.serializer.v09 import BundleSerializerV09
37
27
from bzrlib.branch import Branch
38
28
from bzrlib.diff import internal_diff
39
 
from bzrlib.errors import (BzrError, TestamentMismatch, NotABundle, BadBundle, 
40
 
                           NoSuchFile,)
 
29
from bzrlib.delta import compare_trees
 
30
from bzrlib.errors import BzrError, TestamentMismatch, NotABundle, BadBundle
41
31
from bzrlib.merge import Merge3Merger
42
 
from bzrlib.repofmt import knitrepo
43
32
from bzrlib.osutils import has_symlinks, sha_file
44
33
from bzrlib.tests import (TestCaseInTempDir, TestCaseWithTransport,
45
34
                          TestCase, TestSkipped)
49
38
 
50
39
class MockTree(object):
51
40
    def __init__(self):
52
 
        from bzrlib.inventory import InventoryDirectory, ROOT_ID
 
41
        from bzrlib.inventory import RootEntry, ROOT_ID
53
42
        object.__init__(self)
54
43
        self.paths = {ROOT_ID: ""}
55
44
        self.ids = {"": ROOT_ID}
56
45
        self.contents = {}
57
 
        self.root = InventoryDirectory(ROOT_ID, '', None)
 
46
        self.root = RootEntry(ROOT_ID)
58
47
 
59
48
    inventory = property(lambda x:x)
60
49
 
301
290
    def test_iteration(self):
302
291
        """Ensure that iteration through ids works properly"""
303
292
        btree = self.make_tree_1()[0]
304
 
        self.assertEqual(self.sorted_ids(btree),
305
 
            [inventory.ROOT_ID, 'a', 'b', 'c', 'd'])
 
293
        self.assertEqual(self.sorted_ids(btree), ['a', 'b', 'c', 'd'])
306
294
        btree.note_deletion("grandparent/parent/file")
307
295
        btree.note_id("e", "grandparent/alt_parent/fool", kind="directory")
308
296
        btree.note_last_changed("grandparent/alt_parent/fool", 
309
297
                                "revisionidiguess")
310
 
        self.assertEqual(self.sorted_ids(btree),
311
 
            [inventory.ROOT_ID, 'a', 'b', 'd', 'e'])
312
 
 
313
 
 
314
 
class BundleTester1(TestCaseWithTransport):
315
 
 
316
 
    def test_mismatched_bundle(self):
317
 
        format = bzrdir.BzrDirMetaFormat1()
318
 
        format.repository_format = knitrepo.RepositoryFormatKnit3()
319
 
        serializer = BundleSerializerV08('0.8')
320
 
        b = self.make_branch('.', format=format)
321
 
        self.assertRaises(errors.IncompatibleBundleFormat, serializer.write, 
322
 
                          b.repository, [], {}, StringIO())
323
 
 
324
 
    def test_matched_bundle(self):
325
 
        """Don't raise IncompatibleBundleFormat for knit2 and bundle0.9"""
326
 
        format = bzrdir.BzrDirMetaFormat1()
327
 
        format.repository_format = knitrepo.RepositoryFormatKnit3()
328
 
        serializer = BundleSerializerV09('0.9')
329
 
        b = self.make_branch('.', format=format)
330
 
        serializer.write(b.repository, [], {}, StringIO())
331
 
 
332
 
    def test_mismatched_model(self):
333
 
        """Try copying a bundle from knit2 to knit1"""
334
 
        format = bzrdir.BzrDirMetaFormat1()
335
 
        format.repository_format = knitrepo.RepositoryFormatKnit3()
336
 
        source = self.make_branch_and_tree('source', format=format)
337
 
        source.commit('one', rev_id='one-id')
338
 
        source.commit('two', rev_id='two-id')
339
 
        text = StringIO()
340
 
        write_bundle(source.branch.repository, 'two-id', 'null:', text,
341
 
                     format='0.9')
342
 
        text.seek(0)
343
 
 
344
 
        format = bzrdir.BzrDirMetaFormat1()
345
 
        format.repository_format = knitrepo.RepositoryFormatKnit1()
346
 
        target = self.make_branch('target', format=format)
347
 
        self.assertRaises(errors.IncompatibleRevision, install_bundle, 
348
 
                          target.repository, read_bundle(text))
349
 
 
350
 
 
351
 
class V08BundleTester(TestCaseWithTransport):
352
 
 
353
 
    format = '0.8'
354
 
 
355
 
    def bzrdir_format(self):
356
 
        format = bzrdir.BzrDirMetaFormat1()
357
 
        format.repository_format = knitrepo.RepositoryFormatKnit1()
358
 
        return format
359
 
 
360
 
    def make_branch_and_tree(self, path, format=None):
361
 
        if format is None:
362
 
            format = self.bzrdir_format()
363
 
        return TestCaseWithTransport.make_branch_and_tree(self, path, format)
364
 
 
365
 
    def make_branch(self, path, format=None):
366
 
        if format is None:
367
 
            format = self.bzrdir_format()
368
 
        return TestCaseWithTransport.make_branch(self, path, format)
 
298
        self.assertEqual(self.sorted_ids(btree), ['a', 'b', 'd', 'e'])
 
299
 
 
300
 
 
301
class BundleTester(TestCaseWithTransport):
369
302
 
370
303
    def create_bundle_text(self, base_rev_id, rev_id):
371
304
        bundle_txt = StringIO()
372
305
        rev_ids = write_bundle(self.b1.repository, rev_id, base_rev_id, 
373
 
                               bundle_txt, format=self.format)
 
306
                               bundle_txt)
374
307
        bundle_txt.seek(0)
375
308
        self.assertEqual(bundle_txt.readline(), 
376
 
                         '# Bazaar revision bundle v%s\n' % self.format)
 
309
                         '# Bazaar revision bundle v0.8\n')
377
310
        self.assertEqual(bundle_txt.readline(), '#\n')
378
311
 
379
312
        rev = self.b1.repository.get_revision(rev_id)
439
372
 
440
373
    def test_crlf_bundle(self):
441
374
        try:
442
 
            read_bundle(StringIO('# Bazaar revision bundle v0.8\r\n'))
 
375
            read_bundle(StringIO('# Bazaar revision bundle v0.7\r\n'))
443
376
        except BadBundle:
444
377
            # It is currently permitted for bundles with crlf line endings to
445
378
            # make read_bundle raise a BadBundle, but this should be fixed.
455
388
        else:
456
389
            if not os.path.exists(checkout_dir):
457
390
                os.mkdir(checkout_dir)
458
 
        tree = self.make_branch_and_tree(checkout_dir)
 
391
        tree = BzrDir.create_standalone_workingtree(checkout_dir)
459
392
        s = StringIO()
460
 
        ancestors = write_bundle(self.b1.repository, rev_id, 'null:', s,
461
 
                                 format=self.format)
 
393
        ancestors = write_bundle(self.b1.repository, rev_id, None, s)
462
394
        s.seek(0)
463
395
        assert isinstance(s.getvalue(), str), (
464
396
            "Bundle isn't a bytestring:\n %s..." % repr(s.getvalue())[:40])
468
400
            new = tree.branch.repository.revision_tree(ancestor)
469
401
 
470
402
            # Check that there aren't any inventory level changes
471
 
            delta = new.changes_from(old)
 
403
            delta = compare_trees(old, new)
472
404
            self.assertFalse(delta.has_changed(),
473
405
                             'Revision %s not copied correctly.'
474
406
                             % (ancestor,))
477
409
            for inventory_id in old:
478
410
                try:
479
411
                    old_file = old.get_file(inventory_id)
480
 
                except NoSuchFile:
 
412
                except:
481
413
                    continue
482
414
                if old_file is None:
483
415
                    continue
484
416
                self.assertEqual(old_file.read(),
485
417
                                 new.get_file(inventory_id).read())
486
 
        if not _mod_revision.is_null(rev_id):
 
418
        if rev_id is not None:
487
419
            rh = self.b1.revision_history()
488
420
            tree.branch.set_revision_history(rh[:rh.index(rev_id)+1])
489
421
            tree.update()
490
 
            delta = tree.changes_from(self.b1.repository.revision_tree(rev_id))
 
422
            delta = compare_trees(self.b1.repository.revision_tree(rev_id),
 
423
                                  tree)
491
424
            self.assertFalse(delta.has_changed(),
492
 
                             'Working tree has modifications: %s' % delta)
 
425
                             'Working tree has modifications')
493
426
        return tree
494
427
 
495
428
    def valid_apply_bundle(self, base_rev_id, info, checkout_dir=None):
497
430
        sure everything matches the builtin branch.
498
431
        """
499
432
        to_tree = self.get_checkout(base_rev_id, checkout_dir=checkout_dir)
500
 
        original_parents = to_tree.get_parent_ids()
501
433
        repository = to_tree.branch.repository
502
 
        original_parents = to_tree.get_parent_ids()
503
434
        self.assertIs(repository.has_revision(base_rev_id), True)
504
435
        for rev in info.real_revisions:
505
436
            self.assert_(not repository.has_revision(rev.revision_id),
515
446
        self.assert_(to_tree.branch.repository.has_revision(info.target))
516
447
        # Do we also want to verify that all the texts have been added?
517
448
 
518
 
        self.assertEqual(original_parents + [info.target],
519
 
            to_tree.get_parent_ids())
 
449
        self.assert_(info.target in to_tree.pending_merges())
 
450
 
520
451
 
521
452
        rev = info.real_revisions[-1]
522
453
        base_tree = self.b1.repository.revision_tree(rev.revision_id)
550
481
        self.tree1.add('one')
551
482
        self.tree1.commit('add one', rev_id='a@cset-0-1')
552
483
 
553
 
        bundle = self.get_valid_bundle('null:', 'a@cset-0-1')
 
484
        bundle = self.get_valid_bundle(None, 'a@cset-0-1')
554
485
        # FIXME: The current write_bundle api no longer supports
555
486
        #        setting a custom summary message
556
487
        #        We should re-introduce the ability, and update
592
523
        bundle = self.get_valid_bundle('a@cset-0-1', 'a@cset-0-2')
593
524
 
594
525
        # Check a rollup bundle 
595
 
        bundle = self.get_valid_bundle('null:', 'a@cset-0-2')
 
526
        bundle = self.get_valid_bundle(None, 'a@cset-0-2')
596
527
 
597
528
        # Now delete entries
598
529
        self.tree1.remove(
610
541
        self.assertRaises(TestamentMismatch, self.get_invalid_bundle, 
611
542
                          'a@cset-0-2', 'a@cset-0-3')
612
543
        # Check a rollup bundle 
613
 
        bundle = self.get_valid_bundle('null:', 'a@cset-0-3')
 
544
        bundle = self.get_valid_bundle(None, 'a@cset-0-3')
614
545
 
615
546
        # Now move the directory
616
547
        self.tree1.rename_one('dir', 'sub/dir')
618
549
 
619
550
        bundle = self.get_valid_bundle('a@cset-0-3', 'a@cset-0-4')
620
551
        # Check a rollup bundle 
621
 
        bundle = self.get_valid_bundle('null:', 'a@cset-0-4')
 
552
        bundle = self.get_valid_bundle(None, 'a@cset-0-4')
622
553
 
623
554
        # Modified files
624
555
        open('b1/sub/dir/WithCaps.txt', 'ab').write('\nAdding some text\n')
636
567
                          verbose=False)
637
568
        bundle = self.get_valid_bundle('a@cset-0-5', 'a@cset-0-6')
638
569
        other = self.get_checkout('a@cset-0-5')
639
 
        tree1_inv = self.tree1.branch.repository.get_inventory_xml(
640
 
            'a@cset-0-5')
641
 
        tree2_inv = other.branch.repository.get_inventory_xml('a@cset-0-5')
642
 
        self.assertEqualDiff(tree1_inv, tree2_inv)
643
570
        other.rename_one('sub/dir/nolastnewline.txt', 'sub/nolastnewline.txt')
644
571
        other.commit('rename file', rev_id='a@cset-0-6b')
645
 
        _merge_helper([other.basedir, -1], [None, None],
646
 
                      this_dir=self.tree1.basedir)
 
572
        merge([other.basedir, -1], [None, None], this_dir=self.tree1.basedir)
647
573
        self.tree1.commit(u'Merge', rev_id='a@cset-0-7',
648
574
                          verbose=False)
649
575
        bundle = self.get_valid_bundle('a@cset-0-6', 'a@cset-0-7')
651
577
    def test_symlink_bundle(self):
652
578
        if not has_symlinks():
653
579
            raise TestSkipped("No symlink support")
654
 
        self.tree1 = self.make_branch_and_tree('b1')
 
580
        self.tree1 = BzrDir.create_standalone_workingtree('b1')
655
581
        self.b1 = self.tree1.branch
656
582
        tt = TreeTransform(self.tree1)
657
583
        tt.new_symlink('link', tt.root, 'bar/foo', 'link-1')
658
584
        tt.apply()
659
585
        self.tree1.commit('add symlink', rev_id='l@cset-0-1')
660
 
        self.get_valid_bundle('null:', 'l@cset-0-1')
 
586
        self.get_valid_bundle(None, 'l@cset-0-1')
661
587
        tt = TreeTransform(self.tree1)
662
588
        trans_id = tt.trans_id_tree_file_id('link-1')
663
589
        tt.adjust_path('link2', tt.root, trans_id)
681
607
        self.get_valid_bundle('l@cset-0-3', 'l@cset-0-4')
682
608
 
683
609
    def test_binary_bundle(self):
684
 
        self.tree1 = self.make_branch_and_tree('b1')
 
610
        self.tree1 = BzrDir.create_standalone_workingtree('b1')
685
611
        self.b1 = self.tree1.branch
686
612
        tt = TreeTransform(self.tree1)
687
 
        
688
 
        # Add
689
 
        tt.new_file('file', tt.root, '\x00\n\x00\r\x01\n\x02\r\xff', 'binary-1')
690
 
        tt.new_file('file2', tt.root, '\x01\n\x02\r\x03\n\x04\r\xff', 'binary-2')
 
613
        tt.new_file('file', tt.root, '\x00\xff', 'binary-1')
 
614
        tt.new_file('file2', tt.root, '\x00\xff', 'binary-2')
691
615
        tt.apply()
692
616
        self.tree1.commit('add binary', rev_id='b@cset-0-1')
693
 
        self.get_valid_bundle('null:', 'b@cset-0-1')
694
 
 
695
 
        # Delete
 
617
        self.get_valid_bundle(None, 'b@cset-0-1')
696
618
        tt = TreeTransform(self.tree1)
697
619
        trans_id = tt.trans_id_tree_file_id('binary-1')
698
620
        tt.delete_contents(trans_id)
699
621
        tt.apply()
700
622
        self.tree1.commit('delete binary', rev_id='b@cset-0-2')
701
623
        self.get_valid_bundle('b@cset-0-1', 'b@cset-0-2')
702
 
 
703
 
        # Rename & modify
704
624
        tt = TreeTransform(self.tree1)
705
625
        trans_id = tt.trans_id_tree_file_id('binary-2')
706
626
        tt.adjust_path('file3', tt.root, trans_id)
707
627
        tt.delete_contents(trans_id)
708
 
        tt.create_file('file\rcontents\x00\n\x00', trans_id)
 
628
        tt.create_file('filecontents\x00', trans_id)
709
629
        tt.apply()
710
630
        self.tree1.commit('rename and modify binary', rev_id='b@cset-0-3')
711
631
        self.get_valid_bundle('b@cset-0-2', 'b@cset-0-3')
712
 
 
713
 
        # Modify
714
632
        tt = TreeTransform(self.tree1)
715
633
        trans_id = tt.trans_id_tree_file_id('binary-2')
716
634
        tt.delete_contents(trans_id)
717
 
        tt.create_file('\x00file\rcontents', trans_id)
 
635
        tt.create_file('\x00filecontents', trans_id)
718
636
        tt.apply()
719
637
        self.tree1.commit('just modify binary', rev_id='b@cset-0-4')
720
638
        self.get_valid_bundle('b@cset-0-3', 'b@cset-0-4')
721
639
 
722
 
        # Rollup
723
 
        self.get_valid_bundle('null:', 'b@cset-0-4')
724
 
 
725
640
    def test_last_modified(self):
726
 
        self.tree1 = self.make_branch_and_tree('b1')
 
641
        self.tree1 = BzrDir.create_standalone_workingtree('b1')
727
642
        self.b1 = self.tree1.branch
728
643
        tt = TreeTransform(self.tree1)
729
644
        tt.new_file('file', tt.root, 'file', 'file')
744
659
        tt.create_file('file2', trans_id)
745
660
        tt.apply()
746
661
        other.commit('modify text in another tree', rev_id='a@lmod-0-2b')
747
 
        _merge_helper([other.basedir, -1], [None, None],
748
 
                      this_dir=self.tree1.basedir)
 
662
        merge([other.basedir, -1], [None, None], this_dir=self.tree1.basedir)
749
663
        self.tree1.commit(u'Merge', rev_id='a@lmod-0-3',
750
664
                          verbose=False)
751
665
        self.tree1.commit(u'Merge', rev_id='a@lmod-0-4')
752
666
        bundle = self.get_valid_bundle('a@lmod-0-2a', 'a@lmod-0-4')
753
667
 
754
668
    def test_hide_history(self):
755
 
        self.tree1 = self.make_branch_and_tree('b1')
 
669
        self.tree1 = BzrDir.create_standalone_workingtree('b1')
756
670
        self.b1 = self.tree1.branch
757
671
 
758
672
        open('b1/one', 'wb').write('one\n')
764
678
        self.tree1.commit('modify', rev_id='a@cset-0-3')
765
679
        bundle_file = StringIO()
766
680
        rev_ids = write_bundle(self.tree1.branch.repository, 'a@cset-0-3',
767
 
                               'a@cset-0-1', bundle_file, format=self.format)
768
 
        self.assertNotContainsRe(bundle_file.getvalue(), '\btwo\b')
 
681
                               'a@cset-0-1', bundle_file)
 
682
        self.assertNotContainsRe(bundle_file.getvalue(), 'two')
769
683
        self.assertContainsRe(bundle_file.getvalue(), 'one')
770
684
        self.assertContainsRe(bundle_file.getvalue(), 'three')
771
685
 
785
699
            u'William Dod\xe9\n').encode('utf-8'))
786
700
        f.close()
787
701
 
788
 
        self.tree1.add([u'with Dod\xe9'], ['withdod-id'])
789
 
        self.tree1.commit(u'i18n commit from William Dod\xe9',
 
702
        self.tree1.add([u'with Dod\xe9'])
 
703
        self.tree1.commit(u'i18n commit from William Dod\xe9', 
790
704
                          rev_id='i18n-1', committer=u'William Dod\xe9')
791
705
 
792
 
        if sys.platform == 'darwin':
793
 
            # On Mac the '\xe9' gets changed to 'e\u0301'
794
 
            self.assertEqual([u'.bzr', u'with Dode\u0301'],
795
 
                             sorted(os.listdir(u'b1')))
796
 
            delta = self.tree1.changes_from(self.tree1.basis_tree())
797
 
            self.assertEqual([(u'with Dod\xe9', 'withdod-id', 'file')],
798
 
                             delta.removed)
799
 
            self.knownFailure("Mac OSX doesn't preserve unicode"
800
 
                              " combining characters.")
801
 
 
802
706
        # Add
803
 
        bundle = self.get_valid_bundle('null:', 'i18n-1')
 
707
        bundle = self.get_valid_bundle(None, 'i18n-1')
804
708
 
805
709
        # Modified
806
710
        f = open(u'b1/with Dod\xe9', 'wb')
824
728
        bundle = self.get_valid_bundle('i18n-3', 'i18n-4')
825
729
 
826
730
        # Rollup
827
 
        bundle = self.get_valid_bundle('null:', 'i18n-4')
 
731
        bundle = self.get_valid_bundle(None, 'i18n-4')
828
732
 
829
733
 
830
734
    def test_whitespace_bundle(self):
842
746
        # Added
843
747
        self.tree1.commit('funky whitespace', rev_id='white-1')
844
748
 
845
 
        bundle = self.get_valid_bundle('null:', 'white-1')
 
749
        bundle = self.get_valid_bundle(None, 'white-1')
846
750
 
847
751
        # Modified
848
752
        open('b1/trailing space ', 'ab').write('add some text\n')
863
767
        bundle = self.get_valid_bundle('white-3', 'white-4')
864
768
        
865
769
        # Now test a complet roll-up
866
 
        bundle = self.get_valid_bundle('null:', 'white-4')
867
 
 
868
 
    def test_alt_timezone_bundle(self):
869
 
        self.tree1 = self.make_branch_and_memory_tree('b1')
870
 
        self.b1 = self.tree1.branch
871
 
        builder = treebuilder.TreeBuilder()
872
 
 
873
 
        self.tree1.lock_write()
874
 
        builder.start_tree(self.tree1)
875
 
        builder.build(['newfile'])
876
 
        builder.finish_tree()
877
 
 
878
 
        # Asia/Colombo offset = 5 hours 30 minutes
879
 
        self.tree1.commit('non-hour offset timezone', rev_id='tz-1',
880
 
                          timezone=19800, timestamp=1152544886.0)
881
 
 
882
 
        bundle = self.get_valid_bundle('null:', 'tz-1')
883
 
        
884
 
        rev = bundle.revisions[0]
885
 
        self.assertEqual('Mon 2006-07-10 20:51:26.000000000 +0530', rev.date)
886
 
        self.assertEqual(19800, rev.timezone)
887
 
        self.assertEqual(1152544886.0, rev.timestamp)
888
 
        self.tree1.unlock()
889
 
 
890
 
    def test_bundle_root_id(self):
891
 
        self.tree1 = self.make_branch_and_tree('b1')
892
 
        self.b1 = self.tree1.branch
893
 
        self.tree1.commit('message', rev_id='revid1')
894
 
        bundle = self.get_valid_bundle('null:', 'revid1')
895
 
        tree = bundle.revision_tree(self.b1.repository, 'revid1')
896
 
        self.assertEqual('revid1', tree.inventory.root.revision)
897
 
 
898
 
    def test_install_revisions(self):
899
 
        self.tree1 = self.make_branch_and_tree('b1')
900
 
        self.b1 = self.tree1.branch
901
 
        self.tree1.commit('message', rev_id='rev2a')
902
 
        bundle = self.get_valid_bundle('null:', 'rev2a')
903
 
        branch2 = self.make_branch('b2')
904
 
        self.assertFalse(branch2.repository.has_revision('rev2a'))
905
 
        target_revision = bundle.install_revisions(branch2.repository)
906
 
        self.assertTrue(branch2.repository.has_revision('rev2a'))
907
 
        self.assertEqual('rev2a', target_revision)
908
 
 
909
 
    def test_bundle_empty_property(self):
910
 
        """Test serializing revision properties with an empty value."""
911
 
        tree = self.make_branch_and_memory_tree('tree')
912
 
        tree.lock_write()
913
 
        self.addCleanup(tree.unlock)
914
 
        tree.add([''], ['TREE_ROOT'])
915
 
        tree.commit('One', revprops={'one':'two', 'empty':''}, rev_id='rev1')
916
 
        self.b1 = tree.branch
917
 
        bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
918
 
        self.assertContainsRe(bundle_sio.getvalue(),
919
 
                              '# properties:\n'
920
 
                              '#   branch-nick: tree\n'
921
 
                              '#   empty: \n'
922
 
                              '#   one: two\n'
923
 
                             )
924
 
        bundle = read_bundle(bundle_sio)
925
 
        revision_info = bundle.revisions[0]
926
 
        self.assertEqual('rev1', revision_info.revision_id)
927
 
        rev = revision_info.as_revision()
928
 
        self.assertEqual({'branch-nick':'tree', 'empty':'', 'one':'two'},
929
 
                         rev.properties)
930
 
 
931
 
    def test_bundle_empty_property_alt(self):
932
 
        """Test serializing revision properties with an empty value.
933
 
 
934
 
        Older readers had a bug when reading an empty property.
935
 
        They assumed that all keys ended in ': \n'. However they would write an
936
 
        empty value as ':\n'. This tests make sure that all newer bzr versions
937
 
        can handle th second form.
938
 
        """
939
 
        tree = self.make_branch_and_memory_tree('tree')
940
 
        tree.lock_write()
941
 
        self.addCleanup(tree.unlock)
942
 
        tree.add([''], ['TREE_ROOT'])
943
 
        tree.commit('One', revprops={'one':'two', 'empty':''}, rev_id='rev1')
944
 
        self.b1 = tree.branch
945
 
        bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
946
 
        txt = bundle_sio.getvalue()
947
 
        loc = txt.find('#   empty: ') + len('#   empty:')
948
 
        # Create a new bundle, which strips the trailing space after empty
949
 
        bundle_sio = StringIO(txt[:loc] + txt[loc+1:])
950
 
 
951
 
        self.assertContainsRe(bundle_sio.getvalue(),
952
 
                              '# properties:\n'
953
 
                              '#   branch-nick: tree\n'
954
 
                              '#   empty:\n'
955
 
                              '#   one: two\n'
956
 
                             )
957
 
        bundle = read_bundle(bundle_sio)
958
 
        revision_info = bundle.revisions[0]
959
 
        self.assertEqual('rev1', revision_info.revision_id)
960
 
        rev = revision_info.as_revision()
961
 
        self.assertEqual({'branch-nick':'tree', 'empty':'', 'one':'two'},
962
 
                         rev.properties)
963
 
 
964
 
    def test_bundle_sorted_properties(self):
965
 
        """For stability the writer should write properties in sorted order."""
966
 
        tree = self.make_branch_and_memory_tree('tree')
967
 
        tree.lock_write()
968
 
        self.addCleanup(tree.unlock)
969
 
 
970
 
        tree.add([''], ['TREE_ROOT'])
971
 
        tree.commit('One', rev_id='rev1',
972
 
                    revprops={'a':'4', 'b':'3', 'c':'2', 'd':'1'})
973
 
        self.b1 = tree.branch
974
 
        bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
975
 
        self.assertContainsRe(bundle_sio.getvalue(),
976
 
                              '# properties:\n'
977
 
                              '#   a: 4\n'
978
 
                              '#   b: 3\n'
979
 
                              '#   branch-nick: tree\n'
980
 
                              '#   c: 2\n'
981
 
                              '#   d: 1\n'
982
 
                             )
983
 
        bundle = read_bundle(bundle_sio)
984
 
        revision_info = bundle.revisions[0]
985
 
        self.assertEqual('rev1', revision_info.revision_id)
986
 
        rev = revision_info.as_revision()
987
 
        self.assertEqual({'branch-nick':'tree', 'a':'4', 'b':'3', 'c':'2',
988
 
                          'd':'1'}, rev.properties)
989
 
 
990
 
    def test_bundle_unicode_properties(self):
991
 
        """We should be able to round trip a non-ascii property."""
992
 
        tree = self.make_branch_and_memory_tree('tree')
993
 
        tree.lock_write()
994
 
        self.addCleanup(tree.unlock)
995
 
 
996
 
        tree.add([''], ['TREE_ROOT'])
997
 
        # Revisions themselves do not require anything about revision property
998
 
        # keys, other than that they are a basestring, and do not contain
999
 
        # whitespace.
1000
 
        # However, Testaments assert than they are str(), and thus should not
1001
 
        # be Unicode.
1002
 
        tree.commit('One', rev_id='rev1',
1003
 
                    revprops={'omega':u'\u03a9', 'alpha':u'\u03b1'})
1004
 
        self.b1 = tree.branch
1005
 
        bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
1006
 
        self.assertContainsRe(bundle_sio.getvalue(),
1007
 
                              '# properties:\n'
1008
 
                              '#   alpha: \xce\xb1\n'
1009
 
                              '#   branch-nick: tree\n'
1010
 
                              '#   omega: \xce\xa9\n'
1011
 
                             )
1012
 
        bundle = read_bundle(bundle_sio)
1013
 
        revision_info = bundle.revisions[0]
1014
 
        self.assertEqual('rev1', revision_info.revision_id)
1015
 
        rev = revision_info.as_revision()
1016
 
        self.assertEqual({'branch-nick':'tree', 'omega':u'\u03a9',
1017
 
                          'alpha':u'\u03b1'}, rev.properties)
1018
 
 
1019
 
 
1020
 
class V09BundleKnit2Tester(V08BundleTester):
1021
 
 
1022
 
    format = '0.9'
1023
 
 
1024
 
    def bzrdir_format(self):
1025
 
        format = bzrdir.BzrDirMetaFormat1()
1026
 
        format.repository_format = knitrepo.RepositoryFormatKnit3()
1027
 
        return format
1028
 
 
1029
 
 
1030
 
class V09BundleKnit1Tester(V08BundleTester):
1031
 
 
1032
 
    format = '0.9'
1033
 
 
1034
 
    def bzrdir_format(self):
1035
 
        format = bzrdir.BzrDirMetaFormat1()
1036
 
        format.repository_format = knitrepo.RepositoryFormatKnit1()
1037
 
        return format
 
770
        bundle = self.get_valid_bundle(None, 'white-4')
1038
771
 
1039
772
 
1040
773
class MungedBundleTester(TestCaseWithTransport):
1047
780
        wt.commit('add one', rev_id='a@cset-0-1')
1048
781
        self.build_tree(['b1/two'])
1049
782
        wt.add('two')
1050
 
        wt.commit('add two', rev_id='a@cset-0-2',
1051
 
                  revprops={'branch-nick':'test'})
 
783
        wt.commit('add two', rev_id='a@cset-0-2')
1052
784
 
1053
785
        bundle_txt = StringIO()
1054
786
        rev_ids = write_bundle(wt.branch.repository, 'a@cset-0-2',
1059
791
 
1060
792
    def check_valid(self, bundle):
1061
793
        """Check that after whatever munging, the final object is valid."""
1062
 
        self.assertEqual(['a@cset-0-2'],
 
794
        self.assertEqual(['a@cset-0-2'], 
1063
795
            [r.revision_id for r in bundle.real_revisions])
1064
796
 
1065
797
    def test_extra_whitespace(self):
1098
830
        # creates a blank line at the end, and fails if that
1099
831
        # line is stripped
1100
832
        self.assertEqual('\n\n', raw[-2:])
1101
 
        bundle_txt = StringIO(raw[:-1])
1102
 
 
1103
 
        bundle = read_bundle(bundle_txt)
1104
 
        self.check_valid(bundle)
1105
 
 
1106
 
    def test_opening_text(self):
1107
 
        bundle_txt = self.build_test_bundle()
1108
 
 
1109
 
        bundle_txt = StringIO("Some random\nemail comments\n"
1110
 
                              + bundle_txt.getvalue())
1111
 
 
1112
 
        bundle = read_bundle(bundle_txt)
1113
 
        self.check_valid(bundle)
1114
 
 
1115
 
    def test_trailing_text(self):
1116
 
        bundle_txt = self.build_test_bundle()
1117
 
 
1118
 
        bundle_txt = StringIO(bundle_txt.getvalue() +
1119
 
                              "Some trailing\nrandom\ntext\n")
1120
 
 
1121
 
        bundle = read_bundle(bundle_txt)
1122
 
        self.check_valid(bundle)
1123
 
 
 
833
        bundle_text = StringIO(raw[:-1])
 
834
 
 
835
        bundle = read_bundle(bundle_txt)
 
836
        self.check_valid(bundle)