60
56
from bzrlib.tests.test_http import TestWithTransport_pycurl
61
57
from bzrlib.transport import (
65
62
from bzrlib.transport.http._urllib import HttpTransport_urllib
66
63
from bzrlib.transport.nosmart import NoSmartTransportDecorator
67
64
from bzrlib.transport.readonly import ReadonlyTransportDecorator
68
from bzrlib.repofmt import knitrepo, knitpack_repo
65
from bzrlib.repofmt import knitrepo, weaverepo, pack_repo
71
68
class TestDefaultFormat(TestCase):
73
70
def test_get_set_default_format(self):
74
71
old_format = bzrdir.BzrDirFormat.get_default_format()
75
# default is BzrDirMetaFormat1
76
self.assertIsInstance(old_format, bzrdir.BzrDirMetaFormat1)
72
# default is BzrDirFormat6
73
self.failUnless(isinstance(old_format, bzrdir.BzrDirMetaFormat1))
77
74
controldir.ControlDirFormat._set_default_format(SampleBzrDirFormat())
78
75
# creating a bzr dir should now create an instrumented dir.
80
77
result = bzrdir.BzrDir.create('memory:///')
81
self.assertIsInstance(result, SampleBzrDir)
78
self.failUnless(isinstance(result, SampleBzrDir))
83
80
controldir.ControlDirFormat._set_default_format(old_format)
84
81
self.assertEqual(old_format, bzrdir.BzrDirFormat.get_default_format())
87
class DeprecatedBzrDirFormat(bzrdir.BzrDirFormat):
88
"""A deprecated bzr dir format."""
91
84
class TestFormatRegistry(TestCase):
93
86
def make_format_registry(self):
94
87
my_format_registry = controldir.ControlDirFormatRegistry()
95
my_format_registry.register('deprecated', DeprecatedBzrDirFormat,
96
'Some format. Slower and unawesome and deprecated.',
98
my_format_registry.register_lazy('lazy', 'bzrlib.tests.test_bzrdir',
99
'DeprecatedBzrDirFormat', 'Format registered lazily',
88
my_format_registry.register('weave', bzrdir.BzrDirFormat6,
89
'Pre-0.8 format. Slower and does not support checkouts or shared'
90
' repositories', deprecated=True)
91
my_format_registry.register_lazy('lazy', 'bzrlib.bzrdir',
92
'BzrDirFormat6', 'Format registered lazily', deprecated=True)
101
93
bzrdir.register_metadir(my_format_registry, 'knit',
102
94
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
103
95
'Format using knits',
114
106
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
115
107
'Experimental successor to knit. Use at your own risk.',
116
108
branch_format='bzrlib.branch.BzrBranchFormat6', hidden=True)
117
my_format_registry.register('hiddendeprecated', DeprecatedBzrDirFormat,
118
'Old format. Slower and does not support things. ', hidden=True)
119
my_format_registry.register_lazy('hiddenlazy', 'bzrlib.tests.test_bzrdir',
120
'DeprecatedBzrDirFormat', 'Format registered lazily',
121
deprecated=True, hidden=True)
109
my_format_registry.register('hiddenweave', bzrdir.BzrDirFormat6,
110
'Pre-0.8 format. Slower and does not support checkouts or shared'
111
' repositories', hidden=True)
112
my_format_registry.register_lazy('hiddenlazy', 'bzrlib.bzrdir',
113
'BzrDirFormat6', 'Format registered lazily', deprecated=True,
122
115
return my_format_registry
124
117
def test_format_registry(self):
125
118
my_format_registry = self.make_format_registry()
126
119
my_bzrdir = my_format_registry.make_bzrdir('lazy')
127
self.assertIsInstance(my_bzrdir, DeprecatedBzrDirFormat)
128
my_bzrdir = my_format_registry.make_bzrdir('deprecated')
129
self.assertIsInstance(my_bzrdir, DeprecatedBzrDirFormat)
120
self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
121
my_bzrdir = my_format_registry.make_bzrdir('weave')
122
self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
130
123
my_bzrdir = my_format_registry.make_bzrdir('default')
131
124
self.assertIsInstance(my_bzrdir.repository_format,
132
125
knitrepo.RepositoryFormatKnit1)
176
170
self.assertIs(bzrdir.format_registry.get('dirstate-with-subtree'),
177
171
bzrdir.format_registry.get('default'))
179
repository.format_registry.get_default().__class__,
173
repository.RepositoryFormat.get_default_format().__class__,
180
174
knitrepo.RepositoryFormatKnit3)
182
176
bzrdir.format_registry.set_default_repository(old_default)
184
178
def test_aliases(self):
185
179
a_registry = controldir.ControlDirFormatRegistry()
186
a_registry.register('deprecated', DeprecatedBzrDirFormat,
187
'Old format. Slower and does not support stuff',
189
a_registry.register('deprecatedalias', DeprecatedBzrDirFormat,
190
'Old format. Slower and does not support stuff',
191
deprecated=True, alias=True)
192
self.assertEqual(frozenset(['deprecatedalias']), a_registry.aliases())
180
a_registry.register('weave', bzrdir.BzrDirFormat6,
181
'Pre-0.8 format. Slower and does not support checkouts or shared'
182
' repositories', deprecated=True)
183
a_registry.register('weavealias', bzrdir.BzrDirFormat6,
184
'Pre-0.8 format. Slower and does not support checkouts or shared'
185
' repositories', deprecated=True, alias=True)
186
self.assertEqual(frozenset(['weavealias']), a_registry.aliases())
195
189
class SampleBranch(bzrlib.branch.Branch):
252
246
return "opened branch."
255
class BzrDirFormatTest1(bzrdir.BzrDirMetaFormat1):
258
def get_format_string():
259
return "Test format 1"
262
class BzrDirFormatTest2(bzrdir.BzrDirMetaFormat1):
265
def get_format_string():
266
return "Test format 2"
269
249
class TestBzrDirFormat(TestCaseWithTransport):
270
250
"""Tests for the BzrDirFormat facility."""
272
252
def test_find_format(self):
273
253
# is the right format object found for a branch?
274
254
# create a branch with a few known format objects.
275
bzrdir.BzrProber.formats.register(BzrDirFormatTest1.get_format_string(),
277
self.addCleanup(bzrdir.BzrProber.formats.remove,
278
BzrDirFormatTest1.get_format_string())
279
bzrdir.BzrProber.formats.register(BzrDirFormatTest2.get_format_string(),
281
self.addCleanup(bzrdir.BzrProber.formats.remove,
282
BzrDirFormatTest2.get_format_string())
283
t = self.get_transport()
255
# this is not quite the same as
256
t = get_transport(self.get_url())
284
257
self.build_tree(["foo/", "bar/"], transport=t)
285
258
def check_format(format, url):
286
259
format.initialize(url)
287
t = _mod_transport.get_transport(url)
260
t = get_transport(url)
288
261
found_format = bzrdir.BzrDirFormat.find_format(t)
289
self.assertIsInstance(found_format, format.__class__)
290
check_format(BzrDirFormatTest1(), "foo")
291
check_format(BzrDirFormatTest2(), "bar")
262
self.failUnless(isinstance(found_format, format.__class__))
263
check_format(bzrdir.BzrDirFormat5(), "foo")
264
check_format(bzrdir.BzrDirFormat6(), "bar")
293
266
def test_find_format_nothing_there(self):
294
267
self.assertRaises(NotBranchError,
295
268
bzrdir.BzrDirFormat.find_format,
296
_mod_transport.get_transport('.'))
298
271
def test_find_format_unknown_format(self):
299
t = self.get_transport()
272
t = get_transport(self.get_url())
301
274
t.put_bytes('.bzr/branch-format', '')
302
275
self.assertRaises(UnknownFormatError,
303
276
bzrdir.BzrDirFormat.find_format,
304
_mod_transport.get_transport('.'))
306
279
def test_register_unregister_format(self):
307
280
format = SampleBzrDirFormat()
310
283
format.initialize(url)
311
284
# register a format for it.
312
bzrdir.BzrProber.formats.register(format.get_format_string(), format)
285
bzrdir.BzrDirFormat.register_format(format)
313
286
# which bzrdir.Open will refuse (not supported)
314
287
self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open, url)
315
288
# which bzrdir.open_containing will refuse (not supported)
316
289
self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open_containing, url)
317
290
# but open_downlevel will work
318
t = _mod_transport.get_transport(url)
291
t = get_transport(url)
319
292
self.assertEqual(format.open(t), bzrdir.BzrDir.open_unsupported(url))
320
293
# unregister the format
321
bzrdir.BzrProber.formats.remove(format.get_format_string())
294
bzrdir.BzrDirFormat.unregister_format(format)
322
295
# now open_downlevel should fail too.
323
296
self.assertRaises(UnknownFormatError, bzrdir.BzrDir.open_unsupported, url)
707
680
self.assertEqual(relpath, 'baz')
709
682
def test_open_containing_from_transport(self):
710
self.assertRaises(NotBranchError,
711
bzrdir.BzrDir.open_containing_from_transport,
712
_mod_transport.get_transport(self.get_readonly_url('')))
713
self.assertRaises(NotBranchError,
714
bzrdir.BzrDir.open_containing_from_transport,
715
_mod_transport.get_transport(self.get_readonly_url('g/p/q')))
683
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
684
get_transport(self.get_readonly_url('')))
685
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
686
get_transport(self.get_readonly_url('g/p/q')))
716
687
control = bzrdir.BzrDir.create(self.get_url())
717
688
branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
718
_mod_transport.get_transport(self.get_readonly_url('')))
689
get_transport(self.get_readonly_url('')))
719
690
self.assertEqual('', relpath)
720
691
branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
721
_mod_transport.get_transport(self.get_readonly_url('g/p/q')))
692
get_transport(self.get_readonly_url('g/p/q')))
722
693
self.assertEqual('g/p/q', relpath)
724
695
def test_open_containing_tree_or_branch(self):
768
739
# transport pointing at bzrdir should give a bzrdir with root transport
769
740
# set to the given transport
770
741
control = bzrdir.BzrDir.create(self.get_url())
771
t = self.get_transport()
772
opened_bzrdir = bzrdir.BzrDir.open_from_transport(t)
773
self.assertEqual(t.base, opened_bzrdir.root_transport.base)
742
transport = get_transport(self.get_url())
743
opened_bzrdir = bzrdir.BzrDir.open_from_transport(transport)
744
self.assertEqual(transport.base, opened_bzrdir.root_transport.base)
774
745
self.assertIsInstance(opened_bzrdir, bzrdir.BzrDir)
776
747
def test_open_from_transport_no_bzrdir(self):
777
t = self.get_transport()
778
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport, t)
748
transport = get_transport(self.get_url())
749
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
780
752
def test_open_from_transport_bzrdir_in_parent(self):
781
753
control = bzrdir.BzrDir.create(self.get_url())
782
t = self.get_transport()
784
t = t.clone('subdir')
785
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport, t)
754
transport = get_transport(self.get_url())
755
transport.mkdir('subdir')
756
transport = transport.clone('subdir')
757
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
787
760
def test_sprout_recursive(self):
788
761
tree = self.make_branch_and_tree('tree1',
918
893
root = self.make_repository('', shared=True)
919
894
foo, bar, baz = self.make_foo_bar_baz()
920
895
qux = self.make_bzrdir('foo/qux')
921
t = self.get_transport()
922
branches = bzrdir.BzrDir.find_branches(t)
896
transport = get_transport(self.get_url())
897
branches = bzrdir.BzrDir.find_branches(transport)
923
898
self.assertEqual(baz.root_transport.base, branches[0].base)
924
899
self.assertEqual(foo.root_transport.base, branches[1].base)
925
900
self.assertEqual(bar.root_transport.base, branches[2].base)
927
902
# ensure this works without a top-level repo
928
branches = bzrdir.BzrDir.find_branches(t.clone('foo'))
903
branches = bzrdir.BzrDir.find_branches(transport.clone('foo'))
929
904
self.assertEqual(foo.root_transport.base, branches[0].base)
930
905
self.assertEqual(bar.root_transport.base, branches[1].base)
1011
985
self.assertEqual(2, rpc_count)
988
class TestFormat5(TestCaseWithTransport):
989
"""Tests specific to the version 5 bzrdir format."""
991
def test_same_lockfiles_between_tree_repo_branch(self):
992
# this checks that only a single lockfiles instance is created
993
# for format 5 objects
994
dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
995
def check_dir_components_use_same_lock(dir):
996
ctrl_1 = dir.open_repository().control_files
997
ctrl_2 = dir.open_branch().control_files
998
ctrl_3 = dir.open_workingtree()._control_files
999
self.assertTrue(ctrl_1 is ctrl_2)
1000
self.assertTrue(ctrl_2 is ctrl_3)
1001
check_dir_components_use_same_lock(dir)
1002
# and if we open it normally.
1003
dir = bzrdir.BzrDir.open(self.get_url())
1004
check_dir_components_use_same_lock(dir)
1006
def test_can_convert(self):
1007
# format 5 dirs are convertable
1008
dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
1009
self.assertTrue(dir.can_convert_format())
1011
def test_needs_conversion(self):
1012
# format 5 dirs need a conversion if they are not the default,
1014
dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
1015
# don't need to convert it to itself
1016
self.assertFalse(dir.needs_format_conversion(bzrdir.BzrDirFormat5()))
1017
# do need to convert it to the current default
1018
self.assertTrue(dir.needs_format_conversion(
1019
bzrdir.BzrDirFormat.get_default_format()))
1022
class TestFormat6(TestCaseWithTransport):
1023
"""Tests specific to the version 6 bzrdir format."""
1025
def test_same_lockfiles_between_tree_repo_branch(self):
1026
# this checks that only a single lockfiles instance is created
1027
# for format 6 objects
1028
dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
1029
def check_dir_components_use_same_lock(dir):
1030
ctrl_1 = dir.open_repository().control_files
1031
ctrl_2 = dir.open_branch().control_files
1032
ctrl_3 = dir.open_workingtree()._control_files
1033
self.assertTrue(ctrl_1 is ctrl_2)
1034
self.assertTrue(ctrl_2 is ctrl_3)
1035
check_dir_components_use_same_lock(dir)
1036
# and if we open it normally.
1037
dir = bzrdir.BzrDir.open(self.get_url())
1038
check_dir_components_use_same_lock(dir)
1040
def test_can_convert(self):
1041
# format 6 dirs are convertable
1042
dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
1043
self.assertTrue(dir.can_convert_format())
1045
def test_needs_conversion(self):
1046
# format 6 dirs need an conversion if they are not the default.
1047
dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
1048
self.assertTrue(dir.needs_format_conversion(
1049
bzrdir.BzrDirFormat.get_default_format()))
1052
class NotBzrDir(bzrlib.bzrdir.BzrDir):
1053
"""A non .bzr based control directory."""
1055
def __init__(self, transport, format):
1056
self._format = format
1057
self.root_transport = transport
1058
self.transport = transport.clone('.not')
1061
class NotBzrDirFormat(bzrlib.bzrdir.BzrDirFormat):
1062
"""A test class representing any non-.bzr based disk format."""
1064
def initialize_on_transport(self, transport):
1065
"""Initialize a new .not dir in the base directory of a Transport."""
1066
transport.mkdir('.not')
1067
return self.open(transport)
1069
def open(self, transport):
1070
"""Open this directory."""
1071
return NotBzrDir(transport, self)
1074
def _known_formats(self):
1075
return set([NotBzrDirFormat()])
1078
class NotBzrDirProber(controldir.Prober):
1080
def probe_transport(self, transport):
1081
"""Our format is present if the transport ends in '.not/'."""
1082
if transport.has('.not'):
1083
return NotBzrDirFormat()
1086
class TestNotBzrDir(TestCaseWithTransport):
1087
"""Tests for using the bzrdir api with a non .bzr based disk format.
1089
If/when one of these is in the core, we can let the implementation tests
1093
def test_create_and_find_format(self):
1094
# create a .notbzr dir
1095
format = NotBzrDirFormat()
1096
dir = format.initialize(self.get_url())
1097
self.assertIsInstance(dir, NotBzrDir)
1099
controldir.ControlDirFormat.register_prober(NotBzrDirProber)
1101
found = bzrlib.bzrdir.BzrDirFormat.find_format(
1102
get_transport(self.get_url()))
1103
self.assertIsInstance(found, NotBzrDirFormat)
1105
controldir.ControlDirFormat.unregister_prober(NotBzrDirProber)
1107
def test_included_in_known_formats(self):
1108
not_format = NotBzrDirFormat()
1109
bzrlib.controldir.ControlDirFormat.register_format(not_format)
1111
formats = bzrlib.bzrdir.BzrDirFormat.known_formats()
1112
for format in formats:
1113
if isinstance(format, NotBzrDirFormat):
1115
self.fail("No NotBzrDirFormat in %s" % formats)
1117
bzrlib.controldir.ControlDirFormat.unregister_format(not_format)
1014
1120
class NonLocalTests(TestCaseWithTransport):
1015
1121
"""Tests for bzrdir static behaviour on non local paths."""