~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_branch.py

  • Committer: Andrew Bennetts
  • Date: 2007-03-28 07:08:42 UTC
  • mfrom: (2380 +trunk)
  • mto: (2018.5.146 hpss)
  • mto: This revision was merged to the branch mainline in revision 2414.
  • Revision ID: andrew.bennetts@canonical.com-20070328070842-r843houy668oxb9o
Merge from 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,
42
53
class TestDefaultFormat(TestCase):
43
54
 
44
55
    def test_get_set_default_format(self):
45
 
        old_format = bzrlib.branch.BranchFormat.get_default_format()
 
56
        old_format = BranchFormat.get_default_format()
46
57
        # default is 5
47
 
        self.assertTrue(isinstance(old_format, bzrlib.branch.BzrBranchFormat5))
48
 
        bzrlib.branch.BranchFormat.set_default_format(SampleBranchFormat())
 
58
        self.assertTrue(isinstance(old_format, BzrBranchFormat5))
 
59
        BranchFormat.set_default_format(SampleBranchFormat())
49
60
        try:
50
61
            # the default branch format is used by the meta dir format
51
62
            # which is not the default bzrdir format at this point
53
64
            result = dir.create_branch()
54
65
            self.assertEqual(result, 'A branch')
55
66
        finally:
56
 
            bzrlib.branch.BranchFormat.set_default_format(old_format)
57
 
        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())
58
69
 
59
70
 
60
71
class TestBranchFormat5(TestCaseWithTransport):
77
88
        finally:
78
89
            branch.unlock()
79
90
 
80
 
 
81
 
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):
82
109
    """A sample format
83
110
 
84
111
    this format is initializable, unsupported to aid in testing the 
114
141
            dir = format._matchingbzrdir.initialize(url)
115
142
            dir.create_repository()
116
143
            format.initialize(dir)
117
 
            found_format = bzrlib.branch.BranchFormat.find_format(dir)
 
144
            found_format = BranchFormat.find_format(dir)
118
145
            self.failUnless(isinstance(found_format, format.__class__))
119
 
        check_format(bzrlib.branch.BzrBranchFormat5(), "bar")
 
146
        check_format(BzrBranchFormat5(), "bar")
120
147
        
121
148
    def test_find_format_not_branch(self):
122
149
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
123
150
        self.assertRaises(NotBranchError,
124
 
                          bzrlib.branch.BranchFormat.find_format,
 
151
                          BranchFormat.find_format,
125
152
                          dir)
126
153
 
127
154
    def test_find_format_unknown_format(self):
128
155
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
129
156
        SampleBranchFormat().initialize(dir)
130
157
        self.assertRaises(UnknownFormatError,
131
 
                          bzrlib.branch.BranchFormat.find_format,
 
158
                          BranchFormat.find_format,
132
159
                          dir)
133
160
 
134
161
    def test_register_unregister_format(self):
138
165
        # make a branch
139
166
        format.initialize(dir)
140
167
        # register a format for it.
141
 
        bzrlib.branch.BranchFormat.register_format(format)
 
168
        BranchFormat.register_format(format)
142
169
        # which branch.Open will refuse (not supported)
143
 
        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')
144
172
        # but open_downlevel will work
145
173
        self.assertEqual(format.open(dir), bzrdir.BzrDir.open(self.get_url()).open_branch(unsupported=True))
146
174
        # unregister the format
147
 
        bzrlib.branch.BranchFormat.unregister_format(format)
148
 
 
 
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)
149
283
 
150
284
class TestBranchReference(TestCaseWithTransport):
151
285
    """Tests for the branch reference facility."""
159
293
        target_branch = dir.create_branch()
160
294
        t.mkdir('branch')
161
295
        branch_dir = bzrdirformat.initialize(self.get_url('branch'))
162
 
        made_branch = bzrlib.branch.BranchReferenceFormat().initialize(branch_dir, target_branch)
 
296
        made_branch = BranchReferenceFormat().initialize(branch_dir, target_branch)
163
297
        self.assertEqual(made_branch.base, target_branch.base)
164
298
        opened_branch = branch_dir.open_branch()
165
299
        self.assertEqual(opened_branch.base, target_branch.base)
169
303
 
170
304
    def test_constructor(self):
171
305
        """Check that creating a BranchHooks instance has the right defaults."""
172
 
        hooks = bzrlib.branch.BranchHooks()
 
306
        hooks = BranchHooks()
173
307
        self.assertTrue("set_rh" in hooks, "set_rh not in %s" % hooks)
174
308
        self.assertTrue("post_push" in hooks, "post_push not in %s" % hooks)
175
309
        self.assertTrue("post_commit" in hooks, "post_commit not in %s" % hooks)
179
313
    def test_installed_hooks_are_BranchHooks(self):
180
314
        """The installed hooks object should be a BranchHooks."""
181
315
        # the installed hooks are saved in self._preserved_hooks.
182
 
        self.assertIsInstance(self._preserved_hooks, bzrlib.branch.BranchHooks)
 
316
        self.assertIsInstance(self._preserved_hooks, BranchHooks)
183
317
 
184
318
    def test_install_hook_raises_unknown_hook(self):
185
319
        """install_hook should raise UnknownHook if a hook is unknown."""
186
 
        hooks = bzrlib.branch.BranchHooks()
 
320
        hooks = BranchHooks()
187
321
        self.assertRaises(UnknownHook, hooks.install_hook, 'silly', None)
188
322
 
189
323
    def test_install_hook_appends_known_hook(self):
190
324
        """install_hook should append the callable for known hooks."""
191
 
        hooks = bzrlib.branch.BranchHooks()
 
325
        hooks = BranchHooks()
192
326
        hooks.install_hook('set_rh', None)
193
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")