~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_branch.py

Add bzrlib.pyutils, which has get_named_object, a wrapper around __import__.

This is used to replace various ad hoc implementations of the same logic,
notably the version used in registry's _LazyObjectGetter which had a bug when
getting a module without also getting a member.  And of course, this new
function has unit tests, unlike the replaced code.

This also adds a KnownHooksRegistry subclass to provide a more natural home for
some other logic.

I'm not thrilled about the name of the new module or the new functions, but it's
hard to think of good names for such generic functionality.

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.
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
 
492
542
        self.assertTrue(hasattr(params, 'bzrdir'))
493
543
        self.assertTrue(hasattr(params, 'branch'))
494
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
 
495
554
    def test_post_switch_hook(self):
496
555
        from bzrlib import switch
497
556
        calls = []
564
623
        # this usage of results is not recommended for new code (because it
565
624
        # doesn't describe very well what happened), but for api stability
566
625
        # it's still supported
567
 
        a = "%d revisions pulled" % r
568
 
        self.assertEqual(a, "10 revisions pulled")
 
626
        self.assertEqual(self.applyDeprecated(
 
627
            symbol_versioning.deprecated_in((2, 3, 0)),
 
628
            r.__int__),
 
629
            10)
569
630
 
570
631
    def test_report_changed(self):
571
632
        r = _mod_branch.PullResult()