/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1
# (C) 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 Repository facility that are not interface tests.
18
19
For interface tests see tests/repository_implementations/*.py.
20
21
For concrete class tests see this file, and for storage formats tests
22
also see this file.
23
"""
24
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
25
from stat import *
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
26
from StringIO import StringIO
27
28
import bzrlib.bzrdir as bzrdir
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
29
import bzrlib.errors as errors
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
30
from bzrlib.errors import (NotBranchError,
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
31
                           NoSuchFile,
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
32
                           UnknownFormatError,
33
                           UnsupportedFormatError,
34
                           )
35
import bzrlib.repository as repository
36
from bzrlib.tests import TestCase, TestCaseWithTransport
37
from bzrlib.transport import get_transport
38
from bzrlib.transport.http import HttpServer
39
from bzrlib.transport.memory import MemoryServer
40
41
42
class TestDefaultFormat(TestCase):
43
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
44
    def test_get_set_default_format(self):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
45
        old_format = repository.RepositoryFormat.get_default_format()
46
        # default is None - we cannot create a Repository independently yet
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
47
        self.assertTrue(isinstance(old_format, repository.RepositoryFormat7))
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
48
        repository.RepositoryFormat.set_default_format(SampleRepositoryFormat())
49
        # creating a repository should now create an instrumented dir.
50
        try:
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
51
            # the default branch format is used by the meta dir format
52
            # which is not the default bzrdir format at this point
53
            dir = bzrdir.BzrDirMetaFormat1().initialize('memory:/')
54
            result = dir.create_repository()
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
55
            self.assertEqual(result, 'A bzr repository dir')
56
        finally:
57
            repository.RepositoryFormat.set_default_format(old_format)
58
        self.assertEqual(old_format, repository.RepositoryFormat.get_default_format())
59
60
61
class SampleRepositoryFormat(repository.RepositoryFormat):
62
    """A sample format
63
64
    this format is initializable, unsupported to aid in testing the 
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
65
    open and open(unsupported=True) routines.
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
66
    """
67
68
    def get_format_string(self):
69
        """See RepositoryFormat.get_format_string()."""
70
        return "Sample .bzr repository format."
71
1534.6.1 by Robert Collins
allow API creation of shared repositories
72
    def initialize(self, a_bzrdir, shared=False):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
73
        """Initialize a repository in a BzrDir"""
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
74
        t = a_bzrdir.get_repository_transport(self)
75
        t.put('format', StringIO(self.get_format_string()))
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
76
        return 'A bzr repository dir'
77
78
    def is_supported(self):
79
        return False
80
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
81
    def open(self, a_bzrdir, _found=False):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
82
        return "opened repository."
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
83
84
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
85
class TestRepositoryFormat(TestCaseWithTransport):
86
    """Tests for the Repository format detection used by the bzr meta dir facility.BzrBranchFormat facility."""
87
88
    def test_find_format(self):
89
        # is the right format object found for a repository?
90
        # create a branch with a few known format objects.
91
        # this is not quite the same as 
92
        self.build_tree(["foo/", "bar/"])
93
        def check_format(format, url):
94
            dir = format._matchingbzrdir.initialize(url)
95
            format.initialize(dir)
96
            t = get_transport(url)
97
            found_format = repository.RepositoryFormat.find_format(dir)
98
            self.failUnless(isinstance(found_format, format.__class__))
99
        check_format(repository.RepositoryFormat7(), "bar")
100
        
101
    def test_find_format_no_repository(self):
102
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
103
        self.assertRaises(errors.NoRepositoryPresent,
104
                          repository.RepositoryFormat.find_format,
105
                          dir)
106
107
    def test_find_format_unknown_format(self):
108
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
109
        SampleRepositoryFormat().initialize(dir)
110
        self.assertRaises(UnknownFormatError,
111
                          repository.RepositoryFormat.find_format,
112
                          dir)
113
114
    def test_register_unregister_format(self):
115
        format = SampleRepositoryFormat()
116
        # make a control dir
117
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
118
        # make a repo
119
        format.initialize(dir)
120
        # register a format for it.
121
        repository.RepositoryFormat.register_format(format)
122
        # which repository.Open will refuse (not supported)
123
        self.assertRaises(UnsupportedFormatError, repository.Repository.open, self.get_url())
124
        # but open(unsupported) will work
125
        self.assertEqual(format.open(dir), "opened repository.")
126
        # unregister the format
127
        repository.RepositoryFormat.unregister_format(format)
128
129
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
130
class TestFormat6(TestCaseWithTransport):
131
132
    def test_no_ancestry_weave(self):
133
        control = bzrdir.BzrDirFormat6().initialize(self.get_url())
134
        repo = repository.RepositoryFormat6().initialize(control)
135
        # We no longer need to create the ancestry.weave file
136
        # since it is *never* used.
137
        self.assertRaises(NoSuchFile,
138
                          control.transport.get,
139
                          'ancestry.weave')
140
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
141
142
class TestFormat7(TestCaseWithTransport):
143
    
144
    def test_disk_layout(self):
145
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
146
        repo = repository.RepositoryFormat7().initialize(control)
1534.5.3 by Robert Collins
Make format 4/5/6 branches share a single LockableFiles instance across wt/branch/repository.
147
        # in case of side effects of locking.
148
        repo.lock_write()
149
        repo.unlock()
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
150
        # we want:
151
        # format 'Bazaar-NG Repository format 7'
152
        # lock ''
153
        # inventory.weave == empty_weave
154
        # empty revision-store directory
155
        # empty weaves directory
156
        t = control.get_repository_transport(None)
157
        self.assertEqualDiff('Bazaar-NG Repository format 7',
158
                             t.get('format').read())
159
        self.assertEqualDiff('', t.get('lock').read())
160
        self.assertTrue(S_ISDIR(t.stat('revision-store').st_mode))
161
        self.assertTrue(S_ISDIR(t.stat('weaves').st_mode))
162
        self.assertEqualDiff('# bzr weave file v5\n'
163
                             'w\n'
164
                             'W\n',
165
                             t.get('inventory.weave').read())
1534.6.1 by Robert Collins
allow API creation of shared repositories
166
167
    def test_shared_disk_layout(self):
168
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
169
        repo = repository.RepositoryFormat7().initialize(control, shared=True)
170
        # we want:
171
        # format 'Bazaar-NG Repository format 7'
172
        # lock ''
173
        # inventory.weave == empty_weave
174
        # empty revision-store directory
175
        # empty weaves directory
176
        # a 'shared-storage' marker file.
177
        t = control.get_repository_transport(None)
178
        self.assertEqualDiff('Bazaar-NG Repository format 7',
179
                             t.get('format').read())
180
        self.assertEqualDiff('', t.get('lock').read())
181
        self.assertEqualDiff('', t.get('shared-storage').read())
182
        self.assertTrue(S_ISDIR(t.stat('revision-store').st_mode))
183
        self.assertTrue(S_ISDIR(t.stat('weaves').st_mode))
184
        self.assertEqualDiff('# bzr weave file v5\n'
185
                             'w\n'
186
                             'W\n',
187
                             t.get('inventory.weave').read())
1534.6.5 by Robert Collins
Cloning of repos preserves shared and make-working-tree attributes.
188
189
    def test_shared_no_tree_disk_layout(self):
190
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
191
        repo = repository.RepositoryFormat7().initialize(control, shared=True)
192
        repo.set_make_working_trees(False)
193
        # we want:
194
        # format 'Bazaar-NG Repository format 7'
195
        # lock ''
196
        # inventory.weave == empty_weave
197
        # empty revision-store directory
198
        # empty weaves directory
199
        # a 'shared-storage' marker file.
200
        t = control.get_repository_transport(None)
201
        self.assertEqualDiff('Bazaar-NG Repository format 7',
202
                             t.get('format').read())
203
        self.assertEqualDiff('', t.get('lock').read())
204
        self.assertEqualDiff('', t.get('shared-storage').read())
205
        self.assertEqualDiff('', t.get('no-working-trees').read())
206
        repo.set_make_working_trees(True)
207
        self.assertFalse(t.has('no-working-trees'))
208
        self.assertTrue(S_ISDIR(t.stat('revision-store').st_mode))
209
        self.assertTrue(S_ISDIR(t.stat('weaves').st_mode))
210
        self.assertEqualDiff('# bzr weave file v5\n'
211
                             'w\n'
212
                             'W\n',
213
                             t.get('inventory.weave').read())