~bzr-pqm/bzr/bzr.dev

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,
35
                           UnsupportedFormatError,
36
                           )
37
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
38
from bzrlib.tests import TestCase, TestCaseWithTransport
1534.4.4 by Robert Collins
Make BzrBranchFormat.find_format take a transport not a url for efficiency.
39
from bzrlib.transport import get_transport
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
40
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
41
class TestDefaultFormat(TestCase):
42
43
    def test_get_set_default_format(self):
1508.1.25 by Robert Collins
Update per review comments.
44
        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.
45
        # default is 5
1508.1.25 by Robert Collins
Update per review comments.
46
        self.assertTrue(isinstance(old_format, bzrlib.branch.BzrBranchFormat5))
47
        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.
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
1685.1.42 by John Arbash Meinel
A couple more fixes to make sure memory:/// works correctly.
51
            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.
52
            result = dir.create_branch()
53
            self.assertEqual(result, 'A branch')
54
        finally:
1508.1.25 by Robert Collins
Update per review comments.
55
            bzrlib.branch.BranchFormat.set_default_format(old_format)
56
        self.assertEqual(old_format, bzrlib.branch.BranchFormat.get_default_format())
57
58
1553.5.71 by Martin Pool
Change branch format 5 to use LockDirs, not transport locks
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()
1553.5.72 by Martin Pool
Clean up test for Branch5 lockdirs
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)
1553.5.73 by Martin Pool
Additional test that Branch5 uses lockdir properly
73
        branch.lock_write()
1658.1.5 by Martin Pool
Release more locks taken during test suite
74
        try:
75
            self.assertIsDirectory('.bzr/branch/lock/held', t)
76
        finally:
77
            branch.unlock()
1553.5.71 by Martin Pool
Change branch format 5 to use LockDirs, not transport locks
78
79
1508.1.25 by Robert Collins
Update per review comments.
80
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.
81
    """A sample format
82
83
    this format is initializable, unsupported to aid in testing the 
84
    open and open_downlevel routines.
85
    """
86
87
    def get_format_string(self):
88
        """See BzrBranchFormat.get_format_string()."""
89
        return "Sample branch format."
90
91
    def initialize(self, a_bzrdir):
92
        """Format 4 branches cannot be created."""
93
        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
94
        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.
95
        return 'A branch'
96
97
    def is_supported(self):
98
        return False
99
100
    def open(self, transport, _found=False):
101
        return "opened branch."
102
103
104
class TestBzrBranchFormat(TestCaseWithTransport):
105
    """Tests for the BzrBranchFormat facility."""
106
107
    def test_find_format(self):
108
        # is the right format object found for a branch?
109
        # create a branch with a few known format objects.
110
        # this is not quite the same as 
111
        self.build_tree(["foo/", "bar/"])
112
        def check_format(format, url):
113
            dir = format._matchingbzrdir.initialize(url)
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
114
            dir.create_repository()
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
115
            format.initialize(dir)
1508.1.25 by Robert Collins
Update per review comments.
116
            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.
117
            self.failUnless(isinstance(found_format, format.__class__))
1508.1.25 by Robert Collins
Update per review comments.
118
        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.
119
        
120
    def test_find_format_not_branch(self):
121
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
122
        self.assertRaises(NotBranchError,
1508.1.25 by Robert Collins
Update per review comments.
123
                          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.
124
                          dir)
125
126
    def test_find_format_unknown_format(self):
127
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
128
        SampleBranchFormat().initialize(dir)
129
        self.assertRaises(UnknownFormatError,
1508.1.25 by Robert Collins
Update per review comments.
130
                          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.
131
                          dir)
132
133
    def test_register_unregister_format(self):
134
        format = SampleBranchFormat()
135
        # make a control dir
136
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
137
        # make a branch
138
        format.initialize(dir)
139
        # register a format for it.
1508.1.25 by Robert Collins
Update per review comments.
140
        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.
141
        # which branch.Open will refuse (not supported)
1508.1.25 by Robert Collins
Update per review comments.
142
        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.
143
        # 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.
144
        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.
145
        # unregister the format
1508.1.25 by Robert Collins
Update per review comments.
146
        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.
147
148
149
class TestBranchReference(TestCaseWithTransport):
150
    """Tests for the branch reference facility."""
151
152
    def test_create_open_reference(self):
153
        bzrdirformat = bzrdir.BzrDirMetaFormat1()
154
        t = get_transport(self.get_url('.'))
155
        t.mkdir('repo')
156
        dir = bzrdirformat.initialize(self.get_url('repo'))
157
        dir.create_repository()
158
        target_branch = dir.create_branch()
159
        t.mkdir('branch')
160
        branch_dir = bzrdirformat.initialize(self.get_url('branch'))
1508.1.25 by Robert Collins
Update per review comments.
161
        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.
162
        self.assertEqual(made_branch.base, target_branch.base)
163
        opened_branch = branch_dir.open_branch()
164
        self.assertEqual(opened_branch.base, target_branch.base)