~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_bzrdir.py

(gz) Fix test failure on alpha by correcting format string for
 gc_chk_sha1_record (Martin [gz])

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
from bzrlib import (
27
27
    branch,
28
28
    bzrdir,
29
 
    config,
30
29
    controldir,
31
30
    errors,
32
31
    help_topics,
39
38
    transport as _mod_transport,
40
39
    urlutils,
41
40
    win32utils,
42
 
    workingtree_3,
43
 
    workingtree_4,
 
41
    workingtree,
44
42
    )
45
43
import bzrlib.branch
46
44
from bzrlib.errors import (
67
65
from bzrlib.transport.http._urllib import HttpTransport_urllib
68
66
from bzrlib.transport.nosmart import NoSmartTransportDecorator
69
67
from bzrlib.transport.readonly import ReadonlyTransportDecorator
70
 
from bzrlib.repofmt import knitrepo, knitpack_repo
 
68
from bzrlib.repofmt import knitrepo, pack_repo
71
69
 
72
70
 
73
71
class TestDefaultFormat(TestCase):
75
73
    def test_get_set_default_format(self):
76
74
        old_format = bzrdir.BzrDirFormat.get_default_format()
77
75
        # default is BzrDirMetaFormat1
78
 
        self.assertIsInstance(old_format, bzrdir.BzrDirMetaFormat1)
 
76
        self.failUnless(isinstance(old_format, bzrdir.BzrDirMetaFormat1))
79
77
        controldir.ControlDirFormat._set_default_format(SampleBzrDirFormat())
80
78
        # creating a bzr dir should now create an instrumented dir.
81
79
        try:
82
80
            result = bzrdir.BzrDir.create('memory:///')
83
 
            self.assertIsInstance(result, SampleBzrDir)
 
81
            self.failUnless(isinstance(result, SampleBzrDir))
84
82
        finally:
85
83
            controldir.ControlDirFormat._set_default_format(old_format)
86
84
        self.assertEqual(old_format, bzrdir.BzrDirFormat.get_default_format())
274
272
    def test_find_format(self):
275
273
        # is the right format object found for a branch?
276
274
        # create a branch with a few known format objects.
277
 
        bzrdir.BzrProber.formats.register(BzrDirFormatTest1.get_format_string(),
278
 
            BzrDirFormatTest1())
279
 
        self.addCleanup(bzrdir.BzrProber.formats.remove,
280
 
            BzrDirFormatTest1.get_format_string())
281
 
        bzrdir.BzrProber.formats.register(BzrDirFormatTest2.get_format_string(),
282
 
            BzrDirFormatTest2())
283
 
        self.addCleanup(bzrdir.BzrProber.formats.remove,
284
 
            BzrDirFormatTest2.get_format_string())
 
275
        bzrdir.BzrDirFormat.register_format(BzrDirFormatTest1())
 
276
        self.addCleanup(bzrdir.BzrDirFormat.unregister_format, BzrDirFormatTest1())
 
277
        bzrdir.BzrDirFormat.register_format(BzrDirFormatTest2())
 
278
        self.addCleanup(bzrdir.BzrDirFormat.unregister_format, BzrDirFormatTest2())
285
279
        t = self.get_transport()
286
280
        self.build_tree(["foo/", "bar/"], transport=t)
287
281
        def check_format(format, url):
288
282
            format.initialize(url)
289
 
            t = _mod_transport.get_transport_from_path(url)
 
283
            t = _mod_transport.get_transport(url)
290
284
            found_format = bzrdir.BzrDirFormat.find_format(t)
291
 
            self.assertIsInstance(found_format, format.__class__)
 
285
            self.failUnless(isinstance(found_format, format.__class__))
292
286
        check_format(BzrDirFormatTest1(), "foo")
293
287
        check_format(BzrDirFormatTest2(), "bar")
294
288
 
295
289
    def test_find_format_nothing_there(self):
296
290
        self.assertRaises(NotBranchError,
297
291
                          bzrdir.BzrDirFormat.find_format,
298
 
                          _mod_transport.get_transport_from_path('.'))
 
292
                          _mod_transport.get_transport('.'))
299
293
 
300
294
    def test_find_format_unknown_format(self):
301
295
        t = self.get_transport()
303
297
        t.put_bytes('.bzr/branch-format', '')
304
298
        self.assertRaises(UnknownFormatError,
305
299
                          bzrdir.BzrDirFormat.find_format,
306
 
                          _mod_transport.get_transport_from_path('.'))
 
300
                          _mod_transport.get_transport('.'))
307
301
 
308
302
    def test_register_unregister_format(self):
309
303
        format = SampleBzrDirFormat()
311
305
        # make a bzrdir
312
306
        format.initialize(url)
313
307
        # register a format for it.
314
 
        bzrdir.BzrProber.formats.register(format.get_format_string(), format)
 
308
        bzrdir.BzrDirFormat.register_format(format)
315
309
        # which bzrdir.Open will refuse (not supported)
316
310
        self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open, url)
317
311
        # which bzrdir.open_containing will refuse (not supported)
318
312
        self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open_containing, url)
319
313
        # but open_downlevel will work
320
 
        t = _mod_transport.get_transport_from_url(url)
 
314
        t = _mod_transport.get_transport(url)
321
315
        self.assertEqual(format.open(t), bzrdir.BzrDir.open_unsupported(url))
322
316
        # unregister the format
323
 
        bzrdir.BzrProber.formats.remove(format.get_format_string())
 
317
        bzrdir.BzrDirFormat.unregister_format(format)
324
318
        # now open_downlevel should fail too.
325
319
        self.assertRaises(UnknownFormatError, bzrdir.BzrDir.open_unsupported, url)
326
320
 
503
497
    def test_default_stacking_with_stackable_branch_unstackable_repo(self):
504
498
        # Make stackable source branch with an unstackable repo format.
505
499
        source_bzrdir = self.make_bzrdir('source')
506
 
        knitpack_repo.RepositoryFormatKnitPack1().initialize(source_bzrdir)
 
500
        pack_repo.RepositoryFormatKnitPack1().initialize(source_bzrdir)
507
501
        source_branch = bzrlib.branch.BzrBranchFormat7().initialize(
508
502
            source_bzrdir)
509
503
        # Make a directory with a default stacking policy
711
705
    def test_open_containing_from_transport(self):
712
706
        self.assertRaises(NotBranchError,
713
707
            bzrdir.BzrDir.open_containing_from_transport,
714
 
            _mod_transport.get_transport_from_url(self.get_readonly_url('')))
 
708
            _mod_transport.get_transport(self.get_readonly_url('')))
715
709
        self.assertRaises(NotBranchError,
716
710
            bzrdir.BzrDir.open_containing_from_transport,
717
 
            _mod_transport.get_transport_from_url(
718
 
                self.get_readonly_url('g/p/q')))
 
711
            _mod_transport.get_transport(self.get_readonly_url('g/p/q')))
719
712
        control = bzrdir.BzrDir.create(self.get_url())
720
713
        branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
721
 
            _mod_transport.get_transport_from_url(
722
 
                self.get_readonly_url('')))
 
714
            _mod_transport.get_transport(self.get_readonly_url('')))
723
715
        self.assertEqual('', relpath)
724
716
        branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
725
 
            _mod_transport.get_transport_from_url(
726
 
                self.get_readonly_url('g/p/q')))
 
717
            _mod_transport.get_transport(self.get_readonly_url('g/p/q')))
727
718
        self.assertEqual('g/p/q', relpath)
728
719
 
729
720
    def test_open_containing_tree_or_branch(self):
802
793
        tree2 = tree.bzrdir.sprout('tree2').open_workingtree()
803
794
        tree2.lock_read()
804
795
        self.addCleanup(tree2.unlock)
805
 
        self.assertPathExists('tree2/subtree/file')
 
796
        self.failUnlessExists('tree2/subtree/file')
806
797
        self.assertEqual('tree-reference', tree2.kind('subtree-root'))
807
798
 
808
799
    def test_cloning_metadir(self):
812
803
        branch = self.make_branch('branch', format='knit')
813
804
        format = branch.bzrdir.cloning_metadir()
814
805
        self.assertIsInstance(format.workingtree_format,
815
 
            workingtree_4.WorkingTreeFormat6)
 
806
            workingtree.WorkingTreeFormat3)
816
807
 
817
808
    def test_sprout_recursive_treeless(self):
818
809
        tree = self.make_branch_and_tree('tree1',
842
833
        # #634470.  -- vila 20100909
843
834
        self.assertRaises(errors.NotBranchError,
844
835
                          tree.bzrdir.sprout, 'repo/tree2')
845
 
#        self.assertPathExists('repo/tree2/subtree')
846
 
#        self.assertPathDoesNotExist('repo/tree2/subtree/file')
 
836
#        self.failUnlessExists('repo/tree2/subtree')
 
837
#        self.failIfExists('repo/tree2/subtree/file')
847
838
 
848
839
    def make_foo_bar_baz(self):
849
840
        foo = bzrdir.BzrDir.create_branch_convenience('foo').bzrdir
902
893
        def evaluate(bzrdir):
903
894
            try:
904
895
                repo = bzrdir.open_repository()
905
 
            except errors.NoRepositoryPresent:
 
896
            except NoRepositoryPresent:
906
897
                return True, bzrdir.root_transport.base
907
898
            else:
908
899
                return False, bzrdir.root_transport.base
968
959
        checkout_base = t.clone('checkout').base
969
960
        self.assertEqual(checkout_base, dir.get_workingtree_transport(None).base)
970
961
        self.assertEqual(checkout_base,
971
 
                         dir.get_workingtree_transport(workingtree_3.WorkingTreeFormat3()).base)
 
962
                         dir.get_workingtree_transport(workingtree.WorkingTreeFormat3()).base)
972
963
 
973
964
    def test_meta1dir_uses_lockdir(self):
974
965
        """Meta1 format uses a LockDir to guard the whole directory, not a file."""
1016
1007
        self.assertEqual(2, rpc_count)
1017
1008
 
1018
1009
 
 
1010
class TestFormat5(TestCaseWithTransport):
 
1011
    """Tests specific to the version 5 bzrdir format."""
 
1012
 
 
1013
    def test_same_lockfiles_between_tree_repo_branch(self):
 
1014
        # this checks that only a single lockfiles instance is created
 
1015
        # for format 5 objects
 
1016
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
 
1017
        def check_dir_components_use_same_lock(dir):
 
1018
            ctrl_1 = dir.open_repository().control_files
 
1019
            ctrl_2 = dir.open_branch().control_files
 
1020
            ctrl_3 = dir.open_workingtree()._control_files
 
1021
            self.assertTrue(ctrl_1 is ctrl_2)
 
1022
            self.assertTrue(ctrl_2 is ctrl_3)
 
1023
        check_dir_components_use_same_lock(dir)
 
1024
        # and if we open it normally.
 
1025
        dir = bzrdir.BzrDir.open(self.get_url())
 
1026
        check_dir_components_use_same_lock(dir)
 
1027
 
 
1028
    def test_can_convert(self):
 
1029
        # format 5 dirs are convertable
 
1030
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
 
1031
        self.assertTrue(dir.can_convert_format())
 
1032
 
 
1033
    def test_needs_conversion(self):
 
1034
        # format 5 dirs need a conversion if they are not the default,
 
1035
        # and they aren't
 
1036
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
 
1037
        # don't need to convert it to itself
 
1038
        self.assertFalse(dir.needs_format_conversion(bzrdir.BzrDirFormat5()))
 
1039
        # do need to convert it to the current default
 
1040
        self.assertTrue(dir.needs_format_conversion(
 
1041
            bzrdir.BzrDirFormat.get_default_format()))
 
1042
 
 
1043
 
 
1044
class TestFormat6(TestCaseWithTransport):
 
1045
    """Tests specific to the version 6 bzrdir format."""
 
1046
 
 
1047
    def test_same_lockfiles_between_tree_repo_branch(self):
 
1048
        # this checks that only a single lockfiles instance is created
 
1049
        # for format 6 objects
 
1050
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
 
1051
        def check_dir_components_use_same_lock(dir):
 
1052
            ctrl_1 = dir.open_repository().control_files
 
1053
            ctrl_2 = dir.open_branch().control_files
 
1054
            ctrl_3 = dir.open_workingtree()._control_files
 
1055
            self.assertTrue(ctrl_1 is ctrl_2)
 
1056
            self.assertTrue(ctrl_2 is ctrl_3)
 
1057
        check_dir_components_use_same_lock(dir)
 
1058
        # and if we open it normally.
 
1059
        dir = bzrdir.BzrDir.open(self.get_url())
 
1060
        check_dir_components_use_same_lock(dir)
 
1061
 
 
1062
    def test_can_convert(self):
 
1063
        # format 6 dirs are convertable
 
1064
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
 
1065
        self.assertTrue(dir.can_convert_format())
 
1066
 
 
1067
    def test_needs_conversion(self):
 
1068
        # format 6 dirs need an conversion if they are not the default.
 
1069
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
 
1070
        self.assertTrue(dir.needs_format_conversion(
 
1071
            bzrdir.BzrDirFormat.get_default_format()))
 
1072
 
 
1073
 
 
1074
class NotBzrDir(bzrlib.bzrdir.BzrDir):
 
1075
    """A non .bzr based control directory."""
 
1076
 
 
1077
    def __init__(self, transport, format):
 
1078
        self._format = format
 
1079
        self.root_transport = transport
 
1080
        self.transport = transport.clone('.not')
 
1081
 
 
1082
 
 
1083
class NotBzrDirFormat(bzrlib.bzrdir.BzrDirFormat):
 
1084
    """A test class representing any non-.bzr based disk format."""
 
1085
 
 
1086
    def initialize_on_transport(self, transport):
 
1087
        """Initialize a new .not dir in the base directory of a Transport."""
 
1088
        transport.mkdir('.not')
 
1089
        return self.open(transport)
 
1090
 
 
1091
    def open(self, transport):
 
1092
        """Open this directory."""
 
1093
        return NotBzrDir(transport, self)
 
1094
 
 
1095
    @classmethod
 
1096
    def _known_formats(self):
 
1097
        return set([NotBzrDirFormat()])
 
1098
 
 
1099
 
 
1100
class NotBzrDirProber(controldir.Prober):
 
1101
 
 
1102
    def probe_transport(self, transport):
 
1103
        """Our format is present if the transport ends in '.not/'."""
 
1104
        if transport.has('.not'):
 
1105
            return NotBzrDirFormat()
 
1106
 
 
1107
 
 
1108
class TestNotBzrDir(TestCaseWithTransport):
 
1109
    """Tests for using the bzrdir api with a non .bzr based disk format.
 
1110
 
 
1111
    If/when one of these is in the core, we can let the implementation tests
 
1112
    verify this works.
 
1113
    """
 
1114
 
 
1115
    def test_create_and_find_format(self):
 
1116
        # create a .notbzr dir
 
1117
        format = NotBzrDirFormat()
 
1118
        dir = format.initialize(self.get_url())
 
1119
        self.assertIsInstance(dir, NotBzrDir)
 
1120
        # now probe for it.
 
1121
        controldir.ControlDirFormat.register_prober(NotBzrDirProber)
 
1122
        try:
 
1123
            found = bzrlib.bzrdir.BzrDirFormat.find_format(self.get_transport())
 
1124
            self.assertIsInstance(found, NotBzrDirFormat)
 
1125
        finally:
 
1126
            controldir.ControlDirFormat.unregister_prober(NotBzrDirProber)
 
1127
 
 
1128
    def test_included_in_known_formats(self):
 
1129
        not_format = NotBzrDirFormat()
 
1130
        bzrlib.controldir.ControlDirFormat.register_format(not_format)
 
1131
        try:
 
1132
            formats = bzrlib.bzrdir.BzrDirFormat.known_formats()
 
1133
            for format in formats:
 
1134
                if isinstance(format, NotBzrDirFormat):
 
1135
                    return
 
1136
            self.fail("No NotBzrDirFormat in %s" % formats)
 
1137
        finally:
 
1138
            bzrlib.controldir.ControlDirFormat.unregister_format(not_format)
 
1139
 
 
1140
 
1019
1141
class NonLocalTests(TestCaseWithTransport):
1020
1142
    """Tests for bzrdir static behaviour on non local paths."""
1021
1143
 
1062
1184
        my_bzrdir = bzrdir.BzrDir.open(self.get_url('branch-knit2'))
1063
1185
        checkout_format = my_bzrdir.checkout_metadir()
1064
1186
        self.assertIsInstance(checkout_format.workingtree_format,
1065
 
                              workingtree_4.WorkingTreeFormat4)
 
1187
                              workingtree.WorkingTreeFormat3)
1066
1188
 
1067
1189
 
1068
1190
class TestHTTPRedirections(object):
1212
1334
 
1213
1335
    def __init__(self, *args, **kwargs):
1214
1336
        super(_TestBzrDir, self).__init__(*args, **kwargs)
1215
 
        self.test_branch = _TestBranch(self.transport)
 
1337
        self.test_branch = _TestBranch()
1216
1338
        self.test_branch.repository = self.create_repository()
1217
1339
 
1218
1340
    def open_branch(self, unsupported=False):
1229
1351
class _TestBranch(bzrlib.branch.Branch):
1230
1352
    """Test Branch implementation for TestBzrDirSprout."""
1231
1353
 
1232
 
    def __init__(self, transport, *args, **kwargs):
 
1354
    def __init__(self, *args, **kwargs):
1233
1355
        self._format = _TestBranchFormat()
1234
 
        self._transport = transport
1235
 
        self.base = transport.base
1236
1356
        super(_TestBranch, self).__init__(*args, **kwargs)
1237
1357
        self.calls = []
1238
1358
        self._parent = None
1239
1359
 
1240
1360
    def sprout(self, *args, **kwargs):
1241
1361
        self.calls.append('sprout')
1242
 
        return _TestBranch(self._transport)
 
1362
        return _TestBranch()
1243
1363
 
1244
1364
    def copy_content_into(self, destination, revision_id=None):
1245
1365
        self.calls.append('copy_content_into')
1250
1370
    def get_parent(self):
1251
1371
        return self._parent
1252
1372
 
1253
 
    def _get_config(self):
1254
 
        return config.TransportConfig(self._transport, 'branch.conf')
1255
 
 
1256
1373
    def set_parent(self, parent):
1257
1374
        self._parent = parent
1258
1375