~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_branch.py

  • Committer: Alexander Belchenko
  • Date: 2007-04-19 19:28:39 UTC
  • mto: This revision was merged to the branch mainline in revision 2439.
  • Revision ID: bialix@ukr.net-20070419192839-p964uu06n6vbjgrt
changes after John's review

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2007 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
30
30
    errors,
31
31
    urlutils,
32
32
    )
33
 
import bzrlib.branch
34
 
from bzrlib.branch import (BzrBranch5, 
35
 
                           BzrBranchFormat5)
 
33
from bzrlib.branch import (
 
34
    Branch,
 
35
    BranchHooks,
 
36
    BranchFormat,
 
37
    BranchReferenceFormat,
 
38
    BzrBranch5,
 
39
    BzrBranchFormat5,
 
40
    PullResult,
 
41
    )
36
42
from bzrlib.bzrdir import (BzrDirMetaFormat1, BzrDirMeta1, 
37
43
                           BzrDir, BzrDirFormat)
38
44
from bzrlib.errors import (NotBranchError,
47
53
class TestDefaultFormat(TestCase):
48
54
 
49
55
    def test_get_set_default_format(self):
50
 
        old_format = bzrlib.branch.BranchFormat.get_default_format()
 
56
        old_format = BranchFormat.get_default_format()
51
57
        # default is 5
52
 
        self.assertTrue(isinstance(old_format, bzrlib.branch.BzrBranchFormat5))
53
 
        bzrlib.branch.BranchFormat.set_default_format(SampleBranchFormat())
 
58
        self.assertTrue(isinstance(old_format, BzrBranchFormat5))
 
59
        BranchFormat.set_default_format(SampleBranchFormat())
54
60
        try:
55
61
            # the default branch format is used by the meta dir format
56
62
            # which is not the default bzrdir format at this point
58
64
            result = dir.create_branch()
59
65
            self.assertEqual(result, 'A branch')
60
66
        finally:
61
 
            bzrlib.branch.BranchFormat.set_default_format(old_format)
62
 
        self.assertEqual(old_format, bzrlib.branch.BranchFormat.get_default_format())
 
67
            BranchFormat.set_default_format(old_format)
 
68
        self.assertEqual(old_format, BranchFormat.get_default_format())
63
69
 
64
70
 
65
71
class TestBranchFormat5(TestCaseWithTransport):
99
105
    # recursive section - that is, it appends the branch name.
100
106
 
101
107
 
102
 
class SampleBranchFormat(bzrlib.branch.BranchFormat):
 
108
class SampleBranchFormat(BranchFormat):
103
109
    """A sample format
104
110
 
105
111
    this format is initializable, unsupported to aid in testing the 
135
141
            dir = format._matchingbzrdir.initialize(url)
136
142
            dir.create_repository()
137
143
            format.initialize(dir)
138
 
            found_format = bzrlib.branch.BranchFormat.find_format(dir)
 
144
            found_format = BranchFormat.find_format(dir)
139
145
            self.failUnless(isinstance(found_format, format.__class__))
140
 
        check_format(bzrlib.branch.BzrBranchFormat5(), "bar")
 
146
        check_format(BzrBranchFormat5(), "bar")
141
147
        
142
148
    def test_find_format_not_branch(self):
143
149
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
144
150
        self.assertRaises(NotBranchError,
145
 
                          bzrlib.branch.BranchFormat.find_format,
 
151
                          BranchFormat.find_format,
146
152
                          dir)
147
153
 
148
154
    def test_find_format_unknown_format(self):
149
155
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
150
156
        SampleBranchFormat().initialize(dir)
151
157
        self.assertRaises(UnknownFormatError,
152
 
                          bzrlib.branch.BranchFormat.find_format,
 
158
                          BranchFormat.find_format,
153
159
                          dir)
154
160
 
155
161
    def test_register_unregister_format(self):
159
165
        # make a branch
160
166
        format.initialize(dir)
161
167
        # register a format for it.
162
 
        bzrlib.branch.BranchFormat.register_format(format)
 
168
        BranchFormat.register_format(format)
163
169
        # which branch.Open will refuse (not supported)
164
 
        self.assertRaises(UnsupportedFormatError, bzrlib.branch.Branch.open, self.get_url())
 
170
        self.assertRaises(UnsupportedFormatError, Branch.open, self.get_url())
165
171
        self.make_branch_and_tree('foo')
166
172
        # but open_downlevel will work
167
173
        self.assertEqual(format.open(dir), bzrdir.BzrDir.open(self.get_url()).open_branch(unsupported=True))
168
174
        # unregister the format
169
 
        bzrlib.branch.BranchFormat.unregister_format(format)
 
175
        BranchFormat.unregister_format(format)
170
176
        self.make_branch_and_tree('bar')
171
177
 
172
 
    def test_checkout_format(self):
173
 
        branch = self.make_repository('repository', shared=True)
174
 
        branch = self.make_branch('repository/branch',
175
 
            format='metaweave')
176
 
        tree = branch.create_checkout('checkout')
177
 
        self.assertIs(tree.branch.__class__, _mod_branch.BzrBranch5)
178
 
 
179
178
 
180
179
class TestBranch6(TestCaseWithTransport):
181
180
 
184
183
        format.set_branch_format(_mod_branch.BzrBranchFormat6())
185
184
        branch = self.make_branch('a', format=format)
186
185
        self.assertIsInstance(branch, _mod_branch.BzrBranch6)
187
 
        branch = self.make_branch('b', format='experimental-branch6')
 
186
        branch = self.make_branch('b', format='dirstate-tags')
188
187
        self.assertIsInstance(branch, _mod_branch.BzrBranch6)
189
188
        branch = _mod_branch.Branch.open('a')
190
189
        self.assertIsInstance(branch, _mod_branch.BzrBranch6)
191
190
 
192
191
    def test_layout(self):
193
 
        branch = self.make_branch('a', format='experimental-branch6')
 
192
        branch = self.make_branch('a', format='dirstate-tags')
194
193
        self.failUnlessExists('a/.bzr/branch/last-revision')
195
194
        self.failIfExists('a/.bzr/branch/revision-history')
196
195
 
197
196
    def test_config(self):
198
197
        """Ensure that all configuration data is stored in the branch"""
199
 
        branch = self.make_branch('a', format='experimental-branch6')
 
198
        branch = self.make_branch('a', format='dirstate-tags')
200
199
        branch.set_parent('http://bazaar-vcs.org')
201
200
        self.failIfExists('a/.bzr/branch/parent')
202
201
        self.assertEqual('http://bazaar-vcs.org', branch.get_parent())
210
209
 
211
210
    def test_set_revision_history(self):
212
211
        tree = self.make_branch_and_memory_tree('.',
213
 
            format='experimental-branch6')
 
212
            format='dirstate-tags')
214
213
        tree.lock_write()
215
214
        try:
216
215
            tree.add('.')
225
224
 
226
225
    def test_append_revision(self):
227
226
        tree = self.make_branch_and_tree('branch1',
228
 
            format='experimental-branch6')
 
227
            format='dirstate-tags')
229
228
        tree.lock_write()
230
229
        try:
231
 
            tree.add('.')
232
230
            tree.commit('foo', rev_id='foo')
233
231
            tree.commit('bar', rev_id='bar')
234
232
            tree.commit('baz', rev_id='baz')
250
248
        finally:
251
249
            tree.unlock()
252
250
 
 
251
    def do_checkout_test(self, lightweight=False):
 
252
        tree = self.make_branch_and_tree('source', format='dirstate-with-subtree')
 
253
        subtree = self.make_branch_and_tree('source/subtree',
 
254
            format='dirstate-with-subtree')
 
255
        subsubtree = self.make_branch_and_tree('source/subtree/subsubtree',
 
256
            format='dirstate-with-subtree')
 
257
        self.build_tree(['source/subtree/file',
 
258
                         'source/subtree/subsubtree/file'])
 
259
        subsubtree.add('file')
 
260
        subtree.add('file')
 
261
        subtree.add_reference(subsubtree)
 
262
        tree.add_reference(subtree)
 
263
        tree.commit('a revision')
 
264
        subtree.commit('a subtree file')
 
265
        subsubtree.commit('a subsubtree file')
 
266
        tree.branch.create_checkout('target', lightweight=lightweight)
 
267
        self.failUnlessExists('target')
 
268
        self.failUnlessExists('target/subtree')
 
269
        self.failUnlessExists('target/subtree/file')
 
270
        self.failUnlessExists('target/subtree/subsubtree/file')
 
271
        subbranch = _mod_branch.Branch.open('target/subtree/subsubtree')
 
272
        if lightweight:
 
273
            self.assertEndsWith(subbranch.base, 'source/subtree/subsubtree/')
 
274
        else:
 
275
            self.assertEndsWith(subbranch.base, 'target/subtree/subsubtree/')
 
276
 
 
277
 
 
278
    def test_checkout_with_references(self):
 
279
        self.do_checkout_test()
 
280
 
 
281
    def test_light_checkout_with_references(self):
 
282
        self.do_checkout_test(lightweight=True)
253
283
 
254
284
class TestBranchReference(TestCaseWithTransport):
255
285
    """Tests for the branch reference facility."""
263
293
        target_branch = dir.create_branch()
264
294
        t.mkdir('branch')
265
295
        branch_dir = bzrdirformat.initialize(self.get_url('branch'))
266
 
        made_branch = bzrlib.branch.BranchReferenceFormat().initialize(branch_dir, target_branch)
 
296
        made_branch = BranchReferenceFormat().initialize(branch_dir, target_branch)
267
297
        self.assertEqual(made_branch.base, target_branch.base)
268
298
        opened_branch = branch_dir.open_branch()
269
299
        self.assertEqual(opened_branch.base, target_branch.base)
273
303
 
274
304
    def test_constructor(self):
275
305
        """Check that creating a BranchHooks instance has the right defaults."""
276
 
        hooks = bzrlib.branch.BranchHooks()
 
306
        hooks = BranchHooks()
277
307
        self.assertTrue("set_rh" in hooks, "set_rh not in %s" % hooks)
278
308
        self.assertTrue("post_push" in hooks, "post_push not in %s" % hooks)
279
309
        self.assertTrue("post_commit" in hooks, "post_commit not in %s" % hooks)
283
313
    def test_installed_hooks_are_BranchHooks(self):
284
314
        """The installed hooks object should be a BranchHooks."""
285
315
        # the installed hooks are saved in self._preserved_hooks.
286
 
        self.assertIsInstance(self._preserved_hooks, bzrlib.branch.BranchHooks)
 
316
        self.assertIsInstance(self._preserved_hooks[_mod_branch.Branch], BranchHooks)
287
317
 
288
318
    def test_install_hook_raises_unknown_hook(self):
289
319
        """install_hook should raise UnknownHook if a hook is unknown."""
290
 
        hooks = bzrlib.branch.BranchHooks()
 
320
        hooks = BranchHooks()
291
321
        self.assertRaises(UnknownHook, hooks.install_hook, 'silly', None)
292
322
 
293
323
    def test_install_hook_appends_known_hook(self):
294
324
        """install_hook should append the callable for known hooks."""
295
 
        hooks = bzrlib.branch.BranchHooks()
 
325
        hooks = BranchHooks()
296
326
        hooks.install_hook('set_rh', None)
297
327
        self.assertEqual(hooks['set_rh'], [None])
 
328
 
 
329
 
 
330
class TestPullResult(TestCase):
 
331
 
 
332
    def test_pull_result_to_int(self):
 
333
        # to support old code, the pull result can be used as an int
 
334
        r = PullResult()
 
335
        r.old_revno = 10
 
336
        r.new_revno = 20
 
337
        # this usage of results is not recommended for new code (because it
 
338
        # doesn't describe very well what happened), but for api stability
 
339
        # it's still supported
 
340
        a = "%d revisions pulled" % r
 
341
        self.assertEqual(a, "10 revisions pulled")