~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_branch.py

  • Committer: Martin Pool
  • Date: 2010-04-01 04:41:18 UTC
  • mto: This revision was merged to the branch mainline in revision 5128.
  • Revision ID: mbp@sourcefrog.net-20100401044118-shyctqc02ob08ngz
ignore .testrepository

Show diffs side-by-side

added added

removed removed

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