~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_branch.py

  • Committer: Patch Queue Manager
  • Date: 2012-03-28 16:13:49 UTC
  • mfrom: (6499.2.3 948339-config-caching)
  • Revision ID: pqm@pqm.ubuntu.com-20120328161349-2gsc0g11fcu43hlc
(vila) Properly share mutable config sections and save the branch config
 only during the final unlock (Vincent Ladeuil)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2011 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
28
28
    branch as _mod_branch,
29
29
    bzrdir,
30
30
    config,
 
31
    controldir,
31
32
    errors,
32
33
    symbol_versioning,
33
34
    tests,
101
102
    # recursive section - that is, it appends the branch name.
102
103
 
103
104
 
104
 
class SampleBranchFormat(_mod_branch.BranchFormat):
 
105
class SampleBranchFormat(_mod_branch.BranchFormatMetadir):
105
106
    """A sample format
106
107
 
107
108
    this format is initializable, unsupported to aid in testing the
108
109
    open and open_downlevel routines.
109
110
    """
110
111
 
111
 
    def get_format_string(self):
 
112
    @classmethod
 
113
    def get_format_string(cls):
112
114
        """See BzrBranchFormat.get_format_string()."""
113
115
        return "Sample branch format."
114
116
 
115
 
    def initialize(self, a_bzrdir, name=None, repository=None):
 
117
    def initialize(self, a_bzrdir, name=None, repository=None,
 
118
                   append_revisions_only=None):
116
119
        """Format 4 branches cannot be created."""
117
120
        t = a_bzrdir.get_branch_transport(self, name=name)
118
121
        t.put_bytes('format', self.get_format_string())
121
124
    def is_supported(self):
122
125
        return False
123
126
 
124
 
    def open(self, transport, name=None, _found=False, ignore_fallbacks=False):
 
127
    def open(self, transport, name=None, _found=False, ignore_fallbacks=False,
 
128
             possible_transports=None):
125
129
        return "opened branch."
126
130
 
127
131
 
130
134
SampleSupportedBranchFormatString = "Sample supported branch format."
131
135
 
132
136
# And the format class can then reference the constant to avoid skew.
133
 
class SampleSupportedBranchFormat(_mod_branch.BranchFormat):
 
137
class SampleSupportedBranchFormat(_mod_branch.BranchFormatMetadir):
134
138
    """A sample supported format."""
135
139
 
136
 
    def get_format_string(self):
 
140
    @classmethod
 
141
    def get_format_string(cls):
137
142
        """See BzrBranchFormat.get_format_string()."""
138
143
        return SampleSupportedBranchFormatString
139
144
 
140
 
    def initialize(self, a_bzrdir, name=None):
 
145
    def initialize(self, a_bzrdir, name=None, append_revisions_only=None):
141
146
        t = a_bzrdir.get_branch_transport(self, name=name)
142
147
        t.put_bytes('format', self.get_format_string())
143
148
        return 'A branch'
144
149
 
145
 
    def open(self, transport, name=None, _found=False, ignore_fallbacks=False):
 
150
    def open(self, transport, name=None, _found=False, ignore_fallbacks=False,
 
151
             possible_transports=None):
146
152
        return "opened supported branch."
147
153
 
148
154
 
160
166
    def initialize(self, a_bzrdir, name=None):
161
167
        raise NotImplementedError(self.initialize)
162
168
 
163
 
    def open(self, transport, name=None, _found=False, ignore_fallbacks=False):
 
169
    def open(self, transport, name=None, _found=False, ignore_fallbacks=False,
 
170
             possible_transports=None):
164
171
        raise NotImplementedError(self.open)
165
172
 
166
173
 
176
183
            dir = format._matchingbzrdir.initialize(url)
177
184
            dir.create_repository()
178
185
            format.initialize(dir)
179
 
            found_format = _mod_branch.BranchFormat.find_format(dir)
 
186
            found_format = _mod_branch.BranchFormatMetadir.find_format(dir)
180
187
            self.assertIsInstance(found_format, format.__class__)
181
188
        check_format(_mod_branch.BzrBranchFormat5(), "bar")
182
189
 
191
198
        b = _mod_branch.Branch.open(self.get_url())
192
199
        self.assertEqual(b, "opened supported branch.")
193
200
 
 
201
    def test_from_string(self):
 
202
        self.assertIsInstance(
 
203
            SampleBranchFormat.from_string("Sample branch format."),
 
204
            SampleBranchFormat)
 
205
        self.assertRaises(AssertionError,
 
206
            SampleBranchFormat.from_string, "Different branch format.")
 
207
 
194
208
    def test_find_format_not_branch(self):
195
209
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
196
210
        self.assertRaises(errors.NotBranchError,
197
 
                          _mod_branch.BranchFormat.find_format,
 
211
                          _mod_branch.BranchFormatMetadir.find_format,
198
212
                          dir)
199
213
 
200
214
    def test_find_format_unknown_format(self):
201
215
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
202
216
        SampleBranchFormat().initialize(dir)
203
217
        self.assertRaises(errors.UnknownFormatError,
204
 
                          _mod_branch.BranchFormat.find_format,
 
218
                          _mod_branch.BranchFormatMetadir.find_format,
205
219
                          dir)
206
220
 
207
 
    def test_register_unregister_format(self):
208
 
        # Test the deprecated format registration functions
209
 
        format = SampleBranchFormat()
210
 
        # make a control dir
211
 
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
212
 
        # make a branch
213
 
        format.initialize(dir)
214
 
        # register a format for it.
215
 
        self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
216
 
            _mod_branch.BranchFormat.register_format, format)
217
 
        # which branch.Open will refuse (not supported)
218
 
        self.assertRaises(errors.UnsupportedFormatError,
219
 
                          _mod_branch.Branch.open, self.get_url())
220
 
        self.make_branch_and_tree('foo')
221
 
        # but open_downlevel will work
222
 
        self.assertEqual(
223
 
            format.open(dir),
224
 
            bzrdir.BzrDir.open(self.get_url()).open_branch(unsupported=True))
225
 
        # unregister the format
226
 
        self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
227
 
            _mod_branch.BranchFormat.unregister_format, format)
228
 
        self.make_branch_and_tree('bar')
 
221
    def test_find_format_with_features(self):
 
222
        tree = self.make_branch_and_tree('.', format='2a')
 
223
        tree.branch.update_feature_flags({"name": "optional"})
 
224
        found_format = _mod_branch.BranchFormatMetadir.find_format(tree.bzrdir)
 
225
        self.assertIsInstance(found_format, _mod_branch.BranchFormatMetadir)
 
226
        self.assertEquals(found_format.features.get("name"), "optional")
 
227
        tree.branch.update_feature_flags({"name": None})
 
228
        branch = _mod_branch.Branch.open('.')
 
229
        self.assertEquals(branch._format.features, {})
229
230
 
230
231
 
231
232
class TestBranchFormatRegistry(tests.TestCase):
333
334
        self.assertPathDoesNotExist('a/.bzr/branch/parent')
334
335
        self.assertEqual('http://example.com', branch.get_parent())
335
336
        branch.set_push_location('sftp://example.com')
336
 
        config = branch.get_config()._get_branch_data_config()
337
 
        self.assertEqual('sftp://example.com',
338
 
                         config.get_user_option('push_location'))
 
337
        conf = branch.get_config_stack()
 
338
        self.assertEqual('sftp://example.com', conf.get('push_location'))
339
339
        branch.set_bound_location('ftp://example.com')
340
340
        self.assertPathDoesNotExist('a/.bzr/branch/bound')
341
341
        self.assertEqual('ftp://example.com', branch.get_bound_location())
342
342
 
343
 
    def test_set_revision_history(self):
344
 
        builder = self.make_branch_builder('.', format=self.get_format_name())
345
 
        builder.build_snapshot('foo', None,
346
 
            [('add', ('', None, 'directory', None))],
347
 
            message='foo')
348
 
        builder.build_snapshot('bar', None, [], message='bar')
349
 
        branch = builder.get_branch()
350
 
        branch.lock_write()
351
 
        self.addCleanup(branch.unlock)
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'])
356
 
        self.assertRaises(errors.NotLefthandHistory,
357
 
            self.applyDeprecated, symbol_versioning.deprecated_in((2, 4, 0)),
358
 
            branch.set_revision_history, ['bar'])
359
 
 
360
343
    def do_checkout_test(self, lightweight=False):
361
344
        tree = self.make_branch_and_tree('source',
362
345
            format=self.get_format_name_subtree())
390
373
    def test_light_checkout_with_references(self):
391
374
        self.do_checkout_test(lightweight=True)
392
375
 
393
 
    def test_set_push(self):
394
 
        branch = self.make_branch('source', format=self.get_format_name())
395
 
        branch.get_config().set_user_option('push_location', 'old',
396
 
            store=config.STORE_LOCATION)
397
 
        warnings = []
398
 
        def warning(*args):
399
 
            warnings.append(args[0] % args[1:])
400
 
        _warning = trace.warning
401
 
        trace.warning = warning
402
 
        try:
403
 
            branch.set_push_location('new')
404
 
        finally:
405
 
            trace.warning = _warning
406
 
        self.assertEqual(warnings[0], 'Value "new" is masked by "old" from '
407
 
                         'locations.conf')
408
 
 
409
376
 
410
377
class TestBranch6(TestBranch67, tests.TestCaseWithTransport):
411
378
 
482
449
 
483
450
    def make_branch(self, location, format=None):
484
451
        if format is None:
485
 
            format = bzrdir.format_registry.make_bzrdir('1.9')
 
452
            format = controldir.format_registry.make_bzrdir('1.9')
486
453
            format.set_branch_format(_mod_branch.BzrBranchFormat8())
487
454
        return tests.TestCaseWithTransport.make_branch(
488
455
            self, location, format=format)
540
507
        self.assertEqual(('path3', 'location3'),
541
508
                         branch.get_reference_info('file-id'))
542
509
 
 
510
    def _recordParentMapCalls(self, repo):
 
511
        self._parent_map_calls = []
 
512
        orig_get_parent_map = repo.revisions.get_parent_map
 
513
        def get_parent_map(q):
 
514
            q = list(q)
 
515
            self._parent_map_calls.extend([e[0] for e in q])
 
516
            return orig_get_parent_map(q)
 
517
        repo.revisions.get_parent_map = get_parent_map
 
518
 
 
519
 
543
520
class TestBranchReference(tests.TestCaseWithTransport):
544
521
    """Tests for the branch reference facility."""
545
522
 
559
536
        self.assertEqual(opened_branch.base, target_branch.base)
560
537
 
561
538
    def test_get_reference(self):
562
 
        """For a BranchReference, get_reference should reutrn the location."""
 
539
        """For a BranchReference, get_reference should return the location."""
563
540
        branch = self.make_branch('target')
564
541
        checkout = branch.create_checkout('checkout', lightweight=True)
565
542
        reference_url = branch.bzrdir.root_transport.abspath('') + '/'
575
552
    def test_constructor(self):
576
553
        """Check that creating a BranchHooks instance has the right defaults."""
577
554
        hooks = _mod_branch.BranchHooks()
578
 
        self.assertTrue("set_rh" in hooks, "set_rh not in %s" % hooks)
579
555
        self.assertTrue("post_push" in hooks, "post_push not in %s" % hooks)
580
556
        self.assertTrue("post_commit" in hooks, "post_commit not in %s" % hooks)
581
557
        self.assertTrue("pre_commit" in hooks, "pre_commit not in %s" % hooks)
645
621
    def setUp(self):
646
622
        super(TestBranchOptions, self).setUp()
647
623
        self.branch = self.make_branch('.')
648
 
        self.config = self.branch.get_config()
 
624
        self.config_stack = self.branch.get_config_stack()
649
625
 
650
626
    def check_append_revisions_only(self, expected_value, value=None):
651
627
        """Set append_revisions_only in config and check its interpretation."""
652
628
        if value is not None:
653
 
            self.config.set_user_option('append_revisions_only', value)
 
629
            self.config_stack.set('append_revisions_only', value)
654
630
        self.assertEqual(expected_value,
655
 
                         self.branch._get_append_revisions_only())
 
631
                         self.branch.get_append_revisions_only())
656
632
 
657
633
    def test_valid_append_revisions_only(self):
658
634
        self.assertEquals(None,
659
 
                          self.config.get_user_option('append_revisions_only'))
 
635
                          self.config_stack.get('append_revisions_only'))
660
636
        self.check_append_revisions_only(None)
661
637
        self.check_append_revisions_only(False, 'False')
662
638
        self.check_append_revisions_only(True, 'True')
674
650
        self.check_append_revisions_only(None, 'not-a-bool')
675
651
        self.assertLength(1, self.warnings)
676
652
        self.assertEqual(
677
 
            'Value "not-a-bool" is not a boolean for "append_revisions_only"',
 
653
            'Value "not-a-bool" is not valid for "append_revisions_only"',
678
654
            self.warnings[0])
679
655
 
 
656
    def test_use_fresh_values(self):
 
657
        copy = _mod_branch.Branch.open(self.branch.base)
 
658
        copy.lock_write()
 
659
        try:
 
660
            copy.get_config_stack().set('foo', 'bar')
 
661
        finally:
 
662
            copy.unlock()
 
663
        self.assertFalse(self.branch.is_locked())
 
664
        # Since the branch is locked, the option value won't be saved on disk
 
665
        # so trying to access the config of locked branch via another older
 
666
        # non-locked branch object pointing to the same branch is not supported
 
667
        self.assertEqual(None, self.branch.get_config_stack().get('foo'))
 
668
        # Using a newly created branch object works as expected
 
669
        fresh = _mod_branch.Branch.open(self.branch.base)
 
670
        self.assertEqual('bar', fresh.get_config_stack().get('foo'))
 
671
 
 
672
    def test_set_from_config_get_from_config_stack(self):
 
673
        self.branch.lock_write()
 
674
        self.addCleanup(self.branch.unlock)
 
675
        self.branch.get_config().set_user_option('foo', 'bar')
 
676
        result = self.branch.get_config_stack().get('foo')
 
677
        # https://bugs.launchpad.net/bzr/+bug/948344
 
678
        self.expectFailure('BranchStack uses cache after set_user_option',
 
679
                           self.assertEqual, 'bar', result)
 
680
 
 
681
    def test_set_from_config_stack_get_from_config(self):
 
682
        self.branch.lock_write()
 
683
        self.addCleanup(self.branch.unlock)
 
684
        self.branch.get_config_stack().set('foo', 'bar')
 
685
        # Since the branch is locked, the option value won't be saved on disk
 
686
        # so mixing get() and get_user_option() is broken by design.
 
687
        self.assertEqual(None,
 
688
                         self.branch.get_config().get_user_option('foo'))
 
689
 
 
690
    def test_set_delays_write_when_branch_is_locked(self):
 
691
        self.branch.lock_write()
 
692
        self.addCleanup(self.branch.unlock)
 
693
        self.branch.get_config_stack().set('foo', 'bar')
 
694
        copy = _mod_branch.Branch.open(self.branch.base)
 
695
        result = copy.get_config_stack().get('foo')
 
696
        # Accessing from a different branch object is like accessing from a
 
697
        # different process: the option has not been saved yet and the new
 
698
        # value cannot be seen.
 
699
        self.assertIs(None, result)
 
700
 
680
701
 
681
702
class TestPullResult(tests.TestCase):
682
703
 
683
 
    def test_pull_result_to_int(self):
684
 
        # to support old code, the pull result can be used as an int
685
 
        r = _mod_branch.PullResult()
686
 
        r.old_revno = 10
687
 
        r.new_revno = 20
688
 
        # this usage of results is not recommended for new code (because it
689
 
        # doesn't describe very well what happened), but for api stability
690
 
        # it's still supported
691
 
        self.assertEqual(self.applyDeprecated(
692
 
            symbol_versioning.deprecated_in((2, 3, 0)),
693
 
            r.__int__),
694
 
            10)
695
 
 
696
704
    def test_report_changed(self):
697
705
        r = _mod_branch.PullResult()
698
706
        r.old_revid = "old-revid"
702
710
        f = StringIO()
703
711
        r.report(f)
704
712
        self.assertEqual("Now on revision 20.\n", f.getvalue())
 
713
        self.assertEqual("Now on revision 20.\n", f.getvalue())
705
714
 
706
715
    def test_report_unchanged(self):
707
716
        r = _mod_branch.PullResult()
709
718
        r.new_revid = "same-revid"
710
719
        f = StringIO()
711
720
        r.report(f)
712
 
        self.assertEqual("No revisions to pull.\n", f.getvalue())
713
 
 
 
721
        self.assertEqual("No revisions or tags to pull.\n", f.getvalue())