~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_branch.py

  • Committer: Vincent Ladeuil
  • Date: 2011-11-24 10:47:43 UTC
  • mto: (6321.1.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 6322.
  • Revision ID: v.ladeuil+lp@free.fr-20111124104743-rxqwhmzqu5k17f24
First cut at a working plugin to avoid conflicts in .po files by shelling out to msgmerge.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2012 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
28
28
    branch as _mod_branch,
29
29
    bzrdir,
30
30
    config,
31
 
    controldir,
32
31
    errors,
 
32
    symbol_versioning,
33
33
    tests,
34
34
    trace,
35
35
    urlutils,
36
36
    )
37
 
from bzrlib.branchfmt.fullhistory import (
38
 
    BzrBranch5,
39
 
    BzrBranchFormat5,
40
 
    )
41
37
 
42
38
 
43
39
class TestDefaultFormat(tests.TestCase):
78
74
        url = self.get_url()
79
75
        bdir = bzrdir.BzrDirMetaFormat1().initialize(url)
80
76
        bdir.create_repository()
81
 
        branch = BzrBranchFormat5().initialize(bdir)
 
77
        branch = _mod_branch.BzrBranchFormat5().initialize(bdir)
82
78
        t = self.get_transport()
83
79
        self.log("branch instance is %r" % branch)
84
 
        self.assert_(isinstance(branch, BzrBranch5))
 
80
        self.assert_(isinstance(branch, _mod_branch.BzrBranch5))
85
81
        self.assertIsDirectory('.', t)
86
82
        self.assertIsDirectory('.bzr/branch', t)
87
83
        self.assertIsDirectory('.bzr/branch/lock', t)
105
101
    # recursive section - that is, it appends the branch name.
106
102
 
107
103
 
108
 
class SampleBranchFormat(_mod_branch.BranchFormatMetadir):
 
104
class SampleBranchFormat(_mod_branch.BranchFormat):
109
105
    """A sample format
110
106
 
111
107
    this format is initializable, unsupported to aid in testing the
112
108
    open and open_downlevel routines.
113
109
    """
114
110
 
115
 
    @classmethod
116
 
    def get_format_string(cls):
 
111
    def get_format_string(self):
117
112
        """See BzrBranchFormat.get_format_string()."""
118
113
        return "Sample branch format."
119
114
 
127
122
    def is_supported(self):
128
123
        return False
129
124
 
130
 
    def open(self, transport, name=None, _found=False, ignore_fallbacks=False,
131
 
             possible_transports=None):
 
125
    def open(self, transport, name=None, _found=False, ignore_fallbacks=False):
132
126
        return "opened branch."
133
127
 
134
128
 
137
131
SampleSupportedBranchFormatString = "Sample supported branch format."
138
132
 
139
133
# And the format class can then reference the constant to avoid skew.
140
 
class SampleSupportedBranchFormat(_mod_branch.BranchFormatMetadir):
 
134
class SampleSupportedBranchFormat(_mod_branch.BranchFormat):
141
135
    """A sample supported format."""
142
136
 
143
 
    @classmethod
144
 
    def get_format_string(cls):
 
137
    def get_format_string(self):
145
138
        """See BzrBranchFormat.get_format_string()."""
146
139
        return SampleSupportedBranchFormatString
147
140
 
150
143
        t.put_bytes('format', self.get_format_string())
151
144
        return 'A branch'
152
145
 
153
 
    def open(self, transport, name=None, _found=False, ignore_fallbacks=False,
154
 
             possible_transports=None):
 
146
    def open(self, transport, name=None, _found=False, ignore_fallbacks=False):
155
147
        return "opened supported branch."
156
148
 
157
149
 
169
161
    def initialize(self, a_bzrdir, name=None):
170
162
        raise NotImplementedError(self.initialize)
171
163
 
172
 
    def open(self, transport, name=None, _found=False, ignore_fallbacks=False,
173
 
             possible_transports=None):
 
164
    def open(self, transport, name=None, _found=False, ignore_fallbacks=False):
174
165
        raise NotImplementedError(self.open)
175
166
 
176
167
 
186
177
            dir = format._matchingbzrdir.initialize(url)
187
178
            dir.create_repository()
188
179
            format.initialize(dir)
189
 
            found_format = _mod_branch.BranchFormatMetadir.find_format(dir)
 
180
            found_format = _mod_branch.BranchFormat.find_format(dir)
190
181
            self.assertIsInstance(found_format, format.__class__)
191
 
        check_format(BzrBranchFormat5(), "bar")
 
182
        check_format(_mod_branch.BzrBranchFormat5(), "bar")
192
183
 
193
184
    def test_find_format_factory(self):
194
185
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
201
192
        b = _mod_branch.Branch.open(self.get_url())
202
193
        self.assertEqual(b, "opened supported branch.")
203
194
 
204
 
    def test_from_string(self):
205
 
        self.assertIsInstance(
206
 
            SampleBranchFormat.from_string("Sample branch format."),
207
 
            SampleBranchFormat)
208
 
        self.assertRaises(AssertionError,
209
 
            SampleBranchFormat.from_string, "Different branch format.")
210
 
 
211
195
    def test_find_format_not_branch(self):
212
196
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
213
197
        self.assertRaises(errors.NotBranchError,
214
 
                          _mod_branch.BranchFormatMetadir.find_format,
 
198
                          _mod_branch.BranchFormat.find_format,
215
199
                          dir)
216
200
 
217
201
    def test_find_format_unknown_format(self):
218
202
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
219
203
        SampleBranchFormat().initialize(dir)
220
204
        self.assertRaises(errors.UnknownFormatError,
221
 
                          _mod_branch.BranchFormatMetadir.find_format,
 
205
                          _mod_branch.BranchFormat.find_format,
222
206
                          dir)
223
207
 
224
 
    def test_find_format_with_features(self):
225
 
        tree = self.make_branch_and_tree('.', format='2a')
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})
231
 
        branch = _mod_branch.Branch.open('.')
232
 
        self.assertEquals(branch._format.features, {})
 
208
    def test_register_unregister_format(self):
 
209
        # Test the deprecated format registration functions
 
210
        format = SampleBranchFormat()
 
211
        # make a control dir
 
212
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
 
213
        # make a branch
 
214
        format.initialize(dir)
 
215
        # register a format for it.
 
216
        self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
 
217
            _mod_branch.BranchFormat.register_format, format)
 
218
        # which branch.Open will refuse (not supported)
 
219
        self.assertRaises(errors.UnsupportedFormatError,
 
220
                          _mod_branch.Branch.open, self.get_url())
 
221
        self.make_branch_and_tree('foo')
 
222
        # but open_downlevel will work
 
223
        self.assertEqual(
 
224
            format.open(dir),
 
225
            bzrdir.BzrDir.open(self.get_url()).open_branch(unsupported=True))
 
226
        # unregister the format
 
227
        self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
 
228
            _mod_branch.BranchFormat.unregister_format, format)
 
229
        self.make_branch_and_tree('bar')
233
230
 
234
231
 
235
232
class TestBranchFormatRegistry(tests.TestCase):
337
334
        self.assertPathDoesNotExist('a/.bzr/branch/parent')
338
335
        self.assertEqual('http://example.com', branch.get_parent())
339
336
        branch.set_push_location('sftp://example.com')
340
 
        conf = branch.get_config_stack()
341
 
        self.assertEqual('sftp://example.com', conf.get('push_location'))
 
337
        config = branch.get_config()._get_branch_data_config()
 
338
        self.assertEqual('sftp://example.com',
 
339
                         config.get_user_option('push_location'))
342
340
        branch.set_bound_location('ftp://example.com')
343
341
        self.assertPathDoesNotExist('a/.bzr/branch/bound')
344
342
        self.assertEqual('ftp://example.com', branch.get_bound_location())
345
343
 
 
344
    def test_set_revision_history(self):
 
345
        builder = self.make_branch_builder('.', format=self.get_format_name())
 
346
        builder.build_snapshot('foo', None,
 
347
            [('add', ('', None, 'directory', None))],
 
348
            message='foo')
 
349
        builder.build_snapshot('bar', None, [], message='bar')
 
350
        branch = builder.get_branch()
 
351
        branch.lock_write()
 
352
        self.addCleanup(branch.unlock)
 
353
        self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
 
354
            branch.set_revision_history, ['foo', 'bar'])
 
355
        self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
 
356
                branch.set_revision_history, ['foo'])
 
357
        self.assertRaises(errors.NotLefthandHistory,
 
358
            self.applyDeprecated, symbol_versioning.deprecated_in((2, 4, 0)),
 
359
            branch.set_revision_history, ['bar'])
 
360
 
346
361
    def do_checkout_test(self, lightweight=False):
347
362
        tree = self.make_branch_and_tree('source',
348
363
            format=self.get_format_name_subtree())
376
391
    def test_light_checkout_with_references(self):
377
392
        self.do_checkout_test(lightweight=True)
378
393
 
 
394
    def test_set_push(self):
 
395
        branch = self.make_branch('source', format=self.get_format_name())
 
396
        branch.get_config().set_user_option('push_location', 'old',
 
397
            store=config.STORE_LOCATION)
 
398
        warnings = []
 
399
        def warning(*args):
 
400
            warnings.append(args[0] % args[1:])
 
401
        _warning = trace.warning
 
402
        trace.warning = warning
 
403
        try:
 
404
            branch.set_push_location('new')
 
405
        finally:
 
406
            trace.warning = _warning
 
407
        self.assertEqual(warnings[0], 'Value "new" is masked by "old" from '
 
408
                         'locations.conf')
 
409
 
379
410
 
380
411
class TestBranch6(TestBranch67, tests.TestCaseWithTransport):
381
412
 
452
483
 
453
484
    def make_branch(self, location, format=None):
454
485
        if format is None:
455
 
            format = controldir.format_registry.make_bzrdir('1.9')
 
486
            format = bzrdir.format_registry.make_bzrdir('1.9')
456
487
            format.set_branch_format(_mod_branch.BzrBranchFormat8())
457
488
        return tests.TestCaseWithTransport.make_branch(
458
489
            self, location, format=format)
539
570
        self.assertEqual(opened_branch.base, target_branch.base)
540
571
 
541
572
    def test_get_reference(self):
542
 
        """For a BranchReference, get_reference should return the location."""
 
573
        """For a BranchReference, get_reference should reutrn the location."""
543
574
        branch = self.make_branch('target')
544
575
        checkout = branch.create_checkout('checkout', lightweight=True)
545
576
        reference_url = branch.bzrdir.root_transport.abspath('') + '/'
555
586
    def test_constructor(self):
556
587
        """Check that creating a BranchHooks instance has the right defaults."""
557
588
        hooks = _mod_branch.BranchHooks()
 
589
        self.assertTrue("set_rh" in hooks, "set_rh not in %s" % hooks)
558
590
        self.assertTrue("post_push" in hooks, "post_push not in %s" % hooks)
559
591
        self.assertTrue("post_commit" in hooks, "post_commit not in %s" % hooks)
560
592
        self.assertTrue("pre_commit" in hooks, "pre_commit not in %s" % hooks)
624
656
    def setUp(self):
625
657
        super(TestBranchOptions, self).setUp()
626
658
        self.branch = self.make_branch('.')
627
 
        self.config_stack = self.branch.get_config_stack()
 
659
        self.config = self.branch.get_config()
628
660
 
629
661
    def check_append_revisions_only(self, expected_value, value=None):
630
662
        """Set append_revisions_only in config and check its interpretation."""
631
663
        if value is not None:
632
 
            self.config_stack.set('append_revisions_only', value)
 
664
            self.config.set_user_option('append_revisions_only', value)
633
665
        self.assertEqual(expected_value,
634
666
                         self.branch.get_append_revisions_only())
635
667
 
636
668
    def test_valid_append_revisions_only(self):
637
669
        self.assertEquals(None,
638
 
                          self.config_stack.get('append_revisions_only'))
 
670
                          self.config.get_user_option('append_revisions_only'))
639
671
        self.check_append_revisions_only(None)
640
672
        self.check_append_revisions_only(False, 'False')
641
673
        self.check_append_revisions_only(True, 'True')
653
685
        self.check_append_revisions_only(None, 'not-a-bool')
654
686
        self.assertLength(1, self.warnings)
655
687
        self.assertEqual(
656
 
            'Value "not-a-bool" is not valid for "append_revisions_only"',
 
688
            'Value "not-a-bool" is not a boolean for "append_revisions_only"',
657
689
            self.warnings[0])
658
690
 
659
 
    def test_use_fresh_values(self):
660
 
        copy = _mod_branch.Branch.open(self.branch.base)
661
 
        copy.lock_write()
662
 
        try:
663
 
            copy.get_config_stack().set('foo', 'bar')
664
 
        finally:
665
 
            copy.unlock()
666
 
        self.assertFalse(self.branch.is_locked())
667
 
        # Since the branch is locked, the option value won't be saved on disk
668
 
        # so trying to access the config of locked branch via another older
669
 
        # non-locked branch object pointing to the same branch is not supported
670
 
        self.assertEqual(None, self.branch.get_config_stack().get('foo'))
671
 
        # Using a newly created branch object works as expected
672
 
        fresh = _mod_branch.Branch.open(self.branch.base)
673
 
        self.assertEqual('bar', fresh.get_config_stack().get('foo'))
674
 
 
675
 
    def test_set_from_config_get_from_config_stack(self):
676
 
        self.branch.lock_write()
677
 
        self.addCleanup(self.branch.unlock)
678
 
        self.branch.get_config().set_user_option('foo', 'bar')
679
 
        result = self.branch.get_config_stack().get('foo')
680
 
        # https://bugs.launchpad.net/bzr/+bug/948344
681
 
        self.expectFailure('BranchStack uses cache after set_user_option',
682
 
                           self.assertEqual, 'bar', result)
683
 
 
684
 
    def test_set_from_config_stack_get_from_config(self):
685
 
        self.branch.lock_write()
686
 
        self.addCleanup(self.branch.unlock)
687
 
        self.branch.get_config_stack().set('foo', 'bar')
688
 
        # Since the branch is locked, the option value won't be saved on disk
689
 
        # so mixing get() and get_user_option() is broken by design.
690
 
        self.assertEqual(None,
691
 
                         self.branch.get_config().get_user_option('foo'))
692
 
 
693
 
    def test_set_delays_write_when_branch_is_locked(self):
694
 
        self.branch.lock_write()
695
 
        self.addCleanup(self.branch.unlock)
696
 
        self.branch.get_config_stack().set('foo', 'bar')
697
 
        copy = _mod_branch.Branch.open(self.branch.base)
698
 
        result = copy.get_config_stack().get('foo')
699
 
        # Accessing from a different branch object is like accessing from a
700
 
        # different process: the option has not been saved yet and the new
701
 
        # value cannot be seen.
702
 
        self.assertIs(None, result)
703
 
 
704
691
 
705
692
class TestPullResult(tests.TestCase):
706
693
 
 
694
    def test_pull_result_to_int(self):
 
695
        # to support old code, the pull result can be used as an int
 
696
        r = _mod_branch.PullResult()
 
697
        r.old_revno = 10
 
698
        r.new_revno = 20
 
699
        # this usage of results is not recommended for new code (because it
 
700
        # doesn't describe very well what happened), but for api stability
 
701
        # it's still supported
 
702
        self.assertEqual(self.applyDeprecated(
 
703
            symbol_versioning.deprecated_in((2, 3, 0)),
 
704
            r.__int__),
 
705
            10)
 
706
 
707
707
    def test_report_changed(self):
708
708
        r = _mod_branch.PullResult()
709
709
        r.old_revid = "old-revid"
713
713
        f = StringIO()
714
714
        r.report(f)
715
715
        self.assertEqual("Now on revision 20.\n", f.getvalue())
716
 
        self.assertEqual("Now on revision 20.\n", f.getvalue())
717
716
 
718
717
    def test_report_unchanged(self):
719
718
        r = _mod_branch.PullResult()
722
721
        f = StringIO()
723
722
        r.report(f)
724
723
        self.assertEqual("No revisions or tags to pull.\n", f.getvalue())
 
724