~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_branch.py

  • Committer: mbp at sourcefrog
  • Date: 2005-03-29 07:33:29 UTC
  • Revision ID: mbp@sourcefrog.net-20050329073329-005d4d9140546a47
update ignore patterns

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
 
import bzrlib.branch
28
 
from bzrlib.branch import (BzrBranch5, 
29
 
                           BzrBranchFormat5)
30
 
import bzrlib.bzrdir as bzrdir
31
 
from bzrlib.bzrdir import (BzrDirMetaFormat1, BzrDirMeta1, 
32
 
                           BzrDir, BzrDirFormat)
33
 
from bzrlib.errors import (NotBranchError,
34
 
                           UnknownFormatError,
35
 
                           UnknownHook,
36
 
                           UnsupportedFormatError,
37
 
                           )
38
 
 
39
 
from bzrlib.tests import TestCase, TestCaseWithTransport
40
 
from bzrlib.transport import get_transport
41
 
 
42
 
class TestDefaultFormat(TestCase):
43
 
 
44
 
    def test_get_set_default_format(self):
45
 
        old_format = bzrlib.branch.BranchFormat.get_default_format()
46
 
        # default is 5
47
 
        self.assertTrue(isinstance(old_format, bzrlib.branch.BzrBranchFormat5))
48
 
        bzrlib.branch.BranchFormat.set_default_format(SampleBranchFormat())
49
 
        try:
50
 
            # the default branch format is used by the meta dir format
51
 
            # which is not the default bzrdir format at this point
52
 
            dir = BzrDirMetaFormat1().initialize('memory:///')
53
 
            result = dir.create_branch()
54
 
            self.assertEqual(result, 'A branch')
55
 
        finally:
56
 
            bzrlib.branch.BranchFormat.set_default_format(old_format)
57
 
        self.assertEqual(old_format, bzrlib.branch.BranchFormat.get_default_format())
58
 
 
59
 
 
60
 
class TestBranchFormat5(TestCaseWithTransport):
61
 
    """Tests specific to branch format 5"""
62
 
 
63
 
    def test_branch_format_5_uses_lockdir(self):
64
 
        url = self.get_url()
65
 
        bzrdir = BzrDirMetaFormat1().initialize(url)
66
 
        bzrdir.create_repository()
67
 
        branch = bzrdir.create_branch()
68
 
        t = self.get_transport()
69
 
        self.log("branch instance is %r" % branch)
70
 
        self.assert_(isinstance(branch, BzrBranch5))
71
 
        self.assertIsDirectory('.', t)
72
 
        self.assertIsDirectory('.bzr/branch', t)
73
 
        self.assertIsDirectory('.bzr/branch/lock', t)
74
 
        branch.lock_write()
75
 
        try:
76
 
            self.assertIsDirectory('.bzr/branch/lock/held', t)
77
 
        finally:
78
 
            branch.unlock()
79
 
 
80
 
 
81
 
class SampleBranchFormat(bzrlib.branch.BranchFormat):
82
 
    """A sample format
83
 
 
84
 
    this format is initializable, unsupported to aid in testing the 
85
 
    open and open_downlevel routines.
86
 
    """
87
 
 
88
 
    def get_format_string(self):
89
 
        """See BzrBranchFormat.get_format_string()."""
90
 
        return "Sample branch format."
91
 
 
92
 
    def initialize(self, a_bzrdir):
93
 
        """Format 4 branches cannot be created."""
94
 
        t = a_bzrdir.get_branch_transport(self)
95
 
        t.put_bytes('format', self.get_format_string())
96
 
        return 'A branch'
97
 
 
98
 
    def is_supported(self):
99
 
        return False
100
 
 
101
 
    def open(self, transport, _found=False):
102
 
        return "opened branch."
103
 
 
104
 
 
105
 
class TestBzrBranchFormat(TestCaseWithTransport):
106
 
    """Tests for the BzrBranchFormat facility."""
107
 
 
108
 
    def test_find_format(self):
109
 
        # is the right format object found for a branch?
110
 
        # create a branch with a few known format objects.
111
 
        # this is not quite the same as 
112
 
        self.build_tree(["foo/", "bar/"])
113
 
        def check_format(format, url):
114
 
            dir = format._matchingbzrdir.initialize(url)
115
 
            dir.create_repository()
116
 
            format.initialize(dir)
117
 
            found_format = bzrlib.branch.BranchFormat.find_format(dir)
118
 
            self.failUnless(isinstance(found_format, format.__class__))
119
 
        check_format(bzrlib.branch.BzrBranchFormat5(), "bar")
120
 
        
121
 
    def test_find_format_not_branch(self):
122
 
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
123
 
        self.assertRaises(NotBranchError,
124
 
                          bzrlib.branch.BranchFormat.find_format,
125
 
                          dir)
126
 
 
127
 
    def test_find_format_unknown_format(self):
128
 
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
129
 
        SampleBranchFormat().initialize(dir)
130
 
        self.assertRaises(UnknownFormatError,
131
 
                          bzrlib.branch.BranchFormat.find_format,
132
 
                          dir)
133
 
 
134
 
    def test_register_unregister_format(self):
135
 
        format = SampleBranchFormat()
136
 
        # make a control dir
137
 
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
138
 
        # make a branch
139
 
        format.initialize(dir)
140
 
        # register a format for it.
141
 
        bzrlib.branch.BranchFormat.register_format(format)
142
 
        # which branch.Open will refuse (not supported)
143
 
        self.assertRaises(UnsupportedFormatError, bzrlib.branch.Branch.open, self.get_url())
144
 
        # but open_downlevel will work
145
 
        self.assertEqual(format.open(dir), bzrdir.BzrDir.open(self.get_url()).open_branch(unsupported=True))
146
 
        # unregister the format
147
 
        bzrlib.branch.BranchFormat.unregister_format(format)
148
 
 
149
 
 
150
 
class TestBranchReference(TestCaseWithTransport):
151
 
    """Tests for the branch reference facility."""
152
 
 
153
 
    def test_create_open_reference(self):
154
 
        bzrdirformat = bzrdir.BzrDirMetaFormat1()
155
 
        t = get_transport(self.get_url('.'))
156
 
        t.mkdir('repo')
157
 
        dir = bzrdirformat.initialize(self.get_url('repo'))
158
 
        dir.create_repository()
159
 
        target_branch = dir.create_branch()
160
 
        t.mkdir('branch')
161
 
        branch_dir = bzrdirformat.initialize(self.get_url('branch'))
162
 
        made_branch = bzrlib.branch.BranchReferenceFormat().initialize(branch_dir, target_branch)
163
 
        self.assertEqual(made_branch.base, target_branch.base)
164
 
        opened_branch = branch_dir.open_branch()
165
 
        self.assertEqual(opened_branch.base, target_branch.base)
166
 
 
167
 
 
168
 
class TestHooks(TestCase):
169
 
 
170
 
    def test_constructor(self):
171
 
        """Check that creating a BranchHooks instance has the right defaults."""
172
 
        hooks = bzrlib.branch.BranchHooks()
173
 
        self.assertTrue("set_rh" in hooks, "set_rh not in %s" % hooks)
174
 
 
175
 
    def test_installed_hooks_are_BranchHooks(self):
176
 
        """The installed hooks object should be a BranchHooks."""
177
 
        # the installed hooks are saved in self._preserved_hooks.
178
 
        self.assertIsInstance(self._preserved_hooks, bzrlib.branch.BranchHooks)
179
 
 
180
 
    def test_install_hook_raises_unknown_hook(self):
181
 
        """install_hook should raise UnknownHook if a hook is unknown."""
182
 
        hooks = bzrlib.branch.BranchHooks()
183
 
        self.assertRaises(UnknownHook, hooks.install_hook, 'silly', None)
184
 
 
185
 
    def test_install_hook_appends_known_hook(self):
186
 
        """install_hook should append the callable for known hooks."""
187
 
        hooks = bzrlib.branch.BranchHooks()
188
 
        hooks.install_hook('set_rh', None)
189
 
        self.assertEqual(hooks['set_rh'], [None])