55
61
from bzrlib.tests.test_http import TestWithTransport_pycurl
56
62
from bzrlib.transport import (
61
66
from bzrlib.transport.http._urllib import HttpTransport_urllib
62
67
from bzrlib.transport.nosmart import NoSmartTransportDecorator
63
68
from bzrlib.transport.readonly import ReadonlyTransportDecorator
64
from bzrlib.repofmt import knitrepo, weaverepo, pack_repo
69
from bzrlib.repofmt import knitrepo, knitpack_repo
67
72
class TestDefaultFormat(TestCase):
69
74
def test_get_set_default_format(self):
70
75
old_format = bzrdir.BzrDirFormat.get_default_format()
71
# default is BzrDirFormat6
72
self.failUnless(isinstance(old_format, bzrdir.BzrDirMetaFormat1))
76
# default is BzrDirMetaFormat1
77
self.assertIsInstance(old_format, bzrdir.BzrDirMetaFormat1)
73
78
controldir.ControlDirFormat._set_default_format(SampleBzrDirFormat())
74
79
# creating a bzr dir should now create an instrumented dir.
76
81
result = bzrdir.BzrDir.create('memory:///')
77
self.failUnless(isinstance(result, SampleBzrDir))
82
self.assertIsInstance(result, SampleBzrDir)
79
84
controldir.ControlDirFormat._set_default_format(old_format)
80
85
self.assertEqual(old_format, bzrdir.BzrDirFormat.get_default_format())
88
class DeprecatedBzrDirFormat(bzrdir.BzrDirFormat):
89
"""A deprecated bzr dir format."""
83
92
class TestFormatRegistry(TestCase):
85
94
def make_format_registry(self):
86
95
my_format_registry = controldir.ControlDirFormatRegistry()
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)
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',
92
102
bzrdir.register_metadir(my_format_registry, 'knit',
93
103
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
94
104
'Format using knits',
105
115
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
106
116
'Experimental successor to knit. Use at your own risk.',
107
117
branch_format='bzrlib.branch.BzrBranchFormat6', 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,
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)
114
123
return my_format_registry
116
125
def test_format_registry(self):
117
126
my_format_registry = self.make_format_registry()
118
127
my_bzrdir = my_format_registry.make_bzrdir('lazy')
119
self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
120
my_bzrdir = my_format_registry.make_bzrdir('weave')
121
self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
128
self.assertIsInstance(my_bzrdir, DeprecatedBzrDirFormat)
129
my_bzrdir = my_format_registry.make_bzrdir('deprecated')
130
self.assertIsInstance(my_bzrdir, DeprecatedBzrDirFormat)
122
131
my_bzrdir = my_format_registry.make_bzrdir('default')
123
132
self.assertIsInstance(my_bzrdir.repository_format,
124
133
knitrepo.RepositoryFormatKnit1)
169
177
self.assertIs(bzrdir.format_registry.get('dirstate-with-subtree'),
170
178
bzrdir.format_registry.get('default'))
172
repository.RepositoryFormat.get_default_format().__class__,
180
repository.format_registry.get_default().__class__,
173
181
knitrepo.RepositoryFormatKnit3)
175
183
bzrdir.format_registry.set_default_repository(old_default)
177
185
def test_aliases(self):
178
186
a_registry = controldir.ControlDirFormatRegistry()
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())
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())
188
196
class SampleBranch(bzrlib.branch.Branch):
245
253
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"
248
270
class TestBzrDirFormat(TestCaseWithTransport):
249
271
"""Tests for the BzrDirFormat facility."""
251
273
def test_find_format(self):
252
274
# is the right format object found for a branch?
253
275
# create a branch with a few known format objects.
254
# this is not quite the same as
255
t = get_transport(self.get_url())
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()
256
285
self.build_tree(["foo/", "bar/"], transport=t)
257
286
def check_format(format, url):
258
287
format.initialize(url)
259
t = get_transport(url)
288
t = _mod_transport.get_transport(url)
260
289
found_format = bzrdir.BzrDirFormat.find_format(t)
261
self.failUnless(isinstance(found_format, format.__class__))
262
check_format(bzrdir.BzrDirFormat5(), "foo")
263
check_format(bzrdir.BzrDirFormat6(), "bar")
290
self.assertIsInstance(found_format, format.__class__)
291
check_format(BzrDirFormatTest1(), "foo")
292
check_format(BzrDirFormatTest2(), "bar")
265
294
def test_find_format_nothing_there(self):
266
295
self.assertRaises(NotBranchError,
267
296
bzrdir.BzrDirFormat.find_format,
297
_mod_transport.get_transport('.'))
270
299
def test_find_format_unknown_format(self):
271
t = get_transport(self.get_url())
300
t = self.get_transport()
273
302
t.put_bytes('.bzr/branch-format', '')
274
303
self.assertRaises(UnknownFormatError,
275
304
bzrdir.BzrDirFormat.find_format,
305
_mod_transport.get_transport('.'))
278
307
def test_register_unregister_format(self):
279
308
format = SampleBzrDirFormat()
282
311
format.initialize(url)
283
312
# register a format for it.
284
bzrdir.BzrDirFormat.register_format(format)
313
bzrdir.BzrProber.formats.register(format.get_format_string(), format)
285
314
# which bzrdir.Open will refuse (not supported)
286
315
self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open, url)
287
316
# which bzrdir.open_containing will refuse (not supported)
288
317
self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open_containing, url)
289
318
# but open_downlevel will work
290
t = get_transport(url)
319
t = _mod_transport.get_transport(url)
291
320
self.assertEqual(format.open(t), bzrdir.BzrDir.open_unsupported(url))
292
321
# unregister the format
293
bzrdir.BzrDirFormat.unregister_format(format)
322
bzrdir.BzrProber.formats.remove(format.get_format_string())
294
323
# now open_downlevel should fail too.
295
324
self.assertRaises(UnknownFormatError, bzrdir.BzrDir.open_unsupported, url)
679
708
self.assertEqual(relpath, 'baz')
681
710
def test_open_containing_from_transport(self):
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')))
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')))
686
717
control = bzrdir.BzrDir.create(self.get_url())
687
718
branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
688
get_transport(self.get_readonly_url('')))
719
_mod_transport.get_transport(self.get_readonly_url('')))
689
720
self.assertEqual('', relpath)
690
721
branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
691
get_transport(self.get_readonly_url('g/p/q')))
722
_mod_transport.get_transport(self.get_readonly_url('g/p/q')))
692
723
self.assertEqual('g/p/q', relpath)
694
725
def test_open_containing_tree_or_branch(self):
738
769
# transport pointing at bzrdir should give a bzrdir with root transport
739
770
# set to the given transport
740
771
control = bzrdir.BzrDir.create(self.get_url())
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)
772
t = self.get_transport()
773
opened_bzrdir = bzrdir.BzrDir.open_from_transport(t)
774
self.assertEqual(t.base, opened_bzrdir.root_transport.base)
744
775
self.assertIsInstance(opened_bzrdir, bzrdir.BzrDir)
746
777
def test_open_from_transport_no_bzrdir(self):
747
transport = get_transport(self.get_url())
748
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
778
t = self.get_transport()
779
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport, t)
751
781
def test_open_from_transport_bzrdir_in_parent(self):
752
782
control = bzrdir.BzrDir.create(self.get_url())
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,
783
t = self.get_transport()
785
t = t.clone('subdir')
786
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport, t)
759
788
def test_sprout_recursive(self):
760
789
tree = self.make_branch_and_tree('tree1',
790
819
self.build_tree(['tree1/subtree/file'])
791
820
sub_tree.add('file')
792
821
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')
793
825
tree.bzrdir.destroy_workingtree()
826
# FIXME: subtree/.bzr is left here which allows the test to pass (or
827
# fail :-( ) -- vila 20100909
794
828
repo = self.make_repository('repo', shared=True,
795
829
format='dirstate-with-subtree')
796
830
repo.set_make_working_trees(False)
797
tree.bzrdir.sprout('repo/tree2')
798
self.failUnlessExists('repo/tree2/subtree')
799
self.failIfExists('repo/tree2/subtree/file')
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')
801
844
def make_foo_bar_baz(self):
802
845
foo = bzrdir.BzrDir.create_branch_convenience('foo').bzrdir
878
919
root = self.make_repository('', shared=True)
879
920
foo, bar, baz = self.make_foo_bar_baz()
880
921
qux = self.make_bzrdir('foo/qux')
881
transport = get_transport(self.get_url())
882
branches = bzrdir.BzrDir.find_branches(transport)
922
t = self.get_transport()
923
branches = bzrdir.BzrDir.find_branches(t)
883
924
self.assertEqual(baz.root_transport.base, branches[0].base)
884
925
self.assertEqual(foo.root_transport.base, branches[1].base)
885
926
self.assertEqual(bar.root_transport.base, branches[2].base)
887
928
# ensure this works without a top-level repo
888
branches = bzrdir.BzrDir.find_branches(transport.clone('foo'))
929
branches = bzrdir.BzrDir.find_branches(t.clone('foo'))
889
930
self.assertEqual(foo.root_transport.base, branches[0].base)
890
931
self.assertEqual(bar.root_transport.base, branches[1].base)
917
958
dir.get_branch_transport(bzrlib.branch.BzrBranchFormat5()).base)
918
959
repository_base = t.clone('repository').base
919
960
self.assertEqual(repository_base, dir.get_repository_transport(None).base)
961
repository_format = repository.format_registry.get_default()
920
962
self.assertEqual(repository_base,
921
dir.get_repository_transport(weaverepo.RepositoryFormat7()).base)
963
dir.get_repository_transport(repository_format).base)
922
964
checkout_base = t.clone('checkout').base
923
965
self.assertEqual(checkout_base, dir.get_workingtree_transport(None).base)
924
966
self.assertEqual(checkout_base,
925
dir.get_workingtree_transport(workingtree.WorkingTreeFormat3()).base)
967
dir.get_workingtree_transport(workingtree_3.WorkingTreeFormat3()).base)
927
969
def test_meta1dir_uses_lockdir(self):
928
970
"""Meta1 format uses a LockDir to guard the whole directory, not a file."""
970
1012
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)
1105
1015
class NonLocalTests(TestCaseWithTransport):
1106
1016
"""Tests for bzrdir static behaviour on non local paths."""
1419
1338
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 ?
1421
1343
def setUp(self):
1422
1344
super(TestGenerateBackupName, self).setUp()
1423
self._transport = get_transport(self.get_url())
1345
self._transport = self.get_transport()
1424
1346
bzrdir.BzrDir.create(self.get_url(),
1425
1347
possible_transports=[self._transport])
1426
1348
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')
1428
1355
def test_new(self):
1429
self.assertEqual("a.~1~", self._bzrdir.generate_backup_name("a"))
1356
self.assertEqual("a.~1~", self._bzrdir._available_backup_name("a"))
1431
1358
def test_exiting(self):
1432
1359
self._transport.put_bytes("a.~1~", "some content")
1433
self.assertEqual("a.~2~", self._bzrdir.generate_backup_name("a"))
1360
self.assertEqual("a.~2~", self._bzrdir._available_backup_name("a"))