/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: Richard Wilbur
  • Date: 2016-02-04 19:07:28 UTC
  • mto: This revision was merged to the branch mainline in revision 6618.
  • Revision ID: richard.wilbur@gmail.com-20160204190728-p0zvfii6zase0fw7
Update COPYING.txt from the original http://www.gnu.org/licenses/gpl-2.0.txt  (Only differences were in whitespace.)  Thanks to Petr Stodulka for pointing out the discrepancy.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2012, 2016 Canonical Ltd
 
1
# Copyright (C) 2006-2012 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
22
22
also see this file.
23
23
"""
24
24
 
25
 
from .. import (
 
25
from cStringIO import StringIO
 
26
 
 
27
from bzrlib import (
26
28
    branch as _mod_branch,
 
29
    bzrdir,
27
30
    config,
28
31
    controldir,
29
32
    errors,
31
34
    trace,
32
35
    urlutils,
33
36
    )
34
 
from ..bzr import (
35
 
    branch as _mod_bzrbranch,
36
 
    bzrdir,
37
 
    )
38
 
from ..bzr.fullhistory import (
 
37
from bzrlib.branchfmt.fullhistory import (
39
38
    BzrBranch5,
40
39
    BzrBranchFormat5,
41
40
    )
42
 
from ..sixish import (
43
 
    BytesIO,
44
 
    )
45
 
 
46
 
 
47
 
class TestErrors(tests.TestCase):
48
 
 
49
 
    def test_unstackable_branch_format(self):
50
 
        format = u'foo'
51
 
        url = "/foo"
52
 
        error = _mod_branch.UnstackableBranchFormat(format, url)
53
 
        self.assertEqualDiff(
54
 
            "The branch '/foo'(foo) is not a stackable format. "
55
 
            "You will need to upgrade the branch to permit branch stacking.",
56
 
            str(error))
57
41
 
58
42
 
59
43
class TestDefaultFormat(tests.TestCase):
61
45
    def test_default_format(self):
62
46
        # update this if you change the default branch format
63
47
        self.assertIsInstance(_mod_branch.format_registry.get_default(),
64
 
                _mod_bzrbranch.BzrBranchFormat7)
 
48
                _mod_branch.BzrBranchFormat7)
65
49
 
66
50
    def test_default_format_is_same_as_bzrdir_default(self):
67
51
        # XXX: it might be nice if there was only one place the default was
74
58
    def test_get_set_default_format(self):
75
59
        # set the format and then set it back again
76
60
        old_format = _mod_branch.format_registry.get_default()
77
 
        _mod_branch.format_registry.set_default(
78
 
            SampleBranchFormat())
 
61
        _mod_branch.format_registry.set_default(SampleBranchFormat())
79
62
        try:
80
63
            # the default branch format is used by the meta dir format
81
64
            # which is not the default bzrdir format at this point
98
81
        branch = BzrBranchFormat5().initialize(bdir)
99
82
        t = self.get_transport()
100
83
        self.log("branch instance is %r" % branch)
101
 
        self.assertTrue(isinstance(branch, BzrBranch5))
 
84
        self.assert_(isinstance(branch, BzrBranch5))
102
85
        self.assertIsDirectory('.', t)
103
86
        self.assertIsDirectory('.bzr/branch', t)
104
87
        self.assertIsDirectory('.bzr/branch/lock', t)
122
105
    # recursive section - that is, it appends the branch name.
123
106
 
124
107
 
125
 
class SampleBranchFormat(_mod_bzrbranch.BranchFormatMetadir):
 
108
class SampleBranchFormat(_mod_branch.BranchFormatMetadir):
126
109
    """A sample format
127
110
 
128
111
    this format is initializable, unsupported to aid in testing the
132
115
    @classmethod
133
116
    def get_format_string(cls):
134
117
        """See BzrBranchFormat.get_format_string()."""
135
 
        return b"Sample branch format."
 
118
        return "Sample branch format."
136
119
 
137
 
    def initialize(self, a_controldir, name=None, repository=None,
 
120
    def initialize(self, a_bzrdir, name=None, repository=None,
138
121
                   append_revisions_only=None):
139
122
        """Format 4 branches cannot be created."""
140
 
        t = a_controldir.get_branch_transport(self, name=name)
 
123
        t = a_bzrdir.get_branch_transport(self, name=name)
141
124
        t.put_bytes('format', self.get_format_string())
142
125
        return 'A branch'
143
126
 
151
134
 
152
135
# Demonstrating how lazy loading is often implemented:
153
136
# A constant string is created.
154
 
SampleSupportedBranchFormatString = b"Sample supported branch format."
 
137
SampleSupportedBranchFormatString = "Sample supported branch format."
155
138
 
156
139
# And the format class can then reference the constant to avoid skew.
157
 
class SampleSupportedBranchFormat(_mod_bzrbranch.BranchFormatMetadir):
 
140
class SampleSupportedBranchFormat(_mod_branch.BranchFormatMetadir):
158
141
    """A sample supported format."""
159
142
 
160
143
    @classmethod
162
145
        """See BzrBranchFormat.get_format_string()."""
163
146
        return SampleSupportedBranchFormatString
164
147
 
165
 
    def initialize(self, a_controldir, name=None, append_revisions_only=None):
166
 
        t = a_controldir.get_branch_transport(self, name=name)
 
148
    def initialize(self, a_bzrdir, name=None, append_revisions_only=None):
 
149
        t = a_bzrdir.get_branch_transport(self, name=name)
167
150
        t.put_bytes('format', self.get_format_string())
168
151
        return 'A branch'
169
152
 
183
166
        # Network name always has to be provided.
184
167
        return "extra"
185
168
 
186
 
    def initialize(self, a_controldir, name=None):
 
169
    def initialize(self, a_bzrdir, name=None):
187
170
        raise NotImplementedError(self.initialize)
188
171
 
189
172
    def open(self, transport, name=None, _found=False, ignore_fallbacks=False,
200
183
        # this is not quite the same as
201
184
        self.build_tree(["foo/", "bar/"])
202
185
        def check_format(format, url):
203
 
            dir = format._matchingcontroldir.initialize(url)
 
186
            dir = format._matchingbzrdir.initialize(url)
204
187
            dir.create_repository()
205
188
            format.initialize(dir)
206
 
            found_format = _mod_bzrbranch.BranchFormatMetadir.find_format(dir)
 
189
            found_format = _mod_branch.BranchFormatMetadir.find_format(dir)
207
190
            self.assertIsInstance(found_format, format.__class__)
208
191
        check_format(BzrBranchFormat5(), "bar")
209
192
 
 
193
    def test_find_format_factory(self):
 
194
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
 
195
        SampleSupportedBranchFormat().initialize(dir)
 
196
        factory = _mod_branch.MetaDirBranchFormatFactory(
 
197
            SampleSupportedBranchFormatString,
 
198
            "bzrlib.tests.test_branch", "SampleSupportedBranchFormat")
 
199
        _mod_branch.format_registry.register(factory)
 
200
        self.addCleanup(_mod_branch.format_registry.remove, factory)
 
201
        b = _mod_branch.Branch.open(self.get_url())
 
202
        self.assertEqual(b, "opened supported branch.")
 
203
 
210
204
    def test_from_string(self):
211
205
        self.assertIsInstance(
212
 
            SampleBranchFormat.from_string(b"Sample branch format."),
 
206
            SampleBranchFormat.from_string("Sample branch format."),
213
207
            SampleBranchFormat)
214
208
        self.assertRaises(AssertionError,
215
 
            SampleBranchFormat.from_string, b"Different branch format.")
 
209
            SampleBranchFormat.from_string, "Different branch format.")
216
210
 
217
211
    def test_find_format_not_branch(self):
218
212
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
219
213
        self.assertRaises(errors.NotBranchError,
220
 
                          _mod_bzrbranch.BranchFormatMetadir.find_format,
 
214
                          _mod_branch.BranchFormatMetadir.find_format,
221
215
                          dir)
222
216
 
223
217
    def test_find_format_unknown_format(self):
224
218
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
225
219
        SampleBranchFormat().initialize(dir)
226
220
        self.assertRaises(errors.UnknownFormatError,
227
 
                          _mod_bzrbranch.BranchFormatMetadir.find_format,
 
221
                          _mod_branch.BranchFormatMetadir.find_format,
228
222
                          dir)
229
223
 
230
224
    def test_find_format_with_features(self):
231
225
        tree = self.make_branch_and_tree('.', format='2a')
232
 
        tree.branch.update_feature_flags({b"name": b"optional"})
233
 
        found_format = _mod_bzrbranch.BranchFormatMetadir.find_format(tree.controldir)
234
 
        self.assertIsInstance(found_format, _mod_bzrbranch.BranchFormatMetadir)
235
 
        self.assertEqual(found_format.features.get(b"name"), b"optional")
236
 
        tree.branch.update_feature_flags({b"name": None})
 
226
        tree.branch.update_feature_flags({"name": "optional"})
 
227
        found_format = _mod_branch.BranchFormatMetadir.find_format(tree.bzrdir)
 
228
        self.assertIsInstance(found_format, _mod_branch.BranchFormatMetadir)
 
229
        self.assertEquals(found_format.features.get("name"), "optional")
 
230
        tree.branch.update_feature_flags({"name": None})
237
231
        branch = _mod_branch.Branch.open('.')
238
 
        self.assertEqual(branch._format.features, {})
 
232
        self.assertEquals(branch._format.features, {})
239
233
 
240
234
 
241
235
class TestBranchFormatRegistry(tests.TestCase):
248
242
        self.assertIs(None, self.registry.get_default())
249
243
        format = SampleBranchFormat()
250
244
        self.registry.set_default(format)
251
 
        self.assertEqual(format, self.registry.get_default())
 
245
        self.assertEquals(format, self.registry.get_default())
252
246
 
253
247
    def test_register_unregister_format(self):
254
248
        format = SampleBranchFormat()
255
249
        self.registry.register(format)
256
 
        self.assertEqual(format,
257
 
            self.registry.get(b"Sample branch format."))
 
250
        self.assertEquals(format,
 
251
            self.registry.get("Sample branch format."))
258
252
        self.registry.remove(format)
259
253
        self.assertRaises(KeyError, self.registry.get,
260
 
            b"Sample branch format.")
 
254
            "Sample branch format.")
261
255
 
262
256
    def test_get_all(self):
263
257
        format = SampleBranchFormat()
264
 
        self.assertEqual([], self.registry._get_all())
 
258
        self.assertEquals([], self.registry._get_all())
265
259
        self.registry.register(format)
266
 
        self.assertEqual([format], self.registry._get_all())
 
260
        self.assertEquals([format], self.registry._get_all())
267
261
 
268
262
    def test_register_extra(self):
269
263
        format = SampleExtraBranchFormat()
270
 
        self.assertEqual([], self.registry._get_all())
 
264
        self.assertEquals([], self.registry._get_all())
271
265
        self.registry.register_extra(format)
272
 
        self.assertEqual([format], self.registry._get_all())
 
266
        self.assertEquals([format], self.registry._get_all())
273
267
 
274
268
    def test_register_extra_lazy(self):
275
 
        self.assertEqual([], self.registry._get_all())
276
 
        self.registry.register_extra_lazy("breezy.tests.test_branch",
 
269
        self.assertEquals([], self.registry._get_all())
 
270
        self.registry.register_extra_lazy("bzrlib.tests.test_branch",
277
271
            "SampleExtraBranchFormat")
278
272
        formats = self.registry._get_all()
279
 
        self.assertEqual(1, len(formats))
 
273
        self.assertEquals(1, len(formats))
280
274
        self.assertIsInstance(formats[0], SampleExtraBranchFormat)
281
275
 
282
276
 
 
277
#Used by TestMetaDirBranchFormatFactory 
 
278
FakeLazyFormat = None
 
279
 
 
280
 
 
281
class TestMetaDirBranchFormatFactory(tests.TestCase):
 
282
 
 
283
    def test_get_format_string_does_not_load(self):
 
284
        """Formats have a static format string."""
 
285
        factory = _mod_branch.MetaDirBranchFormatFactory("yo", None, None)
 
286
        self.assertEqual("yo", factory.get_format_string())
 
287
 
 
288
    def test_call_loads(self):
 
289
        # __call__ is used by the network_format_registry interface to get a
 
290
        # Format.
 
291
        global FakeLazyFormat
 
292
        del FakeLazyFormat
 
293
        factory = _mod_branch.MetaDirBranchFormatFactory(None,
 
294
            "bzrlib.tests.test_branch", "FakeLazyFormat")
 
295
        self.assertRaises(AttributeError, factory)
 
296
 
 
297
    def test_call_returns_call_of_referenced_object(self):
 
298
        global FakeLazyFormat
 
299
        FakeLazyFormat = lambda:'called'
 
300
        factory = _mod_branch.MetaDirBranchFormatFactory(None,
 
301
            "bzrlib.tests.test_branch", "FakeLazyFormat")
 
302
        self.assertEqual('called', factory())
 
303
 
 
304
 
283
305
class TestBranch67(object):
284
306
    """Common tests for both branch 6 and 7 which are mostly the same."""
285
307
 
294
316
 
295
317
    def test_creation(self):
296
318
        format = bzrdir.BzrDirMetaFormat1()
297
 
        format.set_branch_format(_mod_bzrbranch.BzrBranchFormat6())
 
319
        format.set_branch_format(_mod_branch.BzrBranchFormat6())
298
320
        branch = self.make_branch('a', format=format)
299
321
        self.assertIsInstance(branch, self.get_class())
300
322
        branch = self.make_branch('b', format=self.get_format_name())
358
380
class TestBranch6(TestBranch67, tests.TestCaseWithTransport):
359
381
 
360
382
    def get_class(self):
361
 
        return _mod_bzrbranch.BzrBranch6
 
383
        return _mod_branch.BzrBranch6
362
384
 
363
385
    def get_format_name(self):
364
386
        return "dirstate-tags"
368
390
 
369
391
    def test_set_stacked_on_url_errors(self):
370
392
        branch = self.make_branch('a', format=self.get_format_name())
371
 
        self.assertRaises(_mod_branch.UnstackableBranchFormat,
 
393
        self.assertRaises(errors.UnstackableBranchFormat,
372
394
            branch.set_stacked_on_url, None)
373
395
 
374
396
    def test_default_stacked_location(self):
375
397
        branch = self.make_branch('a', format=self.get_format_name())
376
 
        self.assertRaises(_mod_branch.UnstackableBranchFormat, branch.get_stacked_on_url)
 
398
        self.assertRaises(errors.UnstackableBranchFormat, branch.get_stacked_on_url)
377
399
 
378
400
 
379
401
class TestBranch7(TestBranch67, tests.TestCaseWithTransport):
380
402
 
381
403
    def get_class(self):
382
 
        return _mod_bzrbranch.BzrBranch7
 
404
        return _mod_branch.BzrBranch7
383
405
 
384
406
    def get_format_name(self):
385
407
        return "1.9"
389
411
 
390
412
    def test_set_stacked_on_url_unstackable_repo(self):
391
413
        repo = self.make_repository('a', format='dirstate-tags')
392
 
        control = repo.controldir
393
 
        branch = _mod_bzrbranch.BzrBranchFormat7().initialize(control)
 
414
        control = repo.bzrdir
 
415
        branch = _mod_branch.BzrBranchFormat7().initialize(control)
394
416
        target = self.make_branch('b')
395
417
        self.assertRaises(errors.UnstackableRepositoryFormat,
396
418
            branch.set_stacked_on_url, target.base)
397
419
 
398
420
    def test_clone_stacked_on_unstackable_repo(self):
399
421
        repo = self.make_repository('a', format='dirstate-tags')
400
 
        control = repo.controldir
401
 
        branch = _mod_bzrbranch.BzrBranchFormat7().initialize(control)
 
422
        control = repo.bzrdir
 
423
        branch = _mod_branch.BzrBranchFormat7().initialize(control)
402
424
        # Calling clone should not raise UnstackableRepositoryFormat.
403
425
        cloned_bzrdir = control.clone('cloned')
404
426
 
421
443
        branch = self.make_branch('a', format=self.get_format_name())
422
444
        target = self.make_branch_and_tree('b', format=self.get_format_name())
423
445
        branch.set_stacked_on_url(target.branch.base)
424
 
        branch = branch.controldir.open_branch()
 
446
        branch = branch.bzrdir.open_branch()
425
447
        revid = target.commit('foo')
426
448
        self.assertTrue(branch.repository.has_revision(revid))
427
449
 
430
452
 
431
453
    def make_branch(self, location, format=None):
432
454
        if format is None:
433
 
            format = controldir.format_registry.make_controldir('1.9')
434
 
            format.set_branch_format(_mod_bzrbranch.BzrBranchFormat8())
 
455
            format = controldir.format_registry.make_bzrdir('1.9')
 
456
            format.set_branch_format(_mod_branch.BzrBranchFormat8())
435
457
        return tests.TestCaseWithTransport.make_branch(
436
458
            self, location, format=format)
437
459
 
438
460
    def create_branch_with_reference(self):
439
461
        branch = self.make_branch('branch')
440
 
        branch._set_all_reference_info({'path': ('location', 'file-id')})
 
462
        branch._set_all_reference_info({'file-id': ('path', 'location')})
441
463
        return branch
442
464
 
443
465
    @staticmethod
454
476
        branch.lock_read()
455
477
        self.addCleanup(branch.unlock)
456
478
        self.instrument_branch(branch, gets)
457
 
        branch.get_reference_info('path')
458
 
        branch.get_reference_info('path')
 
479
        branch.get_reference_info('file-id')
 
480
        branch.get_reference_info('file-id')
459
481
        self.assertEqual(1, len(gets))
460
482
 
461
483
    def test_reference_info_caching_read_unlocked(self):
462
484
        gets = []
463
485
        branch = self.create_branch_with_reference()
464
486
        self.instrument_branch(branch, gets)
465
 
        branch.get_reference_info('path')
466
 
        branch.get_reference_info('path')
 
487
        branch.get_reference_info('file-id')
 
488
        branch.get_reference_info('file-id')
467
489
        self.assertEqual(2, len(gets))
468
490
 
469
491
    def test_reference_info_caching_write_locked(self):
472
494
        branch.lock_write()
473
495
        self.instrument_branch(branch, gets)
474
496
        self.addCleanup(branch.unlock)
475
 
        branch._set_all_reference_info({'path2': ('location2', 'file-id')})
476
 
        location, file_id = branch.get_reference_info('path2')
 
497
        branch._set_all_reference_info({'file-id': ('path2', 'location2')})
 
498
        path, location = branch.get_reference_info('file-id')
477
499
        self.assertEqual(0, len(gets))
478
 
        self.assertEqual('file-id', file_id)
 
500
        self.assertEqual('path2', path)
479
501
        self.assertEqual('location2', location)
480
502
 
481
503
    def test_reference_info_caches_cleared(self):
482
504
        branch = self.make_branch('branch')
483
 
        with branch.lock_write():
484
 
            branch.set_reference_info('path2', 'location2', b'file-id')
 
505
        branch.lock_write()
 
506
        branch.set_reference_info('file-id', 'path2', 'location2')
 
507
        branch.unlock()
485
508
        doppelganger = _mod_branch.Branch.open('branch')
486
 
        doppelganger.set_reference_info('path3', 'location3', b'file-id')
487
 
        self.assertEqual(('location3', b'file-id'),
488
 
                         branch.get_reference_info('path3'))
 
509
        doppelganger.set_reference_info('file-id', 'path3', 'location3')
 
510
        self.assertEqual(('path3', 'location3'),
 
511
                         branch.get_reference_info('file-id'))
489
512
 
490
513
    def _recordParentMapCalls(self, repo):
491
514
        self._parent_map_calls = []
509
532
        target_branch = dir.create_branch()
510
533
        t.mkdir('branch')
511
534
        branch_dir = bzrdirformat.initialize(self.get_url('branch'))
512
 
        made_branch = _mod_bzrbranch.BranchReferenceFormat().initialize(
 
535
        made_branch = _mod_branch.BranchReferenceFormat().initialize(
513
536
            branch_dir, target_branch=target_branch)
514
537
        self.assertEqual(made_branch.base, target_branch.base)
515
538
        opened_branch = branch_dir.open_branch()
519
542
        """For a BranchReference, get_reference should return the location."""
520
543
        branch = self.make_branch('target')
521
544
        checkout = branch.create_checkout('checkout', lightweight=True)
522
 
        reference_url = branch.controldir.root_transport.abspath('') + '/'
 
545
        reference_url = branch.bzrdir.root_transport.abspath('') + '/'
523
546
        # if the api for create_checkout changes to return different checkout types
524
547
        # then this file read will fail.
525
548
        self.assertFileEqual(reference_url, 'checkout/.bzr/branch/location')
526
549
        self.assertEqual(reference_url,
527
 
            _mod_bzrbranch.BranchReferenceFormat().get_reference(checkout.controldir))
 
550
            _mod_branch.BranchReferenceFormat().get_reference(checkout.bzrdir))
528
551
 
529
552
 
530
553
class TestHooks(tests.TestCaseWithTransport):
560
583
        self.assertLength(1, calls)
561
584
        params = calls[0]
562
585
        self.assertIsInstance(params, _mod_branch.BranchInitHookParams)
563
 
        self.assertTrue(hasattr(params, 'controldir'))
 
586
        self.assertTrue(hasattr(params, 'bzrdir'))
564
587
        self.assertTrue(hasattr(params, 'branch'))
565
588
 
566
589
    def test_post_branch_init_hook_repr(self):
573
596
        self.assertStartsWith(param_repr, '<BranchInitHookParams of ')
574
597
 
575
598
    def test_post_switch_hook(self):
576
 
        from .. import switch
 
599
        from bzrlib import switch
577
600
        calls = []
578
601
        _mod_branch.Branch.hooks.install_named_hook('post_switch',
579
602
            calls.append, None)
581
604
        self.build_tree(['branch-1/file-1'])
582
605
        tree.add('file-1')
583
606
        tree.commit('rev1')
584
 
        to_branch = tree.controldir.sprout('branch-2').open_branch()
 
607
        to_branch = tree.bzrdir.sprout('branch-2').open_branch()
585
608
        self.build_tree(['branch-1/file-2'])
586
609
        tree.add('file-2')
587
610
        tree.remove('file-1')
588
611
        tree.commit('rev2')
589
612
        checkout = tree.branch.create_checkout('checkout')
590
613
        self.assertLength(0, calls)
591
 
        switch.switch(checkout.controldir, to_branch)
 
614
        switch.switch(checkout.bzrdir, to_branch)
592
615
        self.assertLength(1, calls)
593
616
        params = calls[0]
594
617
        self.assertIsInstance(params, _mod_branch.SwitchHookParams)
611
634
                         self.branch.get_append_revisions_only())
612
635
 
613
636
    def test_valid_append_revisions_only(self):
614
 
        self.assertEqual(None,
 
637
        self.assertEquals(None,
615
638
                          self.config_stack.get('append_revisions_only'))
616
639
        self.check_append_revisions_only(None)
617
640
        self.check_append_revisions_only(False, 'False')
687
710
        r.old_revno = 10
688
711
        r.new_revid = "new-revid"
689
712
        r.new_revno = 20
690
 
        f = BytesIO()
 
713
        f = StringIO()
691
714
        r.report(f)
692
715
        self.assertEqual("Now on revision 20.\n", f.getvalue())
693
716
        self.assertEqual("Now on revision 20.\n", f.getvalue())
696
719
        r = _mod_branch.PullResult()
697
720
        r.old_revid = "same-revid"
698
721
        r.new_revid = "same-revid"
699
 
        f = BytesIO()
 
722
        f = StringIO()
700
723
        r.report(f)
701
724
        self.assertEqual("No revisions or tags to pull.\n", f.getvalue())