~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_branch.py

  • Committer: Robert Collins
  • Date: 2005-10-06 22:15:52 UTC
  • mfrom: (1185.13.2)
  • mto: This revision was merged to the branch mainline in revision 1420.
  • Revision ID: robertc@robertcollins.net-20051006221552-9b15c96fa504e0ad
mergeĀ fromĀ upstream

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
 
 
254
 
class TestBranchReference(TestCaseWithTransport):
255
 
    """Tests for the branch reference facility."""
256
 
 
257
 
    def test_create_open_reference(self):
258
 
        bzrdirformat = bzrdir.BzrDirMetaFormat1()
259
 
        t = get_transport(self.get_url('.'))
260
 
        t.mkdir('repo')
261
 
        dir = bzrdirformat.initialize(self.get_url('repo'))
262
 
        dir.create_repository()
263
 
        target_branch = dir.create_branch()
264
 
        t.mkdir('branch')
265
 
        branch_dir = bzrdirformat.initialize(self.get_url('branch'))
266
 
        made_branch = bzrlib.branch.BranchReferenceFormat().initialize(branch_dir, target_branch)
267
 
        self.assertEqual(made_branch.base, target_branch.base)
268
 
        opened_branch = branch_dir.open_branch()
269
 
        self.assertEqual(opened_branch.base, target_branch.base)
270
 
 
271
 
 
272
 
class TestHooks(TestCase):
273
 
 
274
 
    def test_constructor(self):
275
 
        """Check that creating a BranchHooks instance has the right defaults."""
276
 
        hooks = bzrlib.branch.BranchHooks()
277
 
        self.assertTrue("set_rh" in hooks, "set_rh not in %s" % hooks)
278
 
        self.assertTrue("post_push" in hooks, "post_push not in %s" % hooks)
279
 
        self.assertTrue("post_commit" in hooks, "post_commit not in %s" % hooks)
280
 
        self.assertTrue("post_pull" in hooks, "post_pull not in %s" % hooks)
281
 
        self.assertTrue("post_uncommit" in hooks, "post_uncommit not in %s" % hooks)
282
 
 
283
 
    def test_installed_hooks_are_BranchHooks(self):
284
 
        """The installed hooks object should be a BranchHooks."""
285
 
        # the installed hooks are saved in self._preserved_hooks.
286
 
        self.assertIsInstance(self._preserved_hooks, bzrlib.branch.BranchHooks)
287
 
 
288
 
    def test_install_hook_raises_unknown_hook(self):
289
 
        """install_hook should raise UnknownHook if a hook is unknown."""
290
 
        hooks = bzrlib.branch.BranchHooks()
291
 
        self.assertRaises(UnknownHook, hooks.install_hook, 'silly', None)
292
 
 
293
 
    def test_install_hook_appends_known_hook(self):
294
 
        """install_hook should append the callable for known hooks."""
295
 
        hooks = bzrlib.branch.BranchHooks()
296
 
        hooks.install_hook('set_rh', None)
297
 
        self.assertEqual(hooks['set_rh'], [None])