~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_branch.py

  • Committer: Jonathan Riddell
  • Date: 2011-05-16 10:05:25 UTC
  • mto: This revision was merged to the branch mainline in revision 5869.
  • Revision ID: jriddell@canonical.com-20110516100525-7q23m5opdnl4qg41
start adding licences

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
 
40
40
 
41
41
    def test_default_format(self):
42
42
        # update this if you change the default branch format
43
 
        self.assertIsInstance(_mod_branch.BranchFormat.get_default_format(),
 
43
        self.assertIsInstance(_mod_branch.format_registry.get_default(),
44
44
                _mod_branch.BzrBranchFormat7)
45
45
 
46
46
    def test_default_format_is_same_as_bzrdir_default(self):
48
48
        # set, but at the moment that's not true -- mbp 20070814 --
49
49
        # https://bugs.launchpad.net/bzr/+bug/132376
50
50
        self.assertEqual(
51
 
            _mod_branch.BranchFormat.get_default_format(),
 
51
            _mod_branch.format_registry.get_default(),
52
52
            bzrdir.BzrDirFormat.get_default_format().get_branch_format())
53
53
 
54
54
    def test_get_set_default_format(self):
55
55
        # 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())
 
56
        old_format = _mod_branch.format_registry.get_default()
 
57
        _mod_branch.format_registry.set_default(SampleBranchFormat())
58
58
        try:
59
59
            # the default branch format is used by the meta dir format
60
60
            # which is not the default bzrdir format at this point
62
62
            result = dir.create_branch()
63
63
            self.assertEqual(result, 'A branch')
64
64
        finally:
65
 
            _mod_branch.BranchFormat.set_default_format(old_format)
 
65
            _mod_branch.format_registry.set_default(old_format)
66
66
        self.assertEqual(old_format,
67
 
                         _mod_branch.BranchFormat.get_default_format())
 
67
                         _mod_branch.format_registry.get_default())
68
68
 
69
69
 
70
70
class TestBranchFormat5(tests.TestCaseWithTransport):
74
74
        url = self.get_url()
75
75
        bdir = bzrdir.BzrDirMetaFormat1().initialize(url)
76
76
        bdir.create_repository()
77
 
        branch = bdir.create_branch()
 
77
        branch = _mod_branch.BzrBranchFormat5().initialize(bdir)
78
78
        t = self.get_transport()
79
79
        self.log("branch instance is %r" % branch)
80
80
        self.assert_(isinstance(branch, _mod_branch.BzrBranch5))
86
86
        self.assertIsDirectory('.bzr/branch/lock/held', t)
87
87
 
88
88
    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()
 
89
        conf = config.LocationConfig.from_string('# comment\n', '.', save=True)
101
90
 
102
91
        branch = self.make_branch('.', format='knit')
103
92
        branch.set_push_location('foo')
106
95
                             "[%s]\n"
107
96
                             "push_location = foo\n"
108
97
                             "push_location:policy = norecurse\n" % local_path,
109
 
                             fn)
 
98
                             config.locations_config_filename())
110
99
 
111
100
    # TODO RBC 20051029 test getting a push location from a branch in a
112
101
    # recursive section - that is, it appends the branch name.
123
112
        """See BzrBranchFormat.get_format_string()."""
124
113
        return "Sample branch format."
125
114
 
126
 
    def initialize(self, a_bzrdir, name=None):
 
115
    def initialize(self, a_bzrdir, name=None, repository=None):
127
116
        """Format 4 branches cannot be created."""
128
117
        t = a_bzrdir.get_branch_transport(self, name=name)
129
118
        t.put_bytes('format', self.get_format_string())
157
146
        return "opened supported branch."
158
147
 
159
148
 
 
149
class SampleExtraBranchFormat(_mod_branch.BranchFormat):
 
150
    """A sample format that is not usable in a metadir."""
 
151
 
 
152
    def get_format_string(self):
 
153
        # This format is not usable in a metadir.
 
154
        return None
 
155
 
 
156
    def network_name(self):
 
157
        # Network name always has to be provided.
 
158
        return "extra"
 
159
 
 
160
    def initialize(self, a_bzrdir, name=None):
 
161
        raise NotImplementedError(self.initialize)
 
162
 
 
163
    def open(self, transport, name=None, _found=False, ignore_fallbacks=False):
 
164
        raise NotImplementedError(self.open)
 
165
 
 
166
 
160
167
class TestBzrBranchFormat(tests.TestCaseWithTransport):
161
168
    """Tests for the BzrBranchFormat facility."""
162
169
 
170
177
            dir.create_repository()
171
178
            format.initialize(dir)
172
179
            found_format = _mod_branch.BranchFormat.find_format(dir)
173
 
            self.failUnless(isinstance(found_format, format.__class__))
 
180
            self.assertIsInstance(found_format, format.__class__)
174
181
        check_format(_mod_branch.BzrBranchFormat5(), "bar")
175
182
 
176
183
    def test_find_format_factory(self):
179
186
        factory = _mod_branch.MetaDirBranchFormatFactory(
180
187
            SampleSupportedBranchFormatString,
181
188
            "bzrlib.tests.test_branch", "SampleSupportedBranchFormat")
182
 
        _mod_branch.BranchFormat.register_format(factory)
183
 
        self.addCleanup(_mod_branch.BranchFormat.unregister_format, factory)
 
189
        _mod_branch.format_registry.register(factory)
 
190
        self.addCleanup(_mod_branch.format_registry.remove, factory)
184
191
        b = _mod_branch.Branch.open(self.get_url())
185
192
        self.assertEqual(b, "opened supported branch.")
186
193
 
198
205
                          dir)
199
206
 
200
207
    def test_register_unregister_format(self):
 
208
        # Test the deprecated format registration functions
201
209
        format = SampleBranchFormat()
202
210
        # make a control dir
203
211
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
204
212
        # make a branch
205
213
        format.initialize(dir)
206
214
        # register a format for it.
207
 
        _mod_branch.BranchFormat.register_format(format)
 
215
        self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
 
216
            _mod_branch.BranchFormat.register_format, format)
208
217
        # which branch.Open will refuse (not supported)
209
218
        self.assertRaises(errors.UnsupportedFormatError,
210
219
                          _mod_branch.Branch.open, self.get_url())
214
223
            format.open(dir),
215
224
            bzrdir.BzrDir.open(self.get_url()).open_branch(unsupported=True))
216
225
        # unregister the format
217
 
        _mod_branch.BranchFormat.unregister_format(format)
 
226
        self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
 
227
            _mod_branch.BranchFormat.unregister_format, format)
218
228
        self.make_branch_and_tree('bar')
219
229
 
220
230
 
 
231
class TestBranchFormatRegistry(tests.TestCase):
 
232
 
 
233
    def setUp(self):
 
234
        super(TestBranchFormatRegistry, self).setUp()
 
235
        self.registry = _mod_branch.BranchFormatRegistry()
 
236
 
 
237
    def test_default(self):
 
238
        self.assertIs(None, self.registry.get_default())
 
239
        format = SampleBranchFormat()
 
240
        self.registry.set_default(format)
 
241
        self.assertEquals(format, self.registry.get_default())
 
242
 
 
243
    def test_register_unregister_format(self):
 
244
        format = SampleBranchFormat()
 
245
        self.registry.register(format)
 
246
        self.assertEquals(format,
 
247
            self.registry.get("Sample branch format."))
 
248
        self.registry.remove(format)
 
249
        self.assertRaises(KeyError, self.registry.get,
 
250
            "Sample branch format.")
 
251
 
 
252
    def test_get_all(self):
 
253
        format = SampleBranchFormat()
 
254
        self.assertEquals([], self.registry._get_all())
 
255
        self.registry.register(format)
 
256
        self.assertEquals([format], self.registry._get_all())
 
257
 
 
258
    def test_register_extra(self):
 
259
        format = SampleExtraBranchFormat()
 
260
        self.assertEquals([], self.registry._get_all())
 
261
        self.registry.register_extra(format)
 
262
        self.assertEquals([format], self.registry._get_all())
 
263
 
 
264
    def test_register_extra_lazy(self):
 
265
        self.assertEquals([], self.registry._get_all())
 
266
        self.registry.register_extra_lazy("bzrlib.tests.test_branch",
 
267
            "SampleExtraBranchFormat")
 
268
        formats = self.registry._get_all()
 
269
        self.assertEquals(1, len(formats))
 
270
        self.assertIsInstance(formats[0], SampleExtraBranchFormat)
 
271
 
 
272
 
221
273
#Used by TestMetaDirBranchFormatFactory 
222
274
FakeLazyFormat = None
223
275
 
270
322
 
271
323
    def test_layout(self):
272
324
        branch = self.make_branch('a', format=self.get_format_name())
273
 
        self.failUnlessExists('a/.bzr/branch/last-revision')
274
 
        self.failIfExists('a/.bzr/branch/revision-history')
275
 
        self.failIfExists('a/.bzr/branch/references')
 
325
        self.assertPathExists('a/.bzr/branch/last-revision')
 
326
        self.assertPathDoesNotExist('a/.bzr/branch/revision-history')
 
327
        self.assertPathDoesNotExist('a/.bzr/branch/references')
276
328
 
277
329
    def test_config(self):
278
330
        """Ensure that all configuration data is stored in the branch"""
279
331
        branch = self.make_branch('a', format=self.get_format_name())
280
 
        branch.set_parent('http://bazaar-vcs.org')
281
 
        self.failIfExists('a/.bzr/branch/parent')
282
 
        self.assertEqual('http://bazaar-vcs.org', branch.get_parent())
283
 
        branch.set_push_location('sftp://bazaar-vcs.org')
 
332
        branch.set_parent('http://example.com')
 
333
        self.assertPathDoesNotExist('a/.bzr/branch/parent')
 
334
        self.assertEqual('http://example.com', branch.get_parent())
 
335
        branch.set_push_location('sftp://example.com')
284
336
        config = branch.get_config()._get_branch_data_config()
285
 
        self.assertEqual('sftp://bazaar-vcs.org',
 
337
        self.assertEqual('sftp://example.com',
286
338
                         config.get_user_option('push_location'))
287
 
        branch.set_bound_location('ftp://bazaar-vcs.org')
288
 
        self.failIfExists('a/.bzr/branch/bound')
289
 
        self.assertEqual('ftp://bazaar-vcs.org', branch.get_bound_location())
 
339
        branch.set_bound_location('ftp://example.com')
 
340
        self.assertPathDoesNotExist('a/.bzr/branch/bound')
 
341
        self.assertEqual('ftp://example.com', branch.get_bound_location())
290
342
 
291
343
    def test_set_revision_history(self):
292
344
        builder = self.make_branch_builder('.', format=self.get_format_name())
297
349
        branch = builder.get_branch()
298
350
        branch.lock_write()
299
351
        self.addCleanup(branch.unlock)
300
 
        branch.set_revision_history(['foo', 'bar'])
301
 
        branch.set_revision_history(['foo'])
 
352
        self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
 
353
            branch.set_revision_history, ['foo', 'bar'])
 
354
        self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
 
355
                branch.set_revision_history, ['foo'])
302
356
        self.assertRaises(errors.NotLefthandHistory,
303
 
                          branch.set_revision_history, ['bar'])
 
357
            self.applyDeprecated, symbol_versioning.deprecated_in((2, 4, 0)),
 
358
            branch.set_revision_history, ['bar'])
304
359
 
305
360
    def do_checkout_test(self, lightweight=False):
306
361
        tree = self.make_branch_and_tree('source',
319
374
        subtree.commit('a subtree file')
320
375
        subsubtree.commit('a subsubtree file')
321
376
        tree.branch.create_checkout('target', lightweight=lightweight)
322
 
        self.failUnlessExists('target')
323
 
        self.failUnlessExists('target/subtree')
324
 
        self.failUnlessExists('target/subtree/file')
325
 
        self.failUnlessExists('target/subtree/subsubtree/file')
 
377
        self.assertPathExists('target')
 
378
        self.assertPathExists('target/subtree')
 
379
        self.assertPathExists('target/subtree/file')
 
380
        self.assertPathExists('target/subtree/subsubtree/file')
326
381
        subbranch = _mod_branch.Branch.open('target/subtree/subsubtree')
327
382
        if lightweight:
328
383
            self.assertEndsWith(subbranch.base, 'source/subtree/subsubtree/')
490
545
 
491
546
    def test_create_open_reference(self):
492
547
        bzrdirformat = bzrdir.BzrDirMetaFormat1()
493
 
        t = transport.get_transport(self.get_url('.'))
 
548
        t = self.get_transport()
494
549
        t.mkdir('repo')
495
550
        dir = bzrdirformat.initialize(self.get_url('repo'))
496
551
        dir.create_repository()
552
607
        self.assertTrue(hasattr(params, 'bzrdir'))
553
608
        self.assertTrue(hasattr(params, 'branch'))
554
609
 
 
610
    def test_post_branch_init_hook_repr(self):
 
611
        param_reprs = []
 
612
        _mod_branch.Branch.hooks.install_named_hook('post_branch_init',
 
613
            lambda params: param_reprs.append(repr(params)), None)
 
614
        branch = self.make_branch('a')
 
615
        self.assertLength(1, param_reprs)
 
616
        param_repr = param_reprs[0]
 
617
        self.assertStartsWith(param_repr, '<BranchInitHookParams of ')
 
618
 
555
619
    def test_post_switch_hook(self):
556
620
        from bzrlib import switch
557
621
        calls = []
624
688
        # this usage of results is not recommended for new code (because it
625
689
        # doesn't describe very well what happened), but for api stability
626
690
        # it's still supported
627
 
        a = "%d revisions pulled" % r
628
 
        self.assertEqual(a, "10 revisions pulled")
 
691
        self.assertEqual(self.applyDeprecated(
 
692
            symbol_versioning.deprecated_in((2, 3, 0)),
 
693
            r.__int__),
 
694
            10)
629
695
 
630
696
    def test_report_changed(self):
631
697
        r = _mod_branch.PullResult()
712
778
                          _mod_branch._run_with_write_locked_target,
713
779
                          lockable, self.func_that_raises)
714
780
        self.assertEqual(['lock_write', 'func called', 'unlock'], self._calls)
715
 
 
716