/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1553.5.71 by Martin Pool
Change branch format 5 to use LockDirs, not transport locks
1
# Copyright (C) 2005, 2006 Canonical Ltd
2
#
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
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.
1553.5.71 by Martin Pool
Change branch format 5 to use LockDirs, not transport locks
7
#
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
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.
1553.5.71 by Martin Pool
Change branch format 5 to use LockDirs, not transport locks
12
#
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
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
1534.4.39 by Robert Collins
Basic BzrDir support.
19
For interface tests see tests/branch_implementations/*.py.
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
20
21
For concrete class tests see this file, and for meta-branch tests
22
also see this file.
23
"""
24
1534.4.7 by Robert Collins
Move downlevel check up to the Branch.open logic, removing it from the Branch constructor and deprecating relax_version_check to the same.
25
from StringIO import StringIO
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
26
1508.1.25 by Robert Collins
Update per review comments.
27
import bzrlib.branch
1553.5.71 by Martin Pool
Change branch format 5 to use LockDirs, not transport locks
28
from bzrlib.branch import (BzrBranch5, 
29
                           BzrBranchFormat5)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
30
import bzrlib.bzrdir as bzrdir
1553.5.71 by Martin Pool
Change branch format 5 to use LockDirs, not transport locks
31
from bzrlib.bzrdir import (BzrDirMetaFormat1, BzrDirMeta1, 
32
                           BzrDir, BzrDirFormat)
1534.4.7 by Robert Collins
Move downlevel check up to the Branch.open logic, removing it from the Branch constructor and deprecating relax_version_check to the same.
33
from bzrlib.errors import (NotBranchError,
34
                           UnknownFormatError,
2245.1.3 by Robert Collins
Add install_hook to the BranchHooks class as the official means for installing a hook.
35
                           UnknownHook,
1534.4.7 by Robert Collins
Move downlevel check up to the Branch.open logic, removing it from the Branch constructor and deprecating relax_version_check to the same.
36
                           UnsupportedFormatError,
37
                           )
38
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
39
from bzrlib.tests import TestCase, TestCaseWithTransport
1534.4.4 by Robert Collins
Make BzrBranchFormat.find_format take a transport not a url for efficiency.
40
from bzrlib.transport import get_transport
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
41
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
42
class TestDefaultFormat(TestCase):
43
44
    def test_get_set_default_format(self):
1508.1.25 by Robert Collins
Update per review comments.
45
        old_format = bzrlib.branch.BranchFormat.get_default_format()
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
46
        # default is 5
1508.1.25 by Robert Collins
Update per review comments.
47
        self.assertTrue(isinstance(old_format, bzrlib.branch.BzrBranchFormat5))
48
        bzrlib.branch.BranchFormat.set_default_format(SampleBranchFormat())
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
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
1685.1.42 by John Arbash Meinel
A couple more fixes to make sure memory:/// works correctly.
52
            dir = BzrDirMetaFormat1().initialize('memory:///')
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
53
            result = dir.create_branch()
54
            self.assertEqual(result, 'A branch')
55
        finally:
1508.1.25 by Robert Collins
Update per review comments.
56
            bzrlib.branch.BranchFormat.set_default_format(old_format)
57
        self.assertEqual(old_format, bzrlib.branch.BranchFormat.get_default_format())
58
59
1553.5.71 by Martin Pool
Change branch format 5 to use LockDirs, not transport locks
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()
1553.5.72 by Martin Pool
Clean up test for Branch5 lockdirs
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)
1553.5.73 by Martin Pool
Additional test that Branch5 uses lockdir properly
74
        branch.lock_write()
1658.1.5 by Martin Pool
Release more locks taken during test suite
75
        try:
76
            self.assertIsDirectory('.bzr/branch/lock/held', t)
77
        finally:
78
            branch.unlock()
1553.5.71 by Martin Pool
Change branch format 5 to use LockDirs, not transport locks
79
80
1508.1.25 by Robert Collins
Update per review comments.
81
class SampleBranchFormat(bzrlib.branch.BranchFormat):
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
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)
1955.3.9 by John Arbash Meinel
Find more occurrances of put() and replace with put_file or put_bytes
95
        t.put_bytes('format', self.get_format_string())
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
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)
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
115
            dir.create_repository()
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
116
            format.initialize(dir)
1508.1.25 by Robert Collins
Update per review comments.
117
            found_format = bzrlib.branch.BranchFormat.find_format(dir)
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
118
            self.failUnless(isinstance(found_format, format.__class__))
1508.1.25 by Robert Collins
Update per review comments.
119
        check_format(bzrlib.branch.BzrBranchFormat5(), "bar")
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
120
        
121
    def test_find_format_not_branch(self):
122
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
123
        self.assertRaises(NotBranchError,
1508.1.25 by Robert Collins
Update per review comments.
124
                          bzrlib.branch.BranchFormat.find_format,
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
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,
1508.1.25 by Robert Collins
Update per review comments.
131
                          bzrlib.branch.BranchFormat.find_format,
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
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.
1508.1.25 by Robert Collins
Update per review comments.
141
        bzrlib.branch.BranchFormat.register_format(format)
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
142
        # which branch.Open will refuse (not supported)
1508.1.25 by Robert Collins
Update per review comments.
143
        self.assertRaises(UnsupportedFormatError, bzrlib.branch.Branch.open, self.get_url())
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
144
        # but open_downlevel will work
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
145
        self.assertEqual(format.open(dir), bzrdir.BzrDir.open(self.get_url()).open_branch(unsupported=True))
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
146
        # unregister the format
1508.1.25 by Robert Collins
Update per review comments.
147
        bzrlib.branch.BranchFormat.unregister_format(format)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
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'))
1508.1.25 by Robert Collins
Update per review comments.
162
        made_branch = bzrlib.branch.BranchReferenceFormat().initialize(branch_dir, target_branch)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
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)
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
166
167
    def test_get_reference(self):
168
        """For a BranchReference, get_reference should reutrn the location."""
169
        branch = self.make_branch('target')
170
        checkout = branch.create_checkout('checkout', lightweight=True)
171
        reference_url = branch.bzrdir.root_transport.abspath('') + '/'
172
        # if the api for create_checkout changes to return different checkout types
173
        # then this file read will fail.
174
        self.assertFileEqual(reference_url, 'checkout/.bzr/branch/location')
175
        self.assertEqual(reference_url,
176
            bzrlib.branch.BranchReferenceFormat().get_reference(checkout.bzrdir))
2018.5.45 by Andrew Bennetts
Merge from bzr.dev
177
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
178
179
class TestHooks(TestCase):
180
2245.1.2 by Robert Collins
Remove the static DefaultHooks method from Branch, replacing it with a derived dict BranchHooks object, which is easier to use and provides a place to put the policy-checking add method discussed on list.
181
    def test_constructor(self):
182
        """Check that creating a BranchHooks instance has the right defaults."""
183
        hooks = bzrlib.branch.BranchHooks()
184
        self.assertTrue("set_rh" in hooks, "set_rh not in %s" % hooks)
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
185
2245.1.2 by Robert Collins
Remove the static DefaultHooks method from Branch, replacing it with a derived dict BranchHooks object, which is easier to use and provides a place to put the policy-checking add method discussed on list.
186
    def test_installed_hooks_are_BranchHooks(self):
187
        """The installed hooks object should be a BranchHooks."""
188
        # the installed hooks are saved in self._preserved_hooks.
189
        self.assertIsInstance(self._preserved_hooks, bzrlib.branch.BranchHooks)
2245.1.3 by Robert Collins
Add install_hook to the BranchHooks class as the official means for installing a hook.
190
191
    def test_install_hook_raises_unknown_hook(self):
192
        """install_hook should raise UnknownHook if a hook is unknown."""
193
        hooks = bzrlib.branch.BranchHooks()
194
        self.assertRaises(UnknownHook, hooks.install_hook, 'silly', None)
195
196
    def test_install_hook_appends_known_hook(self):
197
        """install_hook should append the callable for known hooks."""
198
        hooks = bzrlib.branch.BranchHooks()
199
        hooks.install_hook('set_rh', None)
200
        self.assertEqual(hooks['set_rh'], [None])