/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_bzrdir.py

(jameinel) Transform.rename test shouldn't assume the str form of the
 exception because of locale issues. (Martin [gz])

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2006-2011 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
24
24
import sys
25
25
 
26
26
from bzrlib import (
 
27
    branch,
27
28
    bzrdir,
 
29
    controldir,
28
30
    errors,
29
31
    help_topics,
 
32
    lock,
30
33
    repository,
31
34
    osutils,
32
35
    remote,
 
36
    symbol_versioning,
33
37
    urlutils,
34
38
    win32utils,
35
39
    workingtree,
54
58
from bzrlib.transport import (
55
59
    get_transport,
56
60
    memory,
 
61
    pathfilter,
57
62
    )
58
63
from bzrlib.transport.http._urllib import HttpTransport_urllib
59
64
from bzrlib.transport.nosmart import NoSmartTransportDecorator
67
72
        old_format = bzrdir.BzrDirFormat.get_default_format()
68
73
        # default is BzrDirFormat6
69
74
        self.failUnless(isinstance(old_format, bzrdir.BzrDirMetaFormat1))
70
 
        bzrdir.BzrDirFormat._set_default_format(SampleBzrDirFormat())
 
75
        controldir.ControlDirFormat._set_default_format(SampleBzrDirFormat())
71
76
        # creating a bzr dir should now create an instrumented dir.
72
77
        try:
73
78
            result = bzrdir.BzrDir.create('memory:///')
74
79
            self.failUnless(isinstance(result, SampleBzrDir))
75
80
        finally:
76
 
            bzrdir.BzrDirFormat._set_default_format(old_format)
 
81
            controldir.ControlDirFormat._set_default_format(old_format)
77
82
        self.assertEqual(old_format, bzrdir.BzrDirFormat.get_default_format())
78
83
 
79
84
 
80
85
class TestFormatRegistry(TestCase):
81
86
 
82
87
    def make_format_registry(self):
83
 
        my_format_registry = bzrdir.BzrDirFormatRegistry()
 
88
        my_format_registry = controldir.ControlDirFormatRegistry()
84
89
        my_format_registry.register('weave', bzrdir.BzrDirFormat6,
85
90
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
86
91
            ' repositories', deprecated=True)
87
92
        my_format_registry.register_lazy('lazy', 'bzrlib.bzrdir',
88
93
            'BzrDirFormat6', 'Format registered lazily', deprecated=True)
89
 
        my_format_registry.register_metadir('knit',
 
94
        bzrdir.register_metadir(my_format_registry, 'knit',
90
95
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
91
96
            'Format using knits',
92
97
            )
93
98
        my_format_registry.set_default('knit')
94
 
        my_format_registry.register_metadir(
 
99
        bzrdir.register_metadir(my_format_registry,
95
100
            'branch6',
96
101
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
97
102
            'Experimental successor to knit.  Use at your own risk.',
98
103
            branch_format='bzrlib.branch.BzrBranchFormat6',
99
104
            experimental=True)
100
 
        my_format_registry.register_metadir(
 
105
        bzrdir.register_metadir(my_format_registry,
101
106
            'hidden format',
102
107
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
103
108
            'Experimental successor to knit.  Use at your own risk.',
172
177
            bzrdir.format_registry.set_default_repository(old_default)
173
178
 
174
179
    def test_aliases(self):
175
 
        a_registry = bzrdir.BzrDirFormatRegistry()
 
180
        a_registry = controldir.ControlDirFormatRegistry()
176
181
        a_registry.register('weave', bzrdir.BzrDirFormat6,
177
182
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
178
183
            ' repositories', deprecated=True)
787
792
        self.build_tree(['tree1/subtree/file'])
788
793
        sub_tree.add('file')
789
794
        tree.commit('Initial commit')
 
795
        # The following line force the orhaning to reveal bug #634470
 
796
        tree.branch.get_config().set_user_option(
 
797
            'bzr.transform.orphan_policy', 'move')
790
798
        tree.bzrdir.destroy_workingtree()
 
799
        # FIXME: subtree/.bzr is left here which allows the test to pass (or
 
800
        # fail :-( ) -- vila 20100909
791
801
        repo = self.make_repository('repo', shared=True,
792
802
            format='dirstate-with-subtree')
793
803
        repo.set_make_working_trees(False)
794
 
        tree.bzrdir.sprout('repo/tree2')
795
 
        self.failUnlessExists('repo/tree2/subtree')
796
 
        self.failIfExists('repo/tree2/subtree/file')
 
804
        # FIXME: we just deleted the workingtree and now we want to use it ????
 
805
        # At a minimum, we should use tree.branch below (but this fails too
 
806
        # currently) or stop calling this test 'treeless'. Specifically, I've
 
807
        # turn the line below into an assertRaises when 'subtree/.bzr' is
 
808
        # orphaned and sprout tries to access the branch there (which is left
 
809
        # by bzrdir.BzrDirMeta1.destroy_workingtree when it ignores the
 
810
        # [DeletingParent('Not deleting', u'subtree', None)] conflict). See bug
 
811
        # #634470.  -- vila 20100909
 
812
        self.assertRaises(errors.NotBranchError,
 
813
                          tree.bzrdir.sprout, 'repo/tree2')
 
814
#        self.failUnlessExists('repo/tree2/subtree')
 
815
#        self.failIfExists('repo/tree2/subtree/file')
797
816
 
798
817
    def make_foo_bar_baz(self):
799
818
        foo = bzrdir.BzrDir.create_branch_convenience('foo').bzrdir
807
826
        self.assertEqualBzrdirs([baz, foo, bar],
808
827
                                bzrdir.BzrDir.find_bzrdirs(transport))
809
828
 
 
829
    def make_fake_permission_denied_transport(self, transport, paths):
 
830
        """Create a transport that raises PermissionDenied for some paths."""
 
831
        def filter(path):
 
832
            if path in paths:
 
833
                raise errors.PermissionDenied(path)
 
834
            return path
 
835
        path_filter_server = pathfilter.PathFilteringServer(transport, filter)
 
836
        path_filter_server.start_server()
 
837
        self.addCleanup(path_filter_server.stop_server)
 
838
        path_filter_transport = pathfilter.PathFilteringTransport(
 
839
            path_filter_server, '.')
 
840
        return (path_filter_server, path_filter_transport)
 
841
 
 
842
    def assertBranchUrlsEndWith(self, expect_url_suffix, actual_bzrdirs):
 
843
        """Check that each branch url ends with the given suffix."""
 
844
        for actual_bzrdir in actual_bzrdirs:
 
845
            self.assertEndsWith(actual_bzrdir.user_url, expect_url_suffix)
 
846
 
 
847
    def test_find_bzrdirs_permission_denied(self):
 
848
        foo, bar, baz = self.make_foo_bar_baz()
 
849
        transport = get_transport(self.get_url())
 
850
        path_filter_server, path_filter_transport = \
 
851
            self.make_fake_permission_denied_transport(transport, ['foo'])
 
852
        # local transport
 
853
        self.assertBranchUrlsEndWith('/baz/',
 
854
            bzrdir.BzrDir.find_bzrdirs(path_filter_transport))
 
855
        # smart server
 
856
        smart_transport = self.make_smart_server('.',
 
857
            backing_server=path_filter_server)
 
858
        self.assertBranchUrlsEndWith('/baz/',
 
859
            bzrdir.BzrDir.find_bzrdirs(smart_transport))
 
860
 
810
861
    def test_find_bzrdirs_list_current(self):
811
862
        def list_current(transport):
812
863
            return [s for s in transport.list_dir('') if s != 'baz']
817
868
                                bzrdir.BzrDir.find_bzrdirs(transport,
818
869
                                    list_current=list_current))
819
870
 
820
 
 
821
871
    def test_find_bzrdirs_evaluate(self):
822
872
        def evaluate(bzrdir):
823
873
            try:
856
906
        self.assertEqual(bar.root_transport.base, branches[1].base)
857
907
 
858
908
 
 
909
class TestMissingRepoBranchesSkipped(TestCaseWithMemoryTransport):
 
910
 
 
911
    def test_find_bzrdirs_missing_repo(self):
 
912
        transport = get_transport(self.get_url())
 
913
        arepo = self.make_repository('arepo', shared=True)
 
914
        abranch_url = arepo.user_url + '/abranch'
 
915
        abranch = bzrdir.BzrDir.create(abranch_url).create_branch()
 
916
        transport.delete_tree('arepo/.bzr')
 
917
        self.assertRaises(errors.NoRepositoryPresent,
 
918
            branch.Branch.open, abranch_url)
 
919
        self.make_branch('baz')
 
920
        for actual_bzrdir in bzrdir.BzrDir.find_branches(transport):
 
921
            self.assertEndsWith(actual_bzrdir.user_url, '/baz/')
 
922
 
 
923
 
859
924
class TestMeta1DirFormat(TestCaseWithTransport):
860
925
    """Tests specific to the meta1 dir format."""
861
926
 
1010
1075
    def _known_formats(self):
1011
1076
        return set([NotBzrDirFormat()])
1012
1077
 
1013
 
    @classmethod
 
1078
 
 
1079
class NotBzrDirProber(controldir.Prober):
 
1080
 
1014
1081
    def probe_transport(self, transport):
1015
1082
        """Our format is present if the transport ends in '.not/'."""
1016
1083
        if transport.has('.not'):
1030
1097
        dir = format.initialize(self.get_url())
1031
1098
        self.assertIsInstance(dir, NotBzrDir)
1032
1099
        # now probe for it.
1033
 
        bzrlib.bzrdir.BzrDirFormat.register_control_format(format)
 
1100
        controldir.ControlDirFormat.register_prober(NotBzrDirProber)
1034
1101
        try:
1035
1102
            found = bzrlib.bzrdir.BzrDirFormat.find_format(
1036
1103
                get_transport(self.get_url()))
1037
1104
            self.assertIsInstance(found, NotBzrDirFormat)
1038
1105
        finally:
1039
 
            bzrlib.bzrdir.BzrDirFormat.unregister_control_format(format)
 
1106
            controldir.ControlDirFormat.unregister_prober(NotBzrDirProber)
1040
1107
 
1041
1108
    def test_included_in_known_formats(self):
1042
 
        bzrlib.bzrdir.BzrDirFormat.register_control_format(NotBzrDirFormat)
 
1109
        not_format = NotBzrDirFormat()
 
1110
        bzrlib.controldir.ControlDirFormat.register_format(not_format)
1043
1111
        try:
1044
1112
            formats = bzrlib.bzrdir.BzrDirFormat.known_formats()
1045
1113
            for format in formats:
1047
1115
                    return
1048
1116
            self.fail("No NotBzrDirFormat in %s" % formats)
1049
1117
        finally:
1050
 
            bzrlib.bzrdir.BzrDirFormat.unregister_control_format(NotBzrDirFormat)
 
1118
            bzrlib.controldir.ControlDirFormat.unregister_format(not_format)
1051
1119
 
1052
1120
 
1053
1121
class NonLocalTests(TestCaseWithTransport):
1111
1179
    """
1112
1180
 
1113
1181
    def create_transport_readonly_server(self):
 
1182
        # We don't set the http protocol version, relying on the default
1114
1183
        return http_utils.HTTPServerRedirecting()
1115
1184
 
1116
1185
    def create_transport_secondary_server(self):
 
1186
        # We don't set the http protocol version, relying on the default
1117
1187
        return http_utils.HTTPServerRedirecting()
1118
1188
 
1119
1189
    def setUp(self):
1280
1350
    def set_parent(self, parent):
1281
1351
        self._parent = parent
1282
1352
 
 
1353
    def lock_read(self):
 
1354
        return lock.LogicalLockResult(self.unlock)
 
1355
 
 
1356
    def unlock(self):
 
1357
        return
 
1358
 
1283
1359
 
1284
1360
class TestBzrDirSprout(TestCaseWithMemoryTransport):
1285
1361
 
1351
1427
        self.assertIsInstance(params, RepoInitHookParams)
1352
1428
        self.assertTrue(hasattr(params, 'bzrdir'))
1353
1429
        self.assertTrue(hasattr(params, 'repository'))
 
1430
 
 
1431
    def test_post_repo_init_hook_repr(self):
 
1432
        param_reprs = []
 
1433
        bzrdir.BzrDir.hooks.install_named_hook('post_repo_init',
 
1434
            lambda params: param_reprs.append(repr(params)), None)
 
1435
        self.make_repository('foo')
 
1436
        self.assertLength(1, param_reprs)
 
1437
        param_repr = param_reprs[0]
 
1438
        self.assertStartsWith(param_repr, '<RepoInitHookParams for ')
 
1439
 
 
1440
 
 
1441
class TestGenerateBackupName(TestCaseWithMemoryTransport):
 
1442
    # FIXME: This may need to be unified with test_osutils.TestBackupNames or
 
1443
    # moved to per_bzrdir or per_transport for better coverage ?
 
1444
    # -- vila 20100909
 
1445
 
 
1446
    def setUp(self):
 
1447
        super(TestGenerateBackupName, self).setUp()
 
1448
        self._transport = get_transport(self.get_url())
 
1449
        bzrdir.BzrDir.create(self.get_url(),
 
1450
            possible_transports=[self._transport])
 
1451
        self._bzrdir = bzrdir.BzrDir.open_from_transport(self._transport)
 
1452
 
 
1453
    def test_deprecated_generate_backup_name(self):
 
1454
        res = self.applyDeprecated(
 
1455
                symbol_versioning.deprecated_in((2, 3, 0)),
 
1456
                self._bzrdir.generate_backup_name, 'whatever')
 
1457
 
 
1458
    def test_new(self):
 
1459
        self.assertEqual("a.~1~", self._bzrdir._available_backup_name("a"))
 
1460
 
 
1461
    def test_exiting(self):
 
1462
        self._transport.put_bytes("a.~1~", "some content")
 
1463
        self.assertEqual("a.~2~", self._bzrdir._available_backup_name("a"))