~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-10-10 07:23:07 UTC
  • mfrom: (2067.1.1 urandom-56883)
  • Revision ID: pqm@pqm.ubuntu.com-20061010072307-037a6f63da8a1bdd
(John Arbash Meinel) Handle exceptions while opening /dev/urandom

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2004, 2005, 2006, 2007 Canonical Ltd
 
1
# Copyright (C) 2004-2006 by Canonical Ltd
2
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
20
20
import tempfile
21
21
 
22
22
from bzrlib import (
23
 
    bzrdir,
24
 
    errors,
25
 
    inventory,
26
 
    repository,
27
 
    revision as _mod_revision,
 
23
    bzrdir, 
 
24
    errors, 
 
25
    inventory, 
 
26
    repository, 
28
27
    treebuilder,
29
28
    )
30
 
from bzrlib.builtins import _merge_helper
 
29
from bzrlib.builtins import merge
31
30
from bzrlib.bzrdir import BzrDir
32
31
from bzrlib.bundle.apply_bundle import install_bundle, merge_bundle
33
32
from bzrlib.bundle.bundle_data import BundleTree
39
38
from bzrlib.errors import (BzrError, TestamentMismatch, NotABundle, BadBundle, 
40
39
                           NoSuchFile,)
41
40
from bzrlib.merge import Merge3Merger
42
 
from bzrlib.repofmt import knitrepo
43
41
from bzrlib.osutils import has_symlinks, sha_file
44
42
from bzrlib.tests import (TestCaseInTempDir, TestCaseWithTransport,
45
43
                          TestCase, TestSkipped)
315
313
 
316
314
    def test_mismatched_bundle(self):
317
315
        format = bzrdir.BzrDirMetaFormat1()
318
 
        format.repository_format = knitrepo.RepositoryFormatKnit3()
 
316
        format.repository_format = repository.RepositoryFormatKnit2()
319
317
        serializer = BundleSerializerV08('0.8')
320
318
        b = self.make_branch('.', format=format)
321
 
        self.assertRaises(errors.IncompatibleBundleFormat, serializer.write, 
 
319
        self.assertRaises(errors.IncompatibleFormat, serializer.write, 
322
320
                          b.repository, [], {}, StringIO())
323
321
 
324
322
    def test_matched_bundle(self):
325
 
        """Don't raise IncompatibleBundleFormat for knit2 and bundle0.9"""
 
323
        """Don't raise IncompatibleFormat for knit2 and bundle0.9"""
326
324
        format = bzrdir.BzrDirMetaFormat1()
327
 
        format.repository_format = knitrepo.RepositoryFormatKnit3()
 
325
        format.repository_format = repository.RepositoryFormatKnit2()
328
326
        serializer = BundleSerializerV09('0.9')
329
327
        b = self.make_branch('.', format=format)
330
328
        serializer.write(b.repository, [], {}, StringIO())
332
330
    def test_mismatched_model(self):
333
331
        """Try copying a bundle from knit2 to knit1"""
334
332
        format = bzrdir.BzrDirMetaFormat1()
335
 
        format.repository_format = knitrepo.RepositoryFormatKnit3()
 
333
        format.repository_format = repository.RepositoryFormatKnit2()
336
334
        source = self.make_branch_and_tree('source', format=format)
337
335
        source.commit('one', rev_id='one-id')
338
336
        source.commit('two', rev_id='two-id')
339
337
        text = StringIO()
340
 
        write_bundle(source.branch.repository, 'two-id', 'null:', text,
 
338
        write_bundle(source.branch.repository, 'two-id', None, text, 
341
339
                     format='0.9')
342
340
        text.seek(0)
343
341
 
344
342
        format = bzrdir.BzrDirMetaFormat1()
345
 
        format.repository_format = knitrepo.RepositoryFormatKnit1()
 
343
        format.repository_format = repository.RepositoryFormatKnit1()
346
344
        target = self.make_branch('target', format=format)
347
345
        self.assertRaises(errors.IncompatibleRevision, install_bundle, 
348
346
                          target.repository, read_bundle(text))
354
352
 
355
353
    def bzrdir_format(self):
356
354
        format = bzrdir.BzrDirMetaFormat1()
357
 
        format.repository_format = knitrepo.RepositoryFormatKnit1()
 
355
        format.repository_format = repository.RepositoryFormatKnit1()
358
356
        return format
359
357
 
360
358
    def make_branch_and_tree(self, path, format=None):
457
455
                os.mkdir(checkout_dir)
458
456
        tree = self.make_branch_and_tree(checkout_dir)
459
457
        s = StringIO()
460
 
        ancestors = write_bundle(self.b1.repository, rev_id, 'null:', s,
 
458
        ancestors = write_bundle(self.b1.repository, rev_id, None, s,
461
459
                                 format=self.format)
462
460
        s.seek(0)
463
461
        assert isinstance(s.getvalue(), str), (
483
481
                    continue
484
482
                self.assertEqual(old_file.read(),
485
483
                                 new.get_file(inventory_id).read())
486
 
        if not _mod_revision.is_null(rev_id):
 
484
        if rev_id is not None:
487
485
            rh = self.b1.revision_history()
488
486
            tree.branch.set_revision_history(rh[:rh.index(rev_id)+1])
489
487
            tree.update()
490
488
            delta = tree.changes_from(self.b1.repository.revision_tree(rev_id))
491
489
            self.assertFalse(delta.has_changed(),
492
 
                             'Working tree has modifications: %s' % delta)
 
490
                             'Working tree has modifications')
493
491
        return tree
494
492
 
495
493
    def valid_apply_bundle(self, base_rev_id, info, checkout_dir=None):
550
548
        self.tree1.add('one')
551
549
        self.tree1.commit('add one', rev_id='a@cset-0-1')
552
550
 
553
 
        bundle = self.get_valid_bundle('null:', 'a@cset-0-1')
 
551
        bundle = self.get_valid_bundle(None, 'a@cset-0-1')
554
552
        # FIXME: The current write_bundle api no longer supports
555
553
        #        setting a custom summary message
556
554
        #        We should re-introduce the ability, and update
592
590
        bundle = self.get_valid_bundle('a@cset-0-1', 'a@cset-0-2')
593
591
 
594
592
        # Check a rollup bundle 
595
 
        bundle = self.get_valid_bundle('null:', 'a@cset-0-2')
 
593
        bundle = self.get_valid_bundle(None, 'a@cset-0-2')
596
594
 
597
595
        # Now delete entries
598
596
        self.tree1.remove(
610
608
        self.assertRaises(TestamentMismatch, self.get_invalid_bundle, 
611
609
                          'a@cset-0-2', 'a@cset-0-3')
612
610
        # Check a rollup bundle 
613
 
        bundle = self.get_valid_bundle('null:', 'a@cset-0-3')
 
611
        bundle = self.get_valid_bundle(None, 'a@cset-0-3')
614
612
 
615
613
        # Now move the directory
616
614
        self.tree1.rename_one('dir', 'sub/dir')
618
616
 
619
617
        bundle = self.get_valid_bundle('a@cset-0-3', 'a@cset-0-4')
620
618
        # Check a rollup bundle 
621
 
        bundle = self.get_valid_bundle('null:', 'a@cset-0-4')
 
619
        bundle = self.get_valid_bundle(None, 'a@cset-0-4')
622
620
 
623
621
        # Modified files
624
622
        open('b1/sub/dir/WithCaps.txt', 'ab').write('\nAdding some text\n')
642
640
        self.assertEqualDiff(tree1_inv, tree2_inv)
643
641
        other.rename_one('sub/dir/nolastnewline.txt', 'sub/nolastnewline.txt')
644
642
        other.commit('rename file', rev_id='a@cset-0-6b')
645
 
        _merge_helper([other.basedir, -1], [None, None],
646
 
                      this_dir=self.tree1.basedir)
 
643
        merge([other.basedir, -1], [None, None], this_dir=self.tree1.basedir)
647
644
        self.tree1.commit(u'Merge', rev_id='a@cset-0-7',
648
645
                          verbose=False)
649
646
        bundle = self.get_valid_bundle('a@cset-0-6', 'a@cset-0-7')
657
654
        tt.new_symlink('link', tt.root, 'bar/foo', 'link-1')
658
655
        tt.apply()
659
656
        self.tree1.commit('add symlink', rev_id='l@cset-0-1')
660
 
        self.get_valid_bundle('null:', 'l@cset-0-1')
 
657
        self.get_valid_bundle(None, 'l@cset-0-1')
661
658
        tt = TreeTransform(self.tree1)
662
659
        trans_id = tt.trans_id_tree_file_id('link-1')
663
660
        tt.adjust_path('link2', tt.root, trans_id)
690
687
        tt.new_file('file2', tt.root, '\x01\n\x02\r\x03\n\x04\r\xff', 'binary-2')
691
688
        tt.apply()
692
689
        self.tree1.commit('add binary', rev_id='b@cset-0-1')
693
 
        self.get_valid_bundle('null:', 'b@cset-0-1')
 
690
        self.get_valid_bundle(None, 'b@cset-0-1')
694
691
 
695
692
        # Delete
696
693
        tt = TreeTransform(self.tree1)
720
717
        self.get_valid_bundle('b@cset-0-3', 'b@cset-0-4')
721
718
 
722
719
        # Rollup
723
 
        self.get_valid_bundle('null:', 'b@cset-0-4')
 
720
        self.get_valid_bundle(None, 'b@cset-0-4')
724
721
 
725
722
    def test_last_modified(self):
726
723
        self.tree1 = self.make_branch_and_tree('b1')
744
741
        tt.create_file('file2', trans_id)
745
742
        tt.apply()
746
743
        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)
 
744
        merge([other.basedir, -1], [None, None], this_dir=self.tree1.basedir)
749
745
        self.tree1.commit(u'Merge', rev_id='a@lmod-0-3',
750
746
                          verbose=False)
751
747
        self.tree1.commit(u'Merge', rev_id='a@lmod-0-4')
765
761
        bundle_file = StringIO()
766
762
        rev_ids = write_bundle(self.tree1.branch.repository, 'a@cset-0-3',
767
763
                               'a@cset-0-1', bundle_file, format=self.format)
768
 
        self.assertNotContainsRe(bundle_file.getvalue(), '\btwo\b')
 
764
        self.assertNotContainsRe(bundle_file.getvalue(), 'two')
769
765
        self.assertContainsRe(bundle_file.getvalue(), 'one')
770
766
        self.assertContainsRe(bundle_file.getvalue(), 'three')
771
767
 
785
781
            u'William Dod\xe9\n').encode('utf-8'))
786
782
        f.close()
787
783
 
788
 
        self.tree1.add([u'with Dod\xe9'], ['withdod-id'])
789
 
        self.tree1.commit(u'i18n commit from William Dod\xe9',
 
784
        self.tree1.add([u'with Dod\xe9'])
 
785
        self.tree1.commit(u'i18n commit from William Dod\xe9', 
790
786
                          rev_id='i18n-1', committer=u'William Dod\xe9')
791
787
 
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
788
        # Add
803
 
        bundle = self.get_valid_bundle('null:', 'i18n-1')
 
789
        bundle = self.get_valid_bundle(None, 'i18n-1')
804
790
 
805
791
        # Modified
806
792
        f = open(u'b1/with Dod\xe9', 'wb')
824
810
        bundle = self.get_valid_bundle('i18n-3', 'i18n-4')
825
811
 
826
812
        # Rollup
827
 
        bundle = self.get_valid_bundle('null:', 'i18n-4')
 
813
        bundle = self.get_valid_bundle(None, 'i18n-4')
828
814
 
829
815
 
830
816
    def test_whitespace_bundle(self):
842
828
        # Added
843
829
        self.tree1.commit('funky whitespace', rev_id='white-1')
844
830
 
845
 
        bundle = self.get_valid_bundle('null:', 'white-1')
 
831
        bundle = self.get_valid_bundle(None, 'white-1')
846
832
 
847
833
        # Modified
848
834
        open('b1/trailing space ', 'ab').write('add some text\n')
863
849
        bundle = self.get_valid_bundle('white-3', 'white-4')
864
850
        
865
851
        # Now test a complet roll-up
866
 
        bundle = self.get_valid_bundle('null:', 'white-4')
 
852
        bundle = self.get_valid_bundle(None, 'white-4')
867
853
 
868
854
    def test_alt_timezone_bundle(self):
869
855
        self.tree1 = self.make_branch_and_memory_tree('b1')
879
865
        self.tree1.commit('non-hour offset timezone', rev_id='tz-1',
880
866
                          timezone=19800, timestamp=1152544886.0)
881
867
 
882
 
        bundle = self.get_valid_bundle('null:', 'tz-1')
 
868
        bundle = self.get_valid_bundle(None, 'tz-1')
883
869
        
884
870
        rev = bundle.revisions[0]
885
871
        self.assertEqual('Mon 2006-07-10 20:51:26.000000000 +0530', rev.date)
891
877
        self.tree1 = self.make_branch_and_tree('b1')
892
878
        self.b1 = self.tree1.branch
893
879
        self.tree1.commit('message', rev_id='revid1')
894
 
        bundle = self.get_valid_bundle('null:', 'revid1')
 
880
        bundle = self.get_valid_bundle(None, 'revid1')
895
881
        tree = bundle.revision_tree(self.b1.repository, 'revid1')
896
882
        self.assertEqual('revid1', tree.inventory.root.revision)
897
883
 
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
884
 
1020
885
class V09BundleKnit2Tester(V08BundleTester):
1021
886
 
1023
888
 
1024
889
    def bzrdir_format(self):
1025
890
        format = bzrdir.BzrDirMetaFormat1()
1026
 
        format.repository_format = knitrepo.RepositoryFormatKnit3()
 
891
        format.repository_format = repository.RepositoryFormatKnit2()
1027
892
        return format
1028
893
 
1029
894
 
1033
898
 
1034
899
    def bzrdir_format(self):
1035
900
        format = bzrdir.BzrDirMetaFormat1()
1036
 
        format.repository_format = knitrepo.RepositoryFormatKnit1()
 
901
        format.repository_format = repository.RepositoryFormatKnit1()
1037
902
        return format
1038
903
 
1039
904