/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_branch.py

  • Committer: Jelmer Vernooij
  • Date: 2011-03-10 13:52:27 UTC
  • mto: (5712.4.5 bzrdir-weave)
  • mto: This revision was merged to the branch mainline in revision 5716.
  • Revision ID: jelmer@samba.org-20110310135227-ufcw0utlwsp8r6k6
Revert registry changes.

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
29
29
    bzrdir,
30
30
    config,
31
31
    errors,
 
32
    symbol_versioning,
32
33
    tests,
33
34
    trace,
34
 
    transport,
35
35
    urlutils,
36
36
    )
37
37
 
 
38
from bzrlib.branch_weave import (
 
39
    BzrBranchFormat4,
 
40
    )
 
41
 
38
42
 
39
43
class TestDefaultFormat(tests.TestCase):
40
44
 
41
45
    def test_default_format(self):
42
46
        # update this if you change the default branch format
43
 
        self.assertIsInstance(_mod_branch.BranchFormat.get_default_format(),
 
47
        self.assertIsInstance(_mod_branch.format_registry.get_default(),
44
48
                _mod_branch.BzrBranchFormat7)
45
49
 
46
50
    def test_default_format_is_same_as_bzrdir_default(self):
48
52
        # set, but at the moment that's not true -- mbp 20070814 --
49
53
        # https://bugs.launchpad.net/bzr/+bug/132376
50
54
        self.assertEqual(
51
 
            _mod_branch.BranchFormat.get_default_format(),
 
55
            _mod_branch.format_registry.get_default(),
52
56
            bzrdir.BzrDirFormat.get_default_format().get_branch_format())
53
57
 
54
58
    def test_get_set_default_format(self):
55
59
        # set the format and then set it back again
56
 
        old_format = _mod_branch.BranchFormat.get_default_format()
57
 
        _mod_branch.BranchFormat.set_default_format(SampleBranchFormat())
 
60
        old_format = _mod_branch.format_registry.get_default()
 
61
        _mod_branch.format_registry.set_default(SampleBranchFormat())
58
62
        try:
59
63
            # the default branch format is used by the meta dir format
60
64
            # which is not the default bzrdir format at this point
62
66
            result = dir.create_branch()
63
67
            self.assertEqual(result, 'A branch')
64
68
        finally:
65
 
            _mod_branch.BranchFormat.set_default_format(old_format)
 
69
            _mod_branch.format_registry.set_default(old_format)
66
70
        self.assertEqual(old_format,
67
 
                         _mod_branch.BranchFormat.get_default_format())
 
71
                         _mod_branch.format_registry.get_default())
 
72
 
 
73
 
 
74
class TestBranchFormat4(tests.TestCaseWithTransport):
 
75
    """Tests specific to branch format 4"""
 
76
 
 
77
    def test_no_metadir_support(self):
 
78
        url = self.get_url()
 
79
        bdir = bzrdir.BzrDirMetaFormat1().initialize(url)
 
80
        bdir.create_repository()
 
81
        self.assertRaises(errors.IncompatibleFormat,
 
82
            BzrBranchFormat4().initialize, bdir)
 
83
 
 
84
    def test_supports_bzrdir_6(self):
 
85
        url = self.get_url()
 
86
        bdir = bzrdir.BzrDirFormat6().initialize(url)
 
87
        bdir.create_repository()
 
88
        BzrBranchFormat4().initialize(bdir)
68
89
 
69
90
 
70
91
class TestBranchFormat5(tests.TestCaseWithTransport):
86
107
        self.assertIsDirectory('.bzr/branch/lock/held', t)
87
108
 
88
109
    def test_set_push_location(self):
89
 
        from bzrlib.config import (locations_config_filename,
90
 
                                   ensure_config_dir_exists)
91
 
        ensure_config_dir_exists()
92
 
        fn = locations_config_filename()
93
 
        # write correct newlines to locations.conf
94
 
        # by default ConfigObj uses native line-endings for new files
95
 
        # but uses already existing line-endings if file is not empty
96
 
        f = open(fn, 'wb')
97
 
        try:
98
 
            f.write('# comment\n')
99
 
        finally:
100
 
            f.close()
 
110
        conf = config.LocationConfig.from_string('# comment\n', '.', save=True)
101
111
 
102
112
        branch = self.make_branch('.', format='knit')
103
113
        branch.set_push_location('foo')
106
116
                             "[%s]\n"
107
117
                             "push_location = foo\n"
108
118
                             "push_location:policy = norecurse\n" % local_path,
109
 
                             fn)
 
119
                             config.locations_config_filename())
110
120
 
111
121
    # TODO RBC 20051029 test getting a push location from a branch in a
112
122
    # recursive section - that is, it appends the branch name.
123
133
        """See BzrBranchFormat.get_format_string()."""
124
134
        return "Sample branch format."
125
135
 
126
 
    def initialize(self, a_bzrdir, name=None):
 
136
    def initialize(self, a_bzrdir, name=None, repository=None):
127
137
        """Format 4 branches cannot be created."""
128
138
        t = a_bzrdir.get_branch_transport(self, name=name)
129
139
        t.put_bytes('format', self.get_format_string())
136
146
        return "opened branch."
137
147
 
138
148
 
 
149
# Demonstrating how lazy loading is often implemented:
 
150
# A constant string is created.
 
151
SampleSupportedBranchFormatString = "Sample supported branch format."
 
152
 
 
153
# And the format class can then reference the constant to avoid skew.
 
154
class SampleSupportedBranchFormat(_mod_branch.BranchFormat):
 
155
    """A sample supported format."""
 
156
 
 
157
    def get_format_string(self):
 
158
        """See BzrBranchFormat.get_format_string()."""
 
159
        return SampleSupportedBranchFormatString
 
160
 
 
161
    def initialize(self, a_bzrdir, name=None):
 
162
        t = a_bzrdir.get_branch_transport(self, name=name)
 
163
        t.put_bytes('format', self.get_format_string())
 
164
        return 'A branch'
 
165
 
 
166
    def open(self, transport, name=None, _found=False, ignore_fallbacks=False):
 
167
        return "opened supported branch."
 
168
 
 
169
 
 
170
class SampleExtraBranchFormat(_mod_branch.BranchFormat):
 
171
    """A sample format that is not usable in a metadir."""
 
172
 
 
173
    def get_format_string(self):
 
174
        # This format is not usable in a metadir.
 
175
        return None
 
176
 
 
177
    def network_name(self):
 
178
        # Network name always has to be provided.
 
179
        return "extra"
 
180
 
 
181
    def initialize(self, a_bzrdir, name=None):
 
182
        raise NotImplementedError(self.initialize)
 
183
 
 
184
    def open(self, transport, name=None, _found=False, ignore_fallbacks=False):
 
185
        raise NotImplementedError(self.open)
 
186
 
 
187
 
139
188
class TestBzrBranchFormat(tests.TestCaseWithTransport):
140
189
    """Tests for the BzrBranchFormat facility."""
141
190
 
152
201
            self.failUnless(isinstance(found_format, format.__class__))
153
202
        check_format(_mod_branch.BzrBranchFormat5(), "bar")
154
203
 
 
204
    def test_find_format_factory(self):
 
205
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
 
206
        SampleSupportedBranchFormat().initialize(dir)
 
207
        factory = _mod_branch.MetaDirBranchFormatFactory(
 
208
            SampleSupportedBranchFormatString,
 
209
            "bzrlib.tests.test_branch", "SampleSupportedBranchFormat")
 
210
        _mod_branch.format_registry.register(factory)
 
211
        self.addCleanup(_mod_branch.format_registry.remove, factory)
 
212
        b = _mod_branch.Branch.open(self.get_url())
 
213
        self.assertEqual(b, "opened supported branch.")
 
214
 
155
215
    def test_find_format_not_branch(self):
156
216
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
157
217
        self.assertRaises(errors.NotBranchError,
166
226
                          dir)
167
227
 
168
228
    def test_register_unregister_format(self):
 
229
        # Test the deprecated format registration functions
169
230
        format = SampleBranchFormat()
170
231
        # make a control dir
171
232
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
172
233
        # make a branch
173
234
        format.initialize(dir)
174
235
        # register a format for it.
175
 
        _mod_branch.BranchFormat.register_format(format)
 
236
        self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
 
237
            _mod_branch.BranchFormat.register_format, format)
176
238
        # which branch.Open will refuse (not supported)
177
239
        self.assertRaises(errors.UnsupportedFormatError,
178
240
                          _mod_branch.Branch.open, self.get_url())
182
244
            format.open(dir),
183
245
            bzrdir.BzrDir.open(self.get_url()).open_branch(unsupported=True))
184
246
        # unregister the format
185
 
        _mod_branch.BranchFormat.unregister_format(format)
 
247
        self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
 
248
            _mod_branch.BranchFormat.unregister_format, format)
186
249
        self.make_branch_and_tree('bar')
187
250
 
188
251
 
 
252
class TestBranchFormatRegistry(tests.TestCase):
 
253
 
 
254
    def setUp(self):
 
255
        super(TestBranchFormatRegistry, self).setUp()
 
256
        self.registry = _mod_branch.BranchFormatRegistry()
 
257
 
 
258
    def test_default(self):
 
259
        self.assertIs(None, self.registry.get_default())
 
260
        format = SampleBranchFormat()
 
261
        self.registry.set_default(format)
 
262
        self.assertEquals(format, self.registry.get_default())
 
263
 
 
264
    def test_register_unregister_format(self):
 
265
        format = SampleBranchFormat()
 
266
        self.registry.register(format)
 
267
        self.assertEquals(format,
 
268
            self.registry.get("Sample branch format."))
 
269
        self.registry.remove(format)
 
270
        self.assertRaises(KeyError, self.registry.get,
 
271
            "Sample branch format.")
 
272
 
 
273
    def test_get_all(self):
 
274
        format = SampleBranchFormat()
 
275
        self.assertEquals([], self.registry._get_all())
 
276
        self.registry.register(format)
 
277
        self.assertEquals([format], self.registry._get_all())
 
278
 
 
279
    def test_register_extra(self):
 
280
        format = SampleExtraBranchFormat()
 
281
        self.assertEquals([], self.registry._get_all())
 
282
        self.registry.register_extra(format)
 
283
        self.assertEquals([format], self.registry._get_all())
 
284
 
 
285
    def test_register_extra_lazy(self):
 
286
        self.assertEquals([], self.registry._get_all())
 
287
        self.registry.register_extra_lazy("bzrlib.tests.test_branch",
 
288
            "SampleExtraBranchFormat")
 
289
        formats = self.registry._get_all()
 
290
        self.assertEquals(1, len(formats))
 
291
        self.assertIsInstance(formats[0], SampleExtraBranchFormat)
 
292
 
 
293
 
 
294
#Used by TestMetaDirBranchFormatFactory 
 
295
FakeLazyFormat = None
 
296
 
 
297
 
 
298
class TestMetaDirBranchFormatFactory(tests.TestCase):
 
299
 
 
300
    def test_get_format_string_does_not_load(self):
 
301
        """Formats have a static format string."""
 
302
        factory = _mod_branch.MetaDirBranchFormatFactory("yo", None, None)
 
303
        self.assertEqual("yo", factory.get_format_string())
 
304
 
 
305
    def test_call_loads(self):
 
306
        # __call__ is used by the network_format_registry interface to get a
 
307
        # Format.
 
308
        global FakeLazyFormat
 
309
        del FakeLazyFormat
 
310
        factory = _mod_branch.MetaDirBranchFormatFactory(None,
 
311
            "bzrlib.tests.test_branch", "FakeLazyFormat")
 
312
        self.assertRaises(AttributeError, factory)
 
313
 
 
314
    def test_call_returns_call_of_referenced_object(self):
 
315
        global FakeLazyFormat
 
316
        FakeLazyFormat = lambda:'called'
 
317
        factory = _mod_branch.MetaDirBranchFormatFactory(None,
 
318
            "bzrlib.tests.test_branch", "FakeLazyFormat")
 
319
        self.assertEqual('called', factory())
 
320
 
 
321
 
189
322
class TestBranch67(object):
190
323
    """Common tests for both branch 6 and 7 which are mostly the same."""
191
324
 
217
350
    def test_config(self):
218
351
        """Ensure that all configuration data is stored in the branch"""
219
352
        branch = self.make_branch('a', format=self.get_format_name())
220
 
        branch.set_parent('http://bazaar-vcs.org')
 
353
        branch.set_parent('http://example.com')
221
354
        self.failIfExists('a/.bzr/branch/parent')
222
 
        self.assertEqual('http://bazaar-vcs.org', branch.get_parent())
223
 
        branch.set_push_location('sftp://bazaar-vcs.org')
 
355
        self.assertEqual('http://example.com', branch.get_parent())
 
356
        branch.set_push_location('sftp://example.com')
224
357
        config = branch.get_config()._get_branch_data_config()
225
 
        self.assertEqual('sftp://bazaar-vcs.org',
 
358
        self.assertEqual('sftp://example.com',
226
359
                         config.get_user_option('push_location'))
227
 
        branch.set_bound_location('ftp://bazaar-vcs.org')
 
360
        branch.set_bound_location('ftp://example.com')
228
361
        self.failIfExists('a/.bzr/branch/bound')
229
 
        self.assertEqual('ftp://bazaar-vcs.org', branch.get_bound_location())
 
362
        self.assertEqual('ftp://example.com', branch.get_bound_location())
230
363
 
231
364
    def test_set_revision_history(self):
232
365
        builder = self.make_branch_builder('.', format=self.get_format_name())
430
563
 
431
564
    def test_create_open_reference(self):
432
565
        bzrdirformat = bzrdir.BzrDirMetaFormat1()
433
 
        t = transport.get_transport(self.get_url('.'))
 
566
        t = self.get_transport()
434
567
        t.mkdir('repo')
435
568
        dir = bzrdirformat.initialize(self.get_url('repo'))
436
569
        dir.create_repository()
492
625
        self.assertTrue(hasattr(params, 'bzrdir'))
493
626
        self.assertTrue(hasattr(params, 'branch'))
494
627
 
 
628
    def test_post_branch_init_hook_repr(self):
 
629
        param_reprs = []
 
630
        _mod_branch.Branch.hooks.install_named_hook('post_branch_init',
 
631
            lambda params: param_reprs.append(repr(params)), None)
 
632
        branch = self.make_branch('a')
 
633
        self.assertLength(1, param_reprs)
 
634
        param_repr = param_reprs[0]
 
635
        self.assertStartsWith(param_repr, '<BranchInitHookParams of ')
 
636
 
495
637
    def test_post_switch_hook(self):
496
638
        from bzrlib import switch
497
639
        calls = []
564
706
        # this usage of results is not recommended for new code (because it
565
707
        # doesn't describe very well what happened), but for api stability
566
708
        # it's still supported
567
 
        a = "%d revisions pulled" % r
568
 
        self.assertEqual(a, "10 revisions pulled")
 
709
        self.assertEqual(self.applyDeprecated(
 
710
            symbol_versioning.deprecated_in((2, 3, 0)),
 
711
            r.__int__),
 
712
            10)
569
713
 
570
714
    def test_report_changed(self):
571
715
        r = _mod_branch.PullResult()
652
796
                          _mod_branch._run_with_write_locked_target,
653
797
                          lockable, self.func_that_raises)
654
798
        self.assertEqual(['lock_write', 'func called', 'unlock'], self._calls)
655
 
 
656