~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_branch.py

first cut at merge from integration.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 Canonical Ltd
2
 
#
 
1
# (C) 2005 Canonical Ltd
 
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
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
7
 
#
 
7
 
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
# GNU General Public License for more details.
12
 
#
 
12
 
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
"""Tests for the Branch facility that are not interface  tests.
18
18
 
19
 
For interface tests see tests/branch_implementations/*.py.
 
19
For interface tests see test_branch_implementations.py.
20
20
 
21
21
For concrete class tests see this file, and for meta-branch tests
22
22
also see this file.
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
31
 
from bzrlib.bzrdir import (BzrDirMetaFormat1, BzrDirMeta1, 
32
 
                           BzrDir, BzrDirFormat)
 
27
import bzrlib.branch as branch
33
28
from bzrlib.errors import (NotBranchError,
34
29
                           UnknownFormatError,
35
30
                           UnsupportedFormatError,
36
31
                           )
37
32
 
38
 
from bzrlib.tests import TestCase, TestCaseWithTransport
 
33
from bzrlib.tests import TestCase, TestCaseInTempDir
39
34
from bzrlib.transport import get_transport
40
35
 
41
36
class TestDefaultFormat(TestCase):
42
37
 
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
 
        try:
75
 
            self.assertIsDirectory('.bzr/branch/lock/held', t)
76
 
        finally:
77
 
            branch.unlock()
78
 
 
79
 
 
80
 
class TestBranchEscaping(TestCaseWithTransport):
81
 
    """Test a branch can be correctly stored and used on a vfat-like transport
82
 
    
83
 
    Makes sure we have proper escaping of invalid characters, etc.
84
 
 
85
 
    It'd be better to test all operations on the FakeVFATTransportDecorator,
86
 
    but working trees go straight to the os not through the Transport layer.
87
 
    Therefore we build some history first in the regular way and then 
88
 
    check it's safe to access for vfat.
89
 
    """
90
 
 
91
 
    FOO_ID = 'foo<:>ID'
92
 
    REV_ID = 'revid-1'
93
 
 
94
 
    def setUp(self):
95
 
        super(TestBranchEscaping, self).setUp()
96
 
        from bzrlib.repository import RepositoryFormatKnit1
97
 
        bzrdir = BzrDirMetaFormat1().initialize(self.get_url())
98
 
        repo = RepositoryFormatKnit1().initialize(bzrdir)
99
 
        branch = bzrdir.create_branch()
100
 
        wt = bzrdir.create_workingtree()
101
 
        self.build_tree_contents([("foo", "contents of foo")])
102
 
        # add file with id containing wierd characters
103
 
        wt.add(['foo'], [self.FOO_ID])
104
 
        wt.commit('this is my new commit', rev_id=self.REV_ID)
105
 
 
106
 
    def test_branch_on_vfat(self):
107
 
        from bzrlib.transport.fakevfat import FakeVFATTransportDecorator
108
 
        # now access over vfat; should be safe
109
 
        transport = FakeVFATTransportDecorator('vfat+' + self.get_url())
110
 
        bzrdir, junk = BzrDir.open_containing_from_transport(transport)
111
 
        branch = bzrdir.open_branch()
112
 
        revtree = branch.repository.revision_tree(self.REV_ID)
113
 
        contents = revtree.get_file_text(self.FOO_ID)
114
 
        self.assertEqual(contents, 'contents of foo')
115
 
 
116
 
 
117
 
class SampleBranchFormat(bzrlib.branch.BranchFormat):
 
38
    def test_get_set_default_initializer(self):
 
39
        old_initializer = branch.Branch.get_default_initializer()
 
40
        # default is BzrBranch._initialize
 
41
        self.assertEqual(branch.BzrBranch._initialize, old_initializer)
 
42
        def recorder(url):
 
43
            return "a branch %s" % url
 
44
        branch.Branch.set_default_initializer(recorder)
 
45
        try:
 
46
            b = branch.Branch.create("memory:/")
 
47
            self.assertEqual("a branch memory:/", b)
 
48
        finally:
 
49
            branch.Branch.set_default_initializer(old_initializer)
 
50
        self.assertEqual(old_initializer, branch.Branch.get_default_initializer())
 
51
 
 
52
 
 
53
class SampleBranchFormat(branch.BzrBranchFormat):
118
54
    """A sample format
119
55
 
120
56
    this format is initializable, unsupported to aid in testing the 
125
61
        """See BzrBranchFormat.get_format_string()."""
126
62
        return "Sample branch format."
127
63
 
128
 
    def initialize(self, a_bzrdir):
 
64
    def initialize(self, url):
129
65
        """Format 4 branches cannot be created."""
130
 
        t = a_bzrdir.get_branch_transport(self)
131
 
        t.put('format', StringIO(self.get_format_string()))
 
66
        t = get_transport(url)
 
67
        t.mkdir('.bzr')
 
68
        t.put('.bzr/branch-format', StringIO(self.get_format_string()))
132
69
        return 'A branch'
133
70
 
134
71
    def is_supported(self):
135
72
        return False
136
73
 
137
 
    def open(self, transport, _found=False):
 
74
    def open(self, transport):
138
75
        return "opened branch."
139
76
 
140
77
 
141
 
class TestBzrBranchFormat(TestCaseWithTransport):
 
78
class TestBzrBranchFormat(TestCaseInTempDir):
142
79
    """Tests for the BzrBranchFormat facility."""
143
80
 
144
81
    def test_find_format(self):
147
84
        # this is not quite the same as 
148
85
        self.build_tree(["foo/", "bar/"])
149
86
        def check_format(format, url):
150
 
            dir = format._matchingbzrdir.initialize(url)
151
 
            dir.create_repository()
152
 
            format.initialize(dir)
153
 
            found_format = bzrlib.branch.BranchFormat.find_format(dir)
 
87
            format.initialize(url)
 
88
            t = get_transport(url)
 
89
            found_format = branch.BzrBranchFormat.find_format(t)
154
90
            self.failUnless(isinstance(found_format, format.__class__))
155
 
        check_format(bzrlib.branch.BzrBranchFormat5(), "bar")
 
91
        check_format(branch.BzrBranchFormat5(), "foo")
 
92
        check_format(branch.BzrBranchFormat6(), "bar")
156
93
        
157
94
    def test_find_format_not_branch(self):
158
 
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
159
95
        self.assertRaises(NotBranchError,
160
 
                          bzrlib.branch.BranchFormat.find_format,
161
 
                          dir)
 
96
                          branch.BzrBranchFormat.find_format,
 
97
                          get_transport('.'))
162
98
 
163
99
    def test_find_format_unknown_format(self):
164
 
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
165
 
        SampleBranchFormat().initialize(dir)
 
100
        t = get_transport('.')
 
101
        t.mkdir('.bzr')
 
102
        t.put('.bzr/branch-format', StringIO())
166
103
        self.assertRaises(UnknownFormatError,
167
 
                          bzrlib.branch.BranchFormat.find_format,
168
 
                          dir)
 
104
                          branch.BzrBranchFormat.find_format,
 
105
                          get_transport('.'))
169
106
 
170
107
    def test_register_unregister_format(self):
171
108
        format = SampleBranchFormat()
172
 
        # make a control dir
173
 
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
174
109
        # make a branch
175
 
        format.initialize(dir)
 
110
        format.initialize('.')
176
111
        # register a format for it.
177
 
        bzrlib.branch.BranchFormat.register_format(format)
 
112
        branch.BzrBranchFormat.register_format(format)
178
113
        # which branch.Open will refuse (not supported)
179
 
        self.assertRaises(UnsupportedFormatError, bzrlib.branch.Branch.open, self.get_url())
 
114
        self.assertRaises(UnsupportedFormatError, branch.Branch.open, '.')
180
115
        # but open_downlevel will work
181
 
        self.assertEqual(format.open(dir), bzrdir.BzrDir.open(self.get_url()).open_branch(unsupported=True))
 
116
        t = get_transport('.')
 
117
        self.assertEqual(format.open(t), branch.Branch.open_downlevel('.'))
182
118
        # unregister the format
183
 
        bzrlib.branch.BranchFormat.unregister_format(format)
184
 
 
185
 
 
186
 
class TestBranchReference(TestCaseWithTransport):
187
 
    """Tests for the branch reference facility."""
188
 
 
189
 
    def test_create_open_reference(self):
190
 
        bzrdirformat = bzrdir.BzrDirMetaFormat1()
191
 
        t = get_transport(self.get_url('.'))
192
 
        t.mkdir('repo')
193
 
        dir = bzrdirformat.initialize(self.get_url('repo'))
194
 
        dir.create_repository()
195
 
        target_branch = dir.create_branch()
196
 
        t.mkdir('branch')
197
 
        branch_dir = bzrdirformat.initialize(self.get_url('branch'))
198
 
        made_branch = bzrlib.branch.BranchReferenceFormat().initialize(branch_dir, target_branch)
199
 
        self.assertEqual(made_branch.base, target_branch.base)
200
 
        opened_branch = branch_dir.open_branch()
201
 
        self.assertEqual(opened_branch.base, target_branch.base)
 
119
        branch.BzrBranchFormat.unregister_format(format)
 
120
        # now open_downlevel should fail too.
 
121
        self.assertRaises(UnknownFormatError, branch.Branch.open_downlevel, '.')