/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_branch.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2006-04-12 08:40:15 UTC
  • mfrom: (1651.1.7 bzr.mbp.escape-stores)
  • Revision ID: pqm@pqm.ubuntu.com-20060412084015-e6472a0717edbca6
(mbp) storage escaping, cleanups

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
                           UnsupportedFormatError,
 
36
                           )
 
37
 
 
38
from bzrlib.tests import TestCase, TestCaseWithTransport
 
39
from bzrlib.transport import get_transport
 
40
 
 
41
class TestDefaultFormat(TestCase):
 
42
 
 
43
    def test_get_set_default_format(self):
 
44
        old_format = bzrlib.branch.BranchFormat.get_default_format()
 
45
        # default is 5
 
46
        self.assertTrue(isinstance(old_format, bzrlib.branch.BzrBranchFormat5))
 
47
        bzrlib.branch.BranchFormat.set_default_format(SampleBranchFormat())
 
48
        try:
 
49
            # the default branch format is used by the meta dir format
 
50
            # which is not the default bzrdir format at this point
 
51
            dir = BzrDirMetaFormat1().initialize('memory:/')
 
52
            result = dir.create_branch()
 
53
            self.assertEqual(result, 'A branch')
 
54
        finally:
 
55
            bzrlib.branch.BranchFormat.set_default_format(old_format)
 
56
        self.assertEqual(old_format, bzrlib.branch.BranchFormat.get_default_format())
 
57
 
 
58
 
 
59
class TestBranchFormat5(TestCaseWithTransport):
 
60
    """Tests specific to branch format 5"""
 
61
 
 
62
    def test_branch_format_5_uses_lockdir(self):
 
63
        url = self.get_url()
 
64
        bzrdir = BzrDirMetaFormat1().initialize(url)
 
65
        bzrdir.create_repository()
 
66
        branch = bzrdir.create_branch()
 
67
        t = self.get_transport()
 
68
        self.log("branch instance is %r" % branch)
 
69
        self.assert_(isinstance(branch, BzrBranch5))
 
70
        self.assertIsDirectory('.', t)
 
71
        self.assertIsDirectory('.bzr/branch', t)
 
72
        self.assertIsDirectory('.bzr/branch/lock', t)
 
73
        branch.lock_write()
 
74
        self.assertIsDirectory('.bzr/branch/lock/held', t)
 
75
 
 
76
 
 
77
class TestBranchEscaping(TestCaseWithTransport):
 
78
    """Test a branch can be correctly stored and used on a vfat-like transport
 
79
    
 
80
    Makes sure we have proper escaping of invalid characters, etc.
 
81
 
 
82
    It'd be better to test all operations on the FakeVFATTransportDecorator,
 
83
    but working trees go straight to the os not through the Transport layer.
 
84
    Therefore we build some history first in the regular way and then 
 
85
    check it's safe to access for vfat.
 
86
    """
 
87
 
 
88
    FOO_ID = 'foo<:>ID'
 
89
    REV_ID = 'revid-1'
 
90
 
 
91
    def setUp(self):
 
92
        super(TestBranchEscaping, self).setUp()
 
93
        from bzrlib.repository import RepositoryFormatKnit1
 
94
        bzrdir = BzrDirMetaFormat1().initialize(self.get_url())
 
95
        repo = RepositoryFormatKnit1().initialize(bzrdir)
 
96
        branch = bzrdir.create_branch()
 
97
        wt = bzrdir.create_workingtree()
 
98
        self.build_tree_contents([("foo", "contents of foo")])
 
99
        # add file with id containing wierd characters
 
100
        wt.add(['foo'], [self.FOO_ID])
 
101
        wt.commit('this is my new commit', rev_id=self.REV_ID)
 
102
 
 
103
    def test_branch_on_vfat(self):
 
104
        from bzrlib.transport.fakevfat import FakeVFATTransportDecorator
 
105
        # now access over vfat; should be safe
 
106
        transport = FakeVFATTransportDecorator('vfat+' + self.get_url())
 
107
        bzrdir, junk = BzrDir.open_containing_from_transport(transport)
 
108
        branch = bzrdir.open_branch()
 
109
        revtree = branch.repository.revision_tree(self.REV_ID)
 
110
        contents = revtree.get_file_text(self.FOO_ID)
 
111
        self.assertEqual(contents, 'contents of foo')
 
112
 
 
113
 
 
114
class SampleBranchFormat(bzrlib.branch.BranchFormat):
 
115
    """A sample format
 
116
 
 
117
    this format is initializable, unsupported to aid in testing the 
 
118
    open and open_downlevel routines.
 
119
    """
 
120
 
 
121
    def get_format_string(self):
 
122
        """See BzrBranchFormat.get_format_string()."""
 
123
        return "Sample branch format."
 
124
 
 
125
    def initialize(self, a_bzrdir):
 
126
        """Format 4 branches cannot be created."""
 
127
        t = a_bzrdir.get_branch_transport(self)
 
128
        t.put('format', StringIO(self.get_format_string()))
 
129
        return 'A branch'
 
130
 
 
131
    def is_supported(self):
 
132
        return False
 
133
 
 
134
    def open(self, transport, _found=False):
 
135
        return "opened branch."
 
136
 
 
137
 
 
138
class TestBzrBranchFormat(TestCaseWithTransport):
 
139
    """Tests for the BzrBranchFormat facility."""
 
140
 
 
141
    def test_find_format(self):
 
142
        # is the right format object found for a branch?
 
143
        # create a branch with a few known format objects.
 
144
        # this is not quite the same as 
 
145
        self.build_tree(["foo/", "bar/"])
 
146
        def check_format(format, url):
 
147
            dir = format._matchingbzrdir.initialize(url)
 
148
            dir.create_repository()
 
149
            format.initialize(dir)
 
150
            found_format = bzrlib.branch.BranchFormat.find_format(dir)
 
151
            self.failUnless(isinstance(found_format, format.__class__))
 
152
        check_format(bzrlib.branch.BzrBranchFormat5(), "bar")
 
153
        
 
154
    def test_find_format_not_branch(self):
 
155
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
 
156
        self.assertRaises(NotBranchError,
 
157
                          bzrlib.branch.BranchFormat.find_format,
 
158
                          dir)
 
159
 
 
160
    def test_find_format_unknown_format(self):
 
161
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
 
162
        SampleBranchFormat().initialize(dir)
 
163
        self.assertRaises(UnknownFormatError,
 
164
                          bzrlib.branch.BranchFormat.find_format,
 
165
                          dir)
 
166
 
 
167
    def test_register_unregister_format(self):
 
168
        format = SampleBranchFormat()
 
169
        # make a control dir
 
170
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
 
171
        # make a branch
 
172
        format.initialize(dir)
 
173
        # register a format for it.
 
174
        bzrlib.branch.BranchFormat.register_format(format)
 
175
        # which branch.Open will refuse (not supported)
 
176
        self.assertRaises(UnsupportedFormatError, bzrlib.branch.Branch.open, self.get_url())
 
177
        # but open_downlevel will work
 
178
        self.assertEqual(format.open(dir), bzrdir.BzrDir.open(self.get_url()).open_branch(unsupported=True))
 
179
        # unregister the format
 
180
        bzrlib.branch.BranchFormat.unregister_format(format)
 
181
 
 
182
 
 
183
class TestBranchReference(TestCaseWithTransport):
 
184
    """Tests for the branch reference facility."""
 
185
 
 
186
    def test_create_open_reference(self):
 
187
        bzrdirformat = bzrdir.BzrDirMetaFormat1()
 
188
        t = get_transport(self.get_url('.'))
 
189
        t.mkdir('repo')
 
190
        dir = bzrdirformat.initialize(self.get_url('repo'))
 
191
        dir.create_repository()
 
192
        target_branch = dir.create_branch()
 
193
        t.mkdir('branch')
 
194
        branch_dir = bzrdirformat.initialize(self.get_url('branch'))
 
195
        made_branch = bzrlib.branch.BranchReferenceFormat().initialize(branch_dir, target_branch)
 
196
        self.assertEqual(made_branch.base, target_branch.base)
 
197
        opened_branch = branch_dir.open_branch()
 
198
        self.assertEqual(opened_branch.base, target_branch.base)