~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_bzrdir.py

  • Committer: Jelmer Vernooij
  • Date: 2012-01-27 21:28:56 UTC
  • mto: This revision was merged to the branch mainline in revision 6460.
  • Revision ID: jelmer@samba.org-20120127212856-ewnjgn7fyblphcqw
Migrate mail_client to config stacks.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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())
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 = bzrdir.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')
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
985
1021
        otherdir = bzrdir.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 = bzrdir.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.assertEquals("required", dir._format.features.get("bar"))
 
1036
        tree.bzrdir.update_feature_flags({"bar": None, "nonexistant": None})
 
1037
        dir = bzrdir.BzrDir.open('tree')
 
1038
        self.assertEquals({}, 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
1042
        new_format = bzrdir.format_registry.make_bzrdir('dirstate')
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)
1366
1417
        self.assertEqual("a.~2~", self._bzrdir._available_backup_name("a"))
1367
1418
 
1368
1419
 
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")
 
1420
class TestMeta1DirColoFormat(TestCaseWithTransport):
 
1421
    """Tests specific to the meta1 dir with colocated branches format."""
 
1422
 
 
1423
    def test_supports_colo(self):
 
1424
        format = bzrdir.BzrDirMetaFormat1Colo()
 
1425
        self.assertTrue(format.colocated_branches)
 
1426
 
 
1427
    def test_upgrade_from_2a(self):
 
1428
        tree = self.make_branch_and_tree('.', format='2a')
 
1429
        format = bzrdir.BzrDirMetaFormat1Colo()
 
1430
        self.assertTrue(tree.bzrdir.needs_format_conversion(format))
 
1431
        converter = tree.bzrdir._format.get_converter(format)
 
1432
        result = converter.convert(tree.bzrdir, None)
 
1433
        self.assertIsInstance(result._format, bzrdir.BzrDirMetaFormat1Colo)
 
1434
        self.assertFalse(result.needs_format_conversion(format))
 
1435
 
 
1436
    def test_downgrade_to_2a(self):
 
1437
        tree = self.make_branch_and_tree('.', format='development-colo')
 
1438
        format = bzrdir.BzrDirMetaFormat1()
 
1439
        self.assertTrue(tree.bzrdir.needs_format_conversion(format))
 
1440
        converter = tree.bzrdir._format.get_converter(format)
 
1441
        result = converter.convert(tree.bzrdir, None)
 
1442
        self.assertIsInstance(result._format, bzrdir.BzrDirMetaFormat1)
 
1443
        self.assertFalse(result.needs_format_conversion(format))
 
1444
 
 
1445
    def test_downgrade_to_2a_too_many_branches(self):
 
1446
        tree = self.make_branch_and_tree('.', format='development-colo')
 
1447
        tree.bzrdir.create_branch(name="another-colocated-branch")
 
1448
        converter = tree.bzrdir._format.get_converter(
 
1449
            bzrdir.BzrDirMetaFormat1())
 
1450
        self.assertRaises(errors.BzrError, converter.convert, tree.bzrdir,
 
1451
            None)
 
1452
 
 
1453
 
 
1454
class SampleBzrFormat(bzrdir.BzrFormat):
 
1455
 
 
1456
    @classmethod
 
1457
    def get_format_string(cls):
 
1458
        return "First line\n"
 
1459
 
 
1460
 
 
1461
class TestBzrFormat(TestCase):
 
1462
    """Tests for BzrFormat."""
 
1463
 
 
1464
    def test_as_string(self):
 
1465
        format = SampleBzrFormat()
 
1466
        format.features = {"foo": "required"}
 
1467
        self.assertEquals(format.as_string(),
 
1468
            "First line\n"
 
1469
            "required foo\n")
 
1470
        format.features["another"] = "optional"
 
1471
        self.assertEquals(format.as_string(),
 
1472
            "First line\n"
 
1473
            "required foo\n"
 
1474
            "optional another\n")
 
1475
 
 
1476
    def test_network_name(self):
 
1477
        # The network string should include the feature info
 
1478
        format = SampleBzrFormat()
 
1479
        format.features = {"foo": "required"}
 
1480
        self.assertEquals(
 
1481
            "First line\nrequired foo\n",
 
1482
            format.network_name())
 
1483
 
 
1484
    def test_from_string_no_features(self):
 
1485
        # No features
 
1486
        format = SampleBzrFormat.from_string(
 
1487
            "First line\n")
 
1488
        self.assertEquals({}, format.features)
 
1489
 
 
1490
    def test_from_string_with_feature(self):
 
1491
        # Proper feature
 
1492
        format = SampleBzrFormat.from_string(
 
1493
            "First line\nrequired foo\n")
 
1494
        self.assertEquals("required", format.features.get("foo"))
 
1495
 
 
1496
    def test_from_string_format_string_mismatch(self):
 
1497
        # The first line has to match the format string
 
1498
        self.assertRaises(AssertionError, SampleBzrFormat.from_string,
 
1499
            "Second line\nrequired foo\n")
 
1500
 
 
1501
    def test_from_string_missing_space(self):
 
1502
        # At least one space is required in the feature lines
 
1503
        self.assertRaises(errors.ParseFormatError, SampleBzrFormat.from_string,
 
1504
            "First line\nfoo\n")
 
1505
 
 
1506
    def test_from_string_with_spaces(self):
 
1507
        # Feature with spaces (in case we add stuff like this in the future)
 
1508
        format = SampleBzrFormat.from_string(
 
1509
            "First line\nrequired foo with spaces\n")
 
1510
        self.assertEquals("required", format.features.get("foo with spaces"))
 
1511
 
 
1512
    def test_eq(self):
 
1513
        format1 = SampleBzrFormat()
 
1514
        format1.features = {"nested-trees": "optional"}
 
1515
        format2 = SampleBzrFormat()
 
1516
        format2.features = {"nested-trees": "optional"}
 
1517
        self.assertEquals(format1, format1)
 
1518
        self.assertEquals(format1, format2)
 
1519
        format3 = SampleBzrFormat()
 
1520
        self.assertNotEquals(format1, format3)
 
1521
 
 
1522
    def test_check_support_status_optional(self):
 
1523
        # Optional, so silently ignore
 
1524
        format = SampleBzrFormat()
 
1525
        format.features = {"nested-trees": "optional"}
 
1526
        format.check_support_status(True)
 
1527
        self.addCleanup(SampleBzrFormat.unregister_feature, "nested-trees")
 
1528
        SampleBzrFormat.register_feature("nested-trees")
 
1529
        format.check_support_status(True)
 
1530
 
 
1531
    def test_check_support_status_required(self):
 
1532
        # Optional, so trigger an exception
 
1533
        format = SampleBzrFormat()
 
1534
        format.features = {"nested-trees": "required"}
 
1535
        self.assertRaises(errors.MissingFeature, format.check_support_status,
 
1536
            True)
 
1537
        self.addCleanup(SampleBzrFormat.unregister_feature, "nested-trees")
 
1538
        SampleBzrFormat.register_feature("nested-trees")
 
1539
        format.check_support_status(True)
 
1540
 
 
1541
    def test_check_support_status_unknown(self):
 
1542
        # treat unknown necessity as required
 
1543
        format = SampleBzrFormat()
 
1544
        format.features = {"nested-trees": "unknown"}
 
1545
        self.assertRaises(errors.MissingFeature, format.check_support_status,
 
1546
            True)
 
1547
        self.addCleanup(SampleBzrFormat.unregister_feature, "nested-trees")
 
1548
        SampleBzrFormat.register_feature("nested-trees")
 
1549
        format.check_support_status(True)
 
1550
 
 
1551
    def test_feature_already_registered(self):
 
1552
        # a feature can only be registered once
 
1553
        self.addCleanup(SampleBzrFormat.unregister_feature, "nested-trees")
 
1554
        SampleBzrFormat.register_feature("nested-trees")
 
1555
        self.assertRaises(errors.FeatureAlreadyRegistered,
 
1556
            SampleBzrFormat.register_feature, "nested-trees")
 
1557
 
 
1558
    def test_feature_with_space(self):
 
1559
        # spaces are not allowed in feature names
 
1560
        self.assertRaises(ValueError, SampleBzrFormat.register_feature,
 
1561
            "nested trees")