~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_branch.py

  • Committer: Robert Collins
  • Date: 2007-07-04 08:08:13 UTC
  • mfrom: (2572 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2587.
  • Revision ID: robertc@robertcollins.net-20070704080813-wzebx0r88fvwj5rq
Merge bzr.dev.

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
24
24
 
25
25
from StringIO import StringIO
26
26
 
27
 
import bzrlib.branch
28
 
from bzrlib.branch import (BzrBranch5, 
29
 
                           BzrBranchFormat5)
30
 
import bzrlib.bzrdir as bzrdir
 
27
from bzrlib import (
 
28
    branch as _mod_branch,
 
29
    bzrdir,
 
30
    errors,
 
31
    urlutils,
 
32
    )
 
33
from bzrlib.branch import (
 
34
    Branch,
 
35
    BranchHooks,
 
36
    BranchFormat,
 
37
    BranchReferenceFormat,
 
38
    BzrBranch5,
 
39
    BzrBranchFormat5,
 
40
    PullResult,
 
41
    )
31
42
from bzrlib.bzrdir import (BzrDirMetaFormat1, BzrDirMeta1, 
32
43
                           BzrDir, BzrDirFormat)
33
44
from bzrlib.errors import (NotBranchError,
34
45
                           UnknownFormatError,
 
46
                           UnknownHook,
35
47
                           UnsupportedFormatError,
36
48
                           )
37
49
 
41
53
class TestDefaultFormat(TestCase):
42
54
 
43
55
    def test_get_set_default_format(self):
44
 
        old_format = bzrlib.branch.BranchFormat.get_default_format()
 
56
        old_format = BranchFormat.get_default_format()
45
57
        # default is 5
46
 
        self.assertTrue(isinstance(old_format, bzrlib.branch.BzrBranchFormat5))
47
 
        bzrlib.branch.BranchFormat.set_default_format(SampleBranchFormat())
 
58
        self.assertTrue(isinstance(old_format, BzrBranchFormat5))
 
59
        BranchFormat.set_default_format(SampleBranchFormat())
48
60
        try:
49
61
            # the default branch format is used by the meta dir format
50
62
            # which is not the default bzrdir format at this point
52
64
            result = dir.create_branch()
53
65
            self.assertEqual(result, 'A branch')
54
66
        finally:
55
 
            bzrlib.branch.BranchFormat.set_default_format(old_format)
56
 
        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())
57
69
 
58
70
 
59
71
class TestBranchFormat5(TestCaseWithTransport):
76
88
        finally:
77
89
            branch.unlock()
78
90
 
79
 
 
80
 
class SampleBranchFormat(bzrlib.branch.BranchFormat):
 
91
    def test_set_push_location(self):
 
92
        from bzrlib.config import (locations_config_filename,
 
93
                                   ensure_config_dir_exists)
 
94
        ensure_config_dir_exists()
 
95
        fn = locations_config_filename()
 
96
        branch = self.make_branch('.', format='knit')
 
97
        branch.set_push_location('foo')
 
98
        local_path = urlutils.local_path_from_url(branch.base[:-1])
 
99
        self.assertFileEqual("[%s]\n"
 
100
                             "push_location = foo\n"
 
101
                             "push_location:policy = norecurse" % local_path,
 
102
                             fn)
 
103
 
 
104
    # TODO RBC 20051029 test getting a push location from a branch in a
 
105
    # recursive section - that is, it appends the branch name.
 
106
 
 
107
 
 
108
class SampleBranchFormat(BranchFormat):
81
109
    """A sample format
82
110
 
83
111
    this format is initializable, unsupported to aid in testing the 
113
141
            dir = format._matchingbzrdir.initialize(url)
114
142
            dir.create_repository()
115
143
            format.initialize(dir)
116
 
            found_format = bzrlib.branch.BranchFormat.find_format(dir)
 
144
            found_format = BranchFormat.find_format(dir)
117
145
            self.failUnless(isinstance(found_format, format.__class__))
118
 
        check_format(bzrlib.branch.BzrBranchFormat5(), "bar")
 
146
        check_format(BzrBranchFormat5(), "bar")
119
147
        
120
148
    def test_find_format_not_branch(self):
121
149
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
122
150
        self.assertRaises(NotBranchError,
123
 
                          bzrlib.branch.BranchFormat.find_format,
 
151
                          BranchFormat.find_format,
124
152
                          dir)
125
153
 
126
154
    def test_find_format_unknown_format(self):
127
155
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
128
156
        SampleBranchFormat().initialize(dir)
129
157
        self.assertRaises(UnknownFormatError,
130
 
                          bzrlib.branch.BranchFormat.find_format,
 
158
                          BranchFormat.find_format,
131
159
                          dir)
132
160
 
133
161
    def test_register_unregister_format(self):
137
165
        # make a branch
138
166
        format.initialize(dir)
139
167
        # register a format for it.
140
 
        bzrlib.branch.BranchFormat.register_format(format)
 
168
        BranchFormat.register_format(format)
141
169
        # which branch.Open will refuse (not supported)
142
 
        self.assertRaises(UnsupportedFormatError, bzrlib.branch.Branch.open, self.get_url())
 
170
        self.assertRaises(UnsupportedFormatError, Branch.open, self.get_url())
 
171
        self.make_branch_and_tree('foo')
143
172
        # but open_downlevel will work
144
173
        self.assertEqual(format.open(dir), bzrdir.BzrDir.open(self.get_url()).open_branch(unsupported=True))
145
174
        # unregister the format
146
 
        bzrlib.branch.BranchFormat.unregister_format(format)
147
 
 
 
175
        BranchFormat.unregister_format(format)
 
176
        self.make_branch_and_tree('bar')
 
177
 
 
178
 
 
179
class TestBranch6(TestCaseWithTransport):
 
180
 
 
181
    def test_creation(self):
 
182
        format = BzrDirMetaFormat1()
 
183
        format.set_branch_format(_mod_branch.BzrBranchFormat6())
 
184
        branch = self.make_branch('a', format=format)
 
185
        self.assertIsInstance(branch, _mod_branch.BzrBranch6)
 
186
        branch = self.make_branch('b', format='dirstate-tags')
 
187
        self.assertIsInstance(branch, _mod_branch.BzrBranch6)
 
188
        branch = _mod_branch.Branch.open('a')
 
189
        self.assertIsInstance(branch, _mod_branch.BzrBranch6)
 
190
 
 
191
    def test_layout(self):
 
192
        branch = self.make_branch('a', format='dirstate-tags')
 
193
        self.failUnlessExists('a/.bzr/branch/last-revision')
 
194
        self.failIfExists('a/.bzr/branch/revision-history')
 
195
 
 
196
    def test_config(self):
 
197
        """Ensure that all configuration data is stored in the branch"""
 
198
        branch = self.make_branch('a', format='dirstate-tags')
 
199
        branch.set_parent('http://bazaar-vcs.org')
 
200
        self.failIfExists('a/.bzr/branch/parent')
 
201
        self.assertEqual('http://bazaar-vcs.org', branch.get_parent())
 
202
        branch.set_push_location('sftp://bazaar-vcs.org')
 
203
        config = branch.get_config()._get_branch_data_config()
 
204
        self.assertEqual('sftp://bazaar-vcs.org',
 
205
                         config.get_user_option('push_location'))
 
206
        branch.set_bound_location('ftp://bazaar-vcs.org')
 
207
        self.failIfExists('a/.bzr/branch/bound')
 
208
        self.assertEqual('ftp://bazaar-vcs.org', branch.get_bound_location())
 
209
 
 
210
    def test_set_revision_history(self):
 
211
        tree = self.make_branch_and_memory_tree('.',
 
212
            format='dirstate-tags')
 
213
        tree.lock_write()
 
214
        try:
 
215
            tree.add('.')
 
216
            tree.commit('foo', rev_id='foo')
 
217
            tree.commit('bar', rev_id='bar')
 
218
            tree.branch.set_revision_history(['foo', 'bar'])
 
219
            tree.branch.set_revision_history(['foo'])
 
220
            self.assertRaises(errors.NotLefthandHistory,
 
221
                              tree.branch.set_revision_history, ['bar'])
 
222
        finally:
 
223
            tree.unlock()
 
224
 
 
225
    def test_append_revision(self):
 
226
        tree = self.make_branch_and_tree('branch1',
 
227
            format='dirstate-tags')
 
228
        tree.lock_write()
 
229
        try:
 
230
            tree.commit('foo', rev_id='foo')
 
231
            tree.commit('bar', rev_id='bar')
 
232
            tree.commit('baz', rev_id='baz')
 
233
            tree.set_last_revision('bar')
 
234
            tree.branch.set_last_revision_info(2, 'bar')
 
235
            tree.commit('qux', rev_id='qux')
 
236
            tree.add_parent_tree_id('baz')
 
237
            tree.commit('qux', rev_id='quxx')
 
238
            tree.branch.set_last_revision_info(0, 'null:')
 
239
            self.assertRaises(errors.NotLeftParentDescendant,
 
240
                              tree.branch.append_revision, 'bar')
 
241
            tree.branch.append_revision('foo')
 
242
            self.assertRaises(errors.NotLeftParentDescendant,
 
243
                              tree.branch.append_revision, 'baz')
 
244
            tree.branch.append_revision('bar')
 
245
            tree.branch.append_revision('baz')
 
246
            self.assertRaises(errors.NotLeftParentDescendant,
 
247
                              tree.branch.append_revision, 'quxx')
 
248
        finally:
 
249
            tree.unlock()
 
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)
148
283
 
149
284
class TestBranchReference(TestCaseWithTransport):
150
285
    """Tests for the branch reference facility."""
158
293
        target_branch = dir.create_branch()
159
294
        t.mkdir('branch')
160
295
        branch_dir = bzrdirformat.initialize(self.get_url('branch'))
161
 
        made_branch = bzrlib.branch.BranchReferenceFormat().initialize(branch_dir, target_branch)
 
296
        made_branch = BranchReferenceFormat().initialize(branch_dir, target_branch)
162
297
        self.assertEqual(made_branch.base, target_branch.base)
163
298
        opened_branch = branch_dir.open_branch()
164
299
        self.assertEqual(opened_branch.base, target_branch.base)
 
300
 
 
301
    def test_get_reference(self):
 
302
        """For a BranchReference, get_reference should reutrn the location."""
 
303
        branch = self.make_branch('target')
 
304
        checkout = branch.create_checkout('checkout', lightweight=True)
 
305
        reference_url = branch.bzrdir.root_transport.abspath('') + '/'
 
306
        # if the api for create_checkout changes to return different checkout types
 
307
        # then this file read will fail.
 
308
        self.assertFileEqual(reference_url, 'checkout/.bzr/branch/location')
 
309
        self.assertEqual(reference_url,
 
310
            _mod_branch.BranchReferenceFormat().get_reference(checkout.bzrdir))
 
311
 
 
312
 
 
313
class TestHooks(TestCase):
 
314
 
 
315
    def test_constructor(self):
 
316
        """Check that creating a BranchHooks instance has the right defaults."""
 
317
        hooks = BranchHooks()
 
318
        self.assertTrue("set_rh" in hooks, "set_rh not in %s" % hooks)
 
319
        self.assertTrue("post_push" in hooks, "post_push not in %s" % hooks)
 
320
        self.assertTrue("post_commit" in hooks, "post_commit not in %s" % hooks)
 
321
        self.assertTrue("post_pull" in hooks, "post_pull not in %s" % hooks)
 
322
        self.assertTrue("post_uncommit" in hooks, "post_uncommit not in %s" % hooks)
 
323
 
 
324
    def test_installed_hooks_are_BranchHooks(self):
 
325
        """The installed hooks object should be a BranchHooks."""
 
326
        # the installed hooks are saved in self._preserved_hooks.
 
327
        self.assertIsInstance(self._preserved_hooks[_mod_branch.Branch], BranchHooks)
 
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")