/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1553.5.69 by Martin Pool
BzrDirFormat subclasses can now control what kind of overall lock is used.
1
# Copyright (C) 2005, 2006 Canonical Ltd
1553.5.68 by Martin Pool
Add new TestCaseWithTransport.assertIsDirectory() and tests
2
# 
1534.4.39 by Robert Collins
Basic BzrDir support.
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.
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
7
#
1534.4.39 by Robert Collins
Basic BzrDir support.
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.
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
12
#
1534.4.39 by Robert Collins
Basic BzrDir support.
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 BzrDir facility and any format specific tests.
18
19
For interface contract tests, see tests/bzr_dir_implementations.
20
"""
21
22
from StringIO import StringIO
23
2204.4.1 by Aaron Bentley
Add 'formats' help topic
24
from bzrlib import (
25
    help_topics,
26
    )
1508.1.25 by Robert Collins
Update per review comments.
27
import bzrlib.branch
1534.4.39 by Robert Collins
Basic BzrDir support.
28
import bzrlib.bzrdir as bzrdir
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
29
import bzrlib.errors as errors
1534.4.39 by Robert Collins
Basic BzrDir support.
30
from bzrlib.errors import (NotBranchError,
31
                           UnknownFormatError,
32
                           UnsupportedFormatError,
33
                           )
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
34
import bzrlib.repository as repository
1534.4.39 by Robert Collins
Basic BzrDir support.
35
from bzrlib.tests import TestCase, TestCaseWithTransport
2004.1.25 by v.ladeuil+lp at free
Shuffle http related test code. Hopefully it ends up at the right place :)
36
from bzrlib.tests.HttpServer import HttpServer
1534.4.39 by Robert Collins
Basic BzrDir support.
37
from bzrlib.transport import get_transport
38
from bzrlib.transport.memory import MemoryServer
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
39
import bzrlib.workingtree as workingtree
1534.4.39 by Robert Collins
Basic BzrDir support.
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.39 by Robert Collins
Basic BzrDir support.
45
        old_format = bzrdir.BzrDirFormat.get_default_format()
46
        # default is BzrDirFormat6
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
47
        self.failUnless(isinstance(old_format, bzrdir.BzrDirMetaFormat1))
1534.4.39 by Robert Collins
Basic BzrDir support.
48
        bzrdir.BzrDirFormat.set_default_format(SampleBzrDirFormat())
49
        # creating a bzr dir should now create an instrumented dir.
50
        try:
1685.1.42 by John Arbash Meinel
A couple more fixes to make sure memory:/// works correctly.
51
            result = bzrdir.BzrDir.create('memory:///')
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
52
            self.failUnless(isinstance(result, SampleBzrDir))
1534.4.39 by Robert Collins
Basic BzrDir support.
53
        finally:
54
            bzrdir.BzrDirFormat.set_default_format(old_format)
55
        self.assertEqual(old_format, bzrdir.BzrDirFormat.get_default_format())
56
57
2204.4.1 by Aaron Bentley
Add 'formats' help topic
58
class TestFormatRegistry(TestCase):
59
60
    def make_format_registry(self):
61
        my_format_registry = bzrdir.BzrDirFormatRegistry()
2204.4.7 by Aaron Bentley
restore register_lazy, remove register_factory, other updates
62
        my_format_registry.register('weave', bzrdir.BzrDirFormat6,
2204.4.1 by Aaron Bentley
Add 'formats' help topic
63
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
2204.4.4 by Aaron Bentley
Use BzrDirFormatInfo to distinguish native and deprecated formats
64
            ' repositories', deprecated=True)
2204.4.7 by Aaron Bentley
restore register_lazy, remove register_factory, other updates
65
        my_format_registry.register_lazy('lazy', 'bzrlib.bzrdir', 
66
            'BzrDirFormat6', 'Format registered lazily', deprecated=True)
2204.4.1 by Aaron Bentley
Add 'formats' help topic
67
        my_format_registry.register_metadir('knit', 'RepositoryFormatKnit1',
2204.4.4 by Aaron Bentley
Use BzrDirFormatInfo to distinguish native and deprecated formats
68
            'Format using knits')
2204.4.1 by Aaron Bentley
Add 'formats' help topic
69
        my_format_registry.set_default('knit')
70
        my_format_registry.register_metadir('metaweave', 'RepositoryFormat7',
2204.4.4 by Aaron Bentley
Use BzrDirFormatInfo to distinguish native and deprecated formats
71
            'Transitional format in 0.8.  Slower than knit.', deprecated=True)
2204.4.1 by Aaron Bentley
Add 'formats' help topic
72
        my_format_registry.register_metadir('experimental-knit2', 
73
                                            'RepositoryFormatKnit2',
74
            'Experimental successor to knit.  Use at your own risk.')
75
        return my_format_registry
76
77
    def test_format_registry(self):
78
        my_format_registry = self.make_format_registry()
2204.4.7 by Aaron Bentley
restore register_lazy, remove register_factory, other updates
79
        my_bzrdir = my_format_registry.make_bzrdir('lazy')
80
        self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
2204.4.1 by Aaron Bentley
Add 'formats' help topic
81
        my_bzrdir = my_format_registry.make_bzrdir('weave')
2204.4.7 by Aaron Bentley
restore register_lazy, remove register_factory, other updates
82
        self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
2204.4.1 by Aaron Bentley
Add 'formats' help topic
83
        my_bzrdir = my_format_registry.make_bzrdir('default')
2204.4.7 by Aaron Bentley
restore register_lazy, remove register_factory, other updates
84
        self.assertIsInstance(my_bzrdir.repository_format, 
85
            repository.RepositoryFormatKnit1)
2204.4.1 by Aaron Bentley
Add 'formats' help topic
86
        my_bzrdir = my_format_registry.make_bzrdir('knit')
2204.4.7 by Aaron Bentley
restore register_lazy, remove register_factory, other updates
87
        self.assertIsInstance(my_bzrdir.repository_format, 
88
            repository.RepositoryFormatKnit1)
2204.4.1 by Aaron Bentley
Add 'formats' help topic
89
        my_bzrdir = my_format_registry.make_bzrdir('metaweave')
2204.4.7 by Aaron Bentley
restore register_lazy, remove register_factory, other updates
90
        self.assertIsInstance(my_bzrdir.repository_format, 
91
            repository.RepositoryFormat7)
2204.4.1 by Aaron Bentley
Add 'formats' help topic
92
93
    def test_get_help(self):
94
        my_format_registry = self.make_format_registry()
2204.4.7 by Aaron Bentley
restore register_lazy, remove register_factory, other updates
95
        self.assertEqual('Format registered lazily',
96
                         my_format_registry.get_help('lazy'))
2204.4.4 by Aaron Bentley
Use BzrDirFormatInfo to distinguish native and deprecated formats
97
        self.assertEqual('Format using knits', 
2204.4.1 by Aaron Bentley
Add 'formats' help topic
98
                         my_format_registry.get_help('knit'))
2204.4.4 by Aaron Bentley
Use BzrDirFormatInfo to distinguish native and deprecated formats
99
        self.assertEqual('Format using knits', 
2204.4.1 by Aaron Bentley
Add 'formats' help topic
100
                         my_format_registry.get_help('default'))
101
        self.assertEqual('Pre-0.8 format.  Slower and does not support'
102
                         ' checkouts or shared repositories', 
103
                         my_format_registry.get_help('weave'))
104
        
105
    def test_help_topic(self):
106
        topics = help_topics.HelpTopicRegistry()
107
        topics.register('formats', self.make_format_registry().help_topic, 
108
                        'Directory formats')
109
        topic = topics.get_detail('formats')
2204.4.4 by Aaron Bentley
Use BzrDirFormatInfo to distinguish native and deprecated formats
110
        new, deprecated = topic.split('Deprecated formats')
111
        self.assertContainsRe(new, 'Bazaar directory formats')
112
        self.assertContainsRe(new, 
113
            '  knit/default:\n    \(native\) Format using knits\n')
114
        self.assertContainsRe(deprecated, 
2204.4.7 by Aaron Bentley
restore register_lazy, remove register_factory, other updates
115
            '  lazy:\n    \(native\) Format registered lazily\n')
2204.4.1 by Aaron Bentley
Add 'formats' help topic
116
1508.1.25 by Robert Collins
Update per review comments.
117
class SampleBranch(bzrlib.branch.Branch):
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
118
    """A dummy branch for guess what, dummy use."""
119
120
    def __init__(self, dir):
121
        self.bzrdir = dir
122
123
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
124
class SampleBzrDir(bzrdir.BzrDir):
125
    """A sample BzrDir implementation to allow testing static methods."""
126
1841.2.1 by Jelmer Vernooij
Fix handling of `shared' parameter in BzrDir.create_repository().
127
    def create_repository(self, shared=False):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
128
        """See BzrDir.create_repository."""
129
        return "A repository"
130
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
131
    def open_repository(self):
132
        """See BzrDir.open_repository."""
133
        return "A repository"
134
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
135
    def create_branch(self):
136
        """See BzrDir.create_branch."""
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
137
        return SampleBranch(self)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
138
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
139
    def create_workingtree(self):
140
        """See BzrDir.create_workingtree."""
141
        return "A tree"
142
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
143
1534.4.39 by Robert Collins
Basic BzrDir support.
144
class SampleBzrDirFormat(bzrdir.BzrDirFormat):
145
    """A sample format
146
147
    this format is initializable, unsupported to aid in testing the 
148
    open and open_downlevel routines.
149
    """
150
151
    def get_format_string(self):
152
        """See BzrDirFormat.get_format_string()."""
153
        return "Sample .bzr dir format."
154
155
    def initialize(self, url):
156
        """Create a bzr dir."""
157
        t = get_transport(url)
158
        t.mkdir('.bzr')
1955.3.9 by John Arbash Meinel
Find more occurrances of put() and replace with put_file or put_bytes
159
        t.put_bytes('.bzr/branch-format', self.get_format_string())
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
160
        return SampleBzrDir(t, self)
1534.4.39 by Robert Collins
Basic BzrDir support.
161
162
    def is_supported(self):
163
        return False
164
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
165
    def open(self, transport, _found=None):
1534.4.39 by Robert Collins
Basic BzrDir support.
166
        return "opened branch."
167
168
169
class TestBzrDirFormat(TestCaseWithTransport):
170
    """Tests for the BzrDirFormat facility."""
171
172
    def test_find_format(self):
173
        # is the right format object found for a branch?
174
        # create a branch with a few known format objects.
175
        # this is not quite the same as 
176
        t = get_transport(self.get_url())
177
        self.build_tree(["foo/", "bar/"], transport=t)
178
        def check_format(format, url):
179
            format.initialize(url)
180
            t = get_transport(url)
181
            found_format = bzrdir.BzrDirFormat.find_format(t)
182
            self.failUnless(isinstance(found_format, format.__class__))
183
        check_format(bzrdir.BzrDirFormat5(), "foo")
184
        check_format(bzrdir.BzrDirFormat6(), "bar")
185
        
186
    def test_find_format_nothing_there(self):
187
        self.assertRaises(NotBranchError,
188
                          bzrdir.BzrDirFormat.find_format,
189
                          get_transport('.'))
190
191
    def test_find_format_unknown_format(self):
192
        t = get_transport(self.get_url())
193
        t.mkdir('.bzr')
1955.3.13 by John Arbash Meinel
Run the full test suite, and fix up any deprecation warnings.
194
        t.put_bytes('.bzr/branch-format', '')
1534.4.39 by Robert Collins
Basic BzrDir support.
195
        self.assertRaises(UnknownFormatError,
196
                          bzrdir.BzrDirFormat.find_format,
197
                          get_transport('.'))
198
199
    def test_register_unregister_format(self):
200
        format = SampleBzrDirFormat()
201
        url = self.get_url()
202
        # make a bzrdir
203
        format.initialize(url)
204
        # register a format for it.
205
        bzrdir.BzrDirFormat.register_format(format)
206
        # which bzrdir.Open will refuse (not supported)
207
        self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open, url)
1596.2.1 by Robert Collins
Fix BzrDir.open_containing of unsupported branches.
208
        # which bzrdir.open_containing will refuse (not supported)
209
        self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open_containing, url)
1534.4.39 by Robert Collins
Basic BzrDir support.
210
        # but open_downlevel will work
211
        t = get_transport(url)
212
        self.assertEqual(format.open(t), bzrdir.BzrDir.open_unsupported(url))
213
        # unregister the format
214
        bzrdir.BzrDirFormat.unregister_format(format)
215
        # now open_downlevel should fail too.
216
        self.assertRaises(UnknownFormatError, bzrdir.BzrDir.open_unsupported, url)
217
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
218
    def test_create_repository(self):
219
        format = SampleBzrDirFormat()
220
        old_format = bzrdir.BzrDirFormat.get_default_format()
221
        bzrdir.BzrDirFormat.set_default_format(format)
222
        try:
223
            repo = bzrdir.BzrDir.create_repository(self.get_url())
224
            self.assertEqual('A repository', repo)
225
        finally:
226
            bzrdir.BzrDirFormat.set_default_format(old_format)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
227
1841.2.1 by Jelmer Vernooij
Fix handling of `shared' parameter in BzrDir.create_repository().
228
    def test_create_repository_shared(self):
229
        old_format = bzrdir.BzrDirFormat.get_default_format()
230
        repo = bzrdir.BzrDir.create_repository('.', shared=True)
231
        self.assertTrue(repo.is_shared())
232
1841.2.2 by Jelmer Vernooij
Add more tests for create_repository().
233
    def test_create_repository_nonshared(self):
234
        old_format = bzrdir.BzrDirFormat.get_default_format()
235
        repo = bzrdir.BzrDir.create_repository('.')
236
        self.assertFalse(repo.is_shared())
237
1534.6.10 by Robert Collins
Finish use of repositories support.
238
    def test_create_repository_under_shared(self):
239
        # an explicit create_repository always does so.
240
        # we trust the format is right from the 'create_repository test'
241
        old_format = bzrdir.BzrDirFormat.get_default_format()
242
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
243
        try:
244
            self.make_repository('.', shared=True)
245
            repo = bzrdir.BzrDir.create_repository(self.get_url('child'))
246
            self.assertTrue(isinstance(repo, repository.Repository))
247
            self.assertTrue(repo.bzrdir.root_transport.base.endswith('child/'))
248
        finally:
249
            bzrdir.BzrDirFormat.set_default_format(old_format)
250
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
251
    def test_create_branch_and_repo_uses_default(self):
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
252
        format = SampleBzrDirFormat()
253
        old_format = bzrdir.BzrDirFormat.get_default_format()
254
        bzrdir.BzrDirFormat.set_default_format(format)
255
        try:
256
            branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url())
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
257
            self.assertTrue(isinstance(branch, SampleBranch))
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
258
        finally:
259
            bzrdir.BzrDirFormat.set_default_format(old_format)
260
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
261
    def test_create_branch_and_repo_under_shared(self):
262
        # creating a branch and repo in a shared repo uses the
263
        # shared repository
264
        old_format = bzrdir.BzrDirFormat.get_default_format()
265
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
266
        try:
267
            self.make_repository('.', shared=True)
268
            branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url('child'))
269
            self.assertRaises(errors.NoRepositoryPresent,
270
                              branch.bzrdir.open_repository)
271
        finally:
272
            bzrdir.BzrDirFormat.set_default_format(old_format)
273
274
    def test_create_branch_and_repo_under_shared_force_new(self):
275
        # creating a branch and repo in a shared repo can be forced to 
276
        # make a new repo
277
        old_format = bzrdir.BzrDirFormat.get_default_format()
278
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
279
        try:
280
            self.make_repository('.', shared=True)
281
            branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url('child'),
282
                                                          force_new_repo=True)
283
            branch.bzrdir.open_repository()
284
        finally:
285
            bzrdir.BzrDirFormat.set_default_format(old_format)
286
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
287
    def test_create_standalone_working_tree(self):
288
        format = SampleBzrDirFormat()
289
        old_format = bzrdir.BzrDirFormat.get_default_format()
290
        bzrdir.BzrDirFormat.set_default_format(format)
291
        try:
292
            # note this is deliberately readonly, as this failure should 
293
            # occur before any writes.
294
            self.assertRaises(errors.NotLocalUrl,
295
                              bzrdir.BzrDir.create_standalone_workingtree,
296
                              self.get_readonly_url())
297
            tree = bzrdir.BzrDir.create_standalone_workingtree('.')
298
            self.assertEqual('A tree', tree)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
299
        finally:
300
            bzrdir.BzrDirFormat.set_default_format(old_format)
301
1534.6.10 by Robert Collins
Finish use of repositories support.
302
    def test_create_standalone_working_tree_under_shared_repo(self):
303
        # create standalone working tree always makes a repo.
304
        old_format = bzrdir.BzrDirFormat.get_default_format()
305
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
306
        try:
307
            self.make_repository('.', shared=True)
308
            # note this is deliberately readonly, as this failure should 
309
            # occur before any writes.
310
            self.assertRaises(errors.NotLocalUrl,
311
                              bzrdir.BzrDir.create_standalone_workingtree,
312
                              self.get_readonly_url('child'))
313
            tree = bzrdir.BzrDir.create_standalone_workingtree('child')
314
            tree.bzrdir.open_repository()
315
        finally:
316
            bzrdir.BzrDirFormat.set_default_format(old_format)
317
318
    def test_create_branch_convenience(self):
1534.1.29 by Robert Collins
Add a test environment for InterRepository objects, and remove the fetch corner case tests from test_repository.
319
        # outside a repo the default convenience output is a repo+branch_tree
1534.6.10 by Robert Collins
Finish use of repositories support.
320
        old_format = bzrdir.BzrDirFormat.get_default_format()
321
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
322
        try:
323
            branch = bzrdir.BzrDir.create_branch_convenience('.')
324
            branch.bzrdir.open_workingtree()
325
            branch.bzrdir.open_repository()
326
        finally:
327
            bzrdir.BzrDirFormat.set_default_format(old_format)
328
1725.2.5 by Robert Collins
Bugfix create_branch_convenience at the root of a file system to not loop
329
    def test_create_branch_convenience_root(self):
330
        """Creating a branch at the root of a fs should work."""
331
        self.transport_server = MemoryServer
332
        # outside a repo the default convenience output is a repo+branch_tree
333
        old_format = bzrdir.BzrDirFormat.get_default_format()
334
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
335
        try:
336
            branch = bzrdir.BzrDir.create_branch_convenience(self.get_url())
337
            self.assertRaises(errors.NoWorkingTree,
338
                              branch.bzrdir.open_workingtree)
339
            branch.bzrdir.open_repository()
340
        finally:
341
            bzrdir.BzrDirFormat.set_default_format(old_format)
342
1534.6.10 by Robert Collins
Finish use of repositories support.
343
    def test_create_branch_convenience_under_shared_repo(self):
344
        # inside a repo the default convenience output is a branch+ follow the
345
        # repo tree policy
346
        old_format = bzrdir.BzrDirFormat.get_default_format()
347
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
348
        try:
349
            self.make_repository('.', shared=True)
350
            branch = bzrdir.BzrDir.create_branch_convenience('child')
351
            branch.bzrdir.open_workingtree()
352
            self.assertRaises(errors.NoRepositoryPresent,
353
                              branch.bzrdir.open_repository)
354
        finally:
355
            bzrdir.BzrDirFormat.set_default_format(old_format)
356
            
357
    def test_create_branch_convenience_under_shared_repo_force_no_tree(self):
358
        # inside a repo the default convenience output is a branch+ follow the
359
        # repo tree policy but we can override that
360
        old_format = bzrdir.BzrDirFormat.get_default_format()
361
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
362
        try:
363
            self.make_repository('.', shared=True)
364
            branch = bzrdir.BzrDir.create_branch_convenience('child',
365
                force_new_tree=False)
366
            self.assertRaises(errors.NoWorkingTree,
367
                              branch.bzrdir.open_workingtree)
368
            self.assertRaises(errors.NoRepositoryPresent,
369
                              branch.bzrdir.open_repository)
370
        finally:
371
            bzrdir.BzrDirFormat.set_default_format(old_format)
372
            
373
    def test_create_branch_convenience_under_shared_repo_no_tree_policy(self):
374
        # inside a repo the default convenience output is a branch+ follow the
375
        # repo tree policy
376
        old_format = bzrdir.BzrDirFormat.get_default_format()
377
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
378
        try:
379
            repo = self.make_repository('.', shared=True)
380
            repo.set_make_working_trees(False)
381
            branch = bzrdir.BzrDir.create_branch_convenience('child')
382
            self.assertRaises(errors.NoWorkingTree,
383
                              branch.bzrdir.open_workingtree)
384
            self.assertRaises(errors.NoRepositoryPresent,
385
                              branch.bzrdir.open_repository)
386
        finally:
387
            bzrdir.BzrDirFormat.set_default_format(old_format)
388
389
    def test_create_branch_convenience_under_shared_repo_no_tree_policy_force_tree(self):
390
        # inside a repo the default convenience output is a branch+ follow the
391
        # repo tree policy but we can override that
392
        old_format = bzrdir.BzrDirFormat.get_default_format()
393
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
394
        try:
395
            repo = self.make_repository('.', shared=True)
396
            repo.set_make_working_trees(False)
397
            branch = bzrdir.BzrDir.create_branch_convenience('child',
398
                force_new_tree=True)
399
            branch.bzrdir.open_workingtree()
400
            self.assertRaises(errors.NoRepositoryPresent,
401
                              branch.bzrdir.open_repository)
402
        finally:
403
            bzrdir.BzrDirFormat.set_default_format(old_format)
404
405
    def test_create_branch_convenience_under_shared_repo_force_new_repo(self):
406
        # inside a repo the default convenience output is overridable to give
407
        # repo+branch+tree
408
        old_format = bzrdir.BzrDirFormat.get_default_format()
409
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
410
        try:
411
            self.make_repository('.', shared=True)
412
            branch = bzrdir.BzrDir.create_branch_convenience('child',
413
                force_new_repo=True)
414
            branch.bzrdir.open_repository()
415
            branch.bzrdir.open_workingtree()
416
        finally:
417
            bzrdir.BzrDirFormat.set_default_format(old_format)
418
1534.4.39 by Robert Collins
Basic BzrDir support.
419
420
class ChrootedTests(TestCaseWithTransport):
421
    """A support class that provides readonly urls outside the local namespace.
422
423
    This is done by checking if self.transport_server is a MemoryServer. if it
424
    is then we are chrooted already, if it is not then an HttpServer is used
425
    for readonly urls.
426
    """
427
428
    def setUp(self):
429
        super(ChrootedTests, self).setUp()
430
        if not self.transport_server == MemoryServer:
431
            self.transport_readonly_server = HttpServer
432
433
    def test_open_containing(self):
434
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing,
435
                          self.get_readonly_url(''))
436
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing,
437
                          self.get_readonly_url('g/p/q'))
438
        control = bzrdir.BzrDir.create(self.get_url())
439
        branch, relpath = bzrdir.BzrDir.open_containing(self.get_readonly_url(''))
440
        self.assertEqual('', relpath)
441
        branch, relpath = bzrdir.BzrDir.open_containing(self.get_readonly_url('g/p/q'))
442
        self.assertEqual('g/p/q', relpath)
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
443
1534.6.11 by Robert Collins
Review feedback.
444
    def test_open_containing_from_transport(self):
445
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
1534.6.3 by Robert Collins
find_repository sufficiently robust.
446
                          get_transport(self.get_readonly_url('')))
1534.6.11 by Robert Collins
Review feedback.
447
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
1534.6.3 by Robert Collins
find_repository sufficiently robust.
448
                          get_transport(self.get_readonly_url('g/p/q')))
449
        control = bzrdir.BzrDir.create(self.get_url())
1534.6.11 by Robert Collins
Review feedback.
450
        branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
1534.6.3 by Robert Collins
find_repository sufficiently robust.
451
            get_transport(self.get_readonly_url('')))
452
        self.assertEqual('', relpath)
1534.6.11 by Robert Collins
Review feedback.
453
        branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
1534.6.3 by Robert Collins
find_repository sufficiently robust.
454
            get_transport(self.get_readonly_url('g/p/q')))
455
        self.assertEqual('g/p/q', relpath)
456
1910.11.5 by Andrew Bennetts
Add tests for BzrDir.open_from_transport.
457
    def test_open_from_transport(self):
458
        # transport pointing at bzrdir should give a bzrdir with root transport
459
        # set to the given transport
460
        control = bzrdir.BzrDir.create(self.get_url())
461
        transport = get_transport(self.get_url())
462
        opened_bzrdir = bzrdir.BzrDir.open_from_transport(transport)
463
        self.assertEqual(transport.base, opened_bzrdir.root_transport.base)
464
        self.assertIsInstance(opened_bzrdir, bzrdir.BzrDir)
465
        
466
    def test_open_from_transport_no_bzrdir(self):
467
        transport = get_transport(self.get_url())
468
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
469
                          transport)
470
471
    def test_open_from_transport_bzrdir_in_parent(self):
472
        control = bzrdir.BzrDir.create(self.get_url())
473
        transport = get_transport(self.get_url())
474
        transport.mkdir('subdir')
475
        transport = transport.clone('subdir')
476
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
477
                          transport)
478
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
479
480
class TestMeta1DirFormat(TestCaseWithTransport):
481
    """Tests specific to the meta1 dir format."""
482
483
    def test_right_base_dirs(self):
484
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
485
        t = dir.transport
486
        branch_base = t.clone('branch').base
487
        self.assertEqual(branch_base, dir.get_branch_transport(None).base)
488
        self.assertEqual(branch_base,
1508.1.25 by Robert Collins
Update per review comments.
489
                         dir.get_branch_transport(bzrlib.branch.BzrBranchFormat5()).base)
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
490
        repository_base = t.clone('repository').base
491
        self.assertEqual(repository_base, dir.get_repository_transport(None).base)
492
        self.assertEqual(repository_base,
493
                         dir.get_repository_transport(repository.RepositoryFormat7()).base)
494
        checkout_base = t.clone('checkout').base
495
        self.assertEqual(checkout_base, dir.get_workingtree_transport(None).base)
496
        self.assertEqual(checkout_base,
497
                         dir.get_workingtree_transport(workingtree.WorkingTreeFormat3()).base)
1534.5.3 by Robert Collins
Make format 4/5/6 branches share a single LockableFiles instance across wt/branch/repository.
498
1553.5.69 by Martin Pool
BzrDirFormat subclasses can now control what kind of overall lock is used.
499
    def test_meta1dir_uses_lockdir(self):
500
        """Meta1 format uses a LockDir to guard the whole directory, not a file."""
501
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
502
        t = dir.transport
503
        self.assertIsDirectory('branch-lock', t)
504
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
505
        
506
class TestFormat5(TestCaseWithTransport):
507
    """Tests specific to the version 5 bzrdir format."""
508
509
    def test_same_lockfiles_between_tree_repo_branch(self):
510
        # this checks that only a single lockfiles instance is created 
511
        # for format 5 objects
512
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
513
        def check_dir_components_use_same_lock(dir):
514
            ctrl_1 = dir.open_repository().control_files
515
            ctrl_2 = dir.open_branch().control_files
516
            ctrl_3 = dir.open_workingtree()._control_files
517
            self.assertTrue(ctrl_1 is ctrl_2)
518
            self.assertTrue(ctrl_2 is ctrl_3)
519
        check_dir_components_use_same_lock(dir)
520
        # and if we open it normally.
521
        dir = bzrdir.BzrDir.open(self.get_url())
522
        check_dir_components_use_same_lock(dir)
523
    
1534.5.16 by Robert Collins
Review feedback.
524
    def test_can_convert(self):
525
        # format 5 dirs are convertable
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
526
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
1534.5.16 by Robert Collins
Review feedback.
527
        self.assertTrue(dir.can_convert_format())
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
528
    
1534.5.16 by Robert Collins
Review feedback.
529
    def test_needs_conversion(self):
530
        # format 5 dirs need a conversion if they are not the default.
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
531
        # and they start of not the default.
532
        old_format = bzrdir.BzrDirFormat.get_default_format()
533
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirFormat5())
534
        try:
535
            dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
1534.5.16 by Robert Collins
Review feedback.
536
            self.assertFalse(dir.needs_format_conversion())
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
537
        finally:
538
            bzrdir.BzrDirFormat.set_default_format(old_format)
1534.5.16 by Robert Collins
Review feedback.
539
        self.assertTrue(dir.needs_format_conversion())
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
540
1534.5.3 by Robert Collins
Make format 4/5/6 branches share a single LockableFiles instance across wt/branch/repository.
541
542
class TestFormat6(TestCaseWithTransport):
543
    """Tests specific to the version 6 bzrdir format."""
544
545
    def test_same_lockfiles_between_tree_repo_branch(self):
546
        # this checks that only a single lockfiles instance is created 
547
        # for format 6 objects
548
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
549
        def check_dir_components_use_same_lock(dir):
550
            ctrl_1 = dir.open_repository().control_files
551
            ctrl_2 = dir.open_branch().control_files
552
            ctrl_3 = dir.open_workingtree()._control_files
553
            self.assertTrue(ctrl_1 is ctrl_2)
554
            self.assertTrue(ctrl_2 is ctrl_3)
555
        check_dir_components_use_same_lock(dir)
556
        # and if we open it normally.
557
        dir = bzrdir.BzrDir.open(self.get_url())
558
        check_dir_components_use_same_lock(dir)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
559
    
1534.5.16 by Robert Collins
Review feedback.
560
    def test_can_convert(self):
561
        # format 6 dirs are convertable
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
562
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
1534.5.16 by Robert Collins
Review feedback.
563
        self.assertTrue(dir.can_convert_format())
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
564
    
1534.5.16 by Robert Collins
Review feedback.
565
    def test_needs_conversion(self):
566
        # format 6 dirs need an conversion if they are not the default.
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
567
        old_format = bzrdir.BzrDirFormat.get_default_format()
568
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
569
        try:
570
            dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
1534.5.16 by Robert Collins
Review feedback.
571
            self.assertTrue(dir.needs_format_conversion())
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
572
        finally:
573
            bzrdir.BzrDirFormat.set_default_format(old_format)
1563.1.6 by Robert Collins
Add tests for sftp push, and NonLocalTets for BzrDir.create_branch_convenience, before fixing the failure of it to work on non-local urls.
574
575
1733.1.1 by Robert Collins
Support non '.bzr' control directories in bzrdir.
576
class NotBzrDir(bzrlib.bzrdir.BzrDir):
577
    """A non .bzr based control directory."""
578
579
    def __init__(self, transport, format):
580
        self._format = format
581
        self.root_transport = transport
582
        self.transport = transport.clone('.not')
583
584
585
class NotBzrDirFormat(bzrlib.bzrdir.BzrDirFormat):
586
    """A test class representing any non-.bzr based disk format."""
587
588
    def initialize_on_transport(self, transport):
589
        """Initialize a new .not dir in the base directory of a Transport."""
590
        transport.mkdir('.not')
591
        return self.open(transport)
592
593
    def open(self, transport):
594
        """Open this directory."""
595
        return NotBzrDir(transport, self)
596
597
    @classmethod
1733.1.3 by Robert Collins
Extend the test suite to run bzrdir conformance tests on non .bzr based control dirs.
598
    def _known_formats(self):
599
        return set([NotBzrDirFormat()])
600
601
    @classmethod
1733.1.1 by Robert Collins
Support non '.bzr' control directories in bzrdir.
602
    def probe_transport(self, transport):
603
        """Our format is present if the transport ends in '.not/'."""
1733.1.2 by Robert Collins
bugfix test for non .bzrdir support.
604
        if transport.has('.not'):
1733.1.1 by Robert Collins
Support non '.bzr' control directories in bzrdir.
605
            return NotBzrDirFormat()
606
607
608
class TestNotBzrDir(TestCaseWithTransport):
609
    """Tests for using the bzrdir api with a non .bzr based disk format.
610
    
611
    If/when one of these is in the core, we can let the implementation tests
612
    verify this works.
613
    """
614
615
    def test_create_and_find_format(self):
616
        # create a .notbzr dir 
617
        format = NotBzrDirFormat()
618
        dir = format.initialize(self.get_url())
619
        self.assertIsInstance(dir, NotBzrDir)
620
        # now probe for it.
621
        bzrlib.bzrdir.BzrDirFormat.register_control_format(format)
622
        try:
623
            found = bzrlib.bzrdir.BzrDirFormat.find_format(
624
                get_transport(self.get_url()))
1733.1.2 by Robert Collins
bugfix test for non .bzrdir support.
625
            self.assertIsInstance(found, NotBzrDirFormat)
1733.1.1 by Robert Collins
Support non '.bzr' control directories in bzrdir.
626
        finally:
627
            bzrlib.bzrdir.BzrDirFormat.unregister_control_format(format)
628
1733.1.3 by Robert Collins
Extend the test suite to run bzrdir conformance tests on non .bzr based control dirs.
629
    def test_included_in_known_formats(self):
630
        bzrlib.bzrdir.BzrDirFormat.register_control_format(NotBzrDirFormat)
631
        try:
632
            formats = bzrlib.bzrdir.BzrDirFormat.known_formats()
633
            for format in formats:
634
                if isinstance(format, NotBzrDirFormat):
635
                    return
636
            self.fail("No NotBzrDirFormat in %s" % formats)
637
        finally:
638
            bzrlib.bzrdir.BzrDirFormat.unregister_control_format(NotBzrDirFormat)
639
1733.1.1 by Robert Collins
Support non '.bzr' control directories in bzrdir.
640
1563.1.6 by Robert Collins
Add tests for sftp push, and NonLocalTets for BzrDir.create_branch_convenience, before fixing the failure of it to work on non-local urls.
641
class NonLocalTests(TestCaseWithTransport):
642
    """Tests for bzrdir static behaviour on non local paths."""
643
644
    def setUp(self):
645
        super(NonLocalTests, self).setUp()
646
        self.transport_server = MemoryServer
647
    
648
    def test_create_branch_convenience(self):
649
        # outside a repo the default convenience output is a repo+branch_tree
650
        old_format = bzrdir.BzrDirFormat.get_default_format()
651
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
652
        try:
653
            branch = bzrdir.BzrDir.create_branch_convenience(self.get_url('foo'))
654
            self.assertRaises(errors.NoWorkingTree,
655
                              branch.bzrdir.open_workingtree)
656
            branch.bzrdir.open_repository()
657
        finally:
658
            bzrdir.BzrDirFormat.set_default_format(old_format)
659
660
    def test_create_branch_convenience_force_tree_not_local_fails(self):
661
        # outside a repo the default convenience output is a repo+branch_tree
662
        old_format = bzrdir.BzrDirFormat.get_default_format()
663
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
664
        try:
665
            self.assertRaises(errors.NotLocalUrl,
666
                bzrdir.BzrDir.create_branch_convenience,
667
                self.get_url('foo'),
668
                force_new_tree=True)
669
            t = get_transport(self.get_url('.'))
670
            self.assertFalse(t.has('foo'))
671
        finally:
672
            bzrdir.BzrDirFormat.set_default_format(old_format)
673
1563.2.38 by Robert Collins
make push preserve tree formats.
674
    def test_clone(self):
675
        # clone into a nonlocal path works
676
        old_format = bzrdir.BzrDirFormat.get_default_format()
677
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
678
        try:
679
            branch = bzrdir.BzrDir.create_branch_convenience('local')
680
        finally:
681
            bzrdir.BzrDirFormat.set_default_format(old_format)
682
        branch.bzrdir.open_workingtree()
683
        result = branch.bzrdir.clone(self.get_url('remote'))
684
        self.assertRaises(errors.NoWorkingTree,
685
                          result.open_workingtree)
686
        result.open_branch()
687
        result.open_repository()
688