~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_branch.py

- improved handling of non-ascii branch names and test
  patch from Joel Rosdahl

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 Canonical Ltd
2
 
#
3
 
# This program is free software; you can redistribute it and/or modify
4
 
# it under the terms of the GNU General Public License as published by
5
 
# the Free Software Foundation; either version 2 of the License, or
6
 
# (at your option) any later version.
7
 
#
8
 
# This program is distributed in the hope that it will be useful,
9
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
# GNU General Public License for more details.
12
 
#
13
 
# You should have received a copy of the GNU General Public License
14
 
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
 
 
17
 
"""Tests for the Branch facility that are not interface  tests.
18
 
 
19
 
For interface tests see tests/branch_implementations/*.py.
20
 
 
21
 
For concrete class tests see this file, and for meta-branch tests
22
 
also see this file.
23
 
"""
24
 
 
25
 
from StringIO import StringIO
26
 
 
27
 
from bzrlib import (
28
 
    branch as _mod_branch,
29
 
    bzrdir,
30
 
    errors,
31
 
    urlutils,
32
 
    )
33
 
import bzrlib.branch
34
 
from bzrlib.branch import (BzrBranch5, 
35
 
                           BzrBranchFormat5)
36
 
from bzrlib.bzrdir import (BzrDirMetaFormat1, BzrDirMeta1, 
37
 
                           BzrDir, BzrDirFormat)
38
 
from bzrlib.errors import (NotBranchError,
39
 
                           UnknownFormatError,
40
 
                           UnknownHook,
41
 
                           UnsupportedFormatError,
42
 
                           )
43
 
 
44
 
from bzrlib.tests import TestCase, TestCaseWithTransport
45
 
from bzrlib.transport import get_transport
46
 
 
47
 
class TestDefaultFormat(TestCase):
48
 
 
49
 
    def test_get_set_default_format(self):
50
 
        old_format = bzrlib.branch.BranchFormat.get_default_format()
51
 
        # default is 5
52
 
        self.assertTrue(isinstance(old_format, bzrlib.branch.BzrBranchFormat5))
53
 
        bzrlib.branch.BranchFormat.set_default_format(SampleBranchFormat())
54
 
        try:
55
 
            # the default branch format is used by the meta dir format
56
 
            # which is not the default bzrdir format at this point
57
 
            dir = BzrDirMetaFormat1().initialize('memory:///')
58
 
            result = dir.create_branch()
59
 
            self.assertEqual(result, 'A branch')
60
 
        finally:
61
 
            bzrlib.branch.BranchFormat.set_default_format(old_format)
62
 
        self.assertEqual(old_format, bzrlib.branch.BranchFormat.get_default_format())
63
 
 
64
 
 
65
 
class TestBranchFormat5(TestCaseWithTransport):
66
 
    """Tests specific to branch format 5"""
67
 
 
68
 
    def test_branch_format_5_uses_lockdir(self):
69
 
        url = self.get_url()
70
 
        bzrdir = BzrDirMetaFormat1().initialize(url)
71
 
        bzrdir.create_repository()
72
 
        branch = bzrdir.create_branch()
73
 
        t = self.get_transport()
74
 
        self.log("branch instance is %r" % branch)
75
 
        self.assert_(isinstance(branch, BzrBranch5))
76
 
        self.assertIsDirectory('.', t)
77
 
        self.assertIsDirectory('.bzr/branch', t)
78
 
        self.assertIsDirectory('.bzr/branch/lock', t)
79
 
        branch.lock_write()
80
 
        try:
81
 
            self.assertIsDirectory('.bzr/branch/lock/held', t)
82
 
        finally:
83
 
            branch.unlock()
84
 
 
85
 
    def test_set_push_location(self):
86
 
        from bzrlib.config import (locations_config_filename,
87
 
                                   ensure_config_dir_exists)
88
 
        ensure_config_dir_exists()
89
 
        fn = locations_config_filename()
90
 
        branch = self.make_branch('.', format='knit')
91
 
        branch.set_push_location('foo')
92
 
        local_path = urlutils.local_path_from_url(branch.base[:-1])
93
 
        self.assertFileEqual("[%s]\n"
94
 
                             "push_location = foo\n"
95
 
                             "push_location:policy = norecurse" % local_path,
96
 
                             fn)
97
 
 
98
 
    # TODO RBC 20051029 test getting a push location from a branch in a
99
 
    # recursive section - that is, it appends the branch name.
100
 
 
101
 
 
102
 
class SampleBranchFormat(bzrlib.branch.BranchFormat):
103
 
    """A sample format
104
 
 
105
 
    this format is initializable, unsupported to aid in testing the 
106
 
    open and open_downlevel routines.
107
 
    """
108
 
 
109
 
    def get_format_string(self):
110
 
        """See BzrBranchFormat.get_format_string()."""
111
 
        return "Sample branch format."
112
 
 
113
 
    def initialize(self, a_bzrdir):
114
 
        """Format 4 branches cannot be created."""
115
 
        t = a_bzrdir.get_branch_transport(self)
116
 
        t.put_bytes('format', self.get_format_string())
117
 
        return 'A branch'
118
 
 
119
 
    def is_supported(self):
120
 
        return False
121
 
 
122
 
    def open(self, transport, _found=False):
123
 
        return "opened branch."
124
 
 
125
 
 
126
 
class TestBzrBranchFormat(TestCaseWithTransport):
127
 
    """Tests for the BzrBranchFormat facility."""
128
 
 
129
 
    def test_find_format(self):
130
 
        # is the right format object found for a branch?
131
 
        # create a branch with a few known format objects.
132
 
        # this is not quite the same as 
133
 
        self.build_tree(["foo/", "bar/"])
134
 
        def check_format(format, url):
135
 
            dir = format._matchingbzrdir.initialize(url)
136
 
            dir.create_repository()
137
 
            format.initialize(dir)
138
 
            found_format = bzrlib.branch.BranchFormat.find_format(dir)
139
 
            self.failUnless(isinstance(found_format, format.__class__))
140
 
        check_format(bzrlib.branch.BzrBranchFormat5(), "bar")
141
 
        
142
 
    def test_find_format_not_branch(self):
143
 
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
144
 
        self.assertRaises(NotBranchError,
145
 
                          bzrlib.branch.BranchFormat.find_format,
146
 
                          dir)
147
 
 
148
 
    def test_find_format_unknown_format(self):
149
 
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
150
 
        SampleBranchFormat().initialize(dir)
151
 
        self.assertRaises(UnknownFormatError,
152
 
                          bzrlib.branch.BranchFormat.find_format,
153
 
                          dir)
154
 
 
155
 
    def test_register_unregister_format(self):
156
 
        format = SampleBranchFormat()
157
 
        # make a control dir
158
 
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
159
 
        # make a branch
160
 
        format.initialize(dir)
161
 
        # register a format for it.
162
 
        bzrlib.branch.BranchFormat.register_format(format)
163
 
        # which branch.Open will refuse (not supported)
164
 
        self.assertRaises(UnsupportedFormatError, bzrlib.branch.Branch.open, self.get_url())
165
 
        self.make_branch_and_tree('foo')
166
 
        # but open_downlevel will work
167
 
        self.assertEqual(format.open(dir), bzrdir.BzrDir.open(self.get_url()).open_branch(unsupported=True))
168
 
        # unregister the format
169
 
        bzrlib.branch.BranchFormat.unregister_format(format)
170
 
        self.make_branch_and_tree('bar')
171
 
 
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
 
 
180
 
class TestBranch6(TestCaseWithTransport):
181
 
 
182
 
    def test_creation(self):
183
 
        format = BzrDirMetaFormat1()
184
 
        format.set_branch_format(_mod_branch.BzrBranchFormat6())
185
 
        branch = self.make_branch('a', format=format)
186
 
        self.assertIsInstance(branch, _mod_branch.BzrBranch6)
187
 
        branch = self.make_branch('b', format='experimental-branch6')
188
 
        self.assertIsInstance(branch, _mod_branch.BzrBranch6)
189
 
        branch = _mod_branch.Branch.open('a')
190
 
        self.assertIsInstance(branch, _mod_branch.BzrBranch6)
191
 
 
192
 
    def test_layout(self):
193
 
        branch = self.make_branch('a', format='experimental-branch6')
194
 
        self.failUnlessExists('a/.bzr/branch/last-revision')
195
 
        self.failIfExists('a/.bzr/branch/revision-history')
196
 
 
197
 
    def test_config(self):
198
 
        """Ensure that all configuration data is stored in the branch"""
199
 
        branch = self.make_branch('a', format='experimental-branch6')
200
 
        branch.set_parent('http://bazaar-vcs.org')
201
 
        self.failIfExists('a/.bzr/branch/parent')
202
 
        self.assertEqual('http://bazaar-vcs.org', branch.get_parent())
203
 
        branch.set_push_location('sftp://bazaar-vcs.org')
204
 
        config = branch.get_config()._get_branch_data_config()
205
 
        self.assertEqual('sftp://bazaar-vcs.org',
206
 
                         config.get_user_option('push_location'))
207
 
        branch.set_bound_location('ftp://bazaar-vcs.org')
208
 
        self.failIfExists('a/.bzr/branch/bound')
209
 
        self.assertEqual('ftp://bazaar-vcs.org', branch.get_bound_location())
210
 
 
211
 
    def test_set_revision_history(self):
212
 
        tree = self.make_branch_and_memory_tree('.',
213
 
            format='experimental-branch6')
214
 
        tree.lock_write()
215
 
        try:
216
 
            tree.add('.')
217
 
            tree.commit('foo', rev_id='foo')
218
 
            tree.commit('bar', rev_id='bar')
219
 
            tree.branch.set_revision_history(['foo', 'bar'])
220
 
            tree.branch.set_revision_history(['foo'])
221
 
            self.assertRaises(errors.NotLefthandHistory,
222
 
                              tree.branch.set_revision_history, ['bar'])
223
 
        finally:
224
 
            tree.unlock()
225
 
 
226
 
    def test_append_revision(self):
227
 
        tree = self.make_branch_and_tree('branch1',
228
 
            format='experimental-branch6')
229
 
        tree.lock_write()
230
 
        try:
231
 
            tree.add('.')
232
 
            tree.commit('foo', rev_id='foo')
233
 
            tree.commit('bar', rev_id='bar')
234
 
            tree.commit('baz', rev_id='baz')
235
 
            tree.set_last_revision('bar')
236
 
            tree.branch.set_last_revision_info(2, 'bar')
237
 
            tree.commit('qux', rev_id='qux')
238
 
            tree.add_parent_tree_id('baz')
239
 
            tree.commit('qux', rev_id='quxx')
240
 
            tree.branch.set_last_revision_info(0, 'null:')
241
 
            self.assertRaises(errors.NotLeftParentDescendant,
242
 
                              tree.branch.append_revision, 'bar')
243
 
            tree.branch.append_revision('foo')
244
 
            self.assertRaises(errors.NotLeftParentDescendant,
245
 
                              tree.branch.append_revision, 'baz')
246
 
            tree.branch.append_revision('bar')
247
 
            tree.branch.append_revision('baz')
248
 
            self.assertRaises(errors.NotLeftParentDescendant,
249
 
                              tree.branch.append_revision, 'quxx')
250
 
        finally:
251
 
            tree.unlock()
252
 
 
253
 
    def do_checkout_test(self, lightweight=False):
254
 
        tree = self.make_branch_and_tree('source', format='experimental-knit3')
255
 
        subtree = self.make_branch_and_tree('source/subtree', 
256
 
                                            format='experimental-knit3')
257
 
        subsubtree = self.make_branch_and_tree('source/subtree/subsubtree',
258
 
                                               format='experimental-knit3')
259
 
        self.build_tree(['source/subtree/file',
260
 
                         'source/subtree/subsubtree/file'])
261
 
        subsubtree.add('file')
262
 
        subtree.add('file')
263
 
        subtree.add_reference(subsubtree)
264
 
        tree.add_reference(subtree)
265
 
        tree.commit('a revision')
266
 
        subtree.commit('a subtree file')
267
 
        subsubtree.commit('a subsubtree file')
268
 
        tree.branch.create_checkout('target', lightweight=lightweight)
269
 
        self.failUnlessExists('target')
270
 
        self.failUnlessExists('target/subtree')
271
 
        self.failUnlessExists('target/subtree/file')
272
 
        self.failUnlessExists('target/subtree/subsubtree/file')
273
 
        subbranch = _mod_branch.Branch.open('target/subtree/subsubtree')
274
 
        if lightweight:
275
 
            self.assertEndsWith(subbranch.base, 'source/subtree/subsubtree/')
276
 
        else:
277
 
            self.assertEndsWith(subbranch.base, 'target/subtree/subsubtree/')
278
 
 
279
 
 
280
 
    def test_checkout_with_references(self):
281
 
        self.do_checkout_test()
282
 
 
283
 
    def test_light_checkout_with_references(self):
284
 
        self.do_checkout_test(lightweight=True)
285
 
 
286
 
class TestBranchReference(TestCaseWithTransport):
287
 
    """Tests for the branch reference facility."""
288
 
 
289
 
    def test_create_open_reference(self):
290
 
        bzrdirformat = bzrdir.BzrDirMetaFormat1()
291
 
        t = get_transport(self.get_url('.'))
292
 
        t.mkdir('repo')
293
 
        dir = bzrdirformat.initialize(self.get_url('repo'))
294
 
        dir.create_repository()
295
 
        target_branch = dir.create_branch()
296
 
        t.mkdir('branch')
297
 
        branch_dir = bzrdirformat.initialize(self.get_url('branch'))
298
 
        made_branch = bzrlib.branch.BranchReferenceFormat().initialize(branch_dir, target_branch)
299
 
        self.assertEqual(made_branch.base, target_branch.base)
300
 
        opened_branch = branch_dir.open_branch()
301
 
        self.assertEqual(opened_branch.base, target_branch.base)
302
 
 
303
 
 
304
 
class TestHooks(TestCase):
305
 
 
306
 
    def test_constructor(self):
307
 
        """Check that creating a BranchHooks instance has the right defaults."""
308
 
        hooks = bzrlib.branch.BranchHooks()
309
 
        self.assertTrue("set_rh" in hooks, "set_rh not in %s" % hooks)
310
 
        self.assertTrue("post_push" in hooks, "post_push not in %s" % hooks)
311
 
        self.assertTrue("post_commit" in hooks, "post_commit not in %s" % hooks)
312
 
        self.assertTrue("post_pull" in hooks, "post_pull not in %s" % hooks)
313
 
        self.assertTrue("post_uncommit" in hooks, "post_uncommit not in %s" % hooks)
314
 
 
315
 
    def test_installed_hooks_are_BranchHooks(self):
316
 
        """The installed hooks object should be a BranchHooks."""
317
 
        # the installed hooks are saved in self._preserved_hooks.
318
 
        self.assertIsInstance(self._preserved_hooks, bzrlib.branch.BranchHooks)
319
 
 
320
 
    def test_install_hook_raises_unknown_hook(self):
321
 
        """install_hook should raise UnknownHook if a hook is unknown."""
322
 
        hooks = bzrlib.branch.BranchHooks()
323
 
        self.assertRaises(UnknownHook, hooks.install_hook, 'silly', None)
324
 
 
325
 
    def test_install_hook_appends_known_hook(self):
326
 
        """install_hook should append the callable for known hooks."""
327
 
        hooks = bzrlib.branch.BranchHooks()
328
 
        hooks.install_hook('set_rh', None)
329
 
        self.assertEqual(hooks['set_rh'], [None])