~bzr-pqm/bzr/bzr.dev

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