~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_bzrdir.py

  • Committer: Samuel Bronson
  • Date: 2012-08-30 20:36:18 UTC
  • mto: (6015.57.3 2.4)
  • mto: This revision was merged to the branch mainline in revision 6558.
  • Revision ID: naesten@gmail.com-20120830203618-y2dzw91nqpvpgxvx
Update INSTALL for switch to Python 2.6 and up.

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
 
 
260
256
 
261
257
class BzrDirFormatTest1(bzrdir.BzrDirMetaFormat1):
262
258
 
290
286
        self.build_tree(["foo/", "bar/"], transport=t)
291
287
        def check_format(format, url):
292
288
            format.initialize(url)
293
 
            t = _mod_transport.get_transport_from_path(url)
 
289
            t = _mod_transport.get_transport(url)
294
290
            found_format = bzrdir.BzrDirFormat.find_format(t)
295
291
            self.assertIsInstance(found_format, format.__class__)
296
292
        check_format(BzrDirFormatTest1(), "foo")
299
295
    def test_find_format_nothing_there(self):
300
296
        self.assertRaises(NotBranchError,
301
297
                          bzrdir.BzrDirFormat.find_format,
302
 
                          _mod_transport.get_transport_from_path('.'))
 
298
                          _mod_transport.get_transport('.'))
303
299
 
304
300
    def test_find_format_unknown_format(self):
305
301
        t = self.get_transport()
307
303
        t.put_bytes('.bzr/branch-format', '')
308
304
        self.assertRaises(UnknownFormatError,
309
305
                          bzrdir.BzrDirFormat.find_format,
310
 
                          _mod_transport.get_transport_from_path('.'))
 
306
                          _mod_transport.get_transport('.'))
311
307
 
312
308
    def test_register_unregister_format(self):
313
309
        format = SampleBzrDirFormat()
321
317
        # which bzrdir.open_containing will refuse (not supported)
322
318
        self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open_containing, url)
323
319
        # but open_downlevel will work
324
 
        t = _mod_transport.get_transport_from_url(url)
 
320
        t = _mod_transport.get_transport(url)
325
321
        self.assertEqual(format.open(t), bzrdir.BzrDir.open_unsupported(url))
326
322
        # unregister the format
327
323
        bzrdir.BzrProber.formats.remove(format.get_format_string())
517
513
        # Clone source into directory
518
514
        target = source_bzrdir.clone(self.get_url('parent/target'))
519
515
 
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
 
 
549
516
    def test_sprout_obeys_stacking_policy(self):
550
517
        child_branch, new_child_transport = self.prepare_default_stacking()
551
518
        new_child = child_branch.bzrdir.sprout(new_child_transport.base)
744
711
    def test_open_containing_from_transport(self):
745
712
        self.assertRaises(NotBranchError,
746
713
            bzrdir.BzrDir.open_containing_from_transport,
747
 
            _mod_transport.get_transport_from_url(self.get_readonly_url('')))
 
714
            _mod_transport.get_transport(self.get_readonly_url('')))
748
715
        self.assertRaises(NotBranchError,
749
716
            bzrdir.BzrDir.open_containing_from_transport,
750
 
            _mod_transport.get_transport_from_url(
751
 
                self.get_readonly_url('g/p/q')))
 
717
            _mod_transport.get_transport(self.get_readonly_url('g/p/q')))
752
718
        control = bzrdir.BzrDir.create(self.get_url())
753
719
        branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
754
 
            _mod_transport.get_transport_from_url(
755
 
                self.get_readonly_url('')))
 
720
            _mod_transport.get_transport(self.get_readonly_url('')))
756
721
        self.assertEqual('', relpath)
757
722
        branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
758
 
            _mod_transport.get_transport_from_url(
759
 
                self.get_readonly_url('g/p/q')))
 
723
            _mod_transport.get_transport(self.get_readonly_url('g/p/q')))
760
724
        self.assertEqual('g/p/q', relpath)
761
725
 
762
726
    def test_open_containing_tree_or_branch(self):
824
788
 
825
789
    def test_sprout_recursive(self):
826
790
        tree = self.make_branch_and_tree('tree1',
827
 
                                         format='development-subtree')
 
791
                                         format='dirstate-with-subtree')
828
792
        sub_tree = self.make_branch_and_tree('tree1/subtree',
829
 
            format='development-subtree')
 
793
            format='dirstate-with-subtree')
830
794
        sub_tree.set_root_id('subtree-root')
831
795
        tree.add_reference(sub_tree)
832
796
        self.build_tree(['tree1/subtree/file'])
849
813
 
850
814
    def test_sprout_recursive_treeless(self):
851
815
        tree = self.make_branch_and_tree('tree1',
852
 
            format='development-subtree')
 
816
            format='dirstate-with-subtree')
853
817
        sub_tree = self.make_branch_and_tree('tree1/subtree',
854
 
            format='development-subtree')
 
818
            format='dirstate-with-subtree')
855
819
        tree.add_reference(sub_tree)
856
820
        self.build_tree(['tree1/subtree/file'])
857
821
        sub_tree.add('file')
858
822
        tree.commit('Initial commit')
859
823
        # The following line force the orhaning to reveal bug #634470
860
 
        tree.branch.get_config_stack().set(
 
824
        tree.branch.get_config().set_user_option(
861
825
            'bzr.transform.orphan_policy', 'move')
862
826
        tree.bzrdir.destroy_workingtree()
863
827
        # FIXME: subtree/.bzr is left here which allows the test to pass (or
864
828
        # fail :-( ) -- vila 20100909
865
829
        repo = self.make_repository('repo', shared=True,
866
 
            format='development-subtree')
 
830
            format='dirstate-with-subtree')
867
831
        repo.set_make_working_trees(False)
868
832
        # FIXME: we just deleted the workingtree and now we want to use it ????
869
833
        # At a minimum, we should use tree.branch below (but this fails too
935
899
        def evaluate(bzrdir):
936
900
            try:
937
901
                repo = bzrdir.open_repository()
938
 
            except errors.NoRepositoryPresent:
 
902
            except NoRepositoryPresent:
939
903
                return True, bzrdir.root_transport.base
940
904
            else:
941
905
                return False, bzrdir.root_transport.base
1021
985
        otherdir = bzrdir.format_registry.make_bzrdir('knit')
1022
986
        self.assertEqual(otherdir, mydir)
1023
987
        self.assertFalse(otherdir != mydir)
1024
 
        otherdir2 = bzrdir.format_registry.make_bzrdir('development-subtree')
 
988
        otherdir2 = bzrdir.format_registry.make_bzrdir('dirstate-with-subtree')
1025
989
        self.assertNotEqual(otherdir2, mydir)
1026
990
        self.assertFalse(otherdir2 == mydir)
1027
991
 
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
 
 
1040
992
    def test_needs_conversion_different_working_tree(self):
1041
993
        # meta1dirs need an conversion if any element is not the default.
1042
994
        new_format = bzrdir.format_registry.make_bzrdir('dirstate')
1260
1212
        self.test_branch = _TestBranch(self.transport)
1261
1213
        self.test_branch.repository = self.create_repository()
1262
1214
 
1263
 
    def open_branch(self, unsupported=False, possible_transports=None):
 
1215
    def open_branch(self, unsupported=False):
1264
1216
        return self.test_branch
1265
1217
 
1266
1218
    def cloning_metadir(self, require_stacking=False):
1298
1250
    def _get_config(self):
1299
1251
        return config.TransportConfig(self._transport, 'branch.conf')
1300
1252
 
1301
 
    def _get_config_store(self):
1302
 
        return config.BranchStore(self)
1303
 
 
1304
1253
    def set_parent(self, parent):
1305
1254
        self._parent = parent
1306
1255
 
1371
1320
        self.assertEqual('fail', err._preformatted_string)
1372
1321
 
1373
1322
    def test_post_repo_init(self):
1374
 
        from bzrlib.controldir import RepoInitHookParams
 
1323
        from bzrlib.bzrdir import RepoInitHookParams
1375
1324
        calls = []
1376
1325
        bzrdir.BzrDir.hooks.install_named_hook('post_repo_init',
1377
1326
            calls.append, None)
1417
1366
        self.assertEqual("a.~2~", self._bzrdir._available_backup_name("a"))
1418
1367
 
1419
1368
 
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
 
        result = converter.convert(tree.bzrdir, bzrdir.BzrDirMetaFormat1())
1451
 
        self.assertIsInstance(result._format, bzrdir.BzrDirMetaFormat1)
1452
 
 
1453
 
    def test_nested(self):
1454
 
        tree = self.make_branch_and_tree('.', format='development-colo')
1455
 
        tree.bzrdir.create_branch(name='foo')
1456
 
        tree.bzrdir.create_branch(name='fool/bla')
1457
 
        self.assertRaises(
1458
 
            errors.ParentBranchExists, tree.bzrdir.create_branch,
1459
 
            name='foo/bar')
1460
 
 
1461
 
    def test_parent(self):
1462
 
        tree = self.make_branch_and_tree('.', format='development-colo')
1463
 
        tree.bzrdir.create_branch(name='fool/bla')
1464
 
        tree.bzrdir.create_branch(name='foo/bar')
1465
 
        self.assertRaises(
1466
 
            errors.AlreadyBranchError, tree.bzrdir.create_branch,
1467
 
            name='foo')
1468
 
 
1469
 
 
1470
 
class SampleBzrFormat(bzrdir.BzrFormat):
1471
 
 
1472
 
    @classmethod
1473
 
    def get_format_string(cls):
1474
 
        return "First line\n"
1475
 
 
1476
 
 
1477
 
class TestBzrFormat(TestCase):
1478
 
    """Tests for BzrFormat."""
1479
 
 
1480
 
    def test_as_string(self):
1481
 
        format = SampleBzrFormat()
1482
 
        format.features = {"foo": "required"}
1483
 
        self.assertEquals(format.as_string(),
1484
 
            "First line\n"
1485
 
            "required foo\n")
1486
 
        format.features["another"] = "optional"
1487
 
        self.assertEquals(format.as_string(),
1488
 
            "First line\n"
1489
 
            "required foo\n"
1490
 
            "optional another\n")
1491
 
 
1492
 
    def test_network_name(self):
1493
 
        # The network string should include the feature info
1494
 
        format = SampleBzrFormat()
1495
 
        format.features = {"foo": "required"}
1496
 
        self.assertEquals(
1497
 
            "First line\nrequired foo\n",
1498
 
            format.network_name())
1499
 
 
1500
 
    def test_from_string_no_features(self):
1501
 
        # No features
1502
 
        format = SampleBzrFormat.from_string(
1503
 
            "First line\n")
1504
 
        self.assertEquals({}, format.features)
1505
 
 
1506
 
    def test_from_string_with_feature(self):
1507
 
        # Proper feature
1508
 
        format = SampleBzrFormat.from_string(
1509
 
            "First line\nrequired foo\n")
1510
 
        self.assertEquals("required", format.features.get("foo"))
1511
 
 
1512
 
    def test_from_string_format_string_mismatch(self):
1513
 
        # The first line has to match the format string
1514
 
        self.assertRaises(AssertionError, SampleBzrFormat.from_string,
1515
 
            "Second line\nrequired foo\n")
1516
 
 
1517
 
    def test_from_string_missing_space(self):
1518
 
        # At least one space is required in the feature lines
1519
 
        self.assertRaises(errors.ParseFormatError, SampleBzrFormat.from_string,
1520
 
            "First line\nfoo\n")
1521
 
 
1522
 
    def test_from_string_with_spaces(self):
1523
 
        # Feature with spaces (in case we add stuff like this in the future)
1524
 
        format = SampleBzrFormat.from_string(
1525
 
            "First line\nrequired foo with spaces\n")
1526
 
        self.assertEquals("required", format.features.get("foo with spaces"))
1527
 
 
1528
 
    def test_eq(self):
1529
 
        format1 = SampleBzrFormat()
1530
 
        format1.features = {"nested-trees": "optional"}
1531
 
        format2 = SampleBzrFormat()
1532
 
        format2.features = {"nested-trees": "optional"}
1533
 
        self.assertEquals(format1, format1)
1534
 
        self.assertEquals(format1, format2)
1535
 
        format3 = SampleBzrFormat()
1536
 
        self.assertNotEquals(format1, format3)
1537
 
 
1538
 
    def test_check_support_status_optional(self):
1539
 
        # Optional, so silently ignore
1540
 
        format = SampleBzrFormat()
1541
 
        format.features = {"nested-trees": "optional"}
1542
 
        format.check_support_status(True)
1543
 
        self.addCleanup(SampleBzrFormat.unregister_feature, "nested-trees")
1544
 
        SampleBzrFormat.register_feature("nested-trees")
1545
 
        format.check_support_status(True)
1546
 
 
1547
 
    def test_check_support_status_required(self):
1548
 
        # Optional, so trigger an exception
1549
 
        format = SampleBzrFormat()
1550
 
        format.features = {"nested-trees": "required"}
1551
 
        self.assertRaises(errors.MissingFeature, format.check_support_status,
1552
 
            True)
1553
 
        self.addCleanup(SampleBzrFormat.unregister_feature, "nested-trees")
1554
 
        SampleBzrFormat.register_feature("nested-trees")
1555
 
        format.check_support_status(True)
1556
 
 
1557
 
    def test_check_support_status_unknown(self):
1558
 
        # treat unknown necessity as required
1559
 
        format = SampleBzrFormat()
1560
 
        format.features = {"nested-trees": "unknown"}
1561
 
        self.assertRaises(errors.MissingFeature, format.check_support_status,
1562
 
            True)
1563
 
        self.addCleanup(SampleBzrFormat.unregister_feature, "nested-trees")
1564
 
        SampleBzrFormat.register_feature("nested-trees")
1565
 
        format.check_support_status(True)
1566
 
 
1567
 
    def test_feature_already_registered(self):
1568
 
        # a feature can only be registered once
1569
 
        self.addCleanup(SampleBzrFormat.unregister_feature, "nested-trees")
1570
 
        SampleBzrFormat.register_feature("nested-trees")
1571
 
        self.assertRaises(errors.FeatureAlreadyRegistered,
1572
 
            SampleBzrFormat.register_feature, "nested-trees")
1573
 
 
1574
 
    def test_feature_with_space(self):
1575
 
        # spaces are not allowed in feature names
1576
 
        self.assertRaises(ValueError, SampleBzrFormat.register_feature,
1577
 
            "nested trees")
 
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")