/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: Aaron Bentley
  • Date: 2007-01-12 18:18:41 UTC
  • mto: (2230.3.47 branch6)
  • mto: This revision was merged to the branch mainline in revision 2290.
  • Revision ID: abentley@panoramicfeedback.com-20070112181841-94zoz6cv72clmsft
Get branch6 creation working

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 branch as _mod_branch
 
28
import bzrlib.branch
 
29
from bzrlib.branch import (BzrBranch5, 
 
30
                           BzrBranchFormat5)
 
31
import bzrlib.bzrdir as bzrdir
 
32
from bzrlib.bzrdir import (BzrDirMetaFormat1, BzrDirMeta1, 
 
33
                           BzrDir, BzrDirFormat)
 
34
from bzrlib.errors import (NotBranchError,
 
35
                           UnknownFormatError,
 
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 TestBranch6(TestCaseWithTransport):
 
151
 
 
152
    def test_creation(self):
 
153
        format = BzrDirMetaFormat1()
 
154
        format.branch_format = _mod_branch.BzrBranchFormat6()
 
155
        branch = self.make_branch('a', format=format)
 
156
        self.assertIsInstance(branch, _mod_branch.BzrBranch6)
 
157
        branch = self.make_branch('b', format='experimental-branch6')
 
158
        self.assertIsInstance(branch, _mod_branch.BzrBranch6)
 
159
        branch = _mod_branch.Branch.open('a')
 
160
        self.assertIsInstance(branch, _mod_branch.BzrBranch6)
 
161
 
 
162
    def test_layout(self):
 
163
        branch = self.make_branch('a', format='experimental-branch6')
 
164
        self.failUnlessExists('a/.bzr/branch/last-revision')
 
165
        self.failIfExists('a/.bzr/branch/revision-history')
 
166
 
 
167
 
 
168
 
 
169
 
 
170
class TestBranchReference(TestCaseWithTransport):
 
171
    """Tests for the branch reference facility."""
 
172
 
 
173
    def test_create_open_reference(self):
 
174
        bzrdirformat = bzrdir.BzrDirMetaFormat1()
 
175
        t = get_transport(self.get_url('.'))
 
176
        t.mkdir('repo')
 
177
        dir = bzrdirformat.initialize(self.get_url('repo'))
 
178
        dir.create_repository()
 
179
        target_branch = dir.create_branch()
 
180
        t.mkdir('branch')
 
181
        branch_dir = bzrdirformat.initialize(self.get_url('branch'))
 
182
        made_branch = bzrlib.branch.BranchReferenceFormat().initialize(branch_dir, target_branch)
 
183
        self.assertEqual(made_branch.base, target_branch.base)
 
184
        opened_branch = branch_dir.open_branch()
 
185
        self.assertEqual(opened_branch.base, target_branch.base)