~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_branch.py

  • Committer: Vincent Ladeuil
  • Date: 2012-07-31 09:17:34 UTC
  • mto: This revision was merged to the branch mainline in revision 6554.
  • Revision ID: v.ladeuil+lp@free.fr-20120731091734-700oburs0806cont
SplitĀ eagerĀ test.

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
 
 
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, {})
 
230
 
207
231
    def test_register_unregister_format(self):
208
232
        # Test the deprecated format registration functions
209
233
        format = SampleBranchFormat()
221
245
        # but open_downlevel will work
222
246
        self.assertEqual(
223
247
            format.open(dir),
224
 
            bzrdir.BzrDir.open(self.get_url()).open_branch(unsupported=True))
 
248
            controldir.ControlDir.open(self.get_url()).open_branch(unsupported=True))
225
249
        # unregister the format
226
250
        self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
227
251
            _mod_branch.BranchFormat.unregister_format, format)
333
357
        self.assertPathDoesNotExist('a/.bzr/branch/parent')
334
358
        self.assertEqual('http://example.com', branch.get_parent())
335
359
        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'))
 
360
        conf = branch.get_config_stack()
 
361
        self.assertEqual('sftp://example.com', conf.get('push_location'))
339
362
        branch.set_bound_location('ftp://example.com')
340
363
        self.assertPathDoesNotExist('a/.bzr/branch/bound')
341
364
        self.assertEqual('ftp://example.com', branch.get_bound_location())
390
413
    def test_light_checkout_with_references(self):
391
414
        self.do_checkout_test(lightweight=True)
392
415
 
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
416
 
410
417
class TestBranch6(TestBranch67, tests.TestCaseWithTransport):
411
418
 
482
489
 
483
490
    def make_branch(self, location, format=None):
484
491
        if format is None:
485
 
            format = bzrdir.format_registry.make_bzrdir('1.9')
 
492
            format = controldir.format_registry.make_bzrdir('1.9')
486
493
            format.set_branch_format(_mod_branch.BzrBranchFormat8())
487
494
        return tests.TestCaseWithTransport.make_branch(
488
495
            self, location, format=format)
540
547
        self.assertEqual(('path3', 'location3'),
541
548
                         branch.get_reference_info('file-id'))
542
549
 
 
550
    def _recordParentMapCalls(self, repo):
 
551
        self._parent_map_calls = []
 
552
        orig_get_parent_map = repo.revisions.get_parent_map
 
553
        def get_parent_map(q):
 
554
            q = list(q)
 
555
            self._parent_map_calls.extend([e[0] for e in q])
 
556
            return orig_get_parent_map(q)
 
557
        repo.revisions.get_parent_map = get_parent_map
 
558
 
 
559
 
543
560
class TestBranchReference(tests.TestCaseWithTransport):
544
561
    """Tests for the branch reference facility."""
545
562
 
559
576
        self.assertEqual(opened_branch.base, target_branch.base)
560
577
 
561
578
    def test_get_reference(self):
562
 
        """For a BranchReference, get_reference should reutrn the location."""
 
579
        """For a BranchReference, get_reference should return the location."""
563
580
        branch = self.make_branch('target')
564
581
        checkout = branch.create_checkout('checkout', lightweight=True)
565
582
        reference_url = branch.bzrdir.root_transport.abspath('') + '/'
645
662
    def setUp(self):
646
663
        super(TestBranchOptions, self).setUp()
647
664
        self.branch = self.make_branch('.')
648
 
        self.config = self.branch.get_config()
 
665
        self.config_stack = self.branch.get_config_stack()
649
666
 
650
667
    def check_append_revisions_only(self, expected_value, value=None):
651
668
        """Set append_revisions_only in config and check its interpretation."""
652
669
        if value is not None:
653
 
            self.config.set_user_option('append_revisions_only', value)
 
670
            self.config_stack.set('append_revisions_only', value)
654
671
        self.assertEqual(expected_value,
655
 
                         self.branch._get_append_revisions_only())
 
672
                         self.branch.get_append_revisions_only())
656
673
 
657
674
    def test_valid_append_revisions_only(self):
658
675
        self.assertEquals(None,
659
 
                          self.config.get_user_option('append_revisions_only'))
 
676
                          self.config_stack.get('append_revisions_only'))
660
677
        self.check_append_revisions_only(None)
661
678
        self.check_append_revisions_only(False, 'False')
662
679
        self.check_append_revisions_only(True, 'True')
674
691
        self.check_append_revisions_only(None, 'not-a-bool')
675
692
        self.assertLength(1, self.warnings)
676
693
        self.assertEqual(
677
 
            'Value "not-a-bool" is not a boolean for "append_revisions_only"',
 
694
            'Value "not-a-bool" is not valid for "append_revisions_only"',
678
695
            self.warnings[0])
679
696
 
 
697
    def test_use_fresh_values(self):
 
698
        copy = _mod_branch.Branch.open(self.branch.base)
 
699
        copy.lock_write()
 
700
        try:
 
701
            copy.get_config_stack().set('foo', 'bar')
 
702
        finally:
 
703
            copy.unlock()
 
704
        self.assertFalse(self.branch.is_locked())
 
705
        result = self.branch.get_config_stack().get('foo')
 
706
        # Bug: https://bugs.launchpad.net/bzr/+bug/948339
 
707
        self.expectFailure('Unlocked branches cache their configs',
 
708
            self.assertEqual, 'bar', result)
 
709
 
 
710
    def test_set_from_config_get_from_config_stack(self):
 
711
        self.branch.lock_write()
 
712
        self.addCleanup(self.branch.unlock)
 
713
        self.branch.get_config().set_user_option('foo', 'bar')
 
714
        result = self.branch.get_config_stack().get('foo')
 
715
        # https://bugs.launchpad.net/bzr/+bug/948344
 
716
        self.expectFailure('BranchStack uses cache after set_user_option',
 
717
                           self.assertEqual, 'bar', result)
 
718
 
 
719
    def test_set_from_config_stack_get_from_config(self):
 
720
        self.branch.lock_write()
 
721
        self.addCleanup(self.branch.unlock)
 
722
        self.branch.get_config_stack().set('foo', 'bar')
 
723
        self.assertEqual('bar',
 
724
                         self.branch.get_config().get_user_option('foo'))
 
725
 
 
726
    def test_set_delays_write(self):
 
727
        self.branch.lock_write()
 
728
        self.addCleanup(self.branch.unlock)
 
729
        self.branch.get_config_stack().set('foo', 'bar')
 
730
        copy = _mod_branch.Branch.open(self.branch.base)
 
731
        result = copy.get_config_stack().get('foo')
 
732
        # Bug: https://bugs.launchpad.net/bzr/+bug/948339
 
733
        self.expectFailure("Config writes are not cached.", self.assertIs,
 
734
                           None, result)
 
735
 
680
736
 
681
737
class TestPullResult(tests.TestCase):
682
738
 
702
758
        f = StringIO()
703
759
        r.report(f)
704
760
        self.assertEqual("Now on revision 20.\n", f.getvalue())
 
761
        self.assertEqual("Now on revision 20.\n", f.getvalue())
705
762
 
706
763
    def test_report_unchanged(self):
707
764
        r = _mod_branch.PullResult()
709
766
        r.new_revid = "same-revid"
710
767
        f = StringIO()
711
768
        r.report(f)
712
 
        self.assertEqual("No revisions to pull.\n", f.getvalue())
713
 
 
 
769
        self.assertEqual("No revisions or tags to pull.\n", f.getvalue())