~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_bzrdir.py

  • Committer: Jelmer Vernooij
  • Date: 2016-04-03 16:32:31 UTC
  • mto: This revision was merged to the branch mainline in revision 6617.
  • Revision ID: jelmer@jelmer.uk-20160403163231-h72bo0uyek2gikw0
Don't put French text in doc/en/user-reference when LANGUAGE=fr_CH.UTF_8.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2011 Canonical Ltd
 
1
# Copyright (C) 2006-2013, 2016 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
35
35
    revision as _mod_revision,
36
36
    osutils,
37
37
    remote,
38
 
    symbol_versioning,
39
38
    transport as _mod_transport,
40
39
    urlutils,
41
40
    win32utils,
43
42
    workingtree_4,
44
43
    )
45
44
import bzrlib.branch
 
45
from bzrlib.branchfmt.fullhistory import BzrBranchFormat5
46
46
from bzrlib.errors import (
47
47
    NotBranchError,
48
48
    NoColocatedBranchSupport,
170
170
        self.assertNotContainsRe(new, 'hidden')
171
171
 
172
172
    def test_set_default_repository(self):
173
 
        default_factory = bzrdir.format_registry.get('default')
174
 
        old_default = [k for k, v in bzrdir.format_registry.iteritems()
 
173
        default_factory = controldir.format_registry.get('default')
 
174
        old_default = [k for k, v in controldir.format_registry.iteritems()
175
175
                       if v == default_factory and k != 'default'][0]
176
 
        bzrdir.format_registry.set_default_repository('dirstate-with-subtree')
 
176
        controldir.format_registry.set_default_repository('dirstate-with-subtree')
177
177
        try:
178
 
            self.assertIs(bzrdir.format_registry.get('dirstate-with-subtree'),
179
 
                          bzrdir.format_registry.get('default'))
 
178
            self.assertIs(controldir.format_registry.get('dirstate-with-subtree'),
 
179
                          controldir.format_registry.get('default'))
180
180
            self.assertIs(
181
181
                repository.format_registry.get_default().__class__,
182
182
                knitrepo.RepositoryFormatKnit3)
183
183
        finally:
184
 
            bzrdir.format_registry.set_default_repository(old_default)
 
184
            controldir.format_registry.set_default_repository(old_default)
185
185
 
186
186
    def test_aliases(self):
187
187
        a_registry = controldir.ControlDirFormatRegistry()
212
212
    """A sample BzrDir implementation to allow testing static methods."""
213
213
 
214
214
    def create_repository(self, shared=False):
215
 
        """See BzrDir.create_repository."""
 
215
        """See ControlDir.create_repository."""
216
216
        return "A repository"
217
217
 
218
218
    def open_repository(self):
219
 
        """See BzrDir.open_repository."""
 
219
        """See ControlDir.open_repository."""
220
220
        return SampleRepository(self)
221
221
 
222
222
    def create_branch(self, name=None):
223
 
        """See BzrDir.create_branch."""
 
223
        """See ControlDir.create_branch."""
224
224
        if name is not None:
225
225
            raise NoColocatedBranchSupport(self)
226
226
        return SampleBranch(self)
227
227
 
228
228
    def create_workingtree(self):
229
 
        """See BzrDir.create_workingtree."""
 
229
        """See ControlDir.create_workingtree."""
230
230
        return "A tree"
231
231
 
232
232
 
253
253
    def open(self, transport, _found=None):
254
254
        return "opened branch."
255
255
 
 
256
    @classmethod
 
257
    def from_string(cls, format_string):
 
258
        return cls()
 
259
 
256
260
 
257
261
class BzrDirFormatTest1(bzrdir.BzrDirMetaFormat1):
258
262
 
286
290
        self.build_tree(["foo/", "bar/"], transport=t)
287
291
        def check_format(format, url):
288
292
            format.initialize(url)
289
 
            t = _mod_transport.get_transport(url)
 
293
            t = _mod_transport.get_transport_from_path(url)
290
294
            found_format = bzrdir.BzrDirFormat.find_format(t)
291
295
            self.assertIsInstance(found_format, format.__class__)
292
296
        check_format(BzrDirFormatTest1(), "foo")
295
299
    def test_find_format_nothing_there(self):
296
300
        self.assertRaises(NotBranchError,
297
301
                          bzrdir.BzrDirFormat.find_format,
298
 
                          _mod_transport.get_transport('.'))
 
302
                          _mod_transport.get_transport_from_path('.'))
299
303
 
300
304
    def test_find_format_unknown_format(self):
301
305
        t = self.get_transport()
303
307
        t.put_bytes('.bzr/branch-format', '')
304
308
        self.assertRaises(UnknownFormatError,
305
309
                          bzrdir.BzrDirFormat.find_format,
306
 
                          _mod_transport.get_transport('.'))
 
310
                          _mod_transport.get_transport_from_path('.'))
307
311
 
308
312
    def test_register_unregister_format(self):
309
313
        format = SampleBzrDirFormat()
317
321
        # which bzrdir.open_containing will refuse (not supported)
318
322
        self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open_containing, url)
319
323
        # but open_downlevel will work
320
 
        t = _mod_transport.get_transport(url)
 
324
        t = _mod_transport.get_transport_from_url(url)
321
325
        self.assertEqual(format.open(t), bzrdir.BzrDir.open_unsupported(url))
322
326
        # unregister the format
323
327
        bzrdir.BzrProber.formats.remove(format.get_format_string())
333
337
    def test_create_branch_and_repo_under_shared(self):
334
338
        # creating a branch and repo in a shared repo uses the
335
339
        # shared repository
336
 
        format = bzrdir.format_registry.make_bzrdir('knit')
 
340
        format = controldir.format_registry.make_bzrdir('knit')
337
341
        self.make_repository('.', shared=True, format=format)
338
342
        branch = bzrdir.BzrDir.create_branch_and_repo(
339
343
            self.get_url('child'), format=format)
343
347
    def test_create_branch_and_repo_under_shared_force_new(self):
344
348
        # creating a branch and repo in a shared repo can be forced to
345
349
        # make a new repo
346
 
        format = bzrdir.format_registry.make_bzrdir('knit')
 
350
        format = controldir.format_registry.make_bzrdir('knit')
347
351
        self.make_repository('.', shared=True, format=format)
348
352
        branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url('child'),
349
353
                                                      force_new_repo=True,
363
367
 
364
368
    def test_create_standalone_working_tree_under_shared_repo(self):
365
369
        # create standalone working tree always makes a repo.
366
 
        format = bzrdir.format_registry.make_bzrdir('knit')
 
370
        format = controldir.format_registry.make_bzrdir('knit')
367
371
        self.make_repository('.', shared=True, format=format)
368
372
        # note this is deliberately readonly, as this failure should
369
373
        # occur before any writes.
376
380
 
377
381
    def test_create_branch_convenience(self):
378
382
        # outside a repo the default convenience output is a repo+branch_tree
379
 
        format = bzrdir.format_registry.make_bzrdir('knit')
 
383
        format = controldir.format_registry.make_bzrdir('knit')
380
384
        branch = bzrdir.BzrDir.create_branch_convenience('.', format=format)
381
385
        branch.bzrdir.open_workingtree()
382
386
        branch.bzrdir.open_repository()
383
387
 
384
388
    def test_create_branch_convenience_possible_transports(self):
385
389
        """Check that the optional 'possible_transports' is recognized"""
386
 
        format = bzrdir.format_registry.make_bzrdir('knit')
 
390
        format = controldir.format_registry.make_bzrdir('knit')
387
391
        t = self.get_transport()
388
392
        branch = bzrdir.BzrDir.create_branch_convenience(
389
393
            '.', format=format, possible_transports=[t])
394
398
        """Creating a branch at the root of a fs should work."""
395
399
        self.vfs_transport_factory = memory.MemoryServer
396
400
        # outside a repo the default convenience output is a repo+branch_tree
397
 
        format = bzrdir.format_registry.make_bzrdir('knit')
 
401
        format = controldir.format_registry.make_bzrdir('knit')
398
402
        branch = bzrdir.BzrDir.create_branch_convenience(self.get_url(),
399
403
                                                         format=format)
400
404
        self.assertRaises(errors.NoWorkingTree,
404
408
    def test_create_branch_convenience_under_shared_repo(self):
405
409
        # inside a repo the default convenience output is a branch+ follow the
406
410
        # repo tree policy
407
 
        format = bzrdir.format_registry.make_bzrdir('knit')
 
411
        format = controldir.format_registry.make_bzrdir('knit')
408
412
        self.make_repository('.', shared=True, format=format)
409
413
        branch = bzrdir.BzrDir.create_branch_convenience('child',
410
414
            format=format)
415
419
    def test_create_branch_convenience_under_shared_repo_force_no_tree(self):
416
420
        # inside a repo the default convenience output is a branch+ follow the
417
421
        # repo tree policy but we can override that
418
 
        format = bzrdir.format_registry.make_bzrdir('knit')
 
422
        format = controldir.format_registry.make_bzrdir('knit')
419
423
        self.make_repository('.', shared=True, format=format)
420
424
        branch = bzrdir.BzrDir.create_branch_convenience('child',
421
425
            force_new_tree=False, format=format)
427
431
    def test_create_branch_convenience_under_shared_repo_no_tree_policy(self):
428
432
        # inside a repo the default convenience output is a branch+ follow the
429
433
        # repo tree policy
430
 
        format = bzrdir.format_registry.make_bzrdir('knit')
 
434
        format = controldir.format_registry.make_bzrdir('knit')
431
435
        repo = self.make_repository('.', shared=True, format=format)
432
436
        repo.set_make_working_trees(False)
433
437
        branch = bzrdir.BzrDir.create_branch_convenience('child',
440
444
    def test_create_branch_convenience_under_shared_repo_no_tree_policy_force_tree(self):
441
445
        # inside a repo the default convenience output is a branch+ follow the
442
446
        # repo tree policy but we can override that
443
 
        format = bzrdir.format_registry.make_bzrdir('knit')
 
447
        format = controldir.format_registry.make_bzrdir('knit')
444
448
        repo = self.make_repository('.', shared=True, format=format)
445
449
        repo.set_make_working_trees(False)
446
450
        branch = bzrdir.BzrDir.create_branch_convenience('child',
452
456
    def test_create_branch_convenience_under_shared_repo_force_new_repo(self):
453
457
        # inside a repo the default convenience output is overridable to give
454
458
        # repo+branch+tree
455
 
        format = bzrdir.format_registry.make_bzrdir('knit')
 
459
        format = controldir.format_registry.make_bzrdir('knit')
456
460
        self.make_repository('.', shared=True, format=format)
457
461
        branch = bzrdir.BzrDir.create_branch_convenience('child',
458
462
            force_new_repo=True, format=format)
513
517
        # Clone source into directory
514
518
        target = source_bzrdir.clone(self.get_url('parent/target'))
515
519
 
 
520
    def test_format_initialize_on_transport_ex_stacked_on(self):
 
521
        # trunk is a stackable format.  Note that its in the same server area
 
522
        # which is what launchpad does, but not sufficient to exercise the
 
523
        # general case.
 
524
        trunk = self.make_branch('trunk', format='1.9')
 
525
        t = self.get_transport('stacked')
 
526
        old_fmt = controldir.format_registry.make_bzrdir('pack-0.92')
 
527
        repo_name = old_fmt.repository_format.network_name()
 
528
        # Should end up with a 1.9 format (stackable)
 
529
        repo, control, require_stacking, repo_policy = \
 
530
            old_fmt.initialize_on_transport_ex(t,
 
531
                    repo_format_name=repo_name, stacked_on='../trunk',
 
532
                    stack_on_pwd=t.base)
 
533
        if repo is not None:
 
534
            # Repositories are open write-locked
 
535
            self.assertTrue(repo.is_write_locked())
 
536
            self.addCleanup(repo.unlock)
 
537
        else:
 
538
            repo = control.open_repository()
 
539
        self.assertIsInstance(control, bzrdir.BzrDir)
 
540
        opened = bzrdir.BzrDir.open(t.base)
 
541
        if not isinstance(old_fmt, remote.RemoteBzrDirFormat):
 
542
            self.assertEqual(control._format.network_name(),
 
543
                old_fmt.network_name())
 
544
            self.assertEqual(control._format.network_name(),
 
545
                opened._format.network_name())
 
546
        self.assertEqual(control.__class__, opened.__class__)
 
547
        self.assertLength(1, repo._fallback_repositories)
 
548
 
516
549
    def test_sprout_obeys_stacking_policy(self):
517
550
        child_branch, new_child_transport = self.prepare_default_stacking()
518
551
        new_child = child_branch.bzrdir.sprout(new_child_transport.base)
711
744
    def test_open_containing_from_transport(self):
712
745
        self.assertRaises(NotBranchError,
713
746
            bzrdir.BzrDir.open_containing_from_transport,
714
 
            _mod_transport.get_transport(self.get_readonly_url('')))
 
747
            _mod_transport.get_transport_from_url(self.get_readonly_url('')))
715
748
        self.assertRaises(NotBranchError,
716
749
            bzrdir.BzrDir.open_containing_from_transport,
717
 
            _mod_transport.get_transport(self.get_readonly_url('g/p/q')))
 
750
            _mod_transport.get_transport_from_url(
 
751
                self.get_readonly_url('g/p/q')))
718
752
        control = bzrdir.BzrDir.create(self.get_url())
719
753
        branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
720
 
            _mod_transport.get_transport(self.get_readonly_url('')))
 
754
            _mod_transport.get_transport_from_url(
 
755
                self.get_readonly_url('')))
721
756
        self.assertEqual('', relpath)
722
757
        branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
723
 
            _mod_transport.get_transport(self.get_readonly_url('g/p/q')))
 
758
            _mod_transport.get_transport_from_url(
 
759
                self.get_readonly_url('g/p/q')))
724
760
        self.assertEqual('g/p/q', relpath)
725
761
 
726
762
    def test_open_containing_tree_or_branch(self):
788
824
 
789
825
    def test_sprout_recursive(self):
790
826
        tree = self.make_branch_and_tree('tree1',
791
 
                                         format='dirstate-with-subtree')
 
827
                                         format='development-subtree')
792
828
        sub_tree = self.make_branch_and_tree('tree1/subtree',
793
 
            format='dirstate-with-subtree')
 
829
            format='development-subtree')
794
830
        sub_tree.set_root_id('subtree-root')
795
831
        tree.add_reference(sub_tree)
796
832
        self.build_tree(['tree1/subtree/file'])
813
849
 
814
850
    def test_sprout_recursive_treeless(self):
815
851
        tree = self.make_branch_and_tree('tree1',
816
 
            format='dirstate-with-subtree')
 
852
            format='development-subtree')
817
853
        sub_tree = self.make_branch_and_tree('tree1/subtree',
818
 
            format='dirstate-with-subtree')
 
854
            format='development-subtree')
819
855
        tree.add_reference(sub_tree)
820
856
        self.build_tree(['tree1/subtree/file'])
821
857
        sub_tree.add('file')
822
858
        tree.commit('Initial commit')
823
859
        # The following line force the orhaning to reveal bug #634470
824
 
        tree.branch.get_config().set_user_option(
 
860
        tree.branch.get_config_stack().set(
825
861
            'bzr.transform.orphan_policy', 'move')
826
862
        tree.bzrdir.destroy_workingtree()
827
863
        # FIXME: subtree/.bzr is left here which allows the test to pass (or
828
864
        # fail :-( ) -- vila 20100909
829
865
        repo = self.make_repository('repo', shared=True,
830
 
            format='dirstate-with-subtree')
 
866
            format='development-subtree')
831
867
        repo.set_make_working_trees(False)
832
868
        # FIXME: we just deleted the workingtree and now we want to use it ????
833
869
        # At a minimum, we should use tree.branch below (but this fails too
899
935
        def evaluate(bzrdir):
900
936
            try:
901
937
                repo = bzrdir.open_repository()
902
 
            except NoRepositoryPresent:
 
938
            except errors.NoRepositoryPresent:
903
939
                return True, bzrdir.root_transport.base
904
940
            else:
905
941
                return False, bzrdir.root_transport.base
956
992
        branch_base = t.clone('branch').base
957
993
        self.assertEqual(branch_base, dir.get_branch_transport(None).base)
958
994
        self.assertEqual(branch_base,
959
 
                         dir.get_branch_transport(bzrlib.branch.BzrBranchFormat5()).base)
 
995
                         dir.get_branch_transport(BzrBranchFormat5()).base)
960
996
        repository_base = t.clone('repository').base
961
997
        self.assertEqual(repository_base, dir.get_repository_transport(None).base)
962
998
        repository_format = repository.format_registry.get_default()
979
1015
        Metadirs should compare equal iff they have the same repo, branch and
980
1016
        tree formats.
981
1017
        """
982
 
        mydir = bzrdir.format_registry.make_bzrdir('knit')
 
1018
        mydir = controldir.format_registry.make_bzrdir('knit')
983
1019
        self.assertEqual(mydir, mydir)
984
1020
        self.assertFalse(mydir != mydir)
985
 
        otherdir = bzrdir.format_registry.make_bzrdir('knit')
 
1021
        otherdir = controldir.format_registry.make_bzrdir('knit')
986
1022
        self.assertEqual(otherdir, mydir)
987
1023
        self.assertFalse(otherdir != mydir)
988
 
        otherdir2 = bzrdir.format_registry.make_bzrdir('dirstate-with-subtree')
 
1024
        otherdir2 = controldir.format_registry.make_bzrdir('development-subtree')
989
1025
        self.assertNotEqual(otherdir2, mydir)
990
1026
        self.assertFalse(otherdir2 == mydir)
991
1027
 
 
1028
    def test_with_features(self):
 
1029
        tree = self.make_branch_and_tree('tree', format='2a')
 
1030
        tree.bzrdir.update_feature_flags({"bar": "required"})
 
1031
        self.assertRaises(errors.MissingFeature, bzrdir.BzrDir.open, 'tree')
 
1032
        bzrdir.BzrDirMetaFormat1.register_feature('bar')
 
1033
        self.addCleanup(bzrdir.BzrDirMetaFormat1.unregister_feature, 'bar')
 
1034
        dir = bzrdir.BzrDir.open('tree')
 
1035
        self.assertEqual("required", dir._format.features.get("bar"))
 
1036
        tree.bzrdir.update_feature_flags({"bar": None, "nonexistant": None})
 
1037
        dir = bzrdir.BzrDir.open('tree')
 
1038
        self.assertEqual({}, dir._format.features)
 
1039
 
992
1040
    def test_needs_conversion_different_working_tree(self):
993
1041
        # meta1dirs need an conversion if any element is not the default.
994
 
        new_format = bzrdir.format_registry.make_bzrdir('dirstate')
 
1042
        new_format = controldir.format_registry.make_bzrdir('dirstate')
995
1043
        tree = self.make_branch_and_tree('tree', format='knit')
996
1044
        self.assertTrue(tree.bzrdir.needs_format_conversion(
997
1045
            new_format))
998
1046
 
999
1047
    def test_initialize_on_format_uses_smart_transport(self):
1000
1048
        self.setup_smart_server_with_call_log()
1001
 
        new_format = bzrdir.format_registry.make_bzrdir('dirstate')
 
1049
        new_format = controldir.format_registry.make_bzrdir('dirstate')
1002
1050
        transport = self.get_transport('target')
1003
1051
        transport.ensure_base()
1004
1052
        self.reset_smart_call_log()
1022
1070
 
1023
1071
    def test_create_branch_convenience(self):
1024
1072
        # outside a repo the default convenience output is a repo+branch_tree
1025
 
        format = bzrdir.format_registry.make_bzrdir('knit')
 
1073
        format = controldir.format_registry.make_bzrdir('knit')
1026
1074
        branch = bzrdir.BzrDir.create_branch_convenience(
1027
1075
            self.get_url('foo'), format=format)
1028
1076
        self.assertRaises(errors.NoWorkingTree,
1031
1079
 
1032
1080
    def test_create_branch_convenience_force_tree_not_local_fails(self):
1033
1081
        # outside a repo the default convenience output is a repo+branch_tree
1034
 
        format = bzrdir.format_registry.make_bzrdir('knit')
 
1082
        format = controldir.format_registry.make_bzrdir('knit')
1035
1083
        self.assertRaises(errors.NotLocalUrl,
1036
1084
            bzrdir.BzrDir.create_branch_convenience,
1037
1085
            self.get_url('foo'),
1042
1090
 
1043
1091
    def test_clone(self):
1044
1092
        # clone into a nonlocal path works
1045
 
        format = bzrdir.format_registry.make_bzrdir('knit')
 
1093
        format = controldir.format_registry.make_bzrdir('knit')
1046
1094
        branch = bzrdir.BzrDir.create_branch_convenience('local',
1047
1095
                                                         format=format)
1048
1096
        branch.bzrdir.open_workingtree()
1183
1231
            raise TestSkipped('unable to make file hidden without pywin32 library')
1184
1232
        b = bzrdir.BzrDir.create('.')
1185
1233
        self.build_tree(['a'])
1186
 
        self.assertEquals(['a'], self.get_ls())
 
1234
        self.assertEqual(['a'], self.get_ls())
1187
1235
 
1188
1236
    def test_dot_bzr_hidden_with_url(self):
1189
1237
        if sys.platform == 'win32' and not win32utils.has_win32file:
1190
1238
            raise TestSkipped('unable to make file hidden without pywin32 library')
1191
1239
        b = bzrdir.BzrDir.create(urlutils.local_path_to_url('.'))
1192
1240
        self.build_tree(['a'])
1193
 
        self.assertEquals(['a'], self.get_ls())
 
1241
        self.assertEqual(['a'], self.get_ls())
1194
1242
 
1195
1243
 
1196
1244
class _TestBzrDirFormat(bzrdir.BzrDirMetaFormat1):
1212
1260
        self.test_branch = _TestBranch(self.transport)
1213
1261
        self.test_branch.repository = self.create_repository()
1214
1262
 
1215
 
    def open_branch(self, unsupported=False):
 
1263
    def open_branch(self, unsupported=False, possible_transports=None):
1216
1264
        return self.test_branch
1217
1265
 
1218
1266
    def cloning_metadir(self, require_stacking=False):
1250
1298
    def _get_config(self):
1251
1299
        return config.TransportConfig(self._transport, 'branch.conf')
1252
1300
 
 
1301
    def _get_config_store(self):
 
1302
        return config.BranchStore(self)
 
1303
 
1253
1304
    def set_parent(self, parent):
1254
1305
        self._parent = parent
1255
1306
 
1320
1371
        self.assertEqual('fail', err._preformatted_string)
1321
1372
 
1322
1373
    def test_post_repo_init(self):
1323
 
        from bzrlib.bzrdir import RepoInitHookParams
 
1374
        from bzrlib.controldir import RepoInitHookParams
1324
1375
        calls = []
1325
1376
        bzrdir.BzrDir.hooks.install_named_hook('post_repo_init',
1326
1377
            calls.append, None)
1353
1404
            possible_transports=[self._transport])
1354
1405
        self._bzrdir = bzrdir.BzrDir.open_from_transport(self._transport)
1355
1406
 
1356
 
    def test_deprecated_generate_backup_name(self):
1357
 
        res = self.applyDeprecated(
1358
 
                symbol_versioning.deprecated_in((2, 3, 0)),
1359
 
                self._bzrdir.generate_backup_name, 'whatever')
1360
 
 
1361
1407
    def test_new(self):
1362
1408
        self.assertEqual("a.~1~", self._bzrdir._available_backup_name("a"))
1363
1409
 
1366
1412
        self.assertEqual("a.~2~", self._bzrdir._available_backup_name("a"))
1367
1413
 
1368
1414
 
1369
 
class ExtractFormatStringTests(TestCase):
1370
 
 
1371
 
    def test_normal(self):
1372
 
        self.assertEquals("Bazaar-NG branch, format 0.0.4\n",
1373
 
            bzrdir.extract_format_string("Bazaar-NG branch, format 0.0.4\n"))
1374
 
 
1375
 
    def test_with_optional_feature(self):
1376
 
        self.assertEquals("Bazaar-NG branch, format 0.0.4\n",
1377
 
            bzrdir.extract_format_string("Bazaar-NG branch, format 0.0.4\n"
1378
 
                                         "optional feature foo\n"))
1379
 
 
1380
 
    def test_with_required_feature(self):
1381
 
        self.assertRaises(errors.MissingFeature,
1382
 
            bzrdir.extract_format_string, "Bazaar-NG branch, format 0.0.4\n"
1383
 
                                          "required feature foo\n")
1384
 
 
1385
 
    def test_with_invalid_line(self):
1386
 
        self.assertRaises(errors.ParseFormatError,
1387
 
            bzrdir.extract_format_string, "Bazaar-NG branch, format 0.0.4\n"
1388
 
                                          "requiredfoo\n")
 
1415
class TestMeta1DirColoFormat(TestCaseWithTransport):
 
1416
    """Tests specific to the meta1 dir with colocated branches format."""
 
1417
 
 
1418
    def test_supports_colo(self):
 
1419
        format = bzrdir.BzrDirMetaFormat1Colo()
 
1420
        self.assertTrue(format.colocated_branches)
 
1421
 
 
1422
    def test_upgrade_from_2a(self):
 
1423
        tree = self.make_branch_and_tree('.', format='2a')
 
1424
        format = bzrdir.BzrDirMetaFormat1Colo()
 
1425
        self.assertTrue(tree.bzrdir.needs_format_conversion(format))
 
1426
        converter = tree.bzrdir._format.get_converter(format)
 
1427
        result = converter.convert(tree.bzrdir, None)
 
1428
        self.assertIsInstance(result._format, bzrdir.BzrDirMetaFormat1Colo)
 
1429
        self.assertFalse(result.needs_format_conversion(format))
 
1430
 
 
1431
    def test_downgrade_to_2a(self):
 
1432
        tree = self.make_branch_and_tree('.', format='development-colo')
 
1433
        format = bzrdir.BzrDirMetaFormat1()
 
1434
        self.assertTrue(tree.bzrdir.needs_format_conversion(format))
 
1435
        converter = tree.bzrdir._format.get_converter(format)
 
1436
        result = converter.convert(tree.bzrdir, None)
 
1437
        self.assertIsInstance(result._format, bzrdir.BzrDirMetaFormat1)
 
1438
        self.assertFalse(result.needs_format_conversion(format))
 
1439
 
 
1440
    def test_downgrade_to_2a_too_many_branches(self):
 
1441
        tree = self.make_branch_and_tree('.', format='development-colo')
 
1442
        tree.bzrdir.create_branch(name="another-colocated-branch")
 
1443
        converter = tree.bzrdir._format.get_converter(
 
1444
            bzrdir.BzrDirMetaFormat1())
 
1445
        result = converter.convert(tree.bzrdir, bzrdir.BzrDirMetaFormat1())
 
1446
        self.assertIsInstance(result._format, bzrdir.BzrDirMetaFormat1)
 
1447
 
 
1448
    def test_nested(self):
 
1449
        tree = self.make_branch_and_tree('.', format='development-colo')
 
1450
        tree.bzrdir.create_branch(name='foo')
 
1451
        tree.bzrdir.create_branch(name='fool/bla')
 
1452
        self.assertRaises(
 
1453
            errors.ParentBranchExists, tree.bzrdir.create_branch,
 
1454
            name='foo/bar')
 
1455
 
 
1456
    def test_parent(self):
 
1457
        tree = self.make_branch_and_tree('.', format='development-colo')
 
1458
        tree.bzrdir.create_branch(name='fool/bla')
 
1459
        tree.bzrdir.create_branch(name='foo/bar')
 
1460
        self.assertRaises(
 
1461
            errors.AlreadyBranchError, tree.bzrdir.create_branch,
 
1462
            name='foo')
 
1463
 
 
1464
 
 
1465
class SampleBzrFormat(bzrdir.BzrFormat):
 
1466
 
 
1467
    @classmethod
 
1468
    def get_format_string(cls):
 
1469
        return "First line\n"
 
1470
 
 
1471
 
 
1472
class TestBzrFormat(TestCase):
 
1473
    """Tests for BzrFormat."""
 
1474
 
 
1475
    def test_as_string(self):
 
1476
        format = SampleBzrFormat()
 
1477
        format.features = {"foo": "required"}
 
1478
        self.assertEqual(format.as_string(),
 
1479
            "First line\n"
 
1480
            "required foo\n")
 
1481
        format.features["another"] = "optional"
 
1482
        self.assertEqual(format.as_string(),
 
1483
            "First line\n"
 
1484
            "required foo\n"
 
1485
            "optional another\n")
 
1486
 
 
1487
    def test_network_name(self):
 
1488
        # The network string should include the feature info
 
1489
        format = SampleBzrFormat()
 
1490
        format.features = {"foo": "required"}
 
1491
        self.assertEqual(
 
1492
            "First line\nrequired foo\n",
 
1493
            format.network_name())
 
1494
 
 
1495
    def test_from_string_no_features(self):
 
1496
        # No features
 
1497
        format = SampleBzrFormat.from_string(
 
1498
            "First line\n")
 
1499
        self.assertEqual({}, format.features)
 
1500
 
 
1501
    def test_from_string_with_feature(self):
 
1502
        # Proper feature
 
1503
        format = SampleBzrFormat.from_string(
 
1504
            "First line\nrequired foo\n")
 
1505
        self.assertEqual("required", format.features.get("foo"))
 
1506
 
 
1507
    def test_from_string_format_string_mismatch(self):
 
1508
        # The first line has to match the format string
 
1509
        self.assertRaises(AssertionError, SampleBzrFormat.from_string,
 
1510
            "Second line\nrequired foo\n")
 
1511
 
 
1512
    def test_from_string_missing_space(self):
 
1513
        # At least one space is required in the feature lines
 
1514
        self.assertRaises(errors.ParseFormatError, SampleBzrFormat.from_string,
 
1515
            "First line\nfoo\n")
 
1516
 
 
1517
    def test_from_string_with_spaces(self):
 
1518
        # Feature with spaces (in case we add stuff like this in the future)
 
1519
        format = SampleBzrFormat.from_string(
 
1520
            "First line\nrequired foo with spaces\n")
 
1521
        self.assertEqual("required", format.features.get("foo with spaces"))
 
1522
 
 
1523
    def test_eq(self):
 
1524
        format1 = SampleBzrFormat()
 
1525
        format1.features = {"nested-trees": "optional"}
 
1526
        format2 = SampleBzrFormat()
 
1527
        format2.features = {"nested-trees": "optional"}
 
1528
        self.assertEqual(format1, format1)
 
1529
        self.assertEqual(format1, format2)
 
1530
        format3 = SampleBzrFormat()
 
1531
        self.assertNotEqual(format1, format3)
 
1532
 
 
1533
    def test_check_support_status_optional(self):
 
1534
        # Optional, so silently ignore
 
1535
        format = SampleBzrFormat()
 
1536
        format.features = {"nested-trees": "optional"}
 
1537
        format.check_support_status(True)
 
1538
        self.addCleanup(SampleBzrFormat.unregister_feature, "nested-trees")
 
1539
        SampleBzrFormat.register_feature("nested-trees")
 
1540
        format.check_support_status(True)
 
1541
 
 
1542
    def test_check_support_status_required(self):
 
1543
        # Optional, so trigger an exception
 
1544
        format = SampleBzrFormat()
 
1545
        format.features = {"nested-trees": "required"}
 
1546
        self.assertRaises(errors.MissingFeature, format.check_support_status,
 
1547
            True)
 
1548
        self.addCleanup(SampleBzrFormat.unregister_feature, "nested-trees")
 
1549
        SampleBzrFormat.register_feature("nested-trees")
 
1550
        format.check_support_status(True)
 
1551
 
 
1552
    def test_check_support_status_unknown(self):
 
1553
        # treat unknown necessity as required
 
1554
        format = SampleBzrFormat()
 
1555
        format.features = {"nested-trees": "unknown"}
 
1556
        self.assertRaises(errors.MissingFeature, format.check_support_status,
 
1557
            True)
 
1558
        self.addCleanup(SampleBzrFormat.unregister_feature, "nested-trees")
 
1559
        SampleBzrFormat.register_feature("nested-trees")
 
1560
        format.check_support_status(True)
 
1561
 
 
1562
    def test_feature_already_registered(self):
 
1563
        # a feature can only be registered once
 
1564
        self.addCleanup(SampleBzrFormat.unregister_feature, "nested-trees")
 
1565
        SampleBzrFormat.register_feature("nested-trees")
 
1566
        self.assertRaises(errors.FeatureAlreadyRegistered,
 
1567
            SampleBzrFormat.register_feature, "nested-trees")
 
1568
 
 
1569
    def test_feature_with_space(self):
 
1570
        # spaces are not allowed in feature names
 
1571
        self.assertRaises(ValueError, SampleBzrFormat.register_feature,
 
1572
            "nested trees")