~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_branch.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-09-29 22:03:03 UTC
  • mfrom: (5416.2.6 jam-integration)
  • Revision ID: pqm@pqm.ubuntu.com-20100929220303-cr95h8iwtggco721
(mbp) Add 'break-lock --force'

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
    bzrdir,
30
30
    config,
31
31
    errors,
 
32
    symbol_versioning,
32
33
    tests,
33
34
    trace,
34
35
    transport,
86
87
        self.assertIsDirectory('.bzr/branch/lock/held', t)
87
88
 
88
89
    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()
 
90
        conf = config.LocationConfig.from_string('# comment\n', '.', save=True)
101
91
 
102
92
        branch = self.make_branch('.', format='knit')
103
93
        branch.set_push_location('foo')
106
96
                             "[%s]\n"
107
97
                             "push_location = foo\n"
108
98
                             "push_location:policy = norecurse\n" % local_path,
109
 
                             fn)
 
99
                             config.locations_config_filename())
110
100
 
111
101
    # TODO RBC 20051029 test getting a push location from a branch in a
112
102
    # recursive section - that is, it appends the branch name.
123
113
        """See BzrBranchFormat.get_format_string()."""
124
114
        return "Sample branch format."
125
115
 
126
 
    def initialize(self, a_bzrdir):
 
116
    def initialize(self, a_bzrdir, name=None):
127
117
        """Format 4 branches cannot be created."""
128
 
        t = a_bzrdir.get_branch_transport(self)
 
118
        t = a_bzrdir.get_branch_transport(self, name=name)
129
119
        t.put_bytes('format', self.get_format_string())
130
120
        return 'A branch'
131
121
 
132
122
    def is_supported(self):
133
123
        return False
134
124
 
135
 
    def open(self, transport, _found=False, ignore_fallbacks=False):
 
125
    def open(self, transport, name=None, _found=False, ignore_fallbacks=False):
136
126
        return "opened branch."
137
127
 
138
128
 
 
129
# Demonstrating how lazy loading is often implemented:
 
130
# A constant string is created.
 
131
SampleSupportedBranchFormatString = "Sample supported branch format."
 
132
 
 
133
# And the format class can then reference the constant to avoid skew.
 
134
class SampleSupportedBranchFormat(_mod_branch.BranchFormat):
 
135
    """A sample supported format."""
 
136
 
 
137
    def get_format_string(self):
 
138
        """See BzrBranchFormat.get_format_string()."""
 
139
        return SampleSupportedBranchFormatString
 
140
 
 
141
    def initialize(self, a_bzrdir, name=None):
 
142
        t = a_bzrdir.get_branch_transport(self, name=name)
 
143
        t.put_bytes('format', self.get_format_string())
 
144
        return 'A branch'
 
145
 
 
146
    def open(self, transport, name=None, _found=False, ignore_fallbacks=False):
 
147
        return "opened supported branch."
 
148
 
 
149
 
139
150
class TestBzrBranchFormat(tests.TestCaseWithTransport):
140
151
    """Tests for the BzrBranchFormat facility."""
141
152
 
152
163
            self.failUnless(isinstance(found_format, format.__class__))
153
164
        check_format(_mod_branch.BzrBranchFormat5(), "bar")
154
165
 
 
166
    def test_find_format_factory(self):
 
167
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
 
168
        SampleSupportedBranchFormat().initialize(dir)
 
169
        factory = _mod_branch.MetaDirBranchFormatFactory(
 
170
            SampleSupportedBranchFormatString,
 
171
            "bzrlib.tests.test_branch", "SampleSupportedBranchFormat")
 
172
        _mod_branch.BranchFormat.register_format(factory)
 
173
        self.addCleanup(_mod_branch.BranchFormat.unregister_format, factory)
 
174
        b = _mod_branch.Branch.open(self.get_url())
 
175
        self.assertEqual(b, "opened supported branch.")
 
176
 
155
177
    def test_find_format_not_branch(self):
156
178
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
157
179
        self.assertRaises(errors.NotBranchError,
186
208
        self.make_branch_and_tree('bar')
187
209
 
188
210
 
 
211
#Used by TestMetaDirBranchFormatFactory 
 
212
FakeLazyFormat = None
 
213
 
 
214
 
 
215
class TestMetaDirBranchFormatFactory(tests.TestCase):
 
216
 
 
217
    def test_get_format_string_does_not_load(self):
 
218
        """Formats have a static format string."""
 
219
        factory = _mod_branch.MetaDirBranchFormatFactory("yo", None, None)
 
220
        self.assertEqual("yo", factory.get_format_string())
 
221
 
 
222
    def test_call_loads(self):
 
223
        # __call__ is used by the network_format_registry interface to get a
 
224
        # Format.
 
225
        global FakeLazyFormat
 
226
        del FakeLazyFormat
 
227
        factory = _mod_branch.MetaDirBranchFormatFactory(None,
 
228
            "bzrlib.tests.test_branch", "FakeLazyFormat")
 
229
        self.assertRaises(AttributeError, factory)
 
230
 
 
231
    def test_call_returns_call_of_referenced_object(self):
 
232
        global FakeLazyFormat
 
233
        FakeLazyFormat = lambda:'called'
 
234
        factory = _mod_branch.MetaDirBranchFormatFactory(None,
 
235
            "bzrlib.tests.test_branch", "FakeLazyFormat")
 
236
        self.assertEqual('called', factory())
 
237
 
 
238
 
189
239
class TestBranch67(object):
190
240
    """Common tests for both branch 6 and 7 which are mostly the same."""
191
241
 
438
488
        t.mkdir('branch')
439
489
        branch_dir = bzrdirformat.initialize(self.get_url('branch'))
440
490
        made_branch = _mod_branch.BranchReferenceFormat().initialize(
441
 
            branch_dir, target_branch)
 
491
            branch_dir, target_branch=target_branch)
442
492
        self.assertEqual(made_branch.base, target_branch.base)
443
493
        opened_branch = branch_dir.open_branch()
444
494
        self.assertEqual(opened_branch.base, target_branch.base)
455
505
            _mod_branch.BranchReferenceFormat().get_reference(checkout.bzrdir))
456
506
 
457
507
 
458
 
class TestHooks(tests.TestCase):
 
508
class TestHooks(tests.TestCaseWithTransport):
459
509
 
460
510
    def test_constructor(self):
461
511
        """Check that creating a BranchHooks instance has the right defaults."""
469
519
                        "post_uncommit not in %s" % hooks)
470
520
        self.assertTrue("post_change_branch_tip" in hooks,
471
521
                        "post_change_branch_tip not in %s" % hooks)
 
522
        self.assertTrue("post_branch_init" in hooks,
 
523
                        "post_branch_init not in %s" % hooks)
 
524
        self.assertTrue("post_switch" in hooks,
 
525
                        "post_switch not in %s" % hooks)
472
526
 
473
527
    def test_installed_hooks_are_BranchHooks(self):
474
528
        """The installed hooks object should be a BranchHooks."""
476
530
        self.assertIsInstance(self._preserved_hooks[_mod_branch.Branch][1],
477
531
                              _mod_branch.BranchHooks)
478
532
 
 
533
    def test_post_branch_init_hook(self):
 
534
        calls = []
 
535
        _mod_branch.Branch.hooks.install_named_hook('post_branch_init',
 
536
            calls.append, None)
 
537
        self.assertLength(0, calls)
 
538
        branch = self.make_branch('a')
 
539
        self.assertLength(1, calls)
 
540
        params = calls[0]
 
541
        self.assertIsInstance(params, _mod_branch.BranchInitHookParams)
 
542
        self.assertTrue(hasattr(params, 'bzrdir'))
 
543
        self.assertTrue(hasattr(params, 'branch'))
 
544
 
 
545
    def test_post_branch_init_hook_repr(self):
 
546
        param_reprs = []
 
547
        _mod_branch.Branch.hooks.install_named_hook('post_branch_init',
 
548
            lambda params: param_reprs.append(repr(params)), None)
 
549
        branch = self.make_branch('a')
 
550
        self.assertLength(1, param_reprs)
 
551
        param_repr = param_reprs[0]
 
552
        self.assertStartsWith(param_repr, '<BranchInitHookParams of ')
 
553
 
 
554
    def test_post_switch_hook(self):
 
555
        from bzrlib import switch
 
556
        calls = []
 
557
        _mod_branch.Branch.hooks.install_named_hook('post_switch',
 
558
            calls.append, None)
 
559
        tree = self.make_branch_and_tree('branch-1')
 
560
        self.build_tree(['branch-1/file-1'])
 
561
        tree.add('file-1')
 
562
        tree.commit('rev1')
 
563
        to_branch = tree.bzrdir.sprout('branch-2').open_branch()
 
564
        self.build_tree(['branch-1/file-2'])
 
565
        tree.add('file-2')
 
566
        tree.remove('file-1')
 
567
        tree.commit('rev2')
 
568
        checkout = tree.branch.create_checkout('checkout')
 
569
        self.assertLength(0, calls)
 
570
        switch.switch(checkout.bzrdir, to_branch)
 
571
        self.assertLength(1, calls)
 
572
        params = calls[0]
 
573
        self.assertIsInstance(params, _mod_branch.SwitchHookParams)
 
574
        self.assertTrue(hasattr(params, 'to_branch'))
 
575
        self.assertTrue(hasattr(params, 'revision_id'))
 
576
 
 
577
 
 
578
class TestBranchOptions(tests.TestCaseWithTransport):
 
579
 
 
580
    def setUp(self):
 
581
        super(TestBranchOptions, self).setUp()
 
582
        self.branch = self.make_branch('.')
 
583
        self.config = self.branch.get_config()
 
584
 
 
585
    def check_append_revisions_only(self, expected_value, value=None):
 
586
        """Set append_revisions_only in config and check its interpretation."""
 
587
        if value is not None:
 
588
            self.config.set_user_option('append_revisions_only', value)
 
589
        self.assertEqual(expected_value,
 
590
                         self.branch._get_append_revisions_only())
 
591
 
 
592
    def test_valid_append_revisions_only(self):
 
593
        self.assertEquals(None,
 
594
                          self.config.get_user_option('append_revisions_only'))
 
595
        self.check_append_revisions_only(None)
 
596
        self.check_append_revisions_only(False, 'False')
 
597
        self.check_append_revisions_only(True, 'True')
 
598
        # The following values will cause compatibility problems on projects
 
599
        # using older bzr versions (<2.2) but are accepted
 
600
        self.check_append_revisions_only(False, 'false')
 
601
        self.check_append_revisions_only(True, 'true')
 
602
 
 
603
    def test_invalid_append_revisions_only(self):
 
604
        """Ensure warning is noted on invalid settings"""
 
605
        self.warnings = []
 
606
        def warning(*args):
 
607
            self.warnings.append(args[0] % args[1:])
 
608
        self.overrideAttr(trace, 'warning', warning)
 
609
        self.check_append_revisions_only(None, 'not-a-bool')
 
610
        self.assertLength(1, self.warnings)
 
611
        self.assertEqual(
 
612
            'Value "not-a-bool" is not a boolean for "append_revisions_only"',
 
613
            self.warnings[0])
 
614
 
479
615
 
480
616
class TestPullResult(tests.TestCase):
481
617
 
487
623
        # this usage of results is not recommended for new code (because it
488
624
        # doesn't describe very well what happened), but for api stability
489
625
        # it's still supported
490
 
        a = "%d revisions pulled" % r
491
 
        self.assertEqual(a, "10 revisions pulled")
 
626
        self.assertEqual(self.applyDeprecated(
 
627
            symbol_versioning.deprecated_in((2, 3, 0)),
 
628
            r.__int__),
 
629
            10)
492
630
 
493
631
    def test_report_changed(self):
494
632
        r = _mod_branch.PullResult()