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(),
279
self.addCleanup(bzrdir.BzrProber.formats.remove,
280
BzrDirFormatTest1.get_format_string())
281
bzrdir.BzrProber.formats.register(BzrDirFormatTest2.get_format_string(),
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")
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('.'))
300
294
def test_find_format_unknown_format(self):
301
295
t = self.get_transport()
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)
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)
729
720
def test_open_containing_tree_or_branch(self):
1016
1007
self.assertEqual(2, rpc_count)
1010
class TestFormat5(TestCaseWithTransport):
1011
"""Tests specific to the version 5 bzrdir format."""
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)
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())
1033
def test_needs_conversion(self):
1034
# format 5 dirs need a conversion if they are not the default,
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()))
1044
class TestFormat6(TestCaseWithTransport):
1045
"""Tests specific to the version 6 bzrdir format."""
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)
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())
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()))
1074
class NotBzrDir(bzrlib.bzrdir.BzrDir):
1075
"""A non .bzr based control directory."""
1077
def __init__(self, transport, format):
1078
self._format = format
1079
self.root_transport = transport
1080
self.transport = transport.clone('.not')
1083
class NotBzrDirFormat(bzrlib.bzrdir.BzrDirFormat):
1084
"""A test class representing any non-.bzr based disk format."""
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)
1091
def open(self, transport):
1092
"""Open this directory."""
1093
return NotBzrDir(transport, self)
1096
def _known_formats(self):
1097
return set([NotBzrDirFormat()])
1100
class NotBzrDirProber(controldir.Prober):
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()
1108
class TestNotBzrDir(TestCaseWithTransport):
1109
"""Tests for using the bzrdir api with a non .bzr based disk format.
1111
If/when one of these is in the core, we can let the implementation tests
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)
1121
controldir.ControlDirFormat.register_prober(NotBzrDirProber)
1123
found = bzrlib.bzrdir.BzrDirFormat.find_format(self.get_transport())
1124
self.assertIsInstance(found, NotBzrDirFormat)
1126
controldir.ControlDirFormat.unregister_prober(NotBzrDirProber)
1128
def test_included_in_known_formats(self):
1129
not_format = NotBzrDirFormat()
1130
bzrlib.controldir.ControlDirFormat.register_format(not_format)
1132
formats = bzrlib.bzrdir.BzrDirFormat.known_formats()
1133
for format in formats:
1134
if isinstance(format, NotBzrDirFormat):
1136
self.fail("No NotBzrDirFormat in %s" % formats)
1138
bzrlib.controldir.ControlDirFormat.unregister_format(not_format)
1019
1141
class NonLocalTests(TestCaseWithTransport):
1020
1142
"""Tests for bzrdir static behaviour on non local paths."""
1229
1351
class _TestBranch(bzrlib.branch.Branch):
1230
1352
"""Test Branch implementation for TestBzrDirSprout."""
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
1240
1360
def sprout(self, *args, **kwargs):
1241
1361
self.calls.append('sprout')
1242
return _TestBranch(self._transport)
1362
return _TestBranch()
1244
1364
def copy_content_into(self, destination, revision_id=None):
1245
1365
self.calls.append('copy_content_into')