61
55
from bzrlib.tests.test_http import TestWithTransport_pycurl
62
56
from bzrlib.transport import (
66
61
from bzrlib.transport.http._urllib import HttpTransport_urllib
67
62
from bzrlib.transport.nosmart import NoSmartTransportDecorator
68
63
from bzrlib.transport.readonly import ReadonlyTransportDecorator
69
from bzrlib.repofmt import knitrepo, knitpack_repo
64
from bzrlib.repofmt import knitrepo, weaverepo, pack_repo
72
67
class TestDefaultFormat(TestCase):
74
69
def test_get_set_default_format(self):
75
70
old_format = bzrdir.BzrDirFormat.get_default_format()
76
# default is BzrDirMetaFormat1
77
self.assertIsInstance(old_format, bzrdir.BzrDirMetaFormat1)
71
# default is BzrDirFormat6
72
self.failUnless(isinstance(old_format, bzrdir.BzrDirMetaFormat1))
78
73
controldir.ControlDirFormat._set_default_format(SampleBzrDirFormat())
79
74
# creating a bzr dir should now create an instrumented dir.
81
76
result = bzrdir.BzrDir.create('memory:///')
82
self.assertIsInstance(result, SampleBzrDir)
77
self.failUnless(isinstance(result, SampleBzrDir))
84
79
controldir.ControlDirFormat._set_default_format(old_format)
85
80
self.assertEqual(old_format, bzrdir.BzrDirFormat.get_default_format())
88
class DeprecatedBzrDirFormat(bzrdir.BzrDirFormat):
89
"""A deprecated bzr dir format."""
92
83
class TestFormatRegistry(TestCase):
94
85
def make_format_registry(self):
95
86
my_format_registry = controldir.ControlDirFormatRegistry()
96
my_format_registry.register('deprecated', DeprecatedBzrDirFormat,
97
'Some format. Slower and unawesome and deprecated.',
99
my_format_registry.register_lazy('lazy', 'bzrlib.tests.test_bzrdir',
100
'DeprecatedBzrDirFormat', 'Format registered lazily',
87
my_format_registry.register('weave', bzrdir.BzrDirFormat6,
88
'Pre-0.8 format. Slower and does not support checkouts or shared'
89
' repositories', deprecated=True)
90
my_format_registry.register_lazy('lazy', 'bzrlib.bzrdir',
91
'BzrDirFormat6', 'Format registered lazily', deprecated=True)
102
92
bzrdir.register_metadir(my_format_registry, 'knit',
103
93
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
104
94
'Format using knits',
115
105
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
116
106
'Experimental successor to knit. Use at your own risk.',
117
107
branch_format='bzrlib.branch.BzrBranchFormat6', hidden=True)
118
my_format_registry.register('hiddendeprecated', DeprecatedBzrDirFormat,
119
'Old format. Slower and does not support things. ', hidden=True)
120
my_format_registry.register_lazy('hiddenlazy', 'bzrlib.tests.test_bzrdir',
121
'DeprecatedBzrDirFormat', 'Format registered lazily',
122
deprecated=True, hidden=True)
108
my_format_registry.register('hiddenweave', bzrdir.BzrDirFormat6,
109
'Pre-0.8 format. Slower and does not support checkouts or shared'
110
' repositories', hidden=True)
111
my_format_registry.register_lazy('hiddenlazy', 'bzrlib.bzrdir',
112
'BzrDirFormat6', 'Format registered lazily', deprecated=True,
123
114
return my_format_registry
125
116
def test_format_registry(self):
126
117
my_format_registry = self.make_format_registry()
127
118
my_bzrdir = my_format_registry.make_bzrdir('lazy')
128
self.assertIsInstance(my_bzrdir, DeprecatedBzrDirFormat)
129
my_bzrdir = my_format_registry.make_bzrdir('deprecated')
130
self.assertIsInstance(my_bzrdir, DeprecatedBzrDirFormat)
119
self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
120
my_bzrdir = my_format_registry.make_bzrdir('weave')
121
self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
131
122
my_bzrdir = my_format_registry.make_bzrdir('default')
132
123
self.assertIsInstance(my_bzrdir.repository_format,
133
124
knitrepo.RepositoryFormatKnit1)
177
169
self.assertIs(bzrdir.format_registry.get('dirstate-with-subtree'),
178
170
bzrdir.format_registry.get('default'))
180
repository.format_registry.get_default().__class__,
172
repository.RepositoryFormat.get_default_format().__class__,
181
173
knitrepo.RepositoryFormatKnit3)
183
175
bzrdir.format_registry.set_default_repository(old_default)
185
177
def test_aliases(self):
186
178
a_registry = controldir.ControlDirFormatRegistry()
187
a_registry.register('deprecated', DeprecatedBzrDirFormat,
188
'Old format. Slower and does not support stuff',
190
a_registry.register('deprecatedalias', DeprecatedBzrDirFormat,
191
'Old format. Slower and does not support stuff',
192
deprecated=True, alias=True)
193
self.assertEqual(frozenset(['deprecatedalias']), a_registry.aliases())
179
a_registry.register('weave', bzrdir.BzrDirFormat6,
180
'Pre-0.8 format. Slower and does not support checkouts or shared'
181
' repositories', deprecated=True)
182
a_registry.register('weavealias', bzrdir.BzrDirFormat6,
183
'Pre-0.8 format. Slower and does not support checkouts or shared'
184
' repositories', deprecated=True, alias=True)
185
self.assertEqual(frozenset(['weavealias']), a_registry.aliases())
196
188
class SampleBranch(bzrlib.branch.Branch):
253
245
return "opened branch."
256
class BzrDirFormatTest1(bzrdir.BzrDirMetaFormat1):
259
def get_format_string():
260
return "Test format 1"
263
class BzrDirFormatTest2(bzrdir.BzrDirMetaFormat1):
266
def get_format_string():
267
return "Test format 2"
270
248
class TestBzrDirFormat(TestCaseWithTransport):
271
249
"""Tests for the BzrDirFormat facility."""
273
251
def test_find_format(self):
274
252
# is the right format object found for a branch?
275
253
# create a branch with a few known format objects.
276
bzrdir.BzrProber.formats.register(BzrDirFormatTest1.get_format_string(),
278
self.addCleanup(bzrdir.BzrProber.formats.remove,
279
BzrDirFormatTest1.get_format_string())
280
bzrdir.BzrProber.formats.register(BzrDirFormatTest2.get_format_string(),
282
self.addCleanup(bzrdir.BzrProber.formats.remove,
283
BzrDirFormatTest2.get_format_string())
284
t = self.get_transport()
254
# this is not quite the same as
255
t = get_transport(self.get_url())
285
256
self.build_tree(["foo/", "bar/"], transport=t)
286
257
def check_format(format, url):
287
258
format.initialize(url)
288
t = _mod_transport.get_transport(url)
259
t = get_transport(url)
289
260
found_format = bzrdir.BzrDirFormat.find_format(t)
290
self.assertIsInstance(found_format, format.__class__)
291
check_format(BzrDirFormatTest1(), "foo")
292
check_format(BzrDirFormatTest2(), "bar")
261
self.failUnless(isinstance(found_format, format.__class__))
262
check_format(bzrdir.BzrDirFormat5(), "foo")
263
check_format(bzrdir.BzrDirFormat6(), "bar")
294
265
def test_find_format_nothing_there(self):
295
266
self.assertRaises(NotBranchError,
296
267
bzrdir.BzrDirFormat.find_format,
297
_mod_transport.get_transport('.'))
299
270
def test_find_format_unknown_format(self):
300
t = self.get_transport()
271
t = get_transport(self.get_url())
302
273
t.put_bytes('.bzr/branch-format', '')
303
274
self.assertRaises(UnknownFormatError,
304
275
bzrdir.BzrDirFormat.find_format,
305
_mod_transport.get_transport('.'))
307
278
def test_register_unregister_format(self):
308
279
format = SampleBzrDirFormat()
311
282
format.initialize(url)
312
283
# register a format for it.
313
bzrdir.BzrProber.formats.register(format.get_format_string(), format)
284
bzrdir.BzrDirFormat.register_format(format)
314
285
# which bzrdir.Open will refuse (not supported)
315
286
self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open, url)
316
287
# which bzrdir.open_containing will refuse (not supported)
317
288
self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open_containing, url)
318
289
# but open_downlevel will work
319
t = _mod_transport.get_transport(url)
290
t = get_transport(url)
320
291
self.assertEqual(format.open(t), bzrdir.BzrDir.open_unsupported(url))
321
292
# unregister the format
322
bzrdir.BzrProber.formats.remove(format.get_format_string())
293
bzrdir.BzrDirFormat.unregister_format(format)
323
294
# now open_downlevel should fail too.
324
295
self.assertRaises(UnknownFormatError, bzrdir.BzrDir.open_unsupported, url)
708
679
self.assertEqual(relpath, 'baz')
710
681
def test_open_containing_from_transport(self):
711
self.assertRaises(NotBranchError,
712
bzrdir.BzrDir.open_containing_from_transport,
713
_mod_transport.get_transport(self.get_readonly_url('')))
714
self.assertRaises(NotBranchError,
715
bzrdir.BzrDir.open_containing_from_transport,
716
_mod_transport.get_transport(self.get_readonly_url('g/p/q')))
682
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
683
get_transport(self.get_readonly_url('')))
684
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
685
get_transport(self.get_readonly_url('g/p/q')))
717
686
control = bzrdir.BzrDir.create(self.get_url())
718
687
branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
719
_mod_transport.get_transport(self.get_readonly_url('')))
688
get_transport(self.get_readonly_url('')))
720
689
self.assertEqual('', relpath)
721
690
branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
722
_mod_transport.get_transport(self.get_readonly_url('g/p/q')))
691
get_transport(self.get_readonly_url('g/p/q')))
723
692
self.assertEqual('g/p/q', relpath)
725
694
def test_open_containing_tree_or_branch(self):
769
738
# transport pointing at bzrdir should give a bzrdir with root transport
770
739
# set to the given transport
771
740
control = bzrdir.BzrDir.create(self.get_url())
772
t = self.get_transport()
773
opened_bzrdir = bzrdir.BzrDir.open_from_transport(t)
774
self.assertEqual(t.base, opened_bzrdir.root_transport.base)
741
transport = get_transport(self.get_url())
742
opened_bzrdir = bzrdir.BzrDir.open_from_transport(transport)
743
self.assertEqual(transport.base, opened_bzrdir.root_transport.base)
775
744
self.assertIsInstance(opened_bzrdir, bzrdir.BzrDir)
777
746
def test_open_from_transport_no_bzrdir(self):
778
t = self.get_transport()
779
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport, t)
747
transport = get_transport(self.get_url())
748
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
781
751
def test_open_from_transport_bzrdir_in_parent(self):
782
752
control = bzrdir.BzrDir.create(self.get_url())
783
t = self.get_transport()
785
t = t.clone('subdir')
786
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport, t)
753
transport = get_transport(self.get_url())
754
transport.mkdir('subdir')
755
transport = transport.clone('subdir')
756
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
788
759
def test_sprout_recursive(self):
789
760
tree = self.make_branch_and_tree('tree1',
819
790
self.build_tree(['tree1/subtree/file'])
820
791
sub_tree.add('file')
821
792
tree.commit('Initial commit')
822
# The following line force the orhaning to reveal bug #634470
823
tree.branch.get_config().set_user_option(
824
'bzr.transform.orphan_policy', 'move')
825
793
tree.bzrdir.destroy_workingtree()
826
# FIXME: subtree/.bzr is left here which allows the test to pass (or
827
# fail :-( ) -- vila 20100909
828
794
repo = self.make_repository('repo', shared=True,
829
795
format='dirstate-with-subtree')
830
796
repo.set_make_working_trees(False)
831
# FIXME: we just deleted the workingtree and now we want to use it ????
832
# At a minimum, we should use tree.branch below (but this fails too
833
# currently) or stop calling this test 'treeless'. Specifically, I've
834
# turn the line below into an assertRaises when 'subtree/.bzr' is
835
# orphaned and sprout tries to access the branch there (which is left
836
# by bzrdir.BzrDirMeta1.destroy_workingtree when it ignores the
837
# [DeletingParent('Not deleting', u'subtree', None)] conflict). See bug
838
# #634470. -- vila 20100909
839
self.assertRaises(errors.NotBranchError,
840
tree.bzrdir.sprout, 'repo/tree2')
841
# self.assertPathExists('repo/tree2/subtree')
842
# self.assertPathDoesNotExist('repo/tree2/subtree/file')
797
tree.bzrdir.sprout('repo/tree2')
798
self.failUnlessExists('repo/tree2/subtree')
799
self.failIfExists('repo/tree2/subtree/file')
844
801
def make_foo_bar_baz(self):
845
802
foo = bzrdir.BzrDir.create_branch_convenience('foo').bzrdir
919
878
root = self.make_repository('', shared=True)
920
879
foo, bar, baz = self.make_foo_bar_baz()
921
880
qux = self.make_bzrdir('foo/qux')
922
t = self.get_transport()
923
branches = bzrdir.BzrDir.find_branches(t)
881
transport = get_transport(self.get_url())
882
branches = bzrdir.BzrDir.find_branches(transport)
924
883
self.assertEqual(baz.root_transport.base, branches[0].base)
925
884
self.assertEqual(foo.root_transport.base, branches[1].base)
926
885
self.assertEqual(bar.root_transport.base, branches[2].base)
928
887
# ensure this works without a top-level repo
929
branches = bzrdir.BzrDir.find_branches(t.clone('foo'))
888
branches = bzrdir.BzrDir.find_branches(transport.clone('foo'))
930
889
self.assertEqual(foo.root_transport.base, branches[0].base)
931
890
self.assertEqual(bar.root_transport.base, branches[1].base)
958
917
dir.get_branch_transport(bzrlib.branch.BzrBranchFormat5()).base)
959
918
repository_base = t.clone('repository').base
960
919
self.assertEqual(repository_base, dir.get_repository_transport(None).base)
961
repository_format = repository.format_registry.get_default()
962
920
self.assertEqual(repository_base,
963
dir.get_repository_transport(repository_format).base)
921
dir.get_repository_transport(weaverepo.RepositoryFormat7()).base)
964
922
checkout_base = t.clone('checkout').base
965
923
self.assertEqual(checkout_base, dir.get_workingtree_transport(None).base)
966
924
self.assertEqual(checkout_base,
967
dir.get_workingtree_transport(workingtree_3.WorkingTreeFormat3()).base)
925
dir.get_workingtree_transport(workingtree.WorkingTreeFormat3()).base)
969
927
def test_meta1dir_uses_lockdir(self):
970
928
"""Meta1 format uses a LockDir to guard the whole directory, not a file."""
1012
970
self.assertEqual(2, rpc_count)
973
class TestFormat5(TestCaseWithTransport):
974
"""Tests specific to the version 5 bzrdir format."""
976
def test_same_lockfiles_between_tree_repo_branch(self):
977
# this checks that only a single lockfiles instance is created
978
# for format 5 objects
979
dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
980
def check_dir_components_use_same_lock(dir):
981
ctrl_1 = dir.open_repository().control_files
982
ctrl_2 = dir.open_branch().control_files
983
ctrl_3 = dir.open_workingtree()._control_files
984
self.assertTrue(ctrl_1 is ctrl_2)
985
self.assertTrue(ctrl_2 is ctrl_3)
986
check_dir_components_use_same_lock(dir)
987
# and if we open it normally.
988
dir = bzrdir.BzrDir.open(self.get_url())
989
check_dir_components_use_same_lock(dir)
991
def test_can_convert(self):
992
# format 5 dirs are convertable
993
dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
994
self.assertTrue(dir.can_convert_format())
996
def test_needs_conversion(self):
997
# format 5 dirs need a conversion if they are not the default,
999
dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
1000
# don't need to convert it to itself
1001
self.assertFalse(dir.needs_format_conversion(bzrdir.BzrDirFormat5()))
1002
# do need to convert it to the current default
1003
self.assertTrue(dir.needs_format_conversion(
1004
bzrdir.BzrDirFormat.get_default_format()))
1007
class TestFormat6(TestCaseWithTransport):
1008
"""Tests specific to the version 6 bzrdir format."""
1010
def test_same_lockfiles_between_tree_repo_branch(self):
1011
# this checks that only a single lockfiles instance is created
1012
# for format 6 objects
1013
dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
1014
def check_dir_components_use_same_lock(dir):
1015
ctrl_1 = dir.open_repository().control_files
1016
ctrl_2 = dir.open_branch().control_files
1017
ctrl_3 = dir.open_workingtree()._control_files
1018
self.assertTrue(ctrl_1 is ctrl_2)
1019
self.assertTrue(ctrl_2 is ctrl_3)
1020
check_dir_components_use_same_lock(dir)
1021
# and if we open it normally.
1022
dir = bzrdir.BzrDir.open(self.get_url())
1023
check_dir_components_use_same_lock(dir)
1025
def test_can_convert(self):
1026
# format 6 dirs are convertable
1027
dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
1028
self.assertTrue(dir.can_convert_format())
1030
def test_needs_conversion(self):
1031
# format 6 dirs need an conversion if they are not the default.
1032
dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
1033
self.assertTrue(dir.needs_format_conversion(
1034
bzrdir.BzrDirFormat.get_default_format()))
1037
class NotBzrDir(bzrlib.bzrdir.BzrDir):
1038
"""A non .bzr based control directory."""
1040
def __init__(self, transport, format):
1041
self._format = format
1042
self.root_transport = transport
1043
self.transport = transport.clone('.not')
1046
class NotBzrDirFormat(bzrlib.bzrdir.BzrDirFormat):
1047
"""A test class representing any non-.bzr based disk format."""
1049
def initialize_on_transport(self, transport):
1050
"""Initialize a new .not dir in the base directory of a Transport."""
1051
transport.mkdir('.not')
1052
return self.open(transport)
1054
def open(self, transport):
1055
"""Open this directory."""
1056
return NotBzrDir(transport, self)
1059
def _known_formats(self):
1060
return set([NotBzrDirFormat()])
1063
class NotBzrDirProber(controldir.Prober):
1065
def probe_transport(self, transport):
1066
"""Our format is present if the transport ends in '.not/'."""
1067
if transport.has('.not'):
1068
return NotBzrDirFormat()
1071
class TestNotBzrDir(TestCaseWithTransport):
1072
"""Tests for using the bzrdir api with a non .bzr based disk format.
1074
If/when one of these is in the core, we can let the implementation tests
1078
def test_create_and_find_format(self):
1079
# create a .notbzr dir
1080
format = NotBzrDirFormat()
1081
dir = format.initialize(self.get_url())
1082
self.assertIsInstance(dir, NotBzrDir)
1084
controldir.ControlDirFormat.register_prober(NotBzrDirProber)
1086
found = bzrlib.bzrdir.BzrDirFormat.find_format(
1087
get_transport(self.get_url()))
1088
self.assertIsInstance(found, NotBzrDirFormat)
1090
controldir.ControlDirFormat.unregister_prober(NotBzrDirProber)
1092
def test_included_in_known_formats(self):
1093
not_format = NotBzrDirFormat()
1094
bzrlib.controldir.ControlDirFormat.register_format(not_format)
1096
formats = bzrlib.bzrdir.BzrDirFormat.known_formats()
1097
for format in formats:
1098
if isinstance(format, NotBzrDirFormat):
1100
self.fail("No NotBzrDirFormat in %s" % formats)
1102
bzrlib.controldir.ControlDirFormat.unregister_format(not_format)
1015
1105
class NonLocalTests(TestCaseWithTransport):
1016
1106
"""Tests for bzrdir static behaviour on non local paths."""
1338
1419
class TestGenerateBackupName(TestCaseWithMemoryTransport):
1339
# FIXME: This may need to be unified with test_osutils.TestBackupNames or
1340
# moved to per_bzrdir or per_transport for better coverage ?
1343
1421
def setUp(self):
1344
1422
super(TestGenerateBackupName, self).setUp()
1345
self._transport = self.get_transport()
1423
self._transport = get_transport(self.get_url())
1346
1424
bzrdir.BzrDir.create(self.get_url(),
1347
1425
possible_transports=[self._transport])
1348
1426
self._bzrdir = bzrdir.BzrDir.open_from_transport(self._transport)
1350
def test_deprecated_generate_backup_name(self):
1351
res = self.applyDeprecated(
1352
symbol_versioning.deprecated_in((2, 3, 0)),
1353
self._bzrdir.generate_backup_name, 'whatever')
1355
1428
def test_new(self):
1356
self.assertEqual("a.~1~", self._bzrdir._available_backup_name("a"))
1429
self.assertEqual("a.~1~", self._bzrdir.generate_backup_name("a"))
1358
1431
def test_exiting(self):
1359
1432
self._transport.put_bytes("a.~1~", "some content")
1360
self.assertEqual("a.~2~", self._bzrdir._available_backup_name("a"))
1433
self.assertEqual("a.~2~", self._bzrdir.generate_backup_name("a"))